22//! https://0xparc.github.io/pod2/merkletree.html .
33use anyhow:: { anyhow, Result } ;
44use plonky2:: field:: goldilocks_field:: GoldilocksField ;
5- use plonky2:: hash:: poseidon:: PoseidonHash ;
6- use plonky2:: plonk:: config:: Hasher ;
75use std:: collections:: HashMap ;
86use std:: fmt;
97use std:: iter:: IntoIterator ;
108
11- use crate :: backends:: plonky2:: basetypes:: { Hash , Value , F , NULL } ;
9+ use crate :: backends:: counter;
10+ use crate :: backends:: plonky2:: basetypes:: { hash_fields, Hash , Value , F , NULL } ;
1211
1312/// Implements the MerkleTree specified at
1413/// https://0xparc.github.io/pod2/merkletree.html
@@ -71,6 +70,8 @@ impl MerkleTree {
7170 /// the tree. It returns the `value` of the leaf at the given `key`, and the
7271 /// `MerkleProof`.
7372 pub fn prove ( & self , key : & Value ) -> Result < ( Value , MerkleProof ) > {
73+ counter:: count_tree_proof_gen ( ) ;
74+
7475 let path = keypath ( self . max_depth , * key) ?;
7576
7677 let mut siblings: Vec < Hash > = Vec :: new ( ) ;
@@ -96,6 +97,8 @@ impl MerkleTree {
9697 /// the key-value pair in the leaf reached as a result of
9798 /// resolving `key` as well as a `MerkleProof`.
9899 pub fn prove_nonexistence ( & self , key : & Value ) -> Result < MerkleProof > {
100+ counter:: count_tree_proof_gen ( ) ;
101+
99102 let path = keypath ( self . max_depth , * key) ?;
100103
101104 let mut siblings: Vec < Hash > = Vec :: new ( ) ;
@@ -175,14 +178,7 @@ impl MerkleTree {
175178/// mitigate fake proofs.
176179pub fn kv_hash ( key : & Value , value : Option < Value > ) -> Hash {
177180 value
178- . map ( |v| {
179- Hash (
180- PoseidonHash :: hash_no_pad (
181- & [ key. 0 . to_vec ( ) , v. 0 . to_vec ( ) , vec ! [ GoldilocksField ( 1 ) ] ] . concat ( ) ,
182- )
183- . elements ,
184- )
185- } )
181+ . map ( |v| hash_fields ( & [ key. 0 . to_vec ( ) , v. 0 . to_vec ( ) , vec ! [ GoldilocksField ( 1 ) ] ] . concat ( ) ) )
186182 . unwrap_or ( Hash ( [ GoldilocksField ( 0 ) ; 4 ] ) )
187183}
188184
@@ -253,7 +249,7 @@ impl MerkleProof {
253249 } else {
254250 [ h. 0 , sibling. 0 ] . concat ( )
255251 } ;
256- h = Hash ( PoseidonHash :: hash_no_pad ( & input) . elements ) ;
252+ h = hash_fields ( & input) ;
257253 }
258254 Ok ( h)
259255 }
@@ -319,7 +315,7 @@ impl Node {
319315 }
320316 }
321317
322- /// Goes down from the current node until it encounters a terminal node,
318+ /// Goes down from the current node until it encounter a terminal node,
323319 /// viz. a leaf or empty node, or until it reaches the maximum depth. The
324320 /// `siblings` parameter is used to store the siblings while going down to
325321 /// the leaf, if the given parameter is set to `None`, then no siblings are
@@ -365,6 +361,8 @@ impl Node {
365361
366362 // adds the leaf at the tree from the current node (self), without computing any hash
367363 fn add_leaf ( & mut self , lvl : usize , max_depth : usize , leaf : Leaf ) -> Result < ( ) > {
364+ counter:: count_tree_insert ( ) ;
365+
368366 if lvl >= max_depth {
369367 return Err ( anyhow ! ( "max depth reached" ) ) ;
370368 }
@@ -480,7 +478,7 @@ impl Intermediate {
480478 let l_hash = self . left . compute_hash ( ) ;
481479 let r_hash = self . right . compute_hash ( ) ;
482480 let input: Vec < F > = [ l_hash. 0 , r_hash. 0 ] . concat ( ) ;
483- let h = Hash ( PoseidonHash :: hash_no_pad ( & input) . elements ) ;
481+ let h = hash_fields ( & input) ;
484482 self . hash = Some ( h) ;
485483 h
486484 }
@@ -599,8 +597,11 @@ pub mod tests {
599597 let ( v, proof) = tree. prove ( & Value :: from ( 13 ) ) ?;
600598 assert_eq ! ( v, Value :: from( 1013 ) ) ;
601599 println ! ( "{}" , proof) ;
600+ println ! ( "after proof generation, {}" , counter:: counter_get( ) ) ;
602601
602+ counter:: counter_reset ( ) ;
603603 MerkleTree :: verify ( 32 , tree. root ( ) , & proof, & key, & value) ?;
604+ println ! ( "after verify, {}" , counter:: counter_get( ) ) ;
604605
605606 // Exclusion checks
606607 let key = Value :: from ( 12 ) ;
0 commit comments