From c115799cb47047be59d740e72f792981204fefa8 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 13 Oct 2026 01:39:53 +0530 Subject: [PATCH 01/27] pow%build!: drop `aes_hw`, take hardware AES on aarch64 and x86_64 --- pkgs/pow/Cargo.toml | 3 +-- pkgs/pow/src/echo/simd.rs | 36 +++++++++++++++---------------- pkgs/pow/src/groestl/consts.rs | 8 +++---- pkgs/pow/src/groestl/simd.rs | 26 +++++++++++----------- pkgs/pow/src/shavite/simd.rs | 4 ++-- pkgs/pow/src/util/aes/aarch64.rs | 9 ++++---- pkgs/pow/src/util/aes/amd64.rs | 37 +++++--------------------------- pkgs/pow/src/util/aes/cpu.rs | 17 +++++++++------ pkgs/pow/src/util/aes/mod.rs | 12 +++-------- pkgs/pow/src/util/aes/simd.rs | 2 +- 10 files changed, 62 insertions(+), 92 deletions(-) diff --git a/pkgs/pow/Cargo.toml b/pkgs/pow/Cargo.toml index e7704a491..3ccc511be 100644 --- a/pkgs/pow/Cargo.toml +++ b/pkgs/pow/Cargo.toml @@ -15,8 +15,7 @@ bench = false [features] default = [] std = [] -full = ["std", "aes_hw", "simd"] -aes_hw = ["simd"] +full = ["std", "simd"] portable = [] simd = [] diff --git a/pkgs/pow/src/echo/simd.rs b/pkgs/pow/src/echo/simd.rs index 6341af36f..72fe69027 100644 --- a/pkgs/pow/src/echo/simd.rs +++ b/pkgs/pow/src/echo/simd.rs @@ -7,19 +7,19 @@ //! SIMD Echo-512 implementation. use super::consts::BLOCK; -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] use crate::util::aes::cpu::{round, round_nk}; -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] use crate::util::aes::simd::xtime_packed_u32; use crate::util::memops::{load_u32_le, store_u32_le}; -#[cfg(all(feature = "aes_hw", target_arch = "aarch64"))] +#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] use core::simd::Simd; type AesState = [u32; 4]; -#[cfg(all(feature = "aes_hw", target_arch = "aarch64"))] +#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] type CellVec = Simd; -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] type Grid = [AesState; 16]; type Counter = [u32; 4]; type ChainingValue = [AesState; 8]; @@ -44,7 +44,7 @@ fn increment_counter(counter: &mut Counter, value: u32) { } /// Builds the 16 round keys used by one substitution step. -#[cfg(all(feature = "aes_hw", target_arch = "aarch64"))] +#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] #[inline(always)] fn build_sub_word_keys(counter: &Counter) -> ([CellVec; 16], Counter) { let mut keys = [CellVec::splat(0); 16]; @@ -59,7 +59,7 @@ fn build_sub_word_keys(counter: &Counter) -> ([CellVec; 16], Counter) { } /// Builds the 16 round keys used by one substitution step. -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] fn build_sub_word_keys(counter: &Counter) -> ([AesState; 16], Counter) { let mut keys = [[0u32; 4]; 16]; @@ -74,7 +74,7 @@ fn build_sub_word_keys(counter: &Counter) -> ([AesState; 16], Counter) { } /// Runs the two AES-like rounds on each grid cell. -#[cfg(all(feature = "aes_hw", target_arch = "aarch64"))] +#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] #[inline(always)] fn apply_big_sub_words(grid: &mut [CellVec; 16], round_keys: &[CellVec; 16]) { use crate::util::aes::aarch64::{block_from_vec, round_block, round_nk_block, vec_from_block}; @@ -90,7 +90,7 @@ fn apply_big_sub_words(grid: &mut [CellVec; 16], round_keys: &[CellVec; 16]) { } /// Runs the two AES-like rounds on each grid cell. -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] fn apply_big_sub_words(grid: &mut Grid, round_keys: &[AesState; 16]) { let mut cell = 0; @@ -102,7 +102,7 @@ fn apply_big_sub_words(grid: &mut Grid, round_keys: &[AesState; 16]) { } /// Rotates the second, third, and fourth grid rows. -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] fn apply_big_shift_rows(grid: &mut Grid) { let saved = *grid; @@ -114,7 +114,7 @@ fn apply_big_shift_rows(grid: &mut Grid) { } /// Rotates the second, third, and fourth grid rows. -#[cfg(all(feature = "aes_hw", target_arch = "aarch64"))] +#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] #[inline(always)] fn apply_big_shift_rows_vec(grid: &mut [CellVec; 16]) { let saved = *grid; @@ -125,14 +125,14 @@ fn apply_big_shift_rows_vec(grid: &mut [CellVec; 16]) { } } -#[cfg(all(feature = "aes_hw", target_arch = "aarch64"))] +#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] #[inline(always)] fn xtime_packed_vec(word: CellVec) -> CellVec { (((word & CellVec::splat(0x80808080)) >> CellVec::splat(7)) * CellVec::splat(27)) ^ ((word & CellVec::splat(0x7F7F7F7F)) << CellVec::splat(1)) } -#[cfg(all(feature = "aes_hw", target_arch = "aarch64"))] +#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] #[inline(always)] fn mix_column_vec(grid: &mut [CellVec; 16], a: usize, b: usize, c: usize, d: usize) { let wa = grid[a]; @@ -151,7 +151,7 @@ fn mix_column_vec(grid: &mut [CellVec; 16], a: usize, b: usize, c: usize, d: usi grid[d] = ab2 ^ bc2 ^ cd2 ^ ab ^ wc; } -#[cfg(all(feature = "aes_hw", target_arch = "aarch64"))] +#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] #[inline(always)] fn apply_big_mix_columns_vec(grid: &mut [CellVec; 16]) { mix_column_vec(grid, 0, 1, 2, 3); @@ -161,7 +161,7 @@ fn apply_big_mix_columns_vec(grid: &mut [CellVec; 16]) { } /// Mixes one column of four AES states in `GF(2^8)`. -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] fn mix_column(grid: &mut Grid, a: usize, b: usize, c: usize, d: usize) { let mut word = 0; @@ -185,7 +185,7 @@ fn mix_column(grid: &mut Grid, a: usize, b: usize, c: usize, d: usize) { } /// Applies the column-mixing step to all four grid columns. -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] fn apply_big_mix_columns(grid: &mut Grid) { mix_column(grid, 0, 1, 2, 3); @@ -195,7 +195,7 @@ fn apply_big_mix_columns(grid: &mut Grid) { } /// Runs the compression function on one 1024-bit block. -#[cfg(all(feature = "aes_hw", target_arch = "aarch64"))] +#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] #[inline(always)] pub(super) fn compress_words(chaining_value: &mut ChainingValue, block: &BlockWords, counter: &Counter) { let mut grid = [CellVec::splat(0); 16]; @@ -239,7 +239,7 @@ pub(super) fn compress_words(chaining_value: &mut ChainingValue, block: &BlockWo } /// Runs the compression function on one 1024-bit block. -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] pub(super) fn compress_words(chaining_value: &mut ChainingValue, block: &BlockWords, counter: &Counter) { let mut grid = [[0u32; 4]; 16]; diff --git a/pkgs/pow/src/groestl/consts.rs b/pkgs/pow/src/groestl/consts.rs index dd44e764d..a5c5fd14c 100644 --- a/pkgs/pow/src/groestl/consts.rs +++ b/pkgs/pow/src/groestl/consts.rs @@ -101,7 +101,7 @@ pub(crate) const RC_Q: [[u8; 16]; ROUNDS] = { }; /// Arm AES ShiftRows source index for a 4x4 column-major row. -#[cfg(feature = "aes_hw")] +#[cfg(all(feature = "simd", target_arch = "aarch64", not(feature = "portable")))] const fn aes_shift_rows_source(dst: usize) -> usize { let row = dst % 4; let col = dst / 4; @@ -109,7 +109,7 @@ const fn aes_shift_rows_source(dst: usize) -> usize { } /// Fused SubBytes + ShiftBytes masks for the `P` permutation. -#[cfg(feature = "aes_hw")] +#[cfg(all(feature = "simd", target_arch = "aarch64", not(feature = "portable")))] pub(crate) const SUBSH_P: [[u8; 16]; 8] = { const SHIFTS: [usize; 8] = [0, 1, 2, 3, 4, 5, 6, 11]; let mut masks = [[0u8; 16]; 8]; @@ -126,7 +126,7 @@ pub(crate) const SUBSH_P: [[u8; 16]; 8] = { }; /// Fused SubBytes + ShiftBytes masks for the `Q` permutation. -#[cfg(feature = "aes_hw")] +#[cfg(all(feature = "simd", target_arch = "aarch64", not(feature = "portable")))] pub(crate) const SUBSH_Q: [[u8; 16]; 8] = { const SHIFTS: [usize; 8] = [1, 3, 5, 11, 0, 2, 4, 6]; let mut masks = [[0u8; 16]; 8]; @@ -191,7 +191,7 @@ mod tests { } } - #[cfg(feature = "aes_hw")] + #[cfg(all(feature = "simd", target_arch = "aarch64", not(feature = "portable")))] #[test] fn spot_check_subsh_p() { assert_eq!( diff --git a/pkgs/pow/src/groestl/simd.rs b/pkgs/pow/src/groestl/simd.rs index 9df048caa..9226b231c 100644 --- a/pkgs/pow/src/groestl/simd.rs +++ b/pkgs/pow/src/groestl/simd.rs @@ -7,13 +7,13 @@ //! SIMD Groestl-512 implementation. use super::consts::{BLOCK, IV_ROWS, RC_P, RC_Q, ROUNDS}; -#[cfg(all(feature = "aes_hw", target_arch = "aarch64"))] +#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] use super::consts::{SUBSH_P, SUBSH_Q}; -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] use crate::util::aes::consts::SBOX; use core::simd::num::{SimdInt, SimdUint}; -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] use core::simd::simd_swizzle; use core::simd::Simd; @@ -118,7 +118,7 @@ fn mix_bytes(rows: &mut [Row; 8]) { } /// Applies the AES S-box to every byte in every row. -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] fn sub_bytes(rows: &mut [Row; 8]) { let mut row = 0; @@ -134,7 +134,7 @@ fn sub_bytes(rows: &mut [Row; 8]) { } } -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] const fn rotate_left_indices(shift: usize) -> [usize; 16] { let mut indices = [0usize; 16]; let mut i = 0; @@ -145,7 +145,7 @@ const fn rotate_left_indices(shift: usize) -> [usize; 16] { indices } -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] macro_rules! shift_row { ($row:expr, $shift:expr) => {{ const PATTERN: [usize; 16] = rotate_left_indices($shift); @@ -154,7 +154,7 @@ macro_rules! shift_row { } /// Applies the `P` row shifts. -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] fn shift_bytes_p(rows: &mut [Row; 8]) { rows[1] = shift_row!(rows[1], 1); @@ -167,7 +167,7 @@ fn shift_bytes_p(rows: &mut [Row; 8]) { } /// Applies the `Q` row shifts. -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] fn shift_bytes_q(rows: &mut [Row; 8]) { rows[0] = shift_row!(rows[0], 1); @@ -198,7 +198,7 @@ fn add_rc_q(rows: &mut [Row; 8], round: usize) { } /// Applies substitution and row rotation in one helper on supported targets. -#[cfg(all(feature = "aes_hw", target_arch = "aarch64"))] +#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] #[expect(unsafe_code, reason = "direct vector intrinsics produce the right codegen")] #[inline(always)] fn sub_shift(rows: &mut [Row; 8], masks: &[[u8; 16]; 8]) { @@ -219,7 +219,7 @@ fn sub_shift(rows: &mut [Row; 8], masks: &[[u8; 16]; 8]) { } /// Applies the `P` permutation to the current state. -#[cfg(all(feature = "aes_hw", target_arch = "aarch64"))] +#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] #[inline(always)] fn perm_p(state: &mut [Row; 8]) { let mut round = 0; @@ -232,7 +232,7 @@ fn perm_p(state: &mut [Row; 8]) { } /// Applies the `P` permutation to the current state. -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] fn perm_p(state: &mut [Row; 8]) { let mut round = 0; @@ -246,7 +246,7 @@ fn perm_p(state: &mut [Row; 8]) { } /// Applies the `P` and `Q` permutations in lockstep for one compression. -#[cfg(all(feature = "aes_hw", target_arch = "aarch64"))] +#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] #[inline(always)] fn perm_pq(p_state: &mut [Row; 8], q_state: &mut [Row; 8]) { let mut round = 0; @@ -262,7 +262,7 @@ fn perm_pq(p_state: &mut [Row; 8], q_state: &mut [Row; 8]) { } /// Applies the `P` and `Q` permutations in lockstep for one compression. -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] fn perm_pq(p_state: &mut [Row; 8], q_state: &mut [Row; 8]) { let mut round = 0; diff --git a/pkgs/pow/src/shavite/simd.rs b/pkgs/pow/src/shavite/simd.rs index 6e5c6cbf5..eb8976537 100644 --- a/pkgs/pow/src/shavite/simd.rs +++ b/pkgs/pow/src/shavite/simd.rs @@ -171,7 +171,7 @@ fn inject_counter_words(bundle_words: &mut Bundle, counter: &[u32; 4], injection } /// Runs the 4-round AES bundle that updates one 4-word half. -#[cfg(all(feature = "aes_hw", target_arch = "aarch64"))] +#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] #[inline(always)] fn apply_round_bundle(state: &mut State, left: [usize; 4], right: [usize; 4], keys: &[Bundle]) { use crate::util::aes::aarch64::{load_state, round_nk_block, store_state, xor_block}; @@ -194,7 +194,7 @@ fn apply_round_bundle(state: &mut State, left: [usize; 4], right: [usize; 4], ke } /// Runs the 4-round AES bundle that updates one 4-word half. -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] fn apply_round_bundle(state: &mut State, left: [usize; 4], right: [usize; 4], keys: &[Bundle]) { // Each bundle reads one 4-word half, runs four AES-like rounds, then xors diff --git a/pkgs/pow/src/util/aes/aarch64.rs b/pkgs/pow/src/util/aes/aarch64.rs index b8a699881..0ffcbe0be 100644 --- a/pkgs/pow/src/util/aes/aarch64.rs +++ b/pkgs/pow/src/util/aes/aarch64.rs @@ -7,7 +7,6 @@ //! aarch64 AES helpers for the fused Groestl round path and packed AES rounds. use core::arch::aarch64::{uint8x16_t, vaeseq_u8, vaesmcq_u8, veorq_u8, vqtbl1q_u8}; -#[cfg(feature = "simd")] use core::simd::Simd; type AesState = [u32; 4]; @@ -25,13 +24,11 @@ pub(crate) fn store_state(state: AesBlock) -> AesState { unsafe { core::mem::transmute(state) } } -#[cfg(feature = "simd")] #[inline(always)] pub(crate) fn block_from_vec(state: Simd) -> AesBlock { unsafe { core::mem::transmute(state) } } -#[cfg(feature = "simd")] #[inline(always)] pub(crate) fn vec_from_block(state: AesBlock) -> Simd { unsafe { core::mem::transmute(state) } @@ -56,7 +53,8 @@ unsafe fn round_block_impl(state: AesBlock, key: AesBlock) -> AesBlock { #[inline(always)] pub(crate) fn round_block(state: AesBlock, key: AesBlock) -> AesBlock { - // Safety: this module is only compiled with `aes_hw` + `aarch64`. + // Safety: the parent module skips this module under `portable`, and `lib.rs` + // requires the `aes` target feature otherwise. unsafe { round_block_impl(state, key) } } @@ -74,7 +72,8 @@ unsafe fn round_nk_block_impl(state: AesBlock) -> AesBlock { #[inline(always)] pub(crate) fn round_nk_block(state: AesBlock) -> AesBlock { - // Safety: this module is only compiled with `aes_hw` + `aarch64`. + // Safety: the parent module skips this module under `portable`, and `lib.rs` + // requires the `aes` target feature otherwise. unsafe { round_nk_block_impl(state) } } diff --git a/pkgs/pow/src/util/aes/amd64.rs b/pkgs/pow/src/util/aes/amd64.rs index 011884eba..3b647291b 100644 --- a/pkgs/pow/src/util/aes/amd64.rs +++ b/pkgs/pow/src/util/aes/amd64.rs @@ -10,9 +10,7 @@ //! performs the full AES encryption round (SubBytes + ShiftRows + MixColumns + //! AddRoundKey) in a single instruction. -use core::arch::x86_64::{ - __m128i, _mm_aesenc_si128, _mm_aesenclast_si128, _mm_set_epi32, _mm_setzero_si128, _mm_shuffle_epi8, -}; +use core::arch::x86_64::{__m128i, _mm_aesenc_si128, _mm_set_epi32, _mm_setzero_si128}; #[inline] fn to_m128i(v: &[u32; 4]) -> __m128i { @@ -41,7 +39,8 @@ unsafe fn round_impl(state: &[u32; 4], key: &[u32; 4]) -> [u32; 4] { /// AES encryption round #[inline] pub(crate) fn round(state: &[u32; 4], key: &[u32; 4]) -> [u32; 4] { - // Safety: this module is only compiled with `aes_hw` + `x86_64`. + // Safety: the parent module skips this module under `portable`, and `lib.rs` + // requires the `aes` target feature otherwise. unsafe { round_impl(state, key) } } @@ -55,33 +54,7 @@ unsafe fn round_nk_impl(state: &[u32; 4]) -> [u32; 4] { /// AES round without AddRoundKey (key = 0). #[inline] pub(crate) fn round_nk(state: &[u32; 4]) -> [u32; 4] { - // Safety: this module is only compiled with `aes_hw` + `x86_64`. + // Safety: the parent module skips this module under `portable`, and `lib.rs` + // requires the `aes` target feature otherwise. unsafe { round_nk_impl(state) } } - -/// Inverse AES ShiftRows shuffle for _mm_shuffle_epi8. -#[cfg(test)] -const INV_SHIFT_ROWS: [u8; 16] = [0, 13, 10, 7, 4, 1, 14, 11, 8, 5, 2, 15, 12, 9, 6, 3]; - -/// Applies the AES S-box to all 16 bytes independently. -/// -/// Uses aesenclast (SubBytes + ShiftRows + XOR key, no -/// MixColumns) with zero key, then undoes AES ShiftRows -/// via pshufb. Used by Groestl. -#[cfg(test)] -#[inline] -#[target_feature(enable = "aes,sse2,ssse3")] -unsafe fn sub_bytes_impl(state: &[u8; 16]) -> [u8; 16] { - let s: __m128i = core::mem::transmute::<[u8; 16], __m128i>(*state); - let zero = _mm_setzero_si128(); - let tbl: __m128i = core::mem::transmute::<[u8; 16], __m128i>(INV_SHIFT_ROWS); - let after = _mm_aesenclast_si128(s, zero); - core::mem::transmute::<__m128i, [u8; 16]>(_mm_shuffle_epi8(after, tbl)) -} - -#[cfg(test)] -#[inline] -pub(crate) fn sub_bytes(state: &[u8; 16]) -> [u8; 16] { - // Safety: this module is only compiled with `aes_hw` + `x86_64`. - unsafe { sub_bytes_impl(state) } -} diff --git a/pkgs/pow/src/util/aes/cpu.rs b/pkgs/pow/src/util/aes/cpu.rs index 33ac5fd4d..16397d92d 100644 --- a/pkgs/pow/src/util/aes/cpu.rs +++ b/pkgs/pow/src/util/aes/cpu.rs @@ -4,14 +4,19 @@ // See the accompanying file LICENSE or https://opensource.org/license/MIT // -//! Best available AES round at runtime. +//! Best available AES round. //! -//! Hardware-accelerated on aarch64 with `aes_hw`, scalar T-table fallback -//! otherwise. +//! Hardware AES on aarch64 and x86_64 with `simd` unless `portable` is +//! enabled, scalar T-table fallback otherwise. -#[cfg(all(feature = "aes_hw", target_arch = "aarch64", test))] +#[cfg(all(feature = "simd", target_arch = "aarch64", test, not(feature = "portable")))] pub(crate) use super::aarch64::round; -#[cfg(all(feature = "aes_hw", target_arch = "aarch64"))] +#[cfg(all(feature = "simd", target_arch = "aarch64", not(feature = "portable")))] pub(crate) use super::aarch64::round_nk; -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(all(feature = "simd", target_arch = "x86_64", not(feature = "portable")))] +pub(crate) use super::amd64::{round, round_nk}; +#[cfg(any( + feature = "portable", + not(all(feature = "simd", any(target_arch = "aarch64", target_arch = "x86_64"))) +))] pub(crate) use super::scalar::{round, round_nk}; diff --git a/pkgs/pow/src/util/aes/mod.rs b/pkgs/pow/src/util/aes/mod.rs index c51738126..236bdca2d 100644 --- a/pkgs/pow/src/util/aes/mod.rs +++ b/pkgs/pow/src/util/aes/mod.rs @@ -5,19 +5,13 @@ // //! AES round primitives for Echo, Groestl, and SHAvite. -//! -//! Two layers: -//! - **scalar**: `const fn` T-table lookups, used by scalar reference -//! implementations and for compile-time evaluation. -//! - **cpu**: best available runtime backend. Hardware AES when `aes_hw`, -//! scalar fallback otherwise. Used by SIMD implementations. -#[cfg(all(feature = "aes_hw", target_arch = "aarch64"))] +#[cfg(all(feature = "simd", target_arch = "aarch64", not(feature = "portable")))] #[expect(unsafe_code, reason = "intrinsics")] pub(crate) mod aarch64; -#[cfg(all(feature = "aes_hw", target_arch = "x86_64"))] +#[cfg(all(feature = "simd", target_arch = "x86_64", not(feature = "portable")))] #[expect(unsafe_code, reason = "intrinsics")] -pub(crate) mod amd64; +mod amd64; pub(crate) mod consts; #[cfg(any(test, feature = "simd"))] pub(crate) mod cpu; diff --git a/pkgs/pow/src/util/aes/simd.rs b/pkgs/pow/src/util/aes/simd.rs index fa75e6cdc..726bc2022 100644 --- a/pkgs/pow/src/util/aes/simd.rs +++ b/pkgs/pow/src/util/aes/simd.rs @@ -7,7 +7,7 @@ //! AES helpers for the SIMD runtime path. /// Multiplies each byte in a packed `u32` by `x` in `GF(2^8)`. -#[cfg(not(all(feature = "aes_hw", target_arch = "aarch64")))] +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] pub(crate) fn xtime_packed_u32(word: u32) -> u32 { ((word & 0x80808080) >> 7).wrapping_mul(27) ^ ((word & 0x7F7F7F7F) << 1) From e7a384a1f89f0a49fb43b65dbf145cbfc7146436 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Wed, 14 Oct 2026 02:17:04 +0530 Subject: [PATCH 02/27] pow%refac(aes): move AES from `util` to the crate root `git diff --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space` --- pkgs/pow/src/{util => }/aes/aarch64.rs | 0 pkgs/pow/src/{util => }/aes/amd64.rs | 0 pkgs/pow/src/{util => }/aes/consts.rs | 0 pkgs/pow/src/{util => }/aes/cpu.rs | 0 pkgs/pow/src/{util => }/aes/mod.rs | 0 pkgs/pow/src/{util => }/aes/scalar.rs | 0 pkgs/pow/src/{util => }/aes/simd.rs | 0 pkgs/pow/src/echo/scalar.rs | 2 +- pkgs/pow/src/echo/simd.rs | 6 +++--- pkgs/pow/src/groestl/consts.rs | 2 +- pkgs/pow/src/groestl/simd.rs | 4 ++-- pkgs/pow/src/lib.rs | 1 + pkgs/pow/src/shavite/scalar.rs | 2 +- pkgs/pow/src/shavite/simd.rs | 4 ++-- pkgs/pow/src/util/mod.rs | 1 - 15 files changed, 11 insertions(+), 11 deletions(-) rename pkgs/pow/src/{util => }/aes/aarch64.rs (100%) rename pkgs/pow/src/{util => }/aes/amd64.rs (100%) rename pkgs/pow/src/{util => }/aes/consts.rs (100%) rename pkgs/pow/src/{util => }/aes/cpu.rs (100%) rename pkgs/pow/src/{util => }/aes/mod.rs (100%) rename pkgs/pow/src/{util => }/aes/scalar.rs (100%) rename pkgs/pow/src/{util => }/aes/simd.rs (100%) diff --git a/pkgs/pow/src/util/aes/aarch64.rs b/pkgs/pow/src/aes/aarch64.rs similarity index 100% rename from pkgs/pow/src/util/aes/aarch64.rs rename to pkgs/pow/src/aes/aarch64.rs diff --git a/pkgs/pow/src/util/aes/amd64.rs b/pkgs/pow/src/aes/amd64.rs similarity index 100% rename from pkgs/pow/src/util/aes/amd64.rs rename to pkgs/pow/src/aes/amd64.rs diff --git a/pkgs/pow/src/util/aes/consts.rs b/pkgs/pow/src/aes/consts.rs similarity index 100% rename from pkgs/pow/src/util/aes/consts.rs rename to pkgs/pow/src/aes/consts.rs diff --git a/pkgs/pow/src/util/aes/cpu.rs b/pkgs/pow/src/aes/cpu.rs similarity index 100% rename from pkgs/pow/src/util/aes/cpu.rs rename to pkgs/pow/src/aes/cpu.rs diff --git a/pkgs/pow/src/util/aes/mod.rs b/pkgs/pow/src/aes/mod.rs similarity index 100% rename from pkgs/pow/src/util/aes/mod.rs rename to pkgs/pow/src/aes/mod.rs diff --git a/pkgs/pow/src/util/aes/scalar.rs b/pkgs/pow/src/aes/scalar.rs similarity index 100% rename from pkgs/pow/src/util/aes/scalar.rs rename to pkgs/pow/src/aes/scalar.rs diff --git a/pkgs/pow/src/util/aes/simd.rs b/pkgs/pow/src/aes/simd.rs similarity index 100% rename from pkgs/pow/src/util/aes/simd.rs rename to pkgs/pow/src/aes/simd.rs diff --git a/pkgs/pow/src/echo/scalar.rs b/pkgs/pow/src/echo/scalar.rs index e2924fd42..388b77582 100644 --- a/pkgs/pow/src/echo/scalar.rs +++ b/pkgs/pow/src/echo/scalar.rs @@ -7,7 +7,7 @@ //! Scalar Echo-512 implementation. use super::consts::BLOCK; -use crate::util::aes::{round, round_nk}; +use crate::aes::{round, round_nk}; use crate::util::memops::{extract, load_u32_le, store_u32_le}; /// Increments a 128-bit counter by `val`. diff --git a/pkgs/pow/src/echo/simd.rs b/pkgs/pow/src/echo/simd.rs index 72fe69027..a13c2efa7 100644 --- a/pkgs/pow/src/echo/simd.rs +++ b/pkgs/pow/src/echo/simd.rs @@ -8,9 +8,9 @@ use super::consts::BLOCK; #[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -use crate::util::aes::cpu::{round, round_nk}; +use crate::aes::cpu::{round, round_nk}; #[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -use crate::util::aes::simd::xtime_packed_u32; +use crate::aes::simd::xtime_packed_u32; use crate::util::memops::{load_u32_le, store_u32_le}; #[cfg(all(target_arch = "aarch64", not(feature = "portable")))] @@ -77,7 +77,7 @@ fn build_sub_word_keys(counter: &Counter) -> ([AesState; 16], Counter) { #[cfg(all(target_arch = "aarch64", not(feature = "portable")))] #[inline(always)] fn apply_big_sub_words(grid: &mut [CellVec; 16], round_keys: &[CellVec; 16]) { - use crate::util::aes::aarch64::{block_from_vec, round_block, round_nk_block, vec_from_block}; + use crate::aes::aarch64::{block_from_vec, round_block, round_nk_block, vec_from_block}; let mut cell = 0; while cell < 16 { diff --git a/pkgs/pow/src/groestl/consts.rs b/pkgs/pow/src/groestl/consts.rs index a5c5fd14c..8c63daf06 100644 --- a/pkgs/pow/src/groestl/consts.rs +++ b/pkgs/pow/src/groestl/consts.rs @@ -6,7 +6,7 @@ //! Groestl-512 constants. -use crate::util::aes::consts::SBOX; +use crate::aes::consts::SBOX; use crate::util::math::gf2_mul; pub(crate) const BLOCK: usize = 128; diff --git a/pkgs/pow/src/groestl/simd.rs b/pkgs/pow/src/groestl/simd.rs index 9226b231c..06a0fb45f 100644 --- a/pkgs/pow/src/groestl/simd.rs +++ b/pkgs/pow/src/groestl/simd.rs @@ -10,7 +10,7 @@ use super::consts::{BLOCK, IV_ROWS, RC_P, RC_Q, ROUNDS}; #[cfg(all(target_arch = "aarch64", not(feature = "portable")))] use super::consts::{SUBSH_P, SUBSH_Q}; #[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -use crate::util::aes::consts::SBOX; +use crate::aes::consts::SBOX; use core::simd::num::{SimdInt, SimdUint}; #[cfg(any(feature = "portable", not(target_arch = "aarch64")))] @@ -202,7 +202,7 @@ fn add_rc_q(rows: &mut [Row; 8], round: usize) { #[expect(unsafe_code, reason = "direct vector intrinsics produce the right codegen")] #[inline(always)] fn sub_shift(rows: &mut [Row; 8], masks: &[[u8; 16]; 8]) { - use crate::util::aes::aarch64::sub_shift as hw_sub_shift; + use crate::aes::aarch64::sub_shift as hw_sub_shift; use core::arch::aarch64::uint8x16_t; diff --git a/pkgs/pow/src/lib.rs b/pkgs/pow/src/lib.rs index 942bff88f..cc18c23fb 100644 --- a/pkgs/pow/src/lib.rs +++ b/pkgs/pow/src/lib.rs @@ -50,6 +50,7 @@ compile_error!("dash-pow requires NEON and AES, build with `-C target-feature=+n ))] compile_error!("dash-pow requires 128-bit SIMD, build with `-C target-feature=+simd128` or enable `portable`"); +mod aes; mod blake; mod bmw; mod cubehash; diff --git a/pkgs/pow/src/shavite/scalar.rs b/pkgs/pow/src/shavite/scalar.rs index ec22258ee..28dc2447a 100644 --- a/pkgs/pow/src/shavite/scalar.rs +++ b/pkgs/pow/src/shavite/scalar.rs @@ -7,7 +7,7 @@ //! Scalar SHAvite-3-512 implementation. use super::consts::{BLOCK, IV}; -use crate::util::aes::round_nk; +use crate::aes::round_nk; use crate::util::memops::{extract, load_u32_le, store_u32_le}; /// Expands 128-byte message block into 448 round keys. diff --git a/pkgs/pow/src/shavite/simd.rs b/pkgs/pow/src/shavite/simd.rs index eb8976537..85527529e 100644 --- a/pkgs/pow/src/shavite/simd.rs +++ b/pkgs/pow/src/shavite/simd.rs @@ -7,7 +7,7 @@ //! SIMD SHAvite-3-512 implementation. use super::consts::{BLOCK, IV}; -use crate::util::aes::cpu::round_nk; +use crate::aes::cpu::round_nk; use crate::util::memops::{load_u32_le, store_u32_le}; const SCHEDULE_BUNDLES: usize = 112; @@ -174,7 +174,7 @@ fn inject_counter_words(bundle_words: &mut Bundle, counter: &[u32; 4], injection #[cfg(all(target_arch = "aarch64", not(feature = "portable")))] #[inline(always)] fn apply_round_bundle(state: &mut State, left: [usize; 4], right: [usize; 4], keys: &[Bundle]) { - use crate::util::aes::aarch64::{load_state, round_nk_block, store_state, xor_block}; + use crate::aes::aarch64::{load_state, round_nk_block, store_state, xor_block}; // The bundle always stays in one 128-bit lane. Keeping it packed across the // four rounds avoids rebuilding the lane after every round. diff --git a/pkgs/pow/src/util/mod.rs b/pkgs/pow/src/util/mod.rs index 90580e4d0..c0230c0fd 100644 --- a/pkgs/pow/src/util/mod.rs +++ b/pkgs/pow/src/util/mod.rs @@ -6,7 +6,6 @@ //! Shared utilities. -pub(crate) mod aes; pub(crate) mod arx; pub(crate) mod math; #[allow(dead_code, reason = "contains unopinionated general helpers that may never see use")] From 177d32ef0c4301710f9bea85db082486b357cb63 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Sun, 11 Oct 2026 01:33:38 +0530 Subject: [PATCH 03/27] pow%refac(aes): put every AES backend behind one block interface --- Cargo.lock | 26 ++++++ Cargo.toml | 1 + docs/samples/Cargo.lock | 28 ++++++ pkgs/pow/Cargo.toml | 5 +- pkgs/pow/src/aes/aarch64.rs | 151 +++++++++++++++++++-------------- pkgs/pow/src/aes/amd64.rs | 61 ++++--------- pkgs/pow/src/aes/cpu.rs | 33 +++---- pkgs/pow/src/aes/mod.rs | 141 ++++++++++++++++++------------ pkgs/pow/src/aes/scalar.rs | 18 ++++ pkgs/pow/src/echo/scalar.rs | 2 +- pkgs/pow/src/lib.rs | 4 + pkgs/pow/src/shavite/scalar.rs | 2 +- 12 files changed, 290 insertions(+), 182 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e1de6a457..eb5922014 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -187,6 +187,12 @@ version = "3.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" +[[package]] +name = "bytemuck" +version = "1.25.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95832e849adfb21180ccb6826a99da14e5d266ae5c2e668e1602cf234f153797" + [[package]] name = "cargo-lock" version = "10.1.0" @@ -526,6 +532,7 @@ dependencies = [ "divan", "hex-conservative 1.3.0", "rstest", + "wide", ] [[package]] @@ -1431,6 +1438,15 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" +[[package]] +name = "safe_arch" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42c6efa15875e6ecb39ca61fb0b0c1a40b84fac5a5ffe71eef7d1000c8eb3f5f" +dependencies = [ + "bytemuck", +] + [[package]] name = "secp256k1" version = "0.33.1" @@ -1833,6 +1849,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "wide" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d920ac99c3c8edce110cb8d07dbb324d6d026011dce85b1e9355b70f0adacc4f" +dependencies = [ + "bytemuck", + "safe_arch", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 4d288953e..809eee7d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ rstest = "0.25" serde = { version = "1", default-features = false } subtle = { version = "2.6.1", default-features = false } thiserror = { version = "2", default-features = false } +wide = { version = "1.7.1", default-features = false } zeroize = { version = "1.9.0", default-features = false } [workspace.lints.clippy] diff --git a/docs/samples/Cargo.lock b/docs/samples/Cargo.lock index 8ff070794..10385c8d5 100644 --- a/docs/samples/Cargo.lock +++ b/docs/samples/Cargo.lock @@ -141,6 +141,12 @@ version = "3.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" +[[package]] +name = "bytemuck" +version = "1.25.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95832e849adfb21180ccb6826a99da14e5d266ae5c2e668e1602cf234f153797" + [[package]] name = "cc" version = "1.8.0" @@ -230,6 +236,9 @@ dependencies = [ [[package]] name = "dash-pow" version = "0.0.0" +dependencies = [ + "wide", +] [[package]] name = "dash-primitives" @@ -499,6 +508,15 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" +[[package]] +name = "safe_arch" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42c6efa15875e6ecb39ca61fb0b0c1a40b84fac5a5ffe71eef7d1000c8eb3f5f" +dependencies = [ + "bytemuck", +] + [[package]] name = "secp256k1" version = "0.33.1" @@ -691,6 +709,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "wide" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d920ac99c3c8edce110cb8d07dbb324d6d026011dce85b1e9355b70f0adacc4f" +dependencies = [ + "bytemuck", + "safe_arch", +] + [[package]] name = "xxhash-rust" version = "0.8.19" diff --git a/pkgs/pow/Cargo.toml b/pkgs/pow/Cargo.toml index 3ccc511be..a2e172e7c 100644 --- a/pkgs/pow/Cargo.toml +++ b/pkgs/pow/Cargo.toml @@ -14,11 +14,14 @@ bench = false [features] default = [] -std = [] +std = ["wide/std"] full = ["std", "simd"] portable = [] simd = [] +[dependencies] +wide = { workspace = true } + [dev-dependencies] dash-dev = { version = "0.0.0", path = "../dev", features = ["full"] } divan = { workspace = true } diff --git a/pkgs/pow/src/aes/aarch64.rs b/pkgs/pow/src/aes/aarch64.rs index 0ffcbe0be..0cc2783db 100644 --- a/pkgs/pow/src/aes/aarch64.rs +++ b/pkgs/pow/src/aes/aarch64.rs @@ -4,88 +4,111 @@ // See the accompanying file LICENSE or https://opensource.org/license/MIT // -//! aarch64 AES helpers for the fused Groestl round path and packed AES rounds. +//! AES rounds via the Armv8 Cryptographic Extension. -use core::arch::aarch64::{uint8x16_t, vaeseq_u8, vaesmcq_u8, veorq_u8, vqtbl1q_u8}; -use core::simd::Simd; +#[cfg(feature = "simd")] +pub(crate) use self::packed::{ + block_from_vec, load_state, round_block, round_nk_block, store_state, sub_shift, vec_from_block, xor_block, +}; +use super::Block; -type AesState = [u32; 4]; -pub(crate) type AesBlock = uint8x16_t; +use core::arch::aarch64::{uint8x16_t, vaeseq_u8, vaesmcq_u8, vdupq_n_u8}; #[inline(always)] -pub(crate) fn load_state(state: &AesState) -> AesBlock { - // On little-endian targets the packed-word layout already matches the byte - // layout expected by the AES instructions, so the state can stay packed. - unsafe { core::mem::transmute(*state) } +fn to_native(block: Block) -> uint8x16_t { + // SAFETY: `Block` and `uint8x16_t` are 16 bytes and accept any bit pattern. + unsafe { core::mem::transmute::(block) } } #[inline(always)] -pub(crate) fn store_state(state: AesBlock) -> AesState { - unsafe { core::mem::transmute(state) } +fn from_native(block: uint8x16_t) -> Block { + // SAFETY: `Block` and `uint8x16_t` are 16 bytes and accept any bit pattern. + unsafe { core::mem::transmute::(block) } } +/// Runs SubBytes, ShiftRows and MixColumns, then xors in `key`. +/// +/// `aese` xors its key before SubBytes, so the key goes in after `aesmc`. +/// LLVM folds that xor into the next `aese` when rounds are chained. #[inline(always)] -pub(crate) fn block_from_vec(state: Simd) -> AesBlock { - unsafe { core::mem::transmute(state) } +pub(crate) fn round(state: Block, key: Block) -> Block { + // SAFETY: `aes::mod` skips this module under `portable`, and `lib.rs` + // requires the `neon` and `aes` target features otherwise. + let mixed = unsafe { vaesmcq_u8(vaeseq_u8(to_native(state), vdupq_n_u8(0))) }; + from_native(mixed) ^ key } -#[inline(always)] -pub(crate) fn vec_from_block(state: AesBlock) -> Simd { - unsafe { core::mem::transmute(state) } -} +/// Helpers on packed words and `core::simd` vectors for the SIMD backends. +#[cfg(feature = "simd")] +mod packed { + use core::arch::aarch64::{uint8x16_t, vaeseq_u8, vaesmcq_u8, veorq_u8, vqtbl1q_u8}; + use core::simd::Simd; -/// Applies Groestl's row shift, then AES SubBytes, in one instruction pair. -/// -/// The mask first reshuffles the row so the following AES ShiftRows step lands -/// on Groestl's target byte positions instead of AES's own pattern. -#[inline] -#[target_feature(enable = "neon,aes")] -pub(crate) unsafe fn sub_shift(row: AesBlock, mask: AesBlock, zero: AesBlock) -> AesBlock { - vaeseq_u8(vqtbl1q_u8(row, mask), zero) -} + type AesState = [u32; 4]; + pub(crate) type AesBlock = uint8x16_t; -#[inline] -#[target_feature(enable = "neon,aes")] -unsafe fn round_block_impl(state: AesBlock, key: AesBlock) -> AesBlock { - let zero = core::mem::zeroed::(); - veorq_u8(vaesmcq_u8(vaeseq_u8(state, zero)), key) -} + #[inline(always)] + pub(crate) fn load_state(state: &AesState) -> AesBlock { + // On little-endian targets the packed-word layout already matches the byte + // layout expected by the AES instructions, so the state can stay packed. + unsafe { core::mem::transmute(*state) } + } -#[inline(always)] -pub(crate) fn round_block(state: AesBlock, key: AesBlock) -> AesBlock { - // Safety: the parent module skips this module under `portable`, and `lib.rs` - // requires the `aes` target feature otherwise. - unsafe { round_block_impl(state, key) } -} + #[inline(always)] + pub(crate) fn store_state(state: AesBlock) -> AesState { + unsafe { core::mem::transmute(state) } + } -#[inline(always)] -pub(crate) fn xor_block(state: AesBlock, key: AesBlock) -> AesBlock { - unsafe { veorq_u8(state, key) } -} + #[inline(always)] + pub(crate) fn block_from_vec(state: Simd) -> AesBlock { + unsafe { core::mem::transmute(state) } + } -#[inline] -#[target_feature(enable = "neon,aes")] -unsafe fn round_nk_block_impl(state: AesBlock) -> AesBlock { - let zero = core::mem::zeroed::(); - vaesmcq_u8(vaeseq_u8(state, zero)) -} + #[inline(always)] + pub(crate) fn vec_from_block(state: AesBlock) -> Simd { + unsafe { core::mem::transmute(state) } + } -#[inline(always)] -pub(crate) fn round_nk_block(state: AesBlock) -> AesBlock { - // Safety: the parent module skips this module under `portable`, and `lib.rs` - // requires the `aes` target feature otherwise. - unsafe { round_nk_block_impl(state) } -} + /// Applies Groestl's row shift, then AES SubBytes, in one instruction pair. + /// + /// The mask first reshuffles the row so the following AES ShiftRows step + /// lands on Groestl's target byte positions instead of AES's own pattern. + #[inline] + #[target_feature(enable = "neon,aes")] + pub(crate) unsafe fn sub_shift(row: AesBlock, mask: AesBlock, zero: AesBlock) -> AesBlock { + vaeseq_u8(vqtbl1q_u8(row, mask), zero) + } -/// Runs one AES round on four packed words. -#[cfg_attr(not(test), allow(dead_code))] -#[inline(always)] -pub(crate) fn round(state: &AesState, key: &AesState) -> AesState { - store_state(round_block(load_state(state), load_state(key))) -} + #[inline] + #[target_feature(enable = "neon,aes")] + unsafe fn round_block_impl(state: AesBlock, key: AesBlock) -> AesBlock { + let zero = core::mem::zeroed::(); + veorq_u8(vaesmcq_u8(vaeseq_u8(state, zero)), key) + } -/// Runs one AES round with a zero round key. -#[inline(always)] -pub(crate) fn round_nk(state: &AesState) -> AesState { - store_state(round_nk_block(load_state(state))) + #[inline(always)] + pub(crate) fn round_block(state: AesBlock, key: AesBlock) -> AesBlock { + // Safety: `aes::mod` skips this module under `portable`, and `lib.rs` + // requires the `aes` target feature otherwise. + unsafe { round_block_impl(state, key) } + } + + #[inline(always)] + pub(crate) fn xor_block(state: AesBlock, key: AesBlock) -> AesBlock { + unsafe { veorq_u8(state, key) } + } + + #[inline] + #[target_feature(enable = "neon,aes")] + unsafe fn round_nk_block_impl(state: AesBlock) -> AesBlock { + let zero = core::mem::zeroed::(); + vaesmcq_u8(vaeseq_u8(state, zero)) + } + + #[inline(always)] + pub(crate) fn round_nk_block(state: AesBlock) -> AesBlock { + // Safety: `aes::mod` skips this module under `portable`, and `lib.rs` + // requires the `aes` target feature otherwise. + unsafe { round_nk_block_impl(state) } + } } diff --git a/pkgs/pow/src/aes/amd64.rs b/pkgs/pow/src/aes/amd64.rs index 3b647291b..408611ecd 100644 --- a/pkgs/pow/src/aes/amd64.rs +++ b/pkgs/pow/src/aes/amd64.rs @@ -4,57 +4,28 @@ // See the accompanying file LICENSE or https://opensource.org/license/MIT // -//! AES round via x86_64 AES-NI instructions. -//! -//! Requires AES-NI + SSE2 (`aes` and `sse2` target features). _mm_aesenc_si128 -//! performs the full AES encryption round (SubBytes + ShiftRows + MixColumns + -//! AddRoundKey) in a single instruction. +//! AES rounds via AES-NI. -use core::arch::x86_64::{__m128i, _mm_aesenc_si128, _mm_set_epi32, _mm_setzero_si128}; +use super::Block; -#[inline] -fn to_m128i(v: &[u32; 4]) -> __m128i { - // SAFETY: _mm_set_epi32 is always safe with sse2. - unsafe { _mm_set_epi32(v[3] as i32, v[2] as i32, v[1] as i32, v[0] as i32) } -} - -#[inline] -fn from_m128i(v: __m128i) -> [u32; 4] { - let mut out = [0u32; 4]; - // SAFETY: __m128i and [u32; 4] are both 16 bytes on LE. - unsafe { - core::ptr::copy_nonoverlapping(&v as *const __m128i as *const u8, out.as_mut_ptr() as *mut u8, 16); - } - out -} +use core::arch::x86_64::{__m128i, _mm_aesenc_si128}; -#[inline] -#[target_feature(enable = "aes,sse2")] -unsafe fn round_impl(state: &[u32; 4], key: &[u32; 4]) -> [u32; 4] { - let s = to_m128i(state); - let k = to_m128i(key); - from_m128i(_mm_aesenc_si128(s, k)) -} - -/// AES encryption round -#[inline] -pub(crate) fn round(state: &[u32; 4], key: &[u32; 4]) -> [u32; 4] { - // Safety: the parent module skips this module under `portable`, and `lib.rs` - // requires the `aes` target feature otherwise. - unsafe { round_impl(state, key) } +#[inline(always)] +fn to_native(block: Block) -> __m128i { + // SAFETY: `Block` and `__m128i` are 16 bytes and accept any bit pattern. + unsafe { core::mem::transmute::(block) } } -#[inline] -#[target_feature(enable = "aes,sse2")] -unsafe fn round_nk_impl(state: &[u32; 4]) -> [u32; 4] { - let s = to_m128i(state); - from_m128i(_mm_aesenc_si128(s, _mm_setzero_si128())) +#[inline(always)] +fn from_native(block: __m128i) -> Block { + // SAFETY: `Block` and `__m128i` are 16 bytes and accept any bit pattern. + unsafe { core::mem::transmute::<__m128i, Block>(block) } } -/// AES round without AddRoundKey (key = 0). -#[inline] -pub(crate) fn round_nk(state: &[u32; 4]) -> [u32; 4] { - // Safety: the parent module skips this module under `portable`, and `lib.rs` +/// Runs SubBytes, ShiftRows and MixColumns, then xors in `key`. +#[inline(always)] +pub(crate) fn round(state: Block, key: Block) -> Block { + // SAFETY: `aes::mod` skips this module under `portable`, and `lib.rs` // requires the `aes` target feature otherwise. - unsafe { round_nk_impl(state) } + from_native(unsafe { _mm_aesenc_si128(to_native(state), to_native(key)) }) } diff --git a/pkgs/pow/src/aes/cpu.rs b/pkgs/pow/src/aes/cpu.rs index 16397d92d..ee3bc3166 100644 --- a/pkgs/pow/src/aes/cpu.rs +++ b/pkgs/pow/src/aes/cpu.rs @@ -4,19 +4,22 @@ // See the accompanying file LICENSE or https://opensource.org/license/MIT // -//! Best available AES round. -//! -//! Hardware AES on aarch64 and x86_64 with `simd` unless `portable` is -//! enabled, scalar T-table fallback otherwise. +//! AES rounds on four packed words through the active backend. -#[cfg(all(feature = "simd", target_arch = "aarch64", test, not(feature = "portable")))] -pub(crate) use super::aarch64::round; -#[cfg(all(feature = "simd", target_arch = "aarch64", not(feature = "portable")))] -pub(crate) use super::aarch64::round_nk; -#[cfg(all(feature = "simd", target_arch = "x86_64", not(feature = "portable")))] -pub(crate) use super::amd64::{round, round_nk}; -#[cfg(any( - feature = "portable", - not(all(feature = "simd", any(target_arch = "aarch64", target_arch = "x86_64"))) -))] -pub(crate) use super::scalar::{round, round_nk}; +use super::{block_from_words, words_from_block}; + +use wide::u32x4; + +/// Runs one AES round on four packed words. +#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] +#[inline(always)] +pub(crate) fn round(state: &[u32; 4], key: &[u32; 4]) -> [u32; 4] { + let mixed = super::round(block_from_words(u32x4::new(*state)), block_from_words(u32x4::new(*key))); + words_from_block(mixed).to_array() +} + +/// Runs one AES round on four packed words with an all-zero round key. +#[inline(always)] +pub(crate) fn round_nk(state: &[u32; 4]) -> [u32; 4] { + words_from_block(super::round_nk(block_from_words(u32x4::new(*state)))).to_array() +} diff --git a/pkgs/pow/src/aes/mod.rs b/pkgs/pow/src/aes/mod.rs index 236bdca2d..fab38bf92 100644 --- a/pkgs/pow/src/aes/mod.rs +++ b/pkgs/pow/src/aes/mod.rs @@ -6,93 +6,116 @@ //! AES round primitives for Echo, Groestl, and SHAvite. -#[cfg(all(feature = "simd", target_arch = "aarch64", not(feature = "portable")))] +#[cfg(all(target_arch = "aarch64", target_endian = "little", not(feature = "portable")))] #[expect(unsafe_code, reason = "intrinsics")] pub(crate) mod aarch64; -#[cfg(all(feature = "simd", target_arch = "x86_64", not(feature = "portable")))] +#[cfg(all(target_arch = "x86_64", not(feature = "portable")))] #[expect(unsafe_code, reason = "intrinsics")] mod amd64; pub(crate) mod consts; -#[cfg(any(test, feature = "simd"))] +#[cfg(feature = "simd")] pub(crate) mod cpu; -mod scalar; +pub(crate) mod scalar; #[cfg(feature = "simd")] pub(crate) mod simd; -#[cfg(test)] -pub(crate) use scalar::sub_bytes; -pub(crate) use scalar::{round, round_nk}; +#[cfg(all(target_arch = "aarch64", target_endian = "little", not(feature = "portable")))] +pub(crate) use self::aarch64::round; +#[cfg(all(target_arch = "x86_64", not(feature = "portable")))] +pub(crate) use self::amd64::round; +#[cfg(any( + feature = "portable", + not(any(all(target_arch = "aarch64", target_endian = "little"), target_arch = "x86_64")) +))] +pub(crate) use self::scalar::round_block as round; + +use wide::bytemuck::cast; +use wide::{u32x4, u8x16}; + +/// One 128-bit AES state, byte `4 * column + row`. +pub(crate) type Block = u8x16; + +/// Runs one AES round with an all-zero round key. +#[inline(always)] +pub(crate) fn round_nk(state: Block) -> Block { + round(state, Block::splat(0)) +} + +/// Lays four words out as a block, each word little-endian. +#[inline(always)] +pub(crate) fn block_from_words(words: u32x4) -> Block { + if cfg!(target_endian = "big") { + return cast(u32x4::new(words.to_array().map(u32::swap_bytes))); + } + cast(words) +} + +/// Reads a block as four little-endian words. +#[inline(always)] +pub(crate) fn words_from_block(block: Block) -> u32x4 { + let words: u32x4 = cast(block); + if cfg!(target_endian = "big") { + return u32x4::new(words.to_array().map(u32::swap_bytes)); + } + words +} #[cfg(test)] mod tests { use super::consts::{SBOX, T}; - use super::{cpu, round, round_nk, sub_bytes}; + use super::scalar::{round_block, sub_bytes}; + use super::*; + + const STATE: [u32; 4] = [0x01020304, 0x05060708, 0x090a0b0c, 0x0d0e0f10]; + const KEY: [u32; 4] = [0x11121314, 0x15161718, 0x191a1b1c, 0x1d1e1f20]; - /// AES round on all-zero state+key. + fn block(words: [u32; 4]) -> Block { + block_from_words(u32x4::new(words)) + } + + /// AES round on an all-zero state and key. #[test] fn spot_check_rnd_zeroed() { - let result = round(&[0; 4], &[0; 4]); - assert_eq!(result, [0x63636363; 4]); + assert_eq!(scalar::round(&[0; 4], &[0; 4]), [0x63636363; 4]); } /// AES round with a non-trivial key. #[test] fn spot_check_rnd_key() { - let result = round(&[0; 4], &[1, 2, 3, 4]); assert_eq!( - result, - [0x63636363 ^ 1, 0x63636363 ^ 2, 0x63636363 ^ 3, 0x63636363 ^ 4,] + scalar::round(&[0; 4], &[1, 2, 3, 4]), + [0x63636363 ^ 1, 0x63636363 ^ 2, 0x63636363 ^ 3, 0x63636363 ^ 4] ); } - /// nokey variant matches round with zero key. - #[test] - fn spot_check_rnd_nokey() { - let state = [0x01020304, 0x05060708, 0x090a0b0c, 0x0d0e0f10]; - assert_eq!(round_nk(&state), round(&state, &[0; 4])); - } - - /// Active backend agrees with scalar T-table on non-trivial input. + /// The scalar T-table round agrees with its definition. #[test] fn spot_check_impl() { - let state = [0x01020304, 0x05060708, 0x090a0b0c, 0x0d0e0f10]; - let key = [0x11121314, 0x15161718, 0x191a1b1c, 0x1d1e1f20]; - let result = round(&state, &key); - let expected = [ - key[0] - ^ T[0][(state[0] & 0xFF) as usize] - ^ T[1][((state[1] >> 8) & 0xFF) as usize] - ^ T[2][((state[2] >> 16) & 0xFF) as usize] - ^ T[3][((state[3] >> 24) & 0xFF) as usize], - key[1] - ^ T[0][(state[1] & 0xFF) as usize] - ^ T[1][((state[2] >> 8) & 0xFF) as usize] - ^ T[2][((state[3] >> 16) & 0xFF) as usize] - ^ T[3][((state[0] >> 24) & 0xFF) as usize], - key[2] - ^ T[0][(state[2] & 0xFF) as usize] - ^ T[1][((state[3] >> 8) & 0xFF) as usize] - ^ T[2][((state[0] >> 16) & 0xFF) as usize] - ^ T[3][((state[1] >> 24) & 0xFF) as usize], - key[3] - ^ T[0][(state[3] & 0xFF) as usize] - ^ T[1][((state[0] >> 8) & 0xFF) as usize] - ^ T[2][((state[1] >> 16) & 0xFF) as usize] - ^ T[3][((state[2] >> 24) & 0xFF) as usize], - ]; - assert_eq!(result, expected); + let (state, key) = (STATE, KEY); + let column = |c: usize| { + key[c] + ^ T[0][(state[c] & 0xFF) as usize] + ^ T[1][((state[(c + 1) % 4] >> 8) & 0xFF) as usize] + ^ T[2][((state[(c + 2) % 4] >> 16) & 0xFF) as usize] + ^ T[3][((state[(c + 3) % 4] >> 24) & 0xFF) as usize] + }; + assert_eq!( + scalar::round(&state, &key), + [column(0), column(1), column(2), column(3)] + ); } - /// CPU backend agrees with scalar. + /// The active backend agrees with the scalar round. #[test] - fn cpu_matches_scalar() { - let state = [0x01020304, 0x05060708, 0x090a0b0c, 0x0d0e0f10]; - let key = [0x11121314, 0x15161718, 0x191a1b1c, 0x1d1e1f20]; - assert_eq!(cpu::round(&state, &key), round(&state, &key)); - assert_eq!(cpu::round_nk(&state), round_nk(&state)); + fn round_matches_scalar() { + assert_eq!(round(block(STATE), block(KEY)), round_block(block(STATE), block(KEY))); + assert_eq!( + words_from_block(round_nk(block(STATE))).to_array(), + scalar::round_nk(&STATE) + ); } - /// sub_bytes matches scalar S-box lookup. + /// The scalar SubBytes agrees with the S-box. #[test] fn sub_bytes_matches_sbox() { let input: [u8; 16] = [ @@ -103,4 +126,12 @@ mod tests { assert_eq!(result[i], SBOX[input[i] as usize], "mismatch at byte {i}"); } } + + /// Words land in the block little-endian, word 0 first. + #[test] + fn words_round_trip() { + let packed = block(STATE); + assert_eq!(packed.to_array()[..4], [0x04, 0x03, 0x02, 0x01]); + assert_eq!(words_from_block(packed).to_array(), STATE); + } } diff --git a/pkgs/pow/src/aes/scalar.rs b/pkgs/pow/src/aes/scalar.rs index 276f2d4fa..3abc26059 100644 --- a/pkgs/pow/src/aes/scalar.rs +++ b/pkgs/pow/src/aes/scalar.rs @@ -7,6 +7,9 @@ //! Scalar AES round via T-table lookups. use super::consts::T; +use super::{block_from_words, words_from_block, Block}; + +use wide::u32x4; /// One AES encryption round (SubBytes + ShiftRows + MixColumns + AddRoundKey) /// using T-table lookups. @@ -40,6 +43,21 @@ pub(crate) const fn round_nk(state: &[u32; 4]) -> [u32; 4] { round(state, &[0; 4]) } +/// Runs [`round`] on a block. +#[cfg_attr( + all( + not(test), + not(feature = "portable"), + any(all(target_arch = "aarch64", target_endian = "little"), target_arch = "x86_64") + ), + expect(dead_code, reason = "fallback backend") +)] +#[inline] +pub(crate) fn round_block(state: Block, key: Block) -> Block { + let words = round(&words_from_block(state).to_array(), &words_from_block(key).to_array()); + block_from_words(u32x4::new(words)) +} + /// Applies the AES S-box to all 16 bytes via table lookup. #[cfg(test)] pub(crate) const fn sub_bytes(state: &[u8; 16]) -> [u8; 16] { diff --git a/pkgs/pow/src/echo/scalar.rs b/pkgs/pow/src/echo/scalar.rs index 388b77582..b6f5aa091 100644 --- a/pkgs/pow/src/echo/scalar.rs +++ b/pkgs/pow/src/echo/scalar.rs @@ -7,7 +7,7 @@ //! Scalar Echo-512 implementation. use super::consts::BLOCK; -use crate::aes::{round, round_nk}; +use crate::aes::scalar::{round, round_nk}; use crate::util::memops::{extract, load_u32_le, store_u32_le}; /// Increments a 128-bit counter by `val`. diff --git a/pkgs/pow/src/lib.rs b/pkgs/pow/src/lib.rs index cc18c23fb..99b48b514 100644 --- a/pkgs/pow/src/lib.rs +++ b/pkgs/pow/src/lib.rs @@ -50,6 +50,10 @@ compile_error!("dash-pow requires NEON and AES, build with `-C target-feature=+n ))] compile_error!("dash-pow requires 128-bit SIMD, build with `-C target-feature=+simd128` or enable `portable`"); +#[cfg_attr( + not(any(test, feature = "simd")), + expect(dead_code, reason = "only the SIMD backends use the block interface") +)] mod aes; mod blake; mod bmw; diff --git a/pkgs/pow/src/shavite/scalar.rs b/pkgs/pow/src/shavite/scalar.rs index 28dc2447a..40a5345eb 100644 --- a/pkgs/pow/src/shavite/scalar.rs +++ b/pkgs/pow/src/shavite/scalar.rs @@ -7,7 +7,7 @@ //! Scalar SHAvite-3-512 implementation. use super::consts::{BLOCK, IV}; -use crate::aes::round_nk; +use crate::aes::scalar::round_nk; use crate::util::memops::{extract, load_u32_le, store_u32_le}; /// Expands 128-byte message block into 448 round keys. From a61d839982d45934d0c230ccb1c563a44f50c96d Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Sun, 11 Oct 2026 01:34:53 +0530 Subject: [PATCH 04/27] pow%refac(shavite): run AES rounds through the block interface --- pkgs/pow/src/aes/aarch64.rs | 22 +--------------- pkgs/pow/src/aes/cpu.rs | 1 - pkgs/pow/src/aes/mod.rs | 2 +- pkgs/pow/src/shavite/simd.rs | 50 +++++++++++------------------------- 4 files changed, 17 insertions(+), 58 deletions(-) diff --git a/pkgs/pow/src/aes/aarch64.rs b/pkgs/pow/src/aes/aarch64.rs index 0cc2783db..a980ac8aa 100644 --- a/pkgs/pow/src/aes/aarch64.rs +++ b/pkgs/pow/src/aes/aarch64.rs @@ -7,9 +7,7 @@ //! AES rounds via the Armv8 Cryptographic Extension. #[cfg(feature = "simd")] -pub(crate) use self::packed::{ - block_from_vec, load_state, round_block, round_nk_block, store_state, sub_shift, vec_from_block, xor_block, -}; +pub(crate) use self::packed::{block_from_vec, round_block, round_nk_block, sub_shift, vec_from_block}; use super::Block; use core::arch::aarch64::{uint8x16_t, vaeseq_u8, vaesmcq_u8, vdupq_n_u8}; @@ -44,21 +42,8 @@ mod packed { use core::arch::aarch64::{uint8x16_t, vaeseq_u8, vaesmcq_u8, veorq_u8, vqtbl1q_u8}; use core::simd::Simd; - type AesState = [u32; 4]; pub(crate) type AesBlock = uint8x16_t; - #[inline(always)] - pub(crate) fn load_state(state: &AesState) -> AesBlock { - // On little-endian targets the packed-word layout already matches the byte - // layout expected by the AES instructions, so the state can stay packed. - unsafe { core::mem::transmute(*state) } - } - - #[inline(always)] - pub(crate) fn store_state(state: AesBlock) -> AesState { - unsafe { core::mem::transmute(state) } - } - #[inline(always)] pub(crate) fn block_from_vec(state: Simd) -> AesBlock { unsafe { core::mem::transmute(state) } @@ -93,11 +78,6 @@ mod packed { unsafe { round_block_impl(state, key) } } - #[inline(always)] - pub(crate) fn xor_block(state: AesBlock, key: AesBlock) -> AesBlock { - unsafe { veorq_u8(state, key) } - } - #[inline] #[target_feature(enable = "neon,aes")] unsafe fn round_nk_block_impl(state: AesBlock) -> AesBlock { diff --git a/pkgs/pow/src/aes/cpu.rs b/pkgs/pow/src/aes/cpu.rs index ee3bc3166..75c1316e6 100644 --- a/pkgs/pow/src/aes/cpu.rs +++ b/pkgs/pow/src/aes/cpu.rs @@ -11,7 +11,6 @@ use super::{block_from_words, words_from_block}; use wide::u32x4; /// Runs one AES round on four packed words. -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] pub(crate) fn round(state: &[u32; 4], key: &[u32; 4]) -> [u32; 4] { let mixed = super::round(block_from_words(u32x4::new(*state)), block_from_words(u32x4::new(*key))); diff --git a/pkgs/pow/src/aes/mod.rs b/pkgs/pow/src/aes/mod.rs index fab38bf92..eec9d3b0b 100644 --- a/pkgs/pow/src/aes/mod.rs +++ b/pkgs/pow/src/aes/mod.rs @@ -13,7 +13,7 @@ pub(crate) mod aarch64; #[expect(unsafe_code, reason = "intrinsics")] mod amd64; pub(crate) mod consts; -#[cfg(feature = "simd")] +#[cfg(all(feature = "simd", any(feature = "portable", not(target_arch = "aarch64"))))] pub(crate) mod cpu; pub(crate) mod scalar; #[cfg(feature = "simd")] diff --git a/pkgs/pow/src/shavite/simd.rs b/pkgs/pow/src/shavite/simd.rs index 85527529e..436bff369 100644 --- a/pkgs/pow/src/shavite/simd.rs +++ b/pkgs/pow/src/shavite/simd.rs @@ -7,9 +7,11 @@ //! SIMD SHAvite-3-512 implementation. use super::consts::{BLOCK, IV}; -use crate::aes::cpu::round_nk; +use crate::aes::{block_from_words, round_nk, words_from_block, Block}; use crate::util::memops::{load_u32_le, store_u32_le}; +use wide::u32x4; + const SCHEDULE_BUNDLES: usize = 112; type Bundle = [u32; 4]; type State = [u32; 16]; @@ -140,7 +142,7 @@ fn expand_round_keys(msg: &[u8; BLOCK], counter: &[u32; 4]) -> [Bundle; SCHEDULE /// Applies the AES-based bundle step used by the key schedule. #[inline(always)] fn expand_aes_step(schedule: &[Bundle; SCHEDULE_BUNDLES], bundle: usize) -> Bundle { - let expanded = round_nk(&rotate_bundle_words(schedule[bundle - 8])); + let expanded = words_from_block(round_nk(lane(rotate_bundle_words(schedule[bundle - 8])))).to_array(); [ expanded[0] ^ schedule[bundle - 1][0], expanded[1] ^ schedule[bundle - 1][1], @@ -170,51 +172,29 @@ fn inject_counter_words(bundle_words: &mut Bundle, counter: &[u32; 4], injection } } -/// Runs the 4-round AES bundle that updates one 4-word half. -#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] +/// Lays four state words out as one AES block. #[inline(always)] -fn apply_round_bundle(state: &mut State, left: [usize; 4], right: [usize; 4], keys: &[Bundle]) { - use crate::aes::aarch64::{load_state, round_nk_block, store_state, xor_block}; - - // The bundle always stays in one 128-bit lane. Keeping it packed across the - // four rounds avoids rebuilding the lane after every round. - let mut lane = load_state(&[state[right[0]], state[right[1]], state[right[2]], state[right[3]]]); - - let mut round = 0; - while round < 4 { - lane = round_nk_block(xor_block(lane, load_state(&keys[round]))); - round += 1; - } - - let lane = store_state(lane); - state[left[0]] ^= lane[0]; - state[left[1]] ^= lane[1]; - state[left[2]] ^= lane[2]; - state[left[3]] ^= lane[3]; +fn lane(words: Bundle) -> Block { + block_from_words(u32x4::new(words)) } /// Runs the 4-round AES bundle that updates one 4-word half. -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] fn apply_round_bundle(state: &mut State, left: [usize; 4], right: [usize; 4], keys: &[Bundle]) { - // Each bundle reads one 4-word half, runs four AES-like rounds, then xors + // Each bundle reads one 4-word half, runs four keyed AES rounds, then xors // the result into the opposite half in Feistel order. - let mut lane = [state[right[0]], state[right[1]], state[right[2]], state[right[3]]]; - + let mut block = lane([state[right[0]], state[right[1]], state[right[2]], state[right[3]]]); let mut round = 0; while round < 4 { - lane[0] ^= keys[round][0]; - lane[1] ^= keys[round][1]; - lane[2] ^= keys[round][2]; - lane[3] ^= keys[round][3]; - lane = round_nk(&lane); + block = round_nk(block ^ lane(keys[round])); round += 1; } - state[left[0]] ^= lane[0]; - state[left[1]] ^= lane[1]; - state[left[2]] ^= lane[2]; - state[left[3]] ^= lane[3]; + let mixed = words_from_block(block).to_array(); + state[left[0]] ^= mixed[0]; + state[left[1]] ^= mixed[1]; + state[left[2]] ^= mixed[2]; + state[left[3]] ^= mixed[3]; } /// Rotates the four columns after one round pair. From 219346a4535e735b7524dab9d7a17677b22f86cf Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Sun, 11 Oct 2026 01:35:58 +0530 Subject: [PATCH 05/27] pow%refac(echo): run AES rounds through the block interface --- pkgs/pow/src/aes/aarch64.rs | 45 +------ pkgs/pow/src/aes/cpu.rs | 24 ---- pkgs/pow/src/aes/mod.rs | 4 - pkgs/pow/src/aes/simd.rs | 14 --- pkgs/pow/src/echo/simd.rs | 235 ++++++++---------------------------- 5 files changed, 52 insertions(+), 270 deletions(-) delete mode 100644 pkgs/pow/src/aes/cpu.rs delete mode 100644 pkgs/pow/src/aes/simd.rs diff --git a/pkgs/pow/src/aes/aarch64.rs b/pkgs/pow/src/aes/aarch64.rs index a980ac8aa..f83463c56 100644 --- a/pkgs/pow/src/aes/aarch64.rs +++ b/pkgs/pow/src/aes/aarch64.rs @@ -7,7 +7,7 @@ //! AES rounds via the Armv8 Cryptographic Extension. #[cfg(feature = "simd")] -pub(crate) use self::packed::{block_from_vec, round_block, round_nk_block, sub_shift, vec_from_block}; +pub(crate) use self::packed::sub_shift; use super::Block; use core::arch::aarch64::{uint8x16_t, vaeseq_u8, vaesmcq_u8, vdupq_n_u8}; @@ -36,24 +36,13 @@ pub(crate) fn round(state: Block, key: Block) -> Block { from_native(mixed) ^ key } -/// Helpers on packed words and `core::simd` vectors for the SIMD backends. +/// Groestl's fused row shift on native vectors. #[cfg(feature = "simd")] mod packed { - use core::arch::aarch64::{uint8x16_t, vaeseq_u8, vaesmcq_u8, veorq_u8, vqtbl1q_u8}; - use core::simd::Simd; + use core::arch::aarch64::{uint8x16_t, vaeseq_u8, vqtbl1q_u8}; pub(crate) type AesBlock = uint8x16_t; - #[inline(always)] - pub(crate) fn block_from_vec(state: Simd) -> AesBlock { - unsafe { core::mem::transmute(state) } - } - - #[inline(always)] - pub(crate) fn vec_from_block(state: AesBlock) -> Simd { - unsafe { core::mem::transmute(state) } - } - /// Applies Groestl's row shift, then AES SubBytes, in one instruction pair. /// /// The mask first reshuffles the row so the following AES ShiftRows step @@ -63,32 +52,4 @@ mod packed { pub(crate) unsafe fn sub_shift(row: AesBlock, mask: AesBlock, zero: AesBlock) -> AesBlock { vaeseq_u8(vqtbl1q_u8(row, mask), zero) } - - #[inline] - #[target_feature(enable = "neon,aes")] - unsafe fn round_block_impl(state: AesBlock, key: AesBlock) -> AesBlock { - let zero = core::mem::zeroed::(); - veorq_u8(vaesmcq_u8(vaeseq_u8(state, zero)), key) - } - - #[inline(always)] - pub(crate) fn round_block(state: AesBlock, key: AesBlock) -> AesBlock { - // Safety: `aes::mod` skips this module under `portable`, and `lib.rs` - // requires the `aes` target feature otherwise. - unsafe { round_block_impl(state, key) } - } - - #[inline] - #[target_feature(enable = "neon,aes")] - unsafe fn round_nk_block_impl(state: AesBlock) -> AesBlock { - let zero = core::mem::zeroed::(); - vaesmcq_u8(vaeseq_u8(state, zero)) - } - - #[inline(always)] - pub(crate) fn round_nk_block(state: AesBlock) -> AesBlock { - // Safety: `aes::mod` skips this module under `portable`, and `lib.rs` - // requires the `aes` target feature otherwise. - unsafe { round_nk_block_impl(state) } - } } diff --git a/pkgs/pow/src/aes/cpu.rs b/pkgs/pow/src/aes/cpu.rs deleted file mode 100644 index 75c1316e6..000000000 --- a/pkgs/pow/src/aes/cpu.rs +++ /dev/null @@ -1,24 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! AES rounds on four packed words through the active backend. - -use super::{block_from_words, words_from_block}; - -use wide::u32x4; - -/// Runs one AES round on four packed words. -#[inline(always)] -pub(crate) fn round(state: &[u32; 4], key: &[u32; 4]) -> [u32; 4] { - let mixed = super::round(block_from_words(u32x4::new(*state)), block_from_words(u32x4::new(*key))); - words_from_block(mixed).to_array() -} - -/// Runs one AES round on four packed words with an all-zero round key. -#[inline(always)] -pub(crate) fn round_nk(state: &[u32; 4]) -> [u32; 4] { - words_from_block(super::round_nk(block_from_words(u32x4::new(*state)))).to_array() -} diff --git a/pkgs/pow/src/aes/mod.rs b/pkgs/pow/src/aes/mod.rs index eec9d3b0b..69b622c32 100644 --- a/pkgs/pow/src/aes/mod.rs +++ b/pkgs/pow/src/aes/mod.rs @@ -13,11 +13,7 @@ pub(crate) mod aarch64; #[expect(unsafe_code, reason = "intrinsics")] mod amd64; pub(crate) mod consts; -#[cfg(all(feature = "simd", any(feature = "portable", not(target_arch = "aarch64"))))] -pub(crate) mod cpu; pub(crate) mod scalar; -#[cfg(feature = "simd")] -pub(crate) mod simd; #[cfg(all(target_arch = "aarch64", target_endian = "little", not(feature = "portable")))] pub(crate) use self::aarch64::round; diff --git a/pkgs/pow/src/aes/simd.rs b/pkgs/pow/src/aes/simd.rs deleted file mode 100644 index 726bc2022..000000000 --- a/pkgs/pow/src/aes/simd.rs +++ /dev/null @@ -1,14 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! AES helpers for the SIMD runtime path. - -/// Multiplies each byte in a packed `u32` by `x` in `GF(2^8)`. -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -#[inline(always)] -pub(crate) fn xtime_packed_u32(word: u32) -> u32 { - ((word & 0x80808080) >> 7).wrapping_mul(27) ^ ((word & 0x7F7F7F7F) << 1) -} diff --git a/pkgs/pow/src/echo/simd.rs b/pkgs/pow/src/echo/simd.rs index a13c2efa7..f8f35e207 100644 --- a/pkgs/pow/src/echo/simd.rs +++ b/pkgs/pow/src/echo/simd.rs @@ -7,20 +7,13 @@ //! SIMD Echo-512 implementation. use super::consts::BLOCK; -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -use crate::aes::cpu::{round, round_nk}; -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -use crate::aes::simd::xtime_packed_u32; +use crate::aes::{block_from_words, round, round_nk, words_from_block, Block}; use crate::util::memops::{load_u32_le, store_u32_le}; -#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] -use core::simd::Simd; +use wide::{i8x16, u32x4}; type AesState = [u32; 4]; -#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] -type CellVec = Simd; -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -type Grid = [AesState; 16]; +type Grid = [Block; 16]; type Counter = [u32; 4]; type ChainingValue = [AesState; 8]; pub(super) type BlockWords = [u32; 32]; @@ -43,98 +36,59 @@ fn increment_counter(counter: &mut Counter, value: u32) { } } -/// Builds the 16 round keys used by one substitution step. -#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] +/// Lays four words out as one grid cell. #[inline(always)] -fn build_sub_word_keys(counter: &Counter) -> ([CellVec; 16], Counter) { - let mut keys = [CellVec::splat(0); 16]; - let mut next = *counter; - let mut cell = 0; - while cell < 16 { - keys[cell] = CellVec::from_array(next); - increment_counter(&mut next, 1); - cell += 1; - } - (keys, next) +fn cell(words: AesState) -> Block { + block_from_words(u32x4::new(words)) } /// Builds the 16 round keys used by one substitution step. -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] -fn build_sub_word_keys(counter: &Counter) -> ([AesState; 16], Counter) { - let mut keys = [[0u32; 4]; 16]; +fn build_sub_word_keys(counter: &Counter) -> (Grid, Counter) { + let mut keys = [Block::splat(0); 16]; let mut next = *counter; - let mut cell = 0; - while cell < 16 { - keys[cell] = next; + let mut index = 0; + while index < 16 { + keys[index] = cell(next); increment_counter(&mut next, 1); - cell += 1; + index += 1; } (keys, next) } -/// Runs the two AES-like rounds on each grid cell. -#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] +/// Runs the two AES rounds on each grid cell. #[inline(always)] -fn apply_big_sub_words(grid: &mut [CellVec; 16], round_keys: &[CellVec; 16]) { - use crate::aes::aarch64::{block_from_vec, round_block, round_nk_block, vec_from_block}; - - let mut cell = 0; - while cell < 16 { - grid[cell] = vec_from_block(round_nk_block(round_block( - block_from_vec(grid[cell]), - block_from_vec(round_keys[cell]), - ))); - cell += 1; - } -} - -/// Runs the two AES-like rounds on each grid cell. -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -#[inline(always)] -fn apply_big_sub_words(grid: &mut Grid, round_keys: &[AesState; 16]) { - let mut cell = 0; - while cell < 16 { - grid[cell] = round(&grid[cell], &round_keys[cell]); - grid[cell] = round_nk(&grid[cell]); - cell += 1; +fn apply_big_sub_words(grid: &mut Grid, round_keys: &Grid) { + let mut index = 0; + while index < 16 { + grid[index] = round_nk(round(grid[index], round_keys[index])); + index += 1; } } /// Rotates the second, third, and fourth grid rows. -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] fn apply_big_shift_rows(grid: &mut Grid) { let saved = *grid; - let mut cell = 0; - while cell < 16 { - grid[cell] = saved[BIG_SHIFT_ROWS[cell]]; - cell += 1; - } -} - -/// Rotates the second, third, and fourth grid rows. -#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] -#[inline(always)] -fn apply_big_shift_rows_vec(grid: &mut [CellVec; 16]) { - let saved = *grid; - let mut cell = 0; - while cell < 16 { - grid[cell] = saved[BIG_SHIFT_ROWS[cell]]; - cell += 1; + let mut index = 0; + while index < 16 { + grid[index] = saved[BIG_SHIFT_ROWS[index]]; + index += 1; } } -#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] +/// Multiplies each byte by `x` in `GF(2^8)`. #[inline(always)] -fn xtime_packed_vec(word: CellVec) -> CellVec { - (((word & CellVec::splat(0x80808080)) >> CellVec::splat(7)) * CellVec::splat(27)) - ^ ((word & CellVec::splat(0x7F7F7F7F)) << CellVec::splat(1)) +fn xtime(bytes: Block) -> Block { + const ZERO: i8x16 = i8x16::splat(0); + const POLY: Block = Block::splat(0x1B); + let carry = bytes.cast_signed().simd_lt(ZERO).cast_unsigned() & POLY; + (bytes + bytes) ^ carry } -#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] +/// Mixes one column of four AES states in `GF(2^8)`. #[inline(always)] -fn mix_column_vec(grid: &mut [CellVec; 16], a: usize, b: usize, c: usize, d: usize) { +fn mix_column(grid: &mut Grid, a: usize, b: usize, c: usize, d: usize) { let wa = grid[a]; let wb = grid[b]; let wc = grid[c]; @@ -142,50 +96,16 @@ fn mix_column_vec(grid: &mut [CellVec; 16], a: usize, b: usize, c: usize, d: usi let ab = wa ^ wb; let bc = wb ^ wc; let cd = wc ^ wd; - let ab2 = xtime_packed_vec(ab); - let bc2 = xtime_packed_vec(bc); - let cd2 = xtime_packed_vec(cd); + let ab2 = xtime(ab); + let bc2 = xtime(bc); + let cd2 = xtime(cd); grid[a] = ab2 ^ bc ^ wd; grid[b] = bc2 ^ wa ^ cd; grid[c] = cd2 ^ ab ^ wd; grid[d] = ab2 ^ bc2 ^ cd2 ^ ab ^ wc; } -#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] -#[inline(always)] -fn apply_big_mix_columns_vec(grid: &mut [CellVec; 16]) { - mix_column_vec(grid, 0, 1, 2, 3); - mix_column_vec(grid, 4, 5, 6, 7); - mix_column_vec(grid, 8, 9, 10, 11); - mix_column_vec(grid, 12, 13, 14, 15); -} - -/// Mixes one column of four AES states in `GF(2^8)`. -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -#[inline(always)] -fn mix_column(grid: &mut Grid, a: usize, b: usize, c: usize, d: usize) { - let mut word = 0; - while word < 4 { - let wa = grid[a][word]; - let wb = grid[b][word]; - let wc = grid[c][word]; - let wd = grid[d][word]; - let ab = wa ^ wb; - let bc = wb ^ wc; - let cd = wc ^ wd; - let ab2 = xtime_packed_u32(ab); - let bc2 = xtime_packed_u32(bc); - let cd2 = xtime_packed_u32(cd); - grid[a][word] = ab2 ^ bc ^ wd; - grid[b][word] = bc2 ^ wa ^ cd; - grid[c][word] = cd2 ^ ab ^ wd; - grid[d][word] = ab2 ^ bc2 ^ cd2 ^ ab ^ wc; - word += 1; - } -} - /// Applies the column-mixing step to all four grid columns. -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] #[inline(always)] fn apply_big_mix_columns(grid: &mut Grid) { mix_column(grid, 0, 1, 2, 3); @@ -195,72 +115,17 @@ fn apply_big_mix_columns(grid: &mut Grid) { } /// Runs the compression function on one 1024-bit block. -#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] #[inline(always)] pub(super) fn compress_words(chaining_value: &mut ChainingValue, block: &BlockWords, counter: &Counter) { - let mut grid = [CellVec::splat(0); 16]; - - let mut cell = 0; - while cell < 8 { - grid[cell] = CellVec::from_array(chaining_value[cell]); - cell += 1; - } - - cell = 0; - while cell < 8 { - let offset = cell * 4; - grid[cell + 8] = CellVec::from_array([block[offset], block[offset + 1], block[offset + 2], block[offset + 3]]); - cell += 1; - } - - let mut round_counter = *counter; - let mut round = 0; - while round < 10 { - let (round_keys, next_counter) = build_sub_word_keys(&round_counter); - apply_big_sub_words(&mut grid, &round_keys); - round_counter = next_counter; - apply_big_shift_rows_vec(&mut grid); - apply_big_mix_columns_vec(&mut grid); - round += 1; - } - - let mut cell = 0; - while cell < 8 { - let block_lane = CellVec::from_array([ - block[cell * 4], - block[cell * 4 + 1], - block[cell * 4 + 2], - block[cell * 4 + 3], - ]); - chaining_value[cell] = - (CellVec::from_array(chaining_value[cell]) ^ block_lane ^ grid[cell] ^ grid[cell + 8]).to_array(); - cell += 1; - } -} - -/// Runs the compression function on one 1024-bit block. -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -#[inline(always)] -pub(super) fn compress_words(chaining_value: &mut ChainingValue, block: &BlockWords, counter: &Counter) { - let mut grid = [[0u32; 4]; 16]; - - let mut cell = 0; - while cell < 8 { - grid[cell] = chaining_value[cell]; - cell += 1; - } - - cell = 0; - // The lower half of the 4x4 grid starts from the message block while the - // upper half starts from the chaining value. The big rounds mix both halves - // together before the final feedforward. - while cell < 8 { - let offset = cell * 4; - grid[cell + 8][0] = block[offset]; - grid[cell + 8][1] = block[offset + 1]; - grid[cell + 8][2] = block[offset + 2]; - grid[cell + 8][3] = block[offset + 3]; - cell += 1; + // The upper half of the 4x4 grid starts from the chaining value and the + // lower half from the message block. + let mut grid = [Block::splat(0); 16]; + let mut index = 0; + while index < 8 { + let offset = index * 4; + grid[index] = cell(chaining_value[index]); + grid[index + 8] = cell([block[offset], block[offset + 1], block[offset + 2], block[offset + 3]]); + index += 1; } let mut round_counter = *counter; @@ -274,15 +139,13 @@ pub(super) fn compress_words(chaining_value: &mut ChainingValue, block: &BlockWo round += 1; } - cell = 0; - while cell < 8 { - let mut word = 0; - while word < 4 { - let block_word = block[cell * 4 + word]; - chaining_value[cell][word] ^= block_word ^ grid[cell][word] ^ grid[cell + 8][word]; - word += 1; - } - cell += 1; + index = 0; + while index < 8 { + let offset = index * 4; + let message = cell([block[offset], block[offset + 1], block[offset + 2], block[offset + 3]]); + let mixed = cell(chaining_value[index]) ^ message ^ grid[index] ^ grid[index + 8]; + chaining_value[index] = words_from_block(mixed).to_array(); + index += 1; } } From f420e34891d6368bf733816d4956b9a0103c1cfc Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Sun, 11 Oct 2026 01:38:37 +0530 Subject: [PATCH 06/27] pow%refac(groestl): run AES rounds through the block interface --- pkgs/pow/src/aes/aarch64.rs | 24 ++---- pkgs/pow/src/aes/amd64.rs | 10 ++- pkgs/pow/src/aes/mod.rs | 25 +++---- pkgs/pow/src/aes/scalar.rs | 38 +++++++--- pkgs/pow/src/groestl/consts.rs | 9 +-- pkgs/pow/src/groestl/simd.rs | 130 +++------------------------------ pkgs/pow/src/lib.rs | 2 +- 7 files changed, 69 insertions(+), 169 deletions(-) diff --git a/pkgs/pow/src/aes/aarch64.rs b/pkgs/pow/src/aes/aarch64.rs index f83463c56..603534b00 100644 --- a/pkgs/pow/src/aes/aarch64.rs +++ b/pkgs/pow/src/aes/aarch64.rs @@ -6,8 +6,6 @@ //! AES rounds via the Armv8 Cryptographic Extension. -#[cfg(feature = "simd")] -pub(crate) use self::packed::sub_shift; use super::Block; use core::arch::aarch64::{uint8x16_t, vaeseq_u8, vaesmcq_u8, vdupq_n_u8}; @@ -36,20 +34,10 @@ pub(crate) fn round(state: Block, key: Block) -> Block { from_native(mixed) ^ key } -/// Groestl's fused row shift on native vectors. -#[cfg(feature = "simd")] -mod packed { - use core::arch::aarch64::{uint8x16_t, vaeseq_u8, vqtbl1q_u8}; - - pub(crate) type AesBlock = uint8x16_t; - - /// Applies Groestl's row shift, then AES SubBytes, in one instruction pair. - /// - /// The mask first reshuffles the row so the following AES ShiftRows step - /// lands on Groestl's target byte positions instead of AES's own pattern. - #[inline] - #[target_feature(enable = "neon,aes")] - pub(crate) unsafe fn sub_shift(row: AesBlock, mask: AesBlock, zero: AesBlock) -> AesBlock { - vaeseq_u8(vqtbl1q_u8(row, mask), zero) - } +/// Runs SubBytes and ShiftRows. +#[inline(always)] +pub(crate) fn sub_shift(state: Block) -> Block { + // SAFETY: `aes::mod` skips this module under `portable`, and `lib.rs` + // requires the `neon` and `aes` target features otherwise. + from_native(unsafe { vaeseq_u8(to_native(state), vdupq_n_u8(0)) }) } diff --git a/pkgs/pow/src/aes/amd64.rs b/pkgs/pow/src/aes/amd64.rs index 408611ecd..2fc1c6f75 100644 --- a/pkgs/pow/src/aes/amd64.rs +++ b/pkgs/pow/src/aes/amd64.rs @@ -8,7 +8,7 @@ use super::Block; -use core::arch::x86_64::{__m128i, _mm_aesenc_si128}; +use core::arch::x86_64::{__m128i, _mm_aesenc_si128, _mm_aesenclast_si128, _mm_setzero_si128}; #[inline(always)] fn to_native(block: Block) -> __m128i { @@ -29,3 +29,11 @@ pub(crate) fn round(state: Block, key: Block) -> Block { // requires the `aes` target feature otherwise. from_native(unsafe { _mm_aesenc_si128(to_native(state), to_native(key)) }) } + +/// Runs SubBytes and ShiftRows. +#[inline(always)] +pub(crate) fn sub_shift(state: Block) -> Block { + // SAFETY: `aes::mod` skips this module under `portable`, and `lib.rs` + // requires the `aes` target feature otherwise. + from_native(unsafe { _mm_aesenclast_si128(to_native(state), _mm_setzero_si128()) }) +} diff --git a/pkgs/pow/src/aes/mod.rs b/pkgs/pow/src/aes/mod.rs index 69b622c32..2bb97b9bf 100644 --- a/pkgs/pow/src/aes/mod.rs +++ b/pkgs/pow/src/aes/mod.rs @@ -8,7 +8,7 @@ #[cfg(all(target_arch = "aarch64", target_endian = "little", not(feature = "portable")))] #[expect(unsafe_code, reason = "intrinsics")] -pub(crate) mod aarch64; +mod aarch64; #[cfg(all(target_arch = "x86_64", not(feature = "portable")))] #[expect(unsafe_code, reason = "intrinsics")] mod amd64; @@ -16,14 +16,14 @@ pub(crate) mod consts; pub(crate) mod scalar; #[cfg(all(target_arch = "aarch64", target_endian = "little", not(feature = "portable")))] -pub(crate) use self::aarch64::round; +pub(crate) use self::aarch64::{round, sub_shift}; #[cfg(all(target_arch = "x86_64", not(feature = "portable")))] -pub(crate) use self::amd64::round; +pub(crate) use self::amd64::{round, sub_shift}; #[cfg(any( feature = "portable", not(any(all(target_arch = "aarch64", target_endian = "little"), target_arch = "x86_64")) ))] -pub(crate) use self::scalar::round_block as round; +pub(crate) use self::scalar::{round_block as round, sub_shift_block as sub_shift}; use wide::bytemuck::cast; use wide::{u32x4, u8x16}; @@ -59,7 +59,7 @@ pub(crate) fn words_from_block(block: Block) -> u32x4 { #[cfg(test)] mod tests { use super::consts::{SBOX, T}; - use super::scalar::{round_block, sub_bytes}; + use super::scalar::{round_block, sub_shift_block}; use super::*; const STATE: [u32; 4] = [0x01020304, 0x05060708, 0x090a0b0c, 0x0d0e0f10]; @@ -111,16 +111,15 @@ mod tests { ); } - /// The scalar SubBytes agrees with the S-box. + /// The active backend agrees with the scalar SubBytes and ShiftRows. #[test] - fn sub_bytes_matches_sbox() { - let input: [u8; 16] = [ + fn sub_shift_matches_scalar() { + let input = Block::new([ 0x00, 0x01, 0x53, 0xFF, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xA0, 0xB0, 0xC0, - ]; - let result = sub_bytes(&input); - for i in 0..16 { - assert_eq!(result[i], SBOX[input[i] as usize], "mismatch at byte {i}"); - } + ]); + assert_eq!(sub_shift(input), sub_shift_block(input)); + // Row 0 does not shift, so byte 0 is the S-box of byte 0. + assert_eq!(sub_shift(input).to_array()[0], SBOX[0]); } /// Words land in the block little-endian, word 0 first. diff --git a/pkgs/pow/src/aes/scalar.rs b/pkgs/pow/src/aes/scalar.rs index 3abc26059..fd10baf89 100644 --- a/pkgs/pow/src/aes/scalar.rs +++ b/pkgs/pow/src/aes/scalar.rs @@ -6,11 +6,23 @@ //! Scalar AES round via T-table lookups. -use super::consts::T; +use super::consts::{SBOX, T}; use super::{block_from_words, words_from_block, Block}; use wide::u32x4; +/// Source byte of each ShiftRows output byte in the column-major state. +const SHIFT_ROWS: [usize; 16] = { + let mut source = [0; 16]; + let mut dst = 0; + while dst < 16 { + let (row, col) = (dst % 4, dst / 4); + source[dst] = 4 * ((col + row) % 4) + row; + dst += 1; + } + source +}; + /// One AES encryption round (SubBytes + ShiftRows + MixColumns + AddRoundKey) /// using T-table lookups. pub(crate) const fn round(state: &[u32; 4], key: &[u32; 4]) -> [u32; 4] { @@ -58,15 +70,17 @@ pub(crate) fn round_block(state: Block, key: Block) -> Block { block_from_words(u32x4::new(words)) } -/// Applies the AES S-box to all 16 bytes via table lookup. -#[cfg(test)] -pub(crate) const fn sub_bytes(state: &[u8; 16]) -> [u8; 16] { - use super::consts::SBOX; - let mut out = [0u8; 16]; - let mut i = 0; - while i < 16 { - out[i] = SBOX[state[i] as usize]; - i += 1; - } - out +/// Runs SubBytes and ShiftRows on a block. +#[cfg_attr( + all( + not(test), + not(feature = "portable"), + any(all(target_arch = "aarch64", target_endian = "little"), target_arch = "x86_64") + ), + expect(dead_code, reason = "fallback backend") +)] +#[inline] +pub(crate) fn sub_shift_block(state: Block) -> Block { + let bytes = state.to_array(); + Block::new(core::array::from_fn(|dst| SBOX[bytes[SHIFT_ROWS[dst]] as usize])) } diff --git a/pkgs/pow/src/groestl/consts.rs b/pkgs/pow/src/groestl/consts.rs index 8c63daf06..bca887185 100644 --- a/pkgs/pow/src/groestl/consts.rs +++ b/pkgs/pow/src/groestl/consts.rs @@ -100,8 +100,8 @@ pub(crate) const RC_Q: [[u8; 16]; ROUNDS] = { rc }; -/// Arm AES ShiftRows source index for a 4x4 column-major row. -#[cfg(all(feature = "simd", target_arch = "aarch64", not(feature = "portable")))] +/// AES ShiftRows source index for a 4x4 column-major row. +#[cfg(any(test, feature = "simd"))] const fn aes_shift_rows_source(dst: usize) -> usize { let row = dst % 4; let col = dst / 4; @@ -109,7 +109,7 @@ const fn aes_shift_rows_source(dst: usize) -> usize { } /// Fused SubBytes + ShiftBytes masks for the `P` permutation. -#[cfg(all(feature = "simd", target_arch = "aarch64", not(feature = "portable")))] +#[cfg(any(test, feature = "simd"))] pub(crate) const SUBSH_P: [[u8; 16]; 8] = { const SHIFTS: [usize; 8] = [0, 1, 2, 3, 4, 5, 6, 11]; let mut masks = [[0u8; 16]; 8]; @@ -126,7 +126,7 @@ pub(crate) const SUBSH_P: [[u8; 16]; 8] = { }; /// Fused SubBytes + ShiftBytes masks for the `Q` permutation. -#[cfg(all(feature = "simd", target_arch = "aarch64", not(feature = "portable")))] +#[cfg(feature = "simd")] pub(crate) const SUBSH_Q: [[u8; 16]; 8] = { const SHIFTS: [usize; 8] = [1, 3, 5, 11, 0, 2, 4, 6]; let mut masks = [[0u8; 16]; 8]; @@ -191,7 +191,6 @@ mod tests { } } - #[cfg(all(feature = "simd", target_arch = "aarch64", not(feature = "portable")))] #[test] fn spot_check_subsh_p() { assert_eq!( diff --git a/pkgs/pow/src/groestl/simd.rs b/pkgs/pow/src/groestl/simd.rs index 06a0fb45f..dd071e12a 100644 --- a/pkgs/pow/src/groestl/simd.rs +++ b/pkgs/pow/src/groestl/simd.rs @@ -6,15 +6,10 @@ //! SIMD Groestl-512 implementation. -use super::consts::{BLOCK, IV_ROWS, RC_P, RC_Q, ROUNDS}; -#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] -use super::consts::{SUBSH_P, SUBSH_Q}; -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -use crate::aes::consts::SBOX; +use super::consts::{BLOCK, IV_ROWS, RC_P, RC_Q, ROUNDS, SUBSH_P, SUBSH_Q}; +use crate::aes::{sub_shift as aes_sub_shift, Block}; use core::simd::num::{SimdInt, SimdUint}; -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -use core::simd::simd_swizzle; use core::simd::Simd; type Row = Simd; @@ -117,68 +112,6 @@ fn mix_bytes(rows: &mut [Row; 8]) { rows[7] = b7; } -/// Applies the AES S-box to every byte in every row. -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -#[inline(always)] -fn sub_bytes(rows: &mut [Row; 8]) { - let mut row = 0; - while row < 8 { - let mut bytes = rows[row].to_array(); - let mut col = 0; - while col < 16 { - bytes[col] = SBOX[bytes[col] as usize]; - col += 1; - } - rows[row] = Row::from_array(bytes); - row += 1; - } -} - -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -const fn rotate_left_indices(shift: usize) -> [usize; 16] { - let mut indices = [0usize; 16]; - let mut i = 0; - while i < 16 { - indices[i] = (i + shift) % 16; - i += 1; - } - indices -} - -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -macro_rules! shift_row { - ($row:expr, $shift:expr) => {{ - const PATTERN: [usize; 16] = rotate_left_indices($shift); - simd_swizzle!($row, PATTERN) - }}; -} - -/// Applies the `P` row shifts. -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -#[inline(always)] -fn shift_bytes_p(rows: &mut [Row; 8]) { - rows[1] = shift_row!(rows[1], 1); - rows[2] = shift_row!(rows[2], 2); - rows[3] = shift_row!(rows[3], 3); - rows[4] = shift_row!(rows[4], 4); - rows[5] = shift_row!(rows[5], 5); - rows[6] = shift_row!(rows[6], 6); - rows[7] = shift_row!(rows[7], 11); -} - -/// Applies the `Q` row shifts. -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -#[inline(always)] -fn shift_bytes_q(rows: &mut [Row; 8]) { - rows[0] = shift_row!(rows[0], 1); - rows[1] = shift_row!(rows[1], 3); - rows[2] = shift_row!(rows[2], 5); - rows[3] = shift_row!(rows[3], 11); - rows[5] = shift_row!(rows[5], 2); - rows[6] = shift_row!(rows[6], 4); - rows[7] = shift_row!(rows[7], 6); -} - #[inline(always)] fn add_rc_p(rows: &mut [Row; 8], round: usize) { rows[0] ^= Row::from_array(RC_P[round]); @@ -197,29 +130,21 @@ fn add_rc_q(rows: &mut [Row; 8], round: usize) { rows[7] ^= Row::from_array(RC_Q[round]); } -/// Applies substitution and row rotation in one helper on supported targets. -#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] -#[expect(unsafe_code, reason = "direct vector intrinsics produce the right codegen")] +/// Applies SubBytes and the row shifts with one AES instruction per row. +/// +/// Each mask moves bytes so that AES ShiftRows, which runs after SubBytes, +/// lands them where the Groestl row shift puts them. #[inline(always)] fn sub_shift(rows: &mut [Row; 8], masks: &[[u8; 16]; 8]) { - use crate::aes::aarch64::sub_shift as hw_sub_shift; - - use core::arch::aarch64::uint8x16_t; - - unsafe { - let zero = core::mem::transmute::<[u8; 16], uint8x16_t>([0u8; 16]); - let mut row = 0; - while row < 8 { - let src = core::mem::transmute::(rows[row]); - let mask = core::mem::transmute::<[u8; 16], uint8x16_t>(masks[row]); - rows[row] = core::mem::transmute::(hw_sub_shift(src, mask, zero)); - row += 1; - } + let mut row = 0; + while row < 8 { + let shifted = Block::new(rows[row].to_array()).shuffle(Block::new(masks[row])); + rows[row] = Row::from_array(aes_sub_shift(shifted).to_array()); + row += 1; } } /// Applies the `P` permutation to the current state. -#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] #[inline(always)] fn perm_p(state: &mut [Row; 8]) { let mut round = 0; @@ -231,22 +156,7 @@ fn perm_p(state: &mut [Row; 8]) { } } -/// Applies the `P` permutation to the current state. -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -#[inline(always)] -fn perm_p(state: &mut [Row; 8]) { - let mut round = 0; - while round < ROUNDS { - add_rc_p(state, round); - sub_bytes(state); - shift_bytes_p(state); - mix_bytes(state); - round += 1; - } -} - /// Applies the `P` and `Q` permutations in lockstep for one compression. -#[cfg(all(target_arch = "aarch64", not(feature = "portable")))] #[inline(always)] fn perm_pq(p_state: &mut [Row; 8], q_state: &mut [Row; 8]) { let mut round = 0; @@ -261,24 +171,6 @@ fn perm_pq(p_state: &mut [Row; 8], q_state: &mut [Row; 8]) { } } -/// Applies the `P` and `Q` permutations in lockstep for one compression. -#[cfg(any(feature = "portable", not(target_arch = "aarch64")))] -#[inline(always)] -fn perm_pq(p_state: &mut [Row; 8], q_state: &mut [Row; 8]) { - let mut round = 0; - while round < ROUNDS { - add_rc_p(p_state, round); - add_rc_q(q_state, round); - sub_bytes(p_state); - sub_bytes(q_state); - shift_bytes_p(p_state); - shift_bytes_q(q_state); - mix_bytes(p_state); - mix_bytes(q_state); - round += 1; - } -} - /// Loads the column-major block into eight row vectors. #[inline(always)] fn load_block(block: &[u8; BLOCK]) -> [Row; 8] { diff --git a/pkgs/pow/src/lib.rs b/pkgs/pow/src/lib.rs index 99b48b514..daf084b10 100644 --- a/pkgs/pow/src/lib.rs +++ b/pkgs/pow/src/lib.rs @@ -52,7 +52,7 @@ compile_error!("dash-pow requires 128-bit SIMD, build with `-C target-feature=+s #[cfg_attr( not(any(test, feature = "simd")), - expect(dead_code, reason = "only the SIMD backends use the block interface") + expect(dead_code, unused_imports, reason = "only the SIMD backends use the block interface") )] mod aes; mod blake; From 710bd1bea7174c81a55de981cce75d309a9cd9ec Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Sun, 11 Oct 2026 01:39:12 +0530 Subject: [PATCH 07/27] pow%lint: require `SAFETY` comments on `unsafe` blocks --- pkgs/pow/src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkgs/pow/src/lib.rs b/pkgs/pow/src/lib.rs index daf084b10..1d553b961 100644 --- a/pkgs/pow/src/lib.rs +++ b/pkgs/pow/src/lib.rs @@ -12,6 +12,11 @@ #![no_std] #![cfg_attr(feature = "simd", feature(portable_simd))] +#![deny( + unsafe_op_in_unsafe_fn, + clippy::undocumented_unsafe_blocks, + clippy::unnecessary_safety_comment +)] extern crate alloc; #[cfg(feature = "std")] From 7027c5daad55f89034b2dcf68d7ad007e858117e Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 13 Oct 2026 01:57:38 +0530 Subject: [PATCH 08/27] pow%test(bench): benchmark the selected backend of each algorithm --- pkgs/pow/bench/main.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/pkgs/pow/bench/main.rs b/pkgs/pow/bench/main.rs index 29ed1dc25..098168f40 100644 --- a/pkgs/pow/bench/main.rs +++ b/pkgs/pow/bench/main.rs @@ -22,14 +22,7 @@ macro_rules! bench_algo { }; ($algo:ident) => { mod $algo { - use super::*; - mod scalar { - bench_algo!(@hash512 hash512, pow_crate::$algo::scalar::hash512); - } - #[cfg(feature = "simd")] - mod simd { - bench_algo!(@hash512 hash512, pow_crate::$algo::simd::hash512); - } + bench_algo!(@hash512 hash512, pow_crate::$algo::hash512); } }; } From df92c6fd75093ea3d216aba655b6bd333e468745 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 13 Oct 2026 03:15:08 +0530 Subject: [PATCH 09/27] pow%refac(blake)!: drop scalar backend, inline KAT test --- pkgs/pow/Cargo.toml | 2 - pkgs/pow/src/{blake/simd.rs => blake.rs} | 27 +-- pkgs/pow/src/blake/mod.rs | 25 --- pkgs/pow/src/blake/scalar.rs | 156 ------------------ .../src/{blake/consts.rs => consts/blake.rs} | 0 pkgs/pow/src/consts/mod.rs | 9 + pkgs/pow/src/lib.rs | 3 + pkgs/pow/src/tests.rs | 33 ++++ pkgs/pow/tests/blake.rs | 57 ------- 9 files changed, 59 insertions(+), 253 deletions(-) rename pkgs/pow/src/{blake/simd.rs => blake.rs} (89%) delete mode 100644 pkgs/pow/src/blake/mod.rs delete mode 100644 pkgs/pow/src/blake/scalar.rs rename pkgs/pow/src/{blake/consts.rs => consts/blake.rs} (100%) create mode 100644 pkgs/pow/src/consts/mod.rs create mode 100644 pkgs/pow/src/tests.rs delete mode 100644 pkgs/pow/tests/blake.rs diff --git a/pkgs/pow/Cargo.toml b/pkgs/pow/Cargo.toml index a2e172e7c..d5d68519c 100644 --- a/pkgs/pow/Cargo.toml +++ b/pkgs/pow/Cargo.toml @@ -28,8 +28,6 @@ divan = { workspace = true } hex-conservative = { workspace = true } rstest = { workspace = true } -[[test]] -name = "blake" [[test]] name = "bmw" [[test]] diff --git a/pkgs/pow/src/blake/simd.rs b/pkgs/pow/src/blake.rs similarity index 89% rename from pkgs/pow/src/blake/simd.rs rename to pkgs/pow/src/blake.rs index 5e7551dd0..f7f655539 100644 --- a/pkgs/pow/src/blake/simd.rs +++ b/pkgs/pow/src/blake.rs @@ -4,19 +4,9 @@ // See the accompanying file LICENSE or https://opensource.org/license/MIT // -//! Blake-512 software implementation. -//! -//! Blake-512 processes 128-byte blocks with a 16-word message schedule and a -//! 16-word working state. Each round applies the same `G` mixing function first -//! to the columns, then to the diagonals. -//! -//! The implementation keeps the scalar round structure explicit. The target is -//! the one-message case, not a packed multi-message design. -//! -//! Paper: -//! - BLAKE submission, Section 3 - -use super::consts::{BLOCK, CB, IV, SIGMA}; +//! Blake-512 hash function. + +use crate::consts::blake::{BLOCK, CB, IV, SIGMA}; use crate::util::memops::{load_u64_le, store_u64_le}; /// Compresses one 128-byte block into the chaining state. @@ -148,3 +138,14 @@ pub fn hash512(data: &[u8]) -> [u8; 64] { } out } + +#[cfg(test)] +mod tests { + use super::hash512; + use crate::tests::run_nist_kat; + + #[test] + fn nist_kat() { + run_nist_kat("blake", hash512); + } +} diff --git a/pkgs/pow/src/blake/mod.rs b/pkgs/pow/src/blake/mod.rs deleted file mode 100644 index 51be8f8f4..000000000 --- a/pkgs/pow/src/blake/mod.rs +++ /dev/null @@ -1,25 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Blake-512 hash function. -//! -//! Blake is a Merkle-Damgard construction using a ChaCha-derived G mixing -//! function. It operates on 128-byte blocks with 16 rounds per block, producing -//! a 512-bit digest. The message length is tracked by a 128-bit counter and -//! encoded in the padding as big-endian. - -#[doc(hidden)] -pub mod consts; -#[doc(hidden)] -pub mod scalar; -#[cfg(feature = "simd")] -#[doc(hidden)] -pub mod simd; - -#[cfg(not(feature = "simd"))] -pub use scalar::hash512; -#[cfg(feature = "simd")] -pub use simd::hash512; diff --git a/pkgs/pow/src/blake/scalar.rs b/pkgs/pow/src/blake/scalar.rs deleted file mode 100644 index 072bc4d55..000000000 --- a/pkgs/pow/src/blake/scalar.rs +++ /dev/null @@ -1,156 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Scalar Blake-512 implementation. - -use super::consts::{BLOCK, CB, IV, SIGMA}; -use crate::util::memops::{extract, load_u64_be, store_u64_be}; - -/// Compresses one 128-byte block into the state. -/// -/// `t0`/`t1` is the 128-bit counter AFTER the +1024 advance for this block. -pub const fn compress(h: &mut [u64; 8], block: &[u8], t0: u64, t1: u64) { - let mut m = [0u64; 16]; - let mut mi = 0; - while mi < 16 { - m[mi] = load_u64_be(block, mi); - mi += 1; - } - - #[rustfmt::skip] - let mut v = [ - h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7], - CB[0], CB[1], CB[2], CB[3], - t0 ^ CB[4], t0 ^ CB[5], t1 ^ CB[6], t1 ^ CB[7], - ]; - - let mut r = 0; - while r < 16 { - let s = &SIGMA[r]; - // G mixing function applied to 4 state words at indices (a, b, c, d) - // using message pair m[s[i]], m[s[i+1]] and constants CB[s[i]], CB[s[i+1]]. - macro_rules! g { - ($a:expr, $b:expr, $c:expr, $d:expr, $i:expr) => {{ - v[$a] = v[$a].wrapping_add(v[$b]).wrapping_add(m[s[$i]] ^ CB[s[$i + 1]]); - v[$d] = (v[$d] ^ v[$a]).rotate_right(32); - v[$c] = v[$c].wrapping_add(v[$d]); - v[$b] = (v[$b] ^ v[$c]).rotate_right(25); - v[$a] = v[$a].wrapping_add(v[$b]).wrapping_add(m[s[$i + 1]] ^ CB[s[$i]]); - v[$d] = (v[$d] ^ v[$a]).rotate_right(16); - v[$c] = v[$c].wrapping_add(v[$d]); - v[$b] = (v[$b] ^ v[$c]).rotate_right(11); - }}; - } - - // Column step - g!(0, 4, 8, 12, 0); - g!(1, 5, 9, 13, 2); - g!(2, 6, 10, 14, 4); - g!(3, 7, 11, 15, 6); - - // Diagonal step - g!(0, 5, 10, 15, 8); - g!(1, 6, 11, 12, 10); - g!(2, 7, 8, 13, 12); - g!(3, 4, 9, 14, 14); - r += 1; - } - - let mut i = 0; - while i < 8 { - h[i] ^= v[i] ^ v[i + 8]; - i += 1; - } -} - -/// Advances the 128-bit counter by 1024 (one block worth of bits). -const fn advance_counter(t0: &mut u64, t1: &mut u64) { - *t0 = t0.wrapping_add(1024); - if *t0 < 1024 { - *t1 = t1.wrapping_add(1); - } -} - -pub const fn hash512(data: &[u8]) -> [u8; 64] { - let mut h = IV; - let mut t0: u64 = 0; - let mut t1: u64 = 0; - - // Absorb full 128-byte blocks. - let mut pos = 0; - while pos + BLOCK <= data.len() { - advance_counter(&mut t0, &mut t1); - compress(&mut h, &extract::(data, pos), t0, t1); - pos += BLOCK; - } - - // Padding. - let remaining = data.len() - pos; - let bit_len = (remaining as u64) * 8; - - // Total message length in bits (embedded in padding, computed before - // counter adjustment). - let tl = t0.wrapping_add(bit_len); - let th = if bit_len > 0 && tl < t0 { t1.wrapping_add(1) } else { t1 }; - - // Always advance +1024 before each compress, so the sentinel - // wraps to the correct final value after the advance. - if remaining == 0 { - t0 = 0xffff_ffff_ffff_fc00; - t1 = 0xffff_ffff_ffff_ffff; - } else if t0 == 0 { - t0 = 0xffff_ffff_ffff_fc00u64.wrapping_add(bit_len); - t1 = t1.wrapping_sub(1); - } else { - t0 = t0.wrapping_sub(1024 - bit_len); - } - - let mut pad = [0u8; 2 * BLOCK]; - let mut ci = 0; - while ci < remaining { - pad[ci] = data[pos + ci]; - ci += 1; - } - pad[remaining] = 0x80; - - let single_block = remaining <= 111; - if single_block { - pad[111] |= 0x01; // Blake-512 output-length marker - store_u64_be(&mut pad, 14, th); - store_u64_be(&mut pad, 15, tl); - advance_counter(&mut t0, &mut t1); - compress(&mut h, &extract::(&pad, 0), t0, t1); - } else { - // First block: data remainder + 0x80 padding. - advance_counter(&mut t0, &mut t1); - compress(&mut h, &extract::(&pad, 0), t0, t1); - // Second block: zeros + marker + length. - t0 = 0xffff_ffff_ffff_fc00; - t1 = 0xffff_ffff_ffff_ffff; - let mut fin = [0u8; BLOCK]; - fin[111] = 0x01; - store_u64_be(&mut fin, 14, th); - store_u64_be(&mut fin, 15, tl); - advance_counter(&mut t0, &mut t1); - compress(&mut h, &fin, t0, t1); - } - - let mut out = [0u8; 64]; - let mut i = 0; - while i < 8 { - store_u64_be(&mut out, i, h[i]); - i += 1; - } - out -} - -#[cfg(test)] -mod tests { - use super::*; - - /// Proves hash512 evaluates at compile time. - const _: [u8; 64] = hash512(b""); -} diff --git a/pkgs/pow/src/blake/consts.rs b/pkgs/pow/src/consts/blake.rs similarity index 100% rename from pkgs/pow/src/blake/consts.rs rename to pkgs/pow/src/consts/blake.rs diff --git a/pkgs/pow/src/consts/mod.rs b/pkgs/pow/src/consts/mod.rs new file mode 100644 index 000000000..958042c1b --- /dev/null +++ b/pkgs/pow/src/consts/mod.rs @@ -0,0 +1,9 @@ +// +// Copyright (c) 2026-present, The Dash Core developers +// SPDX-License-Identifier: MIT +// See the accompanying file LICENSE or https://opensource.org/license/MIT +// + +//! Constants of each algorithm. + +pub(crate) mod blake; diff --git a/pkgs/pow/src/lib.rs b/pkgs/pow/src/lib.rs index 1d553b961..894107652 100644 --- a/pkgs/pow/src/lib.rs +++ b/pkgs/pow/src/lib.rs @@ -62,6 +62,7 @@ compile_error!("dash-pow requires 128-bit SIMD, build with `-C target-feature=+s mod aes; mod blake; mod bmw; +mod consts; mod cubehash; mod echo; mod groestl; @@ -71,6 +72,8 @@ mod luffa; mod shavite; mod simd_hash; mod skein; +#[cfg(test)] +mod tests; mod util; #[doc(hidden)] diff --git a/pkgs/pow/src/tests.rs b/pkgs/pow/src/tests.rs new file mode 100644 index 000000000..d3a50435d --- /dev/null +++ b/pkgs/pow/src/tests.rs @@ -0,0 +1,33 @@ +// +// Copyright (c) 2026-present, The Dash Core developers +// SPDX-License-Identifier: MIT +// See the accompanying file LICENSE or https://opensource.org/license/MIT +// + +//! Shared test helpers. + +use dash_dev::{arr_from_hex, Corpus}; + +const NIST_MSG_BLOB: &[u8] = include_bytes!("../corpus/nist_msg.bin"); + +/// Returns the NIST test message of `byte_len` bytes. +/// +/// The message blob contains messages of length 0..=255 bytes, laid out at +/// triangular offsets so each message is uniquely determined by its length. +fn nist_input(byte_len: usize) -> &'static [u8] { + let off = byte_len * byte_len.saturating_sub(1) / 2; + &NIST_MSG_BLOB[off..off + byte_len] +} + +/// Checks `hash512` against every NIST known answer in the `corpus` file. +pub(crate) fn run_nist_kat(corpus: &str, hash512: fn(&[u8]) -> [u8; 64]) { + let vectors = Corpus::open(env!("CARGO_MANIFEST_DIR"), corpus).vectors::("nist"); + for (byte_len, digest) in vectors.iter().enumerate() { + let expected: [u8; 64] = arr_from_hex(digest); + assert_eq!( + hash512(nist_input(byte_len)), + expected, + "{corpus}: mismatch at byte_len={byte_len}" + ); + } +} diff --git a/pkgs/pow/tests/blake.rs b/pkgs/pow/tests/blake.rs deleted file mode 100644 index 24cbb52a1..000000000 --- a/pkgs/pow/tests/blake.rs +++ /dev/null @@ -1,57 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Blake-512 tests. - -mod common; - -#[cfg(feature = "simd")] -use dash_pow::__private::blake::{consts::IV, scalar, simd}; -#[cfg(feature = "simd")] -use rstest::rstest; - -/// Scalar compress takes `&[u8]` and loads BE words internally. SIMD compress -/// takes pre-loaded `&[u64; 16]` words. We bridge via `simd::load_message` to -/// get the same word view. -#[cfg(feature = "simd")] -#[rstest] -#[case::zeros([0u8; 128], 1024, 0)] -#[case::pattern({ - let mut b = [0u8; 128]; - let mut i = 0; - while i < 128 { b[i] = (i as u8).wrapping_mul(7); i += 1; } - b -}, 1024, 0)] -#[case::counter_wrap([0xAAu8; 128], u64::MAX - 512, 0)] -fn compress(#[case] block: [u8; 128], #[case] t0: u64, #[case] t1: u64) { - let mut h_scalar = IV; - let mut h_simd = IV; - scalar::compress(&mut h_scalar, &block, t0, t1); - let words = simd::load_message(&block); - simd::compress(&mut h_simd, &words, t0, t1); - assert_eq!(h_scalar, h_simd, "blake compress diverged"); -} - -mod kat { - use crate::common; - - use dash_pow::__private::blake::scalar; - #[cfg(feature = "simd")] - use dash_pow::__private::blake::simd; - - #[test] - fn scalar() { - let vectors = common::load("blake"); - common::run_nist_kat("blake/scalar", &vectors, scalar::hash512); - } - - #[cfg(feature = "simd")] - #[test] - fn simd() { - let vectors = common::load("blake"); - common::run_nist_kat("blake/simd", &vectors, simd::hash512); - } -} From 3d2c2e8f1f868863dbf5a2dd85db80722e565b1c Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 13 Oct 2026 03:16:01 +0530 Subject: [PATCH 10/27] pow%refac(bmw)!: drop scalar backend, inline KAT test --- pkgs/pow/Cargo.toml | 2 - pkgs/pow/src/{bmw/simd.rs => bmw.rs} | 15 +- pkgs/pow/src/bmw/mod.rs | 20 -- pkgs/pow/src/bmw/scalar.rs | 225 ------------------ pkgs/pow/src/{bmw/consts.rs => consts/bmw.rs} | 0 pkgs/pow/src/consts/mod.rs | 1 + pkgs/pow/tests/bmw.rs | 102 -------- 7 files changed, 14 insertions(+), 351 deletions(-) rename pkgs/pow/src/{bmw/simd.rs => bmw.rs} (97%) delete mode 100644 pkgs/pow/src/bmw/mod.rs delete mode 100644 pkgs/pow/src/bmw/scalar.rs rename pkgs/pow/src/{bmw/consts.rs => consts/bmw.rs} (100%) delete mode 100644 pkgs/pow/tests/bmw.rs diff --git a/pkgs/pow/Cargo.toml b/pkgs/pow/Cargo.toml index d5d68519c..f2addfd3a 100644 --- a/pkgs/pow/Cargo.toml +++ b/pkgs/pow/Cargo.toml @@ -28,8 +28,6 @@ divan = { workspace = true } hex-conservative = { workspace = true } rstest = { workspace = true } -[[test]] -name = "bmw" [[test]] name = "cubehash" [[test]] diff --git a/pkgs/pow/src/bmw/simd.rs b/pkgs/pow/src/bmw.rs similarity index 97% rename from pkgs/pow/src/bmw/simd.rs rename to pkgs/pow/src/bmw.rs index fbca44852..2b9453bd8 100644 --- a/pkgs/pow/src/bmw/simd.rs +++ b/pkgs/pow/src/bmw.rs @@ -4,9 +4,9 @@ // See the accompanying file LICENSE or https://opensource.org/license/MIT // -//! SIMD BMW-512 implementation. +//! BMW-512 (Blue Midnight Wish) hash function. -use super::consts::*; +use crate::consts::bmw::*; use crate::util::memops::{load_u64_le, store_u64_le}; const WORDS: usize = 16; @@ -234,3 +234,14 @@ pub fn hash512(data: &[u8]) -> [u8; 64] { } out } + +#[cfg(test)] +mod tests { + use super::hash512; + use crate::tests::run_nist_kat; + + #[test] + fn nist_kat() { + run_nist_kat("bmw", hash512); + } +} diff --git a/pkgs/pow/src/bmw/mod.rs b/pkgs/pow/src/bmw/mod.rs deleted file mode 100644 index e10ba0f16..000000000 --- a/pkgs/pow/src/bmw/mod.rs +++ /dev/null @@ -1,20 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! BMW-512 (Blue Midnight Wish) hash function. - -#[doc(hidden)] -pub mod consts; -#[doc(hidden)] -pub mod scalar; -#[cfg(feature = "simd")] -#[doc(hidden)] -pub mod simd; - -#[cfg(not(feature = "simd"))] -pub use scalar::hash512; -#[cfg(feature = "simd")] -pub use simd::hash512; diff --git a/pkgs/pow/src/bmw/scalar.rs b/pkgs/pow/src/bmw/scalar.rs deleted file mode 100644 index 8e426d832..000000000 --- a/pkgs/pow/src/bmw/scalar.rs +++ /dev/null @@ -1,225 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Scalar BMW-512 implementation. - -use super::consts::*; -use crate::util::memops::{extract, load_u64_le, store_u64_le}; - -/// S-function: parameterized shift/rotate mixing. -/// -/// For n < 4: `(x >> A) ^ (x << B) ^ rotl(x, C) ^ rotl(x, D)`. For n >= 4: `(x -/// >> A) ^ x`. -const fn sb(n: usize, x: u64) -> u64 { - if n < 4 { - (x >> SB_SHR[n]) ^ (x << SB_SHL[n]) ^ x.rotate_left(SB_RC[n]) ^ x.rotate_left(SB_RD[n]) - } else { - (x >> SB_SHR[n]) ^ x - } -} - -/// Expansion constant: `j * 0x0555555555555555`. -const fn kb(j: usize) -> u64 { - (j as u64).wrapping_mul(0x0555555555555555) -} - -/// Add-element: `rotl(m[j], j+1) + rotl(m[j+3], j+4) - rotl(m[j+10], j+11) + -/// kb(j+16) ^ h[j+7]`. -/// -/// All indices are taken mod 16 for circular access. -const fn add_elt(m: &[u64; 16], h: &[u64; 16], j: usize) -> u64 { - let idx0 = (j) & 15; - let idx3 = (j + 3) & 15; - let idx10 = (j + 10) & 15; - let r0 = m[idx0].rotate_left(idx0 as u32 + 1); - let r3 = m[idx3].rotate_left(idx3 as u32 + 1); - let r10 = m[idx10].rotate_left(idx10 as u32 + 1); - r0.wrapping_add(r3).wrapping_sub(r10).wrapping_add(kb(j + 16)) ^ h[(j + 7) & 15] -} - -/// Compute W[0..16] from message and state using the sign/index tables. -const fn compute_w(m: &[u64; 16], h: &[u64; 16]) -> [u64; 16] { - let mut w = [0u64; 16]; - let mut i = 0; - while i < 16 { - let idx = &W_IDX[i]; - let ops = &W_OPS[i]; - - let mut v = m[idx[0]] ^ h[idx[0]]; - let mut k = 1; - while k < 5 { - if ops[k - 1] { - v = v.wrapping_add(m[idx[k]] ^ h[idx[k]]); - } else { - v = v.wrapping_sub(m[idx[k]] ^ h[idx[k]]); - } - k += 1; - } - w[i] = v; - i += 1; - } - w -} - -/// Shifts a u64 left or right depending on the `left` flag. -const fn directed_shift(x: u64, left: bool, amount: u32) -> u64 { - if left { - x << amount - } else { - x >> amount - } -} - -/// Compresses one 128-byte block: `h` (state) -> `dh` (output). -pub const fn compress(data: &[u8], h: &[u64; 16], dh: &mut [u64; 16]) { - let mut m = [0u64; 16]; - let mut mi = 0; - while mi < 16 { - m[mi] = load_u64_le(data, mi); - mi += 1; - } - let w = compute_w(&m, h); - - // Phase 1: Q[0..16] from W values + H rotation - let mut q = [0u64; 32]; - let mut i = 0; - while i < 16 { - q[i] = sb(i % 5, w[i]).wrapping_add(h[(i + 1) & 15]); - i += 1; - } - - // Phase 2: Q[16..18] via expand1 (sb1, sb2, sb3, sb0 cycling) - i = 16; - while i < 18 { - let j = i - 16; - let mut v = 0u64; - let mut k = 0; - while k < 16 { - v = v.wrapping_add(sb((k + 1) % 4, q[j + k])); - k += 1; - } - q[i] = v.wrapping_add(add_elt(&m, h, j)); - i += 1; - } - - // Phase 3: Q[18..32] via expand2 (rotation interleave) - // q[j] + rb1(q[j+1]) + q[j+2] + rb2(q[j+3]) - // + ... + rb7(q[j+13]) + sb4(q[j+14]) - // + sb5(q[j+15]) - i = 18; - while i < 32 { - let j = i - 16; - let mut v = q[j]; - let mut k = 0; - while k < 7 { - v = v.wrapping_add(q[j + 2 * k + 1].rotate_left(RB[k])); - v = v.wrapping_add(q[j + 2 * k + 2]); - k += 1; - } - // The loop adds q[j+14] as plain, but the spec requires sb4(q[j+14]) - // and sb5(q[j+15]) for the final two terms. - v = v.wrapping_sub(q[j + 14]); - v = v.wrapping_add(sb(4, q[j + 14])); - v = v.wrapping_add(sb(5, q[j + 15])); - q[i] = v.wrapping_add(add_elt(&m, h, j)); - i += 1; - } - - // Phase 4: FOLD -- combine Q values into 16-word output - let xl = q[16] ^ q[17] ^ q[18] ^ q[19] ^ q[20] ^ q[21] ^ q[22] ^ q[23]; - let xh = xl ^ q[24] ^ q[25] ^ q[26] ^ q[27] ^ q[28] ^ q[29] ^ q[30] ^ q[31]; - - // dh[0..8]: table-driven shift/XOR pattern. - i = 0; - while i < 8 { - let (xh_left, xh_amt, q_left, q_amt) = FOLD1[i]; - let term = directed_shift(xh, xh_left, xh_amt) ^ directed_shift(q[16 + i], q_left, q_amt) ^ m[i]; - dh[i] = term.wrapping_add(xl ^ q[24 + i] ^ q[i]); - i += 1; - } - - // dh[8..16]: rotation of earlier dh + shifted xl + Q mixing. - i = 0; - while i < 8 { - let (src, rot, xl_left, xl_amt) = FOLD2[i]; - let q_idx = FOLD2_Q[i]; - dh[8 + i] = dh[src].rotate_left(rot).wrapping_add( - (xh ^ q[24 + i] ^ m[8 + i]).wrapping_add(directed_shift(xl, xl_left, xl_amt) ^ q[q_idx] ^ q[8 + i]), - ); - i += 1; - } -} - -/// Serializes a 16-word state as a 128-byte little-endian block. -const fn state_to_bytes(state: &[u64; 16]) -> [u8; BLOCK] { - let mut buf = [0u8; BLOCK]; - let mut i = 0; - while i < 16 { - store_u64_le(&mut buf, i, state[i]); - i += 1; - } - buf -} - -pub const fn hash512(data: &[u8]) -> [u8; 64] { - let mut h1 = IV; - let mut h2 = [0u64; 16]; - let mut current = &mut h1; - let mut next = &mut h2; - let bit_count = (data.len() as u64) << 3; - - // Absorb full 128-byte blocks. - let mut pos = 0; - while pos + BLOCK <= data.len() { - compress(&extract::(data, pos), current, next); - core::mem::swap(&mut current, &mut next); - pos += BLOCK; - } - - // Padding: 0x80 + zeros + 8-byte LE bit count at offset 120. - let remaining = data.len() - pos; - let mut pad = [0u8; 2 * BLOCK]; - let mut ci = 0; - while ci < remaining { - pad[ci] = data[pos + ci]; - ci += 1; - } - pad[remaining] = 0x80; - - if remaining <= 119 { - store_u64_le(&mut pad, 15, bit_count); - compress(&extract::(&pad, 0), current, next); - core::mem::swap(&mut current, &mut next); - } else { - compress(&extract::(&pad, 0), current, next); - core::mem::swap(&mut current, &mut next); - let mut fin = [0u8; BLOCK]; - store_u64_le(&mut fin, 15, bit_count); - compress(&fin, current, next); - core::mem::swap(&mut current, &mut next); - } - - // Final compression: serialize state as data, use FINAL_B as H. - let data_buf = state_to_bytes(current); - compress(&data_buf, &FINAL_B, next); - - // Output: last 8 words. - let mut out = [0u8; 64]; - let mut i = 0; - while i < 8 { - store_u64_le(&mut out, i, next[i + 8]); - i += 1; - } - out -} - -#[cfg(test)] -mod tests { - use super::*; - - /// Proves hash512 evaluates at compile time. - const _: [u8; 64] = hash512(b""); -} diff --git a/pkgs/pow/src/bmw/consts.rs b/pkgs/pow/src/consts/bmw.rs similarity index 100% rename from pkgs/pow/src/bmw/consts.rs rename to pkgs/pow/src/consts/bmw.rs diff --git a/pkgs/pow/src/consts/mod.rs b/pkgs/pow/src/consts/mod.rs index 958042c1b..6f9f8c756 100644 --- a/pkgs/pow/src/consts/mod.rs +++ b/pkgs/pow/src/consts/mod.rs @@ -7,3 +7,4 @@ //! Constants of each algorithm. pub(crate) mod blake; +pub(crate) mod bmw; diff --git a/pkgs/pow/tests/bmw.rs b/pkgs/pow/tests/bmw.rs deleted file mode 100644 index c36806ea4..000000000 --- a/pkgs/pow/tests/bmw.rs +++ /dev/null @@ -1,102 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! BMW-512 tests. - -mod common; - -#[cfg(feature = "simd")] -use dash_pow::__private::bmw::{consts::IV, scalar, simd}; -#[cfg(feature = "simd")] -use rstest::rstest; - -/// Loads a 128-byte block as sixteen little-endian u64 words. -#[cfg(feature = "simd")] -fn bytes_to_words(block: &[u8; 128]) -> [u64; 16] { - core::array::from_fn(|i| { - let off = i * 8; - u64::from_le_bytes([ - block[off], - block[off + 1], - block[off + 2], - block[off + 3], - block[off + 4], - block[off + 5], - block[off + 6], - block[off + 7], - ]) - }) -} - -/// Creates a 128-byte block filled with a running pattern. -#[cfg(feature = "simd")] -fn make_block(fill: u8) -> [u8; 128] { - let mut b = [0u8; 128]; - for (i, slot) in b.iter_mut().enumerate() { - *slot = fill.wrapping_add(i as u8); - } - b -} - -#[cfg(feature = "simd")] -#[rstest] -#[case::zeros([0u8; 128])] -#[case::pattern(make_block(0x13))] -#[case::high(make_block(0xA7))] -fn compress(#[case] block: [u8; 128]) { - let words = bytes_to_words(&block); - let mut out_scalar = [0u64; 16]; - let mut out_simd = [0u64; 16]; - scalar::compress(&block, &IV, &mut out_scalar); - simd::compress(&words, &IV, &mut out_simd); - assert_eq!(out_scalar, out_simd, "bmw compress diverged"); -} - -#[cfg(feature = "simd")] -#[test] -fn bytes_to_words_round_trip() { - let block = make_block(0x42); - let words = bytes_to_words(&block); - let mut reconstructed = [0u8; 128]; - for (i, &w) in words.iter().enumerate() { - reconstructed[i * 8..(i + 1) * 8].copy_from_slice(&w.to_le_bytes()); - } - assert_eq!(block, reconstructed); -} - -mod kat { - use crate::common; - - use dash_pow::__private::bmw::scalar; - #[cfg(feature = "simd")] - use dash_pow::__private::bmw::simd; - - #[test] - fn nist_vectors_scalar() { - let vectors = common::load("bmw"); - common::run_nist_kat("bmw/scalar", &vectors, scalar::hash512); - } - - #[cfg(feature = "simd")] - #[test] - fn nist_vectors_simd() { - let vectors = common::load("bmw"); - common::run_nist_kat("bmw/simd", &vectors, simd::hash512); - } - - #[cfg(feature = "simd")] - #[test] - fn scalar_simd_agree_on_empty() { - assert_eq!(scalar::hash512(b""), simd::hash512(b"")); - } - - #[cfg(feature = "simd")] - #[test] - fn scalar_simd_agree_on_short() { - let msg = b"dash"; - assert_eq!(scalar::hash512(msg), simd::hash512(msg)); - } -} From 6c2cc39ccf800c54874e09a4d20982a49e142440 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Wed, 7 Oct 2026 11:32:05 +0530 Subject: [PATCH 11/27] pow%refac(groestl)!: use `wide`, drop scalar backend, inline KAT test --- pkgs/pow/Cargo.toml | 2 - .../{groestl/consts.rs => consts/groestl.rs} | 52 ---- pkgs/pow/src/consts/mod.rs | 1 + pkgs/pow/src/{groestl/simd.rs => groestl.rs} | 41 ++-- pkgs/pow/src/groestl/mod.rs | 20 -- pkgs/pow/src/groestl/scalar.rs | 232 ------------------ pkgs/pow/src/lib.rs | 2 +- pkgs/pow/tests/groestl.rs | 157 ------------ 8 files changed, 29 insertions(+), 478 deletions(-) rename pkgs/pow/src/{groestl/consts.rs => consts/groestl.rs} (72%) rename pkgs/pow/src/{groestl/simd.rs => groestl.rs} (87%) delete mode 100644 pkgs/pow/src/groestl/mod.rs delete mode 100644 pkgs/pow/src/groestl/scalar.rs delete mode 100644 pkgs/pow/tests/groestl.rs diff --git a/pkgs/pow/Cargo.toml b/pkgs/pow/Cargo.toml index f2addfd3a..c2cee347c 100644 --- a/pkgs/pow/Cargo.toml +++ b/pkgs/pow/Cargo.toml @@ -33,8 +33,6 @@ name = "cubehash" [[test]] name = "echo" [[test]] -name = "groestl" -[[test]] name = "jh" [[test]] name = "keccak" diff --git a/pkgs/pow/src/groestl/consts.rs b/pkgs/pow/src/consts/groestl.rs similarity index 72% rename from pkgs/pow/src/groestl/consts.rs rename to pkgs/pow/src/consts/groestl.rs index bca887185..4c9414bfe 100644 --- a/pkgs/pow/src/groestl/consts.rs +++ b/pkgs/pow/src/consts/groestl.rs @@ -6,15 +6,9 @@ //! Groestl-512 constants. -use crate::aes::consts::SBOX; -use crate::util::math::gf2_mul; - pub(crate) const BLOCK: usize = 128; pub(crate) const ROUNDS: usize = 14; -/// Groestl MDS column 0 in byte order from row 0 to row 7. -const MDS_COL0: [u8; 8] = [0x02, 0x07, 0x05, 0x03, 0x05, 0x04, 0x03, 0x02]; - /// IV with the 512-bit output size encoded in the last column. pub(crate) const IV: [u64; 16] = { let mut h = [0u64; 16]; @@ -24,7 +18,6 @@ pub(crate) const IV: [u64; 16] = { }; /// IV unpacked into the row-wise layout used by the SIMD backend. -#[cfg(any(test, feature = "simd"))] pub(crate) const IV_ROWS: [[u8; 16]; 8] = { let mut rows = [[0u8; 16]; 8]; let mut row = 0; @@ -39,37 +32,7 @@ pub(crate) const IV_ROWS: [[u8; 16]; 8] = { rows }; -/// `T0[s]` stores MDS column 0 multiplied by `SBOX[s]`. -pub(crate) const T0: [u64; 256] = { - let mut table = [0u64; 256]; - let mut i = 0; - while i < 256 { - let s = SBOX[i]; - let mut word = 0u64; - let mut row = 0; - while row < 8 { - word |= (gf2_mul(MDS_COL0[row], s) as u64) << (row * 8); - row += 1; - } - table[i] = word; - i += 1; - } - table -}; - -/// `T4[s]` is `T0[s]` rotated by four byte positions. -pub(crate) const T4: [u64; 256] = { - let mut table = [0u64; 256]; - let mut i = 0; - while i < 256 { - table[i] = T0[i].rotate_left(32); - i += 1; - } - table -}; - /// Round constants xored into row 0 of the `P` permutation. -#[cfg(any(test, feature = "simd"))] pub(crate) const RC_P: [[u8; 16]; ROUNDS] = { let mut rc = [[0u8; 16]; ROUNDS]; let mut round = 0; @@ -85,7 +48,6 @@ pub(crate) const RC_P: [[u8; 16]; ROUNDS] = { }; /// Round constants xored into row 7 of the `Q` permutation. -#[cfg(any(test, feature = "simd"))] pub(crate) const RC_Q: [[u8; 16]; ROUNDS] = { let mut rc = [[0u8; 16]; ROUNDS]; let mut round = 0; @@ -101,7 +63,6 @@ pub(crate) const RC_Q: [[u8; 16]; ROUNDS] = { }; /// AES ShiftRows source index for a 4x4 column-major row. -#[cfg(any(test, feature = "simd"))] const fn aes_shift_rows_source(dst: usize) -> usize { let row = dst % 4; let col = dst / 4; @@ -109,7 +70,6 @@ const fn aes_shift_rows_source(dst: usize) -> usize { } /// Fused SubBytes + ShiftBytes masks for the `P` permutation. -#[cfg(any(test, feature = "simd"))] pub(crate) const SUBSH_P: [[u8; 16]; 8] = { const SHIFTS: [usize; 8] = [0, 1, 2, 3, 4, 5, 6, 11]; let mut masks = [[0u8; 16]; 8]; @@ -126,7 +86,6 @@ pub(crate) const SUBSH_P: [[u8; 16]; 8] = { }; /// Fused SubBytes + ShiftBytes masks for the `Q` permutation. -#[cfg(feature = "simd")] pub(crate) const SUBSH_Q: [[u8; 16]; 8] = { const SHIFTS: [usize; 8] = [1, 3, 5, 11, 0, 2, 4, 6]; let mut masks = [[0u8; 16]; 8]; @@ -146,17 +105,6 @@ pub(crate) const SUBSH_Q: [[u8; 16]; 8] = { mod tests { use super::*; - #[test] - fn spot_check_t0() { - assert_eq!(T0[0x00], 0xc632f4a5f497a5c6_u64.swap_bytes()); - assert_eq!(T0[0x01], 0xf86f978497eb84f8_u64.swap_bytes()); - } - - #[test] - fn spot_check_t4() { - assert_eq!(T4[0x00], T0[0x00].rotate_left(32)); - } - #[test] fn verify_iv() { assert_eq!(IV[0], 0); diff --git a/pkgs/pow/src/consts/mod.rs b/pkgs/pow/src/consts/mod.rs index 6f9f8c756..34b5e4fe0 100644 --- a/pkgs/pow/src/consts/mod.rs +++ b/pkgs/pow/src/consts/mod.rs @@ -8,3 +8,4 @@ pub(crate) mod blake; pub(crate) mod bmw; +pub(crate) mod groestl; diff --git a/pkgs/pow/src/groestl/simd.rs b/pkgs/pow/src/groestl.rs similarity index 87% rename from pkgs/pow/src/groestl/simd.rs rename to pkgs/pow/src/groestl.rs index dd071e12a..15911162f 100644 --- a/pkgs/pow/src/groestl/simd.rs +++ b/pkgs/pow/src/groestl.rs @@ -4,21 +4,24 @@ // See the accompanying file LICENSE or https://opensource.org/license/MIT // -//! SIMD Groestl-512 implementation. +//! Groestl-512 hash function. -use super::consts::{BLOCK, IV_ROWS, RC_P, RC_Q, ROUNDS, SUBSH_P, SUBSH_Q}; -use crate::aes::{sub_shift as aes_sub_shift, Block}; +use crate::aes::sub_shift as aes_sub_shift; +use crate::consts::groestl::{BLOCK, IV_ROWS, RC_P, RC_Q, ROUNDS, SUBSH_P, SUBSH_Q}; -use core::simd::num::{SimdInt, SimdUint}; -use core::simd::Simd; +use wide::{i8x16, u8x16}; -type Row = Simd; +/// One row of the byte-sliced state, a byte per column. +pub type Row = u8x16; /// Multiplies each byte by `x` in `GF(2^8)`. +/// +/// Doubling by addition and a sign compare avoid byte shifts, which some +/// backends lack. #[inline(always)] fn mul2(row: Row) -> Row { - let mask = (row.cast::() >> Simd::splat(7i8)).cast::(); - (row << Simd::splat(1)) ^ (mask & Row::splat(0x1B)) + let mask = row.cast_signed().simd_lt(i8x16::splat(0)).cast_unsigned(); + (row + row) ^ (mask & Row::splat(0x1B)) } /// Applies the byte-sliced MixBytes transform to eight rows. @@ -114,7 +117,7 @@ fn mix_bytes(rows: &mut [Row; 8]) { #[inline(always)] fn add_rc_p(rows: &mut [Row; 8], round: usize) { - rows[0] ^= Row::from_array(RC_P[round]); + rows[0] ^= Row::new(RC_P[round]); } #[inline(always)] @@ -127,7 +130,7 @@ fn add_rc_q(rows: &mut [Row; 8], round: usize) { rows[4] ^= ff; rows[5] ^= ff; rows[6] ^= ff; - rows[7] ^= Row::from_array(RC_Q[round]); + rows[7] ^= Row::new(RC_Q[round]); } /// Applies SubBytes and the row shifts with one AES instruction per row. @@ -138,8 +141,7 @@ fn add_rc_q(rows: &mut [Row; 8], round: usize) { fn sub_shift(rows: &mut [Row; 8], masks: &[[u8; 16]; 8]) { let mut row = 0; while row < 8 { - let shifted = Block::new(rows[row].to_array()).shuffle(Block::new(masks[row])); - rows[row] = Row::from_array(aes_sub_shift(shifted).to_array()); + rows[row] = aes_sub_shift(rows[row].shuffle(Row::new(masks[row]))); row += 1; } } @@ -183,7 +185,7 @@ fn load_block(block: &[u8; BLOCK]) -> [Row; 8] { bytes[col] = block[col * 8 + row]; col += 1; } - rows[row] = Row::from_array(bytes); + rows[row] = Row::new(bytes); row += 1; } rows @@ -246,7 +248,7 @@ pub fn hash512(data: &[u8]) -> [u8; 64] { let mut chaining_value = [Row::splat(0); 8]; let mut row = 0; while row < 8 { - chaining_value[row] = Row::from_array(IV_ROWS[row]); + chaining_value[row] = Row::new(IV_ROWS[row]); row += 1; } @@ -289,3 +291,14 @@ pub fn hash512(data: &[u8]) -> [u8; 64] { extract_right_half(&chaining_value, &mut out); out } + +#[cfg(test)] +mod tests { + use super::hash512; + use crate::tests::run_nist_kat; + + #[test] + fn nist_kat() { + run_nist_kat("groestl", hash512); + } +} diff --git a/pkgs/pow/src/groestl/mod.rs b/pkgs/pow/src/groestl/mod.rs deleted file mode 100644 index ab30ef7ce..000000000 --- a/pkgs/pow/src/groestl/mod.rs +++ /dev/null @@ -1,20 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Groestl-512 hash function. - -#[doc(hidden)] -pub mod consts; -#[doc(hidden)] -pub mod scalar; -#[cfg(feature = "simd")] -#[doc(hidden)] -pub mod simd; - -#[cfg(not(feature = "simd"))] -pub use scalar::hash512; -#[cfg(feature = "simd")] -pub use simd::hash512; diff --git a/pkgs/pow/src/groestl/scalar.rs b/pkgs/pow/src/groestl/scalar.rs deleted file mode 100644 index 454028e98..000000000 --- a/pkgs/pow/src/groestl/scalar.rs +++ /dev/null @@ -1,232 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Scalar Groestl-512 implementation. - -use super::consts::{BLOCK, IV, ROUNDS, T0, T4}; -use crate::util::memops::{extract, load_u64_le, store_u64_le}; - -// Byte extraction from u64 (LE convention). -#[inline] -const fn b0(x: u64) -> usize { - (x & 0xFF) as usize -} -#[inline] -const fn b1(x: u64) -> usize { - ((x >> 8) & 0xFF) as usize -} -#[inline] -const fn b2(x: u64) -> usize { - ((x >> 16) & 0xFF) as usize -} -#[inline] -const fn b3(x: u64) -> usize { - ((x >> 24) & 0xFF) as usize -} -#[inline] -const fn b4(x: u64) -> usize { - ((x >> 32) & 0xFF) as usize -} -#[inline] -const fn b5(x: u64) -> usize { - ((x >> 40) & 0xFF) as usize -} -#[inline] -const fn b6(x: u64) -> usize { - ((x >> 48) & 0xFF) as usize -} -#[inline] -const fn b7(x: u64) -> usize { - (x >> 56) as usize -} - -/// RSTT: fused SubBytes + ShiftBytes + MixBytes via T-table lookup. -/// -/// `idx` selects the 8 input word indices (ShiftBytes permutation). Uses T0 and -/// T4 with ROTL for T1-T3 and T5-T7. -#[inline] -const fn rstt(a: &[u64; 16], idx: [usize; 8]) -> u64 { - T0[b0(a[idx[0]])] - ^ T0[b1(a[idx[1]])].rotate_left(8) - ^ T0[b2(a[idx[2]])].rotate_left(16) - ^ T0[b3(a[idx[3]])].rotate_left(24) - ^ T4[b4(a[idx[4]])] - ^ T4[b5(a[idx[5]])].rotate_left(8) - ^ T4[b6(a[idx[6]])].rotate_left(16) - ^ T4[b7(a[idx[7]])].rotate_left(24) -} - -/// P round constant (LE): XOR into byte 0 of each word. -#[inline] -const fn pc64(j: u64, r: u64) -> u64 { - j.wrapping_add(r) -} - -/// Q round constant (LE). -#[inline] -const fn qc64(j: u64, r: u64) -> u64 { - (r << 56) ^ !(j << 56) -} - -/// P permutation round. -const fn round_p(a: &mut [u64; 16], r: u64) { - // AddRoundConstant. - let mut j = 0; - while j < 16 { - a[j] ^= pc64((j as u64) * 0x10, r); - j += 1; - } - - // SubBytes + ShiftBytes + MixBytes. - // P shift: (d, d+1, d+2, d+3, d+4, d+5, d+6, d+11) mod 16. - let mut t = [0u64; 16]; - let mut d = 0; - while d < 16 { - #[rustfmt::skip] - let idx = [d & 0xF, (d+1) & 0xF, (d+2) & 0xF, (d+3) & 0xF, (d+4) & 0xF, (d+5) & 0xF, (d+6) & 0xF, (d+11) & 0xF]; - t[d] = rstt(a, idx); - d += 1; - } - *a = t; -} - -/// Q permutation round. -const fn round_q(a: &mut [u64; 16], r: u64) { - // AddRoundConstant. - let mut j = 0; - while j < 16 { - a[j] ^= qc64((j as u64) * 0x10, r); - j += 1; - } - - // SubBytes + ShiftBytes + MixBytes. - // Q shift: (d+1, d+3, d+5, d+11, d, d+2, d+4, d+6) mod 16. - let mut t = [0u64; 16]; - let mut d = 0; - while d < 16 { - #[rustfmt::skip] - let idx = [(d+1) & 0xF, (d+3) & 0xF, (d+5) & 0xF, (d+11) & 0xF, d & 0xF, (d+2) & 0xF, (d+4) & 0xF, (d+6) & 0xF]; - t[d] = rstt(a, idx); - d += 1; - } - *a = t; -} - -/// 14-round P permutation. -pub const fn perm_p(a: &mut [u64; 16]) { - let mut r = 0; - while r < ROUNDS { - round_p(a, r as u64); - r += 1; - } -} - -/// 14-round Q permutation. -pub const fn perm_q(a: &mut [u64; 16]) { - let mut r = 0; - while r < ROUNDS { - round_q(a, r as u64); - r += 1; - } -} - -/// Groestl-512 compression: H = H ^ P(H ^ M) ^ Q(M). -pub const fn compress(h: &mut [u64; 16], buf: &[u8]) { - let mut g = [0u64; 16]; - let mut m = [0u64; 16]; - - let mut u = 0; - while u < 16 { - m[u] = load_u64_le(buf, u); - g[u] = m[u] ^ h[u]; - u += 1; - } - - perm_p(&mut g); - perm_q(&mut m); - - u = 0; - while u < 16 { - h[u] ^= g[u] ^ m[u]; - u += 1; - } -} - -/// Output transformation: H = H ^ P(H), then take H[8..15]. -pub const fn output_transform(h: &mut [u64; 16]) { - let mut x = *h; - perm_p(&mut x); - let mut u = 0; - while u < 16 { - h[u] ^= x[u]; - u += 1; - } -} - -pub const fn hash512(data: &[u8]) -> [u8; 64] { - let mut h = IV; - let mut count = 0u64; - - let mut pos = 0; - while pos + BLOCK <= data.len() { - compress(&mut h, &extract::(data, pos)); - count = count.wrapping_add(1); - pos += BLOCK; - } - - let ptr = data.len() - pos; - let mut pad = [0u8; 256]; - let mut pi = 0; - while pi < ptr { - pad[pi] = data[pos + pi]; - pi += 1; - } - pad[ptr] = 0x80; - - let pad_len; - if ptr < 120 { - pad_len = BLOCK; - count = count.wrapping_add(1); - } else { - pad_len = 2 * BLOCK; - count = count.wrapping_add(2); - } - - // Encode block count as 64-bit BE at end of padding. - let cb = count.to_be_bytes(); - let mut ci = 0; - while ci < 8 { - pad[pad_len - 8 + ci] = cb[ci]; - ci += 1; - } - - // Process padding blocks. - let mut p = 0; - while p < pad_len { - compress(&mut h, &extract::(&pad, p)); - p += BLOCK; - } - - // Output transform. - output_transform(&mut h); - - // Extract H[8..15] as LE bytes. - let mut out = [0u8; 64]; - let mut i = 0; - while i < 8 { - store_u64_le(&mut out, i, h[i + 8]); - i += 1; - } - out -} - -#[cfg(test)] -mod tests { - use super::*; - - /// Proves hash512 evaluates at compile time. - const _: [u8; 64] = hash512(b""); -} diff --git a/pkgs/pow/src/lib.rs b/pkgs/pow/src/lib.rs index 894107652..5da51fc9d 100644 --- a/pkgs/pow/src/lib.rs +++ b/pkgs/pow/src/lib.rs @@ -57,7 +57,7 @@ compile_error!("dash-pow requires 128-bit SIMD, build with `-C target-feature=+s #[cfg_attr( not(any(test, feature = "simd")), - expect(dead_code, unused_imports, reason = "only the SIMD backends use the block interface") + expect(dead_code, reason = "only the SIMD backends use the block interface") )] mod aes; mod blake; diff --git a/pkgs/pow/tests/groestl.rs b/pkgs/pow/tests/groestl.rs deleted file mode 100644 index ee6acbdca..000000000 --- a/pkgs/pow/tests/groestl.rs +++ /dev/null @@ -1,157 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Groestl-512 tests. - -#![cfg_attr(feature = "simd", feature(portable_simd))] - -mod common; - -#[cfg(feature = "simd")] -use dash_pow::__private::groestl::{scalar, simd}; -#[cfg(feature = "simd")] -use rstest::rstest; - -#[cfg(feature = "simd")] -use core::simd::Simd; - -/// Groestl-512 IV: all zeros except the last word encodes the output size. -#[cfg(feature = "simd")] -fn groestl_iv() -> [u64; 16] { - let mut h = [0u64; 16]; - let out = 512u64; - h[15] = ((out & 0xFF) << 56) | ((out & 0xFF00) << 40); - h -} - -/// Groestl-512 IV unpacked into the row-wise layout used by the SIMD path. -#[cfg(feature = "simd")] -fn groestl_iv_rows() -> [Simd; 8] { - let iv = groestl_iv(); - u64_state_to_rows(&iv) -} - -/// Converts a `[u64; 16]` column-major state to eight rows of 16 bytes -/// using the same column-major to row-major transpose as `load_block`. -/// -/// Byte `(row, col)` lives at offset `col * 8 + row` in the flat buffer, -/// and in `state[col]` at byte position `row`. -#[cfg(feature = "simd")] -fn u64_state_to_rows(state: &[u64; 16]) -> [Simd; 8] { - core::array::from_fn(|row| { - let mut bytes = [0u8; 16]; - for col in 0..16 { - bytes[col] = (state[col] >> (row * 8)) as u8; - } - Simd::from_array(bytes) - }) -} - -/// Converts eight row vectors back to a `[u64; 16]` column-major state. -#[cfg(feature = "simd")] -fn rows_to_u64_state(rows: &[Simd; 8]) -> [u64; 16] { - let mut state = [0u64; 16]; - for (col, slot) in state.iter_mut().enumerate() { - let mut word = 0u64; - for (row, r) in rows.iter().enumerate() { - word |= (r.as_array()[col] as u64) << (row * 8); - } - *slot = word; - } - state -} - -#[cfg(feature = "simd")] -#[test] -fn state_round_trip() { - let orig = groestl_iv(); - assert_eq!(rows_to_u64_state(&u64_state_to_rows(&orig)), orig); -} - -/// Creates a 128-byte block filled with a running pattern. -#[cfg(feature = "simd")] -fn make_block(fill: u8) -> [u8; 128] { - let mut b = [0u8; 128]; - for (i, slot) in b.iter_mut().enumerate() { - *slot = fill.wrapping_add(i as u8); - } - b -} - -#[cfg(feature = "simd")] -#[rstest] -#[case::zeros([0u8; 128])] -#[case::pattern(make_block(0x37))] -#[case::high(make_block(0xC1))] -fn compress(#[case] block: [u8; 128]) { - let mut h_scalar = groestl_iv(); - scalar::compress(&mut h_scalar, &block); - - let mut h_simd = groestl_iv_rows(); - simd::compress_block(&mut h_simd, &block); - - let simd_as_u64 = rows_to_u64_state(&h_simd); - assert_eq!(h_scalar, simd_as_u64, "groestl compress diverged"); -} - -#[cfg(feature = "simd")] -#[test] -fn output_transform_agree() { - // Start from a state produced by compressing one block. - let block = make_block(0x55); - let mut h_scalar = groestl_iv(); - scalar::compress(&mut h_scalar, &block); - - let mut h_simd = u64_state_to_rows(&h_scalar); - - scalar::output_transform(&mut h_scalar); - simd::output_transform(&mut h_simd); - - // Compare the right-half extraction (the actual digest bytes). - let mut out_scalar = [0u8; 64]; - for i in 0..8 { - out_scalar[i * 8..(i + 1) * 8].copy_from_slice(&h_scalar[i + 8].to_le_bytes()); - } - - let mut out_simd = [0u8; 64]; - simd::extract_right_half(&h_simd, &mut out_simd); - - assert_eq!(out_scalar, out_simd, "groestl output_transform diverged"); -} - -mod kat { - use crate::common; - - use dash_pow::__private::groestl::scalar; - #[cfg(feature = "simd")] - use dash_pow::__private::groestl::simd; - - #[test] - fn nist_vectors_scalar() { - let vectors = common::load("groestl"); - common::run_nist_kat("groestl/scalar", &vectors, scalar::hash512); - } - - #[cfg(feature = "simd")] - #[test] - fn nist_vectors_simd() { - let vectors = common::load("groestl"); - common::run_nist_kat("groestl/simd", &vectors, simd::hash512); - } - - #[cfg(feature = "simd")] - #[test] - fn scalar_simd_agree_on_empty() { - assert_eq!(scalar::hash512(b""), simd::hash512(b"")); - } - - #[cfg(feature = "simd")] - #[test] - fn scalar_simd_agree_on_short() { - let msg = b"dash"; - assert_eq!(scalar::hash512(msg), simd::hash512(msg)); - } -} From a4b7fb53c14fe108005a3e83682d7255819c0f31 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 13 Oct 2026 03:17:20 +0530 Subject: [PATCH 12/27] pow%refac(skein)!: drop scalar backend, inline KAT test --- pkgs/pow/Cargo.toml | 2 - pkgs/pow/src/consts/mod.rs | 1 + .../src/{skein/consts.rs => consts/skein.rs} | 0 pkgs/pow/src/{skein/simd.rs => skein.rs} | 15 +- pkgs/pow/src/skein/mod.rs | 20 --- pkgs/pow/src/skein/scalar.rs | 87 ----------- pkgs/pow/tests/skein.rs | 143 ------------------ 7 files changed, 14 insertions(+), 254 deletions(-) rename pkgs/pow/src/{skein/consts.rs => consts/skein.rs} (100%) rename pkgs/pow/src/{skein/simd.rs => skein.rs} (94%) delete mode 100644 pkgs/pow/src/skein/mod.rs delete mode 100644 pkgs/pow/src/skein/scalar.rs delete mode 100644 pkgs/pow/tests/skein.rs diff --git a/pkgs/pow/Cargo.toml b/pkgs/pow/Cargo.toml index c2cee347c..7d71ca73b 100644 --- a/pkgs/pow/Cargo.toml +++ b/pkgs/pow/Cargo.toml @@ -42,8 +42,6 @@ name = "luffa" name = "shavite" [[test]] name = "simd_hash" -[[test]] -name = "skein" [[bench]] name = "pow" path = "bench/main.rs" diff --git a/pkgs/pow/src/consts/mod.rs b/pkgs/pow/src/consts/mod.rs index 34b5e4fe0..7a31a4e79 100644 --- a/pkgs/pow/src/consts/mod.rs +++ b/pkgs/pow/src/consts/mod.rs @@ -9,3 +9,4 @@ pub(crate) mod blake; pub(crate) mod bmw; pub(crate) mod groestl; +pub(crate) mod skein; diff --git a/pkgs/pow/src/skein/consts.rs b/pkgs/pow/src/consts/skein.rs similarity index 100% rename from pkgs/pow/src/skein/consts.rs rename to pkgs/pow/src/consts/skein.rs diff --git a/pkgs/pow/src/skein/simd.rs b/pkgs/pow/src/skein.rs similarity index 94% rename from pkgs/pow/src/skein/simd.rs rename to pkgs/pow/src/skein.rs index 17ab1eddb..057f08eea 100644 --- a/pkgs/pow/src/skein/simd.rs +++ b/pkgs/pow/src/skein.rs @@ -4,9 +4,9 @@ // See the accompanying file LICENSE or https://opensource.org/license/MIT // -//! SIMD Skein-512 implementation. +//! Skein-512 hash function. -use super::consts::{BLOCK, IV, NW}; +use crate::consts::skein::{BLOCK, IV, NW}; use crate::util::memops::{load_u64_le, store_u64_le}; use crate::util::threefish::encrypt; @@ -137,3 +137,14 @@ pub fn hash512(data: &[u8]) -> [u8; 64] { } out } + +#[cfg(test)] +mod tests { + use super::hash512; + use crate::tests::run_nist_kat; + + #[test] + fn nist_kat() { + run_nist_kat("skein", hash512); + } +} diff --git a/pkgs/pow/src/skein/mod.rs b/pkgs/pow/src/skein/mod.rs deleted file mode 100644 index 81558f00a..000000000 --- a/pkgs/pow/src/skein/mod.rs +++ /dev/null @@ -1,20 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Skein-512 hash function. - -#[doc(hidden)] -pub mod consts; -#[doc(hidden)] -pub mod scalar; -#[cfg(feature = "simd")] -#[doc(hidden)] -pub mod simd; - -#[cfg(not(feature = "simd"))] -pub use scalar::hash512; -#[cfg(feature = "simd")] -pub use simd::hash512; diff --git a/pkgs/pow/src/skein/scalar.rs b/pkgs/pow/src/skein/scalar.rs deleted file mode 100644 index 6c4b39fdc..000000000 --- a/pkgs/pow/src/skein/scalar.rs +++ /dev/null @@ -1,87 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Scalar Skein-512 implementation. - -use super::consts::{BLOCK, IV, NW}; -use crate::util::memops::{extract, load_u64_le, store_u64_le}; -use crate::util::threefish; - -/// UBI chaining: processes one 64-byte block. -/// -/// `etype` encodes the block type and first/final flags in bits 55..62. -pub const fn ubi(h: &mut [u64; NW], block: &[u8], bcount: u64, extra: usize, etype: u64) { - let t0 = (bcount << 6).wrapping_add(extra as u64); - let t1 = (bcount >> 58) | (etype << 55); - - let mut p = [0u64; NW]; - let mut i = 0; - while i < NW { - p[i] = load_u64_le(block, i); - i += 1; - } - - let plaintext = p; - threefish::encrypt(&mut p, h, &[t0, t1]); - - // Matyas-Meyer-Oseas feedforward. - i = 0; - while i < NW { - h[i] = p[i] ^ plaintext[i]; - i += 1; - } -} - -pub const fn hash512(data: &[u8]) -> [u8; 64] { - let mut h = IV; - let mut bcount: u64 = 0; - - // Etype encoding: (type_code << 1) | (FIRST << 7) | (FINAL << 8). - const MSG: u64 = 48 << 1; - const FIRST: u64 = 1 << 7; - const FINAL: u64 = 1 << 8; - const OUTPUT: u64 = 63 << 1; - - // Process full blocks, deferring the last one so it gets the FINAL flag. - let mut pos = 0; - while pos + BLOCK < data.len() { - let first = if pos == 0 { FIRST } else { 0 }; - ubi(&mut h, &extract::(data, pos), bcount, BLOCK, MSG + first); - bcount += 1; - pos += BLOCK; - } - - // Final message block (may be full or partial, padded with zeros). - let remaining = data.len() - pos; - let mut last = [0u8; BLOCK]; - let mut j = 0; - while j < remaining { - last[j] = data[pos + j]; - j += 1; - } - let first = if bcount == 0 { FIRST } else { 0 }; - ubi(&mut h, &last, bcount, remaining, MSG + FINAL + first); - - // Output block. - let out_block = [0u8; BLOCK]; - ubi(&mut h, &out_block, 0, 8, OUTPUT + FIRST + FINAL); - - let mut out = [0u8; 64]; - let mut i = 0; - while i < NW { - store_u64_le(&mut out, i, h[i]); - i += 1; - } - out -} - -#[cfg(test)] -mod tests { - use super::*; - - /// Proves hash512 evaluates at compile time. - const _: [u8; 64] = hash512(b""); -} diff --git a/pkgs/pow/tests/skein.rs b/pkgs/pow/tests/skein.rs deleted file mode 100644 index 0ca1dca07..000000000 --- a/pkgs/pow/tests/skein.rs +++ /dev/null @@ -1,143 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Skein-512 tests. - -mod common; - -#[cfg(feature = "simd")] -use dash_pow::__private::skein::{consts::IV, scalar, simd}; -#[cfg(feature = "simd")] -use rstest::rstest; - -/// Decodes a 64-byte block as eight little-endian u64 words. -#[cfg(feature = "simd")] -fn bytes_to_words(block: &[u8; 64]) -> [u64; 8] { - core::array::from_fn(|i| { - let off = i * 8; - let mut buf = [0u8; 8]; - buf.copy_from_slice(&block[off..off + 8]); - u64::from_le_bytes(buf) - }) -} - -/// Creates a 64-byte block filled with a running pattern. -#[cfg(feature = "simd")] -fn make_block(fill: u8) -> [u8; 64] { - let mut b = [0u8; 64]; - for (i, slot) in b.iter_mut().enumerate() { - *slot = fill.wrapping_add(i as u8); - } - b -} - -#[cfg(feature = "simd")] -const MSG: u64 = 48 << 1; -#[cfg(feature = "simd")] -const FIRST: u64 = 1 << 7; -#[cfg(feature = "simd")] -const FINAL: u64 = 1 << 8; - -#[cfg(feature = "simd")] -#[rstest] -#[case::zeros_single_block([0u8; 64], 0, 64, MSG + FIRST + FINAL, true, true)] -#[case::pattern_single_block(make_block(0x42), 0, 64, MSG + FIRST + FINAL, true, true)] -#[case::first_only([0u8; 64], 0, 64, MSG + FIRST, true, false)] -fn ubi( - #[case] block: [u8; 64], - #[case] bcount: u64, - #[case] extra: usize, - #[case] etype: u64, - #[case] first: bool, - #[case] final_block: bool, -) { - let block_words = bytes_to_words(&block); - - let mut h_scalar = IV; - let mut h_simd = IV; - - scalar::ubi(&mut h_scalar, &block, bcount, extra, etype); - - simd::ubi( - &mut h_simd, - &block_words, - simd::UbiTweak { - position: (bcount << 6).wrapping_add(extra as u64), - kind: 48, - first, - final_block, - }, - ); - - assert_eq!(h_scalar, h_simd, "skein ubi diverged"); -} - -#[cfg(feature = "simd")] -#[test] -fn output_block_agree() { - // Process empty message through both paths, then compare the output-block step. - let block = [0u8; 64]; - let block_words = bytes_to_words(&block); - - let mut h_scalar = IV; - let mut h_simd = IV; - - // Single message block (empty padded) - scalar::ubi(&mut h_scalar, &block, 0, 0, MSG + FIRST + FINAL); - simd::ubi( - &mut h_simd, - &block_words, - simd::UbiTweak { - position: 0, - kind: 48, - first: true, - final_block: true, - }, - ); - assert_eq!(h_scalar, h_simd, "skein message ubi diverged"); - - // Output block - let out_block = [0u8; 64]; - const OUTPUT: u64 = 63 << 1; - scalar::ubi(&mut h_scalar, &out_block, 0, 8, OUTPUT + FIRST + FINAL); - simd::output_block(&mut h_simd); - - assert_eq!(h_scalar, h_simd, "skein output block diverged"); -} - -mod kat { - use crate::common; - - use dash_pow::__private::skein::scalar; - #[cfg(feature = "simd")] - use dash_pow::__private::skein::simd; - - #[test] - fn nist_vectors_scalar() { - let vectors = common::load("skein"); - common::run_nist_kat("skein/scalar", &vectors, scalar::hash512); - } - - #[cfg(feature = "simd")] - #[test] - fn nist_vectors_simd() { - let vectors = common::load("skein"); - common::run_nist_kat("skein/simd", &vectors, simd::hash512); - } - - #[cfg(feature = "simd")] - #[test] - fn scalar_simd_agree_on_empty() { - assert_eq!(scalar::hash512(b""), simd::hash512(b"")); - } - - #[cfg(feature = "simd")] - #[test] - fn scalar_simd_agree_on_short() { - let msg = b"dash"; - assert_eq!(scalar::hash512(msg), simd::hash512(msg)); - } -} From 82b7bb84941ee4439f8d60dbe9cbfa08071cdc8d Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Wed, 7 Oct 2026 11:35:41 +0530 Subject: [PATCH 13/27] pow%refac(jh)!: use `wide`, drop scalar backend, inline KAT test --- pkgs/pow/Cargo.toml | 2 - pkgs/pow/src/{jh/consts.rs => consts/jh.rs} | 0 pkgs/pow/src/consts/mod.rs | 1 + pkgs/pow/src/{jh/simd.rs => jh.rs} | 47 +++- pkgs/pow/src/jh/mod.rs | 20 -- pkgs/pow/src/jh/scalar.rs | 282 -------------------- pkgs/pow/tests/jh.rs | 151 ----------- 7 files changed, 36 insertions(+), 467 deletions(-) rename pkgs/pow/src/{jh/consts.rs => consts/jh.rs} (100%) rename pkgs/pow/src/{jh/simd.rs => jh.rs} (84%) delete mode 100644 pkgs/pow/src/jh/mod.rs delete mode 100644 pkgs/pow/src/jh/scalar.rs delete mode 100644 pkgs/pow/tests/jh.rs diff --git a/pkgs/pow/Cargo.toml b/pkgs/pow/Cargo.toml index 7d71ca73b..f09da3717 100644 --- a/pkgs/pow/Cargo.toml +++ b/pkgs/pow/Cargo.toml @@ -33,8 +33,6 @@ name = "cubehash" [[test]] name = "echo" [[test]] -name = "jh" -[[test]] name = "keccak" [[test]] name = "luffa" diff --git a/pkgs/pow/src/jh/consts.rs b/pkgs/pow/src/consts/jh.rs similarity index 100% rename from pkgs/pow/src/jh/consts.rs rename to pkgs/pow/src/consts/jh.rs diff --git a/pkgs/pow/src/consts/mod.rs b/pkgs/pow/src/consts/mod.rs index 7a31a4e79..562389c52 100644 --- a/pkgs/pow/src/consts/mod.rs +++ b/pkgs/pow/src/consts/mod.rs @@ -9,4 +9,5 @@ pub(crate) mod blake; pub(crate) mod bmw; pub(crate) mod groestl; +pub(crate) mod jh; pub(crate) mod skein; diff --git a/pkgs/pow/src/jh/simd.rs b/pkgs/pow/src/jh.rs similarity index 84% rename from pkgs/pow/src/jh/simd.rs rename to pkgs/pow/src/jh.rs index a1c9dd891..0064ce4b0 100644 --- a/pkgs/pow/src/jh/simd.rs +++ b/pkgs/pow/src/jh.rs @@ -4,21 +4,21 @@ // See the accompanying file LICENSE or https://opensource.org/license/MIT // -//! SIMD JH-512 implementation. +//! JH-512 hash function. -use super::consts::{BLOCK, IV, ROUND_CONSTS}; +use crate::consts::jh::{BLOCK, IV, ROUND_CONSTS}; use crate::util::memops::{load_u64_le, store_u64_le}; -use core::simd::{simd_swizzle, Simd}; +use wide::u32x4; /// One 128-bit JH row, stored as four 32-bit lanes. -pub type Row = Simd; +pub type Row = u32x4; /// JH state as eight 128-bit rows. type State = [Row; 8]; #[inline] pub fn row_from_u64_pair(pair: [u64; 2]) -> Row { - Row::from_array([ + Row::new([ pair[0] as u32, (pair[0] >> 32) as u32, pair[1] as u32, @@ -67,31 +67,41 @@ fn apply_linear_layer(y: &mut [Row; 8]) { /// Swaps every adjacent bit pair: `(b0, b1) -> (b1, b0)`. #[inline] fn swap_adjacent_bits(x: Row) -> Row { - ((x & Row::splat(0x5555_5555)) << Simd::splat(1)) | ((x & Row::splat(0xaaaa_aaaa)) >> Simd::splat(1)) + const LO: Row = Row::splat(0x5555_5555); + const HI: Row = Row::splat(0xaaaa_aaaa); + ((x & LO) << 1u32) | ((x & HI) >> 1u32) } /// Swaps 2-bit groups inside each 4-bit chunk. #[inline] fn swap_bit_pairs(x: Row) -> Row { - ((x & Row::splat(0x3333_3333)) << Simd::splat(2)) | ((x & Row::splat(0xcccc_cccc)) >> Simd::splat(2)) + const LO: Row = Row::splat(0x3333_3333); + const HI: Row = Row::splat(0xcccc_cccc); + ((x & LO) << 2u32) | ((x & HI) >> 2u32) } /// Swaps the low and high nibble inside each byte. #[inline] fn swap_nibbles(x: Row) -> Row { - ((x & Row::splat(0x0f0f_0f0f)) << Simd::splat(4)) | ((x & Row::splat(0xf0f0_f0f0)) >> Simd::splat(4)) + const LO: Row = Row::splat(0x0f0f_0f0f); + const HI: Row = Row::splat(0xf0f0_f0f0); + ((x & LO) << 4u32) | ((x & HI) >> 4u32) } /// Swaps the low and high byte inside each 16-bit chunk. #[inline] fn swap_bytes_in_half_words(x: Row) -> Row { - ((x & Row::splat(0x00ff_00ff)) << Simd::splat(8)) | ((x & Row::splat(0xff00_ff00)) >> Simd::splat(8)) + const LO: Row = Row::splat(0x00ff_00ff); + const HI: Row = Row::splat(0xff00_ff00); + ((x & LO) << 8u32) | ((x & HI) >> 8u32) } /// Swaps the low and high 16-bit halves inside each 32-bit word. #[inline] fn swap_half_words(x: Row) -> Row { - ((x & Row::splat(0x0000_ffff)) << Simd::splat(16)) | ((x & Row::splat(0xffff_0000)) >> Simd::splat(16)) + const LO: Row = Row::splat(0x0000_ffff); + const HI: Row = Row::splat(0xffff_0000); + ((x & LO) << 16u32) | ((x & HI) >> 16u32) } /// Applies the S-box and linear layer to all eight rows. @@ -143,6 +153,8 @@ fn apply_round_core(state: &mut State, constants: [u64; 4]) { /// dispatch disappears entirely. #[inline(always)] fn apply_swap(state: &mut State) { + const SWAP_WORDS: Row = Row::new([1, 0, 3, 2]); + const SWAP_DWORDS: Row = Row::new([2, 3, 0, 1]); let mut row = 1; while row < 8 { match OP { @@ -151,8 +163,8 @@ fn apply_swap(state: &mut State) { 2 => state[row] = swap_nibbles(state[row]), 3 => state[row] = swap_bytes_in_half_words(state[row]), 4 => state[row] = swap_half_words(state[row]), - 5 => state[row] = simd_swizzle!(state[row], [1, 0, 3, 2]), - 6 => state[row] = simd_swizzle!(state[row], [2, 3, 0, 1]), + 5 => state[row] = state[row].shuffle(SWAP_WORDS), + 6 => state[row] = state[row].shuffle(SWAP_DWORDS), _ => {} } row += 2; @@ -257,3 +269,14 @@ pub fn hash512(data: &[u8]) -> [u8; 64] { } out } + +#[cfg(test)] +mod tests { + use super::hash512; + use crate::tests::run_nist_kat; + + #[test] + fn nist_kat() { + run_nist_kat("jh", hash512); + } +} diff --git a/pkgs/pow/src/jh/mod.rs b/pkgs/pow/src/jh/mod.rs deleted file mode 100644 index 301d440cb..000000000 --- a/pkgs/pow/src/jh/mod.rs +++ /dev/null @@ -1,20 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! JH-512 hash function. - -#[doc(hidden)] -pub mod consts; -#[doc(hidden)] -pub mod scalar; -#[cfg(feature = "simd")] -#[doc(hidden)] -pub mod simd; - -#[cfg(not(feature = "simd"))] -pub use scalar::hash512; -#[cfg(feature = "simd")] -pub use simd::hash512; diff --git a/pkgs/pow/src/jh/scalar.rs b/pkgs/pow/src/jh/scalar.rs deleted file mode 100644 index 05a85138a..000000000 --- a/pkgs/pow/src/jh/scalar.rs +++ /dev/null @@ -1,282 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Scalar JH-512 implementation. - -use super::consts::{BLOCK, IV, ROUND_CONSTS}; - -/// E8 permutation on 16 u64 words: 42 rounds cycling W0-W6. -pub const fn e8(h: &mut [u64; 16]) { - // Extract to locals to avoid array borrow issues. - #[rustfmt::skip] - let [mut h0, mut h1, mut h2, mut h3, - mut h4, mut h5, mut h6, mut h7, - mut h8, mut h9, mut ha, mut hb, - mut hc, mut hd, mut he, mut hf] = *h; - - // S-box: bitslice 4-input nonlinear substitution. - macro_rules! sb { - ($x0:ident, $x1:ident, $x2:ident, $x3:ident, $c:expr) => {{ - $x3 = !$x3; - $x0 ^= $c & !$x2; - let tmp = $c ^ ($x0 & $x1); - $x0 ^= $x2 & $x3; - $x3 ^= !$x1 & $x2; - $x1 ^= $x0 & $x2; - $x2 ^= $x0 & !$x3; - $x0 ^= $x1 | $x3; - $x3 ^= $x1 & $x2; - $x1 ^= tmp & $x0; - $x2 ^= tmp; - }}; - } - - // Linear transform. - macro_rules! lb { - ($x0:ident, $x1:ident, $x2:ident, $x3:ident, - $x4:ident, $x5:ident, $x6:ident, $x7:ident) => {{ - $x4 ^= $x1; - $x5 ^= $x2; - $x6 ^= $x3 ^ $x0; - $x7 ^= $x0; - $x0 ^= $x5; - $x1 ^= $x6; - $x2 ^= $x7 ^ $x4; - $x3 ^= $x4; - }}; - } - - // W permutation: bit-group swap within u32 halves. - macro_rules! wz { - ($x:ident, $mask:expr, $shift:expr) => {{ - let m = ($mask as u64) | (($mask as u64) << 32); - $x = (($x >> $shift) & m) | (($x & m) << $shift); - }}; - } - - // One round: S-box on even+odd columns, linear transform, - // word permutation (W0-W6) on odd-indexed halves. - macro_rules! sl { - ($r:expr, $ro:expr) => {{ - // State layout: 8 pairs (hNh, hNl) stored as H[2N], H[2N+1]. - // S-box columns: (H[0],H[4],H[8],H[12]), (H[1],H[5],H[9],H[13]), - // (H[2],H[6],H[10],H[14]), (H[3],H[7],H[11],H[15]). - sb!(h0, h4, h8, hc, ROUND_CONSTS[$r][0]); // even hi - sb!(h1, h5, h9, hd, ROUND_CONSTS[$r][1]); // even lo - sb!(h2, h6, ha, he, ROUND_CONSTS[$r][2]); // odd hi - sb!(h3, h7, hb, hf, ROUND_CONSTS[$r][3]); // odd lo - lb!(h0, h4, h8, hc, h2, h6, ha, he); // L on hi - lb!(h1, h5, h9, hd, h3, h7, hb, hf); // L on lo - // W permutation on odd pairs: (h2,h3), (h6,h7), (ha,hb), (he,hf) - match $ro { - 0 => { - wz!(h2, 0x55555555u32, 1); - wz!(h3, 0x55555555u32, 1); - wz!(h6, 0x55555555u32, 1); - wz!(h7, 0x55555555u32, 1); - wz!(ha, 0x55555555u32, 1); - wz!(hb, 0x55555555u32, 1); - wz!(he, 0x55555555u32, 1); - wz!(hf, 0x55555555u32, 1); - } - 1 => { - wz!(h2, 0x33333333u32, 2); - wz!(h3, 0x33333333u32, 2); - wz!(h6, 0x33333333u32, 2); - wz!(h7, 0x33333333u32, 2); - wz!(ha, 0x33333333u32, 2); - wz!(hb, 0x33333333u32, 2); - wz!(he, 0x33333333u32, 2); - wz!(hf, 0x33333333u32, 2); - } - 2 => { - wz!(h2, 0x0F0F0F0Fu32, 4); - wz!(h3, 0x0F0F0F0Fu32, 4); - wz!(h6, 0x0F0F0F0Fu32, 4); - wz!(h7, 0x0F0F0F0Fu32, 4); - wz!(ha, 0x0F0F0F0Fu32, 4); - wz!(hb, 0x0F0F0F0Fu32, 4); - wz!(he, 0x0F0F0F0Fu32, 4); - wz!(hf, 0x0F0F0F0Fu32, 4); - } - 3 => { - wz!(h2, 0x00FF00FFu32, 8); - wz!(h3, 0x00FF00FFu32, 8); - wz!(h6, 0x00FF00FFu32, 8); - wz!(h7, 0x00FF00FFu32, 8); - wz!(ha, 0x00FF00FFu32, 8); - wz!(hb, 0x00FF00FFu32, 8); - wz!(he, 0x00FF00FFu32, 8); - wz!(hf, 0x00FF00FFu32, 8); - } - 4 => { - wz!(h2, 0x0000FFFFu32, 16); - wz!(h3, 0x0000FFFFu32, 16); - wz!(h6, 0x0000FFFFu32, 16); - wz!(h7, 0x0000FFFFu32, 16); - wz!(ha, 0x0000FFFFu32, 16); - wz!(hb, 0x0000FFFFu32, 16); - wz!(he, 0x0000FFFFu32, 16); - wz!(hf, 0x0000FFFFu32, 16); - } - 5 => { - // Swap u32 halves within each u64 - h2 = h2.rotate_left(32); - h3 = h3.rotate_left(32); - h6 = h6.rotate_left(32); - h7 = h7.rotate_left(32); - ha = ha.rotate_left(32); - hb = hb.rotate_left(32); - he = he.rotate_left(32); - hf = hf.rotate_left(32); - } - 6 => { - // Swap the two u64s in each pair - ::core::mem::swap(&mut h2, &mut h3); - ::core::mem::swap(&mut h6, &mut h7); - ::core::mem::swap(&mut ha, &mut hb); - ::core::mem::swap(&mut he, &mut hf); - } - _ => {} - } - }}; - } - - let mut r = 0; - while r < 42 { - sl!(r, 0); - sl!(r + 1, 1); - sl!(r + 2, 2); - sl!(r + 3, 3); - sl!(r + 4, 4); - sl!(r + 5, 5); - sl!(r + 6, 6); - r += 7; - } - - *h = [h0, h1, h2, h3, h4, h5, h6, h7, h8, h9, ha, hb, hc, hd, he, hf]; -} - -/// XOR message block into first or second half of state. -const fn xor_block(h: &mut [u64; 16], buf: &[u8], offset: usize) { - let mut i = 0; - while i < 8 { - let off = i * 8; - h[offset + i] ^= u64::from_le_bytes([ - buf[off], - buf[off + 1], - buf[off + 2], - buf[off + 3], - buf[off + 4], - buf[off + 5], - buf[off + 6], - buf[off + 7], - ]); - i += 1; - } -} - -/// Process one 64-byte block through the JH compression. -pub const fn compress(h: &mut [u64; 16], buf: &[u8]) { - xor_block(h, buf, 0); - e8(h); - xor_block(h, buf, 8); -} - -/// Flattens the `[[u64; 2]; 8]` IV into a `[u64; 16]` state array. -const fn flatten_iv() -> [u64; 16] { - let mut h = [0u64; 16]; - let mut i = 0; - while i < 8 { - h[2 * i] = IV[i][0]; - h[2 * i + 1] = IV[i][1]; - i += 1; - } - h -} - -pub const fn hash512(data: &[u8]) -> [u8; 64] { - let mut h = flatten_iv(); - let mut block_count: u64 = 0; - let mut buf = [0u8; BLOCK]; - let mut ptr = 0usize; - - let mut di = 0; - while di < data.len() { - buf[ptr] = data[di]; - ptr += 1; - if ptr == BLOCK { - compress(&mut h, &buf); - block_count += 1; - ptr = 0; - } - di += 1; - } - - // Padding flows through the same buffer to match block boundaries for messages - // at the 64-byte boundary. - let mut pad = [0u8; 128]; - pad[0] = 0x80; - - let nz = if ptr == 0 { 47 } else { 111 - ptr }; - - let bit_lo = (block_count << 9).wrapping_add((ptr as u64) << 3); - let bit_hi = block_count >> 55; - - let len_off = 1 + nz; - let hi_bytes = bit_hi.to_be_bytes(); - let mut ci = 0; - while ci < 8 { - pad[len_off + ci] = hi_bytes[ci]; - ci += 1; - } - let lo_bytes = bit_lo.to_be_bytes(); - ci = 0; - while ci < 8 { - pad[len_off + 8 + ci] = lo_bytes[ci]; - ci += 1; - } - - let total = nz + 17; - let mut fed = 0; - while fed < total { - let space = BLOCK - ptr; - let chunk = if space < total - fed { space } else { total - fed }; - let mut ci = 0; - while ci < chunk { - buf[ptr + ci] = pad[fed + ci]; - ci += 1; - } - ptr += chunk; - fed += chunk; - if ptr == BLOCK { - compress(&mut h, &buf); - ptr = 0; - } - } - - let mut out = [0u8; 64]; - let mut i = 0; - while i < 8 { - let b = h[i + 8].to_le_bytes(); - let base = i * 8; - let mut j = 0; - while j < 8 { - out[base + j] = b[j]; - j += 1; - } - i += 1; - } - out -} - -#[cfg(test)] -mod tests { - use super::*; - - /// Proves hash512 evaluates at compile time. - const _: [u8; 64] = hash512(b""); -} diff --git a/pkgs/pow/tests/jh.rs b/pkgs/pow/tests/jh.rs deleted file mode 100644 index 731376b24..000000000 --- a/pkgs/pow/tests/jh.rs +++ /dev/null @@ -1,151 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! JH-512 tests. - -mod common; - -#[cfg(feature = "simd")] -use dash_pow::__private::jh::{consts::IV, scalar, simd}; -#[cfg(feature = "simd")] -use rstest::rstest; - -/// Converts a flat `[u64; 16]` scalar state to eight SIMD rows. -#[cfg(feature = "simd")] -fn u64_to_rows(state: &[u64; 16]) -> [simd::Row; 8] { - core::array::from_fn(|i| simd::row_from_u64_pair([state[i * 2], state[i * 2 + 1]])) -} - -/// Converts eight SIMD rows back to a flat `[u64; 16]` scalar state. -#[cfg(feature = "simd")] -fn rows_to_u64(rows: &[simd::Row; 8]) -> [u64; 16] { - let mut out = [0u64; 16]; - for i in 0..8 { - let p = simd::row_to_u64_pair(rows[i]); - out[i * 2] = p[0]; - out[i * 2 + 1] = p[1]; - } - out -} - -/// Flattens the `[[u64; 2]; 8]` IV into a `[u64; 16]` state array. -#[cfg(feature = "simd")] -fn flatten_iv() -> [u64; 16] { - let mut h = [0u64; 16]; - for i in 0..8 { - h[2 * i] = IV[i][0]; - h[2 * i + 1] = IV[i][1]; - } - h -} - -/// Decodes a 64-byte block as eight little-endian u64 words. -#[cfg(feature = "simd")] -fn bytes_to_words(block: &[u8; 64]) -> [u64; 8] { - core::array::from_fn(|i| { - let off = i * 8; - let mut buf = [0u8; 8]; - buf.copy_from_slice(&block[off..off + 8]); - u64::from_le_bytes(buf) - }) -} - -#[cfg(feature = "simd")] -#[test] -fn row_round_trip() { - let scalar_iv = flatten_iv(); - assert_eq!(rows_to_u64(&u64_to_rows(&scalar_iv)), scalar_iv); -} - -#[cfg(feature = "simd")] -#[test] -fn row_round_trip_arbitrary() { - let mut state = [0u64; 16]; - for (i, slot) in state.iter_mut().enumerate() { - *slot = 0xdeadbeef_cafebabe_u64.wrapping_mul(i as u64 + 1); - } - assert_eq!(rows_to_u64(&u64_to_rows(&state)), state); -} - -#[cfg(feature = "simd")] -#[rstest] -#[case::zeros([0u8; 64])] -#[case::pattern({ - let mut b = [0u8; 64]; - for (i, slot) in b.iter_mut().enumerate() { *slot = (i as u8).wrapping_mul(11); } - b -})] -#[case::high({ - let mut b = [0u8; 64]; - for (i, slot) in b.iter_mut().enumerate() { *slot = 0xFF - i as u8; } - b -})] -fn compress(#[case] block: [u8; 64]) { - let scalar_iv = flatten_iv(); - let words = bytes_to_words(&block); - - let mut h_scalar = scalar_iv; - let mut h_simd = IV.map(simd::row_from_u64_pair); - - scalar::compress(&mut h_scalar, &block); - simd::compress(&mut h_simd, &words); - - assert_eq!(h_scalar, rows_to_u64(&h_simd), "jh compress diverged"); -} - -#[cfg(feature = "simd")] -#[test] -fn e8_agree() { - let scalar_iv = flatten_iv(); - - let mut h_scalar = scalar_iv; - let mut h_simd = u64_to_rows(&scalar_iv); - - scalar::e8(&mut h_scalar); - - // The SIMD path doesn't expose e8 directly, but we can - // compress a zero block and back out the XOR: - // compress does: state ^= block[0..4], e8, state[4..8] ^= block[0..4] - // With zero block, compress is just e8. - let zero_words = [0u64; 8]; - simd::compress(&mut h_simd, &zero_words); - - assert_eq!(h_scalar, rows_to_u64(&h_simd), "jh e8 diverged"); -} - -mod kat { - use crate::common; - - use dash_pow::__private::jh::scalar; - #[cfg(feature = "simd")] - use dash_pow::__private::jh::simd; - - #[test] - fn nist_vectors_scalar() { - let vectors = common::load("jh"); - common::run_nist_kat("jh/scalar", &vectors, scalar::hash512); - } - - #[cfg(feature = "simd")] - #[test] - fn nist_vectors_simd() { - let vectors = common::load("jh"); - common::run_nist_kat("jh/simd", &vectors, simd::hash512); - } - - #[cfg(feature = "simd")] - #[test] - fn scalar_simd_agree_on_empty() { - assert_eq!(scalar::hash512(b""), simd::hash512(b"")); - } - - #[cfg(feature = "simd")] - #[test] - fn scalar_simd_agree_on_short() { - let msg = b"dash"; - assert_eq!(scalar::hash512(msg), simd::hash512(msg)); - } -} From 2c4a2f4a5010f8c3a12975a94a4f4bb8b23237e2 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 13 Oct 2026 03:18:46 +0530 Subject: [PATCH 14/27] pow%refac(keccak)!: drop scalar backend, inline KAT test --- pkgs/pow/Cargo.toml | 2 - .../{keccak/consts.rs => consts/keccak.rs} | 0 pkgs/pow/src/consts/mod.rs | 1 + pkgs/pow/src/{keccak/simd.rs => keccak.rs} | 103 +++++++++++---- pkgs/pow/src/keccak/mod.rs | 60 --------- pkgs/pow/src/keccak/scalar.rs | 117 ------------------ pkgs/pow/tests/keccak.rs | 52 -------- 7 files changed, 77 insertions(+), 258 deletions(-) rename pkgs/pow/src/{keccak/consts.rs => consts/keccak.rs} (100%) rename pkgs/pow/src/{keccak/simd.rs => keccak.rs} (52%) delete mode 100644 pkgs/pow/src/keccak/mod.rs delete mode 100644 pkgs/pow/src/keccak/scalar.rs delete mode 100644 pkgs/pow/tests/keccak.rs diff --git a/pkgs/pow/Cargo.toml b/pkgs/pow/Cargo.toml index f09da3717..d79a48677 100644 --- a/pkgs/pow/Cargo.toml +++ b/pkgs/pow/Cargo.toml @@ -33,8 +33,6 @@ name = "cubehash" [[test]] name = "echo" [[test]] -name = "keccak" -[[test]] name = "luffa" [[test]] name = "shavite" diff --git a/pkgs/pow/src/keccak/consts.rs b/pkgs/pow/src/consts/keccak.rs similarity index 100% rename from pkgs/pow/src/keccak/consts.rs rename to pkgs/pow/src/consts/keccak.rs diff --git a/pkgs/pow/src/consts/mod.rs b/pkgs/pow/src/consts/mod.rs index 562389c52..d27a302ab 100644 --- a/pkgs/pow/src/consts/mod.rs +++ b/pkgs/pow/src/consts/mod.rs @@ -10,4 +10,5 @@ pub(crate) mod blake; pub(crate) mod bmw; pub(crate) mod groestl; pub(crate) mod jh; +pub(crate) mod keccak; pub(crate) mod skein; diff --git a/pkgs/pow/src/keccak/simd.rs b/pkgs/pow/src/keccak.rs similarity index 52% rename from pkgs/pow/src/keccak/simd.rs rename to pkgs/pow/src/keccak.rs index 6c8f1f435..dad9576b4 100644 --- a/pkgs/pow/src/keccak/simd.rs +++ b/pkgs/pow/src/keccak.rs @@ -4,9 +4,9 @@ // See the accompanying file LICENSE or https://opensource.org/license/MIT // -//! SIMD Keccak-f\[1600\] permutation and sponge. +//! Keccak-512 hash function. -use super::consts::RC; +use crate::consts::keccak::{RC, ROTC}; /// Applies one round of the permutation, reading from `src` and writing to /// `dst`. @@ -34,10 +34,10 @@ fn round_to(src: &[u64; 25], dst: &mut [u64; 25], rc: u64) { // rotations, then chi directly into dst. Only 5 B-values live at once. { let b0 = src[0] ^ d0; - let b1 = (src[6] ^ d1).rotate_left(44); - let b2 = (src[12] ^ d2).rotate_left(43); - let b3 = (src[18] ^ d3).rotate_left(21); - let b4 = (src[24] ^ d4).rotate_left(14); + let b1 = (src[6] ^ d1).rotate_left(ROTC[6]); + let b2 = (src[12] ^ d2).rotate_left(ROTC[12]); + let b3 = (src[18] ^ d3).rotate_left(ROTC[18]); + let b4 = (src[24] ^ d4).rotate_left(ROTC[24]); dst[0] = b0 ^ (!b1 & b2) ^ rc; dst[1] = b1 ^ (!b2 & b3); dst[2] = b2 ^ (!b3 & b4); @@ -45,11 +45,11 @@ fn round_to(src: &[u64; 25], dst: &mut [u64; 25], rc: u64) { dst[4] = b4 ^ (!b0 & b1); } { - let b0 = (src[3] ^ d3).rotate_left(28); - let b1 = (src[9] ^ d4).rotate_left(20); - let b2 = (src[10] ^ d0).rotate_left(3); - let b3 = (src[16] ^ d1).rotate_left(45); - let b4 = (src[22] ^ d2).rotate_left(61); + let b0 = (src[3] ^ d3).rotate_left(ROTC[3]); + let b1 = (src[9] ^ d4).rotate_left(ROTC[9]); + let b2 = (src[10] ^ d0).rotate_left(ROTC[10]); + let b3 = (src[16] ^ d1).rotate_left(ROTC[16]); + let b4 = (src[22] ^ d2).rotate_left(ROTC[22]); dst[5] = b0 ^ (!b1 & b2); dst[6] = b1 ^ (!b2 & b3); dst[7] = b2 ^ (!b3 & b4); @@ -57,11 +57,11 @@ fn round_to(src: &[u64; 25], dst: &mut [u64; 25], rc: u64) { dst[9] = b4 ^ (!b0 & b1); } { - let b0 = (src[1] ^ d1).rotate_left(1); - let b1 = (src[7] ^ d2).rotate_left(6); - let b2 = (src[13] ^ d3).rotate_left(25); - let b3 = (src[19] ^ d4).rotate_left(8); - let b4 = (src[20] ^ d0).rotate_left(18); + let b0 = (src[1] ^ d1).rotate_left(ROTC[1]); + let b1 = (src[7] ^ d2).rotate_left(ROTC[7]); + let b2 = (src[13] ^ d3).rotate_left(ROTC[13]); + let b3 = (src[19] ^ d4).rotate_left(ROTC[19]); + let b4 = (src[20] ^ d0).rotate_left(ROTC[20]); dst[10] = b0 ^ (!b1 & b2); dst[11] = b1 ^ (!b2 & b3); dst[12] = b2 ^ (!b3 & b4); @@ -69,11 +69,11 @@ fn round_to(src: &[u64; 25], dst: &mut [u64; 25], rc: u64) { dst[14] = b4 ^ (!b0 & b1); } { - let b0 = (src[4] ^ d4).rotate_left(27); - let b1 = (src[5] ^ d0).rotate_left(36); - let b2 = (src[11] ^ d1).rotate_left(10); - let b3 = (src[17] ^ d2).rotate_left(15); - let b4 = (src[23] ^ d3).rotate_left(56); + let b0 = (src[4] ^ d4).rotate_left(ROTC[4]); + let b1 = (src[5] ^ d0).rotate_left(ROTC[5]); + let b2 = (src[11] ^ d1).rotate_left(ROTC[11]); + let b3 = (src[17] ^ d2).rotate_left(ROTC[17]); + let b4 = (src[23] ^ d3).rotate_left(ROTC[23]); dst[15] = b0 ^ (!b1 & b2); dst[16] = b1 ^ (!b2 & b3); dst[17] = b2 ^ (!b3 & b4); @@ -81,11 +81,11 @@ fn round_to(src: &[u64; 25], dst: &mut [u64; 25], rc: u64) { dst[19] = b4 ^ (!b0 & b1); } { - let b0 = (src[2] ^ d2).rotate_left(62); - let b1 = (src[8] ^ d3).rotate_left(55); - let b2 = (src[14] ^ d4).rotate_left(39); - let b3 = (src[15] ^ d0).rotate_left(41); - let b4 = (src[21] ^ d1).rotate_left(2); + let b0 = (src[2] ^ d2).rotate_left(ROTC[2]); + let b1 = (src[8] ^ d3).rotate_left(ROTC[8]); + let b2 = (src[14] ^ d4).rotate_left(ROTC[14]); + let b3 = (src[15] ^ d0).rotate_left(ROTC[15]); + let b4 = (src[21] ^ d1).rotate_left(ROTC[21]); dst[20] = b0 ^ (!b1 & b2); dst[21] = b1 ^ (!b2 & b3); dst[22] = b2 ^ (!b3 & b4); @@ -110,6 +110,55 @@ pub fn keccak_f1600(state: &mut [u64; 25]) { } } +/// Keccak-512 sponge parameterised over a permutation function. +pub(crate) fn sponge(data: &[u8], perm: fn(&mut [u64; 25])) -> [u8; 64] { + use crate::consts::keccak::RATE; + use crate::util::memops::{load_u64_le, store_u64_le}; + + let mut state = [0u64; 25]; + let mut pos = 0; + while pos + RATE <= data.len() { + let mut i = 0; + while i < 9 { + state[i] ^= load_u64_le(&data[pos..], i); + i += 1; + } + perm(&mut state); + pos += RATE; + } + + let mut last = [0u8; RATE]; + let remaining = data.len() - pos; + last[..remaining].copy_from_slice(&data[pos..]); + last[remaining] = 0x01; + last[RATE - 1] |= 0x80; + let mut i = 0; + while i < 9 { + state[i] ^= load_u64_le(&last, i); + i += 1; + } + perm(&mut state); + + let mut out = [0u8; 64]; + let mut i = 0; + while i < 8 { + store_u64_le(&mut out, i, state[i]); + i += 1; + } + out +} + pub fn hash512(data: &[u8]) -> [u8; 64] { - super::sponge(data, keccak_f1600) + sponge(data, keccak_f1600) +} + +#[cfg(test)] +mod tests { + use super::hash512; + use crate::tests::run_nist_kat; + + #[test] + fn nist_kat() { + run_nist_kat("keccak", hash512); + } } diff --git a/pkgs/pow/src/keccak/mod.rs b/pkgs/pow/src/keccak/mod.rs deleted file mode 100644 index fc7d1fc4c..000000000 --- a/pkgs/pow/src/keccak/mod.rs +++ /dev/null @@ -1,60 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Keccak-512 hash function. - -#[doc(hidden)] -pub mod consts; -#[doc(hidden)] -pub mod scalar; -#[cfg(feature = "simd")] -#[doc(hidden)] -pub mod simd; - -#[cfg(not(feature = "simd"))] -pub use scalar::hash512; -#[cfg(feature = "simd")] -pub use simd::hash512; - -/// Keccak-512 sponge parameterised over a permutation function. -#[cfg(feature = "simd")] -pub(crate) fn sponge(data: &[u8], perm: fn(&mut [u64; 25])) -> [u8; 64] { - use crate::util::memops::{load_u64_le, store_u64_le}; - - use consts::RATE; - - let mut state = [0u64; 25]; - let mut pos = 0; - while pos + RATE <= data.len() { - let mut i = 0; - while i < 9 { - state[i] ^= load_u64_le(&data[pos..], i); - i += 1; - } - perm(&mut state); - pos += RATE; - } - - let mut last = [0u8; RATE]; - let remaining = data.len() - pos; - last[..remaining].copy_from_slice(&data[pos..]); - last[remaining] = 0x01; - last[RATE - 1] |= 0x80; - let mut i = 0; - while i < 9 { - state[i] ^= load_u64_le(&last, i); - i += 1; - } - perm(&mut state); - - let mut out = [0u8; 64]; - let mut i = 0; - while i < 8 { - store_u64_le(&mut out, i, state[i]); - i += 1; - } - out -} diff --git a/pkgs/pow/src/keccak/scalar.rs b/pkgs/pow/src/keccak/scalar.rs deleted file mode 100644 index ac081b630..000000000 --- a/pkgs/pow/src/keccak/scalar.rs +++ /dev/null @@ -1,117 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Scalar Keccak-f\[1600\] permutation and sponge. - -use super::consts::{RATE, RC, ROTC}; -use crate::util::memops::{extract, load_u64_le, store_u64_le}; - -/// Applies the Keccak-f\[1600\] permutation in place (24 rounds). -/// -/// State is a 5x5 matrix of 64-bit lanes stored in row-major order as -/// `state[x + 5*y]`. -pub const fn keccak_f1600(a: &mut [u64; 25]) { - let mut ri = 0; - while ri < RC.len() { - let rc = RC[ri]; - // theta step: column parity and diffusion - let mut c = [0u64; 5]; - let mut x = 0; - while x < 5 { - c[x] = a[x] ^ a[x + 5] ^ a[x + 10] ^ a[x + 15] ^ a[x + 20]; - x += 1; - } - let mut d = [0u64; 5]; - x = 0; - while x < 5 { - d[x] = c[(x + 4) % 5] ^ c[(x + 1) % 5].rotate_left(1); - x += 1; - } - let mut i = 0; - while i < 25 { - a[i] ^= d[i % 5]; - i += 1; - } - - // rho and pi steps (combined): rotate then move to new position - let mut b = [0u64; 25]; - i = 0; - while i < 25 { - let x = i % 5; - let y = i / 5; - let dst = y + 5 * ((2 * x + 3 * y) % 5); - b[dst] = a[i].rotate_left(ROTC[i]); - i += 1; - } - - // chi step: non-linear mixing per row - x = 0; - while x < 5 { - let mut y = 0; - while y < 5 { - let idx = x + 5 * y; - a[idx] = b[idx] ^ (!b[(x + 1) % 5 + 5 * y] & b[(x + 2) % 5 + 5 * y]); - y += 1; - } - x += 1; - } - - // iota step: round constant injection - a[0] ^= rc; - - ri += 1; - } -} - -/// Absorbs a full `RATE`-byte block into the sponge state. -const fn absorb_block(state: &mut [u64; 25], block: &[u8]) { - let mut i = 0; - while i < RATE / 8 { - state[i] ^= load_u64_le(block, i); - i += 1; - } - keccak_f1600(state); -} - -pub const fn hash512(data: &[u8]) -> [u8; 64] { - let mut state = [0u64; 25]; - - // Absorb full blocks - let mut pos = 0; - while pos + RATE <= data.len() { - absorb_block(&mut state, &extract::(data, pos)); - pos += RATE; - } - - // Pad the final block (multi-rate padding: 10*1) - let mut last = [0u8; RATE]; - let remaining = data.len() - pos; - let mut j = 0; - while j < remaining { - last[j] = data[pos + j]; - j += 1; - } - last[remaining] = 0x01; - last[RATE - 1] |= 0x80; - absorb_block(&mut state, &last); - - // Squeeze: extract 64 bytes (512 bits) from the state - let mut out = [0u8; 64]; - let mut i = 0; - while i < 8 { - store_u64_le(&mut out, i, state[i]); - i += 1; - } - out -} - -#[cfg(test)] -mod tests { - use super::*; - - /// Proves hash512 evaluates at compile time. - const _: [u8; 64] = hash512(b""); -} diff --git a/pkgs/pow/tests/keccak.rs b/pkgs/pow/tests/keccak.rs deleted file mode 100644 index 91644e07d..000000000 --- a/pkgs/pow/tests/keccak.rs +++ /dev/null @@ -1,52 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Keccak-512 tests. - -mod common; - -#[cfg(feature = "simd")] -use dash_pow::__private::keccak::{scalar, simd}; -#[cfg(feature = "simd")] -use rstest::rstest; - -#[cfg(feature = "simd")] -#[rstest] -#[case::zeros(0u64)] -#[case::pattern(0xdead_beef_cafe_babe)] -#[case::sequential(0x0123_4567_89ab_cdef)] -#[case::max(u64::MAX)] -fn keccak_f1600(#[case] seed: u64) { - let mut s = [0u64; 25]; - for (i, slot) in s.iter_mut().enumerate() { - *slot = seed.wrapping_mul(i as u64).wrapping_add(i as u64); - } - let mut t = s; - scalar::keccak_f1600(&mut s); - simd::keccak_f1600(&mut t); - assert_eq!(s, t, "keccak_f1600 diverged with seed {seed:#x}"); -} - -mod kat { - use super::common; - - use dash_pow::__private::keccak::scalar; - #[cfg(feature = "simd")] - use dash_pow::__private::keccak::simd; - - #[test] - fn scalar_nist_kat() { - let vectors = common::load("keccak"); - common::run_nist_kat("keccak/scalar", &vectors, scalar::hash512); - } - - #[cfg(feature = "simd")] - #[test] - fn simd_nist_kat() { - let vectors = common::load("keccak"); - common::run_nist_kat("keccak/simd", &vectors, simd::hash512); - } -} From 681734e027644e6b095517957f2b66e63307d90b Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Wed, 7 Oct 2026 11:37:23 +0530 Subject: [PATCH 15/27] pow%refac(luffa)!: use `wide`, drop scalar backend, inline KAT test --- pkgs/pow/Cargo.toml | 2 - .../src/{luffa/consts.rs => consts/luffa.rs} | 55 ++-- pkgs/pow/src/consts/mod.rs | 1 + pkgs/pow/src/cubehash/simd.rs | 12 +- pkgs/pow/src/{luffa/simd.rs => luffa.rs} | 37 ++- pkgs/pow/src/luffa/mod.rs | 20 -- pkgs/pow/src/luffa/scalar.rs | 236 ------------------ pkgs/pow/src/util/arx.rs | 11 +- pkgs/pow/tests/luffa.rs | 85 ------- 9 files changed, 63 insertions(+), 396 deletions(-) rename pkgs/pow/src/{luffa/consts.rs => consts/luffa.rs} (73%) rename pkgs/pow/src/{luffa/simd.rs => luffa.rs} (88%) delete mode 100644 pkgs/pow/src/luffa/mod.rs delete mode 100644 pkgs/pow/src/luffa/scalar.rs delete mode 100644 pkgs/pow/tests/luffa.rs diff --git a/pkgs/pow/Cargo.toml b/pkgs/pow/Cargo.toml index d79a48677..4c9a843d0 100644 --- a/pkgs/pow/Cargo.toml +++ b/pkgs/pow/Cargo.toml @@ -33,8 +33,6 @@ name = "cubehash" [[test]] name = "echo" [[test]] -name = "luffa" -[[test]] name = "shavite" [[test]] name = "simd_hash" diff --git a/pkgs/pow/src/luffa/consts.rs b/pkgs/pow/src/consts/luffa.rs similarity index 73% rename from pkgs/pow/src/luffa/consts.rs rename to pkgs/pow/src/consts/luffa.rs index db0c8f24b..e7815ada8 100644 --- a/pkgs/pow/src/luffa/consts.rs +++ b/pkgs/pow/src/consts/luffa.rs @@ -10,7 +10,8 @@ //! constants are generated from the Appendix B-1 seeds with the Section 4.4 //! Galois LFSR. -/// Initial state for the five 256-bit branches. +use wide::u32x4; + /// Block size in bytes. pub(crate) const BLOCK: usize = 32; @@ -81,39 +82,27 @@ pub(crate) const RC: [[[u32; 8]; 2]; 5] = { rc }; -#[cfg(feature = "simd")] -mod simd_consts { - use super::RC; - - use core::simd::Simd; - - type WordVec = Simd; - - /// Packed constants for branches 0 through 3, word 0. - pub(crate) const RC_FIRST4_LOW: [WordVec; 8] = { - let mut rc = [Simd::splat(0); 8]; - let mut round = 0; - while round < 8 { - rc[round] = Simd::from_array([RC[0][0][round], RC[1][0][round], RC[2][0][round], RC[3][0][round]]); - round += 1; - } - rc - }; - - /// Packed constants for branches 0 through 3, word 4. - pub(crate) const RC_FIRST4_HIGH: [WordVec; 8] = { - let mut rc = [Simd::splat(0); 8]; - let mut round = 0; - while round < 8 { - rc[round] = Simd::from_array([RC[0][1][round], RC[1][1][round], RC[2][1][round], RC[3][1][round]]); - round += 1; - } - rc - }; -} +/// Packed constants for branches 0 through 3, word 0. +pub(crate) const RC_FIRST4_LOW: [u32x4; 8] = { + let mut rc = [u32x4::splat(0); 8]; + let mut round = 0; + while round < 8 { + rc[round] = u32x4::new([RC[0][0][round], RC[1][0][round], RC[2][0][round], RC[3][0][round]]); + round += 1; + } + rc +}; -#[cfg(feature = "simd")] -pub(crate) use simd_consts::{RC_FIRST4_HIGH, RC_FIRST4_LOW}; +/// Packed constants for branches 0 through 3, word 4. +pub(crate) const RC_FIRST4_HIGH: [u32x4; 8] = { + let mut rc = [u32x4::splat(0); 8]; + let mut round = 0; + while round < 8 { + rc[round] = u32x4::new([RC[0][1][round], RC[1][1][round], RC[2][1][round], RC[3][1][round]]); + round += 1; + } + rc +}; #[cfg(test)] mod tests { diff --git a/pkgs/pow/src/consts/mod.rs b/pkgs/pow/src/consts/mod.rs index d27a302ab..b601dd121 100644 --- a/pkgs/pow/src/consts/mod.rs +++ b/pkgs/pow/src/consts/mod.rs @@ -11,4 +11,5 @@ pub(crate) mod bmw; pub(crate) mod groestl; pub(crate) mod jh; pub(crate) mod keccak; +pub(crate) mod luffa; pub(crate) mod skein; diff --git a/pkgs/pow/src/cubehash/simd.rs b/pkgs/pow/src/cubehash/simd.rs index babdc5f23..2cf7d40f5 100644 --- a/pkgs/pow/src/cubehash/simd.rs +++ b/pkgs/pow/src/cubehash/simd.rs @@ -7,7 +7,6 @@ //! SIMD CubeHash-16/32-512 implementation. use super::consts::{BLOCK, IV}; -use crate::util::arx::rotl_u32x4; use crate::util::memops::{load_u32_le, store_u32_le}; use core::simd::Simd; @@ -15,6 +14,17 @@ use core::simd::Simd; /// Four neighbouring state words packed into one group. pub type U32x4 = Simd; +/// Rotate each lane of a `Simd` value left by `shift` bits. +#[inline] +fn rotl_u32x4(words: U32x4, shift: u32) -> U32x4 { + if shift == 0 { + return words; + } + let left = words << Simd::splat(shift); + let right = words >> Simd::splat(32 - shift); + left | right +} + #[inline] /// Swaps the low and high 2-word halves inside one 4-word register. fn swap_halves(words: U32x4) -> U32x4 { diff --git a/pkgs/pow/src/luffa/simd.rs b/pkgs/pow/src/luffa.rs similarity index 88% rename from pkgs/pow/src/luffa/simd.rs rename to pkgs/pow/src/luffa.rs index 75831bec8..e5afa3abb 100644 --- a/pkgs/pow/src/luffa/simd.rs +++ b/pkgs/pow/src/luffa.rs @@ -4,15 +4,15 @@ // See the accompanying file LICENSE or https://opensource.org/license/MIT // -//! SIMD Luffa-512 implementation. +//! Luffa-512 hash function. -use super::consts::{BLOCK, IV, RC, RC_FIRST4_HIGH, RC_FIRST4_LOW}; +use crate::consts::luffa::{BLOCK, IV, RC, RC_FIRST4_HIGH, RC_FIRST4_LOW}; use crate::util::arx::rotl_u32x4; use crate::util::memops::{load_u32_le, store_u32_le}; -use core::simd::Simd; +use wide::u32x4; -type WordVec = Simd; +type WordVec = u32x4; /// Applies the 4-input bitslice S-box to four selected words. #[inline(always)] @@ -226,17 +226,17 @@ fn load_block_words(buf: &[u8]) -> [u32; 8] { /// permutation, then writes the results back. /// /// Each lane holds the same word index from one branch. That lets one -/// `Simd` instruction update four branches at once while keeping the +/// `u32x4` instruction update four branches at once while keeping the /// branch-local message injection and finalization code unchanged. fn permute_first_four_branches(state: &mut [[u32; 8]; 5]) { - let mut x0 = WordVec::from_array([state[0][0], state[1][0], state[2][0], state[3][0]]); - let mut x1 = WordVec::from_array([state[0][1], state[1][1], state[2][1], state[3][1]]); - let mut x2 = WordVec::from_array([state[0][2], state[1][2], state[2][2], state[3][2]]); - let mut x3 = WordVec::from_array([state[0][3], state[1][3], state[2][3], state[3][3]]); - let mut x4 = WordVec::from_array([state[0][4], state[1][4], state[2][4], state[3][4]]); - let mut x5 = WordVec::from_array([state[0][5], state[1][5], state[2][5], state[3][5]]); - let mut x6 = WordVec::from_array([state[0][6], state[1][6], state[2][6], state[3][6]]); - let mut x7 = WordVec::from_array([state[0][7], state[1][7], state[2][7], state[3][7]]); + let mut x0 = WordVec::new([state[0][0], state[1][0], state[2][0], state[3][0]]); + let mut x1 = WordVec::new([state[0][1], state[1][1], state[2][1], state[3][1]]); + let mut x2 = WordVec::new([state[0][2], state[1][2], state[2][2], state[3][2]]); + let mut x3 = WordVec::new([state[0][3], state[1][3], state[2][3], state[3][3]]); + let mut x4 = WordVec::new([state[0][4], state[1][4], state[2][4], state[3][4]]); + let mut x5 = WordVec::new([state[0][5], state[1][5], state[2][5], state[3][5]]); + let mut x6 = WordVec::new([state[0][6], state[1][6], state[2][6], state[3][6]]); + let mut x7 = WordVec::new([state[0][7], state[1][7], state[2][7], state[3][7]]); let mut round = 0; while round < 8 { @@ -305,3 +305,14 @@ pub fn hash512(data: &[u8]) -> [u8; 64] { out } + +#[cfg(test)] +mod tests { + use super::hash512; + use crate::tests::run_nist_kat; + + #[test] + fn nist_kat() { + run_nist_kat("luffa", hash512); + } +} diff --git a/pkgs/pow/src/luffa/mod.rs b/pkgs/pow/src/luffa/mod.rs deleted file mode 100644 index 957f3b203..000000000 --- a/pkgs/pow/src/luffa/mod.rs +++ /dev/null @@ -1,20 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Luffa-512 hash function. - -#[doc(hidden)] -pub mod consts; -#[doc(hidden)] -pub mod scalar; -#[cfg(feature = "simd")] -#[doc(hidden)] -pub mod simd; - -#[cfg(not(feature = "simd"))] -pub use scalar::hash512; -#[cfg(feature = "simd")] -pub use simd::hash512; diff --git a/pkgs/pow/src/luffa/scalar.rs b/pkgs/pow/src/luffa/scalar.rs deleted file mode 100644 index 0eaa2f0e5..000000000 --- a/pkgs/pow/src/luffa/scalar.rs +++ /dev/null @@ -1,236 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Scalar Luffa-512 implementation. - -use super::consts::{BLOCK, IV, RC}; -use crate::util::memops::extract; - -/// SubCrumb: 4-input bitslice S-box at word indices. -const fn sub_crumb(w: &mut [u32; 8], i0: usize, i1: usize, i2: usize, i3: usize) { - let (mut a0, mut a1, mut a2, mut a3) = (w[i0], w[i1], w[i2], w[i3]); - let tmp = a0; - a0 |= a1; - a2 ^= a3; - a1 = !a1; - a0 ^= a3; - a3 &= tmp; - a1 ^= a3; - a3 ^= a2; - a2 &= a0; - a0 = !a0; - a2 ^= a1; - a1 |= a3; - let tmp = tmp ^ a1; - a3 ^= a2; - a2 &= a1; - a1 ^= a0; - a0 = tmp; - w[i0] = a0; - w[i1] = a1; - w[i2] = a2; - w[i3] = a3; -} - -/// MixWord: rotation-based diffusion on a word pair. -const fn mix_word(w: &mut [u32; 8], u: usize, v: usize) { - w[v] ^= w[u]; - w[u] = w[u].rotate_left(2) ^ w[v]; - w[v] = w[v].rotate_left(14) ^ w[u]; - w[u] = w[u].rotate_left(10) ^ w[v]; - w[v] = w[v].rotate_left(1); -} - -/// M2: word rotation with feedback from word 7. -const fn m2(dst: &mut [u32; 8], src: &[u32; 8]) { - let t = src[7]; - dst[0] = t; - dst[1] = src[0] ^ t; - dst[2] = src[1]; - dst[3] = src[2] ^ t; - dst[4] = src[3] ^ t; - dst[5] = src[4]; - dst[6] = src[5]; - dst[7] = src[6]; -} - -const fn m2_inplace(v: &mut [u32; 8]) { - let c = *v; - m2(v, &c); -} - -const fn xor8(dst: &mut [u32; 8], src: &[u32; 8]) { - let mut i = 0; - while i < 8 { - dst[i] ^= src[i]; - i += 1; - } -} - -/// XOR all 5 chains element-wise -> 32 bytes BE output at `base` offset. -const fn xor_chains(v: &[[u32; 8]; 5], out: &mut [u8; 64], base: usize) { - let mut i = 0; - while i < 8 { - let w = v[0][i] ^ v[1][i] ^ v[2][i] ^ v[3][i] ^ v[4][i]; - let b = w.to_be_bytes(); - out[base + i * 4] = b[0]; - out[base + i * 4 + 1] = b[1]; - out[base + i * 4 + 2] = b[2]; - out[base + i * 4 + 3] = b[3]; - i += 1; - } -} - -/// MI5: message injection across 5 chains. -pub const fn mi5(v: &mut [[u32; 8]; 5], msg: &[u32; 8]) { - // Local copies to avoid nested borrow issues. - let [mut v0, mut v1, mut v2, mut v3, mut v4] = *v; - - let mut a = [0u32; 8]; - let mut i = 0; - while i < 8 { - a[i] = v0[i] ^ v1[i] ^ v2[i] ^ v3[i] ^ v4[i]; - i += 1; - } - m2_inplace(&mut a); - xor8(&mut v0, &a); - xor8(&mut v1, &a); - xor8(&mut v2, &a); - xor8(&mut v3, &a); - xor8(&mut v4, &a); - - // Forward cascade. - let mut b = [0u32; 8]; - m2(&mut b, &v0); - xor8(&mut b, &v1); - m2_inplace(&mut v1); - xor8(&mut v1, &v2); - m2_inplace(&mut v2); - xor8(&mut v2, &v3); - m2_inplace(&mut v3); - xor8(&mut v3, &v4); - m2_inplace(&mut v4); - xor8(&mut v4, &v0); - - // Reverse cascade. - m2(&mut v0, &b); - xor8(&mut v0, &v4); - m2_inplace(&mut v4); - xor8(&mut v4, &v3); - m2_inplace(&mut v3); - xor8(&mut v3, &v2); - m2_inplace(&mut v2); - xor8(&mut v2, &v1); - m2_inplace(&mut v1); - xor8(&mut v1, &b); - - // Message XOR with repeated M2 doubling. - let mut m = *msg; - xor8(&mut v0, &m); - m2_inplace(&mut m); - xor8(&mut v1, &m); - m2_inplace(&mut m); - xor8(&mut v2, &m); - m2_inplace(&mut m); - xor8(&mut v3, &m); - m2_inplace(&mut m); - xor8(&mut v4, &m); - - *v = [v0, v1, v2, v3, v4]; -} - -/// TWEAK5: per-chain rotation on words 4-7. -const fn tweak5(v: &mut [[u32; 8]; 5]) { - let mut i = 1u32; - while i < 5 { - let c = i as usize; - v[c][4] = v[c][4].rotate_left(i); - v[c][5] = v[c][5].rotate_left(i); - v[c][6] = v[c][6].rotate_left(i); - v[c][7] = v[c][7].rotate_left(i); - i += 1; - } -} - -/// 8-round permutation on a single chain. -const fn permute_chain(w: &mut [u32; 8], rc0: &[u32; 8], rc4: &[u32; 8]) { - let mut r = 0; - while r < 8 { - sub_crumb(w, 0, 1, 2, 3); - sub_crumb(w, 5, 6, 7, 4); - mix_word(w, 0, 4); - mix_word(w, 1, 5); - mix_word(w, 2, 6); - mix_word(w, 3, 7); - w[0] ^= rc0[r]; - w[4] ^= rc4[r]; - r += 1; - } -} - -/// P5: 8-round permutation on each chain independently. -pub const fn p5(v: &mut [[u32; 8]; 5]) { - tweak5(v); - let mut c = 0; - while c < 5 { - permute_chain(&mut v[c], &RC[c][0], &RC[c][1]); - c += 1; - } -} - -const fn load_msg(buf: &[u8]) -> [u32; 8] { - let mut out = [0u32; 8]; - let mut i = 0; - while i < 8 { - let o = i * 4; - out[i] = u32::from_be_bytes([buf[o], buf[o + 1], buf[o + 2], buf[o + 3]]); - i += 1; - } - out -} - -pub const fn hash512(data: &[u8]) -> [u8; 64] { - let mut v: [[u32; 8]; 5] = IV; - - let mut pos = 0; - while pos + BLOCK <= data.len() { - mi5(&mut v, &load_msg(&extract::(data, pos))); - p5(&mut v); - pos += BLOCK; - } - - let remaining = data.len() - pos; - let mut pad = [0u8; BLOCK]; - let mut ci = 0; - while ci < remaining { - pad[ci] = data[pos + ci]; - ci += 1; - } - pad[remaining] = 0x80; - - let mut out = [0u8; 64]; - let mut i = 0; - while i < 3 { - mi5(&mut v, &load_msg(&pad)); - p5(&mut v); - match i { - 0 => pad = [0u8; BLOCK], - 1 => xor_chains(&v, &mut out, 0), - 2 => xor_chains(&v, &mut out, 32), - _ => {} - } - i += 1; - } - out -} - -#[cfg(test)] -mod tests { - use super::*; - - /// Proves hash512 evaluates at compile time. - const _: [u8; 64] = hash512(b""); -} diff --git a/pkgs/pow/src/util/arx.rs b/pkgs/pow/src/util/arx.rs index 9829c3228..4fcf8f5b4 100644 --- a/pkgs/pow/src/util/arx.rs +++ b/pkgs/pow/src/util/arx.rs @@ -6,6 +6,8 @@ //! Shared helpers for ARX-style permutations. +use wide::u32x4; + /// Rotate a `u32` word left by `shift` bits. #[expect(dead_code, reason = "used by cubehash consts IV generation")] #[inline] @@ -13,14 +15,11 @@ pub(crate) const fn rotl_u32(word: u32, shift: u32) -> u32 { word.rotate_left(shift) } -/// Rotate each lane of a `Simd` value left by `shift` bits. -#[cfg(feature = "simd")] +/// Rotate each lane of a `u32x4` value left by `shift` bits. #[inline] -pub(crate) fn rotl_u32x4(words: core::simd::Simd, shift: u32) -> core::simd::Simd { +pub(crate) fn rotl_u32x4(words: u32x4, shift: u32) -> u32x4 { if shift == 0 { return words; } - let left = words << core::simd::Simd::splat(shift); - let right = words >> core::simd::Simd::splat(32 - shift); - left | right + (words << shift) | (words >> (32 - shift)) } diff --git a/pkgs/pow/tests/luffa.rs b/pkgs/pow/tests/luffa.rs deleted file mode 100644 index 468143ebf..000000000 --- a/pkgs/pow/tests/luffa.rs +++ /dev/null @@ -1,85 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Luffa-512 tests. - -mod common; - -#[cfg(feature = "simd")] -use dash_pow::__private::luffa::{consts::IV, scalar, simd}; -#[cfg(feature = "simd")] -use rstest::rstest; - -#[cfg(feature = "simd")] -fn make_state(seed: u32) -> [[u32; 8]; 5] { - let mut s = IV; - if seed != 0 { - for (c, row) in s.iter_mut().enumerate() { - for (w, slot) in row.iter_mut().enumerate() { - *slot ^= seed.wrapping_mul((c * 8 + w) as u32); - } - } - } - s -} - -#[cfg(feature = "simd")] -#[rstest] -#[case::iv(0)] -#[case::mixed(0xdeadbeef)] -fn permute(#[case] seed: u32) { - let mut s = make_state(seed); - let mut t = s; - scalar::p5(&mut s); - simd::permute_state(&mut t); - assert_eq!(s, t, "luffa permute diverged"); -} - -#[cfg(feature = "simd")] -#[rstest] -#[case::zeros([0u32; 8])] -#[case::pattern([1, 2, 3, 4, 5, 6, 7, 8])] -fn inject_message(#[case] msg: [u32; 8]) { - let mut s = IV; - let mut t = IV; - scalar::mi5(&mut s, &msg); - simd::inject_message(&mut t, &msg); - assert_eq!(s, t, "luffa inject_message diverged"); -} - -#[cfg(feature = "simd")] -#[test] -fn inject_then_permute() { - let mut s = IV; - let mut t = IV; - let msg = [0xABu32, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x9A]; - scalar::mi5(&mut s, &msg); - scalar::p5(&mut s); - simd::inject_message(&mut t, &msg); - simd::permute_state(&mut t); - assert_eq!(s, t, "luffa inject+permute diverged"); -} - -mod kat { - use super::common; - - use dash_pow::__private::luffa::scalar; - #[cfg(feature = "simd")] - use dash_pow::__private::luffa::simd; - - #[test] - fn scalar_nist_kat() { - let vectors = common::load("luffa"); - common::run_nist_kat("luffa/scalar", &vectors, scalar::hash512); - } - - #[cfg(feature = "simd")] - #[test] - fn simd_nist_kat() { - let vectors = common::load("luffa"); - common::run_nist_kat("luffa/simd", &vectors, simd::hash512); - } -} From 2d419d4a3f8ec9b38ab7bde24b54a546903926f4 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Wed, 7 Oct 2026 11:38:40 +0530 Subject: [PATCH 16/27] pow%refac(cubehash)!: use `wide`, drop scalar backend, inline KAT test --- pkgs/pow/Cargo.toml | 2 - .../consts.rs => consts/cubehash.rs} | 0 pkgs/pow/src/consts/mod.rs | 1 + .../pow/src/{cubehash/simd.rs => cubehash.rs} | 43 +++--- pkgs/pow/src/cubehash/mod.rs | 20 --- pkgs/pow/src/cubehash/scalar.rs | 69 ---------- pkgs/pow/tests/cubehash.rs | 124 ------------------ 7 files changed, 23 insertions(+), 236 deletions(-) rename pkgs/pow/src/{cubehash/consts.rs => consts/cubehash.rs} (100%) rename pkgs/pow/src/{cubehash/simd.rs => cubehash.rs} (85%) delete mode 100644 pkgs/pow/src/cubehash/mod.rs delete mode 100644 pkgs/pow/src/cubehash/scalar.rs delete mode 100644 pkgs/pow/tests/cubehash.rs diff --git a/pkgs/pow/Cargo.toml b/pkgs/pow/Cargo.toml index 4c9a843d0..cb44138fc 100644 --- a/pkgs/pow/Cargo.toml +++ b/pkgs/pow/Cargo.toml @@ -28,8 +28,6 @@ divan = { workspace = true } hex-conservative = { workspace = true } rstest = { workspace = true } -[[test]] -name = "cubehash" [[test]] name = "echo" [[test]] diff --git a/pkgs/pow/src/cubehash/consts.rs b/pkgs/pow/src/consts/cubehash.rs similarity index 100% rename from pkgs/pow/src/cubehash/consts.rs rename to pkgs/pow/src/consts/cubehash.rs diff --git a/pkgs/pow/src/consts/mod.rs b/pkgs/pow/src/consts/mod.rs index b601dd121..e6cf212d4 100644 --- a/pkgs/pow/src/consts/mod.rs +++ b/pkgs/pow/src/consts/mod.rs @@ -8,6 +8,7 @@ pub(crate) mod blake; pub(crate) mod bmw; +pub(crate) mod cubehash; pub(crate) mod groestl; pub(crate) mod jh; pub(crate) mod keccak; diff --git a/pkgs/pow/src/cubehash/simd.rs b/pkgs/pow/src/cubehash.rs similarity index 85% rename from pkgs/pow/src/cubehash/simd.rs rename to pkgs/pow/src/cubehash.rs index 2cf7d40f5..7dbea98b2 100644 --- a/pkgs/pow/src/cubehash/simd.rs +++ b/pkgs/pow/src/cubehash.rs @@ -4,46 +4,36 @@ // See the accompanying file LICENSE or https://opensource.org/license/MIT // -//! SIMD CubeHash-16/32-512 implementation. +//! CubeHash-16/32-512 hash function. -use super::consts::{BLOCK, IV}; +use crate::consts::cubehash::{BLOCK, IV}; +use crate::util::arx::rotl_u32x4; use crate::util::memops::{load_u32_le, store_u32_le}; -use core::simd::Simd; +use wide::u32x4; /// Four neighbouring state words packed into one group. -pub type U32x4 = Simd; - -/// Rotate each lane of a `Simd` value left by `shift` bits. -#[inline] -fn rotl_u32x4(words: U32x4, shift: u32) -> U32x4 { - if shift == 0 { - return words; - } - let left = words << Simd::splat(shift); - let right = words >> Simd::splat(32 - shift); - left | right -} +pub type U32x4 = u32x4; #[inline] /// Swaps the low and high 2-word halves inside one 4-word register. fn swap_halves(words: U32x4) -> U32x4 { let lanes = words.to_array(); - U32x4::from_array([lanes[2], lanes[3], lanes[0], lanes[1]]) + U32x4::new([lanes[2], lanes[3], lanes[0], lanes[1]]) } #[inline] /// Swaps each adjacent 2-word pair inside one 4-word register. fn swap_pairs(words: U32x4) -> U32x4 { let lanes = words.to_array(); - U32x4::from_array([lanes[1], lanes[0], lanes[3], lanes[2]]) + U32x4::new([lanes[1], lanes[0], lanes[3], lanes[2]]) } #[inline] /// Loads one 4-word register from the flat 32-word state. pub fn load_vec(words: &[u32; 32], index: usize) -> U32x4 { let base = index * 4; - U32x4::from_array([words[base], words[base + 1], words[base + 2], words[base + 3]]) + U32x4::new([words[base], words[base + 1], words[base + 2], words[base + 3]]) } #[inline] @@ -61,13 +51,13 @@ fn store_vec(words: &mut [u32; 32], index: usize, value: U32x4) { /// Reads one 32-byte block as the two registers xored into state. fn load_block(block: &[u8]) -> (U32x4, U32x4) { ( - U32x4::from_array([ + U32x4::new([ load_u32_le(block, 0), load_u32_le(block, 1), load_u32_le(block, 2), load_u32_le(block, 3), ]), - U32x4::from_array([ + U32x4::new([ load_u32_le(block, 4), load_u32_le(block, 5), load_u32_le(block, 6), @@ -168,7 +158,7 @@ pub fn hash512(data: &[u8]) -> [u8; 64] { // Finalization: xor 1 into the last state word, then run ten more // 16-round sets. - state[7] ^= U32x4::from_array([0, 0, 0, 1]); + state[7] ^= U32x4::new([0, 0, 0, 1]); let mut i = 0; while i < 10 { sixteen_rounds(&mut state); @@ -189,3 +179,14 @@ pub fn hash512(data: &[u8]) -> [u8; 64] { } out } + +#[cfg(test)] +mod tests { + use super::hash512; + use crate::tests::run_nist_kat; + + #[test] + fn nist_kat() { + run_nist_kat("cubehash", hash512); + } +} diff --git a/pkgs/pow/src/cubehash/mod.rs b/pkgs/pow/src/cubehash/mod.rs deleted file mode 100644 index 7dc89d56a..000000000 --- a/pkgs/pow/src/cubehash/mod.rs +++ /dev/null @@ -1,20 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! CubeHash-16/32-512 hash function. - -#[doc(hidden)] -pub mod consts; -#[doc(hidden)] -pub mod scalar; -#[cfg(feature = "simd")] -#[doc(hidden)] -pub mod simd; - -#[cfg(not(feature = "simd"))] -pub use scalar::hash512; -#[cfg(feature = "simd")] -pub use simd::hash512; diff --git a/pkgs/pow/src/cubehash/scalar.rs b/pkgs/pow/src/cubehash/scalar.rs deleted file mode 100644 index 91d13a5dc..000000000 --- a/pkgs/pow/src/cubehash/scalar.rs +++ /dev/null @@ -1,69 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Scalar CubeHash-16/32-512 implementation. - -use super::consts::{round_pair, BLOCK, IV}; -use crate::util::memops::{extract, load_u32_le, store_u32_le}; - -/// Applies 16 rounds (8 round-pairs) of the CubeHash permutation. -#[inline] -pub const fn sixteen_rounds(s: &mut [u32; 32]) { - let mut i = 0; - while i < 8 { - round_pair(s); - i += 1; - } -} - -/// Absorbs a 32-byte block into the state. -pub const fn absorb_block(state: &mut [u32; 32], block: &[u8]) { - let mut i = 0; - while i < BLOCK / 4 { - state[i] ^= load_u32_le(block, i); - i += 1; - } - sixteen_rounds(state); -} - -pub const fn hash512(data: &[u8]) -> [u8; 64] { - let mut state = IV; - - // Absorb full blocks - let mut pos = 0; - while pos + BLOCK <= data.len() { - absorb_block(&mut state, &extract::(data, pos)); - pos += BLOCK; - } - - // Pad the final block: append 0x80, fill with zeros - let mut last = [0u8; BLOCK]; - let remaining = data.len() - pos; - let mut j = 0; - while j < remaining { - last[j] = data[pos + j]; - j += 1; - } - last[remaining] = 0x80; - absorb_block(&mut state, &last); - - // Finalization: XOR 1 into state[31], then 10 x sixteen_rounds - state[31] ^= 1; - let mut i = 0; - while i < 10 { - sixteen_rounds(&mut state); - i += 1; - } - - // Extract 64 bytes (16 x u32) from state[0..16] - let mut out = [0u8; 64]; - i = 0; - while i < 16 { - store_u32_le(&mut out, i, state[i]); - i += 1; - } - out -} diff --git a/pkgs/pow/tests/cubehash.rs b/pkgs/pow/tests/cubehash.rs deleted file mode 100644 index cd35747b9..000000000 --- a/pkgs/pow/tests/cubehash.rs +++ /dev/null @@ -1,124 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! CubeHash-16/32-512 tests. - -#![cfg_attr(feature = "simd", feature(portable_simd))] - -mod common; - -#[cfg(feature = "simd")] -use dash_pow::__private::cubehash::{consts::IV, scalar, simd}; -#[cfg(feature = "simd")] -use rstest::rstest; - -#[cfg(feature = "simd")] -use core::simd::Simd; - -/// Converts a flat `[u32; 32]` state into the SIMD `[Simd; 8]` layout. -#[cfg(feature = "simd")] -fn flat_to_vec(flat: &[u32; 32]) -> [Simd; 8] { - core::array::from_fn(|i| Simd::from_array([flat[i * 4], flat[i * 4 + 1], flat[i * 4 + 2], flat[i * 4 + 3]])) -} - -/// Converts the SIMD `[Simd; 8]` layout back to a flat `[u32; 32]`. -#[cfg(feature = "simd")] -fn vec_to_flat(v: &[Simd; 8]) -> [u32; 32] { - let mut out = [0u32; 32]; - for (i, lane) in v.iter().enumerate() { - let a = lane.to_array(); - out[i * 4..i * 4 + 4].copy_from_slice(&a); - } - out -} - -#[cfg(feature = "simd")] -#[test] -fn state_round_trip() { - let orig = IV; - assert_eq!(vec_to_flat(&flat_to_vec(&orig)), orig); -} - -#[cfg(feature = "simd")] -#[rstest] -#[case::iv(IV)] -#[case::mixed({ - let mut s = IV; - for (i, slot) in s.iter_mut().enumerate() { *slot ^= 0xdeadbeef_u32.wrapping_mul(i as u32); } - s -})] -fn sixteen_rounds(#[case] init: [u32; 32]) { - let mut s = init; - let mut t = flat_to_vec(&init); - scalar::sixteen_rounds(&mut s); - simd::sixteen_rounds(&mut t); - assert_eq!(s, vec_to_flat(&t), "cubehash sixteen_rounds diverged"); -} - -#[cfg(feature = "simd")] -#[test] -fn absorb_block_agree() { - let block = [0xABu8; 32]; - let mut s_scalar = IV; - let mut s_simd = flat_to_vec(&IV); - scalar::absorb_block(&mut s_scalar, &block); - - let lo = simd::load_vec(&IV, 0); - let hi = simd::load_vec(&IV, 1); - let lo = lo - ^ Simd::from_array([ - u32::from_le_bytes([block[0], block[1], block[2], block[3]]), - u32::from_le_bytes([block[4], block[5], block[6], block[7]]), - u32::from_le_bytes([block[8], block[9], block[10], block[11]]), - u32::from_le_bytes([block[12], block[13], block[14], block[15]]), - ]); - let hi = hi - ^ Simd::from_array([ - u32::from_le_bytes([block[16], block[17], block[18], block[19]]), - u32::from_le_bytes([block[20], block[21], block[22], block[23]]), - u32::from_le_bytes([block[24], block[25], block[26], block[27]]), - u32::from_le_bytes([block[28], block[29], block[30], block[31]]), - ]); - s_simd[0] = lo; - s_simd[1] = hi; - simd::sixteen_rounds(&mut s_simd); - - assert_eq!(s_scalar, vec_to_flat(&s_simd), "cubehash absorb_block diverged"); -} - -mod kat { - use crate::common; - - use dash_pow::__private::cubehash::scalar; - #[cfg(feature = "simd")] - use dash_pow::__private::cubehash::simd; - - #[test] - fn nist_vectors_scalar() { - let vectors = common::load("cubehash"); - common::run_nist_kat("cubehash/scalar", &vectors, scalar::hash512); - } - - #[cfg(feature = "simd")] - #[test] - fn nist_vectors_simd() { - let vectors = common::load("cubehash"); - common::run_nist_kat("cubehash/simd", &vectors, simd::hash512); - } - - #[cfg(feature = "simd")] - #[test] - fn scalar_simd_agree_on_empty() { - assert_eq!(scalar::hash512(b""), simd::hash512(b"")); - } - - #[cfg(feature = "simd")] - #[test] - fn scalar_simd_agree_on_short() { - let msg = b"dash"; - assert_eq!(scalar::hash512(msg), simd::hash512(msg)); - } -} From 4132272ced93db7d4c57de7db114ba482f02ff16 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 13 Oct 2026 03:20:59 +0530 Subject: [PATCH 17/27] pow%refac(shavite)!: drop scalar backend, inline KAT test --- pkgs/pow/Cargo.toml | 2 - pkgs/pow/src/consts/mod.rs | 1 + .../{shavite/consts.rs => consts/shavite.rs} | 0 pkgs/pow/src/lib.rs | 4 - pkgs/pow/src/{shavite/simd.rs => shavite.rs} | 15 +- pkgs/pow/src/shavite/mod.rs | 20 -- pkgs/pow/src/shavite/scalar.rs | 224 ------------------ pkgs/pow/tests/shavite.rs | 56 ----- 8 files changed, 14 insertions(+), 308 deletions(-) rename pkgs/pow/src/{shavite/consts.rs => consts/shavite.rs} (100%) rename pkgs/pow/src/{shavite/simd.rs => shavite.rs} (97%) delete mode 100644 pkgs/pow/src/shavite/mod.rs delete mode 100644 pkgs/pow/src/shavite/scalar.rs delete mode 100644 pkgs/pow/tests/shavite.rs diff --git a/pkgs/pow/Cargo.toml b/pkgs/pow/Cargo.toml index cb44138fc..da016ea18 100644 --- a/pkgs/pow/Cargo.toml +++ b/pkgs/pow/Cargo.toml @@ -31,8 +31,6 @@ rstest = { workspace = true } [[test]] name = "echo" [[test]] -name = "shavite" -[[test]] name = "simd_hash" [[bench]] name = "pow" diff --git a/pkgs/pow/src/consts/mod.rs b/pkgs/pow/src/consts/mod.rs index e6cf212d4..739443704 100644 --- a/pkgs/pow/src/consts/mod.rs +++ b/pkgs/pow/src/consts/mod.rs @@ -13,4 +13,5 @@ pub(crate) mod groestl; pub(crate) mod jh; pub(crate) mod keccak; pub(crate) mod luffa; +pub(crate) mod shavite; pub(crate) mod skein; diff --git a/pkgs/pow/src/shavite/consts.rs b/pkgs/pow/src/consts/shavite.rs similarity index 100% rename from pkgs/pow/src/shavite/consts.rs rename to pkgs/pow/src/consts/shavite.rs diff --git a/pkgs/pow/src/lib.rs b/pkgs/pow/src/lib.rs index 5da51fc9d..b9c181407 100644 --- a/pkgs/pow/src/lib.rs +++ b/pkgs/pow/src/lib.rs @@ -55,10 +55,6 @@ compile_error!("dash-pow requires NEON and AES, build with `-C target-feature=+n ))] compile_error!("dash-pow requires 128-bit SIMD, build with `-C target-feature=+simd128` or enable `portable`"); -#[cfg_attr( - not(any(test, feature = "simd")), - expect(dead_code, reason = "only the SIMD backends use the block interface") -)] mod aes; mod blake; mod bmw; diff --git a/pkgs/pow/src/shavite/simd.rs b/pkgs/pow/src/shavite.rs similarity index 97% rename from pkgs/pow/src/shavite/simd.rs rename to pkgs/pow/src/shavite.rs index 436bff369..7e2d78382 100644 --- a/pkgs/pow/src/shavite/simd.rs +++ b/pkgs/pow/src/shavite.rs @@ -4,10 +4,10 @@ // See the accompanying file LICENSE or https://opensource.org/license/MIT // -//! SIMD SHAvite-3-512 implementation. +//! SHAvite-3-512 hash function. -use super::consts::{BLOCK, IV}; use crate::aes::{block_from_words, round_nk, words_from_block, Block}; +use crate::consts::shavite::{BLOCK, IV}; use crate::util::memops::{load_u32_le, store_u32_le}; use wide::u32x4; @@ -313,3 +313,14 @@ pub fn hash512(data: &[u8]) -> [u8; 64] { } out } + +#[cfg(test)] +mod tests { + use super::hash512; + use crate::tests::run_nist_kat; + + #[test] + fn nist_kat() { + run_nist_kat("shavite", hash512); + } +} diff --git a/pkgs/pow/src/shavite/mod.rs b/pkgs/pow/src/shavite/mod.rs deleted file mode 100644 index 6f501b2a4..000000000 --- a/pkgs/pow/src/shavite/mod.rs +++ /dev/null @@ -1,20 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! SHAvite-3-512 hash function. - -#[doc(hidden)] -pub mod consts; -#[doc(hidden)] -pub mod scalar; -#[cfg(feature = "simd")] -#[doc(hidden)] -pub mod simd; - -#[cfg(not(feature = "simd"))] -pub use scalar::hash512; -#[cfg(feature = "simd")] -pub use simd::hash512; diff --git a/pkgs/pow/src/shavite/scalar.rs b/pkgs/pow/src/shavite/scalar.rs deleted file mode 100644 index 40a5345eb..000000000 --- a/pkgs/pow/src/shavite/scalar.rs +++ /dev/null @@ -1,224 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Scalar SHAvite-3-512 implementation. - -use super::consts::{BLOCK, IV}; -use crate::aes::scalar::round_nk; -use crate::util::memops::{extract, load_u32_le, store_u32_le}; - -/// Expands 128-byte message block into 448 round keys. -const fn key_schedule(msg: &[u8], cnt: &[u32; 4]) -> [u32; 448] { - let mut rk = [0u32; 448]; - let mut i = 0; - while i < 32 { - rk[i] = load_u32_le(msg, i); - i += 1; - } - - // AES expansion step: AES_ROUND_NOKEY then XOR with - // rk[u-4..u-1]. Counter injected at specific offsets. - macro_rules! aes_step { - ($u:expr) => {{ - let x = [rk[$u - 31], rk[$u - 30], rk[$u - 29], rk[$u - 32]]; - let y = round_nk(&x); - rk[$u] = y[0] ^ rk[$u - 4]; - rk[$u + 1] = y[1] ^ rk[$u - 3]; - rk[$u + 2] = y[2] ^ rk[$u - 2]; - rk[$u + 3] = y[3] ^ rk[$u - 1]; - }}; - } - - // Alternating AES and linear blocks (Section 3.4). - let mut u = 32; - loop { - // AES block: 4 pairs of AES steps (32 words). - let mut s = 0; - while s < 4 { - aes_step!(u); - if u == 32 { - rk[32] ^= cnt[0]; - rk[33] ^= cnt[1]; - rk[34] ^= cnt[2]; - rk[35] ^= !cnt[3]; - } else if u == 440 { - rk[440] ^= cnt[1]; - rk[441] ^= cnt[0]; - rk[442] ^= cnt[3]; - rk[443] ^= !cnt[2]; - } - u += 4; - - aes_step!(u); - if u == 164 { - rk[164] ^= cnt[3]; - rk[165] ^= cnt[2]; - rk[166] ^= cnt[1]; - rk[167] ^= !cnt[0]; - } else if u == 316 { - rk[316] ^= cnt[2]; - rk[317] ^= cnt[3]; - rk[318] ^= cnt[0]; - rk[319] ^= !cnt[1]; - } - u += 4; - s += 1; - } - - if u == 448 { - break; - } - - // Linear block: 8 XOR steps (32 words). - s = 0; - while s < 8 { - rk[u] = rk[u - 32] ^ rk[u - 7]; - rk[u + 1] = rk[u - 31] ^ rk[u - 6]; - rk[u + 2] = rk[u - 30] ^ rk[u - 5]; - rk[u + 3] = rk[u - 29] ^ rk[u - 4]; - u += 4; - s += 1; - } - } - rk -} - -/// 4 AES rounds on right half, XOR result into left half. -const fn c512_elt(p: &mut [u32; 16], l: [usize; 4], r: [usize; 4], rk: &[u32; 448], off: usize) { - let mut x = [p[r[0]], p[r[1]], p[r[2]], p[r[3]]]; - let mut k = 0; - while k < 4 { - x[0] ^= rk[off + k * 4]; - x[1] ^= rk[off + k * 4 + 1]; - x[2] ^= rk[off + k * 4 + 2]; - x[3] ^= rk[off + k * 4 + 3]; - x = round_nk(&x); - k += 1; - } - p[l[0]] ^= x[0]; - p[l[1]] ^= x[1]; - p[l[2]] ^= x[2]; - p[l[3]] ^= x[3]; -} - -/// 14-round Feistel compression. -pub const fn compress(h: &mut [u32; 16], msg: &[u8], cnt: &[u32; 4]) { - let rk = key_schedule(msg, cnt); - let mut p = *h; - - let mut off = 0; - let mut r = 0; - while r < 14 { - c512_elt(&mut p, [0, 1, 2, 3], [4, 5, 6, 7], &rk, off); - off += 16; - c512_elt(&mut p, [8, 9, 10, 11], [12, 13, 14, 15], &rk, off); - off += 16; - - // WROT: rotate each column (stride 4) right by 1. - let mut col = 0; - while col < 4 { - let tmp = p[col + 12]; - p[col + 12] = p[col + 8]; - p[col + 8] = p[col + 4]; - p[col + 4] = p[col]; - p[col] = tmp; - col += 1; - } - r += 1; - } - - let mut i = 0; - while i < 16 { - h[i] ^= p[i]; - i += 1; - } -} - -/// Increments a 128-bit counter by `bits`. -const fn inc_counter(cnt: &mut [u32; 4], bits: u32) { - cnt[0] = cnt[0].wrapping_add(bits); - if cnt[0] < bits { - cnt[1] = cnt[1].wrapping_add(1); - if cnt[1] == 0 { - cnt[2] = cnt[2].wrapping_add(1); - if cnt[2] == 0 { - cnt[3] = cnt[3].wrapping_add(1); - } - } - } -} - -pub const fn hash512(data: &[u8]) -> [u8; 64] { - let mut h = IV; - let mut cnt = [0u32; 4]; - - let mut pos = 0; - while pos + BLOCK <= data.len() { - inc_counter(&mut cnt, 1024); - compress(&mut h, &extract::(data, pos), &cnt); - pos += BLOCK; - } - - let ptr = data.len() - pos; - inc_counter(&mut cnt, (ptr as u32) * 8); - // Save counters before possible zeroing. - let saved_cnt = cnt; - - let mut buf = [0u8; BLOCK]; - if ptr == 0 { - buf[0] = 0x80; - cnt = [0; 4]; - } else { - let mut i = 0; - while i < ptr { - buf[i] = data[pos + i]; - i += 1; - } - if ptr < 110 { - buf[ptr] = 0x80; - } else { - buf[ptr] = 0x80; - compress(&mut h, &buf, &cnt); - buf = [0u8; BLOCK]; - cnt = [0; 4]; - } - } - - // Encode saved counter and output size in padding. - // Counter at byte offsets 110, 114, 118, 122 (not u32-aligned). - let b0 = saved_cnt[0].to_le_bytes(); - let b1 = saved_cnt[1].to_le_bytes(); - let b2 = saved_cnt[2].to_le_bytes(); - let b3 = saved_cnt[3].to_le_bytes(); - let mut ci = 0; - while ci < 4 { - buf[110 + ci] = b0[ci]; - buf[114 + ci] = b1[ci]; - buf[118 + ci] = b2[ci]; - buf[122 + ci] = b3[ci]; - ci += 1; - } - // Output size: 16 u32 words. - buf[126] = (16u32 << 5) as u8; - buf[127] = (16u32 >> 3) as u8; - compress(&mut h, &buf, &cnt); - - let mut out = [0u8; 64]; - let mut i = 0; - while i < 16 { - store_u32_le(&mut out, i, h[i]); - i += 1; - } - out -} - -#[cfg(test)] -mod tests { - use super::*; - - /// Proves hash512 evaluates at compile time. - const _: [u8; 64] = hash512(b""); -} diff --git a/pkgs/pow/tests/shavite.rs b/pkgs/pow/tests/shavite.rs deleted file mode 100644 index d53d984d5..000000000 --- a/pkgs/pow/tests/shavite.rs +++ /dev/null @@ -1,56 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! SHAvite-3-512 tests. - -mod common; - -#[cfg(feature = "simd")] -use dash_pow::__private::shavite::{consts::IV, scalar, simd}; -#[cfg(feature = "simd")] -use rstest::rstest; - -#[cfg(feature = "simd")] -fn make_block(fill: u8) -> [u8; 128] { - let mut b = [0u8; 128]; - for (i, slot) in b.iter_mut().enumerate() { - *slot = fill.wrapping_add(i as u8); - } - b -} - -#[cfg(feature = "simd")] -#[rstest] -#[case::zeros([1u32, 0, 0, 0], [0u8; 128])] -#[case::pattern([2, 0, 0, 0], make_block(0xAB))] -fn compress(#[case] counter: [u32; 4], #[case] block: [u8; 128]) { - let mut h_s = IV; - let mut h_t = IV; - scalar::compress(&mut h_s, &block, &counter); - simd::compress_block(&mut h_t, &block, &counter); - assert_eq!(h_s, h_t, "shavite compress diverged"); -} - -mod kat { - use super::common; - - use dash_pow::__private::shavite::scalar; - #[cfg(feature = "simd")] - use dash_pow::__private::shavite::simd; - - #[test] - fn scalar_nist_kat() { - let vectors = common::load("shavite"); - common::run_nist_kat("shavite/scalar", &vectors, scalar::hash512); - } - - #[cfg(feature = "simd")] - #[test] - fn simd_nist_kat() { - let vectors = common::load("shavite"); - common::run_nist_kat("shavite/simd", &vectors, simd::hash512); - } -} From 66235552e104ff8101ea4870d4626231bb7ce86f Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Wed, 7 Oct 2026 11:40:38 +0530 Subject: [PATCH 18/27] pow%refac(simd)!: rename `simd_hash`, use `wide`, drop scalar backend --- pkgs/pow/Cargo.toml | 2 - pkgs/pow/bench/main.rs | 2 +- pkgs/pow/src/consts/mod.rs | 1 + .../{simd_hash/consts.rs => consts/simd.rs} | 0 pkgs/pow/src/lib.rs | 8 +- pkgs/pow/src/{simd_hash => }/simd.rs | 93 +++-- pkgs/pow/src/simd_hash/mod.rs | 20 -- pkgs/pow/src/simd_hash/scalar.rs | 332 ------------------ pkgs/pow/tests/simd_hash.rs | 58 --- 9 files changed, 65 insertions(+), 451 deletions(-) rename pkgs/pow/src/{simd_hash/consts.rs => consts/simd.rs} (100%) rename pkgs/pow/src/{simd_hash => }/simd.rs (86%) delete mode 100644 pkgs/pow/src/simd_hash/mod.rs delete mode 100644 pkgs/pow/src/simd_hash/scalar.rs delete mode 100644 pkgs/pow/tests/simd_hash.rs diff --git a/pkgs/pow/Cargo.toml b/pkgs/pow/Cargo.toml index da016ea18..a9c6ab64e 100644 --- a/pkgs/pow/Cargo.toml +++ b/pkgs/pow/Cargo.toml @@ -30,8 +30,6 @@ rstest = { workspace = true } [[test]] name = "echo" -[[test]] -name = "simd_hash" [[bench]] name = "pow" path = "bench/main.rs" diff --git a/pkgs/pow/bench/main.rs b/pkgs/pow/bench/main.rs index 098168f40..fa827f43b 100644 --- a/pkgs/pow/bench/main.rs +++ b/pkgs/pow/bench/main.rs @@ -46,7 +46,7 @@ bench_algo!(jh); bench_algo!(keccak); bench_algo!(luffa); bench_algo!(shavite); -bench_algo!(simd_hash); +bench_algo!(simd); bench_algo!(skein); fn main() { diff --git a/pkgs/pow/src/consts/mod.rs b/pkgs/pow/src/consts/mod.rs index 739443704..6cf211894 100644 --- a/pkgs/pow/src/consts/mod.rs +++ b/pkgs/pow/src/consts/mod.rs @@ -14,4 +14,5 @@ pub(crate) mod jh; pub(crate) mod keccak; pub(crate) mod luffa; pub(crate) mod shavite; +pub(crate) mod simd; pub(crate) mod skein; diff --git a/pkgs/pow/src/simd_hash/consts.rs b/pkgs/pow/src/consts/simd.rs similarity index 100% rename from pkgs/pow/src/simd_hash/consts.rs rename to pkgs/pow/src/consts/simd.rs diff --git a/pkgs/pow/src/lib.rs b/pkgs/pow/src/lib.rs index b9c181407..c0207e29f 100644 --- a/pkgs/pow/src/lib.rs +++ b/pkgs/pow/src/lib.rs @@ -66,7 +66,7 @@ mod jh; mod keccak; mod luffa; mod shavite; -mod simd_hash; +mod simd; mod skein; #[cfg(test)] mod tests; @@ -101,8 +101,8 @@ pub mod __private { pub mod shavite { pub use crate::shavite::*; } - pub mod simd_hash { - pub use crate::simd_hash::*; + pub mod simd { + pub use crate::simd::*; } pub mod skein { pub use crate::skein::*; @@ -123,7 +123,7 @@ pub fn hash(data: &[u8]) -> [u8; 32] { let h = luffa::hash512(&h); let h = cubehash::hash512(&h); let h = shavite::hash512(&h); - let h = simd_hash::hash512(&h); + let h = simd::hash512(&h); let h = echo::hash512(&h); let mut out = [0u8; 32]; out.copy_from_slice(&h[..32]); diff --git a/pkgs/pow/src/simd_hash/simd.rs b/pkgs/pow/src/simd.rs similarity index 86% rename from pkgs/pow/src/simd_hash/simd.rs rename to pkgs/pow/src/simd.rs index b8db86dea..117908614 100644 --- a/pkgs/pow/src/simd_hash/simd.rs +++ b/pkgs/pow/src/simd.rs @@ -4,19 +4,18 @@ // See the accompanying file LICENSE or https://opensource.org/license/MIT // -//! SIMD implementation of SIMD-512. +//! SIMD-512 hash function. -use super::consts::{ALPHA_TAB, BLOCK, IV, PP8K, WBP, YOFF_B_F, YOFF_B_N}; +use crate::consts::simd::{ALPHA_TAB, BLOCK, IV, PP8K, WBP, YOFF_B_F, YOFF_B_N}; use crate::util::memops::{load_u32_le, store_u32_le}; -use core::simd::cmp::SimdPartialOrd; -use core::simd::{simd_swizzle, Select, Simd}; +use wide::{i32x4, i32x8, u32x8}; pub(super) type BlockWords = [u32; 32]; -type WordVec = Simd; -type IntVec = Simd; -type Int4 = Simd; -const FFT16_SHIFTS: IntVec = IntVec::from_array([0, 1, 2, 3, 4, 5, 6, 7]); +type WordVec = u32x8; +type IntVec = i32x8; +type Int4 = i32x4; +const FFT16_SHIFTS: IntVec = IntVec::new([0, 1, 2, 3, 4, 5, 6, 7]); #[inline(always)] fn reds1(x: i32) -> i32 { @@ -31,22 +30,25 @@ fn reds2(x: i32) -> i32 { #[inline(always)] fn reds1_vec(x: IntVec) -> IntVec { - (x & IntVec::splat(0xFF)) - (x >> IntVec::splat(8)) + const MASK: IntVec = IntVec::splat(0xFF); + (x & MASK) - (x >> 8u32) } #[inline(always)] fn reds2_vec(x: IntVec) -> IntVec { - (x & IntVec::splat(0xFFFF)) + (x >> IntVec::splat(16)) + const MASK: IntVec = IntVec::splat(0xFFFF); + (x & MASK) + (x >> 16u32) } #[inline(always)] fn reds2_vec4(x: Int4) -> Int4 { - (x & Int4::splat(0xFFFF)) + (x >> Int4::splat(16)) + const MASK: Int4 = Int4::splat(0xFFFF); + (x & MASK) + (x >> 16u32) } #[inline(always)] fn load_yoff_vec(yoff: &[u16; 256], index: usize) -> IntVec { - IntVec::from_array([ + IntVec::new([ yoff[index] as i32, yoff[index + 1] as i32, yoff[index + 2] as i32, @@ -60,7 +62,7 @@ fn load_yoff_vec(yoff: &[u16; 256], index: usize) -> IntVec { #[inline(always)] fn load_alpha_vec(v: usize, a_stride: usize) -> Int4 { - Int4::from_array([ + Int4::new([ ALPHA_TAB[v], ALPHA_TAB[v + a_stride], ALPHA_TAB[v + 2 * a_stride], @@ -70,7 +72,7 @@ fn load_alpha_vec(v: usize, a_stride: usize) -> Int4 { #[inline(always)] fn load_alpha_head(a_stride: usize) -> Int4 { - Int4::from_array([ + Int4::new([ ALPHA_TAB[0], ALPHA_TAB[a_stride], ALPHA_TAB[2 * a_stride], @@ -80,7 +82,7 @@ fn load_alpha_head(a_stride: usize) -> Int4 { #[inline(always)] fn load_int4(words: &[i32], offset: usize) -> Int4 { - Int4::from_array([words[offset], words[offset + 1], words[offset + 2], words[offset + 3]]) + Int4::new([words[offset], words[offset + 1], words[offset + 2], words[offset + 3]]) } #[inline(always)] @@ -94,7 +96,7 @@ fn store_int4(words: &mut [i32], offset: usize, value: Int4) { #[inline(always)] fn load_int8(words: &[i32], offset: usize) -> IntVec { - IntVec::from_array([ + IntVec::new([ words[offset], words[offset + 1], words[offset + 2], @@ -131,8 +133,8 @@ fn fft8(x: &[u8], xb: usize, xs: usize, d: &mut [i32; 8]) { let b1 = reds1((x1 << 2) + (x3 << 6)); let b2 = (x1 << 4) - (x3 << 4); let b3 = reds1((x1 << 6) + (x3 << 2)); - let a = Int4::from_array([a0, a1, a2, a3]); - let b = Int4::from_array([b0, b1, b2, b3]); + let a = Int4::new([a0, a1, a2, a3]); + let b = Int4::new([b0, b1, b2, b3]); store_int4(d, 0, a + b); store_int4(d, 4, a - b); } @@ -143,8 +145,8 @@ fn fft16(x: &[u8], xb: usize, xs: usize, q: &mut [i32], rb: usize) { let mut d2 = [0i32; 8]; fft8(x, xb, xs << 1, &mut d1); fft8(x, xb + xs, xs << 1, &mut d2); - let left = IntVec::from_array(d1); - let right = IntVec::from_array(d2) << FFT16_SHIFTS; + let left = IntVec::new(d1); + let right = IntVec::new(d2) << FFT16_SHIFTS; store_int8(q, rb, left + right); store_int8(q, rb + 8, left - right); } @@ -156,8 +158,8 @@ fn fft_loop(q: &mut [i32], rb: usize, hk: usize, a_stride: usize) { let m = load_int4(q, rb); let n = load_int4(q, rb + hk); let mut t = reds2_vec4(n * load_alpha_head(a_stride)).to_array(); - t[0] = n[0]; - let t = Int4::from_array(t); + t[0] = n.as_array()[0]; + let t = Int4::new(t); store_int4(q, rb, m + t); store_int4(q, rb + hk, m - t); @@ -204,6 +206,8 @@ fn fft256(x: &[u8], q: &mut [i32]) { /// Reduces each NTT output into the signed `[-128, 128]` range. #[inline(always)] fn reduce_fft(q: &mut [i32], yoff: &[u16; 256]) { + const MODULUS: IntVec = IntVec::splat(257); + const HALF: IntVec = IntVec::splat(128); let mut i = 0; while i < 256 { let mut tq = load_int8(q, i) + load_yoff_vec(yoff, i); @@ -212,8 +216,8 @@ fn reduce_fft(q: &mut [i32], yoff: &[u16; 256]) { tq = reds1_vec(tq); // The transform works modulo 257. Values above 128 are rewritten as the // equivalent negative representative by subtracting one modulus. - let reduced = tq - IntVec::splat(257); - let mask = tq.simd_le(IntVec::splat(128)); + let reduced = tq - MODULUS; + let mask = tq.simd_le(HALF); store_int8(q, i, mask.select(tq, reduced)); i += 8; } @@ -255,26 +259,36 @@ fn simd_maj(x: u32, y: u32, z: u32) -> u32 { #[inline(always)] fn rotate_left_vec(x: WordVec, shift: u32) -> WordVec { - (x << Simd::splat(shift)) | (x >> Simd::splat(32 - shift)) + (x << shift) | (x >> (32 - shift)) } #[inline(always)] fn xor_permute(x: WordVec, mask: usize) -> WordVec { + // Lane `i` reads lane `i ^ mask`. + const PERMS: [WordVec; 8] = { + let mut perms = [WordVec::splat(0); 8]; + let mut mask = 0; + while mask < 8 { + let mut lanes = [0u32; 8]; + let mut lane = 0; + while lane < 8 { + lanes[lane] = (lane ^ mask) as u32; + lane += 1; + } + perms[mask] = WordVec::new(lanes); + mask += 1; + } + perms + }; match mask { - 1 => simd_swizzle!(x, [1, 0, 3, 2, 5, 4, 7, 6]), - 2 => simd_swizzle!(x, [2, 3, 0, 1, 6, 7, 4, 5]), - 3 => simd_swizzle!(x, [3, 2, 1, 0, 7, 6, 5, 4]), - 4 => simd_swizzle!(x, [4, 5, 6, 7, 0, 1, 2, 3]), - 5 => simd_swizzle!(x, [5, 4, 7, 6, 1, 0, 3, 2]), - 6 => simd_swizzle!(x, [6, 7, 4, 5, 2, 3, 0, 1]), - 7 => simd_swizzle!(x, [7, 6, 5, 4, 3, 2, 1, 0]), + 1..=7 => x.shuffle(PERMS[mask]), _ => x, } } #[inline(always)] fn load_word_vec(state: &[u32; 32], offset: usize) -> WordVec { - WordVec::from_array([ + WordVec::new([ state[offset], state[offset + 1], state[offset + 2], @@ -288,7 +302,7 @@ fn load_word_vec(state: &[u32; 32], offset: usize) -> WordVec { #[inline(always)] fn load_schedule_vec(words: &[u32], offset: usize) -> WordVec { - WordVec::from_array([ + WordVec::new([ words[offset], words[offset + 1], words[offset + 2], @@ -445,3 +459,14 @@ pub fn hash512(data: &[u8]) -> [u8; 64] { } out } + +#[cfg(test)] +mod tests { + use super::hash512; + use crate::tests::run_nist_kat; + + #[test] + fn nist_kat() { + run_nist_kat("simd", hash512); + } +} diff --git a/pkgs/pow/src/simd_hash/mod.rs b/pkgs/pow/src/simd_hash/mod.rs deleted file mode 100644 index 1701f3b55..000000000 --- a/pkgs/pow/src/simd_hash/mod.rs +++ /dev/null @@ -1,20 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! SIMD-512 hash function. - -#[doc(hidden)] -pub mod consts; -#[doc(hidden)] -pub mod scalar; -#[cfg(feature = "simd")] -#[doc(hidden)] -pub mod simd; - -#[cfg(not(feature = "simd"))] -pub use scalar::hash512; -#[cfg(feature = "simd")] -pub use simd::hash512; diff --git a/pkgs/pow/src/simd_hash/scalar.rs b/pkgs/pow/src/simd_hash/scalar.rs deleted file mode 100644 index ec6202252..000000000 --- a/pkgs/pow/src/simd_hash/scalar.rs +++ /dev/null @@ -1,332 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Scalar SIMD-512 implementation. - -use super::consts::{ALPHA_TAB, BLOCK, IV, PP8K, YOFF_B_F, YOFF_B_N}; -use crate::util::memops::{extract, load_u32_le, store_u32_le}; - -// Modular reductions for Z/257Z arithmetic. -const fn reds1(x: i32) -> i32 { - (x & 0xFF) - (x >> 8) -} -const fn reds2(x: i32) -> i32 { - (x & 0xFFFF) + (x >> 16) -} - -/// 8-point FFT butterfly. -const fn fft8(x: &[u8], xb: usize, xs: usize, d: &mut [i32; 8]) { - let x0 = x[xb] as i32; - let x1 = x[xb + xs] as i32; - let x2 = x[xb + 2 * xs] as i32; - let x3 = x[xb + 3 * xs] as i32; - let a0 = x0 + x2; - let a1 = x0 + (x2 << 4); - let a2 = x0 - x2; - let a3 = x0 - (x2 << 4); - let b0 = x1 + x3; - let b1 = reds1((x1 << 2) + (x3 << 6)); - let b2 = (x1 << 4) - (x3 << 4); - let b3 = reds1((x1 << 6) + (x3 << 2)); - d[0] = a0 + b0; - d[1] = a1 + b1; - d[2] = a2 + b2; - d[3] = a3 + b3; - d[4] = a0 - b0; - d[5] = a1 - b1; - d[6] = a2 - b2; - d[7] = a3 - b3; -} - -/// 16-point FFT: two FFT8 combined with alpha=2. -const fn fft16(x: &[u8], xb: usize, xs: usize, q: &mut [i32], rb: usize) { - let mut d1 = [0i32; 8]; - let mut d2 = [0i32; 8]; - fft8(x, xb, xs << 1, &mut d1); - fft8(x, xb + xs, xs << 1, &mut d2); - let mut i = 0; - while i < 8 { - q[rb + i] = d1[i] + (d2[i] << i); - q[rb + i + 8] = d1[i] - (d2[i] << i); - i += 1; - } -} - -/// FFT_LOOP: butterfly with alpha_tab twiddle factors. -const fn fft_loop(q: &mut [i32], rb: usize, hk: usize, a_stride: usize) { - let m = q[rb]; - let n = q[rb + hk]; - q[rb] = m + n; - q[rb + hk] = m - n; - - let mut u; - let mut v = a_stride; - // The C code uses a computed goto to start at element 1. - // We handle element 0 above, then loop from element 1. - { - let m = q[rb + 1]; - let n = q[rb + 1 + hk]; - let t = reds2(n * ALPHA_TAB[v]); - q[rb + 1] = m + t; - q[rb + 1 + hk] = m - t; - let m = q[rb + 2]; - let n = q[rb + 2 + hk]; - let t = reds2(n * ALPHA_TAB[v + a_stride]); - q[rb + 2] = m + t; - q[rb + 2 + hk] = m - t; - let m = q[rb + 3]; - let n = q[rb + 3 + hk]; - let t = reds2(n * ALPHA_TAB[v + 2 * a_stride]); - q[rb + 3] = m + t; - q[rb + 3 + hk] = m - t; - } - u = 4; - v = 4 * a_stride; - while u < hk { - let mut j = 0; - while j < 4 { - let m = q[rb + u + j]; - let n = q[rb + u + j + hk]; - let t = reds2(n * ALPHA_TAB[v + j * a_stride]); - q[rb + u + j] = m + t; - q[rb + u + j + hk] = m - t; - j += 1; - } - u += 4; - v += 4 * a_stride; - } -} - -/// 32-point FFT. -const fn fft32(x: &[u8], xb: usize, xs: usize, q: &mut [i32], rb: usize) { - let xd = xs << 1; - fft16(x, xb, xd, q, rb); - fft16(x, xb + xs, xd, q, rb + 16); - fft_loop(q, rb, 16, 8); -} - -/// 64-point FFT (function call for cache friendliness). -const fn fft64(x: &[u8], xb: usize, xs: usize, q: &mut [i32], rb: usize) { - let xd = xs << 1; - fft32(x, xb, xd, q, rb); - fft32(x, xb + xs, xd, q, rb + 32); - fft_loop(q, rb, 32, 4); -} - -/// 256-point FFT for SIMD-512 message expansion. -const fn fft256(x: &[u8], q: &mut [i32]) { - fft64(x, 0, 4, q, 0); - fft64(x, 2, 4, q, 64); - fft_loop(q, 0, 64, 2); - fft64(x, 1, 4, q, 128); - fft64(x, 3, 4, q, 192); - fft_loop(q, 128, 64, 2); - fft_loop(q, 0, 128, 1); -} - -/// Reduce FFT output to [-128, 128] range with offset table. -const fn reduce_fft(q: &mut [i32], yoff: &[u16; 256]) { - let mut i = 0; - while i < 256 { - let mut tq = q[i] + yoff[i] as i32; - tq = reds2(tq); - tq = reds1(tq); - tq = reds1(tq); - q[i] = if tq <= 128 { tq } else { tq - 257 }; - i += 1; - } -} - -/// Combine two FFT values into a u32 message word. -const fn inner(l: i32, h: i32, mm: i32) -> u32 { - ((l.wrapping_mul(mm) as u32) & 0xFFFF).wrapping_add((h.wrapping_mul(mm) as u32) << 16) -} - -/// Read 64 message words from FFT output for one round. -const fn wbread(q: &[i32], wbp: &[usize], sb: usize, o1: i32, o2: i32, mm: i32, w: &mut [u32; 64]) { - let mut u = 0; - while u < 64 { - let v = wbp[(u >> 3) + sb]; - let mut j = 0; - while j < 8 { - let li = (v as i32) + 2 * (j as i32) + o1; - let hi = (v as i32) + 2 * (j as i32) + o2; - w[u + j] = inner(q[li as usize], q[hi as usize], mm); - j += 1; - } - u += 8; - } -} - -// IF and MAJ boolean functions. -const fn simd_if(x: u32, y: u32, z: u32) -> u32 { - ((y ^ z) & x) ^ z -} -const fn simd_maj(x: u32, y: u32, z: u32) -> u32 { - (x & y) | ((x | y) & z) -} - -/// STEP2_BIG: 8-parallel Feistel step. -/// -/// `pp8b` is XORed with lane index n to select the pre-rotated A value. -/// `use_if`: true selects IF, false selects MAJ. -const fn step2_big(state: &mut [u32; 32], w: &[u32], w_off: usize, use_if: bool, r: u32, s: u32, pp8b: usize) { - // Pre-rotate A values. - let mut ta = [0u32; 8]; - let mut n = 0; - while n < 8 { - ta[n] = state[n].rotate_left(r); - n += 1; - } - - // 8 parallel STEP_ELT operations. - n = 0; - while n < 8 { - let a = state[n]; // A_n - let b = state[8 + n]; // B_n - let c = state[16 + n]; // C_n - let d = state[24 + n]; // D_n - let f = if use_if { simd_if(a, b, c) } else { simd_maj(a, b, c) }; - let tt = d.wrapping_add(w[w_off + n]).wrapping_add(f); - let new_a = tt.rotate_left(s).wrapping_add(ta[pp8b ^ n]); - state[24 + n] = c; // D_n = C_n - state[16 + n] = b; // C_n = B_n - state[8 + n] = ta[n]; // B_n = tA_n - state[n] = new_a; // A_n = new value - n += 1; - } -} - -/// One full round: 8 STEP2_BIG operations (4 IF + 4 MAJ). -const fn one_round_big(state: &mut [u32; 32], w: &[u32; 64], isp: usize, p0: u32, p1: u32, p2: u32, p3: u32) { - step2_big(state, w, 0, true, p0, p1, PP8K[isp]); - step2_big(state, w, 8, true, p1, p2, PP8K[isp + 1]); - step2_big(state, w, 16, true, p2, p3, PP8K[isp + 2]); - step2_big(state, w, 24, true, p3, p0, PP8K[isp + 3]); - step2_big(state, w, 32, false, p0, p1, PP8K[isp + 4]); - step2_big(state, w, 40, false, p1, p2, PP8K[isp + 5]); - step2_big(state, w, 48, false, p2, p3, PP8K[isp + 6]); - step2_big(state, w, 56, false, p3, p0, PP8K[isp + 7]); -} - -/// SIMD-512 compression function. -pub const fn compress(h: &mut [u32; 32], buf: &[u8], last: bool) { - let mut q = [0i32; 256]; - fft256(buf, &mut q); - - if last { - reduce_fft(&mut q, &YOFF_B_F); - } else { - reduce_fft(&mut q, &YOFF_B_N); - } - - // XOR message into state. - let mut state = [0u32; 32]; - let mut i = 0; - while i < 32 { - state[i] = h[i] ^ load_u32_le(buf, i); - i += 1; - } - - // Word base permutation for WBREAD. - #[rustfmt::skip] - let wbp: [usize; 32] = [ - 4 << 4, 6 << 4, 0 << 4, 2 << 4, - 7 << 4, 5 << 4, 3 << 4, 1 << 4, - 15 << 4, 11 << 4, 12 << 4, 8 << 4, - 9 << 4, 13 << 4, 10 << 4, 14 << 4, - 17 << 4, 18 << 4, 23 << 4, 20 << 4, - 22 << 4, 21 << 4, 16 << 4, 19 << 4, - 30 << 4, 24 << 4, 25 << 4, 31 << 4, - 27 << 4, 29 << 4, 28 << 4, 26 << 4, - ]; - - // 4 Feistel rounds. - let mut w = [0u32; 64]; - wbread(&q, &wbp, 0, 0, 1, 185, &mut w); - one_round_big(&mut state, &w, 0, 3, 23, 17, 27); - wbread(&q, &wbp, 8, 0, 1, 185, &mut w); - one_round_big(&mut state, &w, 1, 28, 19, 22, 7); - wbread(&q, &wbp, 16, -256, -128, 233, &mut w); - one_round_big(&mut state, &w, 2, 29, 9, 15, 5); - wbread(&q, &wbp, 24, -383, -255, 233, &mut w); - one_round_big(&mut state, &w, 3, 4, 13, 10, 25); - - // Final mixing: uses original state as message words. - // PP8_4_=XOR 5, PP8_5_=XOR 7, PP8_6_=XOR 4, PP8_0_=XOR 1. - step2_big(&mut state, h, 0, true, 4, 13, 5); - step2_big(&mut state, h, 8, true, 13, 10, 7); - step2_big(&mut state, h, 16, true, 10, 25, 4); - step2_big(&mut state, h, 24, true, 25, 4, 1); - - *h = state; -} - -/// Encode bit count for SIMD-512 (128-byte blocks). -const fn encode_count(dst: &mut [u8], low: u32, high: u32, ptr: usize) { - let lo = low.wrapping_shl(10); - let hi = high.wrapping_shl(10).wrapping_add(low >> 22); - let lo = lo.wrapping_add((ptr as u32) << 3); - let lo_b = lo.to_le_bytes(); - dst[0] = lo_b[0]; - dst[1] = lo_b[1]; - dst[2] = lo_b[2]; - dst[3] = lo_b[3]; - let hi_b = hi.to_le_bytes(); - dst[4] = hi_b[0]; - dst[5] = hi_b[1]; - dst[6] = hi_b[2]; - dst[7] = hi_b[3]; -} - -pub const fn hash512(data: &[u8]) -> [u8; 64] { - let mut h = IV; - let mut count_low = 0u32; - let mut count_high = 0u32; - - let mut pos = 0; - while pos + BLOCK <= data.len() { - compress(&mut h, &extract::(data, pos), false); - count_low = count_low.wrapping_add(1); - if count_low == 0 { - count_high = count_high.wrapping_add(1); - } - pos += BLOCK; - } - - // Finalize. - let ptr = data.len() - pos; - if ptr > 0 { - let mut buf = [0u8; BLOCK]; - let mut ci = 0; - while ci < ptr { - buf[ci] = data[pos + ci]; - ci += 1; - } - compress(&mut h, &buf, false); - } - - let mut buf = [0u8; BLOCK]; - encode_count(&mut buf, count_low, count_high, ptr); - compress(&mut h, &buf, true); - - // Output: first 16 u32 words as LE. - let mut out = [0u8; 64]; - let mut i = 0; - while i < 16 { - store_u32_le(&mut out, i, h[i]); - i += 1; - } - out -} - -#[cfg(test)] -mod tests { - use super::*; - - /// Proves hash512 evaluates at compile time. - const _: [u8; 64] = hash512(b""); -} diff --git a/pkgs/pow/tests/simd_hash.rs b/pkgs/pow/tests/simd_hash.rs deleted file mode 100644 index 7f08edb80..000000000 --- a/pkgs/pow/tests/simd_hash.rs +++ /dev/null @@ -1,58 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! SIMD-512 tests. - -mod common; - -#[cfg(feature = "simd")] -use dash_pow::__private::simd_hash::{consts::IV, scalar, simd}; -#[cfg(feature = "simd")] -use rstest::rstest; - -#[cfg(feature = "simd")] -fn make_block(fill: u8) -> [u8; 128] { - let mut b = [0u8; 128]; - for (i, slot) in b.iter_mut().enumerate() { - *slot = fill.wrapping_add(i as u8); - } - b -} - -#[cfg(feature = "simd")] -#[rstest] -#[case::zeros(false, [0u8; 128])] -#[case::ones(false, [0xFFu8; 128])] -#[case::pattern_nonfinal(false, make_block(0x42))] -#[case::pattern_final(true, make_block(0x42))] -fn compress(#[case] last: bool, #[case] block: [u8; 128]) { - let mut h_s = IV; - let mut h_t = IV; - scalar::compress(&mut h_s, &block, last); - simd::compress(&mut h_t, &block, last); - assert_eq!(h_s, h_t, "simd_hash compress diverged (last={last})"); -} - -mod kat { - use super::common; - - use dash_pow::__private::simd_hash::scalar; - #[cfg(feature = "simd")] - use dash_pow::__private::simd_hash::simd; - - #[test] - fn scalar_nist_kat() { - let vectors = common::load("simd"); - common::run_nist_kat("simd_hash/scalar", &vectors, scalar::hash512); - } - - #[cfg(feature = "simd")] - #[test] - fn simd_nist_kat() { - let vectors = common::load("simd"); - common::run_nist_kat("simd_hash/simd", &vectors, simd::hash512); - } -} From 5f497443e092775ce6395292441c50236a90ab46 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 13 Oct 2026 03:22:05 +0530 Subject: [PATCH 19/27] pow%refac(echo)!: drop scalar backend, inline KAT test --- pkgs/pow/Cargo.toml | 2 - pkgs/pow/src/aes/mod.rs | 2 +- pkgs/pow/src/aes/scalar.rs | 5 - .../src/{echo/consts.rs => consts/echo.rs} | 0 pkgs/pow/src/consts/mod.rs | 1 + pkgs/pow/src/{echo/simd.rs => echo.rs} | 15 +- pkgs/pow/src/echo/mod.rs | 20 -- pkgs/pow/src/echo/scalar.rs | 233 ------------------ pkgs/pow/tests/common/mod.rs | 41 --- pkgs/pow/tests/echo.rs | 61 ----- 10 files changed, 15 insertions(+), 365 deletions(-) rename pkgs/pow/src/{echo/consts.rs => consts/echo.rs} (100%) rename pkgs/pow/src/{echo/simd.rs => echo.rs} (96%) delete mode 100644 pkgs/pow/src/echo/mod.rs delete mode 100644 pkgs/pow/src/echo/scalar.rs delete mode 100644 pkgs/pow/tests/common/mod.rs delete mode 100644 pkgs/pow/tests/echo.rs diff --git a/pkgs/pow/Cargo.toml b/pkgs/pow/Cargo.toml index a9c6ab64e..b474e1b3c 100644 --- a/pkgs/pow/Cargo.toml +++ b/pkgs/pow/Cargo.toml @@ -28,8 +28,6 @@ divan = { workspace = true } hex-conservative = { workspace = true } rstest = { workspace = true } -[[test]] -name = "echo" [[bench]] name = "pow" path = "bench/main.rs" diff --git a/pkgs/pow/src/aes/mod.rs b/pkgs/pow/src/aes/mod.rs index 2bb97b9bf..b28c7eb4d 100644 --- a/pkgs/pow/src/aes/mod.rs +++ b/pkgs/pow/src/aes/mod.rs @@ -107,7 +107,7 @@ mod tests { assert_eq!(round(block(STATE), block(KEY)), round_block(block(STATE), block(KEY))); assert_eq!( words_from_block(round_nk(block(STATE))).to_array(), - scalar::round_nk(&STATE) + scalar::round(&STATE, &[0; 4]) ); } diff --git a/pkgs/pow/src/aes/scalar.rs b/pkgs/pow/src/aes/scalar.rs index fd10baf89..fea1b94e0 100644 --- a/pkgs/pow/src/aes/scalar.rs +++ b/pkgs/pow/src/aes/scalar.rs @@ -50,11 +50,6 @@ pub(crate) const fn round(state: &[u32; 4], key: &[u32; 4]) -> [u32; 4] { ] } -/// AES round without AddRoundKey (key = 0). -pub(crate) const fn round_nk(state: &[u32; 4]) -> [u32; 4] { - round(state, &[0; 4]) -} - /// Runs [`round`] on a block. #[cfg_attr( all( diff --git a/pkgs/pow/src/echo/consts.rs b/pkgs/pow/src/consts/echo.rs similarity index 100% rename from pkgs/pow/src/echo/consts.rs rename to pkgs/pow/src/consts/echo.rs diff --git a/pkgs/pow/src/consts/mod.rs b/pkgs/pow/src/consts/mod.rs index 6cf211894..519bd19e5 100644 --- a/pkgs/pow/src/consts/mod.rs +++ b/pkgs/pow/src/consts/mod.rs @@ -9,6 +9,7 @@ pub(crate) mod blake; pub(crate) mod bmw; pub(crate) mod cubehash; +pub(crate) mod echo; pub(crate) mod groestl; pub(crate) mod jh; pub(crate) mod keccak; diff --git a/pkgs/pow/src/echo/simd.rs b/pkgs/pow/src/echo.rs similarity index 96% rename from pkgs/pow/src/echo/simd.rs rename to pkgs/pow/src/echo.rs index f8f35e207..487474019 100644 --- a/pkgs/pow/src/echo/simd.rs +++ b/pkgs/pow/src/echo.rs @@ -4,10 +4,10 @@ // See the accompanying file LICENSE or https://opensource.org/license/MIT // -//! SIMD Echo-512 implementation. +//! Echo-512 hash function. -use super::consts::BLOCK; use crate::aes::{block_from_words, round, round_nk, words_from_block, Block}; +use crate::consts::echo::BLOCK; use crate::util::memops::{load_u32_le, store_u32_le}; use wide::{i8x16, u32x4}; @@ -216,3 +216,14 @@ pub fn hash512(data: &[u8]) -> [u8; 64] { } out } + +#[cfg(test)] +mod tests { + use super::hash512; + use crate::tests::run_nist_kat; + + #[test] + fn nist_kat() { + run_nist_kat("echo", hash512); + } +} diff --git a/pkgs/pow/src/echo/mod.rs b/pkgs/pow/src/echo/mod.rs deleted file mode 100644 index 6b2b4b85d..000000000 --- a/pkgs/pow/src/echo/mod.rs +++ /dev/null @@ -1,20 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Echo-512 hash function. - -#[doc(hidden)] -pub mod consts; -#[doc(hidden)] -pub mod scalar; -#[cfg(feature = "simd")] -#[doc(hidden)] -pub mod simd; - -#[cfg(not(feature = "simd"))] -pub use scalar::hash512; -#[cfg(feature = "simd")] -pub use simd::hash512; diff --git a/pkgs/pow/src/echo/scalar.rs b/pkgs/pow/src/echo/scalar.rs deleted file mode 100644 index b6f5aa091..000000000 --- a/pkgs/pow/src/echo/scalar.rs +++ /dev/null @@ -1,233 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Scalar Echo-512 implementation. - -use super::consts::BLOCK; -use crate::aes::scalar::{round, round_nk}; -use crate::util::memops::{extract, load_u32_le, store_u32_le}; - -/// Increments a 128-bit counter by `val`. -const fn inc_counter(cnt: &mut [u32; 4], val: u32) { - cnt[0] = cnt[0].wrapping_add(val); - if cnt[0] < val { - cnt[1] = cnt[1].wrapping_add(1); - if cnt[1] == 0 { - cnt[2] = cnt[2].wrapping_add(1); - if cnt[2] == 0 { - cnt[3] = cnt[3].wrapping_add(1); - } - } - } -} - -/// Increments a 128-bit counter by 1. -const fn inc_counter_one(k: &mut [u32; 4]) { - k[0] = k[0].wrapping_add(1); - if k[0] == 0 { - k[1] = k[1].wrapping_add(1); - if k[1] == 0 { - k[2] = k[2].wrapping_add(1); - if k[2] == 0 { - k[3] = k[3].wrapping_add(1); - } - } - } -} - -/// BigSubWords: 2 AES rounds on each of 16 cells with counter key. -const fn big_sub_words(w: &mut [[u32; 4]; 16], k: &mut [u32; 4]) { - let mut n = 0; - while n < 16 { - w[n] = round(&w[n], k); - w[n] = round_nk(&w[n]); - inc_counter_one(k); - n += 1; - } -} - -/// BigShiftRows: row permutations on the 4x4 grid. -const fn big_shift_rows(w: &mut [[u32; 4]; 16]) { - // Row 1: cyclic left shift by 1 across indices 1,5,9,13. - let tmp = w[1]; - w[1] = w[5]; - w[5] = w[9]; - w[9] = w[13]; - w[13] = tmp; - - // Row 2: swap pairs (2,10) and (6,14). - w.swap(2, 10); - w.swap(6, 14); - - // Row 3: cyclic right shift by 1 across indices 3,7,11,15. - let tmp = w[15]; - w[15] = w[11]; - w[11] = w[7]; - w[7] = w[3]; - w[3] = tmp; -} - -/// Byte-wise xtime (multiply by 2 in GF(2^8)) on packed u32. -const fn xtime_u32(x: u32) -> u32 { - ((x & 0x80808080) >> 7).wrapping_mul(27) ^ ((x & 0x7F7F7F7F) << 1) -} - -/// BigMixColumns: GF(2^8) column mix for one column. -const fn mix_column(w: &mut [[u32; 4]; 16], ia: usize, ib: usize, ic: usize, id: usize) { - let mut n = 0; - while n < 4 { - let a = w[ia][n]; - let b = w[ib][n]; - let c = w[ic][n]; - let d = w[id][n]; - let ab = a ^ b; - let bc = b ^ c; - let cd = c ^ d; - let abx = xtime_u32(ab); - let bcx = xtime_u32(bc); - let cdx = xtime_u32(cd); - w[ia][n] = abx ^ bc ^ d; - w[ib][n] = bcx ^ a ^ cd; - w[ic][n] = cdx ^ ab ^ d; - w[id][n] = abx ^ bcx ^ cdx ^ ab ^ c; - n += 1; - } -} - -/// BigMixColumns on all 4 columns. -const fn big_mix_columns(w: &mut [[u32; 4]; 16]) { - mix_column(w, 0, 1, 2, 3); - mix_column(w, 4, 5, 6, 7); - mix_column(w, 8, 9, 10, 11); - mix_column(w, 12, 13, 14, 15); -} - -/// Echo-512 compression. -pub const fn compress(v: &mut [[u32; 4]; 8], buf: &[u8], cnt: &[u32; 4]) { - let mut w = [[0u32; 4]; 16]; - - // First 8 cells from state. - let mut i = 0; - while i < 8 { - w[i] = v[i]; - i += 1; - } - - // Next 8 cells from message buffer. - i = 0; - while i < 8 { - w[i + 8][0] = load_u32_le(buf, i * 4); - w[i + 8][1] = load_u32_le(buf, i * 4 + 1); - w[i + 8][2] = load_u32_le(buf, i * 4 + 2); - w[i + 8][3] = load_u32_le(buf, i * 4 + 3); - i += 1; - } - - // 10 big rounds. - let mut k = *cnt; - let mut r = 0; - while r < 10 { - big_sub_words(&mut w, &mut k); - big_shift_rows(&mut w); - big_mix_columns(&mut w); - r += 1; - } - - // FINAL_BIG: V[u] ^= buf_word[u] ^ W[u] ^ W[u + 8]. - i = 0; - while i < 8 { - let mut j = 0; - while j < 4 { - let buf_word = load_u32_le(buf, i * 4 + j); - v[i][j] ^= buf_word ^ w[i][j] ^ w[i + 8][j]; - j += 1; - } - i += 1; - } -} - -pub const fn hash512(data: &[u8]) -> [u8; 64] { - let mut v = [[512, 0, 0, 0]; 8]; - let mut cnt = [0u32; 4]; - - let mut pos = 0; - while pos + BLOCK <= data.len() { - inc_counter(&mut cnt, 1024); - compress(&mut v, &extract::(data, pos), &cnt); - pos += BLOCK; - } - - let ptr = data.len() - pos; - let elen = (ptr as u32) * 8; - inc_counter(&mut cnt, elen); - - // Save counter for padding trailer. - let saved_cnt = cnt; - - // If no message bits in this block, zero the counter. - if elen == 0 { - cnt = [0; 4]; - } - - let mut buf = [0u8; BLOCK]; - if ptr > 0 { - let mut i = 0; - while i < ptr { - buf[i] = data[pos + i]; - i += 1; - } - } - buf[ptr] = 0x80; - - // If padding doesn't fit (ptr+1 exceeds available space). - if ptr + 1 > BLOCK - 18 { - compress(&mut v, &buf, &cnt); - cnt = [0; 4]; - buf = [0u8; BLOCK]; - } - - // Output size (16-bit LE) at buf[110..112]. - let out_bits = 512u16; - buf[BLOCK - 18] = out_bits as u8; - buf[BLOCK - 17] = (out_bits >> 8) as u8; - - // Saved counter at buf[112..128]. - let b0 = saved_cnt[0].to_le_bytes(); - let b1 = saved_cnt[1].to_le_bytes(); - let b2 = saved_cnt[2].to_le_bytes(); - let b3 = saved_cnt[3].to_le_bytes(); - let mut ci = 0; - while ci < 4 { - buf[BLOCK - 16 + ci] = b0[ci]; - buf[BLOCK - 12 + ci] = b1[ci]; - buf[BLOCK - 8 + ci] = b2[ci]; - buf[BLOCK - 4 + ci] = b3[ci]; - ci += 1; - } - - compress(&mut v, &buf, &cnt); - - // Output: first 16 u32 words from V (4 rows x 4 words). - let mut out = [0u8; 64]; - let mut i = 0; - while i < 4 { - let mut j = 0; - while j < 4 { - store_u32_le(&mut out, i * 4 + j, v[i][j]); - j += 1; - } - i += 1; - } - out -} - -#[cfg(test)] -mod tests { - use super::*; - - /// Proves hash512 evaluates at compile time. - const _: [u8; 64] = hash512(b""); -} diff --git a/pkgs/pow/tests/common/mod.rs b/pkgs/pow/tests/common/mod.rs deleted file mode 100644 index 75564c5a4..000000000 --- a/pkgs/pow/tests/common/mod.rs +++ /dev/null @@ -1,41 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Shared NIST KAT helpers. - -#![allow(dead_code, reason = "usage dependent on build flags")] - -use dash_dev::{arr_from_hex, Corpus}; - -const NIST_MSG_BLOB: &[u8] = include_bytes!("../../corpus/nist_msg.bin"); - -/// Returns the NIST test message of `byte_len` bytes. -/// -/// The message blob contains messages of length 0..=255 bytes, laid out at -/// triangular offsets so each message is uniquely determined by its length. -pub fn nist_input(byte_len: usize) -> &'static [u8] { - assert!(byte_len <= 255, "byte_len must be <= 255"); - let off = byte_len.wrapping_mul(byte_len.wrapping_sub(1)) / 2; - &NIST_MSG_BLOB[off..off + byte_len] -} - -/// Expected digests indexed by input length, as hex. -pub type NistVectors = Vec; - -/// Loads a corpus file by name and returns the parsed vectors. -pub fn load(name: &str) -> NistVectors { - Corpus::open(env!("CARGO_MANIFEST_DIR"), name).vectors("nist") -} - -/// Runs all NIST KAT vectors for a given hash function. -pub fn run_nist_kat(name: &str, vectors: &NistVectors, hash_fn: fn(&[u8]) -> [u8; 64]) { - for (byte_len, digest) in vectors.iter().enumerate() { - let input = nist_input(byte_len); - let expected: [u8; 64] = arr_from_hex(digest); - let got = hash_fn(input); - assert_eq!(got, expected, "{name}: mismatch at byte_len={byte_len}"); - } -} diff --git a/pkgs/pow/tests/echo.rs b/pkgs/pow/tests/echo.rs deleted file mode 100644 index f468c178f..000000000 --- a/pkgs/pow/tests/echo.rs +++ /dev/null @@ -1,61 +0,0 @@ -// -// Copyright (c) 2026-present, The Dash Core developers -// SPDX-License-Identifier: MIT -// See the accompanying file LICENSE or https://opensource.org/license/MIT -// - -//! Echo-512 tests. - -mod common; - -#[cfg(feature = "simd")] -use dash_pow::__private::echo::{scalar, simd}; -#[cfg(feature = "simd")] -use rstest::rstest; - -#[cfg(feature = "simd")] -fn initial_cv() -> [[u32; 4]; 8] { - [[512, 0, 0, 0]; 8] -} - -#[cfg(feature = "simd")] -fn make_block(fill: u8) -> [u8; 128] { - let mut b = [0u8; 128]; - for (i, slot) in b.iter_mut().enumerate() { - *slot = fill.wrapping_add(i as u8); - } - b -} - -#[cfg(feature = "simd")] -#[rstest] -#[case::zeros([1024, 0, 0, 0], [0u8; 128])] -#[case::pattern([2048, 0, 0, 0], make_block(0xCD))] -fn compress(#[case] counter: [u32; 4], #[case] block: [u8; 128]) { - let mut h_s = initial_cv(); - let mut h_t = h_s; - scalar::compress(&mut h_s, &block, &counter); - simd::compress_block(&mut h_t, &block, &counter); - assert_eq!(h_s, h_t, "echo compress diverged"); -} - -mod kat { - use super::common; - - use dash_pow::__private::echo::scalar; - #[cfg(feature = "simd")] - use dash_pow::__private::echo::simd; - - #[test] - fn scalar_nist_kat() { - let vectors = common::load("echo"); - common::run_nist_kat("echo/scalar", &vectors, scalar::hash512); - } - - #[cfg(feature = "simd")] - #[test] - fn simd_nist_kat() { - let vectors = common::load("echo"); - common::run_nist_kat("echo/simd", &vectors, simd::hash512); - } -} From 46be0115d3eea56e4916f3c9de7e53e66d737a5c Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Sun, 11 Oct 2026 01:43:43 +0530 Subject: [PATCH 20/27] pow%build!: drop `simd`, every target builds the `wide` backends --- docs/samples/solver/Cargo.toml | 4 +--- pkgs/pow/Cargo.toml | 4 +--- pkgs/pow/src/lib.rs | 1 - 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/docs/samples/solver/Cargo.toml b/docs/samples/solver/Cargo.toml index c69296a50..118a2f85d 100644 --- a/docs/samples/solver/Cargo.toml +++ b/docs/samples/solver/Cargo.toml @@ -23,9 +23,7 @@ bitcoin-consensus-encoding = { workspace = true, features = ["alloc"] } bitcoin-primitives = { workspace = true, features = ["alloc"] } bitcoin-units = { workspace = true, features = ["alloc"] } dash-num = { version = "0.1.0-beta", path = "../../../pkgs/num", default-features = false } -dash-pow = { version = "0.0.0", path = "../../../pkgs/pow", default-features = false, features = [ - "simd", -] } +dash-pow = { version = "0.0.0", path = "../../../pkgs/pow", default-features = false } dash-primitives = { version = "0.0.0", path = "../../../pkgs/primitives", default-features = false } dash-script = { version = "0.0.0", path = "../../../pkgs/script", default-features = false } dash-types = { version = "0.1.0-beta", path = "../../../pkgs/types", default-features = false } diff --git a/pkgs/pow/Cargo.toml b/pkgs/pow/Cargo.toml index b474e1b3c..65654899a 100644 --- a/pkgs/pow/Cargo.toml +++ b/pkgs/pow/Cargo.toml @@ -15,9 +15,8 @@ bench = false [features] default = [] std = ["wide/std"] -full = ["std", "simd"] +full = ["std"] portable = [] -simd = [] [dependencies] wide = { workspace = true } @@ -32,7 +31,6 @@ rstest = { workspace = true } name = "pow" path = "bench/main.rs" harness = false -required-features = ["simd"] [lints] workspace = true diff --git a/pkgs/pow/src/lib.rs b/pkgs/pow/src/lib.rs index c0207e29f..d32de5edf 100644 --- a/pkgs/pow/src/lib.rs +++ b/pkgs/pow/src/lib.rs @@ -11,7 +11,6 @@ //! to 256 bits. #![no_std] -#![cfg_attr(feature = "simd", feature(portable_simd))] #![deny( unsafe_op_in_unsafe_fn, clippy::undocumented_unsafe_blocks, From 28ca03e564b0b4ee79164c84d205613171caa9f8 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 13 Oct 2026 02:15:38 +0530 Subject: [PATCH 21/27] pow%refac(util): serialise every digest through one helper per word size --- pkgs/pow/src/blake.rs | 11 ++--------- pkgs/pow/src/bmw.rs | 11 ++--------- pkgs/pow/src/cubehash.rs | 12 ++---------- pkgs/pow/src/echo.rs | 15 +++------------ pkgs/pow/src/keccak.rs | 11 ++--------- pkgs/pow/src/shavite.rs | 11 ++--------- pkgs/pow/src/simd.rs | 11 ++--------- pkgs/pow/src/skein.rs | 11 ++--------- pkgs/pow/src/util/memops.rs | 22 ++++++++++++++++++++++ 9 files changed, 39 insertions(+), 76 deletions(-) diff --git a/pkgs/pow/src/blake.rs b/pkgs/pow/src/blake.rs index f7f655539..66c9fe511 100644 --- a/pkgs/pow/src/blake.rs +++ b/pkgs/pow/src/blake.rs @@ -7,7 +7,7 @@ //! Blake-512 hash function. use crate::consts::blake::{BLOCK, CB, IV, SIGMA}; -use crate::util::memops::{load_u64_le, store_u64_le}; +use crate::util::memops::{digest_from_u64_le, load_u64_le, store_u64_le}; /// Compresses one 128-byte block into the chaining state. /// @@ -129,14 +129,7 @@ pub fn hash_to_state(data: &[u8]) -> [u64; 8] { } pub fn hash512(data: &[u8]) -> [u8; 64] { - let h = hash_to_state(data); - let mut out = [0u8; 64]; - let mut i = 0; - while i < 8 { - store_u64_le(&mut out, i, h[i].swap_bytes()); - i += 1; - } - out + digest_from_u64_le(&hash_to_state(data).map(u64::swap_bytes)) } #[cfg(test)] diff --git a/pkgs/pow/src/bmw.rs b/pkgs/pow/src/bmw.rs index 2b9453bd8..c3174dbcf 100644 --- a/pkgs/pow/src/bmw.rs +++ b/pkgs/pow/src/bmw.rs @@ -7,7 +7,7 @@ //! BMW-512 (Blue Midnight Wish) hash function. use crate::consts::bmw::*; -use crate::util::memops::{load_u64_le, store_u64_le}; +use crate::util::memops::{digest_from_u64_le, load_u64_le, store_u64_le}; const WORDS: usize = 16; const QWORDS: usize = 32; @@ -225,14 +225,7 @@ fn hash_to_words(data: &[u8]) -> [u64; 8] { } pub fn hash512(data: &[u8]) -> [u8; 64] { - let words = hash_to_words(data); - let mut out = [0u8; 64]; - let mut index = 0; - while index < 8 { - store_u64_le(&mut out, index, words[index]); - index += 1; - } - out + digest_from_u64_le(&hash_to_words(data)) } #[cfg(test)] diff --git a/pkgs/pow/src/cubehash.rs b/pkgs/pow/src/cubehash.rs index 7dbea98b2..4054201dd 100644 --- a/pkgs/pow/src/cubehash.rs +++ b/pkgs/pow/src/cubehash.rs @@ -8,7 +8,7 @@ use crate::consts::cubehash::{BLOCK, IV}; use crate::util::arx::rotl_u32x4; -use crate::util::memops::{load_u32_le, store_u32_le}; +use crate::util::memops::{digest_from_u32_le, load_u32_le}; use wide::u32x4; @@ -169,15 +169,7 @@ pub fn hash512(data: &[u8]) -> [u8; 64] { store_vec(&mut words, 1, state[1]); store_vec(&mut words, 2, state[2]); store_vec(&mut words, 3, state[3]); - - // Output the first sixteen state words. - let mut out = [0u8; 64]; - i = 0; - while i < 16 { - store_u32_le(&mut out, i, words[i]); - i += 1; - } - out + digest_from_u32_le(&words) } #[cfg(test)] diff --git a/pkgs/pow/src/echo.rs b/pkgs/pow/src/echo.rs index 487474019..9ffcb2041 100644 --- a/pkgs/pow/src/echo.rs +++ b/pkgs/pow/src/echo.rs @@ -8,7 +8,7 @@ use crate::aes::{block_from_words, round, round_nk, words_from_block, Block}; use crate::consts::echo::BLOCK; -use crate::util::memops::{load_u32_le, store_u32_le}; +use crate::util::memops::{digest_from_u32_le, load_u32_le}; use wide::{i8x16, u32x4}; @@ -204,17 +204,8 @@ fn hash_to_cells(data: &[u8]) -> ChainingValue { pub fn hash512(data: &[u8]) -> [u8; 64] { let cells = hash_to_cells(data); - let mut out = [0u8; 64]; - let mut cell = 0; - while cell < 4 { - let mut word = 0; - while word < 4 { - store_u32_le(&mut out, cell * 4 + word, cells[cell][word]); - word += 1; - } - cell += 1; - } - out + let words: [u32; 16] = core::array::from_fn(|word| cells[word / 4][word % 4]); + digest_from_u32_le(&words) } #[cfg(test)] diff --git a/pkgs/pow/src/keccak.rs b/pkgs/pow/src/keccak.rs index dad9576b4..0c0d60fd3 100644 --- a/pkgs/pow/src/keccak.rs +++ b/pkgs/pow/src/keccak.rs @@ -113,7 +113,7 @@ pub fn keccak_f1600(state: &mut [u64; 25]) { /// Keccak-512 sponge parameterised over a permutation function. pub(crate) fn sponge(data: &[u8], perm: fn(&mut [u64; 25])) -> [u8; 64] { use crate::consts::keccak::RATE; - use crate::util::memops::{load_u64_le, store_u64_le}; + use crate::util::memops::{digest_from_u64_le, load_u64_le}; let mut state = [0u64; 25]; let mut pos = 0; @@ -138,14 +138,7 @@ pub(crate) fn sponge(data: &[u8], perm: fn(&mut [u64; 25])) -> [u8; 64] { i += 1; } perm(&mut state); - - let mut out = [0u8; 64]; - let mut i = 0; - while i < 8 { - store_u64_le(&mut out, i, state[i]); - i += 1; - } - out + digest_from_u64_le(&state) } pub fn hash512(data: &[u8]) -> [u8; 64] { diff --git a/pkgs/pow/src/shavite.rs b/pkgs/pow/src/shavite.rs index 7e2d78382..c4986a6bc 100644 --- a/pkgs/pow/src/shavite.rs +++ b/pkgs/pow/src/shavite.rs @@ -8,7 +8,7 @@ use crate::aes::{block_from_words, round_nk, words_from_block, Block}; use crate::consts::shavite::{BLOCK, IV}; -use crate::util::memops::{load_u32_le, store_u32_le}; +use crate::util::memops::{digest_from_u32_le, load_u32_le}; use wide::u32x4; @@ -304,14 +304,7 @@ fn hash_to_words(data: &[u8]) -> State { } pub fn hash512(data: &[u8]) -> [u8; 64] { - let result = hash_to_words(data); - let mut out = [0u8; 64]; - let mut word = 0; - while word < 16 { - store_u32_le(&mut out, word, result[word]); - word += 1; - } - out + digest_from_u32_le(&hash_to_words(data)) } #[cfg(test)] diff --git a/pkgs/pow/src/simd.rs b/pkgs/pow/src/simd.rs index 117908614..af67be3ae 100644 --- a/pkgs/pow/src/simd.rs +++ b/pkgs/pow/src/simd.rs @@ -7,7 +7,7 @@ //! SIMD-512 hash function. use crate::consts::simd::{ALPHA_TAB, BLOCK, IV, PP8K, WBP, YOFF_B_F, YOFF_B_N}; -use crate::util::memops::{load_u32_le, store_u32_le}; +use crate::util::memops::{digest_from_u32_le, load_u32_le}; use wide::{i32x4, i32x8, u32x8}; @@ -450,14 +450,7 @@ pub(super) fn hash_to_words(data: &[u8]) -> [u32; 16] { } pub fn hash512(data: &[u8]) -> [u8; 64] { - let result = hash_to_words(data); - let mut out = [0u8; 64]; - let mut i = 0; - while i < 16 { - store_u32_le(&mut out, i, result[i]); - i += 1; - } - out + digest_from_u32_le(&hash_to_words(data)) } #[cfg(test)] diff --git a/pkgs/pow/src/skein.rs b/pkgs/pow/src/skein.rs index 057f08eea..c372f4c47 100644 --- a/pkgs/pow/src/skein.rs +++ b/pkgs/pow/src/skein.rs @@ -7,7 +7,7 @@ //! Skein-512 hash function. use crate::consts::skein::{BLOCK, IV, NW}; -use crate::util::memops::{load_u64_le, store_u64_le}; +use crate::util::memops::{digest_from_u64_le, load_u64_le}; use crate::util::threefish::encrypt; /// UBI type code for message blocks. @@ -128,14 +128,7 @@ pub fn hash512(data: &[u8]) -> [u8; 64] { let mut state = IV; hash_message_blocks(&mut state, data); output_block(&mut state); - - let mut out = [0u8; 64]; - let mut index = 0; - while index < NW { - store_u64_le(&mut out, index, state[index]); - index += 1; - } - out + digest_from_u64_le(&state) } #[cfg(test)] diff --git a/pkgs/pow/src/util/memops.rs b/pkgs/pow/src/util/memops.rs index 0576fa649..4ce22e9dd 100644 --- a/pkgs/pow/src/util/memops.rs +++ b/pkgs/pow/src/util/memops.rs @@ -86,6 +86,28 @@ pub(crate) const fn store_u64_be(buf: &mut [u8], i: usize, v: u64) { buf[off + 7] = b[7]; } +/// Writes the first sixteen words little-endian into a digest. +#[inline] +pub(crate) fn digest_from_u32_le(words: &[u32; N]) -> [u8; 64] { + const { assert!(N >= 16) }; + let mut out = [0u8; 64]; + for (bytes, word) in out.chunks_exact_mut(4).zip(words) { + bytes.copy_from_slice(&word.to_le_bytes()); + } + out +} + +/// Writes the first eight words little-endian into a digest. +#[inline] +pub(crate) fn digest_from_u64_le(words: &[u64; N]) -> [u8; 64] { + const { assert!(N >= 8) }; + let mut out = [0u8; 64]; + for (bytes, word) in out.chunks_exact_mut(8).zip(words) { + bytes.copy_from_slice(&word.to_le_bytes()); + } + out +} + /// Copies `N` bytes from `src` starting at `offset` into a fixed-size array, /// avoiding range-based slice indexing which is not yet const-stable. #[inline] From 167cb2d42a00c0011d8f2ca92733484da0b8ce45 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 13 Oct 2026 02:18:26 +0530 Subject: [PATCH 22/27] pow%refac: split every message into blocks with `as_chunks` --- pkgs/pow/src/blake.rs | 14 ++++++-------- pkgs/pow/src/bmw.rs | 14 ++++++-------- pkgs/pow/src/cubehash.rs | 16 ++++++---------- pkgs/pow/src/echo.rs | 18 ++++++------------ pkgs/pow/src/groestl.rs | 30 +++++++++--------------------- pkgs/pow/src/jh.rs | 37 +++++++++++-------------------------- pkgs/pow/src/keccak.rs | 29 ++++++++++++----------------- pkgs/pow/src/luffa.rs | 15 ++++++--------- pkgs/pow/src/shavite.rs | 29 +++++++++++------------------ pkgs/pow/src/simd.rs | 17 ++++++----------- pkgs/pow/src/skein.rs | 17 +++++++++-------- pkgs/pow/src/util/memops.rs | 8 ++++++++ 12 files changed, 96 insertions(+), 148 deletions(-) diff --git a/pkgs/pow/src/blake.rs b/pkgs/pow/src/blake.rs index 66c9fe511..4fb03be69 100644 --- a/pkgs/pow/src/blake.rs +++ b/pkgs/pow/src/blake.rs @@ -7,7 +7,7 @@ //! Blake-512 hash function. use crate::consts::blake::{BLOCK, CB, IV, SIGMA}; -use crate::util::memops::{digest_from_u64_le, load_u64_le, store_u64_le}; +use crate::util::memops::{digest_from_u64_le, load_u64_le, store_u64_le, zero_extend}; /// Compresses one 128-byte block into the chaining state. /// @@ -79,14 +79,13 @@ pub fn hash_to_state(data: &[u8]) -> [u64; 8] { let mut t0: u64 = 0; let mut t1: u64 = 0; - let mut pos = 0; - while pos + BLOCK <= data.len() { + let (blocks, tail) = data.as_chunks::(); + for block in blocks { advance_counter(&mut t0, &mut t1); - compress(&mut h, &load_message(&data[pos..(pos + BLOCK)]), t0, t1); - pos += BLOCK; + compress(&mut h, &load_message(block), t0, t1); } - let remaining = data.len() - pos; + let remaining = tail.len(); let bit_len = (remaining as u64) * 8; let tl = t0.wrapping_add(bit_len); let th = if bit_len > 0 && tl < t0 { t1.wrapping_add(1) } else { t1 }; @@ -101,8 +100,7 @@ pub fn hash_to_state(data: &[u8]) -> [u64; 8] { t0 = t0.wrapping_sub(1024 - bit_len); } - let mut pad = [0u8; 2 * BLOCK]; - pad[..remaining].copy_from_slice(&data[pos..]); + let mut pad: [u8; 2 * BLOCK] = zero_extend(tail); pad[remaining] = 0x80; if remaining <= 111 { diff --git a/pkgs/pow/src/bmw.rs b/pkgs/pow/src/bmw.rs index c3174dbcf..d1d15fc11 100644 --- a/pkgs/pow/src/bmw.rs +++ b/pkgs/pow/src/bmw.rs @@ -7,7 +7,7 @@ //! BMW-512 (Blue Midnight Wish) hash function. use crate::consts::bmw::*; -use crate::util::memops::{digest_from_u64_le, load_u64_le, store_u64_le}; +use crate::util::memops::{digest_from_u64_le, load_u64_le, store_u64_le, zero_extend}; const WORDS: usize = 16; const QWORDS: usize = 32; @@ -191,16 +191,14 @@ fn hash_to_words(data: &[u8]) -> [u64; 8] { let mut next = &mut right; let bit_count = (data.len() as u64) << 3; - let mut pos = 0; - while pos + BLOCK <= data.len() { - compress(&load_block(&data[pos..pos + BLOCK]), current, next); + let (blocks, tail) = data.as_chunks::(); + for block in blocks { + compress(&load_block(block), current, next); core::mem::swap(&mut current, &mut next); - pos += BLOCK; } - let mut pad = [0u8; 2 * BLOCK]; - let remaining = data.len() - pos; - pad[..remaining].copy_from_slice(&data[pos..]); + let remaining = tail.len(); + let mut pad: [u8; 2 * BLOCK] = zero_extend(tail); pad[remaining] = 0x80; if remaining <= 119 { diff --git a/pkgs/pow/src/cubehash.rs b/pkgs/pow/src/cubehash.rs index 4054201dd..c0091ae12 100644 --- a/pkgs/pow/src/cubehash.rs +++ b/pkgs/pow/src/cubehash.rs @@ -8,7 +8,7 @@ use crate::consts::cubehash::{BLOCK, IV}; use crate::util::arx::rotl_u32x4; -use crate::util::memops::{digest_from_u32_le, load_u32_le}; +use crate::util::memops::{digest_from_u32_le, load_u32_le, zero_extend}; use wide::u32x4; @@ -142,18 +142,14 @@ pub fn hash512(data: &[u8]) -> [u8; 64] { load_vec(&words, 7), ]; - // Absorb full blocks - let mut pos = 0; - while pos + BLOCK <= data.len() { - absorb_block(&mut state, &data[pos..(pos + BLOCK)]); - pos += BLOCK; + let (blocks, tail) = data.as_chunks::(); + for block in blocks { + absorb_block(&mut state, block); } // Pad the final block: append `0x80`, fill with zeros. - let mut last = [0u8; BLOCK]; - let remaining = data.len() - pos; - last[..remaining].copy_from_slice(&data[pos..]); - last[remaining] = 0x80; + let mut last: [u8; BLOCK] = zero_extend(tail); + last[tail.len()] = 0x80; absorb_block(&mut state, &last); // Finalization: xor 1 into the last state word, then run ten more diff --git a/pkgs/pow/src/echo.rs b/pkgs/pow/src/echo.rs index 9ffcb2041..fae828cfd 100644 --- a/pkgs/pow/src/echo.rs +++ b/pkgs/pow/src/echo.rs @@ -8,7 +8,7 @@ use crate::aes::{block_from_words, round, round_nk, words_from_block, Block}; use crate::consts::echo::BLOCK; -use crate::util::memops::{digest_from_u32_le, load_u32_le}; +use crate::util::memops::{digest_from_u32_le, load_u32_le, zero_extend}; use wide::{i8x16, u32x4}; @@ -160,16 +160,13 @@ fn hash_to_cells(data: &[u8]) -> ChainingValue { let mut chaining_value = [[512, 0, 0, 0]; 8]; let mut counter = [0u32; 4]; - let mut pos = 0; - while pos + BLOCK <= data.len() { + let (blocks, tail) = data.as_chunks::(); + for block in blocks { increment_counter(&mut counter, 1024); - let mut block = [0u8; BLOCK]; - block.copy_from_slice(&data[pos..pos + BLOCK]); - compress_block(&mut chaining_value, &block, &counter); - pos += BLOCK; + compress_block(&mut chaining_value, block, &counter); } - let used = data.len() - pos; + let used = tail.len(); let used_bits = (used as u32) * 8; increment_counter(&mut counter, used_bits); let saved_counter = counter; @@ -178,10 +175,7 @@ fn hash_to_cells(data: &[u8]) -> ChainingValue { counter = [0; 4]; } - let mut block = [0u8; BLOCK]; - if used > 0 { - block[..used].copy_from_slice(&data[pos..]); - } + let mut block: [u8; BLOCK] = zero_extend(tail); block[used] = 0x80; if used + 1 > BLOCK - 18 { diff --git a/pkgs/pow/src/groestl.rs b/pkgs/pow/src/groestl.rs index 15911162f..a91372492 100644 --- a/pkgs/pow/src/groestl.rs +++ b/pkgs/pow/src/groestl.rs @@ -8,6 +8,7 @@ use crate::aes::sub_shift as aes_sub_shift; use crate::consts::groestl::{BLOCK, IV_ROWS, RC_P, RC_Q, ROUNDS, SUBSH_P, SUBSH_Q}; +use crate::util::memops::zero_extend; use wide::{i8x16, u8x16}; @@ -245,26 +246,17 @@ pub fn output_transform(chaining_value: &mut [Row; 8]) { } pub fn hash512(data: &[u8]) -> [u8; 64] { - let mut chaining_value = [Row::splat(0); 8]; - let mut row = 0; - while row < 8 { - chaining_value[row] = Row::new(IV_ROWS[row]); - row += 1; - } + let mut chaining_value = IV_ROWS.map(Row::new); let mut block_count = 0u64; - let mut pos = 0; - while pos + BLOCK <= data.len() { - let mut block = [0u8; BLOCK]; - block.copy_from_slice(&data[pos..pos + BLOCK]); - compress_block(&mut chaining_value, &block); + let (blocks, tail) = data.as_chunks::(); + for block in blocks { + compress_block(&mut chaining_value, block); block_count = block_count.wrapping_add(1); - pos += BLOCK; } - let used = data.len() - pos; - let mut pad = [0u8; 256]; - pad[..used].copy_from_slice(&data[pos..]); + let used = tail.len(); + let mut pad: [u8; 2 * BLOCK] = zero_extend(tail); pad[used] = 0x80; let pad_len = if used < 120 { @@ -277,12 +269,8 @@ pub fn hash512(data: &[u8]) -> [u8; 64] { pad[pad_len - 8..pad_len].copy_from_slice(&block_count.to_be_bytes()); - pos = 0; - while pos < pad_len { - let mut block = [0u8; BLOCK]; - block.copy_from_slice(&pad[pos..pos + BLOCK]); - compress_block(&mut chaining_value, &block); - pos += BLOCK; + for block in pad[..pad_len].as_chunks::().0 { + compress_block(&mut chaining_value, block); } output_transform(&mut chaining_value); diff --git a/pkgs/pow/src/jh.rs b/pkgs/pow/src/jh.rs index 0064ce4b0..14ecc4111 100644 --- a/pkgs/pow/src/jh.rs +++ b/pkgs/pow/src/jh.rs @@ -7,7 +7,7 @@ //! JH-512 hash function. use crate::consts::jh::{BLOCK, IV, ROUND_CONSTS}; -use crate::util::memops::{load_u64_le, store_u64_le}; +use crate::util::memops::{load_u64_le, store_u64_le, zero_extend}; use wide::u32x4; @@ -230,36 +230,21 @@ fn load_block_words(buf: &[u8]) -> [u64; 8] { pub fn hash512(data: &[u8]) -> [u8; 64] { let mut state = IV.map(row_from_u64_pair); - let mut block_count = 0u64; - let mut buf = [0u8; BLOCK]; - let mut pos = 0usize; - - for &byte in data { - buf[pos] = byte; - pos += 1; - if pos == BLOCK { - compress(&mut state, &load_block_words(&buf)); - block_count += 1; - pos = 0; - } + let (blocks, tail) = data.as_chunks::(); + for block in blocks { + compress(&mut state, &load_block_words(block)); } - if pos == 0 { - buf = [0u8; BLOCK]; - buf[0] = 0x80; - let bit_len = block_count << 9; - buf[56..].copy_from_slice(&bit_len.to_be_bytes()); + // A partial tail takes the padding bit and leaves the length to a block of + // its own. Without a tail, padding and length share one block. + let mut buf: [u8; BLOCK] = zero_extend(tail); + buf[tail.len()] = 0x80; + if !tail.is_empty() { compress(&mut state, &load_block_words(&buf)); - } else { - let bit_len = (block_count << 9).wrapping_add((pos as u64) << 3); - buf[pos] = 0x80; - buf[pos + 1..].fill(0); - compress(&mut state, &load_block_words(&buf)); - buf = [0u8; BLOCK]; - buf[56..].copy_from_slice(&bit_len.to_be_bytes()); - compress(&mut state, &load_block_words(&buf)); } + buf[56..].copy_from_slice(&((data.len() as u64) << 3).to_be_bytes()); + compress(&mut state, &load_block_words(&buf)); let mut out = [0u8; 64]; let mut row = 0; diff --git a/pkgs/pow/src/keccak.rs b/pkgs/pow/src/keccak.rs index 0c0d60fd3..fc1eca680 100644 --- a/pkgs/pow/src/keccak.rs +++ b/pkgs/pow/src/keccak.rs @@ -113,30 +113,25 @@ pub fn keccak_f1600(state: &mut [u64; 25]) { /// Keccak-512 sponge parameterised over a permutation function. pub(crate) fn sponge(data: &[u8], perm: fn(&mut [u64; 25])) -> [u8; 64] { use crate::consts::keccak::RATE; - use crate::util::memops::{digest_from_u64_le, load_u64_le}; + use crate::util::memops::{digest_from_u64_le, zero_extend}; let mut state = [0u64; 25]; - let mut pos = 0; - while pos + RATE <= data.len() { - let mut i = 0; - while i < 9 { - state[i] ^= load_u64_le(&data[pos..], i); - i += 1; + let absorb = |state: &mut [u64; 25], block: &[u8; RATE]| { + for (lane, word) in state.iter_mut().zip(block.as_chunks::<8>().0) { + *lane ^= u64::from_le_bytes(*word); } + }; + + let (blocks, tail) = data.as_chunks::(); + for block in blocks { + absorb(&mut state, block); perm(&mut state); - pos += RATE; } - let mut last = [0u8; RATE]; - let remaining = data.len() - pos; - last[..remaining].copy_from_slice(&data[pos..]); - last[remaining] = 0x01; + let mut last: [u8; RATE] = zero_extend(tail); + last[tail.len()] = 0x01; last[RATE - 1] |= 0x80; - let mut i = 0; - while i < 9 { - state[i] ^= load_u64_le(&last, i); - i += 1; - } + absorb(&mut state, &last); perm(&mut state); digest_from_u64_le(&state) } diff --git a/pkgs/pow/src/luffa.rs b/pkgs/pow/src/luffa.rs index e5afa3abb..0ef595bf8 100644 --- a/pkgs/pow/src/luffa.rs +++ b/pkgs/pow/src/luffa.rs @@ -8,7 +8,7 @@ use crate::consts::luffa::{BLOCK, IV, RC, RC_FIRST4_HIGH, RC_FIRST4_LOW}; use crate::util::arx::rotl_u32x4; -use crate::util::memops::{load_u32_le, store_u32_le}; +use crate::util::memops::{load_u32_le, store_u32_le, zero_extend}; use wide::u32x4; @@ -276,17 +276,14 @@ fn permute_first_four_branches(state: &mut [[u32; 8]; 5]) { pub fn hash512(data: &[u8]) -> [u8; 64] { let mut state: [[u32; 8]; 5] = IV; - let mut pos = 0; - while pos + BLOCK <= data.len() { - inject_message(&mut state, &load_block_words(&data[pos..pos + BLOCK])); + let (blocks, tail) = data.as_chunks::(); + for block in blocks { + inject_message(&mut state, &load_block_words(block)); permute_state(&mut state); - pos += BLOCK; } - let remaining = data.len() - pos; - let mut block = [0u8; BLOCK]; - block[..remaining].copy_from_slice(&data[pos..]); - block[remaining] = 0x80; + let mut block: [u8; BLOCK] = zero_extend(tail); + block[tail.len()] = 0x80; let mut out = [0u8; 64]; let mut final_round = 0; diff --git a/pkgs/pow/src/shavite.rs b/pkgs/pow/src/shavite.rs index c4986a6bc..c0f8fbceb 100644 --- a/pkgs/pow/src/shavite.rs +++ b/pkgs/pow/src/shavite.rs @@ -8,7 +8,7 @@ use crate::aes::{block_from_words, round_nk, words_from_block, Block}; use crate::consts::shavite::{BLOCK, IV}; -use crate::util::memops::{digest_from_u32_le, load_u32_le}; +use crate::util::memops::{digest_from_u32_le, load_u32_le, zero_extend}; use wide::u32x4; @@ -265,31 +265,24 @@ fn hash_to_words(data: &[u8]) -> State { let mut chaining_value = IV; let mut counter = [0u32; 4]; - let mut pos = 0; - while pos + BLOCK <= data.len() { + let (blocks, tail) = data.as_chunks::(); + for block in blocks { increment_counter(&mut counter, 1024); - let mut block = [0u8; BLOCK]; - block.copy_from_slice(&data[pos..pos + BLOCK]); - compress_block(&mut chaining_value, &block, &counter); - pos += BLOCK; + compress_block(&mut chaining_value, block, &counter); } - let used = data.len() - pos; + let used = tail.len(); increment_counter(&mut counter, (used as u32) * 8); let saved_counter = counter; - let mut block = [0u8; BLOCK]; + let mut block: [u8; BLOCK] = zero_extend(tail); + block[used] = 0x80; if used == 0 { - block[0] = 0x80; counter = [0; 4]; - } else { - block[..used].copy_from_slice(&data[pos..]); - block[used] = 0x80; - if used >= 110 { - compress_block(&mut chaining_value, &block, &counter); - block = [0u8; BLOCK]; - counter = [0; 4]; - } + } else if used >= 110 { + compress_block(&mut chaining_value, &block, &counter); + block = [0u8; BLOCK]; + counter = [0; 4]; } block[110..114].copy_from_slice(&saved_counter[0].to_le_bytes()); diff --git a/pkgs/pow/src/simd.rs b/pkgs/pow/src/simd.rs index af67be3ae..ba120da33 100644 --- a/pkgs/pow/src/simd.rs +++ b/pkgs/pow/src/simd.rs @@ -7,7 +7,7 @@ //! SIMD-512 hash function. use crate::consts::simd::{ALPHA_TAB, BLOCK, IV, PP8K, WBP, YOFF_B_F, YOFF_B_N}; -use crate::util::memops::{digest_from_u32_le, load_u32_le}; +use crate::util::memops::{digest_from_u32_le, load_u32_le, zero_extend}; use wide::{i32x4, i32x8, u32x8}; @@ -421,23 +421,18 @@ pub(super) fn hash_to_words(data: &[u8]) -> [u32; 16] { let mut count_low = 0u32; let mut count_high = 0u32; - let mut pos = 0; - while pos + BLOCK <= data.len() { - let mut block = [0u8; BLOCK]; - block.copy_from_slice(&data[pos..pos + BLOCK]); - compress(&mut h, &block, false); + let (blocks, tail) = data.as_chunks::(); + for block in blocks { + compress(&mut h, block, false); count_low = count_low.wrapping_add(1); if count_low == 0 { count_high = count_high.wrapping_add(1); } - pos += BLOCK; } - let ptr = data.len() - pos; + let ptr = tail.len(); if ptr > 0 { - let mut block = [0u8; BLOCK]; - block[..ptr].copy_from_slice(&data[pos..]); - compress(&mut h, &block, false); + compress(&mut h, &zero_extend(tail), false); } let mut block = [0u8; BLOCK]; diff --git a/pkgs/pow/src/skein.rs b/pkgs/pow/src/skein.rs index c372f4c47..84ae4954a 100644 --- a/pkgs/pow/src/skein.rs +++ b/pkgs/pow/src/skein.rs @@ -7,7 +7,7 @@ //! Skein-512 hash function. use crate::consts::skein::{BLOCK, IV, NW}; -use crate::util::memops::{digest_from_u64_le, load_u64_le}; +use crate::util::memops::{digest_from_u64_le, load_u64_le, zero_extend}; use crate::util::threefish::encrypt; /// UBI type code for message blocks. @@ -79,26 +79,27 @@ pub fn ubi(state: &mut Chaining, plaintext: &BlockWords, tweak: UbiTweak) { /// Hashes the message stream as one UBI message stream. fn hash_message_blocks(state: &mut Chaining, data: &[u8]) { - let mut pos = 0usize; let mut blocks = 0u64; + // The last block carries the final flag even when it is full, so it is + // split off before the loop. An empty message still has one final block. + let (body, tail) = data.split_at(data.len().saturating_sub(1) / BLOCK * BLOCK); + // UBI counts bytes, not blocks. The tweak position is the total number // of message bytes seen after each block. - while pos + BLOCK < data.len() { + for block in body.as_chunks::().0 { let tweak = UbiTweak { position: (blocks + 1) << 6, kind: TYPE_MSG, first: blocks == 0, final_block: false, }; - ubi(state, &load_block(&data[pos..pos + BLOCK]), tweak); - pos += BLOCK; + ubi(state, &load_block(block), tweak); blocks += 1; } - let remaining = data.len() - pos; - let mut last = [0u8; BLOCK]; - last[..remaining].copy_from_slice(&data[pos..]); + let remaining = tail.len(); + let last: [u8; BLOCK] = zero_extend(tail); let tweak = UbiTweak { position: (blocks << 6).wrapping_add(remaining as u64), kind: TYPE_MSG, diff --git a/pkgs/pow/src/util/memops.rs b/pkgs/pow/src/util/memops.rs index 4ce22e9dd..6d99b173e 100644 --- a/pkgs/pow/src/util/memops.rs +++ b/pkgs/pow/src/util/memops.rs @@ -108,6 +108,14 @@ pub(crate) fn digest_from_u64_le(words: &[u64; N]) -> [u8; 64] { out } +/// Copies `bytes` into the start of a zeroed `N`-byte buffer. +#[inline] +pub(crate) fn zero_extend(bytes: &[u8]) -> [u8; N] { + let mut out = [0u8; N]; + out[..bytes.len()].copy_from_slice(bytes); + out +} + /// Copies `N` bytes from `src` starting at `offset` into a fixed-size array, /// avoiding range-based slice indexing which is not yet const-stable. #[inline] From b85053b1adf618f107cd76851cd2382623c652eb Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 13 Oct 2026 02:19:50 +0530 Subject: [PATCH 23/27] pow%refac: keep HAIFA counters in a `u128`, write sizes as integers --- pkgs/pow/src/echo.rs | 60 ++++++++++++++--------------------------- pkgs/pow/src/shavite.rs | 53 ++++++++++++------------------------ 2 files changed, 37 insertions(+), 76 deletions(-) diff --git a/pkgs/pow/src/echo.rs b/pkgs/pow/src/echo.rs index fae828cfd..c0d42e610 100644 --- a/pkgs/pow/src/echo.rs +++ b/pkgs/pow/src/echo.rs @@ -14,28 +14,13 @@ use wide::{i8x16, u32x4}; type AesState = [u32; 4]; type Grid = [Block; 16]; -type Counter = [u32; 4]; +type Counter = u128; type ChainingValue = [AesState; 8]; pub(super) type BlockWords = [u32; 32]; /// Grid permutation used by the row-shift step. const BIG_SHIFT_ROWS: [usize; 16] = [0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, 1, 6, 11]; -/// Adds `value` to the 128-bit counter used to key the big rounds. -#[inline(always)] -fn increment_counter(counter: &mut Counter, value: u32) { - counter[0] = counter[0].wrapping_add(value); - if counter[0] < value { - counter[1] = counter[1].wrapping_add(1); - if counter[1] == 0 { - counter[2] = counter[2].wrapping_add(1); - if counter[2] == 0 { - counter[3] = counter[3].wrapping_add(1); - } - } - } -} - /// Lays four words out as one grid cell. #[inline(always)] fn cell(words: AesState) -> Block { @@ -44,13 +29,13 @@ fn cell(words: AesState) -> Block { /// Builds the 16 round keys used by one substitution step. #[inline(always)] -fn build_sub_word_keys(counter: &Counter) -> (Grid, Counter) { +fn build_sub_word_keys(counter: Counter) -> (Grid, Counter) { let mut keys = [Block::splat(0); 16]; - let mut next = *counter; + let mut next = counter; let mut index = 0; while index < 16 { - keys[index] = cell(next); - increment_counter(&mut next, 1); + keys[index] = Block::new(next.to_le_bytes()); + next = next.wrapping_add(1); index += 1; } (keys, next) @@ -116,7 +101,7 @@ fn apply_big_mix_columns(grid: &mut Grid) { /// Runs the compression function on one 1024-bit block. #[inline(always)] -pub(super) fn compress_words(chaining_value: &mut ChainingValue, block: &BlockWords, counter: &Counter) { +pub(super) fn compress_words(chaining_value: &mut ChainingValue, block: &BlockWords, counter: Counter) { // The upper half of the 4x4 grid starts from the chaining value and the // lower half from the message block. let mut grid = [Block::splat(0); 16]; @@ -128,10 +113,10 @@ pub(super) fn compress_words(chaining_value: &mut ChainingValue, block: &BlockWo index += 1; } - let mut round_counter = *counter; + let mut round_counter = counter; let mut round = 0; while round < 10 { - let (round_keys, next_counter) = build_sub_word_keys(&round_counter); + let (round_keys, next_counter) = build_sub_word_keys(round_counter); apply_big_sub_words(&mut grid, &round_keys); round_counter = next_counter; apply_big_shift_rows(&mut grid); @@ -151,47 +136,42 @@ pub(super) fn compress_words(chaining_value: &mut ChainingValue, block: &BlockWo /// Runs the compression function on one 1024-bit block. #[inline(always)] -pub fn compress_block(chaining_value: &mut ChainingValue, block: &[u8; BLOCK], counter: &Counter) { +pub fn compress_block(chaining_value: &mut ChainingValue, block: &[u8; BLOCK], counter: Counter) { let words = core::array::from_fn(|word| load_u32_le(block, word)); compress_words(chaining_value, &words, counter); } fn hash_to_cells(data: &[u8]) -> ChainingValue { let mut chaining_value = [[512, 0, 0, 0]; 8]; - let mut counter = [0u32; 4]; + let mut counter = 0u128; let (blocks, tail) = data.as_chunks::(); for block in blocks { - increment_counter(&mut counter, 1024); - compress_block(&mut chaining_value, block, &counter); + counter = counter.wrapping_add(1024); + compress_block(&mut chaining_value, block, counter); } let used = tail.len(); - let used_bits = (used as u32) * 8; - increment_counter(&mut counter, used_bits); + counter = counter.wrapping_add(used as u128 * 8); let saved_counter = counter; - if used_bits == 0 { - counter = [0; 4]; + if used == 0 { + counter = 0; } let mut block: [u8; BLOCK] = zero_extend(tail); block[used] = 0x80; if used + 1 > BLOCK - 18 { - compress_block(&mut chaining_value, &block, &counter); - counter = [0; 4]; + compress_block(&mut chaining_value, &block, counter); + counter = 0; block = [0u8; BLOCK]; } - block[BLOCK - 18] = 0; - block[BLOCK - 17] = 2; - block[BLOCK - 16..BLOCK - 12].copy_from_slice(&saved_counter[0].to_le_bytes()); - block[BLOCK - 12..BLOCK - 8].copy_from_slice(&saved_counter[1].to_le_bytes()); - block[BLOCK - 8..BLOCK - 4].copy_from_slice(&saved_counter[2].to_le_bytes()); - block[BLOCK - 4..BLOCK].copy_from_slice(&saved_counter[3].to_le_bytes()); + block[BLOCK - 18..BLOCK - 16].copy_from_slice(&512u16.to_le_bytes()); + block[BLOCK - 16..].copy_from_slice(&saved_counter.to_le_bytes()); - compress_block(&mut chaining_value, &block, &counter); + compress_block(&mut chaining_value, &block, counter); chaining_value } diff --git a/pkgs/pow/src/shavite.rs b/pkgs/pow/src/shavite.rs index c0f8fbceb..b532201b2 100644 --- a/pkgs/pow/src/shavite.rs +++ b/pkgs/pow/src/shavite.rs @@ -80,16 +80,16 @@ const fn rotate_bundle_words(bundle: Bundle) -> Bundle { /// Reads one counter word according to the schedule descriptor. #[inline(always)] -fn counter_word(counter: &[u32; 4], word: CounterWord) -> u32 { +fn counter_word(counter: u128, word: CounterWord) -> u32 { match word { - CounterWord::Counter(index) => counter[index], - CounterWord::InvertedCounter(index) => !counter[index], + CounterWord::Counter(index) => (counter >> (32 * index)) as u32, + CounterWord::InvertedCounter(index) => !(counter >> (32 * index)) as u32, } } /// Expands one 128-byte block into 112 four-word schedule bundles. #[inline(always)] -fn expand_round_keys_words(msg: &BlockWords, counter: &[u32; 4]) -> [Bundle; SCHEDULE_BUNDLES] { +fn expand_round_keys_words(msg: &BlockWords, counter: u128) -> [Bundle; SCHEDULE_BUNDLES] { let mut schedule = [[0u32; 4]; SCHEDULE_BUNDLES]; let mut bundle = 0; while bundle < 8 { @@ -134,7 +134,7 @@ fn expand_round_keys_words(msg: &BlockWords, counter: &[u32; 4]) -> [Bundle; SCH } #[inline(always)] -fn expand_round_keys(msg: &[u8; BLOCK], counter: &[u32; 4]) -> [Bundle; SCHEDULE_BUNDLES] { +fn expand_round_keys(msg: &[u8; BLOCK], counter: u128) -> [Bundle; SCHEDULE_BUNDLES] { let words = core::array::from_fn(|word| load_u32_le(msg, word)); expand_round_keys_words(&words, counter) } @@ -164,7 +164,7 @@ fn expand_linear_step(schedule: &[Bundle; SCHEDULE_BUNDLES], bundle: usize) -> B /// Injects the HAIFA counter words at one schedule checkpoint. #[inline(always)] -fn inject_counter_words(bundle_words: &mut Bundle, counter: &[u32; 4], injection: CounterInjection) { +fn inject_counter_words(bundle_words: &mut Bundle, counter: u128, injection: CounterInjection) { let mut word = 0; while word < 4 { bundle_words[word] ^= counter_word(counter, injection.words[word]); @@ -210,7 +210,7 @@ fn rotate_columns(state: &mut State) { /// Runs the SHAvite-3-512 compression function. #[inline(always)] -pub fn compress_block(chaining_value: &mut State, msg: &[u8; BLOCK], counter: &[u32; 4]) { +pub fn compress_block(chaining_value: &mut State, msg: &[u8; BLOCK], counter: u128) { let schedule = expand_round_keys(msg, counter); compress_schedule(chaining_value, &schedule); } @@ -246,52 +246,33 @@ fn compress_schedule(chaining_value: &mut State, schedule: &[Bundle; SCHEDULE_BU } } -/// Adds `bits` to the 128-bit message counter. -#[inline(always)] -fn increment_counter(counter: &mut [u32; 4], bits: u32) { - counter[0] = counter[0].wrapping_add(bits); - if counter[0] < bits { - counter[1] = counter[1].wrapping_add(1); - if counter[1] == 0 { - counter[2] = counter[2].wrapping_add(1); - if counter[2] == 0 { - counter[3] = counter[3].wrapping_add(1); - } - } - } -} - fn hash_to_words(data: &[u8]) -> State { let mut chaining_value = IV; - let mut counter = [0u32; 4]; + let mut counter = 0u128; let (blocks, tail) = data.as_chunks::(); for block in blocks { - increment_counter(&mut counter, 1024); - compress_block(&mut chaining_value, block, &counter); + counter = counter.wrapping_add(1024); + compress_block(&mut chaining_value, block, counter); } let used = tail.len(); - increment_counter(&mut counter, (used as u32) * 8); + counter = counter.wrapping_add(used as u128 * 8); let saved_counter = counter; let mut block: [u8; BLOCK] = zero_extend(tail); block[used] = 0x80; if used == 0 { - counter = [0; 4]; + counter = 0; } else if used >= 110 { - compress_block(&mut chaining_value, &block, &counter); + compress_block(&mut chaining_value, &block, counter); block = [0u8; BLOCK]; - counter = [0; 4]; + counter = 0; } - block[110..114].copy_from_slice(&saved_counter[0].to_le_bytes()); - block[114..118].copy_from_slice(&saved_counter[1].to_le_bytes()); - block[118..122].copy_from_slice(&saved_counter[2].to_le_bytes()); - block[122..126].copy_from_slice(&saved_counter[3].to_le_bytes()); - block[126] = (16u32 << 5) as u8; - block[127] = (16u32 >> 3) as u8; - compress_block(&mut chaining_value, &block, &counter); + block[110..126].copy_from_slice(&saved_counter.to_le_bytes()); + block[126..].copy_from_slice(&512u16.to_le_bytes()); + compress_block(&mut chaining_value, &block, counter); chaining_value } From 38e693922e83929318389286fcff9f6791ab6e63 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 13 Oct 2026 03:24:55 +0530 Subject: [PATCH 24/27] pow%refac(keccak): drop the permutation-generic sponge for `hash512` --- pkgs/pow/src/keccak.rs | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/pkgs/pow/src/keccak.rs b/pkgs/pow/src/keccak.rs index fc1eca680..7c10b8f95 100644 --- a/pkgs/pow/src/keccak.rs +++ b/pkgs/pow/src/keccak.rs @@ -6,7 +6,8 @@ //! Keccak-512 hash function. -use crate::consts::keccak::{RC, ROTC}; +use crate::consts::keccak::{RATE, RC, ROTC}; +use crate::util::memops::{digest_from_u64_le, zero_extend}; /// Applies one round of the permutation, reading from `src` and writing to /// `dst`. @@ -110,36 +111,30 @@ pub fn keccak_f1600(state: &mut [u64; 25]) { } } -/// Keccak-512 sponge parameterised over a permutation function. -pub(crate) fn sponge(data: &[u8], perm: fn(&mut [u64; 25])) -> [u8; 64] { - use crate::consts::keccak::RATE; - use crate::util::memops::{digest_from_u64_le, zero_extend}; +/// Absorbs one rate-sized block into the first lanes of the state. +#[inline(always)] +fn absorb(state: &mut [u64; 25], block: &[u8; RATE]) { + for (lane, word) in state.iter_mut().zip(block.as_chunks::<8>().0) { + *lane ^= u64::from_le_bytes(*word); + } +} +pub fn hash512(data: &[u8]) -> [u8; 64] { let mut state = [0u64; 25]; - let absorb = |state: &mut [u64; 25], block: &[u8; RATE]| { - for (lane, word) in state.iter_mut().zip(block.as_chunks::<8>().0) { - *lane ^= u64::from_le_bytes(*word); - } - }; - let (blocks, tail) = data.as_chunks::(); for block in blocks { absorb(&mut state, block); - perm(&mut state); + keccak_f1600(&mut state); } let mut last: [u8; RATE] = zero_extend(tail); last[tail.len()] = 0x01; last[RATE - 1] |= 0x80; absorb(&mut state, &last); - perm(&mut state); + keccak_f1600(&mut state); digest_from_u64_le(&state) } -pub fn hash512(data: &[u8]) -> [u8; 64] { - sponge(data, keccak_f1600) -} - #[cfg(test)] mod tests { use super::hash512; From 2eb3dd32f68e7d1771b736c2bb4b5f4eea551e46 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 13 Oct 2026 03:25:29 +0530 Subject: [PATCH 25/27] pow%refac: expose only `hash512` of each algorithm --- pkgs/pow/src/blake.rs | 6 +++--- pkgs/pow/src/bmw.rs | 2 +- pkgs/pow/src/consts/blake.rs | 2 +- pkgs/pow/src/consts/bmw.rs | 2 +- pkgs/pow/src/consts/cubehash.rs | 2 +- pkgs/pow/src/consts/jh.rs | 2 +- pkgs/pow/src/consts/luffa.rs | 2 +- pkgs/pow/src/consts/shavite.rs | 2 +- pkgs/pow/src/consts/simd.rs | 2 +- pkgs/pow/src/consts/skein.rs | 2 +- pkgs/pow/src/cubehash.rs | 8 ++++---- pkgs/pow/src/echo.rs | 6 +++--- pkgs/pow/src/groestl.rs | 10 +++++----- pkgs/pow/src/jh.rs | 8 ++++---- pkgs/pow/src/keccak.rs | 2 +- pkgs/pow/src/lib.rs | 22 +++++++++++----------- pkgs/pow/src/luffa.rs | 4 ++-- pkgs/pow/src/shavite.rs | 4 ++-- pkgs/pow/src/simd.rs | 14 +++++++------- pkgs/pow/src/skein.rs | 20 ++++++++++---------- 20 files changed, 61 insertions(+), 61 deletions(-) diff --git a/pkgs/pow/src/blake.rs b/pkgs/pow/src/blake.rs index 4fb03be69..90cc60c1e 100644 --- a/pkgs/pow/src/blake.rs +++ b/pkgs/pow/src/blake.rs @@ -13,7 +13,7 @@ use crate::util::memops::{digest_from_u64_le, load_u64_le, store_u64_le, zero_ex /// /// `t0` and `t1` are the 128-bit bit counter after this block has been counted. #[inline(always)] -pub fn compress(h: &mut [u64; 8], m: &[u64; 16], t0: u64, t1: u64) { +fn compress(h: &mut [u64; 8], m: &[u64; 16], t0: u64, t1: u64) { #[rustfmt::skip] // The working state is the chaining value, four fixed constants, and the // counter mixed into the last four words. @@ -68,13 +68,13 @@ fn advance_counter(t0: &mut u64, t1: &mut u64) { /// Loads one block as sixteen message words in the byte order used by the /// compression function. #[inline] -pub fn load_message(block: &[u8]) -> [u64; 16] { +fn load_message(block: &[u8]) -> [u64; 16] { core::array::from_fn(|i| load_u64_le(block, i).swap_bytes()) } /// Runs the full hash and returns the internal 8-word state. #[inline(always)] -pub fn hash_to_state(data: &[u8]) -> [u64; 8] { +fn hash_to_state(data: &[u8]) -> [u64; 8] { let mut h = IV; let mut t0: u64 = 0; let mut t1: u64 = 0; diff --git a/pkgs/pow/src/bmw.rs b/pkgs/pow/src/bmw.rs index d1d15fc11..f58efce3a 100644 --- a/pkgs/pow/src/bmw.rs +++ b/pkgs/pow/src/bmw.rs @@ -165,7 +165,7 @@ fn fold_schedule(message: &[u64; WORDS], q: &[u64; QWORDS], out: &mut [u64; WORD } #[inline(always)] -pub fn compress(message: &[u64; WORDS], state: &[u64; WORDS], out: &mut [u64; WORDS]) { +fn compress(message: &[u64; WORDS], state: &[u64; WORDS], out: &mut [u64; WORDS]) { let mh = xor_words(message, state); let w = compute_w(&mh); diff --git a/pkgs/pow/src/consts/blake.rs b/pkgs/pow/src/consts/blake.rs index bc7c18095..1df642e14 100644 --- a/pkgs/pow/src/consts/blake.rs +++ b/pkgs/pow/src/consts/blake.rs @@ -16,7 +16,7 @@ pub(crate) const BLOCK: usize = 128; /// Blake-512 initial hash values (SHA-512 IV). #[rustfmt::skip] -pub const IV: [u64; 8] = [ +pub(crate) const IV: [u64; 8] = [ 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, diff --git a/pkgs/pow/src/consts/bmw.rs b/pkgs/pow/src/consts/bmw.rs index 667faad6f..b9b47e305 100644 --- a/pkgs/pow/src/consts/bmw.rs +++ b/pkgs/pow/src/consts/bmw.rs @@ -14,7 +14,7 @@ pub(crate) const BLOCK: usize = 128; /// BMW-512 IV: 16 x u64 with each byte incrementing by 8. -pub const IV: [u64; 16] = { +pub(crate) const IV: [u64; 16] = { let mut out = [0u64; 16]; let mut i = 0; while i < 16 { diff --git a/pkgs/pow/src/consts/cubehash.rs b/pkgs/pow/src/consts/cubehash.rs index 5e108e175..463c7d340 100644 --- a/pkgs/pow/src/consts/cubehash.rs +++ b/pkgs/pow/src/consts/cubehash.rs @@ -111,7 +111,7 @@ pub(super) const fn round_pair(s: &mut [u32; 32]) { /// /// Computed by setting state = \[64, 32, 16, 0, ..., 0\] (where 64 = h/8, 32 = /// b, 16 = r) and running 10r = 160 rounds (80 round-pairs). -pub const IV: [u32; 32] = { +pub(crate) const IV: [u32; 32] = { let mut s = [0u32; 32]; s[0] = 64; // h / 8 = 512 / 8 s[1] = 32; // b (block size in bytes) diff --git a/pkgs/pow/src/consts/jh.rs b/pkgs/pow/src/consts/jh.rs index 9df5355c2..70c687e1e 100644 --- a/pkgs/pow/src/consts/jh.rs +++ b/pkgs/pow/src/consts/jh.rs @@ -252,7 +252,7 @@ pub(crate) const ROUND_CONSTS: [[u64; 4]; 42] = { /// The config block has digest size (512 = 0x0200) in the first two bytes. With zero message, F_8 simplifies to E_8. #[rustfmt::skip] /// JH-512 IV derived from the config block. -pub const IV: [[u64; 2]; 8] = [ +pub(crate) const IV: [[u64; 2]; 8] = [ [0x17aa003e964bd16f, 0x43d5157a052e6a63], [0x0bef970c8d5e228a, 0x61c3b3f2591234e9], [0x1e806f53c1a01d89, 0x806d2bea6b05a92a], diff --git a/pkgs/pow/src/consts/luffa.rs b/pkgs/pow/src/consts/luffa.rs index e7815ada8..245f185db 100644 --- a/pkgs/pow/src/consts/luffa.rs +++ b/pkgs/pow/src/consts/luffa.rs @@ -17,7 +17,7 @@ pub(crate) const BLOCK: usize = 32; /// Initial branch states (Appendix C.1). #[rustfmt::skip] -pub const IV: [[u32; 8]; 5] = [ +pub(crate) const IV: [[u32; 8]; 5] = [ [ 0x6d251e69, 0x44b051e0, 0x4eaa6fb4, 0xdbf78465, 0x6e292011, 0x90152df4, 0xee058139, 0xdef610bb, diff --git a/pkgs/pow/src/consts/shavite.rs b/pkgs/pow/src/consts/shavite.rs index 486cb606c..911e93b0c 100644 --- a/pkgs/pow/src/consts/shavite.rs +++ b/pkgs/pow/src/consts/shavite.rs @@ -11,7 +11,7 @@ pub(crate) const BLOCK: usize = 128; /// SHAvite-512 IV. #[rustfmt::skip] -pub const IV: [u32; 16] = [ +pub(crate) const IV: [u32; 16] = [ 0x72fccdd8, 0x79ca4727, 0x128a077b, 0x40d55aec, 0xd1901a06, 0x430ae307, 0xb29f5cd1, 0xdf07fbfc, 0x8e45d73d, 0x681ab538, 0xbde86578, 0xdd577e47, diff --git a/pkgs/pow/src/consts/simd.rs b/pkgs/pow/src/consts/simd.rs index e1443a70e..88bb9eea7 100644 --- a/pkgs/pow/src/consts/simd.rs +++ b/pkgs/pow/src/consts/simd.rs @@ -223,7 +223,7 @@ const fn wbread_const(q: &[i32], sb: usize, o1: i32, o2: i32, mm: i32) -> [u32; } /// IV derived by compressing `"SIMD-512 v1.1"` into the zero state. -pub const IV: [u32; 32] = { +pub(crate) const IV: [u32; 32] = { let mut buf = [0u8; 128]; let msg = b"SIMD-512 v1.1"; let mut i = 0; diff --git a/pkgs/pow/src/consts/skein.rs b/pkgs/pow/src/consts/skein.rs index 0aa53b7dd..274a46d21 100644 --- a/pkgs/pow/src/consts/skein.rs +++ b/pkgs/pow/src/consts/skein.rs @@ -15,7 +15,7 @@ pub(crate) const NW: usize = 8; /// Skein-512 IV (from the specification, derived by UBI-processing /// a config block with schema "SHA3", version 1, output bits 512). #[rustfmt::skip] -pub const IV: [u64; 8] = [ +pub(crate) const IV: [u64; 8] = [ 0x4903adff749c51ce, 0x0d95de399746df03, 0x8fd1934127c79bce, 0x9a255629ff352cb1, 0x5db62599df6ca7b0, 0xeabe394ca9d5c3f4, diff --git a/pkgs/pow/src/cubehash.rs b/pkgs/pow/src/cubehash.rs index c0091ae12..8df29e837 100644 --- a/pkgs/pow/src/cubehash.rs +++ b/pkgs/pow/src/cubehash.rs @@ -13,7 +13,7 @@ use crate::util::memops::{digest_from_u32_le, load_u32_le, zero_extend}; use wide::u32x4; /// Four neighbouring state words packed into one group. -pub type U32x4 = u32x4; +type U32x4 = u32x4; #[inline] /// Swaps the low and high 2-word halves inside one 4-word register. @@ -31,7 +31,7 @@ fn swap_pairs(words: U32x4) -> U32x4 { #[inline] /// Loads one 4-word register from the flat 32-word state. -pub fn load_vec(words: &[u32; 32], index: usize) -> U32x4 { +fn load_vec(words: &[u32; 32], index: usize) -> U32x4 { let base = index * 4; U32x4::new([words[base], words[base + 1], words[base + 2], words[base + 3]]) } @@ -71,7 +71,7 @@ fn load_block(block: &[u8]) -> (U32x4, U32x4) { /// Each round pair updates the upper half, rotates the lower half, swaps lanes, /// then repeats with a different rotation amount. #[inline] -pub fn sixteen_rounds(s: &mut [U32x4; 8]) { +fn sixteen_rounds(s: &mut [U32x4; 8]) { let mut i = 0; while i < 16 { s[4] += s[0]; @@ -117,7 +117,7 @@ pub fn sixteen_rounds(s: &mut [U32x4; 8]) { } /// Absorbs two word vectors into the state. -pub fn absorb_words(state: &mut [U32x4; 8], lo: U32x4, hi: U32x4) { +fn absorb_words(state: &mut [U32x4; 8], lo: U32x4, hi: U32x4) { state[0] ^= lo; state[1] ^= hi; sixteen_rounds(state); diff --git a/pkgs/pow/src/echo.rs b/pkgs/pow/src/echo.rs index c0d42e610..957753fd2 100644 --- a/pkgs/pow/src/echo.rs +++ b/pkgs/pow/src/echo.rs @@ -16,7 +16,7 @@ type AesState = [u32; 4]; type Grid = [Block; 16]; type Counter = u128; type ChainingValue = [AesState; 8]; -pub(super) type BlockWords = [u32; 32]; +type BlockWords = [u32; 32]; /// Grid permutation used by the row-shift step. const BIG_SHIFT_ROWS: [usize; 16] = [0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, 1, 6, 11]; @@ -101,7 +101,7 @@ fn apply_big_mix_columns(grid: &mut Grid) { /// Runs the compression function on one 1024-bit block. #[inline(always)] -pub(super) fn compress_words(chaining_value: &mut ChainingValue, block: &BlockWords, counter: Counter) { +fn compress_words(chaining_value: &mut ChainingValue, block: &BlockWords, counter: Counter) { // The upper half of the 4x4 grid starts from the chaining value and the // lower half from the message block. let mut grid = [Block::splat(0); 16]; @@ -136,7 +136,7 @@ pub(super) fn compress_words(chaining_value: &mut ChainingValue, block: &BlockWo /// Runs the compression function on one 1024-bit block. #[inline(always)] -pub fn compress_block(chaining_value: &mut ChainingValue, block: &[u8; BLOCK], counter: Counter) { +fn compress_block(chaining_value: &mut ChainingValue, block: &[u8; BLOCK], counter: Counter) { let words = core::array::from_fn(|word| load_u32_le(block, word)); compress_words(chaining_value, &words, counter); } diff --git a/pkgs/pow/src/groestl.rs b/pkgs/pow/src/groestl.rs index a91372492..f4b5b57af 100644 --- a/pkgs/pow/src/groestl.rs +++ b/pkgs/pow/src/groestl.rs @@ -13,7 +13,7 @@ use crate::util::memops::zero_extend; use wide::{i8x16, u8x16}; /// One row of the byte-sliced state, a byte per column. -pub type Row = u8x16; +type Row = u8x16; /// Multiplies each byte by `x` in `GF(2^8)`. /// @@ -194,7 +194,7 @@ fn load_block(block: &[u8; BLOCK]) -> [Row; 8] { /// Extracts columns 8 through 15 as the 512-bit output digest. #[inline(always)] -pub fn extract_right_half(rows: &[Row; 8], out: &mut [u8; 64]) { +fn extract_right_half(rows: &[Row; 8], out: &mut [u8; 64]) { let mut col = 8; while col < 16 { let offset = (col - 8) * 8; @@ -223,7 +223,7 @@ fn xor_rows(lhs: &[Row; 8], rhs: &[Row; 8]) -> [Row; 8] { /// Computes `H = H ^ P(H ^ M) ^ Q(M)`. #[inline(always)] -pub fn compress_rows(chaining_value: &mut [Row; 8], message: &[Row; 8]) { +fn compress_rows(chaining_value: &mut [Row; 8], message: &[Row; 8]) { let mut p_state = xor_rows(chaining_value, message); let mut q_state = *message; perm_pq(&mut p_state, &mut q_state); @@ -232,14 +232,14 @@ pub fn compress_rows(chaining_value: &mut [Row; 8], message: &[Row; 8]) { /// Computes `H = H ^ P(H ^ M) ^ Q(M)`. #[inline(always)] -pub fn compress_block(chaining_value: &mut [Row; 8], block: &[u8; BLOCK]) { +fn compress_block(chaining_value: &mut [Row; 8], block: &[u8; BLOCK]) { let message = load_block(block); compress_rows(chaining_value, &message); } /// Computes `H = H ^ P(H)` before the final digest extraction. #[inline(always)] -pub fn output_transform(chaining_value: &mut [Row; 8]) { +fn output_transform(chaining_value: &mut [Row; 8]) { let mut state = *chaining_value; perm_p(&mut state); *chaining_value = xor_rows(chaining_value, &state); diff --git a/pkgs/pow/src/jh.rs b/pkgs/pow/src/jh.rs index 14ecc4111..4e2ae24df 100644 --- a/pkgs/pow/src/jh.rs +++ b/pkgs/pow/src/jh.rs @@ -12,12 +12,12 @@ use crate::util::memops::{load_u64_le, store_u64_le, zero_extend}; use wide::u32x4; /// One 128-bit JH row, stored as four 32-bit lanes. -pub type Row = u32x4; +type Row = u32x4; /// JH state as eight 128-bit rows. type State = [Row; 8]; #[inline] -pub fn row_from_u64_pair(pair: [u64; 2]) -> Row { +fn row_from_u64_pair(pair: [u64; 2]) -> Row { Row::new([ pair[0] as u32, (pair[0] >> 32) as u32, @@ -27,7 +27,7 @@ pub fn row_from_u64_pair(pair: [u64; 2]) -> Row { } #[inline] -pub fn row_to_u64_pair(row: Row) -> [u64; 2] { +fn row_to_u64_pair(row: Row) -> [u64; 2] { let row = row.to_array(); [ u64::from(row[0]) | (u64::from(row[1]) << 32), @@ -208,7 +208,7 @@ fn store_row(buf: &mut [u8], index: usize, row: Row) { } #[inline(always)] -pub fn compress(state: &mut State, block_words: &[u64; 8]) { +fn compress(state: &mut State, block_words: &[u64; 8]) { let mut row = 0; while row < 4 { state[row] ^= row_from_u64_pair([block_words[row * 2], block_words[row * 2 + 1]]); diff --git a/pkgs/pow/src/keccak.rs b/pkgs/pow/src/keccak.rs index 7c10b8f95..1e4bae5a1 100644 --- a/pkgs/pow/src/keccak.rs +++ b/pkgs/pow/src/keccak.rs @@ -101,7 +101,7 @@ fn round_to(src: &[u64; 25], dst: &mut [u64; 25], rc: u64) { /// `state`, the next reads from `alt`, and the final result ends back in /// `state`. #[inline(always)] -pub fn keccak_f1600(state: &mut [u64; 25]) { +fn keccak_f1600(state: &mut [u64; 25]) { let mut alt = [0u64; 25]; let mut i = 0; while i < 24 { diff --git a/pkgs/pow/src/lib.rs b/pkgs/pow/src/lib.rs index d32de5edf..e43266bdc 100644 --- a/pkgs/pow/src/lib.rs +++ b/pkgs/pow/src/lib.rs @@ -74,37 +74,37 @@ mod util; #[doc(hidden)] pub mod __private { pub mod blake { - pub use crate::blake::*; + pub use crate::blake::hash512; } pub mod bmw { - pub use crate::bmw::*; + pub use crate::bmw::hash512; } pub mod cubehash { - pub use crate::cubehash::*; + pub use crate::cubehash::hash512; } pub mod echo { - pub use crate::echo::*; + pub use crate::echo::hash512; } pub mod groestl { - pub use crate::groestl::*; + pub use crate::groestl::hash512; } pub mod jh { - pub use crate::jh::*; + pub use crate::jh::hash512; } pub mod keccak { - pub use crate::keccak::*; + pub use crate::keccak::hash512; } pub mod luffa { - pub use crate::luffa::*; + pub use crate::luffa::hash512; } pub mod shavite { - pub use crate::shavite::*; + pub use crate::shavite::hash512; } pub mod simd { - pub use crate::simd::*; + pub use crate::simd::hash512; } pub mod skein { - pub use crate::skein::*; + pub use crate::skein::hash512; } } diff --git a/pkgs/pow/src/luffa.rs b/pkgs/pow/src/luffa.rs index 0ef595bf8..9fbd4714b 100644 --- a/pkgs/pow/src/luffa.rs +++ b/pkgs/pow/src/luffa.rs @@ -123,7 +123,7 @@ fn write_output_half(state: &[[u32; 8]; 5], out: &mut [u8]) { /// Injects one message block into the five branches. #[inline(always)] -pub fn inject_message(state: &mut [[u32; 8]; 5], msg: &[u32; 8]) { +fn inject_message(state: &mut [[u32; 8]; 5], msg: &[u32; 8]) { let [mut b0, mut b1, mut b2, mut b3, mut b4] = *state; let mut sum = [0u32; 8]; @@ -211,7 +211,7 @@ fn permute_branch(words: &mut [u32; 8], rc0: &[u32; 8], rc4: &[u32; 8]) { /// Runs the 8-round permutation on all five branches. #[inline(always)] -pub fn permute_state(state: &mut [[u32; 8]; 5]) { +fn permute_state(state: &mut [[u32; 8]; 5]) { tweak_branches(state); permute_first_four_branches(state); permute_branch(&mut state[4], &RC[4][0], &RC[4][1]); diff --git a/pkgs/pow/src/shavite.rs b/pkgs/pow/src/shavite.rs index b532201b2..e5433f7a3 100644 --- a/pkgs/pow/src/shavite.rs +++ b/pkgs/pow/src/shavite.rs @@ -15,7 +15,7 @@ use wide::u32x4; const SCHEDULE_BUNDLES: usize = 112; type Bundle = [u32; 4]; type State = [u32; 16]; -pub(super) type BlockWords = [u32; 32]; +type BlockWords = [u32; 32]; /// Selects which counter word is injected at one schedule checkpoint. #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] @@ -210,7 +210,7 @@ fn rotate_columns(state: &mut State) { /// Runs the SHAvite-3-512 compression function. #[inline(always)] -pub fn compress_block(chaining_value: &mut State, msg: &[u8; BLOCK], counter: u128) { +fn compress_block(chaining_value: &mut State, msg: &[u8; BLOCK], counter: u128) { let schedule = expand_round_keys(msg, counter); compress_schedule(chaining_value, &schedule); } diff --git a/pkgs/pow/src/simd.rs b/pkgs/pow/src/simd.rs index ba120da33..d4831354f 100644 --- a/pkgs/pow/src/simd.rs +++ b/pkgs/pow/src/simd.rs @@ -11,7 +11,7 @@ use crate::util::memops::{digest_from_u32_le, load_u32_le, zero_extend}; use wide::{i32x4, i32x8, u32x8}; -pub(super) type BlockWords = [u32; 32]; +type BlockWords = [u32; 32]; type WordVec = u32x8; type IntVec = i32x8; type Int4 = i32x4; @@ -248,7 +248,7 @@ fn wbread(q: &[i32], sb: usize, o1: i32, o2: i32, mm: i32, w: &mut [u32; 64]) { } #[inline(always)] -pub(super) fn simd_if(x: u32, y: u32, z: u32) -> u32 { +fn simd_if(x: u32, y: u32, z: u32) -> u32 { ((y ^ z) & x) ^ z } @@ -326,7 +326,7 @@ fn store_word_vec(state: &mut [u32; 32], offset: usize, value: WordVec) { /// Runs one 8-lane state-update step group. #[inline(always)] -pub(super) fn step2_big( +fn step2_big( state: &mut [u32; 32], w: &[u32], w_off: usize, @@ -354,7 +354,7 @@ pub(super) fn step2_big( } #[inline(always)] -pub(super) fn one_round_big(state: &mut [u32; 32], w: &[u32; 64], isp: usize, p0: u32, p1: u32, p2: u32, p3: u32) { +fn one_round_big(state: &mut [u32; 32], w: &[u32; 64], isp: usize, p0: u32, p1: u32, p2: u32, p3: u32) { step2_big(state, w, 0, simd_if, p0, p1, PP8K[isp]); step2_big(state, w, 8, simd_if, p1, p2, PP8K[isp + 1]); step2_big(state, w, 16, simd_if, p2, p3, PP8K[isp + 2]); @@ -366,7 +366,7 @@ pub(super) fn one_round_big(state: &mut [u32; 32], w: &[u32; 64], isp: usize, p0 } #[inline(always)] -pub fn compress(h: &mut [u32; 32], buf: &[u8; BLOCK], last: bool) { +fn compress(h: &mut [u32; 32], buf: &[u8; BLOCK], last: bool) { let words = core::array::from_fn(|i| load_u32_le(buf, i)); compress_words(h, buf, &words, last); } @@ -408,7 +408,7 @@ fn compress_words(h: &mut [u32; 32], buf: &[u8; BLOCK], words: &BlockWords, last /// Encodes the processed bit count for the final block. #[inline(always)] -pub(super) fn encode_count(dst: &mut [u8], low: u32, high: u32, ptr: usize) { +fn encode_count(dst: &mut [u8], low: u32, high: u32, ptr: usize) { let low_bits = low.wrapping_shl(10); let high_bits = high.wrapping_shl(10).wrapping_add(low >> 22); let low_bits = low_bits.wrapping_add((ptr as u32) << 3); @@ -416,7 +416,7 @@ pub(super) fn encode_count(dst: &mut [u8], low: u32, high: u32, ptr: usize) { dst[4..8].copy_from_slice(&high_bits.to_le_bytes()); } -pub(super) fn hash_to_words(data: &[u8]) -> [u32; 16] { +fn hash_to_words(data: &[u8]) -> [u32; 16] { let mut h = IV; let mut count_low = 0u32; let mut count_high = 0u32; diff --git a/pkgs/pow/src/skein.rs b/pkgs/pow/src/skein.rs index 84ae4954a..33f237fe5 100644 --- a/pkgs/pow/src/skein.rs +++ b/pkgs/pow/src/skein.rs @@ -11,7 +11,7 @@ use crate::util::memops::{digest_from_u64_le, load_u64_le, zero_extend}; use crate::util::threefish::encrypt; /// UBI type code for message blocks. -pub(super) const TYPE_MSG: u64 = 48; +const TYPE_MSG: u64 = 48; /// UBI type code for output blocks. const TYPE_OUTPUT: u64 = 63; /// Marks the first block in one UBI stream. @@ -20,20 +20,20 @@ const FLAG_FIRST: u64 = 1 << 62; const FLAG_FINAL: u64 = 1 << 63; /// Skein chaining value, stored as eight 64-bit words. -pub type Chaining = [u64; NW]; +type Chaining = [u64; NW]; /// One 64-byte Skein block decoded as eight little-endian words. -pub type BlockWords = [u64; NW]; +type BlockWords = [u64; NW]; #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] -pub struct UbiTweak { +struct UbiTweak { /// Total number of bytes seen so far in this UBI stream. - pub position: u64, + position: u64, /// UBI block type, such as "message" or "output". - pub kind: u64, + kind: u64, /// Marks the first block in a UBI stream. - pub first: bool, + first: bool, /// Marks the final block in a UBI stream. - pub final_block: bool, + final_block: bool, } impl UbiTweak { @@ -64,7 +64,7 @@ fn load_block(block: &[u8]) -> BlockWords { /// UBI uses Threefish with the current chaining value as the key. The block /// itself is both the plaintext and part of the feedforward step. #[inline(always)] -pub fn ubi(state: &mut Chaining, plaintext: &BlockWords, tweak: UbiTweak) { +fn ubi(state: &mut Chaining, plaintext: &BlockWords, tweak: UbiTweak) { let mut encrypted = *plaintext; encrypt(&mut encrypted, state, &tweak.as_words()); @@ -114,7 +114,7 @@ fn hash_message_blocks(state: &mut Chaining, data: &[u8]) { /// Skein treats output generation as a separate UBI stream with its own /// block type. Skein-512 needs only the first 64 output bytes. #[inline(always)] -pub fn output_block(state: &mut Chaining) { +fn output_block(state: &mut Chaining) { let zero = [0u64; NW]; let tweak = UbiTweak { position: 8, From a30c69e932c183e246a8ca0bfba7f6e840da5c32 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Sun, 11 Oct 2026 20:05:51 +0530 Subject: [PATCH 26/27] sdk%ci: build and test every feature on the MSRV, lint on `nightly` --- .github/workflows/build_msrv.yml | 64 +++++++++--- .github/workflows/build_nightly.yml | 141 --------------------------- .github/workflows/build_stable.yml | 23 ++--- .github/workflows/pkg_num.yml | 8 -- .github/workflows/pkg_p2p_core.yml | 8 -- .github/workflows/pkg_params.yml | 8 -- .github/workflows/pkg_pkc.yml | 8 -- .github/workflows/pkg_pow.yml | 9 -- .github/workflows/pkg_primitives.yml | 8 -- .github/workflows/pkg_script.yml | 8 -- .github/workflows/pkg_types.yml | 8 -- Justfile | 17 ++-- docs/dev/getting_started.md | 2 +- 13 files changed, 70 insertions(+), 242 deletions(-) delete mode 100644 .github/workflows/build_nightly.yml diff --git a/.github/workflows/build_msrv.yml b/.github/workflows/build_msrv.yml index 671c91a05..69765a6ee 100644 --- a/.github/workflows/build_msrv.yml +++ b/.github/workflows/build_msrv.yml @@ -61,6 +61,14 @@ jobs: key: cargo-deps-${{ hashFiles('Cargo.lock', 'docs/samples/Cargo.lock') }} restore-keys: cargo-deps- + - name: Manage build artifacts + uses: actions/cache@v5 + with: + path: target + key: cargo-build-lint-${{ runner.os }}-${{ runner.arch }}-${{ github.sha }} + restore-keys: | + cargo-build-lint-${{ runner.os }}-${{ runner.arch }}- + - name: Manage CodeQL packs uses: actions/cache@v5 with: @@ -70,6 +78,9 @@ jobs: - name: Run linters run: just lint + - name: Lint every feature combination + run: cargo hack --workspace --exclude-features default --feature-powerset clippy --all-targets -- -D warnings + - name: Check PR title if: github.event_name == 'pull_request' env: @@ -85,6 +96,8 @@ jobs: build: name: Build and test runs-on: ubuntu-24.04-arm + env: + HAS_CODECOV: ${{ secrets.CODECOV_TOKEN != '' }} defaults: run: @@ -133,21 +146,50 @@ jobs: - name: Manage build artifacts uses: actions/cache@v5 with: - path: target + path: | + target + !target/nextest key: cargo-build-msrv-${{ runner.os }}-${{ runner.arch }}-${{ github.sha }} restore-keys: | cargo-build-msrv-${{ runner.os }}-${{ runner.arch }}- - - name: Build workspace + - name: Select the MSRV toolchain run: | - export RUSTC="$TOOLCHAIN_MSRV/bin/rustc" - export RUSTDOC="$TOOLCHAIN_MSRV/bin/rustdoc" - "$TOOLCHAIN_MSRV/bin/cargo" build --workspace --exclude dash-pow --features full - "$TOOLCHAIN_MSRV/bin/cargo" build -p dash-pow --features std + { + echo "RUSTC=$TOOLCHAIN_MSRV/bin/rustc" + echo "RUSTDOC=$TOOLCHAIN_MSRV/bin/rustdoc" + echo "CARGO=$TOOLCHAIN_MSRV/bin/cargo" + } >> "$GITHUB_ENV" - - name: Test workspace + - name: Build every feature + run: '"$CARGO" hack --workspace --exclude-features default --each-feature build' + + - name: Test every feature + run: | + "$CARGO" hack --workspace --exclude-features default --each-feature nextest run --config-file .cargo/nextest.toml --no-tests=warn + "$CARGO" hack --workspace --exclude-features default --each-feature test --doc + + - name: Test workspace (with coverage) run: | - export RUSTC="$TOOLCHAIN_MSRV/bin/rustc" - export RUSTDOC="$TOOLCHAIN_MSRV/bin/rustdoc" - "$TOOLCHAIN_MSRV/bin/cargo" test --workspace --exclude dash-pow --features full - "$TOOLCHAIN_MSRV/bin/cargo" test -p dash-pow --features std + "$CARGO" llvm-cov clean --workspace + "$CARGO" llvm-cov --no-report nextest --config-file .cargo/nextest.toml --workspace --features full + "$CARGO" llvm-cov report --lcov --output-path lcov.info + + - name: Upload coverage to Codecov + if: env.HAS_CODECOV == 'true' + uses: codecov/codecov-action@v5 + with: + files: lcov.info + flags: dash-num,dash-p2p-core,dash-params,dash-pkc,dash-pow,dash-primitives,dash-script,dash-types + token: ${{ secrets.CODECOV_TOKEN }} + + - name: Upload test results to Codecov + if: ${{ !cancelled() && env.HAS_CODECOV == 'true' && hashFiles('target/nextest/default/junit.xml') != '' }} + uses: codecov/codecov-action@v5 + with: + files: target/nextest/default/junit.xml + report_type: test_results + token: ${{ secrets.CODECOV_TOKEN }} + + - name: Sanity check benchmarks + run: '"$CARGO" hack --workspace --exclude-features default --each-feature test --benches' diff --git a/.github/workflows/build_nightly.yml b/.github/workflows/build_nightly.yml deleted file mode 100644 index aeb208259..000000000 --- a/.github/workflows/build_nightly.yml +++ /dev/null @@ -1,141 +0,0 @@ -name: Lint and build on nightly - -on: - workflow_call: - inputs: - package: - description: Cargo package name (e.g. dash-num) - required: true - type: string - package_dir: - description: Package directory relative to pkgs/ (e.g. num) - required: true - type: string - secrets: - CACHIX_AUTH_TOKEN: - required: false - CODECOV_TOKEN: - required: false - -permissions: - contents: read - -jobs: - nightly: - name: ubuntu-24.04-arm (nightly, ${{ matrix.config.name }}) - runs-on: ubuntu-24.04-arm - env: - HAS_CODECOV: ${{ secrets.CODECOV_TOKEN != '' }} - strategy: - fail-fast: false - matrix: - config: - - { name: full, args: "--features full" } - - { name: min, args: "--no-default-features" } - - { name: std, args: "--features std" } - - { name: powerset, args: "" } - - defaults: - run: - shell: nix develop ./contrib/nix#ci --command bash -eo pipefail {0} - - steps: - - name: Checkout - uses: actions/checkout@v6 - with: - fetch-depth: 1 - - - name: Run pull request checks - if: github.event_name == 'pull_request' - shell: bash - run: | - source .github/ci.env - commits=${{ github.event.pull_request.commits }} - if [ "${commits}" -gt "${MAX_PR_COMMITS}" ]; then - echo "::error::PR has ${commits} commits (limit: ${MAX_PR_COMMITS})" - exit 1 - fi - - - name: Install Nix - uses: DeterminateSystems/nix-installer-action@v22 - with: - extra-conf: | - extra-substituters = https://dashpay-base-sdk.cachix.org - extra-trusted-public-keys = dashpay-base-sdk.cachix.org-1:TAoDyYL2e60lUEG8n10gvIZmujM5rJAhaG613fNfOWI= - - - name: Cache Nix - uses: cachix/cachix-action@v17 - with: - name: dashpay-base-sdk - authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}" - skipAddingSubstituter: true - - - name: Manage cargo registry - uses: actions/cache@v5 - with: - path: | - ~/.cargo/registry - ~/.cargo/git - key: cargo-deps-${{ hashFiles('Cargo.lock', 'docs/samples/Cargo.lock') }} - restore-keys: cargo-deps- - - - name: Manage build artifacts - uses: actions/cache@v5 - with: - path: | - target - !target/nextest - key: cargo-build-nightly-${{ runner.os }}-${{ runner.arch }}-${{ inputs.package }}-${{ matrix.config.name }}-${{ github.sha }} - restore-keys: | - cargo-build-nightly-${{ runner.os }}-${{ runner.arch }}-${{ inputs.package }}-${{ matrix.config.name }}- - - - name: Lint package - if: matrix.config.name == 'full' || matrix.config.name == 'min' - run: cargo clippy -p ${{ inputs.package }} ${{ matrix.config.args }} --tests --benches -- -D warnings - - - name: Lint every feature combination - if: matrix.config.name == 'powerset' - run: cargo hack clippy -p ${{ inputs.package }} --feature-powerset --exclude-features default -- -D warnings - - - name: Build package - if: matrix.config.name == 'full' || matrix.config.name == 'min' - run: cargo build -p ${{ inputs.package }} ${{ matrix.config.args }} - - - name: Test package - if: matrix.config.name == 'min' || matrix.config.name == 'std' - run: | - cargo nextest run --config-file .cargo/nextest.toml -p ${{ inputs.package }} ${{ matrix.config.args }} - cargo test --doc -p ${{ inputs.package }} ${{ matrix.config.args }} - - - name: Check formatting - if: matrix.config.name == 'full' - run: python3 maint/lint/lint_rust.py - - - name: Test package (with coverage) - if: matrix.config.name == 'full' - run: | - cargo llvm-cov clean --workspace - cargo llvm-cov --no-report nextest --config-file .cargo/nextest.toml ${{ matrix.config.args }} --package ${{ inputs.package }} - cargo llvm-cov report --lcov --output-path lcov.info - cargo test --doc -p ${{ inputs.package }} ${{ matrix.config.args }} - - - name: Upload coverage to Codecov - if: matrix.config.name == 'full' && env.HAS_CODECOV == 'true' - uses: codecov/codecov-action@v5 - with: - files: lcov.info - flags: ${{ inputs.package }} - token: ${{ secrets.CODECOV_TOKEN }} - - - name: Upload test results to Codecov - if: ${{ !cancelled() && matrix.config.name == 'full' && env.HAS_CODECOV == 'true' && hashFiles('target/nextest/default/junit.xml') != '' }} - uses: codecov/codecov-action@v5 - with: - files: target/nextest/default/junit.xml - report_type: test_results - flags: ${{ inputs.package }} - token: ${{ secrets.CODECOV_TOKEN }} - - - name: Sanity check benchmarks - if: matrix.config.name != 'powerset' - run: cargo test ${{ matrix.config.args }} --package ${{ inputs.package }} --benches diff --git a/.github/workflows/build_stable.yml b/.github/workflows/build_stable.yml index f038d5d42..2a9db3117 100644 --- a/.github/workflows/build_stable.yml +++ b/.github/workflows/build_stable.yml @@ -11,11 +11,6 @@ on: description: Package directory relative to pkgs/ (e.g. num) required: true type: string - features: - description: Cargo features to enable (default full) - required: false - type: string - default: full permissions: contents: read @@ -56,6 +51,11 @@ jobs: - name: Install Rust toolchain uses: dtolnay/rust-toolchain@stable + - name: Install cargo-hack + uses: taiki-e/install-action@v2 + with: + tool: cargo-hack + - name: Manage cargo registry uses: actions/cache@v5 with: @@ -73,17 +73,12 @@ jobs: restore-keys: | cargo-build-stable-${{ runner.os }}-${{ runner.arch }}-${{ inputs.package }}- - - name: Build package - run: cargo build -p ${{ inputs.package }} --features ${{ inputs.features }} - env: - RUSTUP_TOOLCHAIN: stable - - - name: Build package without default features - run: cargo build -p ${{ inputs.package }} --no-default-features + - name: Build every feature + run: cargo hack -p ${{ inputs.package }} --exclude-features default --each-feature build env: RUSTUP_TOOLCHAIN: stable - - name: Test package - run: cargo test -p ${{ inputs.package }} --features ${{ inputs.features }} + - name: Test every feature + run: cargo hack -p ${{ inputs.package }} --exclude-features default --each-feature test env: RUSTUP_TOOLCHAIN: stable diff --git a/.github/workflows/pkg_num.yml b/.github/workflows/pkg_num.yml index a399bc320..ebe7f94c1 100644 --- a/.github/workflows/pkg_num.yml +++ b/.github/workflows/pkg_num.yml @@ -9,7 +9,6 @@ on: - Cargo.toml - .github/workflows/pkg_num.yml - .github/workflows/build_stable.yml - - .github/workflows/build_nightly.yml - contrib/nix/** - pyproject.toml - rust-toolchain.toml @@ -27,10 +26,3 @@ jobs: with: package: dash-num package_dir: num - - nightly: - uses: ./.github/workflows/build_nightly.yml - with: - package: dash-num - package_dir: num - secrets: inherit diff --git a/.github/workflows/pkg_p2p_core.yml b/.github/workflows/pkg_p2p_core.yml index dece34587..135b7f2b0 100644 --- a/.github/workflows/pkg_p2p_core.yml +++ b/.github/workflows/pkg_p2p_core.yml @@ -14,7 +14,6 @@ on: - Cargo.toml - .github/workflows/pkg_p2p_core.yml - .github/workflows/build_stable.yml - - .github/workflows/build_nightly.yml - contrib/nix/** - pyproject.toml - rust-toolchain.toml @@ -32,10 +31,3 @@ jobs: with: package: dash-p2p-core package_dir: p2p_core - - nightly: - uses: ./.github/workflows/build_nightly.yml - with: - package: dash-p2p-core - package_dir: p2p_core - secrets: inherit diff --git a/.github/workflows/pkg_params.yml b/.github/workflows/pkg_params.yml index 9f9c5f6e7..8cf2c7bd2 100644 --- a/.github/workflows/pkg_params.yml +++ b/.github/workflows/pkg_params.yml @@ -12,7 +12,6 @@ on: - Cargo.toml - .github/workflows/pkg_params.yml - .github/workflows/build_stable.yml - - .github/workflows/build_nightly.yml - contrib/nix/** - pyproject.toml - rust-toolchain.toml @@ -30,10 +29,3 @@ jobs: with: package: dash-params package_dir: params - - nightly: - uses: ./.github/workflows/build_nightly.yml - with: - package: dash-params - package_dir: params - secrets: inherit diff --git a/.github/workflows/pkg_pkc.yml b/.github/workflows/pkg_pkc.yml index 7d0cbb259..36da34f96 100644 --- a/.github/workflows/pkg_pkc.yml +++ b/.github/workflows/pkg_pkc.yml @@ -11,7 +11,6 @@ on: - Cargo.toml - .github/workflows/pkg_pkc.yml - .github/workflows/build_stable.yml - - .github/workflows/build_nightly.yml - contrib/nix/** - pyproject.toml - rust-toolchain.toml @@ -29,10 +28,3 @@ jobs: with: package: dash-pkc package_dir: pkc - - nightly: - uses: ./.github/workflows/build_nightly.yml - with: - package: dash-pkc - package_dir: pkc - secrets: inherit diff --git a/.github/workflows/pkg_pow.yml b/.github/workflows/pkg_pow.yml index 26b1816d5..cee9ddeea 100644 --- a/.github/workflows/pkg_pow.yml +++ b/.github/workflows/pkg_pow.yml @@ -10,7 +10,6 @@ on: - Cargo.toml - .github/workflows/pkg_pow.yml - .github/workflows/build_stable.yml - - .github/workflows/build_nightly.yml - contrib/nix/** - pyproject.toml - rust-toolchain.toml @@ -28,11 +27,3 @@ jobs: with: package: dash-pow package_dir: pow - features: std - - nightly: - uses: ./.github/workflows/build_nightly.yml - with: - package: dash-pow - package_dir: pow - secrets: inherit diff --git a/.github/workflows/pkg_primitives.yml b/.github/workflows/pkg_primitives.yml index 40fcaf99c..cd6e44f4b 100644 --- a/.github/workflows/pkg_primitives.yml +++ b/.github/workflows/pkg_primitives.yml @@ -12,7 +12,6 @@ on: - Cargo.toml - .github/workflows/pkg_primitives.yml - .github/workflows/build_stable.yml - - .github/workflows/build_nightly.yml - contrib/nix/** - pyproject.toml - rust-toolchain.toml @@ -30,10 +29,3 @@ jobs: with: package: dash-primitives package_dir: primitives - - nightly: - uses: ./.github/workflows/build_nightly.yml - with: - package: dash-primitives - package_dir: primitives - secrets: inherit diff --git a/.github/workflows/pkg_script.yml b/.github/workflows/pkg_script.yml index f49edae57..4a9fa5843 100644 --- a/.github/workflows/pkg_script.yml +++ b/.github/workflows/pkg_script.yml @@ -10,7 +10,6 @@ on: - Cargo.toml - .github/workflows/pkg_script.yml - .github/workflows/build_stable.yml - - .github/workflows/build_nightly.yml - contrib/nix/** - pyproject.toml - rust-toolchain.toml @@ -28,10 +27,3 @@ jobs: with: package: dash-script package_dir: script - - nightly: - uses: ./.github/workflows/build_nightly.yml - with: - package: dash-script - package_dir: script - secrets: inherit diff --git a/.github/workflows/pkg_types.yml b/.github/workflows/pkg_types.yml index e6d4fff1b..7542a9966 100644 --- a/.github/workflows/pkg_types.yml +++ b/.github/workflows/pkg_types.yml @@ -9,7 +9,6 @@ on: - Cargo.toml - .github/workflows/pkg_types.yml - .github/workflows/build_stable.yml - - .github/workflows/build_nightly.yml - contrib/nix/** - pyproject.toml - rust-toolchain.toml @@ -27,10 +26,3 @@ jobs: with: package: dash-types package_dir: types - - nightly: - uses: ./.github/workflows/build_nightly.yml - with: - package: dash-types - package_dir: types - secrets: inherit diff --git a/Justfile b/Justfile index 48d35ebc0..ad777e387 100644 --- a/Justfile +++ b/Justfile @@ -6,7 +6,8 @@ python := if os() == "windows" { "python" } else { "python3" } # (see https://packages.debian.org/trixie/just) export RUSTDOCFLAGS := "-D warnings" -nextest := "cargo nextest run --config-file .cargo/nextest.toml" +each := "cargo hack --workspace --exclude-features default --each-feature" +powerset := "cargo hack --workspace --exclude-features default --feature-powerset" [private] default: @@ -16,8 +17,7 @@ bench: cargo bench --workspace --features full build: - cargo build --workspace --features full - cargo build --workspace --no-default-features + {{ each }} build comb: {{ python }} contrib/git_filter.py --fast-fail --env NEXTEST_PROFILE=fail-fast develop HEAD -- just test lint @@ -44,11 +44,8 @@ preview: {{ python }} docs/build_docs.py preview test: - cargo clippy --all-targets --no-default-features -- -D warnings - cargo clippy --all-targets --features full -- -D warnings - cargo clippy --all-targets --features full,portable -- -D warnings - cargo hack clippy --ignore-private --feature-powerset --exclude-features default -- -D warnings - {{ nextest }} --workspace --features full - cargo test --doc --workspace --features full + {{ powerset }} clippy --all-targets -- -D warnings + {{ each }} nextest run --config-file .cargo/nextest.toml --no-tests=warn + {{ each }} test --doc cargo doc --workspace --no-deps --features full - cargo test --workspace --features full --benches + {{ each }} test --benches diff --git a/docs/dev/getting_started.md b/docs/dev/getting_started.md index a4f791902..a1bcf8752 100644 --- a/docs/dev/getting_started.md +++ b/docs/dev/getting_started.md @@ -10,7 +10,7 @@ > [!WARNING] > Some platforms offer Rust through their package manager. Depending on the maintainer policy, this version may be > well out of date relative to `stable`. `base-sdk` is only validated against its specified minimum supported Rust -> version (MSRV), its pinned `nightly` and `stable`. +> version (MSRV) and `stable`. Its pinned `nightly` serves development and linting only. > > Anything outside that set is untested and may result in unexpected behaviour. From e659d41ec7f2d237c8643d3dc1d1069dabb61826 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Thu, 15 Oct 2026 04:17:04 +0530 Subject: [PATCH 27/27] pow%doc: document features and required target features --- pkgs/pow/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pkgs/pow/README.md b/pkgs/pow/README.md index 83d6d2d3d..07beeb4d9 100644 --- a/pkgs/pow/README.md +++ b/pkgs/pow/README.md @@ -2,3 +2,29 @@ [![dash-pow](https://img.shields.io/codecov/c/github/dashpay/base-sdk/develop?flag=dash-pow&style=flat&logo=codecov&logoColor=white&label=pow)](https://app.codecov.io/github/dashpay/base-sdk/tree/develop/pkgs%2Fpow) # dash-pow + +This crate supplies the X11 proof-of-work hash chain used by the Dash Base SDK. + +## Features + +This crate is `#![no_std]` + `alloc` by default. + +| Feature | Default | Description | +| ------- | ------- | ----------- | +| `full` | | Enables all features and `std` support. | +| `portable` | | Allows builds without the target features listed below, using software AES. | +| `std` | | Enables `std` support. | + +## Target features + +Without `portable`, builds require the following target features. The repository's `.cargo/config.toml` sets them for +in-tree builds. + +| Architecture | Target features | +| ------------ | --------------- | +| `aarch64` | `neon`, `aes` | +| `wasm32` | `simd128` | +| `x86_64` | `x86-64-v3`, `aes` | + +Builds without `portable` use hardware AES with no runtime detection. Such a binary faults with an illegal instruction +on CPUs that lack AES.