Skip to content

Commit 5ff9af8

Browse files
committed
arithmetic: Make Elem::{add,sub} methods.
Make these methods instead of a freestanding function to make it easier to read/write code that uses them, but mostly to be consistent with other similar changes.
1 parent 48bc92c commit 5ff9af8

File tree

3 files changed

+31
-31
lines changed

3 files changed

+31
-31
lines changed

src/arithmetic/bigint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use crate::{
4848
pub(crate) use {
4949
self::{
5050
boxed_limbs::Uninit,
51-
elem::{elem_add, elem_sub, elem_verify_equal_consttime, verify_inverses_consttime, Elem},
51+
elem::{elem_verify_equal_consttime, verify_inverses_consttime, Elem},
5252
exp::elem_exp_consttime,
5353
modulus::{BoxedIntoMont, IntoMont, Mont, One},
5454
oversized_uninit::OversizedUninit,

src/arithmetic/bigint/elem.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -244,36 +244,36 @@ impl<M> Uninit<M> {
244244
}
245245
}
246246

247-
// TODO: Document why this works for all Montgomery factors.
248-
pub fn elem_add<M, E>(mut a: Elem<M, E>, b: Elem<M, E>, m: &Mont<M>) -> Elem<M, E> {
249-
limb::limbs_add_assign_mod(a.limbs.as_mut(), b.limbs.as_ref(), m.limbs())
250-
.unwrap_or_else(unwrap_impossible_len_mismatch_error);
251-
a
252-
}
247+
impl<M, E> Elem<M, E> {
248+
pub fn add(mut self, b: &Elem<M, E>, m: &Mont<M>) -> Elem<M, E> {
249+
limb::limbs_add_assign_mod(self.limbs.as_mut(), b.limbs.as_ref(), m.limbs())
250+
.unwrap_or_else(unwrap_impossible_len_mismatch_error);
251+
self
252+
}
253253

254-
// TODO: Document why this works for all Montgomery factors.
255-
pub fn elem_sub<M, E>(mut a: Elem<M, E>, b: &Elem<M, E>, m: &Mont<M>) -> Elem<M, E> {
256-
prefixed_extern! {
257-
// `r` and `a` may alias.
258-
fn LIMBS_sub_mod(
259-
r: *mut Limb,
260-
a: *const Limb,
261-
b: *const Limb,
262-
m: *const Limb,
263-
num_limbs: c::NonZero_size_t,
264-
);
254+
pub fn sub(mut self, b: &Elem<M, E>, m: &Mont<M>) -> Elem<M, E> {
255+
prefixed_extern! {
256+
// `r` and `a` may alias.
257+
fn LIMBS_sub_mod(
258+
r: *mut Limb,
259+
a: *const Limb,
260+
b: *const Limb,
261+
m: *const Limb,
262+
num_limbs: c::NonZero_size_t,
263+
);
264+
}
265+
let num_limbs = NonZeroUsize::new(m.limbs().len()).unwrap();
266+
let _: &[Limb] = (InOut(self.limbs.as_mut()), b.limbs.as_ref())
267+
.with_non_dangling_non_null_pointers(num_limbs, |mut r, [a, b]| {
268+
let m = m.limbs().as_ptr(); // Also non-dangling because num_limbs is non-zero.
269+
unsafe {
270+
LIMBS_sub_mod(r.start_mut_ptr(), a, b, m, num_limbs);
271+
r.deref_unchecked().assume_init()
272+
}
273+
})
274+
.unwrap_or_else(unwrap_impossible_len_mismatch_error);
275+
self
265276
}
266-
let num_limbs = NonZeroUsize::new(m.limbs().len()).unwrap();
267-
let _: &[Limb] = (InOut(a.limbs.as_mut()), b.limbs.as_ref())
268-
.with_non_dangling_non_null_pointers(num_limbs, |mut r, [a, b]| {
269-
let m = m.limbs().as_ptr(); // Also non-dangling because num_limbs is non-zero.
270-
unsafe {
271-
LIMBS_sub_mod(r.start_mut_ptr(), a, b, m, num_limbs);
272-
r.deref_unchecked().assume_init()
273-
}
274-
})
275-
.unwrap_or_else(unwrap_impossible_len_mismatch_error);
276-
a
277277
}
278278

279279
/// Verified a == b**-1 (mod m), i.e. a**-1 == b (mod m).

src/rsa/keypair.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ impl KeyPair {
626626
let h = {
627627
let pm = &p.modulus(cpu_features);
628628
let m_2 = pm.alloc_uninit().elem_reduced_once(&m_2, pm, q.len_bits());
629-
bigint::elem_sub(m_1, &m_2, pm).mul(&self.qInv, pm)
629+
m_1.sub(&m_2, pm).mul(&self.qInv, pm)
630630
};
631631

632632
// Step 2.b.iv. The reduction in the modular multiplication isn't
@@ -643,7 +643,7 @@ impl KeyPair {
643643
.encode_mont(n, cpu_features)
644644
.mul(&h, nm);
645645
let m_2 = nm.alloc_uninit().elem_widen(&m_2, nm, q.len_bits())?;
646-
let m = bigint::elem_add(m_2, q_times_h, nm);
646+
let m = m_2.add(&q_times_h, nm);
647647

648648
// Step 2.b.v isn't needed since there are only two primes.
649649

0 commit comments

Comments
 (0)