Skip to content

Commit

Permalink
Peder optimization #1 (#848)
Browse files Browse the repository at this point in the history
* Pedersen

* Add random in bench

* Fix seed

* Opt

* Fix bench

* Apply format

* Change version in winerfell adapter

* Fix clippy

---------

Co-authored-by: Mariano Nicolini <[email protected]>
  • Loading branch information
MauroToscano and entropidelic authored Mar 26, 2024
1 parent 019cfcf commit ac71258
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
5 changes: 5 additions & 0 deletions crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ rayon = { version = "1.8.0", optional = true }
criterion = "0.4"
iai-callgrind.workspace = true
rand = "0.8.5"
rand_chacha = "0.3.1"

[features]
default = ["std"]
Expand All @@ -39,3 +40,7 @@ harness = false
[[bench]]
name= "criterion_poseidon"
harness=false

[[bench]]
name= "criterion_pedersen"
harness=false
27 changes: 27 additions & 0 deletions crypto/benches/criterion_pedersen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use lambdaworks_crypto::hash::pedersen::Pedersen;
use lambdaworks_math::field::element::FieldElement;
use lambdaworks_math::field::fields::fft_friendly::stark_252_prime_field::Stark252PrimeField;
use lambdaworks_math::traits::ByteConversion;
use rand::{RngCore, SeedableRng};
use rand_chacha::ChaCha8Rng;

fn pedersen_benchmarks(c: &mut Criterion) {
let mut rng = ChaCha8Rng::seed_from_u64(2);
let mut felt1: [u8; 32] = Default::default();
rng.fill_bytes(&mut felt1);
let mut felt2: [u8; 32] = Default::default();
rng.fill_bytes(&mut felt2);

let x = FieldElement::<Stark252PrimeField>::from_bytes_be(&felt1).unwrap();
let y = FieldElement::<Stark252PrimeField>::from_bytes_be(&felt2).unwrap();
let mut group = c.benchmark_group("Pedersen Benchmark");
let pedersen = black_box(Pedersen::default());

// Benchmark with black_box is 0.41% faster
group.bench_function("Hashing with black_box", |bench| {
bench.iter(|| black_box(pedersen.hash(&x, &y)))
});
}
criterion_group!(pedersen, pedersen_benchmarks);
criterion_main!(pedersen);
3 changes: 1 addition & 2 deletions crypto/src/hash/pedersen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use lambdaworks_math::{
cyclic_group::IsGroup,
elliptic_curve::short_weierstrass::{
curves::stark_curve::StarkCurve, point::ShortWeierstrassProjectivePoint,
},
Expand Down Expand Up @@ -69,7 +68,7 @@ impl Pedersen {
let offset = bools_to_usize_le(v);
if offset > 0 {
// Table lookup at 'offset-1' in table for chunk 'i'
*acc = acc.operate_with(&prep[i * self.params.table_size + offset - 1]);
*acc = acc.operate_with_affine(&prep[i * self.params.table_size + offset - 1]);
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion examples/prove-miden/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ path = "src/main.rs"
lambdaworks-crypto = { workspace = true }
lambdaworks-math = { workspace = true, features = ["lambdaworks-serde-string"] }
lambdaworks-winterfell-adapter = { workspace = true }
stark-platinum-prover = { git = "https://github.com/lambdaclass/lambdaworks" , rev = "3da725de1e6f76c04ddbb3ccb67e6038a7663134", features = ["winter_compatibility"] }
stark-platinum-prover = { git = "https://github.com/lambdaclass/lambdaworks" , branch = "miden-version", features = ["winter_compatibility"] }

serde = { version = "1.0" }
serde_json = "1"
Expand Down
4 changes: 0 additions & 4 deletions provers/cairo/src/wasm_wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ use lambdaworks_math::field::fields::fft_friendly::stark_252_prime_field::Stark2
use serde::{Deserialize, Serialize};
use stark_platinum_prover::proof::options::ProofOptions;
use stark_platinum_prover::proof::options::SecurityLevel;
use stark_platinum_prover::proof::stark::StarkProof;
use stark_platinum_prover::transcript::StoneProverTranscript;
use stark_platinum_prover::verifier::{IsStarkVerifier, Verifier};
use std::collections::HashMap;
use wasm_bindgen::prelude::wasm_bindgen;

#[wasm_bindgen]
pub struct Stark252PrimeFieldProof(StarkProof<Stark252PrimeField, Stark252PrimeField>);

#[wasm_bindgen]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq, Hash)]
pub struct FE(FieldElement<Stark252PrimeField>);
Expand Down
4 changes: 2 additions & 2 deletions provers/winterfell_adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ edition.workspace = true
license.workspace = true

[dependencies]
lambdaworks-math = { git = "https://github.com/lambdaclass/lambdaworks", rev = "3da725de1e6f76c04ddbb3ccb67e6038a7663134", features = ["winter_compatibility"] }
stark-platinum-prover = { git = "https://github.com/lambdaclass/lambdaworks" , rev = "3da725de1e6f76c04ddbb3ccb67e6038a7663134", features = ["winter_compatibility"] }
lambdaworks-math = { git = "https://github.com/lambdaclass/lambdaworks", branch = "miden-version", features = ["winter_compatibility"] }
stark-platinum-prover = { git = "https://github.com/lambdaclass/lambdaworks" , branch = "miden-version", features = ["winter_compatibility"] }
rand = "0.8.5"
winter-air = { package = "winter-air", version = "0.6.4", default-features = false }
winter-prover = { package = "winter-prover", version = "0.6.4", default-features = false }
Expand Down

0 comments on commit ac71258

Please sign in to comment.