Add mpi_gcd_modinv_odd test functions

Signed-off-by: Felix Conway <felix.conway@arm.com>
This commit is contained in:
Felix Conway
2025-08-04 11:34:45 +01:00
parent 54a94c1598
commit 38ec046c4b

View File

@@ -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)
{