-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #36
- Loading branch information
Showing
11 changed files
with
1,792 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use super::{vec, Rpo256, Vec, Word}; | ||
use core::ops::Deref; | ||
|
||
// MERKLE PATH | ||
// ================================================================================================ | ||
|
||
/// A merkle path container, composed of a sequence of nodes of a Merkle tree. | ||
#[derive(Clone, Debug, Default, PartialEq, Eq)] | ||
pub struct MerklePath { | ||
nodes: Vec<Word>, | ||
} | ||
|
||
impl MerklePath { | ||
// CONSTRUCTORS | ||
// -------------------------------------------------------------------------------------------- | ||
|
||
/// Creates a new Merkle path from a list of nodes. | ||
pub fn new(nodes: Vec<Word>) -> Self { | ||
Self { nodes } | ||
} | ||
|
||
// PROVIDERS | ||
// -------------------------------------------------------------------------------------------- | ||
|
||
/// Returns the depth in which this Merkle path proof is valid. | ||
pub fn depth(&self) -> u8 { | ||
self.nodes.len() as u8 | ||
} | ||
|
||
/// Verify the Merkle opening proof towards the provided root. | ||
/// | ||
/// It will assert `input` exists on a Merkle tree, indexed by `index` for an arbitrary depth. | ||
pub fn verify(&self, mut index: u64, input: Word, root: &Word) -> bool { | ||
let computed = self.nodes.iter().copied().fold(input, |node, sibling| { | ||
// build the input node, considering the parity of the current index. | ||
let is_right_sibling = (index & 1) == 1; | ||
let input = if is_right_sibling { | ||
[sibling.into(), node.into()] | ||
} else { | ||
[node.into(), sibling.into()] | ||
}; | ||
// compute the node and move to the next iteration. | ||
index >>= 1; | ||
Rpo256::merge(&input).into() | ||
}); | ||
root == &computed | ||
} | ||
} | ||
|
||
impl Deref for MerklePath { | ||
type Target = [Word]; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.nodes | ||
} | ||
} | ||
|
||
impl FromIterator<Word> for MerklePath { | ||
fn from_iter<T: IntoIterator<Item = Word>>(iter: T) -> Self { | ||
Self::new(iter.into_iter().collect()) | ||
} | ||
} | ||
|
||
impl IntoIterator for MerklePath { | ||
type Item = Word; | ||
type IntoIter = vec::IntoIter<Word>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
self.nodes.into_iter() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.