Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of Parallel Merkle Tree #125

Merged
merged 24 commits into from
Dec 29, 2023
Merged

Conversation

intx4
Copy link
Contributor

@intx4 intx4 commented Dec 21, 2023

Description

This PR implements a Parallel Version of the Merkle Tree, completely transparent to the user, i.e. it defines two new versions of MerkleTree::new(...) and MerkleTree::new_with_leaf_digest(...) which are compiled (in place of the legacy ones) when the "parallel" feature is enabled. The implementation can be found in src/merkle_tree/mod.rs. We have also included some benchmarks in benches/merkle_tree.rs.
Using our implementation, for a Merkle Tree with 2^20 leaves, using SHA256, we obtain a speedup of 80% over legacy on a M1 MacBook.

closes: #122


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Targeted PR against correct branch (main)
  • Linked to Github issue with discussion and accepted design OR have an explanation in the PR that describes this work.
  • Wrote unit tests
  • Updated relevant documentation in the code
  • Added a relevant changelog entry to the Pending section in CHANGELOG.md
  • Re-reviewed Files changed in the Github PR explorer

@intx4 intx4 requested a review from a team as a code owner December 21, 2023 21:11
@intx4 intx4 requested review from z-tech, Pratyush and weikengchen and removed request for a team December 21, 2023 21:11
@WizardOfMenlo
Copy link
Contributor

There is currently a change of semantics between the parallel and non-parallel version. Namely, if one of the hash functions (the leaf hash or the two to one hash) return Err, the serial version would return it correctly to the caller, while the parallel version would panic. This can be fixed easily enough as long as the error type bound is changed to dyn Error + Send.

  1. Is it acceptable to change API to add the Send bound for the parallel bound? Or even, can we have ark_std::Error to require Send by default?
  2. When is it desirable for the LeafHash, TwoToOneHash to be allowed to fail? In most use-cases I can think of the hashing should always succeed. I would be in favour to refactor those traits to remove the error path. The conversion traits I can see more usecases, but also wouldn't mind changing the API there as well.

@mmagician @Pratyush what are your thoughts?

src/merkle_tree/mod.rs Outdated Show resolved Hide resolved
src/merkle_tree/tests/mod.rs Outdated Show resolved Hide resolved
src/merkle_tree/mod.rs Outdated Show resolved Hide resolved
src/merkle_tree/mod.rs Outdated Show resolved Hide resolved
@mmagician
Copy link
Member

Rather than duplicating the entire method for the parallel feature, I would instead try to re-use as much code as possible and only but blocks of code under #[cfg(feature = "parallel")] and #[cfg(not(feature = "parallel"))]

@intx4
Copy link
Contributor Author

intx4 commented Dec 23, 2023

@mmagician thanks for your feedback, I will address them and get back to you

@intx4
Copy link
Contributor Author

intx4 commented Dec 28, 2023

@mmagician thanks for your feedback, I will address them and get back to you

@mmagician I should have addressed your comments, feel free to get back with feedback :)

benches/merkle_tree.rs Outdated Show resolved Hide resolved
@Pratyush
Copy link
Member

I think it's fine to change the crate::Error from Box<dyn ark::std::Error> to Box<dyn ark::std::Error + Send>. That should solve the issues around unwrapping.

@Pratyush Pratyush merged commit 8bbb482 into arkworks-rs:main Dec 29, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Parallelize the Merkle tree implementation
4 participants