Skip to content

Commit

Permalink
Fixes curve25519_dalek_bits defaults for cross and wasm
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pinkforest committed Dec 9, 2022
1 parent cc304c2 commit c32b031
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,37 @@ 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

# WASM32
- target: wasm32-unknown-unknown
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
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 41 additions & 11 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
//! This selects the curve25519_dalek_bits either by default from target_pointer_width or explicitly set

#[allow(non_camel_case_types)]
enum DalekBits {
Dalek32,
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(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\"");
#[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\""),
}
}

0 comments on commit c32b031

Please sign in to comment.