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
use crate::{
algorithms::{mul_redc_inline, redc_inline, square_redc_inline},
arch::{gcd, gcd_extended, reduce_1},
InvMod, Montgomery, MontgomeryParameters, GCD, U256,
};
impl GCD for U256 {
#[inline(always)]
fn gcd(a: &Self, b: &Self) -> Self {
gcd(a.clone(), b.clone())
}
#[inline(always)]
fn gcd_extended(a: &Self, b: &Self) -> (Self, Self, Self, bool) {
gcd_extended(a.clone(), b.clone())
}
}
impl Montgomery for U256 {
#[inline(always)]
fn reduce_1_inline<M: MontgomeryParameters<UInt = U256>>(&self) -> Self {
reduce_1(self, &M::MODULUS)
}
#[inline(always)]
fn redc_inline<M: MontgomeryParameters<UInt = U256>>(lo: &Self, hi: &Self) -> Self {
redc_inline::<M>(lo, hi)
}
#[inline(always)]
fn square_redc_inline<M: MontgomeryParameters<UInt = U256>>(&self) -> Self {
square_redc_inline::<M>(self)
}
#[inline(always)]
fn mul_redc_inline<M: MontgomeryParameters<UInt = U256>>(&self, rhs: &Self) -> Self {
mul_redc_inline::<M>(self, rhs)
}
#[inline(always)]
fn inv_redc<M: MontgomeryParameters<UInt = U256>>(&self) -> Option<Self> {
self.inv_mod(&M::MODULUS).map(|ni| ni.mul_redc::<M>(&M::R3))
}
}