diff --git a/.circleci/config.yml b/.circleci/config.yml index e203f0b5..f510fe3d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -26,12 +26,21 @@ commands: - *restore-workspace - *restore-cache - run: - name: Test (<< parameters.target >>) + name: Test (pairing) (<< parameters.target >>) command: TARGET=<< parameters.target >> cargo test --release no_output_timeout: 5m - run: - name: Test (GPU) (<< parameters.target >>) - command: TARGET=<< parameters.target >> cargo test --release --features gpu -- --test-threads=1 + name: Test (blst) (<< parameters.target >>) + command: TARGET=<< parameters.target >> cargo test --no-default-features --features blst --release -- --test-threads=1 + no_output_timeout: 5m + - run: + name: Test (pairing, GPU) (<< parameters.target >>) + command: TARGET=<< parameters.target >> cargo test --release --features gpu -- --test-threads=1 + no_output_timeout: 30m + + - run: + name: Test (pairing, GPU) (<< parameters.target >>) + command: TARGET=<< parameters.target >> cargo test --release --no-default-features --features blst,gpu no_output_timeout: 30m jobs: @@ -59,6 +68,7 @@ jobs: - run: cargo fetch - run: rustup install $(cat rust-toolchain) - run: rustup default $(cat rust-toolchain) + - run: rustup install nightly - run: rustup component add rustfmt-preview - run: rustup component add clippy-preview - run: rustc --version @@ -124,8 +134,11 @@ jobs: - run: apt-cache search opencl - run: sudo apt install -y ocl-icd-opencl-dev - run: - name: Run cargo clippy - command: cargo clippy --all-features + name: Run cargo clippy (pairing) + command: cargo clippy --no-default-features --features pairing,gpu + - run: + name: Run cargo clippy (blst) + command: cargo clippy --no-default-features --features blst,gpu build: executor: default @@ -134,9 +147,38 @@ jobs: - *restore-cache - 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 + - run: + name: Run cargo release build (pairing) + command: cargo build --release --no-default-features --features pairing + - run: + name: Run cargo release build (blst) + command: cargo build --release --no-default-features --features blst - run: - name: Run cargo release build - command: cargo build --release + name: Run cargo release build (pairing, gpu) + command: cargo build --release --no-default-features --features pairing,gpu + - run: + name: Run cargo release build (blst, gpu) + command: cargo build --release --no-default-features --features blst,gpu + + build_gbench: + executor: default + steps: + - *restore-workspace + - *restore-cache + - 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 + - run: + name: Run cargo release build (pairing, gpu) + command: cargo +nightly build -Zpackage-features --release -p gbench --no-default-features --features pairing,gpu + - run: + name: Run cargo release build (blst, gpu) + command: cargo +nightly build -Zpackage-features --release -p gbench --no-default-features --features blst,gpu benches: executor: default @@ -149,8 +191,11 @@ jobs: - run: apt-cache search opencl - run: sudo apt install -y ocl-icd-opencl-dev - run: - name: Run cargo build --benches - command: cargo build --benches --all-features --all --release + name: Run cargo build --benches (pairing) + command: cargo build --benches --no-default-features --features pairing --release + - run: + name: Run cargo build --benches (blst) + command: cargo build --benches --no-default-features --features blst --release workflows: version: 2.1 @@ -173,6 +218,9 @@ workflows: - build: requires: - cargo_fetch + - build_gbench: + requires: + - cargo_fetch - benches: requires: - cargo_fetch diff --git a/Cargo.toml b/Cargo.toml index 96f42fa7..ad063a4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,26 +9,24 @@ repository = "https://github.com/porcuquine/poseidon" [dependencies] lazy_static = "1.4.0" -bellperson = "0.9.2" +bellperson = { git = "https://github.com/filecoin-project/bellperson", branch = "blstrs", default-features = false } blake2s_simd = "0.5" byteorder = "1" ff = { version = "0.2.1", package = "fff" } -generic-array = "0.13.2" -paired = "0.20.0" +generic-array = "0.14.4" triton = { version = "2.0.0", package = "neptune-triton", default-features = false, features = ["opencl"], optional = true } - log = "0.4.8" [dev-dependencies] criterion = "0.3" rand = "0.7.0" -sha2 = "0.8" +sha2 = "0.9" tempdir = "0.3" rand_xorshift = "0.2.0" serde_json = "1.0.53" [build-dependencies] -paired = "0.20.0" +bellperson = { git = "https://github.com/filecoin-project/bellperson", branch = "blstrs", default-features = false } ff = { version = "0.2.1", package = "fff" } [[bench]] @@ -44,4 +42,12 @@ incremental = false codegen-units = 1 [features] +default = ["pairing"] gpu = ["triton"] +pairing = ["bellperson/pairing"] +blst = ["bellperson/blst"] + +[workspace] +members = [ + "gbench", +] \ No newline at end of file diff --git a/benches/hash.rs b/benches/hash.rs index f3548715..7026bb83 100644 --- a/benches/hash.rs +++ b/benches/hash.rs @@ -1,9 +1,9 @@ +use bellperson::bls::{Bls12, Fr}; use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use ff::PrimeField; use generic_array::typenum; use neptune::poseidon::{HashMode, PoseidonConstants}; use neptune::*; -use paired::bls12_381::{Bls12, Fr}; use rand::rngs::OsRng; use rand::seq::SliceRandom; use sha2::{Digest, Sha256, Sha512}; @@ -24,19 +24,18 @@ where BenchmarkId::new("Sha2 256", "Generated scalars"), &scalars, |b, s| { + let mut h = Sha256::new(); b.iter(|| { - let mut h = Sha256::new(); - std::iter::repeat(()) .take(A::to_usize()) .map(|_| s.choose(&mut OsRng).unwrap()) .for_each(|scalar| { for val in scalar.into_repr().as_ref() { - h.input(&val.to_le_bytes()); + h.update(&val.to_le_bytes()); } }); - h.result(); + h.finalize_reset() }) }, ); @@ -45,19 +44,19 @@ where BenchmarkId::new("Sha2 512", "Generated scalars"), &scalars, |b, s| { - b.iter(|| { - let mut h = Sha512::new(); + let mut h = Sha512::new(); + b.iter(|| { std::iter::repeat(()) .take(A::to_usize()) .map(|_| s.choose(&mut OsRng).unwrap()) .for_each(|scalar| { for val in scalar.into_repr().as_ref() { - h.input(&val.to_le_bytes()); + h.update(&val.to_le_bytes()); } }); - h.result(); + h.finalize_reset() }) }, ); diff --git a/benches/synthesis.rs b/benches/synthesis.rs index df2c1f61..f860c174 100644 --- a/benches/synthesis.rs +++ b/benches/synthesis.rs @@ -1,4 +1,5 @@ use crate::poseidon::{Arity, PoseidonConstants}; +use bellperson::bls::{Bls12, Fr}; use bellperson::gadgets::num::AllocatedNum; use bellperson::util_cs::bench_cs::BenchCS; use bellperson::{Circuit, ConstraintSystem, SynthesisError}; @@ -7,7 +8,6 @@ use ff::Field; use generic_array::typenum; use neptune::circuit::poseidon_hash; use neptune::*; -use paired::bls12_381::{Bls12, Fr}; use rand::thread_rng; use std::marker::PhantomData; diff --git a/gbench/Cargo.toml b/gbench/Cargo.toml index 842179ad..120b487b 100644 --- a/gbench/Cargo.toml +++ b/gbench/Cargo.toml @@ -8,12 +8,18 @@ license = "MIT OR Apache-2.0" [dependencies] lazy_static = "1.4.0" -bellperson = "0.6.3" +bellperson = { git = "https://github.com/filecoin-project/bellperson", branch = "blstrs", default-features = false } blake2s_simd = "0.5" byteorder = "1" env_logger = "0.7.1" ff = { version = "0.2.1", package = "fff" } -generic-array = "0.13.2" +generic-array = "0.14.4" log = "0.4.8" -paired = "0.20.0" -neptune = { path = "../", features=["gpu"] } +neptune = { path = "../", default-features = false } + +[features] +default = ["pairing", "gpu"] +gpu = ["neptune/gpu"] +pairing = ["neptune/pairing", "bellperson/pairing"] +blst = ["neptune/blst", "bellperson/blst"] + diff --git a/gbench/src/bin/gbench.rs b/gbench/src/main.rs similarity index 99% rename from gbench/src/bin/gbench.rs rename to gbench/src/main.rs index 7d5831c5..4e5d76c5 100644 --- a/gbench/src/bin/gbench.rs +++ b/gbench/src/main.rs @@ -1,3 +1,4 @@ +use bellperson::bls::Fr; use ff::Field; use generic_array::sequence::GenericSequence; use generic_array::typenum::{U11, U8}; @@ -7,7 +8,6 @@ use neptune::batch_hasher::BatcherType; use neptune::column_tree_builder::{ColumnTreeBuilder, ColumnTreeBuilderTrait}; use neptune::error::Error; use neptune::BatchHasher; -use paired::bls12_381::Fr; use std::result::Result; use std::thread; use std::time::Instant; @@ -57,7 +57,7 @@ fn bench_column_building( let _ = builder.add_columns(columns.as_slice()).unwrap(); total_columns += columns.len(); } - println!(""); + println!(); let final_columns: Vec<_> = (0..leaves - total_columns) .map(|_| GenericArray::::generate(|_| constant_element)) diff --git a/rust-toolchain b/rust-toolchain index 3987c472..0a3db35b 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -1.43.1 +1.46.0 diff --git a/src/batch_hasher.rs b/src/batch_hasher.rs index 7b36b8eb..dbd70990 100644 --- a/src/batch_hasher.rs +++ b/src/batch_hasher.rs @@ -6,8 +6,8 @@ use crate::cl; use crate::error::Error; use crate::poseidon::SimplePoseidonBatchHasher; use crate::{Arity, BatchHasher, Strength, DEFAULT_STRENGTH}; +use bellperson::bls::Fr; use generic_array::GenericArray; -use paired::bls12_381::Fr; use std::marker::PhantomData; use triton::FutharkContext; diff --git a/src/circuit.rs b/src/circuit.rs index 127d2955..9ac830c5 100644 --- a/src/circuit.rs +++ b/src/circuit.rs @@ -604,10 +604,10 @@ mod tests { use super::*; use crate::poseidon::HashMode; use crate::{scalar_from_u64, Poseidon, Strength}; + use bellperson::bls::{Bls12, Fr}; use bellperson::util_cs::test_cs::TestConstraintSystem; use bellperson::ConstraintSystem; use generic_array::typenum; - use paired::bls12_381::{Bls12, Fr}; use rand::SeedableRng; use rand_xorshift::XorShiftRng; diff --git a/src/column_tree_builder.rs b/src/column_tree_builder.rs index 14dad850..e66c3f9d 100644 --- a/src/column_tree_builder.rs +++ b/src/column_tree_builder.rs @@ -5,9 +5,9 @@ use crate::error::Error; use crate::poseidon::{Poseidon, PoseidonConstants}; use crate::tree_builder::{TreeBuilder, TreeBuilderTrait}; use crate::{Arity, BatchHasher}; +use bellperson::bls::{Bls12, Fr}; use ff::Field; use generic_array::GenericArray; -use paired::bls12_381::{Bls12, Fr}; pub trait ColumnTreeBuilderTrait where @@ -167,10 +167,10 @@ mod tests { use super::*; use crate::poseidon::Poseidon; use crate::BatchHasher; + use bellperson::bls::Fr; use ff::Field; use generic_array::sequence::GenericSequence; use generic_array::typenum::{U11, U8}; - use paired::bls12_381::Fr; #[test] fn test_column_tree_builder() { diff --git a/src/gpu.rs b/src/gpu.rs index d84dc4c2..9edd65c2 100644 --- a/src/gpu.rs +++ b/src/gpu.rs @@ -4,9 +4,9 @@ use crate::error::Error; use crate::hash_type::HashType; use crate::poseidon::PoseidonConstants; use crate::{Arity, BatchHasher, Strength, DEFAULT_STRENGTH}; +use bellperson::bls::{Bls12, Fr, FrRepr}; use ff::{PrimeField, PrimeFieldDecodingError}; use generic_array::{typenum, ArrayLength, GenericArray}; -use paired::bls12_381::{Bls12, Fr, FrRepr}; use std::collections::HashMap; use std::marker::PhantomData; use std::sync::{Arc, Mutex}; diff --git a/src/hash_type.rs b/src/hash_type.rs index 45c91c30..c7a6c5ba 100644 --- a/src/hash_type.rs +++ b/src/hash_type.rs @@ -117,8 +117,8 @@ fn x_pow2>(coeff: u64, n: i32) -> Fr { mod tests { use super::*; use crate::{scalar_from_u64s, Strength}; + use bellperson::bls::{Bls12, Fr, FrRepr}; use generic_array::typenum::{U15, U8}; - use paired::bls12_381::{Bls12, Fr, FrRepr}; use std::collections::HashSet; #[test] diff --git a/src/lib.rs b/src/lib.rs index a4235927..fbd5c5c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,11 +5,11 @@ extern crate lazy_static; pub use crate::poseidon::{Arity, Poseidon}; use crate::round_constants::generate_constants; +pub use bellperson::bls::Fr as Scalar; +use bellperson::bls::FrRepr; pub use error::Error; use ff::{Field, PrimeField, ScalarEngine}; use generic_array::GenericArray; -pub use paired::bls12_381::Fr as Scalar; -use paired::bls12_381::FrRepr; /// Poseidon circuit pub mod circuit; diff --git a/src/matrix.rs b/src/matrix.rs index 94364cb0..3c5dbd1e 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -353,7 +353,7 @@ pub(crate) fn invert(matrix: &Matrix>) -> Option(m: &Matrix>) -> (Vec>, Vec where - A: Arity, + A: Arity, { constants: PoseidonConstants, max_batch_size: usize, @@ -540,7 +539,7 @@ where impl SimplePoseidonBatchHasher where - A: Arity, + A: Arity, { pub(crate) fn new(max_batch_size: usize) -> Result { Self::new_with_strength(DEFAULT_STRENGTH, max_batch_size) @@ -558,12 +557,9 @@ where } impl BatchHasher for SimplePoseidonBatchHasher where - A: Arity, + A: Arity, { - fn hash( - &mut self, - preimages: &[GenericArray], - ) -> Result, Error> { + fn hash(&mut self, preimages: &[GenericArray]) -> Result, Error> { Ok(preimages .iter() .map(|preimage| Poseidon::new_with_preimage(&preimage, &self.constants).hash()) @@ -579,9 +575,9 @@ where mod tests { use super::*; use crate::*; + use bellperson::bls::{Bls12, Fr}; use ff::Field; use generic_array::typenum; - use paired::bls12_381::{Bls12, Fr}; #[test] fn reset() { diff --git a/src/round_constants.rs b/src/round_constants.rs index 92584ea6..c7d3a91c 100644 --- a/src/round_constants.rs +++ b/src/round_constants.rs @@ -1,6 +1,6 @@ pub use crate::Error; +pub use bellperson::bls::Fr as Scalar; use ff::{PrimeField, PrimeFieldDecodingError, PrimeFieldRepr, ScalarEngine}; -pub use paired::bls12_381::Fr as Scalar; /// From the paper (): /// The round constants are generated using the Grain LFSR [23] in a self-shrinking @@ -207,7 +207,7 @@ fn bytes_into_fr(bytes: &[u8]) -> Result>(); diff --git a/src/tree_builder.rs b/src/tree_builder.rs index 0b783233..20ffc0d8 100644 --- a/src/tree_builder.rs +++ b/src/tree_builder.rs @@ -4,9 +4,9 @@ use crate::cl::GPUSelector; use crate::error::Error; use crate::poseidon::{Poseidon, PoseidonConstants}; use crate::{Arity, BatchHasher}; +use bellperson::bls::{Bls12, Fr}; use ff::Field; use generic_array::GenericArray; -use paired::bls12_381::{Bls12, Fr}; pub trait TreeBuilderTrait where @@ -250,9 +250,9 @@ where #[cfg(test)] mod tests { use super::*; + use bellperson::bls::Fr; use ff::Field; use generic_array::typenum::U8; - use paired::bls12_381::Fr; #[test] fn test_tree_builder() {