1#![allow(clippy::many_single_char_names, clippy::unreadable_literal)]
2use crate::consts::RC;
3
4#[inline(always)]
5fn op_f(w: u32, x: u32, y: u32, z: u32, m: u32, c: u32, s: u32) -> u32 {
6 ((x & y) | (!x & z))
7 .wrapping_add(w)
8 .wrapping_add(m)
9 .wrapping_add(c)
10 .rotate_left(s)
11 .wrapping_add(x)
12}
13#[inline(always)]
14fn op_g(w: u32, x: u32, y: u32, z: u32, m: u32, c: u32, s: u32) -> u32 {
15 ((x & z).wrapping_add(y & !z))
20 .wrapping_add(w)
21 .wrapping_add(m)
22 .wrapping_add(c)
23 .rotate_left(s)
24 .wrapping_add(x)
25}
26
27#[inline(always)]
28fn op_h(w: u32, x: u32, y: u32, z: u32, m: u32, c: u32, s: u32) -> u32 {
29 (x ^ y ^ z)
30 .wrapping_add(w)
31 .wrapping_add(m)
32 .wrapping_add(c)
33 .rotate_left(s)
34 .wrapping_add(x)
35}
36
37#[inline(always)]
38fn op_i(w: u32, x: u32, y: u32, z: u32, m: u32, c: u32, s: u32) -> u32 {
39 (y ^ (x | !z))
40 .wrapping_add(w)
41 .wrapping_add(m)
42 .wrapping_add(c)
43 .rotate_left(s)
44 .wrapping_add(x)
45}
46
47#[inline]
48fn compress_block(state: &mut [u32; 4], input: &[u8; 64]) {
49 let mut a = state[0];
50 let mut b = state[1];
51 let mut c = state[2];
52 let mut d = state[3];
53
54 let mut data = [0u32; 16];
55 for (o, chunk) in data.iter_mut().zip(input.chunks_exact(4)) {
56 *o = u32::from_le_bytes(chunk.try_into().unwrap());
57 }
58
59 a = op_f(a, b, c, d, data[0], RC[0], 7);
61 d = op_f(d, a, b, c, data[1], RC[1], 12);
62 c = op_f(c, d, a, b, data[2], RC[2], 17);
63 b = op_f(b, c, d, a, data[3], RC[3], 22);
64
65 a = op_f(a, b, c, d, data[4], RC[4], 7);
66 d = op_f(d, a, b, c, data[5], RC[5], 12);
67 c = op_f(c, d, a, b, data[6], RC[6], 17);
68 b = op_f(b, c, d, a, data[7], RC[7], 22);
69
70 a = op_f(a, b, c, d, data[8], RC[8], 7);
71 d = op_f(d, a, b, c, data[9], RC[9], 12);
72 c = op_f(c, d, a, b, data[10], RC[10], 17);
73 b = op_f(b, c, d, a, data[11], RC[11], 22);
74
75 a = op_f(a, b, c, d, data[12], RC[12], 7);
76 d = op_f(d, a, b, c, data[13], RC[13], 12);
77 c = op_f(c, d, a, b, data[14], RC[14], 17);
78 b = op_f(b, c, d, a, data[15], RC[15], 22);
79
80 a = op_g(a, b, c, d, data[1], RC[16], 5);
82 d = op_g(d, a, b, c, data[6], RC[17], 9);
83 c = op_g(c, d, a, b, data[11], RC[18], 14);
84 b = op_g(b, c, d, a, data[0], RC[19], 20);
85
86 a = op_g(a, b, c, d, data[5], RC[20], 5);
87 d = op_g(d, a, b, c, data[10], RC[21], 9);
88 c = op_g(c, d, a, b, data[15], RC[22], 14);
89 b = op_g(b, c, d, a, data[4], RC[23], 20);
90
91 a = op_g(a, b, c, d, data[9], RC[24], 5);
92 d = op_g(d, a, b, c, data[14], RC[25], 9);
93 c = op_g(c, d, a, b, data[3], RC[26], 14);
94 b = op_g(b, c, d, a, data[8], RC[27], 20);
95
96 a = op_g(a, b, c, d, data[13], RC[28], 5);
97 d = op_g(d, a, b, c, data[2], RC[29], 9);
98 c = op_g(c, d, a, b, data[7], RC[30], 14);
99 b = op_g(b, c, d, a, data[12], RC[31], 20);
100
101 a = op_h(a, b, c, d, data[5], RC[32], 4);
103 d = op_h(d, a, b, c, data[8], RC[33], 11);
104 c = op_h(c, d, a, b, data[11], RC[34], 16);
105 b = op_h(b, c, d, a, data[14], RC[35], 23);
106
107 a = op_h(a, b, c, d, data[1], RC[36], 4);
108 d = op_h(d, a, b, c, data[4], RC[37], 11);
109 c = op_h(c, d, a, b, data[7], RC[38], 16);
110 b = op_h(b, c, d, a, data[10], RC[39], 23);
111
112 a = op_h(a, b, c, d, data[13], RC[40], 4);
113 d = op_h(d, a, b, c, data[0], RC[41], 11);
114 c = op_h(c, d, a, b, data[3], RC[42], 16);
115 b = op_h(b, c, d, a, data[6], RC[43], 23);
116
117 a = op_h(a, b, c, d, data[9], RC[44], 4);
118 d = op_h(d, a, b, c, data[12], RC[45], 11);
119 c = op_h(c, d, a, b, data[15], RC[46], 16);
120 b = op_h(b, c, d, a, data[2], RC[47], 23);
121
122 a = op_i(a, b, c, d, data[0], RC[48], 6);
124 d = op_i(d, a, b, c, data[7], RC[49], 10);
125 c = op_i(c, d, a, b, data[14], RC[50], 15);
126 b = op_i(b, c, d, a, data[5], RC[51], 21);
127
128 a = op_i(a, b, c, d, data[12], RC[52], 6);
129 d = op_i(d, a, b, c, data[3], RC[53], 10);
130 c = op_i(c, d, a, b, data[10], RC[54], 15);
131 b = op_i(b, c, d, a, data[1], RC[55], 21);
132
133 a = op_i(a, b, c, d, data[8], RC[56], 6);
134 d = op_i(d, a, b, c, data[15], RC[57], 10);
135 c = op_i(c, d, a, b, data[6], RC[58], 15);
136 b = op_i(b, c, d, a, data[13], RC[59], 21);
137
138 a = op_i(a, b, c, d, data[4], RC[60], 6);
139 d = op_i(d, a, b, c, data[11], RC[61], 10);
140 c = op_i(c, d, a, b, data[2], RC[62], 15);
141 b = op_i(b, c, d, a, data[9], RC[63], 21);
142
143 state[0] = state[0].wrapping_add(a);
144 state[1] = state[1].wrapping_add(b);
145 state[2] = state[2].wrapping_add(c);
146 state[3] = state[3].wrapping_add(d);
147}
148
149#[inline]
150pub(super) fn compress(state: &mut [u32; 4], blocks: &[[u8; 64]]) {
151 for block in blocks {
152 compress_block(state, block)
153 }
154}