Skip to content
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

Add target u32/u64 backend overrides #454

Merged
merged 1 commit into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,9 @@ jobs:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
# This filter selects no benchmarks, so we don't run any, only build them.
- run: cargo bench "nonexistentbenchmark"
- name: Build u32 bench
run: env RUSTFLAGS="--cfg curve25519_dalek_bits=\"32\"" cargo bench "nonexistentbenchmark"
- name: Build u64 bench
run: env RUSTFLAGS="--cfg curve25519_dalek_bits=\"64\"" cargo bench "nonexistentbenchmark"
- name: Build default (host native) bench
run: cargo bench "nonexistentbenchmark"
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ major series.

## 4.x series

* Add target u32/u64 backend overrides
* Migrate documentation to docs.rs hosted
* Fix backend documentation generation
* Deprecate `EdwardsPoint::hash_from_bytes` and rename it `EdwardsPoint::nonspect_map_to_curve`
Expand Down
15 changes: 15 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! This selects the curve25519_dalek_bits either by default from target_pointer_width or explicitly set

fn main() {
#[cfg(any(
all(not(target_pointer_width = "64"), not(curve25519_dalek_bits = "64")),
curve25519_dalek_bits = "32"
))]
println!("cargo:rustc-cfg=curve25519_dalek_bits=\"32\"");

#[cfg(any(
all(target_pointer_width = "64", not(curve25519_dalek_bits = "32")),
curve25519_dalek_bits = "64"
))]
println!("cargo:rustc-cfg=curve25519_dalek_bits=\"64\"");
}
13 changes: 9 additions & 4 deletions src/backend/serial/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,23 @@
use cfg_if::cfg_if;

cfg_if! {

if #[cfg(feature = "fiat_backend")] {
#[cfg(not(target_pointer_width = "64"))]

#[cfg(curve25519_dalek_bits = "32")]
pub mod fiat_u32;

#[cfg(target_pointer_width = "64")]
#[cfg(curve25519_dalek_bits = "64")]
pub mod fiat_u64;

} else {
#[cfg(not(target_pointer_width = "64"))]

#[cfg(curve25519_dalek_bits = "32")]
pub mod u32;

#[cfg(target_pointer_width = "64")]
#[cfg(curve25519_dalek_bits = "64")]
pub mod u64;

}
}

Expand Down
12 changes: 6 additions & 6 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ use crate::scalar::Scalar;

cfg_if! {
if #[cfg(feature = "fiat_backend")] {
#[cfg(not(target_pointer_width = "64"))]
#[cfg(curve25519_dalek_bits = "32")]
pub use crate::backend::serial::fiat_u32::constants::*;
#[cfg(target_pointer_width = "64")]
#[cfg(curve25519_dalek_bits = "64")]
pub use crate::backend::serial::fiat_u64::constants::*;
} else {
#[cfg(not(target_pointer_width = "64"))]
#[cfg(curve25519_dalek_bits = "32")]
pub use crate::backend::serial::u32::constants::*;
#[cfg(target_pointer_width = "64")]
#[cfg(curve25519_dalek_bits = "64")]
pub use crate::backend::serial::u64::constants::*;
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@ mod test {

/// Test that d = -121665/121666
#[test]
#[cfg(all(not(target_pointer_width = "64"), not(feature = "fiat_backend")))]
#[cfg(all(curve25519_dalek_bits = "32", not(feature = "fiat_backend")))]
fn test_d_vs_ratio() {
use crate::backend::serial::u32::field::FieldElement2625;
let a = -&FieldElement2625([121665, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
Expand All @@ -162,7 +162,7 @@ mod test {

/// Test that d = -121665/121666
#[test]
#[cfg(all(target_pointer_width = "64", not(feature = "fiat_backend")))]
#[cfg(all(curve25519_dalek_bits = "64", not(feature = "fiat_backend")))]
fn test_d_vs_ratio() {
use crate::backend::serial::u64::field::FieldElement51;
let a = -&FieldElement51([121665, 0, 0, 0, 0]);
Expand Down
10 changes: 5 additions & 5 deletions src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ use crate::constants;

cfg_if! {
if #[cfg(feature = "fiat_backend")] {
#[cfg(not(target_pointer_width = "64"))]
#[cfg(curve25519_dalek_bits = "32")]
pub use backend::serial::fiat_u32::field::*;
#[cfg(target_pointer_width = "64")]
#[cfg(curve25519_dalek_bits = "64")]
pub use backend::serial::fiat_u64::field::*;

/// A `FieldElement` represents an element of the field
Expand All @@ -49,7 +49,7 @@ cfg_if! {
/// implementations.
///
/// Using formally-verified field arithmetic from fiat-crypto.
#[cfg(not(target_pointer_width = "64"))]
#[cfg(curve25519_dalek_bits = "32")]
pub type FieldElement = backend::serial::fiat_u32::field::FieldElement2625;

/// A `FieldElement` represents an element of the field
Expand All @@ -59,9 +59,9 @@ cfg_if! {
/// implementations.
///
/// Using formally-verified field arithmetic from fiat-crypto.
#[cfg(target_pointer_width = "64")]
#[cfg(curve25519_dalek_bits = "64")]
pub type FieldElement = backend::serial::fiat_u64::field::FieldElement51;
} else if #[cfg(target_pointer_width = "64")] {
} else if #[cfg(curve25519_dalek_bits = "64")] {
pub use crate::backend::serial::u64::field::*;

/// A `FieldElement` represents an element of the field
Expand Down
14 changes: 7 additions & 7 deletions src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,36 +172,36 @@ cfg_if! {
///
/// This is a type alias for one of the scalar types in the `backend`
/// module.
#[cfg(not(target_pointer_width = "64"))]
#[cfg(curve25519_dalek_bits = "32")]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "fiat_backend", not(target_pointer_width = "64"))))
doc(cfg(all(feature = "fiat_backend", curve25519_dalek_bits = "32")))
)]
type UnpackedScalar = backend::serial::fiat_u32::scalar::Scalar29;

/// An `UnpackedScalar` represents an element of the field GF(l), optimized for speed.
///
/// This is a type alias for one of the scalar types in the `backend`
/// module.
#[cfg(target_pointer_width = "64")]
#[cfg(curve25519_dalek_bits = "64")]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "fiat_backend", target_pointer_width = "64")))
doc(cfg(all(feature = "fiat_backend", curve25519_dalek_bits = "64")))
)]
type UnpackedScalar = backend::serial::fiat_u64::scalar::Scalar52;
} else if #[cfg(target_pointer_width = "64")] {
} else if #[cfg(curve25519_dalek_bits = "64")] {
/// An `UnpackedScalar` represents an element of the field GF(l), optimized for speed.
///
/// This is a type alias for one of the scalar types in the `backend`
/// module.
#[cfg_attr(docsrs, doc(cfg(target_pointer_width = "64")))]
#[cfg_attr(docsrs, doc(cfg(curve25519_dalek_bits = "64")))]
type UnpackedScalar = backend::serial::u64::scalar::Scalar52;
} else {
/// An `UnpackedScalar` represents an element of the field GF(l), optimized for speed.
///
/// This is a type alias for one of the scalar types in the `backend`
/// module.
#[cfg_attr(docsrs, doc(cfg(not(target_pointer_width = "64"))))]
#[cfg_attr(docsrs, doc(cfg(curve25519_dalek_bits = "64")))]
type UnpackedScalar = backend::serial::u32::scalar::Scalar29;
}
}
Expand Down