-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsecp256k1_lib.cpp
More file actions
executable file
·247 lines (209 loc) · 9.28 KB
/
secp256k1_lib.cpp
File metadata and controls
executable file
·247 lines (209 loc) · 9.28 KB
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
#include <iostream>
#include "secp256k1/SECP256k1.h"
#include "secp256k1/Int.h"
#include "secp256k1/IntGroup.h"
#include "bloom/Bloom.h"
extern "C" {
Secp256K1* secp256k1 = new Secp256K1();
Bloom bf[2];
void Init() {
::secp256k1->Init();
}
void check() {
Int pk;
pk.SetInt32(1);
Point P = ::secp256k1->ComputePublicKey(&pk);
std::cout << ::secp256k1->GetPublicKeyHex(false, P) << std::endl;
std::cout << ::secp256k1->GetPublicKeyHex(true, P) << std::endl;
}
void scalar_multiplication(unsigned char* priv, unsigned char* publicKeyBytesOut) {
Int pk;
pk.Set32Bytes(priv);
if (pk.IsZero()) {
Point P;
P.x.SetInt32(0);
P.y.SetInt32(0);
::secp256k1->GetPubKeyBytes(false, P, publicKeyBytesOut);
}
else {
Point P = ::secp256k1->ComputePublicKey(&pk);
::secp256k1->GetPubKeyBytes(false, P, publicKeyBytesOut);
}
}
void point_multiplication(unsigned char* publicKeyBytesIn, unsigned char* priv, unsigned char* publicKeyBytesOut) {
Point P = ::secp256k1->SetPubKeyBytes(publicKeyBytesIn);
Int pk;
pk.Set32Bytes(priv);
Point ret = ::secp256k1->PointMultiplication(P, &pk);
::secp256k1->GetPubKeyBytes(false, ret, publicKeyBytesOut);
}
void point_division(unsigned char* publicKeyBytesIn, unsigned char* priv, unsigned char* publicKeyBytesOut) {
Point P = ::secp256k1->SetPubKeyBytes(publicKeyBytesIn);
Int pk;
pk.Set32Bytes(priv);
pk.MultInvModN();
Point ret = ::secp256k1->PointMultiplication(P, &pk);
::secp256k1->GetPubKeyBytes(false, ret, publicKeyBytesOut);
}
void double_point(unsigned char* publicKeyBytesIn, unsigned char* publicKeyBytesOut) {
Point P = ::secp256k1->SetPubKeyBytes(publicKeyBytesIn);
P = ::secp256k1->DoubleDirect(P);
::secp256k1->GetPubKeyBytes(false, P, publicKeyBytesOut);
}
void negate_point(unsigned char* publicKeyBytesIn, unsigned char* publicKeyBytesOut) {
Point P = ::secp256k1->SetPubKeyBytes(publicKeyBytesIn);
P.y.ModNeg();
::secp256k1->GetPubKeyBytes(false, P, publicKeyBytesOut);
}
void add_points(unsigned char* publicKeyBytesIn1, unsigned char* publicKeyBytesIn2, unsigned char* publicKeyBytesOut) {
Point P = ::secp256k1->SetPubKeyBytes(publicKeyBytesIn1);
Point Q = ::secp256k1->SetPubKeyBytes(publicKeyBytesIn2);
Point ret = ::secp256k1->AddPoints2(P, Q);
::secp256k1->GetPubKeyBytes(false, ret, publicKeyBytesOut);
}
void add_point_scalar(unsigned char* publicKeyBytesIn, unsigned char* priv, unsigned char* publicKeyBytesOut) {
Point P = ::secp256k1->SetPubKeyBytes(publicKeyBytesIn);
Int pk;
pk.Set32Bytes(priv);
Point Q = ::secp256k1->ComputePublicKey(&pk);
Point ret = ::secp256k1->AddPoints2(P, Q);
::secp256k1->GetPubKeyBytes(false, ret, publicKeyBytesOut);
}
void subtract_points(unsigned char* publicKeyBytesIn1, unsigned char* publicKeyBytesIn2, unsigned char* publicKeyBytesOut) {
Point P = ::secp256k1->SetPubKeyBytes(publicKeyBytesIn1);
Point Q = ::secp256k1->SetPubKeyBytes(publicKeyBytesIn2);
Point ret = ::secp256k1->SubtractPoints2(P, Q);
::secp256k1->GetPubKeyBytes(false, ret, publicKeyBytesOut);
}
void subtract_point_scalar(unsigned char* publicKeyBytesIn, unsigned char* priv, unsigned char* publicKeyBytesOut) {
Point P = ::secp256k1->SetPubKeyBytes(publicKeyBytesIn);
Int pk;
pk.Set32Bytes(priv);
Point Q = ::secp256k1->ComputePublicKey(&pk);
Point ret = ::secp256k1->SubtractPoints2(P, Q);
::secp256k1->GetPubKeyBytes(false, ret, publicKeyBytesOut);
}
void increment_point(unsigned char* publicKeyBytesIn, unsigned char* publicKeyBytesOut) {
Point P = ::secp256k1->SetPubKeyBytes(publicKeyBytesIn);
Point ret = ::secp256k1->AddPoints2(P, ::secp256k1->G);
::secp256k1->GetPubKeyBytes(false, ret, publicKeyBytesOut);
}
void decrement_point(unsigned char* publicKeyBytesIn, unsigned char* publicKeyBytesOut) {
Point P = ::secp256k1->SetPubKeyBytes(publicKeyBytesIn);
Point ret = ::secp256k1->SubtractPoints2(P, ::secp256k1->G);
::secp256k1->GetPubKeyBytes(false, ret, publicKeyBytesOut);
}
bool point_on_curve(unsigned char* publicKeyBytesIn) {
Point P = ::secp256k1->SetPubKeyBytes(publicKeyBytesIn);
return ::secp256k1->EC(P);
}
void get_y(unsigned char* BytesIn, bool isEven, unsigned char* BytesOut) {
Int x1; x1.Set32Bytes(BytesIn);
Int x2 = ::secp256k1->GetYToX(x1, isEven);
x2.Get32Bytes(BytesOut);
}
void privatekey_to_hash160(int type, bool compressed, unsigned char* priv, unsigned char* BytesOut) {
Int pk;
pk.Set32Bytes(priv);
Point P = ::secp256k1->ComputePublicKey(&pk);
::secp256k1->GetHash160(type, compressed, P, BytesOut);
}
void publickey_to_hash160(int type, bool compressed, unsigned char* publicKeyBytesIn, unsigned char* BytesOut) {
Point P = ::secp256k1->SetPubKeyBytes(publicKeyBytesIn);
::secp256k1->GetHash160(type, compressed, P, BytesOut);
}
void privatekey_to_uwif(unsigned char* priv, unsigned char* BytesOut) {
Int pk;
pk.Set32Bytes(priv);
std::string wif = ::secp256k1->GetPrivAddress(false, pk);
for(int i = 0; i < wif.size(); i++) {
BytesOut[i] = wif[i];
}
}
void privatekey_to_cwif(unsigned char* priv, unsigned char* BytesOut) {
Int pk;
pk.Set32Bytes(priv);
std::string wif = ::secp256k1->GetPrivAddress(true, pk);
for(int i = 0; i < wif.size(); i++) {
BytesOut[i] = wif[i];
}
}
void wif_to_privatekey(char* wif, unsigned char* BytesOut) {
Int pk = Secp256K1::DecodePrivateKey2(wif);
pk.Get32Bytes(BytesOut);
}
void privatekey_to_address(int type, bool compressed, unsigned char* priv, unsigned char* BytesOut) {
Int pk;
pk.Set32Bytes(priv);
Point P = ::secp256k1->ComputePublicKey(&pk);
std::string address = ::secp256k1->GetAddressFromPub(type, compressed, P);
for(int i = 0; i < address.size(); i++) {
BytesOut[i] = address[i];
}
}
void publickey_to_address(int type, bool compressed, unsigned char* publicKeyBytesIn, unsigned char* BytesOut) {
Point P = ::secp256k1->SetPubKeyBytes(publicKeyBytesIn);
std::string address = ::secp256k1->GetAddressFromPub(type, compressed, P);
for(int i = 0; i < address.size(); i++) {
BytesOut[i] = address[i];
}
}
void privatekey_to_bech32_address(unsigned char* priv, unsigned char* BytesOut) {
Int pk;
pk.Set32Bytes(priv);
Point P = ::secp256k1->ComputePublicKey(&pk);
std::string address = ::secp256k1->GetBech32Address(P);
for(int i = 0; i < address.size(); i++) {
BytesOut[i] = address[i];
}
}
void publickey_to_bech32_address(unsigned char* publicKeyBytesIn, unsigned char* BytesOut) {
Point P = ::secp256k1->SetPubKeyBytes(publicKeyBytesIn);
std::string address = ::secp256k1->GetBech32Address(P);
for(int i = 0; i < address.size(); i++) {
BytesOut[i] = address[i];
}
}
void publickey_to_bech32_p2wsh_address(unsigned char* publicKeyBytesIn, unsigned char* BytesOut) {
Point P = ::secp256k1->SetPubKeyBytes(publicKeyBytesIn);
std::string address = ::secp256k1->GetBech32P2WSHAddress(P);
for(int i = 0; i < address.size(); i++) {
BytesOut[i] = address[i];
}
}
void hash160_to_address(int type, bool compressed, unsigned char* hash160, unsigned char* BytesOut) {
std::string address = ::secp256k1->GetAddressFromHash(type, compressed, hash160);
for(int i = 0; i < address.size(); i++) {
BytesOut[i] = address[i];
}
}
void publickey_to_point(char* publicKey, unsigned char* publicKeyBytesOut) {
Point ret = ::secp256k1->ParsePublicKeyHex2(publicKey);
::secp256k1->GetPubKeyBytes(false, ret, publicKeyBytesOut);
}
void p2pkh_address_to_hash160(char* address, unsigned char* BytesOut) {
std::string hash160 = ::secp256k1->GetHashFromP2PKHAddress(address);
for(int i = 0; i < hash160.size(); i++) {
BytesOut[i] = hash160[i];
}
}
void init_bloom(int arrayIndex, unsigned long long entries, double error) {
::bf[arrayIndex].init_bloom(entries, error);
}
void bloom_info(int arrayIndex) {
::bf[arrayIndex].print();
}
void bloom_save(int arrayIndex, char* filename) {
::bf[arrayIndex].save(filename);
}
void bloom_load(int arrayIndex, char* filename) {
::bf[arrayIndex].load(filename);
}
void bloom_add(int arrayIndex, char* item, int len) {
::bf[arrayIndex].add(item, len);
}
int bloom_check(int arrayIndex, char* item, int len) {
int in_bloom = ::bf[arrayIndex].check(item, len);
return in_bloom;
}
}