Skip to content

Commit 9baba60

Browse files
committed
bigint: Remove redundant ">= 3" check for Modulus.
As we require the modulus to be multiple limbs long, its value cannot be less than 3.
1 parent b9df614 commit 9baba60

File tree

4 files changed

+2
-64
lines changed

4 files changed

+2
-64
lines changed

build.rs

-1
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,6 @@ fn prefix_all_symbols(pp: char, prefix_prefix: &str, prefix: &str) -> String {
855855
"LIMBS_equal",
856856
"LIMBS_equal_limb",
857857
"LIMBS_less_than",
858-
"LIMBS_less_than_limb",
859858
"LIMBS_reduce_once",
860859
"LIMBS_select_512_32",
861860
"LIMBS_shl_mod",

crypto/limbs/limbs.c

-9
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,6 @@ Limb LIMBS_less_than(const Limb a[], const Limb b[], size_t num_limbs) {
8080
return constant_time_is_nonzero_w(borrow);
8181
}
8282

83-
Limb LIMBS_less_than_limb(const Limb a[], Limb b, size_t num_limbs) {
84-
debug_assert_nonsecret(num_limbs >= 1);
85-
86-
Limb dummy;
87-
Limb lo = constant_time_is_nonzero_w(limb_sub(&dummy, a[0], b));
88-
Limb hi = LIMBS_are_zero(&a[1], num_limbs - 1);
89-
return constant_time_select_w(lo, hi, lo);
90-
}
91-
9283
/* if (r >= m) { r -= m; } */
9384
void LIMBS_reduce_once(Limb r[], const Limb m[], size_t num_limbs) {
9485
debug_assert_nonsecret(num_limbs >= 1);

src/arithmetic/bigint/modulusvalue.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,14 @@ impl<M> OwnedModulusValue<M> {
4444
if n.len() > MODULUS_MAX_LIMBS {
4545
return Err(error::KeyRejected::too_large());
4646
}
47+
const _MODULUS_MIN_LIMBS_AT_LEAST_2: () = assert!(MODULUS_MIN_LIMBS >= 2);
4748
if n.len() < MODULUS_MIN_LIMBS {
4849
return Err(error::KeyRejected::unexpected_error());
4950
}
51+
// The above implies n >= 3, so we don't need to check it.
5052
if limb::limbs_are_even_constant_time(&n).leak() {
5153
return Err(error::KeyRejected::invalid_component());
5254
}
53-
if limb::limbs_less_than_limb_constant_time(&n, 3).leak() {
54-
return Err(error::KeyRejected::unexpected_error());
55-
}
5655

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

src/limb.rs

-51
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ pub fn limbs_less_than_limbs_vartime(a: &[Limb], b: &[Limb]) -> bool {
5858
limbs_less_than_limbs_consttime(a, b).leak()
5959
}
6060

61-
#[inline]
62-
#[cfg(feature = "alloc")]
63-
pub fn limbs_less_than_limb_constant_time(a: &[Limb], b: Limb) -> LimbMask {
64-
unsafe { LIMBS_less_than_limb(a.as_ptr(), b, a.len()) }
65-
}
66-
6761
#[inline]
6862
pub fn limbs_are_zero_constant_time(limbs: &[Limb]) -> LimbMask {
6963
unsafe { LIMBS_are_zero(limbs.as_ptr(), limbs.len()) }
@@ -345,11 +339,6 @@ prefixed_extern! {
345339
fn LIMBS_equal_limb(a: *const Limb, b: Limb, num_limbs: c::size_t) -> LimbMask;
346340
}
347341

348-
#[cfg(feature = "alloc")]
349-
prefixed_extern! {
350-
fn LIMBS_less_than_limb(a: *const Limb, b: Limb, num_limbs: c::size_t) -> LimbMask;
351-
}
352-
353342
#[cfg(test)]
354343
mod tests {
355344
use super::*;
@@ -478,46 +467,6 @@ mod tests {
478467
}
479468
}
480469

481-
#[test]
482-
#[cfg(feature = "alloc")]
483-
fn test_limbs_less_than_limb_constant_time() {
484-
static LESSER: &[(&[LeakyLimb], LeakyLimb)] = &[
485-
(&[0], 1),
486-
(&[0, 0], 1),
487-
(&[1, 0], 2),
488-
(&[2, 0], 3),
489-
(&[2, 0], 3),
490-
(&[MAX - 1], MAX),
491-
(&[MAX - 1, 0], MAX),
492-
];
493-
for &(a, b) in LESSER {
494-
let a = &Vec::from_iter(a.iter().copied().map(Limb::from));
495-
let b = Limb::from(b);
496-
assert!(leak_in_test(limbs_less_than_limb_constant_time(a, b)));
497-
}
498-
static EQUAL: &[(&[LeakyLimb], LeakyLimb)] = &[
499-
(&[0], 0),
500-
(&[0, 0, 0, 0], 0),
501-
(&[1], 1),
502-
(&[1, 0, 0, 0, 0, 0, 0], 1),
503-
(&[MAX], MAX),
504-
];
505-
static GREATER: &[(&[LeakyLimb], LeakyLimb)] = &[
506-
(&[1], 0),
507-
(&[2, 0], 1),
508-
(&[3, 0, 0, 0], 1),
509-
(&[0, 1, 0, 0], 1),
510-
(&[0, 0, 1, 0], 1),
511-
(&[0, 0, 1, 1], 1),
512-
(&[MAX], MAX - 1),
513-
];
514-
for &(a, b) in EQUAL.iter().chain(GREATER.iter()) {
515-
let a = &Vec::from_iter(a.iter().copied().map(Limb::from));
516-
let b = Limb::from(b);
517-
assert!(!leak_in_test(limbs_less_than_limb_constant_time(a, b)));
518-
}
519-
}
520-
521470
#[test]
522471
fn test_parse_big_endian_and_pad_consttime() {
523472
const LIMBS: usize = 4;

0 commit comments

Comments
 (0)