Skip to content

Commit 59b2371

Browse files
committed
snarkpack integration
1 parent b8bff46 commit 59b2371

File tree

6 files changed

+200
-104
lines changed

6 files changed

+200
-104
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ ark-groth16 = { version = "^0.3.0", features = ["r1cs"] }
4242
ark-bw6-761 = { version = "^0.3.0" }
4343
ark-poly-commit = { version = "^0.3.0" }
4444
ark-poly = {version = "^0.3.0"}
45+
snarkpack = { path="../snarkpack"}
46+
4547

4648
lazy_static = "1.4.0"
4749
rand = { version = "0.8", features = [ "std", "std_rng" ] }
@@ -91,5 +93,6 @@ std = ["ark-ff/std", "ark-ec/std", "ark-std/std", "ark-relations/std", "ark-seri
9193

9294
[patch.crates-io]
9395
ark-r1cs-std = { git = "https://github.com/arkworks-rs/r1cs-std/", rev = "a2a5ac491ae005ba2afd03fd21b7d3160d794a83"}
94-
ark-poly-commit = {git = "https://github.com/maramihali/poly-commit"}
96+
ark-poly-commit = {git = "https://github.com/maramihali/poly-commit", branch="pst_g2"}
97+
9598

src/constraints.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,15 +393,13 @@ impl ConstraintSynthesizer<Fr> for R1CSVerificationCircuit {
393393

394394
let expected_claim_post_phase2_var = eval_Z_at_ry_var * scalar_var;
395395
claim_post_phase2_var.enforce_equal(&expected_claim_post_phase2_var)?;
396-
397396
let expected_transcript_state_var = transcript_var.challenge()?;
398397
let claimed_transcript_state_var =
399398
FpVar::<Fr>::new_input(cs, || Ok(self.claimed_transcript_sat_state))?;
400399

401400
// Ensure that the prover and verifier transcipt views are consistent at
402401
// the end of the satisfiability proof.
403402
expected_transcript_state_var.enforce_equal(&claimed_transcript_state_var)?;
404-
405403
Ok(())
406404
}
407405
}

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ impl SNARK {
407407
// side all the previous updates are done on the transcript
408408
// circuit variable and the transcript outside the circuit will be
409409
// inconsistent wrt to the prover's.
410-
transcript.new_from_state(&r1cs_sat_proof.transcript_sat_state);
410+
// transcript.new_from_state(&r1cs_sat_proof.transcript_sat_state);
411411

412412
// We send evaluations of A, B, C at r = (rx, ry) as claims
413413
// to enable the verifier complete the first sum-check
@@ -480,7 +480,7 @@ impl SNARK {
480480
// TODO: find a way to retrieve this state from the circuit. Currently
481481
// the API for generating constraints doesn't support returning values
482482
// computed inside the circuit.
483-
transcript.new_from_state(&self.r1cs_sat_proof.transcript_sat_state);
483+
// transcript.new_from_state(&self.r1cs_sat_proof.transcript_sat_state);
484484

485485
let (Ar, Br, Cr) = &self.inst_evals;
486486
transcript.append_scalar(&Ar);
@@ -598,10 +598,10 @@ impl NIZK {
598598

599599
// We send evaluations of A, B, C at r = (rx, ry) as claims
600600
// to enable the verifier complete the first sum-check
601-
let timer_eval = Timer::new("eval_sparse_polys");
601+
// let timer_eval = Timer::new("eval_sparse_polys");
602602
let (claimed_rx, claimed_ry) = &self.r;
603603
let inst_evals = inst.inst.evaluate(claimed_rx, claimed_ry);
604-
timer_eval.stop();
604+
// timer_eval.stop();
605605

606606
let timer_sat_proof = Timer::new("verify_sat_proof");
607607
assert_eq!(input.assignment.len(), inst.inst.get_num_inputs());

src/poseidon_transcript.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
use crate::group::{CompressedGroup, Fr};
22

33
use super::scalar::Scalar;
4-
use ark_bls12_377::Bls12_377 as I;
4+
use ark_bls12_377::{Bls12_377 as I, G1Affine};
5+
use ark_ec::PairingEngine;
6+
use ark_ff::{Field, PrimeField};
57
use ark_poly_commit::multilinear_pc::data_structures::Commitment;
68
use ark_serialize::CanonicalSerialize;
7-
// use ark_r1cs_std::prelude::*;
89
use ark_sponge::{
910
poseidon::{PoseidonParameters, PoseidonSponge},
1011
CryptographicSponge,
1112
};
13+
use snarkpack::Transcript;
1214

1315
#[derive(Clone)]
1416
/// TODO
@@ -17,6 +19,22 @@ pub struct PoseidonTranscript {
1719
params: PoseidonParameters<Fr>,
1820
}
1921

22+
impl Transcript for PoseidonTranscript {
23+
fn domain_sep(&mut self) {
24+
self.sponge.absorb(&b"testudo".to_vec());
25+
}
26+
27+
fn append<S: CanonicalSerialize>(&mut self, label: &'static [u8], point: &S) {
28+
let mut buf = Vec::new();
29+
point.serialize(&mut buf).expect("serialization failed");
30+
self.sponge.absorb(&buf);
31+
}
32+
33+
fn challenge_scalar<F: PrimeField>(&mut self, label: &'static [u8]) -> F {
34+
self.sponge.squeeze_field_elements(1).remove(0)
35+
}
36+
}
37+
2038
impl PoseidonTranscript {
2139
/// create a new transcript
2240
pub fn new(params: &PoseidonParameters<Fr>) -> Self {
@@ -56,6 +74,12 @@ impl PoseidonTranscript {
5674
}
5775
}
5876

77+
pub fn append_gt(&mut self, g_t: &<I as PairingEngine>::Fqk) {
78+
let mut bytes = Vec::new();
79+
g_t.serialize(&mut bytes).unwrap();
80+
self.append_bytes(&bytes);
81+
}
82+
5983
pub fn challenge_scalar(&mut self) -> Scalar {
6084
self.sponge.squeeze_field_elements(1).remove(0)
6185
}
@@ -82,3 +106,11 @@ impl AppendToPoseidon for Commitment<I> {
82106
transcript.append_bytes(&bytes);
83107
}
84108
}
109+
110+
impl AppendToPoseidon for G1Affine {
111+
fn append_to_poseidon(&self, transcript: &mut PoseidonTranscript) {
112+
let mut bytes = Vec::new();
113+
self.serialize(&mut bytes).unwrap();
114+
transcript.append_bytes(&bytes);
115+
}
116+
}

src/r1csproof.rs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ use crate::group::{Fq, Fr};
44
use crate::math::Math;
55
use crate::parameters::poseidon_params;
66
use crate::poseidon_transcript::{AppendToPoseidon, PoseidonTranscript};
7-
use crate::sqrt_pst::PolyList;
7+
use crate::sqrt_pst::Polynomial;
88
use crate::sumcheck::SumcheckInstanceProof;
99
use ark_bls12_377::Bls12_377 as I;
1010
use ark_bw6_761::BW6_761 as P;
1111
use ark_ec::PairingEngine;
1212
use ark_poly::MultilinearExtension;
1313
use ark_poly_commit::multilinear_pc::data_structures::{Commitment, Proof};
1414
use ark_poly_commit::multilinear_pc::MultilinearPC;
15+
use snarkpack::mipp::MippProof;
1516

1617
use super::commitments::MultiCommitGens;
1718
use super::dense_mlpoly::{DensePolynomial, EqPolynomial, PolyCommitmentGens};
@@ -45,6 +46,7 @@ pub struct R1CSProof {
4546
// The transcript state after the satisfiability proof was computed.
4647
pub transcript_sat_state: Scalar,
4748
pub t: <I as PairingEngine>::Fqk,
49+
pub mipp_proof: MippProof<I>,
4850
}
4951
#[derive(Clone)]
5052
pub struct R1CSSumcheckGens {
@@ -146,12 +148,12 @@ impl R1CSProof {
146148

147149
// create the multilinear witness polynomial from the satisfying assiment
148150
// expressed as the list of sqrt-sized polynomials
149-
let pl = PolyList::new(&vars.clone());
151+
let mut pl = Polynomial::from_evaluations(&vars.clone());
150152

151153
let timer_commit = Timer::new("polycommit");
152154

153155
// commitment list to the satisfying witness polynomial list
154-
let (comm_list, t) = PolyList::commit(&pl, &gens.gens_pc.ck);
156+
let (comm_list, t) = pl.commit(&gens.gens_pc.ck);
155157

156158
let mut bytes = Vec::new();
157159
t.serialize(&mut bytes).unwrap();
@@ -237,31 +239,28 @@ impl R1CSProof {
237239
transcript,
238240
);
239241
timer_sc_proof_phase2.stop();
242+
let c = transcript.challenge_scalar();
243+
transcript.new_from_state(&c);
240244

241245
// TODO: modify the polynomial evaluation in Spartan to be consistent
242246
// with the evaluation in ark-poly-commit so that reversing is not needed
243247
// anymore
244248
let timmer_opening = Timer::new("polyopening");
245-
let mut dummy = ry[1..].to_vec().clone();
246-
dummy.reverse();
247-
let q = pl.get_q(&dummy);
249+
timer_prove.stop();
248250

249-
let (comm, proof_eval_vars_at_ry) = PolyList::open_q(comm_list, &gens.gens_pc.ck, &q, &dummy);
251+
let (comm, proof_eval_vars_at_ry, mipp_proof) =
252+
pl.open(transcript, comm_list, &gens.gens_pc.ck, &ry[1..], &t);
250253
println!(
251254
"proof size (no of quotients): {:?}",
252255
proof_eval_vars_at_ry.proofs.len()
253256
);
254-
// comm.append_to_poseidon(transcript);
257+
255258
timmer_opening.stop();
256259

257260
let timer_polyeval = Timer::new("polyeval");
258-
let eval_vars_at_ry = PolyList::eval_q(q.clone(), &dummy);
261+
let eval_vars_at_ry = pl.eval(&ry[1..]);
259262
timer_polyeval.stop();
260263

261-
timer_prove.stop();
262-
263-
let c = transcript.challenge_scalar();
264-
265264
(
266265
R1CSProof {
267266
comm,
@@ -273,7 +272,8 @@ impl R1CSProof {
273272
rx: rx.clone(),
274273
ry: ry.clone(),
275274
transcript_sat_state: c,
276-
t: t,
275+
t,
276+
mipp_proof,
277277
},
278278
rx,
279279
ry,
@@ -333,6 +333,7 @@ impl R1CSProof {
333333
let dp1 = start.elapsed().as_millis();
334334
prove_inner.stop();
335335

336+
// this is universal, we don't measure it
336337
let start = Instant::now();
337338
let (pk, vk) = Groth16::<P>::setup(circuit.clone(), &mut rng).unwrap();
338339
let ds = start.elapsed().as_millis();
@@ -344,24 +345,25 @@ impl R1CSProof {
344345
prove_outer.stop();
345346

346347
let start = Instant::now();
348+
let verifier_time = Timer::new("groth16_verification");
347349
let is_verified = Groth16::<P>::verify(&vk, &[], &proof).unwrap();
348350
assert!(is_verified);
351+
verifier_time.stop();
349352

350353
let timer_verification = Timer::new("commitverification");
351-
let mut dummy = self.ry[1..].to_vec();
352-
// TODO: ensure ark-poly-commit and Spartan produce consistent results
353-
// when evaluating a polynomial at a given point so this reverse is not
354-
// needed.
355-
dummy.reverse();
354+
transcript.new_from_state(&self.transcript_sat_state);
356355

357356
// Verifies the proof of opening against the result of evaluating the
358357
// witness polynomial at point ry.
359-
let res = PolyList::verify_q(
358+
let res = Polynomial::verify(
359+
transcript,
360360
&gens.gens_pc.vk,
361361
&self.comm,
362-
&dummy,
362+
&self.ry[1..],
363363
self.eval_vars_at_ry,
364364
&self.proof_eval_vars_at_ry,
365+
&self.mipp_proof,
366+
&self.t,
365367
);
366368

367369
timer_verification.stop();
@@ -382,7 +384,10 @@ impl R1CSProof {
382384
transcript: &mut PoseidonTranscript,
383385
gens: &R1CSGens,
384386
) -> Result<usize, ProofVerifyError> {
385-
// self.comm.append_to_poseidon(transcript);
387+
// serialise and add the IPP commitment to the transcript
388+
let mut bytes = Vec::new();
389+
self.t.serialize(&mut bytes).unwrap();
390+
transcript.append_bytes(&bytes);
386391

387392
let c = transcript.challenge_scalar();
388393

0 commit comments

Comments
 (0)