forked from dogecoinfoundation/libdogecoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecc.c
169 lines (152 loc) · 6.2 KB
/
ecc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include "secp256k1/include/secp256k1.h"
#include "secp256k1/include/secp256k1_recovery.h"
#include <dogecoin/random.h>
#include <dogecoin/dogecoin.h>
#include <dogecoin/utils.h>
static secp256k1_context* secp256k1_ctx = NULL;
dogecoin_bool dogecoin_ecc_start(void)
{
dogecoin_random_init();
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
if (secp256k1_ctx == NULL)
return false;
uint8_t seed[32];
int ret;
ret = dogecoin_random_bytes(seed, 32, 0);
if (!ret)
return false;
ret = secp256k1_context_randomize(secp256k1_ctx, seed);
if (!ret)
return false;
return true;
}
void dogecoin_ecc_stop(void)
{
secp256k1_context* ctx = secp256k1_ctx;
secp256k1_ctx = NULL;
if (ctx)
secp256k1_context_destroy(ctx);
}
void dogecoin_ecc_get_pubkey(const uint8_t* private_key, uint8_t* public_key, size_t* in_outlen, dogecoin_bool compressed)
{
secp256k1_pubkey pubkey;
assert(secp256k1_ctx);
assert((int)*in_outlen == (compressed ? 33 : 65));
dogecoin_mem_zero(public_key, *in_outlen);
if (!secp256k1_ec_pubkey_create(secp256k1_ctx, &pubkey, (const unsigned char*)private_key))
return;
if (!secp256k1_ec_pubkey_serialize(secp256k1_ctx, public_key, in_outlen, &pubkey, compressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED))
return;
return;
}
dogecoin_bool dogecoin_ecc_private_key_tweak_add(uint8_t* private_key, const uint8_t* tweak)
{
assert(secp256k1_ctx);
return secp256k1_ec_privkey_tweak_add(secp256k1_ctx, (unsigned char*)private_key, (const unsigned char*)tweak);
}
dogecoin_bool dogecoin_ecc_public_key_tweak_add(uint8_t* public_key_inout, const uint8_t* tweak)
{
size_t out = DOGECOIN_ECKEY_COMPRESSED_LENGTH;
secp256k1_pubkey pubkey;
assert(secp256k1_ctx);
if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &pubkey, public_key_inout, 33))
return false;
if (!secp256k1_ec_pubkey_tweak_add(secp256k1_ctx, &pubkey, (const unsigned char*)tweak))
return false;
if (!secp256k1_ec_pubkey_serialize(secp256k1_ctx, public_key_inout, &out, &pubkey, SECP256K1_EC_COMPRESSED))
return false;
return true;
}
dogecoin_bool dogecoin_ecc_verify_privatekey(const uint8_t* private_key)
{
assert(secp256k1_ctx);
return secp256k1_ec_seckey_verify(secp256k1_ctx, (const unsigned char*)private_key);
}
dogecoin_bool dogecoin_ecc_verify_pubkey(const uint8_t* public_key, dogecoin_bool compressed)
{
secp256k1_pubkey pubkey;
assert(secp256k1_ctx);
if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &pubkey, public_key, compressed ? 33 : 65)) {
dogecoin_mem_zero(&pubkey, sizeof(pubkey));
return false;
}
dogecoin_mem_zero(&pubkey, sizeof(pubkey));
return true;
}
dogecoin_bool dogecoin_ecc_sign(const uint8_t* private_key, const uint256 hash, unsigned char* sigder, size_t* outlen)
{
assert(secp256k1_ctx);
secp256k1_ecdsa_signature sig;
if (!secp256k1_ecdsa_sign(secp256k1_ctx, &sig, hash, private_key, secp256k1_nonce_function_rfc6979, NULL))
return 0;
if (!secp256k1_ecdsa_signature_serialize_der(secp256k1_ctx, sigder, outlen, &sig))
return 0;
return 1;
}
dogecoin_bool dogecoin_ecc_sign_compact(const uint8_t* private_key, const uint256 hash, unsigned char* sigcomp, size_t* outlen)
{
assert(secp256k1_ctx);
secp256k1_ecdsa_signature sig;
if (!secp256k1_ecdsa_sign(secp256k1_ctx, &sig, hash, private_key, secp256k1_nonce_function_rfc6979, NULL))
return 0;
*outlen = 64;
if (!secp256k1_ecdsa_signature_serialize_compact(secp256k1_ctx, sigcomp, &sig))
return 0;
return 1;
}
dogecoin_bool dogecoin_ecc_sign_compact_recoverable(const uint8_t* private_key, const uint256 hash, unsigned char* sigrec, size_t* outlen, int* recid)
{
assert(secp256k1_ctx);
secp256k1_ecdsa_recoverable_signature sig;
if (!secp256k1_ecdsa_sign_recoverable(secp256k1_ctx, &sig, hash, private_key, secp256k1_nonce_function_rfc6979, NULL))
return 0;
*outlen = 65;
if (!secp256k1_ecdsa_recoverable_signature_serialize_compact(secp256k1_ctx, sigrec, recid, &sig))
return 0;
return 1;
}
dogecoin_bool dogecoin_ecc_recover_pubkey(const unsigned char* sigrec, const uint256 hash, const int recid, uint8_t* public_key, size_t* outlen)
{
assert(secp256k1_ctx);
secp256k1_pubkey pubkey;
secp256k1_ecdsa_recoverable_signature sig;
if (!secp256k1_ecdsa_recoverable_signature_parse_compact(secp256k1_ctx, &sig, sigrec, recid))
return false;
if (!secp256k1_ecdsa_recover(secp256k1_ctx, &pubkey, &sig, hash))
return 0;
if (!secp256k1_ec_pubkey_serialize(secp256k1_ctx, public_key, outlen, &pubkey, SECP256K1_EC_COMPRESSED))
return 0;
return 1;
}
dogecoin_bool dogecoin_ecc_verify_sig(const uint8_t* public_key, dogecoin_bool compressed, const uint256 hash, unsigned char* sigder, size_t siglen)
{
assert(secp256k1_ctx);
secp256k1_ecdsa_signature sig;
secp256k1_pubkey pubkey;
if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &pubkey, public_key, compressed ? 33 : 65))
return false;
if (!secp256k1_ecdsa_signature_parse_der(secp256k1_ctx, &sig, sigder, siglen))
return false;
return secp256k1_ecdsa_verify(secp256k1_ctx, &sig, hash, &pubkey);
}
dogecoin_bool dogecoin_ecc_compact_to_der_normalized(unsigned char* sigcomp_in, unsigned char* sigder_out, size_t* sigder_len_out)
{
assert(secp256k1_ctx);
secp256k1_ecdsa_signature sig;
if (!secp256k1_ecdsa_signature_parse_compact(secp256k1_ctx, &sig, sigcomp_in))
return false;
secp256k1_ecdsa_signature sigNorm;
secp256k1_ecdsa_signature_normalize(secp256k1_ctx, &sigNorm, &sig);
return secp256k1_ecdsa_signature_serialize_der(secp256k1_ctx, sigder_out, sigder_len_out, &sigNorm);
}
dogecoin_bool dogecoin_ecc_der_to_compact(unsigned char* sigder_in, size_t sigder_len, unsigned char* sigcomp_out)
{
assert(secp256k1_ctx);
secp256k1_ecdsa_signature sig;
if (!secp256k1_ecdsa_signature_parse_der(secp256k1_ctx, &sig, sigder_in, sigder_len))
return false;
return secp256k1_ecdsa_signature_serialize_compact(secp256k1_ctx, sigcomp_out, &sig);
}