v0.1.2 releaseal-gkr-basic-workflow v0.1.2
@ -0,0 +1,126 @@ |
|||
use super::{Felt, MerkleError, RpoDigest, StarkField};
|
|||
|
|||
// NODE INDEX
|
|||
// ================================================================================================
|
|||
|
|||
/// A Merkle tree address to an arbitrary node.
|
|||
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
|
|||
pub struct NodeIndex {
|
|||
depth: u8,
|
|||
value: u64,
|
|||
}
|
|||
|
|||
impl NodeIndex {
|
|||
// CONSTRUCTORS
|
|||
// --------------------------------------------------------------------------------------------
|
|||
|
|||
/// Creates a new node index.
|
|||
pub const fn new(depth: u8, value: u64) -> Self {
|
|||
Self { depth, value }
|
|||
}
|
|||
|
|||
/// Creates a node index from a pair of field elements representing the depth and value.
|
|||
///
|
|||
/// # Errors
|
|||
///
|
|||
/// Will error if the `u64` representation of the depth doesn't fit a `u8`.
|
|||
pub fn from_elements(depth: &Felt, value: &Felt) -> Result<Self, MerkleError> {
|
|||
let depth = depth.as_int();
|
|||
let depth = u8::try_from(depth).map_err(|_| MerkleError::DepthTooBig(depth))?;
|
|||
let value = value.as_int();
|
|||
Ok(Self::new(depth, value))
|
|||
}
|
|||
|
|||
/// Creates a new node index pointing to the root of the tree.
|
|||
pub const fn root() -> Self {
|
|||
Self { depth: 0, value: 0 }
|
|||
}
|
|||
|
|||
/// Mutates the instance and returns it, replacing the depth.
|
|||
pub const fn with_depth(mut self, depth: u8) -> Self {
|
|||
self.depth = depth;
|
|||
self
|
|||
}
|
|||
|
|||
/// Computes the value of the sibling of the current node.
|
|||
pub fn sibling(mut self) -> Self {
|
|||
self.value ^= 1;
|
|||
self
|
|||
}
|
|||
|
|||
// PROVIDERS
|
|||
// --------------------------------------------------------------------------------------------
|
|||
|
|||
/// Builds a node to be used as input of a hash function when computing a Merkle path.
|
|||
///
|
|||
/// Will evaluate the parity of the current instance to define the result.
|
|||
pub const fn build_node(&self, slf: RpoDigest, sibling: RpoDigest) -> [RpoDigest; 2] {
|
|||
if self.is_value_odd() {
|
|||
[sibling, slf]
|
|||
} else {
|
|||
[slf, sibling]
|
|||
}
|
|||
}
|
|||
|
|||
/// Returns the scalar representation of the depth/value pair.
|
|||
///
|
|||
/// It is computed as `2^depth + value`.
|
|||
pub const fn to_scalar_index(&self) -> u64 {
|
|||
(1 << self.depth as u64) + self.value
|
|||
}
|
|||
|
|||
/// Returns the depth of the current instance.
|
|||
pub const fn depth(&self) -> u8 {
|
|||
self.depth
|
|||
}
|
|||
|
|||
/// Returns the value of the current depth.
|
|||
pub const fn value(&self) -> u64 {
|
|||
self.value
|
|||
}
|
|||
|
|||
/// Returns true if the current value fits the current depth for a binary tree.
|
|||
pub const fn is_valid(&self) -> bool {
|
|||
self.value < (1 << self.depth as u64)
|
|||
}
|
|||
|
|||
/// Returns true if the current instance points to a right sibling node.
|
|||
pub const fn is_value_odd(&self) -> bool {
|
|||
(self.value & 1) == 1
|
|||
}
|
|||
|
|||
/// Returns `true` if the depth is `0`.
|
|||
pub const fn is_root(&self) -> bool {
|
|||
self.depth == 0
|
|||
}
|
|||
|
|||
// STATE MUTATORS
|
|||
// --------------------------------------------------------------------------------------------
|
|||
|
|||
/// Traverse one level towards the root, decrementing the depth by `1`.
|
|||
pub fn move_up(&mut self) -> &mut Self {
|
|||
self.depth = self.depth.saturating_sub(1);
|
|||
self.value >>= 1;
|
|||
self
|
|||
}
|
|||
}
|
|||
|
|||
#[cfg(test)]
|
|||
mod tests {
|
|||
use super::*;
|
|||
use proptest::prelude::*;
|
|||
|
|||
proptest! {
|
|||
#[test]
|
|||
fn arbitrary_index_wont_panic_on_move_up(
|
|||
depth in prop::num::u8::ANY,
|
|||
value in prop::num::u64::ANY,
|
|||
count in prop::num::u8::ANY,
|
|||
) {
|
|||
let mut index = NodeIndex::new(depth, value);
|
|||
for _ in 0..count {
|
|||
index.move_up();
|
|||
}
|
|||
}
|
|||
}
|
|||
}
|
@ -1,344 +0,0 @@ |
|||
use super::{BTreeMap, MerkleError, Rpo256, Vec, Word, ZERO};
|
|||
|
|||
// MERKLE PATH SET
|
|||
// ================================================================================================
|
|||
|
|||
/// A set of Merkle paths.
|
|||
#[derive(Debug, Clone, PartialEq, Eq)]
|
|||
pub struct MerklePathSet {
|
|||
root: Word,
|
|||
total_depth: u32,
|
|||
paths: BTreeMap<u64, Vec<Word>>,
|
|||
}
|
|||
|
|||
impl MerklePathSet {
|
|||
// CONSTRUCTOR
|
|||
// --------------------------------------------------------------------------------------------
|
|||
|
|||
/// Returns an empty MerklePathSet.
|
|||
pub fn new(depth: u32) -> Result<Self, MerkleError> {
|
|||
let root = [ZERO; 4];
|
|||
let paths = BTreeMap::<u64, Vec<Word>>::new();
|
|||
|
|||
Ok(Self {
|
|||
root,
|
|||
total_depth: depth,
|
|||
paths,
|
|||
})
|
|||
}
|
|||
|
|||
// PUBLIC ACCESSORS
|
|||
// --------------------------------------------------------------------------------------------
|
|||
|
|||
/// Adds the specified Merkle path to this [MerklePathSet]. The `index` and `value` parameters
|
|||
/// specify the leaf node at which the path starts.
|
|||
///
|
|||
/// # Errors
|
|||
/// Returns an error if:
|
|||
/// - The specified index is not valid in the context of this Merkle path set (i.e., the index
|
|||
/// implies a greater depth than is specified for this set).
|
|||
/// - The specified path is not consistent with other paths in the set (i.e., resolves to a
|
|||
/// different root).
|
|||
pub fn add_path(
|
|||
&mut self,
|
|||
index: u64,
|
|||
value: Word,
|
|||
path: Vec<Word>,
|
|||
) -> Result<(), MerkleError> {
|
|||
let depth = (path.len() + 1) as u32;
|
|||
if depth != self.total_depth {
|
|||
return Err(MerkleError::InvalidDepth(self.total_depth, depth));
|
|||
}
|
|||
|
|||
// Actual number of node in tree
|
|||
let pos = 2u64.pow(self.total_depth) + index;
|
|||
|
|||
// Index of the leaf path in map. Paths of neighboring leaves are stored in one key-value pair
|
|||
let half_pos = pos / 2;
|
|||
|
|||
let mut extended_path = path;
|
|||
if is_even(pos) {
|
|||
extended_path.insert(0, value);
|
|||
} else {
|
|||
extended_path.insert(1, value);
|
|||
}
|
|||
|
|||
let root_of_current_path = compute_path_root(&extended_path, depth, index);
|
|||
if self.root == [ZERO; 4] {
|
|||
self.root = root_of_current_path;
|
|||
} else if self.root != root_of_current_path {
|
|||
return Err(MerkleError::InvalidPath(extended_path));
|
|||
}
|
|||
self.paths.insert(half_pos, extended_path);
|
|||
|
|||
Ok(())
|
|||
}
|
|||
|
|||
/// Returns the root to which all paths in this set resolve.
|
|||
pub fn root(&self) -> Word {
|
|||
self.root
|
|||
}
|
|||
|
|||
/// Returns the depth of the Merkle tree implied by the paths stored in this set.
|
|||
///
|
|||
/// Merkle tree of depth 1 has two leaves, depth 2 has four leaves etc.
|
|||
pub fn depth(&self) -> u32 {
|
|||
self.total_depth
|
|||
}
|
|||
|
|||
/// Returns a node at the specified index.
|
|||
///
|
|||
/// # Errors
|
|||
/// Returns an error if:
|
|||
/// * The specified index not valid for the depth of structure.
|
|||
/// * Requested node does not exist in the set.
|
|||
pub fn get_node(&self, depth: u32, index: u64) -> Result<Word, MerkleError> {
|
|||
if index >= 2u64.pow(self.total_depth) {
|
|||
return Err(MerkleError::InvalidIndex(self.total_depth, index));
|
|||
}
|
|||
if depth != self.total_depth {
|
|||
return Err(MerkleError::InvalidDepth(self.total_depth, depth));
|
|||
}
|
|||
|
|||
let pos = 2u64.pow(depth) + index;
|
|||
let index = pos / 2;
|
|||
|
|||
match self.paths.get(&index) {
|
|||
None => Err(MerkleError::NodeNotInSet(index)),
|
|||
Some(path) => {
|
|||
if is_even(pos) {
|
|||
Ok(path[0])
|
|||
} else {
|
|||
Ok(path[1])
|
|||
}
|
|||
}
|
|||
}
|
|||
}
|
|||
|
|||
/// Returns a Merkle path to the node at the specified index. The node itself is
|
|||
/// not included in the path.
|
|||
///
|
|||
/// # Errors
|
|||
/// Returns an error if:
|
|||
/// * The specified index not valid for the depth of structure.
|
|||
/// * Node of the requested path does not exist in the set.
|
|||
pub fn get_path(&self, depth: u32, index: u64) -> Result<Vec<Word>, MerkleError> {
|
|||
if index >= 2u64.pow(self.total_depth) {
|
|||
return Err(MerkleError::InvalidIndex(self.total_depth, index));
|
|||
}
|
|||
if depth != self.total_depth {
|
|||
return Err(MerkleError::InvalidDepth(self.total_depth, depth));
|
|||
}
|
|||
|
|||
let pos = 2u64.pow(depth) + index;
|
|||
let index = pos / 2;
|
|||
|
|||
match self.paths.get(&index) {
|
|||
None => Err(MerkleError::NodeNotInSet(index)),
|
|||
Some(path) => {
|
|||
let mut local_path = path.clone();
|
|||
if is_even(pos) {
|
|||
local_path.remove(0);
|
|||
Ok(local_path)
|
|||
} else {
|
|||
local_path.remove(1);
|
|||
Ok(local_path)
|
|||
}
|
|||
}
|
|||
}
|
|||
}
|
|||
|
|||
/// Replaces the leaf at the specified index with the provided value.
|
|||
///
|
|||
/// # Errors
|
|||
/// Returns an error if:
|
|||
/// * Requested node does not exist in the set.
|
|||
pub fn update_leaf(&mut self, index: u64, value: Word) -> Result<(), MerkleError> {
|
|||
let depth = self.depth();
|
|||
if index >= 2u64.pow(depth) {
|
|||
return Err(MerkleError::InvalidIndex(depth, index));
|
|||
}
|
|||
let pos = 2u64.pow(depth) + index;
|
|||
|
|||
let path = match self.paths.get_mut(&(pos / 2)) {
|
|||
None => return Err(MerkleError::NodeNotInSet(index)),
|
|||
Some(path) => path,
|
|||
};
|
|||
|
|||
// Fill old_hashes vector -----------------------------------------------------------------
|
|||
let (old_hashes, _) = compute_path_trace(path, depth, index);
|
|||
|
|||
// Fill new_hashes vector -----------------------------------------------------------------
|
|||
if is_even(pos) {
|
|||
path[0] = value;
|
|||
} else {
|
|||
path[1] = value;
|
|||
}
|
|||
|
|||
let (new_hashes, new_root) = compute_path_trace(path, depth, index);
|
|||
self.root = new_root;
|
|||
|
|||
// update paths ---------------------------------------------------------------------------
|
|||
for path in self.paths.values_mut() {
|
|||
for i in (0..old_hashes.len()).rev() {
|
|||
if path[i + 2] == old_hashes[i] {
|
|||
path[i + 2] = new_hashes[i];
|
|||
break;
|
|||
}
|
|||
}
|
|||
}
|
|||
|
|||
Ok(())
|
|||
}
|
|||
}
|
|||
|
|||
// HELPER FUNCTIONS
|
|||
// --------------------------------------------------------------------------------------------
|
|||
|
|||
fn is_even(pos: u64) -> bool {
|
|||
pos & 1 == 0
|
|||
}
|
|||
|
|||
/// Calculates the hash of the parent node by two sibling ones
|
|||
/// - node — current node
|
|||
/// - node_pos — position of the current node
|
|||
/// - sibling — neighboring vertex in the tree
|
|||
fn calculate_parent_hash(node: Word, node_pos: u64, sibling: Word) -> Word {
|
|||
if is_even(node_pos) {
|
|||
Rpo256::merge(&[node.into(), sibling.into()]).into()
|
|||
} else {
|
|||
Rpo256::merge(&[sibling.into(), node.into()]).into()
|
|||
}
|
|||
}
|
|||
|
|||
/// Returns vector of hashes from current to the root
|
|||
fn compute_path_trace(path: &[Word], depth: u32, index: u64) -> (Vec<Word>, Word) {
|
|||
let mut pos = 2u64.pow(depth) + index;
|
|||
|
|||
let mut computed_hashes = Vec::<Word>::new();
|
|||
|
|||
let mut comp_hash = Rpo256::merge(&[path[0].into(), path[1].into()]).into();
|
|||
|
|||
if path.len() != 2 {
|
|||
for path_hash in path.iter().skip(2) {
|
|||
computed_hashes.push(comp_hash);
|
|||
pos /= 2;
|
|||
comp_hash = calculate_parent_hash(comp_hash, pos, *path_hash);
|
|||
}
|
|||
}
|
|||
|
|||
(computed_hashes, comp_hash)
|
|||
}
|
|||
|
|||
/// Returns hash of the root
|
|||
fn compute_path_root(path: &[Word], depth: u32, index: u64) -> Word {
|
|||
let mut pos = 2u64.pow(depth) + index;
|
|||
|
|||
// hash that is obtained after calculating the current hash and path hash
|
|||
let mut comp_hash = Rpo256::merge(&[path[0].into(), path[1].into()]).into();
|
|||
|
|||
for path_hash in path.iter().skip(2) {
|
|||
pos /= 2;
|
|||
comp_hash = calculate_parent_hash(comp_hash, pos, *path_hash);
|
|||
}
|
|||
|
|||
comp_hash
|
|||
}
|
|||
|
|||
// TESTS
|
|||
// ================================================================================================
|
|||
|
|||
#[cfg(test)]
|
|||
mod tests {
|
|||
use super::calculate_parent_hash;
|
|||
use crate::merkle::int_to_node;
|
|||
|
|||
#[test]
|
|||
fn get_root() {
|
|||
let leaf0 = int_to_node(0);
|
|||
let leaf1 = int_to_node(1);
|
|||
let leaf2 = int_to_node(2);
|
|||
let leaf3 = int_to_node(3);
|
|||
|
|||
let parent0 = calculate_parent_hash(leaf0, 0, leaf1);
|
|||
let parent1 = calculate_parent_hash(leaf2, 2, leaf3);
|
|||
|
|||
let root_exp = calculate_parent_hash(parent0, 0, parent1);
|
|||
|
|||
let mut set = super::MerklePathSet::new(3).unwrap();
|
|||
|
|||
set.add_path(0, leaf0, vec![leaf1, parent1]).unwrap();
|
|||
|
|||
assert_eq!(set.root(), root_exp);
|
|||
}
|
|||
|
|||
#[test]
|
|||
fn add_and_get_path() {
|
|||
let path_6 = vec![int_to_node(7), int_to_node(45), int_to_node(123)];
|
|||
let hash_6 = int_to_node(6);
|
|||
let index = 6u64;
|
|||
let depth = 4u32;
|
|||
let mut set = super::MerklePathSet::new(depth).unwrap();
|
|||
|
|||
set.add_path(index, hash_6, path_6.clone()).unwrap();
|
|||
let stored_path_6 = set.get_path(depth, index).unwrap();
|
|||
|
|||
assert_eq!(path_6, stored_path_6);
|
|||
assert!(set.get_path(depth, 15u64).is_err())
|
|||
}
|
|||
|
|||
#[test]
|
|||
fn get_node() {
|
|||
let path_6 = vec![int_to_node(7), int_to_node(45), int_to_node(123)];
|
|||
let hash_6 = int_to_node(6);
|
|||
let index = 6u64;
|
|||
let depth = 4u32;
|
|||
let mut set = super::MerklePathSet::new(depth).unwrap();
|
|||
|
|||
set.add_path(index, hash_6, path_6).unwrap();
|
|||
|
|||
assert_eq!(int_to_node(6u64), set.get_node(depth, index).unwrap());
|
|||
assert!(set.get_node(depth, 15u64).is_err());
|
|||
}
|
|||
|
|||
#[test]
|
|||
fn update_leaf() {
|
|||
let hash_4 = int_to_node(4);
|
|||
let hash_5 = int_to_node(5);
|
|||
let hash_6 = int_to_node(6);
|
|||
let hash_7 = int_to_node(7);
|
|||
let hash_45 = calculate_parent_hash(hash_4, 12u64, hash_5);
|
|||
let hash_67 = calculate_parent_hash(hash_6, 14u64, hash_7);
|
|||
|
|||
let hash_0123 = int_to_node(123);
|
|||
|
|||
let path_6 = vec![hash_7, hash_45, hash_0123];
|
|||
let path_5 = vec![hash_4, hash_67, hash_0123];
|
|||
let path_4 = vec![hash_5, hash_67, hash_0123];
|
|||
|
|||
let index_6 = 6u64;
|
|||
let index_5 = 5u64;
|
|||
let index_4 = 4u64;
|
|||
let depth = 4u32;
|
|||
let mut set = super::MerklePathSet::new(depth).unwrap();
|
|||
|
|||
set.add_path(index_6, hash_6, path_6).unwrap();
|
|||
set.add_path(index_5, hash_5, path_5).unwrap();
|
|||
set.add_path(index_4, hash_4, path_4).unwrap();
|
|||
|
|||
let new_hash_6 = int_to_node(100);
|
|||
let new_hash_5 = int_to_node(55);
|
|||
|
|||
set.update_leaf(index_6, new_hash_6).unwrap();
|
|||
let new_path_4 = set.get_path(depth, index_4).unwrap();
|
|||
let new_hash_67 = calculate_parent_hash(new_hash_6, 14u64, hash_7);
|
|||
assert_eq!(new_hash_67, new_path_4[1]);
|
|||
|
|||
set.update_leaf(index_5, new_hash_5).unwrap();
|
|||
let new_path_4 = set.get_path(depth, index_4).unwrap();
|
|||
let new_path_6 = set.get_path(depth, index_6).unwrap();
|
|||
let new_hash_45 = calculate_parent_hash(new_hash_5, 13u64, hash_4);
|
|||
assert_eq!(new_hash_45, new_path_6[1]);
|
|||
assert_eq!(new_hash_5, new_path_4[0]);
|
|||
}
|
|||
}
|
@ -0,0 +1,84 @@ |
|||
use super::{vec, NodeIndex, Rpo256, Vec, Word};
|
|||
use core::ops::{Deref, DerefMut};
|
|||
|
|||
// 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
|
|||
// --------------------------------------------------------------------------------------------
|
|||
|
|||
/// Computes the merkle root for this opening.
|
|||
pub fn compute_root(&self, index_value: u64, node: Word) -> Word {
|
|||
let mut index = NodeIndex::new(self.depth(), index_value);
|
|||
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());
|
|||
index.move_up();
|
|||
Rpo256::merge(&input).into()
|
|||
})
|
|||
}
|
|||
|
|||
/// Returns the depth in which this Merkle path proof is valid.
|
|||
pub fn depth(&self) -> u8 {
|
|||
self.nodes.len() as u8
|
|||
}
|
|||
|
|||
/// 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 {
|
|||
root == &self.compute_root(index, node)
|
|||
}
|
|||
}
|
|||
|
|||
impl From<Vec<Word>> for MerklePath {
|
|||
fn from(path: Vec<Word>) -> Self {
|
|||
Self::new(path)
|
|||
}
|
|||
}
|
|||
|
|||
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>;
|
|||
|
|||
fn deref(&self) -> &Self::Target {
|
|||
&self.nodes
|
|||
}
|
|||
}
|
|||
|
|||
impl DerefMut for MerklePath {
|
|||
fn deref_mut(&mut self) -> &mut Self::Target {
|
|||
&mut 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()
|
|||
}
|
|||
}
|
@ -0,0 +1,333 @@ |
|||
use super::{BTreeMap, MerkleError, MerklePath, NodeIndex, Rpo256, Vec, Word, ZERO};
|
|||
|
|||
// MERKLE PATH SET
|
|||
// ================================================================================================
|
|||
|
|||
/// A set of Merkle paths.
|
|||
#[derive(Debug, Clone, PartialEq, Eq)]
|
|||
pub struct MerklePathSet {
|
|||
root: Word,
|
|||
total_depth: u8,
|
|||
paths: BTreeMap<u64, MerklePath>,
|
|||
}
|
|||
|
|||
impl MerklePathSet {
|
|||
// CONSTRUCTOR
|
|||
// --------------------------------------------------------------------------------------------
|
|||
|
|||
/// Returns an empty MerklePathSet.
|
|||
pub fn new(depth: u8) -> Result<Self, MerkleError> {
|
|||
let root = [ZERO; 4];
|
|||
let paths = BTreeMap::new();
|
|||
|
|||
Ok(Self {
|
|||
root,
|
|||
total_depth: depth,
|
|||
paths,
|
|||
})
|
|||
}
|
|||
|
|||
// PUBLIC ACCESSORS
|
|||
// --------------------------------------------------------------------------------------------
|
|||
|
|||
/// Returns the root to which all paths in this set resolve.
|
|||
pub const fn root(&self) -> Word {
|
|||
self.root
|
|||
}
|
|||
|
|||
/// Returns the depth of the Merkle tree implied by the paths stored in this set.
|
|||
///
|
|||
/// Merkle tree of depth 1 has two leaves, depth 2 has four leaves etc.
|
|||
pub const fn depth(&self) -> u8 {
|
|||
self.total_depth
|
|||
}
|
|||
|
|||
/// Returns a node at the specified index.
|
|||
///
|
|||
/// # Errors
|
|||
/// Returns an error if:
|
|||
/// * The specified index is not valid for the depth of structure.
|
|||
/// * Requested node does not exist in the set.
|
|||
pub fn get_node(&self, index: NodeIndex) -> Result<Word, MerkleError> {
|
|||
if !index.with_depth(self.total_depth).is_valid() {
|
|||
return Err(MerkleError::InvalidIndex(
|
|||
index.with_depth(self.total_depth),
|
|||
));
|
|||
}
|
|||
if index.depth() != self.total_depth {
|
|||
return Err(MerkleError::InvalidDepth {
|
|||
expected: self.total_depth,
|
|||
provided: index.depth(),
|
|||
});
|
|||
}
|
|||
|
|||
let index_value = index.to_scalar_index();
|
|||
let parity = index_value & 1;
|
|||
let index_value = index_value / 2;
|
|||
self.paths
|
|||
.get(&index_value)
|
|||
.ok_or(MerkleError::NodeNotInSet(index_value))
|
|||
.map(|path| path[parity as usize])
|
|||
}
|
|||
|
|||
/// Returns a Merkle path to the node at the specified index. The node itself is
|
|||
/// not included in the path.
|
|||
///
|
|||
/// # Errors
|
|||
/// Returns an error if:
|
|||
/// * The specified index is not valid for the depth of structure.
|
|||
/// * Node of the requested path does not exist in the set.
|
|||
pub fn get_path(&self, index: NodeIndex) -> Result<MerklePath, MerkleError> {
|
|||
if !index.with_depth(self.total_depth).is_valid() {
|
|||
return Err(MerkleError::InvalidIndex(index));
|
|||
}
|
|||
if index.depth() != self.total_depth {
|
|||
return Err(MerkleError::InvalidDepth {
|
|||
expected: self.total_depth,
|
|||
provided: index.depth(),
|
|||
});
|
|||
}
|
|||
|
|||
let index_value = index.to_scalar_index();
|
|||
let index = index_value / 2;
|
|||
let parity = index_value & 1;
|
|||
let mut path = self
|
|||
.paths
|
|||
.get(&index)
|
|||
.cloned()
|
|||
.ok_or(MerkleError::NodeNotInSet(index))?;
|
|||
path.remove(parity as usize);
|
|||
Ok(path)
|
|||
}
|
|||
|
|||
// STATE MUTATORS
|
|||
// --------------------------------------------------------------------------------------------
|
|||
|
|||
/// Adds the specified Merkle path to this [MerklePathSet]. The `index` and `value` parameters
|
|||
/// specify the leaf node at which the path starts.
|
|||
///
|
|||
/// # Errors
|
|||
/// Returns an error if:
|
|||
/// - The specified index is is not valid in the context of this Merkle path set (i.e., the
|
|||
/// index implies a greater depth than is specified for this set).
|
|||
/// - The specified path is not consistent with other paths in the set (i.e., resolves to a
|
|||
/// different root).
|
|||
pub fn add_path(
|
|||
&mut self,
|
|||
index_value: u64,
|
|||
value: Word,
|
|||
mut path: MerklePath,
|
|||
) -> Result<(), MerkleError> {
|
|||
let depth = (path.len() + 1) as u8;
|
|||
let mut index = NodeIndex::new(depth, index_value);
|
|||
if index.depth() != self.total_depth {
|
|||
return Err(MerkleError::InvalidDepth {
|
|||
expected: self.total_depth,
|
|||
provided: index.depth(),
|
|||
});
|
|||
}
|
|||
|
|||
// update the current path
|
|||
let index_value = index.to_scalar_index();
|
|||
let upper_index_value = index_value / 2;
|
|||
let parity = index_value & 1;
|
|||
path.insert(parity as usize, value);
|
|||
|
|||
// traverse to the root, updating the nodes
|
|||
let root: Word = Rpo256::merge(&[path[0].into(), path[1].into()]).into();
|
|||
let root = path.iter().skip(2).copied().fold(root, |root, hash| {
|
|||
index.move_up();
|
|||
Rpo256::merge(&index.build_node(root.into(), hash.into())).into()
|
|||
});
|
|||
|
|||
// if the path set is empty (the root is all ZEROs), set the root to the root of the added
|
|||
// path; otherwise, the root of the added path must be identical to the current root
|
|||
if self.root == [ZERO; 4] {
|
|||
self.root = root;
|
|||
} else if self.root != root {
|
|||
return Err(MerkleError::InvalidPath(path));
|
|||
}
|
|||
|
|||
// finish updating the path
|
|||
self.paths.insert(upper_index_value, path);
|
|||
Ok(())
|
|||
}
|
|||
|
|||
/// Replaces the leaf at the specified index with the provided value.
|
|||
///
|
|||
/// # Errors
|
|||
/// Returns an error if:
|
|||
/// * Requested node does not exist in the set.
|
|||
pub fn update_leaf(&mut self, base_index_value: u64, value: Word) -> Result<(), MerkleError> {
|
|||
let depth = self.depth();
|
|||
let mut index = NodeIndex::new(depth, base_index_value);
|
|||
if !index.is_valid() {
|
|||
return Err(MerkleError::InvalidIndex(index));
|
|||
}
|
|||
|
|||
let path = match self
|
|||
.paths
|
|||
.get_mut(&index.clone().move_up().to_scalar_index())
|
|||
{
|
|||
Some(path) => path,
|
|||
None => return Err(MerkleError::NodeNotInSet(base_index_value)),
|
|||
};
|
|||
|
|||
// Fill old_hashes vector -----------------------------------------------------------------
|
|||
let mut current_index = index;
|
|||
let mut old_hashes = Vec::with_capacity(path.len().saturating_sub(2));
|
|||
let mut root: Word = Rpo256::merge(&[path[0].into(), path[1].into()]).into();
|
|||
for hash in path.iter().skip(2).copied() {
|
|||
old_hashes.push(root);
|
|||
current_index.move_up();
|
|||
let input = current_index.build_node(hash.into(), root.into());
|
|||
root = Rpo256::merge(&input).into();
|
|||
}
|
|||
|
|||
// Fill new_hashes vector -----------------------------------------------------------------
|
|||
path[index.is_value_odd() as usize] = value;
|
|||
|
|||
let mut new_hashes = Vec::with_capacity(path.len().saturating_sub(2));
|
|||
let mut new_root: Word = Rpo256::merge(&[path[0].into(), path[1].into()]).into();
|
|||
for path_hash in path.iter().skip(2).copied() {
|
|||
new_hashes.push(new_root);
|
|||
index.move_up();
|
|||
let input = current_index.build_node(path_hash.into(), new_root.into());
|
|||
new_root = Rpo256::merge(&input).into();
|
|||
}
|
|||
|
|||
self.root = new_root;
|
|||
|
|||
// update paths ---------------------------------------------------------------------------
|
|||
for path in self.paths.values_mut() {
|
|||
for i in (0..old_hashes.len()).rev() {
|
|||
if path[i + 2] == old_hashes[i] {
|
|||
path[i + 2] = new_hashes[i];
|
|||
break;
|
|||
}
|
|||
}
|
|||
}
|
|||
|
|||
Ok(())
|
|||
}
|
|||
}
|
|||
|
|||
// TESTS
|
|||
// ================================================================================================
|
|||
|
|||
#[cfg(test)]
|
|||
mod tests {
|
|||
use super::*;
|
|||
use crate::merkle::int_to_node;
|
|||
|
|||
#[test]
|
|||
fn get_root() {
|
|||
let leaf0 = int_to_node(0);
|
|||
let leaf1 = int_to_node(1);
|
|||
let leaf2 = int_to_node(2);
|
|||
let leaf3 = int_to_node(3);
|
|||
|
|||
let parent0 = calculate_parent_hash(leaf0, 0, leaf1);
|
|||
let parent1 = calculate_parent_hash(leaf2, 2, leaf3);
|
|||
|
|||
let root_exp = calculate_parent_hash(parent0, 0, parent1);
|
|||
|
|||
let mut set = super::MerklePathSet::new(3).unwrap();
|
|||
|
|||
set.add_path(0, leaf0, vec![leaf1, parent1].into()).unwrap();
|
|||
|
|||
assert_eq!(set.root(), root_exp);
|
|||
}
|
|||
|
|||
#[test]
|
|||
fn add_and_get_path() {
|
|||
let path_6 = vec![int_to_node(7), int_to_node(45), int_to_node(123)];
|
|||
let hash_6 = int_to_node(6);
|
|||
let index = 6_u64;
|
|||
let depth = 4_u8;
|
|||
let mut set = super::MerklePathSet::new(depth).unwrap();
|
|||
|
|||
set.add_path(index, hash_6, path_6.clone().into()).unwrap();
|
|||
let stored_path_6 = set.get_path(NodeIndex::new(depth, index)).unwrap();
|
|||
|
|||
assert_eq!(path_6, *stored_path_6);
|
|||
assert!(set.get_path(NodeIndex::new(depth, 15_u64)).is_err())
|
|||
}
|
|||
|
|||
#[test]
|
|||
fn get_node() {
|
|||
let path_6 = vec![int_to_node(7), int_to_node(45), int_to_node(123)];
|
|||
let hash_6 = int_to_node(6);
|
|||
let index = 6_u64;
|
|||
let depth = 4_u8;
|
|||
let mut set = MerklePathSet::new(depth).unwrap();
|
|||
|
|||
set.add_path(index, hash_6, path_6.into()).unwrap();
|
|||
|
|||
assert_eq!(
|
|||
int_to_node(6u64),
|
|||
set.get_node(NodeIndex::new(depth, index)).unwrap()
|
|||
);
|
|||
assert!(set.get_node(NodeIndex::new(depth, 15_u64)).is_err());
|
|||
}
|
|||
|
|||
#[test]
|
|||
fn update_leaf() {
|
|||
let hash_4 = int_to_node(4);
|
|||
let hash_5 = int_to_node(5);
|
|||
let hash_6 = int_to_node(6);
|
|||
let hash_7 = int_to_node(7);
|
|||
let hash_45 = calculate_parent_hash(hash_4, 12u64, hash_5);
|
|||
let hash_67 = calculate_parent_hash(hash_6, 14u64, hash_7);
|
|||
|
|||
let hash_0123 = int_to_node(123);
|
|||
|
|||
let path_6 = vec![hash_7, hash_45, hash_0123];
|
|||
let path_5 = vec![hash_4, hash_67, hash_0123];
|
|||
let path_4 = vec![hash_5, hash_67, hash_0123];
|
|||
|
|||
let index_6 = 6_u64;
|
|||
let index_5 = 5_u64;
|
|||
let index_4 = 4_u64;
|
|||
let depth = 4_u8;
|
|||
let mut set = MerklePathSet::new(depth).unwrap();
|
|||
|
|||
set.add_path(index_6, hash_6, path_6.into()).unwrap();
|
|||
set.add_path(index_5, hash_5, path_5.into()).unwrap();
|
|||
set.add_path(index_4, hash_4, path_4.into()).unwrap();
|
|||
|
|||
let new_hash_6 = int_to_node(100);
|
|||
let new_hash_5 = int_to_node(55);
|
|||
|
|||
set.update_leaf(index_6, new_hash_6).unwrap();
|
|||
let new_path_4 = set.get_path(NodeIndex::new(depth, index_4)).unwrap();
|
|||
let new_hash_67 = calculate_parent_hash(new_hash_6, 14_u64, hash_7);
|
|||
assert_eq!(new_hash_67, new_path_4[1]);
|
|||
|
|||
set.update_leaf(index_5, new_hash_5).unwrap();
|
|||
let new_path_4 = set.get_path(NodeIndex::new(depth, index_4)).unwrap();
|
|||
let new_path_6 = set.get_path(NodeIndex::new(depth, index_6)).unwrap();
|
|||
let new_hash_45 = calculate_parent_hash(new_hash_5, 13_u64, hash_4);
|
|||
assert_eq!(new_hash_45, new_path_6[1]);
|
|||
assert_eq!(new_hash_5, new_path_4[0]);
|
|||
}
|
|||
|
|||
// HELPER FUNCTIONS
|
|||
// --------------------------------------------------------------------------------------------
|
|||
|
|||
const fn is_even(pos: u64) -> bool {
|
|||
pos & 1 == 0
|
|||
}
|
|||
|
|||
/// Calculates the hash of the parent node by two sibling ones
|
|||
/// - node — current node
|
|||
/// - node_pos — position of the current node
|
|||
/// - sibling — neighboring vertex in the tree
|
|||
fn calculate_parent_hash(node: Word, node_pos: u64, sibling: Word) -> Word {
|
|||
if is_even(node_pos) {
|
|||
Rpo256::merge(&[node.into(), sibling.into()]).into()
|
|||
} else {
|
|||
Rpo256::merge(&[sibling.into(), node.into()]).into()
|
|||
}
|
|||
}
|
|||
}
|