client.rs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. use rand::rngs::ThreadRng;
  2. use rand::RngCore;
  3. use std::sync::mpsc::*;
  4. use std::thread::*;
  5. use crate::params;
  6. use crate::VecData;
  7. enum Command {
  8. PreProc(usize),
  9. PreProcHandle(Vec<u8>),
  10. }
  11. enum Response {
  12. PubParams(Vec<u8>),
  13. PreProcMsg(Vec<u8>),
  14. }
  15. pub struct Client {
  16. r: usize,
  17. thread_handle: JoinHandle<()>,
  18. incoming_cmd: SyncSender<Command>,
  19. outgoing_resp: Receiver<Response>,
  20. }
  21. impl Client {
  22. pub fn new(r: usize) -> (Self, Vec<u8>) {
  23. let (incoming_cmd, incoming_cmd_recv) = sync_channel(0);
  24. let (outgoing_resp_send, outgoing_resp) = sync_channel(0);
  25. let thread_handle = spawn(move || {
  26. let spiral_params = params::get_spiral_params(r);
  27. let mut clientrng = rand::thread_rng();
  28. let mut spiral_client = spiral_rs::client::Client::init(&spiral_params, &mut clientrng);
  29. // The first communication is the pub_params
  30. let pub_params = spiral_client.generate_keys().serialize();
  31. println!(
  32. "{} {} {} {}",
  33. pub_params[0], pub_params[1], pub_params[2], pub_params[3]
  34. );
  35. outgoing_resp_send
  36. .send(Response::PubParams(pub_params))
  37. .unwrap();
  38. });
  39. let pub_params = match outgoing_resp.recv() {
  40. Ok(Response::PubParams(x)) => x,
  41. _ => panic!("Received something unexpected"),
  42. };
  43. (
  44. Client {
  45. r,
  46. thread_handle,
  47. incoming_cmd,
  48. outgoing_resp,
  49. },
  50. pub_params,
  51. )
  52. }
  53. }
  54. #[repr(C)]
  55. pub struct ClientNewRet {
  56. client: *mut Client,
  57. pub_params: VecData,
  58. }
  59. #[no_mangle]
  60. pub extern "C" fn spir_client_new(r: u8) -> ClientNewRet {
  61. let (client, pub_params) = Client::new(r as usize);
  62. let vecdata = VecData {
  63. data: pub_params.as_ptr(),
  64. len: pub_params.len(),
  65. cap: pub_params.capacity(),
  66. };
  67. std::mem::forget(pub_params);
  68. ClientNewRet {
  69. client: Box::into_raw(Box::new(client)),
  70. pub_params: vecdata,
  71. }
  72. }