Skip to content

Commit

Permalink
Chore/Redesign VM trace generation (#109)
Browse files Browse the repository at this point in the history
* Refactor memory chip and program chip

* Progress (currently debugging CPU tests)

* All tests passing

* Remove warning for read/write macro in cpu trace

* Add in change that got left out

* Lint field_arithmetic/trace.rs

* Fix lint, remove rust backtrace line

* Fix compiler/src/util.rs, remove debugging code

* Comment out test_compiler_break
  • Loading branch information
TlatoaniHJ authored Jul 3, 2024
1 parent 98f72ae commit 12da2a5
Show file tree
Hide file tree
Showing 23 changed files with 740 additions and 734 deletions.
28 changes: 18 additions & 10 deletions compiler/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use p3_field::PrimeField32;
use stark_vm::cpu::{
trace::{Instruction, ProgramExecution},
CpuAir, CpuOptions,
use stark_vm::{
cpu::trace::Instruction,
vm::{
config::{VmConfig, VmParamsConfig},
VirtualMachine,
},
};

pub fn canonical_i32_to_field<F: PrimeField32>(x: i32) -> F {
Expand All @@ -14,13 +17,18 @@ pub fn canonical_i32_to_field<F: PrimeField32>(x: i32) -> F {
}
}

pub fn execute_program<const WORD_SIZE: usize, F: PrimeField32>(
program: Vec<Instruction<F>>,
) -> ProgramExecution<WORD_SIZE, F> {
let cpu = CpuAir::new(CpuOptions {
field_arithmetic_enabled: true,
});
cpu.generate_program_execution(program).unwrap()
pub fn execute_program<const WORD_SIZE: usize, F: PrimeField32>(program: Vec<Instruction<F>>) {
let mut vm = VirtualMachine::<WORD_SIZE, _>::new(
VmConfig {
vm: VmParamsConfig {
field_arithmetic_enabled: true,
limb_bits: 28,
decomp: 4,
},
},
program,
);
vm.traces().unwrap();
}

pub fn display_program<F: PrimeField32>(program: &[Instruction<F>]) {
Expand Down
4 changes: 2 additions & 2 deletions compiler/tests/for_loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ fn test_compiler_break() {

builder.halt();

let program = builder.compile_isa();
execute_program::<WORD_SIZE, _>(program);
// let program = builder.compile_isa();
// execute_program::<WORD_SIZE, _>(program);

// println!("{}", code);

Expand Down
10 changes: 5 additions & 5 deletions vm/bin/src/commands/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use clap::Parser;
use color_eyre::eyre::Result;
use itertools::Itertools;
use p3_matrix::Matrix;
use stark_vm::vm::{config::VmConfig, VirtualMachine};
use stark_vm::vm::{config::VmConfig, get_chips, VirtualMachine};

use crate::asm::parse_asm_file;

Expand Down Expand Up @@ -52,12 +52,12 @@ impl KeygenCommand {

fn execute_helper(self, config: VmConfig) -> Result<()> {
let instructions = parse_asm_file(Path::new(&self.asm_file_path.clone()))?;
let vm = VirtualMachine::<WORD_SIZE, _>::new(config, instructions)?;
let engine = config::baby_bear_poseidon2::default_engine(vm.max_log_degree());
let mut vm = VirtualMachine::<WORD_SIZE, _>::new(config, instructions);
let engine = config::baby_bear_poseidon2::default_engine(vm.max_log_degree()?);
let mut keygen_builder = engine.keygen_builder();

let chips = vm.chips();
let traces = vm.traces();
let traces = vm.traces()?;
let chips = get_chips(&vm);

for (chip, trace) in chips.into_iter().zip_eq(traces) {
keygen_builder.add_air(chip, trace.height(), 0);
Expand Down
15 changes: 9 additions & 6 deletions vm/bin/src/commands/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use afs_test_utils::{
};
use clap::Parser;
use color_eyre::eyre::Result;
use stark_vm::vm::{config::VmConfig, VirtualMachine};
use stark_vm::vm::{config::VmConfig, get_chips, VirtualMachine};

use crate::{
asm::parse_asm_file,
Expand Down Expand Up @@ -54,9 +54,9 @@ impl ProveCommand {
pub fn execute_helper(&self, config: VmConfig) -> Result<()> {
println!("Proving program: {}", self.asm_file_path);
let instructions = parse_asm_file(Path::new(&self.asm_file_path.clone()))?;
let vm = VirtualMachine::<WORD_SIZE, _>::new(config, instructions)?;
let mut vm = VirtualMachine::<WORD_SIZE, _>::new(config, instructions);

let engine = config::baby_bear_poseidon2::default_engine(vm.max_log_degree());
let engine = config::baby_bear_poseidon2::default_engine(vm.max_log_degree()?);
let encoded_pk = read_from_path(&Path::new(&self.keys_folder.clone()).join("partial.pk"))?;
let partial_pk: MultiStarkPartialProvingKey<BabyBearPoseidon2Config> =
bincode::deserialize(&encoded_pk)?;
Expand All @@ -66,19 +66,22 @@ impl ProveCommand {
let prover = engine.prover();
let mut trace_builder = TraceCommitmentBuilder::new(prover.pcs());

for trace in vm.traces() {
for trace in vm.traces()? {
trace_builder.load_trace(trace);
}
trace_builder.commit_current();

let main_trace_data = trace_builder.view(&partial_vk, vm.chips());
let chips = get_chips(&vm);
let num_chips = chips.len();

let main_trace_data = trace_builder.view(&partial_vk, chips);

let mut challenger = engine.new_challenger();
let proof = prover.prove(
&mut challenger,
&partial_pk,
main_trace_data,
&vec![vec![]; vm.chips().len()],
&vec![vec![]; num_chips],
);

let encoded_proof: Vec<u8> = bincode::serialize(&proof).unwrap();
Expand Down
13 changes: 8 additions & 5 deletions vm/bin/src/commands/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use afs_test_utils::{
};
use clap::Parser;
use color_eyre::eyre::Result;
use stark_vm::vm::{config::VmConfig, VirtualMachine};
use stark_vm::vm::{config::VmConfig, get_chips, VirtualMachine};

use crate::{
asm::parse_asm_file,
Expand Down Expand Up @@ -61,24 +61,27 @@ impl VerifyCommand {
pub fn execute_helper(&self, config: VmConfig) -> Result<()> {
println!("Verifying proof file: {}", self.proof_file);
let instructions = parse_asm_file(Path::new(&self.asm_file_path))?;
let vm = VirtualMachine::<WORD_SIZE, _>::new(config, instructions)?;
let mut vm = VirtualMachine::<WORD_SIZE, _>::new(config, instructions);
let encoded_vk = read_from_path(&Path::new(&self.keys_folder).join("partial.vk"))?;
let partial_vk: MultiStarkPartialVerifyingKey<BabyBearPoseidon2Config> =
bincode::deserialize(&encoded_vk)?;

let encoded_proof = read_from_path(Path::new(&self.proof_file))?;
let proof: Proof<BabyBearPoseidon2Config> = bincode::deserialize(&encoded_proof)?;

let engine = config::baby_bear_poseidon2::default_engine(vm.max_log_degree());
let engine = config::baby_bear_poseidon2::default_engine(vm.max_log_degree()?);

let chips = get_chips(&vm);
let num_chips = chips.len();

let mut challenger = engine.new_challenger();
let verifier = engine.verifier();
let result = verifier.verify(
&mut challenger,
partial_vk,
vm.chips(),
chips,
proof,
&vec![vec![]; vm.chips().len()],
&vec![vec![]; num_chips],
);

if result.is_err() {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/cpu/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<T: Clone> CpuIoCols<T> {
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MemoryAccessCols<const WORD_SIZE: usize, T> {
pub enabled: T,

Expand Down
Loading

0 comments on commit 12da2a5

Please sign in to comment.