static initialize comb table

MBEDTLS_ECP_FIXED_POINT_OPTIM aims to speed up ecc multiplication performance.

We compute the comb table in runtime now. It is a costly operation.

This patch add a pre-computed table to initialize well-known curves. It speed up ECDSA signature verify process in runtime by using more ROM size.

Signed-off-by: kXuan <kxuanobj@gmail.com>
This commit is contained in:
kXuan 2021-04-08 14:32:06 +08:00
parent 6d84e917bb
commit ba9cb76e9f
No known key found for this signature in database
GPG Key ID: E53EA872AFCA251C
4 changed files with 4035 additions and 27 deletions

View File

@ -229,7 +229,7 @@ typedef struct mbedtls_ecp_group
int (*t_post)(mbedtls_ecp_point *, void *); /*!< Unused. */ int (*t_post)(mbedtls_ecp_point *, void *); /*!< Unused. */
void *t_data; /*!< Unused. */ void *t_data; /*!< Unused. */
mbedtls_ecp_point *T; /*!< Pre-computed points for ecp_mul_comb(). */ mbedtls_ecp_point *T; /*!< Pre-computed points for ecp_mul_comb(). */
size_t T_size; /*!< The number of pre-computed points. */ size_t T_size; /*!< The number of dynamic allocated pre-computed points. */
} }
mbedtls_ecp_group; mbedtls_ecp_group;
@ -276,15 +276,15 @@ mbedtls_ecp_group;
#if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM) #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
/* /*
* Trade memory for speed on fixed-point multiplication. * Trade ROM usage for speed on fixed-point multiplication.
* *
* This speeds up repeated multiplication of the generator (that is, the * This speeds up repeated multiplication of the generator (that is, the
* multiplication in ECDSA signatures, and half of the multiplications in * multiplication in ECDSA signatures, and half of the multiplications in
* ECDSA verification and ECDHE) by a factor roughly 3 to 4. * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
* *
* The cost is increasing EC peak memory usage by a factor roughly 2. * The cost is increasing ROM usage by a factor roughly 2.
* *
* Change this value to 0 to reduce peak memory usage. * Change this value to 0 to reduce ROM usage.
*/ */
#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */ #define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */
#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */ #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */

View File

@ -728,6 +728,18 @@ void mbedtls_ecp_point_free( mbedtls_ecp_point *pt )
mbedtls_mpi_free( &( pt->Z ) ); mbedtls_mpi_free( &( pt->Z ) );
} }
/*
* Check that the comb table (grp->T) is static initialized.
*/
static int ecp_group_is_static_comb_table( const mbedtls_ecp_group *grp ) {
#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
return grp->T != NULL && grp->T_size == 0;
#else
(void) grp;
return 0;
#endif
}
/* /*
* Unallocate (the components of) a group * Unallocate (the components of) a group
*/ */
@ -747,7 +759,7 @@ void mbedtls_ecp_group_free( mbedtls_ecp_group *grp )
mbedtls_mpi_free( &grp->N ); mbedtls_mpi_free( &grp->N );
} }
if( grp->T != NULL ) if( !ecp_group_is_static_comb_table(grp) && grp->T != NULL )
{ {
for( i = 0; i < grp->T_size; i++ ) for( i = 0; i < grp->T_size; i++ )
mbedtls_ecp_point_free( &grp->T[i] ); mbedtls_ecp_point_free( &grp->T[i] );
@ -2245,11 +2257,16 @@ static unsigned char ecp_pick_window_size( const mbedtls_ecp_group *grp,
w++; w++;
/* /*
* Make sure w is within bounds. * If static comb table may not be used (!p_eq_g) or static comb table does
* not exists, make sure w is within bounds.
* (The last test is useful only for very small curves in the test suite.) * (The last test is useful only for very small curves in the test suite.)
*
* The user reduces MBEDTLS_ECP_WINDOW_SIZE does not changes the size of
* static comb table, because the size of static comb table is fixed when
* it is generated.
*/ */
#if( MBEDTLS_ECP_WINDOW_SIZE < 6 ) #if( MBEDTLS_ECP_WINDOW_SIZE < 6 )
if( w > MBEDTLS_ECP_WINDOW_SIZE ) if( (!p_eq_g || !ecp_group_is_static_comb_table(grp)) && w > MBEDTLS_ECP_WINDOW_SIZE )
w = MBEDTLS_ECP_WINDOW_SIZE; w = MBEDTLS_ECP_WINDOW_SIZE;
#endif #endif
if( w >= grp->nbits ) if( w >= grp->nbits )

File diff suppressed because it is too large Load Diff

View File

@ -251,7 +251,11 @@ static int myrand( void *rng_state, unsigned char *output, size_t len )
#if defined(MBEDTLS_ECP_C) #if defined(MBEDTLS_ECP_C)
void ecp_clear_precomputed( mbedtls_ecp_group *grp ) void ecp_clear_precomputed( mbedtls_ecp_group *grp )
{ {
if( grp->T != NULL ) if( grp->T != NULL
#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
&& grp->T_size != 0
#endif
)
{ {
size_t i; size_t i;
for( i = 0; i < grp->T_size; i++ ) for( i = 0; i < grp->T_size; i++ )