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: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75:
| #include <stdio.h> #include <stdlib.h> #include <time.h> #include <gmp.h>
int main(int argc, char **argv) { gmp_randstate_t randomstate; mpz_t N, d, e, K, g, t, x, x1, y, p;
gmp_randinit_default (randomstate); gmp_randseed_ui(randomstate, time(0));
mpz_init_set_str(N, "25777", 10); mpz_init_set_ui(e, 3); mpz_init_set_ui(d, 16971);
gmp_printf("N=%Zd, e=%Zd, d=%Zd\n", N, e, d);
schritt1: mpz_inits(K, g, t, x, x1, y, p, NULL);
mpz_mul(K, d, e); mpz_sub_ui(K, K, 1);
gmp_printf("K=d*e-1=%Zd\n", K);
schritt2:
do { mpz_urandomm(g, randomstate, N); } while (mpz_cmp_ui(g, 2) < 0);
gmp_printf("Trying g = %Zd\n", g);
mpz_set(t, K);
schritt3: do { if (mpz_divisible_ui_p(t, 2)) { mpz_divexact_ui(t, t, 2); gmp_printf("t= %Zd\n", t); mpz_powm_sec(x, g, t, N); gmp_printf("x= g^t mod N=%Zd\n", x); } else goto schritt2;
if (mpz_cmp_ui(x, 1) > 0) { mpz_sub_ui(x1, x, 1); mpz_gcd (y, x1, N); gmp_printf("y= gcd(x-1,N)=%Zd\n", y); if (mpz_cmp_ui(y, 1) > 0) goto schritt4; } } while (1); schritt4: mpz_cdiv_q(p, N, y); gmp_printf("p= %Zd\n", p); gmp_printf("q= N/p = y =%Zd\n", y);
return 0; } |