Bitcoin ABC 0.33.3
P2P Digital Currency
ecmult_gen_compute_table_impl.h
Go to the documentation of this file.
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
11#include "group_impl.h"
12#include "field_impl.h"
13#include "ecmult_gen.h"
14#include "util.h"
15
16static void secp256k1_ecmult_gen_compute_table(secp256k1_ge_storage* table, const secp256k1_ge* gen, int bits) {
17 int g = ECMULT_GEN_PREC_G(bits);
18 int n = ECMULT_GEN_PREC_N(bits);
19
20 secp256k1_ge* prec = checked_malloc(&default_error_callback, n * g * sizeof(*prec));
22 secp256k1_gej nums_gej;
23 int i, j;
24
25 if (n < 1) {
26 /* This is unreachable (bits <= 8), but suppresses a -Wmaybe-uninitialized compiler warning */
27 return;
28 }
29
30 /* get the generator */
31 secp256k1_gej_set_ge(&gj, gen);
32
33 /* Construct a group element with no known corresponding scalar (nothing up my sleeve). */
34 {
35 static const unsigned char nums_b32[33] = "The scalar for this x is unknown";
36 secp256k1_fe nums_x;
37 secp256k1_ge nums_ge;
38 int r;
39 r = secp256k1_fe_set_b32_limit(&nums_x, nums_b32);
40 (void)r;
41 VERIFY_CHECK(r);
42 r = secp256k1_ge_set_xo_var(&nums_ge, &nums_x, 0);
43 (void)r;
44 VERIFY_CHECK(r);
45 secp256k1_gej_set_ge(&nums_gej, &nums_ge);
46 /* Add G to make the bits in x uniformly distributed. */
47 secp256k1_gej_add_ge_var(&nums_gej, &nums_gej, gen, NULL);
48 }
49
50 /* compute prec. */
51 {
52 secp256k1_gej gbase;
53 secp256k1_gej numsbase;
54 secp256k1_gej* precj = checked_malloc(&default_error_callback, n * g * sizeof(*precj)); /* Jacobian versions of prec. */
55 gbase = gj; /* PREC_G^j * G */
56 numsbase = nums_gej; /* 2^j * nums. */
57 for (j = 0; j < n; j++) {
58 /* Set precj[j*PREC_G .. j*PREC_G+(PREC_G-1)] to (numsbase, numsbase + gbase, ..., numsbase + (PREC_G-1)*gbase). */
59 precj[j*g] = numsbase;
60 for (i = 1; i < g; i++) {
61 secp256k1_gej_add_var(&precj[j*g + i], &precj[j*g + i - 1], &gbase, NULL);
62 }
63 /* Multiply gbase by PREC_G. */
64 for (i = 0; i < bits; i++) {
65 secp256k1_gej_double_var(&gbase, &gbase, NULL);
66 }
67 /* Multiply numbase by 2. */
68 secp256k1_gej_double_var(&numsbase, &numsbase, NULL);
69 if (j == n - 2) {
70 /* In the last iteration, numsbase is (1 - 2^j) * nums instead. */
71 secp256k1_gej_neg(&numsbase, &numsbase);
72 secp256k1_gej_add_var(&numsbase, &numsbase, &nums_gej, NULL);
73 }
74 }
75 secp256k1_ge_set_all_gej_var(prec, precj, n * g);
76 free(precj);
77 }
78 for (j = 0; j < n; j++) {
79 for (i = 0; i < g; i++) {
80 secp256k1_ge_to_storage(&table[j*g + i], &prec[j*g + i]);
81 }
82 }
83 free(prec);
84}
85
86#endif /* SECP256K1_ECMULT_GEN_COMPUTE_TABLE_IMPL_H */
#define ECMULT_GEN_PREC_G(bits)
Definition: ecmult_gen.h:28
#define ECMULT_GEN_PREC_N(bits)
Definition: ecmult_gen.h:29
static void secp256k1_ecmult_gen_compute_table(secp256k1_ge_storage *table, const secp256k1_ge *gen, int bits)
#define secp256k1_fe_set_b32_limit
Definition: field.h:89
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
Set r equal to the double of a.
static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd)
Set a group element (affine) equal to the point with the given X coordinate, and given oddness for Y.
static void secp256k1_gej_add_ge_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, secp256k1_fe *rzr)
Set r equal to the sum of a and b (with b given in affine coordinates).
static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr)
Set r equal to the sum of a and b.
static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len)
Set a batch of group elements equal to the inputs given in jacobian coordinates.
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Set a group element (jacobian) equal to another which is given in affine coordinates.
static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a)
Convert a group element to the storage type.
static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a)
Set r equal to the inverse of a (i.e., mirrored around the X axis)
static const secp256k1_callback default_error_callback
Definition: util.h:84
#define VERIFY_CHECK(cond)
Definition: util.h:130
static SECP256K1_INLINE void * checked_malloc(const secp256k1_callback *cb, size_t size)
Definition: util.h:134
This field implementation represents the value as 10 uint32_t limbs in base 2^26.
Definition: field_10x26.h:14
A group element in affine coordinates on the secp256k1 curve, or occasionally on an isomorphic curve ...
Definition: group.h:16
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:28