Debian Codegraph @

debian/rust-vendor/sm3/src/compress.rs

raw
1 #![allow(clippy::many_single_char_names, clippy::too_many_arguments)]
2 use crate::consts::T32;
3
4 #[inline(always)]
5 fn ff1(x: u32, y: u32, z: u32) -> u32 {
6 x ^ y ^ z
7 }
8
9 #[inline(always)]
10 fn ff2(x: u32, y: u32, z: u32) -> u32 {
11 (x & y) | (x & z) | (y & z)
12 }
13
14 #[inline(always)]
15 fn gg1(x: u32, y: u32, z: u32) -> u32 {
16 x ^ y ^ z
17 }
18
19 #[inline(always)]
20 fn gg2(x: u32, y: u32, z: u32) -> u32 {
21 // This line is equivalent to `(x & y) | (!x & z)`, but executes faster
22 (y ^ z) & x ^ z
23 }
24
25 #[inline(always)]
26 fn p0(x: u32) -> u32 {
27 x ^ x.rotate_left(9) ^ x.rotate_left(17)
28 }
29
30 #[inline(always)]
31 fn p1(x: u32) -> u32 {
32 x ^ x.rotate_left(15) ^ x.rotate_left(23)
33 }
34
35 #[inline(always)]
36 fn w1(x: &[u32; 16], i: usize) -> u32 {
37 x[i & 0x0f]
38 }
39
40 #[inline(always)]
41 fn w2(x: &mut [u32; 16], i: usize) -> u32 {
42 let tw = w1(x, i) ^ w1(x, i - 9) ^ w1(x, i - 3).rotate_left(15);
43 let tw = p1(tw) ^ w1(x, i - 13).rotate_left(7) ^ w1(x, i - 6);
44 x[i & 0x0f] = tw;
45 tw
46 }
47
48 #[inline(always)]
49 fn t(i: usize) -> u32 {
50 T32[i]
51 }
52
53 fn sm3_round1(
54 a: u32,
55 b: u32,
56 c: u32,
57 d: u32,
58 e: u32,
59 f: u32,
60 g: u32,
61 h: u32,
62 t: u32,
63 w1: u32,
64 w2: u32,
65 ) -> [u32; 8] {
66 let ss1 = (a.rotate_left(12).wrapping_add(e).wrapping_add(t)).rotate_left(7);
67 let ss2 = ss1 ^ a.rotate_left(12);
68
69 let d = d
70 .wrapping_add(ff1(a, b, c))
71 .wrapping_add(ss2)
72 .wrapping_add(w1 ^ w2);
73 let h = h
74 .wrapping_add(gg1(e, f, g))
75 .wrapping_add(ss1)
76 .wrapping_add(w1);
77 let b = b.rotate_left(9);
78 let f = f.rotate_left(19);
79 let h = p0(h);
80
81 [a, b, c, d, e, f, g, h]
82 }
83
84 fn sm3_round2(
85 a: u32,
86 b: u32,
87 c: u32,
88 d: u32,
89 e: u32,
90 f: u32,
91 g: u32,
92 h: u32,
93 t: u32,
94 w1: u32,
95 w2: u32,
96 ) -> [u32; 8] {
97 let ss1 = (a.rotate_left(12).wrapping_add(e).wrapping_add(t)).rotate_left(7);
98 let ss2 = ss1 ^ a.rotate_left(12);
99
100 let d = d
101 .wrapping_add(ff2(a, b, c))
102 .wrapping_add(ss2)
103 .wrapping_add(w1 ^ w2);
104 let h = h
105 .wrapping_add(gg2(e, f, g))
106 .wrapping_add(ss1)
107 .wrapping_add(w1);
108 let b = b.rotate_left(9);
109 let f = f.rotate_left(19);
110 let h = p0(h);
111
112 [a, b, c, d, e, f, g, h]
113 }
114
115 macro_rules! R1 {
116 (
117 $a: ident, $b: ident, $c: ident, $d: ident,
118 $e: ident, $f: ident, $g: ident, $h: ident,
119 $t: expr, $w1: expr, $w2: expr
120 ) => {{
121 let out = sm3_round1($a, $b, $c, $d, $e, $f, $g, $h, $t, $w1, $w2);
122 $a = out[0];
123 $b = out[1];
124 $c = out[2];
125 $d = out[3];
126 $e = out[4];
127 $f = out[5];
128 $g = out[6];
129 $h = out[7];
130 }};
131 }
132
133 macro_rules! R2 {
134 (
135 $a: ident, $b: ident, $c: ident, $d: ident,
136 $e: ident, $f: ident, $g: ident, $h: ident,
137 $t: expr, $w1: expr, $w2: expr
138 ) => {{
139 let out = sm3_round2($a, $b, $c, $d, $e, $f, $g, $h, $t, $w1, $w2);
140 $a = out[0];
141 $b = out[1];
142 $c = out[2];
143 $d = out[3];
144 $e = out[4];
145 $f = out[5];
146 $g = out[6];
147 $h = out[7];
148 }};
149 }
150
151 fn compress_u32(state: &mut [u32; 8], block: &[u32; 16]) {
152 let mut x: [u32; 16] = *block;
153
154 let mut a = state[0];
155 let mut b = state[1];
156 let mut c = state[2];
157 let mut d = state[3];
158 let mut e = state[4];
159 let mut f = state[5];
160 let mut g = state[6];
161 let mut h = state[7];
162
163 R1!(a, b, c, d, e, f, g, h, t(0), w1(&x, 0), w1(&x, 4));
164 R1!(d, a, b, c, h, e, f, g, t(1), w1(&x, 1), w1(&x, 5));
165 R1!(c, d, a, b, g, h, e, f, t(2), w1(&x, 2), w1(&x, 6));
166 R1!(b, c, d, a, f, g, h, e, t(3), w1(&x, 3), w1(&x, 7));
167 R1!(a, b, c, d, e, f, g, h, t(4), w1(&x, 4), w1(&x, 8));
168 R1!(d, a, b, c, h, e, f, g, t(5), w1(&x, 5), w1(&x, 9));
169 R1!(c, d, a, b, g, h, e, f, t(6), w1(&x, 6), w1(&x, 10));
170 R1!(b, c, d, a, f, g, h, e, t(7), w1(&x, 7), w1(&x, 11));
171 R1!(a, b, c, d, e, f, g, h, t(8), w1(&x, 8), w1(&x, 12));
172 R1!(d, a, b, c, h, e, f, g, t(9), w1(&x, 9), w1(&x, 13));
173 R1!(c, d, a, b, g, h, e, f, t(10), w1(&x, 10), w1(&x, 14));
174 R1!(b, c, d, a, f, g, h, e, t(11), w1(&x, 11), w1(&x, 15));
175 R1!(a, b, c, d, e, f, g, h, t(12), w1(&x, 12), w2(&mut x, 16));
176 R1!(d, a, b, c, h, e, f, g, t(13), w1(&x, 13), w2(&mut x, 17));
177 R1!(c, d, a, b, g, h, e, f, t(14), w1(&x, 14), w2(&mut x, 18));
178 R1!(b, c, d, a, f, g, h, e, t(15), w1(&x, 15), w2(&mut x, 19));
179 R2!(a, b, c, d, e, f, g, h, t(16), w1(&x, 16), w2(&mut x, 20));
180 R2!(d, a, b, c, h, e, f, g, t(17), w1(&x, 17), w2(&mut x, 21));
181 R2!(c, d, a, b, g, h, e, f, t(18), w1(&x, 18), w2(&mut x, 22));
182 R2!(b, c, d, a, f, g, h, e, t(19), w1(&x, 19), w2(&mut x, 23));
183 R2!(a, b, c, d, e, f, g, h, t(20), w1(&x, 20), w2(&mut x, 24));
184 R2!(d, a, b, c, h, e, f, g, t(21), w1(&x, 21), w2(&mut x, 25));
185 R2!(c, d, a, b, g, h, e, f, t(22), w1(&x, 22), w2(&mut x, 26));
186 R2!(b, c, d, a, f, g, h, e, t(23), w1(&x, 23), w2(&mut x, 27));
187 R2!(a, b, c, d, e, f, g, h, t(24), w1(&x, 24), w2(&mut x, 28));
188 R2!(d, a, b, c, h, e, f, g, t(25), w1(&x, 25), w2(&mut x, 29));
189 R2!(c, d, a, b, g, h, e, f, t(26), w1(&x, 26), w2(&mut x, 30));
190 R2!(b, c, d, a, f, g, h, e, t(27), w1(&x, 27), w2(&mut x, 31));
191 R2!(a, b, c, d, e, f, g, h, t(28), w1(&x, 28), w2(&mut x, 32));
192 R2!(d, a, b, c, h, e, f, g, t(29), w1(&x, 29), w2(&mut x, 33));
193 R2!(c, d, a, b, g, h, e, f, t(30), w1(&x, 30), w2(&mut x, 34));
194 R2!(b, c, d, a, f, g, h, e, t(31), w1(&x, 31), w2(&mut x, 35));
195 R2!(a, b, c, d, e, f, g, h, t(32), w1(&x, 32), w2(&mut x, 36));
196 R2!(d, a, b, c, h, e, f, g, t(33), w1(&x, 33), w2(&mut x, 37));
197 R2!(c, d, a, b, g, h, e, f, t(34), w1(&x, 34), w2(&mut x, 38));
198 R2!(b, c, d, a, f, g, h, e, t(35), w1(&x, 35), w2(&mut x, 39));
199 R2!(a, b, c, d, e, f, g, h, t(36), w1(&x, 36), w2(&mut x, 40));
200 R2!(d, a, b, c, h, e, f, g, t(37), w1(&x, 37), w2(&mut x, 41));
201 R2!(c, d, a, b, g, h, e, f, t(38), w1(&x, 38), w2(&mut x, 42));
202 R2!(b, c, d, a, f, g, h, e, t(39), w1(&x, 39), w2(&mut x, 43));
203 R2!(a, b, c, d, e, f, g, h, t(40), w1(&x, 40), w2(&mut x, 44));
204 R2!(d, a, b, c, h, e, f, g, t(41), w1(&x, 41), w2(&mut x, 45));
205 R2!(c, d, a, b, g, h, e, f, t(42), w1(&x, 42), w2(&mut x, 46));
206 R2!(b, c, d, a, f, g, h, e, t(43), w1(&x, 43), w2(&mut x, 47));
207 R2!(a, b, c, d, e, f, g, h, t(44), w1(&x, 44), w2(&mut x, 48));
208 R2!(d, a, b, c, h, e, f, g, t(45), w1(&x, 45), w2(&mut x, 49));
209 R2!(c, d, a, b, g, h, e, f, t(46), w1(&x, 46), w2(&mut x, 50));
210 R2!(b, c, d, a, f, g, h, e, t(47), w1(&x, 47), w2(&mut x, 51));
211 R2!(a, b, c, d, e, f, g, h, t(48), w1(&x, 48), w2(&mut x, 52));
212 R2!(d, a, b, c, h, e, f, g, t(49), w1(&x, 49), w2(&mut x, 53));
213 R2!(c, d, a, b, g, h, e, f, t(50), w1(&x, 50), w2(&mut x, 54));
214 R2!(b, c, d, a, f, g, h, e, t(51), w1(&x, 51), w2(&mut x, 55));
215 R2!(a, b, c, d, e, f, g, h, t(52), w1(&x, 52), w2(&mut x, 56));
216 R2!(d, a, b, c, h, e, f, g, t(53), w1(&x, 53), w2(&mut x, 57));
217 R2!(c, d, a, b, g, h, e, f, t(54), w1(&x, 54), w2(&mut x, 58));
218 R2!(b, c, d, a, f, g, h, e, t(55), w1(&x, 55), w2(&mut x, 59));
219 R2!(a, b, c, d, e, f, g, h, t(56), w1(&x, 56), w2(&mut x, 60));
220 R2!(d, a, b, c, h, e, f, g, t(57), w1(&x, 57), w2(&mut x, 61));
221 R2!(c, d, a, b, g, h, e, f, t(58), w1(&x, 58), w2(&mut x, 62));
222 R2!(b, c, d, a, f, g, h, e, t(59), w1(&x, 59), w2(&mut x, 63));
223 R2!(a, b, c, d, e, f, g, h, t(60), w1(&x, 60), w2(&mut x, 64));
224 R2!(d, a, b, c, h, e, f, g, t(61), w1(&x, 61), w2(&mut x, 65));
225 R2!(c, d, a, b, g, h, e, f, t(62), w1(&x, 62), w2(&mut x, 66));
226 R2!(b, c, d, a, f, g, h, e, t(63), w1(&x, 63), w2(&mut x, 67));
227
228 state[0] ^= a;
229 state[1] ^= b;
230 state[2] ^= c;
231 state[3] ^= d;
232 state[4] ^= e;
233 state[5] ^= f;
234 state[6] ^= g;
235 state[7] ^= h;
236 }
237
238 pub(crate) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
239 for block in blocks {
240 let mut w = [0u32; 16];
241 for (o, chunk) in w.iter_mut().zip(block.chunks_exact(4)) {
242 *o = u32::from_be_bytes(chunk.try_into().unwrap());
243 }
244 compress_u32(state, &w);
245 }
246 }