smt: add pairs_to_leaf() to trait
This commit is contained in:
parent
b585f9cac6
commit
ae772d2af6
3 changed files with 32 additions and 1 deletions
|
@ -344,6 +344,23 @@ 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());
|
||||||
|
|
||||||
|
if pairs.len() > 1 {
|
||||||
|
SmtLeaf::new_multiple(pairs).unwrap()
|
||||||
|
} else {
|
||||||
|
let (key, value) = pairs.pop().unwrap();
|
||||||
|
// TODO: 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`
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
use alloc::collections::{BTreeMap, BTreeSet};
|
use alloc::{
|
||||||
|
collections::{BTreeMap, BTreeSet},
|
||||||
|
vec::Vec,
|
||||||
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
super::ValuePath, EmptySubtreeRoots, InnerNode, InnerNodeInfo, LeafIndex, MerkleError,
|
super::ValuePath, EmptySubtreeRoots, InnerNode, InnerNodeInfo, LeafIndex, MerkleError,
|
||||||
|
@ -370,4 +373,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