use rand_core::SeedableRng; use rand_xorshift::XorShiftRng; use blstrs::*; use ff::{Field, PrimeField}; #[bench] fn bench_scalar_add_assign(b: &mut ::test::Bencher) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, 0xe5, ]); let v: Vec<(Scalar, Scalar)> = (0..SAMPLES) .map(|_| (Scalar::random(&mut rng), Scalar::random(&mut rng))) .collect(); let mut count = 0; b.iter(|| { let mut tmp = v[count].0; tmp += &v[count].1; count = (count + 1) % SAMPLES; tmp }); } #[bench] fn bench_scalar_sub_assign(b: &mut ::test::Bencher) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, 0xe5, ]); let v: Vec<(Scalar, Scalar)> = (0..SAMPLES) .map(|_| (Scalar::random(&mut rng), Scalar::random(&mut rng))) .collect(); let mut count = 0; b.iter(|| { let mut tmp = v[count].0; tmp -= &v[count].1; count = (count + 1) % SAMPLES; tmp }); } #[bench] fn bench_scalar_mul_assign(b: &mut ::test::Bencher) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, 0xe5, ]); let v: Vec<(Scalar, Scalar)> = (0..SAMPLES) .map(|_| (Scalar::random(&mut rng), Scalar::random(&mut rng))) .collect(); let mut count = 0; b.iter(|| { let mut tmp = v[count].0; tmp *= &v[count].1; count = (count + 1) % SAMPLES; tmp }); } #[bench] fn bench_scalar_square(b: &mut ::test::Bencher) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, 0xe5, ]); let v: Vec = (0..SAMPLES).map(|_| Scalar::random(&mut rng)).collect(); let mut count = 0; b.iter(|| { let tmp = v[count].square(); count = (count + 1) % SAMPLES; tmp }); } #[bench] fn bench_scalar_inverse(b: &mut ::test::Bencher) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, 0xe5, ]); let v: Vec = (0..SAMPLES).map(|_| Scalar::random(&mut rng)).collect(); let mut count = 0; b.iter(|| { count = (count + 1) % SAMPLES; v[count].invert() }); } #[bench] fn bench_scalar_negate(b: &mut ::test::Bencher) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, 0xe5, ]); let v: Vec = (0..SAMPLES).map(|_| Scalar::random(&mut rng)).collect(); let mut count = 0; b.iter(|| { let tmp = -v[count]; count = (count + 1) % SAMPLES; tmp }); } #[bench] fn bench_scalar_sqrt(b: &mut ::test::Bencher) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, 0xe5, ]); let v: Vec = (0..SAMPLES) .map(|_| Scalar::random(&mut rng).square()) .collect(); let mut count = 0; b.iter(|| { count = (count + 1) % SAMPLES; v[count].sqrt() }); } #[bench] fn bench_scalar_to_repr(b: &mut ::test::Bencher) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, 0xe5, ]); let v: Vec = (0..SAMPLES).map(|_| Scalar::random(&mut rng)).collect(); let mut count = 0; b.iter(|| { count = (count + 1) % SAMPLES; v[count].to_repr() }); } #[bench] fn bench_scalar_from_repr(b: &mut ::test::Bencher) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, 0xe5, ]); let v: Vec<[u8; 32]> = (0..SAMPLES) .map(|_| Scalar::random(&mut rng).to_repr()) .collect(); let mut count = 0; b.iter(|| { count = (count + 1) % SAMPLES; Scalar::from_repr(v[count]) }); } #[bench] fn bench_scalar_from_repr_vartime(b: &mut ::test::Bencher) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, 0xe5, ]); let v: Vec<[u8; 32]> = (0..SAMPLES) .map(|_| Scalar::random(&mut rng).to_repr()) .collect(); let mut count = 0; b.iter(|| { count = (count + 1) % SAMPLES; Scalar::from_repr_vartime(v[count]) }); }