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— requiresCopy. Operands by value.constrained— requiresCloneinstead. Mixed value / reference operands.strict— reference-based throughout.Overflowinginstead ofWrappingarithmetic.
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, neverCopy, so heap-allocated bigints qualify.
Structs§
- Field
- Montgomery context over modulus
T, with algorithm choice driven by the personality markerP(defaults toNct= variable-time fast path). - Odd
- Proof that a value is odd.
- Residue
- A value in
Field<T, P>, stored implicitly in Montgomery form.
Traits§
- DivNon
Zero - Infallible division / remainder by a proven-non-zero divisor.
- HasNon
Zero - Bridge to
core::num::NonZero— “the non-zero form ofSelf”. - Mont
Storage - Bound on the value stored in a
Residue. With thezeroizefeature this requiresT: Zeroize; otherwise it’s vacuous. - NonCt
- Sealed marker: the type is not the
Ctpersonality 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 toField<T, Ct>. Reads naturally at construction sites and sidesteps the type-inference ambiguity that bareField::new(modulus)hits —FieldCt::new(modulus)resolves unambiguously because the alias fixesP = Ctat the type level. Symmetric withFieldNct. - Field
Nct - Alias for the Nct variant of
Field. Equivalent toField<T, Nct>(matches the default personality). Provided for symmetry withFieldCtand to side-step the construction-site type-ambiguity pitfall —FieldNct::new(modulus)resolves unambiguously without the type-annotation/turbofish friction ofField::new(modulus). - Residue
Ct - Alias for the Ct variant of
Residue. Equivalent toResidue<'f, T, Ct>. Symmetric withResidueNct. - Residue
Nct - Alias for the Nct variant of
Residue. Equivalent toResidue<'f, T, Nct>. Symmetric withResidueCt.