|
| 1 | +#![no_main] |
| 2 | + |
| 3 | +use lambdaworks_crypto::merkle_tree::merkle::MerkleTree; |
| 4 | +use risc0_aggregation_program::{ChunkAggregatorInput, Hash32}; |
| 5 | +use risc0_zkvm::guest::env; |
| 6 | + |
| 7 | +risc0_zkvm::guest::entry!(main); |
| 8 | + |
| 9 | +// Generated with `make agg_mode_write_program_ids` and copied from program_ids.json |
| 10 | +pub const USER_PROOFS_AGGREGATOR_PROGRAM_IMAGE_ID: [u8; 32] = [ |
| 11 | + 83, 145, 39, 254, 127, 217, 146, 127, 63, 217, 69, 190, 11, 204, 170, 138, 215, 35, 175, 246, |
| 12 | + 209, 154, 52, 243, 85, 37, 177, 147, 22, 153, 155, 156, |
| 13 | +]; |
| 14 | + |
| 15 | +fn main() { |
| 16 | + let input = env::read::<ChunkAggregatorInput>(); |
| 17 | + |
| 18 | + let mut leaves: Vec<Hash32> = vec![]; |
| 19 | + |
| 20 | + for (proof, leaves_commitment) in input.proofs_and_leaves_commitment { |
| 21 | + let image_id = proof.image_id; |
| 22 | + |
| 23 | + // Ensure the aggregated chunk originates from the L1 aggregation program. |
| 24 | + // This validation step guarantees that the proof was genuinely verified |
| 25 | + // by this program. Without this check, a different program using the |
| 26 | + // same public inputs could bypass verification. |
| 27 | + assert!(image_id == USER_PROOFS_AGGREGATOR_PROGRAM_IMAGE_ID); |
| 28 | + |
| 29 | + // Ensure the committed root matches the root of the provided leaves |
| 30 | + let merkle_root: [u8; 32] = proof |
| 31 | + .public_inputs |
| 32 | + .clone() |
| 33 | + .try_into() |
| 34 | + .expect("Public input to be the chunk merkle root"); |
| 35 | + |
| 36 | + let leaves_commitment: Vec<Hash32> = |
| 37 | + leaves_commitment.into_iter().map(|el| Hash32(el)).collect(); |
| 38 | + let merkle_tree = MerkleTree::<Hash32>::build(&leaves_commitment).unwrap(); |
| 39 | + assert!(merkle_root == merkle_tree.root); |
| 40 | + |
| 41 | + leaves.extend(leaves_commitment); |
| 42 | + |
| 43 | + // finally verify the proof |
| 44 | + env::verify(image_id, &proof.public_inputs).expect("proof to be verified correctly"); |
| 45 | + } |
| 46 | + |
| 47 | + let merkle_tree = MerkleTree::<Hash32>::build(&leaves).unwrap(); |
| 48 | + |
| 49 | + env::commit_slice(&merkle_tree.root); |
| 50 | +} |
0 commit comments