Bitcoin ABC 0.32.10
P2P Digital Currency
ecdsa.c
Go to the documentation of this file.
1/*************************************************************************
2 * Written in 2020-2022 by Elichai Turkel *
3 * To the extent possible under law, the author(s) have dedicated all *
4 * copyright and related and neighboring rights to the software in this *
5 * file to the public domain worldwide. This software is distributed *
6 * without any warranty. For the CC0 Public Domain Dedication, see *
7 * EXAMPLES_COPYING or https://creativecommons.org/publicdomain/zero/1.0 *
8 *************************************************************************/
9
10#include <stdio.h>
11#include <assert.h>
12#include <string.h>
13
14#include <secp256k1.h>
15
16#include "random.h"
17
18
19
20int main(void) {
21 /* Instead of signing the message directly, we must sign a 32-byte hash.
22 * Here the message is "Hello, world!" and the hash function was SHA-256.
23 * An actual implementation should just call SHA-256, but this example
24 * hardcodes the output to avoid depending on an additional library.
25 * See https://bitcoin.stackexchange.com/questions/81115/if-someone-wanted-to-pretend-to-be-satoshi-by-posting-a-fake-signature-to-defrau/81116#81116 */
26 unsigned char msg_hash[32] = {
27 0x31, 0x5F, 0x5B, 0xDB, 0x76, 0xD0, 0x78, 0xC4,
28 0x3B, 0x8A, 0xC0, 0x06, 0x4E, 0x4A, 0x01, 0x64,
29 0x61, 0x2B, 0x1F, 0xCE, 0x77, 0xC8, 0x69, 0x34,
30 0x5B, 0xFC, 0x94, 0xC7, 0x58, 0x94, 0xED, 0xD3,
31 };
32 unsigned char seckey[32];
33 unsigned char randomize[32];
34 unsigned char compressed_pubkey[33];
35 unsigned char serialized_signature[64];
36 size_t len;
37 int is_signature_valid;
38 int return_val;
39 secp256k1_pubkey pubkey;
41 /* Before we can call actual API functions, we need to create a "context". */
43 if (!fill_random(randomize, sizeof(randomize))) {
44 printf("Failed to generate randomness\n");
45 return 1;
46 }
47 /* Randomizing the context is recommended to protect against side-channel
48 * leakage See `secp256k1_context_randomize` in secp256k1.h for more
49 * information about it. This should never fail. */
50 return_val = secp256k1_context_randomize(ctx, randomize);
51 assert(return_val);
52
53 /*** Key Generation ***/
54
55 /* If the secret key is zero or out of range (bigger than secp256k1's
56 * order), we try to sample a new key. Note that the probability of this
57 * happening is negligible. */
58 while (1) {
59 if (!fill_random(seckey, sizeof(seckey))) {
60 printf("Failed to generate randomness\n");
61 return 1;
62 }
63 if (secp256k1_ec_seckey_verify(ctx, seckey)) {
64 break;
65 }
66 }
67
68 /* Public key creation using a valid context with a verified secret key should never fail */
69 return_val = secp256k1_ec_pubkey_create(ctx, &pubkey, seckey);
70 assert(return_val);
71
72 /* Serialize the pubkey in a compressed form(33 bytes). Should always return 1. */
73 len = sizeof(compressed_pubkey);
74 return_val = secp256k1_ec_pubkey_serialize(ctx, compressed_pubkey, &len, &pubkey, SECP256K1_EC_COMPRESSED);
75 assert(return_val);
76 /* Should be the same size as the size of the output, because we passed a 33 byte array. */
77 assert(len == sizeof(compressed_pubkey));
78
79 /*** Signing ***/
80
81 /* Generate an ECDSA signature `noncefp` and `ndata` allows you to pass a
82 * custom nonce function, passing `NULL` will use the RFC-6979 safe default.
83 * Signing with a valid context, verified secret key
84 * and the default nonce function should never fail. */
85 return_val = secp256k1_ecdsa_sign(ctx, &sig, msg_hash, seckey, NULL, NULL);
86 assert(return_val);
87
88 /* Serialize the signature in a compact form. Should always return 1
89 * according to the documentation in secp256k1.h. */
90 return_val = secp256k1_ecdsa_signature_serialize_compact(ctx, serialized_signature, &sig);
91 assert(return_val);
92
93
94 /*** Verification ***/
95
96 /* Deserialize the signature. This will return 0 if the signature can't be parsed correctly. */
97 if (!secp256k1_ecdsa_signature_parse_compact(ctx, &sig, serialized_signature)) {
98 printf("Failed parsing the signature\n");
99 return 1;
100 }
101
102 /* Deserialize the public key. This will return 0 if the public key can't be parsed correctly. */
103 if (!secp256k1_ec_pubkey_parse(ctx, &pubkey, compressed_pubkey, sizeof(compressed_pubkey))) {
104 printf("Failed parsing the public key\n");
105 return 1;
106 }
107
108 /* Verify a signature. This will return 1 if it's valid and 0 if it's not. */
109 is_signature_valid = secp256k1_ecdsa_verify(ctx, &sig, msg_hash, &pubkey);
110
111 printf("Is the signature valid? %s\n", is_signature_valid ? "true" : "false");
112 printf("Secret Key: ");
113 print_hex(seckey, sizeof(seckey));
114 printf("Public Key: ");
115 print_hex(compressed_pubkey, sizeof(compressed_pubkey));
116 printf("Signature: ");
117 print_hex(serialized_signature, sizeof(serialized_signature));
118
119
120 /* This will clear everything from the context and free the memory */
122
123 /* It's best practice to try to clear secrets from memory after using them.
124 * This is done because some bugs can allow an attacker to leak memory, for
125 * example through "out of bounds" array access (see Heartbleed), Or the OS
126 * swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
127 *
128 * TODO: Prevent these writes from being optimized out, as any good compiler
129 * will remove any writes that aren't used. */
130 memset(seckey, 0, sizeof(seckey));
131
132 return 0;
133}
int main(void)
Definition: ecdsa.c:20
secp256k1_context * ctx
Definition: bench_impl.h:13
void printf(const char *fmt, const Args &...args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:1126
SchnorrSig sig
Definition: processor.cpp:523
static int fill_random(unsigned char *data, size_t size)
Definition: random.h:37
static void print_hex(unsigned char *data, size_t size)
Definition: random.h:66
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:176
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:743
SECP256K1_API int secp256k1_ecdsa_signature_parse_compact(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *input64) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse an ECDSA signature in compact (64 bytes) format.
Definition: secp256k1.c:371
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:282
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:565
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:137
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:550
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *input, size_t inputlen) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a variable-length public key into the pubkey object.
Definition: secp256k1.c:264
#define SECP256K1_CONTEXT_NONE
Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size,...
Definition: secp256k1.h:203
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:588
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition: secp256k1.h:213
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(const secp256k1_context *ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const secp256k1_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Verify an ECDSA signature.
Definition: secp256k1.c:436
SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context *ctx, unsigned char *output64, const secp256k1_ecdsa_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Serialize an ECDSA signature in compact (64 byte) format.
Definition: secp256k1.c:404
Opaque data structured that holds a parsed ECDSA signature.
Definition: secp256k1.h:87
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:74
assert(!tx.IsCoinBase())