ot_blinds.cpp 12 KB

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