ot_blinds.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #include "ot_blinds.h"
  2. #include <fcntl.h>
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <ENCRYPTO_utils/connection.h>
  6. #include <fstream>
  7. #include <x86intrin.h> // SSE and AVX intrinsics
  8. //const uint64_t n_OTS = 128;
  9. ot_ext_prot test_prots[] = {IKNP};
  10. snd_ot_flavor test_sflavor[] = {Snd_OT, Snd_C_OT, Snd_GC_OT, Snd_R_OT};
  11. rec_ot_flavor test_rflavor[] = {Rec_OT, Rec_R_OT};
  12. //uint64_t test_numots[] = {n_OTS, 3215, 100000};
  13. uint64_t test_bitlen[] = {1, 3, 8, 191};
  14. uint32_t test_nthreads[] = {1, 4};
  15. field_type test_ftype[] = {P_FIELD, ECC_FIELD};
  16. bool test_usemecr[] = {false};
  17. BOOL Cleanup()
  18. {
  19. delete sndthread;
  20. delete rcvthread;
  21. return true;
  22. }
  23. void InitSender(const std::string &address, const int port, CLock *glock)
  24. {
  25. m_nPort = (uint16_t)port;
  26. m_nAddr = &address;
  27. //Server listen
  28. m_Socket = Listen(address, port);
  29. if (!m_Socket)
  30. {
  31. std::cerr << "Listen failed on " << address << ":" << port << "\n";
  32. std::exit(1);
  33. }
  34. sndthread = new SndThread(m_Socket.get(), glock);
  35. rcvthread = new RcvThread(m_Socket.get(), glock);
  36. sndthread->Start();
  37. rcvthread->Start();
  38. }
  39. void InitReceiver(const std::string &address, const int port, CLock *glock)
  40. {
  41. m_nPort = (uint16_t)port;
  42. m_nAddr = &address;
  43. //Client connect
  44. m_Socket = Connect(address, port);
  45. if (!m_Socket)
  46. {
  47. std::cerr << "Connect failed on " << address << ":" << port << "\n";
  48. std::exit(1);
  49. }
  50. sndthread = new SndThread(m_Socket.get(), glock);
  51. rcvthread = new RcvThread(m_Socket.get(), glock);
  52. sndthread->Start();
  53. rcvthread->Start();
  54. }
  55. OTExtSnd *InitOTExtSnd(ot_ext_prot m_eProt, uint32_t nbaseots, uint32_t nchecks, bool enablemecr, field_type ftype, crypto *crypt)
  56. {
  57. OTExtSnd *sender;
  58. switch (m_eProt)
  59. {
  60. case ALSZ:
  61. sender = new ALSZOTExtSnd(crypt, rcvthread, sndthread, nbaseots, nchecks);
  62. break;
  63. case IKNP:
  64. sender = new IKNPOTExtSnd(crypt, rcvthread, sndthread);
  65. break;
  66. case NNOB:
  67. sender = new NNOBOTExtSnd(crypt, rcvthread, sndthread);
  68. break;
  69. default:
  70. sender = new ALSZOTExtSnd(crypt, rcvthread, sndthread, nbaseots, nchecks);
  71. break;
  72. }
  73. if (enablemecr)
  74. sender->EnableMinEntCorrRobustness();
  75. sender->ComputeBaseOTs(ftype);
  76. return sender;
  77. }
  78. OTExtRec *InitOTExtRec(ot_ext_prot m_eProt, uint32_t nbaseots, uint32_t nchecks, bool enablemecr, field_type ftype, crypto *crypt)
  79. {
  80. OTExtRec *receiver;
  81. switch (m_eProt)
  82. {
  83. case ALSZ:
  84. receiver = new ALSZOTExtRec(crypt, rcvthread, sndthread, nbaseots, nchecks);
  85. break;
  86. case IKNP:
  87. receiver = new IKNPOTExtRec(crypt, rcvthread, sndthread);
  88. break;
  89. case NNOB:
  90. receiver = new NNOBOTExtRec(crypt, rcvthread, sndthread);
  91. break;
  92. default:
  93. receiver = new ALSZOTExtRec(crypt, rcvthread, sndthread, nbaseots, nchecks);
  94. break;
  95. }
  96. if (enablemecr)
  97. receiver->EnableMinEntCorrRobustness();
  98. receiver->ComputeBaseOTs(ftype);
  99. return receiver;
  100. }
  101. // This is P0
  102. void run_test_sender(uint32_t numots, uint32_t bitlength, snd_ot_flavor stype, rec_ot_flavor rtype, uint32_t numthreads,
  103. crypto *crypt, OTExtSnd *sender, OTExtRec *receiver)
  104. {
  105. CBitVector delta;
  106. CBitVector X0;
  107. uint32_t nsndvals = 2;
  108. CBitVector **X = (CBitVector **)malloc(sizeof(CBitVector *) * nsndvals);
  109. CBitVector **Y = (CBitVector **)malloc(sizeof(CBitVector *) * nsndvals);
  110. //The masking function with which the values that are sent in the last communication step are processed
  111. XORMasking *m_fMaskFct = new XORMasking(bitlength, delta);
  112. //creates delta as an array with "numOTs" entries of "bitlength" bit-values and fills delta with random values
  113. delta.Create(numots, bitlength, crypt);
  114. //Create X1 and X2 as two arrays with "numOTs" entries of "bitlength" bit-values and resets them to 0
  115. //X1.Create(numots, bitlength, crypt);
  116. //X2.Create(numots, bitlength, crypt);
  117. //X[0] --> T0
  118. //X[1] --> X0
  119. //Y[0] --> Y0
  120. printf("run_test_sender\n");
  121. //(T, X0 \oplus T)
  122. for (uint32_t i = 0; i < nsndvals; i++)
  123. {
  124. X[i] = new CBitVector();
  125. // X[i]->Create(numots, bitlength, crypt);
  126. X[i]->Create(numots, bitlength);
  127. X[i]->FillRand(numots, crypt);
  128. Y[i] = new CBitVector();
  129. // X[i]->Create(numots, bitlength, crypt);
  130. Y[i]->Create(numots, bitlength);
  131. // Y[i]->FillRand(n_OTS, crypt);
  132. Y[i]->SetToOne(); // I added this line
  133. //X[i]->PrintBinary(); // I added this line
  134. }
  135. X0.Create(numots, bitlength);
  136. X0.Reset();
  137. X0.XOR(X[1]);
  138. //Y[0]->Copy(X[1]);
  139. #ifdef VERBOSE
  140. printf("X0:\n");
  141. //X[1]->PrintBinary(); // I added this line
  142. X0.PrintHex(); // I added this line
  143. printf("Y0:\n");
  144. Y[0]->PrintHex(); // I added this line
  145. printf("T0: \n");
  146. X[0]->PrintHex();
  147. #endif
  148. X[1]->XOR(X[0]);
  149. //X[1] --> X0 \oplus T0
  150. //for (uint32_t i = 0; i < nsndvals; i++)
  151. {
  152. // printf("T0:\n");
  153. // X[0]->PrintBinary(); // I added this line
  154. // printf("X0 ^ T0:\n");
  155. // X[1]->PrintBinary(); // I added this line
  156. }
  157. sender->send(numots, bitlength, nsndvals, X, stype, rtype, numthreads, m_fMaskFct);
  158. CBitVector response;
  159. //Pre-generate the respose vector for the results
  160. response.Create(numots, bitlength);
  161. response.Reset();
  162. //Y[0] is the choice vector.
  163. receiver->receive(numots, bitlength, nsndvals, Y[0], &response, stype, rtype, numthreads, m_fMaskFct);
  164. response.XOR(X[0]);
  165. #ifdef VERBOSE
  166. printf("\n\nLearnt Gamma0 = (X1 /cdot Y0) /oplus T------>>>>>> \n");
  167. response.PrintHex();
  168. #endif
  169. int fd = open("./y0", O_CREAT | O_WRONLY | O_TRUNC, 0600);;
  170. ssize_t bytes = write(fd, Y[0]->GetArr(), Y[0]->GetSize());
  171. close(fd);
  172. int fd2 = open("./x0", O_CREAT | O_WRONLY | O_TRUNC, 0600);;
  173. ssize_t bytes2 = write(fd2, X0.GetArr(), X0.GetSize());
  174. close(fd2);
  175. int fd3 = open("./gamma0", O_CREAT | O_WRONLY | O_TRUNC, 0600);;
  176. ssize_t bytes3 = write(fd3, response.GetArr(), response.GetSize());
  177. close(fd3);
  178. // for(size_t j = 0; j < numots; ++j)
  179. // {
  180. // // std::cout << (int) response.GetBitNoMask(j);
  181. // gammafile0 << (int) response.GetBitNoMask(j);
  182. // X0file << (int) X0.GetBitNoMask(j);
  183. // Y0file << (int) Y[0]->GetBitNoMask(j);
  184. // }
  185. for (uint32_t i = 0; i < nsndvals; i++)
  186. {
  187. delete (X[i]);
  188. delete (Y[i]);
  189. }
  190. free(X);
  191. free(Y);
  192. delta.delCBitVector();
  193. delete m_fMaskFct;
  194. }
  195. // This is P1
  196. void run_test_receiver(uint32_t numots, uint32_t bitlength, snd_ot_flavor stype, rec_ot_flavor rtype, uint32_t numthreads,
  197. crypto *crypt, OTExtSnd *sender, OTExtRec *receiver, int m_nPID)
  198. {
  199. CBitVector X1, response;
  200. uint32_t nsndvals = 2;
  201. CBitVector **X = (CBitVector **)malloc(sizeof(CBitVector *) * nsndvals);
  202. CBitVector **Y = (CBitVector **)malloc(sizeof(CBitVector *) * nsndvals);
  203. //X[0] --> T1
  204. //X[1] --> X1
  205. //Y[0] --> Y1
  206. for (uint32_t i = 0; i < nsndvals; i++)
  207. {
  208. X[i] = new CBitVector();
  209. X[i]->Create(numots, bitlength);
  210. X[i]->FillRand(numots, crypt);
  211. Y[i] = new CBitVector();
  212. Y[i]->Create(numots, bitlength);
  213. //Y[i]->FillRand(n_OTS, crypt);
  214. Y[i]->SetToOne();
  215. }
  216. X1.Create(numots, bitlength);
  217. X1.Reset();
  218. X1.XOR(X[1]);
  219. #ifdef VERBOSE
  220. printf("X1:\n");
  221. //X[1]->PrintBinary(); // I added this line
  222. X1.PrintHex(); // I added this line
  223. printf("Y1:\n");
  224. Y[0]->PrintHex(); // I added this line
  225. printf("T1: \n");
  226. X[0]->PrintHex();
  227. #endif
  228. // X[1] -- > X1 \oplus T1
  229. X[1]->XOR(X[0]);
  230. //The masking function with which the values that are sent in the last communication step are processed
  231. XORMasking *m_fMaskFct = new XORMasking(bitlength);
  232. //Pre-generate the respose vector for the results
  233. response.Create(numots, bitlength);
  234. response.Reset();
  235. /*
  236. * The inputs of the receiver in G_OT, C_OT and R_OT are the same. The only difference is the version
  237. * variable that has to match the version of the sender.
  238. */
  239. //Learns: (X0 \cdot Y1) \oplus T0
  240. //Y0 is the choice bits.
  241. receiver->receive(numots, bitlength, nsndvals, Y[0], &response, stype, rtype, numthreads, m_fMaskFct);
  242. sender->send(numots, bitlength, nsndvals, X, stype, rtype, numthreads, m_fMaskFct);
  243. // printf("Learnt: (X0 /cdot Y1) /oplus T0------>>>>>> \n");
  244. // response.PrintBinary();
  245. response.XOR(X[0]);
  246. #ifdef VERBOSE
  247. printf("\n\nLearnt: Gamma1 = (X0 /cdot Y1) /oplus T------>>>>>> \n");
  248. response.PrintHex();
  249. #endif
  250. //std::ofstream gammafile1, X1file, Y1file;
  251. int fd = open("./y1", O_CREAT | O_WRONLY | O_TRUNC, 0600);;
  252. ssize_t bytes = write(fd, Y[0]->GetArr(), Y[0]->GetSize());
  253. #ifdef VERBOSE
  254. std::cout << "bytes = " << bytes << std::endl;
  255. #endif
  256. close(fd);
  257. int fd2 = open("./x1", O_CREAT | O_WRONLY | O_TRUNC, 0600);;
  258. ssize_t bytes2 = write(fd2, X1.GetArr(), X1.GetSize());
  259. close(fd2);
  260. int fd3 = open("./gamma1", O_CREAT | O_WRONLY | O_TRUNC, 0600);;
  261. ssize_t bytes3 = write(fd3, response.GetArr(), response.GetSize());
  262. close(fd3);
  263. #ifdef VERBOSE
  264. std::cout << "X1: \n\n";
  265. X1.PrintHex();
  266. #endif
  267. for (uint32_t i = 0; i < nsndvals; i++)
  268. {
  269. delete (X[i]);
  270. delete (Y[i]);
  271. }
  272. free(X);
  273. free(Y);
  274. delete m_fMaskFct;
  275. // choices.delCBitVector();
  276. response.delCBitVector();
  277. }
  278. int main(int argc, char **argv)
  279. {
  280. std::string addr0 = argv[1]; // "127.0.0.1";
  281. std::string addr1 = argv[2]; // "127.0.0.1";
  282. int port = 7766;
  283. __m128i blinds;
  284. // if (argc != 2)
  285. // {
  286. // std::cout << "Please call with 0 if acting as server or 1 if acting as client" << std::endl;
  287. // return EXIT_FAILURE;
  288. // }
  289. //Determines whether the program is executed in the sender or receiver role
  290. m_nPID = atoi(argv[3]);
  291. int64_t nblindbits = atoi(argv[4]);
  292. std::cout << "Playing as role: " << m_nPID << std::endl;
  293. assert(m_nPID >= 0 && m_nPID <= 1);
  294. //The symmetric security parameter (80, 112, 128)
  295. uint32_t m_nSecParam = 128;
  296. crypto *crypt = new crypto(m_nSecParam, (uint8_t *)m_cConstSeed[m_nPID]);
  297. CLock *glock = new CLock(); // pass this to sender and receiver constructors
  298. uint32_t m_nBaseOTs = 190;
  299. uint32_t m_nChecks = 380;
  300. // NOTE: This vector controls the settings used by the oblivious transfer.
  301. test_options selected_options; // = {IKNP, 128, 1, Snd_C_OT, Rec_OT, 1, ECC_FIELD, false};
  302. selected_options.prot = IKNP;
  303. selected_options.numots = nblindbits; // Number of OTs performed using the extended COT.
  304. selected_options.bitlen = 1;
  305. selected_options.sflavor = Snd_OT; //Snd_C_OT;
  306. selected_options.rflavor = Rec_OT;
  307. selected_options.nthreads = 1; // Number of threads
  308. selected_options.ftype = P_FIELD; // Type of field to use for the base OT.
  309. selected_options.usemecr = false;
  310. // test_options selected_options = {IKNP, 128, 1, Snd_C_OT, Rec_OT, 1, P_FIELD, false}; // Alternative using P_FIELD
  311. if (m_nPID == SERVER_ID) //Play as OT sender
  312. {
  313. InitSender(addr0, port, glock);
  314. OTExtSnd *sender = NULL;
  315. InitReceiver(addr1, port, glock);
  316. OTExtRec *receiver = NULL;
  317. auto start = std::chrono::steady_clock::now();
  318. sender = InitOTExtSnd(selected_options.prot, m_nBaseOTs, m_nChecks, selected_options.usemecr, selected_options.ftype, crypt);
  319. receiver = InitOTExtRec(selected_options.prot, m_nBaseOTs, m_nChecks, selected_options.usemecr, selected_options.ftype, crypt);
  320. std::cout << "--> : " << getProt(selected_options.prot) << " Sender " << selected_options.numots << " " << getSndFlavor(selected_options.sflavor) << " / " << getRecFlavor(selected_options.rflavor) << " on " << selected_options.bitlen << " bits with " << selected_options.nthreads << " threads, " << getFieldType(selected_options.ftype) << " and" << (selected_options.usemecr ? "" : " no") << " MECR" << std::endl;
  321. run_test_sender(selected_options.numots, selected_options.bitlen, selected_options.sflavor, selected_options.rflavor, selected_options.nthreads, crypt, sender, receiver);
  322. auto end = std::chrono::steady_clock::now();
  323. std::chrono::duration<double> elapsed_seconds = end - start;
  324. std::cout << "time to generate and evaluate " << nblindbits << " OTs is: " << elapsed_seconds.count() << "s\n";
  325. std::cout << "bytes transmitted for OT = " << (54292 + 33 * nblindbits / 2) << " bytes\n";
  326. delete sender;
  327. }
  328. else //Play as OT receiver
  329. {
  330. InitReceiver(addr0, port, glock);
  331. OTExtRec *receiver = NULL;
  332. InitSender(addr1, port, glock);
  333. auto start = std::chrono::steady_clock::now();
  334. OTExtSnd *sender = NULL;
  335. receiver = InitOTExtRec(selected_options.prot, m_nBaseOTs, m_nChecks, selected_options.usemecr, selected_options.ftype, crypt);
  336. sender = InitOTExtSnd(selected_options.prot, m_nBaseOTs, m_nChecks, selected_options.usemecr, selected_options.ftype, crypt);
  337. std::cout << "--> : " << getProt(selected_options.prot) << " Receiver " << selected_options.numots << " " << getSndFlavor(selected_options.sflavor) << " / " << getRecFlavor(selected_options.rflavor) << " on " << selected_options.bitlen << " bits with " << selected_options.nthreads << " threads, " << getFieldType(selected_options.ftype) << " and" << (selected_options.usemecr ? "" : " no") << " MECR" << std::endl;
  338. run_test_receiver(selected_options.numots, selected_options.bitlen, selected_options.sflavor, selected_options.rflavor, selected_options.nthreads, crypt, sender, receiver, m_nPID);
  339. delete receiver;
  340. auto end = std::chrono::steady_clock::now();
  341. std::chrono::duration<double> elapsed_seconds = end - start;
  342. std::cout << "time to generate and evaluate " << nblindbits << " OTs is: " << elapsed_seconds.count() << "s\n";
  343. std::cout << "bytes transmitted for OT = " << (54274 + 33 * nblindbits / 2) << " bytes\n";
  344. }
  345. Cleanup();
  346. delete crypt;
  347. delete glock;
  348. return EXIT_SUCCESS;
  349. }