bench_doprf.rs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. //! Benchmarking program for the DOPRF protocols.
  2. //!
  3. //! Use --help to see available options.
  4. use clap::{CommandFactory, Parser};
  5. use communicator::tcp::{make_tcp_communicator, NetworkOptions, NetworkPartyInfo};
  6. use communicator::AbstractCommunicator;
  7. use ff::Field;
  8. use oram::doprf::{
  9. DOPrfParty1, DOPrfParty2, DOPrfParty3, JointDOPrf, MaskedDOPrfParty1, MaskedDOPrfParty2,
  10. MaskedDOPrfParty3,
  11. };
  12. use rand::SeedableRng;
  13. use rand_chacha::ChaChaRng;
  14. use std::process;
  15. use std::time::{Duration, Instant};
  16. use utils::field::Fp;
  17. const PARTY_1: usize = 0;
  18. const PARTY_2: usize = 1;
  19. const PARTY_3: usize = 2;
  20. #[derive(Clone, Copy, Debug, PartialEq, Eq, clap::ValueEnum, strum_macros::Display)]
  21. enum Mode {
  22. Alternating,
  23. Joint,
  24. Masked,
  25. Plain,
  26. }
  27. #[derive(Debug, clap::Parser)]
  28. struct Cli {
  29. /// ID of this party
  30. #[arg(long, short = 'i', value_parser = clap::value_parser!(u32).range(0..3))]
  31. pub party_id: u32,
  32. /// Output bitsize of the DOPrf
  33. #[arg(long, short = 's', value_parser = clap::value_parser!(u32).range(1..))]
  34. pub bitsize: u32,
  35. /// Number of evaluations to compute
  36. #[arg(long, short = 'n', value_parser = clap::value_parser!(u32).range(1..))]
  37. pub num_evaluations: u32,
  38. /// Which protocol variant to benchmark
  39. #[arg(long, short = 'm', value_enum)]
  40. pub mode: Mode,
  41. /// Which address to listen on for incoming connections
  42. #[arg(long, short = 'l')]
  43. pub listen_host: String,
  44. /// Which port to listen on for incoming connections
  45. #[arg(long, short = 'p', value_parser = clap::value_parser!(u16).range(1..))]
  46. pub listen_port: u16,
  47. /// Connection info for each party
  48. #[arg(long, short = 'c', value_name = "PARTY_ID>:<HOST>:<PORT", value_parser = parse_connect)]
  49. pub connect: Vec<(usize, String, u16)>,
  50. /// How long to try connecting before aborting
  51. #[arg(long, short = 't', default_value_t = 10)]
  52. pub connect_timeout_seconds: usize,
  53. }
  54. fn parse_connect(
  55. s: &str,
  56. ) -> Result<(usize, String, u16), Box<dyn std::error::Error + Send + Sync + 'static>> {
  57. let parts: Vec<_> = s.split(":").collect();
  58. if parts.len() != 3 {
  59. return Err(clap::Error::raw(
  60. clap::error::ErrorKind::ValueValidation,
  61. format!("'{}' has not the format '<party-id>:<host>:<post>'", s),
  62. )
  63. .into());
  64. }
  65. let party_id: usize = parts[0].parse()?;
  66. let host = parts[1];
  67. let port: u16 = parts[2].parse()?;
  68. if port == 0 {
  69. return Err(clap::Error::raw(
  70. clap::error::ErrorKind::ValueValidation,
  71. "the port needs to be positive",
  72. )
  73. .into());
  74. }
  75. Ok((party_id, host.to_owned(), port))
  76. }
  77. fn make_random_shares(n: usize) -> Vec<Fp> {
  78. let mut rng = ChaChaRng::from_seed([0u8; 32]);
  79. (0..n).map(|_| Fp::random(&mut rng)).collect()
  80. }
  81. fn bench_plain<C: AbstractCommunicator>(
  82. comm: &mut C,
  83. bitsize: usize,
  84. num_evaluations: usize,
  85. ) -> (Duration, Duration, Duration) {
  86. let shares = make_random_shares(num_evaluations);
  87. match comm.get_my_id() {
  88. PARTY_1 => {
  89. let mut p1 = DOPrfParty1::<Fp>::new(bitsize);
  90. let t_start = Instant::now();
  91. p1.init(comm).expect("init failed");
  92. let t_after_init = Instant::now();
  93. p1.preprocess(comm, num_evaluations)
  94. .expect("preprocess failed");
  95. let t_after_preprocess = Instant::now();
  96. for i in 0..num_evaluations {
  97. p1.eval(comm, 1, &[shares[i]]).expect("eval failed");
  98. }
  99. let t_after_eval = Instant::now();
  100. (
  101. t_after_init - t_start,
  102. t_after_preprocess - t_after_init,
  103. t_after_eval - t_after_preprocess,
  104. )
  105. }
  106. PARTY_2 => {
  107. let mut p2 = DOPrfParty2::<Fp>::new(bitsize);
  108. let t_start = Instant::now();
  109. p2.init(comm).expect("init failed");
  110. let t_after_init = Instant::now();
  111. p2.preprocess(comm, num_evaluations)
  112. .expect("preprocess failed");
  113. let t_after_preprocess = Instant::now();
  114. for i in 0..num_evaluations {
  115. p2.eval(comm, 1, &[shares[i]]).expect("eval failed");
  116. }
  117. let t_after_eval = Instant::now();
  118. (
  119. t_after_init - t_start,
  120. t_after_preprocess - t_after_init,
  121. t_after_eval - t_after_preprocess,
  122. )
  123. }
  124. PARTY_3 => {
  125. let mut p3 = DOPrfParty3::<Fp>::new(bitsize);
  126. let t_start = Instant::now();
  127. p3.init(comm).expect("init failed");
  128. let t_after_init = Instant::now();
  129. p3.preprocess(comm, num_evaluations)
  130. .expect("preprocess failed");
  131. let t_after_preprocess = Instant::now();
  132. for i in 0..num_evaluations {
  133. p3.eval(comm, 1, &[shares[i]]).expect("eval failed");
  134. }
  135. let t_after_eval = Instant::now();
  136. (
  137. t_after_init - t_start,
  138. t_after_preprocess - t_after_init,
  139. t_after_eval - t_after_preprocess,
  140. )
  141. }
  142. _ => panic!("invalid party id"),
  143. }
  144. }
  145. fn bench_masked<C: AbstractCommunicator>(
  146. comm: &mut C,
  147. bitsize: usize,
  148. num_evaluations: usize,
  149. ) -> (Duration, Duration, Duration) {
  150. let shares = make_random_shares(num_evaluations);
  151. match comm.get_my_id() {
  152. PARTY_1 => {
  153. let mut p1 = MaskedDOPrfParty1::<Fp>::new(bitsize);
  154. let t_start = Instant::now();
  155. p1.init(comm).expect("init failed");
  156. let t_after_init = Instant::now();
  157. p1.preprocess(comm, num_evaluations)
  158. .expect("preprocess failed");
  159. let t_after_preprocess = Instant::now();
  160. for i in 0..num_evaluations {
  161. p1.eval(comm, 1, &[shares[i]]).expect("eval failed");
  162. }
  163. let t_after_eval = Instant::now();
  164. (
  165. t_after_init - t_start,
  166. t_after_preprocess - t_after_init,
  167. t_after_eval - t_after_preprocess,
  168. )
  169. }
  170. PARTY_2 => {
  171. let mut p2 = MaskedDOPrfParty2::<Fp>::new(bitsize);
  172. let t_start = Instant::now();
  173. p2.init(comm).expect("init failed");
  174. let t_after_init = Instant::now();
  175. p2.preprocess(comm, num_evaluations)
  176. .expect("preprocess failed");
  177. let t_after_preprocess = Instant::now();
  178. for i in 0..num_evaluations {
  179. p2.eval(comm, 1, &[shares[i]]).expect("eval failed");
  180. }
  181. let t_after_eval = Instant::now();
  182. (
  183. t_after_init - t_start,
  184. t_after_preprocess - t_after_init,
  185. t_after_eval - t_after_preprocess,
  186. )
  187. }
  188. PARTY_3 => {
  189. let mut p3 = MaskedDOPrfParty3::<Fp>::new(bitsize);
  190. let t_start = Instant::now();
  191. p3.init(comm).expect("init failed");
  192. let t_after_init = Instant::now();
  193. p3.preprocess(comm, num_evaluations)
  194. .expect("preprocess failed");
  195. let t_after_preprocess = Instant::now();
  196. for i in 0..num_evaluations {
  197. p3.eval(comm, 1, &[shares[i]]).expect("eval failed");
  198. }
  199. let t_after_eval = Instant::now();
  200. (
  201. t_after_init - t_start,
  202. t_after_preprocess - t_after_init,
  203. t_after_eval - t_after_preprocess,
  204. )
  205. }
  206. _ => panic!("invalid party id"),
  207. }
  208. }
  209. fn bench_joint<C: AbstractCommunicator>(
  210. comm: &mut C,
  211. bitsize: usize,
  212. num_evaluations: usize,
  213. ) -> (Duration, Duration, Duration) {
  214. let shares = make_random_shares(num_evaluations);
  215. let mut p = JointDOPrf::<Fp>::new(bitsize);
  216. let t_start = Instant::now();
  217. p.init(comm).expect("init failed");
  218. let t_after_init = Instant::now();
  219. p.preprocess(comm, num_evaluations)
  220. .expect("preprocess failed");
  221. let t_after_preprocess = Instant::now();
  222. for i in 0..num_evaluations {
  223. p.eval_to_uint::<_, u128>(comm, &[shares[i]])
  224. .expect("eval failed");
  225. }
  226. let t_after_eval = Instant::now();
  227. (
  228. t_after_init - t_start,
  229. t_after_preprocess - t_after_init,
  230. t_after_eval - t_after_preprocess,
  231. )
  232. }
  233. fn bench_alternating<C: AbstractCommunicator>(
  234. comm: &mut C,
  235. bitsize: usize,
  236. num_evaluations: usize,
  237. ) -> (Duration, Duration, Duration) {
  238. let shares = make_random_shares(num_evaluations);
  239. let mut p1 = DOPrfParty1::<Fp>::new(bitsize);
  240. let mut p2 = DOPrfParty2::<Fp>::new(bitsize);
  241. let mut p3 = DOPrfParty3::<Fp>::new(bitsize);
  242. match comm.get_my_id() {
  243. PARTY_1 => {
  244. let t_start = Instant::now();
  245. p1.init(comm).expect("init failed");
  246. p2.init(comm).expect("init failed");
  247. p3.init(comm).expect("init failed");
  248. let t_after_init = Instant::now();
  249. p1.preprocess(comm, num_evaluations)
  250. .expect("preprocess failed");
  251. p2.preprocess(comm, num_evaluations)
  252. .expect("preprocess failed");
  253. p3.preprocess(comm, num_evaluations)
  254. .expect("preprocess failed");
  255. let t_after_preprocess = Instant::now();
  256. for i in 0..num_evaluations {
  257. p1.eval(comm, 1, &[shares[i]]).expect("eval failed");
  258. p2.eval(comm, 1, &[shares[i]]).expect("eval failed");
  259. p3.eval(comm, 1, &[shares[i]]).expect("eval failed");
  260. }
  261. let t_after_eval = Instant::now();
  262. (
  263. t_after_init - t_start,
  264. t_after_preprocess - t_after_init,
  265. t_after_eval - t_after_preprocess,
  266. )
  267. }
  268. PARTY_2 => {
  269. let t_start = Instant::now();
  270. p2.init(comm).expect("init failed");
  271. p3.init(comm).expect("init failed");
  272. p1.init(comm).expect("init failed");
  273. let t_after_init = Instant::now();
  274. p2.preprocess(comm, num_evaluations)
  275. .expect("preprocess failed");
  276. p3.preprocess(comm, num_evaluations)
  277. .expect("preprocess failed");
  278. p1.preprocess(comm, num_evaluations)
  279. .expect("preprocess failed");
  280. let t_after_preprocess = Instant::now();
  281. for i in 0..num_evaluations {
  282. p2.eval(comm, 1, &[shares[i]]).expect("eval failed");
  283. p3.eval(comm, 1, &[shares[i]]).expect("eval failed");
  284. p1.eval(comm, 1, &[shares[i]]).expect("eval failed");
  285. }
  286. let t_after_eval = Instant::now();
  287. (
  288. t_after_init - t_start,
  289. t_after_preprocess - t_after_init,
  290. t_after_eval - t_after_preprocess,
  291. )
  292. }
  293. PARTY_3 => {
  294. let t_start = Instant::now();
  295. p3.init(comm).expect("init failed");
  296. p1.init(comm).expect("init failed");
  297. p2.init(comm).expect("init failed");
  298. let t_after_init = Instant::now();
  299. p3.preprocess(comm, num_evaluations)
  300. .expect("preprocess failed");
  301. p1.preprocess(comm, num_evaluations)
  302. .expect("preprocess failed");
  303. p2.preprocess(comm, num_evaluations)
  304. .expect("preprocess failed");
  305. let t_after_preprocess = Instant::now();
  306. for i in 0..num_evaluations {
  307. p3.eval(comm, 1, &[shares[i]]).expect("eval failed");
  308. p1.eval(comm, 1, &[shares[i]]).expect("eval failed");
  309. p2.eval(comm, 1, &[shares[i]]).expect("eval failed");
  310. }
  311. let t_after_eval = Instant::now();
  312. (
  313. t_after_init - t_start,
  314. t_after_preprocess - t_after_init,
  315. t_after_eval - t_after_preprocess,
  316. )
  317. }
  318. _ => panic!("invalid party id"),
  319. }
  320. }
  321. fn main() {
  322. let cli = Cli::parse();
  323. let mut netopts = NetworkOptions {
  324. listen_host: cli.listen_host,
  325. listen_port: cli.listen_port,
  326. connect_info: vec![NetworkPartyInfo::Listen; 3],
  327. connect_timeout_seconds: cli.connect_timeout_seconds,
  328. };
  329. for c in cli.connect {
  330. if netopts.connect_info[c.0] != NetworkPartyInfo::Listen {
  331. println!(
  332. "{}",
  333. clap::Error::raw(
  334. clap::error::ErrorKind::ValueValidation,
  335. format!("multiple connect arguments for party {}", c.0),
  336. )
  337. .format(&mut Cli::command())
  338. );
  339. process::exit(1);
  340. }
  341. netopts.connect_info[c.0] = NetworkPartyInfo::Connect(c.1, c.2);
  342. }
  343. let mut comm = match make_tcp_communicator(3, cli.party_id as usize, &netopts) {
  344. Ok(comm) => comm,
  345. Err(e) => {
  346. eprintln!("network setup failed: {:?}", e);
  347. process::exit(1);
  348. }
  349. };
  350. let (d_init, d_preprocess, d_eval) = match cli.mode {
  351. Mode::Plain => bench_plain(
  352. &mut comm,
  353. cli.bitsize as usize,
  354. cli.num_evaluations as usize,
  355. ),
  356. Mode::Masked => bench_masked(
  357. &mut comm,
  358. cli.bitsize as usize,
  359. cli.num_evaluations as usize,
  360. ),
  361. Mode::Joint => bench_joint(
  362. &mut comm,
  363. cli.bitsize as usize,
  364. cli.num_evaluations as usize,
  365. ),
  366. Mode::Alternating => bench_alternating(
  367. &mut comm,
  368. cli.bitsize as usize,
  369. cli.num_evaluations as usize,
  370. ),
  371. };
  372. comm.shutdown();
  373. println!("=========== DOPrf ============");
  374. println!("mode: {}", cli.mode);
  375. println!("- {} bit output", cli.bitsize);
  376. println!("- {} evaluations", cli.num_evaluations);
  377. println!("time init: {:3.3} s", d_init.as_secs_f64());
  378. println!("time preprocess: {:3.3} s", d_preprocess.as_secs_f64());
  379. println!(
  380. " per evaluation: {:3.3} s",
  381. d_preprocess.as_secs_f64() / cli.num_evaluations as f64
  382. );
  383. println!("time eval: {:3.3} s", d_eval.as_secs_f64());
  384. println!(
  385. " per evaluation: {:3.3} s",
  386. d_eval.as_secs_f64() / cli.num_evaluations as f64
  387. );
  388. println!("==============================");
  389. }