Skip to main content

crypto_bigint/uint/
add_mod.rs

1//! [`UInt`] addition modulus operations.
2
3use crate::{AddMod, Limb, UInt};
4
5impl<const LIMBS: usize> UInt<LIMBS> {
6    /// Computes `self + rhs mod p` in constant time.
7    ///
8    /// Assumes `self` and `rhs` are `< p`.
9    pub const fn add_mod(&self, rhs: &UInt<LIMBS>, p: &UInt<LIMBS>) -> UInt<LIMBS> {
10        let (out, _carry) = self.adc(rhs, Limb::ZERO);
11
12        // Subtract the modulus, to ensure the result is smaller.
13        out.sub_mod(p, p)
14    }
15}
16
17macro_rules! impl_add_mod {
18    ($($size:expr),+) => {
19        $(
20            impl AddMod for UInt<$size> {
21                type Output = Self;
22
23                fn add_mod(&self, rhs: &Self, p: &Self) -> Self {
24                    debug_assert!(self < p);
25                    debug_assert!(rhs < p);
26                    self.add_mod(rhs, p)
27                }
28            }
29        )+
30    };
31}
32
33impl_add_mod!(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
34
35#[cfg(all(test, feature = "rand"))]
36mod tests {
37    use crate::UInt;
38
39    macro_rules! test_add_mod {
40        ($size:expr, $test_name:ident) => {
41            #[test]
42            fn $test_name() {
43                use crate::Limb;
44                use rand_core::SeedableRng;
45
46                let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(1);
47
48                let moduli = [UInt::<$size>::random(&mut rng), UInt::random(&mut rng)];
49
50                for p in &moduli {
51                    let base_cases = [(1u64, 0u64, 1u64), (0, 1, 1), (0, 0, 0)];
52                    for (a, b, c) in &base_cases {
53                        let a: UInt<$size> = (*a).into();
54                        let b: UInt<$size> = (*b).into();
55                        let c: UInt<$size> = (*c).into();
56
57                        assert_eq!(c, a.add_mod(&b, p));
58                    }
59
60                    assert_eq!(p.add_mod(&0u64.into(), p), 0u64.into());
61                    assert_eq!(p.add_mod(&1u64.into(), p), 1u64.into());
62
63                    if $size > 1 {
64                        for _i in 0..100 {
65                            let a: UInt<$size> = Limb::random(&mut rng).into();
66                            let b: UInt<$size> = Limb::random(&mut rng).into();
67                            let (a, b) = if a < b { (b, a) } else { (a, b) };
68
69                            let c = a.add_mod(&b, p);
70                            assert!(c < *p, "not reduced");
71                            assert_eq!(c, a.wrapping_add(&b), "result incorrect");
72                        }
73                    }
74
75                    for _i in 0..100 {
76                        let a = UInt::<$size>::random_mod(&mut rng, p);
77                        let b = UInt::<$size>::random_mod(&mut rng, p);
78
79                        let c = a.add_mod(&b, p);
80                        assert!(c < *p, "not reduced: {} >= {} ", c, p);
81
82                        let x = a.wrapping_add(&b);
83                        if x < *p {
84                            assert_eq!(c, x, "incorrect result");
85                        }
86                    }
87                }
88            }
89        };
90    }
91
92    // Test requires 1-limb is capable of representing a 64-bit integer
93    #[cfg(target_pointer_width = "64")]
94    test_add_mod!(1, test_add1);
95
96    test_add_mod!(2, test_add2);
97    test_add_mod!(3, test_add3);
98    test_add_mod!(4, test_add4);
99    test_add_mod!(5, test_add5);
100    test_add_mod!(6, test_add6);
101    test_add_mod!(7, test_add7);
102    test_add_mod!(8, test_add8);
103    test_add_mod!(9, test_add9);
104    test_add_mod!(10, test_add10);
105    test_add_mod!(11, test_add11);
106    test_add_mod!(12, test_add12);
107}