Commit 15295419 authored by Pierre Boutillier's avatar Pierre Boutillier
Browse files

Merge branch 'bump-up-bls' into 'master'

Bump up rustc-bls12-381

See merge request !1
parents a1051f87 f9cd7dcd
Pipeline #250922517 passed with stage
in 7 minutes and 21 seconds
*~
\ No newline at end of file
*~
target
\ No newline at end of file
variables:
RUST_VERSION: "1.44.0"
OCAML_VERSION: "4.09"
install-and-remove:
image: ocaml/opam
image: registry.gitlab.com/dannywillems/docker-ocaml-rust:${OCAML_VERSION}-${RUST_VERSION}
before_script:
- . $HOME/.cargo/env
- opam --version
- ocaml --version
- cargo --version
- rustc --version
script:
- opam pin --no-action tezos-rust-libs .
- opam depext tezos-rust-libs
......
This diff is collapsed.
/target
**/*.rs.bk
Cargo.lock
_build
.idea
.ccls-cache
.run-test: &run-test
stage: test
before_script:
- rustc --version
- cargo --version
# wasm-pack
- curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- rustup target install wasm32-unknown-unknown
script:
# Normal compilation
- cargo build
# Run the test
- cargo test --verbose
# Build with wasm-bindgen
- wasm-pack build --target nodejs -- --features=wasm
- wasm-pack build --target no-modules -- --features=wasm
- wasm-pack build --target web -- --features=wasm
- wasm-pack build --target bundler -- --features=wasm
.run-lint: &run-lint
stage: lint
before_script:
- rustc --version
- cargo --version
- rustup component add rustfmt
script:
- rustfmt --check src/*.rs
stages:
- lint
- check-c-header
- test
- publish
lint:
image: 'rust:1.40.0'
<<: *run-lint
check-header-syntax-gcc-10.1.0:
image: 'gcc:10.1.0'
stage: check-c-header
before_script:
- gcc --version
script:
- gcc -fsyntax-only -Wall -Wextra -Wno-deprecated include/*.h
check-header-syntax-gcc-9.3.0:
image: 'gcc:9.3.0'
stage: check-c-header
before_script:
- gcc --version
script:
- gcc -fsyntax-only -Wall -Wextra -Wno-deprecated include/*.h
check-header-syntax-gcc-9:
image: 'gcc:9'
stage: check-c-header
before_script:
- gcc --version
script:
- gcc -fsyntax-only -Wall -Wextra -Wno-deprecated include/*.h
check-header-syntax-gcc-8.4.0:
image: 'gcc:8.4.0'
stage: check-c-header
before_script:
- gcc --version
script:
- gcc -fsyntax-only -Wall -Wextra -Wno-deprecated include/*.h
# Not working anymore because of wasm-bindgen
# test-1.34.0:
# image: 'rust:1.34.0'
# <<: *run-test
# test-1.36.0:
# image: 'rust:1.36.0'
# <<: *run-test
test-1.39.0:
image: 'rust:1.39.0'
<<: *run-test
test-1.40.0:
image: 'rust:1.40.0'
<<: *run-test
test-1.41.0:
image: 'rust:1.41.0'
<<: *run-test
test-1.42.0:
image: 'rust:1.42.0'
<<: *run-test
test-1.43.0:
image: 'rust:1.43.0'
<<: *run-test
test-1.44.0:
image: 'rust:1.44.0'
<<: *run-test
test-1.45.0:
image: 'rust:1.45.0'
<<: *run-test
test-1.46.0:
image: 'rust:1.46.0'
<<: *run-test
npm-bundler-publish:
image: 'rust:1.46.0'
stage: publish
before_script:
- apt-get update -y
- apt-get install jq -y
- rustc --version
- cargo --version
# wasm-pack
- curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- rustup target install wasm32-unknown-unknown
# Install NVM to publish the package
- curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
- export NVM_DIR="$HOME/.nvm"
- . "$NVM_DIR/nvm.sh"
- nvm install 12.16.1
- nvm use 12.16.1
script:
# Build with wasm-bindgen for bundler
- wasm-pack build --out-dir npm-bundler --target bundler --scope dannywillems --release -- --features="wasm,wee_alloc"
- cd npm-bundler
- echo "//gitlab.com/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${NPM_TOKEN}" > .npmrc
- echo "//gitlab.com/api/v4/packages/npm/:_authToken=${NPM_TOKEN}" >> .npmrc
- echo "@dannywillems:registry=https://gitlab.com/api/v4/packages/npm/" >> .npmrc
- echo -e "\nexport { wasm as __wasm };" >> rustc_bls12_381.js
- >
jq --arg id "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/packages/npm/" '. + {"publishConfig": {"@dannywillems:registry": $id}}' < package.json > package.json.new
- mv package.json.new package.json
- >
jq '.files += ["rustc_bls12_381_bg.js"]' < package.json > package.json.new
- mv package.json.new package.json
- cat package.json
- npm publish
only:
- tags
npm-nodejs-publish:
image: 'rust:1.46.0'
stage: publish
before_script:
- apt-get update -y
- apt-get install jq -y
- rustc --version
- cargo --version
# wasm-pack
- curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- rustup target install wasm32-unknown-unknown
# Install NVM to publish the package
- curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
- export NVM_DIR="$HOME/.nvm"
- . "$NVM_DIR/nvm.sh"
- nvm install 12.16.1
- nvm use 12.16.1
script:
# Build with wasm-bindgen for bundler
- wasm-pack build --out-dir nodejs --target nodejs --scope dannywillems --release -- --features="wasm,wee_alloc"
- cd nodejs
- echo "//gitlab.com/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${NPM_TOKEN}" > .npmrc
- echo "//gitlab.com/api/v4/packages/npm/:_authToken=${NPM_TOKEN}" >> .npmrc
- echo "@dannywillems:registry=https://gitlab.com/api/v4/packages/npm/" >> .npmrc
# Overwrite the module name
- >
jq '.name = "@dannywillems/rustc-bls12-381-node"' < package.json > package.json.new
- mv package.json.new package.json
# Overwrite registry to publish
- >
jq --arg id "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/packages/npm/" '. + {"publishConfig": {"@dannywillems:registry": $id}}' < package.json > package.json.new
- mv package.json.new package.json
- cat package.json
- npm publish
only:
- tags
[package]
name = "rustc-bls12-381"
version = "0.7.2"
version = "0.8.1"
authors = ["Danny Willems <be.danny.willems@gmail.com>"]
edition = "2018"
[lib]
name = "rustc_bls12_381"
path = "src/lib.rs"
crate-type = ["staticlib"]
crate-type = ["staticlib", "cdylib", "rlib"]
[dependencies]
ff = "0.6"
pairing = "0.16"
group = "0.6.0"
libc = "0.2"
ff = "=0.6"
pairing = "=0.16"
group = "=0.6.0"
libc = "=0.2"
# Looks like it is required by ff and pairing
rand = "0.7"
rand_core = "0.5.1"
rand = { version = "=0.7", features = ["wasm-bindgen"] }
rand_core = "=0.5.1"
# 0.2.0 fails to build
getrandom = { version = "=0.1.15", features = ["wasm-bindgen"] }
console_error_panic_hook = { version = "0.1.6", optional = true }
wasm-bindgen = { version = "=0.2.67", optional = true }
wee_alloc = {version = "*", optional = true }
[features]
wasm = ["wasm-bindgen", "wee_alloc"]
debug = ["console_error_panic_hook"]
\ No newline at end of file
......@@ -35,3 +35,15 @@ C arrays. The results can be used to get the overhead of the binding.
```
cargo +nightly bench
```
## WASM support
Code can be compiled to wasm to be used in the browser using wasm-pack
```shell
wasm-pack build -- --features wasm
```
The default output will be to used with the bundler `webpack`.
You would also need to get access to the wasm memory of the module. You can add `export { wasm }` because it is not exported by default with the webpack target:
```shell
echo "\nexport { wasm };" >> pkg/rustc_bls12_381.js
```
#![feature(test)]
extern crate test;
extern crate rand;
extern crate rand_core;
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
use ff::Field;
use group::{CurveAffine, CurveProjective, EncodedPoint};
use pairing::bls12_381;
use pairing::bls12_381::Fr;
use pairing::bls12_381::{G1Compressed, G1Uncompressed};
fn generate_uncompressed_random_element() -> G1Uncompressed {
let mut random_gen = rand::thread_rng();
let random_g1 = bls12_381::G1::random(&mut random_gen);
let uncompressed_form = bls12_381::G1Uncompressed::from_affine(random_g1.into_affine());
uncompressed_form
}
fn generate_compressed_random_element() -> G1Compressed {
let mut random_gen = rand::thread_rng();
let random_g1 = bls12_381::G1::random(&mut random_gen);
let compressed_form = bls12_381::G1Compressed::from_affine(random_g1.into_affine());
compressed_form
}
fn generate_random_fr_element() -> Fr {
let mut random_gen = rand::thread_rng();
Fr::random(&mut random_gen)
}
// --------------------- G1 Uncompressed ---------------------------
#[bench]
fn bench_uncompressed_generate_zero(b: &mut Bencher) {
b.iter(|| {
let zero = bls12_381::G1Affine::zero();
let uncompressed_form = bls12_381::G1Uncompressed::from_affine(zero);
uncompressed_form
})
}
#[bench]
fn bench_uncompressed_generate_one(b: &mut Bencher) {
b.iter(|| {
let r = bls12_381::G1Affine::one();
let uncompressed_form = bls12_381::G1Uncompressed::from_affine(r);
uncompressed_form
})
}
#[bench]
fn bench_uncompressed_generate_random(b: &mut Bencher) {
b.iter(|| generate_uncompressed_random_element())
}
#[bench]
fn bench_uncompressed_check_if_zero_on_pregenerated_zero(b: &mut Bencher) {
let g = bls12_381::G1Uncompressed::from_affine(bls12_381::G1Affine::zero());
b.iter(|| {
let g = g.into_affine_unchecked().unwrap();
g.is_zero()
})
}
#[bench]
fn bench_uncompressed_check_if_zero_on_pregenerated_one(b: &mut Bencher) {
let g = bls12_381::G1Uncompressed::from_affine(bls12_381::G1Affine::one());
b.iter(|| {
let g = g.into_affine_unchecked().unwrap();
g.is_zero()
})
}
#[bench]
fn bench_uncompressed_check_if_zero_on_pregenerated_random_element(b: &mut Bencher) {
let g = generate_uncompressed_random_element();
b.iter(|| {
let g = g.into_affine_unchecked().unwrap();
g.is_zero()
})
}
#[bench]
fn bench_uncompressed_add_unchecked_conversion_from_uncompressed_to_affine(b: &mut Bencher) {
let g1 = generate_uncompressed_random_element();
let g2 = generate_uncompressed_random_element();
b.iter(|| {
let mut g1 = g1.into_affine_unchecked().unwrap().into_projective();
let g2 = g2.into_affine_unchecked().unwrap().into_projective();
g1.add_assign(&g2);
bls12_381::G1Uncompressed::from_affine(g1.into_affine())
})
}
#[bench]
fn bench_uncompressed_add_checked_conversion_from_uncompressed_to_affine(b: &mut Bencher) {
let g1 = generate_uncompressed_random_element();
let g2 = generate_uncompressed_random_element();
b.iter(|| {
let mut g1 = g1.into_affine().unwrap().into_projective();
let g2 = g2.into_affine().unwrap().into_projective();
g1.add_assign(&g2);
bls12_381::G1Uncompressed::from_affine(g1.into_affine())
})
}
#[bench]
fn bench_uncompressed_opposite_of_pregenerated_random_element(b: &mut Bencher) {
let g = generate_uncompressed_random_element();
b.iter(|| {
let mut g = g.into_affine_unchecked().unwrap();
g.negate();
bls12_381::G1Uncompressed::from_affine(g)
})
}
#[bench]
fn bench_uncompressed_negate_of_pregenerated_zero_element(b: &mut Bencher) {
let g = bls12_381::G1Uncompressed::from_affine(bls12_381::G1Affine::zero());
b.iter(|| {
let mut g = g.into_affine_unchecked().unwrap();
g.negate();
bls12_381::G1Uncompressed::from_affine(g)
})
}
#[bench]
fn bench_uncompressed_negate_of_pregenerated_one_element(b: &mut Bencher) {
let g = bls12_381::G1Uncompressed::from_affine(bls12_381::G1Affine::one());
b.iter(|| {
let mut g = g.into_affine_unchecked().unwrap();
g.negate();
bls12_381::G1Uncompressed::from_affine(g)
})
}
#[bench]
fn bench_uncompressed_eq_with_pregenerated_random_elements(b: &mut Bencher) {
let g1 = generate_uncompressed_random_element();
let g2 = generate_uncompressed_random_element();
b.iter(|| {
let g1 = g1.into_affine_unchecked().unwrap();
let g2 = g2.into_affine_unchecked().unwrap();
g1 == g2
})
}
#[bench]
fn bench_uncompressed_eq_with_pregenerated_random_element_and_zero(b: &mut Bencher) {
let g1 = generate_uncompressed_random_element();
let g2 = bls12_381::G1Uncompressed::from_affine(bls12_381::G1Affine::zero());
b.iter(|| {
let g1 = g1.into_affine_unchecked().unwrap();
let g2 = g2.into_affine_unchecked().unwrap();
g1 == g2
})
}
#[bench]
fn bench_uncompressed_eq_with_pregenerated_random_element_and_one(b: &mut Bencher) {
let g1 = generate_uncompressed_random_element();
let g2 = bls12_381::G1Uncompressed::from_affine(bls12_381::G1Affine::one());
b.iter(|| {
let g1 = g1.into_affine_unchecked().unwrap();
let g2 = g2.into_affine_unchecked().unwrap();
g1 == g2
})
}
#[bench]
fn bench_uncompressed_mul_with_pregenerated_random_elements(b: &mut Bencher) {
let g = generate_uncompressed_random_element();
let a = generate_random_fr_element();
b.iter(|| {
let mut g = g.into_affine_unchecked().unwrap().into_projective();
g.mul_assign(a);
bls12_381::G1Uncompressed::from_affine(g.into_affine());
})
}
#[bench]
fn bench_from_uncompressed_to_projective_with_unchecked_affine_conversion(b: &mut Bencher) {
let g = generate_uncompressed_random_element();
b.iter(|| {
g.into_affine_unchecked().unwrap().into_projective();
})
}
#[bench]
fn bench_from_uncompressed_to_projective_with_checked_affine_conversion(b: &mut Bencher) {
let g = generate_uncompressed_random_element();
b.iter(|| {
g.into_affine().unwrap().into_projective();
})
}
#[bench]
fn bench_from_uncompressed_to_affine(b: &mut Bencher) {
let g = generate_uncompressed_random_element();
b.iter(|| {
g.into_affine().unwrap();
})
}
#[bench]
fn bench_from_uncompressed_to_unchecked_affine(b: &mut Bencher) {
let g = generate_uncompressed_random_element();
b.iter(|| {
g.into_affine_unchecked().unwrap();
})
}
#[bench]
fn bench_from_affine_to_projective(b: &mut Bencher) {
let g = generate_uncompressed_random_element()
.into_affine()
.unwrap();
b.iter(|| {
g.into_projective();
})
}
#[bench]
fn bench_from_projective_to_affine(b: &mut Bencher) {
let g = generate_uncompressed_random_element()
.into_affine()
.unwrap()
.into_projective();
b.iter(|| {
g.into_affine();
})
}
// --------------------- G1 Compressed ---------------------------
#[bench]
fn bench_compressed_add(b: &mut Bencher) {
let g1 = generate_compressed_random_element();
let g2 = generate_compressed_random_element();
b.iter(|| {
// First we must convert in projective, add, and convert back into
// affine to get the uncompressed form.
let mut g1 = g1.into_affine_unchecked().unwrap().into_projective();
let g2 = g2.into_affine_unchecked().unwrap().into_projective();
g1.add_assign(&g2);
bls12_381::G1Compressed::from_affine(g1.into_affine())
})
}
#[bench]
fn bench_compressed_opposite_of_pregenerated_random_element(b: &mut Bencher) {
let g = generate_compressed_random_element();
b.iter(|| {
let mut g = g.into_affine_unchecked().unwrap();
g.negate();
bls12_381::G1Compressed::from_affine(g)
})
}
#[bench]
fn bench_compressed_negate_of_pregenerated_zero_element(b: &mut Bencher) {
let g = bls12_381::G1Compressed::from_affine(bls12_381::G1Affine::zero());
b.iter(|| {
let mut g = g.into_affine_unchecked().unwrap();
g.negate();
bls12_381::G1Compressed::from_affine(g)
})
}
#[bench]
fn bench_compressed_negate_of_pregenerated_one_element(b: &mut Bencher) {
let g = bls12_381::G1Compressed::from_affine(bls12_381::G1Affine::one());
b.iter(|| {
let mut g = g.into_affine_unchecked().unwrap();
g.negate();
bls12_381::G1Compressed::from_affine(g)
})
}
#[bench]
fn bench_compressed_check_if_zero_on_pregenerated_zero(b: &mut Bencher) {
let g = bls12_381::G1Compressed::from_affine(bls12_381::G1Affine::zero());
b.iter(|| {
let g = g.into_affine_unchecked().unwrap();
g.is_zero()
})
}
#[bench]
fn bench_compressed_check_if_zero_on_pregenerated_one(b: &mut Bencher) {
let g = bls12_381::G1Compressed::from_affine(bls12_381::G1Affine::one());
b.iter(|| {
let g = g.into_affine_unchecked().unwrap();
g.is_zero()
})
}
#[bench]
fn bench_compressed_check_if_zero_on_pregenerated_random_element(b: &mut Bencher) {
let g = generate_compressed_random_element();
b.iter(|| {
let g = g.into_affine_unchecked().unwrap();
g.is_zero()
})
}