modmath/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2
3//! Modular math implemented with traits.
4//!
5//! This crate provides modular arithmetic implemented not for
6//! any particular type, but for any type that implements minimal
7//! set of `core::ops::` and `num_traits::` traits.
8//!
9//! All provided functions are simply free functions.
10//!
11//! There are three verions of each: `basic` that has least amount
12//! of constraints, but requires `Copy` to be implemented for the type.
13//! `constrained` requires `Clone`.
14//! `strict` requires neither, but has most other constaints to be able to
15//! operate with references and [`Overflowing`](https://docs.rs/num-traits/latest/num_traits/ops/overflowing) arithmetic.
16
17//! Tested with builtin integers and [`num-bigint`](https://crates.io/crates/num-bigint), [`crypto-bigint`](https://crates.io/crates/crypto-bigint), [`bnum`](https://crates.io/crates/bnum), [`ibig`](https://crates.io/crates/ibig)
18//! and [`fixed-bigint`](https://crates.io/crates/fixed-bigint) crates. `basic` versions of functions
19//! wont work with `num-bigint` and `ibig` as both require heap
20//! allocation.
21
22#![cfg_attr(not(test), no_std)]
23
24mod add;
25mod exp;
26mod mul;
27mod sub;
28
29mod inv;
30mod montgomery;
31
32pub use add::{basic_mod_add, constrained_mod_add, strict_mod_add};
33pub use exp::{basic_mod_exp, constrained_mod_exp, strict_mod_exp};
34pub use inv::{basic_mod_inv, constrained_mod_inv, strict_mod_inv};
35pub use montgomery::{
36    NPrimeMethod, basic_compute_montgomery_params, basic_compute_montgomery_params_with_method,
37    basic_from_montgomery, basic_montgomery_mod_exp, basic_montgomery_mod_mul,
38    basic_montgomery_mul, basic_to_montgomery, constrained_compute_montgomery_params,
39    constrained_compute_montgomery_params_with_method, constrained_from_montgomery,
40    constrained_montgomery_mod_exp, constrained_montgomery_mod_mul, constrained_montgomery_mul,
41    constrained_to_montgomery, strict_compute_montgomery_params,
42    strict_compute_montgomery_params_with_method, strict_from_montgomery,
43    strict_montgomery_mod_exp, strict_montgomery_mod_mul, strict_to_montgomery,
44};
45pub use mul::{basic_mod_mul, constrained_mod_mul, strict_mod_mul};
46pub use sub::{basic_mod_sub, constrained_mod_sub, strict_mod_sub};
47
48#[cfg(test)]
49#[macro_export]
50macro_rules! maybe_test {
51    (on, $e:expr) => {
52        $e
53    };
54    (off, $e:expr) => {
55        ()
56    };
57}