@@ -28,9 +28,9 @@ use crate::{
2828 backends:: plonky2:: {
2929 basetypes:: D ,
3030 circuits:: common:: { CircuitBuilderPod , ValueTarget } ,
31- primitives:: merkletree:: MerkleProof ,
31+ primitives:: merkletree:: MerkleClaimAndProof ,
3232 } ,
33- middleware:: { Hash , RawValue , EMPTY_HASH , EMPTY_VALUE , F , HASH_SIZE } ,
33+ middleware:: { EMPTY_HASH , EMPTY_VALUE , F , HASH_SIZE } ,
3434} ;
3535
3636/// `MerkleProofGadget` allows to verify both proofs of existence and proofs
@@ -158,33 +158,20 @@ impl MerkleProofGadget {
158158impl MerkleClaimAndProofTarget {
159159 /// assigns the given values to the targets
160160 #[ allow( clippy:: too_many_arguments) ]
161- pub fn set_targets (
162- & self ,
163- pw : & mut PartialWitness < F > ,
164- enabled : bool ,
165- existence : bool ,
166- root : Hash ,
167- proof : MerkleProof ,
168- key : RawValue ,
169- value : RawValue ,
170- ) -> Result < ( ) > {
171- pw. set_bool_target ( self . enabled , enabled) ?;
172- pw. set_hash_target ( self . root , HashOut :: from_vec ( root. 0 . to_vec ( ) ) ) ?;
173- pw. set_target_arr ( & self . key . elements , & key. 0 ) ?;
174- pw. set_target_arr ( & self . value . elements , & value. 0 ) ?;
175- pw. set_bool_target ( self . existence , existence) ?;
176-
177- // pad siblings with zeros to length max_depth
178- let mut siblings = proof. siblings . clone ( ) ;
179- siblings. resize ( self . max_depth , EMPTY_HASH ) ;
180- assert_eq ! ( self . siblings. len( ) , siblings. len( ) ) ;
181-
182- for ( i, sibling) in siblings. iter ( ) . enumerate ( ) {
161+ pub fn set_targets ( & self , pw : & mut PartialWitness < F > , mp : & MerkleClaimAndProof ) -> Result < ( ) > {
162+ pw. set_bool_target ( self . enabled , mp. enabled ) ?;
163+ pw. set_hash_target ( self . root , HashOut :: from_vec ( mp. root . 0 . to_vec ( ) ) ) ?;
164+ pw. set_target_arr ( & self . key . elements , & mp. key . 0 ) ?;
165+ pw. set_target_arr ( & self . value . elements , & mp. value . 0 ) ?;
166+ pw. set_bool_target ( self . existence , mp. proof . existence ) ?;
167+
168+ assert_eq ! ( mp. proof. siblings. len( ) , self . max_depth) ;
169+ for ( i, sibling) in mp. proof . siblings . iter ( ) . enumerate ( ) {
183170 pw. set_hash_target ( self . siblings [ i] , HashOut :: from_vec ( sibling. 0 . to_vec ( ) ) ) ?;
184171 }
185172
186- match proof. other_leaf {
187- Some ( ( k, v) ) if !existence => {
173+ match mp . proof . other_leaf {
174+ Some ( ( k, v) ) if !mp . proof . existence => {
188175 // non-existence case ii) expected leaf does exist but it has a different key
189176 pw. set_bool_target ( self . case_ii_selector , true ) ?;
190177 pw. set_target_arr ( & self . other_key . elements , & k. 0 ) ?;
@@ -263,29 +250,18 @@ impl MerkleProofExistenceGadget {
263250
264251impl MerkleProofExistenceTarget {
265252 /// assigns the given values to the targets
266- pub fn set_targets (
267- & self ,
268- pw : & mut PartialWitness < F > ,
269- // `enabled` determines if the merkleproof verification is enabled
270- enabled : bool ,
271- root : Hash ,
272- proof : MerkleProof ,
273- key : RawValue ,
274- value : RawValue ,
275- ) -> Result < ( ) > {
276- assert ! ( proof. existence) ; // sanity check
253+ pub fn set_targets ( & self , pw : & mut PartialWitness < F > , mp : & MerkleClaimAndProof ) -> Result < ( ) > {
254+ assert ! ( mp. proof. existence) ; // sanity check
277255
278- pw. set_bool_target ( self . enabled , enabled) ?;
279- pw. set_hash_target ( self . root , HashOut :: from_vec ( root. 0 . to_vec ( ) ) ) ?;
280- pw. set_target_arr ( & self . key . elements , & key. 0 ) ?;
281- pw. set_target_arr ( & self . value . elements , & value. 0 ) ?;
256+ pw. set_bool_target ( self . enabled , mp . enabled ) ?;
257+ pw. set_hash_target ( self . root , HashOut :: from_vec ( mp . root . 0 . to_vec ( ) ) ) ?;
258+ pw. set_target_arr ( & self . key . elements , & mp . key . 0 ) ?;
259+ pw. set_target_arr ( & self . value . elements , & mp . value . 0 ) ?;
282260
283261 // pad siblings with zeros to length max_depth
284- let mut siblings = proof. siblings . clone ( ) ;
285- siblings. resize ( self . max_depth , EMPTY_HASH ) ;
286- assert_eq ! ( self . siblings. len( ) , siblings. len( ) ) ;
262+ assert_eq ! ( mp. proof. siblings. len( ) , self . max_depth) ;
287263
288- for ( i, sibling) in siblings. iter ( ) . enumerate ( ) {
264+ for ( i, sibling) in mp . proof . siblings . iter ( ) . enumerate ( ) {
289265 pw. set_hash_target ( self . siblings [ i] , HashOut :: from_vec ( sibling. 0 . to_vec ( ) ) ) ?;
290266 }
291267
@@ -539,12 +515,7 @@ pub mod tests {
539515 let targets = MerkleProofGadget { max_depth } . eval ( & mut builder) ?;
540516 targets. set_targets (
541517 & mut pw,
542- true , // verification enabled
543- existence,
544- tree. root ( ) ,
545- proof,
546- key,
547- value,
518+ & MerkleClaimAndProof :: new ( max_depth, tree. root ( ) , key, Some ( value) , & proof) ?,
548519 ) ?;
549520
550521 // generate & verify proof
@@ -587,7 +558,10 @@ pub mod tests {
587558 let mut pw = PartialWitness :: < F > :: new ( ) ;
588559
589560 let targets = MerkleProofExistenceGadget { max_depth } . eval ( & mut builder) ?;
590- targets. set_targets ( & mut pw, true , tree. root ( ) , proof, key, value) ?;
561+ targets. set_targets (
562+ & mut pw,
563+ & MerkleClaimAndProof :: new ( max_depth, tree. root ( ) , key, Some ( value) , & proof) ?,
564+ ) ?;
591565
592566 // generate & verify proof
593567 let data = builder. build :: < C > ( ) ;
@@ -660,12 +634,7 @@ pub mod tests {
660634 let targets = MerkleProofGadget { max_depth } . eval ( & mut builder) ?;
661635 targets. set_targets (
662636 & mut pw,
663- true , // verification enabled
664- proof. existence ,
665- tree. root ( ) ,
666- proof,
667- key,
668- value,
637+ & MerkleClaimAndProof :: new ( max_depth, tree. root ( ) , key, Some ( value) , & proof) ?,
669638 ) ?;
670639
671640 // generate & verify proof
@@ -707,15 +676,9 @@ pub mod tests {
707676 let mut pw = PartialWitness :: < F > :: new ( ) ;
708677
709678 let targets = MerkleProofGadget { max_depth } . eval ( & mut builder) ?;
710- targets. set_targets (
711- & mut pw,
712- true , // verification enabled
713- true , // proof of existence
714- tree2. root ( ) ,
715- proof. clone ( ) ,
716- key,
717- value,
718- ) ?;
679+ // verification enabled & proof of existence
680+ let mut mp = MerkleClaimAndProof :: new ( max_depth, tree2. root ( ) , key, Some ( value) , & proof) ?;
681+ targets. set_targets ( & mut pw, & mp) ?;
719682
720683 // generate proof, expecting it to fail (since we're using the wrong
721684 // root)
@@ -729,15 +692,9 @@ pub mod tests {
729692 let mut pw = PartialWitness :: < F > :: new ( ) ;
730693
731694 let targets = MerkleProofGadget { max_depth } . eval ( & mut builder) ?;
732- targets. set_targets (
733- & mut pw,
734- false , // verification disabled
735- true , // proof of existence
736- tree2. root ( ) ,
737- proof,
738- key,
739- value,
740- ) ?;
695+ // verification disabled & proof of existence
696+ mp. enabled = false ;
697+ targets. set_targets ( & mut pw, & mp) ?;
741698
742699 // generate proof, should pass despite using wrong witness, since the
743700 // `enabled=false`
0 commit comments