doprf.rs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
  2. use ff::Field;
  3. use oram::doprf::LegendrePrf;
  4. use oram::doprf::{DOPrfParty1, DOPrfParty2, DOPrfParty3};
  5. use oram::doprf::{MaskedDOPrfParty1, MaskedDOPrfParty2, MaskedDOPrfParty3};
  6. use rand::thread_rng;
  7. use utils::field::Fp;
  8. pub fn bench_legendre_prf(c: &mut Criterion) {
  9. let output_bitsize = 64;
  10. let mut group = c.benchmark_group("LegendrePrf");
  11. group.bench_function("keygen", |b| {
  12. b.iter(|| black_box(LegendrePrf::<Fp>::key_gen(output_bitsize)))
  13. });
  14. group.bench_function("eval", |b| {
  15. let key = LegendrePrf::<Fp>::key_gen(output_bitsize);
  16. let x = Fp::random(thread_rng());
  17. b.iter(|| black_box(LegendrePrf::<Fp>::eval_to_uint::<u128>(&key, x)))
  18. });
  19. group.finish();
  20. }
  21. const LOG_NUM_EVALUATIONS: [usize; 4] = [4, 6, 8, 10];
  22. pub fn bench_doprf(c: &mut Criterion) {
  23. let output_bitsize = 128;
  24. let mut group = c.benchmark_group("DOPrf");
  25. let mut party_1 = DOPrfParty1::<Fp>::new(output_bitsize);
  26. let mut party_2 = DOPrfParty2::<Fp>::new(output_bitsize);
  27. let mut party_3 = DOPrfParty3::<Fp>::new(output_bitsize);
  28. group.bench_function("init", |b| {
  29. b.iter(|| {
  30. party_1.reset();
  31. party_2.reset();
  32. party_3.reset();
  33. let (msg_1_2, msg_1_3) = party_1.init_round_0();
  34. let (msg_2_1, msg_2_3) = party_2.init_round_0();
  35. let (msg_3_1, msg_3_2) = party_3.init_round_0();
  36. party_1.init_round_1(msg_2_1, msg_3_1);
  37. party_2.init_round_1(msg_1_2, msg_3_2);
  38. party_3.init_round_1(msg_1_3, msg_2_3);
  39. });
  40. });
  41. {
  42. party_1.reset();
  43. party_2.reset();
  44. party_3.reset();
  45. let (msg_1_2, msg_1_3) = party_1.init_round_0();
  46. let (msg_2_1, msg_2_3) = party_2.init_round_0();
  47. let (msg_3_1, msg_3_2) = party_3.init_round_0();
  48. party_1.init_round_1(msg_2_1, msg_3_1);
  49. party_2.init_round_1(msg_1_2, msg_3_2);
  50. party_3.init_round_1(msg_1_3, msg_2_3);
  51. }
  52. for log_num_evaluations in LOG_NUM_EVALUATIONS {
  53. group.bench_with_input(
  54. BenchmarkId::new("preprocess", log_num_evaluations),
  55. &log_num_evaluations,
  56. |b, &log_num_evaluations| {
  57. let num = 1 << log_num_evaluations;
  58. b.iter(|| {
  59. party_1.reset_preprocessing();
  60. party_2.reset_preprocessing();
  61. party_3.reset_preprocessing();
  62. let (msg_1_2, msg_1_3) = party_1.preprocess_round_0(num);
  63. let (msg_2_1, msg_2_3) = party_2.preprocess_round_0(num);
  64. let (msg_3_1, msg_3_2) = party_3.preprocess_round_0(num);
  65. party_1.preprocess_round_1(num, msg_2_1, msg_3_1);
  66. party_2.preprocess_round_1(num, msg_1_2, msg_3_2);
  67. party_3.preprocess_round_1(num, msg_1_3, msg_2_3);
  68. });
  69. },
  70. );
  71. }
  72. for log_num_evaluations in LOG_NUM_EVALUATIONS {
  73. group.bench_with_input(
  74. BenchmarkId::new("preprocess+eval", log_num_evaluations),
  75. &log_num_evaluations,
  76. |b, &log_num_evaluations| {
  77. let num = 1 << log_num_evaluations;
  78. let shares_1: Vec<Fp> = (0..num).map(|_| Fp::random(thread_rng())).collect();
  79. let shares_2: Vec<Fp> = (0..num).map(|_| Fp::random(thread_rng())).collect();
  80. let shares_3: Vec<Fp> = (0..num).map(|_| Fp::random(thread_rng())).collect();
  81. b.iter(|| {
  82. let (msg_1_2, msg_1_3) = party_1.preprocess_round_0(num);
  83. let (msg_2_1, msg_2_3) = party_2.preprocess_round_0(num);
  84. let (msg_3_1, msg_3_2) = party_3.preprocess_round_0(num);
  85. party_1.preprocess_round_1(num, msg_2_1, msg_3_1);
  86. party_2.preprocess_round_1(num, msg_1_2, msg_3_2);
  87. party_3.preprocess_round_1(num, msg_1_3, msg_2_3);
  88. let (msg_2_1, msg_2_3) = party_2.eval_round_0(num, &shares_2);
  89. let (msg_3_1, _) = party_3.eval_round_0(num, &shares_3);
  90. let (_, msg_1_3) = party_1.eval_round_1(num, &shares_1, &msg_2_1, &msg_3_1);
  91. let _output = party_3.eval_round_2(num, &shares_3, msg_1_3, msg_2_3);
  92. });
  93. },
  94. );
  95. }
  96. group.finish();
  97. }
  98. pub fn bench_masked_doprf(c: &mut Criterion) {
  99. let output_bitsize = 128;
  100. let mut group = c.benchmark_group("MaskedDOPrf");
  101. let mut party_1 = MaskedDOPrfParty1::<Fp>::new(output_bitsize);
  102. let mut party_2 = MaskedDOPrfParty2::<Fp>::new(output_bitsize);
  103. let mut party_3 = MaskedDOPrfParty3::<Fp>::new(output_bitsize);
  104. group.bench_function("init", |b| {
  105. b.iter(|| {
  106. party_1.reset();
  107. party_2.reset();
  108. party_3.reset();
  109. let (msg_1_2, msg_1_3) = party_1.init_round_0();
  110. let (msg_2_1, msg_2_3) = party_2.init_round_0();
  111. let (msg_3_1, msg_3_2) = party_3.init_round_0();
  112. party_1.init_round_1(msg_2_1, msg_3_1);
  113. party_2.init_round_1(msg_1_2, msg_3_2);
  114. party_3.init_round_1(msg_1_3, msg_2_3);
  115. });
  116. });
  117. {
  118. party_1.reset();
  119. party_2.reset();
  120. party_3.reset();
  121. let (msg_1_2, msg_1_3) = party_1.init_round_0();
  122. let (msg_2_1, msg_2_3) = party_2.init_round_0();
  123. let (msg_3_1, msg_3_2) = party_3.init_round_0();
  124. party_1.init_round_1(msg_2_1, msg_3_1);
  125. party_2.init_round_1(msg_1_2, msg_3_2);
  126. party_3.init_round_1(msg_1_3, msg_2_3);
  127. }
  128. for log_num_evaluations in LOG_NUM_EVALUATIONS {
  129. group.bench_with_input(
  130. BenchmarkId::new("preprocess", log_num_evaluations),
  131. &log_num_evaluations,
  132. |b, &log_num_evaluations| {
  133. let num = 1 << log_num_evaluations;
  134. b.iter(|| {
  135. party_1.reset_preprocessing();
  136. party_2.reset_preprocessing();
  137. party_3.reset_preprocessing();
  138. let (msg_1_2, msg_1_3) = party_1.preprocess_round_0(num);
  139. let (msg_2_1, msg_2_3) = party_2.preprocess_round_0(num);
  140. let (msg_3_1, msg_3_2) = party_3.preprocess_round_0(num);
  141. party_1.preprocess_round_1(num, msg_2_1, msg_3_1);
  142. party_2.preprocess_round_1(num, msg_1_2, msg_3_2);
  143. party_3.preprocess_round_1(num, msg_1_3, msg_2_3);
  144. });
  145. },
  146. );
  147. }
  148. for log_num_evaluations in LOG_NUM_EVALUATIONS {
  149. group.bench_with_input(
  150. BenchmarkId::new("preprocess+eval", log_num_evaluations),
  151. &log_num_evaluations,
  152. |b, &log_num_evaluations| {
  153. let num = 1 << log_num_evaluations;
  154. let shares_1: Vec<Fp> = (0..num).map(|_| Fp::random(thread_rng())).collect();
  155. let shares_2: Vec<Fp> = (0..num).map(|_| Fp::random(thread_rng())).collect();
  156. let shares_3: Vec<Fp> = (0..num).map(|_| Fp::random(thread_rng())).collect();
  157. b.iter(|| {
  158. let (msg_1_2, msg_1_3) = party_1.preprocess_round_0(num);
  159. let (msg_2_1, msg_2_3) = party_2.preprocess_round_0(num);
  160. let (msg_3_1, msg_3_2) = party_3.preprocess_round_0(num);
  161. party_1.preprocess_round_1(num, msg_2_1, msg_3_1);
  162. party_2.preprocess_round_1(num, msg_1_2, msg_3_2);
  163. party_3.preprocess_round_1(num, msg_1_3, msg_2_3);
  164. let (_, msg_1_3) = party_1.eval_round_0(num, &shares_1);
  165. let (_, msg_2_3) = party_2.eval_round_0(num, &shares_2);
  166. let (msg_3_1, _) = party_3.eval_round_1(num, &shares_3, &msg_1_3, &msg_2_3);
  167. let _masked_output = party_1.eval_round_2(num, &shares_1, (), msg_3_1);
  168. let _mask2 = party_2.eval_get_output(num);
  169. let _mask3 = party_3.eval_get_output(num);
  170. });
  171. },
  172. );
  173. }
  174. group.finish();
  175. }
  176. criterion_group!(
  177. name = benches;
  178. config = Criterion::default().sample_size(10);
  179. targets = bench_legendre_prf, bench_doprf, bench_masked_doprf
  180. );
  181. criterion_main!(benches);