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

Refactor constraint system #131

Merged
merged 27 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f88b7c7
Implementation of ConstraintSystemImpl
lgiussan Sep 28, 2021
abe2565
Update groth16 and gm17 to use ConstraintSystemImpl
lgiussan Sep 28, 2021
376ebdc
Implement test functionality for ConstraintSystemImpl
lgiussan Sep 30, 2021
6dc70ea
Remove TestConstraintSystem and update tests to use ConstraintSystemI…
lgiussan Sep 30, 2021
1edac58
Add variables to named_objects during alloc
lgiussan Sep 30, 2021
0a3364b
Panic when trying to get or set variables during setup
lgiussan Sep 30, 2021
45ff1e9
Bugfix: building constraint matrices when not required
lgiussan Sep 30, 2021
7d24190
Fix Cargo.toml files
lgiussan Sep 30, 2021
aa6ce0b
Fix Cargo.toml files (hopefully)
lgiussan Oct 1, 2021
23739ca
fix failing proof-systems tests
lgiussan Oct 4, 2021
0bd1bc3
rename ConstraintSystemImpl and ConstraintSystem
lgiussan Oct 18, 2021
d5a8399
Add debugging functions to ConstraintSystem trait
lgiussan Oct 18, 2021
8881671
Merge branch 'development_tmp' into refactor_constraint_system
lgiussan Oct 18, 2021
d2dc6cc
Remove print_named_objects function
lgiussan Oct 19, 2021
d2c83e0
Remove unnecessary override
lgiussan Oct 19, 2021
e5c45a0
Delete commented out code
lgiussan Oct 19, 2021
5ecf4e6
Introduce Debug mode in ConstraintSystem
lgiussan Oct 21, 2021
420256d
Merge remote-tracking branch 'origin/refactor_constraint_system' into…
lgiussan Oct 21, 2021
555ec1f
Changes after code review
lgiussan Oct 25, 2021
6b79526
Changes after code review
lgiussan Oct 26, 2021
04926c8
cargo-fmt
lgiussan Oct 28, 2021
f15ebb4
Merge branch 'development' into refactor_constraint_system
lgiussan Oct 28, 2021
7ab5a5b
Update Cargo.toml files
lgiussan Oct 29, 2021
e25730f
Merge branch 'endo' into refactor_constraint_system
lgiussan Oct 29, 2021
d41aef6
Merge remote-tracking branch 'origin/development' into refactor_const…
lgiussan Nov 16, 2021
a61fc10
Merge branch 'development' into refactor_constraint_system
DanieleDiBenedetto Jan 12, 2022
6ac16de
Fix compilation error
DanieleDiBenedetto Jan 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ edition = "2018"
################################# Dependencies ################################

[dependencies]
algebra = { git = "https://github.com/HorizenOfficial/ginger-lib", branch = "development_tmp", features = ["parallel"] }
algebra = { git = "https://github.com/HorizenOfficial/ginger-lib", branch = "sc_testnet_2", features = ["parallel"] }
bench-utils = { path = "../bench-utils" }

digest = { version = "0.8", optional = true }
Expand Down Expand Up @@ -50,7 +50,7 @@ bn_382 = ["algebra/bn_382"]
tweedle = ["algebra/tweedle"]

[dev-dependencies]
algebra = { git = "https://github.com/HorizenOfficial/ginger-lib", branch = "development_tmp", features = ["edwards_sw6", "jubjub", "sw6", "bls12_377"] }
algebra = { git = "https://github.com/HorizenOfficial/ginger-lib", branch = "sc_testnet_2", features = ["edwards_sw6", "jubjub", "sw6", "bls12_377"] }
primitives = { path = "../primitives", features = ["mnt4_753", "mnt6_753", "bn_382", "tweedle"] }

criterion = "0.3.2"
Expand Down
11 changes: 6 additions & 5 deletions proof-systems/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ edition = "2018"
################################# Dependencies ################################

[dependencies]
algebra = { git = "https://github.com/HorizenOfficial/ginger-lib", branch = "development_tmp", features = [ "parallel", "fft"] }
r1cs-core = { git = "https://github.com/HorizenOfficial/ginger-lib", branch = "development_tmp" }
algebra = { git = "https://github.com/HorizenOfficial/ginger-lib", branch = "sc_testnet_2", features = [ "parallel", "fft"] }
r1cs-core = { git = "https://github.com/HorizenOfficial/ginger-lib", branch = "refactor_constraint_system" }
bench-utils = { path = "../bench-utils" }

marlin = { git = "https://github.com/HorizenLabs/marlin", branch = "dev", optional = true }
poly-commit = { git = "https://github.com/HorizenLabs/poly-commit", branch = "dev", optional = true }
marlin = { git = "https://github.com/HorizenLabs/marlin", branch = "refactor_constraint_system", optional = true }
poly-commit = { git = "https://github.com/HorizenLabs/poly-commit", branch = "sc_testnet_2", optional = true }


r1cs-std = { path = "../r1cs/gadgets/std", optional = true }

Expand All @@ -43,7 +44,7 @@ criterion = "0.3"
rand_xorshift = { version = "0.3.0" }
blake2 = { version = "0.8.1", default-features = false }

algebra = { git = "https://github.com/HorizenOfficial/ginger-lib", branch = "development_tmp", features = ["full", "parallel", "fft"] }
algebra = { git = "https://github.com/HorizenOfficial/ginger-lib", branch = "sc_testnet_2", features = ["full", "parallel", "fft"] }
r1cs-crypto = { path = "../r1cs/gadgets/crypto", features = ["nizk"] }

[features]
Expand Down
4 changes: 2 additions & 2 deletions proof-systems/src/darlin/tests/final_darlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! a simple quadratic relation, allocates a given instance of `FinalDarlinDeferredData`,
//! and wires it to the outside via system inputs.
use algebra::{AffineCurve, ToConstraintField, UniformRand};
use r1cs_core::{ConstraintSynthesizer, ConstraintSystem, SynthesisError};
use r1cs_core::{ConstraintSynthesizer, ConstraintSystemAbstract, SynthesisError};
use crate::darlin::{
pcd::{
PCD, PCDParameters, PCDCircuit,
Expand Down Expand Up @@ -142,7 +142,7 @@ impl<G1, G2> ConstraintSynthesizer<G1::ScalarField> for TestCircuit<G1, G2>
G1: AffineCurve<BaseField = <G2 as AffineCurve>::ScalarField> + ToConstraintField<<G2 as AffineCurve>::ScalarField>,
G2: AffineCurve<BaseField = <G1 as AffineCurve>::ScalarField> + ToConstraintField<<G1 as AffineCurve>::ScalarField>,
{
fn generate_constraints<CS: ConstraintSystem<G1::ScalarField>>(
fn generate_constraints<CS: ConstraintSystemAbstract<G1::ScalarField>>(
self,
cs: &mut CS,
) -> Result<(), SynthesisError>
Expand Down
4 changes: 2 additions & 2 deletions proof-systems/src/darlin/tests/simple_marlin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A R1CS density one test circuit of specified number of constraints, which processes
//! two public inputs satisfying a simple quadratic relation.
use algebra::{Field, AffineCurve, UniformRand};
use r1cs_core::{ConstraintSynthesizer, ConstraintSystem, SynthesisError};
use r1cs_core::{ConstraintSynthesizer, ConstraintSystemAbstract, SynthesisError};
use poly_commit::ipa_pc::{InnerProductArgPC, CommitterKey, UniversalParams};
use marlin::{
Marlin, ProverKey as MarlinProverKey, VerifierKey as MarlinVerifierKey,
Expand Down Expand Up @@ -29,7 +29,7 @@ pub(crate) struct Circuit<F: Field> {
}

impl<ConstraintF: Field> ConstraintSynthesizer<ConstraintF> for Circuit<ConstraintF> {
fn generate_constraints<CS: ConstraintSystem<ConstraintF>>(
fn generate_constraints<CS: ConstraintSystemAbstract<ConstraintF>>(
self,
cs: &mut CS,
) -> Result<(), SynthesisError> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use r1cs_crypto::nizk::{
},
};
use proof_systems::gm17::{Parameters, Proof};
use r1cs_core::{ConstraintSynthesizer, ConstraintSystem, SynthesisError};
use r1cs_core::{ConstraintSynthesizer, ConstraintSystemAbstract, SynthesisError};
use r1cs_std::{
alloc::AllocGadget, bits::ToBitsGadget, boolean::Boolean, fields::fp::FpGadget,
pairing::PairingGadget as PG
Expand Down Expand Up @@ -93,7 +93,7 @@ impl<F: Field> InnerCircuit<F> {
}

impl<F: Field> ConstraintSynthesizer<F> for InnerCircuit<F> {
fn generate_constraints<CS: ConstraintSystem<F>>(
fn generate_constraints<CS: ConstraintSystemAbstract<F>>(
self,
cs: &mut CS,
) -> Result<(), SynthesisError> {
Expand Down Expand Up @@ -164,7 +164,7 @@ impl<C: CurvePair> MiddleCircuit<C> {
impl<C: CurvePair> ConstraintSynthesizer<<C::PairingEngineTock as PairingEngine>::Fr>
for MiddleCircuit<C>
{
fn generate_constraints<CS: ConstraintSystem<<C::PairingEngineTock as PairingEngine>::Fr>>(
fn generate_constraints<CS: ConstraintSystemAbstract<<C::PairingEngineTock as PairingEngine>::Fr>>(
self,
cs: &mut CS,
) -> Result<(), SynthesisError> {
Expand Down Expand Up @@ -247,7 +247,7 @@ impl<C: CurvePair> OuterCircuit<C> {
impl<C: CurvePair> ConstraintSynthesizer<<C::PairingEngineTick as PairingEngine>::Fr>
for OuterCircuit<C>
{
fn generate_constraints<CS: ConstraintSystem<<C::PairingEngineTick as PairingEngine>::Fr>>(
fn generate_constraints<CS: ConstraintSystemAbstract<<C::PairingEngineTick as PairingEngine>::Fr>>(
self,
cs: &mut CS,
) -> Result<(), SynthesisError> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use algebra::Field;
use r1cs_core::{ConstraintSynthesizer, ConstraintSystem, LinearCombination, SynthesisError};
use r1cs_core::{ConstraintSynthesizer, ConstraintSystemAbstract, LinearCombination, SynthesisError};
use std::marker::PhantomData;

pub struct Benchmark<F: Field> {
Expand All @@ -17,7 +17,7 @@ impl<F: Field> Benchmark<F> {
}

impl<F: Field> ConstraintSynthesizer<F> for Benchmark<F> {
fn generate_constraints<CS: ConstraintSystem<F>>(self, cs: &mut CS) -> Result<(), SynthesisError> {
fn generate_constraints<CS: ConstraintSystemAbstract<F>>(self, cs: &mut CS) -> Result<(), SynthesisError> {
let mut assignments = Vec::new();

let mut a_val = F::one();
Expand Down
126 changes: 3 additions & 123 deletions proof-systems/src/gm17/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use algebra::{

use rand::Rng;
use rayon::prelude::*;
use r1cs_core::{ConstraintSynthesizer, ConstraintSystem, Index, LinearCombination, SynthesisError, Variable};
use r1cs_core::{ConstraintSynthesizer, SynthesisError, ConstraintSystem, SynthesisMode};

use crate::gm17::{Parameters, VerifyingKey, r1cs_to_sap::R1CStoSAP};

Expand All @@ -34,117 +34,6 @@ where
generate_parameters::<E, C, R>(circuit, alpha, beta, gamma, g, h, rng)
}

/// This is our assembly structure that we'll use to synthesize the
/// circuit into a SAP.
pub struct KeypairAssembly<E: PairingEngine> {
pub(crate) num_inputs: usize,
pub(crate) num_aux: usize,
pub(crate) num_constraints: usize,
pub(crate) at: Vec<Vec<(E::Fr, Index)>>,
pub(crate) bt: Vec<Vec<(E::Fr, Index)>>,
pub(crate) ct: Vec<Vec<(E::Fr, Index)>>,
}

impl<E: PairingEngine> ConstraintSystem<E::Fr> for KeypairAssembly<E> {
type Root = Self;

#[inline]
fn alloc<F, A, AR>(&mut self, _: A, _: F) -> Result<Variable, SynthesisError>
where
F: FnOnce() -> Result<E::Fr, SynthesisError>,
A: FnOnce() -> AR,
AR: Into<String>,
{
// There is no assignment, so we don't invoke the
// function for obtaining one.

let index = self.num_aux;
self.num_aux += 1;

Ok(Variable::new_unchecked(Index::Aux(index)))
}

#[inline]
fn alloc_input<F, A, AR>(&mut self, _: A, _: F) -> Result<Variable, SynthesisError>
where
F: FnOnce() -> Result<E::Fr, SynthesisError>,
A: FnOnce() -> AR,
AR: Into<String>,
{
// There is no assignment, so we don't invoke the
// function for obtaining one.

let index = self.num_inputs;
self.num_inputs += 1;

Ok(Variable::new_unchecked(Index::Input(index)))
}

fn enforce<A, AR, LA, LB, LC>(&mut self, _: A, a: LA, b: LB, c: LC)
where
A: FnOnce() -> AR,
AR: Into<String>,
LA: FnOnce(LinearCombination<E::Fr>) -> LinearCombination<E::Fr>,
LB: FnOnce(LinearCombination<E::Fr>) -> LinearCombination<E::Fr>,
LC: FnOnce(LinearCombination<E::Fr>) -> LinearCombination<E::Fr>,
{
fn eval<E: PairingEngine>(
l: LinearCombination<E::Fr>,
constraints: &mut [Vec<(E::Fr, Index)>],
this_constraint: usize,
) {
for (var, coeff) in l.as_ref() {
match var.get_unchecked() {
Index::Input(i) => constraints[this_constraint].push((*coeff, Index::Input(i))),
Index::Aux(i) => constraints[this_constraint].push((*coeff, Index::Aux(i))),
}
}
}

self.at.push(vec![]);
self.bt.push(vec![]);
self.ct.push(vec![]);

eval::<E>(
a(LinearCombination::zero()),
&mut self.at,
self.num_constraints,
);
eval::<E>(
b(LinearCombination::zero()),
&mut self.bt,
self.num_constraints,
);
eval::<E>(
c(LinearCombination::zero()),
&mut self.ct,
self.num_constraints,
);

self.num_constraints += 1;
}

fn push_namespace<NR, N>(&mut self, _: N)
where
NR: Into<String>,
N: FnOnce() -> NR,
{
// Do nothing; we don't care about namespaces in this context.
}

fn pop_namespace(&mut self) {
// Do nothing; we don't care about namespaces in this context.
}

fn get_root(&mut self) -> &mut Self::Root {
self
}

fn num_constraints(&self) -> usize {
self.num_constraints
}
}

/// Create parameters for a circuit, given some toxic waste.
pub fn generate_parameters<E, C, R>(
circuit: C,
Expand All @@ -160,17 +49,8 @@ where
C: ConstraintSynthesizer<E::Fr>,
R: Rng,
{
let mut assembly = KeypairAssembly {
num_inputs: 0,
num_aux: 0,
num_constraints: 0,
at: vec![],
bt: vec![],
ct: vec![],
};

// Allocate the "one" input variable
assembly.alloc_input(|| "", || Ok(E::Fr::one()))?;
let mut assembly = ConstraintSystem::<E::Fr>::new();
assembly.set_mode(SynthesisMode::Setup);

// Synthesize the circuit.
let synthesis_time = start_timer!(|| "Constraint synthesis");
Expand Down
Loading