This repository has been archived by the owner on Jan 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,59 @@ | ||
from starkware.cairo.common.uint256 import Uint256, uint256_eq, uint256_le, uint256_sub, uint256_mul | ||
from starkware.cairo.common.uint256 import Uint256, uint256_eq, uint256_le, uint256_sub, uint256_mul, uint256_unsigned_div_rem, uint256_and | ||
from starkware.cairo.common.bool import FALSE | ||
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin | ||
|
||
// @notice Internal exponentiation of two 256-bit integers. | ||
// @dev The result is modulo 2^256. | ||
// @param a The base. | ||
// @param b The exponent. | ||
// @param value - The base. | ||
// @param exponent - The exponent. | ||
// @return The result of the exponentiation. | ||
func uint256_exp{range_check_ptr}(a: Uint256, b: Uint256) -> Uint256 { | ||
let one_uint = Uint256(1, 0); | ||
let zero_uint = Uint256(0, 0); | ||
func uint256_exp{range_check_ptr}(value: Uint256, exponent: Uint256) -> Uint256 { | ||
let one = Uint256(1, 0); | ||
let zero = Uint256(0, 0); | ||
|
||
let (is_b_zero) = uint256_eq(b, zero_uint); | ||
if (is_b_zero != FALSE) { | ||
return one_uint; | ||
let (exponent_is_zero) = uint256_eq(exponent, zero); | ||
if (exponent_is_zero != FALSE) { | ||
return one; | ||
} | ||
let (b_minus_one) = uint256_sub(b, one_uint); | ||
let pow = uint256_exp(a, b_minus_one); | ||
let (res, _) = uint256_mul(a, pow); | ||
let (exponent_minus_one) = uint256_sub(exponent, one); | ||
let pow = uint256_exp(value, exponent_minus_one); | ||
let (res, _) = uint256_mul(value, pow); | ||
return res; | ||
} | ||
|
||
// @notice Internal fast exponentiation of two 256-bit integers. | ||
// @dev The result is modulo 2^256. | ||
// @param value - The base. | ||
// @param exponent - The exponent. | ||
// @return The result of the exponentiation. | ||
func uint256_fast_exp{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}(value: Uint256, exponent: Uint256) -> Uint256 { | ||
alloc_locals; | ||
|
||
let one = Uint256(1, 0); | ||
let zero = Uint256(0, 0); | ||
|
||
let (exponent_is_zero) = uint256_eq(exponent, zero); | ||
if (exponent_is_zero != FALSE) { | ||
return one; | ||
} | ||
|
||
let (exponent_is_one) = uint256_eq(exponent, one); | ||
if (exponent_is_one != FALSE) { | ||
return value; | ||
} | ||
|
||
let (exponent_is_odd) = uint256_and(exponent, one); | ||
if (exponent_is_odd.low != FALSE) { | ||
let (exponent_minus_one) = uint256_sub(exponent, one); | ||
let (half_exponent, _) = uint256_unsigned_div_rem(exponent_minus_one, Uint256(2, 0)); | ||
let pow = uint256_fast_exp(value, half_exponent); | ||
let (res, _) = uint256_mul(pow, pow); | ||
let (res, _) = uint256_mul(res, value); | ||
return res; | ||
} else { | ||
let (half_exponent, _) = uint256_unsigned_div_rem(exponent, Uint256(2, 0)); | ||
let pow = uint256_fast_exp(value, half_exponent); | ||
let (res, _) = uint256_mul(pow, pow); | ||
return res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters