dlrho.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * cudadl version 0.9: Compute discrete logs in smooth group orders
  3. * using CUDA
  4. * Copyright (C) 2012 by Ryan Henry and Ian Goldberg
  5. * {rhenry,iang}@cs.uwaterloo.ca
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of version 3 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <fstream>
  20. #include <sys/time.h>
  21. #include <NTL/vec_ZZ.h>
  22. #include <NTL/ZZ_p.h>
  23. #include <map>
  24. #include <sys/types.h>
  25. #include <sys/wait.h>
  26. #include <sys/socket.h>
  27. #include <cuda_runtime.h>
  28. #include "cudadl.h"
  29. NTL_CLIENT
  30. typedef map<std::string, pair<ZZ,ZZ> > DTable;
  31. struct CBData {
  32. const ZZ_p &base;
  33. const ZZ_p &target;
  34. const ZZ &order;
  35. unsigned long long numdp;
  36. DTable dtable;
  37. bool found_collision;
  38. ZZ expon;
  39. CBData(const ZZ_p &_base, const ZZ_p &_target, const ZZ &_order) :
  40. base(_base), target(_target), order(_order), numdp(0),
  41. found_collision(false) {}
  42. };
  43. // This function is called from inside cuda_dl for each DP it encounters.
  44. // It calls the function named "dpcallback" directly. It would be
  45. // cleaner if this were passed as a function pointer to cuda_dl, but
  46. // that makes nvcc 3.1 segfault. :-p
  47. bool dpcallback(void *cbdata, unsigned short threadId,
  48. unsigned short blockId, unsigned int demux, string x,
  49. unsigned int a_0, unsigned int a_1, unsigned int a_2,
  50. unsigned int b_0, unsigned int b_1, unsigned int b_2)
  51. {
  52. CBData *d = (CBData*)cbdata;
  53. // WARNING: this assumes
  54. // sizeof(unsigned long) == sizeof(unsigned long long) !
  55. ZZ zz_a = to_ZZ(a_2);
  56. zz_a <<= 32;
  57. zz_a += a_1;
  58. zz_a <<= 32;
  59. zz_a += a_0;
  60. ZZ zz_b = to_ZZ(b_2);
  61. zz_b <<= 32;
  62. zz_b += b_1;
  63. zz_b <<= 32;
  64. zz_b += b_0;
  65. //ZZ_p dp = power(d->base, zz_a) * power(d->target, zz_b);
  66. // cerr << "DP " << ++(d->numdp) << "\r";
  67. pair<ZZ,ZZ> ab(zz_a,zz_b);
  68. pair<DTable::iterator, bool> res;
  69. res = d->dtable.insert(DTable::value_type(x, ab));
  70. if (!res.second) {
  71. // Collision!
  72. ZZ adiff = to_ZZ(res.first->second.first) - zz_a;
  73. ZZ bdiff = zz_b - to_ZZ(res.first->second.second);
  74. if (bdiff < 0) bdiff += d->order;
  75. ZZ binv;
  76. if (InvModStatus(binv, bdiff, d->order) == 0) {
  77. d->expon = MulMod(binv, adiff, d->order);
  78. d->found_collision = true;
  79. } else {
  80. if (d->order > (1<<20)) {
  81. cerr << "Unhelpful collision\n";
  82. }
  83. }
  84. }
  85. return d->found_collision;
  86. }
  87. // Compute the discrete log of target mod p, to the given base.
  88. // p must be the current ZZ_p modulus.
  89. // Place the result in exp. fvec is a vector of the factors of
  90. // (p-1)/2, which must each be small enough to compute discrete logs
  91. // with some other method (kangaroo, index calculus, GNFS, etc.).
  92. // label is "p" or "q", to be printed to report progress.
  93. // Return 0 on failure, 1 on success.
  94. static int p_dl(const ZZ_p &target, const ZZ_p &base, ZZ &exp,
  95. const vec_ZZ &fvec, const string &label)
  96. {
  97. const int flen = fvec.length();
  98. // Compute phi(p)
  99. ZZ phip;
  100. phip = 2;
  101. for (int i = 0; i < flen; ++i) {
  102. phip *= fvec[i];
  103. }
  104. // Invariant: the desired exp \equiv (curexp mod curmodulus)
  105. ZZ curmodulus, curexp;
  106. curmodulus = 2;
  107. curexp = 0;
  108. for (int i = 0; i < flen; ++i) {
  109. cout << label << " submodulus " << i+1 << " of " << flen << "... ";
  110. cout.flush();
  111. // Figure out exp mod fvec[i] by taking each side to the power
  112. // of phirho/fvec[i] so that we're working in the
  113. // subgroup of order fvec[i].
  114. ZZ quotient = phip / fvec[i];
  115. ZZ_p subgroup_base = power(base, quotient);
  116. ZZ_p subgroup_target = power(target, quotient);
  117. if (subgroup_base == 1) {
  118. // The original base wasn't a generator of the whole group.
  119. if (subgroup_target == 1) {
  120. cout << "Non-unique solution (mod " << fvec[i] <<")\n";
  121. continue;
  122. } else {
  123. cout << "Target not in subgroup generated by base\n";
  124. return 0;
  125. }
  126. }
  127. // Now use your favourite method to get the DL of
  128. // subgroup_target with base subgroup_base, knowing that it's in
  129. // the range [0,fvec[i]).
  130. cout << "\n";
  131. ZZ md = ZZ_p::modulus();
  132. CBData cbdata(subgroup_base, subgroup_target, fvec[i]);
  133. struct timeval st, et;
  134. gettimeofday(&st, NULL);
  135. cuda_dl(subgroup_base, subgroup_target, fvec[i], md, &cbdata);
  136. ZZ subgroup_dl = cbdata.expon;
  137. gettimeofday(&et, NULL);
  138. unsigned long us_elapsed = (et.tv_sec-st.tv_sec)*1000000 +
  139. (et.tv_usec-st.tv_usec);
  140. printf("%ld.%06ld seconds elapsed\n", us_elapsed/1000000,
  141. us_elapsed % 1000000);
  142. CRT(curexp, curmodulus, subgroup_dl, fvec[i]);
  143. // cout << "CRT\n";
  144. // cout << "curexp = " << curexp << "\n";
  145. // cout << "curmodulus = " << curmodulus << "\n\n";
  146. }
  147. // We'd like a non-negative answer back
  148. if (curexp >= 0) {
  149. exp = curexp;
  150. } else {
  151. exp = curexp + curmodulus;
  152. }
  153. return 1;
  154. }
  155. typedef struct {
  156. pid_t pid;
  157. int rfd;
  158. } PDLHandle;
  159. // Behave like p_dl, but do the work in an asynchronous subprocess
  160. static PDLHandle* p_dl_fork_start(const ZZ &p, const ZZ_p &target,
  161. const ZZ_p &base, const vec_ZZ &fvec, const string &label, int deviceid)
  162. {
  163. PDLHandle *handle = new PDLHandle;
  164. int fds[2];
  165. int res = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
  166. if (res < 0) {
  167. perror("socketpair");
  168. delete handle;
  169. return NULL;
  170. }
  171. handle->rfd = fds[0];
  172. pid_t childpid = fork();
  173. if (childpid == -1) {
  174. perror("fork");
  175. delete handle;
  176. return NULL;
  177. }
  178. if (childpid == 0) {
  179. // Child
  180. close(fds[0]);
  181. int wfd = fds[1];
  182. ZZ exp;
  183. ZZ_p::init(p);
  184. ZZ_p target_p, base_p;
  185. conv(target_p, rep(target));
  186. conv(base_p, rep(base));
  187. cudaError_t cudares = cudaSetDevice(deviceid);
  188. if (cudares != cudaSuccess) {
  189. cerr << "Error setting CUDA device\n";
  190. exit(1);
  191. }
  192. int res = p_dl(target_p, base_p, exp, fvec, label);
  193. if (res) {
  194. // Write the result back to the parent using wfd
  195. unsigned short explen = NumBytes(exp);
  196. res = write(wfd, &explen, sizeof(unsigned short));
  197. unsigned char expbuf[explen];
  198. BytesFromZZ(expbuf, exp, explen);
  199. res = write(wfd, expbuf, explen);
  200. close(wfd);
  201. }
  202. exit(0);
  203. } else {
  204. // Parent
  205. close(fds[1]);
  206. }
  207. return handle;
  208. }
  209. // Wait until the subprocess started by p_dl_fork_start completes, and
  210. // return its result. handle is cleaned up.
  211. static int p_dl_fork_join(PDLHandle *handle, ZZ &exp)
  212. {
  213. if (!handle) return 0;
  214. pid_t childpid = handle->pid;
  215. unsigned short explen;
  216. int res = read(handle->rfd, &explen, sizeof(unsigned short));
  217. if (res < (int)sizeof(unsigned short)) {
  218. close(handle->rfd);
  219. delete handle;
  220. waitpid(childpid, NULL, 0);
  221. return 0;
  222. }
  223. unsigned char expbuf[explen];
  224. res = read(handle->rfd, expbuf, explen);
  225. if (res < explen) {
  226. close(handle->rfd);
  227. delete handle;
  228. waitpid(childpid, NULL, 0);
  229. return 0;
  230. }
  231. ZZFromBytes(exp, expbuf, explen);
  232. close(handle->rfd);
  233. delete handle;
  234. waitpid(childpid, NULL, 0);
  235. return 1;
  236. }
  237. int main(int argc, char **argv)
  238. {
  239. // Initialize the prng with some randomness from the kernel
  240. unsigned char randbuf[1024];
  241. ifstream urand("/dev/urandom");
  242. urand.read((char *)randbuf, sizeof(randbuf));
  243. urand.close();
  244. ZZ randzz = ZZFromBytes(randbuf, sizeof(randbuf));
  245. SetSeed(randzz);
  246. ZZ rho, p, q;
  247. vec_ZZ pfvec, qfvec;
  248. cin >> rho >> p >> pfvec >> q >> qfvec;
  249. // Generate a DLP mod rho (in the large odd-order subgroup)
  250. ZZ_p::init(rho);
  251. ZZ_p base = power(random_ZZ_p(), 2);
  252. ZZ_p target = power(random_ZZ_p(), 2);
  253. cout << "base = " << base << "\n";
  254. cout << "target = " << target << "\n";
  255. ZZ exp_p, exp_q, exp;
  256. int res_p = 0;
  257. int res_q = 0;
  258. PDLHandle *handle_p, *handle_q;
  259. handle_p = p_dl_fork_start(p, target, base, pfvec, "p", 0);
  260. handle_q = p_dl_fork_start(q, target, base, qfvec, "q", 1);
  261. res_p = p_dl_fork_join(handle_p, exp_p);
  262. res_q = p_dl_fork_join(handle_q, exp_q);
  263. if (res_p && res_q) {
  264. ZZ_p::init(rho);
  265. ZZ pm1 = (p - 1)/2;
  266. ZZ qm1 = (q - 1)/2;
  267. if (exp_p < 0) exp_p += pm1;
  268. if (exp_q < 0) exp_q += qm1;
  269. CRT(exp_p, pm1, exp_q, qm1);
  270. exp = exp_p;
  271. // We'd like a non-negative answer back
  272. if (exp < 0) exp += pm1;
  273. cout << "exp = " << exp << "\n";
  274. ZZ_p base_exp;
  275. power(base_exp, base, exp);
  276. if (base_exp == target) {
  277. cout << "CORRECT!\n";
  278. } else {
  279. cout << "INCORRECT:\nbase^exp = " << base_exp << "\n";
  280. cout << "target = " << target << "\n";
  281. }
  282. } else {
  283. cout << "FAIL\n";
  284. }
  285. return 0;
  286. }