bitcoin_hashes/
ripemd160.rs

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