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