-
Notifications
You must be signed in to change notification settings - Fork 7
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
feat(smt): add smt
crate
#41
Merged
sripwoud
merged 11 commits into
privacy-scaling-explorations:main
from
waddaboo:feat/smt
Aug 6, 2024
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4e3e06c
feat(smt): add `smt` crate
waddaboo c73e90f
docs(smt): add README and examples
waddaboo 1893a8f
docs(smt): fix README links
waddaboo ba8f105
fix(smt): fix spelling error
waddaboo 7fb8dd5
docs(smt): fix format
waddaboo 027fd14
Merge branch 'feat/smt' of https://github.com/waddaboo/zk-kit.rust in…
waddaboo c97995c
docs(smt): fix format
waddaboo 701619c
refactor(smt): improve error handling
waddaboo f160840
refactor(smt): implement `FromStr` for `Node`
waddaboo f9b026b
docs(smt): update usage
waddaboo ee23021
docs(smt): update example
waddaboo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "zk-kit-smt" | ||
version.workspace = true | ||
edition.workspace = true | ||
license.workspace = true | ||
publish.workspace = true | ||
description = "Sparse Merkle Tree" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
num-bigint = "0.4.6" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Pinco | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<p align="center"> | ||
<h1 align="center"> | ||
Sparse Merkle tree | ||
</h1> | ||
<p align="center">Sparse Merkle tree implementation in Rust.</p> | ||
</p> | ||
|
||
<p align="center"> | ||
<a href="https://github.com/privacy-scaling-explorations/zk-kit"> | ||
<img src="https://img.shields.io/badge/project-zk--kit-blue.svg?style=flat-square"> | ||
</a> | ||
<a href="https://github.com/privacy-scaling-explorations/zk-kit.rust/tree/main/packages/smt/LICENSE"> | ||
<img alt="License" src="https://img.shields.io/crates/l/zk-kit-smt?style=flat-square"> | ||
</a> | ||
<a href="https://crates.io/crates/zk-kit-smt"> | ||
<img alt="Version" src="https://img.shields.io/crates/v/zk-kit-smt?style=flat-square" /> | ||
</a> | ||
<a href="https://crates.io/crates/zk-kit-smt"> | ||
<img alt="Downloads" src="https://img.shields.io/crates/d/zk-kit-smt?style=flat-square" /> | ||
</a> | ||
</p> | ||
|
||
<div align="center"> | ||
<h4> | ||
<a href="https://appliedzkp.org/discord"> | ||
🗣️ Chat & Support | ||
</a> | ||
</h4> | ||
</div> | ||
|
||
A sparse Merkle tree is a data structure useful for storing a key/value map where every leaf node of the tree contains the cryptographic hash of a key/value pair and every non leaf node contains the concatenated hashes of its child nodes. Sparse Merkle trees provides a secure and efficient verification of large data sets and they are often used in peer-to-peer technologies. This implementation is an optimized version of the traditional sparse Merkle tree and it is based on the concepts expressed in the papers and resources below. | ||
|
||
## References | ||
|
||
1. Rasmus Dahlberg, Tobias Pulls and Roel Peeters. _Efficient Sparse Merkle Trees: Caching Strategies and Secure (Non-)Membership Proofs_. Cryptology ePrint Archive: Report 2016/683, 2016. https://eprint.iacr.org/2016/683. | ||
2. Faraz Haider. _Compact sparse merkle trees_. Cryptology ePrint Archive: Report 2018/955, 2018. https://eprint.iacr.org/2018/955. | ||
3. Jordi Baylina and Marta Bellés. _Sparse Merkle Trees_. https://docs.iden3.io/publications/pdfs/Merkle-Tree.pdf. | ||
4. Vitalik Buterin Fichter. _Optimizing sparse Merkle trees_. https://ethresear.ch/t/optimizing-sparse-merkle-trees/3751. | ||
|
||
--- | ||
|
||
## 🛠 Install | ||
|
||
You can install `zk-kit-smt` crate with `cargo`: | ||
|
||
```bash | ||
cargo add zk-kit-smt | ||
``` | ||
|
||
## 📜 Usage | ||
```rust | ||
use zk_kit_smt::smt::{Key, Node, Value, SMT}; | ||
|
||
fn hash_function(nodes: Vec<Node>) -> Node { | ||
let strings: Vec<String> = nodes.iter().map(|node| node.to_string()).collect(); | ||
Node::Str(strings.join(",")) | ||
} | ||
|
||
fn main() { | ||
// Initialize the Sparse Merkle Tree with a hash function. | ||
let mut smt = SMT::new(hash_function, false); | ||
|
||
let key = Key::Str("abc".to_string()); | ||
let value = Value::Str("123".to_string()); | ||
|
||
// Add a key-value pair to the Sparse Merkle Tree. | ||
smt.add(key.clone(), value.clone()).unwrap(); | ||
|
||
// Get the value of the key. | ||
let get = smt.get(key.clone()); | ||
assert_eq!(get, Some(value)); | ||
|
||
// Update the value of the key. | ||
let new_value = Value::Str("456".to_string()); | ||
let update = smt.update(key.clone(), new_value.clone()); | ||
assert!(update.is_ok()); | ||
assert_eq!(smt.get(key.clone()), Some(new_value)); | ||
|
||
// Create and verify a proof for the key. | ||
let create_proof = smt.create_proof(key.clone()); | ||
let verify_proof = smt.verify_proof(create_proof); | ||
assert!(verify_proof); | ||
|
||
// Delete the key. | ||
let delete = smt.delete(key.clone()); | ||
assert!(delete.is_ok()); | ||
assert_eq!(smt.get(key.clone()), None); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use zk_kit_smt::smt::{Key, Node, Value, SMT}; | ||
|
||
fn hash_function(nodes: Vec<Node>) -> Node { | ||
let strings: Vec<String> = nodes.iter().map(|node| node.to_string()).collect(); | ||
Node::Str(strings.join(",")) | ||
} | ||
|
||
fn main() { | ||
// Initialize the Sparse Merkle Tree with a hash function. | ||
let mut smt = SMT::new(hash_function, false); | ||
|
||
let key = Key::Str("abc".to_string()); | ||
let value = Value::Str("123".to_string()); | ||
|
||
// Add a key-value pair to the Sparse Merkle Tree. | ||
smt.add(key.clone(), value.clone()).unwrap(); | ||
|
||
// Get the value of the key. | ||
let get = smt.get(key.clone()); | ||
assert_eq!(get, Some(value)); | ||
|
||
// Update the value of the key. | ||
let new_value = Value::Str("456".to_string()); | ||
let update = smt.update(key.clone(), new_value.clone()); | ||
assert!(update.is_ok()); | ||
assert_eq!(smt.get(key.clone()), Some(new_value)); | ||
|
||
// Create and verify a proof for the key. | ||
let create_proof = smt.create_proof(key.clone()); | ||
let verify_proof = smt.verify_proof(create_proof); | ||
assert!(verify_proof); | ||
|
||
// Delete the key. | ||
let delete = smt.delete(key.clone()); | ||
assert!(delete.is_ok()); | ||
assert_eq!(smt.get(key.clone()), None); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod smt; | ||
mod utils; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think this should be included in the published crates?
If not we need to add an
exclude
orinclude
field to the crate's manifestThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review!
I saw some other libraries included examples and some did not, so I'm not sure either. Since the README
Usage
section is the same as inexamples
, I think it's ok to remove it?