Debian Codegraph @

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

raw
1 use super::*;
2 use crate::guts::{
3 count_high, count_low, final_block, flag_word, input_debug_asserts, Finalize, LastNode, Stride,
4 };
5
6 // G is the mixing function, called eight times per round in the compression
7 // function. V is the 16-word state vector of the compression function, usually
8 // described as a 4x4 matrix. A, B, C, and D are the mixing indices, set by the
9 // caller first to the four columns of V, and then to its four diagonals. X and
10 // Y are words of input, chosen by the caller according to the message
11 // schedule, SIGMA.
12 #[inline(always)]
13 fn g(v: &mut [Word; 16], a: usize, b: usize, c: usize, d: usize, x: Word, y: Word) {
14 v[a] = v[a].wrapping_add(v[b]).wrapping_add(x);
15 v[d] = (v[d] ^ v[a]).rotate_right(32);
16 v[c] = v[c].wrapping_add(v[d]);
17 v[b] = (v[b] ^ v[c]).rotate_right(24);
18 v[a] = v[a].wrapping_add(v[b]).wrapping_add(y);
19 v[d] = (v[d] ^ v[a]).rotate_right(16);
20 v[c] = v[c].wrapping_add(v[d]);
21 v[b] = (v[b] ^ v[c]).rotate_right(63);
22 }
23
24 // This is too much inlining for some small chips like ARM Cortex-M0, so the
25 // uninline_portable feature is provided to disable it.
26 #[cfg_attr(not(feature = "uninline_portable"), inline(always))]
27 fn round(r: usize, m: &[Word; 16], v: &mut [Word; 16]) {
28 // Select the message schedule based on the round.
29 let s = SIGMA[r];
30
31 // Mix the columns.
32 g(v, 0, 4, 8, 12, m[s[0] as usize], m[s[1] as usize]);
33 g(v, 1, 5, 9, 13, m[s[2] as usize], m[s[3] as usize]);
34 g(v, 2, 6, 10, 14, m[s[4] as usize], m[s[5] as usize]);
35 g(v, 3, 7, 11, 15, m[s[6] as usize], m[s[7] as usize]);
36
37 // Mix the rows.
38 g(v, 0, 5, 10, 15, m[s[8] as usize], m[s[9] as usize]);
39 g(v, 1, 6, 11, 12, m[s[10] as usize], m[s[11] as usize]);
40 g(v, 2, 7, 8, 13, m[s[12] as usize], m[s[13] as usize]);
41 g(v, 3, 4, 9, 14, m[s[14] as usize], m[s[15] as usize]);
42 }
43
44 #[inline(always)]
45 fn compress_block(
46 block: &[u8; BLOCKBYTES],
47 words: &mut [Word; 8],
48 count: Count,
49 last_block: Word,
50 last_node: Word,
51 ) {
52 // Initialize the compression state.
53 let mut v = [
54 words[0],
55 words[1],
56 words[2],
57 words[3],
58 words[4],
59 words[5],
60 words[6],
61 words[7],
62 IV[0],
63 IV[1],
64 IV[2],
65 IV[3],
66 IV[4] ^ count_low(count),
67 IV[5] ^ count_high(count),
68 IV[6] ^ last_block,
69 IV[7] ^ last_node,
70 ];
71
72 // Parse the message bytes as ints in little endian order.
73 const W: usize = size_of::<Word>();
74 let msg_refs: (
75 &[u8; W],
76 &[u8; W],
77 &[u8; W],
78 &[u8; W],
79 &[u8; W],
80 &[u8; W],
81 &[u8; W],
82 &[u8; W],
83 &[u8; W],
84 &[u8; W],
85 &[u8; W],
86 &[u8; W],
87 &[u8; W],
88 &[u8; W],
89 &[u8; W],
90 &[u8; W],
91 ) = (
92 (&block[0 * W..][..W]).try_into().unwrap(),
93 (&block[1 * W..][..W]).try_into().unwrap(),
94 (&block[2 * W..][..W]).try_into().unwrap(),
95 (&block[3 * W..][..W]).try_into().unwrap(),
96 (&block[4 * W..][..W]).try_into().unwrap(),
97 (&block[5 * W..][..W]).try_into().unwrap(),
98 (&block[6 * W..][..W]).try_into().unwrap(),
99 (&block[7 * W..][..W]).try_into().unwrap(),
100 (&block[8 * W..][..W]).try_into().unwrap(),
101 (&block[9 * W..][..W]).try_into().unwrap(),
102 (&block[10 * W..][..W]).try_into().unwrap(),
103 (&block[11 * W..][..W]).try_into().unwrap(),
104 (&block[12 * W..][..W]).try_into().unwrap(),
105 (&block[13 * W..][..W]).try_into().unwrap(),
106 (&block[14 * W..][..W]).try_into().unwrap(),
107 (&block[15 * W..][..W]).try_into().unwrap(),
108 );
109 let m = [
110 Word::from_le_bytes(*msg_refs.0),
111 Word::from_le_bytes(*msg_refs.1),
112 Word::from_le_bytes(*msg_refs.2),
113 Word::from_le_bytes(*msg_refs.3),
114 Word::from_le_bytes(*msg_refs.4),
115 Word::from_le_bytes(*msg_refs.5),
116 Word::from_le_bytes(*msg_refs.6),
117 Word::from_le_bytes(*msg_refs.7),
118 Word::from_le_bytes(*msg_refs.8),
119 Word::from_le_bytes(*msg_refs.9),
120 Word::from_le_bytes(*msg_refs.10),
121 Word::from_le_bytes(*msg_refs.11),
122 Word::from_le_bytes(*msg_refs.12),
123 Word::from_le_bytes(*msg_refs.13),
124 Word::from_le_bytes(*msg_refs.14),
125 Word::from_le_bytes(*msg_refs.15),
126 ];
127
128 round(0, &m, &mut v);
129 round(1, &m, &mut v);
130 round(2, &m, &mut v);
131 round(3, &m, &mut v);
132 round(4, &m, &mut v);
133 round(5, &m, &mut v);
134 round(6, &m, &mut v);
135 round(7, &m, &mut v);
136 round(8, &m, &mut v);
137 round(9, &m, &mut v);
138 round(10, &m, &mut v);
139 round(11, &m, &mut v);
140
141 words[0] ^= v[0] ^ v[8];
142 words[1] ^= v[1] ^ v[9];
143 words[2] ^= v[2] ^ v[10];
144 words[3] ^= v[3] ^ v[11];
145 words[4] ^= v[4] ^ v[12];
146 words[5] ^= v[5] ^ v[13];
147 words[6] ^= v[6] ^ v[14];
148 words[7] ^= v[7] ^ v[15];
149 }
150
151 pub fn compress1_loop(
152 input: &[u8],
153 words: &mut [Word; 8],
154 mut count: Count,
155 last_node: LastNode,
156 finalize: Finalize,
157 stride: Stride,
158 ) {
159 input_debug_asserts(input, finalize);
160
161 let mut local_words = *words;
162
163 let mut fin_offset = input.len().saturating_sub(1);
164 fin_offset -= fin_offset % stride.padded_blockbytes();
165 let mut buf = [0; BLOCKBYTES];
166 let (fin_block, fin_len, _) = final_block(input, fin_offset, &mut buf, stride);
167 let fin_last_block = flag_word(finalize.yes());
168 let fin_last_node = flag_word(finalize.yes() && last_node.yes());
169
170 let mut offset = 0;
171 loop {
172 let block;
173 let count_delta;
174 let last_block;
175 let last_node;
176 if offset == fin_offset {
177 block = fin_block;
178 count_delta = fin_len;
179 last_block = fin_last_block;
180 last_node = fin_last_node;
181 } else {
182 block = (&input[offset..][..BLOCKBYTES]).try_into().unwrap();
183 count_delta = BLOCKBYTES;
184 last_block = flag_word(false);
185 last_node = flag_word(false);
186 };
187
188 count = count.wrapping_add(count_delta as Count);
189 compress_block(block, &mut local_words, count, last_block, last_node);
190
191 // Check for termination before bumping the offset, to avoid overflow.
192 if offset == fin_offset {
193 break;
194 }
195
196 offset += stride.padded_blockbytes();
197 }
198
199 *words = local_words;
200 }