Skip to content

Commit

Permalink
basic constraint: ProgramInstructionsStark
Browse files Browse the repository at this point in the history
  • Loading branch information
supragya committed Jul 7, 2024
1 parent a37d5e3 commit b47d912
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 9 deletions.
71 changes: 62 additions & 9 deletions src/stark_program_instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,71 @@ use plonky2::{
FieldExtension,
},
packed::PackedField,
polynomial::PolynomialValues,
},
hash::hash_types::RichField,
iop::ext_target::ExtensionTarget,
plonk::circuit_builder::CircuitBuilder,
};
use starky::{
constraint_consumer::ConstraintConsumer,
evaluation_frame::StarkFrame,
constraint_consumer::{
ConstraintConsumer,
RecursiveConstraintConsumer,
},
evaluation_frame::{
StarkEvaluationFrame,
StarkFrame,
},
stark::Stark,
util::trace_rows_to_poly_values,
};

pub struct ProgramInstructions<T> {
pub program_counter: T,
pub instruction_data: T,
}
use crate::vm_specs::Program;

const NUMBER_OF_COLS: usize = 2;
/// Represents one row in a STARK table, contains `is_filter` which
/// should be set to `true` in case it represents an actual instruction
//#[repr(C)]
//#[derive(Default, Clone, Copy, PartialEq, Debug)]
//pub struct ProgramInstructions<T> {
// pub program_counter: T, // ID = 0
// pub opcode: T, // ID = 1
// pub filter: T, // ID = 2
//}

const NUMBER_OF_COLS: usize = 3;
const PUBLIC_INPUTS: usize = 0;

pub fn generate_program_instructions_trace<F>(
prog: &Program
) -> Vec<PolynomialValues<F>>
where
F: RichField,
{
let mut trace = prog
.code
.iter()
.map(|(pc, inst)| {
[
// Program Counter (ID = 0)
F::from_canonical_u8(pc.clone()),
// Instruction Opcode (ID = 1)
F::from_canonical_u8(inst.get_opcode()),
// Filter, true if actual instructions (ID = 2)
F::ONE,
]
})
.collect::<Vec<[F; NUMBER_OF_COLS]>>();

// Need to pad the trace to a len of some power of 2
let pow2_len = trace
.len()
.next_power_of_two();
trace.resize(pow2_len, [F::ZERO, F::ZERO, F::ZERO]);

// Convert into polynomial values
trace_rows_to_poly_values(trace)
}

pub struct ProgramInstructionsStark<F, const D: usize> {
pub _f: PhantomData<F>,
}
Expand Down Expand Up @@ -60,14 +107,20 @@ where
FE: FieldExtension<D2, BaseField = F>,
P: PackedField<Scalar = FE>,
{
let local_values = vars.get_local_values();

// Check if filter column is either 0 or 1
let filter_column = local_values[2];
yield_constr.constraint(filter_column * (P::ONES - filter_column));
}

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

fn constraint_degree(&self) -> usize {
Expand Down
18 changes: 18 additions & 0 deletions src/vm_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ pub enum Instruction {
Halt,
}

impl Instruction {
/// Not the best of the implementations. But written it like this
/// for demonstration purposes
pub fn get_opcode(&self) -> u8 {
match self {
Instruction::Add(_, _) => 0,
Instruction::Sub(_, _) => 1,
Instruction::Mul(_, _) => 2,
Instruction::Div(_, _) => 3,
Instruction::Bsl(_, _) => 4,
Instruction::Bsr(_, _) => 5,
Instruction::Lb(_, _) => 6,
Instruction::Sb(_, _) => 7,
Instruction::Halt => 8,
}
}
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct Program {
/// The entrypoint of the program
Expand Down

0 comments on commit b47d912

Please sign in to comment.