Skip to content

Commit

Permalink
Add more docs, improve conversion u8 Felt and make verify_proof gener…
Browse files Browse the repository at this point in the history
…ic over the hasher
  • Loading branch information
AurelienFT committed Jan 8, 2024
1 parent 8b14e0d commit 3d71be9
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/trie/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bitvec::{
};
use derive_more::Constructor;
use mp_felt::Felt252Wrapper;
use mp_hashers::{pedersen::PedersenHasher, HasherT};
use mp_hashers::HasherT;
use parity_scale_codec::{Decode, Encode, Error, Input, Output};
use std::{collections::HashMap, mem};

Expand Down Expand Up @@ -120,7 +120,7 @@ fn test_shared_path_encode_decode() {
}
/// A node used in proof generated by the trie.
///
/// See pathfinders merkle-tree crate for more information.
/// Each node hold only the minimum of data that need to be known for the proof: the child hashes (and path for edge node)
#[derive(Debug, Clone, PartialEq)]
pub enum ProofNode {
Binary {
Expand All @@ -140,12 +140,10 @@ impl ProofNode {
ProofNode::Edge { child, path } => {
let mut bytes = [0u8; 32];
bytes.view_bits_mut::<Msb0>()[256 - path.0.len()..].copy_from_bitslice(&path.0);
// SAFETY: byte array is 32 bytes long
let path_hash = Felt252Wrapper::try_from(&bytes).unwrap();
let mut length = [0; 32];
// Safe as len() is guaranteed to be <= 251
length[31] = path.0.len() as u8;

let length = Felt252Wrapper::try_from(&length).unwrap();
let length = Felt252Wrapper::from(path.0.len() as u8);
Felt252Wrapper(H::hash_elements(child.0, path_hash.0) + length.0)
}
}
Expand Down Expand Up @@ -1114,7 +1112,7 @@ impl<H: HasherT, DB: BonsaiDatabase, ID: Id> MerkleTree<H, DB, ID> {

for proof_node in proofs.iter() {
// Hash mismatch? Return None.
if proof_node.hash::<PedersenHasher>() != expected_hash {
if proof_node.hash::<H>() != expected_hash {
return None;
}
match proof_node {
Expand Down

0 comments on commit 3d71be9

Please sign in to comment.