+
+
+
+
+#ifdef HAVE_OPENSSL
+/* pollard rho, algorithm from Jim Gillogly, May 2000 */
+
+void
+pollard_pminus1(BIGNUM *val)
+{
+ BIGNUM *base, *num, *i, *x;
+
+ base = BN_new();
+ num = BN_new();
+ i = BN_new();
+ x = BN_new();
+
+ BN_set_word(i, 2);
+ BN_set_word(base, 2);
+
+ for (;;) {
+ BN_mod_exp(base, base, i, val, ctx);
+
+ BN_copy(x, base);
+ BN_sub_word(x, 1);
+ BN_gcd(x, x, val, ctx);
+
+ if (!BN_is_one(x)) {
+ if (BN_is_prime(x, PRIME_CHECKS, NULL, NULL,
+ NULL) == 1) {
+ putchar(' ');
+ BN_print_dec_fp(stdout, x);
+ } else
+ pollard_pminus1(x);
+ fflush(stdout);
+
+ BN_div(num, NULL, val, x, ctx);
+ if (BN_is_one(num))
+ return;
+ if (BN_is_prime(num, PRIME_CHECKS, NULL, NULL,
+ NULL) == 1) {
+ putchar(' ');
+ BN_print_dec_fp(stdout, num);
+ fflush(stdout);
+ return;
+ }
+ BN_copy(val, num);
+ }
+ BN_add_word(i, 1);
+ }
+}
+#else
+char *
+BN_bn2dec(const BIGNUM *val)
+{
+ char *buf;
+
+ buf = malloc(100);
+ if (!buf)
+ return buf;
+ snprintf(buf, 100, "%ld", (long)*val);
+ return buf;
+}
+
+BN_ULONG
+BN_div_word(BIGNUM *a, BN_ULONG b)
+{
+ BN_ULONG mod;
+
+ mod = *a % b;
+ *a /= b;
+ return mod;
+}
+#endif