Skip to content

Commit

Permalink
makehappy: fmt;
Browse files Browse the repository at this point in the history
  • Loading branch information
supragya committed Jul 7, 2024
1 parent 344e7e6 commit a37d5e3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 79 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
19 changes: 5 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ mod tests {

use crate::{
preflight_simulator::PreflightSimulation,
vm_specs::{
Instruction,
MemoryLocation,
Program,
Register,
},
vm_specs::{Instruction, MemoryLocation, Program, Register},
};

#[test]
Expand All @@ -51,8 +46,7 @@ mod tests {
.map(|(idx, inst)| (idx as u8, inst))
.collect::<HashMap<u8, Instruction>>();

let memory_init: HashMap<u8, u8> =
HashMap::from_iter(vec![(0x40, 0x20), (0x41, 0x45)]);
let memory_init: HashMap<u8, u8> = HashMap::from_iter(vec![(0x40, 0x20), (0x41, 0x45)]);

let program = Program {
entry_point: 0,
Expand All @@ -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
);
}
Expand Down
58 changes: 14 additions & 44 deletions src/preflight_simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Self> {
pub fn execute_one_cycle(&self, prog: &Program) -> Result<Self> {
// Remember, we have no jump instructions in our VM ISA
// Hence, this following is safe. Otherwise, more complicated
// logic needs to be implemented.
Expand All @@ -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"));
Expand Down Expand Up @@ -148,18 +125,12 @@ impl SimulationRow {
})
}

pub fn get_memory_at(
&self,
address: &u8,
) -> Option<u8> {
self.memory_snapshot
.get(address)
.copied()
pub fn get_memory_at(&self, address: &u8) -> Option<u8> {
self.memory_snapshot.get(address).copied()
}

pub fn get_registers(&self) -> [u8; REGISTER_COUNT] {
self.registers
.clone()
self.registers.clone()
}
}

Expand All @@ -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);
}

Expand Down
39 changes: 20 additions & 19 deletions src/stark_program_instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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<T> {
pub program_counter: T,
Expand All @@ -34,21 +38,20 @@ impl<F, const D: usize> Stark<F, D> for ProgramInstructionsStark<F, D>
where
F: RichField + Extendable<D>,
{
const COLUMNS: usize = NUMBER_OF_COLS;
const PUBLIC_INPUTS: usize = PUBLIC_INPUTS;

type EvaluationFrame<FE, P, const D2: usize> = StarkFrame<P, P::Scalar, NUMBER_OF_COLS, PUBLIC_INPUTS>
where
FE: FieldExtension<D2, BaseField = F>,
P: PackedField<Scalar = FE>;

type EvaluationFrame<FE, P, const D2: usize> = StarkFrame<P, P::Scalar, NUMBER_OF_COLS, PUBLIC_INPUTS>
where
FE: FieldExtension<D2, BaseField = F>,
P: PackedField<Scalar = FE>;
type EvaluationFrameTarget = StarkFrame<
ExtensionTarget<D>,
ExtensionTarget<D>,
NUMBER_OF_COLS,
PUBLIC_INPUTS,
>;


const COLUMNS: usize = NUMBER_OF_COLS;
const PUBLIC_INPUTS: usize = PUBLIC_INPUTS;

fn eval_packed_generic<FE, P, const D2: usize>(
&self,
vars: &Self::EvaluationFrame<FE, P, D2>,
Expand All @@ -57,16 +60,14 @@ where
FE: FieldExtension<D2, BaseField = F>,
P: PackedField<Scalar = FE>,
{

}

fn eval_ext_circuit(
&self,
builder: &mut plonky2::plonk::circuit_builder::CircuitBuilder<F, D>,
vars: &Self::EvaluationFrameTarget,
yield_constr: &mut starky::constraint_consumer::RecursiveConstraintConsumer<F, D>,
) {

&self,
builder: &mut plonky2::plonk::circuit_builder::CircuitBuilder<F, D>,
vars: &Self::EvaluationFrameTarget,
yield_constr: &mut starky::constraint_consumer::RecursiveConstraintConsumer<F, D>,
) {
}

fn constraint_degree(&self) -> usize {
Expand Down

0 comments on commit a37d5e3

Please sign in to comment.