-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitcoin-keygen.c
385 lines (281 loc) · 9.01 KB
/
bitcoin-keygen.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <openssl/ec.h>
#include <openssl/rand.h>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <openssl/bn.h>
#include <openssl/ripemd.h>
#include <qrencode.h>
#define VERSION_PREFIX 0x80 // Prefix for a private key WIF
#define COMPRESSED_FLAG 0x01
#define MAINNET_PREFIX "bc"
static const char * BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
static const char * BECH32_CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
uint32_t polymodStep (uint32_t pre) {
const uint32_t b = pre >> 25;
return ((pre & 0x1FFFFFF) << 5)
^ (-((b >> 0) & 1) & 0x3b6a57b2UL)
^ (-((b >> 1) & 1) & 0x26508e6dUL)
^ (-((b >> 2) & 1) & 0x1ea119faUL)
^ (-((b >> 3) & 1) & 0x3d4233ddUL)
^ (-((b >> 4) & 1) & 0x2a1462b3UL);
}
unsigned char * convertBits (
const unsigned char * input,
size_t inputLength,
int fromBits,
int toBits,
size_t * outputLength,
int pad
) {
int acc = 0;
int bits = 0;
const int maxV = (1 << toBits) - 1;
const int maxAcc = (1 << (fromBits + toBits - 1)) - 1;
size_t retlen = (inputLength * fromBits + toBits - 1) / toBits;
unsigned char * output = malloc(retlen * sizeof(unsigned char));
if (!output) {
return NULL;
}
for (size_t i = 0; i < inputLength; i++) {
int value = input[i];
acc = ((acc << fromBits) | value) & maxAcc;
bits += fromBits;
while (bits >= toBits) {
bits -= toBits;
output[(* outputLength)++] = (acc >> bits) & maxV;
}
}
if (pad) {
if (bits) output[(* outputLength)++] = (acc << (toBits - bits)) & maxV;
} else if (bits >= fromBits || ((acc << (toBits - bits)) & maxV)) {
free(output);
return NULL;
}
return output;
}
int bech32Encode (
char * output,
const char * hrp,
const unsigned char * data,
size_t dataLength
) {
int chk = 1;
for (size_t i = 0; hrp && hrp[i] != '\0'; ++i) {
chk = polymodStep(chk) ^ (hrp[i] >> 5);
}
chk = polymodStep(chk);
for (size_t i = 0; hrp && hrp[i] != '\0'; ++i) {
chk = polymodStep(chk) ^ (hrp[i] & 0x1f);
}
size_t resultLength = 0;
while (hrp[resultLength] != '\0') {
if (hrp[resultLength] >= 'A' && hrp[resultLength] <= 'Z') return -1;
output[resultLength] = hrp[resultLength];
resultLength++;
}
output[resultLength++] = '1';
for (size_t i = 0; i < dataLength; ++i) {
if (data[i] >> 5) return -1; // High bits can't be set in 5-bit values
chk = polymodStep(chk) ^ data[i];
output[resultLength++] = BECH32_CHARSET[data[i]];
}
for (int i = 0; i < 6; ++i) {
chk = polymodStep(chk);
}
chk ^= 1;
for (int i = 0; i < 6; ++i) {
output[resultLength++] = BECH32_CHARSET[(chk >> ((5 - i) * 5)) & 0x1f];
}
output[resultLength] = 0; // null-terminate the result
return 0;
}
int createBech32Address (
char * output,
const char * hrp,
int witnessVersion,
const unsigned char * witnessProgram,
size_t programSize
) {
size_t length = 0;
unsigned char data[64];
data[length++] = witnessVersion;
size_t dataLength = 0;
unsigned char * converted = convertBits(witnessProgram, programSize, 8, 5, &dataLength, 1);
if (!converted) {
return -1;
}
memcpy(&data[length], converted, dataLength);
length += dataLength;
free(converted);
if (bech32Encode(output, hrp, data, length) == -1) {
return -1;
}
return 0;
}
void bigNumToBytes (const BIGNUM * bn, unsigned char * buffer, int length) {
int bnLength = BN_num_bytes(bn);
int padding = length - bnLength;
memset(buffer, 0, padding);
BN_bn2bin(bn, buffer + padding);
}
int base58Encode (const BIGNUM * bn, char * output, int outputLength) {
unsigned char buffer[BN_num_bytes(bn)];
bigNumToBytes(bn, buffer, sizeof(buffer));
char * p = output;
BIGNUM * value = BN_dup(bn);
BN_CTX * ctx = BN_CTX_new();
BIGNUM * dv = BN_new();
BIGNUM * rem = BN_new();
BIGNUM * base = BN_new();
BN_set_word(base, 58);
while (!BN_is_zero(value)) {
if (BN_div(dv, rem, value, base, ctx) == 0) {
BN_CTX_free(ctx);
BN_free(base);
BN_free(rem);
BN_free(dv);
BN_clear_free(value);
return 0;
}
* p = BASE58_ALPHABET[BN_get_word(rem)];
p++;
BN_swap(value, dv);
}
for (int i = sizeof(buffer) - 1; i >= 0; i--) {
if (buffer[i]) break;
* p = '1';
p++;
}
* p = '\0'; // null terminate the string
// Reverse the string
int length = p - output;
for (int i = 0; i < length / 2; i++) {
char t = output[i];
output[i] = output[length - i - 1];
output[length - i - 1] = t;
}
BN_CTX_free(ctx);
BN_free(base);
BN_free(rem);
BN_free(dv);
BN_clear_free(value);
return 1;
}
void printQRCode(const char * data) {
QRcode * qr = QRcode_encodeString(data, 0, QR_ECLEVEL_L, QR_MODE_8, 1);
int width = qr->width;
int realWidth = (width) + 4;
// Top border
for (int i = 0; i < realWidth; i++) {
printf("█");
}
printf("\n");
// Print QR modules (processing two rows at a time)
for (int y = 0; y < width; y += 2) {
printf("██"); // Left border
for (int x = 0; x < width; x++) {
unsigned char moduleUpper = qr->data[y * width + x] & 1;
unsigned char moduleLower = (y + 1 < width) ? qr->data[(y + 1) * width + x] & 1 : 0;
if (moduleUpper && moduleLower) {
printf(" ");
} else if (moduleUpper) {
printf("▄");
} else if (moduleLower) {
printf("▀");
} else {
printf("█");
}
}
printf("██\n"); // Right border
}
// Bottom border
for (int i = 0; i < realWidth; i++) {
printf("▀");
}
printf("\n\n");
// Clean up
QRcode_free(qr);
}
int main () {
// Generate a new EC key on the Bitcoin curve secp256k1
EC_KEY * key = EC_KEY_new_by_curve_name(NID_secp256k1);
if (!key) {
fprintf(stderr, "Unable to generate EC_KEY\n");
return 1;
}
if (!EC_KEY_generate_key(key)) {
fprintf(stderr, "Unable to generate EC private key\n");
EC_KEY_free(key);
return 1;
}
const BIGNUM * privateBN = EC_KEY_get0_private_key(key);
unsigned char private[32];
BN_bn2binpad(privateBN, private, 32);
// Add 0x80 prefix for mainnet private key, and append a 0x01 suffix to indicate a compressed pubkey
unsigned char privateFull[34];
privateFull[0] = VERSION_PREFIX;
memcpy(privateFull + 1, private, 32);
privateFull[33] = COMPRESSED_FLAG;
// Double SHA256 for checksum and take first 4 bytes
unsigned char hash[32];
SHA256(privateFull, 34, hash);
SHA256(hash, 32, hash);
unsigned char checksum[4];
memcpy(checksum, hash, 4);
// Combine the private key with prefix, suffix, and checksum
unsigned char privateEncoded[38];
memcpy(privateEncoded, privateFull, 34);
memcpy(privateEncoded + 34, checksum, 4);
// Convert the result into a BIGNUM
BIGNUM * bnPrivate = BN_new();
BN_bin2bn(privateEncoded, 38, bnPrivate);
// Encode the result using base58 encoding
char wif[52];
if (!base58Encode(bnPrivate, wif, sizeof(wif))) {
fprintf(stderr, "Failed to base58 encode private key\n");
BN_clear_free(bnPrivate);
EC_KEY_free(key);
return 1;
}
BN_clear_free(bnPrivate);
printf("\nPrivate Key WIF: %s\n\n", wif);
printQRCode(wif);
// Create bech32 address for corresponding pubkey
EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
// Allocate enough space to store compressed public key
unsigned char public[33]; // A compressed public key is always 33 bytes long.
// pubLen will contain the actual length of the key after conversion
size_t publicLength = EC_POINT_point2oct(
EC_KEY_get0_group(key),
EC_KEY_get0_public_key(key),
POINT_CONVERSION_COMPRESSED,
public,
sizeof(public),
NULL
);
if (publicLength == 0) {
fprintf(stderr, "Failed to create public key\n");
EC_KEY_free(key);
return 1;
}
// Create 20-byte witness program
unsigned char witnessProgram[20];
SHA256(public, publicLength, hash);
RIPEMD160(hash, SHA256_DIGEST_LENGTH, witnessProgram);
// Create bech32 address
char address[100];
if (createBech32Address(address, MAINNET_PREFIX, 0, witnessProgram, sizeof(witnessProgram)) != 0) {
fprintf(stderr, "Failed to create bech32 address\n");
EC_KEY_free(key);
return 1;
}
printf("Bech32 Address: %s\n\n", address);
printQRCode(address);
fflush(stdout);
EC_KEY_free(key);
return 0;
}