factor out subtree-append logic
This commit is contained in:
parent
632595d23a
commit
6bf895f027
1 changed files with 203 additions and 192 deletions
|
@ -383,7 +383,7 @@ where
|
|||
let hash = Self::hash_leaf(&leaf);
|
||||
|
||||
accumulator.nodes.insert(col, leaf);
|
||||
accumulator.add_leaf(SubtreeLeaf { col, hash });
|
||||
add_subtree_leaf(&mut accumulator.leaves, SubtreeLeaf { col, hash });
|
||||
|
||||
debug_assert!(current_leaf_buffer.is_empty());
|
||||
}
|
||||
|
@ -631,6 +631,7 @@ impl SubtreeLeaf {
|
|||
}
|
||||
}
|
||||
|
||||
/// Helper struct to organize the return value of [`SparseMerkleTree::sorted_pairs_to_leaves()`].
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(crate) struct PairComputations<K, L> {
|
||||
/// Literal leaves to be added to the sparse Merkle tree's internal mapping.
|
||||
|
@ -724,6 +725,36 @@ impl<'s> core::iter::Iterator for SubtreeLeavesIter<'s> {
|
|||
Some(subtree)
|
||||
}
|
||||
}
|
||||
/// Handles the logic for figuring out whether the new leaf starts a new subtree or not.
|
||||
|
||||
fn add_subtree_leaf(subtrees: &mut Vec<Vec<SubtreeLeaf>>, leaf: SubtreeLeaf) {
|
||||
let last_subtree = match subtrees.last_mut() {
|
||||
// Base case.
|
||||
None => {
|
||||
subtrees.push(vec![leaf]);
|
||||
return;
|
||||
},
|
||||
Some(last_subtree) => last_subtree,
|
||||
};
|
||||
|
||||
debug_assert!(!last_subtree.is_empty());
|
||||
debug_assert!(last_subtree.len() <= COLS_PER_SUBTREE as usize);
|
||||
|
||||
// The multiple of 256 after 0 is 1, but 0 and 1 do not belong to different subtrees.
|
||||
let last_subtree_col = u64::max(1, last_subtree.last().unwrap().col);
|
||||
let next_subtree_col = if Integer::is_multiple_of(&last_subtree_col, &COLS_PER_SUBTREE) {
|
||||
u64::next_multiple_of(last_subtree_col + 1, COLS_PER_SUBTREE)
|
||||
} else {
|
||||
last_subtree_col.next_multiple_of(COLS_PER_SUBTREE)
|
||||
};
|
||||
|
||||
if leaf.col < next_subtree_col {
|
||||
last_subtree.push(leaf);
|
||||
} else {
|
||||
let next_subtree = vec![leaf];
|
||||
subtrees.push(next_subtree);
|
||||
}
|
||||
}
|
||||
|
||||
// TESTS
|
||||
// ================================================================================================
|
||||
|
@ -733,9 +764,7 @@ mod test {
|
|||
|
||||
use alloc::{collections::BTreeMap, vec::Vec};
|
||||
|
||||
use num::Integer;
|
||||
|
||||
use super::{InnerNode, PairComputations, SparseMerkleTree, SubtreeLeaf, COLS_PER_SUBTREE};
|
||||
use super::{InnerNode, PairComputations, SparseMerkleTree, SubtreeLeaf};
|
||||
use crate::{
|
||||
hash::rpo::RpoDigest,
|
||||
merkle::{NodeIndex, Smt, SmtLeaf, SMT_DEPTH},
|
||||
|
@ -950,25 +979,7 @@ mod test {
|
|||
accumulated_nodes.extend(nodes);
|
||||
|
||||
for subtree_leaf in next_leaves {
|
||||
if leaf_subtrees.is_empty() {
|
||||
leaf_subtrees.push(vec![subtree_leaf]);
|
||||
continue;
|
||||
}
|
||||
|
||||
let buffer_max_col =
|
||||
u64::max(1, leaf_subtrees.last().unwrap().last().unwrap().col);
|
||||
let next_subtree_col =
|
||||
if Integer::is_multiple_of(&buffer_max_col, &COLS_PER_SUBTREE) {
|
||||
u64::next_multiple_of(buffer_max_col + 1, COLS_PER_SUBTREE)
|
||||
} else {
|
||||
buffer_max_col.next_multiple_of(COLS_PER_SUBTREE)
|
||||
};
|
||||
|
||||
if subtree_leaf.col < next_subtree_col {
|
||||
leaf_subtrees.last_mut().unwrap().push(subtree_leaf);
|
||||
} else {
|
||||
leaf_subtrees.push(vec![subtree_leaf]);
|
||||
}
|
||||
super::add_subtree_leaf(&mut leaf_subtrees, subtree_leaf);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue