Skip to content

Commit

Permalink
feat(example): update worker_compress_proofs to handle reduce-layout
Browse files Browse the repository at this point in the history
  • Loading branch information
dongchangYoo committed Aug 13, 2024
1 parent 357d7f5 commit 7c52236
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 9 deletions.
5 changes: 3 additions & 2 deletions examples/fibonacci/script/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn main() {
// Parse the command line arguments.
let args = ProveArgs::parse();

let raw_core_proof = scenario::core_prove::mpc_prove_core(args.clone()).unwrap();
let _ = scenario::core_prove::scenario_end(args, &raw_core_proof);
let (core_proof, compress_proof) =
scenario::compress_prove::mpc_prove_compress(args.clone()).unwrap();
scenario::compress_prove::scenario_end(args, &core_proof, &compress_proof)
}
54 changes: 51 additions & 3 deletions examples/fibonacci/script/src/scenario/compress_prove.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
use super::core_prove::mpc_prove_core;
use crate::{
operator::operator_prepare_compress_inputs, worker::worker_compress_proofs, ProveArgs,
common,
operator::{operator_prepare_compress_input_chunks, operator_prepare_compress_inputs},
scenario,
worker::worker_compress_proofs,
ProveArgs,
};
use anyhow::Result;
use sp1_core::{stark::ShardProof, utils::BabyBearPoseidon2};
use sp1_prover::SP1ReduceProof;
use sp1_sdk::{SP1Proof, SP1ProofWithPublicValues};

pub fn mpc_prove_compress(args: ProveArgs) -> Result<Vec<u8>> {
pub fn mpc_prove_compress(args: ProveArgs) -> Result<(Vec<u8>, Vec<u8>)> {
let core_proof = mpc_prove_core(args.clone()).unwrap();

let serialize_args = bincode::serialize(&args).unwrap();
Expand Down Expand Up @@ -37,5 +44,46 @@ pub fn mpc_prove_compress(args: ProveArgs) -> Result<Vec<u8>> {
compressed_proofs.push(compressed_proof);
}

Ok(Vec::new())
let mut compress_layer_proofs = compressed_proofs;
let compressed_proof = loop {
// Operator
let mut red_layout = Vec::new();
operator_prepare_compress_input_chunks(&compress_layer_proofs, &mut red_layout);

// Worker
compress_layer_proofs = Vec::new();
for layout in red_layout.iter() {
let mut compressed_proof = Vec::new();
worker_compress_proofs(&serialize_args, &layout, 2, None, &mut compressed_proof);
compress_layer_proofs.push(compressed_proof);
}
if compress_layer_proofs.len() == 1 {
break compress_layer_proofs.remove(0);
}
};

let shard_proof: ShardProof<BabyBearPoseidon2> =
bincode::deserialize(&compressed_proof).unwrap();
let proof = SP1ReduceProof { proof: shard_proof };

let proof = bincode::serialize(&proof).unwrap();
Ok((core_proof, proof))
}

pub fn scenario_end(args: ProveArgs, core_proof: &Vec<u8>, compress_proof: &Vec<u8>) {
let compress_proof_obj: SP1ReduceProof<BabyBearPoseidon2> =
bincode::deserialize(compress_proof).unwrap();

let (client, _, _, vk) = common::init_client(args.clone());
let core_proof = scenario::core_prove::scenario_end(args, &core_proof).unwrap();

let proof = SP1ProofWithPublicValues {
proof: SP1Proof::Compressed(compress_proof_obj.proof),
stdin: core_proof.stdin,
public_values: core_proof.public_values,
sp1_version: client.prover.version().to_string(),
};

client.verify(&proof, &vk).unwrap();
tracing::info!("Successfully verified compress proof");
}
9 changes: 7 additions & 2 deletions examples/fibonacci/script/src/worker/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod steps;

use crate::common;
use crate::common::memory_layouts::SerializableReduceLayout;
use crate::{
common::{
memory_layouts::{SerializableDeferredLayout, SerializableRecursionLayout},
Expand All @@ -12,7 +13,8 @@ use crate::{
use sp1_core::{runtime::ExecutionState, stark::MachineProver};
use steps::{
worker_commit_checkpoint_impl, worker_compress_proofs_for_deferred,
worker_compress_proofs_for_recursion, worker_prove_checkpoint_impl,
worker_compress_proofs_for_recursion, worker_compress_proofs_for_reduce,
worker_prove_checkpoint_impl,
};

pub fn worker_commit_checkpoint(
Expand Down Expand Up @@ -79,7 +81,10 @@ pub fn worker_compress_proofs(
bincode::deserialize(last_proof_public_values.unwrap()).unwrap();
worker_compress_proofs_for_deferred(args_obj, layout, last_public_values).unwrap()
}
LayoutType::Reduce => panic!("Reduce layout is not supported"),
LayoutType::Reduce => {
let layout: SerializableReduceLayout = bincode::deserialize(layout).unwrap();
worker_compress_proofs_for_reduce(args_obj, layout).unwrap()
}
};
*o_proof = bincode::serialize(&compressed_shard_proof).unwrap();
}
34 changes: 32 additions & 2 deletions examples/fibonacci/script/src/worker/steps.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::common;
use crate::common::memory_layouts::{SerializableDeferredLayout, SerializableRecursionLayout};
use crate::common::memory_layouts::{
SerializableDeferredLayout, SerializableRecursionLayout, SerializableReduceLayout,
};
use crate::common::types::{
ChallengerType, CommitmentType, DeferredLayout, RecordType, RecursionLayout,
ChallengerType, CommitmentType, DeferredLayout, RecordType, RecursionLayout, ReduceLayout,
};
use crate::ProveArgs;
use anyhow::Result;
Expand Down Expand Up @@ -220,3 +222,31 @@ pub fn worker_compress_proofs_for_deferred(
.map(|p| (p, ReduceProgramType::Deferred))
.map_err(|e| anyhow::anyhow!("failed to compress machine proof: {:?}", e))
}

pub fn worker_compress_proofs_for_reduce(
args: ProveArgs,
layout: SerializableReduceLayout,
) -> Result<(ShardProof<BabyBearPoseidon2>, ReduceProgramType)> {
let (client, _, pk, _) = common::init_client(args.clone());
let (_, opts, _) = common::bootstrap(&client, &pk).unwrap();

let sp1_prover = client.prover.sp1_prover();

let input = ReduceLayout {
compress_vk: &sp1_prover.compress_vk,
recursive_machine: sp1_prover.compress_prover.machine(),
shard_proofs: layout.shard_proofs,
kinds: layout.kinds,
is_complete: layout.is_complete,
};

sp1_prover
.compress_machine_proof(
input,
&sp1_prover.compress_program,
&sp1_prover.compress_pk,
opts,
)
.map(|p| (p, ReduceProgramType::Reduce))
.map_err(|e| anyhow::anyhow!("failed to compress machine proof: {:?}", e))
}

0 comments on commit 7c52236

Please sign in to comment.