Bitcoin Core  22.99.0
P2P Digital Currency
ecmult_gen_prec_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_PREC_IMPL_H
8 #define SECP256K1_ECMULT_GEN_PREC_IMPL_H
9 
10 #include "ecmult_gen_prec.h"
11 #include "group_impl.h"
12 #include "field_impl.h"
13 #include "ecmult_gen.h"
14 #include "util.h"
15 
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));
21  secp256k1_gej gj;
22  secp256k1_gej nums_gej;
23  int i, j;
24 
25  /* get the generator */
26  secp256k1_gej_set_ge(&gj, gen);
27 
28  /* Construct a group element with no known corresponding scalar (nothing up my sleeve). */
29  {
30  static const unsigned char nums_b32[33] = "The scalar for this x is unknown";
31  secp256k1_fe nums_x;
32  secp256k1_ge nums_ge;
33  int r;
34  r = secp256k1_fe_set_b32(&nums_x, nums_b32);
35  (void)r;
36  VERIFY_CHECK(r);
37  r = secp256k1_ge_set_xo_var(&nums_ge, &nums_x, 0);
38  (void)r;
39  VERIFY_CHECK(r);
40  secp256k1_gej_set_ge(&nums_gej, &nums_ge);
41  /* Add G to make the bits in x uniformly distributed. */
42  secp256k1_gej_add_ge_var(&nums_gej, &nums_gej, gen, NULL);
43  }
44 
45  /* compute prec. */
46  {
47  secp256k1_gej gbase;
48  secp256k1_gej numsbase;
49  secp256k1_gej* precj = checked_malloc(&default_error_callback, n * g * sizeof(*precj)); /* Jacobian versions of prec. */
50  gbase = gj; /* PREC_G^j * G */
51  numsbase = nums_gej; /* 2^j * nums. */
52  for (j = 0; j < n; j++) {
53  /* Set precj[j*PREC_G .. j*PREC_G+(PREC_G-1)] to (numsbase, numsbase + gbase, ..., numsbase + (PREC_G-1)*gbase). */
54  precj[j*g] = numsbase;
55  for (i = 1; i < g; i++) {
56  secp256k1_gej_add_var(&precj[j*g + i], &precj[j*g + i - 1], &gbase, NULL);
57  }
58  /* Multiply gbase by PREC_G. */
59  for (i = 0; i < bits; i++) {
60  secp256k1_gej_double_var(&gbase, &gbase, NULL);
61  }
62  /* Multiply numbase by 2. */
63  secp256k1_gej_double_var(&numsbase, &numsbase, NULL);
64  if (j == n - 2) {
65  /* In the last iteration, numsbase is (1 - 2^j) * nums instead. */
66  secp256k1_gej_neg(&numsbase, &numsbase);
67  secp256k1_gej_add_var(&numsbase, &numsbase, &nums_gej, NULL);
68  }
69  }
70  secp256k1_ge_set_all_gej_var(prec, precj, n * g);
71  free(precj);
72  }
73  for (j = 0; j < n; j++) {
74  for (i = 0; i < g; i++) {
75  secp256k1_ge_to_storage(&table[j*g + i], &prec[j*g + i]);
76  }
77  }
78  free(prec);
79 }
80 
81 #endif /* SECP256K1_ECMULT_GEN_PREC_IMPL_H */
VERIFY_CHECK
#define VERIFY_CHECK(cond)
Definition: util.h:95
field_impl.h
secp256k1_fe_set_b32
static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a)
Set a field element equal to 32-byte big endian value.
group_impl.h
util.h
ecmult_gen_prec.h
secp256k1_gej
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:23
checked_malloc
static SECP256K1_INLINE void * checked_malloc(const secp256k1_callback *cb, size_t size)
Definition: util.h:118
secp256k1_fe
Definition: field_10x26.h:12
secp256k1_ge_storage
Definition: group.h:33
secp256k1_gej_neg
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)
secp256k1_gej_add_var
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.
secp256k1_ge_set_all_gej_var
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.
secp256k1_gej_add_ge_var
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).
secp256k1_ecmult_gen_create_prec_table
static void secp256k1_ecmult_gen_create_prec_table(secp256k1_ge_storage *table, const secp256k1_ge *gen, int bits)
Definition: ecmult_gen_prec_impl.h:16
secp256k1_gej_double_var
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
Set r equal to the double of a.
ECMULT_GEN_PREC_N
#define ECMULT_GEN_PREC_N(bits)
Definition: ecmult_gen.h:17
secp256k1_ge_set_xo_var
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.
ECMULT_GEN_PREC_G
#define ECMULT_GEN_PREC_G(bits)
Definition: ecmult_gen.h:16
secp256k1_ge_to_storage
static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a)
Convert a group element to the storage type.
ecmult_gen.h
ByteUnit::g
@ g
secp256k1_ge
A group element of the secp256k1 curve, in affine coordinates.
Definition: group.h:13
default_error_callback
static const secp256k1_callback default_error_callback
Definition: util.h:49
secp256k1_gej_set_ge
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.