From 2536aa709bc26b6cb8dd840cd2f7368767eee7e6 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 24 Jul 2020 00:12:59 +0200 Subject: [PATCH] mbedtls_mpi_div_mpi: directly grow T1 to its useful size T1 is set to a 2-limb value. The first operation that takes it as input is mbedtls_mpi_mul_int, which makes it grow to 3 limbs. Later it is shifted left, which causes it to grow again. Set its size to the final size from the start. This saves two calls to calloc(), at the expense of a slowdown in some operations involving T1 as input since it now has more leading zeros. Setting T1 to 3 limbs initially instead of 2 saves about 6% of the calloc() calls in test_suite_ecp and does not incur a performance penalty. Setting T1 to A->n + 2 limbs instead of 2 saves about 20% of the calloc calls and does not cause a measurable performance difference on my Linux PC. Signed-off-by: Gilles Peskine --- library/bignum.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/bignum.c b/library/bignum.c index 4413752633..f1e544370f 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -1830,7 +1830,7 @@ int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) ); MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) ); - MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, 2 ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, A->n + 2 ) ); k = mbedtls_mpi_bitlen( &Y ) % biL; if( k < biL - 1 )