Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bigint: Remove redundant ">= 3" check for Modulus. #2184

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,6 @@ fn prefix_all_symbols(pp: char, prefix_prefix: &str, prefix: &str) -> String {
"LIMBS_equal",
"LIMBS_equal_limb",
"LIMBS_less_than",
"LIMBS_less_than_limb",
"LIMBS_reduce_once",
"LIMBS_select_512_32",
"LIMBS_shl_mod",
Expand Down
9 changes: 0 additions & 9 deletions crypto/limbs/limbs.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,6 @@ Limb LIMBS_less_than(const Limb a[], const Limb b[], size_t num_limbs) {
return constant_time_is_nonzero_w(borrow);
}

Limb LIMBS_less_than_limb(const Limb a[], Limb b, size_t num_limbs) {
debug_assert_nonsecret(num_limbs >= 1);

Limb dummy;
Limb lo = constant_time_is_nonzero_w(limb_sub(&dummy, a[0], b));
Limb hi = LIMBS_are_zero(&a[1], num_limbs - 1);
return constant_time_select_w(lo, hi, lo);
}

/* if (r >= m) { r -= m; } */
void LIMBS_reduce_once(Limb r[], const Limb m[], size_t num_limbs) {
debug_assert_nonsecret(num_limbs >= 1);
Expand Down
5 changes: 2 additions & 3 deletions src/arithmetic/bigint/modulusvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ impl<M> OwnedModulusValue<M> {
if n.len() > MODULUS_MAX_LIMBS {
return Err(error::KeyRejected::too_large());
}
const _MODULUS_MIN_LIMBS_AT_LEAST_2: () = assert!(MODULUS_MIN_LIMBS >= 2);
if n.len() < MODULUS_MIN_LIMBS {
return Err(error::KeyRejected::unexpected_error());
}
// The above implies n >= 3, so we don't need to check it.
if limb::limbs_are_even_constant_time(&n).leak() {
return Err(error::KeyRejected::invalid_component());
}
if limb::limbs_less_than_limb_constant_time(&n, 3).leak() {
return Err(error::KeyRejected::unexpected_error());
}

let len_bits = limb::limbs_minimal_bits(&n);

Expand Down
51 changes: 0 additions & 51 deletions src/limb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ pub fn limbs_less_than_limbs_vartime(a: &[Limb], b: &[Limb]) -> bool {
limbs_less_than_limbs_consttime(a, b).leak()
}

#[inline]
#[cfg(feature = "alloc")]
pub fn limbs_less_than_limb_constant_time(a: &[Limb], b: Limb) -> LimbMask {
unsafe { LIMBS_less_than_limb(a.as_ptr(), b, a.len()) }
}

#[inline]
pub fn limbs_are_zero_constant_time(limbs: &[Limb]) -> LimbMask {
unsafe { LIMBS_are_zero(limbs.as_ptr(), limbs.len()) }
Expand Down Expand Up @@ -345,11 +339,6 @@ prefixed_extern! {
fn LIMBS_equal_limb(a: *const Limb, b: Limb, num_limbs: c::size_t) -> LimbMask;
}

#[cfg(feature = "alloc")]
prefixed_extern! {
fn LIMBS_less_than_limb(a: *const Limb, b: Limb, num_limbs: c::size_t) -> LimbMask;
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -478,46 +467,6 @@ mod tests {
}
}

#[test]
#[cfg(feature = "alloc")]
fn test_limbs_less_than_limb_constant_time() {
static LESSER: &[(&[LeakyLimb], LeakyLimb)] = &[
(&[0], 1),
(&[0, 0], 1),
(&[1, 0], 2),
(&[2, 0], 3),
(&[2, 0], 3),
(&[MAX - 1], MAX),
(&[MAX - 1, 0], MAX),
];
for &(a, b) in LESSER {
let a = &Vec::from_iter(a.iter().copied().map(Limb::from));
let b = Limb::from(b);
assert!(leak_in_test(limbs_less_than_limb_constant_time(a, b)));
}
static EQUAL: &[(&[LeakyLimb], LeakyLimb)] = &[
(&[0], 0),
(&[0, 0, 0, 0], 0),
(&[1], 1),
(&[1, 0, 0, 0, 0, 0, 0], 1),
(&[MAX], MAX),
];
static GREATER: &[(&[LeakyLimb], LeakyLimb)] = &[
(&[1], 0),
(&[2, 0], 1),
(&[3, 0, 0, 0], 1),
(&[0, 1, 0, 0], 1),
(&[0, 0, 1, 0], 1),
(&[0, 0, 1, 1], 1),
(&[MAX], MAX - 1),
];
for &(a, b) in EQUAL.iter().chain(GREATER.iter()) {
let a = &Vec::from_iter(a.iter().copied().map(Limb::from));
let b = Limb::from(b);
assert!(!leak_in_test(limbs_less_than_limb_constant_time(a, b)));
}
}

#[test]
fn test_parse_big_endian_and_pad_consttime() {
const LIMBS: usize = 4;
Expand Down