From 235fbe976a2d93abe21aded21de3ff71ef8bb4e6 Mon Sep 17 00:00:00 2001 From: Vivek Pandya Date: Mon, 9 Oct 2023 15:02:01 +0530 Subject: [PATCH] Add column for if is ROW is padding or execution --- poseidon2-starky/src/plonky2/columns.rs | 17 ++++++++++------- poseidon2-starky/src/plonky2/generation.rs | 11 ++++++++++- poseidon2-starky/src/plonky2/stark.rs | 13 +++++++++++-- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/poseidon2-starky/src/plonky2/columns.rs b/poseidon2-starky/src/plonky2/columns.rs index deccaf5..42caf62 100644 --- a/poseidon2-starky/src/plonky2/columns.rs +++ b/poseidon2-starky/src/plonky2/columns.rs @@ -6,28 +6,31 @@ pub(crate) const SBOX_DEGREE: usize = 7; pub(crate) const ROUNDS_F: usize = 8; pub(crate) const ROUNDS_P: usize = 22; +/// If the row is execution or padding. +pub(crate) const COL_IS_EXE: usize = 0; + /// The starting point of the column input -pub(crate) const COL_INPUT_START: usize = 0; +pub(crate) const COL_INPUT_START: usize = 1; /// The starting point of the state after each 1st full round -pub(crate) const COL_1ST_FULLROUND_STATE_START: usize = COL_INPUT_START + STATE_SIZE; // 12 +pub(crate) const COL_1ST_FULLROUND_STATE_START: usize = COL_INPUT_START + STATE_SIZE; // 1 + 12 /// The value of state[0] after each partial round pub(crate) const COL_PARTIAL_ROUND_STATE_START: usize = - COL_1ST_FULLROUND_STATE_START + STATE_SIZE * (ROUNDS_F / 2); // 12 + 48 + COL_1ST_FULLROUND_STATE_START + STATE_SIZE * (ROUNDS_F / 2); // 1 + 12 + 48 /// The starting point of the state after the partial round pub(crate) const COL_PARTIAL_ROUND_END_STATE_START: usize = - COL_PARTIAL_ROUND_STATE_START + ROUNDS_P - 1; // 12 + 48 + 22 + COL_PARTIAL_ROUND_STATE_START + ROUNDS_P - 1; // 1 + 12 + 48 + 22 /// The starting point of the state after each 2nd full round pub(crate) const COL_2ND_FULLROUND_STATE_START: usize = - COL_PARTIAL_ROUND_END_STATE_START + STATE_SIZE; // 12 + 48 + 22 + 12 + COL_PARTIAL_ROUND_END_STATE_START + STATE_SIZE; // 1 + 12 + 48 + 22 + 12 /// The starting point of the column output /// This is the same as the last state after the 2nd full round pub(crate) const COL_OUTPUT_START: usize = - COL_2ND_FULLROUND_STATE_START + STATE_SIZE * ((ROUNDS_F / 2) - 1); // 12 + 48 + 22 + 12 + 36 + COL_2ND_FULLROUND_STATE_START + STATE_SIZE * ((ROUNDS_F / 2) - 1); // 1 + 12 + 48 + 22 + 12 + 36 /// The total number of columns -pub(crate) const NUM_COLS: usize = COL_2ND_FULLROUND_STATE_START + STATE_SIZE * (ROUNDS_F / 2); // 12 + 96 + 22 + 12 + 36 + 12 +pub(crate) const NUM_COLS: usize = COL_2ND_FULLROUND_STATE_START + STATE_SIZE * (ROUNDS_F / 2); // 1 + 12 + 96 + 22 + 12 + 36 + 12 diff --git a/poseidon2-starky/src/plonky2/generation.rs b/poseidon2-starky/src/plonky2/generation.rs index c1be9fe..1694891 100644 --- a/poseidon2-starky/src/plonky2/generation.rs +++ b/poseidon2-starky/src/plonky2/generation.rs @@ -8,6 +8,8 @@ use plonky2::hash::{ poseidon2::{Poseidon2, WIDTH}, }; +use super::columns::COL_IS_EXE; + // Represent a row of the preimage #[derive(Debug, Clone, Default)] pub struct Row { @@ -17,12 +19,17 @@ pub struct Row { /// Pad the trace to a power of 2. #[must_use] fn pad_trace(mut trace: Vec>) -> Vec> { - let ext_trace_len = trace[0].len().next_power_of_two(); + let original_len = trace[0].len(); + let ext_trace_len = original_len.next_power_of_two(); // All columns have their last value duplicated. for row in &mut trace { row.resize(ext_trace_len, *row.last().unwrap()); } + // Set COL_IS_EXE to ZERO + for i in original_len..ext_trace_len { + trace[COL_IS_EXE][i] = F::ZERO; + } trace } @@ -93,6 +100,8 @@ pub fn generate_poseidon2_trace(step_rows: &Vec>) -> [Vec> = vec![vec![F::ZERO; trace_len]; NUM_COLS]; for (i, row) in step_rows.iter().enumerate() { + trace[COL_IS_EXE][i] = F::ONE; + for j in 0..STATE_SIZE { trace[COL_INPUT_START + j][i] = row.preimage[j]; } diff --git a/poseidon2-starky/src/plonky2/stark.rs b/poseidon2-starky/src/plonky2/stark.rs index 2749d0c..f504043 100644 --- a/poseidon2-starky/src/plonky2/stark.rs +++ b/poseidon2-starky/src/plonky2/stark.rs @@ -16,6 +16,8 @@ use starky::vars::{StarkEvaluationTargets, StarkEvaluationVars}; use std::fmt::Display; use std::marker::PhantomData; +use super::columns::{COL_INPUT_START, COL_IS_EXE}; + // degree: 1 fn add_rc_constraints< F: RichField + Extendable, @@ -209,8 +211,15 @@ impl, const D: usize> Stark for Poseidon2_12S P: PackedField, { let lv = vars.local_values; - let mut state: [P; STATE_SIZE] = - matmul_external12_constraints(lv[0..STATE_SIZE].try_into().unwrap()); + + // row can be execution or padding. + yield_constr.constraint(lv[COL_IS_EXE] * (lv[COL_IS_EXE] - P::ONES)); + + let mut state: [P; STATE_SIZE] = matmul_external12_constraints( + lv[COL_INPUT_START..(COL_INPUT_START + STATE_SIZE)] + .try_into() + .unwrap(), + ); // first full rounds for r in 0..(ROUNDS_F / 2) { state = add_rc_constraints(&state, r);