Skip to content

Commit

Permalink
crypto-bigint: rename UInt::shr to UInt::shr_vartime (#590)
Browse files Browse the repository at this point in the history
Renames this function to note that it's constant-time with respect to
the `self` value, but variable-time with respect to the amount the
`self` value is being shifted by.
  • Loading branch information
tarcieri committed Aug 23, 2021
1 parent 2677696 commit 3305cf1
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions crypto-bigint/src/uint/shr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ use core::ops::Shr;

impl<const LIMBS: usize> UInt<LIMBS> {
/// Computes `self >> n`.
// TODO(tarcieri): replace with `const impl Shr<usize>` when stable
///
/// NOTE: this operation is variable time with respect to `n` *ONLY*.
///
/// When used with a fixed `n`, this function is constant-time with respect
/// to `self`.
#[inline(always)]
pub const fn shr(&self, shift: usize) -> Self {
pub const fn shr_vartime(&self, shift: usize) -> Self {
let full_shifts = shift / limb::BIT_SIZE;
let small_shift = shift & (limb::BIT_SIZE - 1);
let mut limbs = [Limb::ZERO; LIMBS];
Expand Down Expand Up @@ -45,16 +49,24 @@ impl<const LIMBS: usize> UInt<LIMBS> {
impl<const LIMBS: usize> Shr<usize> for UInt<LIMBS> {
type Output = UInt<LIMBS>;

/// NOTE: this operation is variable time with respect to `rhs` *ONLY*.
///
/// When used with a fixed `rhs`, this function is constant-time with respect
/// to `self`.
fn shr(self, rhs: usize) -> UInt<LIMBS> {
UInt::shr(&self, rhs)
self.shr_vartime(rhs)
}
}

impl<const LIMBS: usize> Shr<usize> for &UInt<LIMBS> {
type Output = UInt<LIMBS>;

/// NOTE: this operation is variable time with respect to `rhs` *ONLY*.
///
/// When used with a fixed `rhs`, this function is constant-time with respect
/// to `self`.
fn shr(self, rhs: usize) -> UInt<LIMBS> {
UInt::shr(self, rhs)
self.shr_vartime(rhs)
}
}

Expand Down

0 comments on commit 3305cf1

Please sign in to comment.