Skip to content
IntIsProbablePrime

IntIsProbablePrime

Description

Test whether the integer is probably prime using a Miller-Rabin style primality check.

This public macro supports both forms:

  • IntIsProbablePrime(value) - returns the result, no error channel.
  • IntIsProbablePrime(value, error) - writes the error flag through error.

Parameters

Name Direction Description
value in Integer to test.
error out Optional pointer set to true on internal failure and false on success.

Success

Returns true when the value is probably prime.

Failure

Returns false for composite values. With the two-argument form, *error is set to true on internal allocation failure during the witness loop and false otherwise.

Usage example (Cross-references)

Usage examples (Cross-references)
        {
            bool prime_error = false;
            bool prime       = IntIsProbablePrime(modulus, &prime_error);
    
            if (prime_error) {
    
    bool test_int_is_probable_prime(void) {
        WriteFmt("Testing IntIsProbablePrime\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int composite = IntFrom(561, &alloc.base);
    
        bool result = IntIsProbablePrime(&prime);
        result      = result && !IntIsProbablePrime(&composite);
    
        bool result = IntIsProbablePrime(&prime);
        result      = result && !IntIsProbablePrime(&composite);
    
        IntDeinit(&prime);
    
    bool test_m4_one_not_prime(void) {
        WriteFmt("Testing IntIsProbablePrime(1) == false\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int value = IntFrom(1, &alloc.base);
    
        bool result = !IntIsProbablePrime(&value);
    
        IntDeinit(&value);
    
    bool test_m4_two_is_prime(void) {
        WriteFmt("Testing IntIsProbablePrime(2) == true\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int value = IntFrom(2, &alloc.base);
    
        bool result = IntIsProbablePrime(&value);
    
        IntDeinit(&value);
    
    bool test_m4_four_not_prime(void) {
        WriteFmt("Testing IntIsProbablePrime(4) == false\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int value = IntFrom(4, &alloc.base);
    
        bool result = !IntIsProbablePrime(&value);
    
        IntDeinit(&value);
    
    bool test_m4_three_is_prime(void) {
        WriteFmt("Testing IntIsProbablePrime(3) == true\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int value = IntFrom(3, &alloc.base);
    
        bool result = IntIsProbablePrime(&value);
    
        IntDeinit(&value);
    
    bool test_m4_prime_97(void) {
        WriteFmt("Testing IntIsProbablePrime(97) == true (deep witness loop)\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int value = IntFrom(97, &alloc.base);
    
        bool result = IntIsProbablePrime(&value);
    
        IntDeinit(&value);
    
    bool test_m4_composite_561(void) {
        WriteFmt("Testing IntIsProbablePrime(561) == false (Carmichael)\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int value = IntFrom(561, &alloc.base);
    
        bool result = !IntIsProbablePrime(&value);
    
        IntDeinit(&value);
    
    bool test_m4_composite_1763(void) {
        WriteFmt("Testing IntIsProbablePrime(1763 = 41*43) == false (reaches MR)\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int value = IntFrom(1763, &alloc.base);
    
        bool result = !IntIsProbablePrime(&value);
    
        IntDeinit(&value);
    
    bool test_m4_spsp_1373653_composite(void) {
        WriteFmt("Testing IntIsProbablePrime(1373653) == false (SPSP base 2,3)\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int value = IntFromStr("1373653", &alloc.base);
    
        bool result = !IntIsProbablePrime(&value);
    
        IntDeinit(&value);
    
    bool test_m4_large_prime(void) {
        WriteFmt("Testing IntIsProbablePrime(1000000007) == true\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int value = IntFromStr("1000000007", &alloc.base);
    
        bool result = IntIsProbablePrime(&value);
    
        IntDeinit(&value);
    
    bool test_m4_error_flag_cleared(void) {
        WriteFmt("Testing IntIsProbablePrime clears the error flag\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        bool error = true;
    
        bool prime  = IntIsProbablePrime(&value, &error);
        bool result = prime && !error;
        Int composite = IntFrom(1000005u, a); // composite
    
        bool ok = IntIsProbablePrime(&prime) && !IntIsProbablePrime(&composite);
    
        IntDeinit(&prime);
        Int n = IntFrom(2047u, a);
    
        bool ok = !IntIsProbablePrime(&n);
    
        IntDeinit(&n);
        Int  value = IntFrom(561u, alloc);
        bool error = false;
        bool prime = IntIsProbablePrime(&value, &error);
        bool ok    = (error == false) && (prime == false);
Last updated on