1use core::ops::Index;
12use core::slice::SliceIndex;
13use core::str;
14
15use crate::{ripemd160, sha256, FromSliceError};
16
17crate::internal_macros::hash_type! {
18 160,
19 false,
20 "Output of the Bitcoin HASH160 hash function. (RIPEMD160(SHA256))",
21 "crate::util::json_hex_string::len_20"
22}
23
24type HashEngine = sha256::HashEngine;
25
26fn from_engine(e: HashEngine) -> Hash {
27 use crate::Hash as _;
28
29 let sha2 = sha256::Hash::from_engine(e);
30 let rmd = ripemd160::Hash::hash(&sha2[..]);
31
32 let mut ret = [0; 20];
33 ret.copy_from_slice(&rmd[..]);
34 Hash(ret)
35}
36
37#[cfg(test)]
38mod tests {
39 #[test]
40 #[cfg(feature = "alloc")]
41 fn test() {
42 use crate::{hash160, Hash, HashEngine};
43
44 #[derive(Clone)]
45 #[cfg(feature = "alloc")]
46 struct Test {
47 input: Vec<u8>,
48 output: Vec<u8>,
49 output_str: &'static str,
50 }
51
52 #[rustfmt::skip]
53 let tests = vec![
54 Test {
56 input: vec![
57 0x04, 0xa1, 0x49, 0xd7, 0x6c, 0x5d, 0xe2, 0x7a, 0x2d,
58 0xdb, 0xfa, 0xa1, 0x24, 0x6c, 0x4a, 0xdc, 0xd2, 0xb6,
59 0xf7, 0xaa, 0x29, 0x54, 0xc2, 0xe2, 0x53, 0x03, 0xf5,
60 0x51, 0x54, 0xca, 0xad, 0x91, 0x52, 0xe4, 0xf7, 0xe4,
61 0xb8, 0x5d, 0xf1, 0x69, 0xc1, 0x8a, 0x3c, 0x69, 0x7f,
62 0xbb, 0x2d, 0xc4, 0xec, 0xef, 0x94, 0xac, 0x55, 0xfe,
63 0x81, 0x64, 0xcc, 0xf9, 0x82, 0xa1, 0x38, 0x69, 0x1a,
64 0x55, 0x19,
65 ],
66 output: vec![
67 0xda, 0x0b, 0x34, 0x52, 0xb0, 0x6f, 0xe3, 0x41,
68 0x62, 0x6a, 0xd0, 0x94, 0x9c, 0x18, 0x3f, 0xbd,
69 0xa5, 0x67, 0x68, 0x26,
70 ],
71 output_str: "da0b3452b06fe341626ad0949c183fbda5676826",
72 },
73 ];
74
75 for test in tests {
76 let hash = hash160::Hash::hash(&test.input[..]);
78 assert_eq!(hash, test.output_str.parse::<hash160::Hash>().expect("parse hex"));
79 assert_eq!(&hash[..], &test.output[..]);
80 assert_eq!(&hash.to_string(), &test.output_str);
81
82 let mut engine = hash160::Hash::engine();
84 for ch in test.input {
85 engine.input(&[ch]);
86 }
87 let manual_hash = Hash::from_engine(engine);
88 assert_eq!(hash, manual_hash);
89 assert_eq!(hash.to_byte_array()[..].as_ref(), test.output.as_slice());
90 }
91 }
92
93 #[cfg(feature = "serde")]
94 #[test]
95 fn ripemd_serde() {
96 use serde_test::{assert_tokens, Configure, Token};
97
98 use crate::{hash160, Hash};
99
100 #[rustfmt::skip]
101 static HASH_BYTES: [u8; 20] = [
102 0x13, 0x20, 0x72, 0xdf,
103 0x69, 0x09, 0x33, 0x83,
104 0x5e, 0xb8, 0xb6, 0xad,
105 0x0b, 0x77, 0xe7, 0xb6,
106 0xf1, 0x4a, 0xca, 0xd7,
107 ];
108
109 let hash = hash160::Hash::from_slice(&HASH_BYTES).expect("right number of bytes");
110 assert_tokens(&hash.compact(), &[Token::BorrowedBytes(&HASH_BYTES[..])]);
111 assert_tokens(&hash.readable(), &[Token::Str("132072df690933835eb8b6ad0b77e7b6f14acad7")]);
112 }
113}
114
115#[cfg(bench)]
116mod benches {
117 use test::Bencher;
118
119 use crate::{hash160, Hash, HashEngine};
120
121 #[bench]
122 pub fn hash160_10(bh: &mut Bencher) {
123 let mut engine = hash160::Hash::engine();
124 let bytes = [1u8; 10];
125 bh.iter(|| {
126 engine.input(&bytes);
127 });
128 bh.bytes = bytes.len() as u64;
129 }
130
131 #[bench]
132 pub fn hash160_1k(bh: &mut Bencher) {
133 let mut engine = hash160::Hash::engine();
134 let bytes = [1u8; 1024];
135 bh.iter(|| {
136 engine.input(&bytes);
137 });
138 bh.bytes = bytes.len() as u64;
139 }
140
141 #[bench]
142 pub fn hash160_64k(bh: &mut Bencher) {
143 let mut engine = hash160::Hash::engine();
144 let bytes = [1u8; 65536];
145 bh.iter(|| {
146 engine.input(&bytes);
147 });
148 bh.bytes = bytes.len() as u64;
149 }
150}