Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto-bigint: rename UInt::shr to UInt::shr_vartime #590

Merged
merged 1 commit into from
Aug 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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