1 // Bignum prime test (returns 1 if prime, 0 if not)
3 // Uses Algorithm P (probabilistic primality test) from p. 395 of
4 // "The Art of Computer Programming, Volume 2" by Donald E. Knuth.
9 static int mprimef(unsigned int *, unsigned int *, int);
12 mprime(unsigned int *n
)
19 if (MLENGTH(n
) == 1 && n
[0] == 1)
24 if (MLENGTH(n
) == 1 && n
[0] == 2)
40 } while ((q
[0] & 1) == 0);
44 for (i
= 0; i
< 25; i
++)
45 if (mprimef(n
, q
, k
) == 0)
56 //-----------------------------------------------------------------------------
58 // This is the actual implementation of Algorithm P.
60 // Input: n The number in question.
62 // q n = 1 + (2 ^ k) q
66 // Output: 1 when n is probably prime
68 // 0 when n is definitely not prime
70 //-----------------------------------------------------------------------------
73 mprimef(unsigned int *n
, unsigned int *q
, int k
)
76 unsigned int *t
, *x
, *y
;
83 for (i
= 0; i
< MLENGTH(t
); i
++)
86 if (!MZERO(x
) && !MEQUAL(x
, 1))