2016-08-28 10:30:40 -07:00
|
|
|
#![feature(test)]
|
|
|
|
extern crate test;
|
|
|
|
extern crate rand;
|
|
|
|
extern crate bn;
|
2016-09-18 20:24:38 -07:00
|
|
|
extern crate bincode;
|
2016-08-28 10:30:40 -07:00
|
|
|
|
|
|
|
use bn::*;
|
2016-09-18 20:24:38 -07:00
|
|
|
use bincode::SizeLimit::Infinite;
|
|
|
|
use bincode::rustc_serialize::{encode, decode};
|
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
const SAMPLES: usize = 30;
|
2016-09-18 20:24:38 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
macro_rules! benchmark(
|
|
|
|
($name:ident, $input:ident($rng:ident) = $pre:expr; $post:expr) => (
|
|
|
|
#[bench]
|
|
|
|
fn $name(b: &mut test::Bencher) {
|
|
|
|
let $rng = &mut rand::thread_rng();
|
|
|
|
let $input: Vec<_> = (0..SAMPLES).map(|_| $pre).collect();
|
2016-09-18 20:24:38 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
b.bench_n(SAMPLES as u64, |b| {
|
|
|
|
let mut c = 0;
|
2016-09-18 20:24:38 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
b.iter(|| {
|
|
|
|
c += 1;
|
2016-09-18 20:24:38 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
let $input = &$input[c % SAMPLES];
|
2016-09-18 20:24:38 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
$post
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
2016-09-18 20:24:38 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(g1_serialization,
|
|
|
|
input(rng) = G1::random(rng);
|
2016-09-18 20:24:38 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
encode(input, Infinite).unwrap()
|
|
|
|
);
|
2016-09-18 20:24:38 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(g1_serialization_normalized,
|
|
|
|
input(rng) = {let mut tmp = G1::random(rng); tmp.normalize(); tmp};
|
2016-09-18 20:24:38 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
encode(input, Infinite).unwrap()
|
|
|
|
);
|
2016-09-18 20:24:38 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(g2_serialization,
|
|
|
|
input(rng) = G2::random(rng);
|
2016-09-18 20:24:38 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
encode(input, Infinite).unwrap()
|
|
|
|
);
|
2016-08-28 10:30:40 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(g2_serialization_normalized,
|
|
|
|
input(rng) = {let mut tmp = G2::random(rng); tmp.normalize(); tmp};
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
encode(input, Infinite).unwrap()
|
|
|
|
);
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(g1_deserialization,
|
|
|
|
input(rng) = {encode(&G1::random(rng), Infinite).unwrap()};
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
decode::<G1>(input).unwrap()
|
|
|
|
);
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(g2_deserialization,
|
|
|
|
input(rng) = {encode(&G2::random(rng), Infinite).unwrap()};
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
decode::<G2>(input).unwrap()
|
|
|
|
);
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(fr_addition,
|
|
|
|
input(rng) = (Fr::random(rng), Fr::random(rng));
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
input.0 + input.1
|
|
|
|
);
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(fr_subtraction,
|
|
|
|
input(rng) = (Fr::random(rng), Fr::random(rng));
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
input.0 - input.1
|
|
|
|
);
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(fr_multiplication,
|
|
|
|
input(rng) = (Fr::random(rng), Fr::random(rng));
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
input.0 * input.1
|
|
|
|
);
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(fr_inverses,
|
|
|
|
input(rng) = Fr::random(rng);
|
2016-08-28 10:30:40 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
input.inverse()
|
|
|
|
);
|
2016-08-28 10:30:40 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(g1_addition,
|
|
|
|
input(rng) = (G1::random(rng), G1::random(rng));
|
2016-08-28 10:30:40 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
input.0 + input.1
|
|
|
|
);
|
2016-08-28 10:30:40 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(g1_subtraction,
|
|
|
|
input(rng) = (G1::random(rng), G1::random(rng));
|
2016-08-28 10:30:40 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
input.0 - input.1
|
|
|
|
);
|
2016-08-28 10:30:40 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(g1_scalar_multiplication,
|
|
|
|
input(rng) = (G1::random(rng), Fr::random(rng));
|
2016-08-28 10:30:40 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
input.0 * input.1
|
|
|
|
);
|
2016-08-28 10:30:40 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(g2_addition,
|
|
|
|
input(rng) = (G2::random(rng), G2::random(rng));
|
2016-08-28 10:30:40 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
input.0 + input.1
|
|
|
|
);
|
2016-08-28 10:30:40 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(g2_subtraction,
|
|
|
|
input(rng) = (G2::random(rng), G2::random(rng));
|
2016-08-28 10:30:40 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
input.0 - input.1
|
|
|
|
);
|
2016-08-28 10:30:40 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(g2_scalar_multiplication,
|
|
|
|
input(rng) = (G2::random(rng), Fr::random(rng));
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
input.0 * input.1
|
|
|
|
);
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(fq12_scalar_multiplication,
|
|
|
|
input(rng) = {
|
|
|
|
let g1_1 = G1::random(rng);
|
|
|
|
let g2_1 = G2::random(rng);
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
let g1_2 = G1::random(rng);
|
|
|
|
let g2_2 = G2::random(rng);
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
(pairing(g1_1, g2_1), pairing(g1_2, g2_2))
|
|
|
|
};
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
input.0 * input.1
|
|
|
|
);
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(fq12_exponentiation,
|
|
|
|
input(rng) = ({
|
|
|
|
let g1 = G1::random(rng);
|
|
|
|
let g2 = G2::random(rng);
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
pairing(g1, g2)
|
|
|
|
}, Fr::random(rng));
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
input.0.pow(input.1)
|
|
|
|
);
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
benchmark!(perform_pairing,
|
|
|
|
input(rng) = (G1::random(rng), G2::random(rng));
|
2016-08-28 21:04:46 -07:00
|
|
|
|
2016-10-14 12:10:57 -07:00
|
|
|
pairing(input.0, input.1)
|
|
|
|
);
|