clock-curve-math 0.6.0

High-performance, constant-time, cryptography-grade number theory library for ClockCurve ecosystem
Documentation
# ClockIn Ecosystem Integration Guide

## Overview

`clock-curve-math` is designed as a core component of the ClockIn cryptography ecosystem. This document outlines how `clock-curve-math` integrates with other ClockIn components and provides guidance for ecosystem compatibility.

## Ecosystem Components

### Core Dependencies

#### clock-bigint

- **Purpose**: High-performance arbitrary-precision arithmetic
- **Integration**: Default backend for BigInt operations
- **Feature Flag**: `bigint-backend` (enabled by default)
- **API Contract**: Implements `BigIntOps` trait

#### clock-rand

- **Purpose**: Cryptographically secure random number generation
- **Integration**: Random field element and scalar generation
- **Feature Flag**: `rand`
- **API Contract**: Uses `Rng` trait from clock-rand

### Optional Dependencies

#### serde

- **Purpose**: Serialization/deserialization support
- **Integration**: JSON/binary serialization for FieldElement and Scalar
- **Feature Flag**: `serde`
- **API Contract**: Standard serde `Serialize`/`Deserialize` traits

## Integration Patterns

### Backend Selection

`clock-curve-math` supports multiple BigInt backends:

```toml
# High-performance backend (default)
[dependencies]
clock-curve-math = "0.6"

# Minimal no_std backend
[dependencies]
clock-curve-math = { version = "0.6", default-features = false, features = ["custom-limbs"] }

# Mixed ecosystem
[dependencies]
clock-curve-math = { version = "0.6", features = ["rand", "serde"] }
```

### Feature Compatibility Matrix

| Component | bigint-backend | custom-limbs | rand | serde | alloc | std |
|-----------|----------------|--------------|------|-------|-------|-----|
| FieldElement |||||||
| Scalar |||||||
| BigInt ops |||||||
| Batch operations |||||||
| Random generation |||||||
| Serialization |||||||

## API Contracts

### BigInt Backend Contract

All BigInt backends must implement:

```rust
pub trait BigIntOps {
    fn add(&self, rhs: &Self) -> Self;
    fn sub(&self, rhs: &Self) -> Self;
    fn mul(&self, rhs: &Self) -> Self;
    fn div(&self, rhs: &Self) -> Self;
    fn cmp(&self, rhs: &Self) -> Ordering;
    fn is_zero(&self) -> bool;
    fn from_u64(value: u64) -> Self;
    fn from_bytes(bytes: &[u8; 32]) -> Self;
    fn to_bytes(&self) -> [u8; 32];
}
```

### Random Generation Contract

Random generation uses:

```rust
pub trait Rng {
    fn next_u64(&mut self) -> u64;
    fn fill_bytes(&mut self, dest: &mut [u8]);
}
```

### Serialization Contract

Serialization implements standard serde traits:

```rust
#[derive(Serialize, Deserialize)]
pub struct FieldElement { /* ... */ }

#[derive(Serialize, Deserialize)]
pub struct Scalar { /* ... */ }
```

## Ecosystem Compatibility Testing

### Integration Test Suite

The comprehensive integration test suite validates:

1. **Backend Compatibility**: Operations work across all backends
2. **Feature Flag Compatibility**: APIs work with all feature combinations
3. **External Dependencies**: Integration with clock-bigint, clock-rand, serde
4. **Cross-Platform**: Compatibility across target architectures
5. **Memory Management**: no_std, alloc, std configurations

### Running Integration Tests

```bash
# Test all backends
cargo test --test ecosystem_integration

# Test specific feature combinations
cargo test --features bigint-backend,serde
cargo test --no-default-features --features custom-limbs

# Test cross-platform compatibility
cargo test --target x86_64-unknown-linux-gnu
cargo test --target aarch64-unknown-linux-gnu
```

## Migration Guide

### From 0.5.x to 0.6.x

No breaking changes for existing users. All APIs remain backward compatible.

### Adding New Dependencies

#### Adding Random Generation

```rust
// In Cargo.toml
[dependencies]
clock-curve-math = { version = "0.6", features = ["rand"] }
clock-rand = "1.0"

// In code
use clock_rand::Xoshiro256Plus;
use clock_curve_math::{FieldElement, Scalar};

let mut rng = Xoshiro256Plus::new(42);
let fe = FieldElement::random(&mut rng);
let scalar = Scalar::random(&mut rng);
```

#### Adding Serialization

```rust
// In Cargo.toml
[dependencies]
clock-curve-math = { version = "0.6", features = ["serde"] }

// In code
use clock_curve_math::{FieldElement, Scalar};

let fe = FieldElement::from_u64(42);
let json = serde_json::to_string(&fe).unwrap();
let deserialized: FieldElement = serde_json::from_str(&json).unwrap();
```

## Performance Considerations

### Backend Performance

- **`bigint-backend`**: Highest performance, requires heap allocation
- **`custom-limbs`**: Good performance, no_std compatible, no heap allocation
- **Montgomery Arithmetic**: Consistent performance across backends

### Memory Management

```rust
// no_std + custom-limbs (minimal footprint)
clock-curve-math = { version = "0.6", default-features = false, features = ["custom-limbs"] }

// With heap allocation
clock-curve-math = { version = "0.6", default-features = false, features = ["custom-limbs", "alloc"] }

// Full standard library
clock-curve-math = "0.6" // default features
```

## Security Considerations

### Constant-Time Guarantees

All operations maintain constant-time execution regardless of:

- Input values
- Backend selection
- Feature flags enabled

### Random Generation

When using the `rand` feature:

- Use cryptographically secure RNGs
- Seed properly for security
- Consider side-channel attacks in RNG implementation

## Troubleshooting

### Common Issues

#### Backend Selection Issues

```rust
// Ensure correct backend is selected
#[cfg(feature = "bigint-backend")]
use clock_curve_math::bigint::BigInt; // Uses clock-bigint

#[cfg(feature = "custom-limbs")]
use clock_curve_math::bigint::BigInt; // Uses custom implementation
```

#### Feature Flag Conflicts

```toml
# This works
clock-curve-math = { version = "0.6", features = ["rand", "serde"] }

# This conflicts (bigint-backend and custom-limbs are mutually exclusive)
clock-curve-math = { version = "0.6", features = ["bigint-backend", "custom-limbs"] }
```

#### Memory Allocation Issues

```rust
// For no_std environments
#[cfg(not(feature = "alloc"))]
{
    // Basic operations only
    let fe = FieldElement::from_u64(42);
    let result = fe.add(&FieldElement::from_u64(24));
}

#[cfg(feature = "alloc")]
{
    // Advanced operations available
    let elements = vec![FieldElement::from_u64(1), FieldElement::from_u64(2)];
    let inverses = field::batch_inverse(&elements).unwrap();
}
```

## Contributing to Ecosystem

### Adding New Backends

1. Implement `BigIntOps` trait
2. Add feature flag
3. Update CI testing
4. Document performance characteristics

### Adding New Features

1. Follow existing API patterns
2. Maintain constant-time guarantees
3. Add comprehensive tests
4. Update documentation

## Version Compatibility

### Semantic Versioning

`clock-curve-math` follows semantic versioning:

- **Major**: Breaking changes
- **Minor**: New features, backward compatible
- **Patch**: Bug fixes, no API changes

### Ecosystem Version Compatibility

| clock-curve-math | clock-bigint | clock-rand | serde |
|------------------|--------------|------------|-------|
| 0.6.x | 1.0.0 | 1.0.2 | 1.0.228 |
| 0.5.x | 1.0.0 | 1.0.2 | 1.0.228 |
| 0.4.x | 0.1.x | 1.0.2 | 1.0.228 |

## Support

For ecosystem integration issues:

1. Check this documentation
2. Review integration tests
3. File issues with reproduction cases
4. Include your Cargo.toml configuration

---

*This guide ensures smooth integration within the ClockCurve ecosystem while maintaining flexibility for different use cases.*