bitcoin_hashes/
ripemd160.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! RIPEMD160 implementation.
4//!
5
6use core::ops::Index;
7use core::slice::SliceIndex;
8use core::{cmp, str};
9
10use crate::{FromSliceError, HashEngine as _};
11
12crate::internal_macros::hash_type! {
13    160,
14    false,
15    "Output of the RIPEMD160 hash function."
16}
17
18#[cfg(not(hashes_fuzz))]
19fn from_engine(mut e: HashEngine) -> Hash {
20    // pad buffer with a single 1-bit then all 0s, until there are exactly 8 bytes remaining
21    let data_len = e.length as u64;
22
23    let zeroes = [0; BLOCK_SIZE - 8];
24    e.input(&[0x80]);
25    if e.length % BLOCK_SIZE > zeroes.len() {
26        e.input(&zeroes);
27    }
28    let pad_length = zeroes.len() - (e.length % BLOCK_SIZE);
29    e.input(&zeroes[..pad_length]);
30    debug_assert_eq!(e.length % BLOCK_SIZE, zeroes.len());
31
32    e.input(&(8 * data_len).to_le_bytes());
33    debug_assert_eq!(e.length % BLOCK_SIZE, 0);
34
35    Hash(e.midstate())
36}
37
38#[cfg(hashes_fuzz)]
39fn from_engine(e: HashEngine) -> Hash {
40    let mut res = e.midstate();
41    res[0] ^= (e.length & 0xff) as u8;
42    Hash(res)
43}
44
45const BLOCK_SIZE: usize = 64;
46
47/// Engine to compute RIPEMD160 hash function.
48#[derive(Clone)]
49pub struct HashEngine {
50    buffer: [u8; BLOCK_SIZE],
51    h: [u32; 5],
52    length: usize,
53}
54
55impl Default for HashEngine {
56    fn default() -> Self {
57        HashEngine {
58            h: [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0],
59            length: 0,
60            buffer: [0; BLOCK_SIZE],
61        }
62    }
63}
64
65impl crate::HashEngine for HashEngine {
66    type MidState = [u8; 20];
67
68    #[cfg(not(hashes_fuzz))]
69    fn midstate(&self) -> [u8; 20] {
70        let mut ret = [0; 20];
71        for (val, ret_bytes) in self.h.iter().zip(ret.chunks_exact_mut(4)) {
72            ret_bytes.copy_from_slice(&(*val).to_le_bytes());
73        }
74        ret
75    }
76
77    #[cfg(hashes_fuzz)]
78    fn midstate(&self) -> [u8; 20] {
79        let mut ret = [0; 20];
80        ret.copy_from_slice(&self.buffer[..20]);
81        ret
82    }
83
84    const BLOCK_SIZE: usize = 64;
85
86    fn n_bytes_hashed(&self) -> usize { self.length }
87
88    engine_input_impl!();
89}
90
91#[cfg(feature = "small-hash")]
92#[macro_use]
93mod small_hash {
94    #[rustfmt::skip]
95    pub(super) fn round(a: u32, _b: u32, c: u32, _d: u32, e: u32,
96                        x: u32, bits: u32, add: u32, round: u32,
97    ) -> (u32, u32) {
98        let a = a.wrapping_add(round).wrapping_add(x).wrapping_add(add);
99        let a = a.rotate_left(bits).wrapping_add(e);
100        let c = c.rotate_left(10);
101
102        (a, c)
103    }
104
105    macro_rules! round(
106        ($a:expr, $b:expr, $c:expr, $d:expr, $e:expr,
107         $x:expr, $bits:expr, $add:expr, $round:expr) => ({
108            let updates = small_hash::round($a, $b, $c, $d, $e, $x, $bits, $add, $round);
109            $a = updates.0;
110            $c = updates.1;
111        });
112    );
113}
114
115#[cfg(not(feature = "small-hash"))]
116macro_rules! round(
117    ($a:expr, $b:expr, $c:expr, $d:expr, $e:expr,
118     $x:expr, $bits:expr, $add:expr, $round:expr) => ({
119        $a = $a.wrapping_add($round).wrapping_add($x).wrapping_add($add);
120        $a = $a.rotate_left($bits).wrapping_add($e);
121        $c = $c.rotate_left(10);
122    });
123);
124
125macro_rules! process_block(
126    ($h:expr, $data:expr,
127     $( round1: h_ordering $f0:expr, $f1:expr, $f2:expr, $f3:expr, $f4:expr;
128                data_index $data_index1:expr; roll_shift $bits1:expr; )*
129     $( round2: h_ordering $g0:expr, $g1:expr, $g2:expr, $g3:expr, $g4:expr;
130                data_index $data_index2:expr; roll_shift $bits2:expr; )*
131     $( round3: h_ordering $h0:expr, $h1:expr, $h2:expr, $h3:expr, $h4:expr;
132                data_index $data_index3:expr; roll_shift $bits3:expr; )*
133     $( round4: h_ordering $i0:expr, $i1:expr, $i2:expr, $i3:expr, $i4:expr;
134                data_index $data_index4:expr; roll_shift $bits4:expr; )*
135     $( round5: h_ordering $j0:expr, $j1:expr, $j2:expr, $j3:expr, $j4:expr;
136                data_index $data_index5:expr; roll_shift $bits5:expr; )*
137     $( par_round1: h_ordering $pj0:expr, $pj1:expr, $pj2:expr, $pj3:expr, $pj4:expr;
138                    data_index $pdata_index1:expr; roll_shift $pbits1:expr; )*
139     $( par_round2: h_ordering $pi0:expr, $pi1:expr, $pi2:expr, $pi3:expr, $pi4:expr;
140                    data_index $pdata_index2:expr; roll_shift $pbits2:expr; )*
141     $( par_round3: h_ordering $ph0:expr, $ph1:expr, $ph2:expr, $ph3:expr, $ph4:expr;
142                    data_index $pdata_index3:expr; roll_shift $pbits3:expr; )*
143     $( par_round4: h_ordering $pg0:expr, $pg1:expr, $pg2:expr, $pg3:expr, $pg4:expr;
144                    data_index $pdata_index4:expr; roll_shift $pbits4:expr; )*
145     $( par_round5: h_ordering $pf0:expr, $pf1:expr, $pf2:expr, $pf3:expr, $pf4:expr;
146                    data_index $pdata_index5:expr; roll_shift $pbits5:expr; )*
147    ) => ({
148        let mut bb = $h;
149        let mut bbb = $h;
150
151        // Round 1
152        $( round!(bb[$f0], bb[$f1], bb[$f2], bb[$f3], bb[$f4],
153                  $data[$data_index1], $bits1, 0x00000000,
154                  bb[$f1] ^ bb[$f2] ^ bb[$f3]); )*
155
156        // Round 2
157        $( round!(bb[$g0], bb[$g1], bb[$g2], bb[$g3], bb[$g4],
158                  $data[$data_index2], $bits2, 0x5a827999,
159                  (bb[$g1] & bb[$g2]) | (!bb[$g1] & bb[$g3])); )*
160
161        // Round 3
162        $( round!(bb[$h0], bb[$h1], bb[$h2], bb[$h3], bb[$h4],
163                  $data[$data_index3], $bits3, 0x6ed9eba1,
164                  (bb[$h1] | !bb[$h2]) ^ bb[$h3]); )*
165
166        // Round 4
167        $( round!(bb[$i0], bb[$i1], bb[$i2], bb[$i3], bb[$i4],
168                  $data[$data_index4], $bits4, 0x8f1bbcdc,
169                  (bb[$i1] & bb[$i3]) | (bb[$i2] & !bb[$i3])); )*
170
171        // Round 5
172        $( round!(bb[$j0], bb[$j1], bb[$j2], bb[$j3], bb[$j4],
173                  $data[$data_index5], $bits5, 0xa953fd4e,
174                  bb[$j1] ^ (bb[$j2] | !bb[$j3])); )*
175
176        // Parallel rounds: these are the same as the previous five
177        // rounds except that the constants have changed, we work
178        // with the other buffer, and they are applied in reverse
179        // order.
180
181        // Parallel Round 1
182        $( round!(bbb[$pj0], bbb[$pj1], bbb[$pj2], bbb[$pj3], bbb[$pj4],
183                  $data[$pdata_index1], $pbits1, 0x50a28be6,
184                  bbb[$pj1] ^ (bbb[$pj2] | !bbb[$pj3])); )*
185
186        // Porallel Round 2
187        $( round!(bbb[$pi0], bbb[$pi1], bbb[$pi2], bbb[$pi3], bbb[$pi4],
188                  $data[$pdata_index2], $pbits2, 0x5c4dd124,
189                  (bbb[$pi1] & bbb[$pi3]) | (bbb[$pi2] & !bbb[$pi3])); )*
190
191        // Parallel Round 3
192        $( round!(bbb[$ph0], bbb[$ph1], bbb[$ph2], bbb[$ph3], bbb[$ph4],
193                  $data[$pdata_index3], $pbits3, 0x6d703ef3,
194                  (bbb[$ph1] | !bbb[$ph2]) ^ bbb[$ph3]); )*
195
196        // Parallel Round 4
197        $( round!(bbb[$pg0], bbb[$pg1], bbb[$pg2], bbb[$pg3], bbb[$pg4],
198                  $data[$pdata_index4], $pbits4, 0x7a6d76e9,
199                  (bbb[$pg1] & bbb[$pg2]) | (!bbb[$pg1] & bbb[$pg3])); )*
200
201        // Parallel Round 5
202        $( round!(bbb[$pf0], bbb[$pf1], bbb[$pf2], bbb[$pf3], bbb[$pf4],
203                  $data[$pdata_index5], $pbits5, 0x00000000,
204                  bbb[$pf1] ^ bbb[$pf2] ^ bbb[$pf3]); )*
205
206        // Combine results
207        bbb[3] = bbb[3].wrapping_add($h[1]).wrapping_add(bb[2]);
208        $h[1]  =  $h[2].wrapping_add(bb[3]).wrapping_add(bbb[4]);
209        $h[2]  =  $h[3].wrapping_add(bb[4]).wrapping_add(bbb[0]);
210        $h[3]  =  $h[4].wrapping_add(bb[0]).wrapping_add(bbb[1]);
211        $h[4]  =  $h[0].wrapping_add(bb[1]).wrapping_add(bbb[2]);
212        $h[0]  =                                         bbb[3];
213    });
214);
215
216impl HashEngine {
217    fn process_block(&mut self) {
218        debug_assert_eq!(self.buffer.len(), BLOCK_SIZE);
219
220        let mut w = [0u32; 16];
221        for (w_val, buff_bytes) in w.iter_mut().zip(self.buffer.chunks_exact(4)) {
222            *w_val = u32::from_le_bytes(buff_bytes.try_into().expect("4 byte slice"))
223        }
224
225        process_block!(self.h, w,
226            // Round 1
227            round1: h_ordering 0, 1, 2, 3, 4; data_index  0; roll_shift 11;
228            round1: h_ordering 4, 0, 1, 2, 3; data_index  1; roll_shift 14;
229            round1: h_ordering 3, 4, 0, 1, 2; data_index  2; roll_shift 15;
230            round1: h_ordering 2, 3, 4, 0, 1; data_index  3; roll_shift 12;
231            round1: h_ordering 1, 2, 3, 4, 0; data_index  4; roll_shift  5;
232            round1: h_ordering 0, 1, 2, 3, 4; data_index  5; roll_shift  8;
233            round1: h_ordering 4, 0, 1, 2, 3; data_index  6; roll_shift  7;
234            round1: h_ordering 3, 4, 0, 1, 2; data_index  7; roll_shift  9;
235            round1: h_ordering 2, 3, 4, 0, 1; data_index  8; roll_shift 11;
236            round1: h_ordering 1, 2, 3, 4, 0; data_index  9; roll_shift 13;
237            round1: h_ordering 0, 1, 2, 3, 4; data_index 10; roll_shift 14;
238            round1: h_ordering 4, 0, 1, 2, 3; data_index 11; roll_shift 15;
239            round1: h_ordering 3, 4, 0, 1, 2; data_index 12; roll_shift  6;
240            round1: h_ordering 2, 3, 4, 0, 1; data_index 13; roll_shift  7;
241            round1: h_ordering 1, 2, 3, 4, 0; data_index 14; roll_shift  9;
242            round1: h_ordering 0, 1, 2, 3, 4; data_index 15; roll_shift  8;
243
244            // Round 2
245            round2: h_ordering 4, 0, 1, 2, 3; data_index  7; roll_shift  7;
246            round2: h_ordering 3, 4, 0, 1, 2; data_index  4; roll_shift  6;
247            round2: h_ordering 2, 3, 4, 0, 1; data_index 13; roll_shift  8;
248            round2: h_ordering 1, 2, 3, 4, 0; data_index  1; roll_shift 13;
249            round2: h_ordering 0, 1, 2, 3, 4; data_index 10; roll_shift 11;
250            round2: h_ordering 4, 0, 1, 2, 3; data_index  6; roll_shift  9;
251            round2: h_ordering 3, 4, 0, 1, 2; data_index 15; roll_shift  7;
252            round2: h_ordering 2, 3, 4, 0, 1; data_index  3; roll_shift 15;
253            round2: h_ordering 1, 2, 3, 4, 0; data_index 12; roll_shift  7;
254            round2: h_ordering 0, 1, 2, 3, 4; data_index  0; roll_shift 12;
255            round2: h_ordering 4, 0, 1, 2, 3; data_index  9; roll_shift 15;
256            round2: h_ordering 3, 4, 0, 1, 2; data_index  5; roll_shift  9;
257            round2: h_ordering 2, 3, 4, 0, 1; data_index  2; roll_shift 11;
258            round2: h_ordering 1, 2, 3, 4, 0; data_index 14; roll_shift  7;
259            round2: h_ordering 0, 1, 2, 3, 4; data_index 11; roll_shift 13;
260            round2: h_ordering 4, 0, 1, 2, 3; data_index  8; roll_shift 12;
261
262            // Round 3
263            round3: h_ordering 3, 4, 0, 1, 2; data_index  3; roll_shift 11;
264            round3: h_ordering 2, 3, 4, 0, 1; data_index 10; roll_shift 13;
265            round3: h_ordering 1, 2, 3, 4, 0; data_index 14; roll_shift  6;
266            round3: h_ordering 0, 1, 2, 3, 4; data_index  4; roll_shift  7;
267            round3: h_ordering 4, 0, 1, 2, 3; data_index  9; roll_shift 14;
268            round3: h_ordering 3, 4, 0, 1, 2; data_index 15; roll_shift  9;
269            round3: h_ordering 2, 3, 4, 0, 1; data_index  8; roll_shift 13;
270            round3: h_ordering 1, 2, 3, 4, 0; data_index  1; roll_shift 15;
271            round3: h_ordering 0, 1, 2, 3, 4; data_index  2; roll_shift 14;
272            round3: h_ordering 4, 0, 1, 2, 3; data_index  7; roll_shift  8;
273            round3: h_ordering 3, 4, 0, 1, 2; data_index  0; roll_shift 13;
274            round3: h_ordering 2, 3, 4, 0, 1; data_index  6; roll_shift  6;
275            round3: h_ordering 1, 2, 3, 4, 0; data_index 13; roll_shift  5;
276            round3: h_ordering 0, 1, 2, 3, 4; data_index 11; roll_shift 12;
277            round3: h_ordering 4, 0, 1, 2, 3; data_index  5; roll_shift  7;
278            round3: h_ordering 3, 4, 0, 1, 2; data_index 12; roll_shift  5;
279
280            // Round 4
281            round4: h_ordering 2, 3, 4, 0, 1; data_index  1; roll_shift 11;
282            round4: h_ordering 1, 2, 3, 4, 0; data_index  9; roll_shift 12;
283            round4: h_ordering 0, 1, 2, 3, 4; data_index 11; roll_shift 14;
284            round4: h_ordering 4, 0, 1, 2, 3; data_index 10; roll_shift 15;
285            round4: h_ordering 3, 4, 0, 1, 2; data_index  0; roll_shift 14;
286            round4: h_ordering 2, 3, 4, 0, 1; data_index  8; roll_shift 15;
287            round4: h_ordering 1, 2, 3, 4, 0; data_index 12; roll_shift  9;
288            round4: h_ordering 0, 1, 2, 3, 4; data_index  4; roll_shift  8;
289            round4: h_ordering 4, 0, 1, 2, 3; data_index 13; roll_shift  9;
290            round4: h_ordering 3, 4, 0, 1, 2; data_index  3; roll_shift 14;
291            round4: h_ordering 2, 3, 4, 0, 1; data_index  7; roll_shift  5;
292            round4: h_ordering 1, 2, 3, 4, 0; data_index 15; roll_shift  6;
293            round4: h_ordering 0, 1, 2, 3, 4; data_index 14; roll_shift  8;
294            round4: h_ordering 4, 0, 1, 2, 3; data_index  5; roll_shift  6;
295            round4: h_ordering 3, 4, 0, 1, 2; data_index  6; roll_shift  5;
296            round4: h_ordering 2, 3, 4, 0, 1; data_index  2; roll_shift 12;
297
298            // Round 5
299            round5: h_ordering 1, 2, 3, 4, 0; data_index  4; roll_shift  9;
300            round5: h_ordering 0, 1, 2, 3, 4; data_index  0; roll_shift 15;
301            round5: h_ordering 4, 0, 1, 2, 3; data_index  5; roll_shift  5;
302            round5: h_ordering 3, 4, 0, 1, 2; data_index  9; roll_shift 11;
303            round5: h_ordering 2, 3, 4, 0, 1; data_index  7; roll_shift  6;
304            round5: h_ordering 1, 2, 3, 4, 0; data_index 12; roll_shift  8;
305            round5: h_ordering 0, 1, 2, 3, 4; data_index  2; roll_shift 13;
306            round5: h_ordering 4, 0, 1, 2, 3; data_index 10; roll_shift 12;
307            round5: h_ordering 3, 4, 0, 1, 2; data_index 14; roll_shift  5;
308            round5: h_ordering 2, 3, 4, 0, 1; data_index  1; roll_shift 12;
309            round5: h_ordering 1, 2, 3, 4, 0; data_index  3; roll_shift 13;
310            round5: h_ordering 0, 1, 2, 3, 4; data_index  8; roll_shift 14;
311            round5: h_ordering 4, 0, 1, 2, 3; data_index 11; roll_shift 11;
312            round5: h_ordering 3, 4, 0, 1, 2; data_index  6; roll_shift  8;
313            round5: h_ordering 2, 3, 4, 0, 1; data_index 15; roll_shift  5;
314            round5: h_ordering 1, 2, 3, 4, 0; data_index 13; roll_shift  6;
315
316            // Porallel Round 1;
317            par_round1: h_ordering 0, 1, 2, 3, 4; data_index  5; roll_shift  8;
318            par_round1: h_ordering 4, 0, 1, 2, 3; data_index 14; roll_shift  9;
319            par_round1: h_ordering 3, 4, 0, 1, 2; data_index  7; roll_shift  9;
320            par_round1: h_ordering 2, 3, 4, 0, 1; data_index  0; roll_shift 11;
321            par_round1: h_ordering 1, 2, 3, 4, 0; data_index  9; roll_shift 13;
322            par_round1: h_ordering 0, 1, 2, 3, 4; data_index  2; roll_shift 15;
323            par_round1: h_ordering 4, 0, 1, 2, 3; data_index 11; roll_shift 15;
324            par_round1: h_ordering 3, 4, 0, 1, 2; data_index  4; roll_shift  5;
325            par_round1: h_ordering 2, 3, 4, 0, 1; data_index 13; roll_shift  7;
326            par_round1: h_ordering 1, 2, 3, 4, 0; data_index  6; roll_shift  7;
327            par_round1: h_ordering 0, 1, 2, 3, 4; data_index 15; roll_shift  8;
328            par_round1: h_ordering 4, 0, 1, 2, 3; data_index  8; roll_shift 11;
329            par_round1: h_ordering 3, 4, 0, 1, 2; data_index  1; roll_shift 14;
330            par_round1: h_ordering 2, 3, 4, 0, 1; data_index 10; roll_shift 14;
331            par_round1: h_ordering 1, 2, 3, 4, 0; data_index  3; roll_shift 12;
332            par_round1: h_ordering 0, 1, 2, 3, 4; data_index 12; roll_shift  6;
333
334            // Parallel Round 2
335            par_round2: h_ordering 4, 0, 1, 2, 3; data_index  6; roll_shift  9;
336            par_round2: h_ordering 3, 4, 0, 1, 2; data_index 11; roll_shift 13;
337            par_round2: h_ordering 2, 3, 4, 0, 1; data_index  3; roll_shift 15;
338            par_round2: h_ordering 1, 2, 3, 4, 0; data_index  7; roll_shift  7;
339            par_round2: h_ordering 0, 1, 2, 3, 4; data_index  0; roll_shift 12;
340            par_round2: h_ordering 4, 0, 1, 2, 3; data_index 13; roll_shift  8;
341            par_round2: h_ordering 3, 4, 0, 1, 2; data_index  5; roll_shift  9;
342            par_round2: h_ordering 2, 3, 4, 0, 1; data_index 10; roll_shift 11;
343            par_round2: h_ordering 1, 2, 3, 4, 0; data_index 14; roll_shift  7;
344            par_round2: h_ordering 0, 1, 2, 3, 4; data_index 15; roll_shift  7;
345            par_round2: h_ordering 4, 0, 1, 2, 3; data_index  8; roll_shift 12;
346            par_round2: h_ordering 3, 4, 0, 1, 2; data_index 12; roll_shift  7;
347            par_round2: h_ordering 2, 3, 4, 0, 1; data_index  4; roll_shift  6;
348            par_round2: h_ordering 1, 2, 3, 4, 0; data_index  9; roll_shift 15;
349            par_round2: h_ordering 0, 1, 2, 3, 4; data_index  1; roll_shift 13;
350            par_round2: h_ordering 4, 0, 1, 2, 3; data_index  2; roll_shift 11;
351
352            // Parallel Round 3
353            par_round3: h_ordering 3, 4, 0, 1, 2; data_index 15; roll_shift  9;
354            par_round3: h_ordering 2, 3, 4, 0, 1; data_index  5; roll_shift  7;
355            par_round3: h_ordering 1, 2, 3, 4, 0; data_index  1; roll_shift 15;
356            par_round3: h_ordering 0, 1, 2, 3, 4; data_index  3; roll_shift 11;
357            par_round3: h_ordering 4, 0, 1, 2, 3; data_index  7; roll_shift  8;
358            par_round3: h_ordering 3, 4, 0, 1, 2; data_index 14; roll_shift  6;
359            par_round3: h_ordering 2, 3, 4, 0, 1; data_index  6; roll_shift  6;
360            par_round3: h_ordering 1, 2, 3, 4, 0; data_index  9; roll_shift 14;
361            par_round3: h_ordering 0, 1, 2, 3, 4; data_index 11; roll_shift 12;
362            par_round3: h_ordering 4, 0, 1, 2, 3; data_index  8; roll_shift 13;
363            par_round3: h_ordering 3, 4, 0, 1, 2; data_index 12; roll_shift  5;
364            par_round3: h_ordering 2, 3, 4, 0, 1; data_index  2; roll_shift 14;
365            par_round3: h_ordering 1, 2, 3, 4, 0; data_index 10; roll_shift 13;
366            par_round3: h_ordering 0, 1, 2, 3, 4; data_index  0; roll_shift 13;
367            par_round3: h_ordering 4, 0, 1, 2, 3; data_index  4; roll_shift  7;
368            par_round3: h_ordering 3, 4, 0, 1, 2; data_index 13; roll_shift  5;
369
370            // Parallel Round 4
371            par_round4: h_ordering 2, 3, 4, 0, 1; data_index  8; roll_shift 15;
372            par_round4: h_ordering 1, 2, 3, 4, 0; data_index  6; roll_shift  5;
373            par_round4: h_ordering 0, 1, 2, 3, 4; data_index  4; roll_shift  8;
374            par_round4: h_ordering 4, 0, 1, 2, 3; data_index  1; roll_shift 11;
375            par_round4: h_ordering 3, 4, 0, 1, 2; data_index  3; roll_shift 14;
376            par_round4: h_ordering 2, 3, 4, 0, 1; data_index 11; roll_shift 14;
377            par_round4: h_ordering 1, 2, 3, 4, 0; data_index 15; roll_shift  6;
378            par_round4: h_ordering 0, 1, 2, 3, 4; data_index  0; roll_shift 14;
379            par_round4: h_ordering 4, 0, 1, 2, 3; data_index  5; roll_shift  6;
380            par_round4: h_ordering 3, 4, 0, 1, 2; data_index 12; roll_shift  9;
381            par_round4: h_ordering 2, 3, 4, 0, 1; data_index  2; roll_shift 12;
382            par_round4: h_ordering 1, 2, 3, 4, 0; data_index 13; roll_shift  9;
383            par_round4: h_ordering 0, 1, 2, 3, 4; data_index  9; roll_shift 12;
384            par_round4: h_ordering 4, 0, 1, 2, 3; data_index  7; roll_shift  5;
385            par_round4: h_ordering 3, 4, 0, 1, 2; data_index 10; roll_shift 15;
386            par_round4: h_ordering 2, 3, 4, 0, 1; data_index 14; roll_shift  8;
387
388            // Parallel Round 5
389            par_round5: h_ordering 1, 2, 3, 4, 0; data_index 12; roll_shift  8;
390            par_round5: h_ordering 0, 1, 2, 3, 4; data_index 15; roll_shift  5;
391            par_round5: h_ordering 4, 0, 1, 2, 3; data_index 10; roll_shift 12;
392            par_round5: h_ordering 3, 4, 0, 1, 2; data_index  4; roll_shift  9;
393            par_round5: h_ordering 2, 3, 4, 0, 1; data_index  1; roll_shift 12;
394            par_round5: h_ordering 1, 2, 3, 4, 0; data_index  5; roll_shift  5;
395            par_round5: h_ordering 0, 1, 2, 3, 4; data_index  8; roll_shift 14;
396            par_round5: h_ordering 4, 0, 1, 2, 3; data_index  7; roll_shift  6;
397            par_round5: h_ordering 3, 4, 0, 1, 2; data_index  6; roll_shift  8;
398            par_round5: h_ordering 2, 3, 4, 0, 1; data_index  2; roll_shift 13;
399            par_round5: h_ordering 1, 2, 3, 4, 0; data_index 13; roll_shift  6;
400            par_round5: h_ordering 0, 1, 2, 3, 4; data_index 14; roll_shift  5;
401            par_round5: h_ordering 4, 0, 1, 2, 3; data_index  0; roll_shift 15;
402            par_round5: h_ordering 3, 4, 0, 1, 2; data_index  3; roll_shift 13;
403            par_round5: h_ordering 2, 3, 4, 0, 1; data_index  9; roll_shift 11;
404            par_round5: h_ordering 1, 2, 3, 4, 0; data_index 11; roll_shift 11;
405        );
406    }
407}
408
409#[cfg(test)]
410mod tests {
411    #[test]
412    #[cfg(feature = "alloc")]
413    fn test() {
414        use std::convert::TryFrom;
415
416        use crate::{ripemd160, Hash, HashEngine};
417
418        #[derive(Clone)]
419        struct Test {
420            input: &'static str,
421            output: Vec<u8>,
422            output_str: &'static str,
423        }
424
425        #[rustfmt::skip]
426        let tests = vec![
427            // Test messages from FIPS 180-1
428            Test {
429                input: "abc",
430                output: vec![
431                    0x8e, 0xb2, 0x08, 0xf7,
432                    0xe0, 0x5d, 0x98, 0x7a,
433                    0x9b, 0x04, 0x4a, 0x8e,
434                    0x98, 0xc6, 0xb0, 0x87,
435                    0xf1, 0x5a, 0x0b, 0xfc,
436                ],
437                output_str: "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"
438            },
439            Test {
440                input:
441                     "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
442                output: vec![
443                    0x12, 0xa0, 0x53, 0x38,
444                    0x4a, 0x9c, 0x0c, 0x88,
445                    0xe4, 0x05, 0xa0, 0x6c,
446                    0x27, 0xdc, 0xf4, 0x9a,
447                    0xda, 0x62, 0xeb, 0x2b,
448                ],
449                output_str: "12a053384a9c0c88e405a06c27dcf49ada62eb2b"
450            },
451            // Examples from wikipedia
452            Test {
453                input: "The quick brown fox jumps over the lazy dog",
454                output: vec![
455                    0x37, 0xf3, 0x32, 0xf6,
456                    0x8d, 0xb7, 0x7b, 0xd9,
457                    0xd7, 0xed, 0xd4, 0x96,
458                    0x95, 0x71, 0xad, 0x67,
459                    0x1c, 0xf9, 0xdd, 0x3b,
460                ],
461                output_str: "37f332f68db77bd9d7edd4969571ad671cf9dd3b",
462            },
463            Test {
464                input: "The quick brown fox jumps over the lazy cog",
465                output: vec![
466                    0x13, 0x20, 0x72, 0xdf,
467                    0x69, 0x09, 0x33, 0x83,
468                    0x5e, 0xb8, 0xb6, 0xad,
469                    0x0b, 0x77, 0xe7, 0xb6,
470                    0xf1, 0x4a, 0xca, 0xd7,
471                ],
472                output_str: "132072df690933835eb8b6ad0b77e7b6f14acad7",
473            },
474        ];
475
476        for mut test in tests {
477            // Hash through high-level API, check hex encoding/decoding
478            let hash = ripemd160::Hash::hash(test.input.as_bytes());
479            assert_eq!(hash, test.output_str.parse::<ripemd160::Hash>().expect("parse hex"));
480            assert_eq!(&hash[..], &test.output[..]);
481            assert_eq!(&hash.to_string(), &test.output_str);
482            assert_eq!(
483                ripemd160::Hash::from_bytes_ref(
484                    <&[u8; 20]>::try_from(&*test.output).expect("known length")
485                ),
486                &hash
487            );
488            assert_eq!(
489                ripemd160::Hash::from_bytes_mut(
490                    <&mut [u8; 20]>::try_from(&mut *test.output).expect("known length")
491                ),
492                &hash
493            );
494
495            // Hash through engine, checking that we can input byte by byte
496            let mut engine = ripemd160::Hash::engine();
497            for ch in test.input.as_bytes() {
498                engine.input(&[*ch]);
499            }
500            let manual_hash = ripemd160::Hash::from_engine(engine);
501            assert_eq!(hash, manual_hash);
502            assert_eq!(hash.as_byte_array(), test.output.as_slice());
503        }
504    }
505
506    #[cfg(feature = "serde")]
507    #[test]
508    fn ripemd_serde() {
509        use serde_test::{assert_tokens, Configure, Token};
510
511        use crate::{ripemd160, Hash};
512
513        #[rustfmt::skip]
514        static HASH_BYTES: [u8; 20] = [
515            0x13, 0x20, 0x72, 0xdf,
516            0x69, 0x09, 0x33, 0x83,
517            0x5e, 0xb8, 0xb6, 0xad,
518            0x0b, 0x77, 0xe7, 0xb6,
519            0xf1, 0x4a, 0xca, 0xd7,
520        ];
521
522        let hash = ripemd160::Hash::from_slice(&HASH_BYTES).expect("right number of bytes");
523        assert_tokens(&hash.compact(), &[Token::BorrowedBytes(&HASH_BYTES[..])]);
524        assert_tokens(&hash.readable(), &[Token::Str("132072df690933835eb8b6ad0b77e7b6f14acad7")]);
525    }
526}
527
528#[cfg(bench)]
529mod benches {
530    use test::Bencher;
531
532    use crate::{ripemd160, Hash, HashEngine};
533
534    #[bench]
535    pub fn ripemd160_10(bh: &mut Bencher) {
536        let mut engine = ripemd160::Hash::engine();
537        let bytes = [1u8; 10];
538        bh.iter(|| {
539            engine.input(&bytes);
540        });
541        bh.bytes = bytes.len() as u64;
542    }
543
544    #[bench]
545    pub fn ripemd160_1k(bh: &mut Bencher) {
546        let mut engine = ripemd160::Hash::engine();
547        let bytes = [1u8; 1024];
548        bh.iter(|| {
549            engine.input(&bytes);
550        });
551        bh.bytes = bytes.len() as u64;
552    }
553
554    #[bench]
555    pub fn ripemd160_64k(bh: &mut Bencher) {
556        let mut engine = ripemd160::Hash::engine();
557        let bytes = [1u8; 65536];
558        bh.iter(|| {
559            engine.input(&bytes);
560        });
561        bh.bytes = bytes.len() as u64;
562    }
563}