Bitcoin ABC 0.30.7
P2P Digital Currency
ripemd160.cpp
Go to the documentation of this file.
1// Copyright (c) 2014 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <crypto/ripemd160.h>
6
7#include <crypto/common.h>
8
9#include <cstring>
10
11// Internal implementation code.
12namespace {
14namespace ripemd160 {
15 inline uint32_t f1(uint32_t x, uint32_t y, uint32_t z) {
16 return x ^ y ^ z;
17 }
18 inline uint32_t f2(uint32_t x, uint32_t y, uint32_t z) {
19 return (x & y) | (~x & z);
20 }
21 inline uint32_t f3(uint32_t x, uint32_t y, uint32_t z) {
22 return (x | ~y) ^ z;
23 }
24 inline uint32_t f4(uint32_t x, uint32_t y, uint32_t z) {
25 return (x & z) | (y & ~z);
26 }
27 inline uint32_t f5(uint32_t x, uint32_t y, uint32_t z) {
28 return x ^ (y | ~z);
29 }
30
32 inline void Initialize(uint32_t *s) {
33 s[0] = 0x67452301ul;
34 s[1] = 0xEFCDAB89ul;
35 s[2] = 0x98BADCFEul;
36 s[3] = 0x10325476ul;
37 s[4] = 0xC3D2E1F0ul;
38 }
39
40 inline uint32_t rol(uint32_t x, int i) {
41 return (x << i) | (x >> (32 - i));
42 }
43
44 inline void Round(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
45 uint32_t e, uint32_t f, uint32_t x, uint32_t k, int r) {
46 a = rol(a + f + x + k, r) + e;
47 c = rol(c, 10);
48 }
49
50 inline void R11(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
51 uint32_t e, uint32_t x, int r) {
52 Round(a, b, c, d, e, f1(b, c, d), x, 0, r);
53 }
54 inline void R21(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
55 uint32_t e, uint32_t x, int r) {
56 Round(a, b, c, d, e, f2(b, c, d), x, 0x5A827999ul, r);
57 }
58 inline void R31(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
59 uint32_t e, uint32_t x, int r) {
60 Round(a, b, c, d, e, f3(b, c, d), x, 0x6ED9EBA1ul, r);
61 }
62 inline void R41(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
63 uint32_t e, uint32_t x, int r) {
64 Round(a, b, c, d, e, f4(b, c, d), x, 0x8F1BBCDCul, r);
65 }
66 inline void R51(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
67 uint32_t e, uint32_t x, int r) {
68 Round(a, b, c, d, e, f5(b, c, d), x, 0xA953FD4Eul, r);
69 }
70
71 inline void R12(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
72 uint32_t e, uint32_t x, int r) {
73 Round(a, b, c, d, e, f5(b, c, d), x, 0x50A28BE6ul, r);
74 }
75 inline void R22(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
76 uint32_t e, uint32_t x, int r) {
77 Round(a, b, c, d, e, f4(b, c, d), x, 0x5C4DD124ul, r);
78 }
79 inline void R32(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
80 uint32_t e, uint32_t x, int r) {
81 Round(a, b, c, d, e, f3(b, c, d), x, 0x6D703EF3ul, r);
82 }
83 inline void R42(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
84 uint32_t e, uint32_t x, int r) {
85 Round(a, b, c, d, e, f2(b, c, d), x, 0x7A6D76E9ul, r);
86 }
87 inline void R52(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
88 uint32_t e, uint32_t x, int r) {
89 Round(a, b, c, d, e, f1(b, c, d), x, 0, r);
90 }
91
93 void Transform(uint32_t *s, const uint8_t *chunk) {
94 uint32_t a1 = s[0], b1 = s[1], c1 = s[2], d1 = s[3], e1 = s[4];
95 uint32_t a2 = a1, b2 = b1, c2 = c1, d2 = d1, e2 = e1;
96 uint32_t w0 = ReadLE32(chunk + 0), w1 = ReadLE32(chunk + 4),
97 w2 = ReadLE32(chunk + 8), w3 = ReadLE32(chunk + 12);
98 uint32_t w4 = ReadLE32(chunk + 16), w5 = ReadLE32(chunk + 20),
99 w6 = ReadLE32(chunk + 24), w7 = ReadLE32(chunk + 28);
100 uint32_t w8 = ReadLE32(chunk + 32), w9 = ReadLE32(chunk + 36),
101 w10 = ReadLE32(chunk + 40), w11 = ReadLE32(chunk + 44);
102 uint32_t w12 = ReadLE32(chunk + 48), w13 = ReadLE32(chunk + 52),
103 w14 = ReadLE32(chunk + 56), w15 = ReadLE32(chunk + 60);
104
105 R11(a1, b1, c1, d1, e1, w0, 11);
106 R12(a2, b2, c2, d2, e2, w5, 8);
107 R11(e1, a1, b1, c1, d1, w1, 14);
108 R12(e2, a2, b2, c2, d2, w14, 9);
109 R11(d1, e1, a1, b1, c1, w2, 15);
110 R12(d2, e2, a2, b2, c2, w7, 9);
111 R11(c1, d1, e1, a1, b1, w3, 12);
112 R12(c2, d2, e2, a2, b2, w0, 11);
113 R11(b1, c1, d1, e1, a1, w4, 5);
114 R12(b2, c2, d2, e2, a2, w9, 13);
115 R11(a1, b1, c1, d1, e1, w5, 8);
116 R12(a2, b2, c2, d2, e2, w2, 15);
117 R11(e1, a1, b1, c1, d1, w6, 7);
118 R12(e2, a2, b2, c2, d2, w11, 15);
119 R11(d1, e1, a1, b1, c1, w7, 9);
120 R12(d2, e2, a2, b2, c2, w4, 5);
121 R11(c1, d1, e1, a1, b1, w8, 11);
122 R12(c2, d2, e2, a2, b2, w13, 7);
123 R11(b1, c1, d1, e1, a1, w9, 13);
124 R12(b2, c2, d2, e2, a2, w6, 7);
125 R11(a1, b1, c1, d1, e1, w10, 14);
126 R12(a2, b2, c2, d2, e2, w15, 8);
127 R11(e1, a1, b1, c1, d1, w11, 15);
128 R12(e2, a2, b2, c2, d2, w8, 11);
129 R11(d1, e1, a1, b1, c1, w12, 6);
130 R12(d2, e2, a2, b2, c2, w1, 14);
131 R11(c1, d1, e1, a1, b1, w13, 7);
132 R12(c2, d2, e2, a2, b2, w10, 14);
133 R11(b1, c1, d1, e1, a1, w14, 9);
134 R12(b2, c2, d2, e2, a2, w3, 12);
135 R11(a1, b1, c1, d1, e1, w15, 8);
136 R12(a2, b2, c2, d2, e2, w12, 6);
137
138 R21(e1, a1, b1, c1, d1, w7, 7);
139 R22(e2, a2, b2, c2, d2, w6, 9);
140 R21(d1, e1, a1, b1, c1, w4, 6);
141 R22(d2, e2, a2, b2, c2, w11, 13);
142 R21(c1, d1, e1, a1, b1, w13, 8);
143 R22(c2, d2, e2, a2, b2, w3, 15);
144 R21(b1, c1, d1, e1, a1, w1, 13);
145 R22(b2, c2, d2, e2, a2, w7, 7);
146 R21(a1, b1, c1, d1, e1, w10, 11);
147 R22(a2, b2, c2, d2, e2, w0, 12);
148 R21(e1, a1, b1, c1, d1, w6, 9);
149 R22(e2, a2, b2, c2, d2, w13, 8);
150 R21(d1, e1, a1, b1, c1, w15, 7);
151 R22(d2, e2, a2, b2, c2, w5, 9);
152 R21(c1, d1, e1, a1, b1, w3, 15);
153 R22(c2, d2, e2, a2, b2, w10, 11);
154 R21(b1, c1, d1, e1, a1, w12, 7);
155 R22(b2, c2, d2, e2, a2, w14, 7);
156 R21(a1, b1, c1, d1, e1, w0, 12);
157 R22(a2, b2, c2, d2, e2, w15, 7);
158 R21(e1, a1, b1, c1, d1, w9, 15);
159 R22(e2, a2, b2, c2, d2, w8, 12);
160 R21(d1, e1, a1, b1, c1, w5, 9);
161 R22(d2, e2, a2, b2, c2, w12, 7);
162 R21(c1, d1, e1, a1, b1, w2, 11);
163 R22(c2, d2, e2, a2, b2, w4, 6);
164 R21(b1, c1, d1, e1, a1, w14, 7);
165 R22(b2, c2, d2, e2, a2, w9, 15);
166 R21(a1, b1, c1, d1, e1, w11, 13);
167 R22(a2, b2, c2, d2, e2, w1, 13);
168 R21(e1, a1, b1, c1, d1, w8, 12);
169 R22(e2, a2, b2, c2, d2, w2, 11);
170
171 R31(d1, e1, a1, b1, c1, w3, 11);
172 R32(d2, e2, a2, b2, c2, w15, 9);
173 R31(c1, d1, e1, a1, b1, w10, 13);
174 R32(c2, d2, e2, a2, b2, w5, 7);
175 R31(b1, c1, d1, e1, a1, w14, 6);
176 R32(b2, c2, d2, e2, a2, w1, 15);
177 R31(a1, b1, c1, d1, e1, w4, 7);
178 R32(a2, b2, c2, d2, e2, w3, 11);
179 R31(e1, a1, b1, c1, d1, w9, 14);
180 R32(e2, a2, b2, c2, d2, w7, 8);
181 R31(d1, e1, a1, b1, c1, w15, 9);
182 R32(d2, e2, a2, b2, c2, w14, 6);
183 R31(c1, d1, e1, a1, b1, w8, 13);
184 R32(c2, d2, e2, a2, b2, w6, 6);
185 R31(b1, c1, d1, e1, a1, w1, 15);
186 R32(b2, c2, d2, e2, a2, w9, 14);
187 R31(a1, b1, c1, d1, e1, w2, 14);
188 R32(a2, b2, c2, d2, e2, w11, 12);
189 R31(e1, a1, b1, c1, d1, w7, 8);
190 R32(e2, a2, b2, c2, d2, w8, 13);
191 R31(d1, e1, a1, b1, c1, w0, 13);
192 R32(d2, e2, a2, b2, c2, w12, 5);
193 R31(c1, d1, e1, a1, b1, w6, 6);
194 R32(c2, d2, e2, a2, b2, w2, 14);
195 R31(b1, c1, d1, e1, a1, w13, 5);
196 R32(b2, c2, d2, e2, a2, w10, 13);
197 R31(a1, b1, c1, d1, e1, w11, 12);
198 R32(a2, b2, c2, d2, e2, w0, 13);
199 R31(e1, a1, b1, c1, d1, w5, 7);
200 R32(e2, a2, b2, c2, d2, w4, 7);
201 R31(d1, e1, a1, b1, c1, w12, 5);
202 R32(d2, e2, a2, b2, c2, w13, 5);
203
204 R41(c1, d1, e1, a1, b1, w1, 11);
205 R42(c2, d2, e2, a2, b2, w8, 15);
206 R41(b1, c1, d1, e1, a1, w9, 12);
207 R42(b2, c2, d2, e2, a2, w6, 5);
208 R41(a1, b1, c1, d1, e1, w11, 14);
209 R42(a2, b2, c2, d2, e2, w4, 8);
210 R41(e1, a1, b1, c1, d1, w10, 15);
211 R42(e2, a2, b2, c2, d2, w1, 11);
212 R41(d1, e1, a1, b1, c1, w0, 14);
213 R42(d2, e2, a2, b2, c2, w3, 14);
214 R41(c1, d1, e1, a1, b1, w8, 15);
215 R42(c2, d2, e2, a2, b2, w11, 14);
216 R41(b1, c1, d1, e1, a1, w12, 9);
217 R42(b2, c2, d2, e2, a2, w15, 6);
218 R41(a1, b1, c1, d1, e1, w4, 8);
219 R42(a2, b2, c2, d2, e2, w0, 14);
220 R41(e1, a1, b1, c1, d1, w13, 9);
221 R42(e2, a2, b2, c2, d2, w5, 6);
222 R41(d1, e1, a1, b1, c1, w3, 14);
223 R42(d2, e2, a2, b2, c2, w12, 9);
224 R41(c1, d1, e1, a1, b1, w7, 5);
225 R42(c2, d2, e2, a2, b2, w2, 12);
226 R41(b1, c1, d1, e1, a1, w15, 6);
227 R42(b2, c2, d2, e2, a2, w13, 9);
228 R41(a1, b1, c1, d1, e1, w14, 8);
229 R42(a2, b2, c2, d2, e2, w9, 12);
230 R41(e1, a1, b1, c1, d1, w5, 6);
231 R42(e2, a2, b2, c2, d2, w7, 5);
232 R41(d1, e1, a1, b1, c1, w6, 5);
233 R42(d2, e2, a2, b2, c2, w10, 15);
234 R41(c1, d1, e1, a1, b1, w2, 12);
235 R42(c2, d2, e2, a2, b2, w14, 8);
236
237 R51(b1, c1, d1, e1, a1, w4, 9);
238 R52(b2, c2, d2, e2, a2, w12, 8);
239 R51(a1, b1, c1, d1, e1, w0, 15);
240 R52(a2, b2, c2, d2, e2, w15, 5);
241 R51(e1, a1, b1, c1, d1, w5, 5);
242 R52(e2, a2, b2, c2, d2, w10, 12);
243 R51(d1, e1, a1, b1, c1, w9, 11);
244 R52(d2, e2, a2, b2, c2, w4, 9);
245 R51(c1, d1, e1, a1, b1, w7, 6);
246 R52(c2, d2, e2, a2, b2, w1, 12);
247 R51(b1, c1, d1, e1, a1, w12, 8);
248 R52(b2, c2, d2, e2, a2, w5, 5);
249 R51(a1, b1, c1, d1, e1, w2, 13);
250 R52(a2, b2, c2, d2, e2, w8, 14);
251 R51(e1, a1, b1, c1, d1, w10, 12);
252 R52(e2, a2, b2, c2, d2, w7, 6);
253 R51(d1, e1, a1, b1, c1, w14, 5);
254 R52(d2, e2, a2, b2, c2, w6, 8);
255 R51(c1, d1, e1, a1, b1, w1, 12);
256 R52(c2, d2, e2, a2, b2, w2, 13);
257 R51(b1, c1, d1, e1, a1, w3, 13);
258 R52(b2, c2, d2, e2, a2, w13, 6);
259 R51(a1, b1, c1, d1, e1, w8, 14);
260 R52(a2, b2, c2, d2, e2, w14, 5);
261 R51(e1, a1, b1, c1, d1, w11, 11);
262 R52(e2, a2, b2, c2, d2, w0, 15);
263 R51(d1, e1, a1, b1, c1, w6, 8);
264 R52(d2, e2, a2, b2, c2, w3, 13);
265 R51(c1, d1, e1, a1, b1, w15, 5);
266 R52(c2, d2, e2, a2, b2, w9, 11);
267 R51(b1, c1, d1, e1, a1, w13, 6);
268 R52(b2, c2, d2, e2, a2, w11, 11);
269
270 uint32_t t = s[0];
271 s[0] = s[1] + c1 + d2;
272 s[1] = s[2] + d1 + e2;
273 s[2] = s[3] + e1 + a2;
274 s[3] = s[4] + a1 + b2;
275 s[4] = t + b1 + c2;
276 }
277
278} // namespace ripemd160
279
280} // namespace
281
283
285 ripemd160::Initialize(s);
286}
287
288CRIPEMD160 &CRIPEMD160::Write(const uint8_t *data, size_t len) {
289 const uint8_t *end = data + len;
290 size_t bufsize = bytes % 64;
291 if (bufsize && bufsize + len >= 64) {
292 // Fill the buffer, and process it.
293 memcpy(buf + bufsize, data, 64 - bufsize);
294 bytes += 64 - bufsize;
295 data += 64 - bufsize;
297 bufsize = 0;
298 }
299 while (end - data >= 64) {
300 // Process full chunks directly from the source.
301 ripemd160::Transform(s, data);
302 bytes += 64;
303 data += 64;
304 }
305 if (end > data) {
306 // Fill the buffer with what remains.
307 memcpy(buf + bufsize, data, end - data);
308 bytes += end - data;
309 }
310 return *this;
311}
312
313void CRIPEMD160::Finalize(uint8_t hash[OUTPUT_SIZE]) {
314 static const uint8_t pad[64] = {0x80};
315 uint8_t sizedesc[8];
316 WriteLE64(sizedesc, bytes << 3);
317 Write(pad, 1 + ((119 - (bytes % 64)) % 64));
318 Write(sizedesc, 8);
319 WriteLE32(hash, s[0]);
320 WriteLE32(hash + 4, s[1]);
321 WriteLE32(hash + 8, s[2]);
322 WriteLE32(hash + 12, s[3]);
323 WriteLE32(hash + 16, s[4]);
324}
325
327 bytes = 0;
328 ripemd160::Initialize(s);
329 return *this;
330}
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
CRIPEMD160 & Write(const uint8_t *data, size_t len)
Definition: ripemd160.cpp:288
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:313
uint64_t bytes
Definition: ripemd160.h:16
uint8_t buf[64]
Definition: ripemd160.h:15
uint32_t s[5]
Definition: ripemd160.h:14
CRIPEMD160 & Reset()
Definition: ripemd160.cpp:326
static void WriteLE32(uint8_t *ptr, uint32_t x)
Definition: common.h:40
static uint32_t ReadLE32(const uint8_t *ptr)
Definition: common.h:23
static void WriteLE64(uint8_t *ptr, uint64_t x)
Definition: common.h:45
#define Round(a, b, c, d, e, f, g, h, k, w)
Definition: hash_impl.h:24
Internal RIPEMD-160 implementation.
Definition: ripemd160.cpp:14
void Transform(uint32_t *s, const uint8_t *chunk, size_t blocks)