main.rs 7.0 KB

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