dlrho.cc 9.1 KB

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