mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-02 22:03:07 +00:00
Push and arithmetic ops
This commit is contained in:
parent
afb3e4b1e1
commit
2471f5a391
@ -1,3 +1,5 @@
|
||||
use ethereum_types::U256;
|
||||
|
||||
mod add;
|
||||
mod compare;
|
||||
mod modular;
|
||||
@ -7,3 +9,39 @@ mod utils;
|
||||
|
||||
pub mod arithmetic_stark;
|
||||
pub(crate) mod columns;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub(crate) enum BinaryOperator {
|
||||
Mul,
|
||||
Sub,
|
||||
Div,
|
||||
Mod,
|
||||
Lt,
|
||||
Gt,
|
||||
Shl,
|
||||
Shr,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub(crate) enum TernaryOperator {
|
||||
AddMod,
|
||||
SubMod,
|
||||
MulMod,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum Operation {
|
||||
BinaryOperation {
|
||||
operator: BinaryOperator,
|
||||
input0: U256,
|
||||
input1: U256,
|
||||
result: U256,
|
||||
},
|
||||
TernaryOperation {
|
||||
operator: TernaryOperator,
|
||||
input0: U256,
|
||||
input1: U256,
|
||||
input2: U256,
|
||||
result: U256,
|
||||
},
|
||||
}
|
||||
|
||||
@ -15,7 +15,6 @@ use crate::cpu::kernel::aggregator::KERNEL;
|
||||
use crate::cpu::kernel::constants::global_metadata::GlobalMetadata;
|
||||
use crate::memory::segments::Segment;
|
||||
use crate::proof::{BlockMetadata, PublicValues, TrieRoots};
|
||||
use crate::util::trace_rows_to_poly_values;
|
||||
use crate::witness::memory::{MemoryAddress, MemoryState};
|
||||
use crate::witness::state::RegistersState;
|
||||
use crate::witness::traces::Traces;
|
||||
|
||||
@ -5,7 +5,6 @@ use crate::cpu::columns::CpuColumnsView;
|
||||
use crate::cpu::kernel::aggregator::KERNEL;
|
||||
use crate::cpu::membus::NUM_GP_CHANNELS;
|
||||
use crate::cpu::simple_logic::eq_iszero::generate_pinv_diff;
|
||||
use crate::logic;
|
||||
use crate::memory::segments::Segment;
|
||||
use crate::witness::errors::ProgramError;
|
||||
use crate::witness::memory::{MemoryAddress, MemoryState};
|
||||
@ -15,9 +14,11 @@ use crate::witness::util::{
|
||||
mem_read_gp_with_log_and_fill, mem_write_gp_log_and_fill, stack_pop_with_log_and_fill,
|
||||
stack_push_log_and_fill,
|
||||
};
|
||||
use crate::{arithmetic, logic};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub(crate) enum Operation {
|
||||
Push(u8),
|
||||
Dup(u8),
|
||||
Swap(u8),
|
||||
Iszero,
|
||||
@ -26,6 +27,8 @@ pub(crate) enum Operation {
|
||||
Eq,
|
||||
ExitKernel,
|
||||
BinaryLogic(logic::Op),
|
||||
BinaryArithmetic(arithmetic::BinaryOperator),
|
||||
TernaryArithmetic(arithmetic::TernaryOperator),
|
||||
NotImplemented,
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
use plonky2::field::extension::Extendable;
|
||||
use plonky2::field::polynomial::PolynomialValues;
|
||||
use plonky2::field::types::Field;
|
||||
use plonky2::hash::hash_types::RichField;
|
||||
use plonky2::util::timing::TimingTree;
|
||||
|
||||
@ -12,7 +11,7 @@ use crate::keccak_memory::keccak_memory_stark::KeccakMemoryOp;
|
||||
use crate::keccak_sponge::keccak_sponge_stark::KeccakSpongeOp;
|
||||
use crate::util::trace_rows_to_poly_values;
|
||||
use crate::witness::memory::MemoryOp;
|
||||
use crate::{keccak, logic};
|
||||
use crate::{arithmetic, keccak, logic};
|
||||
|
||||
type ArithmeticRow<T> = [T; NUM_ARITH_COLUMNS];
|
||||
|
||||
@ -28,7 +27,7 @@ pub struct TraceCheckpoint {
|
||||
pub(crate) struct Traces<T: Copy> {
|
||||
pub(crate) cpu: Vec<CpuColumnsView<T>>,
|
||||
pub(crate) logic_ops: Vec<logic::Operation>,
|
||||
pub(crate) arithmetic: Vec<ArithmeticRow<T>>,
|
||||
pub(crate) arithmetic: Vec<arithmetic::Operation>,
|
||||
pub(crate) memory_ops: Vec<MemoryOp>,
|
||||
pub(crate) keccak_inputs: Vec<[u64; keccak::keccak_stark::NUM_INPUTS]>,
|
||||
pub(crate) keccak_memory_inputs: Vec<KeccakMemoryOp>,
|
||||
@ -70,12 +69,12 @@ impl<T: Copy> Traces<T> {
|
||||
self.cpu.push(val);
|
||||
}
|
||||
|
||||
pub fn push_logic(&mut self, val: logic::Operation) {
|
||||
self.logic_ops.push(val);
|
||||
pub fn push_logic(&mut self, op: logic::Operation) {
|
||||
self.logic_ops.push(op);
|
||||
}
|
||||
|
||||
pub fn push_arithmetic(&mut self, val: ArithmeticRow<T>) {
|
||||
self.arithmetic.push(val);
|
||||
pub fn push_arithmetic(&mut self, op: arithmetic::Operation) {
|
||||
self.arithmetic.push(op);
|
||||
}
|
||||
|
||||
pub fn push_memory(&mut self, val: MemoryOp) {
|
||||
|
||||
@ -115,7 +115,7 @@ fn decode(registers_state: RegistersState, opcode: u8) -> Result<Operation, Prog
|
||||
(0x5d, true) => Ok(Operation::NotImplemented),
|
||||
(0x5e, true) => Ok(Operation::NotImplemented),
|
||||
(0x5f, true) => Ok(Operation::NotImplemented),
|
||||
(0x60..=0x7f, _) => Ok(Operation::NotImplemented),
|
||||
(0x60..=0x7f, _) => Ok(Operation::Push(opcode & 0x1f)),
|
||||
(0x80..=0x8f, _) => Ok(Operation::Dup(opcode & 0xf)),
|
||||
(0x90..=0x9f, _) => Ok(Operation::Swap(opcode & 0xf)),
|
||||
(0xa0, _) => Ok(Operation::Syscall(opcode)),
|
||||
@ -145,6 +145,7 @@ fn decode(registers_state: RegistersState, opcode: u8) -> Result<Operation, Prog
|
||||
fn fill_op_flag<F: Field>(op: Operation, row: &mut CpuColumnsView<F>) {
|
||||
let flags = &mut row.op;
|
||||
*match op {
|
||||
Operation::Push(_) => &mut flags.push,
|
||||
Operation::Dup(_) => &mut flags.dup,
|
||||
Operation::Swap(_) => &mut flags.swap,
|
||||
Operation::Iszero => &mut flags.iszero,
|
||||
@ -155,7 +156,7 @@ fn fill_op_flag<F: Field>(op: Operation, row: &mut CpuColumnsView<F>) {
|
||||
Operation::BinaryLogic(logic::Op::And) => &mut flags.and,
|
||||
Operation::BinaryLogic(logic::Op::Or) => &mut flags.or,
|
||||
Operation::BinaryLogic(logic::Op::Xor) => &mut flags.xor,
|
||||
Operation::NotImplemented => panic!("operation not implemented"),
|
||||
_ => panic!("operation not implemented: {:?}", op),
|
||||
} = F::ONE;
|
||||
}
|
||||
|
||||
@ -179,7 +180,7 @@ fn perform_op<F: Field>(
|
||||
Operation::BinaryLogic(binary_logic_op) => {
|
||||
generate_binary_logic_op(binary_logic_op, registers_state, memory_state, traces, row)?
|
||||
}
|
||||
Operation::NotImplemented => panic!("operation not implemented"),
|
||||
_ => panic!("operation not implemented: {:?}", op),
|
||||
};
|
||||
|
||||
new_registers_state.program_counter += match op {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user