-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathmain.rs
More file actions
60 lines (50 loc) · 1.77 KB
/
main.rs
File metadata and controls
60 lines (50 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#![no_main]
sp1_zkvm::entrypoint!(main);
use sha2::{Digest, Sha256};
use sha3::Keccak256;
use sp1_aggregation_program::{Input, ProofVkAndPubInputs};
fn combine_hashes(hash_a: &[u8; 32], hash_b: &[u8; 32]) -> [u8; 32] {
let mut hasher = Keccak256::new();
hasher.update(hash_a);
hasher.update(hash_b);
hasher.finalize().into()
}
/// Computes the merkle root for the given proofs using the vk
fn compute_merkle_root(proofs: &[ProofVkAndPubInputs]) -> [u8; 32] {
let mut leaves: Vec<[u8; 32]> = proofs
.chunks(2)
.map(|chunk| match chunk {
[a, b] => combine_hashes(&a.hash(), &b.hash()),
[a] => combine_hashes(&a.hash(), &a.hash()),
_ => panic!("Unexpected chunk leaves"),
})
.collect();
while leaves.len() > 1 {
leaves = leaves
.chunks(2)
.map(|chunk| match chunk {
[a, b] => combine_hashes(&a, &b),
[a] => combine_hashes(&a, &a),
_ => panic!("Unexpected chunk size in leaves"),
})
.collect()
}
leaves[0]
}
pub fn main() {
let input = sp1_zkvm::io::read::<Input>();
// Verify the proofs.
for proof in input.proofs_vk_and_pub_inputs.iter() {
match proof {
ProofVkAndPubInputs::SP1Compressed(proof) => {
let vkey = proof.vk;
let public_values = &proof.public_inputs;
let public_values_digest = Sha256::digest(public_values);
sp1_zkvm::lib::verify::verify_sp1_proof(&vkey, &public_values_digest.into());
}
}
}
let merkle_root = compute_merkle_root(&input.proofs_vk_and_pub_inputs);
assert_eq!(merkle_root, input.merkle_root);
sp1_zkvm::io::commit_slice(&merkle_root);
}