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

Introduce SmtProof #270

Merged
merged 37 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
0be4f67
add conversion for `SmtLeaf`
plafer Feb 5, 2024
260a374
introduce `SmtProof` scaffolding
plafer Feb 5, 2024
a919a13
implement `verify_membership()`
plafer Feb 5, 2024
79a9ceb
SmtLeaf: knows its index
plafer Feb 5, 2024
528f70d
`SmtLeaf::index`
plafer Feb 5, 2024
2744c0a
`SmtLeaf::get_value()` returns an Option
plafer Feb 5, 2024
d162e33
fix `verify_membership()`
plafer Feb 5, 2024
e99ad78
impl `SmtProof::get`
plafer Feb 5, 2024
0a6c8b5
impl `into_parts()`
plafer Feb 5, 2024
607ed74
`SmtProof::compute_root`
plafer Feb 5, 2024
6a07b3a
use `SmtProof` in `Smt::open`
plafer Feb 5, 2024
8e17e9d
`SmtLeaf` constructors
plafer Feb 5, 2024
1d3ed86
Vec
plafer Feb 5, 2024
35ec653
impl `Error` for `SmtLeafError`
plafer Feb 5, 2024
2e02f02
fix std Error
plafer Feb 5, 2024
709383f
move Word/Digest conversions to LeafIndex
plafer Feb 5, 2024
3da137a
`SmtProof::new()` returns an error
plafer Feb 5, 2024
4e1abc6
`SparseMerkleTree::path_and_leaf_to_opening`
plafer Feb 5, 2024
c959e12
`SmtLeaf`: serializable/deserializable
plafer Feb 5, 2024
144979d
`SmtProof`: serializable/deserializable
plafer Feb 5, 2024
39aad16
add tests for SmtLeaf serialization
plafer Feb 5, 2024
3b52394
move `SmtLeaf` to submodule
plafer Feb 5, 2024
a621a9f
use constructors internally
plafer Feb 5, 2024
0a5d7bc
fix docs
plafer Feb 5, 2024
e9f984b
Add `Vec`
plafer Feb 5, 2024
2f162df
add `Vec` to tests
plafer Feb 5, 2024
a6d9e23
no_std use statements
plafer Feb 5, 2024
f52dfb8
fmt
plafer Feb 5, 2024
49518ad
`Errors`: make heading
plafer Feb 6, 2024
4bab1c1
use `SMT_DEPTH`
plafer Feb 6, 2024
a448ff3
SmtLeaf single case: check leaf index
plafer Feb 6, 2024
1622f25
Multiple case: check consistency with leaf index
plafer Feb 6, 2024
6f69128
use `pub(super)` instead of `pub(crate)`
plafer Feb 6, 2024
957375e
use `pub(super)`
plafer Feb 6, 2024
ea5eb8c
`SmtLeaf`: add `num_entries()` accessor
plafer Feb 6, 2024
b5556de
Fix `SmtLeaf` serialization
plafer Feb 6, 2024
148773a
improve leaf serialization tests
plafer Feb 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
10 changes: 10 additions & 0 deletions src/merkle/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::{
};
use core::fmt;

use super::smt::SmtLeafError;

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MerkleError {
ConflictingRoots(Vec<RpoDigest>),
Expand All @@ -20,6 +22,7 @@ pub enum MerkleError {
NodeNotInStore(RpoDigest, NodeIndex),
NumLeavesNotPowerOfTwo(usize),
RootNotInStore(RpoDigest),
SmtLeaf(SmtLeafError),
}

impl fmt::Display for MerkleError {
Expand Down Expand Up @@ -50,9 +53,16 @@ impl fmt::Display for MerkleError {
write!(f, "the leaves count {leaves} is not a power of 2")
}
RootNotInStore(root) => write!(f, "the root {:?} is not in the store", root),
SmtLeaf(smt_leaf_error) => write!(f, "smt leaf error: {smt_leaf_error}"),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for MerkleError {}

impl From<SmtLeafError> for MerkleError {
fn from(value: SmtLeafError) -> Self {
Self::SmtLeaf(value)
}
}
86 changes: 86 additions & 0 deletions src/merkle/smt/full/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use core::fmt;

use crate::{
hash::rpo::RpoDigest,
merkle::{LeafIndex, SMT_DEPTH},
utils::collections::Vec,
Word,
};

// SMT LEAF ERROR
// =================================================================================================

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SmtLeafError {
InconsistentKeys {
entries: Vec<(RpoDigest, Word)>,
key_1: RpoDigest,
key_2: RpoDigest,
},
InvalidNumEntriesForMultiple(usize),
SingleKeyInconsistentWithLeafIndex {
key: RpoDigest,
leaf_index: LeafIndex<SMT_DEPTH>,
},
MultipleKeysInconsistentWithLeafIndex {
leaf_index_from_keys: LeafIndex<SMT_DEPTH>,
leaf_index_supplied: LeafIndex<SMT_DEPTH>,
},
}

#[cfg(feature = "std")]
impl std::error::Error for SmtLeafError {}

impl fmt::Display for SmtLeafError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use SmtLeafError::*;
match self {
InvalidNumEntriesForMultiple(num_entries) => {
write!(f, "Multiple leaf requires 2 or more entries. Got: {num_entries}")
}
InconsistentKeys { entries, key_1, key_2 } => {
write!(f, "Multiple leaf requires all keys to map to the same leaf index. Offending keys: {key_1} and {key_2}. Entries: {entries:?}.")
}
SingleKeyInconsistentWithLeafIndex { key, leaf_index } => {
write!(
f,
"Single key in leaf inconsistent with leaf index. Key: {key}, leaf index: {}",
leaf_index.value()
)
}
MultipleKeysInconsistentWithLeafIndex {
leaf_index_from_keys,
leaf_index_supplied,
} => {
write!(
f,
"Keys in entries map to leaf index {}, but leaf index {} was supplied",
leaf_index_from_keys.value(),
leaf_index_supplied.value()
)
}
}
}
}

// SMT PROOF ERROR
// =================================================================================================

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SmtProofError {
InvalidPathLength(usize),
}

#[cfg(feature = "std")]
impl std::error::Error for SmtProofError {}

impl fmt::Display for SmtProofError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use SmtProofError::*;
match self {
InvalidPathLength(path_length) => {
write!(f, "Invalid Merkle path length. Expected {SMT_DEPTH}, got {path_length}")
}
}
}
}
Loading
Loading