@@ -3416,6 +3416,84 @@ void run_group_decompress(void) {
34163416
34173417/***** ECMULT TESTS *****/ 
34183418
3419+ void  test_pre_g_table (const  secp256k1_ge_storage  *  pre_g , size_t  n ) {
3420+     /* Tests the pre_g / pre_g_128 tables for consistency. 
3421+      * For independent verification we take a "geometric" approach to verification. 
3422+      * We check that every entry is on-curve. 
3423+      * We check that for consecutive entries p and q, that p + gg - q = 0 by checking 
3424+      *  (1) p, gg, and -q are colinear. 
3425+      *  (2) p, gg, and -q are all distinct. 
3426+      * where gg is twice the generator, where the generator is the first table entry. 
3427+      * 
3428+      * Checking the table's generators are correct is done in run_ecmult_pre_g. 
3429+      */ 
3430+     secp256k1_gej  g2 ;
3431+     secp256k1_ge  p , q , gg ;
3432+     secp256k1_fe  dpx , dpy , dqx , dqy ;
3433+     size_t  i ;
3434+ 
3435+     CHECK (0  <  n );
3436+ 
3437+     secp256k1_ge_from_storage (& p , & pre_g [0 ]);
3438+     secp256k1_fe_verify (& p .x );
3439+     secp256k1_fe_verify (& p .y );
3440+     CHECK (secp256k1_ge_is_valid_var (& p ));
3441+ 
3442+     secp256k1_gej_set_ge (& g2 , & p );
3443+     secp256k1_gej_double_var (& g2 , & g2 , NULL );
3444+     secp256k1_ge_set_gej_var (& gg , & g2 );
3445+     for  (i  =  1 ; i  <  n ; ++ i ) {
3446+         secp256k1_fe_negate (& dpx , & p .x , 1 ); secp256k1_fe_add (& dpx , & gg .x ); secp256k1_fe_normalize_weak (& dpx );
3447+         secp256k1_fe_negate (& dpy , & p .y , 1 ); secp256k1_fe_add (& dpy , & gg .y ); secp256k1_fe_normalize_weak (& dpy );
3448+         /* Check that p is not equal to gg */ 
3449+         CHECK (!secp256k1_fe_normalizes_to_zero_var (& dpx ) ||  !secp256k1_fe_normalizes_to_zero_var (& dpy ));
3450+ 
3451+         secp256k1_ge_from_storage (& q , & pre_g [i ]);
3452+         secp256k1_fe_verify (& q .x );
3453+         secp256k1_fe_verify (& q .y );
3454+         CHECK (secp256k1_ge_is_valid_var (& q ));
3455+ 
3456+         secp256k1_fe_negate (& dqx , & q .x , 1 ); secp256k1_fe_add (& dqx , & gg .x ); secp256k1_fe_normalize_weak (& dqx );
3457+         dqy  =  q .y ; secp256k1_fe_add (& dqy , & gg .y ); secp256k1_fe_normalize_weak (& dqy );
3458+         /* Check that -q is not equal to gg */ 
3459+         CHECK (!secp256k1_fe_normalizes_to_zero_var (& dqx ) ||  !secp256k1_fe_normalizes_to_zero_var (& dqy ));
3460+ 
3461+         /* Check that -q is not equal to p */ 
3462+         CHECK (!secp256k1_fe_equal_var (& dpx , & dqx ) ||  !secp256k1_fe_equal_var (& dpy , & dqy ));
3463+ 
3464+         /* Check that p, -q and gg are colinear */ 
3465+         secp256k1_fe_mul (& dpx , & dpx , & dqy );
3466+         secp256k1_fe_mul (& dpy , & dpy , & dqx );
3467+         CHECK (secp256k1_fe_equal_var (& dpx , & dpy ));
3468+ 
3469+         p  =  q ;
3470+     }
3471+ }
3472+ 
3473+ void  run_ecmult_pre_g (void ) {
3474+     secp256k1_ge_storage  gs ;
3475+     secp256k1_gej  gj ;
3476+     secp256k1_ge  g ;
3477+     size_t  i ;
3478+ 
3479+     /* Check that the pre_g and pre_g_128 tables are consistent. */ 
3480+     test_pre_g_table (secp256k1_pre_g , ECMULT_TABLE_SIZE (WINDOW_G ));
3481+     test_pre_g_table (secp256k1_pre_g_128 , ECMULT_TABLE_SIZE (WINDOW_G ));
3482+ 
3483+     /* Check the first entry from the pre_g table. */ 
3484+     secp256k1_ge_to_storage (& gs , & secp256k1_ge_const_g );
3485+     CHECK (secp256k1_memcmp_var (& gs , & secp256k1_pre_g [0 ], sizeof (gs )) ==  0 );
3486+ 
3487+     /* Check the first entry from the pre_g_128 table. */ 
3488+     secp256k1_gej_set_ge (& gj , & secp256k1_ge_const_g );
3489+     for  (i  =  0 ; i  <  128 ; ++ i ) {
3490+       secp256k1_gej_double_var (& gj , & gj , NULL );
3491+     }
3492+     secp256k1_ge_set_gej (& g , & gj );
3493+     secp256k1_ge_to_storage (& gs , & g );
3494+     CHECK (secp256k1_memcmp_var (& gs , & secp256k1_pre_g_128 [0 ], sizeof (gs )) ==  0 );
3495+ }
3496+ 
34193497void  run_ecmult_chain (void ) {
34203498    /* random starting point A (on the curve) */ 
34213499    secp256k1_gej  a  =  SECP256K1_GEJ_CONST (
@@ -6620,6 +6698,7 @@ int main(int argc, char **argv) {
66206698    run_group_decompress ();
66216699
66226700    /* ecmult tests */ 
6701+     run_ecmult_pre_g ();
66236702    run_wnaf ();
66246703    run_point_times_order ();
66256704    run_ecmult_near_split_bound ();
0 commit comments