/* test_mpz.c basic functions */ /* compile: gcc -o test_mpz -I. test_mpz.c -L. -lgmp */ /* assumes gmp.h and libgmp.a in local directory */ #include #include "gmp.h" int miller_rabin(mpz_t n, int s); /* 0 may be prime, 1 is composite */ int witness(mpz_t a, mpz_t n); /* 0 for false, 1 for true */ int main() { mpz_t a, b, c, d; char my_str[1000]; mpz_init(a); /* must init every variable */ mpz_init(b); mpz_init(c); mpz_init(d); mpz_set_str(a, "100000000000000000000", 10); /* radix 10 */ mpz_set_str(b,"1", 10); mpz_sub(c, a, b); mpz_get_str(my_str, 10, c); printf("my_str=%s \n", my_str); mpz_mul(b, c, c); mpz_get_str(my_str, 10, b); printf("b=c*c=%s \n", my_str); mpz_add(a, b, b); mpz_div(d, a, c); mpz_get_str(my_str, 10, d); printf("d=(a=b+b)/c=%s \n", my_str); mpz_gcd(d, a, c); mpz_get_str(my_str, 10, d); printf("d=gcd(a,c)=%s \n", my_str); mpz_get_str(my_str, 2, d); printf("d_binary=%s \n", my_str); return 0; } int miller_rabin(mpz_t n, int s) /* 0 may be prime, 1 is composite */ { int i; mpz_t nm1; mpz_t one; mpz_t a; mpz_init(nm1); mpz_init(one); mpz_init(a); mpz_set_str(one, "1", 10); mpz_sub(nm1, n, one); for(i=0; i