Skip to content

Commit

Permalink
Adds Absorb and AbsorbGadget impl for Groth Verification Key & Var
Browse files Browse the repository at this point in the history
  • Loading branch information
MercysJest committed Jan 9, 2024
1 parent 42b38f1 commit 79596f2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ark-serialize = { version = "0.4.0", default-features = false, features = [ "der
ark-poly = { version = "0.4.0", default-features = false }
ark-std = { version = "0.4.0", default-features = false }
ark-relations = { version = "0.4.0", default-features = false }
ark-crypto-primitives = { version = "0.4.0", default-features = false, features = ["snark"] }
ark-crypto-primitives = { version = "0.4.0", default-features = false, features = ["snark", "sponge"] }
ark-r1cs-std = { version = "0.4.0", default-features = false, optional = true }

tracing = { version = "0.1", default-features = false, features = [ "attributes" ], optional = true }
Expand Down
57 changes: 48 additions & 9 deletions src/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ use crate::{
r1cs_to_qap::{LibsnarkReduction, R1CSToQAP},
Groth16, PreparedVerifyingKey, Proof, VerifyingKey,
};
use ark_crypto_primitives::snark::constraints::{CircuitSpecificSetupSNARKGadget, SNARKGadget};
use ark_crypto_primitives::snark::{BooleanInputVar, SNARK};
use ark_crypto_primitives::{
snark::{
constraints::{CircuitSpecificSetupSNARKGadget, SNARKGadget},
BooleanInputVar, SNARK,
},
sponge::constraints::AbsorbGadget,
};
use ark_ec::{pairing::Pairing, AffineRepr};
use ark_ff::Field;
use ark_r1cs_std::groups::CurveVar;
use ark_r1cs_std::{
alloc::{AllocVar, AllocationMode},
boolean::Boolean,
convert::{ToBitsGadget, ToBytesGadget},
eq::EqGadget,
fields::fp::FpVar,
groups::CurveVar,
pairing::PairingVar,
uint8::UInt8,
};
Expand Down Expand Up @@ -69,7 +75,42 @@ impl<E: Pairing, P: PairingVar<E>> VerifyingKeyVar<E, P> {
}
}

/// Preprocessed verification key parameters variable for the Groth16 construction
impl<E, P> AbsorbGadget<E::BaseField> for VerifyingKeyVar<E, P>
where
E: Pairing,
P: PairingVar<E>,
P::G1Var: AbsorbGadget<E::BaseField>,
P::G2Var: AbsorbGadget<E::BaseField>,
{
fn to_sponge_bytes(&self) -> Result<Vec<UInt8<<E as Pairing>::BaseField>>, SynthesisError> {
let mut bytes = self.alpha_g1.to_sponge_bytes()?;
bytes.extend(self.beta_g2.to_sponge_bytes()?);
bytes.extend(self.gamma_g2.to_sponge_bytes()?);
bytes.extend(self.delta_g2.to_sponge_bytes()?);
self.gamma_abc_g1.iter().try_for_each(|g| {
bytes.extend(g.to_sponge_bytes()?);
Ok(())
})?;
Ok(bytes)
}

fn to_sponge_field_elements(
&self,
) -> Result<Vec<FpVar<<E as Pairing>::BaseField>>, SynthesisError> {
let mut field_elements = self.alpha_g1.to_sponge_field_elements()?;
field_elements.extend(self.beta_g2.to_sponge_field_elements()?);
field_elements.extend(self.gamma_g2.to_sponge_field_elements()?);
field_elements.extend(self.delta_g2.to_sponge_field_elements()?);
self.gamma_abc_g1.iter().try_for_each(|g| {
field_elements.extend(g.to_sponge_field_elements()?);
Ok(())
})?;
Ok(field_elements)
}
}

/// Preprocessed verification key parameters variable for the Groth16
/// construction
#[derive(Derivative)]
#[derivative(
Clone(bound = "P::G1Var: Clone, P::GTVar: Clone, P::G1PreparedVar: Clone, \
Expand Down Expand Up @@ -411,22 +452,20 @@ where
#[cfg(test)]
mod test {
use crate::{constraints::Groth16VerifierGadget, Groth16};
use ark_crypto_primitives::snark::constraints::SNARKGadget;
use ark_crypto_primitives::snark::SNARK;
use ark_crypto_primitives::snark::{constraints::SNARKGadget, SNARK};
use ark_ec::pairing::Pairing;
use ark_ff::{Field, UniformRand};
use ark_mnt4_298::{constraints::PairingVar as MNT4PairingVar, Fr as MNT4Fr, MNT4_298 as MNT4};
use ark_mnt6_298::Fr as MNT6Fr;
use ark_r1cs_std::boolean::Boolean;
use ark_r1cs_std::{alloc::AllocVar, eq::EqGadget};
use ark_r1cs_std::{alloc::AllocVar, boolean::Boolean, eq::EqGadget};
use ark_relations::{
lc, ns,
r1cs::{ConstraintSynthesizer, ConstraintSystem, ConstraintSystemRef, SynthesisError},
};
use ark_std::test_rng;
use ark_std::{
ops::MulAssign,
rand::{RngCore, SeedableRng},
test_rng,
};

#[derive(Copy, Clone)]
Expand Down
34 changes: 31 additions & 3 deletions src/data_structures.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use ark_crypto_primitives::sponge::Absorb;
use ark_ec::pairing::Pairing;
use ark_ff::PrimeField;
use ark_serialize::*;
use ark_std::vec::Vec;

Expand All @@ -23,7 +25,6 @@ impl<E: Pairing> Default for Proof<E> {
}
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

/// A verification key in the Groth16 SNARK.
Expand All @@ -37,7 +38,8 @@ pub struct VerifyingKey<E: Pairing> {
pub gamma_g2: E::G2Affine,
/// The `delta * H`, where `H` is the generator of `E::G2`.
pub delta_g2: E::G2Affine,
/// The `gamma^{-1} * (beta * a_i + alpha * b_i + c_i) * H`, where `H` is the generator of `E::G1`.
/// The `gamma^{-1} * (beta * a_i + alpha * b_i + c_i) * H`, where `H` is
/// the generator of `E::G1`.
pub gamma_abc_g1: Vec<E::G1Affine>,
}

Expand All @@ -53,6 +55,33 @@ impl<E: Pairing> Default for VerifyingKey<E> {
}
}

impl<E> Absorb for VerifyingKey<E>
where
E: Pairing,
E::G1Affine: Absorb,
E::G2Affine: Absorb,
{
fn to_sponge_bytes(&self, dest: &mut Vec<u8>) {
self.alpha_g1.to_sponge_bytes(dest);
self.beta_g2.to_sponge_bytes(dest);
self.gamma_g2.to_sponge_bytes(dest);
self.delta_g2.to_sponge_bytes(dest);
self.gamma_abc_g1
.iter()
.for_each(|g| g.to_sponge_bytes(dest));
}

fn to_sponge_field_elements<F: PrimeField>(&self, dest: &mut Vec<F>) {
self.alpha_g1.to_sponge_field_elements(dest);
self.beta_g2.to_sponge_field_elements(dest);
self.gamma_g2.to_sponge_field_elements(dest);
self.delta_g2.to_sponge_field_elements(dest);
self.gamma_abc_g1
.iter()
.for_each(|g| g.to_sponge_field_elements(dest));
}
}

/// Preprocessed verification key parameters that enable faster verification
/// at the expense of larger size in memory.
#[derive(Clone, Debug, PartialEq, CanonicalSerialize, CanonicalDeserialize)]
Expand Down Expand Up @@ -90,7 +119,6 @@ impl<E: Pairing> Default for PreparedVerifyingKey<E> {
}
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

/// The prover key for for the Groth16 zkSNARK.
Expand Down

0 comments on commit 79596f2

Please sign in to comment.