mirror of
https://github.com/Retropex/bitcoin.git
synced 2025-06-03 07:52:33 +02:00

0cdc758a563 Merge bitcoin-core/secp256k1#1631: release: prepare for 0.6.0 39d5dfd542a release: prepare for 0.6.0 df2eceb2790 build: add ellswift.md and musig.md to release tarball a306bb7e903 tools: fix check-abi.sh after cmake out locations were changed 145868a84d2 Do not export `secp256k1_musig_nonce_gen_internal` b161bffb8bf Merge bitcoin-core/secp256k1#1579: Clear sensitive memory without getting optimized out (revival of #636) a38d879a1a6 Merge bitcoin-core/secp256k1#1628: Name public API structs 7d48f5ed02e Merge bitcoin-core/secp256k1#1581: test, ci: Lower default iteration count to 16 694342fdb71 Name public API structs 0f73caf7c62 test, ci: Lower default iteration count to 16 9a8db52f4e9 Merge bitcoin-core/secp256k1#1582: cmake, test: Add `secp256k1_` prefix to test names 765ef53335a Clear _gej instances after point multiplication to avoid potential leaks 349e6ab916b Introduce separate _clear functions for hash module 99cc9fd6d01 Don't rely on memset to set signed integers to 0 97c57f42ba8 Implement various _clear() functions with secp256k1_memclear() 9bb368d1466 Use secp256k1_memclear() to clear stack memory instead of memset() e3497bbf001 Separate between clearing memory and setting to zero in tests d79a6ccd43a Separate secp256k1_fe_set_int( . , 0 ) from secp256k1_fe_clear() 1c081262227 Add secp256k1_memclear() for clearing secret data 1464f15c812 Merge bitcoin-core/secp256k1#1625: util: Remove unused (u)int64_t formatting macros 980c08df80a util: Remove unused (u)int64_t formatting macros 9b7c59cbb90 Merge bitcoin-core/secp256k1#1624: ci: Update macOS image 096e3e23f63 ci: Update macOS image e7d384488e8 Don't clear secrets in pippenger implementation 68b55209f1b Merge bitcoin-core/secp256k1#1619: musig: ctimetests: fix _declassify range for generated nonce points f0868a9b3d8 Merge bitcoin-core/secp256k1#1595: build: 45839th attempt to fix symbol visibility on Windows 1fae76f50c0 Merge bitcoin-core/secp256k1#1620: Remove unused scratch space from API 8be3839fb2e Remove unused scratch space from API 57eda3ba300 musig: ctimetests: fix _declassify range for generated nonce points 87384f5c0f2 cmake, test: Add `secp256k1_` prefix to test names e59158b6eb7 Merge bitcoin-core/secp256k1#1553: cmake: Set top-level target output locations 18f9b967c25 Merge bitcoin-core/secp256k1#1616: examples: do not retry generating seckey randomness in musig 5bab8f6d3c4 examples: make key generation doc consistent e8908221a45 examples: do not retry generating seckey randomness in musig 70b6be1834e extrakeys: improve doc of keypair_create (don't suggest retry) 01b5893389e Merge bitcoin-core/secp256k1#1599: #1570 improve examples: remove key generation loop cd4f84f3ba8 Improve examples/documentation: remove key generation loops a88aa935063 Merge bitcoin-core/secp256k1#1603: f can never equal -m 3660fe5e2a9 Merge bitcoin-core/secp256k1#1479: Add module "musig" that implements MuSig2 multi-signatures (BIP 327) 168c92011f5 build: allow enabling the musig module in cmake f411841a46b Add module "musig" that implements MuSig2 multi-signatures (BIP 327) 0be79660f38 util: add constant-time is_zero_array function c8fbdb1b972 group: add ge_to_bytes_ext and ge_from_bytes_ext ef7ff03407f f can never equal -m c232486d84e Revert "cmake: Set `ENVIRONMENT` property for examples on Windows" 26e4a7c2146 cmake: Set top-level target output locations 4c57c7a5a95 Merge bitcoin-core/secp256k1#1554: cmake: Clean up testing code 447334cb06d include: Avoid visibility("default") on Windows 472faaa8ee6 Merge bitcoin-core/secp256k1#1604: doc: fix typos in `secp256k1_ecdsa_{recoverable_,}signature` API description 292310fbb24 doc: fix typos in `secp256k1_ecdsa_{recoverable_,}signature` API description 85e224dd97f group: add ge_to_bytes and ge_from_bytes 7c987ec89e6 cmake: Call `enable_testing()` unconditionally 6aa576515ef cmake: Delete `CTest` module git-subtree-dir: src/secp256k1 git-subtree-split: 0cdc758a56360bf58a851fe91085a327ec97685a
213 lines
11 KiB
C
213 lines
11 KiB
C
/***********************************************************************
|
|
* Copyright (c) 2013, 2014 Pieter Wuille *
|
|
* Distributed under the MIT software license, see the accompanying *
|
|
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
|
|
***********************************************************************/
|
|
|
|
#ifndef SECP256K1_GROUP_H
|
|
#define SECP256K1_GROUP_H
|
|
|
|
#include "field.h"
|
|
|
|
/** A group element in affine coordinates on the secp256k1 curve,
|
|
* or occasionally on an isomorphic curve of the form y^2 = x^3 + 7*t^6.
|
|
* Note: For exhaustive test mode, secp256k1 is replaced by a small subgroup of a different curve.
|
|
*/
|
|
typedef struct {
|
|
secp256k1_fe x;
|
|
secp256k1_fe y;
|
|
int infinity; /* whether this represents the point at infinity */
|
|
} secp256k1_ge;
|
|
|
|
#define SECP256K1_GE_CONST(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) {SECP256K1_FE_CONST((a),(b),(c),(d),(e),(f),(g),(h)), SECP256K1_FE_CONST((i),(j),(k),(l),(m),(n),(o),(p)), 0}
|
|
#define SECP256K1_GE_CONST_INFINITY {SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 0), SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 0), 1}
|
|
|
|
/** A group element of the secp256k1 curve, in jacobian coordinates.
|
|
* Note: For exhastive test mode, secp256k1 is replaced by a small subgroup of a different curve.
|
|
*/
|
|
typedef struct {
|
|
secp256k1_fe x; /* actual X: x/z^2 */
|
|
secp256k1_fe y; /* actual Y: y/z^3 */
|
|
secp256k1_fe z;
|
|
int infinity; /* whether this represents the point at infinity */
|
|
} secp256k1_gej;
|
|
|
|
#define SECP256K1_GEJ_CONST(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) {SECP256K1_FE_CONST((a),(b),(c),(d),(e),(f),(g),(h)), SECP256K1_FE_CONST((i),(j),(k),(l),(m),(n),(o),(p)), SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 1), 0}
|
|
#define SECP256K1_GEJ_CONST_INFINITY {SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 0), SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 0), SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 0), 1}
|
|
|
|
typedef struct {
|
|
secp256k1_fe_storage x;
|
|
secp256k1_fe_storage y;
|
|
} secp256k1_ge_storage;
|
|
|
|
#define SECP256K1_GE_STORAGE_CONST(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) {SECP256K1_FE_STORAGE_CONST((a),(b),(c),(d),(e),(f),(g),(h)), SECP256K1_FE_STORAGE_CONST((i),(j),(k),(l),(m),(n),(o),(p))}
|
|
|
|
#define SECP256K1_GE_STORAGE_CONST_GET(t) SECP256K1_FE_STORAGE_CONST_GET(t.x), SECP256K1_FE_STORAGE_CONST_GET(t.y)
|
|
|
|
/** Maximum allowed magnitudes for group element coordinates
|
|
* in affine (x, y) and jacobian (x, y, z) representation. */
|
|
#define SECP256K1_GE_X_MAGNITUDE_MAX 4
|
|
#define SECP256K1_GE_Y_MAGNITUDE_MAX 3
|
|
#define SECP256K1_GEJ_X_MAGNITUDE_MAX 4
|
|
#define SECP256K1_GEJ_Y_MAGNITUDE_MAX 4
|
|
#define SECP256K1_GEJ_Z_MAGNITUDE_MAX 1
|
|
|
|
/** Set a group element equal to the point with given X and Y coordinates */
|
|
static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y);
|
|
|
|
/** Set a group element (affine) equal to the point with the given X coordinate, and given oddness
|
|
* for Y. Return value indicates whether the result is valid. */
|
|
static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd);
|
|
|
|
/** Determine whether x is a valid X coordinate on the curve. */
|
|
static int secp256k1_ge_x_on_curve_var(const secp256k1_fe *x);
|
|
|
|
/** Determine whether fraction xn/xd is a valid X coordinate on the curve (xd != 0). */
|
|
static int secp256k1_ge_x_frac_on_curve_var(const secp256k1_fe *xn, const secp256k1_fe *xd);
|
|
|
|
/** Check whether a group element is the point at infinity. */
|
|
static int secp256k1_ge_is_infinity(const secp256k1_ge *a);
|
|
|
|
/** Check whether a group element is valid (i.e., on the curve). */
|
|
static int secp256k1_ge_is_valid_var(const secp256k1_ge *a);
|
|
|
|
/** Set r equal to the inverse of a (i.e., mirrored around the X axis) */
|
|
static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a);
|
|
|
|
/** Set a group element equal to another which is given in jacobian coordinates. Constant time. */
|
|
static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a);
|
|
|
|
/** Set a group element equal to another which is given in jacobian coordinates. */
|
|
static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a);
|
|
|
|
/** Set a batch of group elements equal to the inputs given in jacobian coordinates */
|
|
static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len);
|
|
|
|
/** Bring a batch of inputs to the same global z "denominator", based on ratios between
|
|
* (omitted) z coordinates of adjacent elements.
|
|
*
|
|
* Although the elements a[i] are _ge rather than _gej, they actually represent elements
|
|
* in Jacobian coordinates with their z coordinates omitted.
|
|
*
|
|
* Using the notation z(b) to represent the omitted z coordinate of b, the array zr of
|
|
* z coordinate ratios must satisfy zr[i] == z(a[i]) / z(a[i-1]) for 0 < 'i' < len.
|
|
* The zr[0] value is unused.
|
|
*
|
|
* This function adjusts the coordinates of 'a' in place so that for all 'i', z(a[i]) == z(a[len-1]).
|
|
* In other words, the initial value of z(a[len-1]) becomes the global z "denominator". Only the
|
|
* a[i].x and a[i].y coordinates are explicitly modified; the adjustment of the omitted z coordinate is
|
|
* implicit.
|
|
*
|
|
* The coordinates of the final element a[len-1] are not changed.
|
|
*/
|
|
static void secp256k1_ge_table_set_globalz(size_t len, secp256k1_ge *a, const secp256k1_fe *zr);
|
|
|
|
/** Check two group elements (affine) for equality in variable time. */
|
|
static int secp256k1_ge_eq_var(const secp256k1_ge *a, const secp256k1_ge *b);
|
|
|
|
/** Set a group element (affine) equal to the point at infinity. */
|
|
static void secp256k1_ge_set_infinity(secp256k1_ge *r);
|
|
|
|
/** Set a group element (jacobian) equal to the point at infinity. */
|
|
static void secp256k1_gej_set_infinity(secp256k1_gej *r);
|
|
|
|
/** Set a group element (jacobian) equal to another which is given in affine coordinates. */
|
|
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a);
|
|
|
|
/** Check two group elements (jacobian) for equality in variable time. */
|
|
static int secp256k1_gej_eq_var(const secp256k1_gej *a, const secp256k1_gej *b);
|
|
|
|
/** Check two group elements (jacobian and affine) for equality in variable time. */
|
|
static int secp256k1_gej_eq_ge_var(const secp256k1_gej *a, const secp256k1_ge *b);
|
|
|
|
/** Compare the X coordinate of a group element (jacobian).
|
|
* The magnitude of the group element's X coordinate must not exceed 31. */
|
|
static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a);
|
|
|
|
/** Set r equal to the inverse of a (i.e., mirrored around the X axis) */
|
|
static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a);
|
|
|
|
/** Check whether a group element is the point at infinity. */
|
|
static int secp256k1_gej_is_infinity(const secp256k1_gej *a);
|
|
|
|
/** Set r equal to the double of a. Constant time. */
|
|
static void secp256k1_gej_double(secp256k1_gej *r, const secp256k1_gej *a);
|
|
|
|
/** Set r equal to the double of a. If rzr is not-NULL this sets *rzr such that r->z == a->z * *rzr (where infinity means an implicit z = 0). */
|
|
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr);
|
|
|
|
/** Set r equal to the sum of a and b. If rzr is non-NULL this sets *rzr such that r->z == a->z * *rzr (a cannot be infinity in that case). */
|
|
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 (with b given in affine coordinates, and not infinity). */
|
|
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). This is more efficient
|
|
than secp256k1_gej_add_var. It is identical to secp256k1_gej_add_ge but without constant-time
|
|
guarantee, and b is allowed to be infinity. If rzr is non-NULL this sets *rzr such that r->z == a->z * *rzr (a cannot be infinity in that case). */
|
|
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 the inverse of b's Z coordinate passed as bzinv). */
|
|
static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, const secp256k1_fe *bzinv);
|
|
|
|
/** Set r to be equal to lambda times a, where lambda is chosen in a way such that this is very fast. */
|
|
static void secp256k1_ge_mul_lambda(secp256k1_ge *r, const secp256k1_ge *a);
|
|
|
|
/** Clear a secp256k1_gej to prevent leaking sensitive information. */
|
|
static void secp256k1_gej_clear(secp256k1_gej *r);
|
|
|
|
/** Clear a secp256k1_ge to prevent leaking sensitive information. */
|
|
static void secp256k1_ge_clear(secp256k1_ge *r);
|
|
|
|
/** Convert a group element to the storage type. */
|
|
static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a);
|
|
|
|
/** Convert a group element back from the storage type. */
|
|
static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage *a);
|
|
|
|
/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. Both *r and *a must be initialized.*/
|
|
static void secp256k1_gej_cmov(secp256k1_gej *r, const secp256k1_gej *a, int flag);
|
|
|
|
/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. Both *r and *a must be initialized.*/
|
|
static void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_ge_storage *a, int flag);
|
|
|
|
/** Rescale a jacobian point by b which must be non-zero. Constant-time. */
|
|
static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *b);
|
|
|
|
/** Convert a group element that is not infinity to a 64-byte array. The output
|
|
* array is platform-dependent. */
|
|
static void secp256k1_ge_to_bytes(unsigned char *buf, const secp256k1_ge *a);
|
|
|
|
/** Convert a 64-byte array into group element. This function assumes that the
|
|
* provided buffer correctly encodes a group element. */
|
|
static void secp256k1_ge_from_bytes(secp256k1_ge *r, const unsigned char *buf);
|
|
|
|
/** Convert a group element (that is allowed to be infinity) to a 64-byte
|
|
* array. The output array is platform-dependent. */
|
|
static void secp256k1_ge_to_bytes_ext(unsigned char *data, const secp256k1_ge *ge);
|
|
|
|
/** Convert a 64-byte array into a group element. This function assumes that the
|
|
* provided buffer is the output of secp256k1_ge_to_bytes_ext. */
|
|
static void secp256k1_ge_from_bytes_ext(secp256k1_ge *ge, const unsigned char *data);
|
|
|
|
/** Determine if a point (which is assumed to be on the curve) is in the correct (sub)group of the curve.
|
|
*
|
|
* In normal mode, the used group is secp256k1, which has cofactor=1 meaning that every point on the curve is in the
|
|
* group, and this function returns always true.
|
|
*
|
|
* When compiling in exhaustive test mode, a slightly different curve equation is used, leading to a group with a
|
|
* (very) small subgroup, and that subgroup is what is used for all cryptographic operations. In that mode, this
|
|
* function checks whether a point that is on the curve is in fact also in that subgroup.
|
|
*/
|
|
static int secp256k1_ge_is_in_correct_subgroup(const secp256k1_ge* ge);
|
|
|
|
/** Check invariants on an affine group element (no-op unless VERIFY is enabled). */
|
|
static void secp256k1_ge_verify(const secp256k1_ge *a);
|
|
#define SECP256K1_GE_VERIFY(a) secp256k1_ge_verify(a)
|
|
|
|
/** Check invariants on a Jacobian group element (no-op unless VERIFY is enabled). */
|
|
static void secp256k1_gej_verify(const secp256k1_gej *a);
|
|
#define SECP256K1_GEJ_VERIFY(a) secp256k1_gej_verify(a)
|
|
|
|
#endif /* SECP256K1_GROUP_H */
|