Make a public version of mpi_montg_init() in bignum_new.c and add unit tests

The unit tests were created by capturing runs of the existing function during
execution of existing unit tests.

Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
This commit is contained in:
Tom Cosgrove 2022-08-17 06:17:00 +01:00
parent 659c84add9
commit 79b70f6394
5 changed files with 108 additions and 11 deletions

View file

@ -56,6 +56,21 @@ void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
(void) mbedtls_mpi_core_add_if( X, N, n, ( carry < borrow ) );
}
/*
* Fast Montgomery initialization (thanks to Tom St Denis).
*/
mbedtls_mpi_uint mbedtls_mpi_montg_init( mbedtls_mpi_uint m0 )
{
mbedtls_mpi_uint x = m0;
x += ( ( m0 + 2 ) & 4 ) << 1;
for( unsigned int i = biL; i >= 8; i /= 2 )
x *= ( 2 - ( m0 * x ) );
return( ~x + 1 );
}
mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
const mbedtls_mpi_uint *s, size_t s_len,
mbedtls_mpi_uint b )