Skip to content

Commit 3305cf1

Browse files
authored
crypto-bigint: rename UInt::shr to UInt::shr_vartime (#590)
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.
1 parent 2677696 commit 3305cf1

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

crypto-bigint/src/uint/shr.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ use core::ops::Shr;
66

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

52+
/// NOTE: this operation is variable time with respect to `rhs` *ONLY*.
53+
///
54+
/// When used with a fixed `rhs`, this function is constant-time with respect
55+
/// to `self`.
4856
fn shr(self, rhs: usize) -> UInt<LIMBS> {
49-
UInt::shr(&self, rhs)
57+
self.shr_vartime(rhs)
5058
}
5159
}
5260

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

64+
/// NOTE: this operation is variable time with respect to `rhs` *ONLY*.
65+
///
66+
/// When used with a fixed `rhs`, this function is constant-time with respect
67+
/// to `self`.
5668
fn shr(self, rhs: usize) -> UInt<LIMBS> {
57-
UInt::shr(self, rhs)
69+
self.shr_vartime(rhs)
5870
}
5971
}
6072

0 commit comments

Comments
 (0)