crypto_bigint/uint/
neg_mod.rs

1//! [`Uint`] modular negation operations.
2
3use crate::{Limb, NegMod, Uint};
4
5impl<const LIMBS: usize> Uint<LIMBS> {
6    /// Computes `-a mod p`.
7    /// Assumes `self` is in `[0, p)`.
8    pub const fn neg_mod(&self, p: &Self) -> Self {
9        let z = self.is_nonzero();
10        let mut ret = p.sbb(self, Limb::ZERO).0;
11        let mut i = 0;
12        while i < LIMBS {
13            // Set ret to 0 if the original value was 0, in which
14            // case ret would be p.
15            ret.limbs[i].0 = z.if_true_word(ret.limbs[i].0);
16            i += 1;
17        }
18        ret
19    }
20
21    /// Computes `-a mod p` for the special modulus
22    /// `p = MAX+1-c` where `c` is small enough to fit in a single [`Limb`].
23    pub const fn neg_mod_special(&self, c: Limb) -> Self {
24        Self::ZERO.sub_mod_special(self, c)
25    }
26}
27
28impl<const LIMBS: usize> NegMod for Uint<LIMBS> {
29    type Output = Self;
30
31    fn neg_mod(&self, p: &Self) -> Self {
32        debug_assert!(self < p);
33        self.neg_mod(p)
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use crate::U256;
40
41    #[test]
42    fn neg_mod_random() {
43        let x =
44            U256::from_be_hex("8d16e171674b4e6d8529edba4593802bf30b8cb161dd30aa8e550d41380007c2");
45        let p =
46            U256::from_be_hex("928334a4e4be0843ec225a4c9c61df34bdc7a81513e4b6f76f2bfa3148e2e1b5");
47
48        let actual = x.neg_mod(&p);
49        let expected =
50            U256::from_be_hex("056c53337d72b9d666f86c9256ce5f08cabc1b63b207864ce0d6ecf010e2d9f3");
51
52        assert_eq!(expected, actual);
53    }
54
55    #[test]
56    fn neg_mod_zero() {
57        let x =
58            U256::from_be_hex("0000000000000000000000000000000000000000000000000000000000000000");
59        let p =
60            U256::from_be_hex("928334a4e4be0843ec225a4c9c61df34bdc7a81513e4b6f76f2bfa3148e2e1b5");
61
62        let actual = x.neg_mod(&p);
63        let expected =
64            U256::from_be_hex("0000000000000000000000000000000000000000000000000000000000000000");
65
66        assert_eq!(expected, actual);
67    }
68}