mirror of
https://github.com/saymrwulf/betrusted-curve25519-dalek-source.git
synced 2026-09-08 21:00:38 +00:00
cleanup to refer to local job primitives
This commit is contained in:
parent
ebcc3702d3
commit
4c58a5166f
1 changed files with 310 additions and 366 deletions
|
|
@ -329,7 +329,7 @@ impl ProjectivePoint {
|
||||||
pub fn as_affine(&self) -> MontgomeryPoint {
|
pub fn as_affine(&self) -> MontgomeryPoint {
|
||||||
//TODO: consider making this a seperate feature. Something like "panic_on_sw_eval" which would
|
//TODO: consider making this a seperate feature. Something like "panic_on_sw_eval" which would
|
||||||
//be ameniable to upstreaming
|
//be ameniable to upstreaming
|
||||||
#[cfg(all(not(test),curve25519_dalek_backend = "u32e_backend"))] // due to issue https://github.com/rust-lang/rust/issues/59168, you will have to manually comment this out when running a test on the full system and not just this crate.
|
#[cfg(all(not(test), curve25519_dalek_backend = "u32e_backend"))] // due to issue https://github.com/rust-lang/rust/issues/59168, you will have to manually comment this out when running a test on the full system and not just this crate.
|
||||||
log::warn!("sw as_affine being used - check for build config errors!");
|
log::warn!("sw as_affine being used - check for build config errors!");
|
||||||
let u = &self.U * &self.W.invert();
|
let u = &self.U * &self.W.invert();
|
||||||
MontgomeryPoint(u.as_bytes())
|
MontgomeryPoint(u.as_bytes())
|
||||||
|
|
@ -463,25 +463,23 @@ impl ProjectivePoint {
|
||||||
mul %31, %29, %21
|
mul %31, %29, %21
|
||||||
fin // finish execution
|
fin // finish execution
|
||||||
);
|
);
|
||||||
let mut engine = engine_25519::Engine25519::new();
|
|
||||||
let mut job = engine_25519::Job {
|
use crate::backend::serial::u32e::*;
|
||||||
id: None,
|
ensure_engine();
|
||||||
ucode: [0; 1024],
|
let mut ucode_hw: &'static mut [u32] = unsafe {
|
||||||
uc_len: mcode.len() as u32,
|
core::slice::from_raw_parts_mut(ENGINE_MEM.unwrap().as_mut_ptr() as *mut u32, 1024)
|
||||||
uc_start: 0,
|
};
|
||||||
window: Some(0),
|
let rf_hw: &mut [u32] = unsafe {
|
||||||
rf: [0; engine_25519::RF_SIZE_IN_U32],
|
core::slice::from_raw_parts_mut(
|
||||||
|
(ENGINE_MEM.unwrap().as_mut_ptr() as usize + RF_U8_BASE) as *mut u32,
|
||||||
|
TOTAL_RF_SIZE_IN_U32,
|
||||||
|
)
|
||||||
};
|
};
|
||||||
for (&src, dst) in mcode.iter().zip(job.ucode.iter_mut()) {
|
|
||||||
*dst = src as u32;
|
|
||||||
}
|
|
||||||
copy_to_rf(self.U.as_bytes(), 29, &mut job.rf);
|
|
||||||
copy_to_rf(self.W.as_bytes(), 30, &mut job.rf);
|
|
||||||
|
|
||||||
// start the run
|
copy_to_rf(self.U.as_bytes(), 29, rf_hw, 0);
|
||||||
let result_rf = engine.spawn_job(job).expect("couldn't run engine job");
|
copy_to_rf(self.W.as_bytes(), 30, rf_hw, 0);
|
||||||
|
|
||||||
MontgomeryPoint(copy_from_rf(31, &result_rf))
|
MontgomeryPoint(run_job(&mut ucode_hw, &rf_hw, &mcode, 0))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -541,27 +539,6 @@ pub(crate) fn differential_add_and_double(
|
||||||
Q.W = t17; // W_{Q'} = U_D * 4 (W_P U_Q - U_P W_Q)^2
|
Q.W = t17; // W_{Q'} = U_D * 4 (W_P U_Q - U_P W_Q)^2
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(curve25519_dalek_backend = "u32e_backend")]
|
|
||||||
fn copy_to_rf(bytes: [u8; 32], register: usize, rf: &mut [u32; engine_25519::RF_SIZE_IN_U32]) {
|
|
||||||
use core::convert::TryInto;
|
|
||||||
for (byte, rf_dst) in bytes.chunks_exact(4).zip(rf[register * 8..(register+1)*8].iter_mut()) {
|
|
||||||
*rf_dst = u32::from_le_bytes(byte.try_into().expect("chunks_exact failed us"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(curve25519_dalek_backend = "u32e_backend")]
|
|
||||||
fn copy_from_rf(register: usize, rf: &[u32; engine_25519::RF_SIZE_IN_U32]) -> [u8; 32] {
|
|
||||||
let mut ret: [u8; 32] = [0; 32];
|
|
||||||
|
|
||||||
for (src, dst) in rf[register*8 .. (register+1)*8].iter().zip(ret.chunks_exact_mut(4).into_iter()) {
|
|
||||||
for (&src_byte, dst_byte) in src.to_le_bytes().iter().zip(dst.iter_mut()) {
|
|
||||||
*dst_byte = src_byte;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)] // absorbed into mul, but might be useful later on as a subroutine for something else
|
#[allow(dead_code)] // absorbed into mul, but might be useful later on as a subroutine for something else
|
||||||
#[cfg(curve25519_dalek_backend = "u32e_backend")]
|
#[cfg(curve25519_dalek_backend = "u32e_backend")]
|
||||||
pub(crate) fn differential_add_and_double(
|
pub(crate) fn differential_add_and_double(
|
||||||
|
|
@ -650,39 +627,36 @@ pub(crate) fn differential_add_and_double(
|
||||||
|
|
||||||
fin // finish execution
|
fin // finish execution
|
||||||
);
|
);
|
||||||
let mut engine = engine_25519::Engine25519::new();
|
use crate::backend::serial::u32e::*;
|
||||||
|
ensure_engine();
|
||||||
let mut job = engine_25519::Job {
|
let mut ucode_hw: &'static mut [u32] = unsafe {
|
||||||
id: None,
|
core::slice::from_raw_parts_mut(ENGINE_MEM.unwrap().as_mut_ptr() as *mut u32, 1024)
|
||||||
ucode: [0; 1024],
|
};
|
||||||
uc_len: mcode.len() as u32,
|
let rf_hw: &mut [u32] = unsafe {
|
||||||
uc_start: 0,
|
core::slice::from_raw_parts_mut(
|
||||||
window: Some(0),
|
(ENGINE_MEM.unwrap().as_mut_ptr() as usize + RF_U8_BASE) as *mut u32,
|
||||||
rf: [0; engine_25519::RF_SIZE_IN_U32],
|
TOTAL_RF_SIZE_IN_U32,
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
for (&src, dst) in mcode.iter().zip(job.ucode.iter_mut()) {
|
|
||||||
*dst = src;
|
|
||||||
}
|
|
||||||
|
|
||||||
// P.U in %20
|
// P.U in %20
|
||||||
// P.W in %21
|
// P.W in %21
|
||||||
// Q.U in %22
|
// Q.U in %22
|
||||||
// Q.W in %23
|
// Q.W in %23
|
||||||
// affine_PmQ in %24
|
// affine_PmQ in %24
|
||||||
copy_to_rf(P.U.as_bytes(), 20, &mut job.rf);
|
copy_to_rf(P.U.as_bytes(), 20, rf_hw, 0);
|
||||||
copy_to_rf(P.W.as_bytes(), 21, &mut job.rf);
|
copy_to_rf(P.W.as_bytes(), 21, rf_hw, 0);
|
||||||
copy_to_rf(Q.U.as_bytes(), 22, &mut job.rf);
|
copy_to_rf(Q.U.as_bytes(), 22, rf_hw, 0);
|
||||||
copy_to_rf(Q.W.as_bytes(), 23, &mut job.rf);
|
copy_to_rf(Q.W.as_bytes(), 23, rf_hw, 0);
|
||||||
copy_to_rf(affine_PmQ.as_bytes(), 24, &mut job.rf);
|
copy_to_rf(affine_PmQ.as_bytes(), 24, rf_hw, 0);
|
||||||
|
|
||||||
// start the run
|
// start the run
|
||||||
let result_rf = engine.spawn_job(job).expect("couldn't run engine job");
|
run_job(&mut ucode_hw, &rf_hw, &mcode, 0);
|
||||||
|
|
||||||
P.U = FieldElement::from_bytes(©_from_rf(20, &result_rf));
|
P.U = FieldElement::from_bytes(©_from_rf(20, &rf_hw, 0));
|
||||||
P.W = FieldElement::from_bytes(©_from_rf(21, &result_rf));
|
P.W = FieldElement::from_bytes(©_from_rf(21, &rf_hw, 0));
|
||||||
Q.U = FieldElement::from_bytes(©_from_rf(22, &result_rf));
|
Q.U = FieldElement::from_bytes(©_from_rf(22, &rf_hw, 0));
|
||||||
Q.W = FieldElement::from_bytes(©_from_rf(23, &result_rf));
|
Q.W = FieldElement::from_bytes(©_from_rf(23, &rf_hw, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
define_mul_assign_variants!(LHS = MontgomeryPoint, RHS = Scalar);
|
define_mul_assign_variants!(LHS = MontgomeryPoint, RHS = Scalar);
|
||||||
|
|
@ -705,10 +679,12 @@ impl Mul<&Scalar> for &MontgomeryPoint {
|
||||||
/// Given `self` \\( = u\_0(P) \\), and a `Scalar` \\(n\\), return \\( u\_0([n]P) \\).
|
/// Given `self` \\( = u\_0(P) \\), and a `Scalar` \\(n\\), return \\( u\_0([n]P) \\).
|
||||||
#[cfg(curve25519_dalek_backend = "u32e_backend")]
|
#[cfg(curve25519_dalek_backend = "u32e_backend")]
|
||||||
fn mul(self, scalar: &Scalar) -> MontgomeryPoint {
|
fn mul(self, scalar: &Scalar) -> MontgomeryPoint {
|
||||||
|
use crate::backend::serial::u32e::*;
|
||||||
|
|
||||||
log::debug!("hw mont");
|
log::debug!("hw mont");
|
||||||
// Algorithm 8 of Costello-Smith 2017
|
// Algorithm 8 of Costello-Smith 2017
|
||||||
let affine_u = FieldElement::from_bytes(&self.0);
|
let affine_u = FieldElement::from_bytes(&self.0);
|
||||||
let mut x0 = ProjectivePoint::identity();
|
let x0 = ProjectivePoint::identity();
|
||||||
let x1 = ProjectivePoint {
|
let x1 = ProjectivePoint {
|
||||||
U: affine_u,
|
U: affine_u,
|
||||||
W: FieldElement::ONE,
|
W: FieldElement::ONE,
|
||||||
|
|
@ -716,10 +692,6 @@ impl Mul<&Scalar> for &MontgomeryPoint {
|
||||||
|
|
||||||
// for now, prefer to use the fully-accelerated version where this code is local to the server
|
// for now, prefer to use the fully-accelerated version where this code is local to the server
|
||||||
// instead of transmitting it every call with the data...gives about a 2x performance speedup
|
// instead of transmitting it every call with the data...gives about a 2x performance speedup
|
||||||
if false {
|
|
||||||
#[cfg(not(test))] // due to issue https://github.com/rust-lang/rust/issues/59168, you will have to manually comment this out when running a test on the full system and not just this crate.
|
|
||||||
log::warn!("wrong multiply being used!");
|
|
||||||
|
|
||||||
let mcode = assemble_engine25519!(
|
let mcode = assemble_engine25519!(
|
||||||
start:
|
start:
|
||||||
// P.U in %20
|
// P.U in %20
|
||||||
|
|
@ -983,72 +955,44 @@ impl Mul<&Scalar> for &MontgomeryPoint {
|
||||||
mul %31, %29, %21
|
mul %31, %29, %21
|
||||||
fin // finish execution
|
fin // finish execution
|
||||||
);
|
);
|
||||||
let mut engine = engine_25519::Engine25519::new();
|
|
||||||
let mut job = engine_25519::Job {
|
let window = 0;
|
||||||
id: None,
|
ensure_engine();
|
||||||
ucode: [0; 1024],
|
let mut ucode_hw: &'static mut [u32] = unsafe {
|
||||||
uc_len: mcode.len() as u32,
|
core::slice::from_raw_parts_mut(ENGINE_MEM.unwrap().as_mut_ptr() as *mut u32, 1024)
|
||||||
uc_start: 0,
|
};
|
||||||
window: Some(0),
|
let mut rf_hw: &mut [u32] = unsafe {
|
||||||
rf: [0; engine_25519::RF_SIZE_IN_U32],
|
core::slice::from_raw_parts_mut(
|
||||||
|
(ENGINE_MEM.unwrap().as_mut_ptr() as usize + RF_U8_BASE) as *mut u32,
|
||||||
|
TOTAL_RF_SIZE_IN_U32,
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
for (&src, dst) in mcode.iter().zip(job.ucode.iter_mut()) {
|
copy_to_rf(x0.U.as_bytes(), 25, &mut rf_hw, window);
|
||||||
*dst = src as u32;
|
copy_to_rf(x0.W.as_bytes(), 26, &mut rf_hw, window);
|
||||||
}
|
copy_to_rf(x1.U.as_bytes(), 27, &mut rf_hw, window);
|
||||||
|
copy_to_rf(x1.W.as_bytes(), 28, &mut rf_hw, window);
|
||||||
|
copy_to_rf(affine_u.as_bytes(), 24, &mut rf_hw, window);
|
||||||
|
copy_to_rf(scalar.bytes, 31, &mut rf_hw, window);
|
||||||
|
copy_to_rf(
|
||||||
|
[
|
||||||
|
254, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00,
|
||||||
|
],
|
||||||
|
19,
|
||||||
|
&mut rf_hw,
|
||||||
|
window,
|
||||||
|
); // 254 as loop counter
|
||||||
|
|
||||||
copy_to_rf(x0.U.as_bytes(), 25, &mut job.rf);
|
MontgomeryPoint(run_job(&mut ucode_hw, &rf_hw, &mcode, window))
|
||||||
copy_to_rf(x0.W.as_bytes(), 26, &mut job.rf);
|
|
||||||
copy_to_rf(x1.U.as_bytes(), 27, &mut job.rf);
|
|
||||||
copy_to_rf(x1.W.as_bytes(), 28, &mut job.rf);
|
|
||||||
copy_to_rf(affine_u.as_bytes(), 24, &mut job.rf);
|
|
||||||
copy_to_rf(scalar.bytes, 31, &mut job.rf);
|
|
||||||
// load the number 254 into the loop index register
|
|
||||||
copy_to_rf([
|
|
||||||
254, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
], 19, &mut job.rf);
|
|
||||||
|
|
||||||
// start the run
|
|
||||||
let result_rf = engine.spawn_job(job).expect("couldn't run engine job");
|
|
||||||
|
|
||||||
if false { // unmerged affine path
|
|
||||||
x0.U = FieldElement::from_bytes(©_from_rf(25, &result_rf));
|
|
||||||
x0.W = FieldElement::from_bytes(©_from_rf(26, &result_rf));
|
|
||||||
|
|
||||||
//Note: is seems this TODO has been handled as ProjectivePoint's as_affine already
|
|
||||||
//has an accelerated version. Should this be removed or is there further
|
|
||||||
//optimization work to be done. If so, what is that work?
|
|
||||||
|
|
||||||
// TODO: optimize this relatively innocuous looking call.
|
|
||||||
// this consumes about 100ms runtime -- need to implement this using
|
|
||||||
// curve25519 acceleration!
|
|
||||||
x0.as_affine()
|
|
||||||
} else {
|
|
||||||
MontgomeryPoint(copy_from_rf(31, &result_rf))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let mut engine = engine_25519::Engine25519::new();
|
|
||||||
let job = engine_25519::MontgomeryJob {
|
|
||||||
x0_u: x0.U.as_bytes(),
|
|
||||||
x0_w: x0.W.as_bytes(),
|
|
||||||
x1_u: x1.U.as_bytes(),
|
|
||||||
x1_w: x1.W.as_bytes(),
|
|
||||||
affine_u: affine_u.as_bytes(),
|
|
||||||
scalar: scalar.bytes,
|
|
||||||
};
|
|
||||||
|
|
||||||
MontgomeryPoint(engine.montgomery_job(job).expect("couldn't run montgomery multiply job"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Given `self` \\( = u\_0(P) \\), and a `Scalar` \\(n\\), return \\( u\_0(\[n\]P) \\)
|
/// Given `self` \\( = u\_0(P) \\), and a `Scalar` \\(n\\), return \\( u\_0(\[n\]P) \\)
|
||||||
#[cfg(not(curve25519_dalek_backend = "u32e_backend"))]
|
#[cfg(not(curve25519_dalek_backend = "u32e_backend"))]
|
||||||
fn mul(self, scalar: &Scalar) -> MontgomeryPoint {
|
fn mul(self, scalar: &Scalar) -> MontgomeryPoint {
|
||||||
// TODO: consider feature "panic_on_sw_eval"
|
// TODO: consider feature "panic_on_sw_eval"
|
||||||
#[cfg(all(not(test),curve25519_dalek_backend = "u32e_backend"))] // due to issue https://github.com/rust-lang/rust/issues/59168, you will have to manually comment this out when running a test on the full system and not just this crate.
|
#[cfg(all(not(test), curve25519_dalek_backend = "u32e_backend"))] // due to issue https://github.com/rust-lang/rust/issues/59168, you will have to manually comment this out when running a test on the full system and not just this crate.
|
||||||
log::warn!("sw montgomery multiply being used - check for build config errors!");
|
log::warn!("sw montgomery multiply being used - check for build config errors!");
|
||||||
// We multiply by the integer representation of the given Scalar. By scalar invariant #1,
|
// We multiply by the integer representation of the given Scalar. By scalar invariant #1,
|
||||||
// the MSB is 0, so we can skip it.
|
// the MSB is 0, so we can skip it.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue