Skip to content

Commit

Permalink
Merge pull request #2956 from autonomys/pre-release-aes
Browse files Browse the repository at this point in the history
Switch to pre-release AES crate to remove workarounds for ARMv8
  • Loading branch information
nazar-pc committed Jul 31, 2024
2 parents dbdfee5 + 8a54587 commit 4433a77
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 30 deletions.
5 changes: 0 additions & 5 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -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"]
4 changes: 1 addition & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 1 addition & 6 deletions .github/workflows/snapshot-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
62 changes: 55 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/subspace-proof-of-time/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
16 changes: 8 additions & 8 deletions crates/subspace-proof-of-time/src/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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() {
Expand All @@ -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::<Vec<_>>();

for _ in 0..checkpoint_iterations / 2 {
Expand Down

0 comments on commit 4433a77

Please sign in to comment.