Skip to content

Commit

Permalink
refactor: remove alexandria_math
Browse files Browse the repository at this point in the history
  • Loading branch information
detectivekim committed Jun 21, 2024
1 parent d0f2574 commit 1cc60e7
Show file tree
Hide file tree
Showing 6 changed files with 457 additions and 56 deletions.
53 changes: 0 additions & 53 deletions Scarb.lock
Original file line number Diff line number Diff line change
@@ -1,63 +1,10 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "alexandria_bytes"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#3041887b95cf10f9d3cd8d75326c754b331f9573"
dependencies = [
"alexandria_data_structures",
"alexandria_math",
]

[[package]]
name = "alexandria_data_structures"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#3041887b95cf10f9d3cd8d75326c754b331f9573"
dependencies = [
"alexandria_encoding",
]

[[package]]
name = "alexandria_encoding"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#3041887b95cf10f9d3cd8d75326c754b331f9573"
dependencies = [
"alexandria_bytes",
"alexandria_math",
"alexandria_numeric",
]

[[package]]
name = "alexandria_math"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#3041887b95cf10f9d3cd8d75326c754b331f9573"
dependencies = [
"alexandria_data_structures",
]

[[package]]
name = "alexandria_numeric"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#3041887b95cf10f9d3cd8d75326c754b331f9573"
dependencies = [
"alexandria_math",
"alexandria_searching",
]

[[package]]
name = "alexandria_searching"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git#3041887b95cf10f9d3cd8d75326c754b331f9573"
dependencies = [
"alexandria_data_structures",
]

[[package]]
name = "clober_cairo"
version = "0.1.0"
dependencies = [
"alexandria_math",
"snforge_std",
]

Expand Down
1 change: 0 additions & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ repository = "https://github.com/clober-dex/clober_cairo"

[dependencies]
starknet = "2.6.3"
alexandria_math = { git = "https://github.com/keep-starknet-strange/alexandria.git" }

[dev-dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.25.0" }
Expand Down
91 changes: 91 additions & 0 deletions src/alexandria/fast_power.cairo
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')
}
Loading

0 comments on commit 1cc60e7

Please sign in to comment.