Skip to content

Example of recursive prover #1987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 63 additions & 24 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ members = [
"ssz-withdrawals/script",
"tendermint/program",
"tendermint/script",
"recursive/lib",
"recursive/program",
"recursive/script",
]
resolver = "2"

Expand Down Expand Up @@ -76,4 +79,4 @@ substrate-bn = { git = "https://github.com/sp1-patches/bn", tag = "patch-0.6.0-s
bls12_381 = { git = "https://github.com/sp1-patches/bls12_381", tag = "patch-0.8.0-sp1-4.0.0" }

# todo!(n) remove
sp1-lib = { path = "../crates/zkvm/lib" }
sp1-lib = { path = "../crates/zkvm/lib" }
Binary file modified examples/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
10 changes: 10 additions & 0 deletions examples/recursive/lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "recursive-lib"
version = "1.1.0"
edition = "2021"
publish = false

[dependencies]
sha2 = "0.10.8"
sp1-zkvm = { path = "../../../crates/zkvm/entrypoint", features = ["verify"] }
serde = { workspace = true }
41 changes: 41 additions & 0 deletions examples/recursive/lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
pub mod utils;

use serde::{Deserialize, Serialize};
use crate::utils::sha256_hash;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CircuitInput {
pub public_input_hash: [u8; 32],
pub public_value: u32,
pub private_value: u32,
}

impl CircuitInput {
pub fn new(public_input_hash: [u8; 32], public_value: u32, private_value: u32) -> Self {
Self {
public_input_hash,
public_value,
private_value,
}
}
}

/// Given `x_0, x_1, x_2, ...., x_{n - 1}` as public values, we want to know `x_0^3 + x_1^3 +
/// x_2^3 + ...+ x_{n - 1}^3`
///
/// core function, say `f(y, x) = y + x^3`, where:
/// 1) y, is the accumulation result, marked as private value
/// 2) x, is the public value
pub fn acc_cubic(public_value: u32, private_value: u32) -> u32 {
private_value.wrapping_add(public_value.wrapping_mul(public_value).wrapping_mul(public_value))
}

/// Verify last prover's proof with two public values:
/// 1) public_input_hash, which is a recursive hash of all public values of acc_cubic `f(y, x)`
/// 2) y, last prover's accumulated result
pub fn verify_proof(vkey_hash: &[u32; 8], public_input_hash: &[u8; 32], private_value: u32) {
let mut bytes = Vec::with_capacity(36);
bytes.extend_from_slice(public_input_hash);
bytes.extend_from_slice(&private_value.to_le_bytes());
sp1_zkvm::lib::verify::verify_sp1_proof(vkey_hash, &sha256_hash(&bytes));
}
16 changes: 16 additions & 0 deletions examples/recursive/lib/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use sha2::{Digest, Sha256};

pub trait AsLittleEndianBytes {
fn to_little_endian(self) -> Self;
}

impl<const N: usize> AsLittleEndianBytes for [u8; N] {
fn to_little_endian(mut self) -> Self {
self.reverse();
self
}
}

pub fn sha256_hash(bytes: &[u8]) -> [u8; 32] {
Sha256::digest(bytes).into()
}
9 changes: 9 additions & 0 deletions examples/recursive/program/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
version = "0.1.0"
name = "recursive-program"
edition = "2021"
publish = false

[dependencies]
sp1-zkvm = { path = "../../../crates/zkvm/entrypoint", features = ["verify"] }
recursive-lib = { path = "../lib" }
Loading
Loading