-
Notifications
You must be signed in to change notification settings - Fork 597
/
Copy pathinteger.cairo
2354 lines (2114 loc) · 64 KB
/
integer.cairo
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
use option::OptionTrait;
use result::ResultTrait;
use traits::{Into, TryInto, Default, Felt252DictValue};
use zeroable::{IsZeroResult, NonZeroIntoImpl, Zeroable};
use serde::Serde;
use array::ArrayTrait;
use array::SpanTrait;
// TODO(spapini): Add method for const creation from Integer.
trait NumericLiteral<T>;
impl NumericLiteralfelt252 of NumericLiteral<felt252>;
#[derive(Copy, Drop)]
extern type u128;
impl NumericLiteralu128 of NumericLiteral<u128>;
extern fn u128_const<value>() -> u128 nopanic;
impl U128Serde of Serde<u128> {
fn serialize(self: @u128, ref output: Array<felt252>) {
Into::<u128, felt252>::into(*self).serialize(ref output);
}
fn deserialize(ref serialized: Span<felt252>) -> Option<u128> {
Option::Some(((*serialized.pop_front()?).try_into())?)
}
}
enum U128sFromFelt252Result {
Narrow: u128,
Wide: (u128, u128),
}
extern fn u128s_from_felt252(a: felt252) -> U128sFromFelt252Result implicits(RangeCheck) nopanic;
#[panic_with('u128_from Overflow', u128_from_felt252)]
fn u128_try_from_felt252(a: felt252) -> Option<u128> implicits(RangeCheck) nopanic {
match u128s_from_felt252(a) {
U128sFromFelt252Result::Narrow(x) => Option::Some(x),
U128sFromFelt252Result::Wide(x) => Option::None,
}
}
extern fn u128_to_felt252(a: u128) -> felt252 nopanic;
extern fn u128_overflowing_add(
lhs: u128, rhs: u128
) -> Result<u128, u128> implicits(RangeCheck) nopanic;
extern fn u128_overflowing_sub(
lhs: u128, rhs: u128
) -> Result<u128, u128> implicits(RangeCheck) nopanic;
fn u128_wrapping_add(lhs: u128, rhs: u128) -> u128 implicits(RangeCheck) nopanic {
match u128_overflowing_add(lhs, rhs) {
Result::Ok(x) => x,
Result::Err(x) => x,
}
}
fn u128_wrapping_sub(a: u128, b: u128) -> u128 implicits(RangeCheck) nopanic {
match u128_overflowing_sub(a, b) {
Result::Ok(x) => x,
Result::Err(x) => x,
}
}
/// A type that contains 4 u128s (a, b, c, d) and guarantees that `a * b = 2**128 * c + d`.
///
/// The guarantee is verified by `u128_mul_guarantee_verify`, which is the only way to destruct this
/// type. This way, one can trust that the guarantee holds although it has not yet been verified.
extern type U128MulGuarantee;
/// Multiplies two u128s and returns a `U128MulGuarantee` for the result of `a * b`.
extern fn u128_guarantee_mul(a: u128, b: u128) -> (u128, u128, U128MulGuarantee) nopanic;
/// Verifies the guarantee and returns the result of `a * b`.
extern fn u128_mul_guarantee_verify(guarantee: U128MulGuarantee) implicits(RangeCheck) nopanic;
/// Multiplies two u128s and returns `(high, low)` - the 128-bit parts of the result.
#[inline(always)]
fn u128_wide_mul(a: u128, b: u128) -> (u128, u128) nopanic {
let (high, low, _) = u128_guarantee_mul(a, b);
(high, low)
}
impl U128MulGuaranteeDestruct of Destruct<U128MulGuarantee> {
fn destruct(self: U128MulGuarantee) nopanic {
u128_mul_guarantee_verify(self);
}
}
extern fn u128_sqrt(value: u128) -> u64 implicits(RangeCheck) nopanic;
fn u128_overflowing_mul(lhs: u128, rhs: u128) -> (u128, bool) implicits(RangeCheck) nopanic {
let (top_word, bottom_word) = u128_wide_mul(lhs, rhs);
match u128_to_felt252(top_word) {
0 => (bottom_word, false),
_ => (bottom_word, true),
}
}
fn u128_checked_add(lhs: u128, rhs: u128) -> Option<u128> implicits(RangeCheck) nopanic {
match u128_overflowing_add(lhs, rhs) {
Result::Ok(r) => Option::Some(r),
Result::Err(r) => Option::None,
}
}
impl U128Add of Add<u128> {
fn add(lhs: u128, rhs: u128) -> u128 {
u128_overflowing_add(lhs, rhs).expect('u128_add Overflow')
}
}
impl U128AddEq of AddEq<u128> {
#[inline(always)]
fn add_eq(ref self: u128, other: u128) {
self = Add::add(self, other);
}
}
#[panic_with('u128_sub Overflow', u128_sub)]
fn u128_checked_sub(lhs: u128, rhs: u128) -> Option<u128> implicits(RangeCheck) nopanic {
match u128_overflowing_sub(lhs, rhs) {
Result::Ok(r) => Option::Some(r),
Result::Err(r) => Option::None,
}
}
impl U128Sub of Sub<u128> {
fn sub(lhs: u128, rhs: u128) -> u128 {
u128_overflowing_sub(lhs, rhs).expect('u128_sub Overflow')
}
}
impl U128SubEq of SubEq<u128> {
#[inline(always)]
fn sub_eq(ref self: u128, other: u128) {
self = Sub::sub(self, other);
}
}
fn u128_checked_mul(lhs: u128, rhs: u128) -> Option<u128> implicits(RangeCheck) nopanic {
let (top_word, bottom_word) = u128_wide_mul(lhs, rhs);
match u128_to_felt252(top_word) {
0 => Option::Some(bottom_word),
_ => Option::None,
}
}
impl U128Mul of Mul<u128> {
fn mul(lhs: u128, rhs: u128) -> u128 {
u128_checked_mul(lhs, rhs).expect('u128_mul Overflow')
}
}
impl U128MulEq of MulEq<u128> {
#[inline(always)]
fn mul_eq(ref self: u128, other: u128) {
self = Mul::mul(self, other);
}
}
#[panic_with('u128 is 0', u128_as_non_zero)]
fn u128_try_as_non_zero(a: u128) -> Option<NonZero<u128>> nopanic {
match u128_is_zero(a) {
IsZeroResult::Zero => Option::None,
IsZeroResult::NonZero(x) => Option::Some(x),
}
}
impl U128TryIntoNonZero of TryInto<u128, NonZero<u128>> {
fn try_into(self: u128) -> Option<NonZero<u128>> {
Option::Some(u128_as_non_zero(self))
}
}
impl U128Div of Div<u128> {
fn div(lhs: u128, rhs: u128) -> u128 {
let (q, r) = u128_safe_divmod(lhs, rhs.try_into().expect('Division by 0'));
q
}
}
impl U128DivEq of DivEq<u128> {
#[inline(always)]
fn div_eq(ref self: u128, other: u128) {
self = Div::div(self, other);
}
}
impl U128Rem of Rem<u128> {
fn rem(lhs: u128, rhs: u128) -> u128 {
let (q, r) = u128_safe_divmod(lhs, rhs.try_into().expect('Division by 0'));
r
}
}
impl U128RemEq of RemEq<u128> {
#[inline(always)]
fn rem_eq(ref self: u128, other: u128) {
self = Rem::rem(self, other);
}
}
impl U128DivRem of DivRem<u128> {
fn div_rem(lhs: u128, rhs: NonZero<u128>) -> (u128, u128) {
u128_safe_divmod(lhs, rhs)
}
}
extern fn u128_safe_divmod(
lhs: u128, rhs: NonZero<u128>
) -> (u128, u128) implicits(RangeCheck) nopanic;
extern fn u128_eq(lhs: u128, rhs: u128) -> bool implicits() nopanic;
impl U128PartialEq of PartialEq<u128> {
#[inline(always)]
fn eq(lhs: @u128, rhs: @u128) -> bool {
u128_eq(*lhs, *rhs)
}
#[inline(always)]
fn ne(lhs: @u128, rhs: @u128) -> bool {
!(*lhs == *rhs)
}
}
impl U128PartialOrd of PartialOrd<u128> {
#[inline(always)]
fn le(lhs: u128, rhs: u128) -> bool {
u128_overflowing_sub(rhs, lhs).into_is_ok()
}
#[inline(always)]
fn ge(lhs: u128, rhs: u128) -> bool {
u128_overflowing_sub(lhs, rhs).into_is_ok()
}
#[inline(always)]
fn lt(lhs: u128, rhs: u128) -> bool {
u128_overflowing_sub(lhs, rhs).into_is_err()
}
#[inline(always)]
fn gt(lhs: u128, rhs: u128) -> bool {
u128_overflowing_sub(rhs, lhs).into_is_err()
}
}
extern type Bitwise;
extern fn bitwise(lhs: u128, rhs: u128) -> (u128, u128, u128) implicits(Bitwise) nopanic;
impl U128BitAnd of BitAnd<u128> {
#[inline(always)]
fn bitand(lhs: u128, rhs: u128) -> u128 {
let (v, _, _) = bitwise(lhs, rhs);
v
}
}
impl U128BitXor of BitXor<u128> {
#[inline(always)]
fn bitxor(lhs: u128, rhs: u128) -> u128 {
let (_, v, _) = bitwise(lhs, rhs);
v
}
}
impl U128BitOr of BitOr<u128> {
#[inline(always)]
fn bitor(lhs: u128, rhs: u128) -> u128 {
let (_, _, v) = bitwise(lhs, rhs);
v
}
}
impl U128BitNot of BitNot<u128> {
fn bitnot(a: u128) -> u128 {
BoundedInt::max() - a
}
}
extern fn u128_is_zero(a: u128) -> IsZeroResult<u128> implicits() nopanic;
extern fn u128_byte_reverse(input: u128) -> u128 implicits(Bitwise) nopanic;
#[derive(Copy, Drop)]
extern type u8;
impl NumericLiteralu8 of NumericLiteral<u8>;
extern fn u8_const<value>() -> u8 nopanic;
extern fn u8_to_felt252(a: u8) -> felt252 nopanic;
#[panic_with('u8_from Overflow', u8_from_felt252)]
extern fn u8_try_from_felt252(a: felt252) -> Option<u8> implicits(RangeCheck) nopanic;
extern fn u8_eq(lhs: u8, rhs: u8) -> bool implicits() nopanic;
impl U8Serde of Serde<u8> {
fn serialize(self: @u8, ref output: Array<felt252>) {
Into::<u8, felt252>::into(*self).serialize(ref output);
}
fn deserialize(ref serialized: Span<felt252>) -> Option<u8> {
Option::Some(((*serialized.pop_front()?).try_into())?)
}
}
impl U8PartialEq of PartialEq<u8> {
#[inline(always)]
fn eq(lhs: @u8, rhs: @u8) -> bool {
u8_eq(*lhs, *rhs)
}
#[inline(always)]
fn ne(lhs: @u8, rhs: @u8) -> bool {
!(*lhs == *rhs)
}
}
impl U8PartialOrd of PartialOrd<u8> {
#[inline(always)]
fn le(lhs: u8, rhs: u8) -> bool {
u8_overflowing_sub(rhs, lhs).into_is_ok()
}
#[inline(always)]
fn ge(lhs: u8, rhs: u8) -> bool {
u8_overflowing_sub(lhs, rhs).into_is_ok()
}
#[inline(always)]
fn lt(lhs: u8, rhs: u8) -> bool {
u8_overflowing_sub(lhs, rhs).into_is_err()
}
#[inline(always)]
fn gt(lhs: u8, rhs: u8) -> bool {
u8_overflowing_sub(rhs, lhs).into_is_err()
}
}
extern fn u8_overflowing_add(lhs: u8, rhs: u8) -> Result<u8, u8> implicits(RangeCheck) nopanic;
extern fn u8_overflowing_sub(lhs: u8, rhs: u8) -> Result<u8, u8> implicits(RangeCheck) nopanic;
fn u8_wrapping_add(lhs: u8, rhs: u8) -> u8 implicits(RangeCheck) nopanic {
match u8_overflowing_add(lhs, rhs) {
Result::Ok(x) => x,
Result::Err(x) => x,
}
}
fn u8_wrapping_sub(lhs: u8, rhs: u8) -> u8 implicits(RangeCheck) nopanic {
match u8_overflowing_sub(lhs, rhs) {
Result::Ok(x) => x,
Result::Err(x) => x,
}
}
fn u8_checked_add(lhs: u8, rhs: u8) -> Option<u8> implicits(RangeCheck) nopanic {
match u8_overflowing_add(lhs, rhs) {
Result::Ok(r) => Option::Some(r),
Result::Err(r) => Option::None,
}
}
impl U8Add of Add<u8> {
fn add(lhs: u8, rhs: u8) -> u8 {
u8_overflowing_add(lhs, rhs).expect('u8_add Overflow')
}
}
impl U8AddEq of AddEq<u8> {
#[inline(always)]
fn add_eq(ref self: u8, other: u8) {
self = Add::add(self, other);
}
}
fn u8_checked_sub(lhs: u8, rhs: u8) -> Option<u8> implicits(RangeCheck) nopanic {
match u8_overflowing_sub(lhs, rhs) {
Result::Ok(r) => Option::Some(r),
Result::Err(r) => Option::None,
}
}
impl U8Sub of Sub<u8> {
fn sub(lhs: u8, rhs: u8) -> u8 {
u8_overflowing_sub(lhs, rhs).expect('u8_sub Overflow')
}
}
impl U8SubEq of SubEq<u8> {
#[inline(always)]
fn sub_eq(ref self: u8, other: u8) {
self = Sub::sub(self, other);
}
}
extern fn u8_wide_mul(lhs: u8, rhs: u8) -> u16 implicits() nopanic;
extern fn u8_sqrt(value: u8) -> u8 implicits(RangeCheck) nopanic;
impl U8Mul of Mul<u8> {
fn mul(lhs: u8, rhs: u8) -> u8 {
u8_wide_mul(lhs, rhs).try_into().expect('u8_mul Overflow')
}
}
impl U8MulEq of MulEq<u8> {
#[inline(always)]
fn mul_eq(ref self: u8, other: u8) {
self = Mul::mul(self, other);
}
}
extern fn u8_is_zero(a: u8) -> IsZeroResult<u8> implicits() nopanic;
extern fn u8_safe_divmod(lhs: u8, rhs: NonZero<u8>) -> (u8, u8) implicits(RangeCheck) nopanic;
#[panic_with('u8 is 0', u8_as_non_zero)]
fn u8_try_as_non_zero(a: u8) -> Option<NonZero<u8>> nopanic {
match u8_is_zero(a) {
IsZeroResult::Zero => Option::None,
IsZeroResult::NonZero(x) => Option::Some(x),
}
}
impl U8TryIntoNonZero of TryInto<u8, NonZero<u8>> {
fn try_into(self: u8) -> Option<NonZero<u8>> {
Option::Some(u8_as_non_zero(self))
}
}
impl U8Div of Div<u8> {
fn div(lhs: u8, rhs: u8) -> u8 {
let (q, r) = u8_safe_divmod(lhs, rhs.try_into().expect('Division by 0'));
q
}
}
impl U8DivEq of DivEq<u8> {
#[inline(always)]
fn div_eq(ref self: u8, other: u8) {
self = Div::div(self, other);
}
}
impl U8Rem of Rem<u8> {
fn rem(lhs: u8, rhs: u8) -> u8 {
let (q, r) = u8_safe_divmod(lhs, rhs.try_into().expect('Division by 0'));
r
}
}
impl U8RemEq of RemEq<u8> {
#[inline(always)]
fn rem_eq(ref self: u8, other: u8) {
self = Rem::rem(self, other);
}
}
impl U8DivRem of DivRem<u8> {
fn div_rem(lhs: u8, rhs: NonZero<u8>) -> (u8, u8) {
u8_safe_divmod(lhs, rhs)
}
}
impl U8BitNot of BitNot<u8> {
fn bitnot(a: u8) -> u8 {
BoundedInt::max() - a
}
}
extern fn u8_bitwise(lhs: u8, rhs: u8) -> (u8, u8, u8) implicits(Bitwise) nopanic;
impl U8BitAnd of BitAnd<u8> {
#[inline(always)]
fn bitand(lhs: u8, rhs: u8) -> u8 {
let (v, _, _) = u8_bitwise(lhs, rhs);
v
}
}
impl U8BitXor of BitXor<u8> {
#[inline(always)]
fn bitxor(lhs: u8, rhs: u8) -> u8 {
let (_, v, _) = u8_bitwise(lhs, rhs);
v
}
}
impl U8BitOr of BitOr<u8> {
#[inline(always)]
fn bitor(lhs: u8, rhs: u8) -> u8 {
let (_, _, v) = u8_bitwise(lhs, rhs);
v
}
}
#[derive(Copy, Drop)]
extern type u16;
impl NumericLiteralu16 of NumericLiteral<u16>;
extern fn u16_const<value>() -> u16 nopanic;
extern fn u16_to_felt252(a: u16) -> felt252 nopanic;
#[panic_with('u16_from Overflow', u16_from_felt252)]
extern fn u16_try_from_felt252(a: felt252) -> Option<u16> implicits(RangeCheck) nopanic;
extern fn u16_eq(lhs: u16, rhs: u16) -> bool implicits() nopanic;
impl U16Serde of Serde<u16> {
fn serialize(self: @u16, ref output: Array<felt252>) {
Into::<u16, felt252>::into(*self).serialize(ref output);
}
fn deserialize(ref serialized: Span<felt252>) -> Option<u16> {
Option::Some(((*serialized.pop_front()?).try_into())?)
}
}
impl U16PartialEq of PartialEq<u16> {
#[inline(always)]
fn eq(lhs: @u16, rhs: @u16) -> bool {
u16_eq(*lhs, *rhs)
}
#[inline(always)]
fn ne(lhs: @u16, rhs: @u16) -> bool {
!(*lhs == *rhs)
}
}
impl U16PartialOrd of PartialOrd<u16> {
#[inline(always)]
fn le(lhs: u16, rhs: u16) -> bool {
u16_overflowing_sub(rhs, lhs).into_is_ok()
}
#[inline(always)]
fn ge(lhs: u16, rhs: u16) -> bool {
u16_overflowing_sub(lhs, rhs).into_is_ok()
}
#[inline(always)]
fn lt(lhs: u16, rhs: u16) -> bool {
u16_overflowing_sub(lhs, rhs).into_is_err()
}
#[inline(always)]
fn gt(lhs: u16, rhs: u16) -> bool {
u16_overflowing_sub(rhs, lhs).into_is_err()
}
}
extern fn u16_overflowing_add(lhs: u16, rhs: u16) -> Result<u16, u16> implicits(RangeCheck) nopanic;
extern fn u16_overflowing_sub(lhs: u16, rhs: u16) -> Result<u16, u16> implicits(RangeCheck) nopanic;
fn u16_wrapping_add(lhs: u16, rhs: u16) -> u16 implicits(RangeCheck) nopanic {
match u16_overflowing_add(lhs, rhs) {
Result::Ok(x) => x,
Result::Err(x) => x,
}
}
fn u16_wrapping_sub(lhs: u16, rhs: u16) -> u16 implicits(RangeCheck) nopanic {
match u16_overflowing_sub(lhs, rhs) {
Result::Ok(x) => x,
Result::Err(x) => x,
}
}
fn u16_checked_add(lhs: u16, rhs: u16) -> Option<u16> implicits(RangeCheck) nopanic {
match u16_overflowing_add(lhs, rhs) {
Result::Ok(r) => Option::Some(r),
Result::Err(r) => Option::None,
}
}
impl U16Add of Add<u16> {
fn add(lhs: u16, rhs: u16) -> u16 {
u16_overflowing_add(lhs, rhs).expect('u16_add Overflow')
}
}
impl U16AddEq of AddEq<u16> {
#[inline(always)]
fn add_eq(ref self: u16, other: u16) {
self = Add::add(self, other);
}
}
fn u16_checked_sub(lhs: u16, rhs: u16) -> Option<u16> implicits(RangeCheck) nopanic {
match u16_overflowing_sub(lhs, rhs) {
Result::Ok(r) => Option::Some(r),
Result::Err(r) => Option::None,
}
}
impl U16Sub of Sub<u16> {
fn sub(lhs: u16, rhs: u16) -> u16 {
u16_overflowing_sub(lhs, rhs).expect('u16_sub Overflow')
}
}
impl U16SubEq of SubEq<u16> {
#[inline(always)]
fn sub_eq(ref self: u16, other: u16) {
self = Sub::sub(self, other);
}
}
extern fn u16_wide_mul(lhs: u16, rhs: u16) -> u32 implicits() nopanic;
extern fn u16_sqrt(value: u16) -> u8 implicits(RangeCheck) nopanic;
impl U16Mul of Mul<u16> {
fn mul(lhs: u16, rhs: u16) -> u16 {
u16_wide_mul(lhs, rhs).try_into().expect('u16_mul Overflow')
}
}
impl U16MulEq of MulEq<u16> {
#[inline(always)]
fn mul_eq(ref self: u16, other: u16) {
self = Mul::mul(self, other);
}
}
extern fn u16_is_zero(a: u16) -> IsZeroResult<u16> implicits() nopanic;
extern fn u16_safe_divmod(lhs: u16, rhs: NonZero<u16>) -> (u16, u16) implicits(RangeCheck) nopanic;
#[panic_with('u16 is 0', u16_as_non_zero)]
fn u16_try_as_non_zero(a: u16) -> Option<NonZero<u16>> nopanic {
match u16_is_zero(a) {
IsZeroResult::Zero => Option::None,
IsZeroResult::NonZero(x) => Option::Some(x),
}
}
impl U16TryIntoNonZero of TryInto<u16, NonZero<u16>> {
fn try_into(self: u16) -> Option<NonZero<u16>> {
Option::Some(u16_as_non_zero(self))
}
}
impl U16Div of Div<u16> {
fn div(lhs: u16, rhs: u16) -> u16 {
let (q, r) = u16_safe_divmod(lhs, rhs.try_into().expect('Division by 0'));
q
}
}
impl U16DivEq of DivEq<u16> {
#[inline(always)]
fn div_eq(ref self: u16, other: u16) {
self = Div::div(self, other);
}
}
impl U16Rem of Rem<u16> {
fn rem(lhs: u16, rhs: u16) -> u16 {
let (q, r) = u16_safe_divmod(lhs, rhs.try_into().expect('Division by 0'));
r
}
}
impl U16RemEq of RemEq<u16> {
#[inline(always)]
fn rem_eq(ref self: u16, other: u16) {
self = Rem::rem(self, other);
}
}
impl U16DivRem of DivRem<u16> {
fn div_rem(lhs: u16, rhs: NonZero<u16>) -> (u16, u16) {
u16_safe_divmod(lhs, rhs)
}
}
impl U16BitNot of BitNot<u16> {
fn bitnot(a: u16) -> u16 {
BoundedInt::max() - a
}
}
extern fn u16_bitwise(lhs: u16, rhs: u16) -> (u16, u16, u16) implicits(Bitwise) nopanic;
impl U16BitAnd of BitAnd<u16> {
#[inline(always)]
fn bitand(lhs: u16, rhs: u16) -> u16 {
let (v, _, _) = u16_bitwise(lhs, rhs);
v
}
}
impl U16BitXor of BitXor<u16> {
#[inline(always)]
fn bitxor(lhs: u16, rhs: u16) -> u16 {
let (_, v, _) = u16_bitwise(lhs, rhs);
v
}
}
impl U16BitOr of BitOr<u16> {
#[inline(always)]
fn bitor(lhs: u16, rhs: u16) -> u16 {
let (_, _, v) = u16_bitwise(lhs, rhs);
v
}
}
#[derive(Copy, Drop)]
extern type u32;
impl NumericLiteralu32 of NumericLiteral<u32>;
extern fn u32_const<value>() -> u32 nopanic;
extern fn u32_to_felt252(a: u32) -> felt252 nopanic;
#[panic_with('u32_from Overflow', u32_from_felt252)]
extern fn u32_try_from_felt252(a: felt252) -> Option<u32> implicits(RangeCheck) nopanic;
extern fn u32_eq(lhs: u32, rhs: u32) -> bool implicits() nopanic;
impl U32Serde of Serde<u32> {
fn serialize(self: @u32, ref output: Array<felt252>) {
Into::<u32, felt252>::into(*self).serialize(ref output);
}
fn deserialize(ref serialized: Span<felt252>) -> Option<u32> {
Option::Some(((*serialized.pop_front()?).try_into())?)
}
}
impl U32PartialEq of PartialEq<u32> {
#[inline(always)]
fn eq(lhs: @u32, rhs: @u32) -> bool {
u32_eq(*lhs, *rhs)
}
#[inline(always)]
fn ne(lhs: @u32, rhs: @u32) -> bool {
!(*lhs == *rhs)
}
}
impl U32PartialOrd of PartialOrd<u32> {
#[inline(always)]
fn le(lhs: u32, rhs: u32) -> bool {
u32_overflowing_sub(rhs, lhs).into_is_ok()
}
#[inline(always)]
fn ge(lhs: u32, rhs: u32) -> bool {
u32_overflowing_sub(lhs, rhs).into_is_ok()
}
#[inline(always)]
fn lt(lhs: u32, rhs: u32) -> bool {
u32_overflowing_sub(lhs, rhs).into_is_err()
}
#[inline(always)]
fn gt(lhs: u32, rhs: u32) -> bool {
u32_overflowing_sub(rhs, lhs).into_is_err()
}
}
extern fn u32_overflowing_add(lhs: u32, rhs: u32) -> Result<u32, u32> implicits(RangeCheck) nopanic;
extern fn u32_overflowing_sub(lhs: u32, rhs: u32) -> Result<u32, u32> implicits(RangeCheck) nopanic;
fn u32_wrapping_add(lhs: u32, rhs: u32) -> u32 implicits(RangeCheck) nopanic {
match u32_overflowing_add(lhs, rhs) {
Result::Ok(x) => x,
Result::Err(x) => x,
}
}
fn u32_wrapping_sub(lhs: u32, rhs: u32) -> u32 implicits(RangeCheck) nopanic {
match u32_overflowing_sub(lhs, rhs) {
Result::Ok(x) => x,
Result::Err(x) => x,
}
}
fn u32_checked_add(lhs: u32, rhs: u32) -> Option<u32> implicits(RangeCheck) nopanic {
match u32_overflowing_add(lhs, rhs) {
Result::Ok(r) => Option::Some(r),
Result::Err(r) => Option::None,
}
}
impl U32Add of Add<u32> {
fn add(lhs: u32, rhs: u32) -> u32 {
u32_overflowing_add(lhs, rhs).expect('u32_add Overflow')
}
}
impl U32AddEq of AddEq<u32> {
#[inline(always)]
fn add_eq(ref self: u32, other: u32) {
self = Add::add(self, other);
}
}
fn u32_checked_sub(lhs: u32, rhs: u32) -> Option<u32> implicits(RangeCheck) nopanic {
match u32_overflowing_sub(lhs, rhs) {
Result::Ok(r) => Option::Some(r),
Result::Err(r) => Option::None,
}
}
impl U32Sub of Sub<u32> {
fn sub(lhs: u32, rhs: u32) -> u32 {
u32_overflowing_sub(lhs, rhs).expect('u32_sub Overflow')
}
}
impl U32SubEq of SubEq<u32> {
#[inline(always)]
fn sub_eq(ref self: u32, other: u32) {
self = Sub::sub(self, other);
}
}
extern fn u32_wide_mul(lhs: u32, rhs: u32) -> u64 implicits() nopanic;
extern fn u32_sqrt(value: u32) -> u16 implicits(RangeCheck) nopanic;
impl U32Mul of Mul<u32> {
fn mul(lhs: u32, rhs: u32) -> u32 {
u32_wide_mul(lhs, rhs).try_into().expect('u32_mul Overflow')
}
}
impl U32MulEq of MulEq<u32> {
#[inline(always)]
fn mul_eq(ref self: u32, other: u32) {
self = Mul::mul(self, other);
}
}
extern fn u32_is_zero(a: u32) -> IsZeroResult<u32> implicits() nopanic;
extern fn u32_safe_divmod(lhs: u32, rhs: NonZero<u32>) -> (u32, u32) implicits(RangeCheck) nopanic;
#[panic_with('u32 is 0', u32_as_non_zero)]
fn u32_try_as_non_zero(a: u32) -> Option<NonZero<u32>> nopanic {
match u32_is_zero(a) {
IsZeroResult::Zero => Option::None,
IsZeroResult::NonZero(x) => Option::Some(x),
}
}
impl U32TryIntoNonZero of TryInto<u32, NonZero<u32>> {
fn try_into(self: u32) -> Option<NonZero<u32>> {
Option::Some(u32_as_non_zero(self))
}
}
impl U32Div of Div<u32> {
fn div(lhs: u32, rhs: u32) -> u32 {
let (q, r) = u32_safe_divmod(lhs, rhs.try_into().expect('Division by 0'));
q
}
}
impl U32DivEq of DivEq<u32> {
#[inline(always)]
fn div_eq(ref self: u32, other: u32) {
self = Div::div(self, other);
}
}
impl U32Rem of Rem<u32> {
fn rem(lhs: u32, rhs: u32) -> u32 {
let (q, r) = u32_safe_divmod(lhs, rhs.try_into().expect('Division by 0'));
r
}
}
impl U32RemEq of RemEq<u32> {
#[inline(always)]
fn rem_eq(ref self: u32, other: u32) {
self = Rem::rem(self, other);
}
}
impl U32DivRem of DivRem<u32> {
fn div_rem(lhs: u32, rhs: NonZero<u32>) -> (u32, u32) {
u32_safe_divmod(lhs, rhs)
}
}
impl U32BitNot of BitNot<u32> {
fn bitnot(a: u32) -> u32 {
BoundedInt::max() - a
}
}
extern fn u32_bitwise(lhs: u32, rhs: u32) -> (u32, u32, u32) implicits(Bitwise) nopanic;
impl U32BitAnd of BitAnd<u32> {
#[inline(always)]
fn bitand(lhs: u32, rhs: u32) -> u32 {
let (v, _, _) = u32_bitwise(lhs, rhs);
v
}
}
impl U32BitXor of BitXor<u32> {
#[inline(always)]
fn bitxor(lhs: u32, rhs: u32) -> u32 {
let (_, v, _) = u32_bitwise(lhs, rhs);
v
}
}
impl U32BitOr of BitOr<u32> {
#[inline(always)]
fn bitor(lhs: u32, rhs: u32) -> u32 {
let (_, _, v) = u32_bitwise(lhs, rhs);
v
}
}
#[derive(Copy, Drop)]
extern type u64;
impl NumericLiteralu64 of NumericLiteral<u64>;
extern fn u64_const<value>() -> u64 nopanic;
extern fn u64_to_felt252(a: u64) -> felt252 nopanic;
#[panic_with('u64_from Overflow', u64_from_felt252)]
extern fn u64_try_from_felt252(a: felt252) -> Option<u64> implicits(RangeCheck) nopanic;
extern fn u64_eq(lhs: u64, rhs: u64) -> bool implicits() nopanic;
impl U64Serde of Serde<u64> {
fn serialize(self: @u64, ref output: Array<felt252>) {
Into::<u64, felt252>::into(*self).serialize(ref output);
}
fn deserialize(ref serialized: Span<felt252>) -> Option<u64> {
Option::Some(((*serialized.pop_front()?).try_into())?)
}
}
impl U64PartialEq of PartialEq<u64> {
#[inline(always)]
fn eq(lhs: @u64, rhs: @u64) -> bool {
u64_eq(*lhs, *rhs)
}
#[inline(always)]
fn ne(lhs: @u64, rhs: @u64) -> bool {
!(*lhs == *rhs)
}
}
impl U64PartialOrd of PartialOrd<u64> {
#[inline(always)]
fn le(lhs: u64, rhs: u64) -> bool {
u64_overflowing_sub(rhs, lhs).into_is_ok()
}
#[inline(always)]
fn ge(lhs: u64, rhs: u64) -> bool {
u64_overflowing_sub(lhs, rhs).into_is_ok()
}
#[inline(always)]
fn lt(lhs: u64, rhs: u64) -> bool {
u64_overflowing_sub(lhs, rhs).into_is_err()
}
#[inline(always)]
fn gt(lhs: u64, rhs: u64) -> bool {
u64_overflowing_sub(rhs, lhs).into_is_err()
}
}
extern fn u64_overflowing_add(lhs: u64, rhs: u64) -> Result<u64, u64> implicits(RangeCheck) nopanic;
extern fn u64_overflowing_sub(lhs: u64, rhs: u64) -> Result<u64, u64> implicits(RangeCheck) nopanic;
fn u64_wrapping_add(lhs: u64, rhs: u64) -> u64 implicits(RangeCheck) nopanic {
match u64_overflowing_add(lhs, rhs) {
Result::Ok(x) => x,
Result::Err(x) => x,
}
}
fn u64_wrapping_sub(lhs: u64, rhs: u64) -> u64 implicits(RangeCheck) nopanic {
match u64_overflowing_sub(lhs, rhs) {
Result::Ok(x) => x,
Result::Err(x) => x,
}
}
fn u64_checked_add(lhs: u64, rhs: u64) -> Option<u64> implicits(RangeCheck) nopanic {
match u64_overflowing_add(lhs, rhs) {
Result::Ok(r) => Option::Some(r),
Result::Err(r) => Option::None,
}
}
impl U64Add of Add<u64> {
fn add(lhs: u64, rhs: u64) -> u64 {
u64_overflowing_add(lhs, rhs).expect('u64_add Overflow')
}
}
impl U64AddEq of AddEq<u64> {
#[inline(always)]
fn add_eq(ref self: u64, other: u64) {
self = Add::add(self, other);
}
}
fn u64_checked_sub(lhs: u64, rhs: u64) -> Option<u64> implicits(RangeCheck) nopanic {
match u64_overflowing_sub(lhs, rhs) {
Result::Ok(r) => Option::Some(r),
Result::Err(r) => Option::None,
}
}
impl U64Sub of Sub<u64> {
fn sub(lhs: u64, rhs: u64) -> u64 {
u64_overflowing_sub(lhs, rhs).expect('u64_sub Overflow')
}
}
impl U64SubEq of SubEq<u64> {
#[inline(always)]
fn sub_eq(ref self: u64, other: u64) {
self = Sub::sub(self, other);
}
}
extern fn u64_wide_mul(lhs: u64, rhs: u64) -> u128 implicits() nopanic;
extern fn u64_sqrt(value: u64) -> u32 implicits(RangeCheck) nopanic;
impl U64Mul of Mul<u64> {
fn mul(lhs: u64, rhs: u64) -> u64 {
u64_wide_mul(lhs, rhs).try_into().expect('u64_mul Overflow')
}
}
impl U64MulEq of MulEq<u64> {
#[inline(always)]
fn mul_eq(ref self: u64, other: u64) {
self = Mul::mul(self, other);
}
}
extern fn u64_is_zero(a: u64) -> IsZeroResult<u64> implicits() nopanic;
extern fn u64_safe_divmod(lhs: u64, rhs: NonZero<u64>) -> (u64, u64) implicits(RangeCheck) nopanic;
#[panic_with('u64 is 0', u64_as_non_zero)]
fn u64_try_as_non_zero(a: u64) -> Option<NonZero<u64>> nopanic {
match u64_is_zero(a) {
IsZeroResult::Zero => Option::None,
IsZeroResult::NonZero(x) => Option::Some(x),
}
}
impl U64TryIntoNonZero of TryInto<u64, NonZero<u64>> {
fn try_into(self: u64) -> Option<NonZero<u64>> {
Option::Some(u64_as_non_zero(self))
}
}