preproc.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <vector>
  2. #include <bsd/stdlib.h> // arc4random_buf
  3. #include "preproc.hpp"
  4. // Open a file for writing with name the given prefix, and ".pX.tY"
  5. // suffix, where X is the (one-digit) player number and Y is the thread
  6. // number
  7. static std::ofstream openfile(const char *prefix, unsigned player,
  8. unsigned thread_num)
  9. {
  10. std::string filename(prefix);
  11. char suffix[20];
  12. sprintf(suffix, ".p%d.t%u", player%10, thread_num);
  13. filename.append(suffix);
  14. std::ofstream f;
  15. f.open(filename);
  16. if (f.fail()) {
  17. std::cerr << "Failed to open " << filename << "\n";
  18. exit(1);
  19. }
  20. return f;
  21. }
  22. // The server-to-computational-peer protocol for sending precomputed
  23. // data is:
  24. //
  25. // One byte: type
  26. // 0x80: Multiplication triple
  27. // 0x81: Multiplication half-triple
  28. // 0x01 to 0x40: DPF of that depth
  29. // 0x00: End of preprocessing
  30. //
  31. // Four bytes: number of objects of that type (not sent for type == 0x00)
  32. //
  33. // Then that number of objects
  34. //
  35. // Repeat the whole thing until type == 0x00 is received
  36. //
  37. // The incoming objects are written into num_threads files in a
  38. // round-robin manner
  39. void preprocessing_comp(MPCIO &mpcio, int num_threads, char **args)
  40. {
  41. while(1) {
  42. unsigned char type = 0;
  43. unsigned int num = 0;
  44. size_t res = mpcio.serverio.recv(&type, 1);
  45. if (res < 1 || type == 0) break;
  46. mpcio.serverio.recv(&num, 4);
  47. if (type == 0x80) {
  48. // Multiplication triples
  49. std::vector<std::ofstream> tripfiles;
  50. for (int i=0; i<num_threads; ++i) {
  51. tripfiles.push_back(openfile("triples", mpcio.player, i));
  52. }
  53. unsigned thread_num = 0;
  54. MultTriple T;
  55. for (unsigned int i=0; i<num; ++i) {
  56. res = mpcio.serverio.recv(&T, sizeof(T));
  57. if (res < sizeof(T)) break;
  58. tripfiles[thread_num].write((const char *)&T, sizeof(T));
  59. thread_num = (thread_num + 1) % num_threads;
  60. }
  61. for (int i=0; i<num_threads; ++i) {
  62. tripfiles[i].close();
  63. }
  64. }
  65. }
  66. }
  67. void preprocessing_server(MPCServerIO &mpcsrvio, char **args)
  68. {
  69. unsigned int numtriples = 100;
  70. if (*args) {
  71. numtriples = atoi(*args);
  72. ++args;
  73. }
  74. unsigned char type = 0x80;
  75. mpcsrvio.p0io.queue(&type, 1);
  76. mpcsrvio.p0io.queue(&numtriples, 4);
  77. mpcsrvio.p1io.queue(&type, 1);
  78. mpcsrvio.p1io.queue(&numtriples, 4);
  79. for (unsigned int i=0; i<numtriples; ++i) {
  80. uint64_t X0, Y0, Z0, X1, Y1, Z1;
  81. arc4random_buf(&X0, sizeof(X0));
  82. arc4random_buf(&Y0, sizeof(Y0));
  83. arc4random_buf(&Z0, sizeof(Z0));
  84. arc4random_buf(&X1, sizeof(X1));
  85. arc4random_buf(&Y1, sizeof(Y1));
  86. Z1 = X0 * Y1 + X1 * Y0 - Z0;
  87. MultTriple T0, T1;
  88. T0 = std::make_tuple(X0, Y0, Z0);
  89. T1 = std::make_tuple(X1, Y1, Z1);
  90. mpcsrvio.p0io.queue(&T0, sizeof(T0));
  91. mpcsrvio.p1io.queue(&T1, sizeof(T1));
  92. }
  93. // That's all
  94. type = 0x00;
  95. mpcsrvio.p0io.queue(&type, 1);
  96. mpcsrvio.p1io.queue(&type, 1);
  97. mpcsrvio.p0io.send();
  98. mpcsrvio.p1io.send();
  99. }