Skip to content

Commit

Permalink
Remove _constant_time suffixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Feb 25, 2025
1 parent 31d55aa commit a5018e1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/arithmetic/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl<M, E> Elem<M, E> {
impl<M, E> Elem<M, E> {
#[inline]
pub fn is_zero(&self) -> bool {
limb::limbs_are_zero_constant_time(&self.limbs).leak()
limb::limbs_are_zero(&self.limbs).leak()
}
}

Expand Down Expand Up @@ -214,7 +214,7 @@ pub fn elem_reduced_once<A, M>(
) -> Elem<M, Unencoded> {
assert_eq!(m.len_bits(), other_modulus_len_bits);
r.limbs.copy_from_slice(&a.limbs);
limb::limbs_reduce_once_constant_time(&mut r.limbs, m.limbs())
limb::limbs_reduce_once(&mut r.limbs, m.limbs())
.unwrap_or_else(unwrap_impossible_len_mismatch_error);
Elem {
limbs: r.limbs,
Expand Down
6 changes: 3 additions & 3 deletions src/ec/suite_b/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl CommonOps {
#[inline]
fn is_zero<M, E: Encoding>(&self, a: &elem::Elem<M, E>) -> bool {
let num_limbs = self.num_limbs.into();
limbs_are_zero_constant_time(&a.limbs[..num_limbs]).leak()
limbs_are_zero(&a.limbs[..num_limbs]).leak()
}

#[inline]
Expand Down Expand Up @@ -494,7 +494,7 @@ impl Modulus<N> {
pub fn elem_reduced_to_scalar(&self, elem: &Elem<Unencoded>) -> Scalar<Unencoded> {
let num_limbs = self.num_limbs.into();
let mut r_limbs = elem.limbs;
limbs_reduce_once_constant_time(&mut r_limbs[..num_limbs], &self.limbs[..num_limbs])
limbs_reduce_once(&mut r_limbs[..num_limbs], &self.limbs[..num_limbs])
.unwrap_or_else(unwrap_impossible_len_mismatch_error);
Scalar {
limbs: r_limbs,
Expand Down Expand Up @@ -577,7 +577,7 @@ pub(super) fn scalar_parse_big_endian_partially_reduced_variable_consttime(
{
let r = &mut r.limbs[..num_limbs];
parse_big_endian_and_pad_consttime(bytes, r)?;
limbs_reduce_once_constant_time(r, &n.limbs[..num_limbs])
limbs_reduce_once(r, &n.limbs[..num_limbs])
.unwrap_or_else(unwrap_impossible_len_mismatch_error);
}

Expand Down
26 changes: 13 additions & 13 deletions src/limb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ pub fn limbs_equal_limbs_consttime(a: &[Limb], b: &[Limb]) -> Result<LimbMask, L
return Err(LenMismatchError::new(a.len()));
}
let all = a.iter().zip(b).fold(0, |running, (a, b)| running | (a ^ b));
Ok(limb_is_zero_constant_time(all))
Ok(limb_is_zero(all))
}

#[inline]
fn limbs_less_than_limbs_constant_time(
fn limbs_less_than_limbs(
a: &[Limb],
b: &[Limb],
) -> Result<LimbMask, LenMismatchError> {
Expand All @@ -74,7 +74,7 @@ pub(crate) fn verify_limbs_less_than_limbs_leak_bit(
a: &[Limb],
b: &[Limb],
) -> Result<(), error::Unspecified> {
let r = limbs_less_than_limbs_constant_time(a, b).map_err(error::erase::<LenMismatchError>)?;
let r = limbs_less_than_limbs(a, b).map_err(error::erase::<LenMismatchError>)?;
if r.leak() {
Ok(())
} else {
Expand All @@ -84,21 +84,21 @@ pub(crate) fn verify_limbs_less_than_limbs_leak_bit(

#[inline]
pub fn limbs_less_than_limbs_vartime(a: &[Limb], b: &[Limb]) -> Result<bool, LenMismatchError> {
let r = limbs_less_than_limbs_constant_time(a, b)?;
let r = limbs_less_than_limbs(a, b)?;
Ok(r.leak())
}

#[inline]
fn limb_is_zero_constant_time(limb: Limb) -> LimbMask {
fn limb_is_zero(limb: Limb) -> LimbMask {
prefixed_extern! {
fn LIMB_is_zero(limb: Limb) -> LimbMask;
}
unsafe { LIMB_is_zero(limb) }
}

#[inline]
pub fn limbs_are_zero_constant_time(limbs: &[Limb]) -> LimbMask {
limb_is_zero_constant_time(limbs.iter().fold(0, |a, b| a | b))
pub fn limbs_are_zero(limbs: &[Limb]) -> LimbMask {
limb_is_zero(limbs.iter().fold(0, |a, b| a | b))
}

/// Leaks one bit of information (other than the lengths of the inputs):
Expand All @@ -107,7 +107,7 @@ pub fn limbs_are_zero_constant_time(limbs: &[Limb]) -> LimbMask {
#[inline]
pub fn limbs_reject_even_leak_bit(limbs: &[Limb]) -> Result<(), error::Unspecified> {
let bottom = *limbs.first().ok_or(error::Unspecified)?;
if limb_is_zero_constant_time(bottom & 1).leak() {
if limb_is_zero(bottom & 1).leak() {
return Err(error::Unspecified);
}
Ok(())
Expand All @@ -117,7 +117,7 @@ pub fn limbs_reject_even_leak_bit(limbs: &[Limb]) -> Result<(), error::Unspecifi
#[inline]
pub fn verify_limbs_equal_1_leak_bit(a: &[Limb]) -> Result<(), error::Unspecified> {
if let [bottom, ref rest @ ..] = *a {
let equal = limb_is_zero_constant_time(bottom ^ 1) & limbs_are_zero_constant_time(rest);
let equal = limb_is_zero(bottom ^ 1) & limbs_are_zero(rest);
if equal.leak() {
return Ok(());
}
Expand Down Expand Up @@ -157,7 +157,7 @@ pub fn limbs_minimal_bits(a: &[Limb]) -> bits::BitLength {

/// Equivalent to `if (r >= m) { r -= m; }`
#[inline]
pub fn limbs_reduce_once_constant_time(r: &mut [Limb], m: &[Limb]) -> Result<(), LenMismatchError> {
pub fn limbs_reduce_once(r: &mut [Limb], m: &[Limb]) -> Result<(), LenMismatchError> {
prefixed_extern! {
fn LIMBS_reduce_once(r: *mut Limb, m: *const Limb, num_limbs: c::NonZero_size_t);
}
Expand Down Expand Up @@ -191,7 +191,7 @@ pub fn parse_big_endian_in_range_and_pad_consttime(
parse_big_endian_and_pad_consttime(input, result)?;
verify_limbs_less_than_limbs_leak_bit(result, max_exclusive)?;
if allow_zero != AllowZero::Yes {
if limbs_are_zero_constant_time(result).leak() {
if limbs_are_zero(result).leak() {
return Err(error::Unspecified);
}
}
Expand Down Expand Up @@ -457,11 +457,11 @@ mod tests {
fn test_limbs_are_zero() {
for zero in ZEROES {
let zero = &Vec::from_iter(zero.iter().copied().map(Limb::from));
assert!(leak_in_test(limbs_are_zero_constant_time(zero)));
assert!(leak_in_test(limbs_are_zero(zero)));
}
for nonzero in NONZEROES {
let nonzero = &Vec::from_iter(nonzero.iter().copied().map(Limb::from));
assert!(!leak_in_test(limbs_are_zero_constant_time(nonzero)));
assert!(!leak_in_test(limbs_are_zero(nonzero)));
}
}

Expand Down

0 comments on commit a5018e1

Please sign in to comment.