Skip to content

Commit

Permalink
fmt and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
TakodaS committed Sep 19, 2023
1 parent d1b8e51 commit b5071fc
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 108 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Cargo.lock
*.pyc
*.sage.py
params
rngNEW/
92 changes: 56 additions & 36 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// where N is the number of threads you want to use (N = 1 for single-thread).

use ark_bls12_381::{Bls12_381, Fr as BlsFr};
use ark_ec::pairing::Pairing;
use ark_ff::PrimeField;
use ark_marlin::{Marlin, SimplePoseidonRng};
use ark_mnt4_298::{Fr as MNT4Fr, MNT4_298};
Expand All @@ -16,9 +17,8 @@ use ark_relations::{
r1cs::{ConstraintSynthesizer, ConstraintSystemRef, SynthesisError},
};
use ark_std::{ops::Mul, UniformRand};
use blake2::Blake2s;
use rand_chacha::ChaChaRng;
use ark_ec::pairing::Pairing;



const NUM_PROVE_REPEATITIONS: usize = 10;
const NUM_VERIFY_REPEATITIONS: usize = 50;
Expand Down Expand Up @@ -77,31 +77,39 @@ macro_rules! marlin_prove_bench {
num_constraints: 65536,
};


let srs = Marlin::<
$bench_field,
SonicKZG10<$bench_pairing_engine, DensePolynomial<$bench_field>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>>
::universal_setup(65536, 65536, 3 * 65536, rng)
.unwrap();
SonicKZG10<
$bench_pairing_engine,
DensePolynomial<$bench_field>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>,
>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>,
>::universal_setup(65536, 65536, 3 * 65536, rng)
.unwrap();
let (pk, _) = Marlin::<
$bench_field,
SonicKZG10<$bench_pairing_engine, DensePolynomial<$bench_field>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>>
::index(&srs, c)
.unwrap();
SonicKZG10<
$bench_pairing_engine,
DensePolynomial<$bench_field>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>,
>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>,
>::index(&srs, c)
.unwrap();

let start = ark_std::time::Instant::now();

for _ in 0..NUM_PROVE_REPEATITIONS {
let _ = Marlin::<
$bench_field,
SonicKZG10<$bench_pairing_engine, DensePolynomial<$bench_field>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>>
::prove(&pk, c.clone(), rng)
$bench_field,
SonicKZG10<
$bench_pairing_engine,
DensePolynomial<$bench_field>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>,
>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>,
>::prove(&pk, c.clone(), rng)
.unwrap();
}

Expand All @@ -125,24 +133,33 @@ macro_rules! marlin_verify_bench {

let srs = Marlin::<
$bench_field,
SonicKZG10<$bench_pairing_engine, DensePolynomial<$bench_field>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>>
::universal_setup(65536, 65536, 3 * 65536, rng)
SonicKZG10<
$bench_pairing_engine,
DensePolynomial<$bench_field>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>,
>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>,
>::universal_setup(65536, 65536, 3 * 65536, rng)
.unwrap();
let (pk, vk) = Marlin::<
$bench_field,
SonicKZG10<$bench_pairing_engine, DensePolynomial<$bench_field>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>>
::index(&srs, c)
SonicKZG10<
$bench_pairing_engine,
DensePolynomial<$bench_field>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>,
>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>,
>::index(&srs, c)
.unwrap();
let proof = Marlin::<
$bench_field,
SonicKZG10<$bench_pairing_engine, DensePolynomial<$bench_field>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>>
::prove(&pk, c.clone(), rng)
SonicKZG10<
$bench_pairing_engine,
DensePolynomial<$bench_field>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>,
>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>,
>::prove(&pk, c.clone(), rng)
.unwrap();

let v = c.a.unwrap().mul(c.b.unwrap());
Expand All @@ -151,11 +168,14 @@ macro_rules! marlin_verify_bench {

for _ in 0..NUM_VERIFY_REPEATITIONS {
let _ = Marlin::<
$bench_field,
SonicKZG10<$bench_pairing_engine, DensePolynomial<$bench_field>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>>
::verify(&vk, &vec![v], &proof, rng)
$bench_field,
SonicKZG10<
$bench_pairing_engine,
DensePolynomial<$bench_field>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>,
>,
SimplePoseidonRng<<$bench_pairing_engine as Pairing>::BaseField>,
>::verify(&vk, &vec![v], &proof, rng)
.unwrap();
}

Expand Down
6 changes: 3 additions & 3 deletions src/ahp/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::ahp::{
AHPForR1CS, Error, LabeledPolynomial,
};
use crate::Vec;
use ark_ff::{PrimeField, Field};
use ark_ff::{Field, PrimeField};
use ark_poly::{EvaluationDomain, GeneralEvaluationDomain};
use ark_relations::r1cs::{
ConstraintSynthesizer, ConstraintSystem, OptimizationGoal, SynthesisError, SynthesisMode,
Expand All @@ -25,7 +25,7 @@ use crate::ahp::constraint_systems::{
/// entries in any of the constraint matrices.
#[derive(Derivative, CanonicalSerialize, CanonicalDeserialize)]
#[derivative(Clone(bound = ""), Copy(bound = ""))]
pub struct IndexInfo<F:Field> {
pub struct IndexInfo<F: Field> {
/// The total number of variables in the constraint system.
pub num_variables: usize,
/// The number of constraints.
Expand All @@ -39,7 +39,7 @@ pub struct IndexInfo<F:Field> {
f: PhantomData<F>,
}

impl<F:Field> IndexInfo<F> {
impl<F: Field> IndexInfo<F> {
/// Construct a new index info
pub fn new(
num_variables: usize,
Expand Down
14 changes: 3 additions & 11 deletions src/ahp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ mod tests {
use ark_ff::{One, UniformRand, Zero};
use ark_poly::{
univariate::{DenseOrSparsePolynomial, DensePolynomial},
Polynomial, DenseUVPolynomial,
DenseUVPolynomial, Polynomial,
};

#[test]
Expand Down Expand Up @@ -414,11 +414,7 @@ mod tests {
divisor
.coeffs
.iter()
.filter_map(|f| if !f.is_zero() {
Some(f)
} else {
None
})
.filter_map(|f| if !f.is_zero() { Some(f) } else { None })
.collect::<Vec<_>>()
);

Expand Down Expand Up @@ -446,11 +442,7 @@ mod tests {
quotient
.coeffs
.iter()
.filter_map(|f| if !f.is_zero() {
Some(f)
} else {
None
})
.filter_map(|f| if !f.is_zero() { Some(f) } else { None })
.collect::<Vec<_>>()
);

Expand Down
22 changes: 15 additions & 7 deletions src/ahp/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::ahp::constraint_systems::{
use crate::{ToString, Vec};
use ark_ff::{Field, PrimeField, Zero};
use ark_poly::{
univariate::DensePolynomial, EvaluationDomain, Evaluations as EvaluationsOnDomain,
GeneralEvaluationDomain, Polynomial, DenseUVPolynomial,
univariate::DensePolynomial, DenseUVPolynomial, EvaluationDomain,
Evaluations as EvaluationsOnDomain, GeneralEvaluationDomain, Polynomial,
};
use ark_relations::r1cs::{
ConstraintSynthesizer, ConstraintSystem, OptimizationGoal, SynthesisError,
Expand Down Expand Up @@ -75,7 +75,11 @@ pub enum ProverMsg<F: Field> {
}

impl<F: Field> CanonicalSerialize for ProverMsg<F> {
fn serialize_with_mode<W: Write>(&self, writer: W, compress: Compress) -> Result<(), SerializationError> {
fn serialize_with_mode<W: Write>(
&self,
writer: W,
compress: Compress,
) -> Result<(), SerializationError> {
let res = match self {
ProverMsg::EmptyMessage => None,
ProverMsg::FieldElements(v) => Some(v.clone()),
Expand All @@ -90,10 +94,10 @@ impl<F: Field> CanonicalSerialize for ProverMsg<F> {
ProverMsg::FieldElements(v) => Some(v.clone()),
};
res.serialized_size(compress)
}
}
}

impl<F:Field> Valid for ProverMsg<F>{
impl<F: Field> Valid for ProverMsg<F> {
fn check(&self) -> Result<(), SerializationError> {
match self {
ProverMsg::EmptyMessage => Ok(()),
Expand All @@ -102,14 +106,18 @@ impl<F:Field> Valid for ProverMsg<F>{
}
}
impl<F: Field> CanonicalDeserialize for ProverMsg<F> {
fn deserialize_with_mode<R: Read>(reader: R, compress:Compress, validate: Validate) -> Result<Self, SerializationError> {
fn deserialize_with_mode<R: Read>(
reader: R,
compress: Compress,
validate: Validate,
) -> Result<Self, SerializationError> {
let res = Option::<Vec<F>>::deserialize_with_mode(reader, compress, validate)?;
if let Some(res) = res {
Ok(ProverMsg::FieldElements(res))
} else {
Ok(ProverMsg::EmptyMessage)
}
}
}
}

/// The first set of prover oracles.
Expand Down
40 changes: 29 additions & 11 deletions src/data_structures.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
use crate::ahp::indexer::*;
use crate::ahp::prover::ProverMsg;
use crate::Vec;
use ark_crypto_primitives::sponge::CryptographicSponge;
use ark_ff::PrimeField;
use ark_poly::univariate::DensePolynomial;
use ark_poly_commit::{BatchLCProof, PolynomialCommitment};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_crypto_primitives::sponge::CryptographicSponge;
use ark_std::format;

/* ************************************************************************* */
/* ************************************************************************* */
/* ************************************************************************* */

/// The universal public parameters for the argument system.
pub type UniversalSRS<F, PC, S> = <PC as PolynomialCommitment<F, DensePolynomial<F>,S>>::UniversalParams;
pub type UniversalSRS<F, PC, S> =
<PC as PolynomialCommitment<F, DensePolynomial<F>, S>>::UniversalParams;

/* ************************************************************************* */
/* ************************************************************************* */
/* ************************************************************************* */

/// Verification key for a specific index (i.e., R1CS matrices).
#[derive(CanonicalSerialize, CanonicalDeserialize)]
pub struct IndexVerifierKey<F: PrimeField, S:CryptographicSponge, PC: PolynomialCommitment<F, DensePolynomial<F>, S>> {
pub struct IndexVerifierKey<
F: PrimeField,
S: CryptographicSponge,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
> {
/// Stores information about the size of the index, as well as its field of
/// definition.
pub index_info: IndexInfo<F>,
Expand All @@ -31,8 +36,8 @@ pub struct IndexVerifierKey<F: PrimeField, S:CryptographicSponge, PC: Polynomial
pub verifier_key: PC::VerifierKey,
}

impl<F: PrimeField, PC: PolynomialCommitment<F, DensePolynomial<F>, S>, S:CryptographicSponge> Clone
for IndexVerifierKey<F, S, PC>
impl<F: PrimeField, PC: PolynomialCommitment<F, DensePolynomial<F>, S>, S: CryptographicSponge>
Clone for IndexVerifierKey<F, S, PC>
{
fn clone(&self) -> Self {
Self {
Expand All @@ -43,7 +48,9 @@ impl<F: PrimeField, PC: PolynomialCommitment<F, DensePolynomial<F>, S>, S:Crypto
}
}

impl<F: PrimeField, PC: PolynomialCommitment<F, DensePolynomial<F>,S>, S:CryptographicSponge> IndexVerifierKey<F,S, PC> {
impl<F: PrimeField, PC: PolynomialCommitment<F, DensePolynomial<F>, S>, S: CryptographicSponge>
IndexVerifierKey<F, S, PC>
{
/// Iterate over the commitments to indexed polynomials in `self`.
pub fn iter(&self) -> impl Iterator<Item = &PC::Commitment> {
self.index_comms.iter()
Expand All @@ -56,9 +63,13 @@ impl<F: PrimeField, PC: PolynomialCommitment<F, DensePolynomial<F>,S>, S:Cryptog

/// Proving key for a specific index (i.e., R1CS matrices).
#[derive(CanonicalSerialize, CanonicalDeserialize)]
pub struct IndexProverKey<F: PrimeField, PC: PolynomialCommitment<F, DensePolynomial<F>,S>,S:CryptographicSponge> {
pub struct IndexProverKey<
F: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
S: CryptographicSponge,
> {
/// The index verifier key.
pub index_vk: IndexVerifierKey<F,S,PC>,
pub index_vk: IndexVerifierKey<F, S, PC>,
/// The randomness for the index polynomial commitments.
pub index_comm_rands: Vec<PC::Randomness>,
/// The index itself.
Expand All @@ -67,7 +78,8 @@ pub struct IndexProverKey<F: PrimeField, PC: PolynomialCommitment<F, DensePolyno
pub committer_key: PC::CommitterKey,
}

impl<F: PrimeField, PC: PolynomialCommitment<F, DensePolynomial<F>,S>,S:CryptographicSponge> Clone for IndexProverKey<F,PC,S>
impl<F: PrimeField, PC: PolynomialCommitment<F, DensePolynomial<F>, S>, S: CryptographicSponge>
Clone for IndexProverKey<F, PC, S>
where
PC::Commitment: Clone,
{
Expand All @@ -87,7 +99,11 @@ where

/// A zkSNARK proof.
#[derive(CanonicalSerialize, CanonicalDeserialize)]
pub struct Proof<F: PrimeField, PC: PolynomialCommitment<F, DensePolynomial<F>,S>,S:CryptographicSponge> {
pub struct Proof<
F: PrimeField,
PC: PolynomialCommitment<F, DensePolynomial<F>, S>,
S: CryptographicSponge,
> {
/// Commitments to the polynomials produced by the AHP prover.
pub commitments: Vec<Vec<PC::Commitment>>,
/// Evaluations of these polynomials.
Expand All @@ -98,7 +114,9 @@ pub struct Proof<F: PrimeField, PC: PolynomialCommitment<F, DensePolynomial<F>,S
pub pc_proof: BatchLCProof<F, PC::BatchProof>,
}

impl<F: PrimeField, PC: PolynomialCommitment<F, DensePolynomial<F>,S>,S:CryptographicSponge> Proof<F, PC,S> {
impl<F: PrimeField, PC: PolynomialCommitment<F, DensePolynomial<F>, S>, S: CryptographicSponge>
Proof<F, PC, S>
{
/// Construct a new proof.
pub fn new(
commitments: Vec<Vec<PC::Commitment>>,
Expand Down
Loading

0 comments on commit b5071fc

Please sign in to comment.