Skip to content

Commit 2c0ae83

Browse files
authored
Zeroes for residues (#139)
- Add DynResidue::zero() - Add Residue::ZERO, and implement ConstantTimeEq, Default, and Zero for Residue
1 parent 6f30b61 commit 2c0ae83

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/uint/modular/constant_mod.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use core::marker::PhantomData;
22

3-
use subtle::{Choice, ConditionallySelectable};
3+
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
44

5-
use crate::{Limb, UInt};
5+
use crate::{Limb, UInt, Zero};
66

77
use super::{reduction::montgomery_reduction, GenericResidue};
88

@@ -50,6 +50,12 @@ where
5050
}
5151

5252
impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Residue<MOD, LIMBS> {
53+
/// The representation of 0 mod `MOD`.
54+
pub const ZERO: Self = Self {
55+
montgomery_form: UInt::<LIMBS>::ZERO,
56+
phantom: PhantomData,
57+
};
58+
5359
/// The representation of 1 mod `MOD`.
5460
pub const ONE: Self = Self {
5561
montgomery_form: MOD::R,
@@ -100,3 +106,19 @@ impl<MOD: ResidueParams<LIMBS> + Copy, const LIMBS: usize> ConditionallySelectab
100106
}
101107
}
102108
}
109+
110+
impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> ConstantTimeEq for Residue<MOD, LIMBS> {
111+
fn ct_eq(&self, other: &Self) -> Choice {
112+
self.montgomery_form.ct_eq(&other.montgomery_form)
113+
}
114+
}
115+
116+
impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Default for Residue<MOD, LIMBS> {
117+
fn default() -> Self {
118+
Self::ZERO
119+
}
120+
}
121+
122+
impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Zero for Residue<MOD, LIMBS> {
123+
const ZERO: Self = Self::ZERO;
124+
}

src/uint/modular/runtime_mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ impl<const LIMBS: usize> DynResidue<LIMBS> {
7676
self.residue_params.mod_neg_inv,
7777
)
7878
}
79+
80+
/// Instantiates a new `Residue` that represents zero.
81+
pub const fn zero(residue_params: DynResidueParams<LIMBS>) -> Self {
82+
Self {
83+
montgomery_form: UInt::<LIMBS>::ZERO,
84+
residue_params,
85+
}
86+
}
7987
}
8088

8189
impl<const LIMBS: usize> GenericResidue<LIMBS> for DynResidue<LIMBS> {

0 commit comments

Comments
 (0)