From cb42e8709614092c14f955de9e75f40d2359a69f Mon Sep 17 00:00:00 2001 From: "pinkforest(she/her)" <36498018+pinkforest@users.noreply.github.com> Date: Sat, 10 Dec 2022 13:09:24 +1100 Subject: [PATCH] Fixes curve25519_dalek_bits defaults for cross and wasm (#465) build.rs was using cfg(target) but it has to evaluate this from env TARGET as build.rs cfg(target) in build context is the builder host and not the target. This change fixes curve25519_dalek_bits lottery to determine the correct automatic curve25119_dalek_bits with the help of platforms crate. As discussed in #456 this also prepares for well known defaults for wasm and arm serial backend via cfg(curve25519_dalek_bits = "64") If the wasm32 or armv7 are going to be u64 serial by default these will be followed up on later. --- .github/workflows/rust.yml | 27 ++++++++++++++++++ CHANGELOG.md | 1 + Cargo.toml | 3 ++ build.rs | 56 ++++++++++++++++++++++++++++++-------- 4 files changed, 75 insertions(+), 12 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 57330f6..4889619 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -48,6 +48,33 @@ jobs: RUSTFLAGS: '--cfg curve25519_dalek_backend="simd" -C target_feature=+avx512ifma' run: cargo build --target x86_64-unknown-linux-gnu + cross: + strategy: + matrix: + include: + # ARM32 + - target: armv7-unknown-linux-gnueabihf + rust: stable + + # ARM64 + - target: aarch64-unknown-linux-gnu + rust: stable + + # PPC32 + - target: powerpc-unknown-linux-gnu + rust: stable + + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - run: ${{ matrix.deps }} + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ matrix.rust }} + targets: ${{ matrix.target }} + - run: cargo install cross + - run: cross test --release --target ${{ matrix.target }} + nightly: name: Test nightly compiler runs-on: ubuntu-latest diff --git a/CHANGELOG.md b/CHANGELOG.md index 841d380..cb1572d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ major series. #### Other changes +* Set well known defaults for wasm and arm * Update Maintenance Policies for SemVer * Migrate documentation to docs.rs hosted * Fix backend documentation generation diff --git a/Cargo.toml b/Cargo.toml index 17ba12e..8b7db78 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,9 @@ hex = "0.4.2" rand = "0.8" rand_core = { version = "0.6", default-features = false, features = ["getrandom"] } +[build-dependencies] +platforms = "3.0.2" + [[bench]] name = "dalek_benchmarks" harness = false diff --git a/build.rs b/build.rs index cb896ef..1df9461 100644 --- a/build.rs +++ b/build.rs @@ -1,15 +1,47 @@ //! 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\""); +#[allow(non_camel_case_types)] +enum DalekBits { + #[cfg_attr(curve25519_dalek_bits = "64", allow(dead_code))] + Dalek32, + #[cfg_attr(curve25519_dalek_bits = "32", allow(dead_code))] + Dalek64, +} + +#[cfg(all(not(curve25519_dalek_bits = "64"), not(curve25519_dalek_bits = "32")))] +#[deny(dead_code)] +fn lotto_curve25519_dalek_bits() -> DalekBits { + use platforms::target::PointerWidth; + + let target_triplet = std::env::var("TARGET").unwrap(); + let platform = platforms::Platform::find(&target_triplet).unwrap(); + + match platform.target_arch { + //Issues: 449 and 456 + //TODO(Arm): Needs tests + benchmarks to back this up + //platforms::target::Arch::Arm => DalekBits::Dalek64, + //TODO(Wasm32): Needs tests + benchmarks to back this up + //platforms::target::Arch::Wasm32 => DalekBits::Dalek64, + _ => match platform.target_pointer_width { + PointerWidth::U64 => DalekBits::Dalek64, + PointerWidth::U32 => DalekBits::Dalek32, + _ => DalekBits::Dalek32, + }, + } +} + +fn main() { + #[cfg(curve25519_dalek_bits = "32")] + let curve25519_dalek_bits = DalekBits::Dalek32; + + #[cfg(curve25519_dalek_bits = "64")] + let curve25519_dalek_bits = DalekBits::Dalek64; + + #[cfg(all(not(curve25519_dalek_bits = "64"), not(curve25519_dalek_bits = "32")))] + let curve25519_dalek_bits = lotto_curve25519_dalek_bits(); + + match curve25519_dalek_bits { + DalekBits::Dalek64 => println!("cargo:rustc-cfg=curve25519_dalek_bits=\"64\""), + DalekBits::Dalek32 => println!("cargo:rustc-cfg=curve25519_dalek_bits=\"32\""), + } }