main.rs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. let query_finish_start = Instant::now();
  79. let entry = client.query_finish(&query_resp);
  80. let query_finish_us = query_finish_start.elapsed().as_micros();
  81. println!("Query client finish: {} µs", query_finish_us);
  82. println!("idx = {}; entry = {}", idx, entry);
  83. }
  84. /*
  85. let spiral_params = params::get_spiral_params(r);
  86. let mut rng = rand::thread_rng();
  87. print_params_summary(&spiral_params);
  88. println!("OT one-time setup: {} µs", otsetup_us);
  89. // One-time setup for the Spiral client
  90. let spc_otsetup_start = Instant::now();
  91. let mut clientrng = rand::thread_rng();
  92. let mut client = Client::init(&spiral_params, &mut clientrng);
  93. let pub_params = client.generate_keys();
  94. let pub_params_buf = pub_params.serialize();
  95. let spc_otsetup_us = spc_otsetup_start.elapsed().as_micros();
  96. let spiral_blocking_factor = spiral_params.db_item_size / mem::size_of::<DbEntry>();
  97. println!(
  98. "Spiral client one-time setup: {} µs, {} bytes",
  99. spc_otsetup_us,
  100. pub_params_buf.len()
  101. );
  102. println!("\n===== PREPROCESSING =====\n");
  103. // Spiral preprocessing: create a PIR lookup for an element at a
  104. // random location
  105. let spc_query_start = Instant::now();
  106. let rand_idx = (rng.next_u64() as usize) % num_records;
  107. let rand_pir_idx = rand_idx / spiral_blocking_factor;
  108. println!("rand_idx = {} rand_pir_idx = {}", rand_idx, rand_pir_idx);
  109. let spc_query = client.generate_query(rand_pir_idx);
  110. let spc_query_buf = spc_query.serialize();
  111. let spc_query_us = spc_query_start.elapsed().as_micros();
  112. println!(
  113. "Spiral query: {} µs, {} bytes",
  114. spc_query_us,
  115. spc_query_buf.len()
  116. );
  117. // Create the database encryption keys and do the OT to fetch the
  118. // right one, but don't actually encrypt the database yet
  119. let dbkeys = gen_db_enc_keys(r);
  120. let otkeyreq_start = Instant::now();
  121. let (keystate, keyquery) = otkey_request(rand_idx, r);
  122. let keyquerysize = keyquery.len() * keyquery[0].len();
  123. let otkeyreq_us = otkeyreq_start.elapsed().as_micros();
  124. let otkeysrv_start = Instant::now();
  125. let keyresponse = otkey_serve(keyquery, &dbkeys);
  126. let keyrespsize = keyresponse.len() * keyresponse[0].len();
  127. let otkeysrv_us = otkeysrv_start.elapsed().as_micros();
  128. let otkeyrcv_start = Instant::now();
  129. let otkey = otkey_receive(keystate, &keyresponse);
  130. let otkeyrcv_us = otkeyrcv_start.elapsed().as_micros();
  131. println!("key OT query in {} µs, {} bytes", otkeyreq_us, keyquerysize);
  132. println!("key OT serve in {} µs, {} bytes", otkeysrv_us, keyrespsize);
  133. println!("key OT receive in {} µs", otkeyrcv_us);
  134. // Create a database with recognizable contents
  135. let db: Vec<DbEntry> = ((0 as DbEntry)..(num_records as DbEntry))
  136. .map(|x| 10000001 * x)
  137. .collect();
  138. println!("\n===== RUNTIME =====\n");
  139. // Pick the record we actually want to query
  140. let q = (rng.next_u64() as usize) % num_records;
  141. // Compute the offset from the record index we're actually looking
  142. // for to the random one we picked earlier. Tell it to the server,
  143. // who will rotate right the database by that amount before
  144. // encrypting it.
  145. let idx_offset = (num_records + rand_idx - q) % num_records;
  146. println!("Send to server {} bytes", 8 /* sizeof(idx_offset) */);
  147. // The server rotates, blinds, and encrypts the database
  148. let blind: DbEntry = 20;
  149. let encdb_start = Instant::now();
  150. let encdb = encdb_xor_keys(&db, &dbkeys, r, idx_offset, blind, num_threads);
  151. let encdb_us = encdb_start.elapsed().as_micros();
  152. println!("Server encrypt database {} µs", encdb_us);
  153. // Load the encrypted database into Spiral
  154. let sps_loaddb_start = Instant::now();
  155. let sps_db = load_db_from_slice_mt(&spiral_params, &encdb, num_threads);
  156. let sps_loaddb_us = sps_loaddb_start.elapsed().as_micros();
  157. println!("Server load database {} µs", sps_loaddb_us);
  158. // Do the PIR query
  159. let sps_query_start = Instant::now();
  160. let sps_query = Query::deserialize(&spiral_params, &spc_query_buf);
  161. let sps_response = process_query(&spiral_params, &pub_params, &sps_query, sps_db.as_slice());
  162. let sps_query_us = sps_query_start.elapsed().as_micros();
  163. println!(
  164. "Server compute response {} µs, {} bytes (*including* the above expansion time)",
  165. sps_query_us,
  166. sps_response.len()
  167. );
  168. // Decode the response to yield the whole Spiral block
  169. let spc_recv_start = Instant::now();
  170. let encdbblock = client.decode_response(sps_response.as_slice());
  171. // Extract the one encrypted DbEntry we were looking for (and the
  172. // only one we are able to decrypt)
  173. let entry_in_block = rand_idx % spiral_blocking_factor;
  174. let loc_in_block = entry_in_block * mem::size_of::<DbEntry>();
  175. let loc_in_block_end = (entry_in_block + 1) * mem::size_of::<DbEntry>();
  176. let encdbentry = DbEntry::from_le_bytes(
  177. encdbblock[loc_in_block..loc_in_block_end]
  178. .try_into()
  179. .unwrap(),
  180. );
  181. let decdbentry = otkey_decrypt(&otkey, rand_idx, encdbentry);
  182. let spc_recv_us = spc_recv_start.elapsed().as_micros();
  183. println!("Client decode response {} µs", spc_recv_us);
  184. println!("index = {}, Response = {}", q, decdbentry);
  185. */
  186. }