crypto_bigint/modular/monty_form/
mul.rs

1//! Multiplications between integers in Montgomery form with a modulus set at runtime.
2
3use super::MontyForm;
4use crate::{
5    modular::mul::{mul_montgomery_form, square_montgomery_form},
6    Square, SquareAssign,
7};
8use core::ops::{Mul, MulAssign};
9
10impl<const LIMBS: usize> MontyForm<LIMBS> {
11    /// Multiplies by `rhs`.
12    pub const fn mul(&self, rhs: &Self) -> Self {
13        Self {
14            montgomery_form: mul_montgomery_form(
15                &self.montgomery_form,
16                &rhs.montgomery_form,
17                &self.params.modulus,
18                self.params.mod_neg_inv,
19            ),
20            params: self.params,
21        }
22    }
23
24    /// Computes the (reduced) square.
25    pub const fn square(&self) -> Self {
26        Self {
27            montgomery_form: square_montgomery_form(
28                &self.montgomery_form,
29                &self.params.modulus,
30                self.params.mod_neg_inv,
31            ),
32            params: self.params,
33        }
34    }
35}
36
37impl<const LIMBS: usize> Mul<&MontyForm<LIMBS>> for &MontyForm<LIMBS> {
38    type Output = MontyForm<LIMBS>;
39    fn mul(self, rhs: &MontyForm<LIMBS>) -> MontyForm<LIMBS> {
40        debug_assert_eq!(self.params, rhs.params);
41        self.mul(rhs)
42    }
43}
44
45impl<const LIMBS: usize> Mul<MontyForm<LIMBS>> for &MontyForm<LIMBS> {
46    type Output = MontyForm<LIMBS>;
47    #[allow(clippy::op_ref)]
48    fn mul(self, rhs: MontyForm<LIMBS>) -> MontyForm<LIMBS> {
49        self * &rhs
50    }
51}
52
53impl<const LIMBS: usize> Mul<&MontyForm<LIMBS>> for MontyForm<LIMBS> {
54    type Output = MontyForm<LIMBS>;
55    #[allow(clippy::op_ref)]
56    fn mul(self, rhs: &MontyForm<LIMBS>) -> MontyForm<LIMBS> {
57        &self * rhs
58    }
59}
60
61impl<const LIMBS: usize> Mul<MontyForm<LIMBS>> for MontyForm<LIMBS> {
62    type Output = MontyForm<LIMBS>;
63    fn mul(self, rhs: MontyForm<LIMBS>) -> MontyForm<LIMBS> {
64        &self * &rhs
65    }
66}
67
68impl<const LIMBS: usize> MulAssign<&MontyForm<LIMBS>> for MontyForm<LIMBS> {
69    fn mul_assign(&mut self, rhs: &MontyForm<LIMBS>) {
70        *self = *self * rhs;
71    }
72}
73
74impl<const LIMBS: usize> MulAssign<MontyForm<LIMBS>> for MontyForm<LIMBS> {
75    fn mul_assign(&mut self, rhs: MontyForm<LIMBS>) {
76        *self *= &rhs;
77    }
78}
79
80impl<const LIMBS: usize> Square for MontyForm<LIMBS> {
81    fn square(&self) -> Self {
82        MontyForm::square(self)
83    }
84}
85
86impl<const LIMBS: usize> SquareAssign for MontyForm<LIMBS> {
87    fn square_assign(&mut self) {
88        *self = self.square()
89    }
90}