Debian Codegraph @

third_party/rust/keccak/src/lib.rs

raw
1 //! Keccak [sponge function](https://en.wikipedia.org/wiki/Sponge_function).
2 //!
3 //! If you are looking for SHA-3 hash functions take a look at [`sha3`][1] and
4 //! [`tiny-keccak`][2] crates.
5 //!
6 //! To disable loop unrolling (e.g. for constraint targets) use `no_unroll`
7 //! feature.
8 //!
9 //! ```
10 //! // Test vectors are from KeccakCodePackage
11 //! let mut data = [0u64; 25];
12 //!
13 //! keccak::f1600(&mut data);
14 //! assert_eq!(data, [
15 //! 0xF1258F7940E1DDE7, 0x84D5CCF933C0478A, 0xD598261EA65AA9EE, 0xBD1547306F80494D,
16 //! 0x8B284E056253D057, 0xFF97A42D7F8E6FD4, 0x90FEE5A0A44647C4, 0x8C5BDA0CD6192E76,
17 //! 0xAD30A6F71B19059C, 0x30935AB7D08FFC64, 0xEB5AA93F2317D635, 0xA9A6E6260D712103,
18 //! 0x81A57C16DBCF555F, 0x43B831CD0347C826, 0x01F22F1A11A5569F, 0x05E5635A21D9AE61,
19 //! 0x64BEFEF28CC970F2, 0x613670957BC46611, 0xB87C5A554FD00ECB, 0x8C3EE88A1CCF32C8,
20 //! 0x940C7922AE3A2614, 0x1841F924A2C509E4, 0x16F53526E70465C2, 0x75F644E97F30A13B,
21 //! 0xEAF1FF7B5CECA249,
22 //! ]);
23 //!
24 //! keccak::f1600(&mut data);
25 //! assert_eq!(data, [
26 //! 0x2D5C954DF96ECB3C, 0x6A332CD07057B56D, 0x093D8D1270D76B6C, 0x8A20D9B25569D094,
27 //! 0x4F9C4F99E5E7F156, 0xF957B9A2DA65FB38, 0x85773DAE1275AF0D, 0xFAF4F247C3D810F7,
28 //! 0x1F1B9EE6F79A8759, 0xE4FECC0FEE98B425, 0x68CE61B6B9CE68A1, 0xDEEA66C4BA8F974F,
29 //! 0x33C43D836EAFB1F5, 0xE00654042719DBD9, 0x7CF8A9F009831265, 0xFD5449A6BF174743,
30 //! 0x97DDAD33D8994B40, 0x48EAD5FC5D0BE774, 0xE3B8C8EE55B7B03C, 0x91A0226E649E42E9,
31 //! 0x900E3129E7BADD7B, 0x202A9EC5FAA3CCE8, 0x5B3402464E1C3DB6, 0x609F4E62A44C1059,
32 //! 0x20D06CD26A8FBF5C,
33 //! ]);
34 //! ```
35 //!
36 //! [1]: https://docs.rs/sha3
37 //! [2]: https://docs.rs/tiny-keccak
38
39 #![no_std]
40 #![cfg_attr(docsrs, feature(doc_auto_cfg))]
41 #![cfg_attr(feature = "simd", feature(portable_simd))]
42 #![doc(
43 html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
44 html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
45 )]
46 #![allow(non_upper_case_globals)]
47 #![warn(
48 clippy::mod_module_files,
49 clippy::unwrap_used,
50 missing_docs,
51 rust_2018_idioms,
52 unused_lifetimes,
53 unused_qualifications
54 )]
55
56 use core::{
57 convert::TryInto,
58 fmt::Debug,
59 mem::size_of,
60 ops::{BitAnd, BitAndAssign, BitXor, BitXorAssign, Not},
61 };
62
63 #[rustfmt::skip]
64 mod unroll;
65
66 #[cfg(all(target_arch = "aarch64", feature = "asm"))]
67 mod armv8;
68
69 #[cfg(all(target_arch = "aarch64", feature = "asm"))]
70 cpufeatures::new!(armv8_sha3_intrinsics, "sha3");
71
72 const PLEN: usize = 25;
73
74 const RHO: [u32; 24] = [
75 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44,
76 ];
77
78 const PI: [usize; 24] = [
79 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1,
80 ];
81
82 const RC: [u64; 24] = [
83 0x0000000000000001,
84 0x0000000000008082,
85 0x800000000000808a,
86 0x8000000080008000,
87 0x000000000000808b,
88 0x0000000080000001,
89 0x8000000080008081,
90 0x8000000000008009,
91 0x000000000000008a,
92 0x0000000000000088,
93 0x0000000080008009,
94 0x000000008000000a,
95 0x000000008000808b,
96 0x800000000000008b,
97 0x8000000000008089,
98 0x8000000000008003,
99 0x8000000000008002,
100 0x8000000000000080,
101 0x000000000000800a,
102 0x800000008000000a,
103 0x8000000080008081,
104 0x8000000000008080,
105 0x0000000080000001,
106 0x8000000080008008,
107 ];
108
109 /// Keccak is a permutation over an array of lanes which comprise the sponge
110 /// construction.
111 pub trait LaneSize:
112 Copy
113 + Clone
114 + Debug
115 + Default
116 + PartialEq
117 + BitAndAssign
118 + BitAnd<Output = Self>
119 + BitXorAssign
120 + BitXor<Output = Self>
121 + Not<Output = Self>
122 {
123 /// Number of rounds of the Keccak-f permutation.
124 const KECCAK_F_ROUND_COUNT: usize;
125
126 /// Truncate function.
127 fn truncate_rc(rc: u64) -> Self;
128
129 /// Rotate left function.
130 fn rotate_left(self, n: u32) -> Self;
131 }
132
133 macro_rules! impl_lanesize {
134 ($type:ty, $round:expr, $truncate:expr) => {
135 impl LaneSize for $type {
136 const KECCAK_F_ROUND_COUNT: usize = $round;
137
138 fn truncate_rc(rc: u64) -> Self {
139 $truncate(rc)
140 }
141
142 fn rotate_left(self, n: u32) -> Self {
143 self.rotate_left(n)
144 }
145 }
146 };
147 }
148
149 impl_lanesize!(u8, 18, |rc: u64| { rc.to_le_bytes()[0] });
150 impl_lanesize!(u16, 20, |rc: u64| {
151 let tmp = rc.to_le_bytes();
152 #[allow(clippy::unwrap_used)]
153 Self::from_le_bytes(tmp[..size_of::<Self>()].try_into().unwrap())
154 });
155 impl_lanesize!(u32, 22, |rc: u64| {
156 let tmp = rc.to_le_bytes();
157 #[allow(clippy::unwrap_used)]
158 Self::from_le_bytes(tmp[..size_of::<Self>()].try_into().unwrap())
159 });
160 impl_lanesize!(u64, 24, |rc: u64| { rc });
161
162 macro_rules! impl_keccak {
163 ($pname:ident, $fname:ident, $type:ty) => {
164
165 /// Keccak-p sponge function
166 pub fn $pname(state: &mut [$type; PLEN], round_count: usize) {
167 keccak_p(state, round_count);
168 }
169
170 /// Keccak-f sponge function
171 pub fn $fname(state: &mut [$type; PLEN]) {
172 keccak_p(state, <$type>::KECCAK_F_ROUND_COUNT);
173 }
174 };
175 }
176
177 impl_keccak!(p200, f200, u8);
178 impl_keccak!(p400, f400, u16);
179 impl_keccak!(p800, f800, u32);
180
181 #[cfg(not(all(target_arch = "aarch64", feature = "asm")))]
182 impl_keccak!(p1600, f1600, u64);
183
184 /// Keccak-p[1600, rc] permutation.
185 #[cfg(all(target_arch = "aarch64", feature = "asm"))]
186 pub fn p1600(state: &mut [u64; PLEN], round_count: usize) {
187 keccak_p(state, round_count);
188 }
189
190 /// Keccak-f[1600] permutation.
191 #[cfg(all(target_arch = "aarch64", feature = "asm"))]
192 pub fn f1600(state: &mut [u64; PLEN]) {
193 if armv8_sha3_intrinsics::get() {
194 unsafe { armv8::f1600_armv8_sha3_asm(state) }
195 } else {
196 keccak_p(state, u64::KECCAK_F_ROUND_COUNT);
197 }
198 }
199
200 #[cfg(feature = "simd")]
201 /// SIMD implementations for Keccak-f1600 sponge function
202 pub mod simd {
203 use crate::{keccak_p, LaneSize, PLEN};
204 pub use core::simd::{u64x2, u64x4, u64x8};
205
206 macro_rules! impl_lanesize_simd_u64xn {
207 ($type:ty) => {
208 impl LaneSize for $type {
209 const KECCAK_F_ROUND_COUNT: usize = 24;
210
211 fn truncate_rc(rc: u64) -> Self {
212 Self::splat(rc)
213 }
214
215 fn rotate_left(self, n: u32) -> Self {
216 self << Self::splat(n.into()) | self >> Self::splat((64 - n).into())
217 }
218 }
219 };
220 }
221
222 impl_lanesize_simd_u64xn!(u64x2);
223 impl_lanesize_simd_u64xn!(u64x4);
224 impl_lanesize_simd_u64xn!(u64x8);
225
226 impl_keccak!(p1600x2, f1600x2, u64x2);
227 impl_keccak!(p1600x4, f1600x4, u64x4);
228 impl_keccak!(p1600x8, f1600x8, u64x8);
229 }
230
231 #[allow(unused_assignments)]
232 /// Generic Keccak-p sponge function
233 pub fn keccak_p<L: LaneSize>(state: &mut [L; PLEN], round_count: usize) {
234 if round_count > L::KECCAK_F_ROUND_COUNT {
235 panic!("A round_count greater than KECCAK_F_ROUND_COUNT is not supported!");
236 }
237
238 // https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf#page=25
239 // "the rounds of KECCAK-p[b, nr] match the last rounds of KECCAK-f[b]"
240 let round_consts = &RC[(L::KECCAK_F_ROUND_COUNT - round_count)..L::KECCAK_F_ROUND_COUNT];
241
242 // not unrolling this loop results in a much smaller function, plus
243 // it positively influences performance due to the smaller load on I-cache
244 for &rc in round_consts {
245 let mut array = [L::default(); 5];
246
247 // Theta
248 unroll5!(x, {
249 unroll5!(y, {
250 array[x] ^= state[5 * y + x];
251 });
252 });
253
254 unroll5!(x, {
255 unroll5!(y, {
256 let t1 = array[(x + 4) % 5];
257 let t2 = array[(x + 1) % 5].rotate_left(1);
258 state[5 * y + x] ^= t1 ^ t2;
259 });
260 });
261
262 // Rho and pi
263 let mut last = state[1];
264 unroll24!(x, {
265 array[0] = state[PI[x]];
266 state[PI[x]] = last.rotate_left(RHO[x]);
267 last = array[0];
268 });
269
270 // Chi
271 unroll5!(y_step, {
272 let y = 5 * y_step;
273
274 unroll5!(x, {
275 array[x] = state[y + x];
276 });
277
278 unroll5!(x, {
279 let t1 = !array[(x + 1) % 5];
280 let t2 = array[(x + 2) % 5];
281 state[y + x] = array[x] ^ (t1 & t2);
282 });
283 });
284
285 // Iota
286 state[0] ^= L::truncate_rc(rc);
287 }
288 }
289
290 #[cfg(test)]
291 mod tests {
292 use crate::{keccak_p, LaneSize, PLEN};
293
294 fn keccak_f<L: LaneSize>(state_first: [L; PLEN], state_second: [L; PLEN]) {
295 let mut state = [L::default(); PLEN];
296
297 keccak_p(&mut state, L::KECCAK_F_ROUND_COUNT);
298 assert_eq!(state, state_first);
299
300 keccak_p(&mut state, L::KECCAK_F_ROUND_COUNT);
301 assert_eq!(state, state_second);
302 }
303
304 #[test]
305 fn keccak_f200() {
306 // Test vectors are copied from XKCP (eXtended Keccak Code Package)
307 // https://github.com/XKCP/XKCP/blob/master/tests/TestVectors/KeccakF-200-IntermediateValues.txt
308 let state_first = [
309 0x3C, 0x28, 0x26, 0x84, 0x1C, 0xB3, 0x5C, 0x17, 0x1E, 0xAA, 0xE9, 0xB8, 0x11, 0x13,
310 0x4C, 0xEA, 0xA3, 0x85, 0x2C, 0x69, 0xD2, 0xC5, 0xAB, 0xAF, 0xEA,
311 ];
312 let state_second = [
313 0x1B, 0xEF, 0x68, 0x94, 0x92, 0xA8, 0xA5, 0x43, 0xA5, 0x99, 0x9F, 0xDB, 0x83, 0x4E,
314 0x31, 0x66, 0xA1, 0x4B, 0xE8, 0x27, 0xD9, 0x50, 0x40, 0x47, 0x9E,
315 ];
316
317 keccak_f::<u8>(state_first, state_second);
318 }
319
320 #[test]
321 fn keccak_f400() {
322 // Test vectors are copied from XKCP (eXtended Keccak Code Package)
323 // https://github.com/XKCP/XKCP/blob/master/tests/TestVectors/KeccakF-400-IntermediateValues.txt
324 let state_first = [
325 0x09F5, 0x40AC, 0x0FA9, 0x14F5, 0xE89F, 0xECA0, 0x5BD1, 0x7870, 0xEFF0, 0xBF8F, 0x0337,
326 0x6052, 0xDC75, 0x0EC9, 0xE776, 0x5246, 0x59A1, 0x5D81, 0x6D95, 0x6E14, 0x633E, 0x58EE,
327 0x71FF, 0x714C, 0xB38E,
328 ];
329 let state_second = [
330 0xE537, 0xD5D6, 0xDBE7, 0xAAF3, 0x9BC7, 0xCA7D, 0x86B2, 0xFDEC, 0x692C, 0x4E5B, 0x67B1,
331 0x15AD, 0xA7F7, 0xA66F, 0x67FF, 0x3F8A, 0x2F99, 0xE2C2, 0x656B, 0x5F31, 0x5BA6, 0xCA29,
332 0xC224, 0xB85C, 0x097C,
333 ];
334
335 keccak_f::<u16>(state_first, state_second);
336 }
337
338 #[test]
339 fn keccak_f800() {
340 // Test vectors are copied from XKCP (eXtended Keccak Code Package)
341 // https://github.com/XKCP/XKCP/blob/master/tests/TestVectors/KeccakF-800-IntermediateValues.txt
342 let state_first = [
343 0xE531D45D, 0xF404C6FB, 0x23A0BF99, 0xF1F8452F, 0x51FFD042, 0xE539F578, 0xF00B80A7,
344 0xAF973664, 0xBF5AF34C, 0x227A2424, 0x88172715, 0x9F685884, 0xB15CD054, 0x1BF4FC0E,
345 0x6166FA91, 0x1A9E599A, 0xA3970A1F, 0xAB659687, 0xAFAB8D68, 0xE74B1015, 0x34001A98,
346 0x4119EFF3, 0x930A0E76, 0x87B28070, 0x11EFE996,
347 ];
348 let state_second = [
349 0x75BF2D0D, 0x9B610E89, 0xC826AF40, 0x64CD84AB, 0xF905BDD6, 0xBC832835, 0x5F8001B9,
350 0x15662CCE, 0x8E38C95E, 0x701FE543, 0x1B544380, 0x89ACDEFF, 0x51EDB5DE, 0x0E9702D9,
351 0x6C19AA16, 0xA2913EEE, 0x60754E9A, 0x9819063C, 0xF4709254, 0xD09F9084, 0x772DA259,
352 0x1DB35DF7, 0x5AA60162, 0x358825D5, 0xB3783BAB,
353 ];
354
355 keccak_f::<u32>(state_first, state_second);
356 }
357
358 #[test]
359 fn keccak_f1600() {
360 // Test vectors are copied from XKCP (eXtended Keccak Code Package)
361 // https://github.com/XKCP/XKCP/blob/master/tests/TestVectors/KeccakF-1600-IntermediateValues.txt
362 let state_first = [
363 0xF1258F7940E1DDE7,
364 0x84D5CCF933C0478A,
365 0xD598261EA65AA9EE,
366 0xBD1547306F80494D,
367 0x8B284E056253D057,
368 0xFF97A42D7F8E6FD4,
369 0x90FEE5A0A44647C4,
370 0x8C5BDA0CD6192E76,
371 0xAD30A6F71B19059C,
372 0x30935AB7D08FFC64,
373 0xEB5AA93F2317D635,
374 0xA9A6E6260D712103,
375 0x81A57C16DBCF555F,
376 0x43B831CD0347C826,
377 0x01F22F1A11A5569F,
378 0x05E5635A21D9AE61,
379 0x64BEFEF28CC970F2,
380 0x613670957BC46611,
381 0xB87C5A554FD00ECB,
382 0x8C3EE88A1CCF32C8,
383 0x940C7922AE3A2614,
384 0x1841F924A2C509E4,
385 0x16F53526E70465C2,
386 0x75F644E97F30A13B,
387 0xEAF1FF7B5CECA249,
388 ];
389 let state_second = [
390 0x2D5C954DF96ECB3C,
391 0x6A332CD07057B56D,
392 0x093D8D1270D76B6C,
393 0x8A20D9B25569D094,
394 0x4F9C4F99E5E7F156,
395 0xF957B9A2DA65FB38,
396 0x85773DAE1275AF0D,
397 0xFAF4F247C3D810F7,
398 0x1F1B9EE6F79A8759,
399 0xE4FECC0FEE98B425,
400 0x68CE61B6B9CE68A1,
401 0xDEEA66C4BA8F974F,
402 0x33C43D836EAFB1F5,
403 0xE00654042719DBD9,
404 0x7CF8A9F009831265,
405 0xFD5449A6BF174743,
406 0x97DDAD33D8994B40,
407 0x48EAD5FC5D0BE774,
408 0xE3B8C8EE55B7B03C,
409 0x91A0226E649E42E9,
410 0x900E3129E7BADD7B,
411 0x202A9EC5FAA3CCE8,
412 0x5B3402464E1C3DB6,
413 0x609F4E62A44C1059,
414 0x20D06CD26A8FBF5C,
415 ];
416
417 keccak_f::<u64>(state_first, state_second);
418 }
419
420 #[cfg(feature = "simd")]
421 mod simd {
422 use super::keccak_f;
423 use core::simd::{u64x2, u64x4, u64x8};
424
425 macro_rules! impl_keccak_f1600xn {
426 ($name:ident, $type:ty) => {
427 #[test]
428 fn $name() {
429 // Test vectors are copied from XKCP (eXtended Keccak Code Package)
430 // https://github.com/XKCP/XKCP/blob/master/tests/TestVectors/KeccakF-1600-IntermediateValues.txt
431 let state_first = [
432 <$type>::splat(0xF1258F7940E1DDE7),
433 <$type>::splat(0x84D5CCF933C0478A),
434 <$type>::splat(0xD598261EA65AA9EE),
435 <$type>::splat(0xBD1547306F80494D),
436 <$type>::splat(0x8B284E056253D057),
437 <$type>::splat(0xFF97A42D7F8E6FD4),
438 <$type>::splat(0x90FEE5A0A44647C4),
439 <$type>::splat(0x8C5BDA0CD6192E76),
440 <$type>::splat(0xAD30A6F71B19059C),
441 <$type>::splat(0x30935AB7D08FFC64),
442 <$type>::splat(0xEB5AA93F2317D635),
443 <$type>::splat(0xA9A6E6260D712103),
444 <$type>::splat(0x81A57C16DBCF555F),
445 <$type>::splat(0x43B831CD0347C826),
446 <$type>::splat(0x01F22F1A11A5569F),
447 <$type>::splat(0x05E5635A21D9AE61),
448 <$type>::splat(0x64BEFEF28CC970F2),
449 <$type>::splat(0x613670957BC46611),
450 <$type>::splat(0xB87C5A554FD00ECB),
451 <$type>::splat(0x8C3EE88A1CCF32C8),
452 <$type>::splat(0x940C7922AE3A2614),
453 <$type>::splat(0x1841F924A2C509E4),
454 <$type>::splat(0x16F53526E70465C2),
455 <$type>::splat(0x75F644E97F30A13B),
456 <$type>::splat(0xEAF1FF7B5CECA249),
457 ];
458 let state_second = [
459 <$type>::splat(0x2D5C954DF96ECB3C),
460 <$type>::splat(0x6A332CD07057B56D),
461 <$type>::splat(0x093D8D1270D76B6C),
462 <$type>::splat(0x8A20D9B25569D094),
463 <$type>::splat(0x4F9C4F99E5E7F156),
464 <$type>::splat(0xF957B9A2DA65FB38),
465 <$type>::splat(0x85773DAE1275AF0D),
466 <$type>::splat(0xFAF4F247C3D810F7),
467 <$type>::splat(0x1F1B9EE6F79A8759),
468 <$type>::splat(0xE4FECC0FEE98B425),
469 <$type>::splat(0x68CE61B6B9CE68A1),
470 <$type>::splat(0xDEEA66C4BA8F974F),
471 <$type>::splat(0x33C43D836EAFB1F5),
472 <$type>::splat(0xE00654042719DBD9),
473 <$type>::splat(0x7CF8A9F009831265),
474 <$type>::splat(0xFD5449A6BF174743),
475 <$type>::splat(0x97DDAD33D8994B40),
476 <$type>::splat(0x48EAD5FC5D0BE774),
477 <$type>::splat(0xE3B8C8EE55B7B03C),
478 <$type>::splat(0x91A0226E649E42E9),
479 <$type>::splat(0x900E3129E7BADD7B),
480 <$type>::splat(0x202A9EC5FAA3CCE8),
481 <$type>::splat(0x5B3402464E1C3DB6),
482 <$type>::splat(0x609F4E62A44C1059),
483 <$type>::splat(0x20D06CD26A8FBF5C),
484 ];
485
486 keccak_f::<$type>(state_first, state_second);
487 }
488 };
489 }
490
491 impl_keccak_f1600xn!(keccak_f1600x2, u64x2);
492 impl_keccak_f1600xn!(keccak_f1600x4, u64x4);
493 impl_keccak_f1600xn!(keccak_f1600x8, u64x8);
494 }
495 }