main.rs 8.5 KB

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