preproc.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <vector>
  2. #include <bsd/stdlib.h> // arc4random_buf
  3. #include "types.hpp"
  4. #include "preproc.hpp"
  5. // Open a file for writing with name the given prefix, and ".pX.tY"
  6. // suffix, where X is the (one-digit) player number and Y is the thread
  7. // number
  8. static std::ofstream openfile(const char *prefix, unsigned player,
  9. unsigned thread_num)
  10. {
  11. std::string filename(prefix);
  12. char suffix[20];
  13. sprintf(suffix, ".p%d.t%u", player%10, thread_num);
  14. filename.append(suffix);
  15. std::ofstream f;
  16. f.open(filename);
  17. if (f.fail()) {
  18. std::cerr << "Failed to open " << filename << "\n";
  19. exit(1);
  20. }
  21. return f;
  22. }
  23. // The server-to-computational-peer protocol for sending precomputed
  24. // data is:
  25. //
  26. // One byte: type
  27. // 0x80: Multiplication triple
  28. // 0x81: Multiplication half-triple
  29. // 0x01 to 0x40: DPF of that depth
  30. // 0x00: End of preprocessing
  31. //
  32. // Four bytes: number of objects of that type (not sent for type == 0x00)
  33. //
  34. // Then that number of objects
  35. //
  36. // Repeat the whole thing until type == 0x00 is received
  37. //
  38. // The incoming objects are written into num_threads files in a
  39. // round-robin manner
  40. void preprocessing_comp(MPCIO &mpcio, int num_threads, char **args)
  41. {
  42. while(1) {
  43. unsigned char type = 0;
  44. unsigned int num = 0;
  45. size_t res = mpcio.serverio.recv(&type, 1);
  46. if (res < 1 || type == 0) break;
  47. mpcio.serverio.recv(&num, 4);
  48. if (type == 0x80) {
  49. // Multiplication triples
  50. std::vector<std::ofstream> tripfiles;
  51. for (int i=0; i<num_threads; ++i) {
  52. tripfiles.push_back(openfile("triples", mpcio.player, i));
  53. }
  54. unsigned thread_num = 0;
  55. MultTriple T;
  56. for (unsigned int i=0; i<num; ++i) {
  57. res = mpcio.serverio.recv(&T, sizeof(T));
  58. if (res < sizeof(T)) break;
  59. tripfiles[thread_num].write((const char *)&T, sizeof(T));
  60. thread_num = (thread_num + 1) % num_threads;
  61. }
  62. for (int i=0; i<num_threads; ++i) {
  63. tripfiles[i].close();
  64. }
  65. } else if (type == 0x81) {
  66. // Multiplication half triples
  67. std::vector<std::ofstream> halffiles;
  68. for (int i=0; i<num_threads; ++i) {
  69. halffiles.push_back(openfile("halves", mpcio.player, i));
  70. }
  71. unsigned thread_num = 0;
  72. HalfTriple H;
  73. for (unsigned int i=0; i<num; ++i) {
  74. res = mpcio.serverio.recv(&H, sizeof(H));
  75. if (res < sizeof(H)) break;
  76. halffiles[thread_num].write((const char *)&H, sizeof(H));
  77. thread_num = (thread_num + 1) % num_threads;
  78. }
  79. for (int i=0; i<num_threads; ++i) {
  80. halffiles[i].close();
  81. }
  82. }
  83. }
  84. }
  85. // Create triples (X0,Y0,Z0),(X1,Y1,Z1) such that
  86. // (X0*Y1 + Y0*X1) = (Z0+Z1)
  87. static void create_triples(MPCServerIO &mpcsrvio, unsigned num)
  88. {
  89. for (unsigned int i=0; i<num; ++i) {
  90. value_t X0, Y0, Z0, X1, Y1, Z1;
  91. arc4random_buf(&X0, sizeof(X0));
  92. arc4random_buf(&Y0, sizeof(Y0));
  93. arc4random_buf(&Z0, sizeof(Z0));
  94. arc4random_buf(&X1, sizeof(X1));
  95. arc4random_buf(&Y1, sizeof(Y1));
  96. Z1 = X0 * Y1 + X1 * Y0 - Z0;
  97. MultTriple T0, T1;
  98. T0 = std::make_tuple(X0, Y0, Z0);
  99. T1 = std::make_tuple(X1, Y1, Z1);
  100. mpcsrvio.p0io.queue(&T0, sizeof(T0));
  101. mpcsrvio.p1io.queue(&T1, sizeof(T1));
  102. }
  103. }
  104. // Create half-triples (X0,Z0),(Y1,Z1) such that
  105. // X0*Y1 = Z0 + Z1
  106. static void create_halftriples(MPCServerIO &mpcsrvio, unsigned num)
  107. {
  108. for (unsigned int i=0; i<num; ++i) {
  109. value_t X0, Z0, Y1, Z1;
  110. arc4random_buf(&X0, sizeof(X0));
  111. arc4random_buf(&Z0, sizeof(Z0));
  112. arc4random_buf(&Y1, sizeof(Y1));
  113. Z1 = X0 * Y1 - Z0;
  114. HalfTriple H0, H1;
  115. H0 = std::make_tuple(X0, Z0);
  116. H1 = std::make_tuple(Y1, Z1);
  117. mpcsrvio.p0io.queue(&H0, sizeof(H0));
  118. mpcsrvio.p1io.queue(&H1, sizeof(H1));
  119. }
  120. }
  121. void preprocessing_server(MPCServerIO &mpcsrvio, char **args)
  122. {
  123. while (*args) {
  124. char *colon = strchr(*args, ':');
  125. if (!colon) {
  126. std::cerr << "Args must be type:num\n";
  127. ++args;
  128. continue;
  129. }
  130. unsigned num = atoi(colon+1);
  131. *colon = '\0';
  132. char *type = *args;
  133. if (!strcmp(type, "t")) {
  134. unsigned char typetag = 0x80;
  135. mpcsrvio.p0io.queue(&typetag, 1);
  136. mpcsrvio.p0io.queue(&num, 4);
  137. mpcsrvio.p1io.queue(&typetag, 1);
  138. mpcsrvio.p1io.queue(&num, 4);
  139. create_triples(mpcsrvio, num);
  140. } else if (!strcmp(type, "h")) {
  141. unsigned char typetag = 0x81;
  142. mpcsrvio.p0io.queue(&typetag, 1);
  143. mpcsrvio.p0io.queue(&num, 4);
  144. mpcsrvio.p1io.queue(&typetag, 1);
  145. mpcsrvio.p1io.queue(&num, 4);
  146. create_halftriples(mpcsrvio, num);
  147. }
  148. ++args;
  149. }
  150. // That's all
  151. unsigned char typetag = 0x00;
  152. mpcsrvio.p0io.queue(&typetag, 1);
  153. mpcsrvio.p1io.queue(&typetag, 1);
  154. mpcsrvio.p0io.send();
  155. mpcsrvio.p1io.send();
  156. }