Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dedupe (Working) #37

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
22 changes: 20 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ impl<G: Group, S: RelaxedR1CSSNARKTrait<G> + UniformSNARKTrait<G> + Precommitted
}

/// Produces prover and verifier keys for the direct SNARK
pub fn setup_precommitted(circuit: C, n: usize) -> Result<(ProverKey<G, S>, VerifierKey<G, S>), SpartanError> {
let (pk, vk) = S::setup_precommitted(circuit, n)?;
pub fn setup_precommitted(circuit: C, n: usize, ck: CommitmentKey::<G>) -> Result<(ProverKey<G, S>, VerifierKey<G, S>), SpartanError> {
let (pk, vk) = S::setup_precommitted(circuit, n, ck)?;
Ok((ProverKey { pk }, VerifierKey { vk }))
}

Expand All @@ -105,11 +105,29 @@ impl<G: Group, S: RelaxedR1CSSNARKTrait<G> + UniformSNARKTrait<G> + Precommitted
})
}

/// Produces a proof of satisfiability of the provided circuit
pub fn prove_precommitted(pk: &ProverKey<G, S>, circuit: C, w_segments: Vec<Vec<G::Scalar>>, comm_w_vec: Vec<Commitment<G>> ) -> Result<Self, SpartanError> {
// prove the instance using Spartan
let snark = S::prove_precommitted(&pk.pk, circuit, w_segments, comm_w_vec)?;

Ok(SNARK {
snark,
_p: Default::default(),
_p2: Default::default(),
})
}

/// Verifies a proof of satisfiability
pub fn verify(&self, vk: &VerifierKey<G, S>, io: &[G::Scalar]) -> Result<(), SpartanError> {
// verify the snark using the constructed instance
self.snark.verify(&vk.vk, io)
}

/// Verifies a proof of satisfiability
pub fn verify_precommitted(&self, vk: &VerifierKey<G, S>, io: &[G::Scalar]) -> Result<(), SpartanError> {
// verify the snark using the constructed instance
self.snark.verify_precommitted(&vk.vk, io)
}
}

type CommitmentKey<G> = <<G as traits::Group>::CE as CommitmentEngineTrait<G>>::CommitmentKey;
Expand Down
14 changes: 13 additions & 1 deletion src/provider/hyrax_pc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
Commitment, CommitmentKey,
};
use core::ops::{Add, AddAssign, Mul, MulAssign};
use crate::provider::bn256_grumpkin::bn256;
use itertools::{
EitherOrBoth::{Both, Left, Right},
Itertools,
Expand All @@ -31,7 +32,8 @@ use std::marker::PhantomData;
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct HyraxCommitmentKey<G: Group> {
ck: PedersenCommitmentKey<G>,
/// ck
pub ck: PedersenCommitmentKey<G>,
}

/// Structure that holds commitments
Expand Down Expand Up @@ -59,6 +61,16 @@ impl<G: Group> Default for HyraxCommitment<G> {
}
}

impl From<Vec<bn256::Affine>> for HyraxCommitment<bn256::Point> {
fn from(v: Vec<bn256::Affine>) -> Self {

HyraxCommitment {
comm: v.iter().map(|x| PedersenCommitment::from(*x)).collect_vec(),
is_default: false,
}
}
}

impl<G: Group> CommitmentTrait<G> for HyraxCommitment<G> {
type CompressedCommitment = HyraxCompressedCommitment<G>;

Expand Down
36 changes: 35 additions & 1 deletion src/provider/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,38 @@ use core::{
};
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use super::bn256_grumpkin::bn256;

/// A type that holds commitment generators
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CommitmentKey<G: Group> {
ck: Vec<G::PreprocessedGroupElement>,
/// Commitment key
pub ck: Vec<G::PreprocessedGroupElement>,
}

/// Generators ck for the bn256 curve
pub fn from_preprocessed_gens<SpartanAffine: halo2curves::CurveAffine>(generators: Vec<SpartanAffine>) -> CommitmentKey<bn256::Point>
where SpartanAffine: halo2curves::CurveAffine<Base = bn256::Base>,
{
let ck: Vec<<bn256::Point as Group>::PreprocessedGroupElement> = generators.into_iter().map(|g| {
let (x, y) = (*g.coordinates().unwrap().x(), *g.coordinates().unwrap().y());
(bn256::Affine {x: x, y: y}).into()
}).collect();

CommitmentKey {
ck,
}
}

/// Generators ck for the bn256 curve
pub fn from_gens_bn256(generators: Vec<bn256::Affine>) -> CommitmentKey<bn256::Point>
{
CommitmentKey {
ck: generators
}
}


/// A type that holds a commitment
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(bound = "")]
Expand Down Expand Up @@ -60,6 +85,15 @@ impl<G: Group> Default for Commitment<G> {
}
}

impl From<bn256::Affine> for Commitment<bn256::Point> {
fn from(v: bn256::Affine) -> Self {

Commitment {
comm: v.into(),
}
}
}

impl<G: Group> TranscriptReprTrait<G> for Commitment<G> {
fn to_transcript_bytes(&self) -> Vec<u8> {
let (x, y, is_infinity) = self.comm.to_coordinates();
Expand Down
52 changes: 52 additions & 0 deletions src/r1cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,3 +785,55 @@ impl<G: Group> TranscriptReprTrait<G> for RelaxedR1CSInstance<G> {
.concat()
}
}

// Segmented structs for Precommitted version

/// A type that holds a witness for a given R1CS instance
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct PrecommittedR1CSWitness<G: Group> {
pub(crate) W: Vec<Vec<G::Scalar>>,
}

/// A type that holds an R1CS instance
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct PrecommittedR1CSInstance<G: Group> {
pub(crate) comm_W: Vec<Commitment<G>>,
pub(crate) X: Vec<G::Scalar>,
}


impl<G: Group> PrecommittedR1CSWitness<G> {
/// A method to create a witness object using a vector of scalars
pub fn new(_S: &R1CSShape<G>, W: Vec<Vec<G::Scalar>>) -> Result<PrecommittedR1CSWitness<G>, SpartanError> {
let w = PrecommittedR1CSWitness { W: W };
Ok(w)
}
}

impl<G: Group> PrecommittedR1CSInstance<G> {
/// A method to create an instance object using consitituent elements
pub fn new(
S: &R1CSShape<G>,
comm_W: Vec<Commitment<G>>,
X: &[G::Scalar],
) -> Result<PrecommittedR1CSInstance<G>, SpartanError> {
if S.num_io != X.len() {
Err(SpartanError::InvalidInputLength)
} else {
Ok(PrecommittedR1CSInstance{
comm_W: comm_W,
X: X.to_owned(),
})
}
}
}

impl<G: Group> TranscriptReprTrait<G> for PrecommittedR1CSInstance<G> {
fn to_transcript_bytes(&self) -> Vec<u8> {
self.comm_W.iter()
.flat_map(|elem| elem.to_transcript_bytes())
.chain(self.X.as_slice().to_transcript_bytes())
.collect::<Vec<_>>()
}
}
Loading