heapsampler.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #ifndef __HEAPSAMPLER_HPP__
  2. #define __HEAPSAMPLER_HPP__
  3. #include <vector>
  4. #include "mpcio.hpp"
  5. #include "coroutine.hpp"
  6. #include "heap.hpp"
  7. // Implement a stream sampler using a MinHeap. A stream sampler will
  8. // sample a random subset of k elements from an arbitrality long stream
  9. // of elements, while using only O(k) memory. The number of elements in
  10. // the stream need not be known in advance. Importantly, no party knows
  11. // which elements ended up in the sample.
  12. // We use the technique from "Path Oblivious Heap" by Elaine Shi
  13. // (IEEE Symsposium on Security and Privacy 2020):
  14. // https://eprint.iacr.org/2019/274.pdf; in particular, Section 5.2
  15. // "Oblivious Streaming Sampler with Applications to Distributed
  16. // Differential Privacy". We correct the typo that the probability to
  17. // keep the mth item (for m>k) is listed as 1/m, but it should be k/m.
  18. // Note that the Shi paper is in the client-server setting, where the
  19. // client _is_ allowed to know which elements did and did not end up in
  20. // the sample (but the server, who stores the heap, is not). In our MPC
  21. // setting, no party may learn which elements ended up in the sample.
  22. class HeapSampler {
  23. // The number of items to sample
  24. size_t k;
  25. // The number of items that have arrived so far
  26. size_t m;
  27. // The MinHeap with O(k) storage that when m>=k stores a
  28. // uniformly random sample of size k of the m items seen so far
  29. MinHeap heap;
  30. // The next random tag to use
  31. RegAS randtag;
  32. // Make the next random tag
  33. void make_randtag(MPCTIO &tio, yield_t &yield);
  34. public:
  35. // Constructor for a HeapSampler that samples k items from a stream
  36. // of abritrary and unknown size, using O(k) memory
  37. HeapSampler(MPCTIO &tio, yield_t &yield, size_t k);
  38. // An element has arrived
  39. void ingest(MPCTIO &tio, yield_t &yield, RegAS elt);
  40. // The stream has ended; output min(k,m) randomly sampled elements.
  41. // After calling this function, the HeapSampler is reset to its
  42. // initial m=0 state.
  43. std::vector<RegAS> close(MPCTIO &tio, yield_t &yield);
  44. };
  45. // A unit test for the HeapSampler
  46. void heapsampler_test(MPCIO &mpcio, const PRACOptions &opts, char **args);
  47. // A unit test for the weighted_coin internal function
  48. void weighted_coin_test(MPCIO &mpcio, const PRACOptions &opts, char **args);
  49. #endif