Skip to content

Commit

Permalink
[kimchi] fix test after witness change (bis)
Browse files Browse the repository at this point in the history
  • Loading branch information
mimoo committed Oct 22, 2021
1 parent e3e622e commit 4f85ca0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 82 deletions.
22 changes: 8 additions & 14 deletions dlog/plonk-15-wires/tests/ec.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
use ark_ec::{AffineCurve, ProjectiveCurve};
use ark_ff::{BigInteger, BitIteratorLE, Field, One, PrimeField, UniformRand, Zero};
use ark_poly::{univariate::DensePolynomial, EvaluationDomain, Radix2EvaluationDomain as D};
use ark_ff::{Field, One, PrimeField, UniformRand, Zero};
use array_init::array_init;
use colored::Colorize;
use commitment_dlog::{
commitment::{b_poly_coefficients, ceil_log2, CommitmentCurve},
commitment::CommitmentCurve,
srs::{endos, SRS},
};
use groupmap::GroupMap;
use mina_curves::pasta::{
fp::Fp as F,
pallas::{Affine as Other, Projective as OtherProjective},
pallas::Affine as Other,
vesta::{Affine, VestaParameters},
};
use oracle::{
poseidon::{ArithmeticSponge, PlonkSpongeConstants15W, Sponge, SpongeConstants},
sponge::{DefaultFqSponge, DefaultFrSponge, ScalarChallenge},
poseidon::PlonkSpongeConstants15W,
sponge::{DefaultFqSponge, DefaultFrSponge},
};
use plonk_15_wires_circuits::{
expr::{Column, Constants, Expr, Linearization, PolishToken},
gate::{CircuitGate, GateType, LookupInfo, LookupsUsed},
gates::poseidon::ROUNDS_PER_ROW,
nolookup::constraints::{zk_w3, ConstraintSystem},
nolookup::scalars::{LookupEvaluations, ProofEvaluations},
polynomials::endosclmul,
gate::{CircuitGate, GateType},
nolookup::constraints::ConstraintSystem,
wires::*,
};
use plonk_15_wires_protocol_dlog::{index::Index, prover::ProverProof};
use rand::{rngs::StdRng, SeedableRng};
use std::fmt::{Display, Formatter};
use std::{rc::Rc, time::Instant};

const PUBLIC: usize = 0;
Expand Down Expand Up @@ -64,7 +58,7 @@ fn ec_test() {
srs.add_lagrange_basis(cs.domain.d1);

let fq_sponge_params = oracle::pasta::fq::params();
let (endo_q, endo_r) = endos::<Other>();
let (endo_q, _endo_r) = endos::<Other>();
let srs = Rc::new(srs);

let index = Index::<Affine>::create(cs, fq_sponge_params, endo_q, srs);
Expand Down
6 changes: 1 addition & 5 deletions dlog/plonk-15-wires/tests/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use array_init::array_init;
use commitment_dlog::{
commitment::{b_poly_coefficients, ceil_log2, CommitmentCurve},
srs::{endos, SRS},
PolyComm,
};
use groupmap::GroupMap;
use mina_curves::pasta::{
Expand All @@ -23,10 +22,7 @@ use plonk_15_wires_circuits::{
nolookup::constraints::ConstraintSystem,
wires::{Wire, COLUMNS, GENERICS},
};
use plonk_15_wires_protocol_dlog::{
index::{Index, VerifierIndex},
prover::ProverProof,
};
use plonk_15_wires_protocol_dlog::{index::Index, prover::ProverProof};
use rand::{rngs::StdRng, SeedableRng};

// aliases
Expand Down
11 changes: 6 additions & 5 deletions dlog/tests/poseidon_vesta_15_wires.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ fn positive(index: &Index<Affine>) {
let mut start = Instant::now();
for test in 0..1 {
// witness for Poseidon permutation custom constraints
let mut witness: [Vec<Fp>; COLUMNS] = array_init(|_| vec![Fp::zero(); max_size]);
let mut witness_cols: [Vec<Fp>; COLUMNS] =
array_init(|_| vec![Fp::zero(); 5 + POS_ROWS_PER_HASH * NUM_POS]);

// creates a random initial state
let init = vec![Fp::rand(rng), Fp::rand(rng), Fp::rand(rng)];
Expand All @@ -133,7 +134,7 @@ fn positive(index: &Index<Affine>) {
let first_row = h * (POS_ROWS_PER_HASH + 1);

// initialize the sponge in the circuit with our random state
let first_state_cols = &mut witness[round_to_cols(0)];
let first_state_cols = &mut witness_cols[round_to_cols(0)];
for state_idx in 0..SPONGE_WIDTH {
first_state_cols[state_idx][first_row] = init[state_idx];
}
Expand Down Expand Up @@ -162,7 +163,7 @@ fn positive(index: &Index<Affine>) {

// apply the sponge and record the result in the witness
let cols_to_update = round_to_cols((round + 1) % ROUNDS_PER_ROW);
witness[cols_to_update]
witness_cols[cols_to_update]
.iter_mut()
.zip(sponge.state.iter())
// update the state (last update is on the next row)
Expand All @@ -172,7 +173,7 @@ fn positive(index: &Index<Affine>) {
}

// verify the circuit satisfiability by the computed witness
index.cs.verify(&witness).unwrap();
index.cs.verify(&witness_cols).unwrap();

//
let prev = {
Expand All @@ -194,7 +195,7 @@ fn positive(index: &Index<Affine>) {
batch.push(
ProverProof::create::<BaseSponge, ScalarSponge>(
&group_map,
&witness,
&witness_cols,
&index,
vec![prev],
)
Expand Down
108 changes: 50 additions & 58 deletions dlog/tests/varbasemul.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
use ark_ec::{AffineCurve, ProjectiveCurve};
use ark_ff::{BigInteger, BitIteratorLE, Field, One, PrimeField, UniformRand, Zero};
use array_init::array_init;
use colored::Colorize;
use commitment_dlog::{
commitment::{b_poly_coefficients, ceil_log2, CommitmentCurve},
commitment::CommitmentCurve,
srs::{endos, SRS},
};
use ark_ec::{AffineCurve, ProjectiveCurve};
use ark_ff::{BigInteger, Field, PrimeField, BitIteratorLE, UniformRand, Zero, One};
use ark_poly::{univariate::DensePolynomial, Radix2EvaluationDomain as D, EvaluationDomain};
use plonk_15_wires_circuits::{
polynomials::varbasemul,
gate::{CircuitGate, GateType, LookupInfo, LookupsUsed},
expr::{PolishToken, Constants, Expr, Column, Linearization},
gates::poseidon::ROUNDS_PER_ROW,
nolookup::constraints::{zk_w3, ConstraintSystem},
nolookup::scalars::{ProofEvaluations, LookupEvaluations},
wires::*,
};
use groupmap::GroupMap;
use mina_curves::pasta::{
fp::{Fp as F},
pallas::{Affine as Other, Projective as OtherProjective},
fp::Fp as F,
pallas::Affine as Other,
vesta::{Affine, VestaParameters},
};
use plonk_15_wires_protocol_dlog::{
index::{Index},
prover::ProverProof,
};
use rand::{rngs::StdRng, SeedableRng};
use array_init::array_init;
use std::fmt::{Formatter, Display};
use groupmap::GroupMap;
use oracle::{
poseidon::{ArithmeticSponge, PlonkSpongeConstants15W, Sponge, SpongeConstants},
poseidon::PlonkSpongeConstants15W,
sponge::{DefaultFqSponge, DefaultFrSponge},
};
use plonk_15_wires_circuits::{
gate::{CircuitGate, GateType},
nolookup::constraints::ConstraintSystem,
polynomials::varbasemul,
wires::*,
};
use plonk_15_wires_protocol_dlog::{index::Index, prover::ProverProof};
use rand::{rngs::StdRng, SeedableRng};
use std::{rc::Rc, time::Instant};

const PUBLIC: usize = 0;
Expand All @@ -55,24 +47,21 @@ fn varbase_mul_test() {

for i in 0..(chunks * num_scalars) {
let row = 2 * i;
gates.push(
CircuitGate {
row,
typ: GateType::Vbmul,
wires: Wire::new(row),
c: vec![],
});
gates.push(
CircuitGate {
row: row + 1,
typ: GateType::Zero,
wires: Wire::new(row + 1),
c: vec![]
});
gates.push(CircuitGate {
row,
typ: GateType::Vbmul,
wires: Wire::new(row),
c: vec![],
});
gates.push(CircuitGate {
row: row + 1,
typ: GateType::Zero,
wires: Wire::new(row + 1),
c: vec![],
});
}

let cs = ConstraintSystem::<F>::create(
gates, vec![], fp_sponge_params, PUBLIC).unwrap();
let cs = ConstraintSystem::<F>::create(gates, vec![], fp_sponge_params, PUBLIC).unwrap();
let n = cs.domain.d1.size as usize;

let mut srs = SRS::create(cs.domain.d1.size as usize);
Expand All @@ -98,41 +87,44 @@ fn varbase_mul_test() {
for i in 0..num_scalars {
let x = F::rand(rng);
let bits_lsb: Vec<_> = BitIteratorLE::new(x.into_repr()).take(num_bits).collect();
let x_ = <Other as AffineCurve>::ScalarField::from_repr(<F as PrimeField>::BigInt::from_bits_le(&bits_lsb[..])).unwrap();
let x_ = <Other as AffineCurve>::ScalarField::from_repr(
<F as PrimeField>::BigInt::from_bits_le(&bits_lsb[..]),
)
.unwrap();

let base = Other::prime_subgroup_generator();
let g = Other::prime_subgroup_generator().into_projective();
let acc = (g + g).into_affine();
let acc = (acc.x, acc.y);

let bits_msb: Vec<_> =
bits_lsb.iter().take(num_bits).map(|x| *x).rev().collect();
let bits_msb: Vec<_> = bits_lsb.iter().take(num_bits).map(|x| *x).rev().collect();

let res =
varbasemul::witness(
&mut witness,
i * rows_per_scalar,
(base.x, base.y),
&bits_msb,
acc);
let res = varbasemul::witness(
&mut witness,
i * rows_per_scalar,
(base.x, base.y),
&bits_msb,
acc,
);

let shift = <Other as AffineCurve>::ScalarField::from(2).pow(&[(bits_msb.len()) as u64]);
let expected =
g.mul((<Other as AffineCurve>::ScalarField::one() + shift + x_.double()).into_repr())
let expected = g
.mul((<Other as AffineCurve>::ScalarField::one() + shift + x_.double()).into_repr())
.into_affine();

assert_eq!(x_.into_repr(), res.n.into_repr());
assert_eq!((expected.x, expected.y), res.acc);
}
println!("{}{:?}", "Witness generation time: ".yellow(), start.elapsed());
println!(
"{}{:?}",
"Witness generation time: ".yellow(),
start.elapsed()
);

let start = Instant::now();
let proof =
ProverProof::create::<BaseSponge, ScalarSponge>(
&group_map,
&witness,
&index,
vec![]).unwrap();
ProverProof::create::<BaseSponge, ScalarSponge>(&group_map, &witness, &index, vec![])
.unwrap();
println!("{}{:?}", "Prover time: ".yellow(), start.elapsed());

let batch: Vec<_> = vec![(&verifier_index, &lgr_comms, &proof)];
Expand Down

0 comments on commit 4f85ca0

Please sign in to comment.