crypto_bigint/uint/modular/constant_mod/
const_mul.rs

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