csecp256k1

Haskell FFI bindings to bitcoin-core/secp256k1 (docs.ppad.tech/csecp256k1).
git clone git://git.ppad.tech/csecp256k1.git
Log | Files | Refs | README | LICENSE

ecmult_gen_compute_table_impl.h (3528B)


      1 /***********************************************************************
      2  * Copyright (c) 2013, 2014, 2015 Pieter Wuille, Gregory Maxwell       *
      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 #ifndef SECP256K1_ECMULT_GEN_COMPUTE_TABLE_IMPL_H
      8 #define SECP256K1_ECMULT_GEN_COMPUTE_TABLE_IMPL_H
      9 
     10 #include "ecmult_gen_compute_table.h"
     11 #include "group_impl.h"
     12 #include "field_impl.h"
     13 #include "ecmult_gen.h"
     14 #include "util.h"
     15 
     16 static void haskellsecp256k1_v0_1_0_ecmult_gen_compute_table(haskellsecp256k1_v0_1_0_ge_storage* table, const haskellsecp256k1_v0_1_0_ge* gen, int bits) {
     17     int g = ECMULT_GEN_PREC_G(bits);
     18     int n = ECMULT_GEN_PREC_N(bits);
     19 
     20     haskellsecp256k1_v0_1_0_ge* prec = checked_malloc(&default_error_callback, n * g * sizeof(*prec));
     21     haskellsecp256k1_v0_1_0_gej gj;
     22     haskellsecp256k1_v0_1_0_gej nums_gej;
     23     int i, j;
     24 
     25     VERIFY_CHECK(g > 0);
     26     VERIFY_CHECK(n > 0);
     27 
     28     /* get the generator */
     29     haskellsecp256k1_v0_1_0_gej_set_ge(&gj, gen);
     30 
     31     /* Construct a group element with no known corresponding scalar (nothing up my sleeve). */
     32     {
     33         static const unsigned char nums_b32[33] = "The scalar for this x is unknown";
     34         haskellsecp256k1_v0_1_0_fe nums_x;
     35         haskellsecp256k1_v0_1_0_ge nums_ge;
     36         int r;
     37         r = haskellsecp256k1_v0_1_0_fe_set_b32_limit(&nums_x, nums_b32);
     38         (void)r;
     39         VERIFY_CHECK(r);
     40         r = haskellsecp256k1_v0_1_0_ge_set_xo_var(&nums_ge, &nums_x, 0);
     41         (void)r;
     42         VERIFY_CHECK(r);
     43         haskellsecp256k1_v0_1_0_gej_set_ge(&nums_gej, &nums_ge);
     44         /* Add G to make the bits in x uniformly distributed. */
     45         haskellsecp256k1_v0_1_0_gej_add_ge_var(&nums_gej, &nums_gej, gen, NULL);
     46     }
     47 
     48     /* compute prec. */
     49     {
     50         haskellsecp256k1_v0_1_0_gej gbase;
     51         haskellsecp256k1_v0_1_0_gej numsbase;
     52         haskellsecp256k1_v0_1_0_gej* precj = checked_malloc(&default_error_callback, n * g * sizeof(*precj));  /* Jacobian versions of prec. */
     53         gbase = gj; /* PREC_G^j * G */
     54         numsbase = nums_gej; /* 2^j * nums. */
     55         for (j = 0; j < n; j++) {
     56             /* Set precj[j*PREC_G .. j*PREC_G+(PREC_G-1)] to (numsbase, numsbase + gbase, ..., numsbase + (PREC_G-1)*gbase). */
     57             precj[j*g] = numsbase;
     58             for (i = 1; i < g; i++) {
     59                 haskellsecp256k1_v0_1_0_gej_add_var(&precj[j*g + i], &precj[j*g + i - 1], &gbase, NULL);
     60             }
     61             /* Multiply gbase by PREC_G. */
     62             for (i = 0; i < bits; i++) {
     63                 haskellsecp256k1_v0_1_0_gej_double_var(&gbase, &gbase, NULL);
     64             }
     65             /* Multiply numbase by 2. */
     66             haskellsecp256k1_v0_1_0_gej_double_var(&numsbase, &numsbase, NULL);
     67             if (j == n - 2) {
     68                 /* In the last iteration, numsbase is (1 - 2^j) * nums instead. */
     69                 haskellsecp256k1_v0_1_0_gej_neg(&numsbase, &numsbase);
     70                 haskellsecp256k1_v0_1_0_gej_add_var(&numsbase, &numsbase, &nums_gej, NULL);
     71             }
     72         }
     73         haskellsecp256k1_v0_1_0_ge_set_all_gej_var(prec, precj, n * g);
     74         free(precj);
     75     }
     76     for (j = 0; j < n; j++) {
     77         for (i = 0; i < g; i++) {
     78             haskellsecp256k1_v0_1_0_ge_to_storage(&table[j*g + i], &prec[j*g + i]);
     79         }
     80     }
     81     free(prec);
     82 }
     83 
     84 #endif /* SECP256K1_ECMULT_GEN_COMPUTE_TABLE_IMPL_H */