Skip to content

Commit

Permalink
fix: logic
Browse files Browse the repository at this point in the history
  • Loading branch information
eigmax committed Nov 27, 2023
1 parent 2624703 commit 7e2b628
Show file tree
Hide file tree
Showing 15 changed files with 1,167 additions and 422 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ 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" }
#plonky2_u32 = { version = "0.1.0", default-features = false }

itertools = "0.11.0"
log = { version = "0.4.14", 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
13 changes: 8 additions & 5 deletions src/cpu/columns/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) union CpuGeneralColumnsView<T: Copy> {
logic: CpuLogicView<T>,
jumps: CpuJumpsView<T>,
shift: CpuShiftView<T>,
io: CpuIOView<T>,
io: CpuIoView<T>,
}

impl<T: Copy> CpuGeneralColumnsView<T> {
Expand Down Expand Up @@ -55,12 +55,12 @@ impl<T: Copy> CpuGeneralColumnsView<T> {
}

// SAFETY: Each view is a valid interpretation of the underlying array.
pub(crate) fn io(&self) -> &CpuIOView<T> {
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> {
pub(crate) fn io_mut(&mut self) -> &mut CpuIoView<T> {
unsafe { &mut self.io }
}
}
Expand Down Expand Up @@ -121,9 +121,12 @@ pub(crate) struct CpuShiftView<T: Copy> {
}

#[derive(Copy, Clone)]
pub(crate) struct CpuIOView<T: Copy> {
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,
pub(crate) values: [T; 8],
}

// `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
40 changes: 20 additions & 20 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 @@ -194,14 +192,14 @@ impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for CpuStark<F, D
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);
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);
pc::eval_packed(local_values, next_values, yield_constr);
//shift::eval_packed(local_values, yield_constr);
//count::eval_packed(local_values, yield_constr);
//syscall::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 @@ -225,14 +223,14 @@ impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for CpuStark<F, D
);
//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);
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);
pc::eval_ext_circuit(builder, local_values, next_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);
count::eval_ext_circuit(builder, local_values, yield_constr);
syscall::eval_ext_circuit(builder, local_values, yield_constr);
}

fn constraint_degree(&self) -> usize {
Expand All @@ -243,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 @@ -311,7 +308,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

0 comments on commit 7e2b628

Please sign in to comment.