Browse Source

primes.c : an iterator abstraction over the prime database primes.txt.

Note: this is completely fucked up: the path for loading the database is not
absolute, assumes the file stands in the project root directory. Moreover, the
loading uses the shitty scanf() function with "%s" - buffer overflows on the
road.
Michele Orrù 11 years ago
parent
commit
0d11fdb697

+ 2 - 1
src/questions/Makefile.am

@@ -17,7 +17,8 @@ QUESTIONS = $(WIENER_QUESTION) $(POLLARD_QUESTION) $(DIXON_QUESTION) \
 
 
 QLIBSOURCES =         qarith.c         include/qarith.h \
 QLIBSOURCES =         qarith.c         include/qarith.h \
 	            qstrings.c         include/qstrings.h \
 	            qstrings.c         include/qstrings.h \
-                allquestions.c         include/questions.h
+                allquestions.c         include/questions.h \
+	              primes.c         include/primes.h
 
 
 lib_LIBRARIES = libquestions.a
 lib_LIBRARIES = libquestions.a
 libquestions_a_SOURCES = $(QUESTIONS) $(QLIBSOURCES)
 libquestions_a_SOURCES = $(QUESTIONS) $(QLIBSOURCES)

+ 45 - 0
src/questions/primes.c

@@ -0,0 +1,45 @@
+/**
+ * \file primes.c
+ *
+ * \brief Fast access to a prime pool
+ *
+ */
+
+#include <openssl/bn.h>
+
+static const char *PRIME_POOL_FILE = "primes.txt";
+FILE *pool = NULL;
+char sp[10];
+
+/**
+ * \brief Prime Poll initialization.
+ *
+ * \return 1 if the initialization suceeded
+ *         0 if the initialization failed
+ *         2 if the initialization had been already performed.
+ */
+int primes_init(void)
+{
+  if (pool) return 2;
+
+  if (!(pool = fopen(PRIME_POOL_FILE, "r")))
+    return 0;
+  else
+    return 1;
+}
+
+
+/**
+ * \brief Next prime in the pool
+ *
+ * \return true if there was another prime, false otherwise.
+ *
+ */
+int primes_next(BIGNUM* p)
+{
+  /* overlow on me, yeah */
+  fscanf(pool, "%s", sp);
+  BN_dec2bn(&p, sp);
+
+  return 1;
+}

+ 5 - 1
src/questions/tests/Makefile.am

@@ -2,9 +2,13 @@
 # unittesting my ass
 # unittesting my ass
 LDADD=../libquestions.a -lssl -lcrypto
 LDADD=../libquestions.a -lssl -lcrypto
 
 
-check_PROGRAMS = test_qarith test_qstrings test_wiener test_pollard test_dixon
+check_PROGRAMS = test_qarith test_qstrings test_wiener test_pollard test_dixon \
+	         test_williams test_primes
 TESTS = $(check_PROGRAMS) test_fermat.test test_metadata.test test_pollardrho.test
 TESTS = $(check_PROGRAMS) test_fermat.test test_metadata.test test_pollardrho.test
 
 
+
+test_primes_sources = test_primes.c
+test_williams_SOURCES = test_williams+1.c
 test_qstrings_SOURCES = test_qstrings.c
 test_qstrings_SOURCES = test_qstrings.c
 test_qarith_SOURCES = test_qarith.c
 test_qarith_SOURCES = test_qarith.c
 test_wiener_SOURCES = test_wiener.c
 test_wiener_SOURCES = test_wiener.c

+ 34 - 0
src/questions/tests/test_primes.c

@@ -0,0 +1,34 @@
+#include <assert.h>
+
+#include <openssl/bn.h>
+
+#include "qa/questions/primes.h"
+
+void test_primes(void)
+{
+  BIGNUM *p = BN_new();
+  BIGNUM *check = BN_new();
+
+  assert(primes_init());
+
+  BN_dec2bn(&check, "2");
+  primes_next(p);
+  assert(!BN_cmp(check, p));
+
+  BN_dec2bn(&check, "3");
+  primes_next(p);
+  assert(!BN_cmp(check, p));
+
+  BN_dec2bn(&check, "5");
+  primes_next(p);
+  assert(!BN_cmp(check, p));
+
+  BN_free(p);
+  BN_free(check);
+}
+
+int main(int argc, char **argv)
+{
+  test_primes();
+  return 0;
+}

+ 1 - 1
src/questions/tests/test_wiener.c

@@ -31,7 +31,7 @@ void test_wiener(void)
   assert(WienerQuestion.ask_rsa(rsa));
   assert(WienerQuestion.ask_rsa(rsa));
 }
 }
 
 
-int main(int argc, char ** argv)
+int main(int argc, char **argv)
 {
 {
   if (WienerQuestion.setup) WienerQuestion.setup();
   if (WienerQuestion.setup) WienerQuestion.setup();
 
 

+ 1 - 1
src/questions/williams+1.c

@@ -45,7 +45,7 @@ void lucas(BIGNUM *v, BIGNUM *w,
       BN_sub(w, w, BN_value_one());
       BN_sub(w, w, BN_value_one());
     } else {
     } else {
       BN_sqr(vv, v, ctx);
       BN_sqr(vv, v, ctx);
-      /* v = v² - 2*/
+      /* v = v² - 2 */
       BN_sub(u, vv, BN_value_one());
       BN_sub(u, vv, BN_value_one());
       BN_sub(u, u, BN_value_one());
       BN_sub(u, u, BN_value_one());
       /* w = vw - τ */
       /* w = vw - τ */