server.rs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. use criterion::{black_box, criterion_group, criterion_main, Criterion};
  2. use pprof::criterion::{Output, PProfProfiler};
  3. use rand::Rng;
  4. use spiral_rs::aligned_memory::AlignedMemory64;
  5. use spiral_rs::client::*;
  6. use spiral_rs::poly::*;
  7. use spiral_rs::server::*;
  8. use spiral_rs::util::*;
  9. use std::time::Duration;
  10. fn criterion_benchmark(c: &mut Criterion) {
  11. let mut group = c.benchmark_group("server");
  12. group
  13. .sample_size(10)
  14. .measurement_time(Duration::from_secs(30));
  15. let params = get_expansion_testing_params();
  16. let v_neg1 = params.get_v_neg1();
  17. let mut seeded_rng = get_seeded_rng();
  18. let mut client = Client::init(&params, &mut seeded_rng);
  19. let public_params = client.generate_keys();
  20. let mut v = Vec::new();
  21. for _ in 0..params.poly_len {
  22. v.push(PolyMatrixNTT::zero(&params, 2, 1));
  23. }
  24. let scale_k = params.modulus / params.pt_modulus;
  25. let mut sigma = PolyMatrixRaw::zero(&params, 1, 1);
  26. sigma.data[7] = scale_k;
  27. v[0] = client.encrypt_matrix_reg(&sigma.ntt());
  28. let v_w_left = public_params.v_expansion_left.unwrap();
  29. let v_w_right = public_params.v_expansion_right.unwrap();
  30. // note: the benchmark on AVX2 is 545ms for the c++ impl
  31. group.bench_function("coefficient_expansion", |b| {
  32. b.iter(|| {
  33. coefficient_expansion(
  34. black_box(&mut v),
  35. black_box(client.g),
  36. black_box(client.stop_round),
  37. black_box(&params),
  38. black_box(&v_w_left),
  39. black_box(&v_w_right),
  40. black_box(&v_neg1),
  41. black_box(params.t_gsw * params.db_dim_2),
  42. )
  43. });
  44. });
  45. let mut seeded_rng = get_seeded_rng();
  46. let trials = params.n * params.n;
  47. let dim0 = 1 << params.db_dim_1;
  48. let num_per = 1 << params.db_dim_2;
  49. let num_items = dim0 * num_per;
  50. let db_size_words = trials * num_items * params.poly_len;
  51. let mut db = vec![0u64; db_size_words];
  52. for i in 0..db_size_words {
  53. db[i] = seeded_rng.gen();
  54. }
  55. let v_reg_sz = dim0 * 2 * params.poly_len;
  56. let mut v_reg_reoriented = AlignedMemory64::new(v_reg_sz);
  57. for i in 0..v_reg_sz {
  58. v_reg_reoriented[i] = seeded_rng.gen();
  59. }
  60. let mut out = Vec::with_capacity(num_per);
  61. for _ in 0..dim0 {
  62. out.push(PolyMatrixNTT::zero(&params, 2, 1));
  63. }
  64. // note: the benchmark on AVX2 is 45ms for the c++ impl
  65. group.bench_function("first_dimension_processing", |b| {
  66. b.iter(|| {
  67. multiply_reg_by_database(
  68. black_box(&mut out),
  69. black_box(db.as_slice()),
  70. black_box(v_reg_reoriented.as_slice()),
  71. black_box(&params),
  72. black_box(dim0),
  73. black_box(num_per)
  74. )
  75. });
  76. });
  77. group.finish();
  78. }
  79. // criterion_group!(benches, criterion_benchmark);
  80. criterion_group! {
  81. name = benches;
  82. config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
  83. targets = criterion_benchmark
  84. }
  85. criterion_main!(benches);