-
Notifications
You must be signed in to change notification settings - Fork 3
/
sha512.wat
651 lines (566 loc) · 38.9 KB
/
sha512.wat
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
(module
(memory (export "memory") 1)
;; registers
(global $a (mut i64) (i64.const 0))
(global $b (mut i64) (i64.const 0))
(global $c (mut i64) (i64.const 0))
(global $d (mut i64) (i64.const 0))
(global $e (mut i64) (i64.const 0))
(global $f (mut i64) (i64.const 0))
(global $g (mut i64) (i64.const 0))
(global $h (mut i64) (i64.const 0))
(func $i64.bswap
(param $b i64)
(result i64)
;; 1 set, 4 get, 8 const, 10 bitwise
(i64.or
(get_local $b)
(i64.const 0xFFFF0000FFFF0000)
(i64.and)
(i64.rotl (i64.const 16))
(get_local $b)
(i64.const 0x0000FFFF0000FFFF)
(i64.and)
(i64.rotr (i64.const 16)))
(set_local $b)
(i64.or
(get_local $b)
(i64.const 0x00FF00FF00FF00FF)
(i64.and)
(i64.rotl (i64.const 8))
(get_local $b)
(i64.const 0xFF00FF00FF00FF00)
(i64.and)
(i64.rotr (i64.const 8))))
(func $four_round
(param $w0 i64) (param $w1 i64) (param $w2 i64) (param $w3 i64)
(param $k0 i64) (param $k1 i64) (param $k2 i64) (param $k3 i64)
;; precomputed values
(local $T1 i64)
(local $T2 i64)
(local $ch_res i64)
(local $maj_res i64)
(local $big_sig0_res i64)
(local $big_sig1_res i64)
;; precompute intermediate values
;; T1 = h + big_sig1(e) + ch(e, f, g) + K0 + W0
;; T2 = big_sig0(a) + Maj(a, b, c)
;; can compute 4 rounds independently
(set_local $ch_res (i64.xor (i64.and (get_global $e) (get_global $f)) (i64.and (i64.xor (get_global $e) (i64.const -1)) (get_global $g))))
(set_local $maj_res (i64.xor (i64.xor (i64.and (get_global $a) (get_global $b)) (i64.and (get_global $a) (get_global $c))) (i64.and (get_global $b) (get_global $c))))
(set_local $big_sig0_res (i64.xor (i64.xor (i64.rotr (get_global $a) (i64.const 28)) (i64.rotr (get_global $a) (i64.const 34))) (i64.rotr (get_global $a) (i64.const 39))))
(set_local $big_sig1_res (i64.xor (i64.xor (i64.rotr (get_global $e) (i64.const 14)) (i64.rotr (get_global $e) (i64.const 18))) (i64.rotr (get_global $e) (i64.const 41))))
(set_local $T1 (i64.add (i64.add (i64.add (i64.add (get_global $h) (get_local $ch_res)) (get_local $big_sig1_res)) (get_local $w0)) (get_local $k0)))
(set_local $T2 (i64.add (get_local $big_sig0_res) (get_local $maj_res)))
(set_global $h (i64.add (get_global $d) (get_local $T1)))
(set_global $d (i64.add (get_local $T1) (get_local $T2)))
(set_local $ch_res (i64.xor (i64.and (get_global $h) (get_global $e)) (i64.and (i64.xor (get_global $h) (i64.const -1)) (get_global $f))))
(set_local $maj_res (i64.xor (i64.xor (i64.and (get_global $d) (get_global $a)) (i64.and (get_global $d) (get_global $b))) (i64.and (get_global $a) (get_global $b))))
(set_local $big_sig0_res (i64.xor (i64.xor (i64.rotr (get_global $d) (i64.const 28)) (i64.rotr (get_global $d) (i64.const 34))) (i64.rotr (get_global $d) (i64.const 39))))
(set_local $big_sig1_res (i64.xor (i64.xor (i64.rotr (get_global $h) (i64.const 14)) (i64.rotr (get_global $h) (i64.const 18))) (i64.rotr (get_global $h) (i64.const 41))))
(set_local $T1 (i64.add (i64.add (i64.add (i64.add (get_global $g) (get_local $ch_res)) (get_local $big_sig1_res)) (get_local $w1)) (get_local $k1)))
(set_local $T2 (i64.add (get_local $big_sig0_res) (get_local $maj_res)))
(set_global $g (i64.add (get_global $c) (get_local $T1)))
(set_global $c (i64.add (get_local $T1) (get_local $T2)))
(set_local $ch_res (i64.xor (i64.and (get_global $g) (get_global $h)) (i64.and (i64.xor (get_global $g) (i64.const -1)) (get_global $e))))
(set_local $maj_res (i64.xor (i64.xor (i64.and (get_global $c) (get_global $d)) (i64.and (get_global $c) (get_global $a))) (i64.and (get_global $d) (get_global $a))))
(set_local $big_sig0_res (i64.xor (i64.xor (i64.rotr (get_global $c) (i64.const 28)) (i64.rotr (get_global $c) (i64.const 34))) (i64.rotr (get_global $c) (i64.const 39))))
(set_local $big_sig1_res (i64.xor (i64.xor (i64.rotr (get_global $g) (i64.const 14)) (i64.rotr (get_global $g) (i64.const 18))) (i64.rotr (get_global $g) (i64.const 41))))
(set_local $T1 (i64.add (i64.add (i64.add (i64.add (get_global $f) (get_local $ch_res)) (get_local $big_sig1_res)) (get_local $w2)) (get_local $k2)))
(set_local $T2 (i64.add (get_local $big_sig0_res) (get_local $maj_res)))
(set_global $f (i64.add (get_global $b) (get_local $T1)))
(set_global $b (i64.add (get_local $T1) (get_local $T2)))
(set_local $ch_res (i64.xor (i64.and (get_global $f) (get_global $g)) (i64.and (i64.xor (get_global $f) (i64.const -1)) (get_global $h))))
(set_local $maj_res (i64.xor (i64.xor (i64.and (get_global $b) (get_global $c)) (i64.and (get_global $b) (get_global $d))) (i64.and (get_global $d) (get_global $c))))
(set_local $big_sig0_res (i64.xor (i64.xor (i64.rotr (get_global $b) (i64.const 28)) (i64.rotr (get_global $b) (i64.const 34))) (i64.rotr (get_global $b) (i64.const 39))))
(set_local $big_sig1_res (i64.xor (i64.xor (i64.rotr (get_global $f) (i64.const 14)) (i64.rotr (get_global $f) (i64.const 18))) (i64.rotr (get_global $f) (i64.const 41))))
(set_local $T1 (i64.add (i64.add (i64.add (i64.add (get_global $e) (get_local $ch_res)) (get_local $big_sig1_res)) (get_local $w3)) (get_local $k3)))
(set_local $T2 (i64.add (get_local $big_sig0_res) (get_local $maj_res)))
(set_global $e (i64.add (get_global $a) (get_local $T1)))
(set_global $a (i64.add (get_local $T1) (get_local $T2))))
(func $expand
(param $a i64) (param $b i64) (param $c i64) (param $d i64)
(result i64)
(i64.add
(i64.add
(i64.add
(i64.xor
(i64.xor
(i64.rotr (get_local $a) (i64.const 19))
(i64.rotr (get_local $a) (i64.const 61)))
(i64.shr_u (get_local $a) (i64.const 6)))
(get_local $b))
(i64.xor
(i64.xor
(i64.rotr (get_local $c) (i64.const 1))
(i64.rotr (get_local $c) (i64.const 8)))
(i64.shr_u (get_local $c) (i64.const 7)))
(get_local $d))))
(func $compress
(param $ctx i32)
(param $w0 i64) (param $w1 i64) (param $w2 i64) (param $w3 i64)
(param $w4 i64) (param $w5 i64) (param $w6 i64) (param $w7 i64)
(param $w8 i64) (param $w9 i64) (param $w10 i64) (param $w11 i64)
(param $w12 i64) (param $w13 i64) (param $w14 i64) (param $w15 i64)
;; store inital state
(if (i64.eq (i64.load offset=208 (get_local $ctx)) (i64.const 0))
(then
(i64.store offset=0 (get_local $ctx) (i64.const 0x6a09e667f3bcc908))
(i64.store offset=8 (get_local $ctx) (i64.const 0xbb67ae8584caa73b))
(i64.store offset=16 (get_local $ctx) (i64.const 0x3c6ef372fe94f82b))
(i64.store offset=24 (get_local $ctx) (i64.const 0xa54ff53a5f1d36f1))
(i64.store offset=32 (get_local $ctx) (i64.const 0x510e527fade682d1))
(i64.store offset=40 (get_local $ctx) (i64.const 0x9b05688c2b3e6c1f))
(i64.store offset=48 (get_local $ctx) (i64.const 0x1f83d9abfb41bd6b))
(i64.store offset=56 (get_local $ctx) (i64.const 0x5be0cd19137e2179))
(i64.store offset=208 (get_local $ctx) (i64.const 1))))
;; load previous hash state into registers
(set_global $a (i64.load offset=0 (get_local $ctx)))
(set_global $b (i64.load offset=8 (get_local $ctx)))
(set_global $c (i64.load offset=16 (get_local $ctx)))
(set_global $d (i64.load offset=24 (get_local $ctx)))
(set_global $e (i64.load offset=32 (get_local $ctx)))
(set_global $f (i64.load offset=40 (get_local $ctx)))
(set_global $g (i64.load offset=48 (get_local $ctx)))
(set_global $h (i64.load offset=56 (get_local $ctx)))
(set_local $w0 (call $i64.bswap (get_local $w0 )))
(set_local $w1 (call $i64.bswap (get_local $w1 )))
(set_local $w2 (call $i64.bswap (get_local $w2 )))
(set_local $w3 (call $i64.bswap (get_local $w3 )))
(set_local $w4 (call $i64.bswap (get_local $w4 )))
(set_local $w5 (call $i64.bswap (get_local $w5 )))
(set_local $w6 (call $i64.bswap (get_local $w6 )))
(set_local $w7 (call $i64.bswap (get_local $w7 )))
(set_local $w8 (call $i64.bswap (get_local $w8 )))
(set_local $w9 (call $i64.bswap (get_local $w9 )))
(set_local $w10 (call $i64.bswap (get_local $w10)))
(set_local $w11 (call $i64.bswap (get_local $w11)))
(set_local $w12 (call $i64.bswap (get_local $w12)))
(set_local $w13 (call $i64.bswap (get_local $w13)))
(set_local $w14 (call $i64.bswap (get_local $w14)))
(set_local $w15 (call $i64.bswap (get_local $w15)))
(call $four_round (get_local $w0) (get_local $w1) (get_local $w2) (get_local $w3) (i64.const 0x428a2f98d728ae22) (i64.const 0x7137449123ef65cd) (i64.const 0xb5c0fbcfec4d3b2f) (i64.const 0xe9b5dba58189dbbc))
(call $four_round (get_local $w4) (get_local $w5) (get_local $w6) (get_local $w7) (i64.const 0x3956c25bf348b538) (i64.const 0x59f111f1b605d019) (i64.const 0x923f82a4af194f9b) (i64.const 0xab1c5ed5da6d8118))
(call $four_round (get_local $w8) (get_local $w9) (get_local $w10) (get_local $w11) (i64.const 0xd807aa98a3030242) (i64.const 0x12835b0145706fbe) (i64.const 0x243185be4ee4b28c) (i64.const 0x550c7dc3d5ffb4e2))
(call $four_round (get_local $w12) (get_local $w13) (get_local $w14) (get_local $w15) (i64.const 0x72be5d74f27b896f) (i64.const 0x80deb1fe3b1696b1) (i64.const 0x9bdc06a725c71235) (i64.const 0xc19bf174cf692694))
(set_local $w0 (call $expand (get_local $w14) (get_local $w9) (get_local $w1) (get_local $w0)))
(set_local $w1 (call $expand (get_local $w15) (get_local $w10) (get_local $w2) (get_local $w1)))
(set_local $w2 (call $expand (get_local $w0 ) (get_local $w11) (get_local $w3) (get_local $w2)))
(set_local $w3 (call $expand (get_local $w1 ) (get_local $w12) (get_local $w4) (get_local $w3)))
(set_local $w4 (call $expand (get_local $w2 ) (get_local $w13) (get_local $w5) (get_local $w4)))
(set_local $w5 (call $expand (get_local $w3 ) (get_local $w14) (get_local $w6) (get_local $w5)))
(set_local $w6 (call $expand (get_local $w4 ) (get_local $w15) (get_local $w7) (get_local $w6)))
(set_local $w7 (call $expand (get_local $w5 ) (get_local $w0) (get_local $w8) (get_local $w7)))
(set_local $w8 (call $expand (get_local $w6 ) (get_local $w1) (get_local $w9) (get_local $w8)))
(set_local $w9 (call $expand (get_local $w7 ) (get_local $w2) (get_local $w10) (get_local $w9)))
(set_local $w10 (call $expand (get_local $w8 ) (get_local $w3) (get_local $w11) (get_local $w10)))
(set_local $w11 (call $expand (get_local $w9 ) (get_local $w4) (get_local $w12) (get_local $w11)))
(set_local $w12 (call $expand (get_local $w10) (get_local $w5) (get_local $w13) (get_local $w12)))
(set_local $w13 (call $expand (get_local $w11) (get_local $w6) (get_local $w14) (get_local $w13)))
(set_local $w14 (call $expand (get_local $w12) (get_local $w7) (get_local $w15) (get_local $w14)))
(set_local $w15 (call $expand (get_local $w13) (get_local $w8) (get_local $w0) (get_local $w15)))
(call $four_round (get_local $w0) (get_local $w1) (get_local $w2) (get_local $w3) (i64.const 0xe49b69c19ef14ad2) (i64.const 0xefbe4786384f25e3) (i64.const 0x0fc19dc68b8cd5b5) (i64.const 0x240ca1cc77ac9c65))
(call $four_round (get_local $w4) (get_local $w5) (get_local $w6) (get_local $w7) (i64.const 0x2de92c6f592b0275) (i64.const 0x4a7484aa6ea6e483) (i64.const 0x5cb0a9dcbd41fbd4) (i64.const 0x76f988da831153b5))
(call $four_round (get_local $w8) (get_local $w9) (get_local $w10) (get_local $w11) (i64.const 0x983e5152ee66dfab) (i64.const 0xa831c66d2db43210) (i64.const 0xb00327c898fb213f) (i64.const 0xbf597fc7beef0ee4))
(call $four_round (get_local $w12) (get_local $w13) (get_local $w14) (get_local $w15) (i64.const 0xc6e00bf33da88fc2) (i64.const 0xd5a79147930aa725) (i64.const 0x06ca6351e003826f) (i64.const 0x142929670a0e6e70))
(set_local $w0 (call $expand (get_local $w14) (get_local $w9) (get_local $w1) (get_local $w0)))
(set_local $w1 (call $expand (get_local $w15) (get_local $w10) (get_local $w2) (get_local $w1)))
(set_local $w2 (call $expand (get_local $w0) (get_local $w11) (get_local $w3) (get_local $w2)))
(set_local $w3 (call $expand (get_local $w1) (get_local $w12) (get_local $w4) (get_local $w3)))
(set_local $w4 (call $expand (get_local $w2) (get_local $w13) (get_local $w5) (get_local $w4)))
(set_local $w5 (call $expand (get_local $w3) (get_local $w14) (get_local $w6) (get_local $w5)))
(set_local $w6 (call $expand (get_local $w4) (get_local $w15) (get_local $w7) (get_local $w6)))
(set_local $w7 (call $expand (get_local $w5) (get_local $w0) (get_local $w8) (get_local $w7)))
(set_local $w8 (call $expand (get_local $w6) (get_local $w1) (get_local $w9) (get_local $w8)))
(set_local $w9 (call $expand (get_local $w7) (get_local $w2) (get_local $w10) (get_local $w9)))
(set_local $w10 (call $expand (get_local $w8) (get_local $w3) (get_local $w11) (get_local $w10)))
(set_local $w11 (call $expand (get_local $w9) (get_local $w4) (get_local $w12) (get_local $w11)))
(set_local $w12 (call $expand (get_local $w10) (get_local $w5) (get_local $w13) (get_local $w12)))
(set_local $w13 (call $expand (get_local $w11) (get_local $w6) (get_local $w14) (get_local $w13)))
(set_local $w14 (call $expand (get_local $w12) (get_local $w7) (get_local $w15) (get_local $w14)))
(set_local $w15 (call $expand (get_local $w13) (get_local $w8) (get_local $w0) (get_local $w15)))
(call $four_round (get_local $w0) (get_local $w1) (get_local $w2) (get_local $w3) (i64.const 0x27b70a8546d22ffc) (i64.const 0x2e1b21385c26c926) (i64.const 0x4d2c6dfc5ac42aed) (i64.const 0x53380d139d95b3df))
(call $four_round (get_local $w4) (get_local $w5) (get_local $w6) (get_local $w7) (i64.const 0x650a73548baf63de) (i64.const 0x766a0abb3c77b2a8) (i64.const 0x81c2c92e47edaee6) (i64.const 0x92722c851482353b))
(call $four_round (get_local $w8) (get_local $w9) (get_local $w10) (get_local $w11) (i64.const 0xa2bfe8a14cf10364) (i64.const 0xa81a664bbc423001) (i64.const 0xc24b8b70d0f89791) (i64.const 0xc76c51a30654be30))
(call $four_round (get_local $w12) (get_local $w13) (get_local $w14) (get_local $w15) (i64.const 0xd192e819d6ef5218) (i64.const 0xd69906245565a910) (i64.const 0xf40e35855771202a) (i64.const 0x106aa07032bbd1b8))
(set_local $w0 (call $expand (get_local $w14) (get_local $w9) (get_local $w1) (get_local $w0)))
(set_local $w1 (call $expand (get_local $w15) (get_local $w10) (get_local $w2) (get_local $w1)))
(set_local $w2 (call $expand (get_local $w0) (get_local $w11) (get_local $w3) (get_local $w2)))
(set_local $w3 (call $expand (get_local $w1) (get_local $w12) (get_local $w4) (get_local $w3)))
(set_local $w4 (call $expand (get_local $w2) (get_local $w13) (get_local $w5) (get_local $w4)))
(set_local $w5 (call $expand (get_local $w3) (get_local $w14) (get_local $w6) (get_local $w5)))
(set_local $w6 (call $expand (get_local $w4) (get_local $w15) (get_local $w7) (get_local $w6)))
(set_local $w7 (call $expand (get_local $w5) (get_local $w0) (get_local $w8) (get_local $w7)))
(set_local $w8 (call $expand (get_local $w6) (get_local $w1) (get_local $w9) (get_local $w8)))
(set_local $w9 (call $expand (get_local $w7) (get_local $w2) (get_local $w10) (get_local $w9)))
(set_local $w10 (call $expand (get_local $w8) (get_local $w3) (get_local $w11) (get_local $w10)))
(set_local $w11 (call $expand (get_local $w9) (get_local $w4) (get_local $w12) (get_local $w11)))
(set_local $w12 (call $expand (get_local $w10) (get_local $w5) (get_local $w13) (get_local $w12)))
(set_local $w13 (call $expand (get_local $w11) (get_local $w6) (get_local $w14) (get_local $w13)))
(set_local $w14 (call $expand (get_local $w12) (get_local $w7) (get_local $w15) (get_local $w14)))
(set_local $w15 (call $expand (get_local $w13) (get_local $w8) (get_local $w0) (get_local $w15)))
(call $four_round (get_local $w0) (get_local $w1) (get_local $w2) (get_local $w3) (i64.const 0x19a4c116b8d2d0c8) (i64.const 0x1e376c085141ab53) (i64.const 0x2748774cdf8eeb99) (i64.const 0x34b0bcb5e19b48a8))
(call $four_round (get_local $w4) (get_local $w5) (get_local $w6) (get_local $w7) (i64.const 0x391c0cb3c5c95a63) (i64.const 0x4ed8aa4ae3418acb) (i64.const 0x5b9cca4f7763e373) (i64.const 0x682e6ff3d6b2b8a3))
(call $four_round (get_local $w8) (get_local $w9) (get_local $w10) (get_local $w11) (i64.const 0x748f82ee5defb2fc) (i64.const 0x78a5636f43172f60) (i64.const 0x84c87814a1f0ab72) (i64.const 0x8cc702081a6439ec))
(call $four_round (get_local $w12) (get_local $w13) (get_local $w14) (get_local $w15) (i64.const 0x90befffa23631e28) (i64.const 0xa4506cebde82bde9) (i64.const 0xbef9a3f7b2c67915) (i64.const 0xc67178f2e372532b))
(set_local $w0 (call $expand (get_local $w14) (get_local $w9) (get_local $w1) (get_local $w0)))
(set_local $w1 (call $expand (get_local $w15) (get_local $w10) (get_local $w2) (get_local $w1)))
(set_local $w2 (call $expand (get_local $w0) (get_local $w11) (get_local $w3) (get_local $w2)))
(set_local $w3 (call $expand (get_local $w1) (get_local $w12) (get_local $w4) (get_local $w3)))
(set_local $w4 (call $expand (get_local $w2) (get_local $w13) (get_local $w5) (get_local $w4)))
(set_local $w5 (call $expand (get_local $w3) (get_local $w14) (get_local $w6) (get_local $w5)))
(set_local $w6 (call $expand (get_local $w4) (get_local $w15) (get_local $w7) (get_local $w6)))
(set_local $w7 (call $expand (get_local $w5) (get_local $w0) (get_local $w8) (get_local $w7)))
(set_local $w8 (call $expand (get_local $w6) (get_local $w1) (get_local $w9) (get_local $w8)))
(set_local $w9 (call $expand (get_local $w7) (get_local $w2) (get_local $w10) (get_local $w9)))
(set_local $w10 (call $expand (get_local $w8) (get_local $w3) (get_local $w11) (get_local $w10)))
(set_local $w11 (call $expand (get_local $w9) (get_local $w4) (get_local $w12) (get_local $w11)))
(set_local $w12 (call $expand (get_local $w10) (get_local $w5) (get_local $w13) (get_local $w12)))
(set_local $w13 (call $expand (get_local $w11) (get_local $w6) (get_local $w14) (get_local $w13)))
(set_local $w14 (call $expand (get_local $w12) (get_local $w7) (get_local $w15) (get_local $w14)))
(set_local $w15 (call $expand (get_local $w13) (get_local $w8) (get_local $w0) (get_local $w15)))
(call $four_round (get_local $w0) (get_local $w1) (get_local $w2) (get_local $w3) (i64.const 0xca273eceea26619c) (i64.const 0xd186b8c721c0c207) (i64.const 0xeada7dd6cde0eb1e) (i64.const 0xf57d4f7fee6ed178))
(call $four_round (get_local $w4) (get_local $w5) (get_local $w6) (get_local $w7) (i64.const 0x06f067aa72176fba) (i64.const 0x0a637dc5a2c898a6) (i64.const 0x113f9804bef90dae) (i64.const 0x1b710b35131c471b))
(call $four_round (get_local $w8) (get_local $w9) (get_local $w10) (get_local $w11) (i64.const 0x28db77f523047d84) (i64.const 0x32caab7b40c72493) (i64.const 0x3c9ebe0a15c9bebc) (i64.const 0x431d67c49c100d4c))
(call $four_round (get_local $w12) (get_local $w13) (get_local $w14) (get_local $w15) (i64.const 0x4cc5d4becb3e42b6) (i64.const 0x597f299cfc657e2a) (i64.const 0x5fcb6fab3ad6faec) (i64.const 0x6c44198c4a475817))
;; store hash values
(i64.store offset=0 (get_local $ctx) (i64.add (i64.load offset=0 (get_local $ctx)) (get_global $a)))
(i64.store offset=8 (get_local $ctx) (i64.add (i64.load offset=8 (get_local $ctx)) (get_global $b)))
(i64.store offset=16 (get_local $ctx) (i64.add (i64.load offset=16 (get_local $ctx)) (get_global $c)))
(i64.store offset=24 (get_local $ctx) (i64.add (i64.load offset=24 (get_local $ctx)) (get_global $d)))
(i64.store offset=32 (get_local $ctx) (i64.add (i64.load offset=32 (get_local $ctx)) (get_global $e)))
(i64.store offset=40 (get_local $ctx) (i64.add (i64.load offset=40 (get_local $ctx)) (get_global $f)))
(i64.store offset=48 (get_local $ctx) (i64.add (i64.load offset=48 (get_local $ctx)) (get_global $g)))
(i64.store offset=56 (get_local $ctx) (i64.add (i64.load offset=56 (get_local $ctx)) (get_global $h))))
(func $sha512 (export "sha512") (param $ctx i32) (param $roi i32) (param $length i32) (param $final i32)
;; (result i32)
;; schema 208 bytes
;; 0..64 hash state
;; 64..80 number of bytes read across all updates (128bit)
;; 80..208 store words between updates
;; 208..216 init flag
(local $bytes_read i64)
(local $bytes_read_overflow i64)
(local $check_overflow i64)
(local $last_word i64)
(local $tail i64)
;; expanded message schedule
(local $w0 i64) (local $w1 i64) (local $w2 i64) (local $w3 i64)
(local $w4 i64) (local $w5 i64) (local $w6 i64) (local $w7 i64)
(local $w8 i64) (local $w9 i64) (local $w10 i64) (local $w11 i64)
(local $w12 i64) (local $w13 i64) (local $w14 i64) (local $w15 i64)
(set_local $bytes_read (i64.load offset=64 (get_local $ctx)))
(set_local $bytes_read_overflow (i64.load offset=72 (get_local $ctx)))
(set_local $tail (i64.add (i64.and (get_local $bytes_read) (i64.const 0x7f)) (i64.extend_u/i32 (get_local $length))))
;; load current block position
(set_local $check_overflow (get_local $bytes_read))
(set_local $bytes_read (i64.add (get_local $bytes_read) (i64.extend_u/i32 (get_local $length))))
(i64.store offset=64 (get_local $ctx) (get_local $bytes_read))
;; carry n > 64 bits for i128 length
(if (i64.lt_u (get_local $bytes_read) (get_local $check_overflow))
(then
(set_local $bytes_read_overflow (i64.add (get_local $bytes_read_overflow) (i64.const 1)))
(i64.store offset=72 (get_local $ctx) (get_local $bytes_read_overflow))))
(block $finish
(set_local $w0 (i64.load offset=80 (get_local $ctx)))
(set_local $w1 (i64.load offset=88 (get_local $ctx)))
(set_local $w2 (i64.load offset=96 (get_local $ctx)))
(set_local $w3 (i64.load offset=104 (get_local $ctx)))
(set_local $w4 (i64.load offset=112 (get_local $ctx)))
(set_local $w5 (i64.load offset=120 (get_local $ctx)))
(set_local $w6 (i64.load offset=128 (get_local $ctx)))
(set_local $w7 (i64.load offset=136 (get_local $ctx)))
(set_local $w8 (i64.load offset=144 (get_local $ctx)))
(set_local $w9 (i64.load offset=152 (get_local $ctx)))
(set_local $w10 (i64.load offset=160 (get_local $ctx)))
(set_local $w11 (i64.load offset=168 (get_local $ctx)))
(set_local $w12 (i64.load offset=176 (get_local $ctx)))
(set_local $w13 (i64.load offset=184 (get_local $ctx)))
(set_local $w14 (i64.load offset=192 (get_local $ctx)))
(set_local $w15 (i64.load offset=200 (get_local $ctx)))
(tee_local $tail (i64.sub (get_local $tail) (i64.const 128)))
(i64.const 0)
(i64.lt_s)
(br_if $finish)
(get_local $ctx)
(get_local $w0 )
(get_local $w1 )
(get_local $w2 )
(get_local $w3 )
(get_local $w4 )
(get_local $w5 )
(get_local $w6 )
(get_local $w7 )
(get_local $w8 )
(get_local $w9 )
(get_local $w10)
(get_local $w11)
(get_local $w12)
(get_local $w13)
(get_local $w14)
(get_local $w15)
(call $compress)
(loop $rest_of_input
(set_local $w0 (i64.load offset=0 (get_local $roi)))
(set_local $w1 (i64.load offset=8 (get_local $roi)))
(set_local $w2 (i64.load offset=16 (get_local $roi)))
(set_local $w3 (i64.load offset=24 (get_local $roi)))
(set_local $w4 (i64.load offset=32 (get_local $roi)))
(set_local $w5 (i64.load offset=40 (get_local $roi)))
(set_local $w6 (i64.load offset=48 (get_local $roi)))
(set_local $w7 (i64.load offset=56 (get_local $roi)))
(set_local $w8 (i64.load offset=64 (get_local $roi)))
(set_local $w9 (i64.load offset=72 (get_local $roi)))
(set_local $w10 (i64.load offset=80 (get_local $roi)))
(set_local $w11 (i64.load offset=88 (get_local $roi)))
(set_local $w12 (i64.load offset=96 (get_local $roi)))
(set_local $w13 (i64.load offset=104 (get_local $roi)))
(set_local $w14 (i64.load offset=112 (get_local $roi)))
(set_local $w15 (i64.load offset=120 (get_local $roi)))
(set_local $roi (i32.add (get_local $roi) (i32.const 128)))
(tee_local $tail (i64.sub (get_local $tail) (i64.const 128)))
(i64.const 0)
(i64.lt_s)
(if
(then
(i64.store offset=80 (get_local $ctx) (get_local $w0))
(i64.store offset=88 (get_local $ctx) (get_local $w1))
(i64.store offset=96 (get_local $ctx) (get_local $w2))
(i64.store offset=104 (get_local $ctx) (get_local $w3))
(i64.store offset=112 (get_local $ctx) (get_local $w4))
(i64.store offset=120 (get_local $ctx) (get_local $w5))
(i64.store offset=128 (get_local $ctx) (get_local $w6))
(i64.store offset=136 (get_local $ctx) (get_local $w7))
(i64.store offset=144 (get_local $ctx) (get_local $w8))
(i64.store offset=152 (get_local $ctx) (get_local $w9))
(i64.store offset=160 (get_local $ctx) (get_local $w10))
(i64.store offset=168 (get_local $ctx) (get_local $w11))
(i64.store offset=176 (get_local $ctx) (get_local $w12))
(i64.store offset=184 (get_local $ctx) (get_local $w13))
(i64.store offset=192 (get_local $ctx) (get_local $w14))
(i64.store offset=200 (get_local $ctx) (get_local $w15))
(br $finish)))
(get_local $ctx)
(get_local $w0 )
(get_local $w1 )
(get_local $w2 )
(get_local $w3 )
(get_local $w4 )
(get_local $w5 )
(get_local $w6 )
(get_local $w7 )
(get_local $w8 )
(get_local $w9 )
(get_local $w10)
(get_local $w11)
(get_local $w12)
(get_local $w13)
(get_local $w14)
(get_local $w15)
(call $compress)
(br $rest_of_input)))
(if (i32.eq (get_local $final) (i32.const 1))
(then
(set_local $tail (i64.and (get_local $bytes_read) (i64.const 0x7f)))
(set_local $last_word (i64.shl (i64.const 0x80) (i64.shl (i64.and (get_local $tail) (i64.const 0x7)) (i64.const 3))))
(block $pad_end
(block $13
(block $12
(block $11
(block $10
(block $9
(block $8
(block $7
(block $6
(block $5
(block $4
(block $3
(block $2
(block $1
(block $0
(block $15
(block $14
(block $switch
(i32.wrap/i64 (get_local $tail))
(i32.const 3)
(i32.shr_u)
(br_table $0 $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15)))
(get_local $last_word)
(get_local $w14)
(i64.or)
(set_local $w14)
(set_local $last_word (i64.const 0)))
(get_local $last_word)
(get_local $w15)
(i64.or)
(set_local $w15)
(set_local $last_word (i64.const 0))
;; compress
(get_local $ctx)
(get_local $w0)
(get_local $w1)
(get_local $w2)
(get_local $w3)
(get_local $w4)
(get_local $w5)
(get_local $w6)
(get_local $w7)
(get_local $w8)
(get_local $w9)
(get_local $w10)
(get_local $w11)
(get_local $w12)
(get_local $w13)
(get_local $w14)
(get_local $w15)
(call $compress)
(i64.store offset=64 (get_local $ctx) (get_local $bytes_read))
;; zero out words
(set_local $w0 (i64.const 0))
(set_local $w1 (i64.const 0))
(set_local $w2 (i64.const 0))
(set_local $w3 (i64.const 0))
(set_local $w4 (i64.const 0))
(set_local $w5 (i64.const 0))
(set_local $w6 (i64.const 0))
(set_local $w7 (i64.const 0))
(set_local $w8 (i64.const 0))
(set_local $w9 (i64.const 0))
(set_local $w10 (i64.const 0))
(set_local $w11 (i64.const 0))
(set_local $w12 (i64.const 0))
(set_local $w13 (i64.const 0))
(set_local $w14 (i64.const 0))
(set_local $w15 (i64.const 0)))
(get_local $last_word)
(get_local $w0)
(i64.or)
(set_local $w0)
(set_local $last_word (i64.const 0)))
(get_local $last_word)
(get_local $w1)
(i64.or)
(set_local $w1)
(set_local $last_word (i64.const 0)))
(get_local $last_word)
(get_local $w2)
(i64.or)
(set_local $w2)
(set_local $last_word (i64.const 0)))
(get_local $last_word)
(get_local $w3)
(i64.or)
(set_local $w3)
(set_local $last_word (i64.const 0)))
(get_local $last_word)
(get_local $w4)
(i64.or)
(set_local $w4)
(set_local $last_word (i64.const 0)))
(get_local $last_word)
(get_local $w5)
(i64.or)
(set_local $w5)
(set_local $last_word (i64.const 0)))
(get_local $last_word)
(get_local $w6)
(i64.or)
(set_local $w6)
(set_local $last_word (i64.const 0)))
(get_local $last_word)
(get_local $w7)
(i64.or)
(set_local $w7)
(set_local $last_word (i64.const 0)))
(get_local $last_word)
(get_local $w8)
(i64.or)
(set_local $w8)
(set_local $last_word (i64.const 0)))
(get_local $last_word)
(get_local $w9)
(i64.or)
(set_local $w9)
(set_local $last_word (i64.const 0)))
(get_local $last_word)
(get_local $w10)
(i64.or)
(set_local $w10)
(set_local $last_word (i64.const 0)))
(get_local $last_word)
(get_local $w11)
(i64.or)
(set_local $w11)
(set_local $last_word (i64.const 0)))
(get_local $last_word)
(get_local $w12)
(i64.or)
(set_local $w12)
(set_local $last_word (i64.const 0)))
(get_local $last_word)
(get_local $w13)
(i64.or)
(set_local $w13)
(set_local $last_word (i64.const 0)))
;; load upper limb of 128bit length
(get_local $bytes_read)
(i64.const 61)
(i64.shr_u)
(get_local $bytes_read_overflow)
(i64.const 3)
(i64.shr_u)
(i64.add)
(call $i64.bswap)
(set_local $w14)
;; load lower limb of 128bit length
(set_local $w15 (call $i64.bswap (i64.mul (get_local $bytes_read) (i64.const 8))))
(get_local $ctx)
(get_local $w0)
(get_local $w1)
(get_local $w2)
(get_local $w3)
(get_local $w4)
(get_local $w5)
(get_local $w6)
(get_local $w7)
(get_local $w8)
(get_local $w9)
(get_local $w10)
(get_local $w11)
(get_local $w12)
(get_local $w13)
(get_local $w14)
(get_local $w15)
(call $compress)
(get_local $ctx)
(i64.load offset=0 (get_local $ctx))
(call $i64.bswap)
(i64.store offset=0)
(get_local $ctx)
(i64.load offset=8 (get_local $ctx))
(call $i64.bswap)
(i64.store offset=8)
(get_local $ctx)
(i64.load offset=16 (get_local $ctx))
(call $i64.bswap)
(i64.store offset=16)
(get_local $ctx)
(i64.load offset=24 (get_local $ctx))
(call $i64.bswap)
(i64.store offset=24)
(get_local $ctx)
(i64.load offset=32 (get_local $ctx))
(call $i64.bswap)
(i64.store offset=32)
(get_local $ctx)
(i64.load offset=40 (get_local $ctx))
(call $i64.bswap)
(i64.store offset=40)
(get_local $ctx)
(i64.load offset=48 (get_local $ctx))
(call $i64.bswap)
(i64.store offset=48)
(get_local $ctx)
(i64.load offset=56 (get_local $ctx))
(call $i64.bswap)
(i64.store offset=56)))))