Browse Source

Cleanup.

* Fix memory-leak in Fermat's factorization;
* Fix grammar error in wiener's page.
Michele Orrù 10 years ago
parent
commit
6037e6c5ef
3 changed files with 19 additions and 25 deletions
  1. 1 1
      book/wiener.tex
  2. 16 22
      src/questions/fermat.c
  3. 2 2
      src/questions/include/questions.h

+ 1 - 1
book/wiener.tex

@@ -174,7 +174,7 @@ A Continued fraction structure may look like this:
 \end{minted}
 where \texttt{bigfraction\_t} is just a pair of \texttt{BIGNUM} \!s
 $\angular{h_i, k_i}$. Whenever we need to produce a new convergent, we increment
-$i \pmod{3}$ and apply the definitions given. The fresh convergent must be
+$i \pmod{3}$ and apply the given definitions. The fresh convergent must be
 tested with very simple algebraic operations. It is worth noting here that
 \ref{eq:wiener:pq} can be solved using the reduced discriminant formula, as
 $p, q$ are odd primes:

+ 16 - 22
src/questions/fermat.c

@@ -22,22 +22,19 @@
 static RSA *
 fermat_question_ask(const RSA *rsa)
 {
-  BN_CTX *ctx;
-  BIGNUM *a, *b, *a2, *b2;
-  BIGNUM *n;
-  BIGNUM *tmp, *rem, *dssdelta;
+  BIGNUM
+    *a = BN_new(),
+    *b = BN_new(),
+    *a2 = BN_new(),
+    *b2 = BN_new();
+  BIGNUM *n = rsa->n;
+  BIGNUM
+    *tmp = BN_new(),
+    *rem = BN_new(),
+    *dssdelta = BN_new();
+  BN_CTX *ctx = BN_CTX_new();
   RSA *ret = NULL;
 
-  ctx = BN_CTX_new();
-  n = rsa->n;
-  a = BN_new();
-  b = BN_new();
-  a2 = BN_new();
-  b2 = BN_new();
-  rem = BN_new();
-  tmp = BN_new();
-  dssdelta = BN_new();
-
   BN_sqrtmod(tmp, rem, n, ctx);
   /* Δ = |p - q| = |a + b - a + b| = |2b| > √N  2⁻¹⁰⁰ */
   BN_rshift(dssdelta, tmp, 101);
@@ -46,7 +43,7 @@ fermat_question_ask(const RSA *rsa)
 
   do {
     /* a² += 2a + 1 */
-    BN_lshift(tmp, a, 1);
+    BN_lshift1(tmp, a);
     BN_uiadd1(tmp);
     BN_uadd(a2, a2, tmp);
     /* a += 1 */
@@ -58,15 +55,10 @@ fermat_question_ask(const RSA *rsa)
   } while (!BN_is_zero(rem) && BN_ucmp(b, dssdelta) < 1);
 
   if (BN_is_zero(rem)) {
-    /* p, q found :) */
-    ret = RSA_new();
-    ret->q = BN_new();
-    ret->p = BN_new();
-
     BN_sqrtmod(a, rem, a2, ctx);
     assert(BN_is_zero(rem));
-    BN_uadd(ret->p, a, b);
-    BN_usub(ret->q, a, b);
+    BN_uadd(a, a, b);
+    ret = qa_RSA_recover(rsa, a, ctx);
   }
 
   BN_CTX_free(ctx);
@@ -75,6 +67,8 @@ fermat_question_ask(const RSA *rsa)
   BN_free(a2);
   BN_free(b2);
   BN_free(dssdelta);
+  BN_free(tmp);
+  BN_free(rem);
   return ret;
 }
 

+ 2 - 2
src/questions/include/questions.h

@@ -42,10 +42,10 @@ void select_question(const char *);
 void select_all_questions(void);
 
 #define REGISTER_QUESTION(q)                      \
-  {                                               \
+  do {                                            \
       extern struct qa_question q;                \
       LIST_INSERT_HEAD(&questions, &q, qs);       \
-  }
+  } while (0);
 
 
 #endif /* _QA_QUESTIONS_H_ */