Crate clock_curve_math

Crate clock_curve_math 

Source
Expand description

§clock-curve-math

High-performance, constant-time, cryptography-grade number theory library for the ClockCurve ecosystem.

§Overview

This crate provides:

  • Big integer arithmetic with constant-time operations
  • Montgomery arithmetic for efficient modular operations
  • FieldElement modulo p = 2^255 - 19
  • Scalar modulo l = 2^252 + 27742317777372353535851937790883648493
  • Constant-time helpers for secure computations

§Features

  • bigint-backend (default): Use clock-bigint as BigInt backend
  • custom-limbs: Use custom limb array implementation
  • rand: Enable random FieldElement/Scalar generation via clock-rand
  • serde: Enable serialization/deserialization
  • alloc: Enable heap allocations

§Error Handling

The library provides comprehensive error handling through the MathError enum. Most operations that can fail return Result<T, MathError>. The library also provides validation functions for checking inputs before operations.

use clock_curve_math::{FieldElement, MathError, validation};

// Validate bytes before creating field element
let bytes = [42u8; 32];
validation::validate_field_bytes(&bytes).unwrap();

// Create field element (will validate internally)
let element = FieldElement::from_bytes(&bytes).unwrap();

§Example

use clock_curve_math::{FieldElement, Scalar};

// Create field elements
let a = FieldElement::from_u64(10);
let b = FieldElement::from_u64(20);

// Perform operations
let sum = a.add(&b);
let product = a.mul(&b);

// Create scalars
let s1 = Scalar::from_u64(5);
let s2 = Scalar::from_u64(7);
let product = s1.mul(&s2);

§Constant-Time Guarantees

All operations in this crate execute in constant time to prevent timing attacks. This includes:

  • All arithmetic operations
  • All comparisons
  • All conditional operations

§No-Std Support

This crate supports no_std environments. The alloc feature is required for some operations when using the bigint-backend feature.

Re-exports§

pub use bigint::BigInt;
pub use bigint::BigIntOps;
pub use bigint::ONE;
pub use bigint::ZERO;
pub use constants::L_LIMBS;
pub use constants::P_LIMBS;
pub use error::MathError;
pub use field::FieldElement;
pub use field::FieldOps;
pub use montgomery::MontgomeryOps;
pub use montgomery::from_montgomery;
pub use montgomery::from_montgomery_l;
pub use montgomery::from_montgomery_p;
pub use montgomery::to_montgomery;
pub use montgomery::to_montgomery_l;
pub use montgomery::to_montgomery_p;
pub use scalar::Scalar;
pub use scalar::ScalarOps;
pub use validation::validate_buffer_size;
pub use validation::validate_exponent;
pub use validation::validate_field_bigint;
pub use validation::validate_field_bytes;
pub use validation::validate_modulus;
pub use validation::validate_non_zero;
pub use validation::validate_scalar_bigint;
pub use validation::validate_scalar_bytes;
pub use ct::ct_eq;
pub use ct::ct_gt;
pub use ct::ct_is_zero;
pub use ct::ct_lt;
pub use ct::ct_mask;
pub use ct::ct_neq;
pub use ct::ct_select;
pub use ct::ct_select_array;
pub use ct::ct_swap;

Modules§

bigint
Big integer arithmetic with constant-time operations.
constants
Mathematical constants for ClockCurve.
ct
Constant-time operations for secure cryptographic computations.
error
Error types for clock-curve-math.
field
Field arithmetic modulo p = 2^255 - 19.
montgomery
Montgomery arithmetic for efficient modular operations.
scalar
Scalar arithmetic modulo l = 2^252 + 27742317777372353535851937790883648493.
validation
Input validation utilities for clock-curve-math.