Bitcoin Core 31.99.0
P2P Digital Currency
ctime_tests.c
Go to the documentation of this file.
1/***********************************************************************
2 * Copyright (c) 2020 Gregory Maxwell *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5 ***********************************************************************/
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10
11#include "../include/secp256k1.h"
12#include "assumptions.h"
13#include "checkmem.h"
14
15#if !SECP256K1_CHECKMEM_ENABLED
16# error "This tool cannot be compiled without memory-checking interface (valgrind or msan)"
17#endif
18
19#ifdef ENABLE_MODULE_ECDH
20# include "../include/secp256k1_ecdh.h"
21#endif
22
23#ifdef ENABLE_MODULE_RECOVERY
24# include "../include/secp256k1_recovery.h"
25#endif
26
27#ifdef ENABLE_MODULE_EXTRAKEYS
28# include "../include/secp256k1_extrakeys.h"
29#endif
30
31#ifdef ENABLE_MODULE_SCHNORRSIG
32#include "../include/secp256k1_schnorrsig.h"
33#endif
34
35#ifdef ENABLE_MODULE_MUSIG
36#include "../include/secp256k1_musig.h"
37#endif
38
39#ifdef ENABLE_MODULE_ELLSWIFT
40#include "../include/secp256k1_ellswift.h"
41#endif
42
43#if defined(__GNUC__)
44# pragma GCC diagnostic push
45# pragma GCC diagnostic warning "-Wunused-function"
46#endif
47
48static void run_tests(secp256k1_context *ctx, unsigned char *key);
49
50int main(void) {
52 unsigned char key[32];
53 int ret, i;
54
56 fprintf(stderr, "This test can only usefully be run inside valgrind because it was not compiled under msan.\n");
57 fprintf(stderr, "Usage: valgrind ./ctime_tests (or with Autotools: libtool --mode=execute valgrind ./ctime_tests)\n");
58 return EXIT_FAILURE;
59 }
64 for (i = 0; i < 32; i++) {
65 key[i] = i + 65;
66 }
67
68 run_tests(ctx, key);
69
70 /* Test context randomisation. Do this last because it leaves the context
71 * tainted. */
75 CHECK(ret);
76
78 return EXIT_SUCCESS;
79}
80
81static void run_tests(secp256k1_context *ctx, unsigned char *key) {
83 secp256k1_pubkey pubkey;
84 size_t siglen = 74;
85 size_t outputlen = 33;
86 int i;
87 int ret;
88 unsigned char msg[32];
89 unsigned char sig[74];
90 unsigned char spubkey[33];
91#ifdef ENABLE_MODULE_RECOVERY
92 secp256k1_ecdsa_recoverable_signature recoverable_signature;
93 int recid;
94#endif
95#ifdef ENABLE_MODULE_EXTRAKEYS
96 secp256k1_keypair keypair;
97#endif
98#ifdef ENABLE_MODULE_ELLSWIFT
99 unsigned char ellswift[64];
100 static const unsigned char prefix[64] = {'t', 'e', 's', 't'};
101#endif
102
103 for (i = 0; i < 32; i++) {
104 msg[i] = i + 1;
105 }
106
107 /* Test keygen. */
109 ret = secp256k1_ec_pubkey_create(ctx, &pubkey, key);
112 CHECK(ret);
113 CHECK(secp256k1_ec_pubkey_serialize(ctx, spubkey, &outputlen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
114
115 /* Test signing. */
117 ret = secp256k1_ecdsa_sign(ctx, &signature, msg, key, NULL, NULL);
120 CHECK(ret);
121 CHECK(secp256k1_ecdsa_signature_serialize_der(ctx, sig, &siglen, &signature));
122
123#ifdef ENABLE_MODULE_ECDH
124 /* Test ECDH. */
126 ret = secp256k1_ecdh(ctx, msg, &pubkey, key, NULL, NULL);
128 CHECK(ret == 1);
129#endif
130
131#ifdef ENABLE_MODULE_RECOVERY
132 /* Test signing a recoverable signature. */
134 ret = secp256k1_ecdsa_sign_recoverable(ctx, &recoverable_signature, msg, key, NULL, NULL);
135 SECP256K1_CHECKMEM_DEFINE(&recoverable_signature, sizeof(recoverable_signature));
137 CHECK(ret);
138 CHECK(secp256k1_ecdsa_recoverable_signature_serialize_compact(ctx, sig, &recid, &recoverable_signature));
139 CHECK(recid >= 0 && recid <= 3);
140#endif
141
145 CHECK(ret == 1);
146
150 CHECK(ret == 1);
151
156 CHECK(ret == 1);
157
162 CHECK(ret == 1);
163
164 /* Test keypair_create and keypair_xonly_tweak_add. */
165#ifdef ENABLE_MODULE_EXTRAKEYS
167 ret = secp256k1_keypair_create(ctx, &keypair, key);
169 CHECK(ret == 1);
170
171 /* The tweak is not treated as a secret in keypair_tweak_add */
173 ret = secp256k1_keypair_xonly_tweak_add(ctx, &keypair, msg);
175 CHECK(ret == 1);
176
178 SECP256K1_CHECKMEM_UNDEFINE(&keypair, sizeof(keypair));
179 ret = secp256k1_keypair_sec(ctx, key, &keypair);
181 CHECK(ret == 1);
182#endif
183
184#ifdef ENABLE_MODULE_SCHNORRSIG
186 ret = secp256k1_keypair_create(ctx, &keypair, key);
188 CHECK(ret == 1);
189 ret = secp256k1_schnorrsig_sign32(ctx, sig, msg, &keypair, NULL);
191 CHECK(ret == 1);
192#endif
193
194#ifdef ENABLE_MODULE_MUSIG
195 {
197 const secp256k1_pubkey *pk_ptr[1];
199 unsigned char session_secrand[32];
200 uint64_t nonrepeating_cnt = 0;
203 const secp256k1_musig_pubnonce *pubnonce_ptr[1];
207 secp256k1_musig_partial_sig partial_sig;
208 unsigned char extra_input[32];
209
210 pk_ptr[0] = &pk;
211 pubnonce_ptr[0] = &pubnonce;
213 memcpy(session_secrand, key, sizeof(session_secrand));
214 session_secrand[0] = session_secrand[0] + 1;
215 memcpy(extra_input, key, sizeof(extra_input));
216 extra_input[0] = extra_input[0] + 2;
217
218 CHECK(secp256k1_keypair_create(ctx, &keypair, key));
219 CHECK(secp256k1_keypair_pub(ctx, &pk, &keypair));
220 CHECK(secp256k1_musig_pubkey_agg(ctx, &agg_pk, &cache, pk_ptr, 1));
221
223 SECP256K1_CHECKMEM_UNDEFINE(session_secrand, sizeof(session_secrand));
224 SECP256K1_CHECKMEM_UNDEFINE(extra_input, sizeof(extra_input));
225 ret = secp256k1_musig_nonce_gen(ctx, &secnonce, &pubnonce, session_secrand, key, &pk, msg, &cache, extra_input);
227 CHECK(ret == 1);
228 ret = secp256k1_musig_nonce_gen_counter(ctx, &secnonce, &pubnonce, nonrepeating_cnt, &keypair, msg, &cache, extra_input);
230 CHECK(ret == 1);
231
232 CHECK(secp256k1_musig_nonce_agg(ctx, &aggnonce, pubnonce_ptr, 1));
233 /* Make sure that previous tests don't undefine msg. It's not used as a secret here. */
235 CHECK(secp256k1_musig_nonce_process(ctx, &session, &aggnonce, msg, &cache) == 1);
236
237 ret = secp256k1_keypair_create(ctx, &keypair, key);
239 CHECK(ret == 1);
240 ret = secp256k1_musig_partial_sign(ctx, &partial_sig, &secnonce, &keypair, &cache, &session);
242 CHECK(ret == 1);
243 }
244#endif
245
246#ifdef ENABLE_MODULE_ELLSWIFT
248 ret = secp256k1_ellswift_create(ctx, ellswift, key, NULL);
250 CHECK(ret == 1);
251
253 ret = secp256k1_ellswift_create(ctx, ellswift, key, ellswift);
255 CHECK(ret == 1);
256
257 for (i = 0; i < 2; i++) {
259 SECP256K1_CHECKMEM_DEFINE(&ellswift, sizeof(ellswift));
260 ret = secp256k1_ellswift_xdh(ctx, msg, ellswift, ellswift, key, i, secp256k1_ellswift_xdh_hash_function_bip324, NULL);
262 CHECK(ret == 1);
263
265 SECP256K1_CHECKMEM_DEFINE(&ellswift, sizeof(ellswift));
266 ret = secp256k1_ellswift_xdh(ctx, msg, ellswift, ellswift, key, i, secp256k1_ellswift_xdh_hash_function_prefix, (void *)prefix);
268 CHECK(ret == 1);
269 }
270
271#endif
272}
273
274#if defined(__GNUC__)
275# pragma GCC diagnostic pop
276#endif
int ret
return EXIT_SUCCESS
#define SECP256K1_CHECKMEM_UNDEFINE(p, len)
Definition: checkmem.h:105
#define SECP256K1_CHECKMEM_DEFINE(p, len)
Definition: checkmem.h:106
#define SECP256K1_CHECKMEM_RUNNING()
Definition: checkmem.h:108
static void run_tests(secp256k1_context *ctx, unsigned char *key)
Definition: ctime_tests.c:81
int main(void)
Definition: ctime_tests.c:50
#define CHECK(cond)
Unconditional failure on condition failure.
Definition: util.h:35
const char * prefix
Definition: rest.cpp:1143
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition: secp256k1.c:190
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_mul(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a secret key by multiplying it by a tweak.
Definition: secp256k1.c:736
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
Randomizes the context to provide enhanced protection against side-channel leakage.
Definition: secp256k1.c:779
#define SECP256K1_CONTEXT_DECLASSIFY
Definition: secp256k1.h:222
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_negate(const secp256k1_context *ctx, unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Negates a secret key in place.
Definition: secp256k1.c:654
SECP256K1_API int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize a pubkey object into a serialized byte sequence.
Definition: secp256k1.c:287
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Verify an elliptic curve secret key.
Definition: secp256k1.c:615
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object (in dynamically allocated memory).
Definition: secp256k1.c:144
SECP256K1_API int secp256k1_ecdsa_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create an ECDSA signature.
Definition: secp256k1.c:601
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the public key for a secret key.
Definition: secp256k1.c:636
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition: secp256k1.h:225
SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize an ECDSA signature in DER format.
Definition: secp256k1.c:433
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_add(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a secret key by adding tweak to it.
Definition: secp256k1.c:696
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdh(const secp256k1_context *ctx, unsigned char *output, const secp256k1_pubkey *pubkey, const unsigned char *seckey, secp256k1_ecdh_hash_function hashfp, void *data) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Compute an EC Diffie-Hellman secret in constant time.
Definition: main_impl.h:34
SECP256K1_API const secp256k1_ellswift_xdh_hash_function secp256k1_ellswift_xdh_hash_function_prefix
An implementation of an secp256k1_ellswift_xdh_hash_function which uses SHA256(prefix64 || ell_a64 ||...
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ellswift_create(const secp256k1_context *ctx, unsigned char *ell64, const unsigned char *seckey32, const unsigned char *auxrnd32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute an ElligatorSwift public key for a secret key.
Definition: main_impl.h:431
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT 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) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(7)
Given a private key, and ElligatorSwift public keys sent in both directions, compute a shared secret ...
Definition: main_impl.h:534
SECP256K1_API const secp256k1_ellswift_xdh_hash_function secp256k1_ellswift_xdh_hash_function_bip324
An implementation of an secp256k1_ellswift_xdh_hash_function compatible with BIP324.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_create(const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the keypair for a valid secret key.
Definition: main_impl.h:196
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_xonly_tweak_add(const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a keypair by adding tweak32 to the secret key and updating the public key accordingly.
Definition: main_impl.h:255
SECP256K1_API int secp256k1_keypair_pub(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const secp256k1_keypair *keypair) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Get the public key from a keypair.
Definition: main_impl.h:224
SECP256K1_API int secp256k1_keypair_sec(const secp256k1_context *ctx, unsigned char *seckey, const secp256k1_keypair *keypair) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Get the secret key from a keypair.
Definition: main_impl.h:214
SECP256K1_API int secp256k1_musig_nonce_agg(const secp256k1_context *ctx, secp256k1_musig_aggnonce *aggnonce, const secp256k1_musig_pubnonce *const *pubnonces, size_t n_pubnonces) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Aggregates the nonces of all signers into a single nonce.
Definition: session_impl.h:510
SECP256K1_API int secp256k1_musig_partial_sign(const secp256k1_context *ctx, secp256k1_musig_partial_sig *partial_sig, secp256k1_musig_secnonce *secnonce, const secp256k1_keypair *keypair, const secp256k1_musig_keyagg_cache *keyagg_cache, const secp256k1_musig_session *session) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(6)
Produces a partial signature.
Definition: session_impl.h:632
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_nonce_gen(const secp256k1_context *ctx, secp256k1_musig_secnonce *secnonce, secp256k1_musig_pubnonce *pubnonce, unsigned char *session_secrand32, const unsigned char *seckey, const secp256k1_pubkey *pubkey, const unsigned char *msg32, const secp256k1_musig_keyagg_cache *keyagg_cache, const unsigned char *extra_input32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(6)
Starts a signing session by generating a nonce.
Definition: session_impl.h:437
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_nonce_gen_counter(const secp256k1_context *ctx, secp256k1_musig_secnonce *secnonce, secp256k1_musig_pubnonce *pubnonce, uint64_t nonrepeating_cnt, const secp256k1_keypair *keypair, const unsigned char *msg32, const secp256k1_musig_keyagg_cache *keyagg_cache, const unsigned char *extra_input32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(5)
Alternative way to generate a nonce and start a signing session.
Definition: session_impl.h:465
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubkey_agg(const secp256k1_context *ctx, secp256k1_xonly_pubkey *agg_pk, secp256k1_musig_keyagg_cache *keyagg_cache, const secp256k1_pubkey *const *pubkeys, size_t n_pubkeys) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(4)
Computes an aggregate public key and uses it to initialize a keyagg_cache.
Definition: keyagg_impl.h:156
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_nonce_process(const secp256k1_context *ctx, secp256k1_musig_session *session, const secp256k1_musig_aggnonce *aggnonce, const unsigned char *msg32, const secp256k1_musig_keyagg_cache *keyagg_cache) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5)
Takes the aggregate nonce and creates a session that is required for signing and verification of part...
Definition: session_impl.h:586
SECP256K1_API int secp256k1_ecdsa_recoverable_signature_serialize_compact(const secp256k1_context *ctx, unsigned char *output64, int *recid, const secp256k1_ecdsa_recoverable_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize an ECDSA signature in compact format (64 bytes + recovery id).
Definition: main_impl.h:60
SECP256K1_API int secp256k1_ecdsa_sign_recoverable(const secp256k1_context *ctx, secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create a recoverable ECDSA signature.
Definition: main_impl.h:123
SECP256K1_API int secp256k1_schnorrsig_sign32(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg32, const secp256k1_keypair *keypair, const unsigned char *aux_rand32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create a Schnorr signature.
Definition: main_impl.h:188
Opaque data structure that holds a parsed ECDSA signature, supporting pubkey recovery.
Opaque data structure that holds a parsed ECDSA signature.
Definition: secp256k1.h:75
Opaque data structure that holds a keypair consisting of a secret and a public key.
Opaque data structure that holds an aggregate public nonce.
This module implements BIP 327 "MuSig2 for BIP340-compatible Multi-Signatures" (https://github....
Opaque data structure that holds a partial MuSig signature.
Opaque data structure that holds a signer's public nonce.
Opaque data structure that holds a signer's secret nonce.
Opaque data structure that holds a MuSig session.
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:62
Opaque data structure that holds a parsed and valid "x-only" public key.