Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add bytemuck::Pod to Hasher::Hash and remove unsafe code. #53

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ once_cell = "1.8"
proptest = { version = "1.0", optional = true }
rand = "0.8.4"
rayon = "1.5.1"
ruint = { version = "=1.11.0", features = ["serde", "num-bigint", "ark-ff"] }
ruint = { version = "=1.11.0", features = [
"serde",
"num-bigint",
"ark-ff",
"bytemuck",
] }
semaphore-depth-config = { path = "crates/semaphore-depth-config" }
serde = "1.0"
sha2 = "0.10.1"
Expand All @@ -83,6 +88,7 @@ bincode = "1.3.3"
# Use the same `ethers-core` version as ark-circom
# TODO: Remove
ethers-core = { git = "https://github.com/gakonst/ethers-rs", default-features = false }
bytemuck = "1.13.1"

[dev-dependencies]
bincode = "1.3.3"
Expand Down
10 changes: 3 additions & 7 deletions src/lazy_merkle_tree.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
merkle_tree::{Branch, Hasher, Proof},
util::as_bytes,

Check warning on line 3 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / Test

unused import: `util::as_bytes`

Check warning on line 3 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `util::as_bytes`

warning: unused import: `util::as_bytes` --> src/lazy_merkle_tree.rs:3:5 | 3 | util::as_bytes, | ^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
};
use std::{
fs::OpenOptions,
Expand Down Expand Up @@ -248,7 +248,7 @@
while current_depth < depth {
result = SparseTree::new(
result,
EmptyTree::new(current_depth, empty_value.clone()).into(),

Check warning on line 251 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:251:47 | 251 | EmptyTree::new(current_depth, empty_value.clone()).into(), | ^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*empty_value` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
)
.into();
current_depth += 1;
Expand All @@ -258,14 +258,14 @@

fn new_with_dense_prefix(depth: usize, prefix_depth: usize, empty_value: &H::Hash) -> Self {
assert!(depth >= prefix_depth);
let mut result: Self = EmptyTree::new(prefix_depth, empty_value.clone())

Check warning on line 261 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:261:61 | 261 | let mut result: Self = EmptyTree::new(prefix_depth, empty_value.clone()) | ^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*empty_value` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
.alloc_dense()
.into();
let mut current_depth = prefix_depth;
while current_depth < depth {
result = SparseTree::new(
result,
EmptyTree::new(current_depth, empty_value.clone()).into(),

Check warning on line 268 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:268:47 | 268 | EmptyTree::new(current_depth, empty_value.clone()).into(), | ^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*empty_value` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
)
.into();
current_depth += 1;
Expand All @@ -288,7 +288,7 @@
while current_depth < depth {
result = SparseTree::new(
result,
EmptyTree::new(current_depth, empty_value.clone()).into(),

Check warning on line 291 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:291:47 | 291 | EmptyTree::new(current_depth, empty_value.clone()).into(), | ^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*empty_value` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
)
.into();
current_depth += 1;
Expand All @@ -310,7 +310,7 @@
while current_depth < depth {
result = SparseTree::new(
result,
EmptyTree::new(current_depth, empty_leaf.clone()).into(),

Check warning on line 313 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:313:47 | 313 | EmptyTree::new(current_depth, empty_leaf.clone()).into(), | ^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*empty_leaf` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
)
.into();
current_depth += 1;
Expand Down Expand Up @@ -457,7 +457,7 @@

fn write_proof(&self, index: usize, path: &mut Vec<Branch<H>>) {
for depth in (1..=self.depth).rev() {
let val = self.empty_tree_values[depth - 1].clone();

Check warning on line 460 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:460:23 | 460 | let val = self.empty_tree_values[depth - 1].clone(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.empty_tree_values[depth - 1]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
let branch = if get_turn_at_depth(index, depth) == Turn::Left {
Branch::Left(val)
} else {
Expand Down Expand Up @@ -512,11 +512,11 @@

#[must_use]
fn root(&self) -> H::Hash {
self.empty_tree_values[self.depth].clone()

Check warning on line 515 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:515:9 | 515 | self.empty_tree_values[self.depth].clone() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.empty_tree_values[self.depth]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}

fn get_leaf(&self) -> H::Hash {
self.empty_tree_values[0].clone()

Check warning on line 519 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:519:9 | 519 | self.empty_tree_values[0].clone() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.empty_tree_values[0]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}
}

Expand Down Expand Up @@ -580,7 +580,7 @@
fn clone(&self) -> Self {
Self {
depth: self.depth,
root: self.root.clone(),

Check warning on line 583 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:583:23 | 583 | root: self.root.clone(), | ^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.root` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
children: self.children.clone(),
}
}
Expand Down Expand Up @@ -626,7 +626,7 @@
) -> Self {
let Some(children) = &self.children else {
// no children – this is a leaf
return Self::new_leaf(value.clone());

Check warning on line 629 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:629:35 | 629 | return Self::new_leaf(value.clone()); | ^^^^^^^^^^^^^ help: try dereferencing it: `*value` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
};

let next_index = clear_turn_at_depth(index, self.depth);
Expand All @@ -652,12 +652,12 @@
}

fn root(&self) -> H::Hash {
self.root.clone()

Check warning on line 655 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:655:9 | 655 | self.root.clone() | ^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.root` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}

fn get_leaf(&self, index: usize) -> H::Hash {
self.children.as_ref().map_or_else(
|| self.root.clone(),

Check warning on line 660 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:660:16 | 660 | || self.root.clone(), | ^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.root` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
|children| {
let next_index = clear_turn_at_depth(index, self.depth);
if get_turn_at_depth(index, self.depth) == Turn::Left {
Expand Down Expand Up @@ -693,11 +693,11 @@
let storage_size = 1 << (depth + 1);
let mut storage = Vec::with_capacity(storage_size);

let empties = repeat(empty_value.clone()).take(leaf_count);

Check warning on line 696 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:696:30 | 696 | let empties = repeat(empty_value.clone()).take(leaf_count); | ^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*empty_value` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
storage.extend(empties);
storage.extend_from_slice(values);
if values.len() < leaf_count {
let empties = repeat(empty_value.clone()).take(leaf_count - values.len());

Check warning on line 700 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:700:34 | 700 | let empties = repeat(empty_value.clone()).take(leaf_count - values.len()); | ^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*empty_value` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
storage.extend(empties);
}

Expand Down Expand Up @@ -750,7 +750,7 @@
fn get_leaf(&self, index: usize) -> H::Hash {
self.with_ref(|r| {
let leaf_index_in_dense_tree = index + (self.root_index << self.depth);
r.storage[leaf_index_in_dense_tree].clone()

Check warning on line 753 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:753:13 | 753 | r.storage[leaf_index_in_dense_tree].clone() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `r.storage[leaf_index_in_dense_tree]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
})
}

Expand All @@ -771,7 +771,7 @@
fn update_with_mutation(&self, index: usize, value: &H::Hash) {
let mut storage = self.storage.lock().expect("lock poisoned, terminating");
let leaf_index_in_dense_tree = index + (self.root_index << self.depth);
storage[leaf_index_in_dense_tree] = value.clone();

Check warning on line 774 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:774:45 | 774 | storage[leaf_index_in_dense_tree] = value.clone(); | ^^^^^^^^^^^^^ help: try dereferencing it: `*value` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
let mut current = leaf_index_in_dense_tree / 2;
while current > 0 {
let left = &storage[2 * current];
Expand All @@ -782,7 +782,7 @@
}

fn root(&self) -> H::Hash {
self.storage.lock().unwrap()[self.root_index].clone()

Check warning on line 785 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:785:9 | 785 | self.storage.lock().unwrap()[self.root_index].clone() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.storage.lock().unwrap()[self.root_index]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}
}

Expand Down Expand Up @@ -811,7 +811,7 @@

impl<'a, H: Hasher> DenseTreeRef<'a, H> {
fn root(&self) -> H::Hash {
self.storage[self.root_index].clone()

Check warning on line 814 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:814:9 | 814 | self.storage[self.root_index].clone() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.storage[self.root_index]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}

const fn left(&self) -> DenseTreeRef<H> {
Expand Down Expand Up @@ -848,7 +848,7 @@

fn update(&self, index: usize, hash: &H::Hash) -> SparseTree<H> {
if self.depth == 0 {
return SparseTree::new_leaf(hash.clone());

Check warning on line 851 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:851:41 | 851 | return SparseTree::new_leaf(hash.clone()); | ^^^^^^^^^^^^ help: try dereferencing it: `*hash` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}
let next_index = clear_turn_at_depth(index, self.depth);
if get_turn_at_depth(index, self.depth) == Turn::Left {
Expand Down Expand Up @@ -977,7 +977,7 @@
fn get_leaf(&self, index: usize) -> H::Hash {
self.with_ref(|r| {
let leaf_index_in_dense_tree = index + (self.root_index << self.depth);
r.storage[leaf_index_in_dense_tree].clone()

Check warning on line 980 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:980:13 | 980 | r.storage[leaf_index_in_dense_tree].clone() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `r.storage[leaf_index_in_dense_tree]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
})
}

Expand All @@ -998,7 +998,7 @@
fn update_with_mutation(&self, index: usize, value: &H::Hash) {
let mut storage = self.storage.lock().expect("lock poisoned, terminating");
let leaf_index_in_dense_tree = index + (self.root_index << self.depth);
storage[leaf_index_in_dense_tree] = value.clone();

Check warning on line 1001 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:1001:45 | 1001 | storage[leaf_index_in_dense_tree] = value.clone(); | ^^^^^^^^^^^^^ help: try dereferencing it: `*value` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
let mut current = leaf_index_in_dense_tree / 2;
while current > 0 {
let left = &storage[2 * current];
Expand All @@ -1009,7 +1009,7 @@
}

fn root(&self) -> H::Hash {
self.storage.lock().expect("lock poisoned")[self.root_index].clone()

Check warning on line 1012 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:1012:9 | 1012 | self.storage.lock().expect("lock poisoned")[self.root_index].clone() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.storage.lock().expect("lock poisoned")[self.root_index]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}
}

Expand Down Expand Up @@ -1038,7 +1038,7 @@

impl<'a, H: Hasher> DenseTreeMMapRef<'a, H> {
fn root(&self) -> H::Hash {
self.storage[self.root_index].clone()

Check warning on line 1041 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:1041:9 | 1041 | self.storage[self.root_index].clone() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.storage[self.root_index]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}

const fn left(&self) -> DenseTreeMMapRef<H> {
Expand Down Expand Up @@ -1075,7 +1075,7 @@

fn update(&self, index: usize, hash: &H::Hash) -> SparseTree<H> {
if self.depth == 0 {
return SparseTree::new_leaf(hash.clone());

Check warning on line 1078 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:1078:41 | 1078 | return SparseTree::new_leaf(hash.clone()); | ^^^^^^^^^^^^ help: try dereferencing it: `*hash` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}
let next_index = clear_turn_at_depth(index, self.depth);
if get_turn_at_depth(index, self.depth) == Turn::Left {
Expand Down Expand Up @@ -1134,7 +1134,7 @@
) -> Result<Self, DenseMMapError> {
// Safety: potential uninitialized padding from `H::Hash` is safe to use if
// we're casting back to the same type.
let buf = unsafe { as_bytes(storage) };
let buf = bytemuck::cast_slice(storage);
let buf_len = buf.len();

let mut file = match OpenOptions::new()
Expand Down Expand Up @@ -1218,17 +1218,13 @@
type Target = [H::Hash];

fn deref(&self) -> &Self::Target {
let bytes: &[u8] = &self.mmap;
let ptr = bytes.as_ptr().cast::<H::Hash>();
unsafe { std::slice::from_raw_parts(ptr, bytes.len() / std::mem::size_of::<H::Hash>()) }
bytemuck::cast_slice(self.mmap.as_slice())
}
}

impl<H: Hasher> DerefMut for MmapMutWrapper<H> {
fn deref_mut(&mut self) -> &mut Self::Target {
let bytes: &mut [u8] = self.mmap.as_mut_slice();
let ptr = bytes.as_mut_ptr().cast::<H::Hash>();
unsafe { std::slice::from_raw_parts_mut(ptr, bytes.len() / std::mem::size_of::<H::Hash>()) }
bytemuck::cast_slice_mut(self.mmap.as_mut_slice())
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! * Disk based storage backend (using mmaped files should be easy)

use crate::Field;
use bytemuck::Pod;
use serde::{Deserialize, Serialize};
use std::{
fmt::Debug,
Expand All @@ -14,7 +15,7 @@
/// Hash types, values and algorithms for a Merkle tree
pub trait Hasher {
/// Type of the leaf and node hashes
type Hash: Clone + Eq + Serialize + Debug + Send + Sync;
type Hash: Clone + Eq + Serialize + Debug + Pod + Send + Sync;

/// Compute the hash of an intermediate node
fn hash_node(left: &Self::Hash, right: &Self::Hash) -> Self::Hash;
Expand Down Expand Up @@ -108,7 +109,7 @@

#[must_use]
pub fn root(&self) -> H::Hash {
self.nodes[0].clone()

Check warning on line 112 in src/merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/merkle_tree.rs:112:9 | 112 | self.nodes[0].clone() | ^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.nodes[0]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy note: the lint level is defined here --> src/lib.rs:2:9 | 2 | #![warn(clippy::all, clippy::cargo)] | ^^^^^^^^^^^ = note: `#[warn(clippy::clone_on_copy)]` implied by `#[warn(clippy::all)]`
}

pub fn set(&mut self, leaf: usize, hash: H::Hash) {
Expand Down Expand Up @@ -149,8 +150,8 @@
while let Some(parent) = parent(index) {
// Add proof for node at index to parent
path.push(match index & 1 {
1 => Branch::Left(self.nodes[index + 1].clone()),

Check warning on line 153 in src/merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/merkle_tree.rs:153:35 | 153 | 1 => Branch::Left(self.nodes[index + 1].clone()), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.nodes[index + 1]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
0 => Branch::Right(self.nodes[index - 1].clone()),

Check warning on line 154 in src/merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/merkle_tree.rs:154:36 | 154 | 0 => Branch::Right(self.nodes[index - 1].clone()), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.nodes[index - 1]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
_ => unreachable!(),
});
index = parent;
Expand Down
Loading