Auto merge of #82 - str4d:read-write-le, r=ebfull

Add read_le and write_le to PrimeFieldRepr
This commit is contained in:
bmerge 2018-05-17 17:55:18 +00:00
commit 09b6e6f921
20 changed files with 271 additions and 183 deletions

@ -2,7 +2,7 @@
name = "pairing" name = "pairing"
# Remember to change version string in README.md. # Remember to change version string in README.md.
version = "0.14.1" version = "0.14.2"
authors = ["Sean Bowe <ewillbefull@gmail.com>"] authors = ["Sean Bowe <ewillbefull@gmail.com>"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
@ -14,7 +14,7 @@ repository = "https://github.com/ebfull/pairing"
[dependencies] [dependencies]
rand = "0.4" rand = "0.4"
byteorder = "1" byteorder = "1"
clippy = { version = "0.0.190", optional = true } clippy = { version = "0.0.200", optional = true }
[features] [features]
unstable-features = ["expose-arith"] unstable-features = ["expose-arith"]

@ -1,8 +1,8 @@
mod g1 { mod g1 {
use rand::{Rand, SeedableRng, XorShiftRng}; use rand::{Rand, SeedableRng, XorShiftRng};
use pairing::CurveProjective;
use pairing::bls12_381::*; use pairing::bls12_381::*;
use pairing::CurveProjective;
#[bench] #[bench]
fn bench_g1_mul_assign(b: &mut ::test::Bencher) { fn bench_g1_mul_assign(b: &mut ::test::Bencher) {
@ -10,7 +10,9 @@ mod g1 {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(G1, Fr)> = (0..SAMPLES).map(|_| (G1::rand(&mut rng), Fr::rand(&mut rng))).collect(); let v: Vec<(G1, Fr)> = (0..SAMPLES)
.map(|_| (G1::rand(&mut rng), Fr::rand(&mut rng)))
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -27,7 +29,9 @@ mod g1 {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(G1, G1)> = (0..SAMPLES).map(|_| (G1::rand(&mut rng), G1::rand(&mut rng))).collect(); let v: Vec<(G1, G1)> = (0..SAMPLES)
.map(|_| (G1::rand(&mut rng), G1::rand(&mut rng)))
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -44,7 +48,9 @@ mod g1 {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(G1, G1Affine)> = (0..SAMPLES).map(|_| (G1::rand(&mut rng), G1::rand(&mut rng).into())).collect(); let v: Vec<(G1, G1Affine)> = (0..SAMPLES)
.map(|_| (G1::rand(&mut rng), G1::rand(&mut rng).into()))
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -59,8 +65,8 @@ mod g1 {
mod g2 { mod g2 {
use rand::{Rand, SeedableRng, XorShiftRng}; use rand::{Rand, SeedableRng, XorShiftRng};
use pairing::CurveProjective;
use pairing::bls12_381::*; use pairing::bls12_381::*;
use pairing::CurveProjective;
#[bench] #[bench]
fn bench_g2_mul_assign(b: &mut ::test::Bencher) { fn bench_g2_mul_assign(b: &mut ::test::Bencher) {
@ -68,7 +74,9 @@ mod g2 {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(G2, Fr)> = (0..SAMPLES).map(|_| (G2::rand(&mut rng), Fr::rand(&mut rng))).collect(); let v: Vec<(G2, Fr)> = (0..SAMPLES)
.map(|_| (G2::rand(&mut rng), Fr::rand(&mut rng)))
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -85,7 +93,9 @@ mod g2 {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(G2, G2)> = (0..SAMPLES).map(|_| (G2::rand(&mut rng), G2::rand(&mut rng))).collect(); let v: Vec<(G2, G2)> = (0..SAMPLES)
.map(|_| (G2::rand(&mut rng), G2::rand(&mut rng)))
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -102,7 +112,9 @@ mod g2 {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(G2, G2Affine)> = (0..SAMPLES).map(|_| (G2::rand(&mut rng), G2::rand(&mut rng).into())).collect(); let v: Vec<(G2, G2Affine)> = (0..SAMPLES)
.map(|_| (G2::rand(&mut rng), G2::rand(&mut rng).into()))
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {

@ -1,7 +1,7 @@
use rand::{Rand, SeedableRng, XorShiftRng}; use rand::{Rand, SeedableRng, XorShiftRng};
use pairing::{Field, PrimeField, PrimeFieldRepr, SqrtField};
use pairing::bls12_381::*; use pairing::bls12_381::*;
use pairing::{Field, PrimeField, PrimeFieldRepr, SqrtField};
#[bench] #[bench]
fn bench_fq_repr_add_nocarry(b: &mut ::test::Bencher) { fn bench_fq_repr_add_nocarry(b: &mut ::test::Bencher) {
@ -9,16 +9,18 @@ fn bench_fq_repr_add_nocarry(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(FqRepr, FqRepr)> = (0..SAMPLES).map(|_| { let v: Vec<(FqRepr, FqRepr)> = (0..SAMPLES)
let mut tmp1 = FqRepr::rand(&mut rng); .map(|_| {
let mut tmp2 = FqRepr::rand(&mut rng); let mut tmp1 = FqRepr::rand(&mut rng);
// Shave a few bits off to avoid overflow. let mut tmp2 = FqRepr::rand(&mut rng);
for _ in 0..3 { // Shave a few bits off to avoid overflow.
tmp1.div2(); for _ in 0..3 {
tmp2.div2(); tmp1.div2();
} tmp2.div2();
(tmp1, tmp2) }
}).collect(); (tmp1, tmp2)
})
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -35,15 +37,17 @@ fn bench_fq_repr_sub_noborrow(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(FqRepr, FqRepr)> = (0..SAMPLES).map(|_| { let v: Vec<(FqRepr, FqRepr)> = (0..SAMPLES)
let tmp1 = FqRepr::rand(&mut rng); .map(|_| {
let mut tmp2 = tmp1; let tmp1 = FqRepr::rand(&mut rng);
// Ensure tmp2 is smaller than tmp1. let mut tmp2 = tmp1;
for _ in 0..10 { // Ensure tmp2 is smaller than tmp1.
tmp2.div2(); for _ in 0..10 {
} tmp2.div2();
(tmp1, tmp2) }
}).collect(); (tmp1, tmp2)
})
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -110,7 +114,9 @@ fn bench_fq_add_assign(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(Fq, Fq)> = (0..SAMPLES).map(|_| (Fq::rand(&mut rng), Fq::rand(&mut rng))).collect(); let v: Vec<(Fq, Fq)> = (0..SAMPLES)
.map(|_| (Fq::rand(&mut rng), Fq::rand(&mut rng)))
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -127,7 +133,9 @@ fn bench_fq_sub_assign(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(Fq, Fq)> = (0..SAMPLES).map(|_| (Fq::rand(&mut rng), Fq::rand(&mut rng))).collect(); let v: Vec<(Fq, Fq)> = (0..SAMPLES)
.map(|_| (Fq::rand(&mut rng), Fq::rand(&mut rng)))
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -144,7 +152,9 @@ fn bench_fq_mul_assign(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(Fq, Fq)> = (0..SAMPLES).map(|_| (Fq::rand(&mut rng), Fq::rand(&mut rng))).collect(); let v: Vec<(Fq, Fq)> = (0..SAMPLES)
.map(|_| (Fq::rand(&mut rng), Fq::rand(&mut rng)))
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -206,15 +216,17 @@ fn bench_fq_negate(b: &mut ::test::Bencher) {
#[bench] #[bench]
fn bench_fq_sqrt(b: &mut ::test::Bencher) { fn bench_fq_sqrt(b: &mut ::test::Bencher) {
const SAMPLES: usize = 1000; const SAMPLES: usize = 1000;
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<Fq> = (0..SAMPLES).map(|_| { let v: Vec<Fq> = (0..SAMPLES)
let mut tmp = Fq::rand(&mut rng); .map(|_| {
tmp.square(); let mut tmp = Fq::rand(&mut rng);
tmp tmp.square();
}).collect(); tmp
})
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -229,9 +241,7 @@ fn bench_fq_into_repr(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<Fq> = (0..SAMPLES).map(|_| { let v: Vec<Fq> = (0..SAMPLES).map(|_| Fq::rand(&mut rng)).collect();
Fq::rand(&mut rng)
}).collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -246,9 +256,9 @@ fn bench_fq_from_repr(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<FqRepr> = (0..SAMPLES).map(|_| { let v: Vec<FqRepr> = (0..SAMPLES)
Fq::rand(&mut rng).into_repr() .map(|_| Fq::rand(&mut rng).into_repr())
}).collect(); .collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {

@ -1,7 +1,7 @@
use rand::{Rand, SeedableRng, XorShiftRng}; use rand::{Rand, SeedableRng, XorShiftRng};
use pairing::Field;
use pairing::bls12_381::*; use pairing::bls12_381::*;
use pairing::Field;
#[bench] #[bench]
fn bench_fq12_add_assign(b: &mut ::test::Bencher) { fn bench_fq12_add_assign(b: &mut ::test::Bencher) {
@ -9,9 +9,9 @@ fn bench_fq12_add_assign(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(Fq12, Fq12)> = (0..SAMPLES).map(|_| { let v: Vec<(Fq12, Fq12)> = (0..SAMPLES)
(Fq12::rand(&mut rng), Fq12::rand(&mut rng)) .map(|_| (Fq12::rand(&mut rng), Fq12::rand(&mut rng)))
}).collect(); .collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -28,9 +28,9 @@ fn bench_fq12_sub_assign(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(Fq12, Fq12)> = (0..SAMPLES).map(|_| { let v: Vec<(Fq12, Fq12)> = (0..SAMPLES)
(Fq12::rand(&mut rng), Fq12::rand(&mut rng)) .map(|_| (Fq12::rand(&mut rng), Fq12::rand(&mut rng)))
}).collect(); .collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -47,9 +47,9 @@ fn bench_fq12_mul_assign(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(Fq12, Fq12)> = (0..SAMPLES).map(|_| { let v: Vec<(Fq12, Fq12)> = (0..SAMPLES)
(Fq12::rand(&mut rng), Fq12::rand(&mut rng)) .map(|_| (Fq12::rand(&mut rng), Fq12::rand(&mut rng)))
}).collect(); .collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -66,9 +66,7 @@ fn bench_fq12_squaring(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<Fq12> = (0..SAMPLES).map(|_| { let v: Vec<Fq12> = (0..SAMPLES).map(|_| Fq12::rand(&mut rng)).collect();
Fq12::rand(&mut rng)
}).collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -85,9 +83,7 @@ fn bench_fq12_inverse(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<Fq12> = (0..SAMPLES).map(|_| { let v: Vec<Fq12> = (0..SAMPLES).map(|_| Fq12::rand(&mut rng)).collect();
Fq12::rand(&mut rng)
}).collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {

@ -1,7 +1,7 @@
use rand::{Rand, SeedableRng, XorShiftRng}; use rand::{Rand, SeedableRng, XorShiftRng};
use pairing::{Field, SqrtField};
use pairing::bls12_381::*; use pairing::bls12_381::*;
use pairing::{Field, SqrtField};
#[bench] #[bench]
fn bench_fq2_add_assign(b: &mut ::test::Bencher) { fn bench_fq2_add_assign(b: &mut ::test::Bencher) {
@ -9,9 +9,9 @@ fn bench_fq2_add_assign(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(Fq2, Fq2)> = (0..SAMPLES).map(|_| { let v: Vec<(Fq2, Fq2)> = (0..SAMPLES)
(Fq2::rand(&mut rng), Fq2::rand(&mut rng)) .map(|_| (Fq2::rand(&mut rng), Fq2::rand(&mut rng)))
}).collect(); .collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -28,9 +28,9 @@ fn bench_fq2_sub_assign(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(Fq2, Fq2)> = (0..SAMPLES).map(|_| { let v: Vec<(Fq2, Fq2)> = (0..SAMPLES)
(Fq2::rand(&mut rng), Fq2::rand(&mut rng)) .map(|_| (Fq2::rand(&mut rng), Fq2::rand(&mut rng)))
}).collect(); .collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -47,9 +47,9 @@ fn bench_fq2_mul_assign(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(Fq2, Fq2)> = (0..SAMPLES).map(|_| { let v: Vec<(Fq2, Fq2)> = (0..SAMPLES)
(Fq2::rand(&mut rng), Fq2::rand(&mut rng)) .map(|_| (Fq2::rand(&mut rng), Fq2::rand(&mut rng)))
}).collect(); .collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -66,9 +66,7 @@ fn bench_fq2_squaring(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<Fq2> = (0..SAMPLES).map(|_| { let v: Vec<Fq2> = (0..SAMPLES).map(|_| Fq2::rand(&mut rng)).collect();
Fq2::rand(&mut rng)
}).collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -85,9 +83,7 @@ fn bench_fq2_inverse(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<Fq2> = (0..SAMPLES).map(|_| { let v: Vec<Fq2> = (0..SAMPLES).map(|_| Fq2::rand(&mut rng)).collect();
Fq2::rand(&mut rng)
}).collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -103,9 +99,7 @@ fn bench_fq2_sqrt(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<Fq2> = (0..SAMPLES).map(|_| { let v: Vec<Fq2> = (0..SAMPLES).map(|_| Fq2::rand(&mut rng)).collect();
Fq2::rand(&mut rng)
}).collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {

@ -1,7 +1,7 @@
use rand::{Rand, SeedableRng, XorShiftRng}; use rand::{Rand, SeedableRng, XorShiftRng};
use pairing::{Field, PrimeField, PrimeFieldRepr, SqrtField};
use pairing::bls12_381::*; use pairing::bls12_381::*;
use pairing::{Field, PrimeField, PrimeFieldRepr, SqrtField};
#[bench] #[bench]
fn bench_fr_repr_add_nocarry(b: &mut ::test::Bencher) { fn bench_fr_repr_add_nocarry(b: &mut ::test::Bencher) {
@ -9,16 +9,18 @@ fn bench_fr_repr_add_nocarry(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(FrRepr, FrRepr)> = (0..SAMPLES).map(|_| { let v: Vec<(FrRepr, FrRepr)> = (0..SAMPLES)
let mut tmp1 = FrRepr::rand(&mut rng); .map(|_| {
let mut tmp2 = FrRepr::rand(&mut rng); let mut tmp1 = FrRepr::rand(&mut rng);
// Shave a few bits off to avoid overflow. let mut tmp2 = FrRepr::rand(&mut rng);
for _ in 0..3 { // Shave a few bits off to avoid overflow.
tmp1.div2(); for _ in 0..3 {
tmp2.div2(); tmp1.div2();
} tmp2.div2();
(tmp1, tmp2) }
}).collect(); (tmp1, tmp2)
})
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -35,15 +37,17 @@ fn bench_fr_repr_sub_noborrow(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(FrRepr, FrRepr)> = (0..SAMPLES).map(|_| { let v: Vec<(FrRepr, FrRepr)> = (0..SAMPLES)
let tmp1 = FrRepr::rand(&mut rng); .map(|_| {
let mut tmp2 = tmp1; let tmp1 = FrRepr::rand(&mut rng);
// Ensure tmp2 is smaller than tmp1. let mut tmp2 = tmp1;
for _ in 0..10 { // Ensure tmp2 is smaller than tmp1.
tmp2.div2(); for _ in 0..10 {
} tmp2.div2();
(tmp1, tmp2) }
}).collect(); (tmp1, tmp2)
})
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -110,7 +114,9 @@ fn bench_fr_add_assign(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(Fr, Fr)> = (0..SAMPLES).map(|_| (Fr::rand(&mut rng), Fr::rand(&mut rng))).collect(); let v: Vec<(Fr, Fr)> = (0..SAMPLES)
.map(|_| (Fr::rand(&mut rng), Fr::rand(&mut rng)))
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -127,7 +133,9 @@ fn bench_fr_sub_assign(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(Fr, Fr)> = (0..SAMPLES).map(|_| (Fr::rand(&mut rng), Fr::rand(&mut rng))).collect(); let v: Vec<(Fr, Fr)> = (0..SAMPLES)
.map(|_| (Fr::rand(&mut rng), Fr::rand(&mut rng)))
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -144,7 +152,9 @@ fn bench_fr_mul_assign(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(Fr, Fr)> = (0..SAMPLES).map(|_| (Fr::rand(&mut rng), Fr::rand(&mut rng))).collect(); let v: Vec<(Fr, Fr)> = (0..SAMPLES)
.map(|_| (Fr::rand(&mut rng), Fr::rand(&mut rng)))
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -210,11 +220,13 @@ fn bench_fr_sqrt(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<Fr> = (0..SAMPLES).map(|_| { let v: Vec<Fr> = (0..SAMPLES)
let mut tmp = Fr::rand(&mut rng); .map(|_| {
tmp.square(); let mut tmp = Fr::rand(&mut rng);
tmp tmp.square();
}).collect(); tmp
})
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -229,9 +241,7 @@ fn bench_fr_into_repr(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<Fr> = (0..SAMPLES).map(|_| { let v: Vec<Fr> = (0..SAMPLES).map(|_| Fr::rand(&mut rng)).collect();
Fr::rand(&mut rng)
}).collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -246,9 +256,9 @@ fn bench_fr_from_repr(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<FrRepr> = (0..SAMPLES).map(|_| { let v: Vec<FrRepr> = (0..SAMPLES)
Fr::rand(&mut rng).into_repr() .map(|_| Fr::rand(&mut rng).into_repr())
}).collect(); .collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {

@ -1,13 +1,13 @@
mod fq;
mod fr;
mod fq2;
mod fq12;
mod ec; mod ec;
mod fq;
mod fq12;
mod fq2;
mod fr;
use rand::{Rand, SeedableRng, XorShiftRng}; use rand::{Rand, SeedableRng, XorShiftRng};
use pairing::{Engine, CurveAffine};
use pairing::bls12_381::*; use pairing::bls12_381::*;
use pairing::{CurveAffine, Engine};
#[bench] #[bench]
fn bench_pairing_g1_preparation(b: &mut ::test::Bencher) { fn bench_pairing_g1_preparation(b: &mut ::test::Bencher) {
@ -47,12 +47,14 @@ fn bench_pairing_miller_loop(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(G1Prepared, G2Prepared)> = (0..SAMPLES).map(|_| let v: Vec<(G1Prepared, G2Prepared)> = (0..SAMPLES)
( .map(|_| {
G1Affine::from(G1::rand(&mut rng)).prepare(), (
G2Affine::from(G2::rand(&mut rng)).prepare() G1Affine::from(G1::rand(&mut rng)).prepare(),
) G2Affine::from(G2::rand(&mut rng)).prepare(),
).collect(); )
})
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -68,12 +70,15 @@ fn bench_pairing_final_exponentiation(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<Fq12> = (0..SAMPLES).map(|_| let v: Vec<Fq12> = (0..SAMPLES)
( .map(|_| {
G1Affine::from(G1::rand(&mut rng)).prepare(), (
G2Affine::from(G2::rand(&mut rng)).prepare() G1Affine::from(G1::rand(&mut rng)).prepare(),
) G2Affine::from(G2::rand(&mut rng)).prepare(),
).map(|(ref p, ref q)| Bls12::miller_loop(&[(p, q)])).collect(); )
})
.map(|(ref p, ref q)| Bls12::miller_loop(&[(p, q)]))
.collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -89,12 +94,9 @@ fn bench_pairing_full(b: &mut ::test::Bencher) {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let v: Vec<(G1, G2)> = (0..SAMPLES).map(|_| let v: Vec<(G1, G2)> = (0..SAMPLES)
( .map(|_| (G1::rand(&mut rng), G2::rand(&mut rng)))
G1::rand(&mut rng), .collect();
G2::rand(&mut rng)
)
).collect();
let mut count = 0; let mut count = 0;
b.iter(|| { b.iter(|| {
@ -102,4 +104,4 @@ fn bench_pairing_full(b: &mut ::test::Bencher) {
count = (count + 1) % SAMPLES; count = (count + 1) % SAMPLES;
tmp tmp
}); });
} }

@ -1,7 +1,7 @@
#![feature(test)] #![feature(test)]
extern crate test;
extern crate rand;
extern crate pairing; extern crate pairing;
extern crate rand;
extern crate test;
mod bls12_381; mod bls12_381;

@ -621,12 +621,14 @@ macro_rules! curve_impl {
} }
pub mod g1 { pub mod g1 {
use super::super::{Bls12, Fq, Fq12, FqRepr, Fr, FrRepr};
use super::g2::G2Affine;
use rand::{Rand, Rng}; use rand::{Rand, Rng};
use std::fmt; use std::fmt;
use super::g2::G2Affine; use {
use super::super::{Bls12, Fq, Fq12, FqRepr, Fr, FrRepr}; BitIterator, CurveAffine, CurveProjective, EncodedPoint, Engine, Field, GroupDecodingError,
use {BitIterator, CurveAffine, CurveProjective, EncodedPoint, Engine, Field, PrimeField, PrimeFieldRepr, SqrtField,
GroupDecodingError, PrimeField, PrimeFieldRepr, SqrtField}; };
curve_impl!( curve_impl!(
"G1", "G1",
@ -1266,12 +1268,14 @@ pub mod g1 {
} }
pub mod g2 { pub mod g2 {
use rand::{Rand, Rng};
use std::fmt;
use super::super::{Bls12, Fq, Fq12, Fq2, FqRepr, Fr, FrRepr}; use super::super::{Bls12, Fq, Fq12, Fq2, FqRepr, Fr, FrRepr};
use super::g1::G1Affine; use super::g1::G1Affine;
use {BitIterator, CurveAffine, CurveProjective, EncodedPoint, Engine, Field, use rand::{Rand, Rng};
GroupDecodingError, PrimeField, PrimeFieldRepr, SqrtField}; use std::fmt;
use {
BitIterator, CurveAffine, CurveProjective, EncodedPoint, Engine, Field, GroupDecodingError,
PrimeField, PrimeFieldRepr, SqrtField,
};
curve_impl!( curve_impl!(
"G2", "G2",

@ -1,6 +1,6 @@
use {Field, PrimeField, PrimeFieldDecodingError, PrimeFieldRepr, SqrtField};
use std::cmp::Ordering;
use super::fq2::Fq2; use super::fq2::Fq2;
use std::cmp::Ordering;
use {Field, PrimeField, PrimeFieldDecodingError, PrimeFieldRepr, SqrtField};
// q = 4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787 // q = 4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787
const MODULUS: FqRepr = FqRepr([ const MODULUS: FqRepr = FqRepr([

@ -1,8 +1,8 @@
use super::fq::FROBENIUS_COEFF_FQ12_C1;
use super::fq2::Fq2;
use super::fq6::Fq6;
use rand::{Rand, Rng}; use rand::{Rand, Rng};
use Field; use Field;
use super::fq6::Fq6;
use super::fq2::Fq2;
use super::fq::FROBENIUS_COEFF_FQ12_C1;
/// An element of Fq12, represented by c0 + c1 * w. /// An element of Fq12, represented by c0 + c1 * w.
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug, Eq, PartialEq)]

@ -1,6 +1,6 @@
use super::fq::{FROBENIUS_COEFF_FQ2_C1, Fq, NEGATIVE_ONE};
use rand::{Rand, Rng}; use rand::{Rand, Rng};
use {Field, SqrtField}; use {Field, SqrtField};
use super::fq::{FROBENIUS_COEFF_FQ2_C1, Fq, NEGATIVE_ONE};
use std::cmp::Ordering; use std::cmp::Ordering;
@ -271,8 +271,8 @@ fn test_fq2_basics() {
#[test] #[test]
fn test_fq2_squaring() { fn test_fq2_squaring() {
use PrimeField;
use super::fq::FqRepr; use super::fq::FqRepr;
use PrimeField;
let mut a = Fq2 { let mut a = Fq2 {
c0: Fq::one(), c0: Fq::one(),
@ -345,8 +345,8 @@ fn test_fq2_squaring() {
#[test] #[test]
fn test_fq2_mul() { fn test_fq2_mul() {
use PrimeField;
use super::fq::FqRepr; use super::fq::FqRepr;
use PrimeField;
let mut a = Fq2 { let mut a = Fq2 {
c0: Fq::from_repr(FqRepr([ c0: Fq::from_repr(FqRepr([
@ -409,8 +409,8 @@ fn test_fq2_mul() {
#[test] #[test]
fn test_fq2_inverse() { fn test_fq2_inverse() {
use PrimeField;
use super::fq::FqRepr; use super::fq::FqRepr;
use PrimeField;
assert!(Fq2::zero().inverse().is_none()); assert!(Fq2::zero().inverse().is_none());
@ -458,8 +458,8 @@ fn test_fq2_inverse() {
#[test] #[test]
fn test_fq2_addition() { fn test_fq2_addition() {
use PrimeField;
use super::fq::FqRepr; use super::fq::FqRepr;
use PrimeField;
let mut a = Fq2 { let mut a = Fq2 {
c0: Fq::from_repr(FqRepr([ c0: Fq::from_repr(FqRepr([
@ -522,8 +522,8 @@ fn test_fq2_addition() {
#[test] #[test]
fn test_fq2_subtraction() { fn test_fq2_subtraction() {
use PrimeField;
use super::fq::FqRepr; use super::fq::FqRepr;
use PrimeField;
let mut a = Fq2 { let mut a = Fq2 {
c0: Fq::from_repr(FqRepr([ c0: Fq::from_repr(FqRepr([
@ -586,8 +586,8 @@ fn test_fq2_subtraction() {
#[test] #[test]
fn test_fq2_negation() { fn test_fq2_negation() {
use PrimeField;
use super::fq::FqRepr; use super::fq::FqRepr;
use PrimeField;
let mut a = Fq2 { let mut a = Fq2 {
c0: Fq::from_repr(FqRepr([ c0: Fq::from_repr(FqRepr([
@ -633,8 +633,8 @@ fn test_fq2_negation() {
#[test] #[test]
fn test_fq2_doubling() { fn test_fq2_doubling() {
use PrimeField;
use super::fq::FqRepr; use super::fq::FqRepr;
use PrimeField;
let mut a = Fq2 { let mut a = Fq2 {
c0: Fq::from_repr(FqRepr([ c0: Fq::from_repr(FqRepr([
@ -680,8 +680,8 @@ fn test_fq2_doubling() {
#[test] #[test]
fn test_fq2_frobenius_map() { fn test_fq2_frobenius_map() {
use PrimeField;
use super::fq::FqRepr; use super::fq::FqRepr;
use PrimeField;
let mut a = Fq2 { let mut a = Fq2 {
c0: Fq::from_repr(FqRepr([ c0: Fq::from_repr(FqRepr([
@ -793,8 +793,8 @@ fn test_fq2_frobenius_map() {
#[test] #[test]
fn test_fq2_sqrt() { fn test_fq2_sqrt() {
use PrimeField;
use super::fq::FqRepr; use super::fq::FqRepr;
use PrimeField;
assert_eq!( assert_eq!(
Fq2 { Fq2 {

@ -1,7 +1,7 @@
use super::fq::{FROBENIUS_COEFF_FQ6_C1, FROBENIUS_COEFF_FQ6_C2};
use super::fq2::Fq2;
use rand::{Rand, Rng}; use rand::{Rand, Rng};
use Field; use Field;
use super::fq2::Fq2;
use super::fq::{FROBENIUS_COEFF_FQ6_C1, FROBENIUS_COEFF_FQ6_C2};
/// An element of Fq6, represented by c0 + c1 * v + c2 * v^(2). /// An element of Fq6, represented by c0 + c1 * v + c2 * v^(2).
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug, Eq, PartialEq)]

@ -1,5 +1,5 @@
use {Field, PrimeField, PrimeFieldDecodingError, PrimeFieldRepr, SqrtField};
use LegendreSymbol::*; use LegendreSymbol::*;
use {Field, PrimeField, PrimeFieldDecodingError, PrimeFieldRepr, SqrtField};
// r = 52435875175126190479447740508185965837690552500527637822603658699938581184513 // r = 52435875175126190479447740508185965837690552500527637822603658699938581184513
const MODULUS: FrRepr = FrRepr([ const MODULUS: FrRepr = FrRepr([

@ -1,20 +1,22 @@
mod ec;
mod fq; mod fq;
mod fr; mod fq12;
mod fq2; mod fq2;
mod fq6; mod fq6;
mod fq12; mod fr;
mod ec;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
pub use self::fr::{Fr, FrRepr}; pub use self::ec::{
G1, G1Affine, G1Compressed, G1Prepared, G1Uncompressed, G2, G2Affine, G2Compressed, G2Prepared,
G2Uncompressed,
};
pub use self::fq::{Fq, FqRepr}; pub use self::fq::{Fq, FqRepr};
pub use self::fq12::Fq12;
pub use self::fq2::Fq2; pub use self::fq2::Fq2;
pub use self::fq6::Fq6; pub use self::fq6::Fq6;
pub use self::fq12::Fq12; pub use self::fr::{Fr, FrRepr};
pub use self::ec::{G1, G1Affine, G1Compressed, G1Prepared, G1Uncompressed, G2, G2Affine,
G2Compressed, G2Prepared, G2Uncompressed};
use super::{BitIterator, CurveAffine, Engine, Field}; use super::{BitIterator, CurveAffine, Engine, Field};

@ -1,5 +1,5 @@
use super::*; use super::*;
use ::*; use *;
#[test] #[test]
fn test_pairing_result_against_relic() { fn test_pairing_result_against_relic() {

@ -9,6 +9,7 @@
#![cfg_attr(feature = "clippy", allow(unreadable_literal))] #![cfg_attr(feature = "clippy", allow(unreadable_literal))]
#![cfg_attr(feature = "clippy", allow(many_single_char_names))] #![cfg_attr(feature = "clippy", allow(many_single_char_names))]
#![cfg_attr(feature = "clippy", allow(new_without_default_derive))] #![cfg_attr(feature = "clippy", allow(new_without_default_derive))]
#![cfg_attr(feature = "clippy", allow(write_literal))]
// Force public structures to implement Debug // Force public structures to implement Debug
#![deny(missing_debug_implementations)] #![deny(missing_debug_implementations)]
@ -23,8 +24,8 @@ pub mod bls12_381;
mod wnaf; mod wnaf;
pub use self::wnaf::Wnaf; pub use self::wnaf::Wnaf;
use std::fmt;
use std::error::Error; use std::error::Error;
use std::fmt;
use std::io::{self, Read, Write}; use std::io::{self, Read, Write};
/// An "engine" is a collection of types (fields, elliptic curve groups, etc.) /// An "engine" is a collection of types (fields, elliptic curve groups, etc.)
@ -407,6 +408,28 @@ pub trait PrimeFieldRepr:
Ok(()) Ok(())
} }
/// Writes this `PrimeFieldRepr` as a little endian integer.
fn write_le<W: Write>(&self, mut writer: W) -> io::Result<()> {
use byteorder::{LittleEndian, WriteBytesExt};
for digit in self.as_ref().iter() {
writer.write_u64::<LittleEndian>(*digit)?;
}
Ok(())
}
/// Reads a little endian integer into this representation.
fn read_le<R: Read>(&mut self, mut reader: R) -> io::Result<()> {
use byteorder::{LittleEndian, ReadBytesExt};
for digit in self.as_mut().iter_mut() {
*digit = reader.read_u64::<LittleEndian>()?;
}
Ok(())
}
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]

@ -47,7 +47,8 @@ pub fn curve_tests<G: CurveProjective>() {
{ {
let a = G::rand(&mut rng); let a = G::rand(&mut rng);
let b = a.into_affine().into_projective(); let b = a.into_affine().into_projective();
let c = a.into_affine() let c = a
.into_affine()
.into_projective() .into_projective()
.into_affine() .into_affine()
.into_projective(); .into_projective();
@ -372,7 +373,8 @@ fn random_transformation_tests<G: CurveProjective>() {
v[s] = v[s].into_affine().into_projective(); v[s] = v[s].into_affine().into_projective();
} }
let expected_v = v.iter() let expected_v = v
.iter()
.map(|v| v.into_affine().into_projective()) .map(|v| v.into_affine().into_projective())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
G::batch_normalization(&mut v); G::batch_normalization(&mut v);

@ -1,4 +1,4 @@
pub mod curve; pub mod curve;
pub mod field;
pub mod engine; pub mod engine;
pub mod field;
pub mod repr; pub mod repr;

@ -12,13 +12,46 @@ fn random_encoding_tests<R: PrimeFieldRepr>() {
for _ in 0..1000 { for _ in 0..1000 {
let r = R::rand(&mut rng); let r = R::rand(&mut rng);
let mut rdecoded = R::default();
let mut v: Vec<u8> = vec![]; // Big endian
r.write_be(&mut v).unwrap(); {
rdecoded.read_be(&v[0..]).unwrap(); let mut rdecoded = R::default();
assert_eq!(r, rdecoded); let mut v: Vec<u8> = vec![];
r.write_be(&mut v).unwrap();
rdecoded.read_be(&v[0..]).unwrap();
assert_eq!(r, rdecoded);
}
// Little endian
{
let mut rdecoded = R::default();
let mut v: Vec<u8> = vec![];
r.write_le(&mut v).unwrap();
rdecoded.read_le(&v[0..]).unwrap();
assert_eq!(r, rdecoded);
}
{
let mut rdecoded_le = R::default();
let mut rdecoded_be_flip = R::default();
let mut v: Vec<u8> = vec![];
r.write_le(&mut v).unwrap();
// This reads in little-endian, so we are done.
rdecoded_le.read_le(&v[..]).unwrap();
// This reads in big-endian, so we perform a swap of the
// bytes beforehand.
let v: Vec<u8> = v.into_iter().rev().collect();
rdecoded_be_flip.read_be(&v[..]).unwrap();
assert_eq!(rdecoded_le, rdecoded_be_flip);
}
} }
} }