From 1f789b83485a1fe7e13470b87948f2829a47c595 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Mon, 30 Dec 2013 17:31:56 +0100 Subject: [PATCH] Lessen peak memory usage in EC by freeing earlier Cuts peak usage by 25% :) --- library/ecp.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/library/ecp.c b/library/ecp.c index 5cde22d69..c3397a4f8 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -791,7 +791,16 @@ static int ecp_normalize_jac_many( const ecp_group *grp, MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X ); MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y ); MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y ); - MPI_CHK( mpi_lset( &T[i]->Z, 1 ) ); + + /* + * Post-precessing: reclaim some memory by shrinking coordinates + * - not storing Z (always 1) + * - shrinking other coordinates, but still keeping the same number of + * limbs as P, as otherwise it will too likely be regrown too fast. + */ + mpi_shrink( &T[i]->X, grp->P.n ); + mpi_shrink( &T[i]->Y, grp->P.n ); + mpi_free( &T[i]->Z ); if( i == 0 ) break; @@ -1205,19 +1214,6 @@ static int ecp_precompute_comb( const ecp_group *grp, ecp_normalize_jac_many( grp, TT, k ); - /* - * Post-precessing: reclaim some memory by - * - not storing Z (always 1) - * - shrinking other coordinates - * Keep the same number of limbs as P to avoid re-growing on next use. - */ - for( i = 0; i < ( 1U << (w-1) ); i++ ) - { - mpi_free( &T[i].Z ); - mpi_shrink( &T[i].X, grp->P.n ); - mpi_shrink( &T[i].Y, grp->P.n ); - } - cleanup: return( ret ); }