Skip to content

Commit

Permalink
Merge pull request #68 from zkcrypto/bitvec-0.22
Browse files Browse the repository at this point in the history
Migrate to bitvec 0.22
  • Loading branch information
str4d committed May 21, 2021
2 parents bd4af09 + 7fe5507 commit cdca9f9
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 28 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.47.0
toolchain: 1.51.0
override: true
- name: cargo fetch
uses: actions-rs/cargo@v1
Expand All @@ -39,7 +39,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.47.0
toolchain: 1.51.0
override: true
# Build benchmarks to prevent bitrot
- name: Build benchmarks
Expand All @@ -49,21 +49,21 @@ jobs:
args: --benches --all-features

clippy:
name: Clippy (1.47.0)
name: Clippy (1.51.0)
timeout-minutes: 30
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.47.0
toolchain: 1.51.0
components: clippy
override: true
- name: Run clippy
uses: actions-rs/clippy-check@v1
with:
name: Clippy (1.47.0)
name: Clippy (1.51.0)
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features --all-targets -- -D warnings

Expand Down Expand Up @@ -119,7 +119,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.47.0
toolchain: 1.51.0
override: true
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to Rust's notion of
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Bumped dependencies to `bitvec 0.22`, `ff 0.10`, `group 0.10`, `pairing 0.20`.
- MSRV is now 1.51.0.

## [0.9.0] - 2021-01-26
### Changed
Expand Down
12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ version = "0.9.0"
edition = "2018"

[dependencies]
bitvec = "0.20"
bitvec = "0.22"
blake2s_simd = "0.5"
ff = "0.9"
futures = "0.1"
Expand Down Expand Up @@ -45,5 +45,15 @@ required-features = ["groth16"]
name = "batch"
harness = false

[[bench]]
name = "slow"
harness = false

[badges]
maintenance = { status = "actively-developed" }

[patch.crates-io]
bls12_381 = { git = "https://github.com/zkcrypto/bls12_381.git", branch = "main" }
ff = { git = "https://github.com/zkcrypto/ff.git", branch = "main" }
group = { git = "https://github.com/zkcrypto/group.git", branch = "main" }
pairing = { git = "https://github.com/zkcrypto/pairing.git", branch = "main" }
48 changes: 48 additions & 0 deletions benches/slow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use bellman::{
multicore::Worker,
multiexp::{multiexp, FullDensity},
};
use bls12_381::{Bls12, Scalar};
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use ff::{Field, PrimeFieldBits};
use futures::Future;
use group::{Curve, Group};
use pairing::Engine;
use rand_core::SeedableRng;
use rand_xorshift::XorShiftRng;
use std::sync::Arc;

fn bench_parts(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed([7; 16]);
let samples = 1 << 16;

let v = Arc::new(
(0..samples)
.map(|_| Scalar::random(&mut rng))
.collect::<Vec<_>>(),
);
let v_bits = Arc::new(v.iter().map(|e| e.to_le_bits()).collect::<Vec<_>>());
let g = Arc::new(
(0..samples)
.map(|_| <Bls12 as Engine>::G1::random(&mut rng).to_affine())
.collect::<Vec<_>>(),
);

let pool = Worker::new();

c.bench_with_input(
BenchmarkId::new("multiexp", samples),
&(pool, g, v_bits),
|b, (pool, g, v_bits)| {
b.iter(|| {
let _: <Bls12 as Engine>::G1 =
multiexp(pool, (g.clone(), 0), FullDensity, v_bits.clone())
.wait()
.unwrap();
})
},
);
}

criterion_group!(benches, bench_parts);
criterion_main!(benches);
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.47.0
1.51.0
6 changes: 3 additions & 3 deletions src/gadgets/boolean.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Gadgets for allocating bits in the circuit and performing boolean logic.

use ff::PrimeField;
use ff::{PrimeField, PrimeFieldBits};

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

Expand Down Expand Up @@ -300,7 +300,7 @@ pub fn u64_into_boolean_vec_le<Scalar: PrimeField, CS: ConstraintSystem<Scalar>>
pub fn field_into_boolean_vec_le<
Scalar: PrimeField,
CS: ConstraintSystem<Scalar>,
F: PrimeField,
F: PrimeFieldBits,
>(
cs: CS,
value: Option<F>,
Expand All @@ -313,7 +313,7 @@ pub fn field_into_boolean_vec_le<
pub fn field_into_allocated_bits_le<
Scalar: PrimeField,
CS: ConstraintSystem<Scalar>,
F: PrimeField,
F: PrimeFieldBits,
>(
mut cs: CS,
value: Option<F>,
Expand Down
6 changes: 4 additions & 2 deletions src/gadgets/num.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Gadgets representing numbers in the scalar field of the underlying curve.

use ff::PrimeField;
use ff::{PrimeField, PrimeFieldBits};

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

Expand Down Expand Up @@ -69,6 +69,7 @@ impl<Scalar: PrimeField> AllocatedNum<Scalar> {
/// congruency is not allowed.)
pub fn to_bits_le_strict<CS>(&self, mut cs: CS) -> Result<Vec<Boolean>, SynthesisError>
where
Scalar: PrimeFieldBits,
CS: ConstraintSystem<Scalar>,
{
pub fn kary_and<Scalar, CS>(
Expand Down Expand Up @@ -195,6 +196,7 @@ impl<Scalar: PrimeField> AllocatedNum<Scalar> {
/// "in the field."
pub fn to_bits_le<CS>(&self, mut cs: CS) -> Result<Vec<Boolean>, SynthesisError>
where
Scalar: PrimeFieldBits,
CS: ConstraintSystem<Scalar>,
{
let bits = boolean::field_into_allocated_bits_le(&mut cs, self.value)?;
Expand Down Expand Up @@ -415,7 +417,7 @@ impl<Scalar: PrimeField> Num<Scalar> {
mod test {
use crate::ConstraintSystem;
use bls12_381::Scalar;
use ff::{Field, PrimeField};
use ff::{Field, PrimeField, PrimeFieldBits};
use rand_core::SeedableRng;
use rand_xorshift::XorShiftRng;
use std::ops::{Neg, SubAssign};
Expand Down
4 changes: 3 additions & 1 deletion src/groth16/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;

use futures::Future;

use ff::{Field, PrimeField};
use ff::{Field, PrimeField, PrimeFieldBits};
use group::{prime::PrimeCurveAffine, Curve};
use pairing::Engine;

Expand Down Expand Up @@ -166,6 +166,7 @@ pub fn create_random_proof<E, C, R, P: ParameterSource<E>>(
) -> Result<Proof<E>, SynthesisError>
where
E: Engine,
E::Fr: PrimeFieldBits,
C: Circuit<E::Fr>,
R: RngCore,
{
Expand All @@ -183,6 +184,7 @@ pub fn create_proof<E, C, P: ParameterSource<E>>(
) -> Result<Proof<E>, SynthesisError>
where
E: Engine,
E::Fr: PrimeFieldBits,
C: Circuit<E::Fr>,
{
let mut prover = ProvingAssignment {
Expand Down
23 changes: 13 additions & 10 deletions src/groth16/tests/dummy_engine.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ff::{Field, FieldBits, PrimeField};
use ff::{Field, FieldBits, PrimeField, PrimeFieldBits};
use group::{
prime::{PrimeCurve, PrimeCurveAffine, PrimeGroup},
Curve, Group, GroupEncoding, UncompressedEncoding, WnafGroup,
Expand Down Expand Up @@ -288,7 +288,6 @@ impl Default for FrRepr {

impl PrimeField for Fr {
type Repr = FrRepr;
type ReprBits = u64;

const NUM_BITS: u32 = 16;
const CAPACITY: u32 = 15;
Expand All @@ -307,18 +306,10 @@ impl PrimeField for Fr {
FrRepr::from(*self)
}

fn to_le_bits(&self) -> FieldBits<Self::ReprBits> {
FieldBits::new((self.0).0 as u64)
}

fn is_odd(&self) -> bool {
(self.0).0 % 2 != 0
}

fn char_le_bits() -> FieldBits<Self::ReprBits> {
FieldBits::new(MODULUS_R.0 as u64)
}

fn multiplicative_generator() -> Fr {
Fr(Wrapping(5))
}
Expand All @@ -328,6 +319,18 @@ impl PrimeField for Fr {
}
}

impl PrimeFieldBits for Fr {
type ReprBits = u64;

fn to_le_bits(&self) -> FieldBits<Self::ReprBits> {
FieldBits::new((self.0).0 as u64)
}

fn char_le_bits() -> FieldBits<Self::ReprBits> {
FieldBits::new(MODULUS_R.0 as u64)
}
}

#[derive(Clone)]
pub struct DummyEngine;

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
//! be separate crates that pull in the dependencies they require.

// Catch documentation errors caused by code changes.
#![deny(intra_doc_link_resolution_failure)]
#![deny(broken_intra_doc_links)]

pub mod domain;
pub mod gadgets;
Expand Down
8 changes: 5 additions & 3 deletions src/multiexp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::multicore::Worker;
use bitvec::vec::BitVec;
use ff::{FieldBits, PrimeField};
use ff::{FieldBits, PrimeField, PrimeFieldBits};
use futures::Future;
use group::prime::{PrimeCurve, PrimeCurveAffine};
use std::io;
Expand Down Expand Up @@ -149,7 +149,7 @@ fn multiexp_inner<Q, D, G, S>(
pool: &Worker,
bases: S,
density_map: D,
exponents: Arc<Vec<FieldBits<<G::Scalar as PrimeField>::ReprBits>>>,
exponents: Arc<Vec<FieldBits<<G::Scalar as PrimeFieldBits>::ReprBits>>>,
mut skip: u32,
c: u32,
handle_trivial: bool,
Expand All @@ -158,6 +158,7 @@ where
for<'a> &'a Q: QueryDensity,
D: Send + Sync + 'static + Clone + AsRef<Q>,
G: PrimeCurve,
G::Scalar: PrimeFieldBits,
S: SourceBuilder<<G as PrimeCurve>::Affine>,
{
// Perform this region of the multiexp
Expand Down Expand Up @@ -263,12 +264,13 @@ pub fn multiexp<Q, D, G, S>(
pool: &Worker,
bases: S,
density_map: D,
exponents: Arc<Vec<FieldBits<<G::Scalar as PrimeField>::ReprBits>>>,
exponents: Arc<Vec<FieldBits<<G::Scalar as PrimeFieldBits>::ReprBits>>>,
) -> Box<dyn Future<Item = G, Error = SynthesisError>>
where
for<'a> &'a Q: QueryDensity,
D: Send + Sync + 'static + Clone + AsRef<Q>,
G: PrimeCurve,
G::Scalar: PrimeFieldBits,
S: SourceBuilder<<G as PrimeCurve>::Affine>,
{
let c = if exponents.len() < 32 {
Expand Down

0 comments on commit cdca9f9

Please sign in to comment.