crypto_bigint/modular/const_monty_form/
neg.rs1use super::{ConstMontyForm, ConstMontyParams};
4use core::ops::Neg;
5
6impl<MOD: ConstMontyParams<LIMBS>, const LIMBS: usize> ConstMontyForm<MOD, LIMBS> {
7 pub const fn neg(&self) -> Self {
9 Self {
10 montgomery_form: self.montgomery_form.neg_mod(MOD::MODULUS.as_ref()),
11 phantom: self.phantom,
12 }
13 }
14}
15
16impl<MOD: ConstMontyParams<LIMBS>, const LIMBS: usize> Neg for ConstMontyForm<MOD, LIMBS> {
17 type Output = Self;
18 fn neg(self) -> Self {
19 ConstMontyForm::neg(&self)
20 }
21}
22
23impl<MOD: ConstMontyParams<LIMBS>, const LIMBS: usize> Neg for &ConstMontyForm<MOD, LIMBS> {
24 type Output = ConstMontyForm<MOD, LIMBS>;
25 fn neg(self) -> ConstMontyForm<MOD, LIMBS> {
26 ConstMontyForm::neg(self)
27 }
28}
29
30#[cfg(test)]
31mod tests {
32 use crate::{
33 const_monty_form, impl_modulus, modular::const_monty_form::ConstMontyParams, U256,
34 };
35
36 impl_modulus!(
37 Modulus,
38 U256,
39 "15477BCCEFE197328255BFA79A1217899016D927EF460F4FF404029D24FA4409"
40 );
41
42 #[test]
43 fn test_negate() {
44 let x =
45 U256::from_be_hex("77117F1273373C26C700D076B3F780074D03339F56DD0EFB60E7F58441FD3685");
46 let x_mod = const_monty_form!(x, Modulus);
47
48 let res = -x_mod;
49 let expected =
50 U256::from_be_hex("089B67BB2C124F084701AD76E8750D321385E35044C74CE457301A2A9BE061B1");
51
52 assert_eq!(res.retrieve(), expected);
53 }
54}