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 ( ) ;
@@ -232,15 +235,15 @@ impl MerkleProof {
232235
233236 let path = keypath ( max_depth, * key) ?;
234237 let mut h = value
235- . map ( |v| Hash ( PoseidonHash :: hash_no_pad ( & [ key. 0 , v. 0 ] . concat ( ) ) . elements ) )
238+ . map ( |v| hash_fields ( & [ key. 0 , v. 0 ] . concat ( ) ) )
236239 . unwrap_or ( Hash ( [ GoldilocksField ( 0 ) ; 4 ] ) ) ;
237240 for ( i, sibling) in self . siblings . iter ( ) . enumerate ( ) . rev ( ) {
238241 let input: Vec < F > = if path[ i] {
239242 [ sibling. 0 , h. 0 ] . concat ( )
240243 } else {
241244 [ h. 0 , sibling. 0 ] . concat ( )
242245 } ;
243- h = Hash ( PoseidonHash :: hash_no_pad ( & input) . elements ) ;
246+ h = hash_fields ( & input) ;
244247 }
245248 Ok ( h)
246249 }
@@ -306,7 +309,7 @@ impl Node {
306309 }
307310 }
308311
309- /// Goes down from the current node until it encounters a terminal node,
312+ /// Goes down from the current node until it encounter a terminal node,
310313 /// viz. a leaf or empty node, or until it reaches the maximum depth. The
311314 /// `siblings` parameter is used to store the siblings while going down to
312315 /// the leaf, if the given parameter is set to `None`, then no siblings are
@@ -352,6 +355,8 @@ impl Node {
352355
353356 // adds the leaf at the tree from the current node (self), without computing any hash
354357 fn add_leaf ( & mut self , lvl : usize , max_depth : usize , leaf : Leaf ) -> Result < ( ) > {
358+ counter:: count_tree_insert ( ) ;
359+
355360 if lvl >= max_depth {
356361 return Err ( anyhow ! ( "max depth reached" ) ) ;
357362 }
@@ -467,7 +472,7 @@ impl Intermediate {
467472 let l_hash = self . left . compute_hash ( ) ;
468473 let r_hash = self . right . compute_hash ( ) ;
469474 let input: Vec < F > = [ l_hash. 0 , r_hash. 0 ] . concat ( ) ;
470- let h = Hash ( PoseidonHash :: hash_no_pad ( & input) . elements ) ;
475+ let h = hash_fields ( & input) ;
471476 self . hash = Some ( h) ;
472477 h
473478 }
@@ -494,7 +499,7 @@ impl Leaf {
494499 }
495500 fn compute_hash ( & mut self ) -> Hash {
496501 let input: Vec < F > = [ self . key . 0 , self . value . 0 ] . concat ( ) ;
497- let h = Hash ( PoseidonHash :: hash_no_pad ( & input) . elements ) ;
502+ let h = hash_fields ( & input) ;
498503 self . hash = Some ( h) ;
499504 h
500505 }
@@ -587,8 +592,11 @@ pub mod tests {
587592 let ( v, proof) = tree. prove ( & Value :: from ( 13 ) ) ?;
588593 assert_eq ! ( v, Value :: from( 1013 ) ) ;
589594 println ! ( "{}" , proof) ;
595+ println ! ( "after proof generation, {}" , counter:: counter_get( ) ) ;
590596
597+ counter:: counter_reset ( ) ;
591598 MerkleTree :: verify ( 32 , tree. root ( ) , & proof, & key, & value) ?;
599+ println ! ( "after verify, {}" , counter:: counter_get( ) ) ;
592600
593601 // Exclusion checks
594602 let key = Value :: from ( 12 ) ;
0 commit comments