Skip to content

Commit 2dc13bf

Browse files
authored
keccak: enable asm backend for p1600 (#68)
1 parent a3a4e01 commit 2dc13bf

3 files changed

Lines changed: 25 additions & 11 deletions

File tree

keccak/benches/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
extern crate keccak;
55
extern crate test;
66

7-
use keccak::{f1600, f200, f400, f800};
7+
use keccak::{f1600, f200, f400, f800, p1600};
88

99
macro_rules! impl_bench {
1010
($name:ident, $fn:ident, $type:expr) => {
@@ -21,6 +21,18 @@ impl_bench!(b_f400, f400, 0u16);
2121
impl_bench!(b_f800, f800, 0u32);
2222
impl_bench!(b_f1600, f1600, 0u64);
2323

24+
#[bench]
25+
fn b_p1600_24(b: &mut test::Bencher) {
26+
let mut data = [0u64; 25];
27+
b.iter(|| p1600(&mut data, 24));
28+
}
29+
30+
#[bench]
31+
fn b_p1600_16(b: &mut test::Bencher) {
32+
let mut data = [0u64; 25];
33+
b.iter(|| p1600(&mut data, 16));
34+
}
35+
2436
#[cfg(feature = "simd")]
2537
mod simd {
2638
use keccak::simd::{f1600x2, f1600x4, f1600x8, u64x2, u64x4, u64x8};

keccak/src/armv8.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/// Keccak-f1600 on ARMv8.4-A with FEAT_SHA3.
1+
/// Keccak-p1600 on ARMv8.4-A with FEAT_SHA3.
22
///
33
/// See p. K12.2.2 p. 11,749 of the ARM Reference manual.
44
/// Adapted from the Keccak-f1600 implementation in the XKCP/K12.
55
/// see <https://github.com/XKCP/K12/blob/df6a21e6d1f34c1aa36e8d702540899c97dba5a0/lib/ARMv8Asha3/KeccakP-1600-ARMv8Asha3.S#L69>
66
#[target_feature(enable = "sha3")]
7-
pub unsafe fn f1600_armv8_sha3_asm(state: &mut [u64; 25]) {
7+
pub unsafe fn p1600_armv8_sha3_asm(state: &mut [u64; 25], round_count: usize) {
88
core::arch::asm!("
99
// Read state
1010
ld1.1d {{ v0- v3}}, [x0], #32
@@ -16,11 +16,9 @@ pub unsafe fn f1600_armv8_sha3_asm(state: &mut [u64; 25]) {
1616
ld1.1d {{v24}}, [x0]
1717
sub x0, x0, #192
1818
19-
// Loop 24 rounds
2019
// NOTE: This loop actually computes two f1600 functions in
2120
// parallel, in both the lower and the upper 64-bit of the
2221
// 128-bit registers v0-v24.
23-
mov x8, #24
2422
0: sub x8, x8, #1
2523
2624
// Theta Calculations
@@ -115,7 +113,8 @@ pub unsafe fn f1600_armv8_sha3_asm(state: &mut [u64; 25]) {
115113
st1.1d {{v24}}, [x0]
116114
",
117115
in("x0") state.as_mut_ptr(),
118-
in("x1") crate::RC.as_ptr(),
116+
in("x1") crate::RC[24-round_count..].as_ptr(),
117+
in("x8") round_count,
119118
clobber_abi("C"),
120119
options(nostack)
121120
);
@@ -185,9 +184,9 @@ mod tests {
185184
];
186185

187186
let mut state = [0u64; 25];
188-
unsafe { f1600_armv8_sha3_asm(&mut state) };
187+
unsafe { p1600_armv8_sha3_asm(&mut state, 24) };
189188
assert_eq!(state, state_first);
190-
unsafe { f1600_armv8_sha3_asm(&mut state) };
189+
unsafe { p1600_armv8_sha3_asm(&mut state, 24) };
191190
assert_eq!(state, state_second);
192191
}
193192
}

keccak/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ impl_lanesize!(u64, 24, |rc: u64| { rc });
161161

162162
macro_rules! impl_keccak {
163163
($pname:ident, $fname:ident, $type:ty) => {
164-
165164
/// Keccak-p sponge function
166165
pub fn $pname(state: &mut [$type; PLEN], round_count: usize) {
167166
keccak_p(state, round_count);
@@ -184,14 +183,18 @@ impl_keccak!(p1600, f1600, u64);
184183
/// Keccak-p[1600, rc] permutation.
185184
#[cfg(all(target_arch = "aarch64", feature = "asm"))]
186185
pub fn p1600(state: &mut [u64; PLEN], round_count: usize) {
187-
keccak_p(state, round_count);
186+
if armv8_sha3_intrinsics::get() {
187+
unsafe { armv8::p1600_armv8_sha3_asm(state, round_count) }
188+
} else {
189+
keccak_p(state, round_count);
190+
}
188191
}
189192

190193
/// Keccak-f[1600] permutation.
191194
#[cfg(all(target_arch = "aarch64", feature = "asm"))]
192195
pub fn f1600(state: &mut [u64; PLEN]) {
193196
if armv8_sha3_intrinsics::get() {
194-
unsafe { armv8::f1600_armv8_sha3_asm(state) }
197+
unsafe { armv8::p1600_armv8_sha3_asm(state, 24) }
195198
} else {
196199
keccak_p(state, u64::KECCAK_F_ROUND_COUNT);
197200
}

0 commit comments

Comments
 (0)