forked from bitbegin/eth-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ripemd160.reds
376 lines (342 loc) · 11.3 KB
/
ripemd160.reds
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
Red/System [
Title: "ripemd160"
Author: "bitbegin"
File: %ripemd160.reds
Tabs: 4
Rights: "Copyright (C) 2011-2018 Red Foundation. All rights reserved."
License: "BSD-3 - https://github.com/red/red/blob/master/BSD-3-License.txt"
]
;-- transplanted from https://github.com/rhash/RHash/blob/master/librhash/has160.c
;-- common functions
#define ROTL32(dword n) [(dword) << (n) xor ((dword) >>> (32 - (n)))]
#define ROTR32(dword n) ((dword) >>> (n) xor ((dword) << (32 - (n))))
; Red/System not support integer64 yet
;#define ROTL64(qword n) ((qword) << (n) xor ((qword) >>> (64 - (n))))
;#define ROTR64(qword n) ((qword) >>> (n) xor ((qword) << (64 - (n))))
get-byte-at: func [arr [byte-ptr!] i [integer!] return: [byte!]
/local temp [byte-ptr!]
][
temp: arr + i
temp/value
]
get-int-at: func [arr [byte-ptr!] i [integer!] return: [integer!]
/local temp [byte-ptr!] ret [integer!]
][
temp: arr + (i * 4)
ret: as-integer temp/value
temp: temp + 1
ret: ret + ((as-integer temp/value) << 8)
temp: temp + 1
ret: ret + ((as-integer temp/value) << 16)
temp: temp + 1
ret: ret + ((as-integer temp/value) << 24)
ret
]
put-int-at: func [arr [byte-ptr!] i [integer!] value [integer!]
/local temp [byte-ptr!] b [byte!]
][
temp: arr + (i * 4)
temp/value: as byte! value
temp: temp + 1
temp/value: as byte! (value >>> 8)
temp: temp + 1
temp/value: as byte! (value >>> 16)
temp: temp + 1
temp/value: as byte! (value >>> 24)
]
ripemd160: context [
;-- five boolean functions
#define RMD_F1(x y z) ((x) xor (y) xor (z))
#define RMD_F2(x y z) ((((y) xor (z)) and (x)) xor (z))
#define RMD_F3(x y z) (((x) or not (y)) xor (z))
#define RMD_F4(x y z) ((((x) xor (y)) and (z)) xor (y))
#define RMD_F5(x y z) ((x) xor ((y) or not (z)))
#define RMD_FUNC(RFUNC A B C D E X S K) [
A: A + RFUNC((B) (C) (D)) + (X) + K
A: ROTL32((A) (S)) + (E)
C: ROTL32((C) 10)
]
;-- steps for the left and right half of algorithm
#define RMD_L1(A B C D E X S) [RMD_FUNC(RMD_F1 A B C D E X S 0)]
#define RMD_L2(A B C D E X S) [RMD_FUNC(RMD_F2 A B C D E X S 5A827999h)]
#define RMD_L3(A B C D E X S) [RMD_FUNC(RMD_F3 A B C D E X S 6ED9EBA1h)]
#define RMD_L4(A B C D E X S) [RMD_FUNC(RMD_F4 A B C D E X S 8F1BBCDCh)]
#define RMD_L5(A B C D E X S) [RMD_FUNC(RMD_F5 A B C D E X S A953FD4Eh)]
#define RMD_R1(A B C D E X S) [RMD_FUNC(RMD_F5 A B C D E X S 50A28BE6h)]
#define RMD_R2(A B C D E X S) [RMD_FUNC(RMD_F4 A B C D E X S 5C4DD124h)]
#define RMD_R3(A B C D E X S) [RMD_FUNC(RMD_F3 A B C D E X S 6D703EF3h)]
#define RMD_R4(A B C D E X S) [RMD_FUNC(RMD_F2 A B C D E X S 7A6D76E9h)]
#define RMD_R5(A B C D E X S) [RMD_FUNC(RMD_F1 A B C D E X S 0)]
length: 0
message: allocate 64
hash: [67452301h EFCDAB89h 98BADCFEh 10325476h C3D2E1F0h]
init: does [
length: 0
hash/1: 67452301h
hash/2: EFCDAB89h
hash/3: 98BADCFEh
hash/4: 10325476h
hash/5: C3D2E1F0h
set-memory message #"^(00)" 64
]
process-block: func [hash [int-ptr!] X [byte-ptr!]
/local
A [integer!]
B [integer!]
C [integer!]
D [integer!]
E [integer!]
a1 [integer!]
b1 [integer!]
c1 [integer!]
d1 [integer!]
e1 [integer!]
][
A: hash/1
B: hash/2
C: hash/3
D: hash/4
E: hash/5
;-- rounds of the left half
RMD_L1(A B C D E (get-int-at X 0) 11)
RMD_L1(E A B C D (get-int-at X 1) 14)
RMD_L1(D E A B C (get-int-at X 2) 15)
RMD_L1(C D E A B (get-int-at X 3) 12)
RMD_L1(B C D E A (get-int-at X 4) 5)
RMD_L1(A B C D E (get-int-at X 5) 8)
RMD_L1(E A B C D (get-int-at X 6) 7)
RMD_L1(D E A B C (get-int-at X 7) 9)
RMD_L1(C D E A B (get-int-at X 8) 11)
RMD_L1(B C D E A (get-int-at X 9) 13)
RMD_L1(A B C D E (get-int-at X 10) 14)
RMD_L1(E A B C D (get-int-at X 11) 15)
RMD_L1(D E A B C (get-int-at X 12) 6)
RMD_L1(C D E A B (get-int-at X 13) 7)
RMD_L1(B C D E A (get-int-at X 14) 9)
RMD_L1(A B C D E (get-int-at X 15) 8)
RMD_L2(E A B C D (get-int-at X 7) 7)
RMD_L2(D E A B C (get-int-at X 4) 6)
RMD_L2(C D E A B (get-int-at X 13) 8)
RMD_L2(B C D E A (get-int-at X 1) 13)
RMD_L2(A B C D E (get-int-at X 10) 11)
RMD_L2(E A B C D (get-int-at X 6) 9)
RMD_L2(D E A B C (get-int-at X 15) 7)
RMD_L2(C D E A B (get-int-at X 3) 15)
RMD_L2(B C D E A (get-int-at X 12) 7)
RMD_L2(A B C D E (get-int-at X 0) 12)
RMD_L2(E A B C D (get-int-at X 9) 15)
RMD_L2(D E A B C (get-int-at X 5) 9)
RMD_L2(C D E A B (get-int-at X 2) 11)
RMD_L2(B C D E A (get-int-at X 14) 7)
RMD_L2(A B C D E (get-int-at X 11) 13)
RMD_L2(E A B C D (get-int-at X 8) 12)
RMD_L3(D E A B C (get-int-at X 3) 11)
RMD_L3(C D E A B (get-int-at X 10) 13)
RMD_L3(B C D E A (get-int-at X 14) 6)
RMD_L3(A B C D E (get-int-at X 4) 7)
RMD_L3(E A B C D (get-int-at X 9) 14)
RMD_L3(D E A B C (get-int-at X 15) 9)
RMD_L3(C D E A B (get-int-at X 8) 13)
RMD_L3(B C D E A (get-int-at X 1) 15)
RMD_L3(A B C D E (get-int-at X 2) 14)
RMD_L3(E A B C D (get-int-at X 7) 8)
RMD_L3(D E A B C (get-int-at X 0) 13)
RMD_L3(C D E A B (get-int-at X 6) 6)
RMD_L3(B C D E A (get-int-at X 13) 5)
RMD_L3(A B C D E (get-int-at X 11) 12)
RMD_L3(E A B C D (get-int-at X 5) 7)
RMD_L3(D E A B C (get-int-at X 12) 5)
RMD_L4(C D E A B (get-int-at X 1) 11)
RMD_L4(B C D E A (get-int-at X 9) 12)
RMD_L4(A B C D E (get-int-at X 11) 14)
RMD_L4(E A B C D (get-int-at X 10) 15)
RMD_L4(D E A B C (get-int-at X 0) 14)
RMD_L4(C D E A B (get-int-at X 8) 15)
RMD_L4(B C D E A (get-int-at X 12) 9)
RMD_L4(A B C D E (get-int-at X 4) 8)
RMD_L4(E A B C D (get-int-at X 13) 9)
RMD_L4(D E A B C (get-int-at X 3) 14)
RMD_L4(C D E A B (get-int-at X 7) 5)
RMD_L4(B C D E A (get-int-at X 15) 6)
RMD_L4(A B C D E (get-int-at X 14) 8)
RMD_L4(E A B C D (get-int-at X 5) 6)
RMD_L4(D E A B C (get-int-at X 6) 5)
RMD_L4(C D E A B (get-int-at X 2) 12)
RMD_L5(B C D E A (get-int-at X 4) 9)
RMD_L5(A B C D E (get-int-at X 0) 15)
RMD_L5(E A B C D (get-int-at X 5) 5)
RMD_L5(D E A B C (get-int-at X 9) 11)
RMD_L5(C D E A B (get-int-at X 7) 6)
RMD_L5(B C D E A (get-int-at X 12) 8)
RMD_L5(A B C D E (get-int-at X 2) 13)
RMD_L5(E A B C D (get-int-at X 10) 12)
RMD_L5(D E A B C (get-int-at X 14) 5)
RMD_L5(C D E A B (get-int-at X 1) 12)
RMD_L5(B C D E A (get-int-at X 3) 13)
RMD_L5(A B C D E (get-int-at X 8) 14)
RMD_L5(E A B C D (get-int-at X 11) 11)
RMD_L5(D E A B C (get-int-at X 6) 8)
RMD_L5(C D E A B (get-int-at X 15) 5)
RMD_L5(B C D E A (get-int-at X 13) 6)
a1: A
b1: B
c1: C
d1: D
e1: E
A: hash/1
B: hash/2
C: hash/3
D: hash/4
E: hash/5
;-- rounds of the right half
RMD_R1(A B C D E (get-int-at X 5) 8)
RMD_R1(E A B C D (get-int-at X 14) 9)
RMD_R1(D E A B C (get-int-at X 7) 9)
RMD_R1(C D E A B (get-int-at X 0) 11)
RMD_R1(B C D E A (get-int-at X 9) 13)
RMD_R1(A B C D E (get-int-at X 2) 15)
RMD_R1(E A B C D (get-int-at X 11) 15)
RMD_R1(D E A B C (get-int-at X 4) 5)
RMD_R1(C D E A B (get-int-at X 13) 7)
RMD_R1(B C D E A (get-int-at X 6) 7)
RMD_R1(A B C D E (get-int-at X 15) 8)
RMD_R1(E A B C D (get-int-at X 8) 11)
RMD_R1(D E A B C (get-int-at X 1) 14)
RMD_R1(C D E A B (get-int-at X 10) 14)
RMD_R1(B C D E A (get-int-at X 3) 12)
RMD_R1(A B C D E (get-int-at X 12) 6)
RMD_R2(E A B C D (get-int-at X 6) 9)
RMD_R2(D E A B C (get-int-at X 11) 13)
RMD_R2(C D E A B (get-int-at X 3) 15)
RMD_R2(B C D E A (get-int-at X 7) 7)
RMD_R2(A B C D E (get-int-at X 0) 12)
RMD_R2(E A B C D (get-int-at X 13) 8)
RMD_R2(D E A B C (get-int-at X 5) 9)
RMD_R2(C D E A B (get-int-at X 10) 11)
RMD_R2(B C D E A (get-int-at X 14) 7)
RMD_R2(A B C D E (get-int-at X 15) 7)
RMD_R2(E A B C D (get-int-at X 8) 12)
RMD_R2(D E A B C (get-int-at X 12) 7)
RMD_R2(C D E A B (get-int-at X 4) 6)
RMD_R2(B C D E A (get-int-at X 9) 15)
RMD_R2(A B C D E (get-int-at X 1) 13)
RMD_R2(E A B C D (get-int-at X 2) 11)
RMD_R3(D E A B C (get-int-at X 15) 9)
RMD_R3(C D E A B (get-int-at X 5) 7)
RMD_R3(B C D E A (get-int-at X 1) 15)
RMD_R3(A B C D E (get-int-at X 3) 11)
RMD_R3(E A B C D (get-int-at X 7) 8)
RMD_R3(D E A B C (get-int-at X 14) 6)
RMD_R3(C D E A B (get-int-at X 6) 6)
RMD_R3(B C D E A (get-int-at X 9) 14)
RMD_R3(A B C D E (get-int-at X 11) 12)
RMD_R3(E A B C D (get-int-at X 8) 13)
RMD_R3(D E A B C (get-int-at X 12) 5)
RMD_R3(C D E A B (get-int-at X 2) 14)
RMD_R3(B C D E A (get-int-at X 10) 13)
RMD_R3(A B C D E (get-int-at X 0) 13)
RMD_R3(E A B C D (get-int-at X 4) 7)
RMD_R3(D E A B C (get-int-at X 13) 5)
RMD_R4(C D E A B (get-int-at X 8) 15)
RMD_R4(B C D E A (get-int-at X 6) 5)
RMD_R4(A B C D E (get-int-at X 4) 8)
RMD_R4(E A B C D (get-int-at X 1) 11)
RMD_R4(D E A B C (get-int-at X 3) 14)
RMD_R4(C D E A B (get-int-at X 11) 14)
RMD_R4(B C D E A (get-int-at X 15) 6)
RMD_R4(A B C D E (get-int-at X 0) 14)
RMD_R4(E A B C D (get-int-at X 5) 6)
RMD_R4(D E A B C (get-int-at X 12) 9)
RMD_R4(C D E A B (get-int-at X 2) 12)
RMD_R4(B C D E A (get-int-at X 13) 9)
RMD_R4(A B C D E (get-int-at X 9) 12)
RMD_R4(E A B C D (get-int-at X 7) 5)
RMD_R4(D E A B C (get-int-at X 10) 15)
RMD_R4(C D E A B (get-int-at X 14) 8)
RMD_R5(B C D E A (get-int-at X 12) 8)
RMD_R5(A B C D E (get-int-at X 15) 5)
RMD_R5(E A B C D (get-int-at X 10) 12)
RMD_R5(D E A B C (get-int-at X 4) 9)
RMD_R5(C D E A B (get-int-at X 1) 12)
RMD_R5(B C D E A (get-int-at X 5) 5)
RMD_R5(A B C D E (get-int-at X 8) 14)
RMD_R5(E A B C D (get-int-at X 7) 6)
RMD_R5(D E A B C (get-int-at X 6) 8)
RMD_R5(C D E A B (get-int-at X 2) 13)
RMD_R5(B C D E A (get-int-at X 13) 6)
RMD_R5(A B C D E (get-int-at X 14) 5)
RMD_R5(E A B C D (get-int-at X 0) 15)
RMD_R5(D E A B C (get-int-at X 3) 13)
RMD_R5(C D E A B (get-int-at X 9) 11)
RMD_R5(B C D E A (get-int-at X 11) 11)
;-- update intermediate hash
D: D + c1 + hash/2
hash/2: hash/3 + d1 + E
hash/3: hash/4 + e1 + A
hash/4: hash/5 + a1 + B
hash/5: hash/1 + b1 + C
hash/1: D
]
update: func [msg [byte-ptr!] size [integer!]
/local
index [integer!]
left [integer!]
][
index: length and 63
length: length + size
;-- fill partial block
if index <> 0 [
left: 64 - index
copy-memory message + index msg either size < left [size][left]
if size < left [exit]
process-block hash message
msg: msg + left
size: size - left
]
while [size >= 64][
process-block hash msg
msg: msg + 64
size: size - 64
]
if size <> 0 [
copy-memory message msg size
]
]
final: func [result [byte-ptr!]
/local
index [integer!]
shift [integer!]
p [int-ptr!]
temp [integer!]
][
index: (length and 63) >> 2
shift: (length and 3) * 8
temp: not (FFFFFFFFh << shift)
temp: temp and get-int-at message index
put-int-at message index temp
temp: 80h << shift
temp: temp xor get-int-at message index
put-int-at message index temp
index: index + 1
if index > 14 [
while [index < 16][
put-int-at message index 0
index: index + 1
]
process-block hash message
index: 0
]
while [index < 14][
put-int-at message index 0
index: index + 1
]
put-int-at message 14 length << 3
put-int-at message 15 length >>> 29
process-block hash message
put-int-at result 0 hash/1
put-int-at result 1 hash/2
put-int-at result 2 hash/3
put-int-at result 3 hash/4
put-int-at result 4 hash/5
]
]