Browse Source

Refactored SG prime code

thajohns 11 months ago
parent
commit
b5d65967e9
2 changed files with 165 additions and 111 deletions
  1. 153
    111
      sgprime.c
  2. 12
    0
      sgprime.h

sieve.c → sgprime.c View File

@@ -1,4 +1,6 @@
1 1
 
2
+#define PRIMORIAL_CAP_CAP 10000000
3
+
2 4
 #include <stdlib.h>
3 5
 #include <stdio.h>
4 6
 #include <pthread.h>
@@ -6,8 +8,6 @@
6 8
 
7 9
 #include <gmp.h>
8 10
 
9
-#include "sieve.h"
10
-
11 11
 struct mpz_list
12 12
 {
13 13
   unsigned long int numel;
@@ -133,33 +133,33 @@ unsigned long int build_sieve(unsigned long int max_primorial, struct mpz_list *
133 133
   return primorial;
134 134
 }
135 135
 
136
-unsigned long int build_totant_list(unsigned long int n, struct mpz_list **list_out)
137
-{
138
-  struct mpz_list *list = malloc(sizeof(struct mpz_list));
139
-  mpz_list_init(list, 16);
140
-
141
-  mpz_t c, g;
142
-  mpz_init(c);
143
-  mpz_init(g);
144
-  mpz_set_ui(c, 1);
145
-
146
-  while (mpz_cmp_ui(c, n) != 0)
147
-  {
148
-    mpz_gcd_ui(g, c, n);
149
-    if (mpz_cmp_ui(g, 1) == 0)
150
-    {
151
-      mpz_set(*mpz_list_push(list), c);
152
-    }
153
-
154
-    mpz_add_ui(c, c, 1);
155
-  }
156
-
157
-  mpz_clear(c);
158
-  mpz_clear(g);
159
-
160
-  *list_out = list;
161
-  return list->numel;
162
-}
136
+//unsigned long int build_totant_list(unsigned long int n, struct mpz_list **list_out)
137
+//{
138
+//  struct mpz_list *list = malloc(sizeof(struct mpz_list));
139
+//  mpz_list_init(list, 16);
140
+//
141
+//  mpz_t c, g;
142
+//  mpz_init(c);
143
+//  mpz_init(g);
144
+//  mpz_set_ui(c, 1);
145
+//
146
+//  while (mpz_cmp_ui(c, n) != 0)
147
+//  {
148
+//    mpz_gcd_ui(g, c, n);
149
+//    if (mpz_cmp_ui(g, 1) == 0)
150
+//    {
151
+//      mpz_set(*mpz_list_push(list), c);
152
+//    }
153
+//
154
+//    mpz_add_ui(c, c, 1);
155
+//  }
156
+//
157
+//  mpz_clear(c);
158
+//  mpz_clear(g);
159
+//
160
+//  *list_out = list;
161
+//  return list->numel;
162
+//}
163 163
 
164 164
 unsigned long int build_sgprime_candidate_list(unsigned long int n, struct mpz_list **list_out)
165 165
 {
@@ -199,44 +199,44 @@ unsigned long int build_sgprime_candidate_list(unsigned long int n, struct mpz_l
199 199
   return list->numel;
200 200
 }
201 201
 
202
-unsigned long int build_sgnprime_candidate_list(int niter, unsigned long int n, struct mpz_list **list_out)
203
-{
204
-  struct mpz_list *list = malloc(sizeof(struct mpz_list));
205
-  mpz_list_init(list, 16);
206
-
207
-  mpz_t c, g, u;
208
-  mpz_init(c);
209
-  mpz_init(g);
210
-  mpz_init(u);
211
-  mpz_set_ui(c, 1);
212
-
213
-  while (mpz_cmp_ui(c, n) != 0)
214
-  {
215
-    mpz_set(u, c);
216
-    mpz_gcd_ui(g, u, n);
217
-    int ii;
218
-    for (ii = 0; ii < niter ; ii++)
219
-    {
220
-      if (mpz_cmp_ui(g, 1) != 0)
221
-        goto cont;
222
-      mpz_add(u, u, u);
223
-      mpz_add_ui(u, u, 1);
224
-      if (mpz_cmp_ui(u, n) > 0)
225
-        mpz_sub_ui(u, u, n);
226
-      mpz_gcd_ui(g, u, n);
227
-    }
228
-    mpz_set(*mpz_list_push(list), c);
229
-cont:
230
-    mpz_add_ui(c, c, 1);
231
-  }
232
-
233
-  mpz_clear(c);
234
-  mpz_clear(g);
235
-  mpz_clear(u);
236
-
237
-  *list_out = list;
238
-  return list->numel;
239
-}
202
+//unsigned long int build_sgnprime_candidate_list(int niter, unsigned long int n, struct mpz_list **list_out)
203
+//{
204
+//  struct mpz_list *list = malloc(sizeof(struct mpz_list));
205
+//  mpz_list_init(list, 16);
206
+//
207
+//  mpz_t c, g, u;
208
+//  mpz_init(c);
209
+//  mpz_init(g);
210
+//  mpz_init(u);
211
+//  mpz_set_ui(c, 1);
212
+//
213
+//  while (mpz_cmp_ui(c, n) != 0)
214
+//  {
215
+//    mpz_set(u, c);
216
+//    mpz_gcd_ui(g, u, n);
217
+//    int ii;
218
+//    for (ii = 0; ii < niter ; ii++)
219
+//    {
220
+//      if (mpz_cmp_ui(g, 1) != 0)
221
+//        goto cont;
222
+//      mpz_add(u, u, u);
223
+//      mpz_add_ui(u, u, 1);
224
+//      if (mpz_cmp_ui(u, n) > 0)
225
+//        mpz_sub_ui(u, u, n);
226
+//      mpz_gcd_ui(g, u, n);
227
+//    }
228
+//    mpz_set(*mpz_list_push(list), c);
229
+//cont:
230
+//    mpz_add_ui(c, c, 1);
231
+//  }
232
+//
233
+//  mpz_clear(c);
234
+//  mpz_clear(g);
235
+//  mpz_clear(u);
236
+//
237
+//  *list_out = list;
238
+//  return list->numel;
239
+//}
240 240
 
241 241
 // This function makes the liberal assumption that each element has another one within the range of an unsigned long ahead of it.
242 242
 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
464 464
   free(success_mutex);
465 465
 }
466 466
 
467
-int main()
467
+struct ui_list *make_difflist(mpz_t lower_bound, unsigned long int *offset, unsigned long int *primorial)
468 468
 {
469 469
   struct mpz_list *list;
470
-
471
-  unsigned long int primorial = build_sieve(100000000, &list);
472
-
473
-//  int ii;
474
-//  for (ii = 0; ii < list->numel; ii++)
475
-//  {
476
-//    printf("%lu\n", mpz_get_ui(list->data[ii]));
477
-//  }
478
- // printf("%lu\n", primorial);
479
-
470
+  unsigned long int primorial_cap;
471
+  if (mpz_cmp_ui(lower_bound, PRIMORIAL_CAP_CAP) >= 0)
472
+  {
473
+    primorial_cap = PRIMORIAL_CAP_CAP;
474
+  } else
475
+  {
476
+    primorial_cap = mpz_get_ui(lower_bound);
477
+  }
478
+  *primorial = build_sieve(primorial_cap, &list);
480 479
   destroy_mpz_list(list);
481 480
   free(list);
482 481
 
483
-  unsigned long int totient = build_sgnprime_candidate_list(2, primorial, &list);
484
-
485
-  printf("finished building candidate list\n");
486
-  fflush(stdout);
487
-//  printf("totient: %lu\n", totient);
488
-//  printf("ratio: %lu\n", primorial / totient);
489
-
482
+  unsigned long int totient = build_sgprime_candidate_list(*primorial, &list);
490 483
   struct ui_list *difflist;
484
+  *offset = differentiate_list(*primorial, list, &difflist);
491 485
 
492
-  unsigned long int offset = differentiate_list(primorial, list, &difflist);
493
-  
494
-//  printf("offset: %lu\n", offset);
495
-
496
-  mpz_t p;
497
-  mpz_init(p);
498
-
499
-  gmp_randstate_t rs;
500
-  gmp_randinit_default(rs);
486
+  destroy_mpz_list(list);
487
+  free(list);
501 488
 
502
-  int bits = 8192;
489
+  return difflist;
490
+}
503 491
 
504
-  int ii;
505
-  for (ii = 0; ii < 1000; ii++)
492
+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)
493
+{
494
+  if (threads <= 1)
506 495
   {
507
-    mpz_urandomb(p, rs, bits);
508
-    mpz_setbit(p, bits);
509
-
510
-    mt_sgprime_search(p, 31, offset, difflist, primorial, 8, 10);
511
-
512
-    gmp_printf("%i-bit safe prime: %Zd\n", bits, p);
496
+    st_sgprime_search(search, reps, offset, difflist, primorial);
497
+  } else
498
+  {
499
+    mt_sgprime_search(search, reps, offset, difflist, primorial, threads, chunksize);
513 500
   }
501
+}
514 502
 
515
-  gmp_randclear(rs);
516
-
517
-  mpz_clear(p);
518
-
503
+void destroy_difflist(struct ui_list *difflist)
504
+{
519 505
   destroy_ui_list(difflist);
520 506
   free(difflist);
521
-  destroy_mpz_list(list);
522
-  free(list);
523
-
524
-  return 0;
525 507
 }
526 508
 
509
+//int main()
510
+//{
511
+//  struct mpz_list *list;
512
+//
513
+//  unsigned long int primorial = build_sieve(100000000, &list);
514
+//
515
+////  int ii;
516
+////  for (ii = 0; ii < list->numel; ii++)
517
+////  {
518
+////    printf("%lu\n", mpz_get_ui(list->data[ii]));
519
+////  }
520
+// // printf("%lu\n", primorial);
521
+//
522
+//  destroy_mpz_list(list);
523
+//  free(list);
524
+//
525
+//  unsigned long int totient = build_sgprime_candidate_list(primorial, &list);
526
+//
527
+////  printf("finished building candidate list\n");
528
+////  fflush(stdout);
529
+////  printf("totient: %lu\n", totient);
530
+////  printf("ratio: %lu\n", primorial / totient);
531
+//
532
+//  struct ui_list *difflist;
533
+//
534
+//  unsigned long int offset = differentiate_list(primorial, list, &difflist);
535
+//  
536
+////  printf("offset: %lu\n", offset);
537
+//
538
+//  mpz_t p;
539
+//  mpz_init(p);
540
+//
541
+//  gmp_randstate_t rs;
542
+//  gmp_randinit_default(rs);
543
+//
544
+//  int bits = 32;
545
+//
546
+//  int ii;
547
+//  for (ii = 0; ii < 1000; ii++)
548
+//  {
549
+//    mpz_urandomb(p, rs, bits);
550
+//    mpz_setbit(p, bits);
551
+//
552
+//    mt_sgprime_search(p, 31, offset, difflist, primorial, 8, 10);
553
+//
554
+//    gmp_printf("%i-bit safe prime: %Zd\n", bits, p);
555
+//  }
556
+//
557
+//  gmp_randclear(rs);
558
+//
559
+//  mpz_clear(p);
560
+//
561
+//  destroy_ui_list(difflist);
562
+//  free(difflist);
563
+//  destroy_mpz_list(list);
564
+//  free(list);
565
+//
566
+//  return 0;
567
+//}
568
+

+ 12
- 0
sgprime.h View File

@@ -0,0 +1,12 @@
1
+#ifndef SIEVE_H
2
+#define SIEVE_H
3
+
4
+#include <gmp.h>
5
+
6
+void *make_difflist(mpz_t lower_bound, unsigned long int *offset);
7
+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)
8
+void destroy_difflist(void *difflist);
9
+
10
+
11
+#endif
12
+

Loading…
Cancel
Save