Debian Codegraph @

debian/rust-vendor/keccak/src/backends/aarch64_sha3.rs

raw
1 //! ARMv8 intrinsics-based backend.
2
3 // TODO(tarcieri): remove when MSRV 1.87
4 #![allow(unsafe_op_in_unsafe_fn)]
5
6 use crate::consts::{PLEN, RC};
7 use crate::types::Fn1600;
8 #[cfg(feature = "parallel")]
9 use crate::types::ParFn1600;
10
11 use core::{arch::aarch64::*, array};
12 #[cfg(feature = "parallel")]
13 use hybrid_array::{Array, typenum::U2};
14
15 /// AArch64 backend implemented using the SHA3 extension.
16 pub(crate) struct Backend;
17
18 impl super::Backend for Backend {
19 #[cfg(feature = "parallel")]
20 type ParSize1600 = U2;
21
22 #[inline]
23 fn get_p1600<const ROUNDS: usize>() -> Fn1600 {
24 // SAFETY: the backend is used only after required target feature checks
25 |state| unsafe { p1600_armv8_sha3(state, ROUNDS) }
26 }
27
28 #[cfg(feature = "parallel")]
29 #[inline]
30 fn get_par_p1600<const ROUNDS: usize>() -> ParFn1600<Self> {
31 // SAFETY: the backend is used only after required target feature checks
32 |Array(state)| unsafe { p1600_armv8_sha3_times2(state, ROUNDS) }
33 }
34 }
35
36 /// Keccak-p1600 on ARMv8.4-A with `FEAT_SHA3`.
37 ///
38 /// See p. K12.2.2 p. 11,749 of the ARM Reference manual.
39 /// Adapted from the Keccak-f1600 implementation in the XKCP/K12.
40 /// see <https://github.com/XKCP/K12/blob/df6a21e6d1f34c1aa36e8d702540899c97dba5a0/lib/ARMv8Asha3/KeccakP-1600-ARMv8Asha3.S#L69>
41 #[target_feature(enable = "sha3")]
42 unsafe fn p1600_armv8_sha3(state: &mut [u64; PLEN], round_count: usize) {
43 let mut s = [*state, Default::default()];
44 // SAFETY: both functions have the same safety invariants, namely they require the `sha3`
45 // target feature is available, and the caller is responsible for ensuring support
46 unsafe { p1600_armv8_sha3_times2(&mut s, round_count) };
47 *state = s[0];
48 }
49
50 /// Keccak-p1600 on ARMv8.4-A with `FEAT_SHA3` with support for 2 parallel states.
51 ///
52 /// See p. K12.2.2 p. 11,749 of the ARM Reference manual.
53 /// Adapted from the Keccak-f1600 implementation in the XKCP/K12.
54 ///
55 /// <https://github.com/XKCP/K12/blob/df6a21e/lib/ARMv8Asha3/KeccakP-1600-ARMv8Asha3.S#L69>
56 #[target_feature(enable = "sha3")]
57 unsafe fn p1600_armv8_sha3_times2(state: &mut [[u64; PLEN]; 2], round_count: usize) {
58 assert!(
59 round_count <= 24,
60 "invalid round count greater than 24: {round_count}",
61 );
62
63 let mut s: [uint64x2_t; PLEN] =
64 array::from_fn(|i| vcombine_u64(vcreate_u64(state[0][i]), vcreate_u64(state[1][i])));
65
66 for &rc in &RC[(24 - round_count)..] {
67 let (d0, d1, d2, d3, d4) = theta(&s);
68 let t = rho_pi(&s, d0, d1, d2, d3, d4);
69 s = chi_iota(&t, rc);
70 }
71
72 for i in 0..PLEN {
73 state[0][i] = vgetq_lane_u64::<0>(s[i]);
74 state[1][i] = vgetq_lane_u64::<1>(s[i]);
75 }
76 }
77
78 #[target_feature(enable = "sha3")]
79 unsafe fn theta(
80 s: &[uint64x2_t; 25],
81 ) -> (uint64x2_t, uint64x2_t, uint64x2_t, uint64x2_t, uint64x2_t) {
82 let c0 = veor3q_u64(s[0], s[5], veor3q_u64(s[10], s[15], s[20]));
83 let c1 = veor3q_u64(s[1], s[6], veor3q_u64(s[11], s[16], s[21]));
84 let c2 = veor3q_u64(s[2], s[7], veor3q_u64(s[12], s[17], s[22]));
85 let c3 = veor3q_u64(s[3], s[8], veor3q_u64(s[13], s[18], s[23]));
86 let c4 = veor3q_u64(s[4], s[9], veor3q_u64(s[14], s[19], s[24]));
87
88 let d0 = vrax1q_u64(c4, c1);
89 let d1 = vrax1q_u64(c0, c2);
90 let d2 = vrax1q_u64(c1, c3);
91 let d3 = vrax1q_u64(c2, c4);
92 let d4 = vrax1q_u64(c3, c0);
93
94 (d0, d1, d2, d3, d4)
95 }
96
97 #[target_feature(enable = "sha3")]
98 unsafe fn rho_pi(
99 s: &[uint64x2_t; 25],
100 d0: uint64x2_t,
101 d1: uint64x2_t,
102 d2: uint64x2_t,
103 d3: uint64x2_t,
104 d4: uint64x2_t,
105 ) -> [uint64x2_t; 25] {
106 let v0 = veorq_u64(s[0], d0);
107 let v25 = vxarq_u64::<63>(s[1], d1);
108 let v1 = vxarq_u64::<20>(s[6], d1);
109 let v6 = vxarq_u64::<44>(s[9], d4);
110 let v9 = vxarq_u64::<3>(s[22], d2);
111 let v22 = vxarq_u64::<25>(s[14], d4);
112 let v14 = vxarq_u64::<46>(s[20], d0);
113 let v26 = vxarq_u64::<2>(s[2], d2);
114 let v2 = vxarq_u64::<21>(s[12], d2);
115 let v12 = vxarq_u64::<39>(s[13], d3);
116 let v13 = vxarq_u64::<56>(s[19], d4);
117 let v19 = vxarq_u64::<8>(s[23], d3);
118 let v23 = vxarq_u64::<23>(s[15], d0);
119 let v15 = vxarq_u64::<37>(s[4], d4);
120 let v28 = vxarq_u64::<50>(s[24], d4);
121 let v24 = vxarq_u64::<62>(s[21], d1);
122 let v8 = vxarq_u64::<9>(s[8], d3);
123 let v4 = vxarq_u64::<19>(s[16], d1);
124 let v16 = vxarq_u64::<28>(s[5], d0);
125 let v5 = vxarq_u64::<36>(s[3], d3);
126 let v27 = vxarq_u64::<43>(s[18], d3);
127 let v3 = vxarq_u64::<49>(s[17], d2);
128 let v30 = vxarq_u64::<54>(s[11], d1);
129 let v31 = vxarq_u64::<58>(s[7], d2);
130 let v29 = vxarq_u64::<61>(s[10], d0);
131 [
132 v0, v25, v26, v5, v15, v16, v1, v31, v8, v6, v29, v30, v2, v12, v22, v23, v4, v3, v27, v13,
133 v14, v24, v9, v19, v28,
134 ]
135 }
136
137 #[target_feature(enable = "sha3")]
138 unsafe fn chi_iota(t: &[uint64x2_t; 25], rc: u64) -> [uint64x2_t; 25] {
139 let rc_v = vdupq_n_u64(rc);
140 let v20 = vbcaxq_u64(t[2], t[14], t[8]);
141 let v21 = vbcaxq_u64(t[8], t[15], t[14]);
142 let v22 = vbcaxq_u64(t[14], t[21], t[15]);
143 let v23 = vbcaxq_u64(t[15], t[2], t[21]);
144 let v24 = vbcaxq_u64(t[21], t[8], t[2]);
145 let v17 = vbcaxq_u64(t[11], t[23], t[17]);
146 let v18 = vbcaxq_u64(t[17], t[4], t[23]);
147 let v19 = vbcaxq_u64(t[23], t[5], t[4]);
148 let v15 = vbcaxq_u64(t[4], t[11], t[5]);
149 let v16 = vbcaxq_u64(t[5], t[17], t[11]);
150 let v10 = vbcaxq_u64(t[1], t[13], t[7]);
151 let v11 = vbcaxq_u64(t[7], t[19], t[13]);
152 let v12 = vbcaxq_u64(t[13], t[20], t[19]);
153 let v13 = vbcaxq_u64(t[19], t[1], t[20]);
154 let v14 = vbcaxq_u64(t[20], t[7], t[1]);
155 let v7 = vbcaxq_u64(t[10], t[22], t[16]);
156 let v8 = vbcaxq_u64(t[16], t[3], t[22]);
157 let v9 = vbcaxq_u64(t[22], t[9], t[3]);
158 let v5 = vbcaxq_u64(t[3], t[10], t[9]);
159 let v6 = vbcaxq_u64(t[9], t[16], t[10]);
160 let v3 = vbcaxq_u64(t[18], t[0], t[24]);
161 let v4 = vbcaxq_u64(t[24], t[6], t[0]);
162 let v0 = vbcaxq_u64(t[0], t[12], t[6]);
163 let v1 = vbcaxq_u64(t[6], t[18], t[12]);
164 let v2 = vbcaxq_u64(t[12], t[24], t[18]);
165 let v0_iota = veorq_u64(v0, rc_v);
166 [
167 v0_iota, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18,
168 v19, v20, v21, v22, v23, v24,
169 ]
170 }