Skip to content

Commit ed9a92f

Browse files
authored
Add subtle trait impls for DynResidue and DynResidueParams (#269)
1 parent e0d71af commit ed9a92f

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/uint/modular/runtime_mod.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::{
77
Retrieve,
88
};
99

10-
use subtle::CtOption;
10+
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
1111

1212
/// Additions between residues with a modulus set at runtime
1313
mod runtime_add;
@@ -103,6 +103,28 @@ impl<const LIMBS: usize> DynResidueParams<LIMBS> {
103103
}
104104
}
105105

106+
impl<const LIMBS: usize> ConditionallySelectable for DynResidueParams<LIMBS> {
107+
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
108+
Self {
109+
modulus: Uint::conditional_select(&a.modulus, &b.modulus, choice),
110+
r: Uint::conditional_select(&a.r, &b.r, choice),
111+
r2: Uint::conditional_select(&a.r2, &b.r2, choice),
112+
r3: Uint::conditional_select(&a.r3, &b.r3, choice),
113+
mod_neg_inv: Limb::conditional_select(&a.mod_neg_inv, &b.mod_neg_inv, choice),
114+
}
115+
}
116+
}
117+
118+
impl<const LIMBS: usize> ConstantTimeEq for DynResidueParams<LIMBS> {
119+
fn ct_eq(&self, other: &Self) -> Choice {
120+
self.modulus.ct_eq(&other.modulus)
121+
& self.r.ct_eq(&other.r)
122+
& self.r2.ct_eq(&other.r2)
123+
& self.r3.ct_eq(&other.r3)
124+
& self.mod_neg_inv.ct_eq(&other.mod_neg_inv)
125+
}
126+
}
127+
106128
/// A residue represented using `LIMBS` limbs. The odd modulus of this residue is set at runtime.
107129
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
108130
pub struct DynResidue<const LIMBS: usize> {
@@ -211,6 +233,30 @@ impl<const LIMBS: usize, P: ResidueParams<LIMBS>> From<&Residue<P, LIMBS>> for D
211233
}
212234
}
213235

236+
impl<const LIMBS: usize> ConditionallySelectable for DynResidue<LIMBS> {
237+
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
238+
Self {
239+
montgomery_form: Uint::conditional_select(
240+
&a.montgomery_form,
241+
&b.montgomery_form,
242+
choice,
243+
),
244+
residue_params: DynResidueParams::conditional_select(
245+
&a.residue_params,
246+
&b.residue_params,
247+
choice,
248+
),
249+
}
250+
}
251+
}
252+
253+
impl<const LIMBS: usize> ConstantTimeEq for DynResidue<LIMBS> {
254+
fn ct_eq(&self, other: &Self) -> Choice {
255+
self.montgomery_form.ct_eq(&other.montgomery_form)
256+
& self.residue_params.ct_eq(&other.residue_params)
257+
}
258+
}
259+
214260
/// NOTE: this does _not_ zeroize the parameters, in order to maintain some form of type consistency
215261
#[cfg(feature = "zeroize")]
216262
impl<const LIMBS: usize> zeroize::Zeroize for DynResidue<LIMBS> {

0 commit comments

Comments
 (0)