From b9df614591b28840994768a56a7c086a741b6f57 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Tue, 10 Dec 2024 16:21:53 -0800 Subject: [PATCH] ec/suite_b: Move `point_sum()` from `CommonOps` to `PrivateKeyOps`. Internally, all the operations do use a single point addition function (per curve) but that's an implementation detail of each operation. --- src/ec/suite_b/ops.rs | 11 +++++------ src/ec/suite_b/ops/p256.rs | 5 ++--- src/ec/suite_b/ops/p384.rs | 3 +-- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/ec/suite_b/ops.rs b/src/ec/suite_b/ops.rs index e8afb9f47..7c36c8a70 100644 --- a/src/ec/suite_b/ops.rs +++ b/src/ec/suite_b/ops.rs @@ -76,8 +76,6 @@ pub struct CommonOps { // In all cases, `r`, `a`, and `b` may all alias each other. elem_mul_mont: unsafe extern "C" fn(r: *mut Limb, a: *const Limb, b: *const Limb), elem_sqr_mont: unsafe extern "C" fn(r: *mut Limb, a: *const Limb), - - point_add_jacobian_impl: unsafe extern "C" fn(r: *mut Limb, a: *const Limb, b: *const Limb), } impl CommonOps { @@ -241,8 +239,8 @@ impl Modulus { } } -impl CommonOps { - fn point_sum(&self, a: &Point, b: &Point, _cpu: cpu::Features) -> Point { +impl PrivateKeyOps { + pub(super) fn point_sum(&self, a: &Point, b: &Point, _cpu: cpu::Features) -> Point { let mut r = Point::new_at_infinity(); unsafe { (self.point_add_jacobian_impl)(r.xyz.as_mut_ptr(), a.xyz.as_ptr(), b.xyz.as_ptr()) @@ -290,6 +288,7 @@ pub struct PrivateKeyOps { p_x: *const Limb, // [num_limbs] p_y: *const Limb, // [num_limbs] ), + point_add_jacobian_impl: unsafe extern "C" fn(r: *mut Limb, a: *const Limb, b: *const Limb), } impl PrivateKeyOps { @@ -486,7 +485,7 @@ fn twin_mul_inefficient( ) -> Point { let scaled_g = ops.point_mul_base(g_scalar, cpu); let scaled_p = ops.point_mul(p_scalar, p_xy, cpu); - ops.common.point_sum(&scaled_g, &scaled_p, cpu) + ops.point_sum(&scaled_g, &scaled_p, cpu) } // This assumes n < q < 2*n. @@ -980,7 +979,7 @@ mod tests { let b = consume_jacobian_point(ops, test_case, "b"); let r_expected: TestPoint = consume_point(ops, test_case, "r"); - let r_actual = ops.common.point_sum(&a, &b, cpu); + let r_actual = ops.point_sum(&a, &b, cpu); assert_point_actual_equals_expected(ops, &r_actual, &r_expected); Ok(()) diff --git a/src/ec/suite_b/ops/p256.rs b/src/ec/suite_b/ops/p256.rs index 20ede07cc..61b14dda0 100644 --- a/src/ec/suite_b/ops/p256.rs +++ b/src/ec/suite_b/ops/p256.rs @@ -33,8 +33,6 @@ pub static COMMON_OPS: CommonOps = CommonOps { elem_mul_mont: p256_mul_mont, elem_sqr_mont: p256_sqr_mont, - - point_add_jacobian_impl: p256_point_add, }; #[cfg(test)] @@ -48,6 +46,7 @@ pub static PRIVATE_KEY_OPS: PrivateKeyOps = PrivateKeyOps { elem_inv_squared: p256_elem_inv_squared, point_mul_base_impl: p256_point_mul_base_impl, point_mul_impl: p256_point_mul, + point_add_jacobian_impl: p256_point_add, }; fn p256_elem_inv_squared(q: &Modulus, a: &Elem) -> Elem { @@ -146,7 +145,7 @@ fn twin_mul_nistz256( ) -> Point { let scaled_g = point_mul_base_vartime(g_scalar, cpu); let scaled_p = PRIVATE_KEY_OPS.point_mul(p_scalar, p_xy, cpu::features()); - PRIVATE_KEY_OPS.common.point_sum(&scaled_g, &scaled_p, cpu) + PRIVATE_KEY_OPS.point_sum(&scaled_g, &scaled_p, cpu) } #[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] diff --git a/src/ec/suite_b/ops/p384.rs b/src/ec/suite_b/ops/p384.rs index c4ab61a49..6950be987 100644 --- a/src/ec/suite_b/ops/p384.rs +++ b/src/ec/suite_b/ops/p384.rs @@ -33,8 +33,6 @@ pub static COMMON_OPS: CommonOps = CommonOps { elem_mul_mont: p384_elem_mul_mont, elem_sqr_mont: p384_elem_sqr_mont, - - point_add_jacobian_impl: p384_point_add, }; pub(super) static GENERATOR: (PublicElem, PublicElem) = ( @@ -47,6 +45,7 @@ pub static PRIVATE_KEY_OPS: PrivateKeyOps = PrivateKeyOps { elem_inv_squared: p384_elem_inv_squared, point_mul_base_impl: p384_point_mul_base_impl, point_mul_impl: p384_point_mul, + point_add_jacobian_impl: p384_point_add, }; fn p384_elem_inv_squared(q: &Modulus, a: &Elem) -> Elem {