precompute_ecmult_gen.c (3239B)
1 /********************************************************************************* 2 * Copyright (c) 2013, 2014, 2015, 2021 Thomas Daede, Cory Fields, Pieter Wuille * 3 * Distributed under the MIT software license, see the accompanying * 4 * file COPYING or https://www.opensource.org/licenses/mit-license.php. * 5 *********************************************************************************/ 6 7 #include <inttypes.h> 8 #include <stdio.h> 9 10 #include "../include/secp256k1.h" 11 12 #include "assumptions.h" 13 #include "util.h" 14 15 #include "group.h" 16 #include "int128_impl.h" 17 #include "ecmult_gen.h" 18 #include "ecmult_gen_compute_table_impl.h" 19 20 int main(int argc, char **argv) { 21 const char outfile[] = "src/precomputed_ecmult_gen.c"; 22 FILE* fp; 23 int bits; 24 25 (void)argc; 26 (void)argv; 27 28 fp = fopen(outfile, "w"); 29 if (fp == NULL) { 30 fprintf(stderr, "Could not open %s for writing!\n", outfile); 31 return -1; 32 } 33 34 fprintf(fp, "/* This file was automatically generated by precompute_ecmult_gen. */\n"); 35 fprintf(fp, "/* See ecmult_gen_impl.h for details about the contents of this file. */\n"); 36 fprintf(fp, "#include \"group.h\"\n"); 37 fprintf(fp, "#include \"ecmult_gen.h\"\n"); 38 fprintf(fp, "#include \"precomputed_ecmult_gen.h\"\n"); 39 fprintf(fp, "#ifdef EXHAUSTIVE_TEST_ORDER\n"); 40 fprintf(fp, "# error Cannot compile precomputed_ecmult_gen.c in exhaustive test mode\n"); 41 fprintf(fp, "#endif /* EXHAUSTIVE_TEST_ORDER */\n"); 42 fprintf(fp, "#define S(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) SECP256K1_GE_STORAGE_CONST(0x##a##u,0x##b##u,0x##c##u,0x##d##u,0x##e##u,0x##f##u,0x##g##u,0x##h##u,0x##i##u,0x##j##u,0x##k##u,0x##l##u,0x##m##u,0x##n##u,0x##o##u,0x##p##u)\n"); 43 fprintf(fp, "const haskellsecp256k1_v0_1_0_ge_storage haskellsecp256k1_v0_1_0_ecmult_gen_prec_table[ECMULT_GEN_PREC_N(ECMULT_GEN_PREC_BITS)][ECMULT_GEN_PREC_G(ECMULT_GEN_PREC_BITS)] = {\n"); 44 45 for (bits = 2; bits <= 8; bits *= 2) { 46 int g = ECMULT_GEN_PREC_G(bits); 47 int n = ECMULT_GEN_PREC_N(bits); 48 int inner, outer; 49 50 haskellsecp256k1_v0_1_0_ge_storage* table = checked_malloc(&default_error_callback, n * g * sizeof(haskellsecp256k1_v0_1_0_ge_storage)); 51 haskellsecp256k1_v0_1_0_ecmult_gen_compute_table(table, &haskellsecp256k1_v0_1_0_ge_const_g, bits); 52 53 fprintf(fp, "#if ECMULT_GEN_PREC_BITS == %d\n", bits); 54 for(outer = 0; outer != n; outer++) { 55 fprintf(fp,"{"); 56 for(inner = 0; inner != g; inner++) { 57 fprintf(fp, "S(%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32 58 ",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32")", 59 SECP256K1_GE_STORAGE_CONST_GET(table[outer * g + inner])); 60 if (inner != g - 1) { 61 fprintf(fp,",\n"); 62 } 63 } 64 if (outer != n - 1) { 65 fprintf(fp,"},\n"); 66 } else { 67 fprintf(fp,"}\n"); 68 } 69 } 70 fprintf(fp, "#endif\n"); 71 free(table); 72 } 73 74 fprintf(fp, "};\n"); 75 fprintf(fp, "#undef S\n"); 76 fclose(fp); 77 78 return 0; 79 }