bench_doram.rs 6.0 KB

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