-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSophieGermain.c
54 lines (38 loc) · 1.11 KB
/
SophieGermain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h> // pour calculer avec de grands nombres
#include "config.h"
void SophieGermain_prime(mpz_t SGprime) {
gmp_randstate_t gmp_state;
int checks;
// Check si le nombre de bits est correct
if (Bits / 16 > 8) {
checks = Bits / 16;
} else {
checks = 8;
}
// Init du generateur aleatoire
gmp_randinit_default(gmp_state);
gmp_randseed_ui(gmp_state, time(NULL));
while (1) {
// Genere un nombre premier aleatoire SGprime de taille 'Bits - 1'
mpz_urandomb(SGprime, gmp_state, Bits - 1);
mpz_nextprime(SGprime, SGprime);
// SGprime = 2 * SGprime + 1
mpz_mul_ui(SGprime, SGprime, 2);
mpz_add_ui(SGprime, SGprime, 1);
// Test de primalité sur SGprime
if (mpz_probab_prime_p(SGprime, checks) > 0) {
break;
}
}
gmp_randclear(gmp_state);
}
int main() {
mpz_t SGprime;
mpz_init(SGprime);
SophieGermain_prime(SGprime);
gmp_printf("Sophie Germain prime number: %Zd\n", SGprime);
mpz_clear(SGprime);
return 0;
}