Advertisement
Guest User

Untitled

a guest
Jul 10th, 2011
1,318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 50.62 KB | None | 0 0
  1. /*
  2. * Vanitygen, vanity bitcoin address generator
  3. * Copyright (C) 2011 <samr7@cs.washington.edu>
  4. *
  5. * Vanitygen is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * any later version.
  9. *
  10. * Vanitygen is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with Vanitygen. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include <assert.h>
  23.  
  24. #include <pthread.h>
  25.  
  26. #include <openssl/sha.h>
  27. #include <openssl/ripemd.h>
  28. #include <openssl/ec.h>
  29. #include <openssl/obj_mac.h>
  30. #include <openssl/bn.h>
  31.  
  32. #include <pcre.h>
  33.  
  34. #ifndef _WIN32
  35. #define INLINE inline
  36. #include <sys/time.h>
  37. #include <errno.h>
  38. #include <unistd.h>
  39. #else
  40. #include "winglue.c"
  41. #endif
  42.  
  43. const char *version = "0.9";
  44. const int debug = 0;
  45. int verbose = 1;
  46. int remove_on_match = 1;
  47. const char *result_file = NULL;
  48.  
  49. const char *b58_alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
  50.  
  51. void
  52. encode_b58_check(void *buf, size_t len, char *result)
  53. {
  54. unsigned char hash1[32];
  55. unsigned char hash2[32];
  56.  
  57. int d, p;
  58.  
  59. BN_CTX *bnctx;
  60. BIGNUM *bn, *bndiv, *bntmp;
  61. BIGNUM bna, bnb, bnbase, bnrem;
  62. unsigned char *binres;
  63. int brlen, zpfx;
  64.  
  65. bnctx = BN_CTX_new();
  66. BN_init(&bna);
  67. BN_init(&bnb);
  68. BN_init(&bnbase);
  69. BN_init(&bnrem);
  70. BN_set_word(&bnbase, 58);
  71.  
  72. bn = &bna;
  73. bndiv = &bnb;
  74.  
  75. brlen = (2 * len) + 4;
  76. binres = (unsigned char*) malloc(brlen);
  77. memcpy(binres, buf, len);
  78.  
  79. SHA256(binres, len, hash1);
  80. SHA256(hash1, sizeof(hash1), hash2);
  81. memcpy(&binres[len], hash2, 4);
  82.  
  83. BN_bin2bn(binres, len + 4, bn);
  84.  
  85. for (zpfx = 0; zpfx < (len + 4) && binres[zpfx] == 0; zpfx++);
  86.  
  87. p = brlen;
  88. while (!BN_is_zero(bn)) {
  89. BN_div(bndiv, &bnrem, bn, &bnbase, bnctx);
  90. bntmp = bn;
  91. bn = bndiv;
  92. bndiv = bntmp;
  93. d = BN_get_word(&bnrem);
  94. binres[--p] = b58_alphabet[d];
  95. }
  96.  
  97. while (zpfx--) {
  98. binres[--p] = b58_alphabet[0];
  99. }
  100.  
  101. memcpy(result, &binres[p], brlen - p);
  102. result[brlen - p] = '\0';
  103.  
  104. free(binres);
  105. BN_clear_free(&bna);
  106. BN_clear_free(&bnb);
  107. BN_clear_free(&bnbase);
  108. BN_clear_free(&bnrem);
  109. BN_CTX_free(bnctx);
  110. }
  111.  
  112. void
  113. encode_address(EC_KEY *pkey, int addrtype, char *result)
  114. {
  115. unsigned char eckey_buf[128], *pend;
  116. unsigned char binres[21] = {0,};
  117. unsigned char hash1[32];
  118.  
  119. pend = eckey_buf;
  120.  
  121. i2o_ECPublicKey(pkey, &pend);
  122.  
  123. binres[0] = addrtype;
  124. SHA256(eckey_buf, pend - eckey_buf, hash1);
  125. RIPEMD160(hash1, sizeof(hash1), &binres[1]);
  126.  
  127. encode_b58_check(binres, sizeof(binres), result);
  128. }
  129.  
  130. void
  131. encode_privkey(EC_KEY *pkey, int addrtype, char *result)
  132. {
  133. unsigned char eckey_buf[128];
  134. const BIGNUM *bn;
  135. int nbytes;
  136.  
  137. bn = EC_KEY_get0_private_key(pkey);
  138.  
  139. eckey_buf[0] = addrtype;
  140. nbytes = BN_bn2bin(bn, &eckey_buf[1]);
  141.  
  142. encode_b58_check(eckey_buf, nbytes + 1, result);
  143. }
  144.  
  145.  
  146. void
  147. dumphex(const unsigned char *src, size_t len)
  148. {
  149. size_t i;
  150. for (i = 0; i < len; i++) {
  151. printf("%02x", src[i]);
  152. }
  153. printf("\n");
  154. }
  155.  
  156. void
  157. dumpbn(const BIGNUM *bn)
  158. {
  159. char *buf;
  160. buf = BN_bn2hex(bn);
  161. printf("%s\n", buf);
  162. OPENSSL_free(buf);
  163. }
  164.  
  165. typedef struct _timing_info_s {
  166. struct _timing_info_s *ti_next;
  167. pthread_t ti_thread;
  168. unsigned long ti_last_rate;
  169. } timing_info_t;
  170.  
  171. void
  172. output_timing(int cycle, struct timeval *last,
  173. unsigned long long found, unsigned long pattcount,
  174. double chance)
  175. {
  176. static unsigned long long total = 0, prevfound = 0, sincelast = 0;
  177. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  178. static timing_info_t *timing_head = NULL;
  179.  
  180. pthread_t me;
  181. struct timeval tvnow, tv;
  182. timing_info_t *tip, *mytip;
  183. unsigned long long rate, myrate;
  184. double count, prob, time, targ;
  185. char linebuf[80];
  186. char *unit;
  187. int rem, p, i;
  188.  
  189. const double targs[] = { 0.5, 0.75, 0.8, 0.9, 0.95, 1.0 };
  190.  
  191. /* Compute the rate */
  192. gettimeofday(&tvnow, NULL);
  193. timersub(&tvnow, last, &tv);
  194. memcpy(last, &tvnow, sizeof(*last));
  195. myrate = tv.tv_usec + (1000000 * tv.tv_sec);
  196. myrate = (1000000ULL * cycle) / myrate;
  197.  
  198. pthread_mutex_lock(&mutex);
  199. me = pthread_self();
  200. rate = myrate;
  201. for (tip = timing_head, mytip = NULL; tip != NULL; tip = tip->ti_next) {
  202. if (pthread_equal(tip->ti_thread, me)) {
  203. mytip = tip;
  204. tip->ti_last_rate = myrate;
  205. } else
  206. rate += tip->ti_last_rate;
  207. }
  208. if (!mytip) {
  209. mytip = (timing_info_t *) malloc(sizeof(*tip));
  210. mytip->ti_next = timing_head;
  211. mytip->ti_thread = me;
  212. timing_head = mytip;
  213. mytip->ti_last_rate = myrate;
  214. }
  215.  
  216. total += cycle;
  217. if (prevfound != found) {
  218. prevfound = found;
  219. sincelast = 0;
  220. }
  221. sincelast += cycle;
  222. count = sincelast;
  223.  
  224. if (mytip != timing_head) {
  225. pthread_mutex_unlock(&mutex);
  226. return;
  227. }
  228. pthread_mutex_unlock(&mutex);
  229.  
  230. rem = sizeof(linebuf);
  231. p = snprintf(linebuf, rem, "[%lld K/s][total %lld]", rate, total);
  232. assert(p > 0);
  233. rem -= p;
  234. if (rem < 0)
  235. rem = 0;
  236.  
  237. if (chance >= 1.0) {
  238. prob = 1.0f - exp(-count/chance);
  239.  
  240. if (prob <= 0.999) {
  241. p = snprintf(&linebuf[p], rem, "[Prob %.1f%%]",
  242. prob * 100);
  243. assert(p > 0);
  244. rem -= p;
  245. if (rem < 0)
  246. rem = 0;
  247. p = sizeof(linebuf) - rem;
  248. }
  249.  
  250. for (i = 0; i < sizeof(targs)/sizeof(targs[0]); i++) {
  251. targ = targs[i];
  252. if ((targ < 1.0) && (prob <= targ))
  253. break;
  254. }
  255.  
  256. if (targ < 1.0) {
  257. time = ((-chance * log(1.0 - targ)) - count) / rate;
  258. unit = "s";
  259. if (time > 60) {
  260. time /= 60;
  261. unit = "min";
  262. if (time > 60) {
  263. time /= 60;
  264. unit = "h";
  265. if (time > 24) {
  266. time /= 24;
  267. unit = "d";
  268. if (time > 365) {
  269. time /= 365;
  270. unit = "y";
  271. }
  272. }
  273. }
  274. }
  275.  
  276. if (time > 1000000) {
  277. p = snprintf(&linebuf[p], rem,
  278. "[%d%% in %e%s]",
  279. (int) (100 * targ), time, unit);
  280. } else {
  281. p = snprintf(&linebuf[p], rem,
  282. "[%d%% in %.1f%s]",
  283. (int) (100 * targ), time, unit);
  284. }
  285. assert(p > 0);
  286. rem -= p;
  287. if (rem < 0)
  288. rem = 0;
  289. p = sizeof(linebuf) - rem;
  290. }
  291. }
  292.  
  293. if (found) {
  294. if (pattcount)
  295. p = snprintf(&linebuf[p], rem, "[Found %lld/%ld]",
  296. found, pattcount);
  297. else
  298. p = snprintf(&linebuf[p], rem, "[Found %lld]", found);
  299. assert(p > 0);
  300. rem -= p;
  301. if (rem < 0)
  302. rem = 0;
  303. }
  304.  
  305. if (rem) {
  306. memset(&linebuf[sizeof(linebuf)-rem], 0x20, rem);
  307. linebuf[sizeof(linebuf)-1] = '\0';
  308. }
  309. printf("\r%s", linebuf);
  310. fflush(stdout);
  311. }
  312.  
  313. void
  314. output_match(EC_KEY *pkey, const char *pattern, int addrtype, int privtype)
  315. {
  316. unsigned char key_buf[512], *pend;
  317. char addr_buf[64];
  318. char privkey_buf[128];
  319. int len;
  320.  
  321. assert(EC_KEY_check_key(pkey));
  322. encode_address(pkey, addrtype, addr_buf);
  323. encode_privkey(pkey, privtype, privkey_buf);
  324.  
  325. if (!result_file || (verbose > 0)) {
  326. printf("\r%79s\rPattern: %s\n", "", pattern);
  327. }
  328.  
  329. if (verbose > 0) {
  330. if (verbose > 1) {
  331. /* Hexadecimal OpenSSL notation */
  332. pend = key_buf;
  333. len = i2o_ECPublicKey(pkey, &pend);
  334. printf("Pubkey (hex) : ");
  335. dumphex(key_buf, len);
  336. pend = key_buf;
  337. len = i2d_ECPrivateKey(pkey, &pend);
  338. printf("Privkey (hex) : ");
  339. dumphex(key_buf, len);
  340. }
  341.  
  342. }
  343.  
  344. if (!result_file || (verbose > 0)) {
  345. printf("Address: %s\n"
  346. "Privkey: %s\n",
  347. addr_buf, privkey_buf);
  348. }
  349.  
  350. if (result_file) {
  351. FILE *fp = fopen(result_file, "a");
  352. if (!fp) {
  353. printf("ERROR: could not open result file: %s\n",
  354. strerror(errno));
  355. } else {
  356. fprintf(fp,
  357. "Pattern: %s\n"
  358. "Address: %s\n"
  359. "Privkey: %s\n",
  360. pattern, addr_buf, privkey_buf);
  361. fclose(fp);
  362. }
  363. }
  364. }
  365.  
  366. const signed char b58_reverse_map[256] = {
  367. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  368. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  369. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  370. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -1, -1, -1, -1, -1,
  371. -1, 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18, 19, 20, 21, -1,
  372. 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1,
  373. -1, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46,
  374. 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1,
  375. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  376. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  377. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  378. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  379. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  380. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  381. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  382. };
  383.  
  384. /*
  385. * Find the bignum ranges that produce a given prefix.
  386. */
  387. int
  388. get_prefix_ranges(int addrtype, const char *pfx, BIGNUM **result,
  389. BN_CTX *bnctx)
  390. {
  391. int i, p, c;
  392. int zero_prefix = 0;
  393. int check_upper = 0;
  394. int b58pow, b58ceil, b58top = 0;
  395. int ret = 0;
  396.  
  397. BIGNUM bntarg, bnceil, bnfloor;
  398. BIGNUM bnbase;
  399. BIGNUM *bnap, *bnbp, *bntp;
  400. BIGNUM *bnhigh = NULL, *bnlow = NULL, *bnhigh2 = NULL, *bnlow2 = NULL;
  401. BIGNUM bntmp, bntmp2;
  402.  
  403. BN_init(&bntarg);
  404. BN_init(&bnceil);
  405. BN_init(&bnfloor);
  406. BN_init(&bnbase);
  407. BN_init(&bntmp);
  408. BN_init(&bntmp2);
  409.  
  410. BN_set_word(&bnbase, 58);
  411.  
  412. p = strlen(pfx);
  413.  
  414. for (i = 0; i < p; i++) {
  415. c = b58_reverse_map[(int)pfx[i]];
  416. if (c == -1) {
  417. printf("Invalid character '%c' in prefix '%s'\n",
  418. pfx[i], pfx);
  419. goto out;
  420. }
  421. if (i == zero_prefix) {
  422. if (c == 0) {
  423. /* Add another zero prefix */
  424. zero_prefix++;
  425. if (zero_prefix > 19) {
  426. printf("Prefix '%s' is too long\n",
  427. pfx);
  428. goto out;
  429. }
  430. continue;
  431. }
  432.  
  433. /* First non-zero character */
  434. b58top = c;
  435. BN_set_word(&bntarg, c);
  436.  
  437. } else {
  438. BN_set_word(&bntmp2, c);
  439. BN_mul(&bntmp, &bntarg, &bnbase, bnctx);
  440. BN_add(&bntarg, &bntmp, &bntmp2);
  441. }
  442. }
  443.  
  444. /* Power-of-two ceiling and floor values based on leading 1s */
  445. BN_clear(&bntmp);
  446. BN_set_bit(&bntmp, 200 - (zero_prefix * 8));
  447. BN_set_word(&bntmp2, 1);
  448. BN_sub(&bnceil, &bntmp, &bntmp2);
  449. BN_set_bit(&bnfloor, 192 - (zero_prefix * 8));
  450.  
  451. bnlow = BN_new();
  452. bnhigh = BN_new();
  453.  
  454. if (b58top) {
  455. /*
  456. * If a non-zero was given in the prefix, find the
  457. * numeric boundaries of the prefix.
  458. */
  459.  
  460. BN_copy(&bntmp, &bnceil);
  461. bnap = &bntmp;
  462. bnbp = &bntmp2;
  463. b58pow = 0;
  464. while (BN_cmp(bnap, &bnbase) > 0) {
  465. b58pow++;
  466. BN_div(bnbp, NULL, bnap, &bnbase, bnctx);
  467. bntp = bnap;
  468. bnap = bnbp;
  469. bnbp = bntp;
  470. }
  471. b58ceil = BN_get_word(bnap);
  472.  
  473. if ((b58pow - (p - zero_prefix)) < 6) {
  474. /*
  475. * Do not allow the prefix to constrain the
  476. * check value, this is ridiculous.
  477. */
  478. printf("Prefix '%s' is too long\n", pfx);
  479. goto out;
  480. }
  481.  
  482. BN_set_word(&bntmp2, b58pow - (p - zero_prefix));
  483. BN_exp(&bntmp, &bnbase, &bntmp2, bnctx);
  484. BN_mul(bnlow, &bntmp, &bntarg, bnctx);
  485. BN_set_word(bnhigh, 1);
  486. BN_sub(&bntmp2, &bntmp, bnhigh);
  487. BN_add(bnhigh, bnlow, &bntmp2);
  488.  
  489. if (b58top <= b58ceil) {
  490. /* Fill out the upper range too */
  491. check_upper = 1;
  492. bnlow2 = BN_new();
  493. bnhigh2 = BN_new();
  494.  
  495. BN_mul(bnlow2, bnlow, &bnbase, bnctx);
  496. BN_mul(&bntmp2, bnhigh, &bnbase, bnctx);
  497. BN_set_word(&bntmp, 57);
  498. BN_add(bnhigh2, &bntmp2, &bntmp);
  499.  
  500. /*
  501. * Addresses above the ceiling will have one
  502. * fewer "1" prefix in front than we require.
  503. */
  504. if (BN_cmp(&bnceil, bnlow2) < 0) {
  505. /* High prefix is above the ceiling */
  506. check_upper = 0;
  507. BN_free(bnhigh2);
  508. bnhigh2 = NULL;
  509. BN_free(bnlow2);
  510. bnlow2 = NULL;
  511. }
  512. else if (BN_cmp(&bnceil, bnhigh2) < 0)
  513. /* High prefix is partly above the ceiling */
  514. BN_copy(bnhigh2, &bnceil);
  515.  
  516. /*
  517. * Addresses below the floor will have another
  518. * "1" prefix in front instead of our target.
  519. */
  520. if (BN_cmp(&bnfloor, bnhigh) >= 0) {
  521. /* Low prefix is completely below the floor */
  522. assert(check_upper);
  523. check_upper = 0;
  524. BN_free(bnhigh);
  525. bnhigh = bnhigh2;
  526. bnhigh2 = NULL;
  527. BN_free(bnlow);
  528. bnlow = bnlow2;
  529. bnlow2 = NULL;
  530. }
  531. else if (BN_cmp(&bnfloor, bnlow) > 0) {
  532. /* Low prefix is partly below the floor */
  533. BN_copy(bnlow, &bnfloor);
  534. }
  535. }
  536.  
  537. } else {
  538. BN_copy(bnhigh, &bnceil);
  539. BN_set_word(bnlow, 0);
  540. }
  541.  
  542. /* Limit the prefix to the address type */
  543. BN_clear(&bntmp);
  544. BN_set_word(&bntmp, addrtype);
  545. BN_lshift(&bntmp2, &bntmp, 192);
  546.  
  547. if (check_upper) {
  548. if (BN_cmp(&bntmp2, bnhigh2) > 0) {
  549. check_upper = 0;
  550. BN_free(bnhigh2);
  551. bnhigh2 = NULL;
  552. BN_free(bnlow2);
  553. bnlow2 = NULL;
  554. }
  555. else if (BN_cmp(&bntmp2, bnlow2) > 0)
  556. BN_copy(bnlow2, &bntmp2);
  557. }
  558.  
  559. if (BN_cmp(&bntmp2, bnhigh) > 0) {
  560. if (!check_upper) {
  561. printf("Prefix '%s' not possible\n", pfx);
  562. goto out;
  563. }
  564. check_upper = 0;
  565. BN_free(bnhigh);
  566. bnhigh = bnhigh2;
  567. bnhigh2 = NULL;
  568. BN_free(bnlow);
  569. bnlow = bnlow2;
  570. bnlow2 = NULL;
  571. }
  572. else if (BN_cmp(&bntmp2, bnlow) > 0) {
  573. BN_copy(bnlow, &bntmp2);
  574. }
  575.  
  576. BN_set_word(&bntmp, addrtype + 1);
  577. BN_lshift(&bntmp2, &bntmp, 192);
  578.  
  579. if (check_upper) {
  580. if (BN_cmp(&bntmp2, bnlow2) < 0) {
  581. check_upper = 0;
  582. BN_free(bnhigh2);
  583. bnhigh2 = NULL;
  584. BN_free(bnlow2);
  585. bnlow2 = NULL;
  586. }
  587. else if (BN_cmp(&bntmp2, bnhigh2) < 0)
  588. BN_copy(bnlow2, &bntmp2);
  589. }
  590.  
  591. if (BN_cmp(&bntmp2, bnlow) < 0) {
  592. if (!check_upper) {
  593. printf("Prefix '%s' not possible\n", pfx);
  594. goto out;
  595. }
  596. check_upper = 0;
  597. BN_free(bnhigh);
  598. bnhigh = bnhigh2;
  599. bnhigh2 = NULL;
  600. BN_free(bnlow);
  601. bnlow = bnlow2;
  602. bnlow2 = NULL;
  603. }
  604. else if (BN_cmp(&bntmp2, bnhigh) < 0) {
  605. BN_copy(bnhigh, &bntmp2);
  606. }
  607.  
  608. /* Address ranges are complete */
  609. assert(check_upper || ((bnlow2 == NULL) && (bnhigh2 == NULL)));
  610. result[0] = bnlow;
  611. result[1] = bnhigh;
  612. result[2] = bnlow2;
  613. result[3] = bnhigh2;
  614. bnlow = NULL;
  615. bnhigh = NULL;
  616. bnlow2 = NULL;
  617. bnhigh2 = NULL;
  618. ret = 1;
  619.  
  620. out:
  621. BN_clear_free(&bntarg);
  622. BN_clear_free(&bnceil);
  623. BN_clear_free(&bnfloor);
  624. BN_clear_free(&bnbase);
  625. BN_clear_free(&bntmp);
  626. BN_clear_free(&bntmp2);
  627. if (bnhigh)
  628. BN_free(bnhigh);
  629. if (bnlow)
  630. BN_free(bnlow);
  631. if (bnhigh2)
  632. BN_free(bnhigh2);
  633. if (bnlow2)
  634. BN_free(bnlow2);
  635.  
  636. return ret;
  637. }
  638.  
  639. /*
  640. * AVL tree implementation
  641. */
  642.  
  643. typedef enum { CENT = 1, LEFT = 0, RIGHT = 2 } avl_balance_t;
  644.  
  645. typedef struct _avl_item_s {
  646. struct _avl_item_s *ai_left, *ai_right, *ai_up;
  647. avl_balance_t ai_balance;
  648. #ifndef NDEBUG
  649. int ai_indexed;
  650. #endif
  651. } avl_item_t;
  652.  
  653. typedef struct _avl_root_s {
  654. avl_item_t *ar_root;
  655. } avl_root_t;
  656.  
  657. INLINE void
  658. avl_root_init(avl_root_t *rootp)
  659. {
  660. rootp->ar_root = NULL;
  661. }
  662.  
  663. INLINE int
  664. avl_root_empty(avl_root_t *rootp)
  665. {
  666. return (rootp->ar_root == NULL) ? 1 : 0;
  667. }
  668.  
  669. INLINE void
  670. avl_item_init(avl_item_t *itemp)
  671. {
  672. memset(itemp, 0, sizeof(*itemp));
  673. itemp->ai_balance = CENT;
  674. }
  675.  
  676. #define container_of(ptr, type, member) \
  677. (((type*) (((unsigned char *)ptr) - \
  678. (size_t)&(((type *)((unsigned char *)0))->member))))
  679.  
  680. #define avl_item_entry(ptr, type, member) \
  681. container_of(ptr, type, member)
  682.  
  683.  
  684.  
  685. INLINE void
  686. _avl_rotate_ll(avl_root_t *rootp, avl_item_t *itemp)
  687. {
  688. avl_item_t *tmp;
  689. tmp = itemp->ai_left;
  690. itemp->ai_left = tmp->ai_right;
  691. if (itemp->ai_left)
  692. itemp->ai_left->ai_up = itemp;
  693. tmp->ai_right = itemp;
  694.  
  695. if (itemp->ai_up) {
  696. if (itemp->ai_up->ai_left == itemp) {
  697. itemp->ai_up->ai_left = tmp;
  698. } else {
  699. assert(itemp->ai_up->ai_right == itemp);
  700. itemp->ai_up->ai_right = tmp;
  701. }
  702. } else {
  703. rootp->ar_root = tmp;
  704. }
  705. tmp->ai_up = itemp->ai_up;
  706. itemp->ai_up = tmp;
  707. }
  708.  
  709. INLINE void
  710. _avl_rotate_lr(avl_root_t *rootp, avl_item_t *itemp)
  711. {
  712. avl_item_t *rcp, *rlcp;
  713. rcp = itemp->ai_left;
  714. rlcp = rcp->ai_right;
  715. if (itemp->ai_up) {
  716. if (itemp == itemp->ai_up->ai_left) {
  717. itemp->ai_up->ai_left = rlcp;
  718. } else {
  719. assert(itemp == itemp->ai_up->ai_right);
  720. itemp->ai_up->ai_right = rlcp;
  721. }
  722. } else {
  723. rootp->ar_root = rlcp;
  724. }
  725. rlcp->ai_up = itemp->ai_up;
  726. rcp->ai_right = rlcp->ai_left;
  727. if (rcp->ai_right)
  728. rcp->ai_right->ai_up = rcp;
  729. itemp->ai_left = rlcp->ai_right;
  730. if (itemp->ai_left)
  731. itemp->ai_left->ai_up = itemp;
  732. rlcp->ai_left = rcp;
  733. rlcp->ai_right = itemp;
  734. rcp->ai_up = rlcp;
  735. itemp->ai_up = rlcp;
  736. }
  737.  
  738. INLINE void
  739. _avl_rotate_rr(avl_root_t *rootp, avl_item_t *itemp)
  740. {
  741. avl_item_t *tmp;
  742. tmp = itemp->ai_right;
  743. itemp->ai_right = tmp->ai_left;
  744. if (itemp->ai_right)
  745. itemp->ai_right->ai_up = itemp;
  746. tmp->ai_left = itemp;
  747.  
  748. if (itemp->ai_up) {
  749. if (itemp->ai_up->ai_right == itemp) {
  750. itemp->ai_up->ai_right = tmp;
  751. } else {
  752. assert(itemp->ai_up->ai_left == itemp);
  753. itemp->ai_up->ai_left = tmp;
  754. }
  755. } else {
  756. rootp->ar_root = tmp;
  757. }
  758. tmp->ai_up = itemp->ai_up;
  759. itemp->ai_up = tmp;
  760. }
  761.  
  762. INLINE void
  763. _avl_rotate_rl(avl_root_t *rootp, avl_item_t *itemp)
  764. {
  765. avl_item_t *rcp, *rlcp;
  766. rcp = itemp->ai_right;
  767. rlcp = rcp->ai_left;
  768. if (itemp->ai_up) {
  769. if (itemp == itemp->ai_up->ai_right) {
  770. itemp->ai_up->ai_right = rlcp;
  771. } else {
  772. assert(itemp == itemp->ai_up->ai_left);
  773. itemp->ai_up->ai_left = rlcp;
  774. }
  775. } else {
  776. rootp->ar_root = rlcp;
  777. }
  778. rlcp->ai_up = itemp->ai_up;
  779. rcp->ai_left = rlcp->ai_right;
  780. if (rcp->ai_left)
  781. rcp->ai_left->ai_up = rcp;
  782. itemp->ai_right = rlcp->ai_left;
  783. if (itemp->ai_right)
  784. itemp->ai_right->ai_up = itemp;
  785. rlcp->ai_right = rcp;
  786. rlcp->ai_left = itemp;
  787. rcp->ai_up = rlcp;
  788. itemp->ai_up = rlcp;
  789. }
  790.  
  791. void
  792. avl_delete_fix(avl_root_t *rootp, avl_item_t *itemp, avl_item_t *parentp)
  793. {
  794. avl_item_t *childp;
  795.  
  796. if ((parentp->ai_left == NULL) &&
  797. (parentp->ai_right == NULL)) {
  798. assert(itemp == NULL);
  799. parentp->ai_balance = CENT;
  800. itemp = parentp;
  801. parentp = itemp->ai_up;
  802. }
  803.  
  804. while (parentp) {
  805. if (itemp == parentp->ai_right) {
  806. itemp = parentp->ai_left;
  807. if (parentp->ai_balance == LEFT) {
  808. /* Parent was left-heavy, now worse */
  809. if (itemp->ai_balance == LEFT) {
  810. /* If left child is also
  811. * left-heavy, LL fixes it. */
  812. _avl_rotate_ll(rootp, parentp);
  813. itemp->ai_balance = CENT;
  814. parentp->ai_balance = CENT;
  815. parentp = itemp;
  816. } else if (itemp->ai_balance == CENT) {
  817. _avl_rotate_ll(rootp, parentp);
  818. itemp->ai_balance = RIGHT;
  819. parentp->ai_balance = LEFT;
  820. break;
  821. } else {
  822. childp = itemp->ai_right;
  823. _avl_rotate_lr(rootp, parentp);
  824. itemp->ai_balance = CENT;
  825. parentp->ai_balance = CENT;
  826. if (childp->ai_balance == RIGHT)
  827. itemp->ai_balance = LEFT;
  828. if (childp->ai_balance == LEFT)
  829. parentp->ai_balance = RIGHT;
  830. childp->ai_balance = CENT;
  831. parentp = childp;
  832. }
  833. } else if (parentp->ai_balance == CENT) {
  834. parentp->ai_balance = LEFT;
  835. break;
  836. } else {
  837. parentp->ai_balance = CENT;
  838. }
  839.  
  840. } else {
  841. itemp = parentp->ai_right;
  842. if (parentp->ai_balance == RIGHT) {
  843. if (itemp->ai_balance == RIGHT) {
  844. _avl_rotate_rr(rootp, parentp);
  845. itemp->ai_balance = CENT;
  846. parentp->ai_balance = CENT;
  847. parentp = itemp;
  848. } else if (itemp->ai_balance == CENT) {
  849. _avl_rotate_rr(rootp, parentp);
  850. itemp->ai_balance = LEFT;
  851. parentp->ai_balance = RIGHT;
  852. break;
  853. } else {
  854. childp = itemp->ai_left;
  855. _avl_rotate_rl(rootp, parentp);
  856.  
  857. itemp->ai_balance = CENT;
  858. parentp->ai_balance = CENT;
  859. if (childp->ai_balance == RIGHT)
  860. parentp->ai_balance = LEFT;
  861. if (childp->ai_balance == LEFT)
  862. itemp->ai_balance = RIGHT;
  863. childp->ai_balance = CENT;
  864. parentp = childp;
  865. }
  866. } else if (parentp->ai_balance == CENT) {
  867. parentp->ai_balance = RIGHT;
  868. break;
  869. } else {
  870. parentp->ai_balance = CENT;
  871. }
  872. }
  873.  
  874. itemp = parentp;
  875. parentp = itemp->ai_up;
  876. }
  877. }
  878.  
  879. void
  880. avl_insert_fix(avl_root_t *rootp, avl_item_t *itemp)
  881. {
  882. avl_item_t *childp, *parentp = itemp->ai_up;
  883. itemp->ai_left = itemp->ai_right = NULL;
  884. #ifndef NDEBUG
  885. assert(!itemp->ai_indexed);
  886. itemp->ai_indexed = 1;
  887. #endif
  888. while (parentp) {
  889. if (itemp == parentp->ai_left) {
  890. if (parentp->ai_balance == LEFT) {
  891. /* Parent was left-heavy, now worse */
  892. if (itemp->ai_balance == LEFT) {
  893. /* If left child is also
  894. * left-heavy, LL fixes it. */
  895. _avl_rotate_ll(rootp, parentp);
  896. itemp->ai_balance = CENT;
  897. parentp->ai_balance = CENT;
  898. break;
  899. } else {
  900. assert(itemp->ai_balance != CENT);
  901. childp = itemp->ai_right;
  902. _avl_rotate_lr(rootp, parentp);
  903. itemp->ai_balance = CENT;
  904. parentp->ai_balance = CENT;
  905. if (childp->ai_balance == RIGHT)
  906. itemp->ai_balance = LEFT;
  907. if (childp->ai_balance == LEFT)
  908. parentp->ai_balance = RIGHT;
  909. childp->ai_balance = CENT;
  910. break;
  911. }
  912. } else if (parentp->ai_balance == CENT) {
  913. parentp->ai_balance = LEFT;
  914. } else {
  915. parentp->ai_balance = CENT;
  916. return;
  917. }
  918. } else {
  919. if (parentp->ai_balance == RIGHT) {
  920. if (itemp->ai_balance == RIGHT) {
  921. _avl_rotate_rr(rootp, parentp);
  922. itemp->ai_balance = CENT;
  923. parentp->ai_balance = CENT;
  924. break;
  925. } else {
  926. assert(itemp->ai_balance != CENT);
  927. childp = itemp->ai_left;
  928. _avl_rotate_rl(rootp, parentp);
  929. itemp->ai_balance = CENT;
  930. parentp->ai_balance = CENT;
  931. if (childp->ai_balance == RIGHT)
  932. parentp->ai_balance = LEFT;
  933. if (childp->ai_balance == LEFT)
  934. itemp->ai_balance = RIGHT;
  935. childp->ai_balance = CENT;
  936. break;
  937. }
  938. } else if (parentp->ai_balance == CENT) {
  939. parentp->ai_balance = RIGHT;
  940. } else {
  941. parentp->ai_balance = CENT;
  942. break;
  943. }
  944. }
  945.  
  946. itemp = parentp;
  947. parentp = itemp->ai_up;
  948. }
  949. }
  950.  
  951. INLINE avl_item_t *
  952. avl_next(avl_item_t *itemp)
  953. {
  954. if (itemp->ai_right) {
  955. itemp = itemp->ai_right;
  956. while (itemp->ai_left)
  957. itemp = itemp->ai_left;
  958. return itemp;
  959. }
  960.  
  961. while (itemp->ai_up && (itemp == itemp->ai_up->ai_right))
  962. itemp = itemp->ai_up;
  963.  
  964. if (!itemp->ai_up)
  965. return NULL;
  966.  
  967. return itemp->ai_up;
  968. }
  969.  
  970. void
  971. avl_remove(avl_root_t *rootp, avl_item_t *itemp)
  972. {
  973. avl_item_t *relocp, *replacep, *parentp = NULL;
  974. #ifndef NDEBUG
  975. assert(itemp->ai_indexed);
  976. itemp->ai_indexed = 0;
  977. #endif
  978. /* If the item is directly replaceable, do it. */
  979. if ((itemp->ai_left == NULL) || (itemp->ai_right == NULL)) {
  980. parentp = itemp->ai_up;
  981. replacep = itemp->ai_left;
  982. if (replacep == NULL)
  983. replacep = itemp->ai_right;
  984. if (replacep != NULL)
  985. replacep->ai_up = parentp;
  986. if (parentp == NULL) {
  987. rootp->ar_root = replacep;
  988. } else {
  989. if (itemp == parentp->ai_left)
  990. parentp->ai_left = replacep;
  991. else
  992. parentp->ai_right = replacep;
  993.  
  994. avl_delete_fix(rootp, replacep, parentp);
  995. }
  996. return;
  997. }
  998.  
  999. /*
  1000. * Otherwise we do an indirect replacement with
  1001. * the item's leftmost right descendant.
  1002. */
  1003. relocp = avl_next(itemp);
  1004. assert(relocp);
  1005. assert(relocp->ai_up != NULL);
  1006. assert(relocp->ai_left == NULL);
  1007. replacep = relocp->ai_right;
  1008. relocp->ai_left = itemp->ai_left;
  1009. if (relocp->ai_left != NULL)
  1010. relocp->ai_left->ai_up = relocp;
  1011. if (itemp->ai_up == NULL)
  1012. rootp->ar_root = relocp;
  1013. else {
  1014. if (itemp == itemp->ai_up->ai_left)
  1015. itemp->ai_up->ai_left = relocp;
  1016. else
  1017. itemp->ai_up->ai_right = relocp;
  1018. }
  1019. if (relocp == relocp->ai_up->ai_left) {
  1020. assert(relocp->ai_up != itemp);
  1021. relocp->ai_up->ai_left = replacep;
  1022. parentp = relocp->ai_up;
  1023. if (replacep != NULL)
  1024. replacep->ai_up = relocp->ai_up;
  1025. relocp->ai_right = itemp->ai_right;
  1026. } else {
  1027. assert(relocp->ai_up == itemp);
  1028. relocp->ai_right = replacep;
  1029. parentp = relocp;
  1030. }
  1031. if (relocp->ai_right != NULL)
  1032. relocp->ai_right->ai_up = relocp;
  1033. relocp->ai_up = itemp->ai_up;
  1034. relocp->ai_balance = itemp->ai_balance;
  1035. avl_delete_fix(rootp, replacep, parentp);
  1036. }
  1037.  
  1038.  
  1039.  
  1040. /*
  1041. * Address prefix AVL tree node
  1042. */
  1043.  
  1044. typedef struct _vg_prefix_s {
  1045. avl_item_t vp_item;
  1046. struct _vg_prefix_s *vp_sibling;
  1047. const char *vp_pattern;
  1048. BIGNUM *vp_low;
  1049. BIGNUM *vp_high;
  1050. } vg_prefix_t;
  1051.  
  1052. void
  1053. vg_prefix_free(vg_prefix_t *vp)
  1054. {
  1055. if (vp->vp_low)
  1056. BN_free(vp->vp_low);
  1057. if (vp->vp_high)
  1058. BN_free(vp->vp_high);
  1059. free(vp);
  1060. }
  1061.  
  1062. vg_prefix_t *
  1063. vg_prefix_avl_search(avl_root_t *rootp, BIGNUM *targ)
  1064. {
  1065. vg_prefix_t *vp;
  1066. avl_item_t *itemp = rootp->ar_root;
  1067.  
  1068. while (itemp) {
  1069. vp = avl_item_entry(itemp, vg_prefix_t, vp_item);
  1070. if (BN_cmp(vp->vp_low, targ) > 0) {
  1071. itemp = itemp->ai_left;
  1072. } else {
  1073. if (BN_cmp(vp->vp_high, targ) < 0) {
  1074. itemp = itemp->ai_right;
  1075. } else
  1076. return vp;
  1077. }
  1078. }
  1079. return NULL;
  1080. }
  1081.  
  1082. vg_prefix_t *
  1083. vg_prefix_avl_insert(avl_root_t *rootp, vg_prefix_t *vpnew)
  1084. {
  1085. vg_prefix_t *vp;
  1086. avl_item_t *itemp = NULL;
  1087. avl_item_t **ptrp = &rootp->ar_root;
  1088. while (*ptrp) {
  1089. itemp = *ptrp;
  1090. vp = avl_item_entry(itemp, vg_prefix_t, vp_item);
  1091. if (BN_cmp(vp->vp_low, vpnew->vp_high) > 0) {
  1092. ptrp = &itemp->ai_left;
  1093. } else {
  1094. if (BN_cmp(vp->vp_high, vpnew->vp_low) < 0) {
  1095. ptrp = &itemp->ai_right;
  1096. } else
  1097. return vp;
  1098. }
  1099. }
  1100. vpnew->vp_item.ai_up = itemp;
  1101. itemp = &vpnew->vp_item;
  1102. *ptrp = itemp;
  1103. avl_insert_fix(rootp, itemp);
  1104. return NULL;
  1105. }
  1106.  
  1107. vg_prefix_t *
  1108. vg_prefix_add(avl_root_t *rootp, const char *pattern, BIGNUM *low, BIGNUM *high)
  1109. {
  1110. vg_prefix_t *vp, *vp2;
  1111. assert(BN_cmp(low, high) < 0);
  1112. vp = (vg_prefix_t *) malloc(sizeof(*vp));
  1113. if (vp) {
  1114. avl_item_init(&vp->vp_item);
  1115. vp->vp_sibling = NULL;
  1116. vp->vp_pattern = pattern;
  1117. vp->vp_low = low;
  1118. vp->vp_high = high;
  1119. vp2 = vg_prefix_avl_insert(rootp, vp);
  1120. if (vp2 != NULL) {
  1121. printf("Prefix '%s' ignored, overlaps '%s'\n",
  1122. pattern, vp2->vp_pattern);
  1123. vg_prefix_free(vp);
  1124. vp = NULL;
  1125. }
  1126. }
  1127. return vp;
  1128. }
  1129.  
  1130. void
  1131. vg_prefix_delete(avl_root_t *rootp, vg_prefix_t *vp)
  1132. {
  1133. vg_prefix_t *sibp, *delp;
  1134.  
  1135. avl_remove(rootp, &vp->vp_item);
  1136. sibp = vp->vp_sibling;
  1137. while (sibp && sibp != vp) {
  1138. avl_remove(rootp, &sibp->vp_item);
  1139. delp = sibp;
  1140. sibp = sibp->vp_sibling;
  1141. vg_prefix_free(delp);
  1142. }
  1143. vg_prefix_free(vp);
  1144. }
  1145.  
  1146. vg_prefix_t *
  1147. vg_prefix_add_ranges(avl_root_t *rootp, const char *pattern, BIGNUM **ranges,
  1148. vg_prefix_t *master)
  1149. {
  1150. vg_prefix_t *vp, *vp2 = NULL;
  1151.  
  1152. assert(ranges[0]);
  1153. vp = vg_prefix_add(rootp, pattern, ranges[0], ranges[1]);
  1154. if (!vp)
  1155. return NULL;
  1156.  
  1157. if (ranges[2]) {
  1158. vp2 = vg_prefix_add(rootp, pattern, ranges[2], ranges[3]);
  1159. if (!vp2) {
  1160. vg_prefix_delete(rootp, vp);
  1161. return NULL;
  1162. }
  1163. }
  1164.  
  1165. if (!master) {
  1166. vp->vp_sibling = vp2;
  1167. if (vp2)
  1168. vp2->vp_sibling = vp;
  1169. } else if (vp2) {
  1170. vp->vp_sibling = vp2;
  1171. vp2->vp_sibling = (master->vp_sibling ?
  1172. master->vp_sibling :
  1173. master);
  1174. master->vp_sibling = vp;
  1175. } else {
  1176. vp->vp_sibling = (master->vp_sibling ?
  1177. master->vp_sibling :
  1178. master);
  1179. master->vp_sibling = vp;
  1180. }
  1181. return vp;
  1182. }
  1183.  
  1184. void
  1185. vg_prefix_range_sum(vg_prefix_t *vp, BIGNUM *result, BIGNUM *tmp1, BIGNUM *tmp2)
  1186. {
  1187. vg_prefix_t *startp;
  1188.  
  1189. BIGNUM *bnptr = result;
  1190.  
  1191. startp = vp;
  1192. BN_clear(result);
  1193. do {
  1194. BN_sub(tmp1, vp->vp_high, vp->vp_low);
  1195. if (bnptr == result) {
  1196. BN_add(tmp2, bnptr, tmp1);
  1197. bnptr = tmp2;
  1198. } else {
  1199. BN_add(result, bnptr, tmp1);
  1200. bnptr = result;
  1201. }
  1202. vp = vp->vp_sibling;
  1203. } while (vp && (vp != startp));
  1204.  
  1205. if (bnptr != result)
  1206. BN_copy(result, bnptr);
  1207. }
  1208.  
  1209.  
  1210. typedef struct _prefix_case_iter_s {
  1211. char ci_prefix[32];
  1212. char ci_case_map[32];
  1213. char ci_nbits;
  1214. int ci_value;
  1215. } prefix_case_iter_t;
  1216.  
  1217. const unsigned char b58_case_map[256] = {
  1218. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1219. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1220. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1221. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1222. 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0,
  1223. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  1224. 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0,
  1225. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  1226. };
  1227.  
  1228. int
  1229. prefix_case_iter_init(prefix_case_iter_t *cip, const char *pfx)
  1230. {
  1231. int i;
  1232.  
  1233. cip->ci_nbits = 0;
  1234. cip->ci_value = 0;
  1235. for (i = 0; pfx[i]; i++) {
  1236. if (i > sizeof(cip->ci_prefix))
  1237. return 0;
  1238. if (!b58_case_map[(int)pfx[i]]) {
  1239. cip->ci_prefix[i] = pfx[i];
  1240. continue;
  1241. }
  1242. cip->ci_prefix[i] = pfx[i] | 0x20;
  1243. cip->ci_case_map[(int)cip->ci_nbits] = i;
  1244. cip->ci_nbits++;
  1245. }
  1246. cip->ci_prefix[i] = '\0';
  1247. return 1;
  1248. }
  1249.  
  1250. int
  1251. prefix_case_iter_next(prefix_case_iter_t *cip)
  1252. {
  1253. unsigned long val, max, mask;
  1254. int i, nbits;
  1255.  
  1256. nbits = cip->ci_nbits;
  1257. max = (1UL << nbits) - 1;
  1258. val = cip->ci_value + 1;
  1259. if (val >= max)
  1260. return 0;
  1261.  
  1262. for (i = 0, mask = 1; i < nbits; i++, mask <<= 1) {
  1263. if (val & mask)
  1264. cip->ci_prefix[(int)cip->ci_case_map[i]] &= 0xdf;
  1265. else
  1266. cip->ci_prefix[(int)cip->ci_case_map[i]] |= 0x20;
  1267. }
  1268. cip->ci_value = val;
  1269. return 1;
  1270. }
  1271.  
  1272.  
  1273. typedef struct _vg_prefix_context_s {
  1274. avl_root_t vcp_avlroot;
  1275. int vcp_addrtype;
  1276. int vcp_privtype;
  1277. unsigned long vcp_npfx;
  1278. unsigned long vcp_npfx_start;
  1279. unsigned long long vcp_found;
  1280. double vcp_chance;
  1281. BIGNUM vcp_difficulty;
  1282. pthread_mutex_t vcp_lock;
  1283. } vg_prefix_context_t;
  1284.  
  1285. void
  1286. vg_prefix_context_free(vg_prefix_context_t *vcpp)
  1287. {
  1288. vg_prefix_t *vp;
  1289. unsigned long npfx_left = 0;
  1290.  
  1291. while (!avl_root_empty(&vcpp->vcp_avlroot)) {
  1292. vp = avl_item_entry(vcpp->vcp_avlroot.ar_root,
  1293. vg_prefix_t, vp_item);
  1294. vg_prefix_delete(&vcpp->vcp_avlroot, vp);
  1295. npfx_left++;
  1296. }
  1297.  
  1298. assert(npfx_left == vcpp->vcp_npfx);
  1299. BN_clear_free(&vcpp->vcp_difficulty);
  1300. pthread_mutex_destroy(&vcpp->vcp_lock);
  1301. free(vcpp);
  1302. }
  1303.  
  1304. void
  1305. vg_prefix_context_next_difficulty(vg_prefix_context_t *vcpp,
  1306. BIGNUM *bntmp, BIGNUM *bntmp2, BN_CTX *bnctx)
  1307. {
  1308. char *dbuf;
  1309.  
  1310. BN_clear(bntmp);
  1311. BN_set_bit(bntmp, 192);
  1312. BN_div(bntmp2, NULL, bntmp, &vcpp->vcp_difficulty, bnctx);
  1313.  
  1314. dbuf = BN_bn2dec(bntmp2);
  1315. if (verbose > 0) {
  1316. if (vcpp->vcp_npfx > 1)
  1317. printf("Next match difficulty: %s (%ld prefixes)\n",
  1318. dbuf, vcpp->vcp_npfx);
  1319. else
  1320. printf("Difficulty: %s\n", dbuf);
  1321. }
  1322. vcpp->vcp_chance = atof(dbuf);
  1323. OPENSSL_free(dbuf);
  1324. }
  1325.  
  1326. vg_prefix_context_t *
  1327. vg_prefix_context_new(int addrtype, int privtype)
  1328. {
  1329. vg_prefix_context_t *vcpp;
  1330.  
  1331. vcpp = (vg_prefix_context_t *) malloc(sizeof(*vcpp));
  1332. if (vcpp) {
  1333. vcpp->vcp_addrtype = addrtype;
  1334. vcpp->vcp_privtype = privtype;
  1335. vcpp->vcp_npfx = 0;
  1336. vcpp->vcp_npfx_start = 0;
  1337. vcpp->vcp_found = 0;
  1338. avl_root_init(&vcpp->vcp_avlroot);
  1339. BN_init(&vcpp->vcp_difficulty);
  1340. pthread_mutex_init(&vcpp->vcp_lock, NULL);
  1341. }
  1342. return vcpp;
  1343. }
  1344.  
  1345. int
  1346. vg_prefix_context_add_patterns(vg_prefix_context_t *vcpp,
  1347. char ** const patterns, int npatterns,
  1348. int caseinsensitive)
  1349. {
  1350. prefix_case_iter_t caseiter;
  1351. vg_prefix_t *vp, *vp2;
  1352. BN_CTX *bnctx;
  1353. BIGNUM bntmp, bntmp2, bntmp3;
  1354. BIGNUM *ranges[4];
  1355. int ret = 0;
  1356. int i, fail;
  1357. unsigned long npfx;
  1358. char *dbuf;
  1359.  
  1360. bnctx = BN_CTX_new();
  1361. BN_init(&bntmp);
  1362. BN_init(&bntmp2);
  1363. BN_init(&bntmp3);
  1364.  
  1365. npfx = 0;
  1366. fail = 0;
  1367. for (i = 0; i < npatterns; i++) {
  1368. if (!caseinsensitive) {
  1369. vp = NULL;
  1370. if (get_prefix_ranges(vcpp->vcp_addrtype, patterns[i],
  1371. ranges, bnctx)) {
  1372. vp = vg_prefix_add_ranges(&vcpp->vcp_avlroot,
  1373. patterns[i],
  1374. ranges, NULL);
  1375. }
  1376.  
  1377. } else {
  1378. /* Case-enumerate the prefix */
  1379. if (!prefix_case_iter_init(&caseiter, patterns[i])) {
  1380. printf("Prefix '%s' is too long\n",
  1381. patterns[i]);
  1382. continue;
  1383. }
  1384.  
  1385. if (caseiter.ci_nbits > 16) {
  1386. printf("WARNING: Prefix '%s' has "
  1387. "2^%d case-varied derivitives\n",
  1388. patterns[i], caseiter.ci_nbits);
  1389. }
  1390.  
  1391. vp = NULL;
  1392. fail = 0;
  1393. do {
  1394. if (!get_prefix_ranges(vcpp->vcp_addrtype,
  1395. caseiter.ci_prefix,
  1396. ranges, bnctx)) {
  1397. fail = 1;
  1398. break;
  1399. }
  1400. vp2 = vg_prefix_add_ranges(&vcpp->vcp_avlroot,
  1401. patterns[i],
  1402. ranges,
  1403. vp);
  1404. if (!vp2) {
  1405. fail = 1;
  1406. break;
  1407. }
  1408. if (!vp)
  1409. vp = vp2;
  1410.  
  1411. } while (prefix_case_iter_next(&caseiter));
  1412.  
  1413. if (fail && vp) {
  1414. vg_prefix_delete(&vcpp->vcp_avlroot, vp);
  1415. vp = NULL;
  1416. }
  1417. }
  1418.  
  1419. if (!vp)
  1420. continue;
  1421.  
  1422. npfx++;
  1423.  
  1424. /* Determine the probability of finding a match */
  1425. vg_prefix_range_sum(vp, &bntmp, &bntmp2, &bntmp3);
  1426. BN_add(&bntmp2, &vcpp->vcp_difficulty, &bntmp);
  1427. BN_copy(&vcpp->vcp_difficulty, &bntmp2);
  1428.  
  1429. if (verbose > 1) {
  1430. BN_clear(&bntmp2);
  1431. BN_set_bit(&bntmp2, 192);
  1432. BN_div(&bntmp3, NULL, &bntmp2, &bntmp, bnctx);
  1433.  
  1434. dbuf = BN_bn2dec(&bntmp3);
  1435. printf("Prefix difficulty: %20s %s\n",
  1436. dbuf, patterns[i]);
  1437. OPENSSL_free(dbuf);
  1438. }
  1439. }
  1440.  
  1441. vcpp->vcp_npfx += npfx;
  1442. vcpp->vcp_npfx_start += npfx;
  1443.  
  1444. if (avl_root_empty(&vcpp->vcp_avlroot)) {
  1445. printf("No prefix patterns to search\n");
  1446. vg_prefix_context_free(vcpp);
  1447. vcpp = NULL;
  1448. goto out;
  1449. }
  1450.  
  1451. assert(npfx);
  1452. ret = 1;
  1453.  
  1454. vg_prefix_context_next_difficulty(vcpp, &bntmp, &bntmp2, bnctx);
  1455.  
  1456. out:
  1457. BN_clear_free(&bntmp);
  1458. BN_clear_free(&bntmp2);
  1459. BN_clear_free(&bntmp3);
  1460. BN_CTX_free(bnctx);
  1461. return ret;
  1462. }
  1463.  
  1464. /*
  1465. * Search for a key for which the encoded address has a specific prefix.
  1466. * Uses bignum arithmetic to predetermine value ranges.
  1467. * Faster than regular expression searching.
  1468. */
  1469. void *
  1470. generate_address_prefix(vg_prefix_context_t *vcpp)
  1471. {
  1472. unsigned char eckey_buf[128];
  1473. unsigned char hash1[32];
  1474. unsigned char binres[25] = {0,};
  1475.  
  1476. int i, c;
  1477.  
  1478. BN_ULONG npoints, rekey_at;
  1479.  
  1480. BN_CTX *bnctx;
  1481. BIGNUM bntarg;
  1482. BIGNUM bnbase;
  1483. BIGNUM bntmp, bntmp2;
  1484.  
  1485. EC_KEY *pkey = NULL;
  1486. const EC_GROUP *pgroup;
  1487. const EC_POINT *pgen;
  1488. EC_POINT *ppnt = NULL;
  1489.  
  1490. struct timeval tvstart;
  1491. vg_prefix_t *vp;
  1492.  
  1493. bnctx = BN_CTX_new();
  1494.  
  1495. BN_init(&bntarg);
  1496. BN_init(&bnbase);
  1497. BN_init(&bntmp);
  1498. BN_init(&bntmp2);
  1499.  
  1500. BN_set_word(&bnbase, 58);
  1501.  
  1502. pthread_mutex_lock(&vcpp->vcp_lock);
  1503.  
  1504. pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
  1505. pgroup = EC_KEY_get0_group(pkey);
  1506. pgen = EC_GROUP_get0_generator(pgroup);
  1507.  
  1508. EC_KEY_precompute_mult(pkey, bnctx);
  1509.  
  1510. pthread_mutex_unlock(&vcpp->vcp_lock);
  1511.  
  1512. npoints = 0;
  1513. rekey_at = 0;
  1514. binres[0] = vcpp->vcp_addrtype;
  1515. c = 0;
  1516. gettimeofday(&tvstart, NULL);
  1517. while (1) {
  1518. if (++npoints >= rekey_at) {
  1519. pthread_mutex_lock(&vcpp->vcp_lock);
  1520. if (avl_root_empty(&vcpp->vcp_avlroot))
  1521. break;
  1522.  
  1523. /* Generate a new random private key */
  1524. EC_KEY_generate_key(pkey);
  1525. npoints = 0;
  1526.  
  1527. pthread_mutex_unlock(&vcpp->vcp_lock);
  1528.  
  1529. /* Determine rekey interval */
  1530. EC_GROUP_get_order(pgroup, &bntmp, bnctx);
  1531. BN_sub(&bntmp2,
  1532. &bntmp,
  1533. EC_KEY_get0_private_key(pkey));
  1534. rekey_at = BN_get_word(&bntmp2);
  1535. if ((rekey_at == BN_MASK2) || (rekey_at > 1000000))
  1536. rekey_at = 1000000;
  1537. assert(rekey_at > 0);
  1538.  
  1539. if (ppnt)
  1540. EC_POINT_free(ppnt);
  1541. ppnt = EC_POINT_dup(EC_KEY_get0_public_key(pkey),
  1542. pgroup);
  1543.  
  1544. } else {
  1545. /* Common case: next point */
  1546. EC_POINT_add(pgroup, ppnt, ppnt, pgen, bnctx);
  1547. }
  1548.  
  1549. /* Hash the public key */
  1550. i = EC_POINT_point2oct(pgroup, ppnt,
  1551. POINT_CONVERSION_UNCOMPRESSED,
  1552. eckey_buf, sizeof(eckey_buf), bnctx);
  1553. SHA256(eckey_buf, i, hash1);
  1554. RIPEMD160(hash1, sizeof(hash1), &binres[1]);
  1555.  
  1556. /*
  1557. * We constrain the prefix so that we can check for a match
  1558. * without generating the lower four byte check code.
  1559. */
  1560.  
  1561. BN_bin2bn(binres, sizeof(binres), &bntarg);
  1562.  
  1563. pthread_mutex_lock(&vcpp->vcp_lock);
  1564. vp = vg_prefix_avl_search(&vcpp->vcp_avlroot, &bntarg);
  1565. if (vp) {
  1566. if (npoints) {
  1567. BN_clear(&bntmp);
  1568. BN_set_word(&bntmp, npoints);
  1569. BN_add(&bntmp2,
  1570. EC_KEY_get0_private_key(pkey),
  1571. &bntmp);
  1572. EC_KEY_set_private_key(pkey, &bntmp2);
  1573. EC_KEY_set_public_key(pkey, ppnt);
  1574.  
  1575. /* Rekey immediately */
  1576. rekey_at = 0;
  1577. npoints = 0;
  1578. }
  1579.  
  1580. output_match(pkey, vp->vp_pattern,
  1581. vcpp->vcp_addrtype,
  1582. vcpp->vcp_privtype);
  1583.  
  1584. vcpp->vcp_found++;
  1585.  
  1586. if (remove_on_match) {
  1587. /* Subtract the range from the difficulty */
  1588. vg_prefix_range_sum(vp, &bntarg,
  1589. &bntmp, &bntmp2);
  1590. BN_sub(&bntmp, &vcpp->vcp_difficulty, &bntarg);
  1591. BN_copy(&vcpp->vcp_difficulty, &bntmp);
  1592.  
  1593. vg_prefix_delete(&vcpp->vcp_avlroot, vp);
  1594. vcpp->vcp_npfx--;
  1595. if (avl_root_empty(&vcpp->vcp_avlroot))
  1596. break;
  1597.  
  1598. vg_prefix_context_next_difficulty(vcpp, &bntmp,
  1599. &bntmp2,
  1600. bnctx);
  1601. }
  1602. }
  1603.  
  1604. if (++c >= 20000) {
  1605. output_timing(c, &tvstart,
  1606. vcpp->vcp_found,
  1607. remove_on_match ? vcpp->vcp_npfx_start : 0,
  1608. vcpp->vcp_chance);
  1609. c = 0;
  1610. }
  1611.  
  1612. if (avl_root_empty(&vcpp->vcp_avlroot))
  1613. break;
  1614. pthread_mutex_unlock(&vcpp->vcp_lock);
  1615. }
  1616.  
  1617. pthread_mutex_unlock(&vcpp->vcp_lock);
  1618. BN_clear_free(&bntarg);
  1619. BN_clear_free(&bnbase);
  1620. BN_clear_free(&bntmp);
  1621. BN_clear_free(&bntmp2);
  1622. BN_CTX_free(bnctx);
  1623. EC_KEY_free(pkey);
  1624. EC_POINT_free(ppnt);
  1625. return NULL;
  1626. }
  1627.  
  1628.  
  1629. typedef struct _vg_regex_context_s {
  1630. pcre **vcr_regex;
  1631. pcre_extra **vcr_regex_extra;
  1632. const char **vcr_regex_pat;
  1633. int vcr_addrtype;
  1634. int vcr_privtype;
  1635. unsigned long long vcr_found;
  1636. unsigned long vcr_nres;
  1637. unsigned long vcr_nres_start;
  1638. unsigned long vcr_nalloc;
  1639. pthread_rwlock_t vcr_lock;
  1640. } vg_regex_context_t;
  1641.  
  1642. vg_regex_context_t *
  1643. vg_regex_context_new(int addrtype, int privtype)
  1644. {
  1645. vg_regex_context_t *vcrp;
  1646.  
  1647. vcrp = (vg_regex_context_t *) malloc(sizeof(*vcrp));
  1648. if (vcrp) {
  1649. vcrp->vcr_regex = NULL;
  1650. vcrp->vcr_found = 0;
  1651. vcrp->vcr_nalloc = 0;
  1652. vcrp->vcr_nres = 0;
  1653. vcrp->vcr_nres_start = 0;
  1654. vcrp->vcr_addrtype = addrtype;
  1655. vcrp->vcr_privtype = privtype;
  1656. pthread_rwlock_init(&vcrp->vcr_lock, NULL);
  1657. }
  1658. return vcrp;
  1659. }
  1660.  
  1661. int
  1662. vg_regex_context_add_patterns(vg_regex_context_t *vcrp,
  1663. char ** const patterns, int npatterns)
  1664. {
  1665. const char *pcre_errptr;
  1666. int pcre_erroffset;
  1667. unsigned long i, nres, count;
  1668. void **mem;
  1669.  
  1670. if (!npatterns)
  1671. return 1;
  1672.  
  1673. if (npatterns > (vcrp->vcr_nalloc - vcrp->vcr_nres)) {
  1674. count = npatterns + vcrp->vcr_nres;
  1675. if (count < (2 * vcrp->vcr_nalloc)) {
  1676. count = (2 * vcrp->vcr_nalloc);
  1677. }
  1678. if (count < 16) {
  1679. count = 16;
  1680. }
  1681. mem = (void **) malloc(3 * count * sizeof(void*));
  1682. if (!mem)
  1683. return 0;
  1684.  
  1685. for (i = 0; i < vcrp->vcr_nres; i++) {
  1686. mem[i] = vcrp->vcr_regex[i];
  1687. mem[count + i] = vcrp->vcr_regex_extra[i];
  1688. mem[(2 * count) + i] = (void *) vcrp->vcr_regex_pat[i];
  1689. }
  1690.  
  1691. if (vcrp->vcr_nalloc)
  1692. free(vcrp->vcr_regex);
  1693. vcrp->vcr_regex = (pcre **) mem;
  1694. vcrp->vcr_regex_extra = (pcre_extra **) &mem[count];
  1695. vcrp->vcr_regex_pat = (const char **) &mem[2 * count];
  1696. vcrp->vcr_nalloc = count;
  1697. }
  1698.  
  1699. nres = vcrp->vcr_nres;
  1700. for (i = 0; i < npatterns; i++) {
  1701. vcrp->vcr_regex[nres] =
  1702. pcre_compile(patterns[i], 0,
  1703. &pcre_errptr, &pcre_erroffset, NULL);
  1704. if (!vcrp->vcr_regex[nres]) {
  1705. const char *spaces = " ";
  1706. printf("%s\n", patterns[i]);
  1707. while (pcre_erroffset > 16) {
  1708. printf("%s", spaces);
  1709. pcre_erroffset -= 16;
  1710. }
  1711. if (pcre_erroffset > 0)
  1712. printf("%s", &spaces[16 - pcre_erroffset]);
  1713. printf("^\nRegex error: %s\n", pcre_errptr);
  1714. continue;
  1715. }
  1716. vcrp->vcr_regex_extra[nres] =
  1717. pcre_study(vcrp->vcr_regex[nres], 0, &pcre_errptr);
  1718. if (pcre_errptr) {
  1719. printf("Regex error: %s\n", pcre_errptr);
  1720. pcre_free(vcrp->vcr_regex[nres]);
  1721. continue;
  1722. }
  1723. vcrp->vcr_regex_pat[nres] = patterns[i];
  1724. nres += 1;
  1725. }
  1726.  
  1727. if (nres == vcrp->vcr_nres)
  1728. return 0;
  1729.  
  1730. vcrp->vcr_nres_start += (nres - vcrp->vcr_nres);
  1731. vcrp->vcr_nres = nres;
  1732. return 1;
  1733. }
  1734.  
  1735. void
  1736. vg_regex_context_free(vg_regex_context_t *vcrp)
  1737. {
  1738. int i;
  1739. for (i = 0; i < vcrp->vcr_nres; i++) {
  1740. if (vcrp->vcr_regex_extra[i])
  1741. pcre_free(vcrp->vcr_regex_extra[i]);
  1742. pcre_free(vcrp->vcr_regex[i]);
  1743. }
  1744. if (vcrp->vcr_nalloc)
  1745. free(vcrp->vcr_regex);
  1746. pthread_rwlock_destroy(&vcrp->vcr_lock);
  1747. free(vcrp);
  1748. }
  1749.  
  1750. /*
  1751. * Search for a key for which the encoded address matches a regular
  1752. * expression.
  1753. * Equivalent behavior to the bitcoin vanity address patch.
  1754. */
  1755. void *
  1756. generate_address_regex(vg_regex_context_t *vcrp)
  1757. {
  1758. unsigned char eckey_buf[128];
  1759. unsigned char hash1[32], hash2[32];
  1760. unsigned char binres[25] = {0,};
  1761. char b58[40];
  1762.  
  1763. int i, c, zpfx, p, d, nres, re_vec[9];
  1764.  
  1765. BN_ULONG npoints, rekey_at;
  1766.  
  1767. BN_CTX *bnctx;
  1768. BIGNUM bna, bnb, bnbase, bnrem, bntmp, bntmp2;
  1769. BIGNUM *bn, *bndiv, *bnptmp;
  1770.  
  1771. EC_KEY *pkey;
  1772. const EC_GROUP *pgroup;
  1773. const EC_POINT *pgen;
  1774. EC_POINT *ppnt = NULL;
  1775. pcre *re;
  1776.  
  1777. struct timeval tvstart;
  1778.  
  1779. bnctx = BN_CTX_new();
  1780.  
  1781. BN_init(&bna);
  1782. BN_init(&bnb);
  1783. BN_init(&bnbase);
  1784. BN_init(&bnrem);
  1785. BN_init(&bntmp);
  1786. BN_init(&bntmp2);
  1787.  
  1788. BN_set_word(&bnbase, 58);
  1789.  
  1790. pthread_rwlock_wrlock(&vcrp->vcr_lock);
  1791.  
  1792. pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
  1793. pgroup = EC_KEY_get0_group(pkey);
  1794. pgen = EC_GROUP_get0_generator(pgroup);
  1795.  
  1796. EC_KEY_precompute_mult(pkey, bnctx);
  1797.  
  1798. pthread_rwlock_unlock(&vcrp->vcr_lock);
  1799.  
  1800. npoints = 0;
  1801. rekey_at = 0;
  1802. binres[0] = vcrp->vcr_addrtype;
  1803. c = 0;
  1804. gettimeofday(&tvstart, NULL);
  1805.  
  1806. while (1) {
  1807. if (++npoints >= rekey_at) {
  1808. pthread_rwlock_wrlock(&vcrp->vcr_lock);
  1809. if (!vcrp->vcr_nres)
  1810. break;
  1811.  
  1812. /* Generate a new random private key */
  1813. EC_KEY_generate_key(pkey);
  1814. npoints = 0;
  1815.  
  1816. pthread_rwlock_unlock(&vcrp->vcr_lock);
  1817.  
  1818. /* Determine rekey interval */
  1819. EC_GROUP_get_order(pgroup, &bntmp, bnctx);
  1820. BN_sub(&bntmp2,
  1821. &bntmp,
  1822. EC_KEY_get0_private_key(pkey));
  1823. rekey_at = BN_get_word(&bntmp2);
  1824. if ((rekey_at == BN_MASK2) || (rekey_at > 1000000))
  1825. rekey_at = 1000000;
  1826. assert(rekey_at > 0);
  1827.  
  1828. if (ppnt)
  1829. EC_POINT_free(ppnt);
  1830. ppnt = EC_POINT_dup(EC_KEY_get0_public_key(pkey),
  1831. pgroup);
  1832.  
  1833. } else {
  1834. /* Common case: next point */
  1835. EC_POINT_add(pgroup, ppnt, ppnt, pgen, bnctx);
  1836. }
  1837.  
  1838. /* Hash the public key */
  1839. d = EC_POINT_point2oct(pgroup, ppnt,
  1840. POINT_CONVERSION_UNCOMPRESSED,
  1841. eckey_buf, sizeof(eckey_buf), bnctx);
  1842. SHA256(eckey_buf, d, hash1);
  1843. RIPEMD160(hash1, sizeof(hash1), &binres[1]);
  1844.  
  1845. /* Hash the hash and write the four byte check code */
  1846. SHA256(binres, 21, hash1);
  1847. SHA256(hash1, sizeof(hash1), hash2);
  1848. memcpy(&binres[21], hash2, 4);
  1849.  
  1850. bn = &bna;
  1851. bndiv = &bnb;
  1852.  
  1853. BN_bin2bn(binres, sizeof(binres), bn);
  1854.  
  1855. /* Compute the complete encoded address */
  1856. for (zpfx = 0; zpfx < 25 && binres[zpfx] == 0; zpfx++);
  1857. p = sizeof(b58) - 1;
  1858. b58[p] = '\0';
  1859. while (!BN_is_zero(bn)) {
  1860. BN_div(bndiv, &bnrem, bn, &bnbase, bnctx);
  1861. bnptmp = bn;
  1862. bn = bndiv;
  1863. bndiv = bnptmp;
  1864. d = BN_get_word(&bnrem);
  1865. b58[--p] = b58_alphabet[d];
  1866. }
  1867. while (zpfx--) {
  1868. b58[--p] = b58_alphabet[0];
  1869. }
  1870.  
  1871. /*
  1872. * Run the regular expressions on it
  1873. * SLOW, runs in linear time with the number of REs
  1874. */
  1875. pthread_rwlock_rdlock(&vcrp->vcr_lock);
  1876. nres = vcrp->vcr_nres;
  1877. if (!nres)
  1878. break;
  1879. for (i = 0; i < nres; i++) {
  1880. d = pcre_exec(vcrp->vcr_regex[i],
  1881. vcrp->vcr_regex_extra[i],
  1882. &b58[p], (sizeof(b58) - 1) - p, 0,
  1883. 0,
  1884. re_vec, sizeof(re_vec)/sizeof(re_vec[0]));
  1885.  
  1886. if (d <= 0) {
  1887. if (d != PCRE_ERROR_NOMATCH) {
  1888. printf("PCRE error: %d\n", d);
  1889. goto out;
  1890. }
  1891. continue;
  1892. }
  1893.  
  1894.  
  1895. re = vcrp->vcr_regex[i];
  1896. pthread_rwlock_unlock(&vcrp->vcr_lock);
  1897. pthread_rwlock_wrlock(&vcrp->vcr_lock);
  1898. nres = vcrp->vcr_nres;
  1899. if ((i >= nres) || (vcrp->vcr_regex[i] != re)) {
  1900. /* Redo the search */
  1901. i = -1;
  1902. pthread_rwlock_unlock(&vcrp->vcr_lock);
  1903. pthread_rwlock_rdlock(&vcrp->vcr_lock);
  1904. nres = vcrp->vcr_nres;
  1905. if (!nres)
  1906. goto out;
  1907. continue;
  1908. }
  1909.  
  1910. if (npoints) {
  1911. BN_clear(&bntmp);
  1912. BN_set_word(&bntmp, npoints);
  1913. BN_add(&bntmp2,
  1914. EC_KEY_get0_private_key(pkey),
  1915. &bntmp);
  1916. EC_KEY_set_private_key(pkey, &bntmp2);
  1917. EC_KEY_set_public_key(pkey, ppnt);
  1918.  
  1919. /* Rekey immediately */
  1920. rekey_at = 0;
  1921. npoints = 0;
  1922. }
  1923.  
  1924. output_match(pkey, vcrp->vcr_regex_pat[i],
  1925. vcrp->vcr_addrtype, vcrp->vcr_privtype);
  1926.  
  1927. vcrp->vcr_found++;
  1928.  
  1929. if (remove_on_match) {
  1930. pcre_free(vcrp->vcr_regex[i]);
  1931. if (vcrp->vcr_regex_extra[i])
  1932. pcre_free(vcrp->vcr_regex_extra[i]);
  1933. nres -= 1;
  1934. vcrp->vcr_nres = nres;
  1935. if (!nres)
  1936. goto out;
  1937. vcrp->vcr_regex[i] = vcrp->vcr_regex[nres];
  1938. vcrp->vcr_regex_extra[i] =
  1939. vcrp->vcr_regex_extra[nres];
  1940. vcrp->vcr_regex_pat[i] =
  1941. vcrp->vcr_regex_pat[nres];
  1942. vcrp->vcr_nres = nres;
  1943. }
  1944. }
  1945.  
  1946. if (++c >= 10000) {
  1947. output_timing(c, &tvstart,
  1948. vcrp->vcr_found,
  1949. remove_on_match ? vcrp->vcr_nres_start : 0,
  1950. 0.0);
  1951. c = 0;
  1952. }
  1953.  
  1954. pthread_rwlock_unlock(&vcrp->vcr_lock);
  1955. }
  1956.  
  1957. out:
  1958. pthread_rwlock_unlock(&vcrp->vcr_lock);
  1959. BN_clear_free(&bna);
  1960. BN_clear_free(&bnb);
  1961. BN_clear_free(&bnbase);
  1962. BN_clear_free(&bnrem);
  1963. BN_clear_free(&bntmp);
  1964. BN_clear_free(&bntmp2);
  1965. BN_CTX_free(bnctx);
  1966. EC_KEY_free(pkey);
  1967. EC_POINT_free(ppnt);
  1968. return NULL;
  1969. }
  1970.  
  1971.  
  1972. int
  1973. read_file(FILE *fp, char ***result, int *rescount)
  1974. {
  1975. int ret = 1;
  1976.  
  1977. char **patterns;
  1978. char *buf = NULL, *obuf, *pat;
  1979. const int blksize = 16*1024;
  1980. int nalloc = 16;
  1981. int npatterns = 0;
  1982. int count, pos;
  1983.  
  1984. patterns = (char**) malloc(sizeof(char*) * nalloc);
  1985. count = 0;
  1986. pos = 0;
  1987.  
  1988. while (1) {
  1989. obuf = buf;
  1990. buf = (char *) malloc(blksize);
  1991. if (!buf) {
  1992. ret = 0;
  1993. break;
  1994. }
  1995. if (pos < count) {
  1996. memcpy(buf, &obuf[pos], count - pos);
  1997. }
  1998. pos = count - pos;
  1999. count = fread(&buf[pos], 1, blksize - pos, fp);
  2000. if (count < 0) {
  2001. printf("Error reading file: %s\n", strerror(errno));
  2002. ret = 0;
  2003. }
  2004. if (count <= 0)
  2005. break;
  2006. count += pos;
  2007. pat = buf;
  2008.  
  2009. while (pos < count) {
  2010. if ((buf[pos] == '\r') || (buf[pos] == '\n')) {
  2011. buf[pos] = '\0';
  2012. if (pat) {
  2013. if (npatterns == nalloc) {
  2014. nalloc *= 2;
  2015. patterns = (char**)
  2016. realloc(patterns,
  2017. sizeof(char*) *
  2018. nalloc);
  2019. }
  2020. patterns[npatterns] = pat;
  2021. npatterns++;
  2022. pat = NULL;
  2023. }
  2024. }
  2025. else if (!pat) {
  2026. pat = &buf[pos];
  2027. }
  2028. pos++;
  2029. }
  2030.  
  2031. pos = pat ? (pat - buf) : count;
  2032. }
  2033.  
  2034. *result = patterns;
  2035. *rescount = npatterns;
  2036.  
  2037. return ret;
  2038. }
  2039.  
  2040. #if !defined(_WIN32)
  2041. int
  2042. count_processors(void)
  2043. {
  2044. FILE *fp;
  2045. char buf[512];
  2046. int count = 0;
  2047.  
  2048. fp = fopen("/proc/cpuinfo", "r");
  2049. if (!fp)
  2050. return -1;
  2051.  
  2052. while (fgets(buf, sizeof(buf), fp)) {
  2053. if (!strncmp(buf, "processor\t", 10))
  2054. count += 1;
  2055. }
  2056. fclose(fp);
  2057. return count;
  2058. }
  2059. #endif
  2060.  
  2061. int
  2062. start_threads(void *(*func)(void *), void *arg, int nthreads)
  2063. {
  2064. pthread_t thread;
  2065.  
  2066. if (nthreads <= 0) {
  2067. /* Determine the number of threads */
  2068. nthreads = count_processors();
  2069. if (nthreads <= 0) {
  2070. printf("ERROR: could not determine processor count\n");
  2071. nthreads = 1;
  2072. }
  2073. }
  2074.  
  2075. if (verbose > 1) {
  2076. printf("Using %d worker thread(s)\n", nthreads);
  2077. }
  2078.  
  2079. while (--nthreads) {
  2080. if (pthread_create(&thread, NULL, func, arg))
  2081. return 0;
  2082. }
  2083.  
  2084. func(arg);
  2085. return 1;
  2086. }
  2087.  
  2088.  
  2089. void
  2090. usage(const char *name)
  2091. {
  2092. printf(
  2093. "Vanitygen %s\n"
  2094. "Usage: %s [-vqrikNT] [-t <threads>] [-f <filename>|-] [<pattern>...]\n"
  2095. "Generates a bitcoin receiving address matching <pattern>, and outputs the\n"
  2096. "address and associated private key. The private key may be stored in a safe\n"
  2097. "location or imported into a bitcoin client to spend any balance received on\n"
  2098. "the address.\n"
  2099. "By default, <pattern> is interpreted as an exact prefix.\n"
  2100. "\n"
  2101. "Options:\n"
  2102. "-v Verbose output\n"
  2103. "-q Quiet output\n"
  2104. "-r Use regular expression match instead of prefix\n"
  2105. " (Feasibility of expression is not checked)\n"
  2106. "-i Case-insensitive prefix search\n"
  2107. "-k Keep pattern and continue search after finding a match\n"
  2108. "-N Generate namecoin address\n"
  2109. "-T Generate bitcoin testnet address\n"
  2110. "-t <threads> Set number of worker threads (Default: number of CPUs)\n"
  2111. "-f <file> File containing list of patterns, one per line\n"
  2112. " (Use \"-\" as the file name for stdin)\n"
  2113. "-o <file> Write pattern matches to <file>\n",
  2114. version, name);
  2115. }
  2116.  
  2117. int
  2118. main(int argc, char **argv)
  2119. {
  2120. int addrtype = 0;
  2121. int privtype = 128;
  2122. int regex = 0;
  2123. int caseinsensitive = 0;
  2124. int opt;
  2125. FILE *fp = NULL;
  2126. char **patterns;
  2127. int npatterns = 0;
  2128. int nthreads = 0;
  2129. void *(*thread_func)(void *) = NULL;
  2130. void *thread_arg = NULL;
  2131.  
  2132. while ((opt = getopt(argc, argv, "vqrikNTt:h?f:o:")) != -1) {
  2133. switch (opt) {
  2134. case 'v':
  2135. verbose = 2;
  2136. break;
  2137. case 'q':
  2138. verbose = 0;
  2139. break;
  2140. case 'r':
  2141. regex = 1;
  2142. break;
  2143. case 'i':
  2144. caseinsensitive = 1;
  2145. break;
  2146. case 'k':
  2147. remove_on_match = 0;
  2148. break;
  2149. case 'N':
  2150. addrtype = 52;
  2151. break;
  2152. case 'T':
  2153. addrtype = 111;
  2154. privtype = 239;
  2155. break;
  2156. case 't':
  2157. nthreads = atoi(optarg);
  2158. if (nthreads == 0) {
  2159. printf("Invalid thread count '%s'\n", optarg);
  2160. return 1;
  2161. }
  2162. break;
  2163. case 'f':
  2164. if (fp) {
  2165. printf("Multiple files specified\n");
  2166. return 1;
  2167. }
  2168. if (!strcmp(optarg, "-")) {
  2169. fp = stdin;
  2170. } else {
  2171. fp = fopen(optarg, "r+");
  2172. if (!fp) {
  2173. printf("Could not open %s: %s\n",
  2174. optarg, strerror(errno));
  2175. return 1;
  2176. }
  2177. }
  2178. break;
  2179. case 'o':
  2180. if (result_file) {
  2181. printf("Multiple output files specified\n");
  2182. return 1;
  2183. }
  2184. result_file = optarg;
  2185. break;
  2186. default:
  2187. usage(argv[0]);
  2188. return 1;
  2189. }
  2190. }
  2191.  
  2192. if (caseinsensitive && regex)
  2193. printf("WARNING: case insensitive mode incompatible with "
  2194. "regular expressions\n");
  2195.  
  2196. if (fp) {
  2197. if (!read_file(fp, &patterns, &npatterns)) {
  2198. printf("Failed to load pattern file\n");
  2199. return 1;
  2200. }
  2201. if (fp != stdin)
  2202. fclose(fp);
  2203.  
  2204. } else {
  2205. if (optind >= argc) {
  2206. usage(argv[0]);
  2207. return 1;
  2208. }
  2209. patterns = &argv[optind];
  2210. npatterns = argc - optind;
  2211. }
  2212.  
  2213. if (regex) {
  2214. vg_regex_context_t *vcrp;
  2215. vcrp = vg_regex_context_new(addrtype, privtype);
  2216. if (!vg_regex_context_add_patterns(vcrp, patterns, npatterns))
  2217. return 1;
  2218. if ((verbose > 0) && (vcrp->vcr_nres > 1))
  2219. printf("Regular expressions: %ld\n", vcrp->vcr_nres);
  2220.  
  2221. thread_func = (void *(*)(void*)) generate_address_regex;
  2222. thread_arg = vcrp;
  2223.  
  2224. } else {
  2225. vg_prefix_context_t *vcpp;
  2226. vcpp = vg_prefix_context_new(addrtype, privtype);
  2227. if (!vg_prefix_context_add_patterns(vcpp, patterns, npatterns,
  2228. caseinsensitive))
  2229. return 1;
  2230.  
  2231. thread_func = (void *(*)(void*)) generate_address_prefix;
  2232. thread_arg = vcpp;
  2233. }
  2234.  
  2235. if (!start_threads(thread_func, thread_arg, nthreads))
  2236. return 1;
  2237. return 0;
  2238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement