gen_N.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <NTL/vec_ZZ.h>
  21. #include <NTL/ZZ_p.h>
  22. NTL_CLIENT
  23. // Make one attempt at generating a "smooth" prime p; i.e. a prime p
  24. // such that all of the prime factors of p-1 are at most l_B bits long.
  25. // Return 1 on success (and p is set to the prime and fvec is set to the
  26. // list of factors of (p-1)/2). Return 0 on failure. It is expected that
  27. // this function will fail most of the time. p will end up being
  28. // p_bits bits long.
  29. static int try_gen_smooth_prime(ZZ& p, vec_ZZ &fvec, long p_bits, long l_B)
  30. {
  31. ZZ prod_so_far;
  32. prod_so_far = 1;
  33. fvec.SetLength(0);
  34. while(NumBits(prod_so_far) < p_bits - 1) {
  35. long next_bits = p_bits - 1 - NumBits(prod_so_far);
  36. if (next_bits > l_B) next_bits = l_B;
  37. // For the last prime, add a bit so we're not accidentally short
  38. // one bit
  39. if (next_bits < l_B) ++next_bits;
  40. // There's only one prime of length < 3
  41. if (next_bits < 3) next_bits = 3;
  42. // cout << "next_bits = " << next_bits << "\n";
  43. ZZ nextprime = GenPrime_ZZ(next_bits);
  44. append(fvec, nextprime);
  45. prod_so_far *= nextprime;
  46. // cout << "next prime = " << nextprime << "\n";
  47. }
  48. // Now see if 2*prod_so_far - 1 is prime
  49. p = prod_so_far * 2;
  50. p += 1;
  51. return NumBits(p) == p_bits && ProbPrime(p);
  52. }
  53. // Verify that all of the elements of v2 are coprime to (a) all of the
  54. // elements of v1, and (b) each other. Return 1 if so, 0 if not.
  55. static int all_coprime(const vec_ZZ &v1, const vec_ZZ &v2)
  56. {
  57. ZZ prod_so_far;
  58. prod_so_far = 1;
  59. const int v1l = v1.length();
  60. for (int i=0; i < v1l; ++i) {
  61. prod_so_far *= v1[i];
  62. }
  63. const int v2l = v2.length();
  64. for (int i=0; i < v2l; ++i) {
  65. ZZ nextnum = v2[i];
  66. ZZ g = GCD(prod_so_far, nextnum);
  67. if (g > 1) {
  68. return 0;
  69. }
  70. prod_so_far *= nextnum;
  71. }
  72. return 1;
  73. }
  74. // Generate rho, a product of two l_B-smooth primes. rho should be
  75. // about rho_bits long. p and q will be assigned the factors of rho.
  76. // pfvec and qfvec will be populated with the prime factors
  77. // of (p-1)/2 and (q-1)/2 respectively. If strong==1, then 4*rho+1 must
  78. // also be prime. [p and q will be 2 mod 3, so 2*p*q+1 will be
  79. // divisible by 3 always.]
  80. static void gen_rho(ZZ &rho, vec_ZZ &pfvec, vec_ZZ &qfvec, ZZ &p, ZZ &q,
  81. long rho_bits, long l_B, int strong)
  82. {
  83. do {
  84. pfvec.SetLength(0);
  85. qfvec.SetLength(0);
  86. while(1) {
  87. cerr << ".";
  88. cerr.flush();
  89. int ret = try_gen_smooth_prime(p, pfvec, rho_bits / 2, l_B);
  90. if (ret == 1 && all_coprime(qfvec /* qfvec is empty */, pfvec)) break;
  91. }
  92. cerr << "\np = " << p << "\n\n";
  93. while(1) {
  94. cerr << ".";
  95. cerr.flush();
  96. int ret = try_gen_smooth_prime(q, qfvec, rho_bits - NumBits(p), l_B);
  97. if (ret == 1 && all_coprime(pfvec, qfvec)) break;
  98. }
  99. cerr << "\nq = " << q << "\n\n";
  100. rho = p * q;
  101. if (!strong) break;
  102. ZZ R = 4*rho + 1;
  103. if (!ProbPrime(R)) {
  104. cerr << "4*rho + 1 = " << R << " not prime.\n\n";
  105. } else {
  106. cerr << "R = " << R << "\n\n";
  107. break;
  108. }
  109. } while (1);
  110. }
  111. int main(int argc, char **argv)
  112. {
  113. long rho_bits = argc > 1 ? atoi(argv[1]) : 1536;
  114. long l_B = argc > 2 ? atoi(argv[2]) : 30;
  115. int strong = argc > 3 ? atoi(argv[3]) : 0;
  116. // Initialize the prng with some randomness from the kernel
  117. unsigned char randbuf[1024];
  118. ifstream urand("/dev/urandom");
  119. urand.read((char *)randbuf, sizeof(randbuf));
  120. urand.close();
  121. ZZ randzz = ZZFromBytes(randbuf, sizeof(randbuf));
  122. SetSeed(randzz);
  123. ZZ rho, p, q;
  124. vec_ZZ pfvec, qfvec;
  125. gen_rho(rho, pfvec, qfvec, p, q, rho_bits, l_B, strong);
  126. cout << rho << "\n" << p << "\n" << pfvec << "\n" << q << "\n"
  127. << qfvec << "\n";
  128. return 0;
  129. }