main.rs 6.6 KB

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