From 8a545874937200bedde7e491005498cc66f3f448 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Wed, 31 Jul 2024 15:30:59 +0300 Subject: [PATCH] Switch to pre-release AES crate to remove workarounds for ARMv8 --- .cargo/config.toml | 5 -- .github/workflows/rust.yml | 4 +- .github/workflows/snapshot-build.yml | 7 +-- Cargo.lock | 62 +++++++++++++++++++++--- crates/subspace-proof-of-time/Cargo.toml | 2 +- crates/subspace-proof-of-time/src/aes.rs | 16 +++--- 6 files changed, 66 insertions(+), 30 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index d9b2e600cf..6f94bc4fba 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,8 +1,3 @@ [target.'cfg(target_arch = "x86_64")'] # Require AES-NI on x86-64 by default rustflags = ["-C", "target-feature=+aes"] - -[target.'cfg(target_arch = "aarch64")'] -# TODO: AES flag is such that we have decent performance on ARMv8, remove once `aes` crate with MSRV bump ships: -# https://github.com/RustCrypto/block-ciphers/pull/395 -rustflags = ["--cfg", "aes_armv8"] diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index d86f3ae74a..9b914d6862 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -24,9 +24,7 @@ env: CARGO_TERM_COLOR: always # Build smaller artifacts to avoid running out of space in CI # TODO: Try to remove once https://github.com/paritytech/substrate/issues/11538 is resolved - # TODO: AES flag is such that we have decent performance on ARMv8, remove once `aes` crate with MSRV bump ships: - # https://github.com/RustCrypto/block-ciphers/pull/395 - RUSTFLAGS: -C strip=symbols -C opt-level=s --cfg aes_armv8 + RUSTFLAGS: -C strip=symbols -C opt-level=s jobs: cargo-fmt: diff --git a/.github/workflows/snapshot-build.yml b/.github/workflows/snapshot-build.yml index c4c7613961..a70b3fc145 100644 --- a/.github/workflows/snapshot-build.yml +++ b/.github/workflows/snapshot-build.yml @@ -103,15 +103,10 @@ jobs: - os: ${{ fromJson(github.repository_owner == 'autonomys' && '["self-hosted", "ubuntu-20.04-x86-64"]' || '"ubuntu-20.04"') }} target: aarch64-unknown-linux-gnu suffix: ubuntu-aarch64-${{ github.ref_name }} - # TODO: AES flag is such that we have decent performance on ARMv8, remove once `aes` crate with MSRV bump ships: - # https://github.com/RustCrypto/block-ciphers/pull/395 - rustflags: "-C linker=aarch64-linux-gnu-gcc --cfg aes_armv8" + rustflags: "-C linker=aarch64-linux-gnu-gcc" - os: ${{ fromJson(github.repository_owner == 'autonomys' && '["self-hosted", "macos-14-arm64"]' || '"macos-14"') }} target: aarch64-apple-darwin suffix: macos-aarch64-${{ github.ref_name }} - # TODO: AES flag is such that we have decent performance on ARMv8, remove once `aes` crate with MSRV bump ships: - # https://github.com/RustCrypto/block-ciphers/pull/395 - rustflags: "--cfg aes_armv8" - os: ${{ fromJson(github.repository_owner == 'autonomys' && '["self-hosted", "windows-server-2022-x86-64"]' || '"windows-2022"') }} target: x86_64-pc-windows-msvc suffix: windows-x86_64-skylake-${{ github.ref_name }} diff --git a/Cargo.lock b/Cargo.lock index 1e2ebf27d3..d8fd6fa63f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -224,7 +224,7 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" dependencies = [ - "crypto-common", + "crypto-common 0.1.6", "generic-array 0.14.7", ] @@ -239,6 +239,17 @@ dependencies = [ "cpufeatures", ] +[[package]] +name = "aes" +version = "0.9.0-pre.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "183b3b4639f8f7237857117abb74f3dc8648b77e67ff78d9cb6959fd7e76f387" +dependencies = [ + "cfg-if", + "cipher 0.5.0-pre.6", + "cpufeatures", +] + [[package]] name = "aes-gcm" version = "0.10.3" @@ -246,7 +257,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "831010a0f742e1209b3bcea8fab6a8e149051ba6099432c8cb2cc117dec3ead1" dependencies = [ "aead", - "aes", + "aes 0.8.4", "cipher 0.4.4", "ctr", "ghash", @@ -1824,11 +1835,21 @@ version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" dependencies = [ - "crypto-common", - "inout", + "crypto-common 0.1.6", + "inout 0.1.3", "zeroize", ] +[[package]] +name = "cipher" +version = "0.5.0-pre.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c71c893d5a1e8257048dbb29954d2e1f85f091a150304f1defe4ca2806da5d3f" +dependencies = [ + "crypto-common 0.2.0-rc.0", + "inout 0.2.0-rc.0", +] + [[package]] name = "clap" version = "4.5.7" @@ -2299,6 +2320,15 @@ dependencies = [ "typenum", ] +[[package]] +name = "crypto-common" +version = "0.2.0-rc.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c070b79a496dccd931229780ad5bbedd535ceff6c3565605a8e440e18e1aa2b" +dependencies = [ + "hybrid-array", +] + [[package]] name = "crypto-mac" version = "0.7.0" @@ -2578,7 +2608,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer 0.10.4", "const-oid", - "crypto-common", + "crypto-common 0.1.6", "subtle 2.6.0", ] @@ -4991,6 +5021,15 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "hybrid-array" +version = "0.2.0-rc.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d306b679262030ad8813a82d4915fc04efff97776e4db7f8eb5137039d56400" +dependencies = [ + "typenum", +] + [[package]] name = "hyper" version = "0.14.29" @@ -5222,6 +5261,15 @@ dependencies = [ "generic-array 0.14.7", ] +[[package]] +name = "inout" +version = "0.2.0-rc.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbc33218cf9ce7b927426ee4ad3501bcc5d8c26bf5fb4a82849a083715aca427" +dependencies = [ + "hybrid-array", +] + [[package]] name = "instant" version = "0.1.13" @@ -13047,7 +13095,7 @@ dependencies = [ name = "subspace-proof-of-time" version = "0.1.0" dependencies = [ - "aes", + "aes 0.9.0-pre.1", "core_affinity", "criterion", "rand 0.8.5", @@ -14281,7 +14329,7 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" dependencies = [ - "crypto-common", + "crypto-common 0.1.6", "subtle 2.6.0", ] diff --git a/crates/subspace-proof-of-time/Cargo.toml b/crates/subspace-proof-of-time/Cargo.toml index d4dae815e2..2e0117c4c3 100644 --- a/crates/subspace-proof-of-time/Cargo.toml +++ b/crates/subspace-proof-of-time/Cargo.toml @@ -15,7 +15,7 @@ include = [ bench = false [dependencies] -aes = "0.8.4" +aes = "0.9.0-pre.1" subspace-core-primitives = { version = "0.1.0", path = "../subspace-core-primitives", default-features = false } thiserror = { version = "1.0.61", optional = true } diff --git a/crates/subspace-proof-of-time/src/aes.rs b/crates/subspace-proof-of-time/src/aes.rs index 36cdc5f166..16d92caf19 100644 --- a/crates/subspace-proof-of-time/src/aes.rs +++ b/crates/subspace-proof-of-time/src/aes.rs @@ -7,8 +7,8 @@ mod x86_64; #[cfg(not(feature = "std"))] extern crate alloc; -use aes::cipher::generic_array::GenericArray; -use aes::cipher::{BlockDecrypt, BlockEncrypt, KeyInit}; +use aes::cipher::array::Array; +use aes::cipher::{BlockCipherDecrypt, BlockCipherEncrypt, KeyInit}; use aes::Aes128; use subspace_core_primitives::{PotCheckpoints, PotKey, PotOutput, PotSeed}; @@ -26,9 +26,9 @@ pub(crate) fn create(seed: PotSeed, key: PotKey, checkpoint_iterations: u32) -> #[cfg(any(not(target_arch = "x86_64"), test))] #[inline(always)] fn create_generic(seed: PotSeed, key: PotKey, checkpoint_iterations: u32) -> PotCheckpoints { - let key = GenericArray::from(*key); + let key = Array::from(*key); let cipher = Aes128::new(&key); - let mut cur_block = GenericArray::from(*seed); + let mut cur_block = Array::from(*seed); let mut checkpoints = PotCheckpoints::default(); for checkpoint in checkpoints.iter_mut() { @@ -54,17 +54,17 @@ pub(crate) fn verify_sequential( ) -> bool { assert_eq!(checkpoint_iterations % 2, 0); - let key = GenericArray::from(*key); + let key = Array::from(*key); let cipher = Aes128::new(&key); let mut inputs = Vec::with_capacity(checkpoints.len()); - inputs.push(GenericArray::from(*seed)); + inputs.push(Array::from(*seed)); for &checkpoint in checkpoints.iter().rev().skip(1).rev() { - inputs.push(GenericArray::from(*checkpoint)); + inputs.push(Array::from(*checkpoint)); } let mut outputs = checkpoints .iter() - .map(|&checkpoint| GenericArray::from(*checkpoint)) + .map(|&checkpoint| Array::from(*checkpoint)) .collect::>(); for _ in 0..checkpoint_iterations / 2 {