Lessen peak memory usage in EC by freeing earlier
Cuts peak usage by 25% :)
diff --git a/library/ecp.c b/library/ecp.c
index 5cde22d..c3397a4 100644
--- a/library/ecp.c
+++ b/library/ecp.c
@@ -791,7 +791,16 @@
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 @@
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 );
}