Skip to content

Commit

Permalink
Use ff crate for Field traits
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Jul 6, 2018
1 parent 10c5010 commit 718b25c
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 37 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ version = "0.1.0"
[dependencies]
rand = "0.4"
bit-vec = "0.4.4"
ff = "0.4"
futures = "0.1"
futures-cpupool = "0.1"
num_cpus = "1"
crossbeam = "0.3"
pairing = "0.14"
byteorder = "1"

[dependencies.pairing]
git = "https://github.com/ebfull/pairing"
rev = "183a64b08e9dc7067f78624ec161371f1829623e"

[features]
default = []
3 changes: 1 addition & 2 deletions src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
//! This allows us to perform polynomial operations in O(n)
//! by performing an O(n log n) FFT over such a domain.
use ff::{Field, PrimeField};
use pairing::{
Engine,
Field,
PrimeField,
CurveProjective
};

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

use std::sync::Arc;

use ff::{Field, PrimeField};
use pairing::{
Engine,
PrimeField,
Field,
Wnaf,
CurveProjective,
CurveAffine
Expand Down
2 changes: 1 addition & 1 deletion src/groth16/mod.rs
Original file line number Diff line number Diff line change
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
3 changes: 1 addition & 2 deletions src/groth16/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use std::sync::Arc;

use futures::Future;

use ff::{Field, PrimeField};
use pairing::{
Engine,
PrimeField,
Field,
CurveProjective,
CurveAffine
};
Expand Down
14 changes: 7 additions & 7 deletions src/groth16/tests/dummy_engine.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use ff::{
Field, LegendreSymbol, PrimeField, PrimeFieldDecodingError,
PrimeFieldRepr, ScalarEngine, SqrtField};
use pairing::{
Engine,
PrimeField,
PrimeFieldRepr,
Field,
SqrtField,
LegendreSymbol,
CurveProjective,
CurveAffine,
PrimeFieldDecodingError,
GroupDecodingError,
EncodedPoint
};
Expand Down Expand Up @@ -263,8 +260,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 Down
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
2 changes: 1 addition & 1 deletion src/groth16/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ff::PrimeField;
use pairing::{
Engine,
CurveProjective,
CurveAffine,
PrimeField
};

use super::{
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
extern crate ff;
extern crate pairing;
extern crate rand;
extern crate num_cpus;
Expand All @@ -12,7 +13,8 @@ mod multiexp;
pub mod domain;
pub mod groth16;

use pairing::{Engine, Field};
use ff::Field;
use pairing::Engine;

use std::ops::{Add, Sub};
use std::fmt;
Expand Down
19 changes: 8 additions & 11 deletions src/multiexp.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use ff::{Field, PrimeField, PrimeFieldRepr, ScalarEngine};
use pairing::{
CurveAffine,
CurveProjective,
Engine,
PrimeField,
Field,
PrimeFieldRepr
};
use std::sync::Arc;
use std::io;
Expand Down Expand Up @@ -141,7 +138,7 @@ fn multiexp_inner<Q, D, G, S>(
pool: &Worker,
bases: S,
density_map: D,
exponents: Arc<Vec<<<G::Engine as Engine>::Fr as PrimeField>::Repr>>,
exponents: Arc<Vec<<<G::Engine as ScalarEngine>::Fr as PrimeField>::Repr>>,
mut skip: u32,
c: u32,
handle_trivial: bool
Expand All @@ -167,8 +164,8 @@ fn multiexp_inner<Q, D, G, S>(
// Create space for the buckets
let mut buckets = vec![<G as CurveAffine>::Projective::zero(); (1 << c) - 1];

let zero = <G::Engine as Engine>::Fr::zero().into_repr();
let one = <G::Engine as Engine>::Fr::one().into_repr();
let zero = <G::Engine as ScalarEngine>::Fr::zero().into_repr();
let one = <G::Engine as ScalarEngine>::Fr::one().into_repr();

// Sort the bases into buckets
for (&exp, density) in exponents.iter().zip(density_map.as_ref().iter()) {
Expand Down Expand Up @@ -211,7 +208,7 @@ fn multiexp_inner<Q, D, G, S>(

skip += c;

if skip >= <G::Engine as Engine>::Fr::NUM_BITS {
if skip >= <G::Engine as ScalarEngine>::Fr::NUM_BITS {
// There isn't another region.
Box::new(this)
} else {
Expand All @@ -238,7 +235,7 @@ pub fn multiexp<Q, D, G, S>(
pool: &Worker,
bases: S,
density_map: D,
exponents: Arc<Vec<<<G::Engine as Engine>::Fr as PrimeField>::Repr>>
exponents: Arc<Vec<<<G::Engine as ScalarEngine>::Fr as PrimeField>::Repr>>
) -> Box<Future<Item=<G as CurveAffine>::Projective, Error=SynthesisError>>
where for<'a> &'a Q: QueryDensity,
D: Send + Sync + 'static + Clone + AsRef<Q>,
Expand Down Expand Up @@ -280,12 +277,12 @@ fn test_with_bls12() {
}

use rand::{self, Rand};
use pairing::bls12_381::Bls12;
use pairing::{bls12_381::Bls12, Engine};

const SAMPLES: usize = 1 << 14;

let rng = &mut rand::thread_rng();
let v = Arc::new((0..SAMPLES).map(|_| <Bls12 as Engine>::Fr::rand(rng).into_repr()).collect::<Vec<_>>());
let v = Arc::new((0..SAMPLES).map(|_| <Bls12 as ScalarEngine>::Fr::rand(rng).into_repr()).collect::<Vec<_>>());
let g = Arc::new((0..SAMPLES).map(|_| <Bls12 as Engine>::G1::rand(rng).into_affine()).collect::<Vec<_>>());

let naive = naive_multiexp(g.clone(), v.clone());
Expand Down
7 changes: 3 additions & 4 deletions tests/mimc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extern crate bellman;
extern crate ff;
extern crate pairing;
extern crate rand;

Expand All @@ -9,10 +10,8 @@ use rand::{thread_rng, Rng};
use std::time::{Duration, Instant};

// Bring in some tools for using pairing-friendly curves
use pairing::{
Engine,
Field
};
use ff::Field;
use pairing::Engine;

// We're going to use the BLS12-381 pairing-friendly elliptic curve.
use pairing::bls12_381::{
Expand Down

0 comments on commit 718b25c

Please sign in to comment.