main.rs 6.4 KB

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