ripemd/
c128.rs

1use core::convert::TryInto;
2
3pub const DIGEST_BUF_LEN: usize = 4;
4pub const WORK_BUF_LEN: usize = 16;
5
6pub const H0: [u32; DIGEST_BUF_LEN] = [0x6745_2301, 0xefcd_ab89, 0x98ba_dcfe, 0x1032_5476];
7
8macro_rules! round(
9    ($a:expr, $b:expr, $c:expr, $d:expr,
10     $x:expr, $bits:expr, $add:expr, $round:expr) => ({
11        $a = $a.wrapping_add($round).wrapping_add($x).wrapping_add($add);
12        $a = $a.rotate_left($bits);
13    });
14);
15
16macro_rules! process_block(
17    ($h:ident, $data:expr,
18     $( round1: h_ordering $f0:expr, $f1:expr, $f2:expr, $f3:expr;
19                data_index $data_index1:expr; roll_shift $bits1:expr )*;
20     $( round2: h_ordering $g0:expr, $g1:expr, $g2:expr, $g3:expr;
21                data_index $data_index2:expr; roll_shift $bits2:expr )*;
22     $( round3: h_ordering $h0:expr, $h1:expr, $h2:expr, $h3:expr;
23                data_index $data_index3:expr; roll_shift $bits3:expr )*;
24     $( round4: h_ordering $i0:expr, $i1:expr, $i2:expr, $i3:expr;
25                data_index $data_index4:expr; roll_shift $bits4:expr )*;
26     $( par_round1: h_ordering $pi0:expr, $pi1:expr, $pi2:expr, $pi3:expr;
27                    data_index $pdata_index1:expr; roll_shift $pbits1:expr )*;
28     $( par_round2: h_ordering $ph0:expr, $ph1:expr, $ph2:expr, $ph3:expr;
29                    data_index $pdata_index2:expr; roll_shift $pbits2:expr )*;
30     $( par_round3: h_ordering $pg0:expr, $pg1:expr, $pg2:expr, $pg3:expr;
31                    data_index $pdata_index3:expr; roll_shift $pbits3:expr )*;
32     $( par_round4: h_ordering $pf0:expr, $pf1:expr, $pf2:expr, $pf3:expr;
33                    data_index $pdata_index4:expr; roll_shift $pbits4:expr )*;
34    ) => ({
35        let mut bb = *$h;
36        let mut bbb = *$h;
37
38        // Round 1
39        $( round!(bb[$f0], bb[$f1], bb[$f2], bb[$f3],
40                  $data[$data_index1], $bits1, 0x0000_0000,
41                  bb[$f1] ^ bb[$f2] ^ bb[$f3]); )*
42
43        // Round 2
44        $( round!(bb[$g0], bb[$g1], bb[$g2], bb[$g3],
45                  $data[$data_index2], $bits2, 0x5a82_7999,
46                  (bb[$g1] & bb[$g2]) | (!bb[$g1] & bb[$g3])); )*
47
48        // Round 3
49        $( round!(bb[$h0], bb[$h1], bb[$h2], bb[$h3],
50                  $data[$data_index3], $bits3, 0x6ed9_eba1,
51                  (bb[$h1] | !bb[$h2]) ^ bb[$h3]); )*
52
53        // Round 4
54        $( round!(bb[$i0], bb[$i1], bb[$i2], bb[$i3],
55                  $data[$data_index4], $bits4, 0x8f1b_bcdc,
56                  (bb[$i1] & bb[$i3]) | (bb[$i2] & !bb[$i3])); )*
57
58        // Parallel rounds: these are the same as the previous five
59        // rounds except that the constants have changed, we work
60        // with the other buffer, and they are applied in reverse
61        // order.
62
63        // Parallel Round 1
64        $( round!(bbb[$pi0], bbb[$pi1], bbb[$pi2], bbb[$pi3],
65                  $data[$pdata_index1], $pbits1, 0x50a2_8be6,
66                  (bbb[$pi1] & bbb[$pi3]) | (bbb[$pi2] & !bbb[$pi3])); )*
67
68        // Parallel Round 2
69        $( round!(bbb[$ph0], bbb[$ph1], bbb[$ph2], bbb[$ph3],
70                  $data[$pdata_index2], $pbits2, 0x5c4d_d124,
71                  (bbb[$ph1] | !bbb[$ph2]) ^ bbb[$ph3]); )*
72
73        // Parallel Round 3
74        $( round!(bbb[$pg0], bbb[$pg1], bbb[$pg2], bbb[$pg3],
75                  $data[$pdata_index3], $pbits3, 0x6d70_3ef3,
76                  (bbb[$pg1] & bbb[$pg2]) | (!bbb[$pg1] & bbb[$pg3])); )*
77
78        // Parallel Round 4
79        $( round!(bbb[$pf0], bbb[$pf1], bbb[$pf2], bbb[$pf3],
80                  $data[$pdata_index4], $pbits4, 0x0000_0000,
81                  bbb[$pf1] ^ bbb[$pf2] ^ bbb[$pf3]); )*
82
83        // Combine results
84        bbb[3] = bbb[3].wrapping_add($h[1]).wrapping_add(bb[2]);
85        $h[1]   = $h[2].wrapping_add(bb[3]).wrapping_add(bbb[0]);
86        $h[2]   = $h[3].wrapping_add(bb[0]).wrapping_add(bbb[1]);
87        $h[3]   = $h[0].wrapping_add(bb[1]).wrapping_add(bbb[2]);
88        $h[0]   =                 bbb[3];
89    });
90);
91
92pub fn compress(h: &mut [u32; DIGEST_BUF_LEN], data: &[u8; 64]) {
93    let mut w = [0u32; WORK_BUF_LEN];
94    for (o, chunk) in w.iter_mut().zip(data.chunks_exact(4)) {
95        *o = u32::from_le_bytes(chunk.try_into().unwrap());
96    }
97    process_block!(h, w[..],
98    // Round 1
99        round1: h_ordering 0, 1, 2, 3; data_index  0; roll_shift 11
100        round1: h_ordering 3, 0, 1, 2; data_index  1; roll_shift 14
101        round1: h_ordering 2, 3, 0, 1; data_index  2; roll_shift 15
102        round1: h_ordering 1, 2, 3, 0; data_index  3; roll_shift 12
103        round1: h_ordering 0, 1, 2, 3; data_index  4; roll_shift  5
104        round1: h_ordering 3, 0, 1, 2; data_index  5; roll_shift  8
105        round1: h_ordering 2, 3, 0, 1; data_index  6; roll_shift  7
106        round1: h_ordering 1, 2, 3, 0; data_index  7; roll_shift  9
107        round1: h_ordering 0, 1, 2, 3; data_index  8; roll_shift 11
108        round1: h_ordering 3, 0, 1, 2; data_index  9; roll_shift 13
109        round1: h_ordering 2, 3, 0, 1; data_index 10; roll_shift 14
110        round1: h_ordering 1, 2, 3, 0; data_index 11; roll_shift 15
111        round1: h_ordering 0, 1, 2, 3; data_index 12; roll_shift  6
112        round1: h_ordering 3, 0, 1, 2; data_index 13; roll_shift  7
113        round1: h_ordering 2, 3, 0, 1; data_index 14; roll_shift  9
114        round1: h_ordering 1, 2, 3, 0; data_index 15; roll_shift  8;
115
116    // Round 2
117        round2: h_ordering 0, 1, 2, 3; data_index  7; roll_shift  7
118        round2: h_ordering 3, 0, 1, 2; data_index  4; roll_shift  6
119        round2: h_ordering 2, 3, 0, 1; data_index 13; roll_shift  8
120        round2: h_ordering 1, 2, 3, 0; data_index  1; roll_shift 13
121        round2: h_ordering 0, 1, 2, 3; data_index 10; roll_shift 11
122        round2: h_ordering 3, 0, 1, 2; data_index  6; roll_shift  9
123        round2: h_ordering 2, 3, 0, 1; data_index 15; roll_shift  7
124        round2: h_ordering 1, 2, 3, 0; data_index  3; roll_shift 15
125        round2: h_ordering 0, 1, 2, 3; data_index 12; roll_shift  7
126        round2: h_ordering 3, 0, 1, 2; data_index  0; roll_shift 12
127        round2: h_ordering 2, 3, 0, 1; data_index  9; roll_shift 15
128        round2: h_ordering 1, 2, 3, 0; data_index  5; roll_shift  9
129        round2: h_ordering 0, 1, 2, 3; data_index  2; roll_shift 11
130        round2: h_ordering 3, 0, 1, 2; data_index 14; roll_shift  7
131        round2: h_ordering 2, 3, 0, 1; data_index 11; roll_shift 13
132        round2: h_ordering 1, 2, 3, 0; data_index  8; roll_shift 12;
133
134    // Round 3
135        round3: h_ordering 0, 1, 2, 3; data_index  3; roll_shift 11
136        round3: h_ordering 3, 0, 1, 2; data_index 10; roll_shift 13
137        round3: h_ordering 2, 3, 0, 1; data_index 14; roll_shift  6
138        round3: h_ordering 1, 2, 3, 0; data_index  4; roll_shift  7
139        round3: h_ordering 0, 1, 2, 3; data_index  9; roll_shift 14
140        round3: h_ordering 3, 0, 1, 2; data_index 15; roll_shift  9
141        round3: h_ordering 2, 3, 0, 1; data_index  8; roll_shift 13
142        round3: h_ordering 1, 2, 3, 0; data_index  1; roll_shift 15
143        round3: h_ordering 0, 1, 2, 3; data_index  2; roll_shift 14
144        round3: h_ordering 3, 0, 1, 2; data_index  7; roll_shift  8
145        round3: h_ordering 2, 3, 0, 1; data_index  0; roll_shift 13
146        round3: h_ordering 1, 2, 3, 0; data_index  6; roll_shift  6
147        round3: h_ordering 0, 1, 2, 3; data_index 13; roll_shift  5
148        round3: h_ordering 3, 0, 1, 2; data_index 11; roll_shift 12
149        round3: h_ordering 2, 3, 0, 1; data_index  5; roll_shift  7
150        round3: h_ordering 1, 2, 3, 0; data_index 12; roll_shift  5;
151
152    // Round 4
153        round4: h_ordering 0, 1, 2, 3; data_index  1; roll_shift 11
154        round4: h_ordering 3, 0, 1, 2; data_index  9; roll_shift 12
155        round4: h_ordering 2, 3, 0, 1; data_index 11; roll_shift 14
156        round4: h_ordering 1, 2, 3, 0; data_index 10; roll_shift 15
157        round4: h_ordering 0, 1, 2, 3; data_index  0; roll_shift 14
158        round4: h_ordering 3, 0, 1, 2; data_index  8; roll_shift 15
159        round4: h_ordering 2, 3, 0, 1; data_index 12; roll_shift  9
160        round4: h_ordering 1, 2, 3, 0; data_index  4; roll_shift  8
161        round4: h_ordering 0, 1, 2, 3; data_index 13; roll_shift  9
162        round4: h_ordering 3, 0, 1, 2; data_index  3; roll_shift 14
163        round4: h_ordering 2, 3, 0, 1; data_index  7; roll_shift  5
164        round4: h_ordering 1, 2, 3, 0; data_index 15; roll_shift  6
165        round4: h_ordering 0, 1, 2, 3; data_index 14; roll_shift  8
166        round4: h_ordering 3, 0, 1, 2; data_index  5; roll_shift  6
167        round4: h_ordering 2, 3, 0, 1; data_index  6; roll_shift  5
168        round4: h_ordering 1, 2, 3, 0; data_index  2; roll_shift 12;
169
170    // Parallel Round 1
171        par_round1: h_ordering 0, 1, 2, 3; data_index  5; roll_shift  8
172        par_round1: h_ordering 3, 0, 1, 2; data_index 14; roll_shift  9
173        par_round1: h_ordering 2, 3, 0, 1; data_index  7; roll_shift  9
174        par_round1: h_ordering 1, 2, 3, 0; data_index  0; roll_shift 11
175        par_round1: h_ordering 0, 1, 2, 3; data_index  9; roll_shift 13
176        par_round1: h_ordering 3, 0, 1, 2; data_index  2; roll_shift 15
177        par_round1: h_ordering 2, 3, 0, 1; data_index 11; roll_shift 15
178        par_round1: h_ordering 1, 2, 3, 0; data_index  4; roll_shift  5
179        par_round1: h_ordering 0, 1, 2, 3; data_index 13; roll_shift  7
180        par_round1: h_ordering 3, 0, 1, 2; data_index  6; roll_shift  7
181        par_round1: h_ordering 2, 3, 0, 1; data_index 15; roll_shift  8
182        par_round1: h_ordering 1, 2, 3, 0; data_index  8; roll_shift 11
183        par_round1: h_ordering 0, 1, 2, 3; data_index  1; roll_shift 14
184        par_round1: h_ordering 3, 0, 1, 2; data_index 10; roll_shift 14
185        par_round1: h_ordering 2, 3, 0, 1; data_index  3; roll_shift 12
186        par_round1: h_ordering 1, 2, 3, 0; data_index 12; roll_shift  6;
187
188    // Parallel Round 2
189        par_round2: h_ordering 0, 1, 2, 3; data_index  6; roll_shift  9
190        par_round2: h_ordering 3, 0, 1, 2; data_index 11; roll_shift 13
191        par_round2: h_ordering 2, 3, 0, 1; data_index  3; roll_shift 15
192        par_round2: h_ordering 1, 2, 3, 0; data_index  7; roll_shift  7
193        par_round2: h_ordering 0, 1, 2, 3; data_index  0; roll_shift 12
194        par_round2: h_ordering 3, 0, 1, 2; data_index 13; roll_shift  8
195        par_round2: h_ordering 2, 3, 0, 1; data_index  5; roll_shift  9
196        par_round2: h_ordering 1, 2, 3, 0; data_index 10; roll_shift 11
197        par_round2: h_ordering 0, 1, 2, 3; data_index 14; roll_shift  7
198        par_round2: h_ordering 3, 0, 1, 2; data_index 15; roll_shift  7
199        par_round2: h_ordering 2, 3, 0, 1; data_index  8; roll_shift 12
200        par_round2: h_ordering 1, 2, 3, 0; data_index 12; roll_shift  7
201        par_round2: h_ordering 0, 1, 2, 3; data_index  4; roll_shift  6
202        par_round2: h_ordering 3, 0, 1, 2; data_index  9; roll_shift 15
203        par_round2: h_ordering 2, 3, 0, 1; data_index  1; roll_shift 13
204        par_round2: h_ordering 1, 2, 3, 0; data_index  2; roll_shift 11;
205
206    // Parallel Round 3
207        par_round3: h_ordering 0, 1, 2, 3; data_index 15; roll_shift  9
208        par_round3: h_ordering 3, 0, 1, 2; data_index  5; roll_shift  7
209        par_round3: h_ordering 2, 3, 0, 1; data_index  1; roll_shift 15
210        par_round3: h_ordering 1, 2, 3, 0; data_index  3; roll_shift 11
211        par_round3: h_ordering 0, 1, 2, 3; data_index  7; roll_shift  8
212        par_round3: h_ordering 3, 0, 1, 2; data_index 14; roll_shift  6
213        par_round3: h_ordering 2, 3, 0, 1; data_index  6; roll_shift  6
214        par_round3: h_ordering 1, 2, 3, 0; data_index  9; roll_shift 14
215        par_round3: h_ordering 0, 1, 2, 3; data_index 11; roll_shift 12
216        par_round3: h_ordering 3, 0, 1, 2; data_index  8; roll_shift 13
217        par_round3: h_ordering 2, 3, 0, 1; data_index 12; roll_shift  5
218        par_round3: h_ordering 1, 2, 3, 0; data_index  2; roll_shift 14
219        par_round3: h_ordering 0, 1, 2, 3; data_index 10; roll_shift 13
220        par_round3: h_ordering 3, 0, 1, 2; data_index  0; roll_shift 13
221        par_round3: h_ordering 2, 3, 0, 1; data_index  4; roll_shift  7
222        par_round3: h_ordering 1, 2, 3, 0; data_index 13; roll_shift  5;
223
224    // Parallel Round 4
225        par_round4: h_ordering 0, 1, 2, 3; data_index  8; roll_shift 15
226        par_round4: h_ordering 3, 0, 1, 2; data_index  6; roll_shift  5
227        par_round4: h_ordering 2, 3, 0, 1; data_index  4; roll_shift  8
228        par_round4: h_ordering 1, 2, 3, 0; data_index  1; roll_shift 11
229        par_round4: h_ordering 0, 1, 2, 3; data_index  3; roll_shift 14
230        par_round4: h_ordering 3, 0, 1, 2; data_index 11; roll_shift 14
231        par_round4: h_ordering 2, 3, 0, 1; data_index 15; roll_shift  6
232        par_round4: h_ordering 1, 2, 3, 0; data_index  0; roll_shift 14
233        par_round4: h_ordering 0, 1, 2, 3; data_index  5; roll_shift  6
234        par_round4: h_ordering 3, 0, 1, 2; data_index 12; roll_shift  9
235        par_round4: h_ordering 2, 3, 0, 1; data_index  2; roll_shift 12
236        par_round4: h_ordering 1, 2, 3, 0; data_index 13; roll_shift  9
237        par_round4: h_ordering 0, 1, 2, 3; data_index  9; roll_shift 12
238        par_round4: h_ordering 3, 0, 1, 2; data_index  7; roll_shift  5
239        par_round4: h_ordering 2, 3, 0, 1; data_index 10; roll_shift 15
240        par_round4: h_ordering 1, 2, 3, 0; data_index 14; roll_shift  8;
241    );
242}