clock_curve_math/lib.rs
1//! # clock-curve-math
2//!
3//! High-performance, constant-time, cryptography-grade number theory library
4//! for the `ClockCurve` ecosystem.
5//!
6//! ## Overview
7//!
8//! This crate provides:
9//! - Big integer arithmetic with constant-time operations
10//! - Montgomery arithmetic for efficient modular operations
11//! - `FieldElement` modulo p = 2^255 - 19
12//! - Scalar modulo l = 2^252 + 27_742_317_777_372_353_535_851_937_790_883_648_493
13//! - Constant-time helpers for secure computations
14//!
15//! ## Features
16//!
17//! ### Backend Selection (choose one)
18//! - **`bigint-backend`** (default): Use `clock-bigint` for high-performance BigInt operations
19//! - **`custom-limbs`**: Pure Rust limb array implementation (no_std compatible)
20//!
21//! ### Optional Features
22//! - **`rand`**: Random FieldElement/Scalar generation via `clock-rand`
23//! - **`serde`**: Serialization/deserialization support
24//!
25//! ### Memory Management
26//! - **`alloc`** (default): Enable heap allocations for advanced operations
27//! - **`std`**: Enable full standard library support
28//!
29//! ### Feature Combinations
30//! | Environment | Features | Description |
31//! |-------------|----------|-------------|
32//! | Full std | default | All features, maximum functionality |
33//! | Embedded no_std | `custom-limbs` | Minimal footprint, no heap allocation |
34//! | Embedded with alloc | `alloc,custom-limbs` | Heap allocation, advanced operations |
35//! | Server with custom backend | `std,custom-limbs` | Full std, custom BigInt implementation |
36//!
37//! ## Error Handling
38//!
39//! The library provides comprehensive error handling through the [`MathError`] enum.
40//! Most operations that can fail return `Result<T, MathError>`. The library also
41//! provides validation functions for checking inputs before operations.
42//!
43//! ```rust
44//! use clock_curve_math::{FieldElement, MathError, validation};
45//!
46//! // Validate bytes before creating field element
47//! let bytes = [42u8; 32];
48//! validation::validate_field_bytes(&bytes).unwrap();
49//!
50//! // Create field element (will validate internally)
51//! let element = FieldElement::from_bytes(&bytes).unwrap();
52//! ```
53//!
54//! ## Example
55//!
56//! ```rust
57//! use clock_curve_math::{FieldElement, Scalar, FieldOps, ScalarOps};
58//!
59//! // Create field elements
60//! let a = FieldElement::from_u64(10);
61//! let b = FieldElement::from_u64(20);
62//!
63//! // Perform operations
64//! let sum = a.add(&b);
65//! let product = a.mul(&b);
66//!
67//! // Create scalars
68//! let s1 = Scalar::from_u64(5);
69//! let s2 = Scalar::from_u64(7);
70//! let scalar_product = s1.mul(&s2);
71//!
72//! // Advanced operations (require alloc feature)
73//! # #[cfg(feature = "alloc")]
74//! let elements = vec![a, b];
75//! # #[cfg(feature = "alloc")]
76//! let inverses = clock_curve_math::field::batch_inverse(&elements).unwrap();
77//! ```
78//!
79//! ## Constant-Time Guarantees
80//!
81//! All operations in this crate execute in constant time to prevent timing attacks.
82//! This includes:
83//! - All arithmetic operations
84//! - All comparisons
85//! - All conditional operations
86//!
87//! ## No-Std Support
88//!
89//! This crate supports `no_std` environments with flexible feature configurations:
90//!
91//! ### Minimal no_std Configuration
92//! For the most constrained environments (embedded systems, kernels):
93//!
94//! ```toml
95//! [dependencies]
96//! clock-curve-math = { version = "0.6", default-features = false, features = ["custom-limbs"] }
97//! ```
98//!
99//! ```rust,no_run
100//! use clock_curve_math::{FieldElement, Scalar, FieldOps, ScalarOps};
101//!
102//! // Basic operations work without heap allocation
103//! let a = FieldElement::from_u64(42);
104//! let b = FieldElement::from_u64(24);
105//! let sum = a.add(&b);
106//! let product = a.mul(&b);
107//! ```
108//!
109//! ### With Heap Allocation
110//! For environments with allocators but no full std:
111//!
112//! ```toml
113//! [dependencies]
114//! clock-curve-math = { version = "0.6", default-features = false, features = ["alloc", "custom-limbs"] }
115//! ```
116//!
117//! ```rust,no_run
118//! use clock_curve_math::{FieldElement, field};
119//!
120//! // Advanced operations requiring allocation
121//! let elements = vec![FieldElement::from_u64(1), FieldElement::from_u64(2)];
122//! let inverses = field::batch_inverse(&elements).unwrap();
123//! ```
124//!
125//! ### Backend Options
126//! - **`custom-limbs`**: Pure no_std, no external dependencies
127//! - **`bigint-backend`**: Uses `clock-bigint` (requires `alloc`)
128//!
129//! ### Cross-Platform Compatibility
130//! Tested architectures: x86_64, aarch64, riscv64gc, armv7, powerpc64, thumbv7em, thumbv8m
131
132#![cfg_attr(not(feature = "std"), no_std)]
133#![warn(missing_docs)]
134#![warn(clippy::all)]
135
136// Core modules
137pub mod api_extensions;
138pub mod bigint;
139pub mod constants;
140pub mod ct;
141pub mod error;
142pub mod extensible;
143pub mod field;
144pub mod montgomery;
145pub mod scalar;
146pub mod validation;
147
148// SIMD preparation module (future optimization)
149#[cfg(feature = "simd")]
150pub mod simd;
151
152// Re-exports
153pub use bigint::{BigInt, BigIntOps, ONE, ZERO};
154pub use constants::{L_LIMBS, P_LIMBS};
155pub use error::MathError;
156pub use field::{ExtendedPoint, FieldElement, FieldOps};
157pub use montgomery::{
158 MontgomeryOps, from_montgomery, from_montgomery_l, from_montgomery_p, to_montgomery,
159 to_montgomery_l, to_montgomery_p,
160};
161pub use scalar::{Scalar, ScalarOps};
162pub use validation::{
163 validate_buffer_size, validate_exponent, validate_field_bigint, validate_field_bytes,
164 validate_modulus, validate_non_zero, validate_scalar_bigint, validate_scalar_bytes,
165};
166
167// Serde re-exports (when serde feature is enabled)
168#[cfg(feature = "serde")]
169pub use {serde::Deserialize, serde::Serialize};
170
171// Constant-time helpers
172pub use ct::{
173 ct_eq, ct_gt, ct_is_zero, ct_lt, ct_mask, ct_neq, ct_select, ct_select_array, ct_swap,
174};