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 11, 2023
1 parent f7a016e commit 6e5c878
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 @@ -101,7 +101,7 @@ impl BoxedUint {
let bit_size = bits + modulus_bits;

let mut m1hp = modulus.clone();
let (m1hp_new, carry) = m1hp.shr1_with_overflow();
let (m1hp_new, carry) = m1hp.shr1_with_carry();
debug_assert!(bool::from(carry));
m1hp = m1hp_new.wrapping_add(&Self::one_with_precision(bits_precision));

Expand All @@ -124,9 +124,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();
let (new_a, overflow) = a.shr1_with_carry();
debug_assert!(!bool::from(overflow));
let (mut new_u, cy) = new_u.shr1_with_overflow();
let (mut new_u, cy) = new_u.shr1_with_carry();
let cy = new_u.conditional_adc_assign(&m1hp, cy);
debug_assert!(!bool::from(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 @@ -97,7 +97,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
let bit_size = bits + modulus_bits;

let mut m1hp = *modulus;
let (m1hp_new, carry) = m1hp.shr1_with_overflow();
let (m1hp_new, carry) = m1hp.shr1_with_carry();
debug_assert!(carry.is_true_vartime());
m1hp = m1hp_new.wrapping_add(&Uint::ONE);

Expand All @@ -119,9 +119,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();
let (new_a, overflow) = a.shr1_with_carry();
debug_assert!(!overflow.is_true_vartime());
let (new_u, cy) = new_u.shr1_with_overflow();
let (new_u, cy) = new_u.shr1_with_carry();
let (new_u, cy) = new_u.conditional_wrapping_add(&m1hp, cy);
debug_assert!(!cy.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 6e5c878

Please sign in to comment.