Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ctian1 committed Apr 9, 2024
1 parent 6b348cd commit 374d157
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 146 deletions.
4 changes: 2 additions & 2 deletions prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl SP1ProverImpl {
let machine = RiscvAir::machine(config.clone());
let program = Program::from(elf);
let (_, vk) = machine.setup(&program);
let reduce_program = build_reduce(vk.chip_information.clone());
let reduce_program = build_reduce();
println!("nb_shards {}", proof.shard_proofs.len());
let config = InnerSC::default();

Expand Down Expand Up @@ -148,7 +148,7 @@ pub fn prove_sp1() -> (Proof<InnerSC>, VerifyingKey<InnerSC>) {
(proof, vk)
}
pub fn prove_compress(sp1_proof: Proof<InnerSC>, vk: VerifyingKey<InnerSC>) {
let program = build_reduce(vk.chip_information.clone());
let program = build_reduce();
todo!()
// let config = InnerSC::default();
// let machine = InnerA::machine(config);
Expand Down
45 changes: 1 addition & 44 deletions recursion/program/src/reduce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,27 @@ use crate::stark::StarkVerifier;
use p3_baby_bear::BabyBear;
use p3_baby_bear::DiffusionMatrixBabybear;
use p3_challenger::DuplexChallenger;
use p3_challenger::{CanObserve, FieldChallenger};
use p3_commit::ExtensionMmcs;
use p3_commit::TwoAdicMultiplicativeCoset;
use p3_field::extension::BinomialExtensionField;
use p3_field::AbstractField;
use p3_field::Field;
use p3_field::TwoAdicField;
use p3_fri::FriConfig;
use p3_matrix::Dimensions;
use p3_merkle_tree::FieldMerkleTreeMmcs;
use p3_poseidon2::Poseidon2;
use p3_poseidon2::Poseidon2ExternalMatrixGeneral;
use p3_symmetric::PaddingFreeSponge;
use p3_symmetric::TruncatedPermutation;
use sp1_core::air::PublicValues;
use sp1_core::air::Word;
use sp1_core::stark::Dom;
use sp1_core::stark::Proof;
use sp1_core::stark::ShardProof;
use sp1_core::stark::VerifyingKey;
use sp1_core::stark::{RiscvAir, StarkGenericConfig};
use sp1_recursion_compiler::asm::AsmConfig;
use sp1_recursion_compiler::asm::VmBuilder;
use sp1_recursion_compiler::ir::Array;
use sp1_recursion_compiler::ir::Builder;
use sp1_recursion_compiler::ir::Felt;
use sp1_recursion_compiler::ir::MemVariable;
use sp1_recursion_compiler::ir::Usize;
use sp1_recursion_compiler::ir::Var;
use sp1_recursion_compiler::ir::Variable;
use sp1_recursion_core::air::Block;
use sp1_recursion_core::runtime::Program as RecursionProgram;
use sp1_recursion_core::runtime::DIGEST_SIZE;
use sp1_recursion_core::stark::config::inner_fri_config;
Expand Down Expand Up @@ -96,49 +86,30 @@ fn clone<T: MemVariable<C>>(builder: &mut RecursionBuilder, var: &T) -> T {
builder.get(&arr, 0)
}

// TODO: proof is only necessary now because it's a constant, it should be I/O soon
pub fn build_reduce(chip_information: Vec<(String, Dom<SC>, Dimensions)>) -> RecursionProgram<Val> {
pub fn build_reduce() -> RecursionProgram<Val> {
let sp1_machine = RiscvAir::machine(SC::default());

let time = Instant::now();
let mut builder = VmBuilder::<F, EF>::default();
let config = const_fri_config(&mut builder, inner_fri_config());
let pcs = TwoAdicFriPcsVariable { config };

// let mut challenger = DuplexChallengerVariable::new(&mut builder);

// let preprocessed_commit_val: [F; DIGEST_SIZE] = sp1_vk.commit.into();
// let preprocessed_commit: Array<C, _> = builder.eval_const(preprocessed_commit_val.to_vec());
// challenger.observe(&mut builder, preprocessed_commit);

// Read witness inputs
let proofs = Vec::<ShardProof<_>>::read(&mut builder);
let is_recursive_flags = Vec::<usize>::read(&mut builder);
let sorted_indices = Vec::<Vec<usize>>::read(&mut builder);
let start_challenger = DuplexChallenger::read(&mut builder);
let mut reconstruct_challenger = DuplexChallenger::read(&mut builder);
let prep_sorted_indices = Vec::<usize>::read(&mut builder);
builder
.range(Usize::Const(0), prep_sorted_indices.len())
.for_each(|i, builder| {
let index = builder.get(&prep_sorted_indices, i);
let code = builder.eval_const(F::from_canonical_u32(10001));
builder.print_f(code);
builder.print_v(index);
});
let prep_domains = Vec::<TwoAdicMultiplicativeCoset<BabyBear>>::read(&mut builder);
let sp1_vk = VerifyingKey::<SC>::read(&mut builder);
let recursion_vk = VerifyingKey::<SC>::read(&mut builder);
let num_proofs = proofs.len();
let code = builder.eval_const(F::from_canonical_u32(1));
builder.print_f(code);

let pre_start_challenger = clone(&mut builder, &start_challenger);
let pre_reconstruct_challenger = clone(&mut builder, &reconstruct_challenger);
let zero: Var<_> = builder.eval_const(F::zero());
let one: Var<_> = builder.eval_const(F::one());
let code = builder.eval_const(F::from_canonical_u32(2));
builder.print_f(code);
builder
.range(Usize::Const(0), num_proofs)
.for_each(|i, builder| {
Expand All @@ -149,29 +120,18 @@ pub fn build_reduce(chip_information: Vec<(String, Dom<SC>, Dimensions)>) -> Rec
// Non-recursive proof
|builder| {
builder.if_eq(proof.index, one).then(|builder| {
let code = builder.eval_const(F::from_canonical_u32(3));
builder.print_f(code);

// Initialize the current challenger
// let h: [BabyBear; DIGEST_SIZE] = sp1_vk.commit.into();
// let const_commit: DigestVariable<C> = builder.eval_const(h.to_vec());
reconstruct_challenger = DuplexChallengerVariable::new(builder);
reconstruct_challenger.observe(builder, sp1_vk.commitment.clone());
// for j in 0..DIGEST_SIZE {
// let element = builder.get(&sp1_vk.commit, j);
// reconstruct_challenger.observe(builder, element);
// }
// reconstruct_challenger
// .observe_slice(builder, &recursion_vk.commitment);
});
for j in 0..DIGEST_SIZE {
let element = builder.get(&proof.commitment.main_commit, j);
reconstruct_challenger.observe(builder, element);
// TODO: observe public values
// challenger.observe_slice(&public_values.to_vec());
}
let code = builder.eval_const(F::from_canonical_u32(4));
builder.print_f(code);
// reconstruct_challenger
// .observe_slice(builder, &proof.commitment.main_commit.vec());
let mut current_challenger = start_challenger.as_clone(builder);
Expand All @@ -183,12 +143,9 @@ pub fn build_reduce(chip_information: Vec<(String, Dom<SC>, Dimensions)>) -> Rec
&mut current_challenger,
&proof,
sorted_indices.clone(),
chip_information.clone(),
prep_sorted_indices.clone(),
prep_domains.clone(),
);
let code = builder.eval_const(F::from_canonical_u32(5));
builder.print_f(code);
},
// Recursive proof
|builder| {},
Expand Down
105 changes: 5 additions & 100 deletions recursion/program/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ where
challenger: &mut DuplexChallengerVariable<C>,
proof: &ShardProofVariable<C>,
chip_sorted_idxs: Array<C, Var<C::N>>,
chip_information: Vec<(String, Dom<SC>, Dimensions)>,
preprocessed_sorted_idxs: Array<C, Var<C::N>>,
prep_domains: Array<C, TwoAdicMultiplicativeCosetVariable<C>>,
) where
Expand Down Expand Up @@ -111,11 +110,16 @@ where
let mut qc_points = builder.dyn_array::<Ext<_, _>>(1);
builder.set(&mut qc_points, 0, zeta);

// Iterate through machine.chips filtered for preprocessed chips.
for (preprocessed_id, chip_id) in machine.preprocessed_chip_ids().into_iter().enumerate() {
// Get index within sorted preprocessed chips.
let preprocessed_sorted_id = builder.get(&preprocessed_sorted_idxs, preprocessed_id);
// Get domain from witnessed domains. Array is ordered by machine.chips ordering.
let domain = builder.get(&prep_domains, preprocessed_id);

// Get index within all sorted chips.
let chip_sorted_id = builder.get(&chip_sorted_idxs, chip_id);
// Get opening from proof.
let opening = builder.get(&opened_values.chips, chip_sorted_id);

let mut trace_points = builder.dyn_array::<Ext<_, _>>(2);
Expand Down Expand Up @@ -379,105 +383,6 @@ pub(crate) mod tests {
);
}

#[test]
fn test_recursive_verify_shard() {
// Generate a dummy proof.
sp1_core::utils::setup_logger();

let elf =
include_bytes!("../../../examples/fibonacci/program/elf/riscv32im-succinct-zkvm-elf");

let machine = A::machine(SC::default());

let (_, vk) = machine.setup(&Program::from(elf));
let proof = SP1Prover::prove_with_config(elf, SP1Stdin::new(), machine.config().clone())
.unwrap()
.proof;
let mut challenger_ver = machine.config().challenger();
machine.verify(&vk, &proof, &mut challenger_ver).unwrap();
println!("Proof generated successfully");

let mut challenger_val = machine.config().challenger();
challenger_val.observe(vk.commit);
proof.shard_proofs.iter().for_each(|proof| {
challenger_val.observe(proof.commitment.main_commit);
let public_values_field = PublicValues::<Word<F>, F>::new(proof.public_values);
challenger_val.observe_slice(&public_values_field.to_vec());
});

let time = Instant::now();
let mut builder = Builder::<InnerConfig>::default();
let config = const_fri_config(&mut builder, inner_fri_config());
let pcs = TwoAdicFriPcsVariable { config };

let mut challenger = DuplexChallengerVariable::new(&mut builder);

let preprocessed_commit_val: [F; DIGEST_SIZE] = vk.commit.into();
let preprocessed_commit: Array<C, _> = builder.eval_const(preprocessed_commit_val.to_vec());
challenger.observe(&mut builder, preprocessed_commit);

let mut witness_stream = Vec::new();
let mut shard_proofs = vec![];
let mut sorted_indices = vec![];
for proof_val in proof.shard_proofs {
witness_stream.extend(proof_val.write());
let sorted_indices_raw: Vec<usize> = machine
.chips_sorted_indices(&proof_val)
.into_iter()
.map(|x| match x {
Some(x) => x,
None => EMPTY,
})
.collect();
witness_stream.extend(sorted_indices_raw.write());
let proof = ShardProof::<_>::read(&mut builder);
let sorted_indices_arr = Vec::<usize>::read(&mut builder);
builder
.range(0, sorted_indices_arr.len())
.for_each(|i, builder| {
let el = builder.get(&sorted_indices_arr, i);
builder.print_v(el);
});
let ShardCommitmentVariable { main_commit, .. } = &proof.commitment;
challenger.observe(&mut builder, main_commit.clone());
let public_values_field = PublicValues::<Word<F>, F>::new(proof_val.public_values);
let public_values_felt: Vec<Felt<F>> = public_values_field
.to_vec()
.iter()
.map(|x| builder.eval(*x))
.collect();
challenger.observe_slice(&mut builder, &public_values_felt);
shard_proofs.push(proof);
sorted_indices.push(sorted_indices_arr);
}

let code = builder.eval(InnerVal::two());
builder.print_v(code);
for (proof, sorted_indices) in shard_proofs.iter().zip(sorted_indices) {
StarkVerifier::<C, SC>::verify_shard(
&mut builder,
&vk,
&pcs,
&machine,
&mut challenger.clone(),
proof,
sorted_indices,
);
}

let program = builder.compile();
let elapsed = time.elapsed();
println!("Building took: {:?}", elapsed);

let time = Instant::now();
let mut runtime = Runtime::<F, EF, _>::new(&program, machine.config().perm.clone());
runtime.witness_stream = witness_stream;
runtime.run();
let elapsed = time.elapsed();
runtime.print_stats();
println!("Execution took: {:?}", elapsed);
}

#[test]
#[ignore]
fn test_kitchen_sink() {
Expand Down

0 comments on commit 374d157

Please sign in to comment.