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