main.rs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // We really want points to be capital letters and scalars to be
  2. // lowercase letters
  3. #![allow(non_snake_case)]
  4. use std::env;
  5. use std::time::Instant;
  6. use spiral_spir::client::Client;
  7. use spiral_spir::init;
  8. use spiral_spir::server::Server;
  9. fn main() {
  10. let args: Vec<String> = env::args().collect();
  11. if args.len() < 2 || args.len() > 5 {
  12. println!(
  13. "Usage: {} r [num_threads [num_preproc [num_pirs]]]\nr = log_2(num_records)",
  14. args[0]
  15. );
  16. return;
  17. }
  18. let r: usize = args[1].parse().unwrap();
  19. let mut num_threads = 1usize;
  20. let mut num_preproc = 1usize;
  21. let mut num_pirs = 1usize;
  22. if args.len() > 2 {
  23. num_threads = args[2].parse().unwrap();
  24. }
  25. if args.len() > 3 {
  26. num_preproc = args[3].parse().unwrap();
  27. }
  28. if args.len() > 4 {
  29. num_pirs = args[4].parse().unwrap();
  30. } else {
  31. num_pirs = num_preproc;
  32. }
  33. let num_records = 1 << r;
  34. println!("===== ONE-TIME SETUP =====\n");
  35. let otsetup_start = Instant::now();
  36. init(num_threads);
  37. let (client, pub_params) = Client::new(r);
  38. let pub_params_len = pub_params.len();
  39. let server = Server::new(r, pub_params);
  40. let otsetup_us = otsetup_start.elapsed().as_micros();
  41. println!("One-time setup: {} µs", otsetup_us);
  42. println!("pub_params len = {}", pub_params_len);
  43. println!("\n===== PREPROCESSING =====\n");
  44. let preproc_client_start = Instant::now();
  45. let preproc_msg = client.preproc_PIRs(num_preproc);
  46. let preproc_client_us = preproc_client_start.elapsed().as_micros();
  47. println!("Preprocessing client: {} µs", preproc_client_us);
  48. println!("preproc_msg len = {}", preproc_msg.len());
  49. let preproc_server_start = Instant::now();
  50. let preproc_resp = server.preproc_PIRs(&preproc_msg);
  51. let preproc_server_us = preproc_server_start.elapsed().as_micros();
  52. println!("Preprocessing server: {} µs", preproc_server_us);
  53. println!("preproc_response len = {}", preproc_resp.len());
  54. /*
  55. let spiral_params = params::get_spiral_params(r);
  56. let mut rng = rand::thread_rng();
  57. print_params_summary(&spiral_params);
  58. println!("OT one-time setup: {} µs", otsetup_us);
  59. // One-time setup for the Spiral client
  60. let spc_otsetup_start = Instant::now();
  61. let mut clientrng = rand::thread_rng();
  62. let mut client = Client::init(&spiral_params, &mut clientrng);
  63. let pub_params = client.generate_keys();
  64. let pub_params_buf = pub_params.serialize();
  65. let spc_otsetup_us = spc_otsetup_start.elapsed().as_micros();
  66. let spiral_blocking_factor = spiral_params.db_item_size / mem::size_of::<DbEntry>();
  67. println!(
  68. "Spiral client one-time setup: {} µs, {} bytes",
  69. spc_otsetup_us,
  70. pub_params_buf.len()
  71. );
  72. println!("\n===== PREPROCESSING =====\n");
  73. // Spiral preprocessing: create a PIR lookup for an element at a
  74. // random location
  75. let spc_query_start = Instant::now();
  76. let rand_idx = (rng.next_u64() as usize) % num_records;
  77. let rand_pir_idx = rand_idx / spiral_blocking_factor;
  78. println!("rand_idx = {} rand_pir_idx = {}", rand_idx, rand_pir_idx);
  79. let spc_query = client.generate_query(rand_pir_idx);
  80. let spc_query_buf = spc_query.serialize();
  81. let spc_query_us = spc_query_start.elapsed().as_micros();
  82. println!(
  83. "Spiral query: {} µs, {} bytes",
  84. spc_query_us,
  85. spc_query_buf.len()
  86. );
  87. // Create the database encryption keys and do the OT to fetch the
  88. // right one, but don't actually encrypt the database yet
  89. let dbkeys = gen_db_enc_keys(r);
  90. let otkeyreq_start = Instant::now();
  91. let (keystate, keyquery) = otkey_request(rand_idx, r);
  92. let keyquerysize = keyquery.len() * keyquery[0].len();
  93. let otkeyreq_us = otkeyreq_start.elapsed().as_micros();
  94. let otkeysrv_start = Instant::now();
  95. let keyresponse = otkey_serve(keyquery, &dbkeys);
  96. let keyrespsize = keyresponse.len() * keyresponse[0].len();
  97. let otkeysrv_us = otkeysrv_start.elapsed().as_micros();
  98. let otkeyrcv_start = Instant::now();
  99. let otkey = otkey_receive(keystate, &keyresponse);
  100. let otkeyrcv_us = otkeyrcv_start.elapsed().as_micros();
  101. println!("key OT query in {} µs, {} bytes", otkeyreq_us, keyquerysize);
  102. println!("key OT serve in {} µs, {} bytes", otkeysrv_us, keyrespsize);
  103. println!("key OT receive in {} µs", otkeyrcv_us);
  104. // Create a database with recognizable contents
  105. let db: Vec<DbEntry> = ((0 as DbEntry)..(num_records as DbEntry))
  106. .map(|x| 10000001 * x)
  107. .collect();
  108. println!("\n===== RUNTIME =====\n");
  109. // Pick the record we actually want to query
  110. let q = (rng.next_u64() as usize) % num_records;
  111. // Compute the offset from the record index we're actually looking
  112. // for to the random one we picked earlier. Tell it to the server,
  113. // who will rotate right the database by that amount before
  114. // encrypting it.
  115. let idx_offset = (num_records + rand_idx - q) % num_records;
  116. println!("Send to server {} bytes", 8 /* sizeof(idx_offset) */);
  117. // The server rotates, blinds, and encrypts the database
  118. let blind: DbEntry = 20;
  119. let encdb_start = Instant::now();
  120. let encdb = encdb_xor_keys(&db, &dbkeys, r, idx_offset, blind, num_threads);
  121. let encdb_us = encdb_start.elapsed().as_micros();
  122. println!("Server encrypt database {} µs", encdb_us);
  123. // Load the encrypted database into Spiral
  124. let sps_loaddb_start = Instant::now();
  125. let sps_db = load_db_from_slice_mt(&spiral_params, &encdb, num_threads);
  126. let sps_loaddb_us = sps_loaddb_start.elapsed().as_micros();
  127. println!("Server load database {} µs", sps_loaddb_us);
  128. // Do the PIR query
  129. let sps_query_start = Instant::now();
  130. let sps_query = Query::deserialize(&spiral_params, &spc_query_buf);
  131. let sps_response = process_query(&spiral_params, &pub_params, &sps_query, sps_db.as_slice());
  132. let sps_query_us = sps_query_start.elapsed().as_micros();
  133. println!(
  134. "Server compute response {} µs, {} bytes (*including* the above expansion time)",
  135. sps_query_us,
  136. sps_response.len()
  137. );
  138. // Decode the response to yield the whole Spiral block
  139. let spc_recv_start = Instant::now();
  140. let encdbblock = client.decode_response(sps_response.as_slice());
  141. // Extract the one encrypted DbEntry we were looking for (and the
  142. // only one we are able to decrypt)
  143. let entry_in_block = rand_idx % spiral_blocking_factor;
  144. let loc_in_block = entry_in_block * mem::size_of::<DbEntry>();
  145. let loc_in_block_end = (entry_in_block + 1) * mem::size_of::<DbEntry>();
  146. let encdbentry = DbEntry::from_le_bytes(
  147. encdbblock[loc_in_block..loc_in_block_end]
  148. .try_into()
  149. .unwrap(),
  150. );
  151. let decdbentry = otkey_decrypt(&otkey, rand_idx, encdbentry);
  152. let spc_recv_us = spc_recv_start.elapsed().as_micros();
  153. println!("Client decode response {} µs", spc_recv_us);
  154. println!("index = {}, Response = {}", q, decdbentry);
  155. */
  156. }