From 4c09b811fafea006ff9f064ad393321041de45ed Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Tue, 28 Nov 2023 09:58:43 -0700 Subject: [PATCH] Assert `Boxed*` size equivalance using `bits_precision` (#344) Several functions on the `Boxed*` types don't yet support implicit widening (#312) and will panic if two or more operands are not the same size (NOTE: we should eventually fix this) Some of these functions previously had debug asserts that the number of limbs are equal, however that's less helpful information when trying to debug these problems than the precision in bits. This changes all of the assertions for size equality to use `bits_precision`. --- src/modular/boxed_residue/mul.rs | 4 ++-- src/uint/boxed.rs | 2 +- src/uint/boxed/add_mod.rs | 4 ++-- src/uint/boxed/div.rs | 2 +- src/uint/boxed/sub_mod.rs | 8 ++++---- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/modular/boxed_residue/mul.rs b/src/modular/boxed_residue/mul.rs index 7636e10c..0b5e1c0c 100644 --- a/src/modular/boxed_residue/mul.rs +++ b/src/modular/boxed_residue/mul.rs @@ -95,8 +95,8 @@ pub(super) fn mul_montgomery_form( modulus: &BoxedUint, mod_neg_inv: Limb, ) -> BoxedUint { - debug_assert_eq!(a.nlimbs(), modulus.nlimbs()); - debug_assert_eq!(b.nlimbs(), modulus.nlimbs()); + debug_assert_eq!(a.bits_precision(), modulus.bits_precision()); + debug_assert_eq!(b.bits_precision(), modulus.bits_precision()); let mut product = a.mul_wide(b); let ret = montgomery_reduction_boxed(&mut product, modulus, mod_neg_inv); diff --git a/src/uint/boxed.rs b/src/uint/boxed.rs index 736dbb8d..1768b48f 100644 --- a/src/uint/boxed.rs +++ b/src/uint/boxed.rs @@ -196,7 +196,7 @@ impl BoxedUint { /// /// Panics if `a` and `b` don't have the same precision. pub fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { - debug_assert_eq!(a.nlimbs(), b.nlimbs()); + debug_assert_eq!(a.bits_precision(), b.bits_precision()); let mut limbs = vec![Limb::ZERO; a.nlimbs()].into_boxed_slice(); for i in 0..a.nlimbs() { diff --git a/src/uint/boxed/add_mod.rs b/src/uint/boxed/add_mod.rs index 953d992c..b1011530 100644 --- a/src/uint/boxed/add_mod.rs +++ b/src/uint/boxed/add_mod.rs @@ -7,8 +7,8 @@ impl BoxedUint { /// /// Assumes `self + rhs` as unbounded integer is `< 2p`. pub fn add_mod(&self, rhs: &Self, p: &Self) -> Self { - debug_assert_eq!(self.nlimbs(), p.nlimbs()); - debug_assert_eq!(rhs.nlimbs(), p.nlimbs()); + debug_assert_eq!(self.bits_precision(), p.bits_precision()); + debug_assert_eq!(rhs.bits_precision(), p.bits_precision()); debug_assert!(self < p); debug_assert!(rhs < p); diff --git a/src/uint/boxed/div.rs b/src/uint/boxed/div.rs index b9a70025..f3275fdd 100644 --- a/src/uint/boxed/div.rs +++ b/src/uint/boxed/div.rs @@ -13,7 +13,7 @@ impl BoxedUint { /// Panics if `self` and `rhs` have different precisions. // TODO(tarcieri): handle different precisions without panicking pub fn rem_vartime(&self, rhs: &NonZero) -> Self { - debug_assert_eq!(self.nlimbs(), rhs.nlimbs()); + debug_assert_eq!(self.bits_precision(), rhs.bits_precision()); let mb = rhs.bits(); let mut bd = self.bits_precision() - mb; let mut rem = self.clone(); diff --git a/src/uint/boxed/sub_mod.rs b/src/uint/boxed/sub_mod.rs index 6f4420ef..a93862ab 100644 --- a/src/uint/boxed/sub_mod.rs +++ b/src/uint/boxed/sub_mod.rs @@ -7,8 +7,8 @@ impl BoxedUint { /// /// Assumes `self - rhs` as unbounded signed integer is in `[-p, p)`. pub fn sub_mod(&self, rhs: &Self, p: &Self) -> Self { - debug_assert_eq!(self.nlimbs(), p.nlimbs()); - debug_assert_eq!(rhs.nlimbs(), p.nlimbs()); + debug_assert_eq!(self.bits_precision(), p.bits_precision()); + debug_assert_eq!(rhs.bits_precision(), p.bits_precision()); debug_assert!(self < p); debug_assert!(rhs < p); @@ -23,8 +23,8 @@ impl BoxedUint { /// Assumes `-(p...) <= (self..., carry) - (rhs...) < (p...)`. #[inline(always)] pub(crate) fn sub_mod_with_carry(&self, carry: Limb, rhs: &Self, p: &Self) -> Self { - debug_assert_eq!(self.nlimbs(), p.nlimbs()); - debug_assert_eq!(rhs.nlimbs(), p.nlimbs()); + debug_assert_eq!(self.bits_precision(), p.bits_precision()); + debug_assert_eq!(rhs.bits_precision(), p.bits_precision()); debug_assert!(carry.0 <= 1); let (out, borrow) = self.sbb(rhs, Limb::ZERO);