Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: memio constraints satisfication #46

Merged
merged 5 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/arithmetic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl BinaryOperator {
}

/// An enum representing arithmetic operations that can be either binary.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub(crate) enum Operation {
BinaryOperation {
operator: BinaryOperator,
Expand Down
4 changes: 1 addition & 3 deletions src/cpu/bootstrap_kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ pub(crate) fn generate_bootstrap_kernel<F: Field>(state: &mut GenerationState<F>
// Iterate through chunks of the code, such that we can write one chunk to memory per row.
for chunk in &KERNEL.program.image.iter().chunks(NUM_GP_CHANNELS) {
let mut cpu_row = CpuColumnsView::default();
println!("trace: {:?}", state.traces.clock());
cpu_row.clock = F::from_canonical_usize(state.traces.clock());
cpu_row.is_bootstrap_kernel = F::ONE;

// Write this chunk to memory, while simultaneously packing its bytes into a u32 word.
for (channel, (addr, val)) in chunk.enumerate() {
// FIXMEBoth instruction and memory data are located in
// code section for MIPS
println!("addr: {}", *addr);
let address = MemoryAddress::new(0, Segment::Code, *addr as usize);
let write =
mem_write_gp_log_and_fill(channel, address, state, &mut cpu_row, (*val).to_be());
Expand Down Expand Up @@ -91,7 +89,7 @@ pub(crate) fn eval_bootstrap_kernel_packed<F: Field, P: PackedField<Scalar = F>>
yield_constr.constraint(filter * (channel.addr_segment - code_segment));
/* FIXME
let delta_virt = channel.addr_virtual + P::from(F::from_canonical_u32(32)) - next_values.mem_channels[i].addr_virtual;
println!("virt {:?} {:?} {:?} {:?} {}", channel.addr_virtual, delta_virt, local_values.clock, NUM_GP_CHANNELS, i);
log::debug!("virt {:?} {:?} {:?} {:?} {}", channel.addr_virtual, delta_virt, local_values.clock, NUM_GP_CHANNELS, i);
yield_constr.constraint_transition(filter * delta_virt);
*/
}
Expand Down
55 changes: 46 additions & 9 deletions src/cpu/cpu_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,20 @@ impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for CpuStark<F, D
let next_values: &[P; NUM_CPU_COLUMNS] = vars.get_next_values().try_into().unwrap();
let next_values: &CpuColumnsView<P> = next_values.borrow();

bootstrap_kernel::eval_bootstrap_kernel_packed(local_values, next_values, yield_constr);
/*
bootstrap_kernel::eval_bootstrap_kernel_packed(local_values, next_values, yield_constr);
contextops::eval_packed(local_values, next_values, yield_constr);
control_flow::eval_packed_generic(local_values, next_values, yield_constr);
*/
decode::eval_packed_generic(local_values, yield_constr);
jumps::eval_packed(local_values, next_values, yield_constr);
membus::eval_packed(local_values, yield_constr);
//memio::eval_packed(local_values, next_values, yield_constr);
memio::eval_packed(local_values, next_values, yield_constr);
pc::eval_packed(local_values, next_values, yield_constr);
/*
shift::eval_packed(local_values, yield_constr);
syscall::eval_packed(local_values, yield_constr);
mov::eval_packed(local_values, yield_constr);
count::eval_packed(local_values, yield_constr);
*/
}

fn eval_ext_circuit(
Expand All @@ -222,27 +220,25 @@ impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for CpuStark<F, D
vars.get_next_values().try_into().unwrap();
let next_values: &CpuColumnsView<ExtensionTarget<D>> = next_values.borrow();

/*
bootstrap_kernel::eval_bootstrap_kernel_ext_circuit(
builder,
local_values,
next_values,
yield_constr,
);
/*
contextops::eval_ext_circuit(builder, local_values, next_values, yield_constr);
control_flow::eval_ext_circuit(builder, local_values, next_values, yield_constr);
*/
decode::eval_ext_circuit(builder, local_values, yield_constr);
jumps::eval_ext_circuit(builder, local_values, next_values, yield_constr);
membus::eval_ext_circuit(builder, local_values, yield_constr);
//memio::eval_ext_circuit(builder, local_values, next_values, yield_constr);
memio::eval_ext_circuit(builder, local_values, next_values, yield_constr);
pc::eval_ext_circuit(builder, local_values, next_values, yield_constr);
/*
shift::eval_ext_circuit(builder, local_values, yield_constr);
syscall::eval_ext_circuit(builder, local_values, yield_constr);
mov::eval_ext_circuit(builder, local_values, yield_constr);
count::eval_ext_circuit(builder, local_values, yield_constr);
*/
}

fn constraint_degree(&self) -> usize {
Expand All @@ -253,10 +249,20 @@ impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for CpuStark<F, D
#[cfg(test)]
mod tests {
use anyhow::Result;
use plonky2::field::extension::{Extendable, FieldExtension};
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};

use crate::cpu::bootstrap_kernel::generate_bootstrap_kernel;
use crate::cpu::columns::{COL_MAP, NUM_CPU_COLUMNS};
use crate::cpu::cpu_stark::CpuStark;
use crate::stark_testing::{test_stark_circuit_constraints, test_stark_low_degree};
use crate::cpu::kernel::KERNEL;
use crate::generation::simulate_cpu;
use crate::generation::state::GenerationState;
use crate::generation::GenerationInputs;
use crate::stark_testing::{
test_stark_check_constraints, test_stark_circuit_constraints,
test_stark_cpu_check_constraints, test_stark_low_degree,
};

#[test]
fn test_stark_degree() -> Result<()> {
Expand Down Expand Up @@ -284,4 +290,35 @@ mod tests {
};
test_stark_circuit_constraints::<F, C, S, D>(stark)
}

#[test]
fn test_stark_check_memio() {
env_logger::try_init().unwrap_or_default();
const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;
type S = CpuStark<F, D>;

let stark = S {
f: Default::default(),
};

let inputs = GenerationInputs {};
let mut state = GenerationState::<F>::new(inputs.clone(), &KERNEL.code, 40000000).unwrap();
generate_bootstrap_kernel::<F>(&mut state);
simulate_cpu::<F, D>(&mut state).unwrap();

let vals: Vec<[F; NUM_CPU_COLUMNS]> = state
.clone()
.traces
.cpu
.into_iter()
.map(|x| x.into())
.collect::<Vec<_>>();

for i in 0..(vals.len() - 1) {
println!("vals: {:?}, cpu column: {:?}", vals[i], state.traces.cpu[i]);
test_stark_cpu_check_constraints::<F, C, S, D>(stark, &vals[i], &vals[i + 1]);
}
}
}
4 changes: 0 additions & 4 deletions src/cross_table_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,6 @@ impl<F: Field> CrossTableLookup<F> {
looking_tables: Vec<TableWithColumns<F>>,
looked_table: TableWithColumns<F>,
) -> Self {
looking_tables
.iter()
.for_each(|e| println!("looking: {}, columns: {:?}", e.columns.len(), e));
println!("looked:{} {:?}", looked_table.columns.len(), looked_table);
assert!(looking_tables
.iter()
.all(|twc| twc.columns.len() == looked_table.columns.len()));
Expand Down
2 changes: 1 addition & 1 deletion src/generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub fn generate_traces<F: RichField + Extendable<D>, const D: usize>(
}

/// Perform MIPS instruction and transit state
fn simulate_cpu<F: RichField + Extendable<D>, const D: usize>(
pub(crate) fn simulate_cpu<F: RichField + Extendable<D>, const D: usize>(
state: &mut GenerationState<F>,
) -> anyhow::Result<()> {
let mut step = 0;
Expand Down
1 change: 1 addition & 0 deletions src/generation/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub(crate) struct GenerationStateCheckpoint {
pub(crate) traces: TraceCheckpoint,
}

#[derive(Clone)]
pub(crate) struct GenerationState<F: Field> {
pub(crate) inputs: GenerationInputs,
pub(crate) registers: RegistersState,
Expand Down
2 changes: 1 addition & 1 deletion src/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Op {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub(crate) struct Operation {
operator: Op,
input0: u32,
Expand Down
42 changes: 42 additions & 0 deletions src/stark_testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,45 @@ pub fn test_stark_check_constraints<
assert_eq!(acc, F::Extension::ZERO);
}
}

pub fn test_stark_cpu_check_constraints<
F: RichField + Extendable<D>,
C: GenericConfig<D, F = F>,
S: Stark<F, D>,
const D: usize,
>(
stark: S,
lv: &[C::F],
nv: &[C::F],
) {
// Compute native constraint evaluation on random values.
let vars = S::EvaluationFrame::from_values(
&lv.iter()
.copied()
.map(F::Extension::from_basefield)
.collect::<Vec<_>>()[..],
&nv.iter()
.copied()
.map(F::Extension::from_basefield)
.collect::<Vec<_>>()[..],
);

let alphas = F::rand_vec(1);
let z_last = F::Extension::rand();
let lagrange_first = F::Extension::rand();
let lagrange_last = F::Extension::rand();
let mut consumer = ConstraintConsumer::<F::Extension>::new(
alphas
.iter()
.copied()
.map(F::Extension::from_basefield)
.collect(),
z_last,
lagrange_first,
lagrange_last,
);
stark.eval_packed_generic(&vars, &mut consumer);
for &acc in &consumer.constraint_accs {
assert_eq!(acc, F::Extension::ZERO);
}
}
2 changes: 1 addition & 1 deletion src/witness/traces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct TraceCheckpoint {
pub(self) memory_len: usize,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub(crate) struct Traces<T: Copy> {
pub(crate) arithmetic_ops: Vec<arithmetic::Operation>,
// pub(crate) byte_packing_ops: Vec<BytePackingOp>,
Expand Down
6 changes: 5 additions & 1 deletion src/witness/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ fn decode(registers: RegistersState, insn: u32) -> Result<Operation, ProgramErro
let target = insn & 0x3ffffff;
log::debug!(
"op {}, func {}, rt {}, rs {}, rd {}",
opcode, func, rt, rs, rd
opcode,
func,
rt,
rs,
rd
);
log::debug!(
"decode: insn {:X}, opcode {:X}, func {:X}",
Expand Down