浏览代码

Introducing stranamore, discovering the love of two different random modulus.

Michele Orrù 11 年之前
父节点
当前提交
f51fe8bc07
共有 4 个文件被更改,包括 113 次插入1 次删除
  1. 4 1
      src/Makefile.am
  2. 93 0
      src/stranamore.c
  3. 7 0
      src/tests/test_stranamore.test
  4. 9 0
      src/tests/test_stranamore.txt

+ 4 - 1
src/Makefile.am

@@ -1,9 +1,12 @@
 SUBDIRS = questions/ apps/ tests/
 
-bin_PROGRAMS = qa indiana
+bin_PROGRAMS = qa indiana stranamore
 
 qa_SOURCES = qa.c qa_sock.c cmdline.c
 qa_LDADD = questions/libquestions.a -lssl -lcrypto
 
 indiana_SOURCES = indiana.c qa_sock.c
 indiana_LDADD = questions/libquestions.a -lssl -lcrypto
+
+stranamore_SOURCES = stranamore.c
+stranamore_LDADD = -lssl -lcrypto

+ 93 - 0
src/stranamore.c

@@ -0,0 +1,93 @@
+/**
+ * \file stranamore.c
+ *
+ * \brief Probe for common prime factors among a file of public moduluses.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <mpi.h>
+#include <openssl/bn.h>
+
+#define EQUAL_BN "equal"
+#define PRIME    "prime"
+
+int next_mod(BIGNUM **n, FILE *fp)
+{
+  static char buf[1000];
+
+  if (fscanf(fp, "%s", buf) != 1)
+    return 0;
+  else return BN_hex2bn(n, buf);
+}
+
+/**
+ * \brief Test for a pair of moduluses having a prime factor in common.
+ *
+ */
+int test(BIGNUM *n, BIGNUM *m)
+{
+  BIGNUM *g;
+  BN_CTX *ctx;
+  int ret = 0;
+
+  g = BN_new();
+  ctx = BN_CTX_new();
+
+  if (!BN_cmp(n, m)) {
+    fprintf(stderr, "%-8s: ", EQUAL_BN);
+    BN_print_fp(stderr, n);
+    fprintf(stderr, "\n");
+    ret = 1;
+    goto end;
+  }
+
+  BN_gcd(g, n, m, ctx);
+  if (!BN_is_one(g)) {
+    fprintf(stdout, "%-8s: ", PRIME);
+    BN_print_fp(stdout, n);
+    fprintf(stdout, "  ");
+    BN_print_fp(stdout, m);
+    fprintf(stdout, "\n");
+    ret = 1;
+  }
+
+
+ end:
+  BN_CTX_free(ctx);
+  BN_free(g);
+
+  return ret;
+}
+
+int main(int argc, char **argv)
+{
+  FILE *fst, *snd;
+  BIGNUM *n, *m;
+
+  n = BN_new();
+  m = BN_new();
+
+  MPI_Init(0, NULL);
+
+  if (argc < 2) return EXIT_FAILURE;
+  fst = fopen(argv[argc-1], "r");
+  if (!fst) return EXIT_FAILURE;
+  snd = fopen(argv[argc-1], "r");
+  if (!snd) return EXIT_FAILURE;
+
+
+  while (next_mod(&n, fst)) {
+    fseek(snd, ftell(fst), SEEK_SET);
+    /* trash first modulus */
+    next_mod(&m, snd);
+    while (next_mod(&m, snd)) test(n, m);
+  }
+
+  BN_free(n);
+  BN_free(m);
+
+  MPI_Finalize();
+  return 0;
+
+}

+ 7 - 0
src/tests/test_stranamore.test

@@ -0,0 +1,7 @@
+#!/bin/bash
+
+../stranamore test_stranamore.txt 2>&1 | grep -q 41
+[[ $? == 0 ]] || exit 1
+
+../stranamore test_stranamore.txt  2>&1 | grep -q 97
+[[ $? == 0 ]] || exit 1

+ 9 - 0
src/tests/test_stranamore.txt

@@ -0,0 +1,9 @@
+4
+3
+5
+19
+41
+97
+11
+121
+97