Skip to content

Commit

Permalink
Assert Boxed* size equivalance using bits_precision (#344)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
tarcieri authored Nov 28, 2023
1 parent 2038768 commit 4c09b81
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/modular/boxed_residue/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/uint/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions src/uint/boxed/add_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/uint/boxed/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>) -> 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();
Expand Down
8 changes: 4 additions & 4 deletions src/uint/boxed/sub_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down

0 comments on commit 4c09b81

Please sign in to comment.