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 #50

Merged
merged 13 commits into from
Nov 28, 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ plonky2 = { git = "https://github.com/0xPolygonZero/plonky2", branch = "main", f
starky = { git = "https://github.com/0xPolygonZero/plonky2", branch = "main" , features = ["timing"] }
plonky2_util = { git = "https://github.com/0xPolygonZero/plonky2", branch = "main" }
plonky2_maybe_rayon = { git = "https://github.com/0xPolygonZero/plonky2", branch = "main" }

itertools = "0.11.0"
log = { version = "0.4.14", default-features = false }
anyhow = { version = "1.0.40", default-features = false }
Expand Down
6 changes: 1 addition & 5 deletions src/all_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ use crate::cpu::cpu_stark;
use crate::cpu::cpu_stark::CpuStark;
use crate::cpu::membus::NUM_GP_CHANNELS;
use crate::cross_table_lookup::{CrossTableLookup, TableWithColumns};
use crate::keccak::keccak_stark;
use crate::keccak::keccak_stark::KeccakStark;
use crate::keccak_sponge::columns::KECCAK_RATE_BYTES;
use crate::keccak_sponge::keccak_sponge_stark;
use crate::keccak_sponge::keccak_sponge_stark::KeccakSpongeStark;

use crate::logic;
use crate::logic::LogicStark;
use crate::memory::memory_stark;
Expand Down
4 changes: 2 additions & 2 deletions src/cpu/bootstrap_kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub(crate) fn eval_bootstrap_kernel_packed<F: Field, P: PackedField<Scalar = F>>
// If this is a bootloading row and the i'th memory channel is used, it must have the right
// address, name context = 0, segment = Code, virt + 4 = next_virt
let code_segment = F::from_canonical_usize(Segment::Code as usize);
for (i, channel) in local_values.mem_channels.iter().enumerate() {
for (_i, channel) in local_values.mem_channels.iter().enumerate() {
let filter = local_is_bootstrap * channel.used;
yield_constr.constraint(filter * channel.addr_context);
yield_constr.constraint(filter * (channel.addr_segment - code_segment));
Expand Down Expand Up @@ -137,7 +137,7 @@ pub(crate) fn eval_bootstrap_kernel_ext_circuit<F: RichField + Extendable<D>, co
// address, name context = 0, segment = Code, virt + 4 = next_virt
let code_segment =
builder.constant_extension(F::Extension::from_canonical_usize(Segment::Code as usize));
for (i, channel) in local_values.mem_channels.iter().enumerate() {
for (_i, channel) in local_values.mem_channels.iter().enumerate() {
let filter = builder.mul_extension(local_is_bootstrap, channel.used);
let constraint = builder.mul_extension(filter, channel.addr_context);
yield_constr.constraint(builder, constraint);
Expand Down
23 changes: 17 additions & 6 deletions src/cpu/columns/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub(crate) union CpuGeneralColumnsView<T: Copy> {
logic: CpuLogicView<T>,
jumps: CpuJumpsView<T>,
shift: CpuShiftView<T>,
io: CpuIoView<T>,
}

impl<T: Copy> CpuGeneralColumnsView<T> {
Expand Down Expand Up @@ -52,6 +53,16 @@ impl<T: Copy> CpuGeneralColumnsView<T> {
pub(crate) fn shift_mut(&mut self) -> &mut CpuShiftView<T> {
unsafe { &mut self.shift }
}

// SAFETY: Each view is a valid interpretation of the underlying array.
pub(crate) fn io(&self) -> &CpuIoView<T> {
unsafe { &self.io }
}

// SAFETY: Each view is a valid interpretation of the underlying array.
pub(crate) fn io_mut(&mut self) -> &mut CpuIoView<T> {
unsafe { &mut self.io }
}
}

impl<T: Copy + PartialEq> PartialEq<Self> for CpuGeneralColumnsView<T> {
Expand Down Expand Up @@ -88,8 +99,6 @@ pub(crate) struct CpuSyscallView<T: Copy> {
pub(crate) sysnum: [T; 11],
pub(crate) a0: [T; 3],
pub(crate) a1: T,
// pub(crate) a1: [T;2],
// pub(crate) sz: [T;2],
}

#[derive(Copy, Clone)]
Expand All @@ -112,10 +121,12 @@ pub(crate) struct CpuShiftView<T: Copy> {
}

#[derive(Copy, Clone)]
pub(crate) struct CpuGPRView<T: Copy> {
// For a shift amount of displacement: [T], this is the inverse of
// sum(displacement[1..]) or zero if the sum is zero.
pub(crate) regs: [T; 32],
pub(crate) struct CpuIoView<T: Copy> {
pub(crate) rs_le: [T; 32],
pub(crate) rt_le: [T; 32],
pub(crate) mem_le: [T; 32],
pub(crate) micro_op: [T; 8],
pub(crate) diff_inv: T,
}

// `u8` is guaranteed to have a `size_of` of 1.
Expand Down
3 changes: 2 additions & 1 deletion src/cpu/columns/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ pub struct OpsColumnsView<T: Copy> {
pub get_context: T,
pub set_context: T,
pub exit_kernel: T,
pub m_op_general: T,
pub m_op_load: T,
pub m_op_store: T,

pub syscall: T,
}
Expand Down
5 changes: 3 additions & 2 deletions src/cpu/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer
use crate::cpu::columns::{CpuColumnsView, COL_MAP};
// use crate::cpu::kernel::aggregator::KERNEL;

const NATIVE_INSTRUCTIONS: [usize; 12] = [
const NATIVE_INSTRUCTIONS: [usize; 13] = [
COL_MAP.op.binary_op,
COL_MAP.op.eq_iszero,
COL_MAP.op.logic_op,
Expand All @@ -23,7 +23,8 @@ const NATIVE_INSTRUCTIONS: [usize; 12] = [
COL_MAP.op.get_context,
COL_MAP.op.set_context,
// not EXIT_KERNEL (performs a jump)
COL_MAP.op.m_op_general,
COL_MAP.op.m_op_load,
COL_MAP.op.m_op_store,
// not SYSCALL (performs a jump)
// not exceptions (also jump)
];
Expand Down
44 changes: 16 additions & 28 deletions src/cpu/cpu_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ use super::columns::CpuColumnsView;
use crate::all_stark::Table;
use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
use crate::cpu::columns::{COL_MAP, NUM_CPU_COLUMNS};
use crate::cpu::{
bootstrap_kernel, control_flow, count, decode, jumps, membus, memio, pc, shift, syscall,
};
use crate::cpu::{bootstrap_kernel, count, decode, jumps, membus, memio, pc, syscall};
use crate::cross_table_lookup::{Column, TableWithColumns};
use crate::evaluation_frame::{StarkEvaluationFrame, StarkFrame};
use crate::memory::segments::Segment;
Expand Down Expand Up @@ -191,24 +189,17 @@ 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);
contextops::eval_packed(local_values, next_values, yield_constr);
control_flow::eval_packed_generic(local_values, next_values, yield_constr);
*/
syscall::eval_packed(local_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);
pc::eval_packed(local_values, next_values, yield_constr);
shift::eval_packed(local_values, yield_constr);
syscall::eval_packed(local_values, yield_constr);
//shift::eval_packed(local_values, yield_constr);
count::eval_packed(local_values, yield_constr);

*/
syscall::eval_packed(local_values, yield_constr);
}

fn eval_ext_circuit(
Expand All @@ -224,27 +215,21 @@ 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);
*/
/*
//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);
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);
//shift::eval_ext_circuit(builder, local_values, yield_constr);
count::eval_ext_circuit(builder, local_values, yield_constr);

*/
syscall::eval_ext_circuit(builder, local_values, yield_constr);
}

Expand All @@ -256,19 +241,18 @@ 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::columns::NUM_CPU_COLUMNS;
use crate::cpu::cpu_stark::CpuStark;
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_stark_circuit_constraints, test_stark_cpu_check_constraints, test_stark_low_degree,
};

#[test]
Expand Down Expand Up @@ -299,6 +283,7 @@ mod tests {
}

#[test]
#[ignore]
fn test_stark_check_memio() {
env_logger::try_init().unwrap_or_default();
const D: usize = 2;
Expand All @@ -324,7 +309,10 @@ mod tests {
.collect::<Vec<_>>();

for i in 0..(vals.len() - 1) {
println!("vals: {:?}, cpu column: {:?}", vals[i], state.traces.cpu[i]);
println!(
"[] vals: {:?},\ncpu column: {:?}",
vals[i], state.traces.cpu[i]
);
test_stark_cpu_check_constraints::<F, C, S, D>(stark, &vals[i], &vals[i + 1]);
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/cpu/decode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use plonky2::field::extension::Extendable;
use plonky2::field::packed::PackedField;
use plonky2::field::types::Field;

use plonky2::hash::hash_types::RichField;
use plonky2::iop::ext_target::ExtensionTarget;

Expand Down Expand Up @@ -46,13 +46,14 @@ const OPCODES: [(u32, usize, bool, usize); 10] = [
/// List of combined opcodes requiring a special handling.
/// Each index in the list corresponds to an arbitrary combination
/// of opcodes defined in evm/src/cpu/columns/ops.rs.
const COMBINED_OPCODES: [usize; 6] = [
const COMBINED_OPCODES: [usize; 7] = [
COL_MAP.op.logic_op,
COL_MAP.op.binary_op,
COL_MAP.op.binary_imm_op,
COL_MAP.op.shift,
COL_MAP.op.shift_imm,
COL_MAP.op.m_op_general,
COL_MAP.op.m_op_load,
COL_MAP.op.m_op_store,
];

/// Break up an opcode (which is 32 bits long) into its 32 bits.
Expand Down Expand Up @@ -105,8 +106,6 @@ pub fn eval_ext_circuit<F: RichField + Extendable<D>, const D: usize>(
lv: &CpuColumnsView<ExtensionTarget<D>>,
yield_constr: &mut RecursiveConstraintConsumer<F, D>,
) {
let one = builder.one_extension();

// Note: The constraints below do not need to be restricted to CPU cycles.

// Ensure that the kernel flag is valid (either 0 or 1).
Expand Down
Loading
Loading