smt: add pairs_to_leaf() to trait
This commit is contained in:
parent
8e594963d2
commit
b954bb5287
3 changed files with 35 additions and 0 deletions
|
@ -344,6 +344,30 @@ impl SparseMerkleTree<SMT_DEPTH> for Smt {
|
||||||
fn path_and_leaf_to_opening(path: MerklePath, leaf: SmtLeaf) -> SmtProof {
|
fn path_and_leaf_to_opening(path: MerklePath, leaf: SmtLeaf) -> SmtProof {
|
||||||
SmtProof::new_unchecked(path, leaf)
|
SmtProof::new_unchecked(path, leaf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn pairs_to_leaf(mut pairs: Vec<(RpoDigest, Word)>) -> SmtLeaf {
|
||||||
|
assert!(!pairs.is_empty());
|
||||||
|
|
||||||
|
// FIXME
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
let mut control = pairs.clone();
|
||||||
|
control.sort_by_key(|(key, _)| Self::key_to_leaf_index(key).index.value());
|
||||||
|
assert_eq!(control, pairs);
|
||||||
|
}
|
||||||
|
|
||||||
|
if pairs.len() > 1 {
|
||||||
|
SmtLeaf::new_multiple(pairs).unwrap()
|
||||||
|
} else {
|
||||||
|
let (key, value) = pairs.pop().unwrap();
|
||||||
|
// FIXME: should we ever be constructing empty leaves from pairs?
|
||||||
|
if value == Self::EMPTY_VALUE {
|
||||||
|
let index = Self::key_to_leaf_index(&key);
|
||||||
|
SmtLeaf::new_empty(index)
|
||||||
|
} else {
|
||||||
|
SmtLeaf::new_single(key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Smt {
|
impl Default for Smt {
|
||||||
|
|
|
@ -338,6 +338,10 @@ pub(crate) trait SparseMerkleTree<const DEPTH: u8> {
|
||||||
/// Maps a key to a leaf index
|
/// Maps a key to a leaf index
|
||||||
fn key_to_leaf_index(key: &Self::Key) -> LeafIndex<DEPTH>;
|
fn key_to_leaf_index(key: &Self::Key) -> LeafIndex<DEPTH>;
|
||||||
|
|
||||||
|
/// Constructs a single leaf from an arbitrary amount of key-value pairs.
|
||||||
|
/// Those pairs must all have the same leaf index.
|
||||||
|
fn pairs_to_leaf(pairs: Vec<(Self::Key, Self::Value)>) -> Self::Leaf;
|
||||||
|
|
||||||
/// Maps a (MerklePath, Self::Leaf) to an opening.
|
/// Maps a (MerklePath, Self::Leaf) to an opening.
|
||||||
///
|
///
|
||||||
/// The length `path` is guaranteed to be equal to `DEPTH`
|
/// The length `path` is guaranteed to be equal to `DEPTH`
|
||||||
|
|
|
@ -370,4 +370,11 @@ impl<const DEPTH: u8> SparseMerkleTree<DEPTH> for SimpleSmt<DEPTH> {
|
||||||
fn path_and_leaf_to_opening(path: MerklePath, leaf: Word) -> ValuePath {
|
fn path_and_leaf_to_opening(path: MerklePath, leaf: Word) -> ValuePath {
|
||||||
(path, leaf).into()
|
(path, leaf).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn pairs_to_leaf(mut pairs: Vec<(LeafIndex<DEPTH>, Word)>) -> Word {
|
||||||
|
// SimpleSmt can't have more than one value per key.
|
||||||
|
assert_eq!(pairs.len(), 1);
|
||||||
|
let (_key, value) = pairs.pop().unwrap();
|
||||||
|
value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue