bench_doram.rs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. use clap::{CommandFactory, Parser};
  2. use communicator::tcp::{make_tcp_communicator, NetworkOptions, NetworkPartyInfo};
  3. use communicator::AbstractCommunicator;
  4. use cuckoo::hash::AesHashFunction;
  5. use dpf::mpdpf::SmartMpDpf;
  6. use dpf::spdpf::HalfTreeSpDpf;
  7. use ff::{Field, PrimeField};
  8. use oram::common::{InstructionShare, Operation};
  9. use oram::oram::{DistributedOram, DistributedOramProtocol, Runtimes};
  10. use rand::{Rng, SeedableRng};
  11. use rand_chacha::ChaChaRng;
  12. use rayon;
  13. use std::process;
  14. use std::time::Instant;
  15. use utils::field::Fp;
  16. type MPDPF = SmartMpDpf<Fp, HalfTreeSpDpf<Fp>, AesHashFunction<u16>>;
  17. type DOram = DistributedOramProtocol<Fp, MPDPF, HalfTreeSpDpf<Fp>>;
  18. #[derive(Debug, clap::Parser)]
  19. struct Cli {
  20. /// ID of this party
  21. #[arg(long, short = 'i', value_parser = clap::value_parser!(u32).range(0..3))]
  22. pub party_id: u32,
  23. /// Log2 of the database size, must be even
  24. #[arg(long, short = 's', value_parser = parse_log_db_size)]
  25. pub log_db_size: u32,
  26. /// Use preprocessing
  27. #[arg(long)]
  28. pub preprocess: bool,
  29. /// How many threads to use for the computation
  30. #[arg(long, short = 't', default_value_t = 1)]
  31. pub threads: usize,
  32. /// Which address to listen on for incoming connections
  33. #[arg(long, short = 'l')]
  34. pub listen_host: String,
  35. /// Which port to listen on for incoming connections
  36. #[arg(long, short = 'p', value_parser = clap::value_parser!(u16).range(1..))]
  37. pub listen_port: u16,
  38. /// Connection info for each party
  39. #[arg(long, short = 'c', value_name = "PARTY_ID>:<HOST>:<PORT", value_parser = parse_connect)]
  40. pub connect: Vec<(usize, String, u16)>,
  41. /// How long to try connecting before aborting
  42. #[arg(long, default_value_t = 10)]
  43. pub connect_timeout_seconds: usize,
  44. }
  45. fn parse_log_db_size(s: &str) -> Result<u32, Box<dyn std::error::Error + Send + Sync + 'static>> {
  46. let log_db_size: u32 = s.parse()?;
  47. if log_db_size & 1 == 1 {
  48. return Err(clap::Error::raw(
  49. clap::error::ErrorKind::InvalidValue,
  50. format!("log_db_size must be even"),
  51. )
  52. .into());
  53. }
  54. Ok(log_db_size)
  55. }
  56. fn parse_connect(
  57. s: &str,
  58. ) -> Result<(usize, String, u16), Box<dyn std::error::Error + Send + Sync + 'static>> {
  59. let parts: Vec<_> = s.split(":").collect();
  60. if parts.len() != 3 {
  61. return Err(clap::Error::raw(
  62. clap::error::ErrorKind::ValueValidation,
  63. format!("'{}' has not the format '<party-id>:<host>:<post>'", s),
  64. )
  65. .into());
  66. }
  67. let party_id: usize = parts[0].parse()?;
  68. let host = parts[1];
  69. let port: u16 = parts[2].parse()?;
  70. if port == 0 {
  71. return Err(clap::Error::raw(
  72. clap::error::ErrorKind::ValueValidation,
  73. "the port needs to be positive",
  74. )
  75. .into());
  76. }
  77. Ok((party_id, host.to_owned(), port))
  78. }
  79. fn main() {
  80. let cli = Cli::parse();
  81. let mut netopts = NetworkOptions {
  82. listen_host: cli.listen_host,
  83. listen_port: cli.listen_port,
  84. connect_info: vec![NetworkPartyInfo::Listen; 3],
  85. connect_timeout_seconds: cli.connect_timeout_seconds,
  86. };
  87. rayon::ThreadPoolBuilder::new()
  88. .num_threads(cli.threads)
  89. .build_global()
  90. .unwrap();
  91. for c in cli.connect {
  92. if netopts.connect_info[c.0] != NetworkPartyInfo::Listen {
  93. println!(
  94. "{}",
  95. clap::Error::raw(
  96. clap::error::ErrorKind::ValueValidation,
  97. format!("multiple connect arguments for party {}", c.0),
  98. )
  99. .format(&mut Cli::command())
  100. );
  101. process::exit(1);
  102. }
  103. netopts.connect_info[c.0] = NetworkPartyInfo::Connect(c.1, c.2);
  104. }
  105. let mut comm = match make_tcp_communicator(3, cli.party_id as usize, &netopts) {
  106. Ok(comm) => comm,
  107. Err(e) => {
  108. eprintln!("network setup failed: {:?}", e);
  109. process::exit(1);
  110. }
  111. };
  112. let mut doram = DOram::new(cli.party_id as usize, cli.log_db_size);
  113. let db_size = 1 << cli.log_db_size;
  114. let db_share: Vec<_> = vec![Fp::ZERO; db_size];
  115. let stash_size = 1 << (cli.log_db_size >> 1);
  116. let instructions = if cli.party_id == 0 {
  117. let mut rng = ChaChaRng::from_seed([0u8; 32]);
  118. (0..stash_size)
  119. .map(|_| InstructionShare {
  120. operation: Operation::Write.encode(),
  121. address: Fp::from_u128(rng.gen_range(0..db_size) as u128),
  122. value: Fp::random(&mut rng),
  123. })
  124. .collect()
  125. } else {
  126. vec![
  127. InstructionShare {
  128. operation: Fp::ZERO,
  129. address: Fp::ZERO,
  130. value: Fp::ZERO
  131. };
  132. stash_size
  133. ]
  134. };
  135. let t_start = Instant::now();
  136. doram.init(&mut comm, &db_share).expect("init failed");
  137. let d_init = Instant::now() - t_start;
  138. let mut runtimes = Runtimes::default();
  139. let d_preprocess = if cli.preprocess {
  140. let t_start = Instant::now();
  141. runtimes = doram
  142. .preprocess_with_runtimes(&mut comm, 1, Some(runtimes))
  143. .expect("preprocess failed")
  144. .unwrap();
  145. t_start.elapsed()
  146. } else {
  147. Default::default()
  148. };
  149. let t_start = Instant::now();
  150. for (_i, inst) in instructions.iter().enumerate() {
  151. // println!("executing instruction #{i}: {inst:?}");
  152. runtimes = doram
  153. .access_with_runtimes(&mut comm, *inst, Some(runtimes))
  154. .expect("access failed")
  155. .1
  156. .unwrap();
  157. }
  158. let d_accesses = Instant::now() - t_start;
  159. println!(
  160. "time init: {:10.3} ms",
  161. d_init.as_secs_f64() * 1000.0
  162. );
  163. println!(
  164. "time preprocess: {:10.3} ms",
  165. d_preprocess.as_secs_f64() * 1000.0
  166. );
  167. println!(
  168. " per accesses: {:10.3} ms",
  169. d_preprocess.as_secs_f64() * 1000.0 / stash_size as f64
  170. );
  171. println!(
  172. "time accesses: {:10.3} ms{}",
  173. d_accesses.as_secs_f64() * 1000.0,
  174. if cli.preprocess {
  175. " (online only)"
  176. } else {
  177. ""
  178. }
  179. );
  180. println!(
  181. " per accesses: {:10.3} ms",
  182. d_accesses.as_secs_f64() * 1000.0 / stash_size as f64
  183. );
  184. comm.shutdown();
  185. runtimes.print(cli.party_id as usize + 1, stash_size);
  186. }