2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
15 #include <openssl/bn.h>
16 #include <openssl/crypto.h>
17 #include <openssl/err.h>
18 #include <openssl/rand.h>
19 #include "internal/nelem.h"
20 #include "internal/numbers.h"
23 #include "crypto/bn.h"
27 static int test_is_prime_enhanced(void)
33 ret
= TEST_ptr(bn
= BN_new())
34 /* test passing a prime returns the correct status */
35 && TEST_true(BN_set_word(bn
, 11))
36 /* return extra parameters related to composite */
37 && TEST_true(ossl_bn_miller_rabin_is_prime(bn
, 10, ctx
, NULL
, 1,
39 && TEST_int_eq(status
, BN_PRIMETEST_PROBABLY_PRIME
);
44 static int composites
[] = {
48 static int test_is_composite_enhanced(int id
)
54 ret
= TEST_ptr(bn
= BN_new())
55 /* negative tests for different composite numbers */
56 && TEST_true(BN_set_word(bn
, composites
[id
]))
57 && TEST_true(ossl_bn_miller_rabin_is_prime(bn
, 10, ctx
, NULL
, 1,
59 && TEST_int_ne(status
, BN_PRIMETEST_PROBABLY_PRIME
);
65 /* Test that multiplying all the small primes from 3 to 751 equals a constant.
66 * This test is mainly used to test that both 32 and 64 bit are correct.
68 static int test_bn_small_factors(void)
73 if (!(TEST_ptr(b
= BN_new()) && TEST_true(BN_set_word(b
, 3))))
76 for (i
= 1; i
< NUMPRIMES
; i
++) {
77 prime_t p
= primes
[i
];
78 if (p
> 3 && p
<= 751 && !BN_mul_word(b
, p
))
83 ret
= TEST_BN_eq(ossl_bn_get0_small_factors(), b
);
91 if (!TEST_ptr(ctx
= BN_CTX_new()))
94 ADD_TEST(test_is_prime_enhanced
);
95 ADD_ALL_TESTS(test_is_composite_enhanced
, (int)OSSL_NELEM(composites
));
96 ADD_TEST(test_bn_small_factors
);
101 void cleanup_tests(void)