-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathkeysubtracter.c
588 lines (551 loc) · 16.2 KB
/
keysubtracter.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
Developed by Luis Alberto
email: [email protected]
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <gmp.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include "util.h"
#include "gmpecc.h"
#include "base58/libbase58.h"
#include "rmd160/rmd160.h"
#include "sha256/sha256.h"
const char *version = "0.1.20210918";
const char *EC_constant_N = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141";
const char *EC_constant_P = "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f";
const char *EC_constant_Gx = "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798";
const char *EC_constant_Gy = "483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8";
const char *formats[3] = {"publickey","rmd160","address"};
const char *looks[2] = {"compress","uncompress"};
void showhelp();
void set_format(char *param);
void set_look(char *param);
void set_bit(char *param);
void set_publickey(char *param);
void set_range(char *param);
void generate_straddress(struct Point *publickey,bool compress,char *dst);
void generate_strrmd160(struct Point *publickey,bool compress,char *dst);
void generate_strpublickey(struct Point *publickey,bool compress,char *dst);
char *str_output = NULL;
char str_publickey[131];
char str_rmd160[41];
char str_address[41];
struct Point target_publickey,base_publickey,sum_publickey,negated_publickey,dst_publickey;
int FLAG_RANGE = 0;
int FLAG_BIT = 0;
int FLAG_RANDOM = 0;
int FLAG_PUBLIC = 0;
int FLAG_FORMART = 0;
int FLAG_HIDECOMMENT = 0;
int FLAG_LOOK = 0;
int FLAG_MODE = 0;
int FLAG_N;
uint64_t N = 0,M;
mpz_t min_range,max_range,diff,TWO,base_key,sum_key,dst_key;
gmp_randstate_t state;
int main(int argc, char **argv) {
FILE *OUTPUT;
char c;
uint64_t i = 0;
mpz_init_set_str(EC.p, EC_constant_P, 16);
mpz_init_set_str(EC.n, EC_constant_N, 16);
mpz_init_set_str(G.x , EC_constant_Gx, 16);
mpz_init_set_str(G.y , EC_constant_Gy, 16);
init_doublingG(&G);
mpz_init(min_range);
mpz_init(max_range);
mpz_init(diff);
mpz_init_set_ui(TWO,2);
mpz_init(target_publickey.x);
mpz_init_set_ui(target_publickey.y,0);
while ((c = getopt(argc, argv, "hvxRb:n:o:p:r:f:l:")) != -1) {
switch(c) {
case 'x':
FLAG_HIDECOMMENT = 1;
break;
case 'h':
showhelp();
exit(0);
break;
case 'b':
set_bit((char *)optarg);
FLAG_BIT = 1;
break;
case 'n':
N = strtol((char *)optarg,NULL,10);
if(N<= 0) {
fprintf(stderr,"[E] invalid bit N number %s\n",optarg);
exit(0);
}
FLAG_N = 1;
break;
case 'o':
str_output = (char *)optarg;
break;
case 'p':
set_publickey((char *)optarg);
FLAG_PUBLIC = 1;
break;
case 'r':
set_range((char *)optarg);
FLAG_RANGE = 1;
break;
case 'R':
FLAG_RANDOM = 1;
break;
case 'v':
printf("version %s\n",version);
exit(0);
break;
case 'l':
set_look((char *)optarg);
break;
case 'f':
set_format((char *)optarg);
break;
}
}
if((FLAG_BIT || FLAG_RANGE) && FLAG_PUBLIC && FLAG_N) {
if(str_output) {
OUTPUT = fopen(str_output,"a");
if(OUTPUT == NULL) {
fprintf(stderr,"can't opent file %s\n",str_output);
OUTPUT = stdout;
}
}
else {
OUTPUT = stdout;
}
if(N % 2 == 1) {
N++;
}
M = N /2;
mpz_sub(diff,max_range,min_range);
mpz_init(base_publickey.x);
mpz_init(base_publickey.y);
mpz_init(sum_publickey.x);
mpz_init(sum_publickey.y);
mpz_init(negated_publickey.x);
mpz_init(negated_publickey.y);
mpz_init(dst_publickey.x);
mpz_init(dst_publickey.y);
mpz_init(base_key);
mpz_init(sum_key);
if(FLAG_RANDOM) {
gmp_randinit_mt(state);
gmp_randseed_ui(state, ((int)clock()) + ((int)time(NULL)) );
for(i = 0; i < M;i++) {
mpz_urandomm(base_key,state,diff);
Scalar_Multiplication(G,&base_publickey,base_key);
Point_Negation(&base_publickey,&negated_publickey);
Point_Addition(&base_publickey,&target_publickey,&dst_publickey);
switch(FLAG_FORMART) {
case 0: //Publickey
generate_strpublickey(&dst_publickey,FLAG_LOOK == 0,str_publickey);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_publickey);
}
else {
gmp_fprintf(OUTPUT,"%s # - %Zd\n",str_publickey,base_key);
}
Point_Addition(&negated_publickey,&target_publickey,&dst_publickey);
generate_strpublickey(&dst_publickey,FLAG_LOOK == 0,str_publickey);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_publickey);
}
else {
gmp_fprintf(OUTPUT,"%s # + %Zd\n",str_publickey,base_key);
}
break;
case 1: //rmd160
generate_strrmd160(&dst_publickey,FLAG_LOOK == 0,str_rmd160);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_rmd160);
}
else {
gmp_fprintf(OUTPUT,"%s # - %Zd\n",str_rmd160,base_key);
}
Point_Addition(&negated_publickey,&target_publickey,&dst_publickey);
generate_strrmd160(&dst_publickey,FLAG_LOOK == 0,str_rmd160);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_rmd160);
}
else {
gmp_fprintf(OUTPUT,"%s # + %Zd\n",str_rmd160,base_key);
}
break;
case 2: //address
generate_straddress(&dst_publickey,FLAG_LOOK == 0,str_address);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_address);
}
else {
gmp_fprintf(OUTPUT,"%s # - %Zd\n",str_address,base_key);
}
Point_Addition(&negated_publickey,&target_publickey,&dst_publickey);
generate_straddress(&dst_publickey,FLAG_LOOK == 0,str_address);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_address);
}
else {
gmp_fprintf(OUTPUT,"%s # + %Zd\n",str_address,base_key);
}
break;
}
}
switch(FLAG_FORMART) {
case 0: //Publickey
generate_strpublickey(&target_publickey,FLAG_LOOK == 0,str_publickey);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_publickey);
}
else {
fprintf(OUTPUT,"%s # target\n",str_publickey);
}
break;
case 1: //rmd160
generate_strrmd160(&target_publickey,FLAG_LOOK == 0,str_rmd160);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_rmd160);
}
else {
fprintf(OUTPUT,"%s # target\n",str_rmd160);
}
break;
case 2: //address
generate_straddress(&target_publickey,FLAG_LOOK == 0,str_address);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_address);
}
else {
fprintf(OUTPUT,"%s # target\n",str_address);
}
break;
}
}
else {
mpz_cdiv_q_ui(base_key,diff,M);
Scalar_Multiplication(G,&base_publickey,base_key);
mpz_set(sum_publickey.x,base_publickey.x);
mpz_set(sum_publickey.y,base_publickey.y);
mpz_set(sum_key,base_key);
for(i = 0; i < M;i++) {
Point_Negation(&sum_publickey,&negated_publickey);
Point_Addition(&sum_publickey,&target_publickey,&dst_publickey);
switch(FLAG_FORMART) {
case 0: //Publickey
generate_strpublickey(&dst_publickey,FLAG_LOOK == 0,str_publickey);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_publickey);
}
else {
gmp_fprintf(OUTPUT,"%s # - %Zd\n",str_publickey,sum_key);
}
Point_Addition(&negated_publickey,&target_publickey,&dst_publickey);
generate_strpublickey(&dst_publickey,FLAG_LOOK == 0,str_publickey);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_publickey);
}
else {
gmp_fprintf(OUTPUT,"%s # + %Zd\n",str_publickey,sum_key);
}
break;
case 1: //rmd160
generate_strrmd160(&dst_publickey,FLAG_LOOK == 0,str_rmd160);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_rmd160);
}
else {
gmp_fprintf(OUTPUT,"%s # - %Zd\n",str_rmd160,sum_key);
}
Point_Addition(&negated_publickey,&target_publickey,&dst_publickey);
generate_strrmd160(&dst_publickey,FLAG_LOOK == 0,str_rmd160);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_rmd160);
}
else {
gmp_fprintf(OUTPUT,"%s # + %Zd\n",str_rmd160,sum_key);
}
break;
case 2: //address
generate_straddress(&dst_publickey,FLAG_LOOK == 0,str_address);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_address);
}
else {
gmp_fprintf(OUTPUT,"%s # - %Zd\n",str_address,sum_key);
}
Point_Addition(&negated_publickey,&target_publickey,&dst_publickey);
generate_straddress(&dst_publickey,FLAG_LOOK == 0,str_address);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_address);
}
else {
gmp_fprintf(OUTPUT,"%s # + %Zd\n",str_address,sum_key);
}
break;
}
Point_Addition(&sum_publickey,&base_publickey,&dst_publickey);
mpz_set(sum_publickey.x,dst_publickey.x);
mpz_set(sum_publickey.y,dst_publickey.y);
mpz_add(sum_key,sum_key,base_key);
}
switch(FLAG_FORMART) {
case 0: //Publickey
generate_strpublickey(&target_publickey,FLAG_LOOK == 0,str_publickey);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_publickey);
}
else {
fprintf(OUTPUT,"%s # target\n",str_publickey);
}
break;
case 1: //rmd160
generate_strrmd160(&target_publickey,FLAG_LOOK == 0,str_rmd160);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_rmd160);
}
else {
fprintf(OUTPUT,"%s # target\n",str_rmd160);
}
break;
case 2: //address
generate_straddress(&target_publickey,FLAG_LOOK == 0,str_address);
if(FLAG_HIDECOMMENT) {
fprintf(OUTPUT,"%s\n",str_address);
}
else {
fprintf(OUTPUT,"%s # target\n",str_address);
}
break;
}
}
mpz_clear(base_publickey.x);
mpz_clear(base_publickey.y);
mpz_clear(sum_publickey.x);
mpz_clear(sum_publickey.y);
mpz_clear(negated_publickey.x);
mpz_clear(negated_publickey.y);
mpz_clear(dst_publickey.x);
mpz_clear(dst_publickey.y);
mpz_clear(base_key);
mpz_clear(sum_key);
}
else {
fprintf(stderr,"Version: %s\n",version);
fprintf(stderr,"[E] there are some missing parameter\n");
showhelp();
}
return 0;
}
void showhelp() {
printf("\nUsage:\n-h\t\tshow this help\n");
printf("-b bits\t\tFor some puzzles you only need a bit range\n");
printf("-f format\tOutput format <publickey, rmd160, address>. Default: publickey\n");
printf("-l look\t\tOutput <compress, uncompress>. Default: compress\n");
printf("-n number\tNumber of publikeys to be geneted, this numbe will be even\n");
printf("-o file\t\tOutput file, if you omit this option the out will go to the standar output\n");
printf("-p key\t\tPublickey to be substracted compress or uncompress\n");
printf("-r A:B\t\trange A to B\n");
printf("-R\t\tSet the publickey substraction Random instead of secuential\n");
printf("-x\t\tExclude comment\n\n");
printf("Developed by albertobsd\n\n");
}
void set_bit(char *param) {
mpz_t MPZAUX;
int bitrange = strtol(param,NULL,10);
if(bitrange > 0 && bitrange <=256 ) {
mpz_init(MPZAUX);
mpz_pow_ui(MPZAUX,TWO,bitrange-1);
mpz_set(min_range,MPZAUX);
mpz_pow_ui(MPZAUX,TWO,bitrange);
mpz_sub_ui(MPZAUX,MPZAUX,1);
mpz_set(max_range,MPZAUX);
gmp_fprintf(stderr,"[+] Min range: %Zx\n",min_range);
gmp_fprintf(stderr,"[+] Max range: %Zx\n",max_range);
mpz_clear(MPZAUX);
}
else {
fprintf(stderr,"[E] invalid bit param: %s\n",param);
exit(0);
}
}
void set_publickey(char *param) {
char hexvalue[65];
char *dest;
int len;
len = strlen(param);
dest = (char*) calloc(len+1,1);
if(dest == NULL) {
fprintf(stderr,"[E] Error calloc\n");
exit(0);
}
memset(hexvalue,0,65);
memcpy(dest,param,len);
trim(dest," \t\n\r");
len = strlen(dest);
switch(len) {
case 66:
mpz_set_str(target_publickey.x,dest+2,16);
break;
case 130:
memcpy(hexvalue,dest+2,64);
mpz_set_str(target_publickey.x,hexvalue,16);
memcpy(hexvalue,dest+66,64);
mpz_set_str(target_publickey.y,hexvalue,16);
break;
}
if(mpz_cmp_ui(target_publickey.y,0) == 0) {
mpz_t mpz_aux,mpz_aux2,Ysquared;
mpz_init(mpz_aux);
mpz_init(mpz_aux2);
mpz_init(Ysquared);
mpz_pow_ui(mpz_aux,target_publickey.x,3);
mpz_add_ui(mpz_aux2,mpz_aux,7);
mpz_mod(Ysquared,mpz_aux2,EC.p);
mpz_add_ui(mpz_aux,EC.p,1);
mpz_fdiv_q_ui(mpz_aux2,mpz_aux,4);
mpz_powm(target_publickey.y,Ysquared,mpz_aux2,EC.p);
mpz_sub(mpz_aux, EC.p,target_publickey.y);
switch(dest[1]) {
case '2':
if(mpz_tstbit(target_publickey.y, 0) == 1) {
mpz_set(target_publickey.y,mpz_aux);
}
break;
case '3':
if(mpz_tstbit(target_publickey.y, 0) == 0) {
mpz_set(target_publickey.y,mpz_aux);
}
break;
default:
fprintf(stderr,"[E] Some invalid bit in the publickey: %s\n",dest);
exit(0);
break;
}
mpz_clear(mpz_aux);
mpz_clear(mpz_aux2);
mpz_clear(Ysquared);
}
free(dest);
}
void set_range(char *param) {
Tokenizer tk;
char *dest;
int len;
len = strlen(param);
dest = (char*) calloc(len+1,1);
if(dest == NULL) {
fprintf(stderr,"[E] Error calloc\n");
exit(0);
}
memcpy(dest,param,len);
dest[len] = '\0';
stringtokenizer(dest,&tk);
if(tk.n == 2) {
mpz_init_set_str(min_range,nextToken(&tk),16);
mpz_init_set_str(max_range,nextToken(&tk),16);
}
else {
fprintf(stderr,"%i\n",tk.n);
fprintf(stderr,"[E] Invalid range expected format A:B\n");
exit(0);
}
freetokenizer(&tk);
free(dest);
}
void set_format(char *param) {
int index = indexOf(param,formats,3);
if(index == -1) {
fprintf(stderr,"[E] Unknow format: %s\n",param);
}
else {
FLAG_FORMART = index;
}
}
void set_look(char *param) {
int index = indexOf(param,looks,2);
if(index == -1) {
fprintf(stderr,"[E] Unknow look: %s\n",param);
}
else {
FLAG_LOOK = index;
}
}
void generate_strpublickey(struct Point *publickey,bool compress,char *dst) {
memset(dst,0,132);
if(compress) {
if(mpz_tstbit(publickey->y, 0) == 0) { // Even
gmp_snprintf (dst,67,"02%0.64Zx",publickey->x);
}
else {
gmp_snprintf(dst,67,"03%0.64Zx",publickey->x);
}
}
else {
gmp_snprintf(dst,131,"04%0.64Zx%0.64Zx",publickey->x,publickey->y);
}
}
void generate_strrmd160(struct Point *publickey,bool compress,char *dst) {
char str_publickey[131];
char bin_publickey[65];
char bin_sha256[32];
char bin_rmd160[20];
memset(dst,0,42);
if(compress) {
if(mpz_tstbit(publickey->y, 0) == 0) { // Even
gmp_snprintf (str_publickey,67,"02%0.64Zx",publickey->x);
}
else {
gmp_snprintf(str_publickey,67,"03%0.64Zx",publickey->x);
}
hexs2bin(str_publickey,bin_publickey);
sha256(bin_publickey, 33, bin_sha256);
}
else {
gmp_snprintf(str_publickey,131,"04%0.64Zx%0.64Zx",publickey->x,publickey->y);
hexs2bin(str_publickey,bin_publickey);
sha256(bin_publickey, 65, bin_sha256);
}
RMD160Data((const unsigned char*)bin_sha256,32, bin_rmd160);
tohex_dst(bin_rmd160,20,dst);
}
void generate_straddress(struct Point *publickey,bool compress,char *dst) {
char str_publickey[131];
char bin_publickey[65];
char bin_sha256[32];
char bin_digest[60];
size_t pubaddress_size = 42;
memset(dst,0,42);
if(compress) {
if(mpz_tstbit(publickey->y, 0) == 0) { // Even
gmp_snprintf (str_publickey,67,"02%0.64Zx",publickey->x);
}
else {
gmp_snprintf(str_publickey,67,"03%0.64Zx",publickey->x);
}
hexs2bin(str_publickey,bin_publickey);
sha256(bin_publickey, 33, bin_sha256);
}
else {
gmp_snprintf(str_publickey,131,"04%0.64Zx%0.64Zx",publickey->x,publickey->y);
hexs2bin(str_publickey,bin_publickey);
sha256(bin_publickey, 65, bin_sha256);
}
RMD160Data((const unsigned char*)bin_sha256,32, bin_digest+1);
/* Firts byte 0, this is for the Address begining with 1.... */
bin_digest[0] = 0;
/* Double sha256 checksum */
sha256(bin_digest, 21, bin_digest+21);
sha256(bin_digest+21, 32, bin_digest+21);
/* Get the address */
if(!b58enc(dst,&pubaddress_size,bin_digest,25)){
fprintf(stderr,"error b58enc\n");
}
}