Debian Codegraph @

debian/rust-vendor/blake3/src/portable.rs

raw
1 use crate::{
2 BLOCK_LEN, CVBytes, CVWords, IV, IncrementCounter, MSG_SCHEDULE, OUT_LEN, counter_high,
3 counter_low,
4 };
5
6 #[inline(always)]
7 fn g(state: &mut [u32; 16], a: usize, b: usize, c: usize, d: usize, x: u32, y: u32) {
8 state[a] = state[a].wrapping_add(state[b]).wrapping_add(x);
9 state[d] = (state[d] ^ state[a]).rotate_right(16);
10 state[c] = state[c].wrapping_add(state[d]);
11 state[b] = (state[b] ^ state[c]).rotate_right(12);
12 state[a] = state[a].wrapping_add(state[b]).wrapping_add(y);
13 state[d] = (state[d] ^ state[a]).rotate_right(8);
14 state[c] = state[c].wrapping_add(state[d]);
15 state[b] = (state[b] ^ state[c]).rotate_right(7);
16 }
17
18 #[inline(always)]
19 fn round(state: &mut [u32; 16], msg: &[u32; 16], round: usize) {
20 // Select the message schedule based on the round.
21 let schedule = MSG_SCHEDULE[round];
22
23 // Mix the columns.
24 g(state, 0, 4, 8, 12, msg[schedule[0]], msg[schedule[1]]);
25 g(state, 1, 5, 9, 13, msg[schedule[2]], msg[schedule[3]]);
26 g(state, 2, 6, 10, 14, msg[schedule[4]], msg[schedule[5]]);
27 g(state, 3, 7, 11, 15, msg[schedule[6]], msg[schedule[7]]);
28
29 // Mix the diagonals.
30 g(state, 0, 5, 10, 15, msg[schedule[8]], msg[schedule[9]]);
31 g(state, 1, 6, 11, 12, msg[schedule[10]], msg[schedule[11]]);
32 g(state, 2, 7, 8, 13, msg[schedule[12]], msg[schedule[13]]);
33 g(state, 3, 4, 9, 14, msg[schedule[14]], msg[schedule[15]]);
34 }
35
36 #[inline(always)]
37 fn compress_pre(
38 cv: &CVWords,
39 block: &[u8; BLOCK_LEN],
40 block_len: u8,
41 counter: u64,
42 flags: u8,
43 ) -> [u32; 16] {
44 let block_words = crate::platform::words_from_le_bytes_64(block);
45
46 let mut state = [
47 cv[0],
48 cv[1],
49 cv[2],
50 cv[3],
51 cv[4],
52 cv[5],
53 cv[6],
54 cv[7],
55 IV[0],
56 IV[1],
57 IV[2],
58 IV[3],
59 counter_low(counter),
60 counter_high(counter),
61 block_len as u32,
62 flags as u32,
63 ];
64
65 round(&mut state, &block_words, 0);
66 round(&mut state, &block_words, 1);
67 round(&mut state, &block_words, 2);
68 round(&mut state, &block_words, 3);
69 round(&mut state, &block_words, 4);
70 round(&mut state, &block_words, 5);
71 round(&mut state, &block_words, 6);
72
73 state
74 }
75
76 pub fn compress_in_place(
77 cv: &mut CVWords,
78 block: &[u8; BLOCK_LEN],
79 block_len: u8,
80 counter: u64,
81 flags: u8,
82 ) {
83 let state = compress_pre(cv, block, block_len, counter, flags);
84
85 cv[0] = state[0] ^ state[8];
86 cv[1] = state[1] ^ state[9];
87 cv[2] = state[2] ^ state[10];
88 cv[3] = state[3] ^ state[11];
89 cv[4] = state[4] ^ state[12];
90 cv[5] = state[5] ^ state[13];
91 cv[6] = state[6] ^ state[14];
92 cv[7] = state[7] ^ state[15];
93 }
94
95 pub fn compress_xof(
96 cv: &CVWords,
97 block: &[u8; BLOCK_LEN],
98 block_len: u8,
99 counter: u64,
100 flags: u8,
101 ) -> [u8; 64] {
102 let mut state = compress_pre(cv, block, block_len, counter, flags);
103 state[0] ^= state[8];
104 state[1] ^= state[9];
105 state[2] ^= state[10];
106 state[3] ^= state[11];
107 state[4] ^= state[12];
108 state[5] ^= state[13];
109 state[6] ^= state[14];
110 state[7] ^= state[15];
111 state[8] ^= cv[0];
112 state[9] ^= cv[1];
113 state[10] ^= cv[2];
114 state[11] ^= cv[3];
115 state[12] ^= cv[4];
116 state[13] ^= cv[5];
117 state[14] ^= cv[6];
118 state[15] ^= cv[7];
119 crate::platform::le_bytes_from_words_64(&state)
120 }
121
122 pub fn hash1<const N: usize>(
123 input: &[u8; N],
124 key: &CVWords,
125 counter: u64,
126 flags: u8,
127 flags_start: u8,
128 flags_end: u8,
129 out: &mut CVBytes,
130 ) {
131 debug_assert_eq!(N % BLOCK_LEN, 0, "uneven blocks");
132 let mut cv = *key;
133 let mut block_flags = flags | flags_start;
134 let mut slice = &input[..];
135 while slice.len() >= BLOCK_LEN {
136 if slice.len() == BLOCK_LEN {
137 block_flags |= flags_end;
138 }
139 compress_in_place(
140 &mut cv,
141 (&slice[..BLOCK_LEN]).try_into().unwrap(),
142 BLOCK_LEN as u8,
143 counter,
144 block_flags,
145 );
146 block_flags = flags;
147 slice = &slice[BLOCK_LEN..];
148 }
149 *out = crate::platform::le_bytes_from_words_32(&cv);
150 }
151
152 pub fn hash_many<const N: usize>(
153 inputs: &[&[u8; N]],
154 key: &CVWords,
155 mut counter: u64,
156 increment_counter: IncrementCounter,
157 flags: u8,
158 flags_start: u8,
159 flags_end: u8,
160 out: &mut [u8],
161 ) {
162 debug_assert!(out.len() >= inputs.len() * OUT_LEN, "out too short");
163 for (&input, output) in inputs.iter().zip(out.chunks_exact_mut(OUT_LEN)) {
164 hash1(
165 input,
166 key,
167 counter,
168 flags,
169 flags_start,
170 flags_end,
171 (&mut output[..OUT_LEN]).try_into().unwrap(),
172 );
173 if increment_counter.yes() {
174 counter += 1;
175 }
176 }
177 }
178
179 #[cfg(test)]
180 pub mod test {
181 use super::*;
182
183 // This is basically testing the portable implementation against itself,
184 // but it also checks that compress_in_place and compress_xof are
185 // consistent. And there are tests against the reference implementation and
186 // against hardcoded test vectors elsewhere.
187 #[test]
188 fn test_compress() {
189 crate::test::test_compress_fn(compress_in_place, compress_xof);
190 }
191
192 // Ditto.
193 #[test]
194 fn test_hash_many() {
195 crate::test::test_hash_many_fn(hash_many, hash_many);
196 }
197 }