crypto_bigint/modular/boxed_monty_form/
neg.rs1use super::BoxedMontyForm;
4use core::ops::Neg;
5
6impl BoxedMontyForm {
7 pub fn neg(&self) -> Self {
9 Self {
10 montgomery_form: self.montgomery_form.neg_mod(&self.params.modulus),
11 params: self.params.clone(),
12 }
13 }
14}
15
16impl Neg for BoxedMontyForm {
17 type Output = Self;
18 fn neg(self) -> Self {
19 BoxedMontyForm::neg(&self)
20 }
21}
22
23impl Neg for &BoxedMontyForm {
24 type Output = BoxedMontyForm;
25 fn neg(self) -> BoxedMontyForm {
26 BoxedMontyForm::neg(self)
27 }
28}
29
30#[cfg(test)]
31mod tests {
32 use crate::{
33 modular::{BoxedMontyForm, BoxedMontyParams},
34 BoxedUint,
35 };
36 use hex_literal::hex;
37
38 #[test]
39 fn neg_expected() {
40 let modulus = BoxedUint::from_be_slice(
41 &hex!("ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551"),
42 256,
43 )
44 .expect("error creating modulus");
45 let params = BoxedMontyParams::new(modulus.to_odd().unwrap());
46
47 let x = BoxedUint::from_be_slice(
48 &hex!("44acf6b7e36c1342c2c5897204fe09504e1e2efb1a900377dbc4e7a6a133ec56"),
49 256,
50 )
51 .expect("error creating boxeduint");
52 let x_mod = BoxedMontyForm::new(x, params.clone());
53
54 assert!(bool::from((x_mod.neg() + x_mod).is_zero()));
55 }
56}