11#![ no_main]
22
3+ use lambdaworks_crypto:: merkle_tree:: merkle:: MerkleTree ;
34use risc0_aggregation_program:: { Input , Risc0ImageIdAndPubInputs } ;
45use risc0_zkvm:: guest:: env;
5- use tiny_keccak:: { Hasher , Keccak } ;
66
77risc0_zkvm:: guest:: entry!( main) ;
88
9- fn combine_hashes ( hash_a : & [ u8 ; 32 ] , hash_b : & [ u8 ; 32 ] ) -> [ u8 ; 32 ] {
10- let mut hasher = Keccak :: v256 ( ) ;
11- hasher. update ( hash_a) ;
12- hasher. update ( hash_b) ;
13-
14- let mut hash = [ 0u8 ; 32 ] ;
15- hasher. finalize ( & mut hash) ;
16- hash
17- }
18-
19- /// Computes the merkle root for the given proofs
20- fn compute_merkle_root ( proofs : & [ Risc0ImageIdAndPubInputs ] ) -> [ u8 ; 32 ] {
21- let mut leaves: Vec < [ u8 ; 32 ] > = proofs
22- . chunks ( 2 )
23- . map ( |chunk| match chunk {
24- [ a, b] => combine_hashes ( & a. commitment ( ) , & b. commitment ( ) ) ,
25- [ a] => combine_hashes ( & a. commitment ( ) , & a. commitment ( ) ) ,
26- _ => panic ! ( "Unexpected chunk leaves" ) ,
27- } )
28- . collect ( ) ;
29-
30- while leaves. len ( ) > 1 {
31- leaves = leaves
32- . chunks ( 2 )
33- . map ( |chunk| match chunk {
34- [ a, b] => combine_hashes ( & a, & b) ,
35- [ a] => combine_hashes ( & a, & a) ,
36- _ => panic ! ( "Unexpected chunk size in leaves" ) ,
37- } )
38- . collect ( )
39- }
40-
41- leaves[ 0 ]
42- }
43-
449fn main ( ) {
4510 let input = env:: read :: < Input > ( ) ;
4611
@@ -52,7 +17,8 @@ fn main() {
5217 env:: verify ( image_id. clone ( ) , & public_inputs) . expect ( "proof to be verified correctly" ) ;
5318 }
5419
55- let merkle_root = compute_merkle_root ( & input. proofs_image_id_and_pub_inputs ) ;
20+ let merkle_tree: MerkleTree < Risc0ImageIdAndPubInputs > =
21+ MerkleTree :: build ( & input. proofs_image_id_and_pub_inputs ) . unwrap ( ) ;
5622
57- env:: commit_slice ( & merkle_root ) ;
23+ env:: commit_slice ( & merkle_tree . root ) ;
5824}
0 commit comments