Expose operations on `legacy-api` types required for serialization & testing.
This commit is contained in:
parent
7cb2bdcc8f
commit
71557cc0e3
|
@ -248,19 +248,23 @@ impl<H: Hashable + Clone, const DEPTH: u8> Frontier<H, DEPTH> {
|
|||
}
|
||||
|
||||
#[cfg(feature = "legacy-api")]
|
||||
pub(crate) struct PathFiller<H> {
|
||||
pub(crate) queue: VecDeque<H>,
|
||||
pub struct PathFiller<H> {
|
||||
queue: VecDeque<H>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "legacy-api")]
|
||||
impl<H: Hashable> PathFiller<H> {
|
||||
pub(crate) fn empty() -> Self {
|
||||
pub fn empty() -> Self {
|
||||
PathFiller {
|
||||
queue: VecDeque::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn next(&mut self, level: Level) -> H {
|
||||
pub fn new(queue: VecDeque<H>) -> Self {
|
||||
Self { queue }
|
||||
}
|
||||
|
||||
pub fn next(&mut self, level: Level) -> H {
|
||||
self.queue
|
||||
.pop_front()
|
||||
.unwrap_or_else(|| H::empty_root(level))
|
||||
|
@ -287,63 +291,32 @@ impl<H, const DEPTH: u8> CommitmentTree<H, DEPTH> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_frontier(frontier: &Frontier<H, DEPTH>) -> Self
|
||||
where
|
||||
H: Clone,
|
||||
{
|
||||
frontier.value().map_or_else(Self::empty, |f| {
|
||||
let mut ommers_iter = f.ommers().iter().cloned();
|
||||
let (left, right) = if f.position().is_odd() {
|
||||
(
|
||||
ommers_iter
|
||||
.next()
|
||||
.expect("An ommer must exist if the frontier position is odd"),
|
||||
Some(f.leaf().clone()),
|
||||
)
|
||||
} else {
|
||||
(f.leaf().clone(), None)
|
||||
};
|
||||
|
||||
let upos: usize = f.position().into();
|
||||
Self {
|
||||
left: Some(left),
|
||||
pub fn from_parts(
|
||||
left: Option<H>,
|
||||
right: Option<H>,
|
||||
parents: Vec<Option<H>>,
|
||||
) -> Result<Self, ()> {
|
||||
if parents.len() < usize::from(DEPTH) {
|
||||
Ok(CommitmentTree {
|
||||
left,
|
||||
right,
|
||||
parents: (1u8..DEPTH)
|
||||
.into_iter()
|
||||
.map(|i| {
|
||||
if upos & (1 << i) == 0 {
|
||||
None
|
||||
} else {
|
||||
ommers_iter.next()
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
})
|
||||
parents,
|
||||
})
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_frontier(&self) -> Frontier<H, DEPTH>
|
||||
where
|
||||
H: Hashable + Clone,
|
||||
{
|
||||
if self.size() == 0 {
|
||||
Frontier::empty()
|
||||
} else {
|
||||
let ommers_iter = self.parents.iter().filter_map(|v| v.as_ref()).cloned();
|
||||
let (leaf, ommers) = match (self.left.as_ref(), self.right.as_ref()) {
|
||||
(Some(a), None) => (a.clone(), ommers_iter.collect()),
|
||||
(Some(a), Some(b)) => (
|
||||
b.clone(),
|
||||
Some(a.clone()).into_iter().chain(ommers_iter).collect(),
|
||||
),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
pub fn left(&self) -> &Option<H> {
|
||||
&self.left
|
||||
}
|
||||
|
||||
// If a frontier cannot be successfully constructed from the
|
||||
// parts of a commitment tree, it is a programming error.
|
||||
Frontier::from_parts((self.size() - 1).into(), leaf, ommers)
|
||||
.expect("Frontier should be constructable from CommitmentTree.")
|
||||
}
|
||||
pub fn right(&self) -> &Option<H> {
|
||||
&self.right
|
||||
}
|
||||
|
||||
pub fn parents(&self) -> &Vec<Option<H>> {
|
||||
&self.parents
|
||||
}
|
||||
|
||||
/// Returns the number of leaf nodes in the tree.
|
||||
|
@ -381,6 +354,59 @@ impl<H, const DEPTH: u8> CommitmentTree<H, DEPTH> {
|
|||
|
||||
#[cfg(feature = "legacy-api")]
|
||||
impl<H: Hashable + Clone, const DEPTH: u8> CommitmentTree<H, DEPTH> {
|
||||
pub fn from_frontier(frontier: &Frontier<H, DEPTH>) -> Self {
|
||||
frontier.value().map_or_else(Self::empty, |f| {
|
||||
let mut ommers_iter = f.ommers().iter().cloned();
|
||||
let (left, right) = if f.position().is_odd() {
|
||||
(
|
||||
ommers_iter
|
||||
.next()
|
||||
.expect("An ommer must exist if the frontier position is odd"),
|
||||
Some(f.leaf().clone()),
|
||||
)
|
||||
} else {
|
||||
(f.leaf().clone(), None)
|
||||
};
|
||||
|
||||
let upos: usize = f.position().into();
|
||||
Self {
|
||||
left: Some(left),
|
||||
right,
|
||||
parents: (1u8..DEPTH)
|
||||
.into_iter()
|
||||
.map(|i| {
|
||||
if upos & (1 << i) == 0 {
|
||||
None
|
||||
} else {
|
||||
ommers_iter.next()
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn to_frontier(&self) -> Frontier<H, DEPTH> {
|
||||
if self.size() == 0 {
|
||||
Frontier::empty()
|
||||
} else {
|
||||
let ommers_iter = self.parents.iter().filter_map(|v| v.as_ref()).cloned();
|
||||
let (leaf, ommers) = match (self.left.as_ref(), self.right.as_ref()) {
|
||||
(Some(a), None) => (a.clone(), ommers_iter.collect()),
|
||||
(Some(a), Some(b)) => (
|
||||
b.clone(),
|
||||
Some(a.clone()).into_iter().chain(ommers_iter).collect(),
|
||||
),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
// If a frontier cannot be successfully constructed from the
|
||||
// parts of a commitment tree, it is a programming error.
|
||||
Frontier::from_parts((self.size() - 1).into(), leaf, ommers)
|
||||
.expect("Frontier should be constructable from CommitmentTree.")
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a leaf node to the tree.
|
||||
///
|
||||
/// Returns an error if the tree is full.
|
||||
|
@ -422,10 +448,10 @@ impl<H: Hashable + Clone, const DEPTH: u8> CommitmentTree<H, DEPTH> {
|
|||
|
||||
/// Returns the current root of the tree.
|
||||
pub fn root(&self) -> H {
|
||||
self.root_inner(DEPTH, PathFiller::empty())
|
||||
self.root_at_depth(DEPTH, PathFiller::empty())
|
||||
}
|
||||
|
||||
pub(crate) fn root_inner(&self, depth: u8, mut filler: PathFiller<H>) -> H {
|
||||
pub fn root_at_depth(&self, depth: u8, mut filler: PathFiller<H>) -> H {
|
||||
assert!(depth > 0);
|
||||
|
||||
// 1) Hash left and right leaves together.
|
||||
|
|
|
@ -14,16 +14,8 @@ pub struct MerklePath<H, const DEPTH: u8> {
|
|||
}
|
||||
|
||||
impl<H, const DEPTH: u8> MerklePath<H, DEPTH> {
|
||||
pub fn auth_path(&self) -> &[(H, bool)] {
|
||||
&self.auth_path
|
||||
}
|
||||
|
||||
pub fn position(&self) -> u64 {
|
||||
self.position
|
||||
}
|
||||
|
||||
/// Constructs a Merkle path directly from a path and position.
|
||||
pub fn from_path(auth_path: Vec<(H, bool)>, position: u64) -> Result<Self, ()> {
|
||||
pub fn from_parts(auth_path: Vec<(H, bool)>, position: u64) -> Result<Self, ()> {
|
||||
if auth_path.len() == usize::from(DEPTH) {
|
||||
Ok(MerklePath {
|
||||
auth_path,
|
||||
|
@ -33,6 +25,14 @@ impl<H, const DEPTH: u8> MerklePath<H, DEPTH> {
|
|||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn auth_path(&self) -> &[(H, bool)] {
|
||||
&self.auth_path
|
||||
}
|
||||
|
||||
pub fn position(&self) -> u64 {
|
||||
self.position
|
||||
}
|
||||
}
|
||||
|
||||
impl<H: Hashable, const DEPTH: u8> MerklePath<H, DEPTH> {
|
||||
|
@ -91,7 +91,7 @@ pub struct IncrementalWitness<H, const DEPTH: u8> {
|
|||
impl<H, const DEPTH: u8> IncrementalWitness<H, DEPTH> {
|
||||
/// Creates an `IncrementalWitness` for the most recent commitment added to the given
|
||||
/// [`CommitmentTree`].
|
||||
pub fn from_tree(tree: CommitmentTree<H, DEPTH>) -> IncrementalWitness<H, DEPTH> {
|
||||
pub fn from_tree(tree: CommitmentTree<H, DEPTH>) -> Self {
|
||||
IncrementalWitness {
|
||||
tree,
|
||||
filled: vec![],
|
||||
|
@ -100,6 +100,35 @@ impl<H, const DEPTH: u8> IncrementalWitness<H, DEPTH> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_parts(
|
||||
tree: CommitmentTree<H, DEPTH>,
|
||||
filled: Vec<H>,
|
||||
cursor: Option<CommitmentTree<H, DEPTH>>,
|
||||
) -> Self {
|
||||
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
|
||||
}
|
||||
|
||||
/// Returns the position of the witnessed leaf node in the commitment tree.
|
||||
pub fn position(&self) -> usize {
|
||||
self.tree.size() - 1
|
||||
|
@ -150,11 +179,9 @@ impl<H: Hashable + Clone, const DEPTH: u8> IncrementalWitness<H, DEPTH> {
|
|||
let cursor_root = self
|
||||
.cursor
|
||||
.as_ref()
|
||||
.map(|c| c.root_inner(self.cursor_depth, PathFiller::empty()));
|
||||
.map(|c| c.root_at_depth(self.cursor_depth, PathFiller::empty()));
|
||||
|
||||
PathFiller {
|
||||
queue: self.filled.iter().cloned().chain(cursor_root).collect(),
|
||||
}
|
||||
PathFiller::new(self.filled.iter().cloned().chain(cursor_root).collect())
|
||||
}
|
||||
|
||||
/// Tracks a leaf node that has been added to the underlying tree.
|
||||
|
@ -166,7 +193,7 @@ impl<H: Hashable + Clone, const DEPTH: u8> IncrementalWitness<H, DEPTH> {
|
|||
cursor.append(node).expect("cursor should not be full");
|
||||
if cursor.is_complete(self.cursor_depth) {
|
||||
self.filled
|
||||
.push(cursor.root_inner(self.cursor_depth, PathFiller::empty()));
|
||||
.push(cursor.root_at_depth(self.cursor_depth, PathFiller::empty()));
|
||||
} else {
|
||||
self.cursor = Some(cursor);
|
||||
}
|
||||
|
@ -191,11 +218,7 @@ impl<H: Hashable + Clone, const DEPTH: u8> IncrementalWitness<H, DEPTH> {
|
|||
|
||||
/// Returns the current root of the tree corresponding to the witness.
|
||||
pub fn root(&self) -> H {
|
||||
self.root_inner(DEPTH)
|
||||
}
|
||||
|
||||
fn root_inner(&self, depth: u8) -> H {
|
||||
self.tree.root_inner(depth, self.filler())
|
||||
self.tree.root_at_depth(DEPTH, self.filler())
|
||||
}
|
||||
|
||||
/// Returns the current witness, or None if the tree is empty.
|
||||
|
@ -234,6 +257,6 @@ impl<H: Hashable + Clone, const DEPTH: u8> IncrementalWitness<H, DEPTH> {
|
|||
|
||||
assert_eq!(auth_path.len(), usize::from(depth));
|
||||
|
||||
MerklePath::from_path(auth_path, self.position() as u64).ok()
|
||||
MerklePath::from_parts(auth_path, self.position() as u64).ok()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue