Merge bitcoin-core/secp256k1#1126: API cleanup with respect to contexts
4386a2306c2b8cf9ad3040d8010e4295f6f01490 examples: Switch to NONE contexts (Tim Ruffing) 7289b51d31bf091330f1bcae397fba8b2b2d54ab docs: Use doxygen style if and only if comment is user-facing (Tim Ruffing) e7d0185c901dfd6986476ba85aa03f5cfa0951f9 docs: Get rid of "initialized for signing" terminology (Tim Ruffing) 06126364ad988771d762923ce71e63e7f5c56951 docs: Tidy and improve docs about contexts and randomization (Tim Ruffing) e02d6862bddfc4c18116c22deb86c29380a7bfce selftest: Expose in public API (Tim Ruffing) e383fbfa66d2c7f48c06a4f4810b5e6db945d2c7 selftest: Rename internal function to make name available for API (Tim Ruffing) d2c6d48de3c7032fc6d96e8efecb5a933f3c009c tests: Use new name of static context (Tim Ruffing) 53796d2b24e813750feae73e85c0a6eee40dc391 contexts: Rename static context (Tim Ruffing) 72fedf8a6cff9e26882fa0bc923da0429b6916af docs: Improve docs for static context (Tim Ruffing) 316ac7625ad1fbfc5b5b317dfbc7bdab534aaa3e contexts: Deprecate all context flags except SECP256K1_CONTEXT_NONE (Tim Ruffing) 1a553ee8be295f20aca3bc24d85732074b888b87 docs: Change signature "validation" to "verification" (Tim Ruffing) ee7341fbac1d159a198780c94aa8e0a025e28848 docs: Never require a verification context (Tim Ruffing) Pull request description: ACKs for top commit: sipa: utACK 4386a2306c2b8cf9ad3040d8010e4295f6f01490 jonasnick: ACK 4386a2306c2b8cf9ad3040d8010e4295f6f01490 Tree-SHA512: 7bf07dfae0ecbf7de1418de64ef743a23dc5f244aeba2c1cf3ecbdc117d6ac12bb6c8f17f739605566074a9b901765ee4a32288b6edc6f9a0040a70cb472f6ee
This commit is contained in:
commit
e3f84777eb
@ -43,8 +43,7 @@ extern "C" {
|
|||||||
/** Export a private key in DER format.
|
/** Export a private key in DER format.
|
||||||
*
|
*
|
||||||
* Returns: 1 if the private key was valid.
|
* Returns: 1 if the private key was valid.
|
||||||
* Args: ctx: pointer to a context object, initialized for signing (cannot
|
* Args: ctx: pointer to a context object (not secp256k1_context_static).
|
||||||
* be NULL)
|
|
||||||
* Out: privkey: pointer to an array for storing the private key in BER.
|
* Out: privkey: pointer to an array for storing the private key in BER.
|
||||||
* Should have space for 279 bytes, and cannot be NULL.
|
* Should have space for 279 bytes, and cannot be NULL.
|
||||||
* privkeylen: Pointer to an int where the length of the private key in
|
* privkeylen: Pointer to an int where the length of the private key in
|
||||||
|
@ -9,6 +9,13 @@ Each change falls into one of the following categories: Added, Changed, Deprecat
|
|||||||
### Changed
|
### Changed
|
||||||
- Enable modules schnorrsig, extrakeys and ECDH by default in ./configure
|
- Enable modules schnorrsig, extrakeys and ECDH by default in ./configure
|
||||||
|
|
||||||
|
### Deprecated
|
||||||
|
- Deprecated context flags `SECP256K1_CONTEXT_VERIFY` and `SECP256K1_CONTEXT_SIGN`. Use `SECP256K1_CONTEXT_NONE` instead.
|
||||||
|
- Renamed `secp256k1_context_no_precomp` to `secp256k1_context_static`.
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Added `secp256k1_selftest`, to be used in conjunction with `secp256k1_context_static`.
|
||||||
|
|
||||||
## [MAJOR.MINOR.PATCH] - YYYY-MM-DD
|
## [MAJOR.MINOR.PATCH] - YYYY-MM-DD
|
||||||
|
|
||||||
### Added/Changed/Deprecated/Removed/Fixed/Security
|
### Added/Changed/Deprecated/Removed/Fixed/Security
|
||||||
|
@ -30,12 +30,8 @@ int main(void) {
|
|||||||
secp256k1_pubkey pubkey1;
|
secp256k1_pubkey pubkey1;
|
||||||
secp256k1_pubkey pubkey2;
|
secp256k1_pubkey pubkey2;
|
||||||
|
|
||||||
/* The specification in secp256k1.h states that `secp256k1_ec_pubkey_create`
|
/* Before we can call actual API functions, we need to create a "context". */
|
||||||
* needs a context object initialized for signing, which is why we create
|
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
|
||||||
* a context with the SECP256K1_CONTEXT_SIGN flag.
|
|
||||||
* (The docs for `secp256k1_ecdh` don't require any special context, just
|
|
||||||
* some initialized context) */
|
|
||||||
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
|
|
||||||
if (!fill_random(randomize, sizeof(randomize))) {
|
if (!fill_random(randomize, sizeof(randomize))) {
|
||||||
printf("Failed to generate randomness\n");
|
printf("Failed to generate randomness\n");
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -38,12 +38,8 @@ int main(void) {
|
|||||||
int return_val;
|
int return_val;
|
||||||
secp256k1_pubkey pubkey;
|
secp256k1_pubkey pubkey;
|
||||||
secp256k1_ecdsa_signature sig;
|
secp256k1_ecdsa_signature sig;
|
||||||
/* The specification in secp256k1.h states that `secp256k1_ec_pubkey_create` needs
|
/* Before we can call actual API functions, we need to create a "context". */
|
||||||
* a context object initialized for signing and `secp256k1_ecdsa_verify` needs
|
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
|
||||||
* a context initialized for verification, which is why we create a context
|
|
||||||
* for both signing and verification with the SECP256K1_CONTEXT_SIGN and
|
|
||||||
* SECP256K1_CONTEXT_VERIFY flags. */
|
|
||||||
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
|
|
||||||
if (!fill_random(randomize, sizeof(randomize))) {
|
if (!fill_random(randomize, sizeof(randomize))) {
|
||||||
printf("Failed to generate randomness\n");
|
printf("Failed to generate randomness\n");
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -30,12 +30,8 @@ int main(void) {
|
|||||||
int return_val;
|
int return_val;
|
||||||
secp256k1_xonly_pubkey pubkey;
|
secp256k1_xonly_pubkey pubkey;
|
||||||
secp256k1_keypair keypair;
|
secp256k1_keypair keypair;
|
||||||
/* The specification in secp256k1_extrakeys.h states that `secp256k1_keypair_create`
|
/* Before we can call actual API functions, we need to create a "context". */
|
||||||
* needs a context object initialized for signing. And in secp256k1_schnorrsig.h
|
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
|
||||||
* they state that `secp256k1_schnorrsig_verify` needs a context initialized for
|
|
||||||
* verification, which is why we create a context for both signing and verification
|
|
||||||
* with the SECP256K1_CONTEXT_SIGN and SECP256K1_CONTEXT_VERIFY flags. */
|
|
||||||
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
|
|
||||||
if (!fill_random(randomize, sizeof(randomize))) {
|
if (!fill_random(randomize, sizeof(randomize))) {
|
||||||
printf("Failed to generate randomness\n");
|
printf("Failed to generate randomness\n");
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -7,7 +7,7 @@ extern "C" {
|
|||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
/* Unless explicitly stated all pointer arguments must not be NULL.
|
/** Unless explicitly stated all pointer arguments must not be NULL.
|
||||||
*
|
*
|
||||||
* The following rules specify the order of arguments in API calls:
|
* The following rules specify the order of arguments in API calls:
|
||||||
*
|
*
|
||||||
@ -24,15 +24,19 @@ extern "C" {
|
|||||||
* 5. Opaque data pointers follow the function pointer they are to be passed to.
|
* 5. Opaque data pointers follow the function pointer they are to be passed to.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/** Opaque data structure that holds context information (precomputed tables etc.).
|
/** Opaque data structure that holds context information
|
||||||
*
|
*
|
||||||
* The purpose of context structures is to cache large precomputed data tables
|
* The primary purpose of context objects is to store randomization data for
|
||||||
* that are expensive to construct, and also to maintain the randomization data
|
* enhanced protection against side-channel leakage. This protection is only
|
||||||
* for blinding.
|
* effective if the context is randomized after its creation. See
|
||||||
|
* secp256k1_context_create for creation of contexts and
|
||||||
|
* secp256k1_context_randomize for randomization.
|
||||||
*
|
*
|
||||||
* Do not create a new context object for each operation, as construction is
|
* A secondary purpose of context objects is to store pointers to callback
|
||||||
* far slower than all other API calls (~100 times slower than an ECDSA
|
* functions that the library will call when certain error states arise. See
|
||||||
* verification).
|
* secp256k1_context_set_error_callback as well as
|
||||||
|
* secp256k1_context_set_illegal_callback for details. Future library versions
|
||||||
|
* may use context objects for additional purposes.
|
||||||
*
|
*
|
||||||
* A constructed context can safely be used from multiple threads
|
* A constructed context can safely be used from multiple threads
|
||||||
* simultaneously, but API calls that take a non-const pointer to a context
|
* simultaneously, but API calls that take a non-const pointer to a context
|
||||||
@ -45,7 +49,7 @@ extern "C" {
|
|||||||
*/
|
*/
|
||||||
typedef struct secp256k1_context_struct secp256k1_context;
|
typedef struct secp256k1_context_struct secp256k1_context;
|
||||||
|
|
||||||
/** Opaque data structure that holds rewriteable "scratch space"
|
/** Opaque data structure that holds rewritable "scratch space"
|
||||||
*
|
*
|
||||||
* The purpose of this structure is to replace dynamic memory allocations,
|
* The purpose of this structure is to replace dynamic memory allocations,
|
||||||
* because we target architectures where this may not be available. It is
|
* because we target architectures where this may not be available. It is
|
||||||
@ -130,7 +134,7 @@ typedef int (*secp256k1_nonce_function)(
|
|||||||
# define SECP256K1_INLINE inline
|
# define SECP256K1_INLINE inline
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
/** When this header is used at build-time the SECP256K1_BUILD define needs to be set
|
/* When this header is used at build-time the SECP256K1_BUILD define needs to be set
|
||||||
* to correctly setup export attributes and nullness checks. This is normally done
|
* to correctly setup export attributes and nullness checks. This is normally done
|
||||||
* by secp256k1.c but to guard against this header being included before secp256k1.c
|
* by secp256k1.c but to guard against this header being included before secp256k1.c
|
||||||
* has had a chance to set the define (e.g. via test harnesses that just includes
|
* has had a chance to set the define (e.g. via test harnesses that just includes
|
||||||
@ -159,7 +163,7 @@ typedef int (*secp256k1_nonce_function)(
|
|||||||
# endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**Warning attributes
|
/* Warning attributes
|
||||||
* NONNULL is not used if SECP256K1_BUILD is set to avoid the compiler optimizing out
|
* NONNULL is not used if SECP256K1_BUILD is set to avoid the compiler optimizing out
|
||||||
* some paranoid null checks. */
|
* some paranoid null checks. */
|
||||||
# if defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
|
# if defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
|
||||||
@ -173,7 +177,7 @@ typedef int (*secp256k1_nonce_function)(
|
|||||||
# define SECP256K1_ARG_NONNULL(_x)
|
# define SECP256K1_ARG_NONNULL(_x)
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
/** Attribute for marking functions, types, and variables as deprecated */
|
/* Attribute for marking functions, types, and variables as deprecated */
|
||||||
#if !defined(SECP256K1_BUILD) && defined(__has_attribute)
|
#if !defined(SECP256K1_BUILD) && defined(__has_attribute)
|
||||||
# if __has_attribute(__deprecated__)
|
# if __has_attribute(__deprecated__)
|
||||||
# define SECP256K1_DEPRECATED(_msg) __attribute__ ((__deprecated__(_msg)))
|
# define SECP256K1_DEPRECATED(_msg) __attribute__ ((__deprecated__(_msg)))
|
||||||
@ -184,22 +188,26 @@ typedef int (*secp256k1_nonce_function)(
|
|||||||
# define SECP256K1_DEPRECATED(_msg)
|
# define SECP256K1_DEPRECATED(_msg)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/** All flags' lower 8 bits indicate what they're for. Do not use directly. */
|
/* All flags' lower 8 bits indicate what they're for. Do not use directly. */
|
||||||
#define SECP256K1_FLAGS_TYPE_MASK ((1 << 8) - 1)
|
#define SECP256K1_FLAGS_TYPE_MASK ((1 << 8) - 1)
|
||||||
#define SECP256K1_FLAGS_TYPE_CONTEXT (1 << 0)
|
#define SECP256K1_FLAGS_TYPE_CONTEXT (1 << 0)
|
||||||
#define SECP256K1_FLAGS_TYPE_COMPRESSION (1 << 1)
|
#define SECP256K1_FLAGS_TYPE_COMPRESSION (1 << 1)
|
||||||
/** The higher bits contain the actual data. Do not use directly. */
|
/* The higher bits contain the actual data. Do not use directly. */
|
||||||
#define SECP256K1_FLAGS_BIT_CONTEXT_VERIFY (1 << 8)
|
#define SECP256K1_FLAGS_BIT_CONTEXT_VERIFY (1 << 8)
|
||||||
#define SECP256K1_FLAGS_BIT_CONTEXT_SIGN (1 << 9)
|
#define SECP256K1_FLAGS_BIT_CONTEXT_SIGN (1 << 9)
|
||||||
#define SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY (1 << 10)
|
#define SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY (1 << 10)
|
||||||
#define SECP256K1_FLAGS_BIT_COMPRESSION (1 << 8)
|
#define SECP256K1_FLAGS_BIT_COMPRESSION (1 << 8)
|
||||||
|
|
||||||
/** Flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and
|
/** Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and
|
||||||
* secp256k1_context_preallocated_create. */
|
* secp256k1_context_preallocated_create. */
|
||||||
|
#define SECP256K1_CONTEXT_NONE (SECP256K1_FLAGS_TYPE_CONTEXT)
|
||||||
|
|
||||||
|
/** Deprecated context flags. These flags are treated equivalent to SECP256K1_CONTEXT_NONE. */
|
||||||
#define SECP256K1_CONTEXT_VERIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_VERIFY)
|
#define SECP256K1_CONTEXT_VERIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_VERIFY)
|
||||||
#define SECP256K1_CONTEXT_SIGN (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_SIGN)
|
#define SECP256K1_CONTEXT_SIGN (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_SIGN)
|
||||||
|
|
||||||
|
/* Testing flag. Do not use. */
|
||||||
#define SECP256K1_CONTEXT_DECLASSIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY)
|
#define SECP256K1_CONTEXT_DECLASSIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY)
|
||||||
#define SECP256K1_CONTEXT_NONE (SECP256K1_FLAGS_TYPE_CONTEXT)
|
|
||||||
|
|
||||||
/** Flag to pass to secp256k1_ec_pubkey_serialize. */
|
/** Flag to pass to secp256k1_ec_pubkey_serialize. */
|
||||||
#define SECP256K1_EC_COMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)
|
#define SECP256K1_EC_COMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)
|
||||||
@ -212,23 +220,66 @@ typedef int (*secp256k1_nonce_function)(
|
|||||||
#define SECP256K1_TAG_PUBKEY_HYBRID_EVEN 0x06
|
#define SECP256K1_TAG_PUBKEY_HYBRID_EVEN 0x06
|
||||||
#define SECP256K1_TAG_PUBKEY_HYBRID_ODD 0x07
|
#define SECP256K1_TAG_PUBKEY_HYBRID_ODD 0x07
|
||||||
|
|
||||||
/** A simple secp256k1 context object with no precomputed tables. These are useful for
|
/** A built-in constant secp256k1 context object with static storage duration, to be
|
||||||
* type serialization/parsing functions which require a context object to maintain
|
* used in conjunction with secp256k1_selftest.
|
||||||
* API consistency, but currently do not require expensive precomputations or dynamic
|
*
|
||||||
* allocations.
|
* This context object offers *only limited functionality* , i.e., it cannot be used
|
||||||
|
* for API functions that perform computations involving secret keys, e.g., signing
|
||||||
|
* and public key generation. If this restriction applies to a specific API function,
|
||||||
|
* it is mentioned in its documentation. See secp256k1_context_create if you need a
|
||||||
|
* full context object that supports all functionality offered by the library.
|
||||||
|
*
|
||||||
|
* It is highly recommended to call secp256k1_selftest before using this context.
|
||||||
*/
|
*/
|
||||||
SECP256K1_API extern const secp256k1_context *secp256k1_context_no_precomp;
|
SECP256K1_API extern const secp256k1_context *secp256k1_context_static;
|
||||||
|
|
||||||
|
/** Deprecated alias for secp256k1_context_static. */
|
||||||
|
SECP256K1_API extern const secp256k1_context *secp256k1_context_no_precomp
|
||||||
|
SECP256K1_DEPRECATED("Use secp256k1_context_static instead");
|
||||||
|
|
||||||
|
/** Perform basic self tests (to be used in conjunction with secp256k1_context_static)
|
||||||
|
*
|
||||||
|
* This function performs self tests that detect some serious usage errors and
|
||||||
|
* similar conditions, e.g., when the library is compiled for the wrong endianness.
|
||||||
|
* This is a last resort measure to be used in production. The performed tests are
|
||||||
|
* very rudimentary and are not intended as a replacement for running the test
|
||||||
|
* binaries.
|
||||||
|
*
|
||||||
|
* It is highly recommended to call this before using secp256k1_context_static.
|
||||||
|
* It is not necessary to call this function before using a context created with
|
||||||
|
* secp256k1_context_create (or secp256k1_context_preallocated_create), which will
|
||||||
|
* take care of performing the self tests.
|
||||||
|
*
|
||||||
|
* If the tests fail, this function will call the default error handler to abort the
|
||||||
|
* program (see secp256k1_context_set_error_callback).
|
||||||
|
*/
|
||||||
|
SECP256K1_API void secp256k1_selftest(void);
|
||||||
|
|
||||||
|
|
||||||
/** Create a secp256k1 context object (in dynamically allocated memory).
|
/** Create a secp256k1 context object (in dynamically allocated memory).
|
||||||
*
|
*
|
||||||
* This function uses malloc to allocate memory. It is guaranteed that malloc is
|
* This function uses malloc to allocate memory. It is guaranteed that malloc is
|
||||||
* called at most once for every call of this function. If you need to avoid dynamic
|
* called at most once for every call of this function. If you need to avoid dynamic
|
||||||
* memory allocation entirely, see the functions in secp256k1_preallocated.h.
|
* memory allocation entirely, see secp256k1_context_static and the functions in
|
||||||
|
* secp256k1_preallocated.h.
|
||||||
*
|
*
|
||||||
* Returns: a newly created context object.
|
* Returns: a newly created context object.
|
||||||
* In: flags: which parts of the context to initialize.
|
* In: flags: Always set to SECP256K1_CONTEXT_NONE (see below).
|
||||||
*
|
*
|
||||||
* See also secp256k1_context_randomize.
|
* The only valid non-deprecated flag in recent library versions is
|
||||||
|
* SECP256K1_CONTEXT_NONE, which will create a context sufficient for all functionality
|
||||||
|
* offered by the library. All other (deprecated) flags will be treated as equivalent
|
||||||
|
* to the SECP256K1_CONTEXT_NONE flag. Though the flags parameter primarily exists for
|
||||||
|
* historical reasons, future versions of the library may introduce new flags.
|
||||||
|
*
|
||||||
|
* If the context is intended to be used for API functions that perform computations
|
||||||
|
* involving secret keys, e.g., signing and public key generation, then it is highly
|
||||||
|
* recommended to call secp256k1_context_randomize on the context before calling
|
||||||
|
* those API functions. This will provide enhanced protection against side-channel
|
||||||
|
* leakage, see secp256k1_context_randomize for details.
|
||||||
|
*
|
||||||
|
* Do not create a new context object for each operation, as construction and
|
||||||
|
* randomization can take non-negligible time.
|
||||||
*/
|
*/
|
||||||
SECP256K1_API secp256k1_context* secp256k1_context_create(
|
SECP256K1_API secp256k1_context* secp256k1_context_create(
|
||||||
unsigned int flags
|
unsigned int flags
|
||||||
@ -308,7 +359,10 @@ SECP256K1_API void secp256k1_context_set_illegal_callback(
|
|||||||
) SECP256K1_ARG_NONNULL(1);
|
) SECP256K1_ARG_NONNULL(1);
|
||||||
|
|
||||||
/** Set a callback function to be called when an internal consistency check
|
/** Set a callback function to be called when an internal consistency check
|
||||||
* fails. The default is crashing.
|
* fails.
|
||||||
|
*
|
||||||
|
* The default callback writes an error message to stderr and calls abort
|
||||||
|
* to abort the program.
|
||||||
*
|
*
|
||||||
* This can only trigger in case of a hardware failure, miscompilation,
|
* This can only trigger in case of a hardware failure, miscompilation,
|
||||||
* memory corruption, serious bug in the library, or other error would can
|
* memory corruption, serious bug in the library, or other error would can
|
||||||
@ -426,8 +480,8 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_cmp(
|
|||||||
* encoding is invalid. R and S with value 0 are allowed in the encoding.
|
* encoding is invalid. R and S with value 0 are allowed in the encoding.
|
||||||
*
|
*
|
||||||
* After the call, sig will always be initialized. If parsing failed or R or
|
* After the call, sig will always be initialized. If parsing failed or R or
|
||||||
* S are zero, the resulting sig value is guaranteed to fail validation for any
|
* S are zero, the resulting sig value is guaranteed to fail verification for
|
||||||
* message and public key.
|
* any message and public key.
|
||||||
*/
|
*/
|
||||||
SECP256K1_API int secp256k1_ecdsa_signature_parse_compact(
|
SECP256K1_API int secp256k1_ecdsa_signature_parse_compact(
|
||||||
const secp256k1_context* ctx,
|
const secp256k1_context* ctx,
|
||||||
@ -447,7 +501,7 @@ SECP256K1_API int secp256k1_ecdsa_signature_parse_compact(
|
|||||||
* encoded numbers are out of range.
|
* encoded numbers are out of range.
|
||||||
*
|
*
|
||||||
* After the call, sig will always be initialized. If parsing failed or the
|
* After the call, sig will always be initialized. If parsing failed or the
|
||||||
* encoded numbers are out of range, signature validation with it is
|
* encoded numbers are out of range, signature verification with it is
|
||||||
* guaranteed to fail for every message and public key.
|
* guaranteed to fail for every message and public key.
|
||||||
*/
|
*/
|
||||||
SECP256K1_API int secp256k1_ecdsa_signature_parse_der(
|
SECP256K1_API int secp256k1_ecdsa_signature_parse_der(
|
||||||
@ -494,7 +548,7 @@ SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(
|
|||||||
*
|
*
|
||||||
* Returns: 1: correct signature
|
* Returns: 1: correct signature
|
||||||
* 0: incorrect or unparseable signature
|
* 0: incorrect or unparseable signature
|
||||||
* Args: ctx: a secp256k1 context object, initialized for verification.
|
* Args: ctx: a secp256k1 context object.
|
||||||
* In: sig: the signature being verified.
|
* In: sig: the signature being verified.
|
||||||
* msghash32: the 32-byte message hash being verified.
|
* msghash32: the 32-byte message hash being verified.
|
||||||
* The verifier must make sure to apply a cryptographic
|
* The verifier must make sure to apply a cryptographic
|
||||||
@ -511,7 +565,7 @@ SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(
|
|||||||
*
|
*
|
||||||
* If you need to accept ECDSA signatures from sources that do not obey this
|
* If you need to accept ECDSA signatures from sources that do not obey this
|
||||||
* rule, apply secp256k1_ecdsa_signature_normalize to the signature prior to
|
* rule, apply secp256k1_ecdsa_signature_normalize to the signature prior to
|
||||||
* validation, but be aware that doing so results in malleable signatures.
|
* verification, but be aware that doing so results in malleable signatures.
|
||||||
*
|
*
|
||||||
* For details, see the comments for that function.
|
* For details, see the comments for that function.
|
||||||
*/
|
*/
|
||||||
@ -582,7 +636,7 @@ SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_def
|
|||||||
*
|
*
|
||||||
* Returns: 1: signature created
|
* Returns: 1: signature created
|
||||||
* 0: the nonce generation function failed, or the secret key was invalid.
|
* 0: the nonce generation function failed, or the secret key was invalid.
|
||||||
* Args: ctx: pointer to a context object, initialized for signing.
|
* Args: ctx: pointer to a context object (not secp256k1_context_static).
|
||||||
* Out: sig: pointer to an array where the signature will be placed.
|
* Out: sig: pointer to an array where the signature will be placed.
|
||||||
* In: msghash32: the 32-byte message hash being signed.
|
* In: msghash32: the 32-byte message hash being signed.
|
||||||
* seckey: pointer to a 32-byte secret key.
|
* seckey: pointer to a 32-byte secret key.
|
||||||
@ -626,7 +680,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(
|
|||||||
*
|
*
|
||||||
* Returns: 1: secret was valid, public key stores.
|
* Returns: 1: secret was valid, public key stores.
|
||||||
* 0: secret was invalid, try again.
|
* 0: secret was invalid, try again.
|
||||||
* Args: ctx: pointer to a context object, initialized for signing.
|
* Args: ctx: pointer to a context object (not secp256k1_context_static).
|
||||||
* Out: pubkey: pointer to the created public key.
|
* Out: pubkey: pointer to the created public key.
|
||||||
* In: seckey: pointer to a 32-byte secret key.
|
* In: seckey: pointer to a 32-byte secret key.
|
||||||
*/
|
*/
|
||||||
@ -705,7 +759,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add(
|
|||||||
* Returns: 0 if the arguments are invalid or the resulting public key would be
|
* Returns: 0 if the arguments are invalid or the resulting public key would be
|
||||||
* invalid (only when the tweak is the negation of the corresponding
|
* invalid (only when the tweak is the negation of the corresponding
|
||||||
* secret key). 1 otherwise.
|
* secret key). 1 otherwise.
|
||||||
* Args: ctx: pointer to a context object initialized for validation.
|
* Args: ctx: pointer to a context object.
|
||||||
* In/Out: pubkey: pointer to a public key object. pubkey will be set to an
|
* In/Out: pubkey: pointer to a public key object. pubkey will be set to an
|
||||||
* invalid value if this function returns 0.
|
* invalid value if this function returns 0.
|
||||||
* In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
|
* In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
|
||||||
@ -750,7 +804,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul(
|
|||||||
/** Tweak a public key by multiplying it by a tweak value.
|
/** Tweak a public key by multiplying it by a tweak value.
|
||||||
*
|
*
|
||||||
* Returns: 0 if the arguments are invalid. 1 otherwise.
|
* Returns: 0 if the arguments are invalid. 1 otherwise.
|
||||||
* Args: ctx: pointer to a context object initialized for validation.
|
* Args: ctx: pointer to a context object.
|
||||||
* In/Out: pubkey: pointer to a public key object. pubkey will be set to an
|
* In/Out: pubkey: pointer to a public key object. pubkey will be set to an
|
||||||
* invalid value if this function returns 0.
|
* invalid value if this function returns 0.
|
||||||
* In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
|
* In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
|
||||||
@ -764,30 +818,41 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
|
|||||||
const unsigned char *tweak32
|
const unsigned char *tweak32
|
||||||
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
|
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
|
||||||
|
|
||||||
/** Updates the context randomization to protect against side-channel leakage.
|
/** Randomizes the context to provide enhanced protection against side-channel leakage.
|
||||||
* Returns: 1: randomization successfully updated or nothing to randomize
|
*
|
||||||
|
* Returns: 1: randomization successful (or called on copy of secp256k1_context_static)
|
||||||
* 0: error
|
* 0: error
|
||||||
* Args: ctx: pointer to a context object.
|
* Args: ctx: pointer to a context object.
|
||||||
* In: seed32: pointer to a 32-byte random seed (NULL resets to initial state)
|
* In: seed32: pointer to a 32-byte random seed (NULL resets to initial state)
|
||||||
*
|
*
|
||||||
* While secp256k1 code is written to be constant-time no matter what secret
|
* While secp256k1 code is written and tested to be constant-time no matter what
|
||||||
* values are, it's possible that a future compiler may output code which isn't,
|
* secret values are, it is possible that a compiler may output code which is not,
|
||||||
* and also that the CPU may not emit the same radio frequencies or draw the same
|
* and also that the CPU may not emit the same radio frequencies or draw the same
|
||||||
* amount power for all values.
|
* amount of power for all values. Randomization of the context shields against
|
||||||
|
* side-channel observations which aim to exploit secret-dependent behaviour in
|
||||||
|
* certain computations which involve secret keys.
|
||||||
*
|
*
|
||||||
* This function provides a seed which is combined into the blinding value: that
|
* It is highly recommended to call this function on contexts returned from
|
||||||
* blinding value is added before each multiplication (and removed afterwards) so
|
* secp256k1_context_create or secp256k1_context_clone (or from the corresponding
|
||||||
* that it does not affect function results, but shields against attacks which
|
* functions in secp256k1_preallocated.h) before using these contexts to call API
|
||||||
* rely on any input-dependent behaviour.
|
* functions that perform computations involving secret keys, e.g., signing and
|
||||||
|
* public key generation. It is possible to call this function more than once on
|
||||||
|
* the same context, and doing so before every few computations involving secret
|
||||||
|
* keys is recommended as a defense-in-depth measure.
|
||||||
*
|
*
|
||||||
* This function has currently an effect only on contexts initialized for signing
|
* Currently, the random seed is mainly used for blinding multiplications of a
|
||||||
* because randomization is currently used only for signing. However, this is not
|
* secret scalar with the elliptic curve base point. Multiplications of this
|
||||||
* guaranteed and may change in the future. It is safe to call this function on
|
* kind are performed by exactly those API functions which are documented to
|
||||||
* contexts not initialized for signing; then it will have no effect and return 1.
|
* require a context that is not the secp256k1_context_static. As a rule of thumb,
|
||||||
|
* these are all functions which take a secret key (or a keypair) as an input.
|
||||||
|
* A notable exception to that rule is the ECDH module, which relies on a different
|
||||||
|
* kind of elliptic curve point multiplication and thus does not benefit from
|
||||||
|
* enhanced protection against side-channel leakage currently.
|
||||||
*
|
*
|
||||||
* You should call this after secp256k1_context_create or
|
* It is safe call this function on a copy of secp256k1_context_static in writable
|
||||||
* secp256k1_context_clone (and secp256k1_context_preallocated_create or
|
* memory (e.g., obtained via secp256k1_context_clone). In that case, this
|
||||||
* secp256k1_context_clone, resp.), and you may call this repeatedly afterwards.
|
* function is guaranteed to return 1, but the call will have no effect because
|
||||||
|
* the static context (or a copy thereof) is not meant to be randomized.
|
||||||
*/
|
*/
|
||||||
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(
|
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(
|
||||||
secp256k1_context* ctx,
|
secp256k1_context* ctx,
|
||||||
|
@ -108,7 +108,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_from_pubke
|
|||||||
* invalid (only when the tweak is the negation of the corresponding
|
* invalid (only when the tweak is the negation of the corresponding
|
||||||
* secret key). 1 otherwise.
|
* secret key). 1 otherwise.
|
||||||
*
|
*
|
||||||
* Args: ctx: pointer to a context object initialized for verification.
|
* Args: ctx: pointer to a context object.
|
||||||
* Out: output_pubkey: pointer to a public key to store the result. Will be set
|
* Out: output_pubkey: pointer to a public key to store the result. Will be set
|
||||||
* to an invalid value if this function returns 0.
|
* to an invalid value if this function returns 0.
|
||||||
* In: internal_pubkey: pointer to an x-only pubkey to apply the tweak to.
|
* In: internal_pubkey: pointer to an x-only pubkey to apply the tweak to.
|
||||||
@ -137,7 +137,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_tweak_add(
|
|||||||
*
|
*
|
||||||
* Returns: 0 if the arguments are invalid or the tweaked pubkey is not the
|
* Returns: 0 if the arguments are invalid or the tweaked pubkey is not the
|
||||||
* result of tweaking the internal_pubkey with tweak32. 1 otherwise.
|
* result of tweaking the internal_pubkey with tweak32. 1 otherwise.
|
||||||
* Args: ctx: pointer to a context object initialized for verification.
|
* Args: ctx: pointer to a context object.
|
||||||
* In: tweaked_pubkey32: pointer to a serialized xonly_pubkey.
|
* In: tweaked_pubkey32: pointer to a serialized xonly_pubkey.
|
||||||
* tweaked_pk_parity: the parity of the tweaked pubkey (whose serialization
|
* tweaked_pk_parity: the parity of the tweaked pubkey (whose serialization
|
||||||
* is passed in as tweaked_pubkey32). This must match the
|
* is passed in as tweaked_pubkey32). This must match the
|
||||||
@ -159,7 +159,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_tweak_add_
|
|||||||
*
|
*
|
||||||
* Returns: 1: secret was valid, keypair is ready to use
|
* Returns: 1: secret was valid, keypair is ready to use
|
||||||
* 0: secret was invalid, try again with a different secret
|
* 0: secret was invalid, try again with a different secret
|
||||||
* Args: ctx: pointer to a context object, initialized for signing.
|
* Args: ctx: pointer to a context object (not secp256k1_context_static).
|
||||||
* Out: keypair: pointer to the created keypair.
|
* Out: keypair: pointer to the created keypair.
|
||||||
* In: seckey: pointer to a 32-byte secret key.
|
* In: seckey: pointer to a 32-byte secret key.
|
||||||
*/
|
*/
|
||||||
@ -228,7 +228,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_xonly_pub(
|
|||||||
* invalid (only when the tweak is the negation of the keypair's
|
* invalid (only when the tweak is the negation of the keypair's
|
||||||
* secret key). 1 otherwise.
|
* secret key). 1 otherwise.
|
||||||
*
|
*
|
||||||
* Args: ctx: pointer to a context object initialized for verification.
|
* Args: ctx: pointer to a context object.
|
||||||
* In/Out: keypair: pointer to a keypair to apply the tweak to. Will be set to
|
* In/Out: keypair: pointer to a keypair to apply the tweak to. Will be set to
|
||||||
* an invalid value if this function returns 0.
|
* an invalid value if this function returns 0.
|
||||||
* In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according
|
* In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according
|
||||||
|
@ -58,6 +58,8 @@ SECP256K1_API size_t secp256k1_context_preallocated_size(
|
|||||||
* bytes, as detailed above.
|
* bytes, as detailed above.
|
||||||
* flags: which parts of the context to initialize.
|
* flags: which parts of the context to initialize.
|
||||||
*
|
*
|
||||||
|
* See secp256k1_context_create (in secp256k1.h) for further details.
|
||||||
|
*
|
||||||
* See also secp256k1_context_randomize (in secp256k1.h)
|
* See also secp256k1_context_randomize (in secp256k1.h)
|
||||||
* and secp256k1_context_preallocated_destroy.
|
* and secp256k1_context_preallocated_destroy.
|
||||||
*/
|
*/
|
||||||
|
@ -72,7 +72,7 @@ SECP256K1_API int secp256k1_ecdsa_recoverable_signature_serialize_compact(
|
|||||||
*
|
*
|
||||||
* Returns: 1: signature created
|
* Returns: 1: signature created
|
||||||
* 0: the nonce generation function failed, or the secret key was invalid.
|
* 0: the nonce generation function failed, or the secret key was invalid.
|
||||||
* Args: ctx: pointer to a context object, initialized for signing.
|
* Args: ctx: pointer to a context object (not secp256k1_context_static).
|
||||||
* Out: sig: pointer to an array where the signature will be placed.
|
* Out: sig: pointer to an array where the signature will be placed.
|
||||||
* In: msghash32: the 32-byte message hash being signed.
|
* In: msghash32: the 32-byte message hash being signed.
|
||||||
* seckey: pointer to a 32-byte secret key.
|
* seckey: pointer to a 32-byte secret key.
|
||||||
@ -94,7 +94,7 @@ SECP256K1_API int secp256k1_ecdsa_sign_recoverable(
|
|||||||
*
|
*
|
||||||
* Returns: 1: public key successfully recovered (which guarantees a correct signature).
|
* Returns: 1: public key successfully recovered (which guarantees a correct signature).
|
||||||
* 0: otherwise.
|
* 0: otherwise.
|
||||||
* Args: ctx: pointer to a context object, initialized for verification.
|
* Args: ctx: pointer to a context object.
|
||||||
* Out: pubkey: pointer to the recovered public key.
|
* Out: pubkey: pointer to the recovered public key.
|
||||||
* In: sig: pointer to initialized signature that supports pubkey recovery.
|
* In: sig: pointer to initialized signature that supports pubkey recovery.
|
||||||
* msghash32: the 32-byte message hash assumed to be signed.
|
* msghash32: the 32-byte message hash assumed to be signed.
|
||||||
|
@ -106,7 +106,7 @@ typedef struct {
|
|||||||
* signatures from being valid in multiple contexts by accident.
|
* signatures from being valid in multiple contexts by accident.
|
||||||
*
|
*
|
||||||
* Returns 1 on success, 0 on failure.
|
* Returns 1 on success, 0 on failure.
|
||||||
* Args: ctx: pointer to a context object, initialized for signing.
|
* Args: ctx: pointer to a context object (not secp256k1_context_static).
|
||||||
* Out: sig64: pointer to a 64-byte array to store the serialized signature.
|
* Out: sig64: pointer to a 64-byte array to store the serialized signature.
|
||||||
* In: msg32: the 32-byte message being signed.
|
* In: msg32: the 32-byte message being signed.
|
||||||
* keypair: pointer to an initialized keypair.
|
* keypair: pointer to an initialized keypair.
|
||||||
@ -161,7 +161,7 @@ SECP256K1_API int secp256k1_schnorrsig_sign_custom(
|
|||||||
*
|
*
|
||||||
* Returns: 1: correct signature
|
* Returns: 1: correct signature
|
||||||
* 0: incorrect signature
|
* 0: incorrect signature
|
||||||
* Args: ctx: a secp256k1 context object, initialized for verification.
|
* Args: ctx: a secp256k1 context object.
|
||||||
* In: sig64: pointer to the 64-byte signature to verify.
|
* In: sig64: pointer to the 64-byte signature to verify.
|
||||||
* msg: the message being verified. Can only be NULL if msglen is 0.
|
* msg: the message being verified. Can only be NULL if msglen is 0.
|
||||||
* msglen: length of the message
|
* msglen: length of the message
|
||||||
|
@ -359,7 +359,7 @@ void test_keypair(void) {
|
|||||||
secp256k1_context *none = api_test_context(SECP256K1_CONTEXT_NONE, &ecount);
|
secp256k1_context *none = api_test_context(SECP256K1_CONTEXT_NONE, &ecount);
|
||||||
secp256k1_context *sign = api_test_context(SECP256K1_CONTEXT_SIGN, &ecount);
|
secp256k1_context *sign = api_test_context(SECP256K1_CONTEXT_SIGN, &ecount);
|
||||||
secp256k1_context *verify = api_test_context(SECP256K1_CONTEXT_VERIFY, &ecount);
|
secp256k1_context *verify = api_test_context(SECP256K1_CONTEXT_VERIFY, &ecount);
|
||||||
secp256k1_context *sttc = secp256k1_context_clone(secp256k1_context_no_precomp);
|
secp256k1_context *sttc = secp256k1_context_clone(secp256k1_context_static);
|
||||||
secp256k1_context_set_error_callback(sttc, counting_illegal_callback_fn, &ecount);
|
secp256k1_context_set_error_callback(sttc, counting_illegal_callback_fn, &ecount);
|
||||||
secp256k1_context_set_illegal_callback(sttc, counting_illegal_callback_fn, &ecount);
|
secp256k1_context_set_illegal_callback(sttc, counting_illegal_callback_fn, &ecount);
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ void test_ecdsa_recovery_api(void) {
|
|||||||
secp256k1_context *sign = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
|
secp256k1_context *sign = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
|
||||||
secp256k1_context *vrfy = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
|
secp256k1_context *vrfy = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
|
||||||
secp256k1_context *both = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
|
secp256k1_context *both = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
|
||||||
secp256k1_context *sttc = secp256k1_context_clone(secp256k1_context_no_precomp);
|
secp256k1_context *sttc = secp256k1_context_clone(secp256k1_context_static);
|
||||||
secp256k1_pubkey pubkey;
|
secp256k1_pubkey pubkey;
|
||||||
secp256k1_pubkey recpubkey;
|
secp256k1_pubkey recpubkey;
|
||||||
secp256k1_ecdsa_signature normal_sig;
|
secp256k1_ecdsa_signature normal_sig;
|
||||||
|
@ -132,7 +132,7 @@ void test_schnorrsig_api(void) {
|
|||||||
secp256k1_context *sign = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
|
secp256k1_context *sign = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
|
||||||
secp256k1_context *vrfy = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
|
secp256k1_context *vrfy = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
|
||||||
secp256k1_context *both = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
|
secp256k1_context *both = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
|
||||||
secp256k1_context *sttc = secp256k1_context_clone(secp256k1_context_no_precomp);
|
secp256k1_context *sttc = secp256k1_context_clone(secp256k1_context_static);
|
||||||
int ecount;
|
int ecount;
|
||||||
|
|
||||||
secp256k1_context_set_error_callback(none, counting_illegal_callback_fn, &ecount);
|
secp256k1_context_set_error_callback(none, counting_illegal_callback_fn, &ecount);
|
||||||
|
@ -64,13 +64,20 @@ struct secp256k1_context_struct {
|
|||||||
int declassify;
|
int declassify;
|
||||||
};
|
};
|
||||||
|
|
||||||
static const secp256k1_context secp256k1_context_no_precomp_ = {
|
static const secp256k1_context secp256k1_context_static_ = {
|
||||||
{ 0 },
|
{ 0 },
|
||||||
{ secp256k1_default_illegal_callback_fn, 0 },
|
{ secp256k1_default_illegal_callback_fn, 0 },
|
||||||
{ secp256k1_default_error_callback_fn, 0 },
|
{ secp256k1_default_error_callback_fn, 0 },
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_no_precomp_;
|
const secp256k1_context *secp256k1_context_static = &secp256k1_context_static_;
|
||||||
|
const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_static_;
|
||||||
|
|
||||||
|
void secp256k1_selftest(void) {
|
||||||
|
if (!secp256k1_selftest_passes()) {
|
||||||
|
secp256k1_callback_call(&default_error_callback, "self test failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
size_t secp256k1_context_preallocated_size(unsigned int flags) {
|
size_t secp256k1_context_preallocated_size(unsigned int flags) {
|
||||||
size_t ret = sizeof(secp256k1_context);
|
size_t ret = sizeof(secp256k1_context);
|
||||||
@ -96,9 +103,7 @@ secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigne
|
|||||||
size_t prealloc_size;
|
size_t prealloc_size;
|
||||||
secp256k1_context* ret;
|
secp256k1_context* ret;
|
||||||
|
|
||||||
if (!secp256k1_selftest()) {
|
secp256k1_selftest();
|
||||||
secp256k1_callback_call(&default_error_callback, "self test failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
prealloc_size = secp256k1_context_preallocated_size(flags);
|
prealloc_size = secp256k1_context_preallocated_size(flags);
|
||||||
if (prealloc_size == 0) {
|
if (prealloc_size == 0) {
|
||||||
@ -150,7 +155,7 @@ secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) {
|
void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) {
|
||||||
ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
|
ARG_CHECK_NO_RETURN(ctx != secp256k1_context_static);
|
||||||
if (ctx != NULL) {
|
if (ctx != NULL) {
|
||||||
secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
|
secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
|
||||||
}
|
}
|
||||||
@ -164,7 +169,7 @@ void secp256k1_context_destroy(secp256k1_context* ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
|
void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
|
||||||
ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
|
ARG_CHECK_NO_RETURN(ctx != secp256k1_context_static);
|
||||||
if (fun == NULL) {
|
if (fun == NULL) {
|
||||||
fun = secp256k1_default_illegal_callback_fn;
|
fun = secp256k1_default_illegal_callback_fn;
|
||||||
}
|
}
|
||||||
@ -173,7 +178,7 @@ void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
|
void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
|
||||||
ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
|
ARG_CHECK_NO_RETURN(ctx != secp256k1_context_static);
|
||||||
if (fun == NULL) {
|
if (fun == NULL) {
|
||||||
fun = secp256k1_default_error_callback_fn;
|
fun = secp256k1_default_error_callback_fn;
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ static int secp256k1_selftest_sha256(void) {
|
|||||||
return secp256k1_memcmp_var(out, output32, 32) == 0;
|
return secp256k1_memcmp_var(out, output32, 32) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int secp256k1_selftest(void) {
|
static int secp256k1_selftest_passes(void) {
|
||||||
return secp256k1_selftest_sha256();
|
return secp256k1_selftest_sha256();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
17
src/tests.c
17
src/tests.c
@ -141,6 +141,11 @@ void random_scalar_order_b32(unsigned char *b32) {
|
|||||||
secp256k1_scalar_get_b32(b32, &num);
|
secp256k1_scalar_get_b32(b32, &num);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void run_selftest_tests(void) {
|
||||||
|
/* Test public API */
|
||||||
|
secp256k1_selftest();
|
||||||
|
}
|
||||||
|
|
||||||
void run_context_tests(int use_prealloc) {
|
void run_context_tests(int use_prealloc) {
|
||||||
secp256k1_pubkey pubkey;
|
secp256k1_pubkey pubkey;
|
||||||
secp256k1_pubkey zero_pubkey;
|
secp256k1_pubkey zero_pubkey;
|
||||||
@ -164,12 +169,15 @@ void run_context_tests(int use_prealloc) {
|
|||||||
secp256k1_scalar msg, key, nonce;
|
secp256k1_scalar msg, key, nonce;
|
||||||
secp256k1_scalar sigr, sigs;
|
secp256k1_scalar sigr, sigs;
|
||||||
|
|
||||||
|
/* Check that deprecated secp256k1_context_no_precomp is an alias to secp256k1_context_static. */
|
||||||
|
CHECK(secp256k1_context_no_precomp == secp256k1_context_static);
|
||||||
|
|
||||||
if (use_prealloc) {
|
if (use_prealloc) {
|
||||||
none_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE));
|
none_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE));
|
||||||
sign_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN));
|
sign_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN));
|
||||||
vrfy_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY));
|
vrfy_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY));
|
||||||
both_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY));
|
both_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY));
|
||||||
sttc_prealloc = malloc(secp256k1_context_preallocated_clone_size(secp256k1_context_no_precomp));
|
sttc_prealloc = malloc(secp256k1_context_preallocated_clone_size(secp256k1_context_static));
|
||||||
CHECK(none_prealloc != NULL);
|
CHECK(none_prealloc != NULL);
|
||||||
CHECK(sign_prealloc != NULL);
|
CHECK(sign_prealloc != NULL);
|
||||||
CHECK(vrfy_prealloc != NULL);
|
CHECK(vrfy_prealloc != NULL);
|
||||||
@ -179,13 +187,13 @@ void run_context_tests(int use_prealloc) {
|
|||||||
sign = secp256k1_context_preallocated_create(sign_prealloc, SECP256K1_CONTEXT_SIGN);
|
sign = secp256k1_context_preallocated_create(sign_prealloc, SECP256K1_CONTEXT_SIGN);
|
||||||
vrfy = secp256k1_context_preallocated_create(vrfy_prealloc, SECP256K1_CONTEXT_VERIFY);
|
vrfy = secp256k1_context_preallocated_create(vrfy_prealloc, SECP256K1_CONTEXT_VERIFY);
|
||||||
both = secp256k1_context_preallocated_create(both_prealloc, SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
|
both = secp256k1_context_preallocated_create(both_prealloc, SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
|
||||||
sttc = secp256k1_context_preallocated_clone(secp256k1_context_no_precomp, sttc_prealloc);
|
sttc = secp256k1_context_preallocated_clone(secp256k1_context_static, sttc_prealloc);
|
||||||
} else {
|
} else {
|
||||||
none = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
|
none = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
|
||||||
sign = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
|
sign = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
|
||||||
vrfy = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
|
vrfy = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
|
||||||
both = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
|
both = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
|
||||||
sttc = secp256k1_context_clone(secp256k1_context_no_precomp);
|
sttc = secp256k1_context_clone(secp256k1_context_static);
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(&zero_pubkey, 0, sizeof(zero_pubkey));
|
memset(&zero_pubkey, 0, sizeof(zero_pubkey));
|
||||||
@ -5799,7 +5807,7 @@ void run_ec_pubkey_parse_test(void) {
|
|||||||
ecount = 0;
|
ecount = 0;
|
||||||
VG_UNDEF(&pubkey, sizeof(pubkey));
|
VG_UNDEF(&pubkey, sizeof(pubkey));
|
||||||
CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeyc, 65) == 1);
|
CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeyc, 65) == 1);
|
||||||
CHECK(secp256k1_ec_pubkey_parse(secp256k1_context_no_precomp, &pubkey, pubkeyc, 65) == 1);
|
CHECK(secp256k1_ec_pubkey_parse(secp256k1_context_static, &pubkey, pubkeyc, 65) == 1);
|
||||||
VG_CHECK(&pubkey, sizeof(pubkey));
|
VG_CHECK(&pubkey, sizeof(pubkey));
|
||||||
CHECK(ecount == 0);
|
CHECK(ecount == 0);
|
||||||
VG_UNDEF(&ge, sizeof(ge));
|
VG_UNDEF(&ge, sizeof(ge));
|
||||||
@ -7385,6 +7393,7 @@ int main(int argc, char **argv) {
|
|||||||
secp256k1_testrand_init(argc > 2 ? argv[2] : NULL);
|
secp256k1_testrand_init(argc > 2 ? argv[2] : NULL);
|
||||||
|
|
||||||
/* initialize */
|
/* initialize */
|
||||||
|
run_selftest_tests();
|
||||||
run_context_tests(0);
|
run_context_tests(0);
|
||||||
run_context_tests(1);
|
run_context_tests(1);
|
||||||
run_scratch_tests();
|
run_scratch_tests();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user