arnaucube
arnaucube/miden-cryptoPublic Self-hosted repository
fix: compilation errors
Bobbin Threadbare bobbinth@protonmail.com
Tue, 13 Jun 2023 22:43:08 -0700
commit

fe5cac9edc06afe9f8872ebf551b80c0958ab4d3

parent

53d52b8adcc987ba15d8112d2083830a8943dc5b

M src/hash/rpo/tests.rs → src/hash/rpo/tests.rs

@@ -206,7 +206,7 @@ // input length that is prime; hence guaranteed to not be divisible by any choice of chunk

// size. // // this is a preliminary test to the fuzzy-stress of proptest. - Rpo256::hash(&vec![0; 113]); + Rpo256::hash(&[0; 113]); } #[test]

@@ -230,8 +230,8 @@ }

proptest! { #[test] - fn rpo256_wont_panic_with_arbitrary_input(ref vec in any::<Vec<u8>>()) { - Rpo256::hash(&vec); + fn rpo256_wont_panic_with_arbitrary_input(ref bytes in any::<Vec<u8>>()) { + Rpo256::hash(bytes); } }
M src/merkle/index.rs → src/merkle/index.rs

@@ -190,7 +190,7 @@ let mut depth = value.ilog2() as u8;

if value > (1 << depth) { // round up depth += 1; } - NodeIndex::new(depth, value.into()).unwrap() + NodeIndex::new(depth, value).unwrap() } }
M src/merkle/merkle_tree.rs → src/merkle/merkle_tree.rs

@@ -287,7 +287,7 @@ assert_eq!(8, tree.nodes.len());

// leaves were copied correctly for (a, b) in tree.nodes.iter().skip(4).zip(LEAVES4.iter()) { - assert_eq!(*a, RpoDigest::from(*b)); + assert_eq!(a, b); } let (root, node2, node3) = compute_internal_nodes();

@@ -341,7 +341,7 @@ // update one leaf

let value = 3; let new_node = int_to_leaf(9); let mut expected_leaves = digests_to_words(&LEAVES8); - expected_leaves[value as usize] = new_node.into(); + expected_leaves[value as usize] = new_node; let expected_tree = super::MerkleTree::new(expected_leaves.clone()).unwrap(); tree.update_leaf(value, new_node).unwrap();

@@ -408,8 +408,8 @@ let word = [Felt::new(a), Felt::new(b), Felt::new(c), Felt::new(d)];

let digest = RpoDigest::from(word); // assert the addresses are different - let word_ptr = (&word).as_ptr() as *const u8; - let digest_ptr = (&digest).as_ptr() as *const u8; + let word_ptr = word.as_ptr() as *const u8; + let digest_ptr = digest.as_ptr() as *const u8; assert_ne!(word_ptr, digest_ptr); // compare the bytes representation
M src/merkle/mmr/tests.rs → src/merkle/mmr/tests.rs

@@ -439,15 +439,15 @@ }

#[test] fn test_mmr_peaks_hash_odd() { - let peaks: Vec<_> = (0..=17).map(|i| int_to_node(i)).collect(); + let peaks: Vec<_> = (0..=17).map(int_to_node).collect(); let accumulator = MmrPeaks { num_leaves: (1 << peaks.len()) - 1, peaks: peaks.clone(), }; - // odd length bigger than 16 is padded to the next even nubmer - let mut expected_peaks = peaks.clone(); + // odd length bigger than 16 is padded to the next even number + let mut expected_peaks = peaks; expected_peaks.resize(18, RpoDigest::default()); assert_eq!( accumulator.hash_peaks(),

@@ -488,5 +488,5 @@ // HELPER FUNCTIONS

// ================================================================================================ fn digests_to_elements(digests: &[RpoDigest]) -> Vec<Felt> { - digests.iter().flat_map(|v| Word::from(v)).collect() + digests.iter().flat_map(Word::from).collect() }
M src/merkle/partial_mt/mod.rs → src/merkle/partial_mt/mod.rs

@@ -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 @@ /// Index of the root node.

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 @@

// CONSTRUCTORS // -------------------------------------------------------------------------------------------- - /// Returns a new emply [PartialMerkleTree]. + /// Returns a new empty [PartialMerkleTree]. pub fn new() -> Self { PartialMerkleTree { max_depth: 0,

@@ -108,7 +107,7 @@ self.leaves.iter().for_each(|&leaf| {

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 @@ let sibling_index = index.sibling();

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 @@ self.leaves.insert(sibling_node_index);

// 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 @@ // by the call of `self.leaves.remove(&index_value);`

// - 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 @@ // path; otherwise, the root of the added path must be identical to the current root

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(())
M src/merkle/partial_mt/tests.rs → src/merkle/partial_mt/tests.rs

@@ -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 @@ // / \ / \ / \ / \

// (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 @@ /// PMT using `add_path()` by adding Merkle Paths from node 33 and node 22 to the empty PMT. Then

/// 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 @@ let expected_path33 = ms.get_path(expected_root, NODE33).unwrap();

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 @@ let actual_root = pmt.root();

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 @@ let path33 = ms.get_path(expected_root, NODE33).unwrap();

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 @@ .map(|&leaf| {

( 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 @@

// 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 @@

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 @@ // (32) (33)

// // 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 @@ // (32) (33)

// // 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 @@ (NODE32, value32),

(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 @@ let path33 = vec![int_to_node(1), int_to_node(2), int_to_node(3)].into();

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 @@

/// 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 @@ }

#[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()); }
M src/merkle/path_set.rs → src/merkle/path_set.rs

@@ -354,35 +354,35 @@

let m = Rpo256::merge(&[i, j]); let n = Rpo256::merge(&[k, l]); - let root = Rpo256::merge(&[m.into(), n.into()]); + let root = Rpo256::merge(&[m, n]); let mut set = MerklePathSet::new(3); let value = b; let index = 1; let path = MerklePath::new([a, j, n].to_vec()); - set.add_path(index, value.into(), path.clone()).unwrap(); + set.add_path(index, value.into(), path).unwrap(); assert_eq!(*value, set.get_leaf(index).unwrap()); assert_eq!(root, set.root()); let value = e; let index = 4; - let path = MerklePath::new([f.into(), l.into(), m.into()].to_vec()); - set.add_path(index, value.into(), path.clone()).unwrap(); + let path = MerklePath::new([f, l, m].to_vec()); + set.add_path(index, value.into(), path).unwrap(); assert_eq!(*value, set.get_leaf(index).unwrap()); assert_eq!(root, set.root()); let value = a; let index = 0; - let path = MerklePath::new([b.into(), j.into(), n.into()].to_vec()); - set.add_path(index, value.into(), path.clone()).unwrap(); + let path = MerklePath::new([b, j, n].to_vec()); + set.add_path(index, value.into(), path).unwrap(); assert_eq!(*value, set.get_leaf(index).unwrap()); assert_eq!(root, set.root()); let value = h; let index = 7; - let path = MerklePath::new([g.into(), k.into(), m.into()].to_vec()); - set.add_path(index, value.into(), path.clone()).unwrap(); + let path = MerklePath::new([g, k, m].to_vec()); + set.add_path(index, value.into(), path).unwrap(); assert_eq!(*value, set.get_leaf(index).unwrap()); assert_eq!(root, set.root()); }
M src/merkle/simple_smt/tests.rs → src/merkle/simple_smt/tests.rs

@@ -203,7 +203,7 @@ let depth = 3;

let entries = vec![(0, a), (1, b), (4, c), (7, d)]; let tree = SimpleSmt::with_leaves(depth, entries).unwrap(); - assert_eq!(tree.root(), RpoDigest::from(k)); + assert_eq!(tree.root(), k); let cases: Vec<(u8, u64, Vec<RpoDigest>)> = vec![ (3, 0, vec![b.into(), f, j]),
M src/merkle/tiered_smt/tests.rs → src/merkle/tiered_smt/tests.rs

@@ -432,10 +432,10 @@

fn get_non_empty_nodes(store: &MerkleStore) -> Vec<InnerNodeInfo> { store .inner_nodes() - .filter(|node| !is_empty_subtree(&RpoDigest::from(node.value))) + .filter(|node| !is_empty_subtree(&node.value)) .collect::<Vec<_>>() } fn is_empty_subtree(node: &RpoDigest) -> bool { - EmptySubtreeRoots::empty_hashes(255).contains(&node) + EmptySubtreeRoots::empty_hashes(255).contains(node) }