clock-bigint
Deterministic constant-time big integers for blockchain consensus engines.
Overview
clock-bigint is a production-ready big integer crate that implements the complete ClockinChain Big Integer specifications:
- Representation Specification v1.0 - Canonical encoding and memory layout
- Arithmetic Algorithm Specification v1.0 - Deterministic arithmetic operations
- Montgomery & Modular Exponentiation Specification v1.0 - Cryptographic arithmetic
- Gas Schedule Specification v1.0 - Precomputable gas metering
Features
- ✅ No_std compatible: Works in embedded and WASM environments (default)
- ✅ Deterministic: Bit-for-bit identical results across all platforms
- ✅ Constant-time: All operations execute in constant time to prevent timing attacks
- ✅ Canonical encoding: Unique representation for each integer value
- ✅ Gas metering: Precomputable gas costs for VM execution
- ✅ Montgomery arithmetic: Full support for cryptographic operations
- ✅ Time-sliced operations: Pausable long operations for async VM execution
- ✅ ZK support: Constraint-emitting backend for zero-knowledge proofs (feature-gated)
Quick Start
use ;
// Fixed-length BigInt (256-bit) - works in no_std
let a = U256from_u64;
let b = U256from_u64;
// Dynamic-length BigInt - requires alloc feature
let mut x = from_u64; // max 512 limbs
let y = from_u64;
// Arithmetic operations
let sum = add.unwrap;
let product = mul.unwrap;
Montgomery Arithmetic
use ;
// Create Montgomery context for modulus - requires alloc feature
let modulus = from_u64; // Must be odd
let ctx = new.unwrap;
// Convert to Montgomery form
let a = from_u64;
let a_mont = to_mont.unwrap;
// Montgomery multiplication
let b = from_u64;
let b_mont = to_mont.unwrap;
let c_mont = mont_mul.unwrap;
// Convert back
let c = from_mont.unwrap;
// c = (5 * 7) mod 97 = 35
// Modular exponentiation
let exponent = from_u64;
let result = mod_pow.unwrap;
// result = 5^3 mod 97 = 125 mod 97 = 28
Canonical Encoding
use ;
let value = from_u64;
// Encode to canonical binary format - requires alloc feature
let encoded = encode;
// Format: [sign: u8][limb_count: u32 LE][limbs: u64[] LE]
// Decode with validation
let decoded = decode.unwrap;
assert_eq!;
Gas Metering
use gas;
// Calculate gas cost for operations
let n = 4; // 256-bit (4 limbs)
let add_cost = cost_add;
let mul_cost = cost_mul;
let modexp_cost = cost_modexp; // 256-bit exponent
// All costs are deterministic and precomputable
Type Aliases
use ;
// Fixed-width types for common sizes
let x: U256 = U256from_u64;
let y: U512 = U512from_u64;
Features
alloc- Dynamic BigInt support with heap allocation (default)zk- Enable ZK constraint backendserde- Serialization supportstd- Alias foralloc(compatibility)bench- Benchmarking infrastructurefuzz- Fuzzing targets
Feature Overview
| Feature | Fixed-size BigInts | Dynamic BigInts | Montgomery | Encoding | Timeboxed |
|---|---|---|---|---|---|
| default | ✅ | ✅ | ✅ | ✅ | ✅ |
| no-alloc | ✅ | ❌ | ❌ | ❌ | ❌ |
Default (no_std + alloc): Full functionality with optional heap allocation No-alloc: Only fixed-size operations for constrained environments
Specifications
This crate implements the following normative specifications:
-
ClockinChain Big Integer Representation Specification v1.0
- Fixed 64-bit limbs in little-endian order
- Canonical form enforcement
- Zero representation rules
-
Arithmetic Algorithm Specification v1.0
- Constant-time addition (adc algorithm)
- Constant-time subtraction (sbb algorithm)
- Comba/Karatsuba multiplication
- Knuth D division
-
Montgomery & Modular Exponentiation Specification v1.0
- Montgomery reduction
- Montgomery multiplication
- Montgomery ladder exponentiation
-
Gas Schedule Specification v1.0
- Deterministic cost functions
- Precomputable gas metering
- DoS protection (MAX_LIMBS = 512)
No-Std Usage
For embedded systems, WASM, or other constrained environments:
// In Cargo.toml
clock-bigint =
// In your code
use ;
// Only fixed-size BigInts available (no heap allocation)
let a: U256 = U256from_u64;
let b: U256 = U256from_u64;
// Arithmetic through BigIntCore trait
assert_eq!;
assert_eq!;
To enable dynamic BigInts and cryptographic operations:
[]
= { = "1.0", = ["alloc"] }
Performance Targets
- 256-bit add: ~8 cycles
- 256-bit mul: ~40 cycles
- 2048-bit modexp: within 10% of GMP
- Deterministic across platforms: Guaranteed
Safety
This crate is designed for cryptographic and consensus-critical applications:
- No_std compatible: Works in embedded and WASM environments
- Constant-time: No secret-dependent branches or memory accesses
- Deterministic: Identical results across all platforms
- Canonical: Unique encoding prevents consensus malleability
- Audited: Designed for security review
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.