Repository navigation
Expand file tree
/
Copy pathModexp.lean
More file actions
8938 lines (8811 loc) · 483 KB
/
Copy pathModexp.lean
File metadata and controls
8938 lines (8811 loc) · 483 KB
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
import FeProofs.Precompiles.Exported.Shared
/-!
# Precompiles: the exported contract `Modexp`
The definitions between the markers are the output of `fe-lean-export` on the contract `Modexp` of
the ingot `FeProofs/Precompiles` (Fe branch `fevm-precompiles`, golden file
`tests/golden/precompiles_modexp.lean`), copied in by `scripts/sync_exported.py`: fevm's MODEXP
(`modexp::run`) on the call data in memory, given all the gas a `u64` holds, with the bignum
arithmetic of `modexp.fe` and `bignum.fe` (the small moduli of one to four limbs at `Small_1` to
`Small_4`, the Montgomery and the even cases, the division of `Modulus`), the fallback arm
(`recv_fallback`), the runtime entry and the deployment entry. The definitions the ingot's contracts
share are in `Shared.lean`, which the text names through `Shared`, and the core and std definitions
in `FeProofs/Lib/Exported.lean`.
-/
open Ethereum Ethereum.EVM Fe Fe.EVM
namespace FeProofs.Precompiles.Exported.Modexp
section
set_option linter.unusedVariables false
-- `Modulus_reduce` and `even_power` run loops one after another, each inside the exit of the one
-- before, with many variables: Lean elaborates them deeper than its default limit of 512.
set_option maxRecDepth 2048
-- Mathlib's and Batteries' linters walk the elaboration of each definition recursively and run
-- out of stack on the deepest of them; the exported text has nothing for them to check.
set_option linter.all false
-- BEGIN fe-lean-export: precompiles_modexp
export Lib (Evm_calldatacopy Evm_calldatasize Evm_return_data Evm_revert MemArray_as_slice_mut_u64 MemArray_as_slice_ref_u64 MemArray_drop_u64 MemArray_index_mut_u64 MemArray_index_ref_u64 MemArray_len_u64 MemArray_ptr_u64 MemArray_try_new_zeroed_u64 MemArray_u64 MemArray_zero_u64 MemBuffer MemBuffer_drop MemBuffer_empty MemBuffer_len MemBuffer_new MemBuffer_ptr MemBuffer_span_ref MemBuffer_try_new MemSlice_copy_from_u64 MemSlice_empty_u8 MemSlice_head_mut_u64 MemSlice_head_ref_u64 MemSlice_len_u64 MemSlice_ptr_u64 MemSlice_slice_mut_u64 MemSlice_slice_ref_u64 MemSlice_u64 Option_MemArray_u64 Option_MemArray_u64.None Option_MemArray_u64.Some Range_Known_0_Known_5 Range_Known_0_Known_5.mk Range_Known_0_Known_6 Range_Known_0_Known_6.mk Range_Known_0_Known_8 Range_Known_0_Known_8.mk Range_Known_0_Unknown Range_Known_0_Unknown.mk Range_Known_1_Unknown Range_Known_1_Unknown.mk Range_Unknown_Unknown Range_Unknown_Unknown.mk Range_Unknown_Unknown_at Range_Unknown_Unknown_has Range_Unknown_Unknown_next Range_Unknown_Unknown_start Range_at_0 Range_at_0_5 Range_at_0_6 Range_at_0_8 Range_at_1 Range_has_0 Range_has_0_5 Range_has_0_6 Range_has_0_8 Range_has_1 Range_next_0 Range_next_0_5 Range_next_0_6 Range_next_0_8 Range_next_1 Range_start_0 Range_start_0_5 Range_start_0_6 Range_start_0_8 Range_start_1 cast_u8_u64 downcast_truncate_u128_u64 downcast_truncate_u256_u64 downcast_truncate_u256_usize downcast_truncate_u64_u8 downcast_truncate_u64_usize downcast_truncate_usize_u64 downcast_truncate_usize_usize downcast_unchecked_u256_usize «assert»)
export Shared (MemBuffer_byte_at MemBuffer_lane_at MemBuffer_len_1 Output Output.mk SUCCESS adc host_allocation sbb)
structure Exponent where
limbs : MemArray_u64
top : UInt256
total : UInt256
deriving Inhabited
inductive Option_Exponent where
| None
| Some (f0_1 : Exponent)
deriving Inhabited
def Option_Exponent.tag : Option_Exponent → UInt256
| .None => UInt256.ofNat 0
| .Some .. => UInt256.ofNat 1
def Option_Exponent.Some_0 : Option_Exponent → Exponent
| .Some f0_1 => f0_1
| _ => default
def Option_Exponent.set_Some_0 (x : Option_Exponent) (v : Exponent) : Option_Exponent :=
match x with
| .Some f0_1 => .Some v
| x => x
structure Modulus where
limbs : MemArray_u64
len : UInt256
shift : UInt256
reciprocal : UInt256
deriving Inhabited
inductive Option_Modulus where
| None
| Some (f0_1 : Modulus)
deriving Inhabited
def Option_Modulus.tag : Option_Modulus → UInt256
| .None => UInt256.ofNat 0
| .Some .. => UInt256.ofNat 1
def Option_Modulus.Some_0 : Option_Modulus → Modulus
| .Some f0_1 => f0_1
| _ => default
def Option_Modulus.set_Some_0 (x : Option_Modulus) (v : Modulus) : Option_Modulus :=
match x with
| .Some f0_1 => .Some v
| x => x
structure Wide where
l0 : UInt256
l1 : UInt256
l2 : UInt256
l3 : UInt256
deriving Inhabited
structure Small_1 where
m : Wide
inverse : UInt256
deriving Inhabited
structure Window where
«end» : UInt256
entry : UInt256
odd : Bool
deriving Inhabited
structure Small_2 where
m : Wide
inverse : UInt256
deriving Inhabited
structure Small_3 where
m : Wide
inverse : UInt256
deriving Inhabited
structure Small_4 where
m : Wide
inverse : UInt256
deriving Inhabited
structure Montgomery where
limbs : MemArray_u64
len : UInt256
inverse : UInt256
deriving Inhabited
/-- The `init` of contract `Modexp`, which its source leaves out -/
def init : FeM Unit := do
return ()
/-- `lib::input<Evm>` -/
def input_Evm_6 : FeM MemBuffer := do
let mut input : MemBuffer := default
-- let len = raw.calldatasize()
let len ← Evm_calldatasize
-- var input = MemBuffer::new(len.downcast_unchecked())
let v3 ← downcast_unchecked_u256_usize len
let input_4 ← MemBuffer_new v3
input := input_4
-- raw.calldatacopy(dest: mut input, dest_offset: 0, src_offset: 0, len)
let (v10, v10_0) ← Evm_calldatacopy input (UInt256.ofNat 0) (UInt256.ofNat 0) len
input := v10_0
return input
/-- `lib::modexp::input_word<MemBuffer>` -/
def input_word_MemBuffer (input : MemBuffer) (offset : UInt256) (len : UInt256) : FeM UInt256 := do
let mut value : UInt256 := default
let mut i : UInt256 := default
-- let end = input.len()
let «end» ← MemBuffer_len_1 input
let join : UInt256 → UInt256 → UInt256 → FeM UInt256 := fun value i present => do
let mut value := value
let mut i := i
-- if present == 0 { return 0 }
if decide (present = (UInt256.ofNat 0)) then
return UInt256.ofNat 0
else
-- var value: u256 = 0
value := (UInt256.ofNat 0)
-- var i: usize = 0
i := (UInt256.ofNat 0)
let rec loop (value : UInt256) (i : UInt256) : FeM UInt256 := do
let mut value := value
let mut i := i
-- while i + 8 <= present {
let v35 := decide ((i + (UInt256.ofNat 8)).toNat ≤ present.toNat)
if v35 then
-- value = (value << 64) | swap_bytes(input.lane_at(offset + i)) as u256
let v39 := value <<< (UInt256.ofNat 64)
let v43 ← MemBuffer_lane_at input (offset + i)
value := (v39 ||| (IntTy.swapBytes (.u 64) v43))
-- i += 8
i := (i + (UInt256.ofNat 8))
loop value i
else
let rec loop_1 (value : UInt256) (i : UInt256) : FeM UInt256 := do
let mut value := value
let mut i := i
-- while i < present {
if decide (i.toNat < present.toNat) then
-- value = (value << 8) | input.byte_at(offset + i) as u256
let v60 := value <<< (UInt256.ofNat 8)
let v64 ← MemBuffer_byte_at input (offset + i)
value := (v60 ||| v64)
-- i += 1
i := (i + (UInt256.ofNat 1))
loop_1 value i
else
return value <<< ((len - present) * (UInt256.ofNat 8))
partial_fixpoint
loop_1 value i
partial_fixpoint
loop value i
-- let present = if offset >= end { 0 } else if end - offset < len { end - offset } else { len }
if decide («end».toNat ≤ offset.toNat) then
join value i (UInt256.ofNat 0)
else
let join_1 : UInt256 → UInt256 → UInt256 → FeM UInt256 := fun value i v4 => do
let mut value := value
let mut i := i
join value i v4
if decide ((«end» - offset).toNat < len.toNat) then
join_1 value i («end» - offset)
else
join_1 value i len
/-- `lib::modexp::MAX_LENGTH` -/
def MAX_LENGTH : FeM UInt256 := do
return UInt256.ofNat 1024
/-- `lib::FAILURE` -/
def FAILURE : FeM UInt256 := do
return UInt256.ofNat 1
/-- `lib::modexp::bit_length` -/
def bit_length (value : UInt256) : FeM UInt256 := do
-- (256 - leading_zeros(value)).downcast_truncate()
let v3 ← checkedSub (UInt256.ofNat 256) (IntTy.leadingZeros (.u 256) value)
let v4 ← downcast_truncate_u256_u64 v3
return v4
/-- `lib::modexp::gas_cost<MemBuffer>` -/
def gas_cost_MemBuffer (input : MemBuffer) (base : UInt256) (exponent : UInt256) (modulus : UInt256) : FeM UInt256 := do
let join : UInt256 → FeM UInt256 := fun v4 => do
-- let longest: u64 = (if base > modulus { base } else { modulus }).downcast_truncate()
let longest ← downcast_truncate_usize_u64 v4
-- let words = (longest + 7) / 8
let v21 ← IntTy.add (.u 64) longest (UInt256.ofNat 7)
let words ← checkedDiv v21 (UInt256.ofNat 8)
let join_1 : UInt256 → FeM UInt256 := fun complexity => do
-- let head = input_word(input, offset: 96 + base, len: if exponent < 32 { exponent } else { 32 })
let v36 ← checkedAdd (UInt256.ofNat 96) base
let join_2 : UInt256 → FeM UInt256 := fun v6 => do
-- let head = input_word(input, offset: 96 + base, len: if exponent < 32 { exponent } else { 32 })
let head ← input_word_MemBuffer input v36 v6
-- let head_bits = bit_length(head)
let head_bits ← bit_length head
let join_3 : UInt256 → FeM UInt256 := fun top_bits => do
let join_4 : UInt256 → FeM UInt256 := fun extra => do
-- let count = 16 * extra + top_bits
let v68 ← IntTy.mul (.u 64) (UInt256.ofNat 16) extra
let count ← IntTy.add (.u 64) v68 top_bits
let join_5 : UInt256 → FeM UInt256 := fun v9 => do
-- let cost = complexity * if count == 0 { 1 } else { count }
let cost ← IntTy.mul (.u 64) complexity v9
let join_6 : UInt256 → FeM UInt256 := fun v10 => do
return v10
-- if cost < 500 { 500 } else { cost }
if decide (cost.toNat < (UInt256.ofNat 500).toNat) then
join_6 (UInt256.ofNat 500)
else
join_6 cost
-- let cost = complexity * if count == 0 { 1 } else { count }
if decide (count = (UInt256.ofNat 0)) then
join_5 (UInt256.ofNat 1)
else
join_5 count
-- let extra: u64 = if exponent > 32 { (exponent - 32).downcast_truncate() } else { 0 }
if decide ((UInt256.ofNat 32).toNat < exponent.toNat) then
-- (exponent - 32).downcast_truncate()
let v61 ← checkedSub exponent (UInt256.ofNat 32)
let v62 ← downcast_truncate_usize_u64 v61
join_4 v62
else
join_4 (UInt256.ofNat 0)
-- let top_bits = if head_bits > 0 { head_bits - 1 } else { 0 }
if decide ((UInt256.ofNat 0).toNat < head_bits.toNat) then
-- head_bits - 1
let v51 ← IntTy.sub (.u 64) head_bits (UInt256.ofNat 1)
join_3 v51
else
join_3 (UInt256.ofNat 0)
if decide (exponent.toNat < (UInt256.ofNat 32).toNat) then
join_2 exponent
else
join_2 (UInt256.ofNat 32)
-- let complexity = if longest > 32 { 2 * words * words } else { 16 }
if decide ((UInt256.ofNat 32).toNat < longest.toNat) then
-- 2 * words * words
let v28 ← IntTy.mul (.u 64) (UInt256.ofNat 2) words
let v29 ← IntTy.mul (.u 64) v28 words
join_1 v29
else
join_1 (UInt256.ofNat 16)
-- let longest: u64 = (if base > modulus { base } else { modulus }).downcast_truncate()
if decide (modulus.toNat < base.toNat) then
join base
else
join modulus
/-- `lib::modexp::input_byte<MemBuffer>` -/
def input_byte_MemBuffer (input : MemBuffer) (index : UInt256) : FeM UInt256 := do
-- if index < input.len() { input.byte_at(index) } else { 0 }
let v4 ← MemBuffer_len_1 input
let join : UInt256 → FeM UInt256 := fun v2 => do
return v2
if decide (index.toNat < v4.toNat) then
-- input.byte_at(index)
let v6 ← MemBuffer_byte_at input index
join v6
else
join (UInt256.ofNat 0)
/-- `lib::modexp::input_limb<MemBuffer>` -/
def input_limb_MemBuffer (input : MemBuffer) (offset : UInt256) (len : UInt256) (index : UInt256) : FeM UInt256 := do
let mut value : UInt256 := default
let mut byte : UInt256 := default
-- let end = offset + len - index * 8
let «end» := (offset + len) - (index * (UInt256.ofNat 8))
-- if index * 8 + 8 <= len && end <= input.len() { return swap_bytes(input.lane_at(end - 8)) }
let v18 := decide (((index * (UInt256.ofNat 8)) + (UInt256.ofNat 8)).toNat ≤ len.toNat)
let join : UInt256 → UInt256 → FeM UInt256 := fun value byte => do
let mut value := value
let mut byte := byte
-- var value: u64 = 0
value := (UInt256.ofNat 0)
-- var byte: usize = 8
byte := (UInt256.ofNat 8)
let rec loop (value : UInt256) (byte : UInt256) : FeM UInt256 := do
let mut value := value
let mut byte := byte
-- while byte > 0 {
if decide ((UInt256.ofNat 0).toNat < byte.toNat) then
-- byte -= 1
byte := (byte - (UInt256.ofNat 1))
-- let position = index * 8 + byte
let position := (index * (UInt256.ofNat 8)) + byte
-- value = value << 8
value := (IntTy.shl (.u 64) value (UInt256.ofNat 8))
let join_1 : UInt256 → UInt256 → FeM UInt256 := fun value byte => do
let mut value := value
let mut byte := byte
loop value byte
-- if position < len { value |= input_byte(input, offset + len - 1 - position) as u64 }
if decide (position.toNat < len.toNat) then
-- value |= input_byte(input, offset + len - 1 - position) as u64
let v60 ← input_byte_MemBuffer input (((offset + len) - (UInt256.ofNat 1)) - position)
value := (value ||| v60)
join_1 value byte
else
join_1 value byte
else
return value
partial_fixpoint
loop value byte
if v18 then
let v19 ← MemBuffer_len_1 input
if decide («end».toNat ≤ v19.toNat) then
-- return swap_bytes(input.lane_at(end - 8))
let v23 ← MemBuffer_lane_at input («end» - (UInt256.ofNat 8))
return IntTy.swapBytes (.u 64) v23
else
join value byte
else
join value byte
/-- `lib::modexp::input_limbs<MemBuffer>` -/
def input_limbs_MemBuffer (input : MemBuffer) (offset : UInt256) (len : UInt256) (count : UInt256) : FeM Option_MemArray_u64 := do
let mut limbs : MemArray_u64 := default
let mut r1 : Range_Known_0_Unknown := default
-- var limbs = MemArray<u64>::try_new_zeroed(count)?
let v6 ← MemArray_try_new_zeroed_u64 count
if match v6 with | .Some .. => true | _ => false then
let limbs_8 := v6.Some_0
limbs := limbs_8
-- let present = (len + 7) / 8
let v12 ← checkedAdd len (UInt256.ofNat 7)
let present ← checkedDiv v12 (UInt256.ofNat 8)
let join : MemArray_u64 → Range_Known_0_Unknown → UInt256 → FeM Option_MemArray_u64 := fun limbs r1 read => do
let mut limbs := limbs
let mut r1 := r1
-- for i in 0..read { limbs[i] = input_limb(input, offset, len, i) }
r1 := (Range_Known_0_Unknown.mk () read)
let v26 ← Range_start_0 r1
let rec loop (limbs : MemArray_u64) (r1 : Range_Known_0_Unknown) (v5 : UInt256) : FeM Option_MemArray_u64 := do
let mut limbs := limbs
let mut r1 := r1
-- for i in 0..read { limbs[i] = input_limb(input, offset, len, i) }
let v29 ← Range_has_0 r1 v5
if v29 then
let v31 ← Range_at_0 r1 v5
let i := v31
-- limbs[i] = input_limb(input, offset, len, i)
let v33 ← input_limb_MemBuffer input offset len i
let (v35, v35_k) ← MemArray_index_mut_u64 limbs i
mstoreInt (.u 64) v35 v33
let v35_0 ← v35_k v35
limbs := v35_0
-- for i in 0..read { limbs[i] = input_limb(input, offset, len, i) }
let v38 ← Range_next_0 r1 v5
loop limbs r1 v38
else
-- Option::Some(limbs)
let limbs_40 := limbs
return Option_MemArray_u64.Some limbs_40
partial_fixpoint
loop limbs r1 v26
-- let read = if present < count { present } else { count }
if decide (present.toNat < count.toNat) then
join limbs r1 present
else
join limbs r1 count
else
match (← pure v6) with
| .Some .. =>
let _ ← MemArray_drop_u64 v6.Some_0
pure ()
| _ => pure ()
return Option_MemArray_u64.None
/-- `Exponent::try_read<MemBuffer>` -/
def Exponent_try_read_MemBuffer (input : MemBuffer) (start : UInt256) (len : UInt256) : FeM Option_Exponent := do
let mut limbs : MemArray_u64 := default
let mut count : UInt256 := default
-- let total = len * 8
let total ← checkedMul len (UInt256.ofNat 8)
-- let limbs = input_limbs(input, offset: start, len, count: (len + 7) / 8)?
let v10 ← checkedAdd len (UInt256.ofNat 7)
let v12 ← checkedDiv v10 (UInt256.ofNat 8)
let v13 ← input_limbs_MemBuffer input start len v12
if match v13 with | .Some .. => true | _ => false then
let limbs_15 := v13.Some_0
limbs := limbs_15
-- var count = limbs.len()
let count_18 ← MemArray_len_u64 limbs
count := count_18
let rec loop (limbs : MemArray_u64) (count : UInt256) : FeM Option_Exponent := do
let mut limbs := limbs
let mut count := count
let join : MemArray_u64 → UInt256 → FeM Option_Exponent := fun limbs count => do
let mut limbs := limbs
let mut count := count
let join_1 : MemArray_u64 → UInt256 → UInt256 → FeM Option_Exponent := fun limbs count top => do
let mut limbs := limbs
let mut count := count
-- Option::Some(Self { limbs, top, total })
let limbs_68 := limbs
let v70 := (Exponent.mk limbs_68 top total)
return Option_Exponent.Some v70
-- let top = if count == 0 { total } else {
if decide (count = (UInt256.ofNat 0)) then
join_1 limbs count total
else
-- let highest: usize = (255 - leading_zeros(limbs[count - 1] as u256)).downcast_truncate()
let v47 ← checkedSub count (UInt256.ofNat 1)
let v49 ← MemArray_index_ref_u64 limbs v47
let v50 ← mloadInt (.u 64) v49
let v53 ← checkedSub (UInt256.ofNat 255) (IntTy.leadingZeros (.u 256) v50)
let highest ← downcast_truncate_u256_usize v53
-- total - 1 - ((count - 1) * 64 + highest)
let v57 ← checkedSub total (UInt256.ofNat 1)
let v61 ← checkedSub count (UInt256.ofNat 1)
let v63 ← checkedMul v61 (UInt256.ofNat 64)
let v64 ← checkedAdd v63 highest
let v65 ← checkedSub v57 v64
join_1 limbs count v65
-- while count > 0 && limbs[count - 1] == 0 { count -= 1 }
if decide ((UInt256.ofNat 0).toNat < count.toNat) then
let v27 ← checkedSub count (UInt256.ofNat 1)
let v29 ← MemArray_index_ref_u64 limbs v27
let v30 ← mloadInt (.u 64) v29
if decide (v30 = (UInt256.ofNat 0)) then
-- count -= 1
let count_35 ← checkedSub count (UInt256.ofNat 1)
count := count_35
loop limbs count
else
join limbs count
else
join limbs count
partial_fixpoint
loop limbs count
else
match (← pure v13) with
| .Some .. =>
let _ ← MemArray_drop_u64 v13.Some_0
pure ()
| _ => pure ()
return Option_Exponent.None
/-- `lib::bignum::LIMB_BASE` -/
def LIMB_BASE : FeM UInt256 := do
return UInt256.ofNat 18446744073709551616
/-- `lib::bignum::reciprocal_2by1` -/
def reciprocal_2by1 (d : UInt256) : FeM UInt256 := do
-- (all / d as u128 - LIMB_BASE).downcast_truncate()
let v5 ← checkedDiv (UInt256.ofNat 340282366920938463463374607431768211455) d
let v6 ← LIMB_BASE
let v7 ← IntTy.sub (.u 128) v5 v6
let v8 ← downcast_truncate_u128_u64 v7
return v8
/-- `lib::bignum::reciprocal_3by2` -/
def reciprocal_3by2 (d1 : UInt256) (d0 : UInt256) : FeM UInt256 := do
let mut v : UInt256 := default
let mut p : UInt256 := default
-- var v = reciprocal_2by1(d1)
let v_3 ← reciprocal_2by1 d1
v := v_3
-- var p = d1 * v + d0
p := (IntTy.addW (.u 64) (IntTy.mulW (.u 64) d1 v) d0)
let join : UInt256 → UInt256 → FeM UInt256 := fun v p => do
let mut v := v
let mut p := p
-- let t = v as u128 * d0 as u128
let t := IntTy.mulW (.u 128) v d0
-- let t1: u64 = (t >> 64).downcast_truncate()
let t1 ← downcast_truncate_u128_u64 (t >>> (UInt256.ofNat 64))
-- let t0: u64 = t.downcast_truncate()
let t0 ← downcast_truncate_u128_u64 t
-- p += t1
p := (IntTy.addW (.u 64) p t1)
let join_1 : UInt256 → UInt256 → FeM UInt256 := fun v p => do
let mut v := v
let mut p := p
return v
-- if p < t1 {
if decide (p.toNat < t1.toNat) then
-- v -= 1
v := (IntTy.subW (.u 64) v (UInt256.ofNat 1))
let join_2 : UInt256 → UInt256 → Unit → FeM UInt256 := fun v p v2 => do
let mut v := v
let mut p := p
join_1 v p
let join_3 : UInt256 → UInt256 → FeM UInt256 := fun v p => do
let mut v := v
let mut p := p
-- v -= 1
v := (IntTy.subW (.u 64) v (UInt256.ofNat 1))
join_2 v p ()
-- if p > d1 || (p == d1 && t0 >= d0) { v -= 1 }
if decide (d1.toNat < p.toNat) then
join_3 v p
else
let join_4 : UInt256 → UInt256 → FeM UInt256 := fun v p => do
let mut v := v
let mut p := p
join_2 v p ()
if decide (p = d1) then
if decide (d0.toNat ≤ t0.toNat) then
join_3 v p
else
join_4 v p
else
join_4 v p
else
join_1 v p
-- if p < d0 {
if decide (p.toNat < d0.toNat) then
-- v -= 1
v := (IntTy.subW (.u 64) v (UInt256.ofNat 1))
let join_5 : UInt256 → UInt256 → FeM UInt256 := fun v p => do
let mut v := v
let mut p := p
-- p -= d1
p := (IntTy.subW (.u 64) p d1)
join v p
-- if p >= d1 {
if decide (d1.toNat ≤ p.toNat) then
-- v -= 1
v := (IntTy.subW (.u 64) v (UInt256.ofNat 1))
-- p -= d1
p := (IntTy.subW (.u 64) p d1)
join_5 v p
else
join_5 v p
else
join v p
/-- `Modulus::try_new` -/
def Modulus_try_new (value : MemArray_u64) (len : UInt256) : FeM Option_Modulus := do
let mut limbs : MemArray_u64 := default
let mut r1 : Range_Known_0_Unknown := default
-- let count: usize = len.downcast_truncate()
let count ← downcast_truncate_u64_usize len
let join : MemArray_u64 → Range_Known_0_Unknown → Bool → FeM Option_Modulus := fun limbs r1 v2 => do
let mut limbs := limbs
let mut r1 := r1
-- core::assert(len > 0 && count <= value.len() && value[(len - 1).downcast_truncate()] != 0)
let _ ← «assert» v2
-- let shift: u64 = (leading_zeros(value[(len - 1).downcast_truncate()] as u256) - 192).downcast_truncate()
let v28 ← IntTy.sub (.u 64) len (UInt256.ofNat 1)
let v29 ← downcast_truncate_u64_usize v28
let v30 ← MemArray_index_ref_u64 value v29
let v31 ← mloadInt (.u 64) v30
let v35 ← checkedSub (IntTy.leadingZeros (.u 256) v31) (UInt256.ofNat 192)
let shift ← downcast_truncate_u256_u64 v35
-- var limbs = MemArray<u64>::try_new_zeroed(len.downcast_truncate())?
let v38 ← downcast_truncate_u64_usize len
let v39 ← MemArray_try_new_zeroed_u64 v38
if match v39 with | .Some .. => true | _ => false then
let limbs_41 := v39.Some_0
limbs := limbs_41
-- for i in 0..len.downcast_truncate() {
let v44 ← downcast_truncate_u64_usize len
r1 := (Range_Known_0_Unknown.mk () v44)
let v48 ← Range_start_0 r1
let rec loop (limbs : MemArray_u64) (r1 : Range_Known_0_Unknown) (v3 : UInt256) : FeM Option_Modulus := do
let mut limbs := limbs
let mut r1 := r1
-- for i in 0..len.downcast_truncate() {
let v51 ← Range_has_0 r1 v3
if v51 then
let v53 ← Range_at_0 r1 v3
let i := v53
-- let high = value[i] << shift
let v55 ← MemArray_index_ref_u64 value i
let v56 ← mloadInt (.u 64) v55
let high := IntTy.shl (.u 64) v56 shift
let join_1 : MemArray_u64 → Range_Known_0_Unknown → UInt256 → FeM Option_Modulus := fun limbs r1 low => do
let mut limbs := limbs
let mut r1 := r1
-- limbs[i] = high | low
let (v76, v76_k) ← MemArray_index_mut_u64 limbs i
mstoreInt (.u 64) v76 (high ||| low)
let v76_0 ← v76_k v76
limbs := v76_0
-- for i in 0..len.downcast_truncate() {
let v79 ← Range_next_0 r1 v3
loop limbs r1 v79
let join_2 : MemArray_u64 → Range_Known_0_Unknown → FeM Option_Modulus := fun limbs r1 => do
let mut limbs := limbs
let mut r1 := r1
join_1 limbs r1 (UInt256.ofNat 0)
-- let low = if shift == 0 || i == 0 { 0 } else { value[i - 1] >> (64 - shift) }
if decide (shift = (UInt256.ofNat 0)) then
join_2 limbs r1
else
if decide (i = (UInt256.ofNat 0)) then
join_2 limbs r1
else
-- value[i - 1] >> (64 - shift)
let v64 ← checkedSub i (UInt256.ofNat 1)
let v65 ← MemArray_index_ref_u64 value v64
let v66 ← mloadInt (.u 64) v65
let v68 ← IntTy.sub (.u 64) (UInt256.ofNat 64) shift
join_1 limbs r1 (v66 >>> v68)
else
let join_3 : MemArray_u64 → Range_Known_0_Unknown → UInt256 → FeM Option_Modulus := fun limbs r1 reciprocal => do
let mut limbs := limbs
let mut r1 := r1
-- Option::Some(Self { limbs, len, shift, reciprocal })
let limbs_102 := limbs
let v105 := (Modulus.mk limbs_102 len shift reciprocal)
return Option_Modulus.Some v105
-- let reciprocal = if count == 1 { reciprocal_2by1(limbs[0]) } else {
if decide (count = (UInt256.ofNat 1)) then
-- reciprocal_2by1(limbs[0])
let v85 ← MemArray_index_ref_u64 limbs (UInt256.ofNat 0)
let v86 ← mloadInt (.u 64) v85
let v87 ← reciprocal_2by1 v86
join_3 limbs r1 v87
else
-- reciprocal_3by2(limbs[count - 1], limbs[count - 2])
let v90 ← checkedSub count (UInt256.ofNat 1)
let v92 ← MemArray_index_ref_u64 limbs v90
let v94 ← checkedSub count (UInt256.ofNat 2)
let v96 ← MemArray_index_ref_u64 limbs v94
let v97 ← mloadInt (.u 64) v92
let v98 ← mloadInt (.u 64) v96
let v99 ← reciprocal_3by2 v97 v98
join_3 limbs r1 v99
partial_fixpoint
loop limbs r1 v48
else
match (← pure v39) with
| .Some .. =>
let _ ← MemArray_drop_u64 v39.Some_0
pure ()
| _ => pure ()
return Option_Modulus.None
let join_4 : MemArray_u64 → Range_Known_0_Unknown → FeM Option_Modulus := fun limbs r1 => do
let mut limbs := limbs
let mut r1 := r1
join limbs r1 false
-- core::assert(len > 0 && count <= value.len() && value[(len - 1).downcast_truncate()] != 0)
if decide ((UInt256.ofNat 0).toNat < len.toNat) then
let v11 ← MemArray_len_u64 value
if decide (count.toNat ≤ v11.toNat) then
let v15 ← IntTy.sub (.u 64) len (UInt256.ofNat 1)
let v16 ← downcast_truncate_u64_usize v15
let v17 ← MemArray_index_ref_u64 value v16
let v18 ← mloadInt (.u 64) v17
if decide (v18 ≠ (UInt256.ofNat 0)) then
join limbs r1 true
else
join_4 limbs r1
else
join_4 limbs r1
else
join_4 limbs r1
/-- `Wide::of` -/
def Wide_of (limbs : MemArray_u64) (n : UInt256) : FeM Wide := do
-- Wide {
let v6 ← MemArray_index_ref_u64 limbs (UInt256.ofNat 0)
let v7 ← mloadInt (.u 64) v6
let join : UInt256 → FeM Wide := fun v2 => do
let join_1 : UInt256 → FeM Wide := fun v3 => do
let join_2 : UInt256 → FeM Wide := fun v4 => do
return Wide.mk v7 v2 v3 v4
-- Wide {
if decide ((UInt256.ofNat 3).toNat < n.toNat) then
-- limbs[3]
let v30 ← MemArray_index_ref_u64 limbs (UInt256.ofNat 3)
-- Wide {
let v31 ← mloadInt (.u 64) v30
join_2 v31
else
join_2 (UInt256.ofNat 0)
-- Wide {
if decide ((UInt256.ofNat 2).toNat < n.toNat) then
-- limbs[2]
let v21 ← MemArray_index_ref_u64 limbs (UInt256.ofNat 2)
-- Wide {
let v22 ← mloadInt (.u 64) v21
join_1 v22
else
join_1 (UInt256.ofNat 0)
if decide ((UInt256.ofNat 1).toNat < n.toNat) then
-- limbs[1]
let v12 ← MemArray_index_ref_u64 limbs (UInt256.ofNat 1)
-- Wide {
let v13 ← mloadInt (.u 64) v12
join v13
else
join (UInt256.ofNat 0)
/-- `lib::modexp::window_width` -/
def window_width (bits : UInt256) : FeM UInt256 := do
let mut best : UInt256 := default
let mut best_cost : UInt256 := default
let mut w : UInt256 := default
-- var best: usize = 1
best := (UInt256.ofNat 1)
-- var best_cost = bits / 2
let best_cost_5 ← checkedDiv bits (UInt256.ofNat 2)
best_cost := best_cost_5
-- var w: usize = 2
w := (UInt256.ofNat 2)
let rec loop (best : UInt256) (best_cost : UInt256) (w : UInt256) : FeM UInt256 := do
let mut best := best
let mut best_cost := best_cost
let mut w := w
-- while w <= 7 {
if decide (w.toNat ≤ (UInt256.ofNat 7).toNat) then
-- let cost = (1 << (w - 1)) + bits / (w + 1)
let v17 ← checkedSub w (UInt256.ofNat 1)
let v23 ← checkedAdd w (UInt256.ofNat 1)
let v24 ← checkedDiv bits v23
let cost ← checkedAdd ((UInt256.ofNat 1) <<< v17) v24
let join : UInt256 → UInt256 → UInt256 → FeM UInt256 := fun best best_cost w => do
let mut best := best
let mut best_cost := best_cost
let mut w := w
-- w += 1
let w_40 ← checkedAdd w (UInt256.ofNat 1)
w := w_40
loop best best_cost w
-- if cost < best_cost {
if decide (cost.toNat < best_cost.toNat) then
-- best = w
best := w
-- best_cost = cost
best_cost := cost
join best best_cost w
else
join best best_cost w
else
return best
partial_fixpoint
loop best best_cost w
def Small_double_1_k2 (self : Small_1) (x : Wide) (s0 : UInt256) (s1 : UInt256) (s2 : UInt256) (s3 : UInt256) (d0 : UInt256) (d1 : UInt256) (v45 : (UInt256 × UInt256)) (d2 : UInt256) : FeM Wide := do
-- let (d3, borrow) = sbb(s3, self.m.l3, borrow)
let v51 ← sbb s3 self.m.l3 v45.snd
let d3 := v51.fst
-- let (_, borrow) = sbb(x.l3 >> 63, 0, borrow)
let v60 ← sbb (x.l3 >>> (UInt256.ofNat 63)) (UInt256.ofNat 0) v51.snd
let join : Wide → FeM Wide := fun v2 => do
return v2
-- if borrow == 1 { Wide { l0: s0, l1: s1, l2: s2, l3: s3 } } else { Wide { l0: d0, l1: d1, l2: d2, l3: d3 } }
if decide (v60.snd = (UInt256.ofNat 1)) then
join (Wide.mk s0 s1 s2 s3)
else
join (Wide.mk d0 d1 d2 d3)
def Small_double_1_k1 (self : Small_1) (x : Wide) (s0 : UInt256) (s1 : UInt256) (s2 : UInt256) : FeM Wide := do
-- let s3 = (x.l3 << 1) | (x.l2 >> 63)
let s3 := (IntTy.shl (.u 64) x.l3 (UInt256.ofNat 1)) ||| (x.l2 >>> (UInt256.ofNat 63))
-- let (d0, borrow) = sbb(s0, self.m.l0, 0)
let v33 ← sbb s0 self.m.l0 (UInt256.ofNat 0)
let d0 := v33.fst
-- let (d1, borrow) = sbb(s1, self.m.l1, borrow)
let v39 ← sbb s1 self.m.l1 v33.snd
let d1 := v39.fst
-- let (d2, borrow) = sbb(s2, self.m.l2, borrow)
let v45 ← sbb s2 self.m.l2 v39.snd
let d2 := v45.fst
Small_double_1_k2 self x s0 s1 s2 s3 d0 d1 v45 d2
/-- `Small<1>::double` -/
def Small_double_1 (self : Small_1) (x : Wide) : FeM Wide := do
-- let s0 = x.l0 << 1
let s0 := IntTy.shl (.u 64) x.l0 (UInt256.ofNat 1)
-- let s1 = (x.l1 << 1) | (x.l0 >> 63)
let s1 := (IntTy.shl (.u 64) x.l1 (UInt256.ofNat 1)) ||| (x.l0 >>> (UInt256.ofNat 63))
-- let s2 = (x.l2 << 1) | (x.l1 >> 63)
let s2 := (IntTy.shl (.u 64) x.l2 (UInt256.ofNat 1)) ||| (x.l1 >>> (UInt256.ofNat 63))
Small_double_1_k1 self x s0 s1 s2
/-- `lib::montgomery::mac` -/
def mac (a : UInt256) (b : UInt256) (c : UInt256) (carry : UInt256) : FeM (UInt256 × UInt256) := do
-- let t = (a as u128) * (b as u128) + (c as u128) + (carry as u128)
let v8 ← IntTy.mul (.u 128) a b
let v11 ← IntTy.add (.u 128) v8 c
let t ← IntTy.add (.u 128) v11 carry
-- (t.downcast_truncate(), (t >> 64).downcast_truncate())
let v16 ← downcast_truncate_u128_u64 t
let v19 ← downcast_truncate_u128_u64 (t >>> (UInt256.ofNat 64))
return (v16, v19)
def Small_row2_1_k1 (self : Small_1) (t0 : UInt256) (v15 : (UInt256 × UInt256)) (v21 : (UInt256 × UInt256)) (m : UInt256) : FeM (Wide × UInt256) := do
-- let (_, c) = mac(m, self.m.l0, t0, 0)
let v31 ← mac m self.m.l0 t0 (UInt256.ofNat 0)
-- let (r0, c) = mac(m, self.m.l1, t1, c)
let v35 ← mac m self.m.l1 v15.fst v31.snd
-- let (r1, c) = adc(t2, 0, c)
let v41 ← adc v21.fst (UInt256.ofNat 0) v35.snd
-- (Wide { l0: r0, l1: r1, l2: 0, l3: 0 }, t3 + c)
let v48 := (Wide.mk v35.fst v41.fst (UInt256.ofNat 0) (UInt256.ofNat 0))
return (v48, (IntTy.addW (.u 64) v21.snd v41.snd))
/-- `Small<1>::row2` -/
def Small_row2_1 (self : Small_1) (a : Wide) (b : UInt256) (t : Wide) (top : UInt256) : FeM (Wide × UInt256) := do
-- let (t0, c) = mac(a.l0, b, t.l0, 0)
let v8 ← mac a.l0 b t.l0 (UInt256.ofNat 0)
let t0 := v8.fst
-- let (t1, c) = mac(a.l1, b, t.l1, c)
let v15 ← mac a.l1 b t.l1 v8.snd
-- let (t2, t3) = adc(top, c, 0)
let v21 ← adc top v15.snd (UInt256.ofNat 0)
-- let m = t0 * self.inverse
let m := IntTy.mulW (.u 64) t0 self.inverse
Small_row2_1_k1 self t0 v15 v21 m
def Small_row3_1_k2 (v28 : (UInt256 × UInt256)) (v42 : (UInt256 × UInt256)) (v48 : (UInt256 × UInt256)) : FeM (Wide × UInt256) := do
-- let (r2, c) = adc(t3, 0, c)
let v54 ← adc v28.fst (UInt256.ofNat 0) v48.snd
-- (Wide { l0: r0, l1: r1, l2: r2, l3: 0 }, t4 + c)
let v60 := (Wide.mk v42.fst v48.fst v54.fst (UInt256.ofNat 0))
return (v60, (IntTy.addW (.u 64) v28.snd v54.snd))
def Small_row3_1_k1 (self : Small_1) (t0 : UInt256) (v15 : (UInt256 × UInt256)) (v22 : (UInt256 × UInt256)) (v28 : (UInt256 × UInt256)) : FeM (Wide × UInt256) := do
-- let m = t0 * self.inverse
let m := IntTy.mulW (.u 64) t0 self.inverse
-- let (_, c) = mac(m, self.m.l0, t0, 0)
let v38 ← mac m self.m.l0 t0 (UInt256.ofNat 0)
-- let (r0, c) = mac(m, self.m.l1, t1, c)
let v42 ← mac m self.m.l1 v15.fst v38.snd
-- let (r1, c) = mac(m, self.m.l2, t2, c)
let v48 ← mac m self.m.l2 v22.fst v42.snd
Small_row3_1_k2 v28 v42 v48
/-- `Small<1>::row3` -/
def Small_row3_1 (self : Small_1) (a : Wide) (b : UInt256) (t : Wide) (top : UInt256) : FeM (Wide × UInt256) := do
-- let (t0, c) = mac(a.l0, b, t.l0, 0)
let v8 ← mac a.l0 b t.l0 (UInt256.ofNat 0)
let t0 := v8.fst
-- let (t1, c) = mac(a.l1, b, t.l1, c)
let v15 ← mac a.l1 b t.l1 v8.snd
-- let (t2, c) = mac(a.l2, b, t.l2, c)
let v22 ← mac a.l2 b t.l2 v15.snd
-- let (t3, t4) = adc(top, c, 0)
let v28 ← adc top v22.snd (UInt256.ofNat 0)
Small_row3_1_k1 self t0 v15 v22 v28
def Small_row4_1_k2 (self : Small_1) (v22 : (UInt256 × UInt256)) (v29 : (UInt256 × UInt256)) (v35 : (UInt256 × UInt256)) (m : UInt256) (v49 : (UInt256 × UInt256)) : FeM (Wide × UInt256) := do
-- let (r1, c) = mac(m, self.m.l2, t2, c)
let v55 ← mac m self.m.l2 v22.fst v49.snd
-- let (r2, c) = mac(m, self.m.l3, t3, c)
let v61 ← mac m self.m.l3 v29.fst v55.snd
-- let (r3, c) = adc(t4, 0, c)
let v67 ← adc v35.fst (UInt256.ofNat 0) v61.snd
-- (Wide { l0: r0, l1: r1, l2: r2, l3: r3 }, t5 + c)
let v74 := ((Wide.mk v49.fst v55.fst v61.fst v67.fst), (IntTy.addW (.u 64) v35.snd v67.snd))
return v74
def Small_row4_1_k1 (self : Small_1) (a : Wide) (b : UInt256) (t : Wide) (top : UInt256) (t0 : UInt256) (v15 : (UInt256 × UInt256)) (v22 : (UInt256 × UInt256)) : FeM (Wide × UInt256) := do
-- let (t3, c) = mac(a.l3, b, t.l3, c)
let v29 ← mac a.l3 b t.l3 v22.snd
-- let (t4, t5) = adc(top, c, 0)
let v35 ← adc top v29.snd (UInt256.ofNat 0)
-- let m = t0 * self.inverse
let m := IntTy.mulW (.u 64) t0 self.inverse
-- let (_, c) = mac(m, self.m.l0, t0, 0)
let v45 ← mac m self.m.l0 t0 (UInt256.ofNat 0)
-- let (r0, c) = mac(m, self.m.l1, t1, c)
let v49 ← mac m self.m.l1 v15.fst v45.snd
Small_row4_1_k2 self v22 v29 v35 m v49
/-- `Small<1>::row4` -/
def Small_row4_1 (self : Small_1) (a : Wide) (b : UInt256) (t : Wide) (top : UInt256) : FeM (Wide × UInt256) := do
-- let (t0, c) = mac(a.l0, b, t.l0, 0)
let v8 ← mac a.l0 b t.l0 (UInt256.ofNat 0)
let t0 := v8.fst
-- let (t1, c) = mac(a.l1, b, t.l1, c)
let v15 ← mac a.l1 b t.l1 v8.snd
-- let (t2, c) = mac(a.l2, b, t.l2, c)
let v22 ← mac a.l2 b t.l2 v15.snd
Small_row4_1_k1 self a b t top t0 v15 v22
/-- `Small<1>::mul` -/
def Small_mul_1 (self : Small_1) (a : Wide) (b : Wide) : FeM Wide := do
let mut zero : Wide := default
let mut t_r1 : Wide := default
let mut t_r2 : Wide := default
let mut t_r3 : Wide := default
let mut t_r4 : Wide := default
let mut t_r5 : Wide := default
let mut t_r6 : Wide := default
-- let zero = Wide { l0: 0, l1: 0, l2: 0, l3: 0 }
let zero_12 := (Wide.mk (UInt256.ofNat 0) (UInt256.ofNat 0) (UInt256.ofNat 0) (UInt256.ofNat 0))
zero := zero_12
-- if L == 1 {
if decide ((UInt256.ofNat 1) = (UInt256.ofNat 1)) then
-- let t = a.l0 as u128 * b.l0 as u128
let t_21 := IntTy.mulW (.u 128) a.l0 b.l0
-- let low: u64 = t.downcast_truncate()
let low ← downcast_truncate_u128_u64 t_21
-- let product = q as u128 * self.m.l0 as u128
let product := IntTy.mulW (.u 128) (IntTy.mulW (.u 64) low self.inverse) self.m.l0
-- let sum = (t >> 64) + (product >> 64) + if low != 0 { 1 } else { 0 }
let v37 := IntTy.addW (.u 128) (t_21 >>> (UInt256.ofNat 64)) (product >>> (UInt256.ofNat 64))
let join : Wide → Wide → Wide → Wide → Wide → Wide → Wide → UInt256 → FeM Wide := fun zero t_r1 t_r2 t_r3 t_r4 t_r5 t_r6 v3 => do
let mut zero := zero
let mut t_r1 := t_r1
let mut t_r2 := t_r2
let mut t_r3 := t_r3
let mut t_r4 := t_r4
let mut t_r5 := t_r5
let mut t_r6 := t_r6
-- let sum = (t >> 64) + (product >> 64) + if low != 0 { 1 } else { 0 }
let sum := IntTy.addW (.u 128) v37 v3
let join_1 : Wide → Wide → Wide → Wide → Wide → Wide → Wide → UInt256 → FeM Wide := fun zero t_r1 t_r2 t_r3 t_r4 t_r5 t_r6 reduced => do
let mut zero := zero
let mut t_r1 := t_r1
let mut t_r2 := t_r2
let mut t_r3 := t_r3
let mut t_r4 := t_r4
let mut t_r5 := t_r5
let mut t_r6 := t_r6
-- return Wide { l0: reduced.downcast_truncate(), l1: 0, l2: 0, l3: 0 }
let v55 ← downcast_truncate_u128_u64 reduced
let v59 := (Wide.mk v55 (UInt256.ofNat 0) (UInt256.ofNat 0) (UInt256.ofNat 0))
return v59
-- let reduced = if sum >= self.m.l0 as u128 { sum - self.m.l0 as u128 } else { sum }
if decide (self.m.l0.toNat ≤ sum.toNat) then
join_1 zero t_r1 t_r2 t_r3 t_r4 t_r5 t_r6 (IntTy.subW (.u 128) sum self.m.l0)
else
join_1 zero t_r1 t_r2 t_r3 t_r4 t_r5 t_r6 sum
if decide (low ≠ (UInt256.ofNat 0)) then
join zero t_r1 t_r2 t_r3 t_r4 t_r5 t_r6 (UInt256.ofNat 1)
else
join zero t_r1 t_r2 t_r3 t_r4 t_r5 t_r6 (UInt256.ofNat 0)
else
-- if L == 2 {
if decide ((UInt256.ofNat 1) = (UInt256.ofNat 2)) then
-- let (t, top) = self.row2(a, b.l0, zero, 0)
let v68 ← Small_row2_1 self a b.l0 zero (UInt256.ofNat 0)
t_r1 := v68.fst
-- let (t, top) = self.row2(a, b.l1, t, top)
let v75 ← Small_row2_1 self a b.l1 t_r1 v68.snd
let t_76 := v75.fst
-- let (d0, borrow) = sbb(t.l0, self.m.l0, 0)
let v83 ← sbb t_76.l0 self.m.l0 (UInt256.ofNat 0)
let d0_84 := v83.fst
-- let (d1, borrow) = sbb(t.l1, self.m.l1, borrow)
let v90 ← sbb t_76.l1 self.m.l1 v83.snd
let d1_91 := v90.fst
-- let (_, borrow) = sbb(top, 0, borrow)
let v96 ← sbb v75.snd (UInt256.ofNat 0) v90.snd
let join_2 : Wide → Wide → Wide → Wide → Wide → Wide → Wide → Wide → FeM Wide := fun zero t_r1 t_r2 t_r3 t_r4 t_r5 t_r6 v5 => do
let mut zero := zero
let mut t_r1 := t_r1
let mut t_r2 := t_r2
let mut t_r3 := t_r3
let mut t_r4 := t_r4
let mut t_r5 := t_r5
let mut t_r6 := t_r6
return v5
-- return if borrow == 1 { t } else { Wide { l0: d0, l1: d1, l2: 0, l3: 0 } }
if decide (v96.snd = (UInt256.ofNat 1)) then
join_2 zero t_r1 t_r2 t_r3 t_r4 t_r5 t_r6 t_76
else
-- Wide { l0: d0, l1: d1, l2: 0, l3: 0 }
let v104 := (Wide.mk d0_84 d1_91 (UInt256.ofNat 0) (UInt256.ofNat 0))
join_2 zero t_r1 t_r2 t_r3 t_r4 t_r5 t_r6 v104
else
-- if L == 3 {
if decide ((UInt256.ofNat 1) = (UInt256.ofNat 3)) then
-- let (t, top) = self.row3(a, b.l0, zero, 0)
let v114 ← Small_row3_1 self a b.l0 zero (UInt256.ofNat 0)