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.
This commit is contained in:
Brian Smith 2023-11-22 13:05:44 -08:00
parent 3afbcc5dc5
commit bc00f7e58c
2 changed files with 9 additions and 24 deletions

View File

@ -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) {

View File

@ -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<Unencoded>) -> 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<M>(
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;