1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-08-08 17:42:09 +03:00

Merge pull request #6750 from tom-cosgrove-arm/issue-6023-mod_inv_prime

Bignum: Implement mbedtls_mpi_mod_raw_inv_prime()
This commit is contained in:
Manuel Pégourié-Gonnard
2022-12-12 09:52:21 +01:00
committed by GitHub
8 changed files with 197 additions and 4 deletions

View File

@@ -1097,6 +1097,12 @@ void mpi_core_exp_mod( char * input_N, char * input_A,
TEST_EQUAL( 0, memcmp( X, Y, N_limbs * sizeof( mbedtls_mpi_uint ) ) );
/* Check when output aliased to input */
mbedtls_mpi_core_exp_mod( A, A, N, N_limbs, E, E_limbs, R2, T );
TEST_EQUAL( 0, memcmp( X, A, N_limbs * sizeof( mbedtls_mpi_uint ) ) );
exit:
mbedtls_free( T );
mbedtls_free( A );

View File

@@ -349,6 +349,75 @@ exit:
/* BEGIN MERGE SLOT 3 */
/* BEGIN_CASE */
void mpi_mod_raw_inv_prime( char * input_N, char * input_A, char * input_X )
{
mbedtls_mpi_uint *A = NULL;
mbedtls_mpi_uint *N = NULL;
mbedtls_mpi_uint *X = NULL;
size_t A_limbs, N_limbs, X_limbs;
mbedtls_mpi_uint *Y = NULL;
mbedtls_mpi_uint *T = NULL;
const mbedtls_mpi_uint *R2 = NULL;
/* Legacy MPIs for computing R2 */
mbedtls_mpi N_mpi; /* gets set up manually, aliasing N, so no need to free */
mbedtls_mpi R2_mpi;
mbedtls_mpi_init( &R2_mpi );
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &A, &A_limbs, input_A ) );
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &N_limbs, input_N ) );
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &X, &X_limbs, input_X ) );
ASSERT_ALLOC( Y, N_limbs );
TEST_EQUAL( A_limbs, N_limbs );
TEST_EQUAL( X_limbs, N_limbs );
N_mpi.s = 1;
N_mpi.p = N;
N_mpi.n = N_limbs;
TEST_EQUAL( 0, mbedtls_mpi_core_get_mont_r2_unsafe( &R2_mpi, &N_mpi ) );
TEST_EQUAL( 0, mbedtls_mpi_grow( &R2_mpi, N_limbs ) );
R2 = R2_mpi.p;
size_t working_limbs = mbedtls_mpi_mod_raw_inv_prime_working_limbs( N_limbs );
/* No point exactly duplicating the code in mbedtls_mpi_mod_raw_inv_prime_working_limbs()
* to see if the output is correct, but we can check that it's in a
* reasonable range. The current calculation works out as
* `1 + N_limbs * (welem + 4)`, where welem is the number of elements in
* the window (1 << 1 up to 1 << 6).
*/
size_t min_expected_working_limbs = 1 + N_limbs * 5;
size_t max_expected_working_limbs = 1 + N_limbs * 68;
TEST_LE_U( min_expected_working_limbs, working_limbs );
TEST_LE_U( working_limbs, max_expected_working_limbs );
ASSERT_ALLOC( T, working_limbs );
mbedtls_mpi_mod_raw_inv_prime( Y, A, N, N_limbs, R2, T );
TEST_EQUAL( 0, memcmp( X, Y, N_limbs * sizeof( mbedtls_mpi_uint ) ) );
/* Check when output aliased to input */
mbedtls_mpi_mod_raw_inv_prime( A, A, N, N_limbs, R2, T );
TEST_EQUAL( 0, memcmp( X, A, N_limbs * sizeof( mbedtls_mpi_uint ) ) );
exit:
mbedtls_free( T );
mbedtls_free( A );
mbedtls_free( N );
mbedtls_free( X );
mbedtls_free( Y );
mbedtls_mpi_free( &R2_mpi );
// R2 doesn't need to be freed as it is only aliasing R2_mpi
// N_mpi doesn't need to be freed as it is only aliasing N
}
/* END_CASE */
/* END MERGE SLOT 3 */
/* BEGIN MERGE SLOT 4 */