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

WIP: Fixes for 15 wires PLONK #126

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
62 changes: 1 addition & 61 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[workspace]
members = [
"dlog",
"pairing",
"oracle",
"dlog_solver",
]
Expand Down
48 changes: 38 additions & 10 deletions circuits/plonk-15-wires/src/gates/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,61 @@ Constraint vector format:
*****************************************************************************************************************/

use algebra::FftField;
use oracle::poseidon::{ArithmeticSpongeParams, Plonk15SpongeConstants, sbox};
use crate::{wires::GateWires, wires::{COLUMNS, WIRES}, nolookup::constraints::ConstraintSystem};
use oracle::poseidon::{SpongeConstants, Plonk15SpongeConstants, sbox};
use crate::{wires::GateWires, wires::{COLUMNS}, nolookup::constraints::ConstraintSystem};
use crate::gate::{CircuitGate, GateType};
use array_init::array_init;
use std::ops::Range;

pub const SPONGE_WIDTH: usize = SPONGE_WIDTH;
pub const SPONGE_WIDTH: usize = Plonk15SpongeConstants::SPONGE_WIDTH;
pub const ROUNDS_PER_ROW: usize = COLUMNS / SPONGE_WIDTH;

// There are 5 round states per row. We put the first state first, followed by the last state
// so that they are both accessible by the permutation argument.
pub const STATE_ORDER : [usize; ROUNDS_PER_ROW] = [0, 2, 3, 4, 1];

pub fn round_range(i : usize) -> Range<usize> {
let slot = STATE_ORDER[i];
let start = slot * SPONGE_WIDTH;
start..(start + SPONGE_WIDTH)
}

impl<F: FftField> CircuitGate<F>
{
pub fn create_poseidon
(
row: usize,
wires: GateWires,
c: Vec<F>
// Coefficients are passed in in the logical order
c: [[F; SPONGE_WIDTH]; ROUNDS_PER_ROW]
) -> Self
{
CircuitGate
{
row,
typ: GateType::Poseidon,
wires,
c
c: c.iter().flatten().map(|x| *x).collect()
}
}

pub fn verify_poseidon(&self, witness: &[Vec<F>; COLUMNS], cs: &ConstraintSystem<F>) -> bool
{
let this: [[F; SPONGE_WIDTH]; ROUNDS_PER_ROW] = array_init(|i| array_init(|j| witness[ROUNDS_PER_ROW*i+j][self.row]));
let next: [F; SPONGE_WIDTH] = array_init(|i| witness[i][self.row+1]);
// TODO: Needs to be fixed

let this: [[F; SPONGE_WIDTH]; ROUNDS_PER_ROW] = array_init(|round| {
let wire = STATE_ORDER[round];
array_init(|col| witness[col + wire * SPONGE_WIDTH][self.row])
});
let next: [F; SPONGE_WIDTH] = array_init(|i| witness[i + STATE_ORDER[0] * SPONGE_WIDTH][self.row+1]);

let rc = self.rc();

let perm: [Vec<F>; ROUNDS_PER_ROW] = array_init
(
|j|
|round|
cs.fr_sponge_params.mds.iter().enumerate().
map(|(i, m)| rc[j][i] + &this[j].iter().zip(m.iter()).fold(F::zero(), |x, (s, &m)| m * sbox::<F, Plonk15SpongeConstants>(*s) + x)).collect::<Vec<_>>()
map(|(i, m)| rc[round][i] + &this[round].iter().zip(m.iter()).fold(F::zero(), |x, (s, &m)| m * sbox::<F, Plonk15SpongeConstants>(*s) + x)).collect::<Vec<_>>()
);

self.typ == GateType::Poseidon
Expand All @@ -56,8 +74,18 @@ impl<F: FftField> CircuitGate<F>
}

pub fn ps(&self) -> F {if self.typ == GateType::Poseidon {F::one()} else {F::zero()}}

// Coefficients are output here in the logical order
pub fn rc(&self) -> [[F; SPONGE_WIDTH]; ROUNDS_PER_ROW]
{
array_init(|i| array_init(|j| if self.typ == GateType::Poseidon {self.c[WIRES[ROUNDS_PER_ROW*i+j]]} else {F::zero()}))
array_init(|round| {
array_init(|col| {
if self.typ == GateType::Poseidon {
self.c[SPONGE_WIDTH*round+col]
} else {
F::zero()
}
})
})
}
}
5 changes: 2 additions & 3 deletions circuits/plonk-15-wires/src/nolookup/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,8 @@ impl<F: FftField + SquareRootField> ConstraintSystem<F>
map(|gate| if gate.typ == GateType::Generic {gate.c[COLUMNS+1]} else {F::zero()}).collect(), domain.d1).interpolate(),

// poseidon constraint polynomials
rcm: array_init(|j| {array_init(|i| E::<F, D<F>>::from_vec_and_domain(gates.iter().
map(|gate| if gate.typ == GateType::Poseidon {gate.rc()[i][j]} else {F::zero()}).collect(), domain.d1).interpolate())}),

rcm: array_init(|round| {array_init(|col| E::<F, D<F>>::from_vec_and_domain(gates.iter().
map(|gate| if gate.typ == GateType::Poseidon {gate.rc()[round][col]} else {F::zero()}).collect(), domain.d1).interpolate())}),

ps4: psm.evaluate_over_domain_by_ref(domain.d4),
ps8: psm.evaluate_over_domain_by_ref(domain.d8),
Expand Down
2 changes: 1 addition & 1 deletion circuits/plonk-15-wires/src/nolookup/scalars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This source file implements Plonk prover polynomial evaluations primitive.

use crate::wires::*;
use algebra::{FftField, Field};
use oracle::{sponge_5_wires::ScalarChallenge, utils::PolyUtils};
use oracle::{sponge::ScalarChallenge, utils::PolyUtils};
use ff_fft::DensePolynomial;
use array_init::array_init;

Expand Down
8 changes: 7 additions & 1 deletion circuits/plonk-15-wires/src/polynomials/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use ff_fft::{Evaluations, DensePolynomial, Radix2EvaluationDomain as D};
use crate::polynomial::WitnessOverDomains;
use crate::nolookup::constraints::ConstraintSystem;
use crate::nolookup::scalars::ProofEvaluations;
use crate::wires::GENERICS;
use oracle::utils::PolyUtils;

impl<F: FftField + SquareRootField> ConstraintSystem<F>
Expand All @@ -26,7 +27,12 @@ impl<F: FftField + SquareRootField> ConstraintSystem<F>

pub fn gnrc_scalars(evals: &ProofEvaluations<F>) -> Vec<F>
{
vec![evals.w[0] * &evals.w[1], evals.w[0], evals.w[1], evals.w[2], evals.w[3], evals.w[4], F::one()]
let mut res = vec![evals.w[0] * &evals.w[1]];
for i in 0..GENERICS {
res.push(evals.w[i]);
}
res.push(F::one());
return res;
}

// generic constraint linearization poly contribution computation
Expand Down
10 changes: 6 additions & 4 deletions circuits/plonk-15-wires/src/polynomials/permutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ impl<F: FftField + SquareRootField> ConstraintSystem<F>
&lagrange.d8.this.w.iter().zip(self.sigmal8.iter()).
map(|(p, s)| p + &(l0 + &s.scale(oracles.beta))).
fold(lagrange.d8.next.z.clone(), |x, y| &x * &y)).
scale(oracles.alpha)
scale(alpha[0])
*
&self.zkpl
,
&bnd1.scale(alpha[0]) + &bnd2.scale(alpha[1])
&bnd1.scale(alpha[1]) + &bnd2.scale(alpha[2])
))
}

Expand All @@ -60,21 +60,23 @@ impl<F: FftField + SquareRootField> ConstraintSystem<F>
(
&self, e: &Vec<ProofEvaluations<F>>,
oracles: &RandomOracles<F>,
alpha: &[F],
) -> DensePolynomial<F>
{
self.sigmam[PERMUTS-1].scale(Self::perm_scalars(e, oracles, self.zkpm.evaluate(oracles.zeta)))
self.sigmam[PERMUTS-1].scale(Self::perm_scalars(e, oracles, alpha, self.zkpm.evaluate(oracles.zeta)))
}

pub fn perm_scalars
(
e: &Vec<ProofEvaluations<F>>,
oracles: &RandomOracles<F>,
alpha: &[F],
z: F,
) -> F
{
-e[0].w.iter().zip(e[0].s.iter()).
map(|(w, s)| oracles.gamma + &(oracles.beta * s) + w).
fold(e[1].z * &oracles.beta * &oracles.alpha * &z, |x, y| x * y)
fold(e[1].z * &oracles.beta * alpha[0] * &z, |x, y| x * y)
}

// permutation aggregation polynomial computation
Expand Down
Loading