Implemented generic doubling

This commit is contained in:
Manuel Pégourié-Gonnard 2012-11-06 16:10:47 +01:00 committed by Paul Bakker
parent ab38b70816
commit de532ee73f

View file

@ -180,7 +180,6 @@ static int ecp_add_generic( const ecp_group *grp, ecp_point *R,
/* /*
* LL = L^2 mod p * LL = L^2 mod p
* X = L^2 - P.X - Q.X
*/ */
MPI_CHK( mpi_mul_mpi( &LL, &L, &L ) ); MPI_CHK( mpi_mul_mpi( &LL, &L, &L ) );
MPI_CHK( mpi_mod_mpi( &LL, &LL, &grp->P ) ); MPI_CHK( mpi_mod_mpi( &LL, &LL, &grp->P ) );
@ -215,15 +214,60 @@ cleanup:
/* /*
* Doubling: R = 2 * P, generic case (P != 0, R != 0) * Doubling: R = 2 * P, generic case (P != 0, R != 0)
* Cf SEC1 v2 p. 7, item 5
*/ */
static int ecp_double_generic( const ecp_group *grp, ecp_point *R, static int ecp_double_generic( const ecp_group *grp, ecp_point *R,
const ecp_point *P ) const ecp_point *P )
{ {
int ret = 0; int ret = 0;
mpi LN, LD, K, L, LL, X, Y;
(void) grp; mpi_init( &LN ); mpi_init( &LD ); mpi_init( &K ); mpi_init( &L );
(void) R; mpi_init( &LL ); mpi_init( &X ); mpi_init( &Y );
(void) P;
/*
* L = 3 (P.X - 1) (P.X + 1) / (2 P.Y) mod p
*/
MPI_CHK( mpi_copy( &LD, &P->Y ) );
MPI_CHK( mpi_shift_l( &LD, 1 ) );
MPI_CHK( mpi_inv_mod( &K, &LD, &grp->P ) );
MPI_CHK( mpi_mul_int( &K, &K, 3 ) );
MPI_CHK( mpi_sub_int( &LN, &P->X, 1 ) );
MPI_CHK( mpi_mul_mpi( &K, &K, &LN ) );
MPI_CHK( mpi_add_int( &LN, &P->X, 1 ) );
MPI_CHK( mpi_mul_mpi( &K, &K, &LN ) );
MPI_CHK( mpi_mod_mpi( &L, &K, &grp->P ) );
/*
* LL = L^2 mod p
*/
MPI_CHK( mpi_mul_mpi( &LL, &L, &L ) );
MPI_CHK( mpi_mod_mpi( &LL, &LL, &grp->P ) );
/*
* X = L^2 - 2 * P.X
*/
MPI_CHK( mpi_sub_mpi( &X, &LL, &P->X ) );
MPI_CHK( mpi_sub_mpi( &X, &X, &P->X ) );
/*
* Y = L * (P.X - X) - P.Y
*/
MPI_CHK( mpi_sub_mpi( &Y, &P->X, &X) );
MPI_CHK( mpi_mul_mpi( &Y, &Y, &L ) );
MPI_CHK( mpi_sub_mpi( &Y, &Y, &P->Y ) );
/*
* R = (X mod p, Y mod p)
*/
R->is_zero = 0;
MPI_CHK( mpi_mod_mpi( &R->X, &X, &grp->P ) );
MPI_CHK( mpi_mod_mpi( &R->Y, &Y, &grp->P ) );
cleanup:
mpi_free( &LN ); mpi_init( &LD ); mpi_init( &K ); mpi_init( &L );
mpi_free( &LL ); mpi_init( &X ); mpi_init( &Y );
return( ret ); return( ret );
} }
@ -264,7 +308,6 @@ int ecp_add( const ecp_group *grp, ecp_point *R,
return ret; return ret;
} }
#if defined(POLARSSL_SELF_TEST) #if defined(POLARSSL_SELF_TEST)
/* /*
@ -290,7 +333,6 @@ static void ecp_point_print( const ecp_point *P )
printf( "(%lu, %lu)\n", P->X.p[0], P->Y.p[0] ); printf( "(%lu, %lu)\n", P->X.p[0], P->Y.p[0] );
} }
/* /*
* Checkup routine * Checkup routine
* *
@ -303,20 +345,21 @@ int ecp_self_test( int verbose )
int ret = 0; int ret = 0;
unsigned i; unsigned i;
ecp_group grp; ecp_group grp;
ecp_point O, A, B, C, D, E, F, G, TMP; ecp_point O, A, B, C, D, E, F, G, H, TMP;
ecp_point *add_tbl[][3] = ecp_point *add_tbl[][3] =
{ {
{&O, &O, &O}, {&O, &A, &A}, {&A, &O, &A}, {&O, &O, &O}, {&O, &A, &A}, {&A, &O, &A},
{&A, &A, &O}, {&B, &C, &O}, {&C, &B, &O}, {&A, &A, &O}, {&B, &C, &O}, {&C, &B, &O},
{&A, &D, &E}, {&D, &A, &E}, {&A, &D, &E}, {&D, &A, &E},
{&B, &D, &F}, {&D, &B, &F}, {&B, &D, &F}, {&D, &B, &F},
// {&D, &D, &G}, {&D, &D, &G}, {&B, &B, &H},
}; };
ecp_group_init( &grp ); ecp_group_init( &grp );
ecp_point_init( &O ); ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &O ); ecp_point_init( &A ); ecp_point_init( &B );
ecp_point_init( &C ); ecp_point_init( &D ); ecp_point_init( &E ); ecp_point_init( &C ); ecp_point_init( &D ); ecp_point_init( &E );
ecp_point_init( &F ); ecp_point_init( &G ); ecp_point_init( &TMP ); ecp_point_init( &F ); ecp_point_init( &G ); ecp_point_init( &H );
ecp_point_init( &TMP );
ecp_set_zero( &O ); ecp_set_zero( &O );
MPI_CHK( ecp_group_read_string( &grp, 10, "47", "4", "17", "42", "13" ) ); MPI_CHK( ecp_group_read_string( &grp, 10, "47", "4", "17", "42", "13" ) );
@ -327,6 +370,7 @@ int ecp_self_test( int verbose )
MPI_CHK( ecp_point_read_string( &E, 10, "34", "14" ) ); MPI_CHK( ecp_point_read_string( &E, 10, "34", "14" ) );
MPI_CHK( ecp_point_read_string( &F, 10, "45", "7" ) ); MPI_CHK( ecp_point_read_string( &F, 10, "45", "7" ) );
MPI_CHK( ecp_point_read_string( &G, 10, "21", "32" ) ); MPI_CHK( ecp_point_read_string( &G, 10, "21", "32" ) );
MPI_CHK( ecp_point_read_string( &H, 10, "27", "30" ) );
if( verbose != 0 ) if( verbose != 0 )
printf( " ECP test #1 (ecp_add): " ); printf( " ECP test #1 (ecp_add): " );
@ -350,7 +394,7 @@ int ecp_self_test( int verbose )
} }
if (verbose != 0 ) if (verbose != 0 )
printf( " passed\n" ); printf( "passed\n" );
cleanup: cleanup:
@ -360,7 +404,8 @@ cleanup:
ecp_group_free( &grp ); ecp_group_free( &grp );
ecp_point_free( &O ); ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &O ); ecp_point_free( &A ); ecp_point_free( &B );
ecp_point_free( &C ); ecp_point_free( &D ); ecp_point_free( &E ); ecp_point_free( &C ); ecp_point_free( &D ); ecp_point_free( &E );
ecp_point_free( &F ); ecp_point_free( &G ); ecp_point_free( &F ); ecp_point_free( &G ); ecp_point_free( &H );
ecp_point_free( &TMP );
if( verbose != 0 ) if( verbose != 0 )
printf( "\n" ); printf( "\n" );