cdpf.tcc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Templated method implementations for cdpf.hpp
  2. // I/O for CDPFs
  3. // Read the DPF from the output stream. You can use this to read DPFs
  4. // from files or from the network.
  5. template <typename T>
  6. T& operator>>(T &is, CDPF &cdpf)
  7. {
  8. is.read((char *)&cdpf.seed, sizeof(cdpf.seed));
  9. cdpf.whichhalf = get_lsb(cdpf.seed);
  10. uint8_t depth = VALUE_BITS - 7;
  11. cdpf.cw.clear();
  12. for (uint8_t i=0; i<depth; ++i) {
  13. DPFnode cw;
  14. is.read((char *)&cw, sizeof(cw));
  15. cdpf.cw.push_back(cw);
  16. }
  17. value_t cfbits = 0;
  18. is.read((char *)&cfbits, BITBYTES(depth));
  19. cdpf.cfbits = cfbits;
  20. is.read((char *)&cdpf.leaf_cwr, sizeof(cdpf.leaf_cwr));
  21. is.read((char *)&cdpf.as_target, sizeof(cdpf.as_target));
  22. is.read((char *)&cdpf.xs_target, sizeof(cdpf.xs_target));
  23. return is;
  24. }
  25. // Write the DPF to the output stream. You can use this to write DPFs
  26. // to files or over the network.
  27. template <typename T>
  28. T& operator<<(T &os, const CDPF &cdpf)
  29. {
  30. os.write((const char *)&cdpf.seed, sizeof(cdpf.seed));
  31. uint8_t depth = VALUE_BITS - 7;
  32. for (uint8_t i=0; i<depth; ++i) {
  33. os.write((const char *)&cdpf.cw[i], sizeof(cdpf.cw[i]));
  34. }
  35. os.write((const char *)&cdpf.cfbits, BITBYTES(depth));
  36. os.write((const char *)&cdpf.leaf_cwr, sizeof(cdpf.leaf_cwr));
  37. os.write((const char *)&cdpf.as_target, sizeof(cdpf.as_target));
  38. os.write((const char *)&cdpf.xs_target, sizeof(cdpf.xs_target));
  39. return os;
  40. }
  41. // Determine whether the given additively or XOR shared element is 0.
  42. // The output is a bit share, which is a share of 1 iff the passed
  43. // element is a share of 0. Note also that you can compare two RegAS or
  44. // RegXS values A and B for equality by passing A-B here.
  45. //
  46. // Cost:
  47. // 1 word sent in 1 message
  48. // VALUE_BITS - 7 = 57 local AES operations
  49. template <typename T>
  50. RegBS CDPF::is_zero(MPCTIO &tio, yield_t &yield,
  51. const T &x, size_t &aes_ops)
  52. {
  53. // Reconstruct S = target-x
  54. // The server does nothing in this protocol
  55. if (tio.player() < 2) {
  56. T S_share;
  57. get_target(S_share);
  58. S_share -= x;
  59. tio.iostream_peer() << S_share;
  60. yield();
  61. T peer_S_share;
  62. tio.iostream_peer() >> peer_S_share;
  63. S_share += peer_S_share;
  64. value_t S = S_share.share();
  65. // After that one single-word exchange, the rest of this
  66. // algorithm is entirely a local computation.
  67. return is_zero(S, aes_ops);
  68. } else {
  69. yield();
  70. }
  71. // The server gets a share of 0
  72. RegBS eq;
  73. return eq;
  74. }