From 1a7c5ef42bc6b1a5e6a6cd6ba9c014d8e7163ad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 13 Aug 2015 10:19:09 +0200 Subject: [PATCH] Optimize some case of mbedtls_ecp_muladd() Those are used by EC-JPAKE --- library/ecp.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/library/ecp.c b/library/ecp.c index 54f51ab50..89a5612b9 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -1697,8 +1697,17 @@ int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, mbedtls_ecp_point_init( &mP ); - MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, &mP, m, P, NULL, NULL ) ); - MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, R, n, Q, NULL, NULL ) ); + /* Optimize some simple special cases */ + if( mbedtls_mpi_cmp_int( m, 1 ) == 0 ) + 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_normalize_jac( grp, R ) );