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

ref(merkle): rework API #48

Merged
merged 25 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
848be6a
ref(merkle): use macros to simplify test setup
alexfertel Apr 26, 2024
69fb6ba
feat(merkle): optimize sorting the pair to be hashed
alexfertel Apr 29, 2024
732e55c
feat(merkle): add inline hint to sorted_hash
alexfertel Apr 29, 2024
aa8f192
feat(merkle): simplify generic hashing API & remove alloy_primitives
alexfertel Apr 29, 2024
8fc8f1e
fix(merkle): update tests and imports
alexfertel Apr 29, 2024
8772146
Merge branch 'main' into rework-merkle-proofing-api
alexfertel Apr 29, 2024
15f7960
feat(merkle): add inline hint to build_hasher
alexfertel Apr 29, 2024
d3a0b36
ref(merkle): extract implementation to a separate module
alexfertel Apr 29, 2024
e212e89
lint(fmt): fix formatting
alexfertel Apr 29, 2024
955c3b6
lint: appease clippy
alexfertel Apr 29, 2024
0e9bae0
feat(merkle): add must_use hint to verify
alexfertel Apr 29, 2024
c2dacb9
feat(merkle): simplify Verifier API
alexfertel May 1, 2024
a3c918d
feat(merkle): convert panics to errors
alexfertel May 1, 2024
c3ee05e
fix(merkle): fix typos
alexfertel May 1, 2024
e30ce36
chore: ignore .DS_Store files
alexfertel May 1, 2024
ef9793e
feat(merkle): add example contract
alexfertel May 1, 2024
464d678
lint: appease clippy
alexfertel May 1, 2024
e1cc212
lint: appease clippy
alexfertel May 1, 2024
c5a9b7f
lint: appease clippy
alexfertel May 1, 2024
ab37411
build(merkle): replace const-hex with hex-literal
alexfertel May 1, 2024
f31692d
fix(merkle): polish docs around hashing
alexfertel May 1, 2024
fc7e84d
ref(merkle): move things around & add tests
alexfertel May 2, 2024
9e6eeb6
lint: appease clippy
alexfertel May 2, 2024
0ba04c8
lint: appease clippy
alexfertel May 2, 2024
abb404a
chore: ignore .DS_Store files in subdirectories
alexfertel May 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
**/node_modules/

docs/build/

**/.DS_Store
43 changes: 20 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"lib/grip",
"lib/grip-proc",
"examples/erc20",
"examples/merkle-proofs",
]
# Explicitly set the resolver to version 2, which is the default for packages
# with edition >= 2021.
Expand Down
26 changes: 26 additions & 0 deletions examples/merkle-proofs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "merkle-proofs-example"
edition.workspace = true
license.workspace = true
repository.workspace = true
publish = false
version = "0.0.0"

[dependencies]
crypto = { path = "../../lib/crypto", features = ["merkle", "multi-proof"] }
alloy-primitives.workspace = true
alloy-sol-types.workspace = true
stylus-sdk.workspace = true
stylus-proc.workspace = true
mini-alloc.workspace = true

[features]
default = []

# Enables using the standard library. This is not included in the default
# features, because this crate is meant to be used in a `no_std` environment.
# Currently, the std feature is only used for testing purposes.
std = []

[lib]
crate-type = ["lib", "cdylib"]
91 changes: 91 additions & 0 deletions examples/merkle-proofs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]
extern crate alloc;

use alloc::vec::Vec;

use alloy_primitives::B256;
use crypto::{
merkle::{self, Verifier},
KeccakBuilder,
};
use stylus_proc::SolidityError;
use stylus_sdk::{
alloy_sol_types::sol,
prelude::{entrypoint, external, sol_storage},
};

sol! {
error MerkleProofInvalidMultiProofLength();
error MerkleProofInvalidRootChild();
error MerkleProofInvalidTotalHashes();
}

#[global_allocator]
static ALLOC: mini_alloc::MiniAlloc = mini_alloc::MiniAlloc::INIT;

#[cfg(not(any(
test,
feature = "std",
target_arch = "wasm32-unknown-unknown"
)))]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

#[derive(SolidityError)]
pub enum VerifierError {
InvalidProofLength(MerkleProofInvalidMultiProofLength),
InvalidRootChild(MerkleProofInvalidRootChild),
InvalidTotalHashes(MerkleProofInvalidTotalHashes),
}

impl core::convert::From<merkle::MultiProofError> for VerifierError {
fn from(value: merkle::MultiProofError) -> Self {
match value {
merkle::MultiProofError::InvalidProofLength => {
VerifierError::InvalidProofLength(
MerkleProofInvalidMultiProofLength {},
)
}
merkle::MultiProofError::InvalidRootChild => {
VerifierError::InvalidRootChild(MerkleProofInvalidRootChild {})
}
merkle::MultiProofError::InvalidTotalHashes => {
VerifierError::InvalidTotalHashes(
MerkleProofInvalidTotalHashes {},
)
}
}
}
}

sol_storage! {
#[entrypoint]
struct VerifierContract { }
}

#[external]
impl VerifierContract {
pub fn verify(&self, proof: Vec<B256>, root: B256, leaf: B256) -> bool {
let proof: Vec<[u8; 32]> = proof.into_iter().map(|m| *m).collect();
Verifier::<KeccakBuilder>::verify(&proof, *root, *leaf)
}

pub fn verify_multi_proof(
&self,
proof: Vec<B256>,
proof_flags: Vec<bool>,
root: B256,
leaves: Vec<B256>,
) -> Result<bool, VerifierError> {
let proof: Vec<[u8; 32]> = proof.into_iter().map(|m| *m).collect();
let leaves: Vec<[u8; 32]> = leaves.into_iter().map(|m| *m).collect();
Ok(Verifier::<KeccakBuilder>::verify_multi_proof(
&proof,
&proof_flags,
*root,
&leaves,
)?)
}
}
12 changes: 8 additions & 4 deletions lib/crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ license.workspace = true
repository.workspace = true
version = "0.1.0"

[dependencies]
mini-alloc.workspace = true
tiny-keccak = { version = "2.0.2", features = ["keccak"] }

[dev-dependencies]
alloy-primitives = { version = "0.6.4", default-features = false }
const-hex = { version = "1.11.1", default-features = false }
hex-literal = "0.4.1"
rand = "0.8.5"

[features]
default = []
std = []

merkle = []
multi_proof = ["merkle"]
multi-proof = ["merkle"]
Loading
Loading