Skip to content

Commit

Permalink
exp to var-time
Browse files Browse the repository at this point in the history
  • Loading branch information
Al-Kindi-0 committed Nov 2, 2022
1 parent 7cbf0bb commit eea409c
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions math/src/field/f64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,20 @@ impl FieldElement for BaseElement {

#[inline]
fn exp(self, power: Self::PositiveInteger) -> Self {
let mut b: Self;
let mut r = Self::ONE;
for i in (0..64).rev() {
r = r.square();
b = r;
b *= self;
// Constant-time branching
let mask = -(((power >> i) & 1 == 1) as i64) as u64;
r.0 ^= mask & (r.0 ^ b.0);
let mut b = self;

if power == 0 {
return Self::ONE;
} else if b == Self::ZERO {
return Self::ZERO;
}

let mut r = if power & 1 == 1 { b } else { Self::ONE };
for i in 1..64 - power.leading_zeros() {
b = b.square();
if (power >> i) & 1 == 1 {
r *= b;
}
}

r
Expand Down

0 comments on commit eea409c

Please sign in to comment.