Skip to content

Commit 37d8f10

Browse files
committed
use overflowing_shl instead of shl_vartime; same for shr
1 parent fc9a322 commit 37d8f10

File tree

6 files changed

+20
-56
lines changed

6 files changed

+20
-56
lines changed

benches/boxed_uint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn bench_shifts(c: &mut Criterion) {
1111
group.bench_function("shl_vartime", |b| {
1212
b.iter_batched(
1313
|| BoxedUint::random(&mut OsRng, UINT_BITS),
14-
|x| black_box(x.shl_vartime(UINT_BITS / 2 + 10)),
14+
|x| black_box(x.overflowing_shl(UINT_BITS / 2 + 10).0),
1515
BatchSize::SmallInput,
1616
)
1717
});
@@ -27,7 +27,7 @@ fn bench_shifts(c: &mut Criterion) {
2727
group.bench_function("shr_vartime", |b| {
2828
b.iter_batched(
2929
|| BoxedUint::random(&mut OsRng, UINT_BITS),
30-
|x| black_box(x.shr_vartime(UINT_BITS / 2 + 10)),
30+
|x| black_box(x.overflowing_shr(UINT_BITS / 2 + 10).0),
3131
BatchSize::SmallInput,
3232
)
3333
});

src/uint/boxed/bits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ mod tests {
140140
fn uint_with_bits_at(positions: &[u32]) -> BoxedUint {
141141
let mut result = BoxedUint::zero_with_precision(256);
142142
for &pos in positions {
143-
result |= BoxedUint::one_with_precision(256).shl_vartime(pos).0;
143+
result |= BoxedUint::one_with_precision(256).overflowing_shl(pos).0;
144144
}
145145
result
146146
}

src/uint/boxed/div.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl BoxedUint {
5252
let mut bd = self.bits_precision() - mb;
5353
let mut rem = self.clone();
5454
// Will not overflow since `bd < bits_precision`
55-
let mut c = rhs.shl_vartime(bd).0;
55+
let mut c = rhs.overflowing_shl(bd).0;
5656

5757
loop {
5858
let (r, borrow) = rem.sbb(&c, Limb::ZERO);
@@ -135,7 +135,7 @@ impl BoxedUint {
135135
let mut remainder = self.clone();
136136
let mut quotient = Self::zero_with_precision(self.bits_precision());
137137
// Will not overflow since `bd < bits_precision`
138-
let mut c = rhs.shl_vartime(bd).0;
138+
let mut c = rhs.overflowing_shl(bd).0;
139139

140140
loop {
141141
let (mut r, borrow) = remainder.sbb(&c, Limb::ZERO);

src/uint/boxed/shl.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,6 @@ impl BoxedUint {
111111
Some(())
112112
}
113113

114-
/// Computes `self << shift`.
115-
/// Returns `None` if `shift >= self.bits_precision()`.
116-
///
117-
/// NOTE: this operation is variable time with respect to `shift` *ONLY*.
118-
///
119-
/// When used with a fixed `shift`, this function is constant-time with respect to `self`.
120-
#[inline(always)]
121-
pub fn shl_vartime(&self, shift: u32) -> (Self, ConstChoice) {
122-
let mut result = Self::zero_with_precision(self.bits_precision());
123-
let success = self.shl_vartime_into(&mut result, shift);
124-
// TODO: is this okay?
125-
126-
(
127-
result,
128-
// If success, then return ConstChoice::False since it's not overflowing
129-
success.map_or(ConstChoice::TRUE, |_| ConstChoice::FALSE),
130-
)
131-
}
132-
133114
/// Computes `self << 1` in constant-time.
134115
pub(crate) fn shl1(&self) -> Self {
135116
let mut ret = self.clone();
@@ -240,19 +221,19 @@ mod tests {
240221
assert_eq!(BoxedUint::from(4u8), &one << 2);
241222
assert_eq!(
242223
BoxedUint::from(0x80000000000000000u128),
243-
one.shl_vartime(67).0
224+
one.overflowing_shl(67).0
244225
);
245226
}
246227

247228
#[test]
248229
fn shl_vartime() {
249230
let one = BoxedUint::one_with_precision(128);
250231

251-
assert_eq!(BoxedUint::from(2u8), one.shl_vartime(1).0);
252-
assert_eq!(BoxedUint::from(4u8), one.shl_vartime(2).0);
232+
assert_eq!(BoxedUint::from(2u8), one.overflowing_shl(1).0);
233+
assert_eq!(BoxedUint::from(4u8), one.overflowing_shl(2).0);
253234
assert_eq!(
254235
BoxedUint::from(0x80000000000000000u128),
255-
one.shl_vartime(67).0
236+
one.overflowing_shl(67).0
256237
);
257238
}
258239
}

src/uint/boxed/shr.rs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! [`BoxedUint`] bitwise right shift operations.
22
3-
use crate::{BoxedUint, ConstChoice, ConstantTimeSelect, Limb, WrappingShr, Zero};
3+
use crate::{BoxedUint, ConstantTimeSelect, Limb, WrappingShr, Zero};
44
use core::ops::{Shr, ShrAssign};
55
use subtle::{Choice, ConstantTimeLess};
66

@@ -115,24 +115,6 @@ impl BoxedUint {
115115
Some(())
116116
}
117117

118-
/// Computes `self >> shift`.
119-
/// Returns `None` if `shift >= self.bits_precision()`.
120-
///
121-
/// NOTE: this operation is variable time with respect to `shift` *ONLY*.
122-
///
123-
/// When used with a fixed `shift`, this function is constant-time with respect to `self`.
124-
#[inline(always)]
125-
pub fn shr_vartime(&self, shift: u32) -> (Self, ConstChoice) {
126-
let mut result = Self::zero_with_precision(self.bits_precision());
127-
let success = self.shr_vartime_into(&mut result, shift);
128-
// TODO: is this okay?
129-
(
130-
result,
131-
// If success, then return ConstChoice::False since it's not overflowing
132-
success.map_or(ConstChoice::TRUE, |_| ConstChoice::FALSE),
133-
)
134-
}
135-
136118
/// Computes `self >> 1` in constant-time, returning a true [`Choice`]
137119
/// if the least significant bit was set, and a false [`Choice::FALSE`] otherwise.
138120
pub(crate) fn shr1_with_carry(&self) -> (Self, Choice) {
@@ -221,9 +203,9 @@ mod tests {
221203
#[test]
222204
fn shr_vartime() {
223205
let n = BoxedUint::from(0x80000000000000000u128);
224-
assert_eq!(BoxedUint::zero(), n.shr_vartime(68).0);
225-
assert_eq!(BoxedUint::one(), n.shr_vartime(67).0);
226-
assert_eq!(BoxedUint::from(2u8), n.shr_vartime(66).0);
227-
assert_eq!(BoxedUint::from(4u8), n.shr_vartime(65).0);
206+
assert_eq!(BoxedUint::zero(), n.overflowing_shr(68).0);
207+
assert_eq!(BoxedUint::one(), n.overflowing_shr(67).0);
208+
assert_eq!(BoxedUint::from(2u8), n.overflowing_shr(66).0);
209+
assert_eq!(BoxedUint::from(4u8), n.overflowing_shr(65).0);
228210
}
229211
}

tests/boxed_uint_proptests.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
#![cfg(feature = "alloc")]
44

55
use core::cmp::Ordering;
6-
use crypto_bigint::{BoxedUint, CheckedAdd, ConstChoice, Integer, Limb, NonZero};
6+
use crypto_bigint::{BoxedUint, CheckedAdd, Integer, Limb, NonZero};
77
use num_bigint::{BigUint, ModInverse};
88
use num_traits::identities::One;
99
use proptest::prelude::*;
10+
use subtle::Choice;
1011

1112
fn to_biguint(uint: &BoxedUint) -> BigUint {
1213
BigUint::from_bytes_be(&uint.to_be_bytes())
@@ -239,10 +240,10 @@ proptest! {
239240
let shift = u32::from(shift) % (a.bits_precision() * 2);
240241

241242
let expected = to_uint((a_bi << shift as usize) & ((BigUint::one() << a.bits_precision() as usize) - BigUint::one()));
242-
let (actual, overflow) = a.shl_vartime(shift);
243+
let (actual, overflow) = a.overflowing_shl(shift);
243244

244245
if shift >= a.bits_precision() {
245-
assert!(<ConstChoice as Into<bool>>::into(overflow));
246+
assert!(<Choice as Into<bool>>::into(overflow));
246247
}
247248
else {
248249
assert_eq!(expected, actual);
@@ -275,10 +276,10 @@ proptest! {
275276
let shift = u32::from(shift) % (a.bits_precision() * 2);
276277

277278
let expected = to_uint(a_bi >> shift as usize);
278-
let (actual, overflow) = a.shr_vartime(shift);
279+
let (actual, overflow) = a.overflowing_shr(shift);
279280

280281
if shift >= a.bits_precision() {
281-
assert!(<ConstChoice as Into<bool>>::into(overflow));
282+
assert!(<Choice as Into<bool>>::into(overflow));
282283
} else {
283284
assert_eq!(expected, actual);
284285
}

0 commit comments

Comments
 (0)