From 357665bed393eb489c12dcf6f56b0ea0b8b075a8 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 | 9 ++++----- src/ec/suite_b/ops/p256.rs | 5 ++--- src/ec/suite_b/ops/p384.rs | 3 +-- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/ec/suite_b/ops.rs b/src/ec/suite_b/ops.rs index 2f10fcc3f5..5951686006 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,7 +239,7 @@ impl Modulus { } } -impl CommonOps { +impl PrivateKeyOps { pub(super) fn point_sum(&self, a: &Point, b: &Point, _cpu: cpu::Features) -> Point { let mut r = Point::new_at_infinity(); unsafe { @@ -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 20ede07cca..61b14dda05 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 c4ab61a49f..6950be9879 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 {