1 #include "miller_rabin.h"
3 static inline cmph_uint64 int_pow(cmph_uint64 a, cmph_uint64 d, cmph_uint64 n)
10 res =(((cmph_uint64)res) * a_pow) % n;
11 a_pow = (((cmph_uint64)a_pow) * a_pow) % n;
17 static inline cmph_uint8 check_witness(cmph_uint64 a_exp_d, cmph_uint64 n, cmph_uint64 s)
20 cmph_uint64 a_exp = a_exp_d;
21 if(a_exp == 1 || a_exp == (n - 1))
23 for(i = 1; i < s; i++)
25 a_exp = (((cmph_uint64)a_exp) * a_exp) % n;
32 cmph_uint8 check_primality(cmph_uint64 n)
34 cmph_uint64 a, d, s, a_exp_d;
43 //we decompoe the number n - 1 into 2^s*d
53 a_exp_d = int_pow(a, d, n);
54 if(check_witness(a_exp_d, n, s) == 0)
57 a_exp_d = int_pow(a, d, n);
58 if(check_witness(a_exp_d, n, s) == 0)
61 a_exp_d = int_pow(a, d, n);
62 if(check_witness(a_exp_d, n, s) == 0)