Skip to content

Commit

Permalink
perf: add parallel Merkle tree generation feature (#710)
Browse files Browse the repository at this point in the history
For now only leaves hashing is parallel. Inner nodes are left for future
work as they require bigger changes to make the process iterative rather
than recursive.
  • Loading branch information
Oppen authored Dec 5, 2023
1 parent 3724c1c commit f3622cc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 2 additions & 0 deletions crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ sha3 = "0.10"
sha2 = "0.10"
thiserror = "1.0.38"
serde = { version = "1.0", features = ["derive"] }
rayon = { version = "1.8.0", optional = true }

[dev-dependencies]
criterion = "0.4"
Expand All @@ -21,6 +22,7 @@ rand = "0.8.5"

[features]
test_fiat_shamir = []
parallel = ["dep:rayon"]

[[bench]]
name = "criterion_merkle"
Expand Down
13 changes: 9 additions & 4 deletions crypto/src/merkle_tree/traits.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "parallel")]
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};

/// A backend for Merkle trees. This defines raw `Data` from which the Merkle
/// tree is built from. It also defines the `Node` type and the hash function
/// used to build parent nodes from children nodes.
Expand All @@ -11,10 +14,12 @@ pub trait IsMerkleTreeBackend: Default {
/// This function takes the list of data from which the Merkle
/// tree will be built from and converts it to a list of leaf nodes.
fn hash_leaves(unhashed_leaves: &[Self::Data]) -> Vec<Self::Node> {
unhashed_leaves
.iter()
.map(|leaf| Self::hash_data(leaf))
.collect()
#[cfg(feature = "parallel")]
let iter = unhashed_leaves.par_iter();
#[cfg(not(feature = "parallel"))]
let iter = unhashed_leaves.iter();

iter.map(|leaf| Self::hash_data(leaf)).collect()
}

/// This function takes to children nodes and builds a new parent node.
Expand Down
2 changes: 1 addition & 1 deletion provers/stark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ wasm-bindgen-test = "0.3.0"
test_fiat_shamir = []
instruments = [] # This enables timing prints in prover and verifier
metal = ["lambdaworks-math/metal"]
parallel = ["dep:rayon"]
parallel = ["dep:rayon", "lambdaworks-crypto/parallel"]
wasm = ["dep:wasm-bindgen", "dep:serde-wasm-bindgen", "dep:web-sys"]

[target.'cfg(not(all(target_arch = "wasm32", target_os = "unknown")))'.dev-dependencies]
Expand Down

0 comments on commit f3622cc

Please sign in to comment.