Skip to content

Commit

Permalink
feat: add blstrs and paired backends
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Oct 27, 2020
1 parent 7b7a4a4 commit 118b481
Show file tree
Hide file tree
Showing 39 changed files with 300 additions and 145 deletions.
125 changes: 111 additions & 14 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,52 @@ restore-cache: &restore-cache
- repo-source-{{ .Branch }}-{{ .Revision }}

commands:
test_target:
test_target_pairing:
parameters:
target:
type: string
steps:
- *restore-workspace
- *restore-cache
- run:
name: Test (<< parameters.target >>)
command: TARGET=<< parameters.target >> cargo test
name: Test pairing (<< parameters.target >>)
command: TARGET=<< parameters.target >> cargo test --no-default-features --features pairing
no_output_timeout: 15m

test_target_pairing_gpu:
parameters:
target:
type: string
steps:
- *restore-workspace
- *restore-cache
- run:
name: Test pairing (GPU) (<< parameters.target >>)
command: TARGET=<< parameters.target >> cargo test --release --no-default-features --features gpu,pairing
no_output_timeout: 30m

test_target_blst:
parameters:
target:
type: string
steps:
- *restore-workspace
- *restore-cache
- run:
name: Test blst (<< parameters.target >>)
command: TARGET=<< parameters.target >> cargo test --no-default-features --features blst
no_output_timeout: 15m

test_target_blst_gpu:
parameters:
target:
type: string
steps:
- *restore-workspace
- *restore-cache
- run:
name: Test (GPU) (<< parameters.target >>)
command: TARGET=<< parameters.target >> cargo test --release --features gpu
name: Test blst (GPU) (<< parameters.target >>)
command: TARGET=<< parameters.target >> cargo test --release --no-default-features --features gpu,blst
no_output_timeout: 30m

jobs:
Expand Down Expand Up @@ -74,15 +106,49 @@ jobs:
- "~/.cargo"
- "~/.rustup"

test_x86_64-unknown-linux-gnu:
test_pairing_x86_64-unknown-linux-gnu:
executor: default
steps:
- run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
- run: source $BASH_ENV
- run: sudo apt-get update -y
- run: apt-cache search opencl
- run: sudo apt install -y ocl-icd-opencl-dev
- test_target_pairing:
target: "x86_64-unknown-linux-gnu"

test_pairing_gpu_x86_64-unknown-linux-gnu:
executor: default
steps:
- run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
- run: source $BASH_ENV
- run: sudo apt-get update -y
- run: apt-cache search opencl
- run: sudo apt install -y ocl-icd-opencl-dev
- test_target_pairing_gpu:
target: "x86_64-unknown-linux-gnu"


test_blst_x86_64-unknown-linux-gnu:
executor: default
steps:
- run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
- run: source $BASH_ENV
- run: sudo apt-get update -y
- run: apt-cache search opencl
- run: sudo apt install -y ocl-icd-opencl-dev
- test_target_blst:
target: "x86_64-unknown-linux-gnu"

test_blst_gpu_x86_64-unknown-linux-gnu:
executor: default
steps:
- run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
- run: source $BASH_ENV
- run: sudo apt-get update -y
- run: apt-cache search opencl
- run: sudo apt install -y ocl-icd-opencl-dev
- test_target:
- test_target_blst_gpu:
target: "x86_64-unknown-linux-gnu"

rustfmt:
Expand All @@ -104,10 +170,16 @@ jobs:
- run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
- run: source $BASH_ENV
- run:
name: Run cargo clippy
command: cargo clippy --all-features
name: Run cargo clippy (blst)
command: cargo clippy --no-default-features --features blst
- run:
name: Run cargo clippy (pairing)
command: cargo clippy --no-default-features --features pairing
- run:
name: Run cargo clippy (gpu)
command: cargo clippy --features gpu

build:
build_blst:
executor: default
steps:
- *restore-workspace
Expand All @@ -116,8 +188,18 @@ jobs:
- run: source $BASH_ENV
- run:
name: Run cargo release build
command: cargo build --release
command: cargo build --release --no-default-features --features blst

build_pairing:
executor: default
steps:
- *restore-workspace
- *restore-cache
- run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
- run: source $BASH_ENV
- run:
name: Run cargo release build
command: cargo build --release --no-default-features --features pairing

workflows:
version: 2.1
Expand All @@ -131,10 +213,25 @@ workflows:
- clippy:
requires:
- cargo_fetch
- test_x86_64-unknown-linux-gnu:

- test_pairing_x86_64-unknown-linux-gnu:
requires:
- cargo_fetch
- test_pairing_gpu_x86_64-unknown-linux-gnu:
requires:
- cargo_fetch

- test_blst_x86_64-unknown-linux-gnu:
requires:
- cargo_fetch
- test_blst_gpu_x86_64-unknown-linux-gnu:
requires:
- cargo_fetch

- build_blst:
requires:
- cargo_fetch
- build:
- build_pairing:
requires:
- cargo_fetch


30 changes: 19 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,44 @@ bit-vec = "0.6"
blake2s_simd = "0.5"
ff = { version = "0.2.0", package = "fff" }
groupy = "0.3.1"
num_cpus = { version = "1", optional = true }
paired = { version = "0.20.0", optional = true }
rand_core = "0.5"
byteorder = "1"
log = "0.4.8"
lazy_static = "1.4.0"
ocl = { version = "0.19.4", package = "fil-ocl", optional = true }
ff-cl-gen = { version = "0.1.1", optional = true }
fs2 = { version = "0.4.3", optional = true }
rand = "0.7"
rayon = "1.3.0"
memmap = "0.7.0"
thiserror = "1.0.10"
ahash = "0.3.4"
ahash = "0.4.5"

blstrs = { git = "https://github.com/dignifiedquire/blstrs", optional = true }
paired = { version = "0.20.1", optional = true }

num_cpus = { version = "1", optional = true }
crossbeam-channel = { version = "0.4.4", optional = true }

ocl = { version = "0.19.4", package = "fil-ocl", optional = true }
ff-cl-gen = { version = "0.1.1", optional = true }
fs2 = { version = "0.4.3", optional = true }

[dev-dependencies]
hex-literal = "0.2"
hex-literal = "0.3"
rand_xorshift = "0.2"
sha2 = "0.8"
sha2 = "0.9"
env_logger = "0.7.1"
criterion = "0.3.2"

[features]
default = ["groth16", "multicore"]
default = ["groth16", "multicore", "pairing"]
gpu = ["ocl", "ff-cl-gen", "fs2"]
groth16 = ["paired"]
groth16 = []
multicore = ["crossbeam-channel", "num_cpus"]

blst = ["blstrs", "groth16", "multicore"]
blst-serde = ["blstrs/serde"]
pairing = ["paired", "groth16", "multicore"]
pairing-serde = ["paired/serde"]

[[test]]
name = "mimc"
path = "tests/mimc.rs"
Expand All @@ -53,7 +62,6 @@ required-features = ["groth16"]
maintenance = { status = "actively-developed" }



[[bench]]
name = "lc"
harness = false
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
and primitive structures, as well as basic gadget implementations such as
booleans and number abstractions.

## Backend

There are currently two backends available for the implementation of Bls12 381,
- [`paired`](https://github.com/filecoin-project/paired) - pure Rust implementation
- [`blstrs`](https://github.com/filecoin-project/blstrs) - optimized with hand tuned assembly, using [blst](https://github.com/supranational/blst)

they can be selected using `pairing` or `blst` features.
The default for now is `pairing`, as the secure and audited choice.

## GPU

This fork contains GPU parallel acceleration to the FFT and Multiexponentation algorithms in the groth16 prover codebase under a conditional compilation feature `#[cfg(feature = "gpu")]` and `gpu-test` for testing.
Expand Down
3 changes: 1 addition & 2 deletions benches/lc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use bellperson::{Index, LinearCombination, Variable};
use bellperson::{bls::Bls12, Index, LinearCombination, Variable};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use ff::{Field, ScalarEngine};
use paired::bls12_381::Bls12;

fn lc_benchmark(c: &mut Criterion) {
c.bench_function("LinearCombination::add((Fr, Variable))", |b| {
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2020-01-08
1.46.0
15 changes: 15 additions & 0 deletions src/bls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[cfg(feature = "blst")]
pub use blstrs::{
Bls12, Engine, Fp as Fq, Fp12 as Fq12, Fp2 as Fq2, FpRepr as FqRepr, G1Affine, G1Compressed,
G1Projective, G1Uncompressed, G2Affine, G2Compressed, G2Prepared, G2Projective, G2Uncompressed,
PairingCurveAffine, Scalar as Fr, ScalarRepr as FrRepr,
};

#[cfg(feature = "pairing")]
pub use paired::{
bls12_381::{
Bls12, Fq, Fq12, Fq2, FqRepr, Fr, FrRepr, G1Affine, G1Compressed, G1Uncompressed, G2Affine,
G2Compressed, G2Prepared, G2Uncompressed, G1 as G1Projective, G2 as G2Projective,
},
Engine, PairingCurveAffine,
};
Loading

0 comments on commit 118b481

Please sign in to comment.