use alloc::vec::Vec;
use core::convert::TryInto;
use core::iter::repeat;
use crate::{
frontier::{CommitmentTree, PathFiller},
Hashable, Level, MerklePath, Position, Source,
};
#[derive(Clone, Debug)]
pub struct IncrementalWitness<H, const DEPTH: u8> {
tree: CommitmentTree<H, DEPTH>,
filled: Vec<H>,
cursor_depth: u8,
cursor: Option<CommitmentTree<H, DEPTH>>,
}
impl<H, const DEPTH: u8> IncrementalWitness<H, DEPTH> {
pub fn from_tree(tree: CommitmentTree<H, DEPTH>) -> Option<Self> {
(!tree.is_empty()).then(|| IncrementalWitness {
tree,
filled: vec![],
cursor_depth: 0,
cursor: None,
})
}
#[cfg(feature = "test-dependencies")]
pub fn invalid_empty_witness() -> Self {
Self {
tree: CommitmentTree::empty(),
filled: vec![],
cursor_depth: 0,
cursor: None,
}
}
pub fn from_parts(
tree: CommitmentTree<H, DEPTH>,
filled: Vec<H>,
cursor: Option<CommitmentTree<H, DEPTH>>,
) -> Option<Self> {
(!(tree.is_empty() && filled.is_empty() && cursor.is_none())).then(|| {
let mut witness = IncrementalWitness {
tree,
filled,
cursor_depth: 0,
cursor,
};
witness.cursor_depth = witness.next_depth();
witness
})
}
pub fn tree(&self) -> &CommitmentTree<H, DEPTH> {
&self.tree
}
pub fn filled(&self) -> &Vec<H> {
&self.filled
}
pub fn cursor(&self) -> &Option<CommitmentTree<H, DEPTH>> {
&self.cursor
}
pub fn witnessed_position(&self) -> Position {
Position::try_from(self.tree.size() - 1)
.expect("Commitment trees with more than 2^64 leaves are unsupported.")
}
pub fn tip_position(&self) -> Position {
let leaves_to_cursor_start = self
.witnessed_position()
.witness_addrs(Level::from(DEPTH))
.filter_map(|(addr, source)| {
if source == Source::Future {
Some(addr)
} else {
None
}
})
.take(self.filled.len())
.fold(0u64, |acc, addr| acc + (1u64 << u8::from(addr.level())));
self.witnessed_position()
+ leaves_to_cursor_start
+ self.cursor.as_ref().map_or(0, |c| {
u64::try_from(c.size())
.expect("Note commitment trees with > 2^64 leaves are not supported.")
})
}
fn next_depth(&self) -> u8 {
let mut skip: u8 = self
.filled
.len()
.try_into()
.expect("Merkle tree depths may not exceed the bounds of a u8");
if self.tree.left.is_none() {
if skip > 0 {
skip -= 1;
} else {
return 0;
}
}
if self.tree.right.is_none() {
if skip > 0 {
skip -= 1;
} else {
return 0;
}
}
let mut d = 1;
for p in &self.tree.parents {
if p.is_none() {
if skip > 0 {
skip -= 1;
} else {
return d;
}
}
d += 1;
}
d + skip
}
}
impl<H: Hashable + Clone, const DEPTH: u8> IncrementalWitness<H, DEPTH> {
fn filler(&self) -> PathFiller<H> {
let cursor_root = self
.cursor
.as_ref()
.map(|c| c.root_at_depth(self.cursor_depth, PathFiller::empty()));
PathFiller::new(self.filled.iter().cloned().chain(cursor_root).collect())
}
#[allow(clippy::result_unit_err)]
pub fn append(&mut self, node: H) -> Result<(), ()> {
if let Some(mut cursor) = self.cursor.take() {
cursor.append(node).expect("cursor should not be full");
if cursor.is_complete(self.cursor_depth) {
self.filled
.push(cursor.root_at_depth(self.cursor_depth, PathFiller::empty()));
} else {
self.cursor = Some(cursor);
}
} else {
self.cursor_depth = self.next_depth();
if self.cursor_depth >= DEPTH {
return Err(());
}
if self.cursor_depth == 0 {
self.filled.push(node);
} else {
let mut cursor = CommitmentTree::empty();
cursor.append(node).expect("cursor should not be full");
self.cursor = Some(cursor);
}
}
Ok(())
}
pub fn root(&self) -> H {
self.tree.root_at_depth(DEPTH, self.filler())
}
pub fn path(&self) -> Option<MerklePath<H, DEPTH>> {
self.path_inner(DEPTH)
}
fn path_inner(&self, depth: u8) -> Option<MerklePath<H, DEPTH>> {
let mut filler = self.filler();
let mut auth_path = Vec::new();
if let Some(node) = &self.tree.left {
if self.tree.right.is_some() {
auth_path.push(node.clone());
} else {
auth_path.push(filler.next(0.into()));
}
} else {
return None;
}
for (p, i) in self
.tree
.parents
.iter()
.chain(repeat(&None))
.take((depth - 1).into())
.zip(0u8..)
{
auth_path.push(match p {
Some(node) => node.clone(),
None => filler.next(Level::from(i + 1)),
});
}
assert_eq!(auth_path.len(), usize::from(depth));
MerklePath::from_parts(auth_path, self.witnessed_position()).ok()
}
}
#[cfg(test)]
mod tests {
use alloc::string::{String, ToString};
use crate::{frontier::CommitmentTree, witness::IncrementalWitness, Position};
#[test]
fn witness_tip_position() {
let mut base_tree = CommitmentTree::<String, 6>::empty();
for c in 'a'..'h' {
base_tree.append(c.to_string()).unwrap();
}
let mut witness = IncrementalWitness::from_tree(base_tree).unwrap();
for c in 'h'..'z' {
witness.append(c.to_string()).unwrap();
}
assert_eq!(witness.tip_position(), Position::from(24));
}
}