crypto_bigint/uint/
add_mod.rs
1use crate::{AddMod, Limb, UInt};
4
5impl<const LIMBS: usize> UInt<LIMBS> {
6 pub const fn add_mod(&self, rhs: &UInt<LIMBS>, p: &UInt<LIMBS>) -> UInt<LIMBS> {
10 let (w, carry) = self.adc(rhs, Limb::ZERO);
11
12 let (w, borrow) = w.sbb(p, Limb::ZERO);
14 let (_, borrow) = carry.sbb(Limb::ZERO, borrow);
15
16 let mut i = 0;
20 let mut res = Self::ZERO;
21 let mut carry = Limb::ZERO;
22
23 while i < LIMBS {
24 let rhs = p.limbs[i].bitand(borrow);
25 let (limb, c) = w.limbs[i].adc(rhs, carry);
26 res.limbs[i] = limb;
27 carry = c;
28 i += 1;
29 }
30
31 res
32 }
33
34 pub const fn add_mod_special(&self, rhs: &Self, c: Limb) -> Self {
39 let (out, carry) = self.adc(rhs, c);
41
42 let l = carry.0.wrapping_sub(1) & c.0;
46 let (out, _) = out.sbb(&UInt::from_word(l), Limb::ZERO);
47 out
48 }
49}
50
51impl<const LIMBS: usize> AddMod for UInt<LIMBS> {
52 type Output = Self;
53
54 fn add_mod(&self, rhs: &Self, p: &Self) -> Self {
55 debug_assert!(self < p);
56 debug_assert!(rhs < p);
57 self.add_mod(rhs, p)
58 }
59}
60
61#[cfg(all(test, feature = "rand"))]
62mod tests {
63 use crate::{Limb, NonZero, Random, RandomMod, UInt, U256};
64 use rand_core::SeedableRng;
65
66 #[test]
69 fn add_mod_nist_p256() {
70 let a =
71 U256::from_be_hex("44acf6b7e36c1342c2c5897204fe09504e1e2efb1a900377dbc4e7a6a133ec56");
72 let b =
73 U256::from_be_hex("d5777c45019673125ad240f83094d4252d829516fac8601ed01979ec1ec1a251");
74 let n =
75 U256::from_be_hex("ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551");
76
77 let actual = a.add_mod(&b, &n);
78 let expected =
79 U256::from_be_hex("1a2472fde50286541d97ca6a3592dd75beb9c9646e40c511b82496cfc3926956");
80
81 assert_eq!(expected, actual);
82 }
83
84 macro_rules! test_add_mod_special {
85 ($size:expr, $test_name:ident) => {
86 #[test]
87 fn $test_name() {
88 let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(1);
89 let moduli = [
90 NonZero::<Limb>::random(&mut rng),
91 NonZero::<Limb>::random(&mut rng),
92 ];
93
94 for special in &moduli {
95 let p = &NonZero::new(UInt::ZERO.wrapping_sub(&UInt::from_word(special.0)))
96 .unwrap();
97
98 let minus_one = p.wrapping_sub(&UInt::ONE);
99
100 let base_cases = [
101 (UInt::ZERO, UInt::ZERO, UInt::ZERO),
102 (UInt::ONE, UInt::ZERO, UInt::ONE),
103 (UInt::ZERO, UInt::ONE, UInt::ONE),
104 (minus_one, UInt::ONE, UInt::ZERO),
105 (UInt::ONE, minus_one, UInt::ZERO),
106 ];
107 for (a, b, c) in &base_cases {
108 let x = a.add_mod_special(b, *special.as_ref());
109 assert_eq!(*c, x, "{} + {} mod {} = {} != {}", a, b, p, x, c);
110 }
111
112 for _i in 0..100 {
113 let a = UInt::<$size>::random_mod(&mut rng, p);
114 let b = UInt::<$size>::random_mod(&mut rng, p);
115
116 let c = a.add_mod_special(&b, *special.as_ref());
117 assert!(c < **p, "not reduced: {} >= {} ", c, p);
118
119 let expected = a.add_mod(&b, p);
120 assert_eq!(c, expected, "incorrect result");
121 }
122 }
123 }
124 };
125 }
126
127 test_add_mod_special!(1, add_mod_special_1);
128 test_add_mod_special!(2, add_mod_special_2);
129 test_add_mod_special!(3, add_mod_special_3);
130 test_add_mod_special!(4, add_mod_special_4);
131 test_add_mod_special!(5, add_mod_special_5);
132 test_add_mod_special!(6, add_mod_special_6);
133 test_add_mod_special!(7, add_mod_special_7);
134 test_add_mod_special!(8, add_mod_special_8);
135 test_add_mod_special!(9, add_mod_special_9);
136 test_add_mod_special!(10, add_mod_special_10);
137 test_add_mod_special!(11, add_mod_special_11);
138 test_add_mod_special!(12, add_mod_special_12);
139}