Skip to content

Commit

Permalink
Use Euclid to compute the inverse faster
Browse files Browse the repository at this point in the history
  • Loading branch information
Oppen committed Jan 9, 2023
1 parent 3d1e376 commit 30c5148
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions felt/src/bigint_felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,27 +423,39 @@ impl<'a> Pow<u32> for &'a FeltBigInt {
impl Div for FeltBigInt {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
let mut x = rhs.0.modpow(&(&*CAIRO_PRIME - 2usize), &CAIRO_PRIME);
x *= self.0;
FeltBigInt::from(x)
let x = rhs
.0
.to_bigint()
.unwrap()
.extended_gcd(&CAIRO_SIGNED_PRIME)
.x;
self * &FeltBigInt::from(x)
}
}

impl<'a> Div for &'a FeltBigInt {
type Output = FeltBigInt;
fn div(self, rhs: Self) -> Self::Output {
let mut x = rhs.0.modpow(&(&*CAIRO_PRIME - 2usize), &CAIRO_PRIME);
x *= &self.0;
FeltBigInt::from(x)
let x = rhs
.0
.to_bigint()
.unwrap()
.extended_gcd(&CAIRO_SIGNED_PRIME)
.x;
self * &FeltBigInt::from(x)
}
}

impl<'a> Div<FeltBigInt> for &'a FeltBigInt {
type Output = FeltBigInt;
fn div(self, rhs: FeltBigInt) -> Self::Output {
let mut x = rhs.0.modpow(&(&*CAIRO_PRIME - 2usize), &CAIRO_PRIME);
x *= &self.0;
FeltBigInt::from(x)
let x = rhs
.0
.to_bigint()
.unwrap()
.extended_gcd(&CAIRO_SIGNED_PRIME)
.x;
self * &FeltBigInt::from(x)
}
}

Expand Down

1 comment on commit 30c5148

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.30.

Benchmark suite Current: 30c5148 Previous: 3d1e376 Ratio
cairo_run(cairo_programs/benchmarks/compare_arrays_200000.json 1053561733 ns/iter (± 20511856) 790609706 ns/iter (± 2436773) 1.33
cairo_run(cairo_programs/benchmarks/factorial_multirun.json 401065109 ns/iter (± 8808496) 294588909 ns/iter (± 772258) 1.36
cairo_run(cairo_programs/benchmarks/fibonacci_1000_multirun.json 187633197 ns/iter (± 6736698) 141312551 ns/iter (± 525024) 1.33
cairo_run(cairo_programs/benchmarks/integration_builtins.json 623929994 ns/iter (± 13024627) 449138679 ns/iter (± 2451924) 1.39
cairo_run(cairo_programs/benchmarks/linear_search.json 136338014 ns/iter (± 4599561) 98859398 ns/iter (± 523922) 1.38
cairo_run(cairo_programs/benchmarks/keccak_integration_benchmark.json 1863077591 ns/iter (± 25610949) 1357866537 ns/iter (± 10453250) 1.37
cairo_run(cairo_programs/benchmarks/secp_integration_benchmark.json 2152658549 ns/iter (± 32753788) 1464468690 ns/iter (± 10117973) 1.47
cairo_run(cairo_programs/benchmarks/blake2s_integration_benchmark.json 1739351025 ns/iter (± 23163737) 1211152532 ns/iter (± 4911100) 1.44
cairo_run(cairo_programs/benchmarks/dict_integration_benchmark.json 1222290734 ns/iter (± 20350959) 894118025 ns/iter (± 4994588) 1.37
cairo_run(cairo_programs/benchmarks/memory_integration_benchmark.json 682827207 ns/iter (± 12018974) 506275425 ns/iter (± 1367378) 1.35
cairo_run(cairo_programs/benchmarks/math_cmp_and_pow_integration_benchmark.json 25636512 ns/iter (± 1391881) 19117731 ns/iter (± 79026) 1.34
cairo_run(cairo_programs/benchmarks/operations_with_data_structures_benchmarks.json 2721487324 ns/iter (± 26398078) 1894194711 ns/iter (± 19359441) 1.44
cairo_run(cairo_programs/benchmarks/uint256_integration_benchmark.json 1823291795 ns/iter (± 28265963) 1252863890 ns/iter (± 4869886) 1.46

This comment was automatically generated by workflow using github-action-benchmark.

CC: @unbalancedparentheses

Please sign in to comment.