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

Combine PUSH0 and PC flags. #1256

Merged
merged 4 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 1 addition & 2 deletions evm/src/cpu/columns/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ pub struct OpsColumnsView<T: Copy> {
pub prover_input: T,
pub pop: T,
pub jumps: T, // Combines JUMP and JUMPI flags.
pub pc: T,
pub jumpdest: T,
pub push0: T,
pub push: T,
pub dup: T,
pub swap: T,
Expand All @@ -30,6 +28,7 @@ pub struct OpsColumnsView<T: Copy> {
pub mload_32bytes: T,
pub exit_kernel: T,
pub m_op_general: T,
pub pc_push0: T, // Combines PC and PUSH0

pub syscall: T,
pub exception: T,
Expand Down
5 changes: 2 additions & 3 deletions evm/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; 18] = [
const NATIVE_INSTRUCTIONS: [usize; 17] = [
COL_MAP.op.binary_op,
COL_MAP.op.ternary_op,
COL_MAP.op.fp254_op,
Expand All @@ -20,9 +20,8 @@ const NATIVE_INSTRUCTIONS: [usize; 18] = [
COL_MAP.op.prover_input,
COL_MAP.op.pop,
// not JUMPS (possible need to jump)
COL_MAP.op.pc,
COL_MAP.op.pc_push0,
COL_MAP.op.jumpdest,
COL_MAP.op.push0,
// not PUSH (need to increment by more than 1)
COL_MAP.op.dup,
COL_MAP.op.swap,
Expand Down
22 changes: 18 additions & 4 deletions evm/src/cpu/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::cpu::columns::{CpuColumnsView, COL_MAP};
/// behavior.
/// Note: invalid opcodes are not represented here. _Any_ opcode is permitted to decode to
/// `is_invalid`. The kernel then verifies that the opcode was _actually_ invalid.
const OPCODES: [(u8, usize, bool, usize); 17] = [
const OPCODES: [(u8, usize, bool, usize); 15] = [
// (start index of block, number of top bits to check (log2), kernel-only, flag column)
// ADD, MUL, SUB, DIV, MOD, LT, GT and BYTE flags are handled partly manually here, and partly through the Arithmetic table CTL.
// ADDMOD, MULMOD and SUBMOD flags are handled partly manually here, and partly through the Arithmetic table CTL.
Expand All @@ -36,9 +36,7 @@ const OPCODES: [(u8, usize, bool, usize); 17] = [
(0x49, 0, true, COL_MAP.op.prover_input),
(0x50, 0, false, COL_MAP.op.pop),
(0x56, 1, false, COL_MAP.op.jumps), // 0x56-0x57
(0x58, 0, false, COL_MAP.op.pc),
(0x5b, 0, false, COL_MAP.op.jumpdest),
(0x5f, 0, false, COL_MAP.op.push0),
(0x60, 5, false, COL_MAP.op.push), // 0x60-0x7f
(0x80, 4, false, COL_MAP.op.dup), // 0x80-0x8f
(0x90, 4, false, COL_MAP.op.swap), // 0x90-0x9f
Expand All @@ -53,13 +51,14 @@ const OPCODES: [(u8, usize, bool, usize); 17] = [
/// 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.fp254_op,
COL_MAP.op.binary_op,
COL_MAP.op.ternary_op,
COL_MAP.op.shift,
COL_MAP.op.m_op_general,
COL_MAP.op.pc_push0,
];

/// Break up an opcode (which is 8 bits long) into its eight bits.
Expand Down Expand Up @@ -148,6 +147,12 @@ pub fn eval_packed_generic<P: PackedField>(
* (opcode - P::Scalar::from_canonical_usize(0xfc_usize))
* lv.op.m_op_general;
yield_constr.constraint(m_op_constr);

// Manually check lv.op.pc_push0.
let pc_push0_constr = (opcode - P::Scalar::from_canonical_usize(0x58_usize))
* (opcode - P::Scalar::from_canonical_usize(0x5f_usize))
* lv.op.pc_push0;
yield_constr.constraint(pc_push0_constr);
}

pub fn eval_ext_circuit<F: RichField + Extendable<D>, const D: usize>(
Expand Down Expand Up @@ -250,4 +255,13 @@ pub fn eval_ext_circuit<F: RichField + Extendable<D>, const D: usize>(
m_op_constr = builder.mul_extension(m_op_constr, lv.op.m_op_general);

yield_constr.constraint(builder, m_op_constr);

// Manually check lv.op.pc_push0.
let pc_opcode = builder.constant_extension(F::Extension::from_canonical_usize(0x58_usize));
let push0_opcode = builder.constant_extension(F::Extension::from_canonical_usize(0x5f_usize));
let pc_constr = builder.sub_extension(opcode, pc_opcode);
let push0_constr = builder.sub_extension(opcode, push0_opcode);
let mut pc_push0_constr = builder.mul_extension(pc_constr, push0_constr);
pc_push0_constr = builder.mul_extension(pc_push0_constr, lv.op.pc_push0);
yield_constr.constraint(builder, pc_push0_constr);
}
3 changes: 1 addition & 2 deletions evm/src/cpu/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ const SIMPLE_OPCODES: OpsColumnsView<Option<u32>> = OpsColumnsView {
prover_input: KERNEL_ONLY_INSTR,
pop: G_BASE,
jumps: None, // Combined flag handled separately.
pc: G_BASE,
pc_push0: G_BASE,
jumpdest: G_JUMPDEST,
push0: G_BASE,
push: G_VERYLOW,
dup: G_VERYLOW,
swap: G_VERYLOW,
Expand Down
8 changes: 6 additions & 2 deletions evm/src/cpu/pc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pub fn eval_packed<P: PackedField>(
nv: &CpuColumnsView<P>,
yield_constr: &mut ConstraintConsumer<P>,
) {
let filter = lv.op.pc;
// `PUSH0`'s opcode is odd, while `PC`'s opcode is even.
let filter = lv.op.pc_push0 * (P::ONES - lv.opcode_bits[0]);
let new_stack_top = nv.mem_channels[0].value;
yield_constr.constraint(filter * (new_stack_top[0] - lv.program_counter));
for &limb in &new_stack_top[1..] {
Expand All @@ -25,7 +26,10 @@ pub fn eval_ext_circuit<F: RichField + Extendable<D>, const D: usize>(
nv: &CpuColumnsView<ExtensionTarget<D>>,
yield_constr: &mut RecursiveConstraintConsumer<F, D>,
) {
let filter = lv.op.pc;
// `PUSH0`'s opcode is odd, while `PC`'s opcode is even.
let one = builder.one_extension();
let mut filter = builder.sub_extension(one, lv.opcode_bits[0]);
filter = builder.mul_extension(lv.op.pc_push0, filter);
let new_stack_top = nv.mem_channels[0].value;
{
let diff = builder.sub_extension(new_stack_top[0], lv.program_counter);
Expand Down
6 changes: 4 additions & 2 deletions evm/src/cpu/push0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pub fn eval_packed<P: PackedField>(
nv: &CpuColumnsView<P>,
yield_constr: &mut ConstraintConsumer<P>,
) {
let filter = lv.op.push0;
// `PUSH0`'s opcode is odd, while `PC`'s opcode is even.
let filter = lv.op.pc_push0 * lv.opcode_bits[0];
for limb in nv.mem_channels[0].value {
yield_constr.constraint(filter * limb);
}
Expand All @@ -23,7 +24,8 @@ pub fn eval_ext_circuit<F: RichField + Extendable<D>, const D: usize>(
nv: &CpuColumnsView<ExtensionTarget<D>>,
yield_constr: &mut RecursiveConstraintConsumer<F, D>,
) {
let filter = lv.op.push0;
// `PUSH0`'s opcode is odd, while `PC`'s opcode is even.
let filter = builder.mul_extension(lv.op.pc_push0, lv.opcode_bits[0]);
for limb in nv.mem_channels[0].value {
let constr = builder.mul_extension(filter, limb);
yield_constr.constraint(builder, constr);
Expand Down
8 changes: 1 addition & 7 deletions evm/src/cpu/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub(crate) const STACK_BEHAVIORS: OpsColumnsView<Option<StackBehavior>> = OpsCol
disable_other_channels: true,
}),
jumps: None, // Depends on whether it's a JUMP or a JUMPI.
pc: Some(StackBehavior {
pc_push0: Some(StackBehavior {
num_pops: 0,
pushes: true,
new_top_stack_channel: None,
Expand All @@ -102,12 +102,6 @@ pub(crate) const STACK_BEHAVIORS: OpsColumnsView<Option<StackBehavior>> = OpsCol
new_top_stack_channel: None,
disable_other_channels: true,
}),
push0: Some(StackBehavior {
num_pops: 0,
pushes: true,
new_top_stack_channel: None,
disable_other_channels: true,
}),
push: None, // TODO
dup: None,
swap: None,
Expand Down
6 changes: 2 additions & 4 deletions evm/src/witness/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ fn decode(registers: RegistersState, opcode: u8) -> Result<Operation, ProgramErr
fn fill_op_flag<F: Field>(op: Operation, row: &mut CpuColumnsView<F>) {
let flags = &mut row.op;
*match op {
Operation::Push(0) => &mut flags.push0,
Operation::Push(1..) => &mut flags.push,
Operation::Dup(_) => &mut flags.dup,
Operation::Swap(_) => &mut flags.swap,
Expand All @@ -179,7 +178,7 @@ fn fill_op_flag<F: Field>(op: Operation, row: &mut CpuColumnsView<F>) {
Operation::ProverInput => &mut flags.prover_input,
Operation::Pop => &mut flags.pop,
Operation::Jump | Operation::Jumpi => &mut flags.jumps,
Operation::Pc => &mut flags.pc,
Operation::Pc | Operation::Push(0) => &mut flags.pc_push0,
Operation::Jumpdest => &mut flags.jumpdest,
Operation::GetContext => &mut flags.get_context,
Operation::SetContext => &mut flags.set_context,
Expand All @@ -193,7 +192,7 @@ fn fill_op_flag<F: Field>(op: Operation, row: &mut CpuColumnsView<F>) {
// Equal to the number of pops if an operation pops without pushing, and `None` otherwise.
fn get_op_special_length(op: Operation) -> Option<usize> {
let behavior_opt = match op {
Operation::Push(0) => STACK_BEHAVIORS.push0,
Operation::Push(0) | Operation::Pc => STACK_BEHAVIORS.pc_push0,
Operation::Push(1..) => STACK_BEHAVIORS.push,
Operation::Dup(_) => STACK_BEHAVIORS.dup,
Operation::Swap(_) => STACK_BEHAVIORS.swap,
Expand All @@ -216,7 +215,6 @@ fn get_op_special_length(op: Operation) -> Option<usize> {
Operation::Pop => STACK_BEHAVIORS.pop,
Operation::Jump => JUMP_OP,
Operation::Jumpi => JUMPI_OP,
Operation::Pc => STACK_BEHAVIORS.pc,
Operation::Jumpdest => STACK_BEHAVIORS.jumpdest,
Operation::GetContext => STACK_BEHAVIORS.get_context,
Operation::SetContext => None,
Expand Down