From 98bab6877a2f06b9b6eae1d56ece59a93767caab Mon Sep 17 00:00:00 2001 From: str4d Date: Thu, 17 May 2018 09:34:16 +1200 Subject: [PATCH 1/5] Add read_le and write_le to PrimeFieldRepr --- src/lib.rs | 22 ++++++++++++++++++++++ src/tests/repr.rs | 43 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 481ff26..930d806 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -407,6 +407,28 @@ pub trait PrimeFieldRepr: Ok(()) } + + /// Writes this `PrimeFieldRepr` as a little endian integer. + fn write_le(&self, mut writer: W) -> io::Result<()> { + use byteorder::{LittleEndian, WriteBytesExt}; + + for digit in self.as_ref().iter() { + writer.write_u64::(*digit)?; + } + + Ok(()) + } + + /// Reads a little endian integer into this representation. + fn read_le(&mut self, mut reader: R) -> io::Result<()> { + use byteorder::{LittleEndian, ReadBytesExt}; + + for digit in self.as_mut().iter_mut() { + *digit = reader.read_u64::()?; + } + + Ok(()) + } } #[derive(Debug, PartialEq)] diff --git a/src/tests/repr.rs b/src/tests/repr.rs index 3b9d76d..681a476 100644 --- a/src/tests/repr.rs +++ b/src/tests/repr.rs @@ -12,13 +12,46 @@ fn random_encoding_tests() { for _ in 0..1000 { let r = R::rand(&mut rng); - let mut rdecoded = R::default(); - let mut v: Vec = vec![]; - r.write_be(&mut v).unwrap(); - rdecoded.read_be(&v[0..]).unwrap(); + // Big endian + { + let mut rdecoded = R::default(); - assert_eq!(r, rdecoded); + let mut v: Vec = 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 = 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 = 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 = v.into_iter().rev().collect(); + rdecoded_be_flip.read_be(&v[..]).unwrap(); + + assert_eq!(rdecoded_le, rdecoded_be_flip); + } } } From 2a28b614689d3219afe36d51c75768c6e76000ca Mon Sep 17 00:00:00 2001 From: str4d Date: Thu, 17 May 2018 16:52:19 +1200 Subject: [PATCH 2/5] Bump version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index da8dc5e..f1f104c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "pairing" # Remember to change version string in README.md. -version = "0.14.1" +version = "0.14.2" authors = ["Sean Bowe "] license = "MIT/Apache-2.0" From 97bdd1655f85d4d2ffef5a1e87f5e37cdf20b333 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 17 May 2018 10:44:28 -0600 Subject: [PATCH 3/5] Update clippy --- Cargo.toml | 2 +- src/lib.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index f1f104c..98725aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ repository = "https://github.com/ebfull/pairing" [dependencies] rand = "0.4" byteorder = "1" -clippy = { version = "0.0.190", optional = true } +clippy = { version = "0.0.200", optional = true } [features] unstable-features = ["expose-arith"] diff --git a/src/lib.rs b/src/lib.rs index 930d806..5343c6e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ #![cfg_attr(feature = "clippy", allow(unreadable_literal))] #![cfg_attr(feature = "clippy", allow(many_single_char_names))] #![cfg_attr(feature = "clippy", allow(new_without_default_derive))] +#![cfg_attr(feature = "clippy", allow(write_literal))] // Force public structures to implement Debug #![deny(missing_debug_implementations)] From e4143a4bbc06f18e354ac276a181571eb5f9fff0 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 17 May 2018 10:50:56 -0600 Subject: [PATCH 4/5] Apply rustfmt to benchmarks --- benches/bls12_381/ec.rs | 24 +++++++++--- benches/bls12_381/fq.rs | 78 +++++++++++++++++++++----------------- benches/bls12_381/fq12.rs | 26 ++++++------- benches/bls12_381/fq2.rs | 30 ++++++--------- benches/bls12_381/fr.rs | 76 +++++++++++++++++++++---------------- benches/bls12_381/mod.rs | 42 ++++++++++---------- benches/pairing_benches.rs | 4 +- 7 files changed, 152 insertions(+), 128 deletions(-) diff --git a/benches/bls12_381/ec.rs b/benches/bls12_381/ec.rs index 608e7f0..421881d 100644 --- a/benches/bls12_381/ec.rs +++ b/benches/bls12_381/ec.rs @@ -10,7 +10,9 @@ mod g1 { 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; b.iter(|| { @@ -27,7 +29,9 @@ mod g1 { 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; b.iter(|| { @@ -44,7 +48,9 @@ mod g1 { 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; b.iter(|| { @@ -68,7 +74,9 @@ mod g2 { 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; b.iter(|| { @@ -85,7 +93,9 @@ mod g2 { 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; b.iter(|| { @@ -102,7 +112,9 @@ mod g2 { 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; b.iter(|| { diff --git a/benches/bls12_381/fq.rs b/benches/bls12_381/fq.rs index 9b2a08e..8dad757 100644 --- a/benches/bls12_381/fq.rs +++ b/benches/bls12_381/fq.rs @@ -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 v: Vec<(FqRepr, FqRepr)> = (0..SAMPLES).map(|_| { - let mut tmp1 = FqRepr::rand(&mut rng); - let mut tmp2 = FqRepr::rand(&mut rng); - // Shave a few bits off to avoid overflow. - for _ in 0..3 { - tmp1.div2(); - tmp2.div2(); - } - (tmp1, tmp2) - }).collect(); + let v: Vec<(FqRepr, FqRepr)> = (0..SAMPLES) + .map(|_| { + let mut tmp1 = FqRepr::rand(&mut rng); + let mut tmp2 = FqRepr::rand(&mut rng); + // Shave a few bits off to avoid overflow. + for _ in 0..3 { + tmp1.div2(); + tmp2.div2(); + } + (tmp1, tmp2) + }) + .collect(); let mut count = 0; 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 v: Vec<(FqRepr, FqRepr)> = (0..SAMPLES).map(|_| { - let tmp1 = FqRepr::rand(&mut rng); - let mut tmp2 = tmp1; - // Ensure tmp2 is smaller than tmp1. - for _ in 0..10 { - tmp2.div2(); - } - (tmp1, tmp2) - }).collect(); + let v: Vec<(FqRepr, FqRepr)> = (0..SAMPLES) + .map(|_| { + let tmp1 = FqRepr::rand(&mut rng); + let mut tmp2 = tmp1; + // Ensure tmp2 is smaller than tmp1. + for _ in 0..10 { + tmp2.div2(); + } + (tmp1, tmp2) + }) + .collect(); let mut count = 0; 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 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; 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 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; 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 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; b.iter(|| { @@ -206,15 +216,17 @@ fn bench_fq_negate(b: &mut ::test::Bencher) { #[bench] 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 v: Vec = (0..SAMPLES).map(|_| { - let mut tmp = Fq::rand(&mut rng); - tmp.square(); - tmp - }).collect(); + let v: Vec = (0..SAMPLES) + .map(|_| { + let mut tmp = Fq::rand(&mut rng); + tmp.square(); + tmp + }) + .collect(); let mut count = 0; 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 v: Vec = (0..SAMPLES).map(|_| { - Fq::rand(&mut rng) - }).collect(); + let v: Vec = (0..SAMPLES).map(|_| Fq::rand(&mut rng)).collect(); let mut count = 0; 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 v: Vec = (0..SAMPLES).map(|_| { - Fq::rand(&mut rng).into_repr() - }).collect(); + let v: Vec = (0..SAMPLES) + .map(|_| Fq::rand(&mut rng).into_repr()) + .collect(); let mut count = 0; b.iter(|| { diff --git a/benches/bls12_381/fq12.rs b/benches/bls12_381/fq12.rs index 26c33c8..42ae9c0 100644 --- a/benches/bls12_381/fq12.rs +++ b/benches/bls12_381/fq12.rs @@ -9,9 +9,9 @@ fn bench_fq12_add_assign(b: &mut ::test::Bencher) { let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); - let v: Vec<(Fq12, Fq12)> = (0..SAMPLES).map(|_| { - (Fq12::rand(&mut rng), Fq12::rand(&mut rng)) - }).collect(); + let v: Vec<(Fq12, Fq12)> = (0..SAMPLES) + .map(|_| (Fq12::rand(&mut rng), Fq12::rand(&mut rng))) + .collect(); let mut count = 0; 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 v: Vec<(Fq12, Fq12)> = (0..SAMPLES).map(|_| { - (Fq12::rand(&mut rng), Fq12::rand(&mut rng)) - }).collect(); + let v: Vec<(Fq12, Fq12)> = (0..SAMPLES) + .map(|_| (Fq12::rand(&mut rng), Fq12::rand(&mut rng))) + .collect(); let mut count = 0; 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 v: Vec<(Fq12, Fq12)> = (0..SAMPLES).map(|_| { - (Fq12::rand(&mut rng), Fq12::rand(&mut rng)) - }).collect(); + let v: Vec<(Fq12, Fq12)> = (0..SAMPLES) + .map(|_| (Fq12::rand(&mut rng), Fq12::rand(&mut rng))) + .collect(); let mut count = 0; 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 v: Vec = (0..SAMPLES).map(|_| { - Fq12::rand(&mut rng) - }).collect(); + let v: Vec = (0..SAMPLES).map(|_| Fq12::rand(&mut rng)).collect(); let mut count = 0; 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 v: Vec = (0..SAMPLES).map(|_| { - Fq12::rand(&mut rng) - }).collect(); + let v: Vec = (0..SAMPLES).map(|_| Fq12::rand(&mut rng)).collect(); let mut count = 0; b.iter(|| { diff --git a/benches/bls12_381/fq2.rs b/benches/bls12_381/fq2.rs index d5f835d..733a745 100644 --- a/benches/bls12_381/fq2.rs +++ b/benches/bls12_381/fq2.rs @@ -9,9 +9,9 @@ fn bench_fq2_add_assign(b: &mut ::test::Bencher) { let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); - let v: Vec<(Fq2, Fq2)> = (0..SAMPLES).map(|_| { - (Fq2::rand(&mut rng), Fq2::rand(&mut rng)) - }).collect(); + let v: Vec<(Fq2, Fq2)> = (0..SAMPLES) + .map(|_| (Fq2::rand(&mut rng), Fq2::rand(&mut rng))) + .collect(); let mut count = 0; 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 v: Vec<(Fq2, Fq2)> = (0..SAMPLES).map(|_| { - (Fq2::rand(&mut rng), Fq2::rand(&mut rng)) - }).collect(); + let v: Vec<(Fq2, Fq2)> = (0..SAMPLES) + .map(|_| (Fq2::rand(&mut rng), Fq2::rand(&mut rng))) + .collect(); let mut count = 0; 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 v: Vec<(Fq2, Fq2)> = (0..SAMPLES).map(|_| { - (Fq2::rand(&mut rng), Fq2::rand(&mut rng)) - }).collect(); + let v: Vec<(Fq2, Fq2)> = (0..SAMPLES) + .map(|_| (Fq2::rand(&mut rng), Fq2::rand(&mut rng))) + .collect(); let mut count = 0; 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 v: Vec = (0..SAMPLES).map(|_| { - Fq2::rand(&mut rng) - }).collect(); + let v: Vec = (0..SAMPLES).map(|_| Fq2::rand(&mut rng)).collect(); let mut count = 0; 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 v: Vec = (0..SAMPLES).map(|_| { - Fq2::rand(&mut rng) - }).collect(); + let v: Vec = (0..SAMPLES).map(|_| Fq2::rand(&mut rng)).collect(); let mut count = 0; 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 v: Vec = (0..SAMPLES).map(|_| { - Fq2::rand(&mut rng) - }).collect(); + let v: Vec = (0..SAMPLES).map(|_| Fq2::rand(&mut rng)).collect(); let mut count = 0; b.iter(|| { diff --git a/benches/bls12_381/fr.rs b/benches/bls12_381/fr.rs index 58575b6..0ee13f8 100644 --- a/benches/bls12_381/fr.rs +++ b/benches/bls12_381/fr.rs @@ -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 v: Vec<(FrRepr, FrRepr)> = (0..SAMPLES).map(|_| { - let mut tmp1 = FrRepr::rand(&mut rng); - let mut tmp2 = FrRepr::rand(&mut rng); - // Shave a few bits off to avoid overflow. - for _ in 0..3 { - tmp1.div2(); - tmp2.div2(); - } - (tmp1, tmp2) - }).collect(); + let v: Vec<(FrRepr, FrRepr)> = (0..SAMPLES) + .map(|_| { + let mut tmp1 = FrRepr::rand(&mut rng); + let mut tmp2 = FrRepr::rand(&mut rng); + // Shave a few bits off to avoid overflow. + for _ in 0..3 { + tmp1.div2(); + tmp2.div2(); + } + (tmp1, tmp2) + }) + .collect(); let mut count = 0; 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 v: Vec<(FrRepr, FrRepr)> = (0..SAMPLES).map(|_| { - let tmp1 = FrRepr::rand(&mut rng); - let mut tmp2 = tmp1; - // Ensure tmp2 is smaller than tmp1. - for _ in 0..10 { - tmp2.div2(); - } - (tmp1, tmp2) - }).collect(); + let v: Vec<(FrRepr, FrRepr)> = (0..SAMPLES) + .map(|_| { + let tmp1 = FrRepr::rand(&mut rng); + let mut tmp2 = tmp1; + // Ensure tmp2 is smaller than tmp1. + for _ in 0..10 { + tmp2.div2(); + } + (tmp1, tmp2) + }) + .collect(); let mut count = 0; 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 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; 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 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; 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 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; 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 v: Vec = (0..SAMPLES).map(|_| { - let mut tmp = Fr::rand(&mut rng); - tmp.square(); - tmp - }).collect(); + let v: Vec = (0..SAMPLES) + .map(|_| { + let mut tmp = Fr::rand(&mut rng); + tmp.square(); + tmp + }) + .collect(); let mut count = 0; 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 v: Vec = (0..SAMPLES).map(|_| { - Fr::rand(&mut rng) - }).collect(); + let v: Vec = (0..SAMPLES).map(|_| Fr::rand(&mut rng)).collect(); let mut count = 0; 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 v: Vec = (0..SAMPLES).map(|_| { - Fr::rand(&mut rng).into_repr() - }).collect(); + let v: Vec = (0..SAMPLES) + .map(|_| Fr::rand(&mut rng).into_repr()) + .collect(); let mut count = 0; b.iter(|| { diff --git a/benches/bls12_381/mod.rs b/benches/bls12_381/mod.rs index 64a90cc..cf447ed 100644 --- a/benches/bls12_381/mod.rs +++ b/benches/bls12_381/mod.rs @@ -6,7 +6,7 @@ mod ec; use rand::{Rand, SeedableRng, XorShiftRng}; -use pairing::{Engine, CurveAffine}; +use pairing::{CurveAffine, Engine}; use pairing::bls12_381::*; #[bench] @@ -47,12 +47,14 @@ fn bench_pairing_miller_loop(b: &mut ::test::Bencher) { let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); - let v: Vec<(G1Prepared, G2Prepared)> = (0..SAMPLES).map(|_| - ( - G1Affine::from(G1::rand(&mut rng)).prepare(), - G2Affine::from(G2::rand(&mut rng)).prepare() - ) - ).collect(); + let v: Vec<(G1Prepared, G2Prepared)> = (0..SAMPLES) + .map(|_| { + ( + G1Affine::from(G1::rand(&mut rng)).prepare(), + G2Affine::from(G2::rand(&mut rng)).prepare(), + ) + }) + .collect(); let mut count = 0; 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 v: Vec = (0..SAMPLES).map(|_| - ( - 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(); + let v: Vec = (0..SAMPLES) + .map(|_| { + ( + 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(); let mut count = 0; 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 v: Vec<(G1, G2)> = (0..SAMPLES).map(|_| - ( - G1::rand(&mut rng), - G2::rand(&mut rng) - ) - ).collect(); + let v: Vec<(G1, G2)> = (0..SAMPLES) + .map(|_| (G1::rand(&mut rng), G2::rand(&mut rng))) + .collect(); let mut count = 0; b.iter(|| { @@ -102,4 +104,4 @@ fn bench_pairing_full(b: &mut ::test::Bencher) { count = (count + 1) % SAMPLES; tmp }); -} \ No newline at end of file +} diff --git a/benches/pairing_benches.rs b/benches/pairing_benches.rs index 3ae12e0..424c4e7 100644 --- a/benches/pairing_benches.rs +++ b/benches/pairing_benches.rs @@ -1,7 +1,7 @@ #![feature(test)] -extern crate test; -extern crate rand; extern crate pairing; +extern crate rand; +extern crate test; mod bls12_381; From da5f1d3e3705fc0dee72e9cbbb4093383964fb0c Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 17 May 2018 11:59:20 -0600 Subject: [PATCH 5/5] Update to latest rustfmt --- benches/bls12_381/ec.rs | 4 ++-- benches/bls12_381/fq.rs | 2 +- benches/bls12_381/fq12.rs | 2 +- benches/bls12_381/fq2.rs | 2 +- benches/bls12_381/fr.rs | 2 +- benches/bls12_381/mod.rs | 10 +++++----- src/bls12_381/ec.rs | 20 ++++++++++++-------- src/bls12_381/fq.rs | 4 ++-- src/bls12_381/fq12.rs | 6 +++--- src/bls12_381/fq2.rs | 20 ++++++++++---------- src/bls12_381/fq6.rs | 4 ++-- src/bls12_381/fr.rs | 2 +- src/bls12_381/mod.rs | 16 +++++++++------- src/bls12_381/tests/mod.rs | 2 +- src/lib.rs | 2 +- src/tests/curve.rs | 6 ++++-- src/tests/mod.rs | 2 +- 17 files changed, 57 insertions(+), 49 deletions(-) diff --git a/benches/bls12_381/ec.rs b/benches/bls12_381/ec.rs index 421881d..cbd0590 100644 --- a/benches/bls12_381/ec.rs +++ b/benches/bls12_381/ec.rs @@ -1,8 +1,8 @@ mod g1 { use rand::{Rand, SeedableRng, XorShiftRng}; - use pairing::CurveProjective; use pairing::bls12_381::*; + use pairing::CurveProjective; #[bench] fn bench_g1_mul_assign(b: &mut ::test::Bencher) { @@ -65,8 +65,8 @@ mod g1 { mod g2 { use rand::{Rand, SeedableRng, XorShiftRng}; - use pairing::CurveProjective; use pairing::bls12_381::*; + use pairing::CurveProjective; #[bench] fn bench_g2_mul_assign(b: &mut ::test::Bencher) { diff --git a/benches/bls12_381/fq.rs b/benches/bls12_381/fq.rs index 8dad757..af4dba4 100644 --- a/benches/bls12_381/fq.rs +++ b/benches/bls12_381/fq.rs @@ -1,7 +1,7 @@ use rand::{Rand, SeedableRng, XorShiftRng}; -use pairing::{Field, PrimeField, PrimeFieldRepr, SqrtField}; use pairing::bls12_381::*; +use pairing::{Field, PrimeField, PrimeFieldRepr, SqrtField}; #[bench] fn bench_fq_repr_add_nocarry(b: &mut ::test::Bencher) { diff --git a/benches/bls12_381/fq12.rs b/benches/bls12_381/fq12.rs index 42ae9c0..226b850 100644 --- a/benches/bls12_381/fq12.rs +++ b/benches/bls12_381/fq12.rs @@ -1,7 +1,7 @@ use rand::{Rand, SeedableRng, XorShiftRng}; -use pairing::Field; use pairing::bls12_381::*; +use pairing::Field; #[bench] fn bench_fq12_add_assign(b: &mut ::test::Bencher) { diff --git a/benches/bls12_381/fq2.rs b/benches/bls12_381/fq2.rs index 733a745..ec26e98 100644 --- a/benches/bls12_381/fq2.rs +++ b/benches/bls12_381/fq2.rs @@ -1,7 +1,7 @@ use rand::{Rand, SeedableRng, XorShiftRng}; -use pairing::{Field, SqrtField}; use pairing::bls12_381::*; +use pairing::{Field, SqrtField}; #[bench] fn bench_fq2_add_assign(b: &mut ::test::Bencher) { diff --git a/benches/bls12_381/fr.rs b/benches/bls12_381/fr.rs index 0ee13f8..7278629 100644 --- a/benches/bls12_381/fr.rs +++ b/benches/bls12_381/fr.rs @@ -1,7 +1,7 @@ use rand::{Rand, SeedableRng, XorShiftRng}; -use pairing::{Field, PrimeField, PrimeFieldRepr, SqrtField}; use pairing::bls12_381::*; +use pairing::{Field, PrimeField, PrimeFieldRepr, SqrtField}; #[bench] fn bench_fr_repr_add_nocarry(b: &mut ::test::Bencher) { diff --git a/benches/bls12_381/mod.rs b/benches/bls12_381/mod.rs index cf447ed..9b46c85 100644 --- a/benches/bls12_381/mod.rs +++ b/benches/bls12_381/mod.rs @@ -1,13 +1,13 @@ -mod fq; -mod fr; -mod fq2; -mod fq12; mod ec; +mod fq; +mod fq12; +mod fq2; +mod fr; use rand::{Rand, SeedableRng, XorShiftRng}; -use pairing::{CurveAffine, Engine}; use pairing::bls12_381::*; +use pairing::{CurveAffine, Engine}; #[bench] fn bench_pairing_g1_preparation(b: &mut ::test::Bencher) { diff --git a/src/bls12_381/ec.rs b/src/bls12_381/ec.rs index 5f651ca..5cd5091 100644 --- a/src/bls12_381/ec.rs +++ b/src/bls12_381/ec.rs @@ -621,12 +621,14 @@ macro_rules! curve_impl { } pub mod g1 { + use super::super::{Bls12, Fq, Fq12, FqRepr, Fr, FrRepr}; + use super::g2::G2Affine; use rand::{Rand, Rng}; use std::fmt; - use super::g2::G2Affine; - use super::super::{Bls12, Fq, Fq12, FqRepr, Fr, FrRepr}; - use {BitIterator, CurveAffine, CurveProjective, EncodedPoint, Engine, Field, - GroupDecodingError, PrimeField, PrimeFieldRepr, SqrtField}; + use { + BitIterator, CurveAffine, CurveProjective, EncodedPoint, Engine, Field, GroupDecodingError, + PrimeField, PrimeFieldRepr, SqrtField, + }; curve_impl!( "G1", @@ -1266,12 +1268,14 @@ pub mod g1 { } pub mod g2 { - use rand::{Rand, Rng}; - use std::fmt; use super::super::{Bls12, Fq, Fq12, Fq2, FqRepr, Fr, FrRepr}; use super::g1::G1Affine; - use {BitIterator, CurveAffine, CurveProjective, EncodedPoint, Engine, Field, - GroupDecodingError, PrimeField, PrimeFieldRepr, SqrtField}; + use rand::{Rand, Rng}; + use std::fmt; + use { + BitIterator, CurveAffine, CurveProjective, EncodedPoint, Engine, Field, GroupDecodingError, + PrimeField, PrimeFieldRepr, SqrtField, + }; curve_impl!( "G2", diff --git a/src/bls12_381/fq.rs b/src/bls12_381/fq.rs index c35360a..738da38 100644 --- a/src/bls12_381/fq.rs +++ b/src/bls12_381/fq.rs @@ -1,6 +1,6 @@ -use {Field, PrimeField, PrimeFieldDecodingError, PrimeFieldRepr, SqrtField}; -use std::cmp::Ordering; use super::fq2::Fq2; +use std::cmp::Ordering; +use {Field, PrimeField, PrimeFieldDecodingError, PrimeFieldRepr, SqrtField}; // q = 4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787 const MODULUS: FqRepr = FqRepr([ diff --git a/src/bls12_381/fq12.rs b/src/bls12_381/fq12.rs index ae79e42..2bec0b1 100644 --- a/src/bls12_381/fq12.rs +++ b/src/bls12_381/fq12.rs @@ -1,8 +1,8 @@ +use super::fq::FROBENIUS_COEFF_FQ12_C1; +use super::fq2::Fq2; +use super::fq6::Fq6; use rand::{Rand, Rng}; 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. #[derive(Copy, Clone, Debug, Eq, PartialEq)] diff --git a/src/bls12_381/fq2.rs b/src/bls12_381/fq2.rs index 873652c..18cd580 100644 --- a/src/bls12_381/fq2.rs +++ b/src/bls12_381/fq2.rs @@ -1,6 +1,6 @@ +use super::fq::{FROBENIUS_COEFF_FQ2_C1, Fq, NEGATIVE_ONE}; use rand::{Rand, Rng}; use {Field, SqrtField}; -use super::fq::{FROBENIUS_COEFF_FQ2_C1, Fq, NEGATIVE_ONE}; use std::cmp::Ordering; @@ -271,8 +271,8 @@ fn test_fq2_basics() { #[test] fn test_fq2_squaring() { - use PrimeField; use super::fq::FqRepr; + use PrimeField; let mut a = Fq2 { c0: Fq::one(), @@ -345,8 +345,8 @@ fn test_fq2_squaring() { #[test] fn test_fq2_mul() { - use PrimeField; use super::fq::FqRepr; + use PrimeField; let mut a = Fq2 { c0: Fq::from_repr(FqRepr([ @@ -409,8 +409,8 @@ fn test_fq2_mul() { #[test] fn test_fq2_inverse() { - use PrimeField; use super::fq::FqRepr; + use PrimeField; assert!(Fq2::zero().inverse().is_none()); @@ -458,8 +458,8 @@ fn test_fq2_inverse() { #[test] fn test_fq2_addition() { - use PrimeField; use super::fq::FqRepr; + use PrimeField; let mut a = Fq2 { c0: Fq::from_repr(FqRepr([ @@ -522,8 +522,8 @@ fn test_fq2_addition() { #[test] fn test_fq2_subtraction() { - use PrimeField; use super::fq::FqRepr; + use PrimeField; let mut a = Fq2 { c0: Fq::from_repr(FqRepr([ @@ -586,8 +586,8 @@ fn test_fq2_subtraction() { #[test] fn test_fq2_negation() { - use PrimeField; use super::fq::FqRepr; + use PrimeField; let mut a = Fq2 { c0: Fq::from_repr(FqRepr([ @@ -633,8 +633,8 @@ fn test_fq2_negation() { #[test] fn test_fq2_doubling() { - use PrimeField; use super::fq::FqRepr; + use PrimeField; let mut a = Fq2 { c0: Fq::from_repr(FqRepr([ @@ -680,8 +680,8 @@ fn test_fq2_doubling() { #[test] fn test_fq2_frobenius_map() { - use PrimeField; use super::fq::FqRepr; + use PrimeField; let mut a = Fq2 { c0: Fq::from_repr(FqRepr([ @@ -793,8 +793,8 @@ fn test_fq2_frobenius_map() { #[test] fn test_fq2_sqrt() { - use PrimeField; use super::fq::FqRepr; + use PrimeField; assert_eq!( Fq2 { diff --git a/src/bls12_381/fq6.rs b/src/bls12_381/fq6.rs index d9a6c33..c065f27 100644 --- a/src/bls12_381/fq6.rs +++ b/src/bls12_381/fq6.rs @@ -1,7 +1,7 @@ +use super::fq::{FROBENIUS_COEFF_FQ6_C1, FROBENIUS_COEFF_FQ6_C2}; +use super::fq2::Fq2; use rand::{Rand, Rng}; 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). #[derive(Copy, Clone, Debug, Eq, PartialEq)] diff --git a/src/bls12_381/fr.rs b/src/bls12_381/fr.rs index 31fbe8b..4e9d6ab 100644 --- a/src/bls12_381/fr.rs +++ b/src/bls12_381/fr.rs @@ -1,5 +1,5 @@ -use {Field, PrimeField, PrimeFieldDecodingError, PrimeFieldRepr, SqrtField}; use LegendreSymbol::*; +use {Field, PrimeField, PrimeFieldDecodingError, PrimeFieldRepr, SqrtField}; // r = 52435875175126190479447740508185965837690552500527637822603658699938581184513 const MODULUS: FrRepr = FrRepr([ diff --git a/src/bls12_381/mod.rs b/src/bls12_381/mod.rs index c35b852..a5db4b5 100644 --- a/src/bls12_381/mod.rs +++ b/src/bls12_381/mod.rs @@ -1,20 +1,22 @@ +mod ec; mod fq; -mod fr; +mod fq12; mod fq2; mod fq6; -mod fq12; -mod ec; +mod fr; #[cfg(test)] 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::fq12::Fq12; pub use self::fq2::Fq2; pub use self::fq6::Fq6; -pub use self::fq12::Fq12; -pub use self::ec::{G1, G1Affine, G1Compressed, G1Prepared, G1Uncompressed, G2, G2Affine, - G2Compressed, G2Prepared, G2Uncompressed}; +pub use self::fr::{Fr, FrRepr}; use super::{BitIterator, CurveAffine, Engine, Field}; diff --git a/src/bls12_381/tests/mod.rs b/src/bls12_381/tests/mod.rs index 41a60a4..bf6c595 100644 --- a/src/bls12_381/tests/mod.rs +++ b/src/bls12_381/tests/mod.rs @@ -1,5 +1,5 @@ use super::*; -use ::*; +use *; #[test] fn test_pairing_result_against_relic() { diff --git a/src/lib.rs b/src/lib.rs index 5343c6e..08365f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,8 +24,8 @@ pub mod bls12_381; mod wnaf; pub use self::wnaf::Wnaf; -use std::fmt; use std::error::Error; +use std::fmt; use std::io::{self, Read, Write}; /// An "engine" is a collection of types (fields, elliptic curve groups, etc.) diff --git a/src/tests/curve.rs b/src/tests/curve.rs index fa656cc..1480b74 100644 --- a/src/tests/curve.rs +++ b/src/tests/curve.rs @@ -47,7 +47,8 @@ pub fn curve_tests() { { let a = G::rand(&mut rng); let b = a.into_affine().into_projective(); - let c = a.into_affine() + let c = a + .into_affine() .into_projective() .into_affine() .into_projective(); @@ -372,7 +373,8 @@ fn random_transformation_tests() { 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()) .collect::>(); G::batch_normalization(&mut v); diff --git a/src/tests/mod.rs b/src/tests/mod.rs index cf00add..bc83958 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,4 +1,4 @@ pub mod curve; -pub mod field; pub mod engine; +pub mod field; pub mod repr;