Skip to main content

Crate modmath

Crate modmath 

Source
Expand description

Modular math implemented with traits.

Provides modular arithmetic against any type implementing a minimal set of core::ops:: and const_num_traits:: traits — primitive integers or any bigint backend.

Schoolbook surface lives in three bound-flavor modules:

  • basic — requires Copy. Operands by value.
  • constrained — requires Clone instead. Mixed value / reference operands.
  • strict — reference-based throughout. Overflowing instead of Wrapping arithmetic.

Montgomery surface: Field / FieldCt / FieldNct are the high-level type with precomputed parameters and lifetime-branded Residue values. Each flavor’s montgomery submodule has the free-function building blocks (compute_params, to_mont, mul, mod_mul, …); <flavor>::montgomery::wide::* exposes the wide-REDC primitives (redc, mul, plus ct::* siblings).

Constant-time variants — branchless finalize via subtle — sit alongside their non-CT siblings (FieldCt, Field<T, Ct>, _ct function suffixes, <flavor>::montgomery::wide::ct::*).

Backend-agnostic: any integer type implementing the required traits works — built-in integers or third-party bigints. The basic flavor’s Copy bound rules out heap-allocated backends; those use constrained or strict.

Re-exports§

pub use montgomery::NPrimeMethod;
pub use montgomery::type_bit_width;
pub use montgomery::compute_n_prime_newton;
pub use montgomery::compute_r_mod_n;
pub use montgomery::compute_r_mod_n_ct;
pub use montgomery::compute_r2_mod_n;
pub use montgomery::compute_r2_mod_n_ct;
pub use montgomery::CiosMontMul;
pub use montgomery::CiosMontMulCt;

Modules§

basic
Schoolbook modular arithmetic for Copy-bound types.
constrained
Schoolbook modular arithmetic for Clone-bound types with mixed by-value and by-reference parameters.
montgomery
Flavor-neutral Montgomery arithmetic primitives.
strict
Schoolbook modular arithmetic with reference-based parameters throughout — Clone, never Copy, so heap-allocated bigints qualify.

Structs§

Field
Montgomery context over modulus T, with algorithm choice driven by the personality marker P (defaults to Nct = variable-time fast path).
Odd
Proof that a value is odd.
Residue
A value in Field<T, P>, stored implicitly in Montgomery form.

Traits§

DivNonZero
Infallible division / remainder by a proven-non-zero divisor.
HasNonZero
Bridge to core::num::NonZero — “the non-zero form of Self”.
MontStorage
Bound on the value stored in a Residue. With the zeroize feature this requires T: Zeroize; otherwise it’s vacuous.
NonCt
Sealed marker: the type is not the Ct personality and may flow through modmath’s variable-time algorithms.
Parity
Checks whether a value is odd or even.
WideMul
Widening multiplication producing a double-width (lo, hi) result.

Type Aliases§

FieldCt
Alias for the Ct variant of Field. Equivalent to Field<T, Ct>. Reads naturally at construction sites and sidesteps the type-inference ambiguity that bare Field::new(modulus) hits — FieldCt::new(modulus) resolves unambiguously because the alias fixes P = Ct at the type level. Symmetric with FieldNct.
FieldNct
Alias for the Nct variant of Field. Equivalent to Field<T, Nct> (matches the default personality). Provided for symmetry with FieldCt and to side-step the construction-site type-ambiguity pitfall — FieldNct::new(modulus) resolves unambiguously without the type-annotation/turbofish friction of Field::new(modulus).
ResidueCt
Alias for the Ct variant of Residue. Equivalent to Residue<'f, T, Ct>. Symmetric with ResidueNct.
ResidueNct
Alias for the Nct variant of Residue. Equivalent to Residue<'f, T, Nct>. Symmetric with ResidueCt.