Skip to content

Commit

Permalink
Update to latest plonky and starky crate
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekvpandya committed Oct 11, 2023
1 parent dbe5702 commit f754022
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion poseidon2-starky/benches/poseidon2_starky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn bench_horizen_poseidon2_starky(c: &mut Criterion) {
b.iter_batched(
|| trace_poly_values.clone(),
|trace_poly_values| {
prove::<F, C, S, D>(stark, &config, trace_poly_values, [], &mut timing).unwrap();
prove::<F, C, S, D>(stark, &config, trace_poly_values, &[], &mut timing).unwrap();
},
criterion::BatchSize::SmallInput,
);
Expand Down
2 changes: 1 addition & 1 deletion poseidon2-starky/benches/poseidon2_starky_plonky2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn bench_plonky2_poseidon2_starky(c: &mut Criterion) {
b.iter_batched(
|| trace_poly_values.clone(),
|trace_poly_values| {
prove::<F, C, S, D>(stark, &config, trace_poly_values, [], &mut timing).unwrap();
prove::<F, C, S, D>(stark, &config, trace_poly_values, &[], &mut timing).unwrap();
},
criterion::BatchSize::SmallInput,
);
Expand Down
22 changes: 15 additions & 7 deletions poseidon2-starky/src/horizen/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use plonky2::field::packed::PackedField;
use plonky2::field::polynomial::PolynomialValues;
use plonky2::field::types::Field;
use plonky2::hash::hash_types::RichField;
use plonky2::iop::ext_target::ExtensionTarget;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
use starky::evaluation_frame::{StarkEvaluationFrame, StarkFrame};
use starky::stark::Stark;
use starky::vars::{StarkEvaluationTargets, StarkEvaluationVars};
use std::marker::PhantomData;
use zkhash::fields::goldilocks::FpGoldiLocks;
use zkhash::poseidon2::poseidon2_instance_goldilocks::{MAT_DIAG8_M_1, RC8};
Expand Down Expand Up @@ -149,19 +150,26 @@ pub struct Poseidon2Stark<F, const D: usize> {
pub _f: PhantomData<F>,
}

const COLUMNS: usize = NUM_COLS;
const PUBLIC_INPUTS: usize = 0;

impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for Poseidon2Stark<F, D> {
const COLUMNS: usize = NUM_COLS;
const PUBLIC_INPUTS: usize = 0;
type EvaluationFrame<FE, P, const D2: usize> = StarkFrame<P, P::Scalar, COLUMNS, PUBLIC_INPUTS>
where
FE: FieldExtension<D2, BaseField = F>,
P: PackedField<Scalar = FE>;
type EvaluationFrameTarget =
StarkFrame<ExtensionTarget<D>, ExtensionTarget<D>, COLUMNS, PUBLIC_INPUTS>;

fn eval_packed_generic<FE, P, const D2: usize>(
&self,
vars: StarkEvaluationVars<FE, P, { Self::COLUMNS }, { Self::PUBLIC_INPUTS }>,
vars: &Self::EvaluationFrame<FE, P, D2>,
yield_constr: &mut ConstraintConsumer<P>,
) where
FE: FieldExtension<D2, BaseField = F>,
P: PackedField<Scalar = FE>,
{
let lv = vars.local_values;
let lv = vars.get_local_values();
let mut state = matmul_external8_constraints(lv[0..STATE_SIZE].try_into().unwrap());

// first full rounds
Expand Down Expand Up @@ -219,7 +227,7 @@ impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for Poseidon2Star
fn eval_ext_circuit(
&self,
_builder: &mut CircuitBuilder<F, D>,
_vars: StarkEvaluationTargets<D, { Self::COLUMNS }, { Self::PUBLIC_INPUTS }>,
_vars: &Self::EvaluationFrameTarget,
_yield_constr: &mut RecursiveConstraintConsumer<F, D>,
) {
unimplemented!()
Expand Down Expand Up @@ -274,7 +282,7 @@ mod tests {
stark,
&config,
trace_poly_values,
[],
&[],
&mut TimingTree::default(),
)?;
verify_stark_proof(stark, proof, &config)
Expand Down
2 changes: 1 addition & 1 deletion poseidon2-starky/src/plonky2/generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub fn generate_poseidon2_trace<F: RichField>(step_rows: &Vec<Row<F>>) -> [Vec<F

add_rows(step_rows, true);

if step_rows.len() == 0 {
if step_rows.is_empty() {
let preimage = (0..STATE_SIZE).map(|_| F::rand()).collect::<Vec<_>>();
let dummy_rows = vec![Row {
preimage: preimage.try_into().expect("can't fail"),
Expand Down
23 changes: 16 additions & 7 deletions poseidon2-starky/src/plonky2/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use plonky2::field::polynomial::PolynomialValues;
use plonky2::field::types::Field;
use plonky2::hash::hash_types::RichField;
use plonky2::hash::poseidon2::Poseidon2;
use plonky2::iop::ext_target::ExtensionTarget;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use starky::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
use starky::evaluation_frame::{StarkEvaluationFrame, StarkFrame};
use starky::stark::Stark;
use starky::vars::{StarkEvaluationTargets, StarkEvaluationVars};
use std::fmt::Display;
use std::marker::PhantomData;

Expand Down Expand Up @@ -199,22 +200,30 @@ impl<F, const D: usize> Display for Poseidon2_12Stark<F, D> {
}
}

const COLUMNS: usize = NUM_COLS;
const PUBLIC_INPUTS: usize = 0;

impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for Poseidon2_12Stark<F, D> {
const COLUMNS: usize = NUM_COLS;
const PUBLIC_INPUTS: usize = 0;
type EvaluationFrame<FE, P, const D2: usize> = StarkFrame<P, P::Scalar, COLUMNS, PUBLIC_INPUTS>
where
FE: FieldExtension<D2, BaseField = F>,
P: PackedField<Scalar = FE>;
type EvaluationFrameTarget =
StarkFrame<ExtensionTarget<D>, ExtensionTarget<D>, COLUMNS, PUBLIC_INPUTS>;
fn eval_packed_generic<FE, P, const D2: usize>(
&self,
vars: StarkEvaluationVars<FE, P, { Self::COLUMNS }, { Self::PUBLIC_INPUTS }>,
vars: &Self::EvaluationFrame<FE, P, D2>,
yield_constr: &mut ConstraintConsumer<P>,
) where
FE: FieldExtension<D2, BaseField = F>,
P: PackedField<Scalar = FE>,
{
let lv = vars.local_values;
let lv = vars.get_local_values();

// row can be execution or padding.
yield_constr.constraint(lv[COL_IS_EXE] * (lv[COL_IS_EXE] - P::ONES));

#[allow(clippy::range_plus_one)]
let mut state: [P; STATE_SIZE] = matmul_external12_constraints(
lv[COL_INPUT_START..(COL_INPUT_START + STATE_SIZE)]
.try_into()
Expand Down Expand Up @@ -273,7 +282,7 @@ impl<F: RichField + Extendable<D>, const D: usize> Stark<F, D> for Poseidon2_12S
fn eval_ext_circuit(
&self,
_builder: &mut CircuitBuilder<F, D>,
_vars: StarkEvaluationTargets<D, { Self::COLUMNS }, { Self::PUBLIC_INPUTS }>,
_vars: &Self::EvaluationFrameTarget,
_yield_constr: &mut RecursiveConstraintConsumer<F, D>,
) {
unimplemented!()
Expand Down Expand Up @@ -328,7 +337,7 @@ mod tests {
stark,
&config,
trace_poly_values,
[],
&[],
&mut TimingTree::default(),
)?;
verify_stark_proof(stark, proof, &config)
Expand Down

0 comments on commit f754022

Please sign in to comment.