From 38ec046c4bd035584be58bc1ca4644da4b413d29 Mon Sep 17 00:00:00 2001 From: Felix Conway Date: Mon, 4 Aug 2025 11:34:45 +0100 Subject: [PATCH] Add mpi_gcd_modinv_odd test functions Signed-off-by: Felix Conway --- tests/suites/test_suite_bignum.function | 165 ++++++++++++++++++++++++ 1 file changed, 165 insertions(+) diff --git a/tests/suites/test_suite_bignum.function b/tests/suites/test_suite_bignum.function index 36f1476d76..b8af87fb34 100644 --- a/tests/suites/test_suite_bignum.function +++ b/tests/suites/test_suite_bignum.function @@ -1149,6 +1149,171 @@ exit: } /* END_CASE */ +/* BEGIN_CASE */ +void mpi_gcd_modinv_odd_both(char *input_A, char *input_N, + char *result_G, char *result_I, + int return_code) +{ + int has_inverse = strcmp(result_I, "no_inverse") ? 1 : 0; + mbedtls_mpi G, I, A, N, exp_G, exp_I; + int res; + mbedtls_mpi_init(&G); mbedtls_mpi_init(&I); mbedtls_mpi_init(&A); mbedtls_mpi_init(&N); + mbedtls_mpi_init(&exp_G); mbedtls_mpi_init(&exp_I); + TEST_EQUAL(mbedtls_test_read_mpi(&A, input_A), 0); + TEST_EQUAL(mbedtls_test_read_mpi(&N, input_N), 0); + TEST_EQUAL(mbedtls_test_read_mpi(&exp_G, result_G), 0); + if (has_inverse) { + TEST_EQUAL(mbedtls_test_read_mpi(&exp_I, result_I), 0); + } + + res = mbedtls_mpi_gcd_modinv_odd(&G, &I, &A, &N); + TEST_EQUAL(res, return_code); + if (res == 0) { + TEST_ASSERT(sign_is_valid(&G)); + TEST_EQUAL(mbedtls_mpi_cmp_mpi(&G, &exp_G), 0); + /* If there is no inverse then the value returned in I will be + * indeterminate, and so not useful or possible to test. */ + if (has_inverse) { + TEST_ASSERT(sign_is_valid(&I)); + TEST_EQUAL(mbedtls_mpi_cmp_mpi(&I, &exp_I), 0); + } + } + + /* Test pointer aliasing where &G == &A. */ + TEST_EQUAL(mbedtls_test_read_mpi(&G, input_A), 0); + res = mbedtls_mpi_gcd_modinv_odd(&G, &I, /* A */ &G, &N); + TEST_EQUAL(res, return_code); + if (res == 0) { + TEST_ASSERT(sign_is_valid(&G)); + TEST_EQUAL(mbedtls_mpi_cmp_mpi(&G, &exp_G), 0); + /* If there is no inverse then the value returned in I will be + * indeterminate, and so not useful or possible to test. */ + if (has_inverse) { + TEST_ASSERT(sign_is_valid(&I)); + TEST_EQUAL(mbedtls_mpi_cmp_mpi(&I, &exp_I), 0); + } + } + + /* Test pointer aliasing where &G == &N. This should fail. */ + TEST_EQUAL(mbedtls_test_read_mpi(&G, input_N), 0); + res = mbedtls_mpi_gcd_modinv_odd(&G, &I, &A, /* N */ &G); + TEST_EQUAL(res, MBEDTLS_ERR_MPI_BAD_INPUT_DATA); + + /* Test pointer aliasing where &I == &A. */ + TEST_EQUAL(mbedtls_test_read_mpi(&I, input_A), 0); + res = mbedtls_mpi_gcd_modinv_odd(&G, &I, /* A */ &I, &N); + TEST_EQUAL(res, return_code); + if (res == 0) { + TEST_ASSERT(sign_is_valid(&G)); + TEST_EQUAL(mbedtls_mpi_cmp_mpi(&G, &exp_G), 0); + /* If there is no inverse then the value returned in I will be + * indeterminate, and so not useful or possible to test. */ + if (has_inverse) { + TEST_ASSERT(sign_is_valid(&I)); + TEST_EQUAL(mbedtls_mpi_cmp_mpi(&I, &exp_I), 0); + } + } + + /* Test pointer aliasing where &I == &N. This should fail. */ + TEST_EQUAL(mbedtls_test_read_mpi(&I, input_N), 0); + res = mbedtls_mpi_gcd_modinv_odd(&G, &I, &A, /* N */ &I); + TEST_EQUAL(res, MBEDTLS_ERR_MPI_BAD_INPUT_DATA); + + /* Test pointer aliasing where &A == &N. This should fail. */ + res = mbedtls_mpi_gcd_modinv_odd(&G, &I, &A, /* N */ &A); + TEST_EQUAL(res, MBEDTLS_ERR_MPI_BAD_INPUT_DATA); +} +/* END_CASE */ + +/* BEGIN_CASE */ +void mpi_gcd_modinv_odd_only_gcd(char *input_A, char *input_N, + char *result_G, int return_code) +{ + mbedtls_mpi G, A, N, exp_G; + int res; + mbedtls_mpi_init(&G); mbedtls_mpi_init(&A); mbedtls_mpi_init(&N); + mbedtls_mpi_init(&exp_G); + TEST_EQUAL(mbedtls_test_read_mpi(&A, input_A), 0); + TEST_EQUAL(mbedtls_test_read_mpi(&N, input_N), 0); + TEST_EQUAL(mbedtls_test_read_mpi(&exp_G, result_G), 0); + + res = mbedtls_mpi_gcd_modinv_odd(&G, NULL, &A, &N); + TEST_EQUAL(res, return_code); + if (res == 0) { + TEST_ASSERT(sign_is_valid(&G)); + TEST_EQUAL(mbedtls_mpi_cmp_mpi(&G, &exp_G), 0); + } + + /* Test pointer aliasing where &G == &A. */ + TEST_EQUAL(mbedtls_test_read_mpi(&G, input_A), 0); + res = mbedtls_mpi_gcd_modinv_odd(&G, NULL, /* A */ &G, &N); + TEST_EQUAL(res, return_code); + if (res == 0) { + TEST_ASSERT(sign_is_valid(&G)); + TEST_EQUAL(mbedtls_mpi_cmp_mpi(&G, &exp_G), 0); + } + + /* Test pointer aliasing where &G == &N. */ + TEST_EQUAL(mbedtls_test_read_mpi(&G, input_N), 0); + res = mbedtls_mpi_gcd_modinv_odd(&G, NULL, &A, /* N */ &G); + TEST_EQUAL(res, return_code); + if (res == 0) { + TEST_ASSERT(sign_is_valid(&G)); + TEST_EQUAL(mbedtls_mpi_cmp_mpi(&G, &exp_G), 0); + } + + /* Test pointer aliasing where &A == &N. This should fail. */ + res = mbedtls_mpi_gcd_modinv_odd(&G, NULL, &A, /* N */ &A); + TEST_EQUAL(res, MBEDTLS_ERR_MPI_BAD_INPUT_DATA); +} +/* END_CASE */ + +/* BEGIN_CASE */ +void mpi_gcd_modinv_odd_only_modinv(char *input_A, char *input_N, + char *result_I, int return_code) +{ + int has_inverse = strcmp(result_I, "no_inverse") ? 1 : 0; + mbedtls_mpi I, A, N, exp_I; + int res; + mbedtls_mpi_init(&I); mbedtls_mpi_init(&A); mbedtls_mpi_init(&N); + mbedtls_mpi_init(&exp_I); + TEST_EQUAL(mbedtls_test_read_mpi(&A, input_A), 0); + TEST_EQUAL(mbedtls_test_read_mpi(&N, input_N), 0); + if (has_inverse) { + TEST_EQUAL(mbedtls_test_read_mpi(&exp_I, result_I), 0); + } + + res = mbedtls_mpi_gcd_modinv_odd(NULL, &I, &A, &N); + TEST_EQUAL(res, return_code); + /* If there is no inverse then the value returned in I will be + * indeterminate, and so not useful or possible to test. */ + if (res == 0 && has_inverse) { + TEST_ASSERT(sign_is_valid(&I)); + TEST_EQUAL(mbedtls_mpi_cmp_mpi(&I, &exp_I), 0); + } + + /* Test pointer aliasing where &I == &A. */ + TEST_EQUAL(mbedtls_test_read_mpi(&I, input_A), 0); + res = mbedtls_mpi_gcd_modinv_odd(NULL, &I, /* A */ &I, &N); + TEST_EQUAL(res, return_code); + /* If there is no inverse then the value returned in I will be + * indeterminate, and so not useful or possible to test. */ + if (res == 0 && has_inverse) { + TEST_ASSERT(sign_is_valid(&I)); + TEST_EQUAL(mbedtls_mpi_cmp_mpi(&I, &exp_I), 0); + } + + /* Test pointer aliasing where &I == &N. This should fail. */ + TEST_EQUAL(mbedtls_test_read_mpi(&I, input_N), 0); + res = mbedtls_mpi_gcd_modinv_odd(NULL, &I, &A, /* N */ &I); + TEST_EQUAL(res, MBEDTLS_ERR_MPI_BAD_INPUT_DATA); + + /* Test pointer aliasing where &A == &N. This should fail. */ + res = mbedtls_mpi_gcd_modinv_odd(NULL, &I, &A, /* N */ &A); + TEST_EQUAL(res, MBEDTLS_ERR_MPI_BAD_INPUT_DATA); +} +/* END_CASE */ + /* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */ void mpi_is_prime(char *input_X, int div_result) {