Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
ecmult_gen_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_IMPL_H
8#define SECP256K1_ECMULT_GEN_IMPL_H
9
10#include "util.h"
11#include "scalar.h"
12#include "group.h"
13#include "ecmult_gen.h"
14#include "hash_impl.h"
15#ifdef USE_ECMULT_STATIC_PRECOMPUTATION
16#include "ecmult_static_context.h"
17#endif
18
19#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
21#else
23#endif
24
28
30#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
34 int i, j;
36 void* const base = *prealloc;
37#endif
38
39 if (ctx->prec != NULL) {
40 return;
41 }
42#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
44
45 /* get the generator */
47
48 /* Construct a group element with no known corresponding scalar (nothing up my sleeve). */
49 {
50 static const unsigned char nums_b32[33] = "The scalar for this x is unknown";
53 int r;
55 (void)r;
56 VERIFY_CHECK(r);
58 (void)r;
59 VERIFY_CHECK(r);
61 /* Add G to make the bits in x uniformly distributed. */
63 }
64
65 /* compute prec. */
66 {
67 secp256k1_gej precj[ECMULT_GEN_PREC_N * ECMULT_GEN_PREC_G]; /* Jacobian versions of prec. */
70 gbase = gj; /* PREC_G^j * G */
71 numsbase = nums_gej; /* 2^j * nums. */
72 for (j = 0; j < ECMULT_GEN_PREC_N; j++) {
73 /* Set precj[j*PREC_G .. j*PREC_G+(PREC_G-1)] to (numsbase, numsbase + gbase, ..., numsbase + (PREC_G-1)*gbase). */
75 for (i = 1; i < ECMULT_GEN_PREC_G; i++) {
77 }
78 /* Multiply gbase by PREC_G. */
79 for (i = 0; i < ECMULT_GEN_PREC_B; i++) {
81 }
82 /* Multiply numbase by 2. */
84 if (j == ECMULT_GEN_PREC_N - 2) {
85 /* In the last iteration, numsbase is (1 - 2^j) * nums instead. */
88 }
89 }
91 }
92 for (j = 0; j < ECMULT_GEN_PREC_N; j++) {
93 for (i = 0; i < ECMULT_GEN_PREC_G; i++) {
94 secp256k1_ge_to_storage(&(*ctx->prec)[j][i], &prec[j*ECMULT_GEN_PREC_G + i]);
95 }
96 }
97#else
100#endif
102}
103
107
109#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
110 if (src->prec != NULL) {
111 /* We cast to void* first to suppress a -Wcast-align warning. */
112 dst->prec = (secp256k1_ge_storage (*)[ECMULT_GEN_PREC_N][ECMULT_GEN_PREC_G])(void*)((unsigned char*)dst + ((unsigned char*)src->prec - (unsigned char*)src));
113 }
114#else
115 (void)dst, (void)src;
116#endif
117}
118
124
126 secp256k1_ge add;
129 int bits;
130 int i, j;
131 memset(&adds, 0, sizeof(adds));
132 *r = ctx->initial;
133 /* Blind scalar/point multiplication by computing (n-b)G + bG instead of nG. */
134 secp256k1_scalar_add(&gnb, gn, &ctx->blind);
135 add.infinity = 0;
136 for (j = 0; j < ECMULT_GEN_PREC_N; j++) {
138 for (i = 0; i < ECMULT_GEN_PREC_G; i++) {
149 secp256k1_ge_storage_cmov(&adds, &(*ctx->prec)[j][i], i == bits);
150 }
152 secp256k1_gej_add_ge(r, r, &add);
153 }
154 bits = 0;
155 secp256k1_ge_clear(&add);
157}
158
159/* Setup blinding values for secp256k1_ecmult_gen. */
163 secp256k1_fe s;
164 unsigned char nonce32[32];
166 int overflow;
167 unsigned char keydata[64] = {0};
168 if (seed32 == NULL) {
169 /* When seed is NULL, reset the initial point and blinding value. */
171 secp256k1_gej_neg(&ctx->initial, &ctx->initial);
172 secp256k1_scalar_set_int(&ctx->blind, 1);
173 }
174 /* The prior blinding value (if not reset) is chained forward by including it in the hash. */
180 memcpy(keydata, nonce32, 32);
181 if (seed32 != NULL) {
182 memcpy(keydata + 32, seed32, 32);
183 }
184 secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, seed32 ? 64 : 32);
185 memset(keydata, 0, sizeof(keydata));
186 /* Accept unobservably small non-uniformity. */
191 /* Randomize the projection to defend against multiplier sidechannels. */
192 secp256k1_gej_rescale(&ctx->initial, &s);
196 /* A blinding value of 0 works, but would undermine the projection hardening. */
199 memset(nonce32, 0, 32);
202 ctx->blind = b;
203 ctx->initial = gb;
206}
207
208#endif /* SECP256K1_ECMULT_GEN_IMPL_H */
secp256k1_context * ctx
#define ECMULT_GEN_PREC_G
Definition ecmult_gen.h:17
#define ECMULT_GEN_PREC_B
Definition ecmult_gen.h:16
#define ECMULT_GEN_PREC_N
Definition ecmult_gen.h:18
static void secp256k1_ecmult_gen_context_clear(secp256k1_ecmult_gen_context *ctx)
static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp256k1_gej *r, const secp256k1_scalar *gn)
static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const unsigned char *seed32)
static void secp256k1_ecmult_gen_context_init(secp256k1_ecmult_gen_context *ctx)
static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_context *ctx)
static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx, void **prealloc)
static void secp256k1_ecmult_gen_context_finalize_memcpy(secp256k1_ecmult_gen_context *dst, const secp256k1_ecmult_gen_context *src)
static const size_t SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE
static void secp256k1_fe_clear(secp256k1_fe *a)
Sets a field element equal to zero, initializing all fields.
static void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag)
If flag is true, set *r equal to *a; otherwise leave it.
static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a)
Set a field element equal to 32-byte big endian value.
static int secp256k1_fe_is_zero(const secp256k1_fe *a)
Verify whether a field element is zero.
static const secp256k1_fe secp256k1_fe_one
Definition field_impl.h:143
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 void secp256k1_gej_clear(secp256k1_gej *r)
Clear a secp256k1_gej to prevent leaking sensitive information.
static void secp256k1_ge_clear(secp256k1_ge *r)
Clear a secp256k1_ge to prevent leaking sensitive information.
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_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b)
Set r equal to the sum of a and b (with b given in affine coordinates, and not infinity).
static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage *a)
Convert a group element back from the storage type.
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_gej_rescale(secp256k1_gej *r, const secp256k1_fe *b)
Rescale a jacobian point by b which must be non-zero.
static void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_ge_storage *a, int flag)
If flag is true, set *r equal to *a; otherwise leave it.
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_ge secp256k1_ge_const_g
Generator for secp256k1, value 'g' defined in "Standards for Efficient Cryptography" (SEC2) 2....
Definition group_impl.h:52
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
static void secp256k1_scalar_cmov(secp256k1_scalar *r, const secp256k1_scalar *a, int flag)
If flag is true, set *r equal to *a; otherwise leave it.
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *bin, int *overflow)
Set a scalar from a big endian byte array.
static int secp256k1_scalar_is_zero(const secp256k1_scalar *a)
Check whether a scalar equals zero.
static void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v)
Set a scalar to an unsigned integer.
static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar *a)
Convert a scalar to a byte array.
static unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
Access bits from a scalar.
static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
Add two scalars together (modulo the group order).
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a)
Compute the complement of a scalar (modulo the group order).
static void secp256k1_scalar_clear(secp256k1_scalar *r)
Clear a scalar to prevent the leak of sensitive data.
static const secp256k1_scalar secp256k1_scalar_one
Definition scalar_impl.h:31
static void secp256k1_rfc6979_hmac_sha256_generate(secp256k1_rfc6979_hmac_sha256 *rng, unsigned char *out, size_t outlen)
static void secp256k1_rfc6979_hmac_sha256_initialize(secp256k1_rfc6979_hmac_sha256 *rng, const unsigned char *key, size_t keylen)
static void secp256k1_rfc6979_hmac_sha256_finalize(secp256k1_rfc6979_hmac_sha256 *rng)
static SECP256K1_INLINE void * manual_alloc(void **prealloc_ptr, size_t alloc_size, void *base, size_t max_size)
Definition util.h:134
#define ROUND_TO_ALIGN(size)
Definition util.h:116
#define VERIFY_CHECK(cond)
Definition util.h:68
secp256k1_ge_storage(* prec)[ECMULT_GEN_PREC_N][ECMULT_GEN_PREC_G]
Definition ecmult_gen.h:33
A group element of the secp256k1 curve, in affine coordinates.
Definition group.h:13
int infinity
Definition group.h:16
A group element of the secp256k1 curve, in jacobian coordinates.
Definition group.h:23
A scalar modulo the group order of the secp256k1 curve.
Definition scalar_4x64.h:13