Bitcoin ABC 0.33.3
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_SCHNORR
27#endif
28
29#ifdef ENABLE_MODULE_EXTRAKEYS
30# include "../include/secp256k1_extrakeys.h"
31#endif
32
33#ifdef ENABLE_MODULE_SCHNORRSIG
34#include "../include/secp256k1_schnorrsig.h"
35#endif
36
37static void run_tests(secp256k1_context *ctx, unsigned char *key);
38
39int main(void) {
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. */
63 SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
64 CHECK(ret);
65
67 return 0;
68}
69
70static void run_tests(secp256k1_context *ctx, unsigned char *key) {
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
88 for (i = 0; i < 32; i++) {
89 msg[i] = i + 1;
90 }
91
92 /* Test keygen. */
94 ret = secp256k1_ec_pubkey_create(ctx, &pubkey, key);
96 SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
97 CHECK(ret);
98 CHECK(secp256k1_ec_pubkey_serialize(ctx, spubkey, &outputlen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
99
100 /* Test signing. */
102 ret = secp256k1_ecdsa_sign(ctx, &signature, msg, key, NULL, NULL);
104 SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
105 CHECK(ret);
107
108#ifdef ENABLE_MODULE_ECDH
109 /* Test ECDH. */
111 ret = secp256k1_ecdh(ctx, msg, &pubkey, key, NULL, NULL);
112 SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
113 CHECK(ret == 1);
114#endif
115
116#ifdef ENABLE_MODULE_RECOVERY
117 /* Test signing a recoverable signature. */
119 ret = secp256k1_ecdsa_sign_recoverable(ctx, &recoverable_signature, msg, key, NULL, NULL);
120 SECP256K1_CHECKMEM_DEFINE(&recoverable_signature, sizeof(recoverable_signature));
121 SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
122 CHECK(ret);
123 CHECK(secp256k1_ecdsa_recoverable_signature_serialize_compact(ctx, sig, &recid, &recoverable_signature));
124 CHECK(recid >= 0 && recid <= 3);
125#endif
126
127#ifdef ENABLE_MODULE_SCHNORR
128 /* Test schnorr signing. */
130 ret = secp256k1_schnorr_sign(ctx, sig, msg, key, NULL, NULL);
131 SECP256K1_CHECKMEM_DEFINE(&sig, sizeof(64));
132 SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
133 CHECK(ret);
134#endif
135
138 SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
139 CHECK(ret == 1);
140
143 SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
144 CHECK(ret == 1);
145
149 SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
150 CHECK(ret == 1);
151
155 SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
156 CHECK(ret == 1);
157
158 /* Test keypair_create and keypair_xonly_tweak_add. */
159#ifdef ENABLE_MODULE_EXTRAKEYS
161 ret = secp256k1_keypair_create(ctx, &keypair, key);
162 SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
163 CHECK(ret == 1);
164
165 /* The tweak is not treated as a secret in keypair_tweak_add */
167 ret = secp256k1_keypair_xonly_tweak_add(ctx, &keypair, msg);
168 SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
169 CHECK(ret == 1);
170
172 SECP256K1_CHECKMEM_UNDEFINE(&keypair, sizeof(keypair));
173 ret = secp256k1_keypair_sec(ctx, key, &keypair);
174 SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
175 CHECK(ret == 1);
176#endif
177
178#ifdef ENABLE_MODULE_SCHNORRSIG
180 ret = secp256k1_keypair_create(ctx, &keypair, key);
181 SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
182 CHECK(ret == 1);
183 ret = secp256k1_schnorrsig_sign32(ctx, sig, msg, &keypair, NULL);
184 SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
185 CHECK(ret == 1);
186#endif
187}
#define SECP256K1_CHECKMEM_UNDEFINE(p, len)
Definition: checkmem.h:76
#define SECP256K1_CHECKMEM_DEFINE(p, len)
Definition: checkmem.h:77
#define SECP256K1_CHECKMEM_RUNNING()
Definition: checkmem.h:79
static void run_tests(secp256k1_context *ctx, unsigned char *key)
Definition: ctime_tests.c:70
int main(void)
Definition: ctime_tests.c:39
secp256k1_context * ctx
Definition: bench_impl.h:13
SchnorrSig sig
Definition: processor.cpp:537
#define CHECK(cond)
Definition: util.h:115
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:186
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:704
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:751
#define SECP256K1_CONTEXT_DECLASSIFY
Definition: secp256k1.h:185
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:614
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:290
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:573
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:140
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:558
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:596
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition: secp256k1.h:188
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:400
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:660
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 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_schnorr_sign(const secp256k1_context *ctx, unsigned char *sig64, 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 signature using a custom EC-Schnorr-SHA256 construction.
Definition: main_impl.h:32
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:74
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:61