ot_blinds.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. CBitVector **XoplusY = (CBitVector **)malloc(sizeof(CBitVector *) * nsndvals);
  111. //The masking function with which the values that are sent in the last communication step are processed
  112. XORMasking *m_fMaskFct = new XORMasking(bitlength, delta);
  113. //creates delta as an array with "numOTs" entries of "bitlength" bit-values and fills delta with random values
  114. delta.Create(numots, bitlength, crypt);
  115. //Create X1 and X2 as two arrays with "numOTs" entries of "bitlength" bit-values and resets them to 0
  116. //X1.Create(numots, bitlength, crypt);
  117. //X2.Create(numots, bitlength, crypt);
  118. //X[0] --> T0
  119. //X[1] --> X0
  120. //Y[0] --> Y0
  121. printf("run_test_sender\n");
  122. //(T, X0 \oplus T)
  123. for (uint32_t i = 0; i < nsndvals; i++)
  124. {
  125. X[i] = new CBitVector();
  126. // X[i]->Create(numots, bitlength, crypt);
  127. X[i]->Create(numots, bitlength);
  128. X[i]->FillRand(numots, crypt);
  129. Y[i] = new CBitVector();
  130. // X[i]->Create(numots, bitlength, crypt);
  131. Y[i]->Create(numots, bitlength);
  132. // Y[i]->FillRand(n_OTS, crypt);
  133. Y[i]->SetToOne(); // I added this line
  134. //X[i]->PrintBinary(); // I added this line
  135. }
  136. X0.Create(numots, bitlength);
  137. X0.Reset();
  138. X0.XOR(X[1]);
  139. //Y[0]->Copy(X[1]);
  140. #ifdef VERBOSE
  141. printf("X0:\n");
  142. //X[1]->PrintBinary(); // I added this line
  143. X0.PrintHex(); // I added this line
  144. printf("Y0:\n");
  145. Y[0]->PrintHex(); // I added this line
  146. printf("T0: \n");
  147. X[0]->PrintHex();
  148. #endif
  149. X[1]->XOR(X[0]);
  150. //X[1] --> X0 \oplus T0
  151. //for (uint32_t i = 0; i < nsndvals; i++)
  152. {
  153. // printf("T0:\n");
  154. // X[0]->PrintBinary(); // I added this line
  155. // printf("X0 ^ T0:\n");
  156. // X[1]->PrintBinary(); // I added this line
  157. }
  158. sender->send(numots, bitlength, nsndvals, X, stype, rtype, numthreads, m_fMaskFct);
  159. CBitVector response;
  160. //Pre-generate the respose vector for the results
  161. response.Create(numots, bitlength);
  162. response.Reset();
  163. //Y[0] is the choice vector.
  164. receiver->receive(numots, bitlength, nsndvals, Y[0], &response, stype, rtype, numthreads, m_fMaskFct);
  165. response.XOR(X[0]);
  166. #ifdef VERBOSE
  167. printf("\n\nLearnt Gamma0 = (X1 /cdot Y0) /oplus T------>>>>>> \n");
  168. response.PrintHex();
  169. #endif
  170. // std::ofstream gammafile0, X0file, Y0file;
  171. // gammafile0.open ("gamma0.txt", std::ios::out);
  172. // X0file.open("X0.txt", std::ios::out);
  173. // Y0file.open("Y0.txt", std::ios::out);
  174. int fd = open("./y0", O_CREAT | O_WRONLY | O_TRUNC, 0600);;
  175. ssize_t bytes = write(fd, Y[0]->GetArr(), Y[0]->GetSize());
  176. close(fd);
  177. int fd2 = open("./x0", O_CREAT | O_WRONLY | O_TRUNC, 0600);;
  178. ssize_t bytes2 = write(fd2, X0.GetArr(), X0.GetSize());
  179. close(fd2);
  180. int fd3 = open("./gamma0", O_CREAT | O_WRONLY | O_TRUNC, 0600);;
  181. ssize_t bytes3 = write(fd3, response.GetArr(), response.GetSize());
  182. close(fd3);
  183. // for(size_t j = 0; j < numots; ++j)
  184. // {
  185. // // std::cout << (int) response.GetBitNoMask(j);
  186. // gammafile0 << (int) response.GetBitNoMask(j);
  187. // X0file << (int) X0.GetBitNoMask(j);
  188. // Y0file << (int) Y[0]->GetBitNoMask(j);
  189. // }
  190. for (uint32_t i = 0; i < nsndvals; i++)
  191. {
  192. delete (X[i]);
  193. }
  194. free(X);
  195. delta.delCBitVector();
  196. delete m_fMaskFct;
  197. }
  198. // This is P1
  199. void run_test_receiver(uint32_t numots, uint32_t bitlength, snd_ot_flavor stype, rec_ot_flavor rtype, uint32_t numthreads,
  200. crypto *crypt, OTExtSnd *sender, OTExtRec *receiver, int m_nPID)
  201. {
  202. CBitVector X1, response;
  203. uint32_t nsndvals = 2;
  204. CBitVector **X = (CBitVector **)malloc(sizeof(CBitVector *) * nsndvals);
  205. CBitVector **Y = (CBitVector **)malloc(sizeof(CBitVector *) * nsndvals);
  206. //X[0] --> T1
  207. //X[1] --> X1
  208. //Y[0] --> Y1
  209. for (uint32_t i = 0; i < nsndvals; i++)
  210. {
  211. X[i] = new CBitVector();
  212. X[i]->Create(numots, bitlength);
  213. X[i]->FillRand(numots, crypt);
  214. Y[i] = new CBitVector();
  215. Y[i]->Create(numots, bitlength);
  216. //Y[i]->FillRand(n_OTS, crypt);
  217. Y[i]->SetToOne();
  218. }
  219. X1.Create(numots, bitlength);
  220. X1.Reset();
  221. X1.XOR(X[1]);
  222. #ifdef VERBOSE
  223. printf("X1:\n");
  224. //X[1]->PrintBinary(); // I added this line
  225. X1.PrintHex(); // I added this line
  226. printf("Y1:\n");
  227. Y[0]->PrintHex(); // I added this line
  228. printf("T1: \n");
  229. X[0]->PrintHex();
  230. #endif
  231. // X[1] -- > X1 \oplus T1
  232. X[1]->XOR(X[0]);
  233. //The masking function with which the values that are sent in the last communication step are processed
  234. XORMasking *m_fMaskFct = new XORMasking(bitlength);
  235. //Pre-generate the respose vector for the results
  236. response.Create(numots, bitlength);
  237. response.Reset();
  238. /*
  239. * The inputs of the receiver in G_OT, C_OT and R_OT are the same. The only difference is the version
  240. * variable that has to match the version of the sender.
  241. */
  242. //Learns: (X0 \cdot Y1) \oplus T0
  243. //Y0 is the choice bits.
  244. receiver->receive(numots, bitlength, nsndvals, Y[0], &response, stype, rtype, numthreads, m_fMaskFct);
  245. sender->send(numots, bitlength, nsndvals, X, stype, rtype, numthreads, m_fMaskFct);
  246. // printf("Learnt: (X0 /cdot Y1) /oplus T0------>>>>>> \n");
  247. // response.PrintBinary();
  248. response.XOR(X[0]);
  249. #ifdef VERBOSE
  250. printf("\n\nLearnt: Gamma1 = (X0 /cdot Y1) /oplus T------>>>>>> \n");
  251. response.PrintHex();
  252. #endif
  253. //std::ofstream gammafile1, X1file, Y1file;
  254. int fd = open("./y1", O_CREAT | O_WRONLY | O_TRUNC, 0600);;
  255. ssize_t bytes = write(fd, Y[0]->GetArr(), Y[0]->GetSize());
  256. #ifdef VERBOSE
  257. std::cout << "bytes = " << bytes << std::endl;
  258. #endif
  259. close(fd);
  260. int fd2 = open("./x1", O_CREAT | O_WRONLY | O_TRUNC, 0600);;
  261. ssize_t bytes2 = write(fd2, X1.GetArr(), X1.GetSize());
  262. close(fd2);
  263. int fd3 = open("./gamma1", O_CREAT | O_WRONLY | O_TRUNC, 0600);;
  264. ssize_t bytes3 = write(fd3, response.GetArr(), response.GetSize());
  265. close(fd3);
  266. #ifdef VERBOSE
  267. std::cout << "X1: \n\n";
  268. X1.PrintHex();
  269. #endif
  270. // gammafile1.open ("gamma1.txt", std::ios::out);
  271. // X1file.open("X1.txt", std::ios::out);
  272. // Y1file.open("Y1.txt", std::ios::out);
  273. // for(size_t j = 0; j < numots; ++j)
  274. // {
  275. // // std::cout << (int) response.GetBitNoMask(j);
  276. // gammafile1 << (int) response.GetBitNoMask(j);
  277. // X1file << (int) X1.GetBitNoMask(j);
  278. // Y1file << (int) Y[0]->GetBitNoMask(j);
  279. // }
  280. //gammafile1.close();
  281. delete m_fMaskFct;
  282. // choices.delCBitVector();
  283. response.delCBitVector();
  284. }
  285. int main(int argc, char **argv)
  286. {
  287. std::string addr0 = argv[1]; // "127.0.0.1";
  288. std::string addr1 = argv[2]; // "127.0.0.1";
  289. int port = 7766;
  290. __m128i blinds;
  291. // if (argc != 2)
  292. // {
  293. // std::cout << "Please call with 0 if acting as server or 1 if acting as client" << std::endl;
  294. // return EXIT_FAILURE;
  295. // }
  296. //Determines whether the program is executed in the sender or receiver role
  297. m_nPID = atoi(argv[3]);
  298. int64_t nblindbits = atoi(argv[4]);
  299. std::cout << "Playing as role: " << m_nPID << std::endl;
  300. assert(m_nPID >= 0 && m_nPID <= 1);
  301. //The symmetric security parameter (80, 112, 128)
  302. uint32_t m_nSecParam = 128;
  303. crypto *crypt = new crypto(m_nSecParam, (uint8_t *)m_cConstSeed[m_nPID]);
  304. CLock *glock = new CLock(); // pass this to sender and receiver constructors
  305. uint32_t m_nBaseOTs = 190;
  306. uint32_t m_nChecks = 380;
  307. // NOTE: This vector controls the settings used by the oblivious transfer.
  308. test_options selected_options; // = {IKNP, 128, 1, Snd_C_OT, Rec_OT, 1, ECC_FIELD, false};
  309. selected_options.prot = IKNP;
  310. selected_options.numots = nblindbits; // Number of OTs performed using the extended COT.
  311. selected_options.bitlen = 1;
  312. selected_options.sflavor = Snd_OT; //Snd_C_OT;
  313. selected_options.rflavor = Rec_OT;
  314. selected_options.nthreads = 1; // Number of threads
  315. selected_options.ftype = P_FIELD; // Type of field to use for the base OT.
  316. selected_options.usemecr = false;
  317. // test_options selected_options = {IKNP, 128, 1, Snd_C_OT, Rec_OT, 1, P_FIELD, false}; // Alternative using P_FIELD
  318. if (m_nPID == SERVER_ID) //Play as OT sender
  319. {
  320. InitSender(addr0, port, glock);
  321. OTExtSnd *sender = NULL;
  322. InitReceiver(addr1, port, glock);
  323. OTExtRec *receiver = NULL;
  324. auto start = std::chrono::steady_clock::now();
  325. sender = InitOTExtSnd(selected_options.prot, m_nBaseOTs, m_nChecks, selected_options.usemecr, selected_options.ftype, crypt);
  326. receiver = InitOTExtRec(selected_options.prot, m_nBaseOTs, m_nChecks, selected_options.usemecr, selected_options.ftype, crypt);
  327. 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;
  328. run_test_sender(selected_options.numots, selected_options.bitlen, selected_options.sflavor, selected_options.rflavor, selected_options.nthreads, crypt, sender, receiver);
  329. auto end = std::chrono::steady_clock::now();
  330. std::chrono::duration<double> elapsed_seconds = end - start;
  331. std::cout << "time to generate and evaluate " << nblindbits << " OTs is: " << elapsed_seconds.count() << "s\n";
  332. std::cout << "bytes transmitted for OT = " << (54292 + 33 * nblindbits / 2) << " bytes\n";
  333. delete sender;
  334. }
  335. else //Play as OT receiver
  336. {
  337. InitReceiver(addr0, port, glock);
  338. OTExtRec *receiver = NULL;
  339. InitSender(addr1, port, glock);
  340. auto start = std::chrono::steady_clock::now();
  341. OTExtSnd *sender = NULL;
  342. receiver = InitOTExtRec(selected_options.prot, m_nBaseOTs, m_nChecks, selected_options.usemecr, selected_options.ftype, crypt);
  343. sender = InitOTExtSnd(selected_options.prot, m_nBaseOTs, m_nChecks, selected_options.usemecr, selected_options.ftype, crypt);
  344. 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;
  345. run_test_receiver(selected_options.numots, selected_options.bitlen, selected_options.sflavor, selected_options.rflavor, selected_options.nthreads, crypt, sender, receiver, m_nPID);
  346. delete receiver;
  347. auto end = std::chrono::steady_clock::now();
  348. std::chrono::duration<double> elapsed_seconds = end - start;
  349. std::cout << "time to generate and evaluate " << nblindbits << " OTs is: " << elapsed_seconds.count() << "s\n";
  350. std::cout << "bytes transmitted for OT = " << (54274 + 33 * nblindbits / 2) << " bytes\n";
  351. }
  352. Cleanup();
  353. delete crypt;
  354. delete glock;
  355. return EXIT_SUCCESS;
  356. }