-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtc_keys.cpp
151 lines (116 loc) · 3.85 KB
/
btc_keys.cpp
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
#include "bf.hpp"
#include <openssl/sha.h>
#include <openssl/ripemd.h>
// -------------------- GEN KEY PAIR --------------------
typedef unsigned char byte;
/* See https://en.wikipedia.org/wiki/Positional_notation#Base_conversion */
char *base58(byte *s, int s_size, char *out, int out_size)
{
static const char *tmpl = "123456789"
"ABCDEFGHJKLMNPQRSTUVWXYZ"
"abcdefghijkmnopqrstuvwxyz";
int c, i, n;
out[n = out_size] = 0;
while (n--)
{
for (c = i = 0; i < s_size; i++)
{
c = c * 256 + s[i];
s[i] = c / 58;
c %= 58;
}
out[n] = tmpl[c];
}
return out;
}
void c_bf::gen_btc_key_pair(unsigned char *seckey, unsigned char *address) {
secp256k1_pubkey pubkey;
unsigned char public_key[65];
size_t len = 65;
// byte s[65];
byte rmd[5 + RIPEMD160_DIGEST_LENGTH];
if (p_complexity) {
memset(seckey, 0, 32 - p_complexity);
if (*(unsigned int *)&seckey[32 - sizeof(unsigned int)] == 0)
seckey[31] = 1;
}
// for (int i = 0; i < 32; i++) {
// printf("%02x", seckey[i]);
// }
// printf("\n");
secp256k1_ec_pubkey_create(p_btc_ctx, &pubkey, seckey);
// for (int i = 0; i < 32; i++) {
// printf("%02x", seckey[i]);
// }
// printf("\n");
// print_key(priv, crypto_sign_SEEDBYTES, "start private2");
// print_key(pubkey.data, 64, "secp256k1_ec_pubkey_create");
secp256k1_ec_pubkey_serialize(p_btc_ctx, public_key, &len, &pubkey,
// SECP256K1_EC_UNCOMPRESSED
SECP256K1_EC_COMPRESSED
);
// print_key(public_key, len, "secp256k1_ec_pubkey_serialize");
// for (j = 0; j < 65; j++) {
// s[j] = public_key[j];
// // printf("%02x", public_key[j]);
// }
// printf("\n");
/* Set 0x00 byte for main net */
rmd[0] = 0;
// RIPEMD160(SHA256(s, 65, 0), SHA256_DIGEST_LENGTH, rmd + 1);
RIPEMD160(SHA256(public_key, 33, 0), SHA256_DIGEST_LENGTH, rmd + 1);
memcpy(rmd + 21, SHA256(SHA256(rmd, 21, 0), SHA256_DIGEST_LENGTH, 0), 4);
// for (j = 0; j < 25; j++) {
// printf("%02x", rmd[j]);
// }
// printf("\n");
// char addr[34];
base58(rmd, 25, (char *)address, 34);
/* Count the number of 1s at the beginning of the address */
int n = 0;
for (n = 0; address[n] == '1'; n++)
;
/* Do we need to remove any 1s? */
if (n > 1)
{
/* Move the memory so that the address begins at the final 1 */
memmove(address, address + (n - 1), 34 - (n - 1));
/* Force the address to finish at the correct length */
// pubaddress[34 - (n - 1)] = '\0';
// address[34 - (n - 1)] = '\0';
}
// printf("Address: %s\n", address);
}
void c_bf::gen_btc_pub(unsigned char *seckey, unsigned char *pub,
std::vector<unsigned char> & e) {
secp256k1_pubkey pubkey;
unsigned char public_key[65];
size_t len = 65;
byte rmd[5 + RIPEMD160_DIGEST_LENGTH];
if (p_complexity) {
memset(seckey, 0, 32 - p_complexity);
if (*(unsigned int *)&seckey[32 - sizeof(unsigned int)] == 0)
seckey[31] = 1;
}
// for (int i = 0; i < 32; i++) {
// printf("%02x", seckey[i]);
// }
// printf("\n");
secp256k1_ec_pubkey_create(p_btc_ctx, &pubkey, seckey);
// for (int i = 0; i < 32; i++) {
// printf("%02x", seckey[i]);
// }
// printf("\n");
// print_key(priv, crypto_sign_SEEDBYTES, "start private2");
// print_key(pubkey.data, 64, "secp256k1_ec_pubkey_create");
secp256k1_ec_pubkey_serialize(p_btc_ctx, public_key, &len, &pubkey,
// SECP256K1_EC_UNCOMPRESSED
SECP256K1_EC_COMPRESSED
);
// print_key(public_key, len, "secp256k1_ec_pubkey_serialize");
/* Set 0x00 byte for main net */
rmd[0] = 0;
// RIPEMD160(SHA256(s, 65, 0), SHA256_DIGEST_LENGTH, rmd + 1);
RIPEMD160(SHA256(public_key, 33, 0), SHA256_DIGEST_LENGTH, rmd + 1);
memcpy(&e[0], &rmd[1], 20);
}