From bc00f7e58c0c3549cdec8f2922e4038a574f6ef0 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Wed, 22 Nov 2023 13:05:44 -0800 Subject: [PATCH] ec: NFC: Refactor `scalar_sum` to eliminate `LIMBS_add_mod` use. Use the pattern we typically use where one argument is passed by value. This lets us use `limbs_add_assign_mod`, eliminating the `unsafe` direct use of `LIMBS_add_mod`. This will make future refactoring easier. This also eliminates the need to construct and zeroize a new scalar `r` for the result. --- src/ec/suite_b/ecdsa/signing.rs | 2 +- src/ec/suite_b/ops.rs | 31 ++++++++----------------------- 2 files changed, 9 insertions(+), 24 deletions(-) diff --git a/src/ec/suite_b/ecdsa/signing.rs b/src/ec/suite_b/ecdsa/signing.rs index 587086c89..3320937f8 100644 --- a/src/ec/suite_b/ecdsa/signing.rs +++ b/src/ec/suite_b/ecdsa/signing.rs @@ -266,7 +266,7 @@ impl EcdsaKeyPair { // Step 6. let s = { let dr = scalar_ops.scalar_product(&self.d, &r); - let e_plus_dr = scalar_sum(cops, &e, &dr); + let e_plus_dr = scalar_sum(cops, &e, dr); scalar_ops.scalar_product(&k_inv, &e_plus_dr) }; if cops.is_zero(&s) { diff --git a/src/ec/suite_b/ops.rs b/src/ec/suite_b/ops.rs index 579d05e14..eeb15fe8d 100644 --- a/src/ec/suite_b/ops.rs +++ b/src/ec/suite_b/ops.rs @@ -12,7 +12,7 @@ // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -use crate::{arithmetic::limbs_from_hex, arithmetic::montgomery::*, c, error, limb::*}; +use crate::{arithmetic::limbs_from_hex, arithmetic::montgomery::*, error, limb::*}; use core::marker::PhantomData; pub use self::elem::*; @@ -326,18 +326,13 @@ pub fn elem_reduced_to_scalar(ops: &CommonOps, elem: &Elem) -> Scalar } } -pub fn scalar_sum(ops: &CommonOps, a: &Scalar, b: &Scalar) -> Scalar { - let mut r = Scalar::zero(); - unsafe { - LIMBS_add_mod( - r.limbs.as_mut_ptr(), - a.limbs.as_ptr(), - b.limbs.as_ptr(), - ops.n.limbs.as_ptr(), - ops.num_limbs, - ) - } - r +pub fn scalar_sum(ops: &CommonOps, a: &Scalar, mut b: Scalar) -> Scalar { + limbs_add_assign_mod( + &mut b.limbs[..ops.num_limbs], + &a.limbs[..ops.num_limbs], + &ops.n.limbs[..ops.num_limbs], + ); + b } // Returns (`a` squared `squarings` times) * `b`. @@ -425,16 +420,6 @@ fn parse_big_endian_fixed_consttime( Ok(r) } -prefixed_extern! { - fn LIMBS_add_mod( - r: *mut Limb, - a: *const Limb, - b: *const Limb, - m: *const Limb, - num_limbs: c::size_t, - ); -} - #[cfg(test)] mod tests { extern crate alloc;