diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 8914164a1..a586cf1ff 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -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" diff --git a/CHANGELOG.md b/CHANGELOG.md index f2126ab39..27af55f8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/src/backend/serial/mod.rs b/src/backend/serial/mod.rs index 4ce4d4f45..ebcb58641 100644 --- a/src/backend/serial/mod.rs +++ b/src/backend/serial/mod.rs @@ -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; + } } diff --git a/src/constants.rs b/src/constants.rs index ee7184c00..01cb71744 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -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::*; } } @@ -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]); @@ -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]); diff --git a/src/field.rs b/src/field.rs index 445ca5e45..c537ee7be 100644 --- a/src/field.rs +++ b/src/field.rs @@ -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 @@ -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 @@ -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 diff --git a/src/lib.rs b/src/lib.rs index e831b4f78..f41ea2d66 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,23 @@ )] #![doc(html_root_url = "https://docs.rs/curve25519-dalek/4.0.0-pre.2")] #![doc = include_str!("../README.md")] +//------------------------------------------------------------------------ +// curve25519-dalek arithmetric default and overrides +//------------------------------------------------------------------------ +#![cfg_attr( + any( + all(not(target_pointer_width = "64"), not(dalek_bits = "64")), + dalek_bits = "32" + ), + cfg(dalek_bits = "32") +)] +#![cfg_attr( + any( + all(target_pointer_width = "64", not(dalek_bits = "32")), + dalek_bits = "64" + ), + cfg(dalek_bits = "64") +)] //------------------------------------------------------------------------ // External dependencies: diff --git a/src/scalar.rs b/src/scalar.rs index ec639b3d5..a29def34a 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -172,10 +172,10 @@ 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; @@ -183,25 +183,25 @@ cfg_if! { /// /// 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; } }