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

Make pairing and groth16 optional 🎉 #25

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 14 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,24 @@ version = "0.1.0"
[dependencies]
rand = "0.4"
bit-vec = "0.4.4"
ff = "0.4"
futures = "0.1"
futures-cpupool = "0.1"
group = "0.1"
num_cpus = "1"
crossbeam = "0.3"
pairing = "0.14"
byteorder = "1"

[dependencies.pairing]
git = "https://github.com/str4d/pairing"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the idea that this will change back once zkcrypto/pairing#91 has been merged?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that is what happened.

rev = "3d41ee5abaa4888ff3607689aba007be8856816d"
optional = true

[features]
default = []
groth16 = ["pairing"]
default = ["groth16"]

[[test]]
name = "mimc"
path = "tests/mimc.rs"
required-features = ["groth16"]
39 changes: 19 additions & 20 deletions src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@
//! This allows us to perform polynomial operations in O(n)
//! by performing an O(n log n) FFT over such a domain.

use pairing::{
Engine,
Field,
PrimeField,
CurveProjective
};
use ff::{Field, PrimeField, ScalarEngine};
use group::CurveProjective;

use super::{
SynthesisError
};

use super::multicore::Worker;

pub struct EvaluationDomain<E: Engine, G: Group<E>> {
pub struct EvaluationDomain<E: ScalarEngine, G: Group<E>> {
coeffs: Vec<G>,
exp: u32,
omega: E::Fr,
Expand All @@ -32,7 +28,7 @@ pub struct EvaluationDomain<E: Engine, G: Group<E>> {
minv: E::Fr
}

impl<E: Engine, G: Group<E>> EvaluationDomain<E, G> {
impl<E: ScalarEngine, G: Group<E>> EvaluationDomain<E, G> {
pub fn as_ref(&self) -> &[G] {
&self.coeffs
}
Expand Down Expand Up @@ -189,7 +185,7 @@ impl<E: Engine, G: Group<E>> EvaluationDomain<E, G> {
}
}

pub trait Group<E: Engine>: Sized + Copy + Clone + Send + Sync {
pub trait Group<E: ScalarEngine>: Sized + Copy + Clone + Send + Sync {
fn group_zero() -> Self;
fn group_mul_assign(&mut self, by: &E::Fr);
fn group_add_assign(&mut self, other: &Self);
Expand Down Expand Up @@ -227,23 +223,23 @@ impl<G: CurveProjective> Group<G::Engine> for Point<G> {
}
}

pub struct Scalar<E: Engine>(pub E::Fr);
pub struct Scalar<E: ScalarEngine>(pub E::Fr);

impl<E: Engine> PartialEq for Scalar<E> {
impl<E: ScalarEngine> PartialEq for Scalar<E> {
fn eq(&self, other: &Scalar<E>) -> bool {
self.0 == other.0
}
}

impl<E: Engine> Copy for Scalar<E> { }
impl<E: ScalarEngine> Copy for Scalar<E> { }

impl<E: Engine> Clone for Scalar<E> {
impl<E: ScalarEngine> Clone for Scalar<E> {
fn clone(&self) -> Scalar<E> {
*self
}
}

impl<E: Engine> Group<E> for Scalar<E> {
impl<E: ScalarEngine> Group<E> for Scalar<E> {
fn group_zero() -> Self {
Scalar(E::Fr::zero())
}
Expand All @@ -258,7 +254,7 @@ impl<E: Engine> Group<E> for Scalar<E> {
}
}

fn best_fft<E: Engine, T: Group<E>>(a: &mut [T], worker: &Worker, omega: &E::Fr, log_n: u32)
fn best_fft<E: ScalarEngine, T: Group<E>>(a: &mut [T], worker: &Worker, omega: &E::Fr, log_n: u32)
{
let log_cpus = worker.log_num_cpus();

Expand All @@ -269,7 +265,7 @@ fn best_fft<E: Engine, T: Group<E>>(a: &mut [T], worker: &Worker, omega: &E::Fr,
}
}

fn serial_fft<E: Engine, T: Group<E>>(a: &mut [T], omega: &E::Fr, log_n: u32)
fn serial_fft<E: ScalarEngine, T: Group<E>>(a: &mut [T], omega: &E::Fr, log_n: u32)
{
fn bitreverse(mut n: u32, l: u32) -> u32 {
let mut r = 0;
Expand Down Expand Up @@ -314,7 +310,7 @@ fn serial_fft<E: Engine, T: Group<E>>(a: &mut [T], omega: &E::Fr, log_n: u32)
}
}

fn parallel_fft<E: Engine, T: Group<E>>(
fn parallel_fft<E: ScalarEngine, T: Group<E>>(
a: &mut [T],
worker: &Worker,
omega: &E::Fr,
Expand Down Expand Up @@ -375,12 +371,13 @@ fn parallel_fft<E: Engine, T: Group<E>>(

// Test multiplying various (low degree) polynomials together and
// comparing with naive evaluations.
#[cfg(feature = "pairing")]
#[test]
fn polynomial_arith() {
use pairing::bls12_381::Bls12;
use rand::{self, Rand};

fn test_mul<E: Engine, R: rand::Rng>(rng: &mut R)
fn test_mul<E: ScalarEngine, R: rand::Rng>(rng: &mut R)
{
let worker = Worker::new();

Expand Down Expand Up @@ -422,12 +419,13 @@ fn polynomial_arith() {
test_mul::<Bls12, _>(rng);
}

#[cfg(feature = "pairing")]
#[test]
fn fft_composition() {
use pairing::bls12_381::Bls12;
use rand;

fn test_comp<E: Engine, R: rand::Rng>(rng: &mut R)
fn test_comp<E: ScalarEngine, R: rand::Rng>(rng: &mut R)
{
let worker = Worker::new();

Expand Down Expand Up @@ -460,13 +458,14 @@ fn fft_composition() {
test_comp::<Bls12, _>(rng);
}

#[cfg(feature = "pairing")]
#[test]
fn parallel_fft_consistency() {
use pairing::bls12_381::Bls12;
use rand::{self, Rand};
use std::cmp::min;

fn test_consistency<E: Engine, R: rand::Rng>(rng: &mut R)
fn test_consistency<E: ScalarEngine, R: rand::Rng>(rng: &mut R)
{
let worker = Worker::new();

Expand Down
11 changes: 3 additions & 8 deletions src/groth16/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ use rand::Rng;

use std::sync::Arc;

use pairing::{
Engine,
PrimeField,
Field,
Wnaf,
CurveProjective,
CurveAffine
};
use ff::{Field, PrimeField};
use group::{CurveAffine, CurveProjective, Wnaf};
use pairing::Engine;

use super::{
Parameters,
Expand Down
10 changes: 5 additions & 5 deletions src/groth16/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use group::{CurveAffine, EncodedPoint};
use pairing::{
Engine,
CurveAffine,
EncodedPoint
PairingCurveAffine,
};

use ::{
Expand Down Expand Up @@ -385,9 +385,9 @@ pub struct PreparedVerifyingKey<E: Engine> {
/// Pairing result of alpha*beta
alpha_g1_beta_g2: E::Fqk,
/// -gamma in G2
neg_gamma_g2: <E::G2Affine as CurveAffine>::Prepared,
neg_gamma_g2: <E::G2Affine as PairingCurveAffine>::Prepared,
/// -delta in G2
neg_delta_g2: <E::G2Affine as CurveAffine>::Prepared,
neg_delta_g2: <E::G2Affine as PairingCurveAffine>::Prepared,
/// Copy of IC from `VerifiyingKey`.
ic: Vec<E::G1Affine>
}
Expand Down Expand Up @@ -486,8 +486,8 @@ mod test_with_bls12_381 {
use super::*;
use {Circuit, SynthesisError, ConstraintSystem};

use ff::Field;
use rand::{Rand, thread_rng};
use pairing::{Field};
use pairing::bls12_381::{Bls12, Fr};

#[test]
Expand Down
10 changes: 3 additions & 7 deletions src/groth16/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ use std::sync::Arc;

use futures::Future;

use pairing::{
Engine,
PrimeField,
Field,
CurveProjective,
CurveAffine
};
use ff::{Field, PrimeField};
use group::{CurveAffine, CurveProjective};
use pairing::Engine;

use super::{
ParameterSource,
Expand Down
44 changes: 21 additions & 23 deletions src/groth16/tests/dummy_engine.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
use pairing::{
Engine,
PrimeField,
PrimeFieldRepr,
Field,
SqrtField,
LegendreSymbol,
CurveProjective,
CurveAffine,
PrimeFieldDecodingError,
GroupDecodingError,
EncodedPoint
};
use ff::{
Field, LegendreSymbol, PrimeField, PrimeFieldDecodingError,
PrimeFieldRepr, ScalarEngine, SqrtField};
use group::{CurveAffine, CurveProjective, EncodedPoint, GroupDecodingError};
use pairing::{Engine, PairingCurveAffine};

use std::cmp::Ordering;
use std::fmt;
Expand Down Expand Up @@ -263,8 +255,11 @@ impl PrimeField for Fr {
#[derive(Clone)]
pub struct DummyEngine;

impl Engine for DummyEngine {
impl ScalarEngine for DummyEngine {
type Fr = Fr;
}

impl Engine for DummyEngine {
type G1 = Fr;
type G1Affine = Fr;
type G2 = Fr;
Expand All @@ -277,8 +272,8 @@ impl Engine for DummyEngine {

fn miller_loop<'a, I>(i: I) -> Self::Fqk
where I: IntoIterator<Item=&'a (
&'a <Self::G1Affine as CurveAffine>::Prepared,
&'a <Self::G2Affine as CurveAffine>::Prepared
&'a <Self::G1Affine as PairingCurveAffine>::Prepared,
&'a <Self::G2Affine as PairingCurveAffine>::Prepared
)>
{
let mut acc = <Fr as Field>::zero();
Expand Down Expand Up @@ -401,11 +396,8 @@ impl EncodedPoint for FakePoint {
}

impl CurveAffine for Fr {
type Pair = Fr;
type PairingResult = Fr;
type Compressed = FakePoint;
type Uncompressed = FakePoint;
type Prepared = Fr;
type Projective = Fr;
type Base = Fr;
type Scalar = Fr;
Expand Down Expand Up @@ -437,15 +429,21 @@ impl CurveAffine for Fr {
res
}

fn into_projective(&self) -> Self::Projective {
*self
}
}

impl PairingCurveAffine for Fr {
type Prepared = Fr;
type Pair = Fr;
type PairingResult = Fr;

fn prepare(&self) -> Self::Prepared {
*self
}

fn pairing_with(&self, other: &Self::Pair) -> Self::PairingResult {
self.mul(*other)
}

fn into_projective(&self) -> Self::Projective {
*self
}
}
7 changes: 2 additions & 5 deletions src/groth16/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use pairing::{
Engine,
Field,
PrimeField
};
use ff::{Field, PrimeField};
use pairing::Engine;

mod dummy_engine;
use self::dummy_engine::*;
Expand Down
9 changes: 3 additions & 6 deletions src/groth16/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use pairing::{
Engine,
CurveProjective,
CurveAffine,
PrimeField
};
use ff::PrimeField;
use group::{CurveAffine, CurveProjective};
use pairing::{Engine, PairingCurveAffine};

use super::{
Proof,
Expand Down
Loading