Crate modmath

Source
Expand description

Modular math implemented with traits.

This crate provides modular arithmetic implemented not for any particular type, but for any type that implements minimal set of core::ops:: and num_traits:: traits.

All provided functions are simply free functions.

There are three verions of each: basic that has least amount of constraints, but requires Copy to be implemented for the type. constrained requires Clone. strict requires neither, but has most other constaints to be able to operate with references and Overflowing arithmetic. Tested with builtin integers and num-bigint, crypto-bigint, bnum, ibig and fixed-bigint crates. basic versions of functions wont work with num-bigint and ibig as both require heap allocation.

Enums§

NPrimeMethod
Methods for computing N’ in Montgomery parameter computation

Functions§

basic_compute_montgomery_params
Montgomery parameter computation (Basic) with default method Computes R, R^(-1) mod N, N’, and R bit length for Montgomery arithmetic using trial search Returns None if parameter computation fails
basic_compute_montgomery_params_with_method
Montgomery parameter computation (Basic) Computes R, R^(-1) mod N, N’, and R bit length for Montgomery arithmetic Returns None if parameter computation fails
basic_from_montgomery
Convert from Montgomery form (Basic): (a * R) -> a mod N Uses Montgomery reduction algorithm
basic_mod_add
Modular Addition (Basic)
basic_mod_exp
Modular Exponentiation (Basic)
basic_mod_inv
Modular Inverse (Basic)
basic_mod_mul
Modular Multiplication (Basic)
basic_mod_sub
Modular Subtraction (Basic)
basic_montgomery_mod_exp
Montgomery-based modular exponentiation (Basic): base^exponent mod modulus Uses Montgomery arithmetic for efficient repeated multiplication Returns None if Montgomery parameter computation fails
basic_montgomery_mod_mul
Complete Montgomery modular multiplication (Basic): A * B mod N Returns None if Montgomery parameter computation fails
basic_montgomery_mul
Montgomery multiplication (Basic): (a * R) * (b * R) -> (a * b * R) mod N
basic_to_montgomery
Convert to Montgomery form (Basic): a -> (a * R) mod N
constrained_compute_montgomery_params
Montgomery parameter computation (Constrained) with default method Computes R, R^(-1) mod N, N’, and R bit length for Montgomery arithmetic Returns None if parameter computation fails
constrained_compute_montgomery_params_with_method
Montgomery parameter computation (Constrained) Computes R, R^(-1) mod N, N’, and R bit length for Montgomery arithmetic Returns None if N’ computation fails or R^(-1) mod N cannot be found
constrained_from_montgomery
Convert from Montgomery form (Constrained): (a * R) -> a mod N Uses Montgomery reduction algorithm
constrained_mod_add
Modular Addition (Constrained)
constrained_mod_exp
Modular Exponentiation (Constrained)
constrained_mod_inv
Modular Inverse (Constrained)
constrained_mod_mul
Modular Multiplication (Constrained)
constrained_mod_sub
Modular Subtraction (Constrained)
constrained_montgomery_mod_exp
Montgomery-based modular exponentiation (Constrained): base^exponent mod modulus Uses Montgomery arithmetic for efficient repeated multiplication Returns None if Montgomery parameter computation fails
constrained_montgomery_mod_mul
Complete Montgomery modular multiplication (Constrained): A * B mod N Returns None if Montgomery parameter computation fails
constrained_montgomery_mul
Montgomery multiplication (Constrained): (a * R) * (b * R) -> (a * b * R) mod N
constrained_to_montgomery
Convert to Montgomery form (Constrained): a -> (a * R) mod N
strict_compute_montgomery_params
Montgomery parameter computation (Strict) Computes R, R^(-1) mod N, N’, and R bit length for Montgomery arithmetic Uses reference-based operations to minimize copying of large integers
strict_compute_montgomery_params_with_method
Montgomery parameter computation with method selection (Strict) Computes R, R^(-1) mod N, N’, and R bit length for Montgomery arithmetic using specified method Uses reference-based operations to minimize copying of large integers Returns None if parameter computation fails
strict_from_montgomery
Convert from Montgomery form (Strict): (a * R) -> a mod N Uses Montgomery reduction algorithm with reference-based operations to minimize copying of large integers
strict_mod_add
Modular Addition (Strict)
strict_mod_exp
Modular Exponentiation (Strict)
strict_mod_inv
Modular Inverse (Strict)
strict_mod_mul
Modular Multiplication (Strict)
strict_mod_sub
Modular Subtraction (Strict)
strict_montgomery_mod_exp
Montgomery-based modular exponentiation (Strict): base^exponent mod modulus Uses Montgomery arithmetic for efficient repeated multiplication with reference-based operations to minimize copying of large integers Returns None if Montgomery parameter computation fails
strict_montgomery_mod_mul
Complete Montgomery modular multiplication (Strict): A * B mod N Uses reference-based operations throughout to minimize copying of large integers Returns None if Montgomery parameter computation fails
strict_to_montgomery
Convert to Montgomery form (Strict): a -> (a * R) mod N Uses reference-based operations to minimize copying of large integers