mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2025-01-09 14:35:45 +00:00
Optimize more common cases in ecp_muladd()
This commit is contained in:
parent
241bf6717a
commit
00992d45c0
|
@ -1667,8 +1667,39 @@ cleanup:
|
||||||
}
|
}
|
||||||
#endif /* ECP_SHORTWEIERSTRASS */
|
#endif /* ECP_SHORTWEIERSTRASS */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* R = m * P with shortcuts for m == 1 and m == -1
|
||||||
|
* NOT constant-time - ONLY for short Weierstrass!
|
||||||
|
*/
|
||||||
|
static int mbedtls_ecp_mul_shortcuts( mbedtls_ecp_group *grp,
|
||||||
|
mbedtls_ecp_point *R,
|
||||||
|
const mbedtls_mpi *m,
|
||||||
|
const mbedtls_ecp_point *P )
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if( mbedtls_mpi_cmp_int( m, 1 ) == 0 )
|
||||||
|
{
|
||||||
|
MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
|
||||||
|
}
|
||||||
|
else if( mbedtls_mpi_cmp_int( m, -1 ) == 0 )
|
||||||
|
{
|
||||||
|
MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
|
||||||
|
if( mbedtls_mpi_cmp_int( &R->Y, 0 ) != 0 )
|
||||||
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, R, m, P, NULL, NULL ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
return( ret );
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Linear combination
|
* Linear combination
|
||||||
|
* NOT constant-time
|
||||||
*/
|
*/
|
||||||
int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
||||||
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
|
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
|
||||||
|
@ -1682,16 +1713,8 @@ int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
||||||
|
|
||||||
mbedtls_ecp_point_init( &mP );
|
mbedtls_ecp_point_init( &mP );
|
||||||
|
|
||||||
/* Optimize some simple special cases */
|
MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, &mP, m, P ) );
|
||||||
if( mbedtls_mpi_cmp_int( m, 1 ) == 0 )
|
MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, R, n, Q ) );
|
||||||
MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &mP, P ) );
|
|
||||||
else
|
|
||||||
MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, &mP, m, P, NULL, NULL ) );
|
|
||||||
|
|
||||||
if( mbedtls_mpi_cmp_int( n, 1 ) == 0 )
|
|
||||||
MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, Q ) );
|
|
||||||
else
|
|
||||||
MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, R, n, Q, NULL, NULL ) );
|
|
||||||
|
|
||||||
MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, &mP, R ) );
|
MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, &mP, R ) );
|
||||||
MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, R ) );
|
MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, R ) );
|
||||||
|
|
Loading…
Reference in a new issue