diff --git a/Cargo.toml b/Cargo.toml index 7243861..2a36e25 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -plonky2 = { git = "https://github.com/0xPolygonZero/plonky2" } -starky = { git = "https://github.com/0xPolygonZero/plonky2" } +# Depending on PLONKY2 v0.2.3 +plonky2 = { git = "https://github.com/0xPolygonZero/plonky2", rev = "76da138" } +starky = { git = "https://github.com/0xPolygonZero/plonky2", rev = "76da138" } anyhow = "1.0.86" diff --git a/src/lib.rs b/src/lib.rs index c867615..20181b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,12 +25,7 @@ mod tests { use crate::{ preflight_simulator::PreflightSimulation, - vm_specs::{ - Instruction, - MemoryLocation, - Program, - Register, - }, + vm_specs::{Instruction, MemoryLocation, Program, Register}, }; #[test] @@ -51,8 +46,7 @@ mod tests { .map(|(idx, inst)| (idx as u8, inst)) .collect::>(); - let memory_init: HashMap = - HashMap::from_iter(vec![(0x40, 0x20), (0x41, 0x45)]); + let memory_init: HashMap = HashMap::from_iter(vec![(0x40, 0x20), (0x41, 0x45)]); let program = Program { entry_point: 0, @@ -67,12 +61,9 @@ mod tests { let simulation = simulation.unwrap(); assert_eq!( - simulation.trace_rows[simulation - .trace_rows - .len() - - 1] - .get_memory_at(&expected.0) - .unwrap(), + simulation.trace_rows[simulation.trace_rows.len() - 1] + .get_memory_at(&expected.0) + .unwrap(), expected.1 ); } diff --git a/src/preflight_simulator.rs b/src/preflight_simulator.rs index 23ee7f4..926d009 100644 --- a/src/preflight_simulator.rs +++ b/src/preflight_simulator.rs @@ -6,17 +6,9 @@ use std::collections::HashMap; -use anyhow::{ - anyhow, - Context, - Result, -}; - -use crate::vm_specs::{ - Instruction, - Program, - REGISTER_COUNT, -}; +use anyhow::{anyhow, Context, Result}; + +use crate::vm_specs::{Instruction, Program, REGISTER_COUNT}; /// Each `SimulationRow` describes the state of simulation at each step /// of execution @@ -67,16 +59,11 @@ impl SimulationRow { program_counter, is_halted: false, registers: [0; REGISTER_COUNT], - memory_snapshot: prog - .memory_init - .clone(), + memory_snapshot: prog.memory_init.clone(), }) } - pub fn execute_one_cycle( - &self, - prog: &Program, - ) -> Result { + pub fn execute_one_cycle(&self, prog: &Program) -> Result { // Remember, we have no jump instructions in our VM ISA // Hence, this following is safe. Otherwise, more complicated // logic needs to be implemented. @@ -92,23 +79,13 @@ impl SimulationRow { let is_halted = instruction == Instruction::Halt; let mut registers = self.registers; - let mut memory_snapshot = self - .memory_snapshot - .clone(); + let mut memory_snapshot = self.memory_snapshot.clone(); match self.instruction { - Instruction::Add(a, b) => { - registers[usize::from(a)] += registers[usize::from(b)] - } - Instruction::Sub(a, b) => { - registers[usize::from(a)] += registers[usize::from(b)] - } - Instruction::Mul(a, b) => { - registers[usize::from(a)] += registers[usize::from(b)] - } - Instruction::Div(a, b) => { - registers[usize::from(a)] += registers[usize::from(b)] - } + Instruction::Add(a, b) => registers[usize::from(a)] += registers[usize::from(b)], + Instruction::Sub(a, b) => registers[usize::from(a)] += registers[usize::from(b)], + Instruction::Mul(a, b) => registers[usize::from(a)] += registers[usize::from(b)], + Instruction::Div(a, b) => registers[usize::from(a)] += registers[usize::from(b)], Instruction::Bsl(reg, amount) => { if registers[usize::from(amount)] >= 8 { return Err(anyhow!("invalid shift amount")); @@ -148,18 +125,12 @@ impl SimulationRow { }) } - pub fn get_memory_at( - &self, - address: &u8, - ) -> Option { - self.memory_snapshot - .get(address) - .copied() + pub fn get_memory_at(&self, address: &u8) -> Option { + self.memory_snapshot.get(address).copied() } pub fn get_registers(&self) -> [u8; REGISTER_COUNT] { - self.registers - .clone() + self.registers.clone() } } @@ -183,8 +154,7 @@ impl PreflightSimulation { while trace_rows.len() < MAX_CPU_CYCLES_ALLOWED && !trace_rows[trace_rows.len() - 1].is_halted { - let current_row = - trace_rows[trace_rows.len() - 1].execute_one_cycle(prog)?; + let current_row = trace_rows[trace_rows.len() - 1].execute_one_cycle(prog)?; trace_rows.push(current_row); } diff --git a/src/stark_program_instructions.rs b/src/stark_program_instructions.rs index 72f5e6c..a8288c6 100644 --- a/src/stark_program_instructions.rs +++ b/src/stark_program_instructions.rs @@ -4,6 +4,7 @@ //! needs to be differentiated from actual running process trace, since //! that may be longer than "program" owing to actual execution of jumps. +use core::marker::PhantomData; use plonky2::{ field::{ extension::{ @@ -12,11 +13,14 @@ use plonky2::{ }, packed::PackedField, }, - iop::ext_target::ExtensionTarget, hash::hash_types::RichField, + iop::ext_target::ExtensionTarget, +}; +use starky::{ + constraint_consumer::ConstraintConsumer, + evaluation_frame::StarkFrame, + stark::Stark, }; -use starky::{constraint_consumer::ConstraintConsumer, evaluation_frame::StarkFrame, stark::Stark}; -use core::marker::PhantomData; pub struct ProgramInstructions { pub program_counter: T, @@ -34,21 +38,20 @@ impl Stark for ProgramInstructionsStark where F: RichField + Extendable, { - const COLUMNS: usize = NUMBER_OF_COLS; - const PUBLIC_INPUTS: usize = PUBLIC_INPUTS; - - type EvaluationFrame = StarkFrame - where - FE: FieldExtension, - P: PackedField; - + type EvaluationFrame = StarkFrame + where + FE: FieldExtension, + P: PackedField; type EvaluationFrameTarget = StarkFrame< ExtensionTarget, ExtensionTarget, NUMBER_OF_COLS, PUBLIC_INPUTS, >; - + + const COLUMNS: usize = NUMBER_OF_COLS; + const PUBLIC_INPUTS: usize = PUBLIC_INPUTS; + fn eval_packed_generic( &self, vars: &Self::EvaluationFrame, @@ -57,16 +60,14 @@ where FE: FieldExtension, P: PackedField, { - } fn eval_ext_circuit( - &self, - builder: &mut plonky2::plonk::circuit_builder::CircuitBuilder, - vars: &Self::EvaluationFrameTarget, - yield_constr: &mut starky::constraint_consumer::RecursiveConstraintConsumer, - ) { - + &self, + builder: &mut plonky2::plonk::circuit_builder::CircuitBuilder, + vars: &Self::EvaluationFrameTarget, + yield_constr: &mut starky::constraint_consumer::RecursiveConstraintConsumer, + ) { } fn constraint_degree(&self) -> usize {