crypto_bigint/uint/modular/constant_mod/
const_neg.rs

1use core::ops::Neg;
2
3use super::{Residue, ResidueParams};
4
5impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Residue<MOD, LIMBS> {
6    /// Negates the number.
7    pub const fn neg(&self) -> Self {
8        Self::ZERO.sub(self)
9    }
10}
11
12impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Neg for Residue<MOD, LIMBS> {
13    type Output = Self;
14    fn neg(self) -> Self {
15        Residue::neg(&self)
16    }
17}
18
19impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Neg for &Residue<MOD, LIMBS> {
20    type Output = Residue<MOD, LIMBS>;
21    fn neg(self) -> Residue<MOD, LIMBS> {
22        Residue::neg(self)
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use crate::{const_residue, impl_modulus, modular::constant_mod::ResidueParams, U256};
29
30    impl_modulus!(
31        Modulus,
32        U256,
33        "15477BCCEFE197328255BFA79A1217899016D927EF460F4FF404029D24FA4409"
34    );
35
36    #[test]
37    fn test_negate() {
38        let x =
39            U256::from_be_hex("77117F1273373C26C700D076B3F780074D03339F56DD0EFB60E7F58441FD3685");
40        let x_mod = const_residue!(x, Modulus);
41
42        let res = -x_mod;
43        let expected =
44            U256::from_be_hex("089B67BB2C124F084701AD76E8750D321385E35044C74CE457301A2A9BE061B1");
45
46        assert_eq!(res.retrieve(), expected);
47    }
48}