lib.rs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. mod utils;
  2. use wasm_bindgen::prelude::*;
  3. use spiral_rs::{params::*, util::*, client::*, discrete_gaussian::*};
  4. const UUID_V4_LEN: usize = 36;
  5. // console_log! macro
  6. #[wasm_bindgen]
  7. extern "C" {
  8. #[wasm_bindgen(js_namespace = console)]
  9. fn log(s: &str);
  10. }
  11. macro_rules! console_log {
  12. ($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
  13. }
  14. // Container class for a static lifetime Client
  15. // Avoids a lifetime in the return signature of bound Rust functions
  16. #[wasm_bindgen]
  17. pub struct WrappedClient {
  18. client: Client<'static>
  19. }
  20. // Unsafe global with a static lifetime
  21. // Accessed unsafely only once, at load / setup
  22. static mut PARAMS: Params = get_empty_params();
  23. // Very simply test to ensure random generation is not obviously biased.
  24. fn dg_seems_okay() {
  25. let params = get_test_params();
  26. let mut dg = DiscreteGaussian::init(&params);
  27. let mut v = Vec::new();
  28. let trials = 10000;
  29. let mut sum = 0;
  30. for _ in 0..trials {
  31. let val = dg.sample();
  32. v.push(val);
  33. sum += val;
  34. }
  35. let mean = sum as f64 / trials as f64;
  36. let std_dev = params.noise_width / f64::sqrt(2f64 * std::f64::consts::PI);
  37. let std_dev_of_mean = std_dev / f64::sqrt(trials as f64);
  38. assert!(f64::abs(mean) < std_dev_of_mean * 5f64);
  39. }
  40. // Initializes a client; can optionally take in a set of parameters
  41. #[wasm_bindgen]
  42. pub fn initialize(json_params: Option<String>) -> WrappedClient {
  43. dg_seems_okay();
  44. // spiral_rs::ntt::test::ntt_correct();
  45. let cfg = r#"
  46. {'n': 2,
  47. 'nu_1': 9,
  48. 'nu_2': 6,
  49. 'p': 256,
  50. 'q_prime_bits': 20,
  51. 's_e': 87.62938774292914,
  52. 't_GSW': 8,
  53. 't_conv': 4,
  54. 't_exp': 8,
  55. 't_exp_right': 56}
  56. "#;
  57. let mut cfg = cfg.replace("'", "\"");
  58. if json_params.is_some() {
  59. cfg = json_params.unwrap();
  60. }
  61. let client;
  62. // this minimal unsafe operation is need to initialize state
  63. unsafe {
  64. PARAMS = params_from_json(&cfg);
  65. client = Client::init(&PARAMS);
  66. }
  67. WrappedClient { client }
  68. }
  69. #[wasm_bindgen]
  70. pub fn generate_public_parameters(c: &mut WrappedClient) -> Box<[u8]> {
  71. c.client.generate_keys().serialize().into_boxed_slice()
  72. }
  73. #[wasm_bindgen]
  74. pub fn generate_query(c: &mut WrappedClient, id: &str, idx_target: usize) -> Box<[u8]> {
  75. assert_eq!(id.len(), UUID_V4_LEN);
  76. let query = c.client.generate_query(idx_target);
  77. let mut query_buf = query.serialize();
  78. let mut full_query_buf = id.as_bytes().to_vec();
  79. full_query_buf.append(&mut query_buf);
  80. full_query_buf.into_boxed_slice()
  81. }
  82. #[wasm_bindgen]
  83. pub fn decode_response(c: &mut WrappedClient, data: Box<[u8]>) -> Box<[u8]> {
  84. c.client.decode_response(&*data).into_boxed_slice()
  85. }