Skip to content

Commit

Permalink
Rename sh(r/l)1_with_overflow to *_with_carry
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Dec 12, 2023
1 parent 17145af commit 1640e79
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/modular/div_by_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(crate) fn div_by_2<const LIMBS: usize>(a: &Uint<LIMBS>, modulus: &Uint<LIMBS
// ("+1" because both `a` and `modulus` are odd, we lose 0.5 in each integer division).
// This will not overflow, so we can just use wrapping operations.

let (half, is_odd) = a.shr1_with_overflow();
let (half, is_odd) = a.shr1_with_carry();
let half_modulus = modulus.shr1();

let if_even = half;
Expand Down
6 changes: 3 additions & 3 deletions src/uint/boxed/inv_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ impl BoxedUint {
let cyy = new_u.conditional_adc_assign(modulus, cy);
debug_assert!(bool::from(cy.ct_eq(&cyy)));

let (new_a, overflow) = a.shr1_with_overflow();
debug_assert!(bool::from(!modulus_is_odd | !overflow));
let (mut new_u, cy) = new_u.shr1_with_overflow();
let (new_a, carry) = a.shr1_with_carry();
debug_assert!(bool::from(!modulus_is_odd | !carry));
let (mut new_u, cy) = new_u.shr1_with_carry();
let cy = new_u.conditional_adc_assign(&m1hp, cy);
debug_assert!(bool::from(!modulus_is_odd | !cy));

Expand Down
6 changes: 3 additions & 3 deletions src/uint/boxed/shr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ impl BoxedUint {
success.map(|_| result)
}

/// Computes `self >> 1` in constant-time, returning a true [`Choice`] if the overflowing bit
/// was set, and a false [`Choice::FALSE`] otherwise.
pub(crate) fn shr1_with_overflow(&self) -> (Self, Choice) {
/// Computes `self >> 1` in constant-time, returning a true [`Choice`]
/// if the least significant bit was set, and a false [`Choice::FALSE`] otherwise.
pub(crate) fn shr1_with_carry(&self) -> (Self, Choice) {
let carry = self.limbs[0].0 & 1;
(self.shr1(), Choice::from(carry as u8))
}
Expand Down
6 changes: 3 additions & 3 deletions src/uint/inv_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ impl<const LIMBS: usize> Uint<LIMBS> {
let (new_u, cyy) = new_u.conditional_wrapping_add(modulus, cy);
debug_assert!(cy.is_true_vartime() == cyy.is_true_vartime());

let (new_a, overflow) = a.shr1_with_overflow();
debug_assert!(modulus_is_odd.not().or(overflow.not()).is_true_vartime());
let (new_u, cy) = new_u.shr1_with_overflow();
let (new_a, carry) = a.shr1_with_carry();
debug_assert!(modulus_is_odd.not().or(carry.not()).is_true_vartime());
let (new_u, cy) = new_u.shr1_with_carry();
let (new_u, cy) = new_u.conditional_wrapping_add(&m1hp, cy);
debug_assert!(modulus_is_odd.not().or(cy.not()).is_true_vartime());

Expand Down
8 changes: 4 additions & 4 deletions src/uint/shl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ impl<const LIMBS: usize> Uint<LIMBS> {
(Uint::<LIMBS>::new(limbs), Limb(carry))
}

/// Computes `self << 1` in constant-time, returning [`CtChoice::TRUE`] if the overflowing bit
/// was set, and [`CtChoice::FALSE`] otherwise.
/// Computes `self << 1` in constant-time, returning [`CtChoice::TRUE`]
/// if the most significant bit was set, and [`CtChoice::FALSE`] otherwise.
#[inline(always)]
pub(crate) const fn shl1_with_overflow(&self) -> (Self, CtChoice) {
pub(crate) const fn shl1_with_carry(&self) -> (Self, CtChoice) {
let mut ret = Self::ZERO;
let mut i = 0;
let mut carry = Limb::ZERO;
Expand All @@ -138,7 +138,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
/// Computes `self << 1` in constant-time.
pub(crate) const fn shl1(&self) -> Self {
// TODO(tarcieri): optimized implementation
self.shl1_with_overflow().0
self.shl1_with_carry().0
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/uint/shr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ impl<const LIMBS: usize> Uint<LIMBS> {
}
}

/// Computes `self >> 1` in constant-time, returning [`CtChoice::TRUE`] if the overflowing bit
/// was set, and [`CtChoice::FALSE`] otherwise.
/// Computes `self >> 1` in constant-time, returning [`CtChoice::TRUE`]
/// if the least significant bit was set, and [`CtChoice::FALSE`] otherwise.
#[inline(always)]
pub(crate) const fn shr1_with_overflow(&self) -> (Self, CtChoice) {
pub(crate) const fn shr1_with_carry(&self) -> (Self, CtChoice) {
let mut ret = Self::ZERO;
let mut i = LIMBS;
let mut carry = Limb::ZERO;
Expand All @@ -113,7 +113,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
/// Computes `self >> 1` in constant-time.
pub(crate) const fn shr1(&self) -> Self {
// TODO(tarcieri): optimized implementation
self.shr1_with_overflow().0
self.shr1_with_carry().0
}
}

Expand Down

0 comments on commit 1640e79

Please sign in to comment.