Skip to content

Commit

Permalink
Merge pull request #9 from kroma-network/feat/integrate-kona-guest
Browse files Browse the repository at this point in the history
feat: make the multi-prover running with a random guest.“
  • Loading branch information
dongchangYoo authored Aug 16, 2024
2 parents aad6eee + 4582ea2 commit bd78e12
Show file tree
Hide file tree
Showing 19 changed files with 217 additions and 170 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions examples/Cargo.lock

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

30 changes: 3 additions & 27 deletions examples/fibonacci/script/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,24 @@
pub mod common;
pub mod operator;
pub mod scenario;
pub mod worker;

use alloy_sol_types::{sol, SolType};
use clap::{ArgEnum, Parser};
use clap::Parser;
use serde::{Deserialize, Serialize};
use sp1_sdk::{HashableKey, SP1ProofWithPublicValues, SP1VerifyingKey};
use std::path::PathBuf;

#[derive(ArgEnum, Debug, Clone, Serialize, Deserialize, PartialEq)]
#[clap(rename_all = "kebab_case")]
pub enum ProofType {
CORE,
COMPRESS,
PLONK,
}

/// The arguments for the prove command.
#[derive(Parser, Debug, Clone, Serialize, Deserialize)]
pub struct ProveArgs {
pub struct FibonacciArgs {
#[clap(long, default_value = "20")]
pub n: u32,

#[clap(long, default_value = "false")]
pub evm: bool,
}

impl ProveArgs {
pub fn to_bytes(&self) -> Vec<u8> {
bincode::serialize(self).unwrap()
}

pub fn from_slice(args: &[u8]) -> Self {
bincode::deserialize(args).unwrap()
}
}

/// The public values encoded as a tuple that can be easily deserialized inside Solidity.
pub type PublicValuesTuple = sol! {
tuple(uint32, uint32, uint32)
};

/// A fixture that can be used to test the verification of SP1 zkVM proofs inside Solidity.
// A fixture that can be used to test the verification of SP1 zkVM proofs inside Solidity.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct SP1FibonacciProofFixture {
Expand Down
21 changes: 12 additions & 9 deletions examples/fibonacci/script/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@
//! RUST_LOG=info cargo run --package fibonacci-script --bin prove --release
//! ```

pub mod common;
pub mod operator;
pub mod worker;

use clap::Parser;
use fibonacci_script::{scenario, ProveArgs};
use fibonacci_script::FibonacciArgs;
use sp1_sdk::multi_prover::{common::ProveArgs, scenario};

pub const FIBONACCI_ELF: &[u8] = include_bytes!("../../program/elf/riscv32im-succinct-zkvm-elf");

fn main() {
// Setup the logger.
sp1_sdk::utils::setup_logger();
// Parse the command line arguments.
let args = ProveArgs::parse();
let fibonacci_args = FibonacciArgs::parse();

let args = ProveArgs {
zkvm_input: fibonacci_args.n.to_le_bytes().to_vec(),
elf: FIBONACCI_ELF.to_vec(),
};

let (core_proof, _, plonk_proof) =
scenario::plonk_prove::mpc_prove_plonk(args.clone()).unwrap();
scenario::plonk_prove::scenario_end(args, &core_proof, &plonk_proof)
let (core_proof, _, plonk_proof) = scenario::plonk_prove::mpc_prove_plonk(&args).unwrap();
scenario::plonk_prove::scenario_end(&args, &core_proof, &plonk_proof)
}
4 changes: 4 additions & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ p3-commit = { workspace = true }
p3-field = { workspace = true }
p3-baby-bear = { workspace = true }
p3-fri = { workspace = true }
p3-challenger = { workspace = true }
p3-poseidon2 = { workspace = true }
p3-symmetric = { workspace = true }
sp1-recursion-core = { workspace = true }
indicatif = "0.17.8"
tracing = "0.1.40"
hex = "0.4.3"
Expand Down
1 change: 1 addition & 0 deletions sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod proto {
pub mod action;
pub mod artifacts;
pub mod install;
pub mod multi_prover;
#[cfg(feature = "network")]
pub mod network;
#[cfg(feature = "network")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::types::{DeferredLayout, RecursionLayout, ReduceLayout};
use crate::operator::utils::ChallengerState;
use crate::multi_prover::operator::utils::ChallengerState;
use p3_baby_bear::BabyBear;
use serde::{Deserialize, Serialize};
use sp1_core::{air::Word, stark::ShardProof, utils::BabyBearPoseidon2};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
pub mod types;
pub mod memory_layouts;
pub mod types;

use crate::{ProverClient, SP1ProofKind};
use anyhow::Result;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use sp1_core::{
runtime::{Program, Runtime},
runtime::{Program, Runtime, SP1Context, SP1ContextBuilder},
utils::SP1ProverOpts,
};
use sp1_prover::types::{SP1ProvingKey, SP1VerifyingKey};
use sp1_sdk::{ProverClient, SP1Context, SP1ContextBuilder, SP1ProofKind, SP1Stdin};
use sp1_prover::{
types::{SP1ProvingKey, SP1VerifyingKey},
SP1Stdin,
};
use std::sync::Arc;
use sysinfo::System;

use crate::ProveArgs;

static LIMIT_RAM_GB: u64 = 120;

/// The ELF (executable and linkable format) file for the Succinct RISC-V zkVM.
///
/// This file is generated by running `cargo prove build` inside the `program` directory.
pub const FIBONACCI_ELF: &[u8] = include_bytes!("../../../program/elf/riscv32im-succinct-zkvm-elf");
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ProveArgs<T: Serialize> {
pub zkvm_input: T,
pub elf: Vec<u8>,
}

impl<T: Serialize + DeserializeOwned> ProveArgs<T> {
pub fn to_bytes(&self) -> Vec<u8> {
bincode::serialize(self).unwrap()
}

pub fn from_slice(args: &[u8]) -> Self {
bincode::deserialize(args).unwrap()
}
}

pub fn init_client(args: ProveArgs) -> (ProverClient, SP1Stdin, SP1ProvingKey, SP1VerifyingKey) {
pub fn init_client<T: Serialize>(
args: &ProveArgs<T>,
) -> (ProverClient, SP1Stdin, SP1ProvingKey, SP1VerifyingKey) {
let client = ProverClient::new();
let (pk, vk) = client.setup(FIBONACCI_ELF);
let (pk, vk) = client.setup(&args.elf);
let mut stdin = SP1Stdin::new();
stdin.write(&args.n);
stdin.write(&args.zkvm_input);

(client, stdin, pk, vk)
}
Expand All @@ -33,6 +48,7 @@ pub fn bootstrap<'a>(
client: &'a ProverClient,
pk: &SP1ProvingKey,
) -> Result<(Program, SP1ProverOpts, SP1Context<'a>)> {
// TODO(Ethan): remove `kind` since it is not used.
let kind = SP1ProofKind::default();
let opts = SP1ProverOpts::default();

Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions sdk/src/multi_prover/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod common;
pub mod operator;
pub mod scenario;
pub mod worker;
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
pub mod steps;
pub mod utils;

use crate::common;
use crate::common::{
use crate::multi_prover::common::{self, ProveArgs};
use crate::multi_prover::common::{
memory_layouts::{SerializableDeferredLayout, SerializableRecursionLayout},
types::{CommitmentType, PublicValueStreamType},
};
use crate::ProveArgs;
use p3_baby_bear::BabyBear;
use serde::de::DeserializeOwned;
use serde::Serialize;
use sp1_core::air::{PublicValues, Word};
use sp1_core::stark::{MachineProver, StarkGenericConfig};
use sp1_core::utils::BabyBearPoseidon2;
Expand All @@ -20,16 +21,16 @@ use steps::{
};
use utils::{read_bin_file_to_vec, ChallengerState};

pub fn operator_split_into_checkpoints(
pub fn operator_split_into_checkpoints<T: Serialize + DeserializeOwned>(
args: &[u8],
o_public_values_stream: &mut Vec<u8>,
o_public_values: &mut Vec<u8>,
o_checkpoints: &mut Vec<Vec<u8>>,
o_cycles: &mut u64,
) {
let args_obj = ProveArgs::from_slice(args);
let args_obj: ProveArgs<T> = ProveArgs::from_slice(args);
let (public_values_stream, public_values, checkpoints, cycles) =
operator_split_into_checkpoints_impl(args_obj).unwrap();
operator_split_into_checkpoints_impl(&args_obj).unwrap();

*o_public_values_stream = bincode::serialize(&public_values_stream).unwrap();
*o_public_values = bincode::serialize(&public_values).unwrap();
Expand All @@ -40,13 +41,13 @@ pub fn operator_split_into_checkpoints(
*o_cycles = cycles;
}

pub fn operator_absorb_commits(
args: &Vec<u8>,
pub fn operator_absorb_commits<T: Serialize + DeserializeOwned>(
args: &[u8],
commitments_vec: &[Vec<Vec<u8>>],
records_vec: &[Vec<Vec<u8>>],
o_challenger_state: &mut Vec<u8>,
) {
let args_obj = ProveArgs::from_slice(args.as_slice());
let args_obj: ProveArgs<T> = ProveArgs::from_slice(args);
let commitments_vec: Vec<Vec<CommitmentType>> = commitments_vec
.iter()
.map(|commitments| {
Expand All @@ -73,18 +74,18 @@ pub fn operator_absorb_commits(
.sum::<usize>()
);

let challenger = operator_absorb_commits_impl(args_obj, commitments_vec, records_vec).unwrap();
let challenger = operator_absorb_commits_impl(&args_obj, commitments_vec, records_vec).unwrap();
*o_challenger_state = ChallengerState::from(&challenger).to_bytes();
}

pub fn operator_construct_sp1_core_proof(
pub fn operator_construct_sp1_core_proof<T: Serialize + DeserializeOwned>(
args: &Vec<u8>,
shard_proofs_vec: &[Vec<Vec<u8>>],
public_values_stream: &[u8],
cycles: u64,
o_proof: &mut Vec<u8>,
) {
let args_obj = ProveArgs::from_slice(args.as_slice());
let args_obj: ProveArgs<T> = ProveArgs::from_slice(args.as_slice());
let shard_proofs_vec_obj = shard_proofs_vec
.iter()
.map(|proofs| {
Expand All @@ -98,7 +99,7 @@ pub fn operator_construct_sp1_core_proof(
bincode::deserialize(public_values_stream).unwrap();

let proof = construct_sp1_core_proof_impl(
args_obj,
&args_obj,
shard_proofs_vec_obj,
public_values_stream_obj,
cycles,
Expand All @@ -107,17 +108,17 @@ pub fn operator_construct_sp1_core_proof(
*o_proof = bincode::serialize(&proof).unwrap();
}

pub fn operator_prepare_compress_inputs(
pub fn operator_prepare_compress_inputs<T: Serialize + DeserializeOwned>(
args: &Vec<u8>,
core_proof: &[u8],
o_rec_layouts: &mut Vec<Vec<u8>>,
o_def_layouts: &mut Vec<Vec<u8>>,
o_last_proof_public_values: &mut Vec<u8>,
) {
let args_obj = ProveArgs::from_slice(args.as_slice());
let args_obj: ProveArgs<T> = ProveArgs::from_slice(args.as_slice());
let core_proof_obj: SP1CoreProof = bincode::deserialize(&core_proof).unwrap();

let (client, stdin, _, vk) = common::init_client(args_obj);
let (client, stdin, _, vk) = common::init_client(&args_obj);

let mut leaf_challenger = client.prover.sp1_prover().core_prover.config().challenger();
let (core_inputs, deferred_inputs) = operator_prepare_compress_inputs_impl(
Expand Down Expand Up @@ -174,26 +175,30 @@ pub fn operator_prepare_compress_input_chunks(
.collect();
}

pub fn operator_prove_shrink(
args: &Vec<u8>,
pub fn operator_prove_shrink<T: Serialize + DeserializeOwned>(
args: &[u8],
compressed_proof: &[u8],
o_shrink_proof: &mut Vec<u8>,
) {
let args_obj = ProveArgs::from_slice(args.as_slice());
let args_obj: ProveArgs<T> = ProveArgs::from_slice(args);
let compressed_proof_obj: SP1ReduceProof<BabyBearPoseidon2> =
bincode::deserialize(compressed_proof).unwrap();

let shrink_proof = operator_prove_shrink_impl(args_obj, compressed_proof_obj).unwrap();
let shrink_proof = operator_prove_shrink_impl(&args_obj, compressed_proof_obj).unwrap();

*o_shrink_proof = bincode::serialize(&shrink_proof).unwrap();
}

pub fn operator_prove_plonk(args: &Vec<u8>, shrink_proof: &[u8], o_plonk_proof: &mut Vec<u8>) {
let args_obj = ProveArgs::from_slice(args.as_slice());
pub fn operator_prove_plonk<T: Serialize + DeserializeOwned>(
args: &Vec<u8>,
shrink_proof: &[u8],
o_plonk_proof: &mut Vec<u8>,
) {
let args_obj: ProveArgs<T> = ProveArgs::from_slice(args.as_slice());
let shrink_proof_obj: SP1ReduceProof<BabyBearPoseidon2> =
bincode::deserialize(shrink_proof).unwrap();

let plonk_proof = operator_prove_plonk_impl(args_obj, shrink_proof_obj).unwrap();
let plonk_proof = operator_prove_plonk_impl(&args_obj, shrink_proof_obj).unwrap();

*o_plonk_proof = bincode::serialize(&plonk_proof).unwrap();
}
Loading

0 comments on commit bd78e12

Please sign in to comment.