Skip to content

Commit

Permalink
Add target u32/u64 backend override
Browse files Browse the repository at this point in the history
As suggested in 453 it is sometimes feasible to
select the backend bits via an override.

This change provides an override via `cfg(dalek_bits)`
to override the used serial or fiat target backend.
  • Loading branch information
pinkforest committed Dec 7, 2022
1 parent 29466f1 commit 5557755
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 23 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,7 @@ 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 dalek_bits=\"32\"" cargo bench "nonexistentbenchmark"
- name: Build u64 bench
run: env RUSTFLAGS="--cfg dalek_bits=\"64\"" 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 dalek_bits either by default from target_pointer_width or explicitly set
fn main() {
#[cfg(any(
all(not(target_pointer_width = "64"), not(dalek_bits = "64")),
dalek_bits = "32"
))]
println!("cargo:rustc-cfg=dalek_bits=\"32\"");

#[cfg(any(
all(target_pointer_width = "64", not(dalek_bits = "32")),
dalek_bits = "64"
))]
println!("cargo:rustc-cfg=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(dalek_bits = "32")]
pub mod fiat_u32;

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

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

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

#[cfg(target_pointer_width = "64")]
#[cfg(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(dalek_bits = "32")]
pub use crate::backend::serial::fiat_u32::constants::*;
#[cfg(target_pointer_width = "64")]
#[cfg(dalek_bits = "64")]
pub use crate::backend::serial::fiat_u64::constants::*;
} else {
#[cfg(not(target_pointer_width = "64"))]
#[cfg(dalek_bits = "32")]
pub use crate::backend::serial::u32::constants::*;
#[cfg(target_pointer_width = "64")]
#[cfg(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(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(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(dalek_bits = "32")]
pub use backend::serial::fiat_u32::field::*;
#[cfg(target_pointer_width = "64")]
#[cfg(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(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(dalek_bits = "64")]
pub type FieldElement = backend::serial::fiat_u64::field::FieldElement51;
} else if #[cfg(target_pointer_width = "64")] {
} else if #[cfg(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(dalek_bits = "32")]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "fiat_backend", not(target_pointer_width = "64"))))
doc(cfg(all(feature = "fiat_backend", 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(dalek_bits = "64")]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "fiat_backend", target_pointer_width = "64")))
doc(cfg(all(feature = "fiat_backend", dalek_bits = "64")))
)]
type UnpackedScalar = backend::serial::fiat_u64::scalar::Scalar52;
} else if #[cfg(target_pointer_width = "64")] {
} else if #[cfg(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(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(dalek_bits = "64")))]
type UnpackedScalar = backend::serial::u32::scalar::Scalar29;
}
}
Expand Down

0 comments on commit 5557755

Please sign in to comment.