Skip to content

Commit

Permalink
make generator public for consistency circuit use (#8)
Browse files Browse the repository at this point in the history
* public key

* pk pub

* make clippy happy

* public commit

* pub gen vec

* pub eqpoly

* hyrax fields pub

* eq proof pub

* public

* pub compressedSNARK

* make s_i scalars

* why not working

* batch inv

* compilation, tests suspiciously all pass

* alright, convinced

* comments, clippy

* comments, clippy

* clean up

* clean up
  • Loading branch information
jkwoods authored Dec 7, 2023
1 parent 1a15695 commit 68c962f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 24 deletions.
34 changes: 22 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,18 +571,28 @@ where
S1: RelaxedR1CSSNARKTrait<G1>,
S2: RelaxedR1CSSNARKTrait<G2>,
{
r_U_primary: RelaxedR1CSInstance<G1>,
l_u_primary: R1CSInstance<G1>,
nifs_primary: NIFS<G1>,
f_W_snark_primary: S1,

r_U_secondary: RelaxedR1CSInstance<G2>,
l_u_secondary: R1CSInstance<G2>,
nifs_secondary: NIFS<G2>,
f_W_snark_secondary: S2,

zn_primary: Vec<G1::Scalar>,
zn_secondary: Vec<G2::Scalar>,
/// r_U_primary
pub r_U_primary: RelaxedR1CSInstance<G1>,
/// l_u_primary
pub l_u_primary: R1CSInstance<G1>,
/// nifs_primary
pub nifs_primary: NIFS<G1>,
/// f_W_snark_primary
pub f_W_snark_primary: S1,

/// r_U_secondary
pub r_U_secondary: RelaxedR1CSInstance<G2>,
/// l_u_secondary
pub l_u_secondary: R1CSInstance<G2>,
/// nifs_secondary
pub nifs_secondary: NIFS<G2>,
/// f_W_snark_secondary
pub f_W_snark_secondary: S2,

/// zn primary
pub zn_primary: Vec<G1::Scalar>,
/// zn secondary
pub zn_secondary: Vec<G2::Scalar>,

_p_c1: PhantomData<C1>,
_p_c2: PhantomData<C2>,
Expand Down
6 changes: 4 additions & 2 deletions src/provider/hyrax_pc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ pub struct PolyCommit<G: Group> {

/// Hyrax PC generators and functions to commit and prove evaluation
pub struct HyraxPC<G: Group> {
gens_v: CommitmentGens<G>, // generator for vectors
gens_s: CommitmentGens<G>, // generator for scalars (eval)
/// generator for vectors
pub gens_v: CommitmentGens<G>, // generator for vectors
/// generator for scalars (eval)
pub gens_s: CommitmentGens<G>, // generator for scalars (eval)
}

impl<G: Group> AppendToTranscriptTrait for PolyCommit<G> {
Expand Down
9 changes: 6 additions & 3 deletions src/provider/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ use serde::{Deserialize, Serialize};
/// A type that holds commitment generators
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CommitmentGens<G: Group> {
gens: Vec<G::PreprocessedGroupElement>,
h: G::PreprocessedGroupElement,
/// generator vec
pub gens: Vec<G::PreprocessedGroupElement>,
/// temporary public for debug
pub h: G::PreprocessedGroupElement,
_p: PhantomData<G>,
}

/// A type that holds a commitment
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct Commitment<G: Group> {
pub(crate) comm: G,
/// commitment elt
pub comm: G,
}

/// A type that holds a compressed commitment
Expand Down
3 changes: 2 additions & 1 deletion src/spartan/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ where
{
gens: R1CSGens<G>,
S: R1CSShape<G>,
pk: ProverKey<G, EE>,
/// pk
pub pk: ProverKey<G, EE>,
}

/// A type that holds Spartan's verifier's key
Expand Down
5 changes: 3 additions & 2 deletions src/spartan/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This module implements RelaxedR1CSSNARKTrait and CAPRelaxedR1CSSNARKTrait using Spartan that is generic
//! over the polynomial commitment and evaluation argument (i.e., a PCS)
pub mod direct;
mod nizk;
pub mod nizk;
pub mod polynomial;
mod sumcheck;

Expand Down Expand Up @@ -61,7 +61,8 @@ impl<G: Group> SumcheckGens<G> {
#[derive(Serialize, Deserialize)]
#[serde(bound = "")]
pub struct ProverKey<G: Group, EE: EvaluationEngineTrait<G, CE = G::CE>> {
gens: EE::EvaluationGens,
/// gens
pub gens: EE::EvaluationGens,
sumcheck_gens: SumcheckGens<G>,
S: R1CSShape<G>,
}
Expand Down
30 changes: 27 additions & 3 deletions src/spartan/nizk.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! This module defines nizk proofs
#![allow(clippy::too_many_arguments)]
#![allow(clippy::type_complexity)]
use crate::errors::NovaError;
Expand All @@ -13,6 +14,7 @@ use merlin::Transcript;
use rand::rngs::OsRng;
use serde::{Deserialize, Serialize};

/// KnowledgeProof
#[derive(Debug, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct KnowledgeProof<G: Group> {
Expand All @@ -21,13 +23,17 @@ pub struct KnowledgeProof<G: Group> {
z2: G::Scalar,
}

/// EqualityProof
#[derive(Debug, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct EqualityProof<G: Group> {
alpha: CompressedCommitment<G>,
z: G::Scalar,
/// alpha
pub alpha: CompressedCommitment<G>,
/// z
pub z: G::Scalar,
}

/// ProductProof
#[derive(Debug, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct ProductProof<G: Group> {
Expand All @@ -37,6 +43,7 @@ pub struct ProductProof<G: Group> {
z: [G::Scalar; 5],
}

/// DocProductProof
#[derive(Debug, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct DotProductProof<G: Group> {
Expand All @@ -47,11 +54,13 @@ pub struct DotProductProof<G: Group> {
z_beta: G::Scalar,
}

/// KnowledgeProof
impl<G: Group> KnowledgeProof<G> {
fn protocol_name() -> &'static [u8] {
b"knowledge proof"
}

/// prove
pub fn prove(
gens_n: &CommitmentGens<G>,
transcript: &mut Transcript,
Expand All @@ -78,6 +87,7 @@ impl<G: Group> KnowledgeProof<G> {
Ok((Self { alpha, z1, z2 }, C))
}

/// verify
pub fn verify(
&self,
gens_n: &CommitmentGens<G>,
Expand All @@ -101,11 +111,14 @@ impl<G: Group> KnowledgeProof<G> {
}
}

/// EqualityProof
impl<G: Group> EqualityProof<G> {
fn protocol_name() -> &'static [u8] {
/// protocol name
pub fn protocol_name() -> &'static [u8] {
b"equality proof"
}

/// prove
pub fn prove(
gens_n: &CommitmentGens<G>,
transcript: &mut Transcript,
Expand Down Expand Up @@ -142,6 +155,7 @@ impl<G: Group> EqualityProof<G> {
Ok((Self { alpha, z }, C1, C2))
}

/// verify
pub fn verify(
&self,
gens_n: &CommitmentGens<G>,
Expand Down Expand Up @@ -171,11 +185,14 @@ impl<G: Group> EqualityProof<G> {
}
}

/// product proof
impl<G: Group> ProductProof<G> {
/// protocol name
fn protocol_name() -> &'static [u8] {
b"product proof"
}

/// prove
pub fn prove(
gens_n: &CommitmentGens<G>,
transcript: &mut Transcript,
Expand Down Expand Up @@ -247,6 +264,7 @@ impl<G: Group> ProductProof<G> {
))
}

/// check_equality
fn check_equality(
P: &CompressedCommitment<G>,
X: &CompressedCommitment<G>,
Expand All @@ -261,6 +279,7 @@ impl<G: Group> ProductProof<G> {
Ok(lhs == rhs)
}

/// verify
pub fn verify(
&self,
gens_n: &CommitmentGens<G>,
Expand Down Expand Up @@ -305,11 +324,14 @@ impl<G: Group> ProductProof<G> {
}
}

/// DotProductProof
impl<G: Group> DotProductProof<G> {
/// protocol name
pub fn protocol_name() -> &'static [u8] {
b"dot product proof"
}

/// comppute dot product
pub fn compute_dotproduct(a: &[G::Scalar], b: &[G::Scalar]) -> G::Scalar {
assert_eq!(a.len(), b.len());
let mut result = G::Scalar::zero();
Expand All @@ -321,6 +343,7 @@ impl<G: Group> DotProductProof<G> {
result
}

/// prove
pub fn prove(
gens_1: &CommitmentGens<G>, // generator of size 1
gens_n: &CommitmentGens<G>, // generators of size n
Expand Down Expand Up @@ -384,6 +407,7 @@ impl<G: Group> DotProductProof<G> {
)
}

/// verify
pub fn verify(
&self,
gens_1: &CommitmentGens<G>, // generator of size 1
Expand Down
6 changes: 5 additions & 1 deletion src/spartan/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use core::ops::Index;
use ff::PrimeField;
use rayon::prelude::*;

pub(crate) struct EqPolynomial<Scalar: PrimeField> {
/// Polynomial struct
pub struct EqPolynomial<Scalar: PrimeField> {
r: Vec<Scalar>,
}

Expand All @@ -21,6 +22,7 @@ impl<Scalar: PrimeField> EqPolynomial<Scalar> {
.fold(Scalar::one(), |acc, item| acc * item)
}

/// evals
pub fn evals(&self) -> Vec<Scalar> {
let ell = self.r.len();
let mut evals: Vec<Scalar> = vec![Scalar::zero(); (2_usize).pow(ell as u32)];
Expand All @@ -44,10 +46,12 @@ impl<Scalar: PrimeField> EqPolynomial<Scalar> {
evals
}

/// factored lens
pub fn compute_factored_lens(ell: usize) -> (usize, usize) {
(ell / 2, ell - ell / 2)
}

/// factored evals
pub fn compute_factored_evals(&self) -> (Vec<Scalar>, Vec<Scalar>) {
let ell = self.r.len();
let (left_num_vars, _right_num_vars) = EqPolynomial::<Scalar>::compute_factored_lens(ell);
Expand Down

0 comments on commit 68c962f

Please sign in to comment.