1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*!
# Fast Modular Arithmetic
[](https://crates.io/crates/lib_modulo)
[](https://docs.rs/lib_modulo)


High-performance word-size modular arithmetic using Barrett, Montgomery or Plantard multiplication.
# Overview
Naive modular multiplication typically requires widening the operands, followed by an expensive division.
This crate avoids division by using:
- [Barrett multiplication](https://doi.org/10.1007/3-540-47721-7_24)
- [Montgomery multiplication](https://doi.org/10.1090/s0025-5718-1985-0777282-x)
- [Plantard multiplication](https://thomas-plantard.github.io/pdf/Plantard21.pdf)
These techniques significantly improve performance, especially when the modulus is determined at *runtime*.
# Selection guide
| Type | Modulus | Notes |
|------------------|---------------------|----------------------------------------------------|
| [`Modulus32`] | odd, < 2^31.3... | fastest |
| [`Modulus32Any`] | in `[2, 2^32)` | supports even moduli, zero-cost modulus switching |
| [`Modulus64`] | odd, fits in `u64` | supports large moduli |
# Advanced usage
`Residue{N}` types hold a reference to their corresponding `Modulus{N}` for convenience.
However, when storing many residues sharing the same modulus, this can be memory-intensive.
In such cases, [`Raw32`] or [`Raw64`] can be used instead.
The caller is responsible for associating them with the correct modulus.
# Example
Below is an implementation of a rolling hash algorithm using [`Residue64`].
This allows the use of large prime moduli without overflow.
```
*/
//! ```
pub use *;
pub use *;
pub use *;
pub use *;