Skip to content

Commit

Permalink
add features for auto-release of hardware and warning on sw fallback
Browse files Browse the repository at this point in the history
These features should help the crate work more seamlessly with the
existing API, at perhaps some performance penalty that is still
to be determined.
  • Loading branch information
bunnie committed Mar 28, 2024
1 parent 0077595 commit c1b910a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions curve25519-dalek/src/backend/serial/u32e/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,14 @@ pub(crate) fn engine(a: &[u8; 32], b: &[u8; 32], op: EngineOp) -> Engine25519 {
}
}

#[cfg(feature="auto-release")]
free_engine();

Engine25519 { 0: result }
}
_ => {
// fallback to fiat crypto field arithmetic...
#[cfg(feature="warn-fallback")]
log::warn!("Hardware acceleration unavailable, falling back to software");
let fiat_a = bytes_to_fiat(a);
let fiat_b = bytes_to_fiat(b);
Expand Down
15 changes: 13 additions & 2 deletions curve25519-dalek/src/montgomery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,13 @@ impl ProjectivePoint {
copy_to_rf(self.U.as_bytes(), 29, rf_hw, 0);
copy_to_rf(self.W.as_bytes(), 30, rf_hw, 0);

MontgomeryPoint(run_job(&mut ucode_hw, &rf_hw, &mcode, 0))
let r = MontgomeryPoint(run_job(&mut ucode_hw, &rf_hw, &mcode, 0));
#[cfg(feature="auto-release")]
free_engine();
r
}
_ => {
#[cfg(feature="warn-fallback")]
log::warn!("Hardware acceleration unavailable, falling back to software");
let u = &self.U * &self.W.invert();
MontgomeryPoint(u.as_bytes())
Expand Down Expand Up @@ -651,8 +655,11 @@ pub(crate) fn differential_add_and_double(
P.W = FieldElement::from_bytes(&copy_from_rf(21, &rf_hw, 0));
Q.U = FieldElement::from_bytes(&copy_from_rf(22, &rf_hw, 0));
Q.W = FieldElement::from_bytes(&copy_from_rf(23, &rf_hw, 0));
#[cfg(feature="auto-release")]
free_engine();
}
_ => {
#[cfg(feature="warn-fallback")]
log::warn!("Hardware acceleration unavailable, falling back to software");
let t0 = &P.U + &P.W;
let t1 = &P.U - &P.W;
Expand Down Expand Up @@ -1012,9 +1019,13 @@ impl Mul<&Scalar> for &MontgomeryPoint {
window,
); // 254 as loop counter

MontgomeryPoint(run_job(&mut ucode_hw, &rf_hw, &mcode, window))
let r = MontgomeryPoint(run_job(&mut ucode_hw, &rf_hw, &mcode, window));
#[cfg(feature="auto-release")]
free_engine();
r
}
_ => {
#[cfg(feature="warn-fallback")]
log::warn!("Hardware acceleration unavailable, falling back to software");
// We multiply by the integer representation of the given Scalar. By scalar invariant #1,
// the MSB is 0, so we can skip it.
Expand Down

0 comments on commit c1b910a

Please sign in to comment.