generated from keep-starknet-strange/alexandria
-
Notifications
You must be signed in to change notification settings - Fork 83
/
math.cairo
724 lines (613 loc) · 19.5 KB
/
math.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
use core::integer::{u512};
use core::num::traits::{Zero, One, BitSize, OverflowingAdd, OverflowingMul};
use core::panic_with_felt252;
use core::traits::{BitAnd};
// === Exponentiation ===
pub trait Exponentiation<T> {
/// Raise a number to a power.
///
/// # Arguments
///
/// * `self` - The base number
/// * `exponent` - The exponent to raise the base to
///
/// # Returns
///
/// The result of raising `self` to the power of `exponent`
///
/// # Panics
///
/// Panics if the result overflows the type T.
fn pow(self: T, exponent: T) -> T;
}
impl ExponentiationImpl<
T,
+Zero<T>,
+One<T>,
+Add<T>,
+Sub<T>,
+Mul<T>,
+Div<T>,
+BitAnd<T>,
+PartialEq<T>,
+Copy<T>,
+Drop<T>
> of Exponentiation<T> {
fn pow(self: T, mut exponent: T) -> T {
let zero = Zero::zero();
if self.is_zero() {
return zero;
}
let one = One::one();
let mut result = one;
let mut base = self;
let two = one + one;
loop {
if exponent & one == one {
result = result * base;
}
exponent = exponent / two;
if exponent == zero {
break result;
}
base = base * base;
}
}
}
pub trait WrappingExponentiation<T> {
/// Raise a number to a power modulo MAX<T> (max value of type T).
/// Instead of explicitly providing a modulo, we use overflowing functions
/// from the core library, which wrap around when overflowing.
///
/// # Arguments
///
/// * `self` - The base number
/// * `exponent` - The exponent to raise the base to
///
/// # Returns
///
/// The result of base raised to the power of exp modulo MAX<T>.
fn wrapping_pow(self: T, exponent: T) -> T;
/// Performs exponentiation by repeatedly multiplying the base number with itself.
///
/// This function uses a simple loop to perform exponentiation. It continues to multiply
/// the base number (`self`) with itself, for the number of times specified by `exponent`.
/// The method uses a wrapping strategy to handle overflow, which means if the result
/// overflows the type `T`, then higher bits are discarded and the result is wrapped.
///
/// # Arguments
///
/// * `self` - The base number of type `T`.
/// * `exponent` - The exponent to which the base number is raised, also of type `T`.
///
/// # Returns
///
/// The result of raising `self` to the power of `exponent`, of type `T`.
/// The result is wrapped in case of overflow.
fn wrapping_spow(self: T, exponent: T) -> T;
/// Performs exponentiation using the binary exponentiation method.
///
/// This function calculates the power of a number using binary exponentiation, which is
/// an optimized method for exponentiation that reduces the number of multiplications.
/// It works by repeatedly squaring the base and reducing the exponent by half, using
/// a wrapping strategy to handle overflow. This means if intermediate or final results
/// overflow the type `T`, then the higher bits are discarded and the result is wrapped.
///
/// # Arguments
///
/// * `self` - The base number of type `T`.
/// * `exponent` - The exponent to which the base number is raised, also of type `T`.
///
/// # Returns
///
/// The result of raising `self` to the power of `exponent`, of type `T`.
/// The result is wrapped in case of overflow.
fn wrapping_fpow(self: T, exponent: T) -> T;
}
pub impl WrappingExponentiationImpl<
T,
+OverflowingMul<T>,
+Zero<T>,
+One<T>,
+Add<T>,
+Mul<T>,
+Div<T>,
+Rem<T>,
+Copy<T>,
+Drop<T>,
+PartialEq<T>,
+PartialOrd<T>,
+core::ops::SubAssign<T, T>
> of WrappingExponentiation<T> {
fn wrapping_pow(self: T, exponent: T) -> T {
if exponent == Zero::zero() {
return One::one();
}
if self == Zero::zero() {
return Zero::zero();
}
let one = One::<T>::one();
let ten = one + one + one + one + one + one + one + one + one + one;
if exponent > ten {
self.wrapping_fpow(exponent)
} else {
self.wrapping_spow(exponent)
}
}
fn wrapping_spow(self: T, exponent: T) -> T {
let mut exponent = exponent;
let mut base = self;
let mut result = One::one();
while exponent != Zero::zero() {
let (new_result, _) = result.overflowing_mul(base);
result = new_result;
exponent -= One::one();
};
result
}
fn wrapping_fpow(self: T, exponent: T) -> T {
let mut result = One::one();
let mut base = self;
let mut exponent = exponent;
let two = One::<T>::one() + One::<T>::one();
loop {
if exponent % two != Zero::zero() {
let (new_result, _) = result.overflowing_mul(base);
result = new_result;
}
exponent = exponent / two;
if exponent == Zero::zero() {
break result;
}
let (new_base, _) = base.overflowing_mul(base);
base = new_base;
}
}
}
// === BitShift ===
pub trait Bitshift<T> {
/// Shift a number left by a given number of bits.
///
/// # Arguments
///
/// * `self` - The number to shift
/// * `shift` - The number of bits to shift by
///
/// # Returns
///
/// The result of shifting `self` left by `shift` bits
///
/// # Panics
///
/// Panics if the shift is greater than 255.
/// Panics if the result overflows the type T.
fn shl(self: T, shift: T) -> T;
/// Shift a number right by a given number of bits.
///
/// # Arguments
///
/// * `self` - The number to shift
/// * `shift` - The number of bits to shift by
///
/// # Returns
///
/// The result of shifting `self` right by `shift` bits
///
/// # Panics
///
/// Panics if the shift is greater than 255.
fn shr(self: T, shift: T) -> T;
}
impl BitshiftImpl<
T,
+Zero<T>,
+One<T>,
+Add<T>,
+Sub<T>,
+Div<T>,
+Mul<T>,
+Exponentiation<T>,
+Copy<T>,
+Drop<T>,
+PartialOrd<T>,
+BitSize<T>,
+TryInto<usize, T>,
> of Bitshift<T> {
fn shl(self: T, shift: T) -> T {
// if we shift by more than nb_bits of T, the result is 0
// we early return to save gas and prevent unexpected behavior
if shift > BitSize::<T>::bits().try_into().unwrap() - One::one() {
panic_with_felt252('mul Overflow');
}
let two = One::one() + One::one();
self * two.pow(shift)
}
fn shr(self: T, shift: T) -> T {
// early return to save gas if shift > nb_bits of T
if shift > BitSize::<T>::bits().try_into().unwrap() - One::one() {
panic_with_felt252('mul Overflow');
}
let two = One::one() + One::one();
self / two.pow(shift)
}
}
pub trait WrappingBitshift<T> {
/// Shift a number left by a given number of bits.
/// If the shift is greater than 255, the result is 0.
/// The bits moved after the 256th one are discarded, the new bits are set to 0.
///
/// # Arguments
///
/// * `self` - The number to shift
/// * `shift` - The number of bits to shift by
///
/// # Returns
///
/// The result of shifting `self` left by `shift` bits, wrapped if necessary
fn wrapping_shl(self: T, shift: T) -> T;
/// Shift a number right by a given number of bits.
/// If the shift is greater than 255, the result is 0.
///
/// # Arguments
///
/// * `self` - The number to shift
/// * `shift` - The number of bits to shift by
///
/// # Returns
///
/// The result of shifting `self` right by `shift` bits, or 0 if shift > 255
fn wrapping_shr(self: T, shift: T) -> T;
}
pub impl WrappingBitshiftImpl<
T,
+Zero<T>,
+One<T>,
+Add<T>,
+Sub<T>,
+Div<T>,
+Exponentiation<T>,
+PartialOrd<T>,
+Drop<T>,
+Copy<T>,
+OverflowingMul<T>,
+WrappingExponentiation<T>,
+BitSize<T>,
+TryInto<usize, T>,
> of WrappingBitshift<T> {
fn wrapping_shl(self: T, shift: T) -> T {
let two = One::<T>::one() + One::<T>::one();
let (result, _) = self.overflowing_mul(two.wrapping_pow(shift));
result
}
fn wrapping_shr(self: T, shift: T) -> T {
let two = One::<T>::one() + One::<T>::one();
if shift > BitSize::<T>::bits().try_into().unwrap() - One::one() {
return Zero::zero();
}
self / two.pow(shift)
}
}
// === Standalone functions ===
/// Adds two 256-bit unsigned integers, returning a 512-bit unsigned integer result.
///
/// limb3 will always be 0, because the maximum sum of two 256-bit numbers is at most
/// 2**257 - 2 which fits in 257 bits.
///
/// # Arguments
///
/// * `a` - First 256-bit unsigned integer
/// * `b` - Second 256-bit unsigned integer
///
/// # Returns
///
/// A 512-bit unsigned integer representing the sum of `a` and `b`
pub fn u256_wide_add(a: u256, b: u256) -> u512 {
let (sum, overflow) = a.overflowing_add(b);
let limb0 = sum.low;
let limb1 = sum.high;
let limb2 = if overflow {
1
} else {
0
};
let limb3 = 0;
u512 { limb0, limb1, limb2, limb3 }
}
#[cfg(test)]
mod tests {
use core::integer::{u512};
use core::num::traits::{OverflowingMul, WrappingMul, SaturatingAdd, Bounded};
use crate::math::{
Exponentiation, WrappingExponentiation, u256_wide_add, Bitshift, WrappingBitshift,
};
use super::OverflowingAdd;
#[test]
fn test_wrapping_pow() {
assert(5_u256.wrapping_pow(10) == 9765625, '5^10 should be 9765625');
assert(
5_u256
.wrapping_pow(
90
) == 807793566946316088741610050849573099185363389551639556884765625,
'5^90 failed'
);
assert(2_u256.wrapping_pow(256) == 0, 'should wrap to 0');
assert(123456_u256.wrapping_pow(0) == 1, 'n^0 should be 1');
assert(0_u256.wrapping_pow(123456) == 0, '0^n should be 0');
}
#[test]
fn test_pow() {
assert(5_u256.pow(10) == 9765625, '5^10 should be 9765625');
assert(5_u256.pow(45) == 28421709430404007434844970703125, '5^45 failed');
assert(123456_u256.pow(0) == 1, 'n^0 should be 1');
assert(0_u256.pow(123456) == 0, '0^n should be 0');
}
#[test]
fn test_wrapping_fast_pow() {
let exp = 3_u256.wrapping_fpow(10);
assert(
3_u256
.wrapping_fpow(
exp
) == 6701808933569337837891967767170127839253608180143676463326689955522159283811,
'3^(3^10) failed'
);
}
#[test]
fn test_wrapping_fast_pow_0() {
assert(3_u256.wrapping_fpow(0) == 1, '3^(0) should be 1');
}
#[test]
fn test_wrapping_fast_base_0() {
assert(0_u256.wrapping_fpow(42) == 0, '0^(42) should be 0');
}
#[test]
fn test_wrapping_fast_base_0_pow_0() {
assert(0_u256.wrapping_fpow(0) == 1, '0^(0) should be 1');
}
#[test]
#[should_panic(expected: ('u256_mul Overflow',))]
fn test_pow_should_overflow() {
2_u256.pow(256);
}
#[test]
fn test_wide_add_basic() {
let a = 1000;
let b = 500;
let (_, overflow) = a.overflowing_add(b);
let expected = u512 { limb0: 1500, limb1: 0, limb2: 0, limb3: 0, };
let result = u256_wide_add(a, b);
assert(!overflow, 'shouldnt overflow');
assert(result == expected, 'wrong result');
}
#[test]
fn test_wide_add_overflow() {
let a = Bounded::<u256>::MAX;
let b = 1;
let (_, overflow) = a.overflowing_add(b);
let expected = u512 { limb0: 0, limb1: 0, limb2: 1, limb3: 0, };
let result = u256_wide_add(a, b);
assert(overflow, 'should overflow');
assert(result == expected, 'wrong result');
}
#[test]
fn test_wide_add_max_values() {
let a = Bounded::<u256>::MAX;
let b = Bounded::<u256>::MAX;
let expected = u512 {
limb0: 0xfffffffffffffffffffffffffffffffe,
limb1: 0xffffffffffffffffffffffffffffffff,
limb2: 1,
limb3: 0,
};
let result = u256_wide_add(a, b);
assert(result == expected, 'wrong result');
}
#[test]
fn test_shl() {
// Given
let a = 0x00000091b2efa2bfd58aee61f24201bac4e64f70ca2b9d9491e82a498f2aab3f_u256;
// 1-byte shift is an 8-bit shift
let shift = 3 * 8;
// When
let result = a.shl(shift);
// Then
let expected = 0x91b2efa2bfd58aee61f24201bac4e64f70ca2b9d9491e82a498f2aab3f000000_u256;
assert(result == expected, 'wrong result');
}
#[test]
#[should_panic(expected: ('mul Overflow',))]
fn test_shl_256_bits_overflow() {
// Given
let a = 0x00000091b2efa2bfd58aee61f24201bac4e64f70ca2b9d9491e82a498faab3fe_u256;
// 1-byte shift is an 8-bit shift
let shift = 32 * 8;
// When & Then 2.pow(256) overflows u256
a.shl(shift);
}
#[test]
#[should_panic(expected: ('u256_mul Overflow',))]
fn test_shl_overflow() {
// Given
let a = 0x00000091b2efa2bfd58aee61f24201bac4e64f70ca2b9d9491e82a498faab3fe_u256;
// 1-byte shift is an 8-bit shift
let shift = 4 * 8;
// When & Then a << 32 overflows u256
a.shl(shift);
}
#[test]
fn test_wrapping_shl_overflow() {
// Given
let a = 0x00000091b2efa2bfd58aee61f24201bac4e64f70ca2b9d9491e82a498faab3fe_u256;
// 1-byte shift is an 8-bit shift
let shift = 12 * 8;
// When
let result = a.wrapping_shl(shift);
// Then
// The bits moved after the 256th one are discarded, the new bits are set to 0.
let expected = 0xf24201bac4e64f70ca2b9d9491e82a498faab3fe000000000000000000000000_u256;
assert(result == expected, 'wrong result');
}
#[test]
fn test_wrapping_shl() {
// Given
let a = 0x00000091b2efa2bfd58aee61f24201bac4e64f70ca2b9d9491e82a498f2aab3f_u256;
// 1-byte shift is an 8-bit shift
let shift = 3 * 8;
// When
let result = a.wrapping_shl(shift);
// Then
let expected = 0x91b2efa2bfd58aee61f24201bac4e64f70ca2b9d9491e82a498f2aab3f000000_u256;
assert(result == expected, 'wrong result');
}
#[test]
fn test_shr() {
// Given
let a = 0x0091b2efa2bfd58aee61f24201bac4e64f70ca2b9d9491e82a498f2aade6263a_u256;
// 1-byte shift is an 8-bit shift
let shift = 1 * 8;
// When
let result = a.shr(shift);
// Then
let expected = 0x000091b2efa2bfd58aee61f24201bac4e64f70ca2b9d9491e82a498f2aade626_u256;
assert(result == expected, 'wrong result');
}
#[test]
#[should_panic(expected: ('mul Overflow',))]
fn test_shr_256_bits_overflow() {
let a = 0xab91b2efa2bfd58aee61f24201bac4e64f70ca2b9d9491e82a498f2aade6263a_u256;
let shift = 32 * 8;
// When & Then 2.pow(256) overflows u256
a.shr(shift);
}
#[test]
fn test_wrapping_shr() {
// Given
let a = 0x0091b2efa2bfd58aee61f24201bac4e64f70ca2b9d9491e82a498f2aade6263a_u256;
// 1-byte shift is an 8-bit shift
let shift = 2 * 8;
// When
let result = a.wrapping_shr(shift);
// Then
let expected = 0x00000091b2efa2bfd58aee61f24201bac4e64f70ca2b9d9491e82a498f2aade6_u256;
assert(result == expected, 'wrong result');
}
#[test]
fn test_wrapping_shr_to_zero() {
// Given
let a = 0xab91b2efa2bfd58aee61f24201bac4e64f70ca2b9d9491e82a498f2aade6263a_u256;
// 1-byte shift is an 8-bit shift
let shift = 32 * 8;
// When
let result = a.wrapping_shr(shift);
// Then
let expected = 0_u256;
assert(result == expected, 'wrong result');
}
#[test]
fn test_u8_overflowing_mul_not_overflow_case() {
let result = 5_u8.overflowing_mul(10);
assert_eq!(result, (50, false));
}
#[test]
fn test_u8_overflowing_mul_overflow_case() {
let result = Bounded::<u8>::MAX.overflowing_mul(Bounded::MAX);
assert_eq!(result, (1, true));
}
#[test]
fn test_u8_wrapping_mul_not_overflow_case() {
let result = 5_u8.wrapping_mul(10);
assert_eq!(result, 50);
}
#[test]
fn test_u8_wrapping_mul_overflow_case() {
let result = Bounded::<u8>::MAX.wrapping_mul(Bounded::MAX);
assert_eq!(result, 1);
}
#[test]
fn test_u32_overflowing_mul_not_overflow_case() {
let result = 5_u32.overflowing_mul(10);
assert_eq!(result, (50, false));
}
#[test]
fn test_u32_overflowing_mul_overflow_case() {
let result = Bounded::<u32>::MAX.overflowing_mul(Bounded::MAX);
assert_eq!(result, (1, true));
}
#[test]
fn test_u32_wrapping_mul_not_overflow_case() {
let result = 5_u32.wrapping_mul(10);
assert_eq!(result, 50);
}
#[test]
fn test_u32_wrapping_mul_overflow_case() {
let result = Bounded::<u32>::MAX.wrapping_mul(Bounded::MAX);
assert_eq!(result, 1);
}
#[test]
fn test_u64_overflowing_mul_not_overflow_case() {
let result = 5_u64.overflowing_mul(10);
assert_eq!(result, (50, false));
}
#[test]
fn test_u64_overflowing_mul_overflow_case() {
let result = Bounded::<u64>::MAX.overflowing_mul(Bounded::MAX);
assert_eq!(result, (1, true));
}
#[test]
fn test_u64_wrapping_mul_not_overflow_case() {
let result = 5_u64.wrapping_mul(10);
assert_eq!(result, 50);
}
#[test]
fn test_u64_wrapping_mul_overflow_case() {
let result = Bounded::<u64>::MAX.wrapping_mul(Bounded::MAX);
assert_eq!(result, 1);
}
#[test]
fn test_u128_overflowing_mul_not_overflow_case() {
let result = 5_u128.overflowing_mul(10);
assert_eq!(result, (50, false));
}
#[test]
fn test_u128_overflowing_mul_overflow_case() {
let result = Bounded::<u128>::MAX.overflowing_mul(Bounded::MAX);
assert_eq!(result, (1, true));
}
#[test]
fn test_u128_wrapping_mul_not_overflow_case() {
let result = 5_u128.wrapping_mul(10);
assert_eq!(result, 50);
}
#[test]
fn test_u128_wrapping_mul_overflow_case() {
let result = Bounded::<u128>::MAX.wrapping_mul(Bounded::MAX);
assert_eq!(result, 1);
}
#[test]
fn test_u256_overflowing_mul_not_overflow_case() {
let result = 5_u256.overflowing_mul(10);
assert_eq!(result, (50, false));
}
#[test]
fn test_u256_overflowing_mul_overflow_case() {
let result = Bounded::<u256>::MAX.overflowing_mul(Bounded::MAX);
assert_eq!(result, (1, true));
}
#[test]
fn test_u256_wrapping_mul_not_overflow_case() {
let result = 5_u256.wrapping_mul(10);
assert_eq!(result, 50);
}
#[test]
fn test_u256_wrapping_mul_overflow_case() {
let result = Bounded::<u256>::MAX.wrapping_mul(Bounded::MAX);
assert_eq!(result, 1);
}
#[test]
fn test_saturating_add() {
let max = Bounded::<u8>::MAX;
assert_eq!(max.saturating_add(1), Bounded::<u8>::MAX);
assert_eq!((max - 2).saturating_add(1), max - 1);
}
}