-
Notifications
You must be signed in to change notification settings - Fork 23
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
6 changed files
with
135 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ mod flip_endiannes; | |
mod from_span; | ||
mod horner_eval; | ||
mod to_array; | ||
mod math; | ||
|
||
#[cfg(test)] | ||
mod tests; |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
fn pow(base: felt252, exp: felt252) -> felt252 { | ||
if exp == 0 { | ||
return 1; // Return 1 for zero exponent | ||
} | ||
let mut exp: u256 = exp.into(); | ||
let mut res = 1; // Initialize result as 1 | ||
let mut curr_base = base; // Current base value | ||
|
||
loop { | ||
if exp == 0 { | ||
break; | ||
} else { | ||
if exp % 2 == 1 { | ||
res = res * curr_base; // Multiply result only when exp is odd | ||
} | ||
curr_base = curr_base * curr_base; // Square the base for next iteration | ||
exp = exp / 2; // Divide exponent by 2 | ||
} | ||
}; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ mod test_flip_endiannes; | |
mod test_from_span; | ||
mod test_horner_eval; | ||
mod test_to_array; | ||
mod test_math; |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use cairo_verifier::common::math::pow; | ||
|
||
#[test] | ||
#[available_gas(9999999999)] | ||
fn test_pow_1() { | ||
let base = 1934568044210770965733097210694395167600009938751278224656090409051406060084; | ||
let exp = 69439516760000993875127; | ||
assert( | ||
pow( | ||
base, exp | ||
) == 2804690217475462062143361339624939640984649667966511418446363596075299761851, | ||
'Invalid value' | ||
); | ||
} | ||
|
||
#[test] | ||
#[available_gas(9999999999)] | ||
fn test_pow_2() { | ||
let base = 193456804421077096570009938751278224656090409051406060084; | ||
let exp = 193456804421077096570009938751278224656090409051406060084; | ||
assert( | ||
pow( | ||
base, exp | ||
) == 2672162222334975109199941471365701890765112108683608796920114577809390903720, | ||
'Invalid value' | ||
); | ||
} |
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