Browse Source

Refactored SG prime code

master
thajohns 2 years ago
parent
commit
b5d65967e9
  1. 264
      sgprime.c
  2. 12
      sgprime.h

264
sieve.c → sgprime.c

@ -1,4 +1,6 @@
#define PRIMORIAL_CAP_CAP 10000000
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
@ -6,8 +8,6 @@
#include <gmp.h>
#include "sieve.h"
struct mpz_list
{
unsigned long int numel;
@ -133,33 +133,33 @@ unsigned long int build_sieve(unsigned long int max_primorial, struct mpz_list *
return primorial;
}
unsigned long int build_totant_list(unsigned long int n, struct mpz_list **list_out)
{
struct mpz_list *list = malloc(sizeof(struct mpz_list));
mpz_list_init(list, 16);
mpz_t c, g;
mpz_init(c);
mpz_init(g);
mpz_set_ui(c, 1);
while (mpz_cmp_ui(c, n) != 0)
{
mpz_gcd_ui(g, c, n);
if (mpz_cmp_ui(g, 1) == 0)
{
mpz_set(*mpz_list_push(list), c);
}
mpz_add_ui(c, c, 1);
}
mpz_clear(c);
mpz_clear(g);
*list_out = list;
return list->numel;
}
//unsigned long int build_totant_list(unsigned long int n, struct mpz_list **list_out)
//{
// struct mpz_list *list = malloc(sizeof(struct mpz_list));
// mpz_list_init(list, 16);
//
// mpz_t c, g;
// mpz_init(c);
// mpz_init(g);
// mpz_set_ui(c, 1);
//
// while (mpz_cmp_ui(c, n) != 0)
// {
// mpz_gcd_ui(g, c, n);
// if (mpz_cmp_ui(g, 1) == 0)
// {
// mpz_set(*mpz_list_push(list), c);
// }
//
// mpz_add_ui(c, c, 1);
// }
//
// mpz_clear(c);
// mpz_clear(g);
//
// *list_out = list;
// return list->numel;
//}
unsigned long int build_sgprime_candidate_list(unsigned long int n, struct mpz_list **list_out)
{
@ -199,44 +199,44 @@ unsigned long int build_sgprime_candidate_list(unsigned long int n, struct mpz_l
return list->numel;
}
unsigned long int build_sgnprime_candidate_list(int niter, unsigned long int n, struct mpz_list **list_out)
{
struct mpz_list *list = malloc(sizeof(struct mpz_list));
mpz_list_init(list, 16);
mpz_t c, g, u;
mpz_init(c);
mpz_init(g);
mpz_init(u);
mpz_set_ui(c, 1);
while (mpz_cmp_ui(c, n) != 0)
{
mpz_set(u, c);
mpz_gcd_ui(g, u, n);
int ii;
for (ii = 0; ii < niter ; ii++)
{
if (mpz_cmp_ui(g, 1) != 0)
goto cont;
mpz_add(u, u, u);
mpz_add_ui(u, u, 1);
if (mpz_cmp_ui(u, n) > 0)
mpz_sub_ui(u, u, n);
mpz_gcd_ui(g, u, n);
}
mpz_set(*mpz_list_push(list), c);
cont:
mpz_add_ui(c, c, 1);
}
mpz_clear(c);
mpz_clear(g);
mpz_clear(u);
*list_out = list;
return list->numel;
}
//unsigned long int build_sgnprime_candidate_list(int niter, unsigned long int n, struct mpz_list **list_out)
//{
// struct mpz_list *list = malloc(sizeof(struct mpz_list));
// mpz_list_init(list, 16);
//
// mpz_t c, g, u;
// mpz_init(c);
// mpz_init(g);
// mpz_init(u);
// mpz_set_ui(c, 1);
//
// while (mpz_cmp_ui(c, n) != 0)
// {
// mpz_set(u, c);
// mpz_gcd_ui(g, u, n);
// int ii;
// for (ii = 0; ii < niter ; ii++)
// {
// if (mpz_cmp_ui(g, 1) != 0)
// goto cont;
// mpz_add(u, u, u);
// mpz_add_ui(u, u, 1);
// if (mpz_cmp_ui(u, n) > 0)
// mpz_sub_ui(u, u, n);
// mpz_gcd_ui(g, u, n);
// }
// mpz_set(*mpz_list_push(list), c);
//cont:
// mpz_add_ui(c, c, 1);
// }
//
// mpz_clear(c);
// mpz_clear(g);
// mpz_clear(u);
//
// *list_out = list;
// return list->numel;
//}
// This function makes the liberal assumption that each element has another one within the range of an unsigned long ahead of it.
unsigned long int differentiate_list(unsigned long int max, struct mpz_list *list, struct ui_list **out_list)
@ -464,63 +464,105 @@ void mt_sgprime_search(mpz_t r, int reps, unsigned long int offset, struct ui_li
free(success_mutex);
}
int main()
struct ui_list *make_difflist(mpz_t lower_bound, unsigned long int *offset, unsigned long int *primorial)
{
struct mpz_list *list;
unsigned long int primorial = build_sieve(100000000, &list);
// int ii;
// for (ii = 0; ii < list->numel; ii++)
// {
// printf("%lu\n", mpz_get_ui(list->data[ii]));
// }
// printf("%lu\n", primorial);
unsigned long int primorial_cap;
if (mpz_cmp_ui(lower_bound, PRIMORIAL_CAP_CAP) >= 0)
{
primorial_cap = PRIMORIAL_CAP_CAP;
} else
{
primorial_cap = mpz_get_ui(lower_bound);
}
*primorial = build_sieve(primorial_cap, &list);
destroy_mpz_list(list);
free(list);
unsigned long int totient = build_sgnprime_candidate_list(2, primorial, &list);
printf("finished building candidate list\n");
fflush(stdout);
// printf("totient: %lu\n", totient);
// printf("ratio: %lu\n", primorial / totient);
unsigned long int totient = build_sgprime_candidate_list(*primorial, &list);
struct ui_list *difflist;
*offset = differentiate_list(*primorial, list, &difflist);
unsigned long int offset = differentiate_list(primorial, list, &difflist);
// printf("offset: %lu\n", offset);
mpz_t p;
mpz_init(p);
gmp_randstate_t rs;
gmp_randinit_default(rs);
destroy_mpz_list(list);
free(list);
int bits = 8192;
return difflist;
}
int ii;
for (ii = 0; ii < 1000; ii++)
void get_sg_prime(struct ui_list *difflist, unsigned long int offset, unsigned long int primorial, mpz_t search, unsigned int threads, unsigned int reps, unsigned int chunksize)
{
if (threads <= 1)
{
mpz_urandomb(p, rs, bits);
mpz_setbit(p, bits);
mt_sgprime_search(p, 31, offset, difflist, primorial, 8, 10);
gmp_printf("%i-bit safe prime: %Zd\n", bits, p);
st_sgprime_search(search, reps, offset, difflist, primorial);
} else
{
mt_sgprime_search(search, reps, offset, difflist, primorial, threads, chunksize);
}
}
gmp_randclear(rs);
mpz_clear(p);
void destroy_difflist(struct ui_list *difflist)
{
destroy_ui_list(difflist);
free(difflist);
destroy_mpz_list(list);
free(list);
return 0;
}
//int main()
//{
// struct mpz_list *list;
//
// unsigned long int primorial = build_sieve(100000000, &list);
//
//// int ii;
//// for (ii = 0; ii < list->numel; ii++)
//// {
//// printf("%lu\n", mpz_get_ui(list->data[ii]));
//// }
// // printf("%lu\n", primorial);
//
// destroy_mpz_list(list);
// free(list);
//
// unsigned long int totient = build_sgprime_candidate_list(primorial, &list);
//
//// printf("finished building candidate list\n");
//// fflush(stdout);
//// printf("totient: %lu\n", totient);
//// printf("ratio: %lu\n", primorial / totient);
//
// struct ui_list *difflist;
//
// unsigned long int offset = differentiate_list(primorial, list, &difflist);
//
//// printf("offset: %lu\n", offset);
//
// mpz_t p;
// mpz_init(p);
//
// gmp_randstate_t rs;
// gmp_randinit_default(rs);
//
// int bits = 32;
//
// int ii;
// for (ii = 0; ii < 1000; ii++)
// {
// mpz_urandomb(p, rs, bits);
// mpz_setbit(p, bits);
//
// mt_sgprime_search(p, 31, offset, difflist, primorial, 8, 10);
//
// gmp_printf("%i-bit safe prime: %Zd\n", bits, p);
// }
//
// gmp_randclear(rs);
//
// mpz_clear(p);
//
// destroy_ui_list(difflist);
// free(difflist);
// destroy_mpz_list(list);
// free(list);
//
// return 0;
//}

12
sgprime.h

@ -0,0 +1,12 @@
#ifndef SIEVE_H
#define SIEVE_H
#include <gmp.h>
void *make_difflist(mpz_t lower_bound, unsigned long int *offset);
mpz_t get_sg_prime(void *difflist, unsigned long int offset, unsigned long int primorial, mpz_t start_search, unsigned int threads, unsigned int reps, unsigned int chunksize)
void destroy_difflist(void *difflist);
#endif
Loading…
Cancel
Save