crypto_bigint/modular/const_monty_form/
mul.rs

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