-
Notifications
You must be signed in to change notification settings - Fork 709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attempt to migrate away from bn_mul_mont into Rust #1278
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1166,7 +1166,7 @@ fn greater_than(a: &Nonnegative, b: &Nonnegative) -> bool { | |
|
||
#[derive(Clone)] | ||
#[repr(transparent)] | ||
struct N0([Limb; 2]); | ||
pub(crate) struct N0(pub(crate) [Limb; 2]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of making the inner field |
||
|
||
const N0_LIMBS_USED: usize = 64 / LIMB_BITS; | ||
|
||
|
@@ -1186,7 +1186,7 @@ impl From<u64> for N0 { | |
} | ||
|
||
/// r *= a | ||
fn limbs_mont_mul(r: &mut [Limb], a: &[Limb], m: &[Limb], n0: &N0) { | ||
pub(crate) fn limbs_mont_mul(r: &mut [Limb], a: &[Limb], m: &[Limb], n0: &N0) { | ||
debug_assert_eq!(r.len(), m.len()); | ||
debug_assert_eq!(a.len(), m.len()); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,7 +66,7 @@ pub struct CommonOps { | |
pub b: Elem<R>, | ||
|
||
// 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_mul_mont: fn(r: &mut [Limb], a: &[Limb], b: &[Limb]), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll have to leave this as it was so that the P256 implementation can directly call the assembly/C code without any additional overhead. |
||
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), | ||
|
@@ -225,9 +225,9 @@ impl PublicKeyOps { | |
// TODO: do something about this. | ||
unsafe { | ||
(self.common.elem_mul_mont)( | ||
r.limbs.as_mut_ptr(), | ||
parsed.limbs.as_ptr(), | ||
self.common.q.rr.as_ptr(), | ||
&mut r.limbs, | ||
&parsed.limbs, | ||
&self.common.q.rr, | ||
) | ||
} | ||
Ok(r) | ||
|
@@ -240,7 +240,7 @@ pub struct ScalarOps { | |
pub common: &'static CommonOps, | ||
|
||
scalar_inv_to_mont_impl: fn(a: &Scalar) -> Scalar<R>, | ||
scalar_mul_mont: unsafe extern "C" fn(r: *mut Limb, a: *const Limb, b: *const Limb), | ||
scalar_mul_mont: fn(r: &mut [Limb], a: &[Limb], b: &[Limb]), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto: We'll have to leave this as it was so that the P256 implementation can directly call the assembly/C code without any additional overhead. |
||
} | ||
|
||
impl ScalarOps { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ use super::{ | |
elem::{binary_op, binary_op_assign}, | ||
elem_sqr_mul, elem_sqr_mul_acc, Modulus, *, | ||
}; | ||
use crate::arithmetic::bigint::{limbs_mont_mul, N0}; | ||
use core::marker::PhantomData; | ||
|
||
macro_rules! p256_limbs { | ||
|
@@ -64,7 +65,7 @@ pub static COMMON_OPS: CommonOps = CommonOps { | |
encoding: PhantomData, // R | ||
}, | ||
|
||
elem_mul_mont: p256_mul_mont, | ||
elem_mul_mont: p256_mul_mont_rs, | ||
elem_sqr_mont: p256_sqr_mont, | ||
|
||
point_add_jacobian_impl: p256_point_add, | ||
|
@@ -190,22 +191,22 @@ fn p256_scalar_inv_to_mont(a: &Scalar<Unencoded>) -> Scalar<R> { | |
#[inline] | ||
fn sqr(a: &Scalar<R>) -> Scalar<R> { | ||
let mut tmp = Scalar::zero(); | ||
unsafe { p256_scalar_sqr_rep_mont(tmp.limbs.as_mut_ptr(), a.limbs.as_ptr(), 1) } | ||
p256_scalar_sqr_rep_mont(&mut tmp.limbs, &a.limbs, 1); | ||
tmp | ||
} | ||
|
||
// Returns (`a` squared `squarings` times) * `b`. | ||
fn sqr_mul(a: &Scalar<R>, squarings: Limb, b: &Scalar<R>) -> Scalar<R> { | ||
debug_assert!(squarings >= 1); | ||
let mut tmp = Scalar::zero(); | ||
unsafe { p256_scalar_sqr_rep_mont(tmp.limbs.as_mut_ptr(), a.limbs.as_ptr(), squarings) } | ||
p256_scalar_sqr_rep_mont(&mut tmp.limbs, &a.limbs, squarings); | ||
mul(&tmp, b) | ||
} | ||
|
||
// Sets `acc` = (`acc` squared `squarings` times) * `b`. | ||
fn sqr_mul_acc(acc: &mut Scalar<R>, squarings: Limb, b: &Scalar<R>) { | ||
debug_assert!(squarings >= 1); | ||
unsafe { p256_scalar_sqr_rep_mont(acc.limbs.as_mut_ptr(), acc.limbs.as_ptr(), squarings) } | ||
p256_scalar_sqr_rep_mont(&mut acc.limbs, &acc.limbs, squarings); | ||
binary_op_assign(p256_scalar_mul_mont, acc, b); | ||
} | ||
|
||
|
@@ -297,6 +298,23 @@ fn p256_scalar_inv_to_mont(a: &Scalar<Unencoded>) -> Scalar<R> { | |
acc | ||
} | ||
|
||
const N_N0: N0 = N0([0xccd1c8aa, 0xee00bc4f]); | ||
fn p256_scalar_mul_mont(r: &mut [Limb], a: &[Limb], b: &[Limb]) { | ||
limbs_mont_mul(r, a, b, &N_N0) | ||
} | ||
|
||
fn p256_scalar_sqr_rep_mont(r: &mut [Limb], a: &[Limb], rep: Limb) { | ||
debug_assert!(rep >= 1); | ||
p256_scalar_mul_mont(r, a, a); | ||
for i in 1..rep { | ||
p256_scalar_mul_mont(r, r, r); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doing this would regress the performance on x86-64 and AArch64 targets that have an optimized implementation of this. |
||
|
||
fn p256_mul_mont_rs(r: &mut [Limb], a: &[Limb], b: &[Limb]) { | ||
unsafe { p256_mul_mont(r.as_mut_ptr(), a.as_ptr(), b.as_ptr()) } | ||
} | ||
|
||
prefixed_extern! { | ||
pub(super) fn p256_mul_mont( | ||
r: *mut Limb, // [COMMON_OPS.num_limbs] | ||
|
@@ -319,15 +337,4 @@ prefixed_extern! { | |
p_x: *const Limb, // [COMMON_OPS.num_limbs] | ||
p_y: *const Limb, // [COMMON_OPS.num_limbs] | ||
); | ||
|
||
fn p256_scalar_mul_mont( | ||
r: *mut Limb, // [COMMON_OPS.num_limbs] | ||
a: *const Limb, // [COMMON_OPS.num_limbs] | ||
b: *const Limb, // [COMMON_OPS.num_limbs] | ||
); | ||
fn p256_scalar_sqr_rep_mont( | ||
r: *mut Limb, // [COMMON_OPS.num_limbs] | ||
a: *const Limb, // [COMMON_OPS.num_limbs] | ||
rep: Limb, | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
\o/