dlrho.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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, unsigned int *dpwords,
  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. string x((const char *)(dpwords), WORDS*sizeof(unsigned int));
  70. res = d->dtable.insert(DTable::value_type(x, ab));
  71. if (!res.second) {
  72. // Collision!
  73. ZZ adiff = to_ZZ(res.first->second.first) - zz_a;
  74. ZZ bdiff = zz_b - to_ZZ(res.first->second.second);
  75. if (bdiff < 0) bdiff += d->order;
  76. ZZ binv;
  77. if (InvModStatus(binv, bdiff, d->order) == 0) {
  78. d->expon = MulMod(binv, adiff, d->order);
  79. d->found_collision = true;
  80. } else {
  81. if (d->order > (1<<20)) {
  82. cerr << "Unhelpful collision\n";
  83. }
  84. }
  85. }
  86. return d->found_collision;
  87. }
  88. // Compute the discrete log of target mod p, to the given base.
  89. // p must be the current ZZ_p modulus.
  90. // Place the result in exp. fvec is a vector of the factors of
  91. // (p-1)/2, which must each be small enough to compute discrete logs
  92. // with some other method (kangaroo, index calculus, GNFS, etc.).
  93. // label is "p" or "q", to be printed to report progress.
  94. // Return 0 on failure, 1 on success.
  95. static int p_dl(const ZZ_p &target, const ZZ_p &base, ZZ &exp,
  96. const vec_ZZ &fvec, const string &label)
  97. {
  98. const int flen = fvec.length();
  99. // Compute phi(p)
  100. ZZ phip;
  101. phip = 2;
  102. for (int i = 0; i < flen; ++i) {
  103. phip *= fvec[i];
  104. }
  105. // Invariant: the desired exp \equiv (curexp mod curmodulus)
  106. ZZ curmodulus, curexp;
  107. curmodulus = 2;
  108. curexp = 0;
  109. for (int i = 0; i < flen; ++i) {
  110. cout << label << " submodulus " << i+1 << " of " << flen << "... ";
  111. cout.flush();
  112. // Figure out exp mod fvec[i] by taking each side to the power
  113. // of phirho/fvec[i] so that we're working in the
  114. // subgroup of order fvec[i].
  115. ZZ quotient = phip / fvec[i];
  116. ZZ_p subgroup_base = power(base, quotient);
  117. ZZ_p subgroup_target = power(target, quotient);
  118. if (subgroup_base == 1) {
  119. // The original base wasn't a generator of the whole group.
  120. if (subgroup_target == 1) {
  121. cout << "Non-unique solution (mod " << fvec[i] <<")\n";
  122. continue;
  123. } else {
  124. cout << "Target not in subgroup generated by base\n";
  125. return 0;
  126. }
  127. }
  128. // Now use your favourite method to get the DL of
  129. // subgroup_target with base subgroup_base, knowing that it's in
  130. // the range [0,fvec[i]).
  131. cout << "\n";
  132. ZZ md = ZZ_p::modulus();
  133. CBData cbdata(subgroup_base, subgroup_target, fvec[i]);
  134. struct timeval st, et;
  135. gettimeofday(&st, NULL);
  136. cuda_dl(subgroup_base, subgroup_target, fvec[i], md, &cbdata);
  137. ZZ subgroup_dl = cbdata.expon;
  138. gettimeofday(&et, NULL);
  139. unsigned long us_elapsed = (et.tv_sec-st.tv_sec)*1000000 +
  140. (et.tv_usec-st.tv_usec);
  141. printf("%ld.%06ld seconds elapsed\n", us_elapsed/1000000,
  142. us_elapsed % 1000000);
  143. CRT(curexp, curmodulus, subgroup_dl, fvec[i]);
  144. // cout << "CRT\n";
  145. // cout << "curexp = " << curexp << "\n";
  146. // cout << "curmodulus = " << curmodulus << "\n\n";
  147. }
  148. // We'd like a non-negative answer back
  149. if (curexp >= 0) {
  150. exp = curexp;
  151. } else {
  152. exp = curexp + curmodulus;
  153. }
  154. return 1;
  155. }
  156. typedef struct {
  157. pid_t pid;
  158. int rfd;
  159. } PDLHandle;
  160. // Behave like p_dl, but do the work in an asynchronous subprocess
  161. static PDLHandle* p_dl_fork_start(const ZZ &p, const ZZ_p &target,
  162. const ZZ_p &base, const vec_ZZ &fvec, const string &label, int deviceid)
  163. {
  164. PDLHandle *handle = new PDLHandle;
  165. int fds[2];
  166. int res = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
  167. if (res < 0) {
  168. perror("socketpair");
  169. delete handle;
  170. return NULL;
  171. }
  172. handle->rfd = fds[0];
  173. pid_t childpid = fork();
  174. if (childpid == -1) {
  175. perror("fork");
  176. delete handle;
  177. return NULL;
  178. }
  179. if (childpid == 0) {
  180. // Child
  181. close(fds[0]);
  182. int wfd = fds[1];
  183. ZZ exp;
  184. ZZ_p::init(p);
  185. ZZ_p target_p, base_p;
  186. conv(target_p, rep(target));
  187. conv(base_p, rep(base));
  188. cudaError_t cudares = cudaSetDevice(deviceid);
  189. if (cudares != cudaSuccess) {
  190. cerr << "Error setting CUDA device\n";
  191. exit(1);
  192. }
  193. int res = p_dl(target_p, base_p, exp, fvec, label);
  194. if (res) {
  195. // Write the result back to the parent using wfd
  196. unsigned short explen = NumBytes(exp);
  197. res = write(wfd, &explen, sizeof(unsigned short));
  198. unsigned char expbuf[explen];
  199. BytesFromZZ(expbuf, exp, explen);
  200. res = write(wfd, expbuf, explen);
  201. close(wfd);
  202. }
  203. exit(0);
  204. } else {
  205. // Parent
  206. close(fds[1]);
  207. }
  208. return handle;
  209. }
  210. // Wait until the subprocess started by p_dl_fork_start completes, and
  211. // return its result. handle is cleaned up.
  212. static int p_dl_fork_join(PDLHandle *handle, ZZ &exp)
  213. {
  214. if (!handle) return 0;
  215. pid_t childpid = handle->pid;
  216. unsigned short explen;
  217. int res = read(handle->rfd, &explen, sizeof(unsigned short));
  218. if (res < (int)sizeof(unsigned short)) {
  219. close(handle->rfd);
  220. delete handle;
  221. waitpid(childpid, NULL, 0);
  222. return 0;
  223. }
  224. unsigned char expbuf[explen];
  225. res = read(handle->rfd, expbuf, explen);
  226. if (res < explen) {
  227. close(handle->rfd);
  228. delete handle;
  229. waitpid(childpid, NULL, 0);
  230. return 0;
  231. }
  232. ZZFromBytes(exp, expbuf, explen);
  233. close(handle->rfd);
  234. delete handle;
  235. waitpid(childpid, NULL, 0);
  236. return 1;
  237. }
  238. int main(int argc, char **argv)
  239. {
  240. // Initialize the prng with some randomness from the kernel
  241. unsigned char randbuf[1024];
  242. ifstream urand("/dev/urandom");
  243. urand.read((char *)randbuf, sizeof(randbuf));
  244. urand.close();
  245. ZZ randzz = ZZFromBytes(randbuf, sizeof(randbuf));
  246. SetSeed(randzz);
  247. ZZ rho, p, q;
  248. vec_ZZ pfvec, qfvec;
  249. cin >> rho >> p >> pfvec >> q >> qfvec;
  250. // Generate a DLP mod rho (in the large odd-order subgroup)
  251. ZZ_p::init(rho);
  252. ZZ_p base = power(random_ZZ_p(), 2);
  253. ZZ_p target = power(random_ZZ_p(), 2);
  254. cout << "base = " << base << "\n";
  255. cout << "target = " << target << "\n";
  256. ZZ exp_p, exp_q, exp;
  257. int res_p = 0;
  258. int res_q = 0;
  259. PDLHandle *handle_p, *handle_q;
  260. handle_p = p_dl_fork_start(p, target, base, pfvec, "p", 0);
  261. handle_q = p_dl_fork_start(q, target, base, qfvec, "q", 1);
  262. res_p = p_dl_fork_join(handle_p, exp_p);
  263. res_q = p_dl_fork_join(handle_q, exp_q);
  264. if (res_p && res_q) {
  265. ZZ_p::init(rho);
  266. ZZ pm1 = (p - 1)/2;
  267. ZZ qm1 = (q - 1)/2;
  268. if (exp_p < 0) exp_p += pm1;
  269. if (exp_q < 0) exp_q += qm1;
  270. CRT(exp_p, pm1, exp_q, qm1);
  271. exp = exp_p;
  272. // We'd like a non-negative answer back
  273. if (exp < 0) exp += pm1;
  274. cout << "exp = " << exp << "\n";
  275. ZZ_p base_exp;
  276. power(base_exp, base, exp);
  277. if (base_exp == target) {
  278. cout << "CORRECT!\n";
  279. } else {
  280. cout << "INCORRECT:\nbase^exp = " << base_exp << "\n";
  281. cout << "target = " << target << "\n";
  282. }
  283. } else {
  284. cout << "FAIL\n";
  285. }
  286. return 0;
  287. }