Bitcoin Core  27.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 
9 #include "../include/secp256k1.h"
10 #include "assumptions.h"
11 #include "checkmem.h"
12 
13 #if !SECP256K1_CHECKMEM_ENABLED
14 # error "This tool cannot be compiled without memory-checking interface (valgrind or msan)"
15 #endif
16 
17 #ifdef ENABLE_MODULE_ECDH
18 # include "../include/secp256k1_ecdh.h"
19 #endif
20 
21 #ifdef ENABLE_MODULE_RECOVERY
22 # include "../include/secp256k1_recovery.h"
23 #endif
24 
25 #ifdef ENABLE_MODULE_EXTRAKEYS
26 # include "../include/secp256k1_extrakeys.h"
27 #endif
28 
29 #ifdef ENABLE_MODULE_SCHNORRSIG
30 #include "../include/secp256k1_schnorrsig.h"
31 #endif
32 
33 #ifdef ENABLE_MODULE_ELLSWIFT
34 #include "../include/secp256k1_ellswift.h"
35 #endif
36 
37 static void run_tests(secp256k1_context *ctx, unsigned char *key);
38 
39 int main(void) {
40  secp256k1_context* ctx;
41  unsigned char key[32];
42  int ret, i;
43 
45  fprintf(stderr, "This test can only usefully be run inside valgrind because it was not compiled under msan.\n");
46  fprintf(stderr, "Usage: libtool --mode=execute valgrind ./ctime_tests\n");
47  return 1;
48  }
53  for (i = 0; i < 32; i++) {
54  key[i] = i + 65;
55  }
56 
57  run_tests(ctx, key);
58 
59  /* Test context randomisation. Do this last because it leaves the context
60  * tainted. */
62  ret = secp256k1_context_randomize(ctx, key);
64  CHECK(ret);
65 
67  return 0;
68 }
69 
70 static void run_tests(secp256k1_context *ctx, unsigned char *key) {
71  secp256k1_ecdsa_signature signature;
72  secp256k1_pubkey pubkey;
73  size_t siglen = 74;
74  size_t outputlen = 33;
75  int i;
76  int ret;
77  unsigned char msg[32];
78  unsigned char sig[74];
79  unsigned char spubkey[33];
80 #ifdef ENABLE_MODULE_RECOVERY
81  secp256k1_ecdsa_recoverable_signature recoverable_signature;
82  int recid;
83 #endif
84 #ifdef ENABLE_MODULE_EXTRAKEYS
85  secp256k1_keypair keypair;
86 #endif
87 #ifdef ENABLE_MODULE_ELLSWIFT
88  unsigned char ellswift[64];
89  static const unsigned char prefix[64] = {'t', 'e', 's', 't'};
90 #endif
91 
92  for (i = 0; i < 32; i++) {
93  msg[i] = i + 1;
94  }
95 
96  /* Test keygen. */
98  ret = secp256k1_ec_pubkey_create(ctx, &pubkey, key);
100  SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
101  CHECK(ret);
102  CHECK(secp256k1_ec_pubkey_serialize(ctx, spubkey, &outputlen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
103 
104  /* Test signing. */
106  ret = secp256k1_ecdsa_sign(ctx, &signature, msg, key, NULL, NULL);
108  SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
109  CHECK(ret);
110  CHECK(secp256k1_ecdsa_signature_serialize_der(ctx, sig, &siglen, &signature));
111 
112 #ifdef ENABLE_MODULE_ECDH
113  /* Test ECDH. */
115  ret = secp256k1_ecdh(ctx, msg, &pubkey, key, NULL, NULL);
116  SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
117  CHECK(ret == 1);
118 #endif
119 
120 #ifdef ENABLE_MODULE_RECOVERY
121  /* Test signing a recoverable signature. */
123  ret = secp256k1_ecdsa_sign_recoverable(ctx, &recoverable_signature, msg, key, NULL, NULL);
124  SECP256K1_CHECKMEM_DEFINE(&recoverable_signature, sizeof(recoverable_signature));
125  SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
126  CHECK(ret);
127  CHECK(secp256k1_ecdsa_recoverable_signature_serialize_compact(ctx, sig, &recid, &recoverable_signature));
128  CHECK(recid >= 0 && recid <= 3);
129 #endif
130 
132  ret = secp256k1_ec_seckey_verify(ctx, key);
133  SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
134  CHECK(ret == 1);
135 
137  ret = secp256k1_ec_seckey_negate(ctx, key);
138  SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
139  CHECK(ret == 1);
140 
144  SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
145  CHECK(ret == 1);
146 
150  SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
151  CHECK(ret == 1);
152 
153  /* Test keypair_create and keypair_xonly_tweak_add. */
154 #ifdef ENABLE_MODULE_EXTRAKEYS
156  ret = secp256k1_keypair_create(ctx, &keypair, key);
157  SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
158  CHECK(ret == 1);
159 
160  /* The tweak is not treated as a secret in keypair_tweak_add */
162  ret = secp256k1_keypair_xonly_tweak_add(ctx, &keypair, msg);
163  SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
164  CHECK(ret == 1);
165 
167  SECP256K1_CHECKMEM_UNDEFINE(&keypair, sizeof(keypair));
168  ret = secp256k1_keypair_sec(ctx, key, &keypair);
169  SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
170  CHECK(ret == 1);
171 #endif
172 
173 #ifdef ENABLE_MODULE_SCHNORRSIG
175  ret = secp256k1_keypair_create(ctx, &keypair, key);
176  SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
177  CHECK(ret == 1);
178  ret = secp256k1_schnorrsig_sign32(ctx, sig, msg, &keypair, NULL);
179  SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
180  CHECK(ret == 1);
181 #endif
182 
183 #ifdef ENABLE_MODULE_ELLSWIFT
185  ret = secp256k1_ellswift_create(ctx, ellswift, key, NULL);
186  SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
187  CHECK(ret == 1);
188 
190  ret = secp256k1_ellswift_create(ctx, ellswift, key, ellswift);
191  SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
192  CHECK(ret == 1);
193 
194  for (i = 0; i < 2; i++) {
196  SECP256K1_CHECKMEM_DEFINE(&ellswift, sizeof(ellswift));
197  ret = secp256k1_ellswift_xdh(ctx, msg, ellswift, ellswift, key, i, secp256k1_ellswift_xdh_hash_function_bip324, NULL);
198  SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
199  CHECK(ret == 1);
200 
202  SECP256K1_CHECKMEM_DEFINE(&ellswift, sizeof(ellswift));
203  ret = secp256k1_ellswift_xdh(ctx, msg, ellswift, ellswift, key, i, secp256k1_ellswift_xdh_hash_function_prefix, (void *)prefix);
204  SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
205  CHECK(ret == 1);
206  }
207 
208 #endif
209 }
int ret
#define SECP256K1_CHECKMEM_UNDEFINE(p, len)
Definition: checkmem.h:90
#define SECP256K1_CHECKMEM_DEFINE(p, len)
Definition: checkmem.h:91
#define SECP256K1_CHECKMEM_RUNNING()
Definition: checkmem.h:93
static void run_tests(secp256k1_context *ctx, unsigned char *key)
Definition: ctime_tests.c:70
int main(void)
Definition: ctime_tests.c:39
#define CHECK(cond)
Unconditional failure on condition failure.
Definition: util.h:35
const char * prefix
Definition: rest.cpp:1006
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:187
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:721
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:768
#define SECP256K1_CONTEXT_DECLASSIFY
Definition: secp256k1.h:212
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:631
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:280
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 ECDSA secret key.
Definition: secp256k1.c:590
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:576
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:613
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:141
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition: secp256k1.h:215
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:418
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:677
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:29
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:450
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:549
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_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 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 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_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:195
Opaque data structured that holds a parsed ECDSA signature, supporting pubkey recovery.
Opaque data structured that holds a parsed ECDSA signature.
Definition: secp256k1.h:87
Opaque data structure that holds a keypair consisting of a secret and a public key.
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:74