Skip to content

Commit 124a7cb

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 32cbeec commit 124a7cb

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
@@ -250,36 +250,36 @@ impl<M> Uninit<M> {
250250
}
251251
}
252252

253-
// TODO: Document why this works for all Montgomery factors.
254-
pub fn elem_add<M, E>(mut a: Elem<M, E>, b: Elem<M, E>, m: &Mont<M>) -> Elem<M, E> {
255-
limb::limbs_add_assign_mod(a.limbs.as_mut(), b.limbs.as_ref(), m.limbs())
256-
.unwrap_or_else(unwrap_impossible_len_mismatch_error);
257-
a
258-
}
253+
impl<M, E> Elem<M, E> {
254+
pub fn add(mut self, b: &Elem<M, E>, m: &Mont<M>) -> Elem<M, E> {
255+
limb::limbs_add_assign_mod(self.limbs.as_mut(), b.limbs.as_ref(), m.limbs())
256+
.unwrap_or_else(unwrap_impossible_len_mismatch_error);
257+
self
258+
}
259259

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

285285
/// 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)