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 #[must_use]
9 pub const fn neg(&self) -> Self {
10 Self {
11 montgomery_form: self
12 .montgomery_form
13 .neg_mod(MOD::PARAMS.modulus.as_nz_ref()),
14 phantom: self.phantom,
15 }
16 }
17}
18
19impl<MOD: ConstMontyParams<LIMBS>, const LIMBS: usize> Neg for ConstMontyForm<MOD, LIMBS> {
20 type Output = Self;
21 fn neg(self) -> Self {
22 ConstMontyForm::neg(&self)
23 }
24}
25
26impl<MOD: ConstMontyParams<LIMBS>, const LIMBS: usize> Neg for &ConstMontyForm<MOD, LIMBS> {
27 type Output = ConstMontyForm<MOD, LIMBS>;
28 fn neg(self) -> ConstMontyForm<MOD, LIMBS> {
29 ConstMontyForm::neg(self)
30 }
31}
32
33#[cfg(test)]
34mod tests {
35 use crate::{
36 U256, const_monty_form, const_monty_params, modular::const_monty_form::ConstMontyParams,
37 };
38
39 const_monty_params!(
40 Modulus,
41 U256,
42 "15477BCCEFE197328255BFA79A1217899016D927EF460F4FF404029D24FA4409"
43 );
44
45 const_monty_form!(Fe, Modulus);
46
47 #[test]
48 fn test_negate() {
49 let x =
50 U256::from_be_hex("77117F1273373C26C700D076B3F780074D03339F56DD0EFB60E7F58441FD3685");
51 let x_mod = Fe::new(&x);
52
53 let res = -x_mod;
54 let expected =
55 U256::from_be_hex("089B67BB2C124F084701AD76E8750D321385E35044C74CE457301A2A9BE061B1");
56
57 assert_eq!(res.retrieve(), expected);
58 }
59}