Skip to content

Commit

Permalink
ec/suite_b: Move point_sum() from CommonOps to PrivateKeyOps.
Browse files Browse the repository at this point in the history
Internally, all the operations do use a single point addition
function (per curve) but that's an implementation detail of each
operation.
  • Loading branch information
briansmith committed Dec 11, 2024
1 parent 01742ba commit bdb3859
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
11 changes: 5 additions & 6 deletions src/ec/suite_b/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -241,8 +239,8 @@ impl Modulus<Q> {
}
}

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())
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -980,7 +979,7 @@ mod tests {
let b = consume_jacobian_point(ops, test_case, "b");
let r_expected: TestPoint<R> = 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(())
Expand Down
5 changes: 2 additions & 3 deletions src/ec/suite_b/ops/p256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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<Q>, a: &Elem<R>) -> Elem<R> {
Expand Down Expand Up @@ -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"))]
Expand Down
3 changes: 1 addition & 2 deletions src/ec/suite_b/ops/p384.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R>, PublicElem<R>) = (
Expand All @@ -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<Q>, a: &Elem<R>) -> Elem<R> {
Expand Down

0 comments on commit bdb3859

Please sign in to comment.