Skip to content

Commit

Permalink
Merge pull request #127 from str4d/more-crate-updates
Browse files Browse the repository at this point in the history
More crate updates
  • Loading branch information
str4d authored Sep 18, 2019
2 parents 2b6fbfd + 28dcc1c commit d2da9cf
Show file tree
Hide file tree
Showing 17 changed files with 291 additions and 199 deletions.
313 changes: 208 additions & 105 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions bellman/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ futures = "0.1"
futures-cpupool = { version = "0.1", optional = true }
group = { path = "../group" }
num_cpus = { version = "1", optional = true }
crossbeam = { version = "0.3", optional = true }
crossbeam = { version = "0.7", optional = true }
pairing = { path = "../pairing", optional = true }
rand_core = "0.5"
byteorder = "1"

[dev-dependencies]
hex-literal = "0.1"
hex-literal = "0.2"
rand = "0.7"
rand_xorshift = "0.2"
sha2 = "0.8"
Expand Down
14 changes: 7 additions & 7 deletions bellman/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl<E: ScalarEngine, G: Group<E>> EvaluationDomain<E, G> {
let minv = self.minv;

for v in self.coeffs.chunks_mut(chunk) {
scope.spawn(move || {
scope.spawn(move |_scope| {
for v in v {
v.group_mul_assign(&minv);
}
Expand All @@ -103,7 +103,7 @@ impl<E: ScalarEngine, G: Group<E>> EvaluationDomain<E, G> {
pub fn distribute_powers(&mut self, worker: &Worker, g: E::Fr) {
worker.scope(self.coeffs.len(), |scope, chunk| {
for (i, v) in self.coeffs.chunks_mut(chunk).enumerate() {
scope.spawn(move || {
scope.spawn(move |_scope| {
let mut u = g.pow(&[(i * chunk) as u64]);
for v in v.iter_mut() {
v.group_mul_assign(&u);
Expand Down Expand Up @@ -146,7 +146,7 @@ impl<E: ScalarEngine, G: Group<E>> EvaluationDomain<E, G> {

worker.scope(self.coeffs.len(), |scope, chunk| {
for v in self.coeffs.chunks_mut(chunk) {
scope.spawn(move || {
scope.spawn(move |_scope| {
for v in v {
v.group_mul_assign(&i);
}
Expand All @@ -165,7 +165,7 @@ impl<E: ScalarEngine, G: Group<E>> EvaluationDomain<E, G> {
.chunks_mut(chunk)
.zip(other.coeffs.chunks(chunk))
{
scope.spawn(move || {
scope.spawn(move |_scope| {
for (a, b) in a.iter_mut().zip(b.iter()) {
a.group_mul_assign(&b.0);
}
Expand All @@ -184,7 +184,7 @@ impl<E: ScalarEngine, G: Group<E>> EvaluationDomain<E, G> {
.chunks_mut(chunk)
.zip(other.coeffs.chunks(chunk))
{
scope.spawn(move || {
scope.spawn(move |_scope| {
for (a, b) in a.iter_mut().zip(b.iter()) {
a.group_sub_assign(&b);
}
Expand Down Expand Up @@ -335,7 +335,7 @@ fn parallel_fft<E: ScalarEngine, T: Group<E>>(
let a = &*a;

for (j, tmp) in tmp.iter_mut().enumerate() {
scope.spawn(move || {
scope.spawn(move |_scope| {
// Shuffle into a sub-FFT
let omega_j = omega.pow(&[j as u64]);
let omega_step = omega.pow(&[(j as u64) << log_new_n]);
Expand Down Expand Up @@ -363,7 +363,7 @@ fn parallel_fft<E: ScalarEngine, T: Group<E>>(
let tmp = &tmp;

for (idx, a) in a.chunks_mut(chunk).enumerate() {
scope.spawn(move || {
scope.spawn(move |_scope| {
let mut idx = idx * chunk;
let mask = (1 << log_cpus) - 1;
for a in a {
Expand Down
16 changes: 5 additions & 11 deletions bellman/src/gadgets/blake2s.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
use pairing::Engine;

use super::{boolean::Boolean, multieq::MultiEq, uint32::UInt32};
use crate::{ConstraintSystem, SynthesisError};

use super::boolean::Boolean;

use super::uint32::UInt32;

use super::multieq::MultiEq;
use ff::ScalarEngine;

/*
2.1. Parameters
Expand Down Expand Up @@ -81,7 +75,7 @@ const SIGMA: [[usize; 16]; 10] = [
END FUNCTION.
*/

fn mixing_g<E: Engine, CS: ConstraintSystem<E>, M>(
fn mixing_g<E: ScalarEngine, CS: ConstraintSystem<E>, M>(
mut cs: M,
v: &mut [UInt32],
a: usize,
Expand Down Expand Up @@ -166,7 +160,7 @@ where
END FUNCTION.
*/

fn blake2s_compression<E: Engine, CS: ConstraintSystem<E>>(
fn blake2s_compression<E: ScalarEngine, CS: ConstraintSystem<E>>(
mut cs: CS,
h: &mut [UInt32],
m: &[UInt32],
Expand Down Expand Up @@ -339,7 +333,7 @@ fn blake2s_compression<E: Engine, CS: ConstraintSystem<E>>(
END FUNCTION.
*/

pub fn blake2s<E: Engine, CS: ConstraintSystem<E>>(
pub fn blake2s<E: ScalarEngine, CS: ConstraintSystem<E>>(
mut cs: CS,
input: &[Boolean],
personalization: &[u8],
Expand Down
33 changes: 16 additions & 17 deletions bellman/src/gadgets/boolean.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use ff::{BitIterator, Field, PrimeField};
use pairing::Engine;
use ff::{BitIterator, Field, PrimeField, ScalarEngine};

use crate::{ConstraintSystem, LinearCombination, SynthesisError, Variable};

Expand Down Expand Up @@ -31,7 +30,7 @@ impl AllocatedBit {
must_be_false: &AllocatedBit,
) -> Result<Self, SynthesisError>
where
E: Engine,
E: ScalarEngine,
CS: ConstraintSystem<E>,
{
let var = cs.alloc(
Expand Down Expand Up @@ -68,7 +67,7 @@ impl AllocatedBit {
/// boolean value.
pub fn alloc<E, CS>(mut cs: CS, value: Option<bool>) -> Result<Self, SynthesisError>
where
E: Engine,
E: ScalarEngine,
CS: ConstraintSystem<E>,
{
let var = cs.alloc(
Expand Down Expand Up @@ -101,7 +100,7 @@ impl AllocatedBit {
/// an `AllocatedBit`.
pub fn xor<E, CS>(mut cs: CS, a: &Self, b: &Self) -> Result<Self, SynthesisError>
where
E: Engine,
E: ScalarEngine,
CS: ConstraintSystem<E>,
{
let mut result_value = None;
Expand Down Expand Up @@ -153,7 +152,7 @@ impl AllocatedBit {
/// an `AllocatedBit`.
pub fn and<E, CS>(mut cs: CS, a: &Self, b: &Self) -> Result<Self, SynthesisError>
where
E: Engine,
E: ScalarEngine,
CS: ConstraintSystem<E>,
{
let mut result_value = None;
Expand Down Expand Up @@ -191,7 +190,7 @@ impl AllocatedBit {
/// Calculates `a AND (NOT b)`.
pub fn and_not<E, CS>(mut cs: CS, a: &Self, b: &Self) -> Result<Self, SynthesisError>
where
E: Engine,
E: ScalarEngine,
CS: ConstraintSystem<E>,
{
let mut result_value = None;
Expand Down Expand Up @@ -229,7 +228,7 @@ impl AllocatedBit {
/// Calculates `(NOT a) AND (NOT b)`.
pub fn nor<E, CS>(mut cs: CS, a: &Self, b: &Self) -> Result<Self, SynthesisError>
where
E: Engine,
E: ScalarEngine,
CS: ConstraintSystem<E>,
{
let mut result_value = None;
Expand Down Expand Up @@ -265,7 +264,7 @@ impl AllocatedBit {
}
}

pub fn u64_into_boolean_vec_le<E: Engine, CS: ConstraintSystem<E>>(
pub fn u64_into_boolean_vec_le<E: ScalarEngine, CS: ConstraintSystem<E>>(
mut cs: CS,
value: Option<u64>,
) -> Result<Vec<Boolean>, SynthesisError> {
Expand Down Expand Up @@ -296,7 +295,7 @@ pub fn u64_into_boolean_vec_le<E: Engine, CS: ConstraintSystem<E>>(
Ok(bits)
}

pub fn field_into_boolean_vec_le<E: Engine, CS: ConstraintSystem<E>, F: PrimeField>(
pub fn field_into_boolean_vec_le<E: ScalarEngine, CS: ConstraintSystem<E>, F: PrimeField>(
cs: CS,
value: Option<F>,
) -> Result<Vec<Boolean>, SynthesisError> {
Expand All @@ -305,7 +304,7 @@ pub fn field_into_boolean_vec_le<E: Engine, CS: ConstraintSystem<E>, F: PrimeFie
Ok(v.into_iter().map(Boolean::from).collect())
}

pub fn field_into_allocated_bits_le<E: Engine, CS: ConstraintSystem<E>, F: PrimeField>(
pub fn field_into_allocated_bits_le<E: ScalarEngine, CS: ConstraintSystem<E>, F: PrimeField>(
mut cs: CS,
value: Option<F>,
) -> Result<Vec<AllocatedBit>, SynthesisError> {
Expand Down Expand Up @@ -367,7 +366,7 @@ impl Boolean {

pub fn enforce_equal<E, CS>(mut cs: CS, a: &Self, b: &Self) -> Result<(), SynthesisError>
where
E: Engine,
E: ScalarEngine,
CS: ConstraintSystem<E>,
{
match (a, b) {
Expand Down Expand Up @@ -419,7 +418,7 @@ impl Boolean {
}
}

pub fn lc<E: Engine>(&self, one: Variable, coeff: E::Fr) -> LinearCombination<E> {
pub fn lc<E: ScalarEngine>(&self, one: Variable, coeff: E::Fr) -> LinearCombination<E> {
match *self {
Boolean::Constant(c) => {
if c {
Expand Down Expand Up @@ -452,7 +451,7 @@ impl Boolean {
/// Perform XOR over two boolean operands
pub fn xor<'a, E, CS>(cs: CS, a: &'a Self, b: &'a Self) -> Result<Self, SynthesisError>
where
E: Engine,
E: ScalarEngine,
CS: ConstraintSystem<E>,
{
match (a, b) {
Expand All @@ -474,7 +473,7 @@ impl Boolean {
/// Perform AND over two boolean operands
pub fn and<'a, E, CS>(cs: CS, a: &'a Self, b: &'a Self) -> Result<Self, SynthesisError>
where
E: Engine,
E: ScalarEngine,
CS: ConstraintSystem<E>,
{
match (a, b) {
Expand Down Expand Up @@ -508,7 +507,7 @@ impl Boolean {
c: &'a Self,
) -> Result<Self, SynthesisError>
where
E: Engine,
E: ScalarEngine,
CS: ConstraintSystem<E>,
{
let ch_value = match (a.get_value(), b.get_value(), c.get_value()) {
Expand Down Expand Up @@ -615,7 +614,7 @@ impl Boolean {
c: &'a Self,
) -> Result<Self, SynthesisError>
where
E: Engine,
E: ScalarEngine,
CS: ConstraintSystem<E>,
{
let maj_value = match (a.get_value(), b.get_value(), c.get_value()) {
Expand Down
9 changes: 4 additions & 5 deletions bellman/src/gadgets/lookup.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use ff::Field;
use pairing::Engine;
use ff::{Field, ScalarEngine};

use super::boolean::Boolean;
use super::num::{AllocatedNum, Num};
use super::*;
use crate::ConstraintSystem;

// Synthesize the constants for each base pattern.
fn synth<'a, E: Engine, I>(window_size: usize, constants: I, assignment: &mut [E::Fr])
fn synth<'a, E: ScalarEngine, I>(window_size: usize, constants: I, assignment: &mut [E::Fr])
where
I: IntoIterator<Item = &'a E::Fr>,
{
Expand All @@ -28,7 +27,7 @@ where

/// Performs a 3-bit window table lookup. `bits` is in
/// little-endian order.
pub fn lookup3_xy<E: Engine, CS>(
pub fn lookup3_xy<E: ScalarEngine, CS>(
mut cs: CS,
bits: &[Boolean],
coords: &[(E::Fr, E::Fr)],
Expand Down Expand Up @@ -118,7 +117,7 @@ where

/// Performs a 3-bit window table lookup, where
/// one of the bits is a sign bit.
pub fn lookup3_xy_with_conditional_negation<E: Engine, CS>(
pub fn lookup3_xy_with_conditional_negation<E: ScalarEngine, CS>(
mut cs: CS,
bits: &[Boolean],
coords: &[(E::Fr, E::Fr)],
Expand Down
11 changes: 5 additions & 6 deletions bellman/src/gadgets/multieq.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use ff::{Field, PrimeField};
use pairing::Engine;
use ff::{Field, PrimeField, ScalarEngine};

use crate::{ConstraintSystem, LinearCombination, SynthesisError, Variable};

pub struct MultiEq<E: Engine, CS: ConstraintSystem<E>> {
pub struct MultiEq<E: ScalarEngine, CS: ConstraintSystem<E>> {
cs: CS,
ops: usize,
bits_used: usize,
lhs: LinearCombination<E>,
rhs: LinearCombination<E>,
}

impl<E: Engine, CS: ConstraintSystem<E>> MultiEq<E, CS> {
impl<E: ScalarEngine, CS: ConstraintSystem<E>> MultiEq<E, CS> {
pub fn new(cs: CS) -> Self {
MultiEq {
cs,
Expand Down Expand Up @@ -58,15 +57,15 @@ impl<E: Engine, CS: ConstraintSystem<E>> MultiEq<E, CS> {
}
}

impl<E: Engine, CS: ConstraintSystem<E>> Drop for MultiEq<E, CS> {
impl<E: ScalarEngine, CS: ConstraintSystem<E>> Drop for MultiEq<E, CS> {
fn drop(&mut self) {
if self.bits_used > 0 {
self.accumulate();
}
}
}

impl<E: Engine, CS: ConstraintSystem<E>> ConstraintSystem<E> for MultiEq<E, CS> {
impl<E: ScalarEngine, CS: ConstraintSystem<E>> ConstraintSystem<E> for MultiEq<E, CS> {
type Root = Self;

fn one() -> Variable {
Expand Down
7 changes: 3 additions & 4 deletions bellman/src/gadgets/multipack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ use super::boolean::Boolean;
use super::num::Num;
use super::Assignment;
use crate::{ConstraintSystem, SynthesisError};
use ff::{Field, PrimeField};
use pairing::Engine;
use ff::{Field, PrimeField, ScalarEngine};

/// Takes a sequence of booleans and exposes them as compact
/// public inputs
pub fn pack_into_inputs<E, CS>(mut cs: CS, bits: &[Boolean]) -> Result<(), SynthesisError>
where
E: Engine,
E: ScalarEngine,
CS: ConstraintSystem<E>,
{
for (i, bits) in bits.chunks(E::Fr::CAPACITY as usize).enumerate() {
Expand Down Expand Up @@ -49,7 +48,7 @@ pub fn bytes_to_bits_le(bytes: &[u8]) -> Vec<bool> {
.collect()
}

pub fn compute_multipacking<E: Engine>(bits: &[bool]) -> Vec<E::Fr> {
pub fn compute_multipacking<E: ScalarEngine>(bits: &[bool]) -> Vec<E::Fr> {
let mut result = vec![];

for bits in bits.chunks(E::Fr::CAPACITY as usize) {
Expand Down
Loading

0 comments on commit d2da9cf

Please sign in to comment.