Bitcoin ABC 0.33.5
P2P Digital Currency
main_impl.h
Go to the documentation of this file.
1/***********************************************************************
2 * Distributed under the MIT software license, see the accompanying *
3 * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
4 ***********************************************************************/
5
6#ifndef SECP256K1_MODULE_ELLSWIFT_MAIN_H
7#define SECP256K1_MODULE_ELLSWIFT_MAIN_H
8
9#include "../../../include/secp256k1.h"
10#include "../../../include/secp256k1_ellswift.h"
11#include "../../eckey.h"
12#include "../../hash.h"
13
15static const secp256k1_fe secp256k1_ellswift_c1 = SECP256K1_FE_CONST(0x851695d4, 0x9a83f8ef, 0x919bb861, 0x53cbcb16, 0x630fb68a, 0xed0a766a, 0x3ec693d6, 0x8e6afa40);
17static const secp256k1_fe secp256k1_ellswift_c2 = SECP256K1_FE_CONST(0x7ae96a2b, 0x657c0710, 0x6e64479e, 0xac3434e9, 0x9cf04975, 0x12f58995, 0xc1396c28, 0x719501ee);
19static const secp256k1_fe secp256k1_ellswift_c3 = SECP256K1_FE_CONST(0x7ae96a2b, 0x657c0710, 0x6e64479e, 0xac3434e9, 0x9cf04975, 0x12f58995, 0xc1396c28, 0x719501ef);
21static const secp256k1_fe secp256k1_ellswift_c4 = SECP256K1_FE_CONST(0x851695d4, 0x9a83f8ef, 0x919bb861, 0x53cbcb16, 0x630fb68a, 0xed0a766a, 0x3ec693d6, 0x8e6afa41);
22
25 /* The implemented algorithm is the following (all operations in GF(p)):
26 *
27 * - Let c0 = sqrt(-3) = 0xa2d2ba93507f1df233770c2a797962cc61f6d15da14ecd47d8d27ae1cd5f852.
28 * - If u = 0, set u = 1.
29 * - If t = 0, set t = 1.
30 * - If u^3+7+t^2 = 0, set t = 2*t.
31 * - Let X = (u^3+7-t^2)/(2*t).
32 * - Let Y = (X+t)/(c0*u).
33 * - If x3 = u+4*Y^2 is a valid x coordinate, return it.
34 * - If x2 = (-X/Y-u)/2 is a valid x coordinate, return it.
35 * - Return x1 = (X/Y-u)/2 (which is now guaranteed to be a valid x coordinate).
36 *
37 * Introducing s=t^2, g=u^3+7, and simplifying x1=-(x2+u) we get:
38 *
39 * - Let c0 = ...
40 * - If u = 0, set u = 1.
41 * - If t = 0, set t = 1.
42 * - Let s = t^2
43 * - Let g = u^3+7
44 * - If g+s = 0, set t = 2*t, s = 4*s
45 * - Let X = (g-s)/(2*t).
46 * - Let Y = (X+t)/(c0*u) = (g+s)/(2*c0*t*u).
47 * - If x3 = u+4*Y^2 is a valid x coordinate, return it.
48 * - If x2 = (-X/Y-u)/2 is a valid x coordinate, return it.
49 * - Return x1 = -(x2+u).
50 *
51 * Now substitute Y^2 = -(g+s)^2/(12*s*u^2) and X/Y = c0*u*(g-s)/(g+s). This
52 * means X and Y do not need to be evaluated explicitly anymore.
53 *
54 * - ...
55 * - If g+s = 0, set s = 4*s.
56 * - If x3 = u-(g+s)^2/(3*s*u^2) is a valid x coordinate, return it.
57 * - If x2 = (-c0*u*(g-s)/(g+s)-u)/2 is a valid x coordinate, return it.
58 * - Return x1 = -(x2+u).
59 *
60 * Simplifying x2 using 2 additional constants:
61 *
62 * - Let c1 = (c0-1)/2 = 0x851695d49a83f8ef919bb86153cbcb16630fb68aed0a766a3ec693d68e6afa40.
63 * - Let c2 = (-c0-1)/2 = 0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee.
64 * - ...
65 * - If x2 = u*(c1*s+c2*g)/(g+s) is a valid x coordinate, return it.
66 * - ...
67 *
68 * Writing x3 as a fraction:
69 *
70 * - ...
71 * - If x3 = (3*s*u^3-(g+s)^2)/(3*s*u^2) ...
72 * - ...
73
74 * Overall, we get:
75 *
76 * - Let c1 = 0x851695d49a83f8ef919bb86153cbcb16630fb68aed0a766a3ec693d68e6afa40.
77 * - Let c2 = 0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee.
78 * - If u = 0, set u = 1.
79 * - If t = 0, set s = 1, else set s = t^2.
80 * - Let g = u^3+7.
81 * - If g+s = 0, set s = 4*s.
82 * - If x3 = (3*s*u^3-(g+s)^2)/(3*s*u^2) is a valid x coordinate, return it.
83 * - If x2 = u*(c1*s+c2*g)/(g+s) is a valid x coordinate, return it.
84 * - Return x1 = -(x2+u).
85 */
86 secp256k1_fe u1, s, g, p, d, n, l;
87 u1 = *u;
89 secp256k1_fe_sqr(&s, t);
91 secp256k1_fe_sqr(&l, &u1); /* l = u^2 */
92 secp256k1_fe_mul(&g, &l, &u1); /* g = u^3 */
93 secp256k1_fe_add_int(&g, SECP256K1_B); /* g = u^3 + 7 */
94 p = g; /* p = g */
95 secp256k1_fe_add(&p, &s); /* p = g+s */
98 /* Recompute p = g+s */
99 p = g; /* p = g */
100 secp256k1_fe_add(&p, &s); /* p = g+s */
101 }
102 secp256k1_fe_mul(&d, &s, &l); /* d = s*u^2 */
103 secp256k1_fe_mul_int(&d, 3); /* d = 3*s*u^2 */
104 secp256k1_fe_sqr(&l, &p); /* l = (g+s)^2 */
105 secp256k1_fe_negate(&l, &l, 1); /* l = -(g+s)^2 */
106 secp256k1_fe_mul(&n, &d, &u1); /* n = 3*s*u^3 */
107 secp256k1_fe_add(&n, &l); /* n = 3*s*u^3-(g+s)^2 */
109 /* Return x3 = n/d = (3*s*u^3-(g+s)^2)/(3*s*u^2) */
110 *xn = n;
111 *xd = d;
112 return;
113 }
114 *xd = p;
115 secp256k1_fe_mul(&l, &secp256k1_ellswift_c1, &s); /* l = c1*s */
116 secp256k1_fe_mul(&n, &secp256k1_ellswift_c2, &g); /* n = c2*g */
117 secp256k1_fe_add(&n, &l); /* n = c1*s+c2*g */
118 secp256k1_fe_mul(&n, &n, &u1); /* n = u*(c1*s+c2*g) */
119 /* Possible optimization: in the invocation below, p^2 = (g+s)^2 is computed,
120 * which we already have computed above. This could be deduplicated. */
122 /* Return x2 = n/p = u*(c1*s+c2*g)/(g+s) */
123 *xn = n;
124 return;
125 }
126 secp256k1_fe_mul(&l, &p, &u1); /* l = u*(g+s) */
127 secp256k1_fe_add(&n, &l); /* n = u*(c1*s+c2*g)+u*(g+s) */
128 secp256k1_fe_negate(xn, &n, 2); /* n = -u*(c1*s+c2*g)-u*(g+s) */
129#ifdef VERIFY
131#endif
132 /* Return x3 = n/p = -(u*(c1*s+c2*g)/(g+s)+u) */
133}
134
137 secp256k1_fe xn, xd;
139 secp256k1_fe_inv_var(&xd, &xd);
140 secp256k1_fe_mul(x, &xn, &xd);
141}
142
145 secp256k1_fe x;
148}
149
150/* Try to complete an ElligatorSwift encoding (u, t) for X coordinate x, given u and x.
151 *
152 * There may be up to 8 distinct t values such that (u, t) decodes back to x, but also
153 * fewer, or none at all. Each such partial inverse can be accessed individually using a
154 * distinct input argument c (in range 0-7), and some or all of these may return failure.
155 * The following guarantees exist:
156 * - Given (x, u), no two distinct c values give the same successful result t.
157 * - Every successful result maps back to x through secp256k1_ellswift_xswiftec_var.
158 * - Given (x, u), all t values that map back to x can be reached by combining the
159 * successful results from this function over all c values, with the exception of:
160 * - this function cannot be called with u=0
161 * - no result with t=0 will be returned
162 * - no result for which u^3 + t^2 + 7 = 0 will be returned.
163 *
164 * The rather unusual encoding of bits in c (a large "if" based on the middle bit, and then
165 * using the low and high bits to pick signs of square roots) is to match the paper's
166 * encoding more closely: c=0 through c=3 match branches 1..4 in the paper, while c=4 through
167 * c=7 are copies of those with an additional negation of sqrt(w).
168 */
169static int secp256k1_ellswift_xswiftec_inv_var(secp256k1_fe *t, const secp256k1_fe *x_in, const secp256k1_fe *u_in, int c) {
170 /* The implemented algorithm is this (all arithmetic, except involving c, is mod p):
171 *
172 * - If (c & 2) = 0:
173 * - If (-x-u) is a valid X coordinate, fail.
174 * - Let s=-(u^3+7)/(u^2+u*x+x^2).
175 * - If s is not square, fail.
176 * - Let v=x.
177 * - If (c & 2) = 2:
178 * - Let s=x-u.
179 * - If s is not square, fail.
180 * - Let r=sqrt(-s*(4*(u^3+7)+3*u^2*s)); fail if it doesn't exist.
181 * - If (c & 1) = 1 and r = 0, fail.
182 * - If s=0, fail.
183 * - Let v=(r/s-u)/2.
184 * - Let w=sqrt(s).
185 * - If (c & 5) = 0: return -w*(c3*u + v).
186 * - If (c & 5) = 1: return w*(c4*u + v).
187 * - If (c & 5) = 4: return w*(c3*u + v).
188 * - If (c & 5) = 5: return -w*(c4*u + v).
189 */
190 secp256k1_fe x = *x_in, u = *u_in, g, v, s, m, r, q;
191 int ret;
192
195
196#ifdef VERIFY
197 VERIFY_CHECK(c >= 0 && c < 8);
199#endif
200
201 if (!(c & 2)) {
202 /* c is in {0, 1, 4, 5}. In this case we look for an inverse under the x1 (if c=0 or
203 * c=4) formula, or x2 (if c=1 or c=5) formula. */
204
205 /* If -u-x is a valid X coordinate, fail. This would yield an encoding that roundtrips
206 * back under the x3 formula instead (which has priority over x1 and x2, so the decoding
207 * would not match x). */
208 m = x; /* m = x */
209 secp256k1_fe_add(&m, &u); /* m = u+x */
210 secp256k1_fe_negate(&m, &m, 2); /* m = -u-x */
211 /* Test if (-u-x) is a valid X coordinate. If so, fail. */
212 if (secp256k1_ge_x_on_curve_var(&m)) return 0;
213
214 /* Let s = -(u^3 + 7)/(u^2 + u*x + x^2) [first part] */
215 secp256k1_fe_sqr(&s, &m); /* s = (u+x)^2 */
216 secp256k1_fe_negate(&s, &s, 1); /* s = -(u+x)^2 */
217 secp256k1_fe_mul(&m, &u, &x); /* m = u*x */
218 secp256k1_fe_add(&s, &m); /* s = -(u^2 + u*x + x^2) */
219
220 /* Note that at this point, s = 0 is impossible. If it were the case:
221 * s = -(u^2 + u*x + x^2) = 0
222 * => u^2 + u*x + x^2 = 0
223 * => (u + 2*x) * (u^2 + u*x + x^2) = 0
224 * => 2*x^3 + 3*x^2*u + 3*x*u^2 + u^3 = 0
225 * => (x + u)^3 + x^3 = 0
226 * => x^3 = -(x + u)^3
227 * => x^3 + B = (-u - x)^3 + B
228 *
229 * However, we know x^3 + B is square (because x is on the curve) and
230 * that (-u-x)^3 + B is not square (the secp256k1_ge_x_on_curve_var(&m)
231 * test above would have failed). This is a contradiction, and thus the
232 * assumption s=0 is false. */
233#ifdef VERIFY
235#endif
236
237 /* If s is not square, fail. We have not fully computed s yet, but s is square iff
238 * -(u^3+7)*(u^2+u*x+x^2) is square (because a/b is square iff a*b is square and b is
239 * nonzero). */
240 secp256k1_fe_sqr(&g, &u); /* g = u^2 */
241 secp256k1_fe_mul(&g, &g, &u); /* g = u^3 */
242 secp256k1_fe_add_int(&g, SECP256K1_B); /* g = u^3+7 */
243 secp256k1_fe_mul(&m, &s, &g); /* m = -(u^3 + 7)*(u^2 + u*x + x^2) */
244 if (!secp256k1_fe_is_square_var(&m)) return 0;
245
246 /* Let s = -(u^3 + 7)/(u^2 + u*x + x^2) [second part] */
247 secp256k1_fe_inv_var(&s, &s); /* s = -1/(u^2 + u*x + x^2) [no div by 0] */
248 secp256k1_fe_mul(&s, &s, &g); /* s = -(u^3 + 7)/(u^2 + u*x + x^2) */
249
250 /* Let v = x. */
251 v = x;
252 } else {
253 /* c is in {2, 3, 6, 7}. In this case we look for an inverse under the x3 formula. */
254
255 /* Let s = x-u. */
256 secp256k1_fe_negate(&m, &u, 1); /* m = -u */
257 s = m; /* s = -u */
258 secp256k1_fe_add(&s, &x); /* s = x-u */
259
260 /* If s is not square, fail. */
261 if (!secp256k1_fe_is_square_var(&s)) return 0;
262
263 /* Let r = sqrt(-s*(4*(u^3+7)+3*u^2*s)); fail if it doesn't exist. */
264 secp256k1_fe_sqr(&g, &u); /* g = u^2 */
265 secp256k1_fe_mul(&q, &s, &g); /* q = s*u^2 */
266 secp256k1_fe_mul_int(&q, 3); /* q = 3*s*u^2 */
267 secp256k1_fe_mul(&g, &g, &u); /* g = u^3 */
268 secp256k1_fe_mul_int(&g, 4); /* g = 4*u^3 */
269 secp256k1_fe_add_int(&g, 4 * SECP256K1_B); /* g = 4*(u^3+7) */
270 secp256k1_fe_add(&q, &g); /* q = 4*(u^3+7)+3*s*u^2 */
271 secp256k1_fe_mul(&q, &q, &s); /* q = s*(4*(u^3+7)+3*u^2*s) */
272 secp256k1_fe_negate(&q, &q, 1); /* q = -s*(4*(u^3+7)+3*u^2*s) */
273 if (!secp256k1_fe_is_square_var(&q)) return 0;
274 ret = secp256k1_fe_sqrt(&r, &q); /* r = sqrt(-s*(4*(u^3+7)+3*u^2*s)) */
275 VERIFY_CHECK(ret);
276
277 /* If (c & 1) = 1 and r = 0, fail. */
278 if (EXPECT((c & 1) && secp256k1_fe_normalizes_to_zero_var(&r), 0)) return 0;
279
280 /* If s = 0, fail. */
281 if (EXPECT(secp256k1_fe_normalizes_to_zero_var(&s), 0)) return 0;
282
283 /* Let v = (r/s-u)/2. */
284 secp256k1_fe_inv_var(&v, &s); /* v = 1/s [no div by 0] */
285 secp256k1_fe_mul(&v, &v, &r); /* v = r/s */
286 secp256k1_fe_add(&v, &m); /* v = r/s-u */
287 secp256k1_fe_half(&v); /* v = (r/s-u)/2 */
288 }
289
290 /* Let w = sqrt(s). */
291 ret = secp256k1_fe_sqrt(&m, &s); /* m = sqrt(s) = w */
292 VERIFY_CHECK(ret);
293
294 /* Return logic. */
295 if ((c & 5) == 0 || (c & 5) == 5) {
296 secp256k1_fe_negate(&m, &m, 1); /* m = -w */
297 }
298 /* Now m = {-w if c&5=0 or c&5=5; w otherwise}. */
300 /* u = {c4 if c&1=1; c3 otherwise}*u */
301 secp256k1_fe_add(&u, &v); /* u = {c4 if c&1=1; c3 otherwise}*u + v */
302 secp256k1_fe_mul(t, &m, &u);
303 return 1;
304}
305
311static void secp256k1_ellswift_prng(unsigned char* out32, const secp256k1_sha256 *hasher, uint32_t cnt) {
312 secp256k1_sha256 hash = *hasher;
313 unsigned char buf4[4];
314#ifdef VERIFY
315 size_t blocks = hash.bytes >> 6;
316#endif
317 buf4[0] = cnt;
318 buf4[1] = cnt >> 8;
319 buf4[2] = cnt >> 16;
320 buf4[3] = cnt >> 24;
321 secp256k1_sha256_write(&hash, buf4, 4);
322 secp256k1_sha256_finalize(&hash, out32);
323#ifdef VERIFY
324 /* Writing and finalizing together should trigger exactly one SHA256 compression. */
325 VERIFY_CHECK(((hash.bytes) >> 6) == (blocks + 1));
326#endif
327}
328
335static void secp256k1_ellswift_xelligatorswift_var(unsigned char *u32, secp256k1_fe *t, const secp256k1_fe *x, const secp256k1_sha256 *hasher) {
336 /* Pool of 3-bit branch values. */
337 unsigned char branch_hash[32];
338 /* Number of 3-bit values in branch_hash left. */
339 int branches_left = 0;
340 /* Field elements u and branch values are extracted from RNG based on hasher for consecutive
341 * values of cnt. cnt==0 is first used to populate a pool of 64 4-bit branch values. The 64
342 * cnt values that follow are used to generate field elements u. cnt==65 (and multiples
343 * thereof) are used to repopulate the pool and start over, if that were ever necessary.
344 * On average, 4 iterations are needed. */
345 uint32_t cnt = 0;
346 while (1) {
347 int branch;
348 secp256k1_fe u;
349 /* If the pool of branch values is empty, populate it. */
350 if (branches_left == 0) {
351 secp256k1_ellswift_prng(branch_hash, hasher, cnt++);
352 branches_left = 64;
353 }
354 /* Take a 3-bit branch value from the branch pool (top bit is discarded). */
355 --branches_left;
356 branch = (branch_hash[branches_left >> 1] >> ((branches_left & 1) << 2)) & 7;
357 /* Compute a new u value by hashing. */
358 secp256k1_ellswift_prng(u32, hasher, cnt++);
359 /* overflow is not a problem (we prefer uniform u32 over uniform u). */
361 /* Since u is the output of a hash, it should practically never be 0. We could apply the
362 * u=0 to u=1 correction here too to deal with that case still, but it's such a low
363 * probability event that we do not bother. */
364#ifdef VERIFY
366#endif
367 /* Find a remainder t, and return it if found. */
368 if (EXPECT(secp256k1_ellswift_xswiftec_inv_var(t, x, &u, branch), 0)) break;
369 }
370}
371
378static void secp256k1_ellswift_elligatorswift_var(unsigned char *u32, secp256k1_fe *t, const secp256k1_ge *p, const secp256k1_sha256 *hasher) {
379 secp256k1_ellswift_xelligatorswift_var(u32, t, &p->x, hasher);
382 secp256k1_fe_negate(t, t, 1);
384 }
385}
386
390 hash->s[0] = 0xd1a6524bul;
391 hash->s[1] = 0x028594b3ul;
392 hash->s[2] = 0x96e42f4eul;
393 hash->s[3] = 0x1037a177ul;
394 hash->s[4] = 0x1b8fcb8bul;
395 hash->s[5] = 0x56023885ul;
396 hash->s[6] = 0x2560ede1ul;
397 hash->s[7] = 0xd626b715ul;
398
399 hash->bytes = 64;
400}
401
402int secp256k1_ellswift_encode(const secp256k1_context *ctx, unsigned char *ell64, const secp256k1_pubkey *pubkey, const unsigned char *rnd32) {
403 secp256k1_ge p;
404 VERIFY_CHECK(ctx != NULL);
405 ARG_CHECK(ell64 != NULL);
406 ARG_CHECK(pubkey != NULL);
407 ARG_CHECK(rnd32 != NULL);
408
409 if (secp256k1_pubkey_load(ctx, &p, pubkey)) {
410 secp256k1_fe t;
411 unsigned char p64[64] = {0};
412 size_t ser_size;
413 int ser_ret;
414 secp256k1_sha256 hash;
415
416 /* Set up hasher state; the used RNG is H(pubkey || "\x00"*31 || rnd32 || cnt++), using
417 * BIP340 tagged hash with tag "secp256k1_ellswift_encode". */
419 ser_ret = secp256k1_eckey_pubkey_serialize(&p, p64, &ser_size, 1);
420 VERIFY_CHECK(ser_ret && ser_size == 33);
421 secp256k1_sha256_write(&hash, p64, sizeof(p64));
422 secp256k1_sha256_write(&hash, rnd32, 32);
423
424 /* Compute ElligatorSwift encoding and construct output. */
425 secp256k1_ellswift_elligatorswift_var(ell64, &t, &p, &hash); /* puts u in ell64[0..32] */
426 secp256k1_fe_get_b32(ell64 + 32, &t); /* puts t in ell64[32..64] */
427 return 1;
428 }
429 /* Only reached in case the provided pubkey is invalid. */
430 memset(ell64, 0, 64);
431 return 0;
432}
433
437 hash->s[0] = 0xd29e1bf5ul;
438 hash->s[1] = 0xf7025f42ul;
439 hash->s[2] = 0x9b024773ul;
440 hash->s[3] = 0x094cb7d5ul;
441 hash->s[4] = 0xe59ed789ul;
442 hash->s[5] = 0x03bc9786ul;
443 hash->s[6] = 0x68335b35ul;
444 hash->s[7] = 0x4e363b53ul;
445
446 hash->bytes = 64;
447}
448
449int secp256k1_ellswift_create(const secp256k1_context *ctx, unsigned char *ell64, const unsigned char *seckey32, const unsigned char *auxrnd32) {
450 secp256k1_ge p;
451 secp256k1_fe t;
452 secp256k1_sha256 hash;
453 secp256k1_scalar seckey_scalar;
454 int ret;
455 static const unsigned char zero32[32] = {0};
456
457 /* Sanity check inputs. */
458 VERIFY_CHECK(ctx != NULL);
459 ARG_CHECK(ell64 != NULL);
460 memset(ell64, 0, 64);
462 ARG_CHECK(seckey32 != NULL);
463
464 /* Compute (affine) public key */
465 ret = secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &seckey_scalar, &p, seckey32);
466 secp256k1_declassify(ctx, &p, sizeof(p)); /* not constant time in produced pubkey */
469
470 /* Set up hasher state. The used RNG is H(privkey || "\x00"*32 [|| auxrnd32] || cnt++),
471 * using BIP340 tagged hash with tag "secp256k1_ellswift_create". */
473 secp256k1_sha256_write(&hash, seckey32, 32);
474 secp256k1_sha256_write(&hash, zero32, sizeof(zero32));
475 secp256k1_declassify(ctx, &hash, sizeof(hash)); /* private key is hashed now */
476 if (auxrnd32) secp256k1_sha256_write(&hash, auxrnd32, 32);
477
478 /* Compute ElligatorSwift encoding and construct output. */
479 secp256k1_ellswift_elligatorswift_var(ell64, &t, &p, &hash); /* puts u in ell64[0..32] */
480 secp256k1_fe_get_b32(ell64 + 32, &t); /* puts t in ell64[32..64] */
481
482 secp256k1_memczero(ell64, 64, !ret);
483 secp256k1_scalar_clear(&seckey_scalar);
484
485 return ret;
486}
487
488int secp256k1_ellswift_decode(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *ell64) {
489 secp256k1_fe u, t;
490 secp256k1_ge p;
491 VERIFY_CHECK(ctx != NULL);
492 ARG_CHECK(pubkey != NULL);
493 ARG_CHECK(ell64 != NULL);
494
495 secp256k1_fe_set_b32_mod(&u, ell64);
496 secp256k1_fe_set_b32_mod(&t, ell64 + 32);
499 secp256k1_pubkey_save(pubkey, &p);
500 return 1;
501}
502
503static int ellswift_xdh_hash_function_prefix(unsigned char *output, const unsigned char *x32, const unsigned char *ell_a64, const unsigned char *ell_b64, void *data) {
505
507 secp256k1_sha256_write(&sha, data, 64);
508 secp256k1_sha256_write(&sha, ell_a64, 64);
509 secp256k1_sha256_write(&sha, ell_b64, 64);
510 secp256k1_sha256_write(&sha, x32, 32);
511 secp256k1_sha256_finalize(&sha, output);
512
513 return 1;
514}
515
519 hash->s[0] = 0x8c12d730ul;
520 hash->s[1] = 0x827bd392ul;
521 hash->s[2] = 0x9e4fb2eeul;
522 hash->s[3] = 0x207b373eul;
523 hash->s[4] = 0x2292bd7aul;
524 hash->s[5] = 0xaa5441bcul;
525 hash->s[6] = 0x15c3779ful;
526 hash->s[7] = 0xcfb52549ul;
527
528 hash->bytes = 64;
529}
530
531static int ellswift_xdh_hash_function_bip324(unsigned char* output, const unsigned char *x32, const unsigned char *ell_a64, const unsigned char *ell_b64, void *data) {
533
534 (void)data;
535
537 secp256k1_sha256_write(&sha, ell_a64, 64);
538 secp256k1_sha256_write(&sha, ell_b64, 64);
539 secp256k1_sha256_write(&sha, x32, 32);
540 secp256k1_sha256_finalize(&sha, output);
541
542 return 1;
543}
544
547
548int secp256k1_ellswift_xdh(const secp256k1_context *ctx, unsigned char *output, const unsigned char *ell_a64, const unsigned char *ell_b64, const unsigned char *seckey32, int party, secp256k1_ellswift_xdh_hash_function hashfp, void *data) {
549 int ret = 0;
550 int overflow;
552 secp256k1_fe xn, xd, px, u, t;
553 unsigned char sx[32];
554 const unsigned char* theirs64;
555
556 VERIFY_CHECK(ctx != NULL);
557 ARG_CHECK(output != NULL);
558 ARG_CHECK(ell_a64 != NULL);
559 ARG_CHECK(ell_b64 != NULL);
560 ARG_CHECK(seckey32 != NULL);
561 ARG_CHECK(hashfp != NULL);
562
563 /* Load remote public key (as fraction). */
564 theirs64 = party ? ell_a64 : ell_b64;
565 secp256k1_fe_set_b32_mod(&u, theirs64);
566 secp256k1_fe_set_b32_mod(&t, theirs64 + 32);
567 secp256k1_ellswift_xswiftec_frac_var(&xn, &xd, &u, &t);
568
569 /* Load private key (using one if invalid). */
570 secp256k1_scalar_set_b32(&s, seckey32, &overflow);
571 overflow = secp256k1_scalar_is_zero(&s);
573
574 /* Compute shared X coordinate. */
575 secp256k1_ecmult_const_xonly(&px, &xn, &xd, &s, 1);
577 secp256k1_fe_get_b32(sx, &px);
578
579 /* Invoke hasher */
580 ret = hashfp(output, sx, ell_a64, ell_b64, data);
581
582 memset(sx, 0, 32);
585
586 return !!ret & !overflow;
587}
588
589#endif
static int secp256k1_eckey_pubkey_serialize(secp256k1_ge *elem, unsigned char *pub, size_t *size, int compressed)
static int secp256k1_ecmult_const_xonly(secp256k1_fe *r, const secp256k1_fe *n, const secp256k1_fe *d, const secp256k1_scalar *q, int known_on_curve)
Same as secp256k1_ecmult_const, but takes in an x coordinate of the base point only,...
static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_context *ctx)
static void secp256k1_ellswift_swiftec_var(secp256k1_ge *p, const secp256k1_fe *u, const secp256k1_fe *t)
Decode ElligatorSwift encoding (u, t) to point P.
Definition: main_impl.h:144
const secp256k1_ellswift_xdh_hash_function secp256k1_ellswift_xdh_hash_function_prefix
Definition: main_impl.h:545
int secp256k1_ellswift_create(const secp256k1_context *ctx, unsigned char *ell64, const unsigned char *seckey32, const unsigned char *auxrnd32)
Compute an ElligatorSwift public key for a secret key.
Definition: main_impl.h:449
int secp256k1_ellswift_xdh(const secp256k1_context *ctx, unsigned char *output, const unsigned char *ell_a64, const unsigned char *ell_b64, const unsigned char *seckey32, int party, secp256k1_ellswift_xdh_hash_function hashfp, void *data)
Given a private key, and ElligatorSwift public keys sent in both directions, compute a shared secret ...
Definition: main_impl.h:548
static const secp256k1_fe secp256k1_ellswift_c3
c3 = (-sqrt(-3)+1)/2 = -c1 = c2+1
Definition: main_impl.h:19
static const secp256k1_fe secp256k1_ellswift_c4
c4 = (sqrt(-3)+1)/2 = -c2 = c1+1
Definition: main_impl.h:21
static void secp256k1_ellswift_xswiftec_var(secp256k1_fe *x, const secp256k1_fe *u, const secp256k1_fe *t)
Decode ElligatorSwift encoding (u, t) to X coordinate.
Definition: main_impl.h:136
static void secp256k1_ellswift_sha256_init_create(secp256k1_sha256 *hash)
Set hash state to the BIP340 tagged hash midstate for "secp256k1_ellswift_create".
Definition: main_impl.h:435
static const secp256k1_fe secp256k1_ellswift_c2
c2 = (-sqrt(-3)-1)/2 = -(c1+1)
Definition: main_impl.h:17
static void secp256k1_ellswift_xelligatorswift_var(unsigned char *u32, secp256k1_fe *t, const secp256k1_fe *x, const secp256k1_sha256 *hasher)
Find an ElligatorSwift encoding (u, t) for X coordinate x, and random Y coordinate.
Definition: main_impl.h:335
static int ellswift_xdh_hash_function_prefix(unsigned char *output, const unsigned char *x32, const unsigned char *ell_a64, const unsigned char *ell_b64, void *data)
Definition: main_impl.h:503
const secp256k1_ellswift_xdh_hash_function secp256k1_ellswift_xdh_hash_function_bip324
Definition: main_impl.h:546
static void secp256k1_ellswift_xswiftec_frac_var(secp256k1_fe *xn, secp256k1_fe *xd, const secp256k1_fe *u, const secp256k1_fe *t)
Decode ElligatorSwift encoding (u, t) to a fraction xn/xd representing a curve X coordinate.
Definition: main_impl.h:24
static void secp256k1_ellswift_elligatorswift_var(unsigned char *u32, secp256k1_fe *t, const secp256k1_ge *p, const secp256k1_sha256 *hasher)
Find an ElligatorSwift encoding (u, t) for point P.
Definition: main_impl.h:378
static int secp256k1_ellswift_xswiftec_inv_var(secp256k1_fe *t, const secp256k1_fe *x_in, const secp256k1_fe *u_in, int c)
Definition: main_impl.h:169
static void secp256k1_ellswift_sha256_init_bip324(secp256k1_sha256 *hash)
Set hash state to the BIP340 tagged hash midstate for "bip324_ellswift_xonly_ecdh".
Definition: main_impl.h:517
static int ellswift_xdh_hash_function_bip324(unsigned char *output, const unsigned char *x32, const unsigned char *ell_a64, const unsigned char *ell_b64, void *data)
Definition: main_impl.h:531
static void secp256k1_ellswift_sha256_init_encode(secp256k1_sha256 *hash)
Set hash state to the BIP340 tagged hash midstate for "secp256k1_ellswift_encode".
Definition: main_impl.h:388
static void secp256k1_ellswift_prng(unsigned char *out32, const secp256k1_sha256 *hasher, uint32_t cnt)
Use SHA256 as a PRNG, returning SHA256(hasher || cnt).
Definition: main_impl.h:311
int secp256k1_ellswift_encode(const secp256k1_context *ctx, unsigned char *ell64, const secp256k1_pubkey *pubkey, const unsigned char *rnd32)
Construct a 64-byte ElligatorSwift encoding of a given pubkey.
Definition: main_impl.h:402
int secp256k1_ellswift_decode(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *ell64)
Decode a 64-bytes ElligatorSwift encoded public key.
Definition: main_impl.h:488
static const secp256k1_fe secp256k1_ellswift_c1
c1 = (sqrt(-3)-1)/2
Definition: main_impl.h:15
#define secp256k1_fe_negate(r, a, m)
Negate a field element.
Definition: field.h:221
#define secp256k1_fe_mul_int(r, a)
Multiply a field element with a small integer.
Definition: field.h:243
#define secp256k1_fe_normalizes_to_zero_var
Definition: field.h:82
#define secp256k1_fe_normalize_weak
Definition: field.h:79
#define secp256k1_fe_is_odd
Definition: field.h:86
#define secp256k1_fe_mul
Definition: field.h:94
static const secp256k1_fe secp256k1_fe_one
Definition: field.h:68
static int secp256k1_fe_sqrt(secp256k1_fe *SECP256K1_RESTRICT r, const secp256k1_fe *SECP256K1_RESTRICT a)
Compute a square root of a field element.
#define secp256k1_fe_add
Definition: field.h:93
#define secp256k1_fe_clear
Definition: field.h:84
#define secp256k1_fe_normalize_var
Definition: field.h:80
#define secp256k1_fe_half
Definition: field.h:102
#define SECP256K1_FE_CONST(d7, d6, d5, d4, d3, d2, d1, d0)
This expands to an initializer for a secp256k1_fe valued sum((i*32) * d_i, i=0..7) mod p.
Definition: field.h:66
#define secp256k1_fe_inv_var
Definition: field.h:100
#define secp256k1_fe_is_square_var
Definition: field.h:104
#define secp256k1_fe_set_b32_mod
Definition: field.h:88
#define secp256k1_fe_get_b32
Definition: field.h:90
#define secp256k1_fe_sqr
Definition: field.h:95
#define secp256k1_fe_normalize
Definition: field.h:78
#define secp256k1_fe_add_int
Definition: field.h:103
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 int secp256k1_ge_x_on_curve_var(const secp256k1_fe *x)
Determine whether x is a valid X coordinate on the curve.
static int secp256k1_ge_x_frac_on_curve_var(const secp256k1_fe *xn, const secp256k1_fe *xd)
Determine whether fraction xn/xd is a valid X coordinate on the curve (xd != 0).
#define SECP256K1_B
Definition: group_impl.h:71
secp256k1_context * ctx
Definition: bench_impl.h:13
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_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:27
static void secp256k1_sha256_initialize(secp256k1_sha256 *hash)
static void secp256k1_sha256_finalize(secp256k1_sha256 *hash, unsigned char *out32)
static void secp256k1_sha256_write(secp256k1_sha256 *hash, const unsigned char *data, size_t size)
#define EXPECT(x, c)
Definition: util.h:118
#define VERIFY_CHECK(cond)
Definition: util.h:143
static SECP256K1_INLINE void secp256k1_memczero(void *s, size_t len, int flag)
Definition: util.h:206
#define ARG_CHECK(cond)
Definition: secp256k1.c:44
static int secp256k1_ec_pubkey_create_helper(const secp256k1_ecmult_gen_context *ecmult_gen_ctx, secp256k1_scalar *seckey_scalar, secp256k1_ge *p, const unsigned char *seckey)
Definition: secp256k1.c:584
static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context *ctx, const void *p, size_t len)
Definition: secp256k1.c:235
static int secp256k1_pubkey_load(const secp256k1_context *ctx, secp256k1_ge *ge, const secp256k1_pubkey *pubkey)
Definition: secp256k1.c:239
static void secp256k1_pubkey_save(secp256k1_pubkey *pubkey, secp256k1_ge *ge)
Definition: secp256k1.c:258
int(* secp256k1_ellswift_xdh_hash_function)(unsigned char *output, const unsigned char *x32, const unsigned char *ell_a64, const unsigned char *ell_b64, void *data)
A pointer to a function used by secp256k1_ellswift_xdh to hash the shared X coordinate along with the...
secp256k1_ecmult_gen_context ecmult_gen_ctx
Definition: secp256k1.c:61
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
secp256k1_fe x
Definition: group.h:17
secp256k1_fe y
Definition: group.h:18
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:61
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
uint64_t bytes
Definition: hash.h:16
uint32_t s[8]
Definition: hash.h:14