field.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. use bincode;
  2. use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
  3. use ff::Field;
  4. use rand::thread_rng;
  5. use utils::field::{legendre_symbol_exp, legendre_symbol_rug, Fp};
  6. const VEC_LENS: [usize; 4] = [1, 4, 16, 64];
  7. pub fn bench_encode(c: &mut Criterion) {
  8. for vec_len in VEC_LENS {
  9. c.bench_with_input(
  10. BenchmarkId::new("Fp::encode", vec_len),
  11. &vec_len,
  12. |b, &vec_len| {
  13. let x: Vec<_> = (0..vec_len).map(|_| Fp::random(thread_rng())).collect();
  14. b.iter(|| {
  15. black_box(
  16. bincode::encode_to_vec(&x, bincode::config::standard())
  17. .expect("encode failed"),
  18. )
  19. });
  20. },
  21. );
  22. }
  23. }
  24. pub fn bench_decode(c: &mut Criterion) {
  25. for vec_len in VEC_LENS {
  26. c.bench_with_input(
  27. BenchmarkId::new("Fp::decode", vec_len),
  28. &vec_len,
  29. |b, &vec_len| {
  30. let x: Vec<_> = (0..vec_len).map(|_| Fp::random(thread_rng())).collect();
  31. let bytes =
  32. bincode::encode_to_vec(&x, bincode::config::standard()).expect("encode failed");
  33. b.iter(|| {
  34. black_box(
  35. bincode::decode_from_slice::<Vec<Fp>, _>(
  36. &bytes,
  37. bincode::config::standard(),
  38. )
  39. .expect("decode failed"),
  40. )
  41. });
  42. },
  43. );
  44. }
  45. }
  46. pub fn bench_legendre_symbol(c: &mut Criterion) {
  47. let mut g = c.benchmark_group("LegendreSymbol");
  48. g.bench_function("exp", |b| {
  49. let x = Fp::random(thread_rng());
  50. b.iter(|| black_box(legendre_symbol_exp(x)));
  51. });
  52. g.bench_function("rug", |b| {
  53. let x = Fp::random(thread_rng());
  54. b.iter(|| black_box(legendre_symbol_rug(x)));
  55. });
  56. }
  57. criterion_group!(benches, bench_encode, bench_decode, bench_legendre_symbol);
  58. criterion_main!(benches);