Skip to content

Commit 514a35d

Browse files
committed
refactor: split guest program into two
1 parent 9d8fced commit 514a35d

File tree

21 files changed

+232
-76
lines changed

21 files changed

+232
-76
lines changed

.github/workflows/prover.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ jobs:
4646
--github-token "${{ secrets.GITHUB_TOKEN }}" \
4747
--pr-number "${{ github.event.pull_request.number }}" \
4848
--commit-hash "${{ github.sha }}" \
49-
--programs fibonacci,sha2-chain,schnorr-sig-verify
49+
--programs fibonacci,sha2-chain,schnorr-sig-verify,groth16-verify-sp1,groth16-verify-risc0
5050
env:
5151
RUSTFLAGS: "-C target-cpu=native -C link-arg=-fuse-ld=lld"

Cargo.lock

Lines changed: 13 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ members = [
1818
# examples
1919
"examples/fibonacci-composition",
2020
"examples/fibonacci",
21-
"examples/groth16-verify",
21+
"examples/groth16-verify-sp1",
22+
"examples/groth16-verify-risc0",
2223
"examples/sha2-chain",
2324
"examples/schnorr-sig-verify",
2425
]
@@ -35,7 +36,8 @@ fibonacci = { path = "examples/fibonacci" }
3536
fibonacci-composition = { path = "examples/fibonacci-composition" }
3637
sha2-chain = { path = "examples/sha2-chain" }
3738
schnorr-sig-verify = { path = "examples/schnorr-sig-verify" }
38-
groth16-verify = { path = "examples/groth16-verify" }
39+
groth16-verify-sp1 = { path = "examples/groth16-verify-sp1" }
40+
groth16-verify-risc0 = { path = "examples/groth16-verify-risc0" }
3941

4042
arbitrary = { version = "1.3.2", features = ["derive"] }
4143
async-trait = "0.1.86"

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ CARGO_INSTALL_EXTRA_FLAGS ?=
1515
FEATURES ?=
1616

1717
# List of programs
18-
PROGRAMS ?= groth16-verify
18+
PROGRAMS ?= groth16-verify-sp1
1919

2020
# ZkVm to use
2121
ZKVM ?= risc0

artifacts/risc0/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ methods = [
1515
"sha2-chain",
1616
"schnorr-sig-verify",
1717
"fibonacci-composition",
18-
"groth16-verify",
18+
"groth16-verify-risc0",
19+
"groth16-verify-sp1",
1920
]

artifacts/sp1/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ pub const FIBONACCI_ELF: &[u8] = include_elf!("guest-sp1-fibonacci");
55
pub const FIBONACCI_COMPOSITION_ELF: &[u8] = include_elf!("guest-sp1-fibonacci-composition");
66
pub const SHA2_CHAIN_ELF: &[u8] = include_elf!("guest-sp1-sha2-chain");
77
pub const SCHNORR_SIG_VERIFY_ELF: &[u8] = include_elf!("guest-sp1-schnorr-sig-verify");
8-
pub const GROTH16_VERIFY_ELF: &[u8] = include_elf!("guest-sp1-groth16-verify");
8+
pub const GROTH16_VERIFY_SP1_ELF: &[u8] = include_elf!("guest-sp1-groth16-verify-sp1");
9+
pub const GROTH16_VERIFY_RISC0_ELF: &[u8] = include_elf!("guest-sp1-groth16-verify-risc0");
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
[package]
2-
name = "groth16-verify"
2+
name = "groth16-verify-risc0"
33
version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
77
serde.workspace = true
8-
zkaleido-sp1-groth16-verifier = { path = "../../adapters/sp1/groth16-verifier" }
98
zkaleido-risc0-groth16-verifier = { path = "../../adapters/risc0/groth16-verifier" }
109
zkaleido = { path = "../../zkaleido" }
1110
zkaleido-native-adapter = { path = "../../adapters/native" }

examples/groth16-verify/src/input.rs renamed to examples/groth16-verify-risc0/src/input.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,15 @@ use serde::{Deserialize, Serialize};
44
use zkaleido::ProofReceipt;
55

66
#[derive(Debug, Clone, Serialize, Deserialize)]
7-
pub struct Groth16VerifyInput {
7+
pub struct Risc0Groth16VerifyInput {
88
pub risc0_receipt: ProofReceipt,
99
pub risc0_vk: [u8; 32],
10-
pub sp1_receipt: ProofReceipt,
11-
pub sp1_vk: [u8; 32],
1210
}
1311

14-
impl Groth16VerifyInput {
12+
impl Risc0Groth16VerifyInput {
1513
pub fn load() -> Self {
1614
let base = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
1715

18-
let sp1_vk_hex = "0000e3572a33647cba427acbaecac23a01e237a8140d2c91b3873457beb5be13";
19-
let sp1_vk: [u8; 32] = hex::decode(sp1_vk_hex).unwrap().try_into().unwrap();
20-
let sp1_proof_file = base.join(format!(
21-
"../../adapters/sp1/groth16-verifier/proofs/fibonacci_sp1_0x{}.proof.bin",
22-
sp1_vk_hex
23-
));
24-
let sp1_receipt = ProofReceipt::load(sp1_proof_file).unwrap();
25-
2616
let risc0_vk_hex = "25eaa741d5cb0d99fe15ce60c4bf3886f96932f22ee67041f0b4165203ef5a02";
2717
let risc0_vk: [u8; 32] = hex::decode(risc0_vk_hex).unwrap().try_into().unwrap();
2818
let proof_file = base.join(format!(
@@ -31,11 +21,9 @@ impl Groth16VerifyInput {
3121
));
3222
let risc0_receipt = ProofReceipt::load(proof_file).unwrap();
3323

34-
Groth16VerifyInput {
24+
Risc0Groth16VerifyInput {
3525
risc0_receipt,
3626
risc0_vk,
37-
sp1_receipt,
38-
sp1_vk,
3927
}
4028
}
4129
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use zkaleido::ZkVmEnv;
2+
3+
use crate::input::Risc0Groth16VerifyInput;
4+
5+
pub mod input;
6+
pub mod program;
7+
8+
pub fn process_groth16_verify_risc0(zkvm: &impl ZkVmEnv) {
9+
let Risc0Groth16VerifyInput {
10+
risc0_receipt,
11+
risc0_vk,
12+
} = zkvm.read_serde();
13+
14+
let risc0_verified =
15+
zkaleido_risc0_groth16_verifier::verify_groth16(&risc0_receipt, &risc0_vk).is_ok();
16+
17+
zkvm.commit_serde(&risc0_verified);
18+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use zkaleido::{ProofType, ZkVmInputResult, ZkVmProgram, ZkVmProgramPerf};
2+
3+
use crate::input::Risc0Groth16VerifyInput;
4+
5+
pub struct Risc0Groth16VerifyProgram;
6+
7+
impl ZkVmProgram for Risc0Groth16VerifyProgram {
8+
type Input = Risc0Groth16VerifyInput;
9+
type Output = bool;
10+
11+
fn name() -> String {
12+
"risc0_groth16_verify".to_string()
13+
}
14+
15+
fn proof_type() -> zkaleido::ProofType {
16+
ProofType::Core
17+
}
18+
19+
fn prepare_input<'a, B>(input: &'a Self::Input) -> ZkVmInputResult<B::Input>
20+
where
21+
B: zkaleido::ZkVmInputBuilder<'a>,
22+
{
23+
B::new().write_serde(input)?.build()
24+
}
25+
26+
fn process_output<H>(
27+
public_values: &zkaleido::PublicValues,
28+
) -> zkaleido::ZkVmResult<Self::Output>
29+
where
30+
H: zkaleido::ZkVmHost,
31+
{
32+
H::extract_serde_public_output(public_values)
33+
}
34+
}
35+
36+
impl ZkVmProgramPerf for Risc0Groth16VerifyProgram {}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
use std::sync::Arc;
41+
42+
use zkaleido::ZkVmProgram;
43+
use zkaleido_native_adapter::{NativeHost, NativeMachine};
44+
45+
use crate::{
46+
input::Risc0Groth16VerifyInput, process_groth16_verify_risc0,
47+
program::Risc0Groth16VerifyProgram,
48+
};
49+
50+
fn get_native_host() -> NativeHost {
51+
NativeHost {
52+
process_proof: Arc::new(Box::new(move |zkvm: &NativeMachine| {
53+
process_groth16_verify_risc0(zkvm);
54+
Ok(())
55+
})),
56+
}
57+
}
58+
59+
#[test]
60+
fn test_native() {
61+
let input = Risc0Groth16VerifyInput::load();
62+
let host = get_native_host();
63+
let receipt = Risc0Groth16VerifyProgram::prove(&input, &host).unwrap();
64+
let risc0_verified =
65+
Risc0Groth16VerifyProgram::process_output::<NativeHost>(receipt.public_values())
66+
.unwrap();
67+
68+
assert!(risc0_verified);
69+
}
70+
}

0 commit comments

Comments
 (0)