stranamore.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * \file stranamore.c
  3. *
  4. * \brief Probe for common prime factors among a file of public moduluses.
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <mpi.h>
  9. #include <openssl/bn.h>
  10. #define EQUAL_BN "equal"
  11. #define PRIME "prime"
  12. int next_mod(BIGNUM **n, FILE *fp)
  13. {
  14. static char buf[2048];
  15. if (fscanf(fp, "%s", buf) != 1)
  16. return 0;
  17. else return BN_hex2bn(n, buf);
  18. }
  19. /**
  20. * \brief Test for a pair of moduluses having a prime factor in common.
  21. *
  22. */
  23. int test(BIGNUM *n, BIGNUM *m)
  24. {
  25. BIGNUM *g;
  26. BN_CTX *ctx;
  27. int ret = 0;
  28. if (!BN_cmp(n, m)) return 1;
  29. g = BN_new();
  30. ctx = BN_CTX_new();
  31. BN_gcd(g, n, m, ctx);
  32. if (!BN_is_one(g)) {
  33. fprintf(stdout, "%-8s: ", PRIME);
  34. BN_print_fp(stdout, n);
  35. fprintf(stdout, " ");
  36. BN_print_fp(stdout, m);
  37. fprintf(stdout, "\n");
  38. ret = 1;
  39. }
  40. BN_CTX_free(ctx);
  41. BN_free(g);
  42. return ret;
  43. }
  44. int main(int argc, char **argv)
  45. {
  46. FILE *fst, *snd;
  47. BIGNUM *n, *m;
  48. int i;
  49. int proc, procs;
  50. MPI_Init(&argc, &argv);
  51. MPI_Comm_rank(MPI_COMM_WORLD, &proc);
  52. MPI_Comm_size(MPI_COMM_WORLD, &procs);
  53. if (argc < 2) return EXIT_FAILURE;
  54. fst = fopen(argv[argc-1], "r");
  55. if (!fst) return EXIT_FAILURE;
  56. snd = fopen(argv[argc-1], "r");
  57. if (!snd) return EXIT_FAILURE;
  58. n = BN_new();
  59. m = BN_new();
  60. for (i=0; next_mod(&n, fst); i=(i+1) % procs) {
  61. if (i != proc) continue;
  62. fseek(snd, ftell(fst), SEEK_SET);
  63. /* trash first modulus */
  64. while (next_mod(&m, snd))
  65. test(n, m);
  66. }
  67. BN_free(n);
  68. BN_free(m);
  69. fclose(fst);
  70. fclose(snd);
  71. MPI_Finalize();
  72. return 0;
  73. }