-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
d0f2574
commit 1cc60e7
Showing
6 changed files
with
457 additions
and
56 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
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,91 @@ | ||
//! # Fast power algorithm | ||
|
||
/// Calculate the base ^ power | ||
/// using the fast powering algorithm | ||
/// # Arguments | ||
/// * ` base ` - The base of the exponentiation | ||
/// * ` power ` - The power of the exponentiation | ||
/// # Returns | ||
/// * ` T ` - The result of base ^ power | ||
/// # Panics | ||
/// * ` base ` is 0 | ||
#[feature("deprecated-op-assign-traits")] | ||
pub fn fast_power< | ||
T, | ||
+Div<T>, | ||
+Rem<T>, | ||
+Into<u8, T>, | ||
+Into<T, u256>, | ||
+TryInto<u256, T>, | ||
+PartialEq<T>, | ||
+Copy<T>, | ||
+Drop<T> | ||
>( | ||
base: T, mut power: T | ||
) -> T { | ||
assert!(base != 0_u8.into(), "fast_power: invalid input"); | ||
|
||
let mut base: u256 = base.into(); | ||
let mut result: u256 = 1; | ||
|
||
loop { | ||
if power % 2_u8.into() != 0_u8.into() { | ||
result *= base; | ||
} | ||
power = power / 2_u8.into(); | ||
if (power == 0_u8.into()) { | ||
break; | ||
} | ||
base *= base; | ||
}; | ||
|
||
result.try_into().expect('too large to fit output type') | ||
} | ||
|
||
/// Calculate the ( base ^ power ) mod modulus | ||
/// using the fast powering algorithm | ||
/// # Arguments | ||
/// * ` base ` - The base of the exponentiation | ||
/// * ` power ` - The power of the exponentiation | ||
/// * ` modulus ` - The modulus used in the calculation | ||
/// # Returns | ||
/// * ` T ` - The result of ( base ^ power ) mod modulus | ||
/// # Panics | ||
/// * ` base ` is 0 | ||
#[feature("deprecated-op-assign-traits")] | ||
pub fn fast_power_mod< | ||
T, | ||
+Div<T>, | ||
+Rem<T>, | ||
+Into<u8, T>, | ||
+Into<T, u256>, | ||
+TryInto<u256, T>, | ||
+PartialEq<T>, | ||
+Copy<T>, | ||
+Drop<T> | ||
>( | ||
base: T, mut power: T, modulus: T | ||
) -> T { | ||
assert!(base != 0_u8.into(), "fast_power: invalid input"); | ||
|
||
if modulus == 1_u8.into() { | ||
return 0_u8.into(); | ||
} | ||
|
||
let mut base: u256 = base.into(); | ||
let modulus: u256 = modulus.into(); | ||
let mut result: u256 = 1; | ||
|
||
loop { | ||
if power % 2_u8.into() != 0_u8.into() { | ||
result = (result * base) % modulus; | ||
} | ||
power = power / 2_u8.into(); | ||
if (power == 0_u8.into()) { | ||
break; | ||
} | ||
base = (base * base) % modulus; | ||
}; | ||
|
||
result.try_into().expect('too large to fit output type') | ||
} |
Oops, something went wrong.