mirror of
https://github.com/arnaucube/miden-crypto.git
synced 2026-01-12 09:01:29 +01:00
refactor: refactor crypto APIs to use RpoDigest instead of Word
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use super::{vec, InnerNodeInfo, MerkleError, NodeIndex, Rpo256, Vec, Word};
|
||||
use super::{vec, InnerNodeInfo, MerkleError, NodeIndex, Rpo256, RpoDigest, Vec};
|
||||
use core::ops::{Deref, DerefMut};
|
||||
|
||||
// MERKLE PATH
|
||||
@@ -7,7 +7,7 @@ use core::ops::{Deref, DerefMut};
|
||||
/// 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>,
|
||||
nodes: Vec<RpoDigest>,
|
||||
}
|
||||
|
||||
impl MerklePath {
|
||||
@@ -15,7 +15,7 @@ impl MerklePath {
|
||||
// --------------------------------------------------------------------------------------------
|
||||
|
||||
/// Creates a new Merkle path from a list of nodes.
|
||||
pub fn new(nodes: Vec<Word>) -> Self {
|
||||
pub fn new(nodes: Vec<RpoDigest>) -> Self {
|
||||
Self { nodes }
|
||||
}
|
||||
|
||||
@@ -28,13 +28,13 @@ impl MerklePath {
|
||||
}
|
||||
|
||||
/// Computes the merkle root for this opening.
|
||||
pub fn compute_root(&self, index: u64, node: Word) -> Result<Word, MerkleError> {
|
||||
pub fn compute_root(&self, index: u64, node: RpoDigest) -> Result<RpoDigest, MerkleError> {
|
||||
let mut index = NodeIndex::new(self.depth(), index)?;
|
||||
let root = self.nodes.iter().copied().fold(node, |node, sibling| {
|
||||
// compute the node and move to the next iteration.
|
||||
let input = index.build_node(node.into(), sibling.into());
|
||||
let input = index.build_node(node, sibling);
|
||||
index.move_up();
|
||||
Rpo256::merge(&input).into()
|
||||
Rpo256::merge(&input)
|
||||
});
|
||||
Ok(root)
|
||||
}
|
||||
@@ -42,7 +42,7 @@ impl MerklePath {
|
||||
/// Verifies the Merkle opening proof towards the provided root.
|
||||
///
|
||||
/// Returns `true` if `node` exists at `index` in a Merkle tree with `root`.
|
||||
pub fn verify(&self, index: u64, node: Word, root: &Word) -> bool {
|
||||
pub fn verify(&self, index: u64, node: RpoDigest, root: &RpoDigest) -> bool {
|
||||
match self.compute_root(index, node) {
|
||||
Ok(computed_root) => root == &computed_root,
|
||||
Err(_) => false,
|
||||
@@ -55,7 +55,11 @@ impl MerklePath {
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns an error if the specified index is not valid for this path.
|
||||
pub fn inner_nodes(&self, index: u64, node: Word) -> Result<InnerNodeIterator, MerkleError> {
|
||||
pub fn inner_nodes(
|
||||
&self,
|
||||
index: u64,
|
||||
node: RpoDigest,
|
||||
) -> Result<InnerNodeIterator, MerkleError> {
|
||||
Ok(InnerNodeIterator {
|
||||
nodes: &self.nodes,
|
||||
index: NodeIndex::new(self.depth(), index)?,
|
||||
@@ -64,8 +68,8 @@ impl MerklePath {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Word>> for MerklePath {
|
||||
fn from(path: Vec<Word>) -> Self {
|
||||
impl From<Vec<RpoDigest>> for MerklePath {
|
||||
fn from(path: Vec<RpoDigest>) -> Self {
|
||||
Self::new(path)
|
||||
}
|
||||
}
|
||||
@@ -73,7 +77,7 @@ impl From<Vec<Word>> for MerklePath {
|
||||
impl Deref for MerklePath {
|
||||
// we use `Vec` here instead of slice so we can call vector mutation methods directly from the
|
||||
// merkle path (example: `Vec::remove`).
|
||||
type Target = Vec<Word>;
|
||||
type Target = Vec<RpoDigest>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.nodes
|
||||
@@ -89,15 +93,15 @@ impl DerefMut for MerklePath {
|
||||
// ITERATORS
|
||||
// ================================================================================================
|
||||
|
||||
impl FromIterator<Word> for MerklePath {
|
||||
fn from_iter<T: IntoIterator<Item = Word>>(iter: T) -> Self {
|
||||
impl FromIterator<RpoDigest> for MerklePath {
|
||||
fn from_iter<T: IntoIterator<Item = RpoDigest>>(iter: T) -> Self {
|
||||
Self::new(iter.into_iter().collect())
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoIterator for MerklePath {
|
||||
type Item = Word;
|
||||
type IntoIter = vec::IntoIter<Word>;
|
||||
type Item = RpoDigest;
|
||||
type IntoIter = vec::IntoIter<RpoDigest>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.nodes.into_iter()
|
||||
@@ -106,9 +110,9 @@ impl IntoIterator for MerklePath {
|
||||
|
||||
/// An iterator over internal nodes of a [MerklePath].
|
||||
pub struct InnerNodeIterator<'a> {
|
||||
nodes: &'a Vec<Word>,
|
||||
nodes: &'a Vec<RpoDigest>,
|
||||
index: NodeIndex,
|
||||
value: Word,
|
||||
value: RpoDigest,
|
||||
}
|
||||
|
||||
impl<'a> Iterator for InnerNodeIterator<'a> {
|
||||
@@ -123,7 +127,7 @@ impl<'a> Iterator for InnerNodeIterator<'a> {
|
||||
(self.value, self.nodes[sibling_pos])
|
||||
};
|
||||
|
||||
self.value = Rpo256::merge(&[left.into(), right.into()]).into();
|
||||
self.value = Rpo256::merge(&[left, right]);
|
||||
self.index.move_up();
|
||||
|
||||
Some(InnerNodeInfo {
|
||||
@@ -144,7 +148,7 @@ impl<'a> Iterator for InnerNodeIterator<'a> {
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct ValuePath {
|
||||
/// The node value opening for `path`.
|
||||
pub value: Word,
|
||||
pub value: RpoDigest,
|
||||
/// The path from `value` to `root` (exclusive).
|
||||
pub path: MerklePath,
|
||||
}
|
||||
@@ -156,7 +160,7 @@ pub struct ValuePath {
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct RootPath {
|
||||
/// The node value opening for `path`.
|
||||
pub root: Word,
|
||||
pub root: RpoDigest,
|
||||
/// The path from `value` to `root` (exclusive).
|
||||
pub path: MerklePath,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user