dlrho.cc 9.0 KB

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