|
@@ -4,42 +4,47 @@
|
|
|
* \brief Fast access to a prime pool
|
|
|
*
|
|
|
*/
|
|
|
-#include <stdint.h>
|
|
|
+#include <strings.h>
|
|
|
|
|
|
#include <openssl/bn.h>
|
|
|
|
|
|
+#include "qa/questions/primes.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.
|
|
|
+ * \return a new prime iterator if the initialization succeeded, NULL
|
|
|
+ * otherwise.
|
|
|
*/
|
|
|
-int primes_init(void)
|
|
|
+pit_t *primes_init(void)
|
|
|
{
|
|
|
- if (pool) return 2;
|
|
|
-
|
|
|
- if (!(pool = fopen(PRIME_POOL_FILE, "r")))
|
|
|
- return 0;
|
|
|
- else
|
|
|
- return 1;
|
|
|
+ return fopen(PRIME_POOL_FILE, "r");
|
|
|
}
|
|
|
|
|
|
|
|
|
+/**
|
|
|
+ * \brief Prime Pool Iterator destructor.
|
|
|
+ *
|
|
|
+ */
|
|
|
+void prime_iterator_free(pit_t *it)
|
|
|
+{
|
|
|
+ /* XXX. check for errors. */
|
|
|
+ fclose(it);
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* \brief Next prime in the pool
|
|
|
*
|
|
|
* \return true if there was another prime, false otherwise.
|
|
|
*
|
|
|
*/
|
|
|
-int primes_next(BIGNUM* p)
|
|
|
+int primes_next(pit_t *it, BIGNUM* p)
|
|
|
{
|
|
|
+ static char sp[10];
|
|
|
/* overlow on me, yeah */
|
|
|
- fscanf(pool, "%s", sp);
|
|
|
+ fscanf(it, "%s", sp);
|
|
|
BN_dec2bn(&p, sp);
|
|
|
|
|
|
return 1;
|
|
@@ -55,19 +60,23 @@ int primes_next(BIGNUM* p)
|
|
|
* thresh, false otherwise
|
|
|
*/
|
|
|
int
|
|
|
-smooth(BIGNUM *x, BN_CTX *ctx, uint8_t* v, size_t thresh)
|
|
|
+smooth(BIGNUM *x, BN_CTX *ctx, char* v, size_t thresh)
|
|
|
{
|
|
|
BIGNUM *p = BN_new();
|
|
|
BIGNUM *rem = BN_new();
|
|
|
BIGNUM *dv = BN_new();
|
|
|
+ pit_t *it;
|
|
|
size_t i;
|
|
|
|
|
|
i = 0;
|
|
|
BN_zero(rem);
|
|
|
+ bzero(v, thresh);
|
|
|
if (BN_cmp(x, BN_value_one()) < 1) return 0;
|
|
|
|
|
|
i = 0;
|
|
|
- for (primes_init(); primes_next(p) && i < thresh; i++) {
|
|
|
+ for (it = primes_init();
|
|
|
+ primes_next(it, p) && i < thresh;
|
|
|
+ i++) {
|
|
|
BN_div(dv, rem, x, p, ctx);
|
|
|
for (v[i] = 0; BN_is_zero(rem); BN_div(dv, rem, x, p, ctx)) {
|
|
|
v[i] = (v[i] + 1) % 2;
|
|
@@ -80,6 +89,7 @@ smooth(BIGNUM *x, BN_CTX *ctx, uint8_t* v, size_t thresh)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ prime_iterator_free(it);
|
|
|
BN_free(p);
|
|
|
return 0;
|
|
|
}
|