-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargon2.wat
6591 lines (4902 loc) · 358 KB
/
argon2.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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(module
;; 0-64 reserved for param block
(func $i32.log (import "debug" "log") (param i32))
(func $i32.log_tee (import "debug" "log_tee") (param i32) (result i32))
;; No i64 interop with JS yet - but maybe coming with WebAssembly BigInt
;; So we can instead fake this by splitting the i64 into two i32 limbs,
;; however these are WASM functions using i32x2.log:
(func $i32x2.log (import "debug" "log") (param i32) (param i32))
(func $f32.log (import "debug" "log") (param f32))
(func $f32.log_tee (import "debug" "log_tee") (param f32) (result f32))
(func $f64.log (import "debug" "log") (param f64))
(func $f64.log_tee (import "debug" "log_tee") (param f64) (result f64))
(global $register0 (mut i64) (i64.const 0))
(global $register1 (mut i64) (i64.const 0))
(global $register2 (mut i64) (i64.const 0))
(global $register3 (mut i64) (i64.const 0))
(func $i64.log
(param $0 i64)
(call $i32x2.log
;; Upper limb
(i32.wrap/i64
(i64.shr_s (get_local $0)
(i64.const 32)))
;; Lower limb
(i32.wrap/i64 (get_local $0))))
(func $i64.log_tee
(param $0 i64)
(result i64)
(call $i64.log (get_local $0))
(return (get_local $0)))
(memory (export "memory") 32767)
(func (export "argon2_init") (param $ptr i32) (param $memory_blocks i32) (param $tag_length i32) (param $iterations i32) (param $type i32)
;; b array: 0-128
(i32.store offset=0 (get_local $ptr) (i32.const 1))
(i32.store offset=4 (get_local $ptr) (get_local $tag_length))
(i32.store offset=8 (get_local $ptr) (get_local $memory_blocks))
(i32.store offset=12 (get_local $ptr) (get_local $iterations))
(i32.store offset=16 (get_local $ptr) (i32.const 0x13))
(i32.store offset=20 (get_local $ptr) (get_local $type))
(i32.store offset=24 (get_local $ptr) (i32.add (get_local $ptr) (i32.const 8192)))
(i32.store offset=28 (get_local $ptr) (i32.const 0))
;; pseudo_rands 32..1056
(call $init_block (i32.add (get_local $ptr) (i32.const 32)))
;; input_block 1056..2080
(call $init_block (i32.add (get_local $ptr) (i32.const 1056)))
;; 1056..1064 pass
;; 1064..1072 lane
;; 1072..1080 slice
;; 1080..1088 memory blocks
;; 1088..1096 passes
;; 1096..1104 type: starts as 1 -> argon2i
;; 1104..1112 initialise counter
(i64.store offset=8 (i32.const 1056) (i64.const 0))
(i32.store offset=24 (i32.const 1056) (get_local $memory_blocks))
(i32.store offset=32 (i32.const 1056) (get_local $iterations))
(i32.store offset=40 (i32.const 1056) (get_local $type))
(i64.store offset=48 (i32.const 1056) (i64.const 0))
;; tmp block 2080..3104
(call $init_block (i32.add (get_local $ptr) (i32.const 2080)))
;; zero block 3104..4128
(call $init_block (i32.add (get_local $ptr) (i32.const 3104)))
;; H0 string 4128..4192
(i64.store offset=4128 (get_local $ptr) (i64.const 0))
(i64.store offset=4136 (get_local $ptr) (i64.const 0))
(i64.store offset=4144 (get_local $ptr) (i64.const 0))
(i64.store offset=4152 (get_local $ptr) (i64.const 0))
(i64.store offset=4160 (get_local $ptr) (i64.const 0))
(i64.store offset=4168 (get_local $ptr) (i64.const 0))
(i64.store offset=4176 (get_local $ptr) (i64.const 0))
(i64.store offset=4184 (get_local $ptr) (i64.const 0))
(i64.store offset=4192 (get_local $ptr) (i64.const 0))
;; blake2b internal states 4200..4736
(i64.store offset=4200 (get_local $ptr) (i64.const 0))
(i64.store offset=4208 (get_local $ptr) (i64.const 0))
(i64.store offset=4216 (get_local $ptr) (i64.const 0))
(i64.store offset=4224 (get_local $ptr) (i64.const 0))
(i64.store offset=4232 (get_local $ptr) (i64.const 0))
(i64.store offset=4240 (get_local $ptr) (i64.const 0))
(i64.store offset=4248 (get_local $ptr) (i64.const 0))
(i64.store offset=4256 (get_local $ptr) (i64.const 0))
(i64.store offset=4264 (get_local $ptr) (i64.const 0))
(i64.store offset=4272 (get_local $ptr) (i64.const 0))
(i64.store offset=4280 (get_local $ptr) (i64.const 0))
(i64.store offset=4288 (get_local $ptr) (i64.const 0))
(i64.store offset=4296 (get_local $ptr) (i64.const 0))
(i64.store offset=4304 (get_local $ptr) (i64.const 0))
(i64.store offset=4312 (get_local $ptr) (i64.const 0))
(i64.store offset=4320 (get_local $ptr) (i64.const 0))
(i64.store offset=4328 (get_local $ptr) (i64.const 0))
(i64.store offset=4336 (get_local $ptr) (i64.const 0))
(i64.store offset=4344 (get_local $ptr) (i64.const 0))
(i64.store offset=4352 (get_local $ptr) (i64.const 0))
(i64.store offset=4360 (get_local $ptr) (i64.const 0))
(i64.store offset=4368 (get_local $ptr) (i64.const 0))
(i64.store offset=4376 (get_local $ptr) (i64.const 0))
(i64.store offset=4384 (get_local $ptr) (i64.const 0))
(i64.store offset=4392 (get_local $ptr) (i64.const 0))
(i64.store offset=4400 (get_local $ptr) (i64.const 0))
(i64.store offset=4408 (get_local $ptr) (i64.const 0))
(i64.store offset=4416 (get_local $ptr) (i64.const 0))
(i64.store offset=4424 (get_local $ptr) (i64.const 0))
(i64.store offset=4432 (get_local $ptr) (i64.const 0))
(i64.store offset=4440 (get_local $ptr) (i64.const 0))
(i64.store offset=4448 (get_local $ptr) (i64.const 0))
(i64.store offset=4456 (get_local $ptr) (i64.const 0))
(i64.store offset=4464 (get_local $ptr) (i64.const 0))
(i64.store offset=4472 (get_local $ptr) (i64.const 0))
(i64.store offset=4480 (get_local $ptr) (i64.const 0))
(i64.store offset=4488 (get_local $ptr) (i64.const 0))
(i64.store offset=4496 (get_local $ptr) (i64.const 0))
(i64.store offset=4504 (get_local $ptr) (i64.const 0))
(i64.store offset=4512 (get_local $ptr) (i64.const 0))
(i64.store offset=4520 (get_local $ptr) (i64.const 0))
(i64.store offset=4528 (get_local $ptr) (i64.const 0))
(i64.store offset=4536 (get_local $ptr) (i64.const 0))
(i64.store offset=4544 (get_local $ptr) (i64.const 0))
(i64.store offset=4552 (get_local $ptr) (i64.const 0))
(i64.store offset=4560 (get_local $ptr) (i64.const 0))
(i64.store offset=4568 (get_local $ptr) (i64.const 0))
(i64.store offset=4576 (get_local $ptr) (i64.const 0))
(i64.store offset=4584 (get_local $ptr) (i64.const 0))
(i64.store offset=4592 (get_local $ptr) (i64.const 0))
(i64.store offset=4600 (get_local $ptr) (i64.const 0))
(i64.store offset=4608 (get_local $ptr) (i64.const 0))
(i64.store offset=4616 (get_local $ptr) (i64.const 0))
(i64.store offset=4624 (get_local $ptr) (i64.const 0))
(i64.store offset=4632 (get_local $ptr) (i64.const 0))
(i64.store offset=4640 (get_local $ptr) (i64.const 0))
(i64.store offset=4648 (get_local $ptr) (i64.const 0))
(i64.store offset=4656 (get_local $ptr) (i64.const 0))
(i64.store offset=4664 (get_local $ptr) (i64.const 0))
(i64.store offset=4672 (get_local $ptr) (i64.const 0))
(i64.store offset=4680 (get_local $ptr) (i64.const 0))
(i64.store offset=4688 (get_local $ptr) (i64.const 0))
(i64.store offset=4696 (get_local $ptr) (i64.const 0))
(i64.store offset=4704 (get_local $ptr) (i64.const 0))
(i64.store offset=4712 (get_local $ptr) (i64.const 0))
(i64.store offset=4720 (get_local $ptr) (i64.const 0))
(i64.store offset=4728 (get_local $ptr) (i64.const 0)))
(func $argon2_hash (export "argon2_hash") (param $ctx i32) (param $input i32) (param $input_len i32)
(local $hash_ctx i32)
(local $pass i32)
(local $passes i32)
(local $memory i32)
(local $memory_end i32)
(local $last_block i32)
(local $head i32)
(local $type i32)
(i32.load offset=20 (get_local $ctx))
(set_local $type)
(get_local $input)
(set_local $head)
(i32.load offset=12 (get_local $ctx))
(set_local $passes)
(get_local $ctx)
(i32.const 4192)
(i32.add)
(set_local $hash_ctx)
(get_local $ctx)
(i32.const 8192)
(i32.add)
(set_local $memory)
(i32.load offset=8 (get_local $ctx))
(i32.const 0xfffffffc)
(i32.and)
(i32.const 1024)
(i32.mul)
(get_local $memory)
(i32.add)
(set_local $memory_end)
(i32.store8 offset=0 (get_local $hash_ctx) (i32.const 64))
(i32.store8 offset=1 (get_local $hash_ctx) (i32.const 0))
(i32.store8 offset=2 (get_local $hash_ctx) (i32.const 1))
(i32.store8 offset=3 (get_local $hash_ctx) (i32.const 1))
(call $blake2b_init (get_local $hash_ctx) (i32.const 64))
(call $blake2b_update (get_local $hash_ctx) (get_local $ctx) (i32.add (i32.const 24) (get_local $ctx)))
(call $blake2b_update (get_local $hash_ctx) (get_local $input) (get_local $input_len))
(call $blake2b_final (get_local $hash_ctx))
;; TODO: provide blake2b_final with $out param
;; clear inputs
(block $done
(loop $sanitize
(get_local $input)
(get_local $input_len)
(i32.ge_u)
(br_if $done)
(i64.store offset=0 (get_local $input) (i64.const 0))
(i64.store offset=8 (get_local $input) (i64.const 0))
(i64.store offset=16 (get_local $input) (i64.const 0))
(i64.store offset=24 (get_local $input) (i64.const 0))
(i64.store offset=32 (get_local $input) (i64.const 0))
(i64.store offset=40 (get_local $input) (i64.const 0))
(i64.store offset=48 (get_local $input) (i64.const 0))
(i64.store offset=56 (get_local $input) (i64.const 0))
(i64.store offset=64 (get_local $input) (i64.const 0))
(i64.store offset=72 (get_local $input) (i64.const 0))
(i64.store offset=80 (get_local $input) (i64.const 0))
(i64.store offset=88 (get_local $input) (i64.const 0))
(i64.store offset=96 (get_local $input) (i64.const 0))
(i64.store offset=104 (get_local $input) (i64.const 0))
(i64.store offset=112 (get_local $input) (i64.const 0))
(i64.store offset=120 (get_local $input) (i64.const 0))
(get_local $input)
(i32.const 128)
(i32.add)
(set_local $input)
(br $sanitize)))
(i64.store offset=4128 (get_local $ctx) (i64.load offset=128 (get_local $hash_ctx)))
(i64.store offset=4136 (get_local $ctx) (i64.load offset=136 (get_local $hash_ctx)))
(i64.store offset=4144 (get_local $ctx) (i64.load offset=144 (get_local $hash_ctx)))
(i64.store offset=4152 (get_local $ctx) (i64.load offset=152 (get_local $hash_ctx)))
(i64.store offset=4160 (get_local $ctx) (i64.load offset=160 (get_local $hash_ctx)))
(i64.store offset=4168 (get_local $ctx) (i64.load offset=168 (get_local $hash_ctx)))
(i64.store offset=4176 (get_local $ctx) (i64.load offset=176 (get_local $hash_ctx)))
(i64.store offset=4184 (get_local $ctx) (i64.load offset=184 (get_local $hash_ctx)))
(call $fill_initial_blocks (get_local $ctx) (i32.add (get_local $ctx) (i32.const 4128)) (get_local $memory))
(block $done
(loop $start
(get_local $pass)
(get_local $passes)
(i32.eq)
(br_if $done)
(get_local $ctx)
(get_local $pass)
(get_local $type)
(call $fill_memory_blocks)
(get_local $pass)
(i32.const 1)
(i32.add)
(set_local $pass)
(br $start)))
(get_local $memory)
(i32.load offset=8 (get_local $ctx))
(i32.const 0xfffffffc)
(i32.and)
(i32.const 1024)
(i32.mul)
(i32.add)
(set_local $memory_end)
;; finalize
(get_local $hash_ctx)
(get_local $memory_end)
(i32.const 1024)
(i32.sub)
(get_local $memory_end)
(i32.load offset=4 (get_local $ctx))
(get_local $head)
(call $blake2b_long))
(func $generate_addresses (param $ctx i32) (param $pass i32) (param $slice i32)
(local $memory_blocks i32)
(local $passes i32)
(local $type i32)
(local $counter i64)
(local $zero_block i32)
(local $tmp_block i32)
(local $input_block i32)
(local $pseudo_rand_block i32)
(set_local $pseudo_rand_block (i32.add (get_local $ctx (i32.const 32))))
(set_local $input_block (i32.add (get_local $ctx (i32.const 1056))))
(set_local $tmp_block (i32.add (get_local $ctx (i32.const 2080))))
(set_local $zero_block (i32.add (get_local $ctx (i32.const 3104))))
(set_local $counter (i64.load offset=48 (get_local $input_block)))
;; (call $i64.log (get_local $counter))
;; (call $init_block (get_local $input_block))
(call $init_block (get_local $zero_block))
(call $init_block (get_local $tmp_block))
(call $init_block (get_local $pseudo_rand_block))
;; reset counter
(i32.load offset=16 (get_local $input_block))
(get_local $slice)
(i32.ne)
(if (then
(i64.const 0)
(set_local $counter)))
(i32.load offset=8 (get_local $ctx))
(i32.const 0xfffffffc)
(i32.and)
(set_local $memory_blocks)
(i32.load offset=12 (get_local $ctx))
(set_local $passes)
(i32.load offset=20 (get_local $ctx))
(set_local $type)
(i32.store offset=0 (get_local $input_block) (get_local $pass))
(i32.store offset=16 (get_local $input_block) (get_local $slice))
(i32.store offset=24 (get_local $input_block) (get_local $memory_blocks))
(i32.store offset=32 (get_local $input_block) (get_local $passes))
(i32.store offset=40 (get_local $input_block) (get_local $type))
(i64.store offset=48 (get_local $input_block) (get_local $counter))
;; increment counter
(get_local $input_block)
(get_local $counter)
(i64.const 1)
(i64.add)
(i64.store offset=48)
(call $fill_block (get_local $zero_block) (get_local $input_block) (get_local $tmp_block) (i32.const 1))
(call $fill_block (get_local $zero_block) (get_local $tmp_block) (get_local $pseudo_rand_block) (i32.const 1)))
(func $fill_memory_blocks (param $ctx i32) (param $pass i32) (param $type i32)
(local $segment i32)
(local $argon_type i32)
(i32.const 1)
(set_local $argon_type)
(block $proceed
(get_local $type)
(i32.const 1)
(i32.eq)
(br_if $proceed)
(get_local $pass)
(i32.const 1)
(i32.ge_u)
(br_if $proceed)
(i32.const 1)
(set_local $type)
(loop $next_segment
(get_local $segment)
(i32.const 2)
(i32.eq)
(if (then
(i32.const 2)
(set_local $type)
(br $proceed)))
(get_local $ctx)
(get_local $segment)
(get_local $pass)
(get_local $type)
(call $fill_segment)
(get_local $segment)
(i32.const 1)
(i32.add)
(set_local $segment)
(br $next_segment))
)
(block $end
(loop $next_segment
(get_local $segment)
(i32.const 4)
(i32.eq)
(br_if $end)
(get_local $ctx)
(get_local $segment)
(get_local $pass)
(get_local $type)
(call $fill_segment)
(get_local $segment)
(i32.const 1)
(i32.add)
(set_local $segment)
(br $next_segment))))
(func $fill_initial_blocks (param $ctx i32) (param $h0 i32) (param $memory i32)
(i64.store offset=64 (get_local $h0) (i64.const 0))
(i32.add (get_local $ctx) (i32.const 4200))
(get_local $h0)
(i32.add (get_local $h0) (i32.const 72))
(i32.const 1024)
(get_local $memory)
(call $blake2b_long)
(i32.store (i32.add (get_local $h0) (i32.const 64)) (i32.const 1))
(i32.add (get_local $ctx) (i32.const 4200))
(get_local $h0)
(i32.add (get_local $h0) (i32.const 72))
(i32.const 1024)
(i32.add (i32.const 1024) (get_local $memory))
(call $blake2b_long))
(func $relative_position (param $pseudo_rand i64) (param $area i64)
(result i32)
(local $j1 i64)
(local $j2 i64)
(local $next_index i64)
(get_local $pseudo_rand)
(i64.const 0xffffffff)
(i64.and)
(set_local $j1)
(set_local $next_index (i64.shr_u (i64.mul (get_local $j1) (get_local $j1)) (i64.const 32)))
(set_local $next_index (i64.shr_u (i64.mul (get_local $area) (get_local $next_index)) (i64.const 32)))
(i64.sub (i64.sub (get_local $area) (i64.const 1)) (get_local $next_index))
(i32.wrap/i64))
(func $reference_block_pos (param $lane_length i32) (param $pos i32) (param $pass i32) (param $pseudo_rand i64)
(result i32)
(local $start_pos i32)
(local $area i32)
(local $segment_length i32)
(get_local $lane_length)
(i32.const 2)
(i32.shr_u)
(set_local $segment_length)
(set_local $start_pos (i32.const 0))
(block $fi
;; first pass
(get_local $pass)
(i32.const 0)
(i32.eq)
(if (then
(get_local $pos)
(i32.const 1)
(i32.sub)
(set_local $area)
(br $fi)))
;; subsequent passes
;; area = 3 segments + prev blocks in this segment
(get_local $lane_length)
(get_local $segment_length)
(i32.sub)
(get_local $pos)
(get_local $segment_length)
(i32.rem_u)
(i32.add)
(i32.const 1)
(i32.sub)
(set_local $area)
;; start pos is beginning of next segment
(get_local $pos)
(get_local $segment_length)
(i32.div_u)
(i32.const 1)
(i32.add)
(i32.const 4)
(i32.rem_u)
(get_local $segment_length)
(i32.mul)
(set_local $start_pos))
(get_local $start_pos)
(call $relative_position (get_local $pseudo_rand) (i64.extend_u/i32 (get_local $area)))
(i32.add)
(get_local $lane_length)
(i32.rem_u))
(func $fill_segment (param $ctx i32) (param $slice_index i32) (param $pass i32) (param $type i32)
(local $pseudo_rand i64)
(local $starting_index i32)
(local $lane_length i32)
(local $segment_length i32)
(local $curr_offset i32)
(local $prev_offset i32)
(local $ref_offset i32)
(local $memory_offset i32)
(local $i i32)
(i32.load offset=8 (get_local $ctx))
(i32.const 0xfffffffc)
(i32.and)
(tee_local $lane_length)
(i32.const 2)
(i32.shr_u)
(set_local $segment_length)
(i32.const 0)
(set_local $starting_index)
(i32.load offset=24 (get_local $ctx))
(set_local $memory_offset)
(get_local $pass)
(get_local $slice_index)
(i32.add)
(i32.const 0)
(i32.eq)
(if (then
(i32.const 2)
(set_local $starting_index)))
(call $generate_addresses (get_local $ctx) (get_local $pass) (get_local $slice_index))
(get_local $slice_index)
(get_local $segment_length)
(i32.mul)
(get_local $starting_index)
(i32.add)
(get_local $lane_length) ;; one lane, offset is modulo lane_length
(i32.rem_u)
(set_local $curr_offset)
(get_local $curr_offset)
(i32.const 1)
(i32.sub)
(set_local $prev_offset)
(get_local $curr_offset)
(i32.const 0)
(i32.eq)
(if (then
(get_local $prev_offset)
(get_local $lane_length)
(i32.add)
(set_local $prev_offset)))
(get_local $starting_index)
(set_local $i)
(block $end
(loop $start
(get_local $i)
(get_local $segment_length)
(i32.eq)
(br_if $end)
(get_local $curr_offset)
(i32.const 1)
(i32.eq)
(if (then
(get_local $curr_offset)
(i32.const 1)
(i32.sub)
(set_local $prev_offset)))
(block $data_independent
(get_local $type)
(i32.const 1)
(i32.eq)
(if (then
(get_local $i)
(i32.const 0x7f)
(i32.and)
(i32.const 0)
(i32.eq)
(get_local $i)
(i32.const 0)
(i32.ne)
(i32.and)
(if (then
(call $generate_addresses (get_local $ctx) (get_local $pass) (get_local $slice_index))))
(get_local $ctx)
(get_local $curr_offset)
(get_local $segment_length)
(i32.rem_u)
(i32.const 0x7f)
(i32.and)
(i32.add)
(i32.const 8)
(i32.mul)
(i64.load offset=32)
(set_local $pseudo_rand)
(br $data_independent)))
(i32.add (get_local $memory_offset) (i32.shl (get_local $prev_offset) (i32.const 10)))
(i64.load)
(set_local $pseudo_rand))
(call $reference_block_pos (get_local $lane_length) (get_local $curr_offset) (get_local $pass) (get_local $pseudo_rand))
(set_local $ref_offset)
(i32.add (get_local $memory_offset) (i32.shl (get_local $prev_offset) (i32.const 10)))
(i32.add (get_local $memory_offset) (i32.shl (get_local $ref_offset) (i32.const 10)))
(i32.add (get_local $memory_offset) (i32.shl (get_local $curr_offset) (i32.const 10)))
(get_local $pass)
(call $fill_block)
(get_local $i)
(i32.const 1)
(i32.add)
(set_local $i)
(get_local $curr_offset)
(i32.const 1)
(i32.add)
(get_local $lane_length)
(i32.rem_u)
(set_local $curr_offset)
(get_local $prev_offset)
(i32.const 1)
(i32.add)
(set_local $prev_offset)
(br $start))))
(func $blake2b_init (param $ptr i32) (param $outlen i32)
;; b array: 0-128
;; (i64.store offset=0 (get_local $ptr) (i64.const 0))
(i64.store offset=8 (get_local $ptr) (i64.const 0))
(i64.store offset=16 (get_local $ptr) (i64.const 0))
(i64.store offset=24 (get_local $ptr) (i64.const 0))
(i64.store offset=32 (get_local $ptr) (i64.const 0))
(i64.store offset=40 (get_local $ptr) (i64.const 0))
(i64.store offset=48 (get_local $ptr) (i64.const 0))
(i64.store offset=56 (get_local $ptr) (i64.const 0))
(i64.store offset=64 (get_local $ptr) (i64.const 0))
(i64.store offset=72 (get_local $ptr) (i64.const 0))
(i64.store offset=80 (get_local $ptr) (i64.const 0))
(i64.store offset=88 (get_local $ptr) (i64.const 0))
(i64.store offset=96 (get_local $ptr) (i64.const 0))
(i64.store offset=104 (get_local $ptr) (i64.const 0))
(i64.store offset=112 (get_local $ptr) (i64.const 0))
(i64.store offset=120 (get_local $ptr) (i64.const 0))
;; h array: 128-192, (8 * i64)
;; TODO: support xor against param block and stuff, for now just xor against length
(i64.store offset=128 (get_local $ptr) (i64.xor (i64.const 0x6a09e667f3bcc908) (i64.load offset=0 (get_local $ptr))))
(i64.store offset=136 (get_local $ptr) (i64.xor (i64.const 0xbb67ae8584caa73b) (i64.load offset=8 (get_local $ptr))))
(i64.store offset=144 (get_local $ptr) (i64.xor (i64.const 0x3c6ef372fe94f82b) (i64.load offset=16 (get_local $ptr))))
(i64.store offset=152 (get_local $ptr) (i64.xor (i64.const 0xa54ff53a5f1d36f1) (i64.load offset=24 (get_local $ptr))))
(i64.store offset=160 (get_local $ptr) (i64.xor (i64.const 0x510e527fade682d1) (i64.load offset=32 (get_local $ptr))))
(i64.store offset=168 (get_local $ptr) (i64.xor (i64.const 0x9b05688c2b3e6c1f) (i64.load offset=40 (get_local $ptr))))
(i64.store offset=176 (get_local $ptr) (i64.xor (i64.const 0x1f83d9abfb41bd6b) (i64.load offset=48 (get_local $ptr))))
(i64.store offset=184 (get_local $ptr) (i64.xor (i64.const 0x5be0cd19137e2179) (i64.load offset=56 (get_local $ptr))))
;; t int.64: 192-200
(i64.store offset=192 (get_local $ptr) (i64.const 0))
;; c int.64: 200-208
(i64.store offset=200 (get_local $ptr) (i64.const 0))
;; f int.64: 208-216
(i64.store offset=208 (get_local $ptr) (i64.const 0)))
(func $blake2b_update (export "blake2b_update") (param $ctx i32) (param $input i32) (param $input_end i32)
(local $t i32)
(local $c i32)
(local $i i32)
;; load ctx.t, ctx.c
(set_local $t (i32.add (get_local $ctx) (i32.const 192)))
(set_local $c (i32.add (get_local $ctx) (i32.const 200)))
;; i = ctx.c
(set_local $i (i32.wrap/i64 (i64.load (get_local $c))))
(block $end
(loop $start
(br_if $end (i32.eq (get_local $input) (get_local $input_end)))
(if (i32.eq (get_local $i) (i32.const 128))
(then
(i64.store (get_local $t) (i64.add (i64.load (get_local $t)) (i64.extend_u/i32 (get_local $i))))
(set_local $i (i32.const 0))
(call $blake2b_compress (get_local $ctx))
)
)
(i32.store8 (i32.add (get_local $ctx) (get_local $i)) (i32.load8_u (get_local $input)))
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(set_local $input (i32.add (get_local $input) (i32.const 1)))
(br $start)
)
)
(i64.store (get_local $c) (i64.extend_u/i32 (get_local $i)))
)
(func $blake2b_final (export "blake2b_final") (param $ctx i32)
(local $t i32)
(local $c i32)
(local $i i32)
;; load ctx.t, ctx.c
(set_local $t (i32.add (get_local $ctx) (i32.const 192)))
(set_local $c (i32.add (get_local $ctx) (i32.const 200)))
;; ctx.t += ctx.c
(i64.store (get_local $t) (i64.add (i64.load (get_local $t)) (i64.load (get_local $c))))
;; set ctx.f to last_block
(i64.store offset=208 (get_local $ctx) (i64.const 0xffffffffffffffff))
;; i = ctx.c
(set_local $i (i32.wrap/i64 (i64.load (get_local $c))))
;; zero out remaining, i..128
(block $end
(loop $start
(br_if $end (i32.eq (get_local $i) (i32.const 128)))
(i32.store8 (i32.add (get_local $ctx) (get_local $i)) (i32.const 0))
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(br $start)
)
)
;; ctx.c = i (for good meassure)
(i64.store (get_local $c) (i64.extend_u/i32 (get_local $i)))
(call $blake2b_compress (get_local $ctx))
)
(func $blake2b_long (export "blake2b_long") (param $ctx i32) (param $input i32) (param $input_end i32) (param $len i32) (param $out i32)
(local $r i32)
(local $ctx2 i32)
(local $tmp i32)
(i32.store offset=0 (i32.const 28) (get_local $len))
(set_local $r (i32.sub (i32.div_u (i32.add (get_local $len) (i32.const 31)) (i32.const 32)) (i32.const 2)))
(set_local $r (i32.mul (get_local $r) (i32.xor (i32.shr_u (get_local $r) (i32.const 31)) (i32.const 1))))
(set_local $ctx2 (i32.add (get_local $ctx) (i32.const 216)))
(set_local $tmp (i32.const 64))
(i32.lt_u (get_local $len) (i32.const 64))
(if (then
(set_local $tmp (get_local $len))))
(i64.store (get_local $ctx) (i64.const 0))
(i32.store8 offset=0 (get_local $ctx) (get_local $tmp))
(i32.store8 offset=1 (get_local $ctx) (i32.const 0))
(i32.store8 offset=2 (get_local $ctx) (i32.const 1))
(i32.store8 offset=3 (get_local $ctx) (i32.const 1))
(call $blake2b_init (get_local $ctx) (get_local $tmp))
(call $blake2b_update (get_local $ctx) (i32.const 28) (i32.const 32))
(call $blake2b_update (get_local $ctx) (get_local $input) (get_local $input_end))
(call $blake2b_final (get_local $ctx))
;; this needs to come after
(i64.store offset=0 (get_local $out) (i64.load offset=128 (get_local $ctx)))
(i64.store offset=8 (get_local $out) (i64.load offset=136 (get_local $ctx)))
(i64.store offset=16 (get_local $out) (i64.load offset=144 (get_local $ctx)))
(i64.store offset=24 (get_local $out) (i64.load offset=152 (get_local $ctx)))
(set_local $len (i32.sub (get_local $len) (i32.const 32)))
(set_local $out (i32.add (get_local $out) (i32.const 32)))
(block $end
(loop $start
(i32.eq (get_local $r) (i32.const 0))
(br_if $end)
(set_local $tmp (i32.const 64))
(i32.lt_u (get_local $len) (i32.const 64))
(if (then
(set_local $tmp (get_local $len))))
(i64.store (get_local $ctx2) (i64.const 0))
(i32.store8 offset=0 (get_local $ctx2) (get_local $tmp))
(i32.store8 offset=1 (get_local $ctx2) (i32.const 0))
(i32.store8 offset=2 (get_local $ctx2) (i32.const 1))
(i32.store8 offset=3 (get_local $ctx2) (i32.const 1))
(call $blake2b_init (get_local $ctx2) (get_local $tmp))
(call $blake2b_update (get_local $ctx2) (i32.add (get_local $ctx) (i32.const 128)) (i32.add (get_local $ctx) (i32.const 192)))
(call $blake2b_final (get_local $ctx2))
(i64.store offset=0 (get_local $out) (i64.load offset=128 (get_local $ctx2)))
(i64.store offset=8 (get_local $out) (i64.load offset=136 (get_local $ctx2)))
(i64.store offset=16 (get_local $out) (i64.load offset=144 (get_local $ctx2)))
(i64.store offset=24 (get_local $out) (i64.load offset=152 (get_local $ctx2)))
(set_local $out (i32.add (get_local $out) (i32.const 32)))
(set_local $r (i32.sub (get_local $r) (i32.const 1)))
(set_local $len (i32.sub (get_local $len) (i32.const 32)))
(set_local $tmp (get_local $ctx2))
(set_local $ctx2 (get_local $ctx))
(set_local $ctx (get_local $tmp))
(br $start)))
(i64.store offset=0 (get_local $out) (i64.load offset=160 (get_local $ctx)))
(i64.store offset=8 (get_local $out) (i64.load offset=168 (get_local $ctx)))
(i64.store offset=16 (get_local $out) (i64.load offset=176 (get_local $ctx)))
(i64.store offset=24 (get_local $out) (i64.load offset=184 (get_local $ctx))))
(func $init_block (param $ptr i32)
(i64.store offset=0 (get_local $ptr) (i64.const 0))
(i64.store offset=8 (get_local $ptr) (i64.const 0))
(i64.store offset=16 (get_local $ptr) (i64.const 0))
(i64.store offset=24 (get_local $ptr) (i64.const 0))
(i64.store offset=32 (get_local $ptr) (i64.const 0))
(i64.store offset=40 (get_local $ptr) (i64.const 0))
(i64.store offset=48 (get_local $ptr) (i64.const 0))
(i64.store offset=56 (get_local $ptr) (i64.const 0))
(i64.store offset=64 (get_local $ptr) (i64.const 0))
(i64.store offset=72 (get_local $ptr) (i64.const 0))
(i64.store offset=80 (get_local $ptr) (i64.const 0))
(i64.store offset=88 (get_local $ptr) (i64.const 0))
(i64.store offset=96 (get_local $ptr) (i64.const 0))
(i64.store offset=104 (get_local $ptr) (i64.const 0))
(i64.store offset=112 (get_local $ptr) (i64.const 0))
(i64.store offset=120 (get_local $ptr) (i64.const 0))
(i64.store offset=128 (get_local $ptr) (i64.const 0))
(i64.store offset=136 (get_local $ptr) (i64.const 0))
(i64.store offset=144 (get_local $ptr) (i64.const 0))
(i64.store offset=152 (get_local $ptr) (i64.const 0))
(i64.store offset=160 (get_local $ptr) (i64.const 0))
(i64.store offset=168 (get_local $ptr) (i64.const 0))
(i64.store offset=176 (get_local $ptr) (i64.const 0))
(i64.store offset=184 (get_local $ptr) (i64.const 0))
(i64.store offset=192 (get_local $ptr) (i64.const 0))
(i64.store offset=200 (get_local $ptr) (i64.const 0))
(i64.store offset=208 (get_local $ptr) (i64.const 0))
(i64.store offset=216 (get_local $ptr) (i64.const 0))
(i64.store offset=224 (get_local $ptr) (i64.const 0))
(i64.store offset=232 (get_local $ptr) (i64.const 0))
(i64.store offset=240 (get_local $ptr) (i64.const 0))
(i64.store offset=248 (get_local $ptr) (i64.const 0))
(i64.store offset=256 (get_local $ptr) (i64.const 0))
(i64.store offset=264 (get_local $ptr) (i64.const 0))
(i64.store offset=272 (get_local $ptr) (i64.const 0))
(i64.store offset=280 (get_local $ptr) (i64.const 0))
(i64.store offset=288 (get_local $ptr) (i64.const 0))
(i64.store offset=296 (get_local $ptr) (i64.const 0))
(i64.store offset=304 (get_local $ptr) (i64.const 0))
(i64.store offset=312 (get_local $ptr) (i64.const 0))
(i64.store offset=320 (get_local $ptr) (i64.const 0))
(i64.store offset=328 (get_local $ptr) (i64.const 0))
(i64.store offset=336 (get_local $ptr) (i64.const 0))
(i64.store offset=344 (get_local $ptr) (i64.const 0))
(i64.store offset=352 (get_local $ptr) (i64.const 0))
(i64.store offset=360 (get_local $ptr) (i64.const 0))
(i64.store offset=368 (get_local $ptr) (i64.const 0))
(i64.store offset=376 (get_local $ptr) (i64.const 0))
(i64.store offset=384 (get_local $ptr) (i64.const 0))
(i64.store offset=392 (get_local $ptr) (i64.const 0))
(i64.store offset=400 (get_local $ptr) (i64.const 0))
(i64.store offset=408 (get_local $ptr) (i64.const 0))
(i64.store offset=416 (get_local $ptr) (i64.const 0))
(i64.store offset=424 (get_local $ptr) (i64.const 0))
(i64.store offset=432 (get_local $ptr) (i64.const 0))
(i64.store offset=440 (get_local $ptr) (i64.const 0))
(i64.store offset=448 (get_local $ptr) (i64.const 0))
(i64.store offset=456 (get_local $ptr) (i64.const 0))
(i64.store offset=464 (get_local $ptr) (i64.const 0))
(i64.store offset=472 (get_local $ptr) (i64.const 0))
(i64.store offset=480 (get_local $ptr) (i64.const 0))
(i64.store offset=488 (get_local $ptr) (i64.const 0))
(i64.store offset=496 (get_local $ptr) (i64.const 0))
(i64.store offset=504 (get_local $ptr) (i64.const 0))
(i64.store offset=512 (get_local $ptr) (i64.const 0))
(i64.store offset=520 (get_local $ptr) (i64.const 0))
(i64.store offset=528 (get_local $ptr) (i64.const 0))
(i64.store offset=536 (get_local $ptr) (i64.const 0))
(i64.store offset=544 (get_local $ptr) (i64.const 0))
(i64.store offset=552 (get_local $ptr) (i64.const 0))
(i64.store offset=560 (get_local $ptr) (i64.const 0))
(i64.store offset=568 (get_local $ptr) (i64.const 0))
(i64.store offset=576 (get_local $ptr) (i64.const 0))
(i64.store offset=584 (get_local $ptr) (i64.const 0))
(i64.store offset=592 (get_local $ptr) (i64.const 0))
(i64.store offset=600 (get_local $ptr) (i64.const 0))
(i64.store offset=608 (get_local $ptr) (i64.const 0))
(i64.store offset=616 (get_local $ptr) (i64.const 0))
(i64.store offset=624 (get_local $ptr) (i64.const 0))
(i64.store offset=632 (get_local $ptr) (i64.const 0))
(i64.store offset=640 (get_local $ptr) (i64.const 0))
(i64.store offset=648 (get_local $ptr) (i64.const 0))
(i64.store offset=656 (get_local $ptr) (i64.const 0))
(i64.store offset=664 (get_local $ptr) (i64.const 0))
(i64.store offset=672 (get_local $ptr) (i64.const 0))
(i64.store offset=680 (get_local $ptr) (i64.const 0))
(i64.store offset=688 (get_local $ptr) (i64.const 0))
(i64.store offset=696 (get_local $ptr) (i64.const 0))
(i64.store offset=704 (get_local $ptr) (i64.const 0))
(i64.store offset=712 (get_local $ptr) (i64.const 0))
(i64.store offset=720 (get_local $ptr) (i64.const 0))
(i64.store offset=728 (get_local $ptr) (i64.const 0))
(i64.store offset=736 (get_local $ptr) (i64.const 0))
(i64.store offset=744 (get_local $ptr) (i64.const 0))
(i64.store offset=752 (get_local $ptr) (i64.const 0))
(i64.store offset=760 (get_local $ptr) (i64.const 0))
(i64.store offset=768 (get_local $ptr) (i64.const 0))
(i64.store offset=776 (get_local $ptr) (i64.const 0))
(i64.store offset=784 (get_local $ptr) (i64.const 0))
(i64.store offset=792 (get_local $ptr) (i64.const 0))
(i64.store offset=800 (get_local $ptr) (i64.const 0))
(i64.store offset=808 (get_local $ptr) (i64.const 0))
(i64.store offset=816 (get_local $ptr) (i64.const 0))
(i64.store offset=824 (get_local $ptr) (i64.const 0))
(i64.store offset=832 (get_local $ptr) (i64.const 0))
(i64.store offset=840 (get_local $ptr) (i64.const 0))
(i64.store offset=848 (get_local $ptr) (i64.const 0))
(i64.store offset=856 (get_local $ptr) (i64.const 0))
(i64.store offset=864 (get_local $ptr) (i64.const 0))
(i64.store offset=872 (get_local $ptr) (i64.const 0))
(i64.store offset=880 (get_local $ptr) (i64.const 0))
(i64.store offset=888 (get_local $ptr) (i64.const 0))
(i64.store offset=896 (get_local $ptr) (i64.const 0))
(i64.store offset=904 (get_local $ptr) (i64.const 0))
(i64.store offset=912 (get_local $ptr) (i64.const 0))
(i64.store offset=920 (get_local $ptr) (i64.const 0))
(i64.store offset=928 (get_local $ptr) (i64.const 0))
(i64.store offset=936 (get_local $ptr) (i64.const 0))
(i64.store offset=944 (get_local $ptr) (i64.const 0))
(i64.store offset=952 (get_local $ptr) (i64.const 0))
(i64.store offset=960 (get_local $ptr) (i64.const 0))
(i64.store offset=968 (get_local $ptr) (i64.const 0))
(i64.store offset=976 (get_local $ptr) (i64.const 0))
(i64.store offset=984 (get_local $ptr) (i64.const 0))
(i64.store offset=992 (get_local $ptr) (i64.const 0))
(i64.store offset=1000 (get_local $ptr) (i64.const 0))
(i64.store offset=1008 (get_local $ptr) (i64.const 0))
(i64.store offset=1016 (get_local $ptr) (i64.const 0)))
(func $fill_block (param $prev_block i32) (param $ref_block i32) (param $next_block i32) (param $xor i32)
(local $tmp i64)
(local $b0 i64) (local $b1 i64) (local $b2 i64) (local $b3 i64)
(local $b4 i64) (local $b5 i64) (local $b6 i64) (local $b7 i64)
(local $b8 i64) (local $b9 i64) (local $b10 i64) (local $b11 i64)
(local $b12 i64) (local $b13 i64) (local $b14 i64) (local $b15 i64)
(local $b16 i64) (local $b17 i64) (local $b18 i64) (local $b19 i64)
(local $b20 i64) (local $b21 i64) (local $b22 i64) (local $b23 i64)
(local $b24 i64) (local $b25 i64) (local $b26 i64) (local $b27 i64)
(local $b28 i64) (local $b29 i64) (local $b30 i64) (local $b31 i64)
(local $b32 i64) (local $b33 i64) (local $b34 i64) (local $b35 i64)
(local $b36 i64) (local $b37 i64) (local $b38 i64) (local $b39 i64)
(local $b40 i64) (local $b41 i64) (local $b42 i64) (local $b43 i64)
(local $b44 i64) (local $b45 i64) (local $b46 i64) (local $b47 i64)
(local $b48 i64) (local $b49 i64) (local $b50 i64) (local $b51 i64)
(local $b52 i64) (local $b53 i64) (local $b54 i64) (local $b55 i64)
(local $b56 i64) (local $b57 i64) (local $b58 i64) (local $b59 i64)
(local $b60 i64) (local $b61 i64) (local $b62 i64) (local $b63 i64)
(local $b64 i64) (local $b65 i64) (local $b66 i64) (local $b67 i64)
(local $b68 i64) (local $b69 i64) (local $b70 i64) (local $b71 i64)
(local $b72 i64) (local $b73 i64) (local $b74 i64) (local $b75 i64)
(local $b76 i64) (local $b77 i64) (local $b78 i64) (local $b79 i64)
(local $b80 i64) (local $b81 i64) (local $b82 i64) (local $b83 i64)
(local $b84 i64) (local $b85 i64) (local $b86 i64) (local $b87 i64)
(local $b88 i64) (local $b89 i64) (local $b90 i64) (local $b91 i64)
(local $b92 i64) (local $b93 i64) (local $b94 i64) (local $b95 i64)
(local $b96 i64) (local $b97 i64) (local $b98 i64) (local $b99 i64)
(local $b100 i64) (local $b101 i64) (local $b102 i64) (local $b103 i64)
(local $b104 i64) (local $b105 i64) (local $b106 i64) (local $b107 i64)
(local $b108 i64) (local $b109 i64) (local $b110 i64) (local $b111 i64)
(local $b112 i64) (local $b113 i64) (local $b114 i64) (local $b115 i64)
(local $b116 i64) (local $b117 i64) (local $b118 i64) (local $b119 i64)
(local $b120 i64) (local $b121 i64) (local $b122 i64) (local $b123 i64)
(local $b124 i64) (local $b125 i64) (local $b126 i64) (local $b127 i64)
(local $tmp0 i64) (local $tmp1 i64) (local $tmp2 i64) (local $tmp3 i64)
(local $tmp4 i64) (local $tmp5 i64) (local $tmp6 i64) (local $tmp7 i64)
(local $tmp8 i64) (local $tmp9 i64) (local $tmp10 i64) (local $tmp11 i64)
(local $tmp12 i64) (local $tmp13 i64) (local $tmp14 i64) (local $tmp15 i64)
(local $tmp16 i64) (local $tmp17 i64) (local $tmp18 i64) (local $tmp19 i64)
(local $tmp20 i64) (local $tmp21 i64) (local $tmp22 i64) (local $tmp23 i64)
(local $tmp24 i64) (local $tmp25 i64) (local $tmp26 i64) (local $tmp27 i64)
(local $tmp28 i64) (local $tmp29 i64) (local $tmp30 i64) (local $tmp31 i64)
(local $tmp32 i64) (local $tmp33 i64) (local $tmp34 i64) (local $tmp35 i64)
(local $tmp36 i64) (local $tmp37 i64) (local $tmp38 i64) (local $tmp39 i64)
(local $tmp40 i64) (local $tmp41 i64) (local $tmp42 i64) (local $tmp43 i64)