Skip to content

Commit 0a46e9c

Browse files
committed
feat: sp1 aggregation program
1 parent 7a95c59 commit 0a46e9c

File tree

5 files changed

+85
-11
lines changed

5 files changed

+85
-11
lines changed

aggregation-mode/Cargo.lock

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aggregation-mode/zkvm/Cargo.lock

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aggregation-mode/zkvm/sp1/Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ edition = "2021"
55
resolver = "2"
66

77
[dependencies]
8-
sp1-zkvm = "4.1.3"
8+
sp1-zkvm = { version = "4.1.3", features = ["verify"] }
9+
sha2 = "0.10.8"
910
serde = { version = "1.0.203", features = ["derive"] }
1011
serde_json = "1.0.117"
1112

13+
[patch.crates-io]
14+
sha2 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", tag = "sha2-v0.10.8-patch-v1" }
15+
1216
[lib]
1317
path = "./src/lib.rs"
18+
19+
[[bin]]
20+
name = "sp1_verifier_program"
21+
path = "src/main.rs"
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
use serde::{Deserialize, Serialize};
2+
use sha2::{Digest, Sha256};
23

34
#[derive(Serialize, Deserialize)]
45
pub struct SP1CompressedProof {
56
vk: Vec<u8>,
6-
public_inputs: Vec<Vec<u8>>,
7+
pub public_inputs: Vec<u8>,
8+
}
9+
10+
impl SP1CompressedProof {
11+
pub fn vk(&self) -> [u32; 8] {
12+
assert!(self.vk.len() >= 32, "vk must be at least 32 bytes long");
13+
14+
let mut bytes = [0_32; 8];
15+
16+
for (i, chunk) in self.vk.chunks_exact(4).enumerate() {
17+
bytes[i] = u32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]);
18+
}
19+
20+
bytes
21+
}
22+
23+
pub fn hash(&self) -> [u8; 32] {
24+
let mut hasher = Sha256::new();
25+
hasher.update(&self.vk);
26+
hasher.update(&self.public_inputs);
27+
hasher.finalize().into()
28+
}
729
}
Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,53 @@
11
#![no_main]
22
sp1_zkvm::entrypoint!(main);
33

4+
use sha2::{Digest, Sha256};
45
use sp1_verifier_program::SP1CompressedProof;
56

6-
/// TODO: write proof aggregation program
7-
///
8-
/// For now we are only receiving the inputs and committing a silly output
9-
/// Future iteration will include the aggregation of proofs and will return the
10-
/// proof of the verification of proofs + merkle tree leaves
7+
fn combine_hashes(hash_a: &[u8; 32], hash_b: &[u8; 32]) -> [u8; 32] {
8+
let mut hasher = Sha256::new();
9+
hasher.update(hash_a);
10+
hasher.update(hash_b);
11+
hasher.finalize().into()
12+
}
13+
14+
/// Computes the merkle root for the given proofs using the vk
15+
fn compute_merkle_root(proofs: &[SP1CompressedProof]) -> [u8; 32] {
16+
let mut leaves: Vec<[u8; 32]> = proofs
17+
.chunks(2)
18+
.map(|chunk| match chunk {
19+
[a, b] => combine_hashes(&a.hash(), &b.hash()),
20+
[a] => combine_hashes(&a.hash(), &a.hash()),
21+
_ => panic!("Unexpected chunk size in get_parent_nodes"),
22+
})
23+
.collect();
24+
25+
while leaves.len() > 1 {
26+
leaves = leaves
27+
.chunks(2)
28+
.map(|chunk| match chunk {
29+
[a, b] => combine_hashes(&a, &b),
30+
[a] => combine_hashes(&a, &a),
31+
_ => panic!("Unexpected chunk size in get_parent_nodes"),
32+
})
33+
.collect()
34+
}
35+
36+
leaves[0]
37+
}
38+
39+
// TODO: Update input and use AlignedVerificationData
1140
pub fn main() {
1241
let input = sp1_zkvm::io::read::<Vec<SP1CompressedProof>>();
13-
let result = input.len() + 1;
14-
sp1_zkvm::io::commit(&result);
42+
43+
// Verify the proofs.
44+
for proof in input.iter() {
45+
let vkey = proof.vk();
46+
let public_values = &proof.public_inputs;
47+
let public_values_digest = Sha256::digest(public_values);
48+
sp1_zkvm::lib::verify::verify_sp1_proof(&vkey, &public_values_digest.into());
49+
}
50+
51+
let merkle_root = compute_merkle_root(&input);
52+
sp1_zkvm::io::commit_slice(&merkle_root);
1553
}

0 commit comments

Comments
 (0)