mirror of
https://github.com/arnaucube/miden-crypto.git
synced 2026-01-12 09:01:29 +01:00
fix: compilation errors
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
use super::{
|
||||
BTreeMap, BTreeSet, MerkleError, MerklePath, NodeIndex, Rpo256, RpoDigest, ValuePath, Vec,
|
||||
Word, EMPTY_WORD,
|
||||
BTreeMap, BTreeSet, MerkleError, MerklePath, NodeIndex, Rpo256, RpoDigest, ValuePath, Vec, ZERO,
|
||||
};
|
||||
use crate::utils::{format, string::String, word_to_hex};
|
||||
use core::fmt;
|
||||
@@ -15,7 +14,7 @@ mod tests;
|
||||
const ROOT_INDEX: NodeIndex = NodeIndex::root();
|
||||
|
||||
/// An RpoDigest consisting of 4 ZERO elements.
|
||||
const EMPTY_DIGEST: RpoDigest = RpoDigest::new(EMPTY_WORD);
|
||||
const EMPTY_DIGEST: RpoDigest = RpoDigest::new([ZERO; 4]);
|
||||
|
||||
// PARTIAL MERKLE TREE
|
||||
// ================================================================================================
|
||||
@@ -50,7 +49,7 @@ impl PartialMerkleTree {
|
||||
// CONSTRUCTORS
|
||||
// --------------------------------------------------------------------------------------------
|
||||
|
||||
/// Returns a new emply [PartialMerkleTree].
|
||||
/// Returns a new empty [PartialMerkleTree].
|
||||
pub fn new() -> Self {
|
||||
PartialMerkleTree {
|
||||
max_depth: 0,
|
||||
@@ -108,7 +107,7 @@ impl PartialMerkleTree {
|
||||
paths.push((
|
||||
leaf,
|
||||
ValuePath {
|
||||
value: *self.get_node(leaf).expect("Failed to get leaf node"),
|
||||
value: self.get_node(leaf).expect("Failed to get leaf node"),
|
||||
path: self.get_path(leaf).expect("Failed to get path"),
|
||||
},
|
||||
));
|
||||
@@ -142,7 +141,7 @@ impl PartialMerkleTree {
|
||||
index.move_up();
|
||||
let sibling =
|
||||
self.nodes.get(&sibling_index).cloned().expect("Sibling node not in the map");
|
||||
path.push(Word::from(sibling));
|
||||
path.push(sibling);
|
||||
}
|
||||
Ok(MerklePath::new(path))
|
||||
}
|
||||
@@ -189,11 +188,11 @@ impl PartialMerkleTree {
|
||||
|
||||
// add provided node and its sibling to the nodes map
|
||||
self.nodes.insert(index_value, value);
|
||||
self.nodes.insert(sibling_node_index, path[0].into());
|
||||
self.nodes.insert(sibling_node_index, path[0]);
|
||||
|
||||
// traverse to the root, updating the nodes
|
||||
let mut index_value = index_value;
|
||||
let node = Rpo256::merge(&index_value.build_node(value, path[0].into()));
|
||||
let node = Rpo256::merge(&index_value.build_node(value, path[0]));
|
||||
let root = path.iter().skip(1).copied().fold(node, |node, hash| {
|
||||
index_value.move_up();
|
||||
// insert calculated node to the nodes map
|
||||
@@ -215,11 +214,11 @@ impl PartialMerkleTree {
|
||||
// - New node can be a calculated node or a "sibling" node from a Merkle Path:
|
||||
// --- Calculated node, obviously, never can be a leaf.
|
||||
// --- Sibling node can be only a leaf, because otherwise it is not a new node.
|
||||
if self.nodes.insert(sibling_node, hash.into()).is_none() {
|
||||
if self.nodes.insert(sibling_node, hash).is_none() {
|
||||
self.leaves.insert(sibling_node);
|
||||
}
|
||||
|
||||
Rpo256::merge(&index_value.build_node(node, hash.into()))
|
||||
Rpo256::merge(&index_value.build_node(node, hash))
|
||||
});
|
||||
|
||||
// if the path set is empty (the root is all ZEROs), set the root to the root of the added
|
||||
@@ -227,7 +226,7 @@ impl PartialMerkleTree {
|
||||
if self.root() == EMPTY_DIGEST {
|
||||
self.nodes.insert(ROOT_INDEX, root);
|
||||
} else if self.root() != root {
|
||||
return Err(MerkleError::ConflictingRoots([*self.root(), *root].to_vec()));
|
||||
return Err(MerkleError::ConflictingRoots([self.root(), root].to_vec()));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::{
|
||||
super::{int_to_node, MerkleStore, MerkleTree, NodeIndex, PartialMerkleTree},
|
||||
ValuePath, Vec, Word,
|
||||
super::{digests_to_words, int_to_node, MerkleStore, MerkleTree, NodeIndex, PartialMerkleTree},
|
||||
RpoDigest, ValuePath, Vec,
|
||||
};
|
||||
|
||||
// TEST DATA
|
||||
@@ -18,7 +18,7 @@ const NODE31: NodeIndex = NodeIndex::new_unchecked(3, 1);
|
||||
const NODE32: NodeIndex = NodeIndex::new_unchecked(3, 2);
|
||||
const NODE33: NodeIndex = NodeIndex::new_unchecked(3, 3);
|
||||
|
||||
const VALUES8: [Word; 8] = [
|
||||
const VALUES8: [RpoDigest; 8] = [
|
||||
int_to_node(30),
|
||||
int_to_node(31),
|
||||
int_to_node(32),
|
||||
@@ -44,22 +44,21 @@ const VALUES8: [Word; 8] = [
|
||||
// (30) (31) (32) (33) (34) (35) (36) (37)
|
||||
//
|
||||
// Where node number is a concatenation of its depth and index. For example, node with
|
||||
// NodeIndex(3, 5) will be labled as `35`. Leaves of the tree are shown as nodes with parenthesis
|
||||
// NodeIndex(3, 5) will be labeled as `35`. Leaves of the tree are shown as nodes with parenthesis
|
||||
// (33).
|
||||
|
||||
/// Checks that root returned by `root()` function is equal to the expected one.
|
||||
#[test]
|
||||
fn get_root() {
|
||||
let mt = MerkleTree::new(VALUES8.to_vec()).unwrap();
|
||||
let mt = MerkleTree::new(digests_to_words(&VALUES8)).unwrap();
|
||||
let expected_root = mt.root();
|
||||
|
||||
let ms = MerkleStore::from(&mt);
|
||||
|
||||
let path33 = ms.get_path(expected_root, NODE33).unwrap();
|
||||
|
||||
let pmt = PartialMerkleTree::with_paths([(3, path33.value.into(), path33.path)]).unwrap();
|
||||
let pmt = PartialMerkleTree::with_paths([(3, path33.value, path33.path)]).unwrap();
|
||||
|
||||
assert_eq!(pmt.root(), expected_root.into());
|
||||
assert_eq!(pmt.root(), expected_root);
|
||||
}
|
||||
|
||||
/// This test checks correctness of the `add_path()` and `get_path()` functions. First it creates a
|
||||
@@ -67,7 +66,7 @@ fn get_root() {
|
||||
/// it checks that paths returned by `get_path()` function are equal to the expected ones.
|
||||
#[test]
|
||||
fn add_and_get_paths() {
|
||||
let mt = MerkleTree::new(VALUES8.to_vec()).unwrap();
|
||||
let mt = MerkleTree::new(digests_to_words(&VALUES8)).unwrap();
|
||||
let expected_root = mt.root();
|
||||
|
||||
let ms = MerkleStore::from(&mt);
|
||||
@@ -76,10 +75,8 @@ fn add_and_get_paths() {
|
||||
let expected_path22 = ms.get_path(expected_root, NODE22).unwrap();
|
||||
|
||||
let mut pmt = PartialMerkleTree::new();
|
||||
pmt.add_path(3, expected_path33.value.into(), expected_path33.path.clone())
|
||||
.unwrap();
|
||||
pmt.add_path(2, expected_path22.value.into(), expected_path22.path.clone())
|
||||
.unwrap();
|
||||
pmt.add_path(3, expected_path33.value, expected_path33.path.clone()).unwrap();
|
||||
pmt.add_path(2, expected_path22.value, expected_path22.path.clone()).unwrap();
|
||||
|
||||
let path33 = pmt.get_path(NODE33).unwrap();
|
||||
let path22 = pmt.get_path(NODE22).unwrap();
|
||||
@@ -87,58 +84,58 @@ fn add_and_get_paths() {
|
||||
|
||||
assert_eq!(expected_path33.path, path33);
|
||||
assert_eq!(expected_path22.path, path22);
|
||||
assert_eq!(expected_root, *actual_root);
|
||||
assert_eq!(expected_root, actual_root);
|
||||
}
|
||||
|
||||
/// Checks that function `get_node` used on nodes 10 and 32 returns expected values.
|
||||
#[test]
|
||||
fn get_node() {
|
||||
let mt = MerkleTree::new(VALUES8.to_vec()).unwrap();
|
||||
let mt = MerkleTree::new(digests_to_words(&VALUES8)).unwrap();
|
||||
let expected_root = mt.root();
|
||||
|
||||
let ms = MerkleStore::from(&mt);
|
||||
|
||||
let path33 = ms.get_path(expected_root, NODE33).unwrap();
|
||||
|
||||
let pmt = PartialMerkleTree::with_paths([(3, path33.value.into(), path33.path)]).unwrap();
|
||||
let pmt = PartialMerkleTree::with_paths([(3, path33.value, path33.path)]).unwrap();
|
||||
|
||||
assert_eq!(ms.get_node(expected_root, NODE32).unwrap(), *pmt.get_node(NODE32).unwrap());
|
||||
assert_eq!(ms.get_node(expected_root, NODE10).unwrap(), *pmt.get_node(NODE10).unwrap());
|
||||
assert_eq!(ms.get_node(expected_root, NODE32).unwrap(), pmt.get_node(NODE32).unwrap());
|
||||
assert_eq!(ms.get_node(expected_root, NODE10).unwrap(), pmt.get_node(NODE10).unwrap());
|
||||
}
|
||||
|
||||
/// Updates leaves of the PMT using `update_leaf()` function and checks that new root of the tree
|
||||
/// is equal to the expected one.
|
||||
#[test]
|
||||
fn update_leaf() {
|
||||
let mt = MerkleTree::new(VALUES8.to_vec()).unwrap();
|
||||
let mt = MerkleTree::new(digests_to_words(&VALUES8)).unwrap();
|
||||
let root = mt.root();
|
||||
|
||||
let mut ms = MerkleStore::from(&mt);
|
||||
let path33 = ms.get_path(root, NODE33).unwrap();
|
||||
|
||||
let mut pmt = PartialMerkleTree::with_paths([(3, path33.value.into(), path33.path)]).unwrap();
|
||||
let mut pmt = PartialMerkleTree::with_paths([(3, path33.value, path33.path)]).unwrap();
|
||||
|
||||
let new_value32 = int_to_node(132);
|
||||
let expected_root = ms.set_node(root, NODE32, new_value32).unwrap().root;
|
||||
|
||||
pmt.update_leaf(NODE32, new_value32.into()).unwrap();
|
||||
pmt.update_leaf(NODE32, new_value32).unwrap();
|
||||
let actual_root = pmt.root();
|
||||
|
||||
assert_eq!(expected_root, *actual_root);
|
||||
assert_eq!(expected_root, actual_root);
|
||||
|
||||
let new_value20 = int_to_node(120);
|
||||
let expected_root = ms.set_node(expected_root, NODE20, new_value20).unwrap().root;
|
||||
|
||||
pmt.update_leaf(NODE20, new_value20.into()).unwrap();
|
||||
pmt.update_leaf(NODE20, new_value20).unwrap();
|
||||
let actual_root = pmt.root();
|
||||
|
||||
assert_eq!(expected_root, *actual_root);
|
||||
assert_eq!(expected_root, actual_root);
|
||||
}
|
||||
|
||||
/// Checks that paths of the PMT returned by `paths()` function are equal to the expected ones.
|
||||
#[test]
|
||||
fn get_paths() {
|
||||
let mt = MerkleTree::new(VALUES8.to_vec()).unwrap();
|
||||
let mt = MerkleTree::new(digests_to_words(&VALUES8)).unwrap();
|
||||
let expected_root = mt.root();
|
||||
|
||||
let ms = MerkleStore::from(&mt);
|
||||
@@ -147,8 +144,8 @@ fn get_paths() {
|
||||
let path22 = ms.get_path(expected_root, NODE22).unwrap();
|
||||
|
||||
let mut pmt = PartialMerkleTree::new();
|
||||
pmt.add_path(3, path33.value.into(), path33.path.clone()).unwrap();
|
||||
pmt.add_path(2, path22.value.into(), path22.path.clone()).unwrap();
|
||||
pmt.add_path(3, path33.value, path33.path).unwrap();
|
||||
pmt.add_path(2, path22.value, path22.path).unwrap();
|
||||
// After PMT creation with path33 (33; 32, 20, 11) and path22 (22; 23, 10) we will have this
|
||||
// tree:
|
||||
//
|
||||
@@ -170,7 +167,7 @@ fn get_paths() {
|
||||
(
|
||||
leaf,
|
||||
ValuePath {
|
||||
value: mt.get_node(leaf).unwrap().into(),
|
||||
value: mt.get_node(leaf).unwrap(),
|
||||
path: mt.get_path(leaf).unwrap(),
|
||||
},
|
||||
)
|
||||
@@ -185,7 +182,7 @@ fn get_paths() {
|
||||
// Checks correctness of leaves determination when using the `leaves()` function.
|
||||
#[test]
|
||||
fn leaves() {
|
||||
let mt = MerkleTree::new(VALUES8.to_vec()).unwrap();
|
||||
let mt = MerkleTree::new(digests_to_words(&VALUES8)).unwrap();
|
||||
let expected_root = mt.root();
|
||||
|
||||
let ms = MerkleStore::from(&mt);
|
||||
@@ -193,7 +190,7 @@ fn leaves() {
|
||||
let path33 = ms.get_path(expected_root, NODE33).unwrap();
|
||||
let path22 = ms.get_path(expected_root, NODE22).unwrap();
|
||||
|
||||
let mut pmt = PartialMerkleTree::with_paths([(3, path33.value.into(), path33.path)]).unwrap();
|
||||
let mut pmt = PartialMerkleTree::with_paths([(3, path33.value, path33.path)]).unwrap();
|
||||
// After PMT creation with path33 (33; 32, 20, 11) we will have this tree:
|
||||
//
|
||||
// ______root______
|
||||
@@ -206,17 +203,17 @@ fn leaves() {
|
||||
//
|
||||
// Which have leaf nodes 11, 20, 32 and 33.
|
||||
|
||||
let value11 = mt.get_node(NODE11).unwrap().into();
|
||||
let value20 = mt.get_node(NODE20).unwrap().into();
|
||||
let value32 = mt.get_node(NODE32).unwrap().into();
|
||||
let value33 = mt.get_node(NODE33).unwrap().into();
|
||||
let value11 = mt.get_node(NODE11).unwrap();
|
||||
let value20 = mt.get_node(NODE20).unwrap();
|
||||
let value32 = mt.get_node(NODE32).unwrap();
|
||||
let value33 = mt.get_node(NODE33).unwrap();
|
||||
|
||||
let leaves = vec![(NODE11, value11), (NODE20, value20), (NODE32, value32), (NODE33, value33)];
|
||||
|
||||
let expected_leaves = leaves.iter().map(|&tuple| tuple);
|
||||
let expected_leaves = leaves.iter().copied();
|
||||
assert!(expected_leaves.eq(pmt.leaves()));
|
||||
|
||||
pmt.add_path(2, path22.value.into(), path22.path).unwrap();
|
||||
pmt.add_path(2, path22.value, path22.path).unwrap();
|
||||
// After adding the path22 (22; 23, 10) to the existing PMT we will have this tree:
|
||||
//
|
||||
// ______root______
|
||||
@@ -229,11 +226,11 @@ fn leaves() {
|
||||
//
|
||||
// Which have leaf nodes 20, 22, 23, 32 and 33.
|
||||
|
||||
let value20 = mt.get_node(NODE20).unwrap().into();
|
||||
let value22 = mt.get_node(NODE22).unwrap().into();
|
||||
let value23 = mt.get_node(NODE23).unwrap().into();
|
||||
let value32 = mt.get_node(NODE32).unwrap().into();
|
||||
let value33 = mt.get_node(NODE33).unwrap().into();
|
||||
let value20 = mt.get_node(NODE20).unwrap();
|
||||
let value22 = mt.get_node(NODE22).unwrap();
|
||||
let value23 = mt.get_node(NODE23).unwrap();
|
||||
let value32 = mt.get_node(NODE32).unwrap();
|
||||
let value33 = mt.get_node(NODE33).unwrap();
|
||||
|
||||
let leaves = vec![
|
||||
(NODE20, value20),
|
||||
@@ -243,7 +240,7 @@ fn leaves() {
|
||||
(NODE33, value33),
|
||||
];
|
||||
|
||||
let expected_leaves = leaves.iter().map(|&tuple| tuple);
|
||||
let expected_leaves = leaves.iter().copied();
|
||||
assert!(expected_leaves.eq(pmt.leaves()));
|
||||
}
|
||||
|
||||
@@ -254,22 +251,22 @@ fn err_add_path() {
|
||||
let path22 = vec![int_to_node(4), int_to_node(5)].into();
|
||||
|
||||
let mut pmt = PartialMerkleTree::new();
|
||||
pmt.add_path(3, int_to_node(6).into(), path33).unwrap();
|
||||
pmt.add_path(3, int_to_node(6), path33).unwrap();
|
||||
|
||||
assert!(pmt.add_path(2, int_to_node(7).into(), path22).is_err());
|
||||
assert!(pmt.add_path(2, int_to_node(7), path22).is_err());
|
||||
}
|
||||
|
||||
/// Checks that the request of the node which is not in the PMT will cause an error.
|
||||
#[test]
|
||||
fn err_get_node() {
|
||||
let mt = MerkleTree::new(VALUES8.to_vec()).unwrap();
|
||||
let mt = MerkleTree::new(digests_to_words(&VALUES8)).unwrap();
|
||||
let expected_root = mt.root();
|
||||
|
||||
let ms = MerkleStore::from(&mt);
|
||||
|
||||
let path33 = ms.get_path(expected_root, NODE33).unwrap();
|
||||
|
||||
let pmt = PartialMerkleTree::with_paths([(3, path33.value.into(), path33.path)]).unwrap();
|
||||
let pmt = PartialMerkleTree::with_paths([(3, path33.value, path33.path)]).unwrap();
|
||||
|
||||
assert!(pmt.get_node(NODE22).is_err());
|
||||
assert!(pmt.get_node(NODE23).is_err());
|
||||
@@ -280,14 +277,14 @@ fn err_get_node() {
|
||||
/// Checks that the request of the path from the leaf which is not in the PMT will cause an error.
|
||||
#[test]
|
||||
fn err_get_path() {
|
||||
let mt = MerkleTree::new(VALUES8.to_vec()).unwrap();
|
||||
let mt = MerkleTree::new(digests_to_words(&VALUES8)).unwrap();
|
||||
let expected_root = mt.root();
|
||||
|
||||
let ms = MerkleStore::from(&mt);
|
||||
|
||||
let path33 = ms.get_path(expected_root, NODE33).unwrap();
|
||||
|
||||
let pmt = PartialMerkleTree::with_paths([(3, path33.value.into(), path33.path)]).unwrap();
|
||||
let pmt = PartialMerkleTree::with_paths([(3, path33.value, path33.path)]).unwrap();
|
||||
|
||||
assert!(pmt.get_path(NODE22).is_err());
|
||||
assert!(pmt.get_path(NODE23).is_err());
|
||||
@@ -297,17 +294,17 @@ fn err_get_path() {
|
||||
|
||||
#[test]
|
||||
fn err_update_leaf() {
|
||||
let mt = MerkleTree::new(VALUES8.to_vec()).unwrap();
|
||||
let mt = MerkleTree::new(digests_to_words(&VALUES8)).unwrap();
|
||||
let expected_root = mt.root();
|
||||
|
||||
let ms = MerkleStore::from(&mt);
|
||||
|
||||
let path33 = ms.get_path(expected_root, NODE33).unwrap();
|
||||
|
||||
let mut pmt = PartialMerkleTree::with_paths([(3, path33.value.into(), path33.path)]).unwrap();
|
||||
let mut pmt = PartialMerkleTree::with_paths([(3, path33.value, path33.path)]).unwrap();
|
||||
|
||||
assert!(pmt.update_leaf(NODE22, int_to_node(22).into()).is_err());
|
||||
assert!(pmt.update_leaf(NODE23, int_to_node(23).into()).is_err());
|
||||
assert!(pmt.update_leaf(NODE30, int_to_node(30).into()).is_err());
|
||||
assert!(pmt.update_leaf(NODE31, int_to_node(31).into()).is_err());
|
||||
assert!(pmt.update_leaf(NODE22, int_to_node(22)).is_err());
|
||||
assert!(pmt.update_leaf(NODE23, int_to_node(23)).is_err());
|
||||
assert!(pmt.update_leaf(NODE30, int_to_node(30)).is_err());
|
||||
assert!(pmt.update_leaf(NODE31, int_to_node(31)).is_err());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user