|
| 1 | +//! NTT primes and constants for 32-bit Word targets. |
| 2 | +//! |
| 3 | +//! Uses Proth primes of the form `K * 2^N + 1`. |
| 4 | +//! All constants computed by `integer/src/mul/ntt/compute_constants.py`. |
| 5 | +
|
| 6 | +use num_modular::FixedProth32; |
| 7 | + |
| 8 | +// Proth reducer instances — each with a different (N, K) pair. |
| 9 | +pub type Rp0 = FixedProth32<26, 7>; |
| 10 | +pub type Rp1 = FixedProth32<27, 15>; |
| 11 | +pub type Rp2 = FixedProth32<27, 17>; |
| 12 | + |
| 13 | +pub const P0: Rp0 = FixedProth32::<26, 7>; |
| 14 | +pub const P1: Rp1 = FixedProth32::<27, 15>; |
| 15 | +pub const P2: Rp2 = FixedProth32::<27, 17>; |
| 16 | + |
| 17 | +pub const K: usize = 3; |
| 18 | +pub const MAX_LOG_N: u32 = 26; |
| 19 | +pub const B_PACK_MIN: u32 = 8; |
| 20 | +pub const B_PACK_CANDIDATES: &[u32] = &[32, 16, 8]; |
| 21 | + |
| 22 | +pub type Lane = u32; |
| 23 | + |
| 24 | +/// Primitive `MAX_LOG_N`-th roots of unity for each prime. |
| 25 | +pub const OMEGA_MAX: [Lane; K] = [ |
| 26 | + 0x0000088b, // P0 |
| 27 | + 0x3a26eef8, // P1 |
| 28 | + 0x1aa0ab5e, // P2 |
| 29 | +]; |
| 30 | + |
| 31 | +pub const CRT_INV_IJ: [[Lane; K]; K] = [[0, 0x4e42c85b, 0x5fb425ef], [0, 0, 0x44000009], [0, 0, 0]]; |
| 32 | + |
| 33 | +/// Prime moduli indexed by PI. |
| 34 | +pub const MODULI: [Lane; K] = [Rp0::MODULUS, Rp1::MODULUS, Rp2::MODULUS]; |
| 35 | + |
| 36 | +#[cfg(test)] |
| 37 | +mod tests { |
| 38 | + use super::*; |
| 39 | + use num_modular::Reducer; |
| 40 | + |
| 41 | + type ReducerFns = (fn(Lane) -> Lane, fn(Lane) -> Lane, fn(Lane) -> Lane); |
| 42 | + |
| 43 | + #[test] |
| 44 | + fn test_primes_proth_form() { |
| 45 | + assert_eq!(MODULI[0], 7u32 * (1u32 << 26) + 1); |
| 46 | + assert_eq!(MODULI[1], 15u32 * (1u32 << 27) + 1); |
| 47 | + assert_eq!(MODULI[2], 17u32 * (1u32 << 27) + 1); |
| 48 | + } |
| 49 | + |
| 50 | + #[test] |
| 51 | + fn test_primes_v2() { |
| 52 | + for &p in &MODULI { |
| 53 | + let v2 = (p - 1).trailing_zeros(); |
| 54 | + assert!(v2 >= MAX_LOG_N, "v2(p-1) = {v2} < MAX_LOG_N"); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + #[test] |
| 59 | + fn test_omega_order() { |
| 60 | + for (pi, &omega_max) in OMEGA_MAX.iter().enumerate() { |
| 61 | + let p = MODULI[pi]; |
| 62 | + let (sqr, to_m, from_m): ReducerFns = match pi { |
| 63 | + 0 => { |
| 64 | + (|w| P0.reduce((w as u64) * (w as u64)), |v| P0.transform(v), |v| P0.residue(v)) |
| 65 | + } |
| 66 | + 1 => { |
| 67 | + (|w| P1.reduce((w as u64) * (w as u64)), |v| P1.transform(v), |v| P1.residue(v)) |
| 68 | + } |
| 69 | + 2 => { |
| 70 | + (|w| P2.reduce((w as u64) * (w as u64)), |v| P2.transform(v), |v| P2.residue(v)) |
| 71 | + } |
| 72 | + _ => unreachable!(), |
| 73 | + }; |
| 74 | + |
| 75 | + let mut w = to_m(omega_max); |
| 76 | + for _ in 0..MAX_LOG_N - 1 { |
| 77 | + w = sqr(w); |
| 78 | + } |
| 79 | + assert_eq!(from_m(w), p - 1, "omega^(2^(MAX_LOG_N-1)) != -1 mod p for prime {pi}"); |
| 80 | + w = sqr(w); |
| 81 | + assert_eq!(from_m(w), 1, "omega^(2^MAX_LOG_N) != 1 mod p for prime {pi}"); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments