2020#include "group_impl.h"
2121#include "ecmult.h"
2222
23- void print_table ( FILE * fp , const char * name , int window_g , const secp256k1_gej * gen , int with_conditionals ) {
24- static secp256k1_gej gj ;
25- static secp256k1_ge ge , dgen ;
26- static secp256k1_ge_storage ges ;
23+ /* Construct table of all odd multiples of gen in range 1..(2**(window_g-1)-1). */
24+ static void secp256k1_ecmult_compute_table ( secp256k1_ge_storage * table , int window_g , const secp256k1_gej * gen ) {
25+ secp256k1_gej gj ;
26+ secp256k1_ge ge , dgen ;
2727 int j ;
28- int i ;
2928
3029 gj = * gen ;
3130 secp256k1_ge_set_gej_var (& ge , & gj );
32- secp256k1_ge_to_storage (& ges , & ge );
31+ secp256k1_ge_to_storage (& table [0 ], & ge );
32+
33+ secp256k1_gej_double_var (& gj , gen , NULL );
34+ secp256k1_ge_set_gej_var (& dgen , & gj );
35+
36+ for (j = 1 ; j < ECMULT_TABLE_SIZE (window_g ); ++ j ) {
37+ secp256k1_gej_set_ge (& gj , & ge );
38+ secp256k1_gej_add_ge_var (& gj , & gj , & dgen , NULL );
39+ secp256k1_ge_set_gej_var (& ge , & gj );
40+ secp256k1_ge_to_storage (& table [j ], & ge );
41+ }
42+ }
43+
44+ /* Like secp256k1_ecmult_compute_table, but one for both gen and gen*2^128. */
45+ static void secp256k1_ecmult_compute_two_tables (secp256k1_ge_storage * table , secp256k1_ge_storage * table_128 , int window_g , const secp256k1_ge * gen ) {
46+ secp256k1_gej gj ;
47+ int i ;
48+
49+ secp256k1_gej_set_ge (& gj , gen );
50+ secp256k1_ecmult_compute_table (table , window_g , & gj );
51+ for (i = 0 ; i < 128 ; ++ i ) {
52+ secp256k1_gej_double_var (& gj , & gj , NULL );
53+ }
54+ secp256k1_ecmult_compute_table (table_128 , window_g , & gj );
55+ }
56+
57+ static void print_table (FILE * fp , const char * name , int window_g , const secp256k1_ge_storage * table , int with_conditionals ) {
58+ int j ;
59+ int i ;
3360
3461 fprintf (fp , "static const secp256k1_ge_storage %s[ECMULT_TABLE_SIZE(WINDOW_G)] = {\n" , name );
3562 fprintf (fp , " S(%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32
3663 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ")\n" ,
37- SECP256K1_GE_STORAGE_CONST_GET (ges ));
38-
39- secp256k1_gej_double_var (& gj , gen , NULL );
40- secp256k1_ge_set_gej_var (& dgen , & gj );
64+ SECP256K1_GE_STORAGE_CONST_GET (table [0 ]));
4165
4266 j = 1 ;
4367 for (i = 3 ; i <= window_g ; ++ i ) {
4468 if (with_conditionals ) {
4569 fprintf (fp , "#if ECMULT_TABLE_SIZE(WINDOW_G) > %ld\n" , ECMULT_TABLE_SIZE (i - 1 ));
4670 }
4771 for (;j < ECMULT_TABLE_SIZE (i ); ++ j ) {
48- secp256k1_gej_set_ge (& gj , & ge );
49- secp256k1_gej_add_ge_var (& gj , & gj , & dgen , NULL );
50- secp256k1_ge_set_gej_var (& ge , & gj );
51- secp256k1_ge_to_storage (& ges , & ge );
52-
5372 fprintf (fp , ",S(%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32
5473 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ",%" PRIx32 ")\n" ,
55- SECP256K1_GE_STORAGE_CONST_GET (ges ));
74+ SECP256K1_GE_STORAGE_CONST_GET (table [ j ] ));
5675 }
5776 if (with_conditionals ) {
5877 fprintf (fp , "#endif\n" );
@@ -61,16 +80,17 @@ void print_table(FILE *fp, const char *name, int window_g, const secp256k1_gej *
6180 fprintf (fp , "};\n" );
6281}
6382
64- void print_two_tables (FILE * fp , int window_g , const secp256k1_ge * g , int with_conditionals ) {
65- secp256k1_gej gj ;
66- int i ;
83+ static void print_two_tables (FILE * fp , int window_g , const secp256k1_ge * g , int with_conditionals ) {
84+ secp256k1_ge_storage * table = malloc ( ECMULT_TABLE_SIZE ( window_g ) * sizeof ( secp256k1_ge_storage )) ;
85+ secp256k1_ge_storage * table_128 = malloc ( ECMULT_TABLE_SIZE ( window_g ) * sizeof ( secp256k1_ge_storage )) ;
6786
68- secp256k1_gej_set_ge (& gj , g );
69- print_table (fp , "secp256k1_pre_g" , window_g , & gj , with_conditionals );
70- for (i = 0 ; i < 128 ; ++ i ) {
71- secp256k1_gej_double_var (& gj , & gj , NULL );
72- }
73- print_table (fp , "secp256k1_pre_g_128" , window_g , & gj , with_conditionals );
87+ secp256k1_ecmult_compute_two_tables (table , table_128 , window_g , g );
88+
89+ print_table (fp , "secp256k1_pre_g" , window_g , table , with_conditionals );
90+ print_table (fp , "secp256k1_pre_g_128" , window_g , table_128 , with_conditionals );
91+
92+ free (table );
93+ free (table_128 );
7494}
7595
7696int main (void ) {
0 commit comments