crypto_bigint/modular/boxed_monty_form/
add.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! Additions between boxed Montgoemery form integers.

use super::BoxedMontyForm;
use core::ops::{Add, AddAssign};

impl BoxedMontyForm {
    /// Adds `rhs`.
    pub fn add(&self, rhs: &Self) -> Self {
        debug_assert_eq!(self.params, rhs.params);

        Self {
            montgomery_form: self
                .montgomery_form
                .add_mod(&rhs.montgomery_form, &self.params.modulus),
            params: self.params.clone(),
        }
    }

    /// Double `self`.
    pub fn double(&self) -> Self {
        Self {
            montgomery_form: self.montgomery_form.double_mod(&self.params.modulus),
            params: self.params.clone(),
        }
    }
}

impl Add<&BoxedMontyForm> for &BoxedMontyForm {
    type Output = BoxedMontyForm;
    fn add(self, rhs: &BoxedMontyForm) -> BoxedMontyForm {
        self.add(rhs)
    }
}

impl Add<BoxedMontyForm> for &BoxedMontyForm {
    type Output = BoxedMontyForm;
    #[allow(clippy::op_ref)]
    fn add(self, rhs: BoxedMontyForm) -> BoxedMontyForm {
        self + &rhs
    }
}

impl Add<&BoxedMontyForm> for BoxedMontyForm {
    type Output = BoxedMontyForm;
    #[allow(clippy::op_ref)]
    fn add(self, rhs: &BoxedMontyForm) -> BoxedMontyForm {
        &self + rhs
    }
}

impl Add<BoxedMontyForm> for BoxedMontyForm {
    type Output = BoxedMontyForm;
    fn add(self, rhs: BoxedMontyForm) -> BoxedMontyForm {
        &self + &rhs
    }
}

impl AddAssign<&BoxedMontyForm> for BoxedMontyForm {
    fn add_assign(&mut self, rhs: &BoxedMontyForm) {
        debug_assert_eq!(self.params, rhs.params);
        self.montgomery_form = self
            .montgomery_form
            .add_mod(&rhs.montgomery_form, &self.params.modulus)
    }
}

impl AddAssign<BoxedMontyForm> for BoxedMontyForm {
    fn add_assign(&mut self, rhs: BoxedMontyForm) {
        *self += &rhs;
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        modular::{BoxedMontyForm, BoxedMontyParams},
        BoxedUint,
    };
    use hex_literal::hex;

    #[test]
    fn add_overflow() {
        let modulus = BoxedUint::from_be_slice(
            &hex!("ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551"),
            256,
        )
        .unwrap();
        let params = BoxedMontyParams::new(modulus.to_odd().unwrap());

        let x = BoxedUint::from_be_slice(
            &hex!("44acf6b7e36c1342c2c5897204fe09504e1e2efb1a900377dbc4e7a6a133ec56"),
            256,
        )
        .unwrap();
        let mut x_mod = BoxedMontyForm::new(x, params.clone());

        let y = BoxedUint::from_be_slice(
            &hex!("d5777c45019673125ad240f83094d4252d829516fac8601ed01979ec1ec1a251"),
            256,
        )
        .unwrap();
        let y_mod = BoxedMontyForm::new(y, params.clone());

        x_mod += &y_mod;

        let expected = BoxedUint::from_be_slice(
            &hex!("1a2472fde50286541d97ca6a3592dd75beb9c9646e40c511b82496cfc3926956"),
            256,
        )
        .unwrap();

        assert_eq!(expected, x_mod.retrieve());
    }
}