X-Git-Url: https://ginac.de/CLN/cln.git//cln.git?a=blobdiff_plain;f=src%2Fnumtheory%2Fcl_nt_isprobprime.cc;h=0f248c356f50aa4a3d09fe6738229098aca84f07;hb=22549ef70fee95faab1e9f2adaf710ba9e0bdabf;hp=3b79d2f7343c1afb295bfc25c786a35487e12cc1;hpb=850abfde7f0d985ba01526c346bcd0d733562943;p=cln.git diff --git a/src/numtheory/cl_nt_isprobprime.cc b/src/numtheory/cl_nt_isprobprime.cc index 3b79d2f..0f248c3 100644 --- a/src/numtheory/cl_nt_isprobprime.cc +++ b/src/numtheory/cl_nt_isprobprime.cc @@ -10,20 +10,26 @@ // Implementation. #include "cl_IF.h" -#include "cln/abort.h" +#include "cln/integer_io.h" +#include "cln/exception.h" +#include namespace cln { -cl_boolean isprobprime (const cl_I& n) +bool isprobprime (const cl_I& n) { - if (!(n > 0)) - cl_abort(); + if (!(n > 0)) { + std::ostringstream buf; + fprint(buf, n); + fprint(buf, " is not a positive integer."); + throw runtime_exception(buf.str()); + } // With a Miller-Rabin count = 50 the final error probability is // 4^-50 < 10^-30. var int count = 50; // Step 1: Trial division (rules out 87% of all numbers quickly). const uint32 trialdivide_limit = 70; - var uintL l = integer_length(n); + var uintC l = integer_length(n); if (l <= 32) { var uint32 nn = cl_I_to_UL(n); if (nn <= cl_small_prime_table_limit) { @@ -32,12 +38,12 @@ cl_boolean isprobprime (const cl_I& n) if (i < cl_small_prime_table_size && ((unsigned int) cl_small_prime_table[i] == nn || nn == 2)) - return cl_true; + return true; else - return cl_false; + return false; } if ((nn % 2) == 0 || cl_trialdivision(nn,1,trialdivide_limit)) - return cl_false; + return false; // For small n, only few Miller-Rabin tests are needed. if (nn < 2000U) count = 1; // {2} else if (nn < 1300000U) count = 2; // {2,3} @@ -47,10 +53,10 @@ cl_boolean isprobprime (const cl_I& n) var uint32 nhi = cl_I_to_UL(ldb(n,cl_byte(32,32))); var uint32 nlo = cl_I_to_UL(ldb(n,cl_byte(32,0))); if ((nlo % 2) == 0 || cl_trialdivision(nhi,nlo,1,trialdivide_limit)) - return cl_false; + return false; } else { if (evenp(n) || cl_trialdivision(n,1,trialdivide_limit)) - return cl_false; + return false; } // Step 2: Miller-Rabin test. return cl_miller_rabin_test(n,count,NULL);