Skip to content

Commit

Permalink
Get a generic impl of StarkFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashtare committed Sep 20, 2023
1 parent 3e27b98 commit 804497c
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 221 deletions.
33 changes: 3 additions & 30 deletions evm/src/arithmetic/arithmetic_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::all_stark::Table;
use crate::arithmetic::{addcy, byte, columns, divmod, modular, mul, Operation};
use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
use crate::cross_table_lookup::{Column, TableWithColumns};
use crate::evaluation_frame::StarkEvaluationFrame;
use crate::evaluation_frame::{StarkEvaluationFrame, StarkFrame};
use crate::lookup::{eval_lookups, eval_lookups_circuit, permuted_cols};
use crate::permutation::PermutationPair;
use crate::stark::Stark;
Expand Down Expand Up @@ -169,40 +169,13 @@ impl<F: RichField, const D: usize> ArithmeticStark<F, D> {
}
}

pub struct ArithmeticStarkEvaluationFrame<T: Copy + Default> {
local_values: [T; NUM_ARITH_COLUMNS],
next_values: [T; NUM_ARITH_COLUMNS],
}

impl<T: Copy + Default> StarkEvaluationFrame<T> for ArithmeticStarkEvaluationFrame<T> {
const COLUMNS: usize = NUM_ARITH_COLUMNS;

fn get_local_values(&self) -> &[T] {
&self.local_values
}

fn get_next_values(&self) -> &[T] {
&self.next_values
}

fn from_values(lv: &[T], nv: &[T]) -> Self {
assert_eq!(lv.len(), Self::COLUMNS);
assert_eq!(nv.len(), Self::COLUMNS);

Self {
local_values: lv.try_into().unwrap(),
next_values: nv.try_into().unwrap(),
}
}
}

impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for ArithmeticStark<F, D> {
type EvaluationFrame<FE, P, const D2: usize> = ArithmeticStarkEvaluationFrame<P>
type EvaluationFrame<FE, P, const D2: usize> = StarkFrame<P, NUM_ARITH_COLUMNS>
where
FE: FieldExtension<D2, BaseField = F>,
P: PackedField<Scalar = FE>;

type EvaluationFrameTarget = ArithmeticStarkEvaluationFrame<ExtensionTarget<D>>;
type EvaluationFrameTarget = StarkFrame<ExtensionTarget<D>, NUM_ARITH_COLUMNS>;

fn eval_packed_generic<FE, P, const D2: usize>(
&self,
Expand Down
33 changes: 3 additions & 30 deletions evm/src/byte_packing/byte_packing_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use crate::byte_packing::columns::{
};
use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
use crate::cross_table_lookup::Column;
use crate::evaluation_frame::StarkEvaluationFrame;
use crate::evaluation_frame::{StarkEvaluationFrame, StarkFrame};
use crate::lookup::{eval_lookups, eval_lookups_circuit, permuted_cols};
use crate::stark::Stark;
use crate::witness::memory::MemoryAddress;
Expand Down Expand Up @@ -280,40 +280,13 @@ impl<F: RichField + Extendable<D>, const D: usize> BytePackingStark<F, D> {
}
}

pub struct BytePackingStarkEvaluationFrame<T: Copy + Default> {
local_values: [T; NUM_COLUMNS],
next_values: [T; NUM_COLUMNS],
}

impl<T: Copy + Default> StarkEvaluationFrame<T> for BytePackingStarkEvaluationFrame<T> {
const COLUMNS: usize = NUM_COLUMNS;

fn get_local_values(&self) -> &[T] {
&self.local_values
}

fn get_next_values(&self) -> &[T] {
&self.next_values
}

fn from_values(lv: &[T], nv: &[T]) -> Self {
assert_eq!(lv.len(), Self::COLUMNS);
assert_eq!(nv.len(), Self::COLUMNS);

Self {
local_values: lv.try_into().unwrap(),
next_values: nv.try_into().unwrap(),
}
}
}

impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for BytePackingStark<F, D> {
type EvaluationFrame<FE, P, const D2: usize> = BytePackingStarkEvaluationFrame<P>
type EvaluationFrame<FE, P, const D2: usize> = StarkFrame<P, NUM_COLUMNS>
where
FE: FieldExtension<D2, BaseField = F>,
P: PackedField<Scalar = FE>;

type EvaluationFrameTarget = BytePackingStarkEvaluationFrame<ExtensionTarget<D>>;
type EvaluationFrameTarget = StarkFrame<ExtensionTarget<D>, NUM_COLUMNS>;

fn eval_packed_generic<FE, P, const D2: usize>(
&self,
Expand Down
44 changes: 12 additions & 32 deletions evm/src/cpu/cpu_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::cpu::{
modfp254, pc, push0, shift, simple_logic, stack, stack_bounds, syscalls_exceptions,
};
use crate::cross_table_lookup::{Column, TableWithColumns};
use crate::evaluation_frame::StarkEvaluationFrame;
use crate::evaluation_frame::{StarkEvaluationFrame, StarkFrame};
use crate::memory::segments::Segment;
use crate::memory::{NUM_CHANNELS, VALUE_LIMBS};
use crate::stark::Stark;
Expand Down Expand Up @@ -227,40 +227,13 @@ impl<F: RichField, const D: usize> CpuStark<F, D> {
}
}

pub struct CpuStarkEvaluationFrame<T: Copy + Default> {
local_values: [T; NUM_CPU_COLUMNS],
next_values: [T; NUM_CPU_COLUMNS],
}

impl<T: Copy + Default> StarkEvaluationFrame<T> for CpuStarkEvaluationFrame<T> {
const COLUMNS: usize = NUM_CPU_COLUMNS;

fn get_local_values(&self) -> &[T] {
&self.local_values
}

fn get_next_values(&self) -> &[T] {
&self.next_values
}

fn from_values(lv: &[T], nv: &[T]) -> Self {
assert_eq!(lv.len(), Self::COLUMNS);
assert_eq!(nv.len(), Self::COLUMNS);

Self {
local_values: lv.try_into().unwrap(),
next_values: nv.try_into().unwrap(),
}
}
}

impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for CpuStark<F, D> {
type EvaluationFrame<FE, P, const D2: usize> = CpuStarkEvaluationFrame<P>
type EvaluationFrame<FE, P, const D2: usize> = StarkFrame<P, NUM_CPU_COLUMNS>
where
FE: FieldExtension<D2, BaseField = F>,
P: PackedField<Scalar = FE>;

type EvaluationFrameTarget = CpuStarkEvaluationFrame<ExtensionTarget<D>>;
type EvaluationFrameTarget = StarkFrame<ExtensionTarget<D>, NUM_CPU_COLUMNS>;

fn eval_packed_generic<FE, P, const D2: usize>(
&self,
Expand Down Expand Up @@ -303,8 +276,15 @@ impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for CpuStark<F, D
vars: &Self::EvaluationFrameTarget,
yield_constr: &mut RecursiveConstraintConsumer<F, D>,
) {
let local_values = vars.local_values.borrow();
let next_values = vars.next_values.borrow();
let local_values =
TryInto::<[ExtensionTarget<D>; NUM_CPU_COLUMNS]>::try_into(vars.get_local_values())
.unwrap();
let local_values: &CpuColumnsView<ExtensionTarget<D>> = local_values.borrow();
let next_values =
TryInto::<[ExtensionTarget<D>; NUM_CPU_COLUMNS]>::try_into(vars.get_next_values())
.unwrap();
let next_values: &CpuColumnsView<ExtensionTarget<D>> = next_values.borrow();

bootstrap_kernel::eval_bootstrap_kernel_ext_circuit(
builder,
local_values,
Expand Down
27 changes: 27 additions & 0 deletions evm/src/evaluation_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,30 @@ pub trait StarkEvaluationFrame<T: Copy + Clone + Default>: Sized {
/// the provided slices lengths match the `Self::COLUMNS` value.
fn from_values(lv: &[T], nv: &[T]) -> Self;
}

pub struct StarkFrame<T: Copy + Clone + Default, const N: usize> {
local_values: [T; N],
next_values: [T; N],
}

impl<T: Copy + Clone + Default, const N: usize> StarkEvaluationFrame<T> for StarkFrame<T, N> {
const COLUMNS: usize = N;

fn get_local_values(&self) -> &[T] {
&self.local_values
}

fn get_next_values(&self) -> &[T] {
&self.next_values
}

fn from_values(lv: &[T], nv: &[T]) -> Self {
assert_eq!(lv.len(), Self::COLUMNS);
assert_eq!(nv.len(), Self::COLUMNS);

Self {
local_values: lv.try_into().unwrap(),
next_values: nv.try_into().unwrap(),
}
}
}
32 changes: 3 additions & 29 deletions evm/src/keccak/keccak_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use plonky2::util::timing::TimingTree;

use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
use crate::cross_table_lookup::Column;
use crate::evaluation_frame::StarkEvaluationFrame;
use crate::evaluation_frame::{StarkEvaluationFrame, StarkFrame};
use crate::keccak::columns::{
reg_a, reg_a_prime, reg_a_prime_prime, reg_a_prime_prime_0_0_bit, reg_a_prime_prime_prime,
reg_b, reg_c, reg_c_prime, reg_input_limb, reg_output_limb, reg_preimage, reg_step,
Expand Down Expand Up @@ -238,40 +238,14 @@ impl<F: RichField + Extendable<D>, const D: usize> KeccakStark<F, D> {
trace_polys
}
}
pub struct KeccakStarkEvaluationFrame<T: Copy + Default> {
local_values: [T; NUM_COLUMNS],
next_values: [T; NUM_COLUMNS],
}

impl<T: Copy + Default> StarkEvaluationFrame<T> for KeccakStarkEvaluationFrame<T> {
const COLUMNS: usize = NUM_COLUMNS;

fn get_local_values(&self) -> &[T] {
&self.local_values
}

fn get_next_values(&self) -> &[T] {
&self.next_values
}

fn from_values(lv: &[T], nv: &[T]) -> Self {
assert_eq!(lv.len(), Self::COLUMNS);
assert_eq!(nv.len(), Self::COLUMNS);

Self {
local_values: lv.try_into().unwrap(),
next_values: nv.try_into().unwrap(),
}
}
}

impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for KeccakStark<F, D> {
type EvaluationFrame<FE, P, const D2: usize> = KeccakStarkEvaluationFrame<P>
type EvaluationFrame<FE, P, const D2: usize> = StarkFrame<P, NUM_COLUMNS>
where
FE: FieldExtension<D2, BaseField = F>,
P: PackedField<Scalar = FE>;

type EvaluationFrameTarget = KeccakStarkEvaluationFrame<ExtensionTarget<D>>;
type EvaluationFrameTarget = StarkFrame<ExtensionTarget<D>, NUM_COLUMNS>;

fn eval_packed_generic<FE, P, const D2: usize>(
&self,
Expand Down
9 changes: 4 additions & 5 deletions evm/src/keccak/round_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ use plonky2::hash::hash_types::RichField;
use plonky2::iop::ext_target::ExtensionTarget;
use plonky2::plonk::circuit_builder::CircuitBuilder;

use super::keccak_stark::KeccakStarkEvaluationFrame;
use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
use crate::evaluation_frame::StarkEvaluationFrame;
use crate::keccak::columns::reg_step;
use crate::evaluation_frame::{StarkEvaluationFrame, StarkFrame};
use crate::keccak::columns::{reg_step, NUM_COLUMNS};
use crate::keccak::keccak_stark::NUM_ROUNDS;

pub(crate) fn eval_round_flags<F: Field, P: PackedField<Scalar = F>>(
vars: &KeccakStarkEvaluationFrame<P>,
vars: &StarkFrame<P, NUM_COLUMNS>,
yield_constr: &mut ConstraintConsumer<P>,
) {
let local_values = vars.get_local_values();
Expand Down Expand Up @@ -41,7 +40,7 @@ pub(crate) fn eval_round_flags<F: Field, P: PackedField<Scalar = F>>(

pub(crate) fn eval_round_flags_recursively<F: RichField + Extendable<D>, const D: usize>(
builder: &mut CircuitBuilder<F, D>,
vars: &KeccakStarkEvaluationFrame<ExtensionTarget<D>>,
vars: &StarkFrame<ExtensionTarget<D>, NUM_COLUMNS>,
yield_constr: &mut RecursiveConstraintConsumer<F, D>,
) {
let one = builder.one_extension();
Expand Down
53 changes: 19 additions & 34 deletions evm/src/keccak_sponge/keccak_sponge_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use plonky2_util::ceil_div_usize;
use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
use crate::cpu::kernel::keccak_util::keccakf_u32s;
use crate::cross_table_lookup::Column;
use crate::evaluation_frame::StarkEvaluationFrame;
use crate::evaluation_frame::{StarkEvaluationFrame, StarkFrame};
use crate::keccak_sponge::columns::*;
use crate::stark::Stark;
use crate::util::trace_rows_to_poly_values;
Expand Down Expand Up @@ -422,40 +422,13 @@ impl<F: RichField + Extendable<D>, const D: usize> KeccakSpongeStark<F, D> {
}
}

pub struct KeccakSpongeStarkEvaluationFrame<T: Copy + Default> {
local_values: [T; NUM_KECCAK_SPONGE_COLUMNS],
next_values: [T; NUM_KECCAK_SPONGE_COLUMNS],
}

impl<T: Copy + Default> StarkEvaluationFrame<T> for KeccakSpongeStarkEvaluationFrame<T> {
const COLUMNS: usize = NUM_KECCAK_SPONGE_COLUMNS;

fn get_local_values(&self) -> &[T] {
&self.local_values
}

fn get_next_values(&self) -> &[T] {
&self.next_values
}

fn from_values(lv: &[T], nv: &[T]) -> Self {
assert_eq!(lv.len(), Self::COLUMNS);
assert_eq!(nv.len(), Self::COLUMNS);

Self {
local_values: lv.try_into().unwrap(),
next_values: nv.try_into().unwrap(),
}
}
}

impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for KeccakSpongeStark<F, D> {
type EvaluationFrame<FE, P, const D2: usize> = KeccakSpongeStarkEvaluationFrame<P>
type EvaluationFrame<FE, P, const D2: usize> = StarkFrame<P, NUM_KECCAK_SPONGE_COLUMNS>
where
FE: FieldExtension<D2, BaseField = F>,
P: PackedField<Scalar = FE>;

type EvaluationFrameTarget = KeccakSpongeStarkEvaluationFrame<ExtensionTarget<D>>;
type EvaluationFrameTarget = StarkFrame<ExtensionTarget<D>, NUM_KECCAK_SPONGE_COLUMNS>;

fn eval_packed_generic<FE, P, const D2: usize>(
&self,
Expand All @@ -465,8 +438,12 @@ impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for KeccakSpongeS
FE: FieldExtension<D2, BaseField = F>,
P: PackedField<Scalar = FE>,
{
let local_values: &KeccakSpongeColumnsView<P> = vars.local_values.borrow();
let next_values: &KeccakSpongeColumnsView<P> = vars.next_values.borrow();
let local_values =
TryInto::<[P; NUM_KECCAK_SPONGE_COLUMNS]>::try_into(vars.get_local_values()).unwrap();
let local_values: &KeccakSpongeColumnsView<P> = local_values.borrow();
let next_values =
TryInto::<[P; NUM_KECCAK_SPONGE_COLUMNS]>::try_into(vars.get_next_values()).unwrap();
let next_values: &KeccakSpongeColumnsView<P> = next_values.borrow();

// Each flag (full-input block, final block or implied dummy flag) must be boolean.
let is_full_input_block = local_values.is_full_input_block;
Expand Down Expand Up @@ -572,8 +549,16 @@ impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for KeccakSpongeS
vars: &Self::EvaluationFrameTarget,
yield_constr: &mut RecursiveConstraintConsumer<F, D>,
) {
let local_values: &KeccakSpongeColumnsView<ExtensionTarget<D>> = vars.local_values.borrow();
let next_values: &KeccakSpongeColumnsView<ExtensionTarget<D>> = vars.next_values.borrow();
let local_values = TryInto::<[ExtensionTarget<D>; NUM_KECCAK_SPONGE_COLUMNS]>::try_into(
vars.get_local_values(),
)
.unwrap();
let local_values: &KeccakSpongeColumnsView<ExtensionTarget<D>> = local_values.borrow();
let next_values = TryInto::<[ExtensionTarget<D>; NUM_KECCAK_SPONGE_COLUMNS]>::try_into(
vars.get_next_values(),
)
.unwrap();
let next_values: &KeccakSpongeColumnsView<ExtensionTarget<D>> = next_values.borrow();

let one = builder.one_extension();

Expand Down
Loading

0 comments on commit 804497c

Please sign in to comment.