Skip to content

Commit

Permalink
Merge pull request #9 from kenorb-contrib/dev
Browse files Browse the repository at this point in the history
Dev to master
  • Loading branch information
kenorb authored Nov 3, 2024
2 parents 7f2a936 + 07cc8e3 commit 6287ef5
Show file tree
Hide file tree
Showing 36 changed files with 456 additions and 1,082 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ jobs:
Pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v1
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: ~/.cache/pre-commit
key: pre-commit|${{ hashFiles('.pre-commit-config.yaml') }}
- uses: actions/setup-python@v2
with:
python-version: 3.9.10
- uses: pre-commit/[email protected]
- uses: actions/setup-python@v3
- uses: pre-commit/[email protected]
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "secp256k1"]
path = secp256k1
url = https://github.com/ryancdotorg/secp256k1.git
path = secp256k1
url = https://github.com/bitcoin-core/secp256k1.git
[submodule "scrypt-jane"]
path = scrypt-jane
url = https://github.com/ryancdotorg/scrypt-jane.git
14 changes: 0 additions & 14 deletions 2.py

This file was deleted.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ algo/brainwalletio.o: algo/brainwalletio.c scrypt-jane/scrypt-jane.h
algo/brainv2.o: algo/brainv2.c scrypt-jane/scrypt-jane.h

ec_pubkey_fast.o: ec_pubkey_fast.c secp256k1/include/secp256k1.h
$(COMPILE) -Wno-unused-function -c $< -o $@
$(COMPILE) -Wno-unused-function -c $< -o $@ -I secp256k1

%.o: %.c
$(COMPILE) -c $< -o $@
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ installed (at least openssl and gmp are required along with libsecp256k1's
build dependencies). I really need to learn autotools. If you file an issue
about a build failure in libsecp256k1 I will close it.

Dependencies should install with
Dependencies should install with:

```
apt install autoconf autogen git libtool make
apt install build-essential libgmp-dev libgmp3-dev libssl-dev
apt install autoconf autogen automake git libtool make
apt install build-essential libgmp-dev libgmp3-dev libssl-dev zlib1g-dev
```

Supported build target is currently Ubuntu 20.04 on amd64/x86_64. Issues with
Expand Down
5 changes: 5 additions & 0 deletions blfchk.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ int main(int argc, char **argv) {
bit = BH17(hash.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH18(hash.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH19(hash.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH20(hash.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH21(hash.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH22(hash.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH23(hash.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH24(hash.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }

if (hfile && !hsearchf(hfile, &hash)) {
//fprintf(ofile, "%s (false positive)\n", hex(hash.uc, sizeof(hash.uc), buf, sizeof(buf)));
Expand Down
5 changes: 5 additions & 0 deletions bloom.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ void bloom_set_hash160(unsigned char *bloom, uint32_t *h) {
t = BH17(h); BLOOM_SET_BIT(t);
t = BH18(h); BLOOM_SET_BIT(t);
t = BH19(h); BLOOM_SET_BIT(t);
t = BH20(h); BLOOM_SET_BIT(t);
t = BH21(h); BLOOM_SET_BIT(t);
t = BH22(h); BLOOM_SET_BIT(t);
t = BH23(h); BLOOM_SET_BIT(t);
t = BH24(h); BLOOM_SET_BIT(t);
}

/*
Expand Down
18 changes: 17 additions & 1 deletion bloom.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stdint.h>

/* 2^32 bits */
#define BLOOM_SIZE (512*1024*1024)
#define BLOOM_SIZE (512*1024*1024*2)

#define BLOOM_SET_BIT(N) (bloom[(N)>>3] = bloom[(N)>>3] | (1<<((N)&7)))
#define BLOOM_GET_BIT(N) ( ( bloom[(N)>>3]>>((N)&7) )&1)
Expand Down Expand Up @@ -33,6 +33,17 @@
#define BH17(N) (N[2]<<24|N[3]>> 8)
#define BH18(N) (N[3]<<24|N[4]>> 8)
#define BH19(N) (N[4]<<24|N[0]>> 8)
#define BH20(N) (N[0]<<16|N[1]>>24)
#define BH21(N) (N[1]<<16|N[2]>>24)
#define BH22(N) (N[2]<<16|N[3]>>24)
#define BH23(N) (N[3]<<16|N[4]>>24)
#define BH24(N) (N[4]<<16|N[0]>>24)

#define BH20(N) (N[0]<<16|N[1]>>24)
#define BH21(N) (N[1]<<16|N[2]>>24)
#define BH22(N) (N[2]<<16|N[3]>>24)
#define BH23(N) (N[3]<<16|N[4]>>24)
#define BH24(N) (N[4]<<16|N[0]>>24)

void bloom_set_hash160(unsigned char *, uint32_t *);
#pragma GCC diagnostic ignored "-Wunused-function"
Expand All @@ -58,6 +69,11 @@ static unsigned int bloom_chk_hash160(unsigned char *bloom, uint32_t *h) {
t = BH17(h); if (BLOOM_GET_BIT(t) == 0) { return 0; }
t = BH18(h); if (BLOOM_GET_BIT(t) == 0) { return 0; }
t = BH19(h); if (BLOOM_GET_BIT(t) == 0) { return 0; }
t = BH20(h); if (BLOOM_GET_BIT(t) == 0) { return 0; }
t = BH21(h); if (BLOOM_GET_BIT(t) == 0) { return 0; }
t = BH22(h); if (BLOOM_GET_BIT(t) == 0) { return 0; }
t = BH23(h); if (BLOOM_GET_BIT(t) == 0) { return 0; }
t = BH24(h); if (BLOOM_GET_BIT(t) == 0) { return 0; }
return 1;
}
#pragma GCC diagnostic pop
Expand Down
146 changes: 95 additions & 51 deletions brainflayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <time.h>
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
Expand Down Expand Up @@ -33,7 +33,10 @@
#include "algo/sha3.h"

// raise this if you really want, but quickly diminishing returns
#define BATCH_MAX 4096
#define BATCH_MAX 4096*14

// Number of supported bloom files.
#define BOPT_MAX 10

static int brainflayer_is_init = 0;

Expand All @@ -44,8 +47,8 @@ typedef struct pubhashfn_s {

static unsigned char *mem;

static mmapf_ctx bloom_mmapf;
static unsigned char *bloom = NULL;
static mmapf_ctx bloom_mmapf[10];
static unsigned char *bloom, *blooms[10];

static unsigned char *unhexed = NULL;
static size_t unhexed_sz = 4096;
Expand Down Expand Up @@ -348,6 +351,7 @@ void usage(unsigned char *name) {
printf("Usage: %s [OPTION]...\n\n\
-a open output file in append mode\n\
-b FILE check for matches against bloom filter FILE\n\
multiple files be be specified\n\
-f FILE verify matches against sorted hash160s in FILE\n\
-i FILE read from FILE instead of stdin\n\
-o FILE write to FILE instead of stdout\n\
Expand All @@ -359,6 +363,9 @@ void usage(unsigned char *name) {
c - compressed address\n\
e - ethereum address\n\
x - most signifigant bits of x coordinate\n\
-C keep original control characters\n\
by default \\r and \\n are removed\n\
(option is ignored in hex mode)\n\
-t TYPE inputs are TYPE - supported types:\n\
sha256 (default) - classic brainwallet\n\
sha3 - sha3-256\n\
Expand All @@ -369,7 +376,7 @@ void usage(unsigned char *name) {
rush - rushwallet (requires -r) FAST\n\
keccak - keccak256 (ethercamp/old ethaddress)\n\
camp2 - keccak256 * 2031 (new ethercamp)\n\
shaxn - N rounds of SHA-256\n\
shaxn - N rounds of SHA-256\n\
-x treat input as hex encoded\n\
-s SALT use SALT for salted input types (default: none)\n\
-p PASSPHRASE use PASSPHRASE for salted input types, inputs\n\
Expand All @@ -381,7 +388,8 @@ void usage(unsigned char *name) {
-n K/N use only the Kth of every N input lines\n\
-B batch size for affine transformations\n\
must be a power of 2 (default/max: %d)\n\
-w WINDOW_SIZE window size for ecmult table (default: 16)\n\
-N number of SHA rounds (used only in '-t shaxn' mode)\n\
-w WINDOW_SIZE window size for ecmult table (default: 19)\n\
uses about 3 * 2^w KiB memory on startup, but\n\
only about 2^w KiB once the table is built\n\
-m FILE load ecmult table from FILE\n\
Expand Down Expand Up @@ -411,10 +419,11 @@ int main(int argc, char **argv) {

unsigned char modestr[64];

int spok = 0, aopt = 0, vopt = 0, wopt = 16, xopt = 0;
int nopt_mod = 0, nopt_rem = 0, Bopt = 0, Nopt = 2;
int spok = 0, aopt = 0, boptn = 0, vopt = 0, wopt = 19, xopt = 0;
int nopt_mod = 0, nopt_rem = 0, Bopt = 0, Copt = 0, Nopt = 2;
uint64_t kopt = 0;
unsigned char *bopt = NULL, *iopt = NULL, *oopt = NULL;
unsigned char *bopts[BOPT_MAX];
unsigned char *iopt = NULL, *oopt = NULL;
unsigned char *topt = NULL, *sopt = NULL, *popt = NULL;
unsigned char *mopt = NULL, *fopt = NULL, *ropt = NULL;
unsigned char *Iopt = NULL, *copt = NULL;
Expand All @@ -431,7 +440,7 @@ int main(int argc, char **argv) {
unsigned char batch_priv[BATCH_MAX][32];
unsigned char batch_upub[BATCH_MAX][65];

while ((c = getopt(argc, argv, "avxb:hi:k:f:m:n:o:p:s:r:c:t:w:I:B:N:")) != -1) {
while ((c = getopt(argc, argv, "avxb:hi:k:f:m:n:o:p:s:r:c:t:w:CI:B:N:")) != -1) {
switch (c) {
case 'a':
aopt = 1; // open output file in append mode
Expand Down Expand Up @@ -464,7 +473,13 @@ int main(int argc, char **argv) {
vopt = 1; // verbose
break;
case 'b':
bopt = optarg; // bloom filter file
if (boptn < BATCH_MAX) {
bopts[boptn] = optarg; // bloom filter file
boptn++;
}
else {
fprintf(stderr, "Number of bloom files reached maximum!\n");
}
break;
case 'f':
fopt = optarg; // full filter file
Expand Down Expand Up @@ -493,6 +508,9 @@ int main(int argc, char **argv) {
case 't':
topt = optarg; // type of input
break;
case 'C':
Copt = 1;
break;
case 'I':
Iopt = optarg; // start key for incremental
xopt = 1; // input is hex encoded
Expand All @@ -515,7 +533,7 @@ int main(int argc, char **argv) {
if (optind == 1 && argc == 2) {
// older versions of brainflayer had the bloom filter file as a
// single optional argument, this keeps compatibility with that
bopt = argv[1];
bopts[0] = argv[1];
} else {
fprintf(stderr, "Invalid arguments:\n");
while (optind < argc) {
Expand Down Expand Up @@ -684,17 +702,26 @@ int main(int argc, char **argv) {

snprintf(modestr, sizeof(modestr), xopt ? "(hex)%s" : "%s", topt);

if (bopt) {
if ((ret = mmapf(&bloom_mmapf, bopt, BLOOM_SIZE, MMAPF_RNDRD)) != MMAPF_OKAY) {
bail(1, "failed to open bloom filter '%s': %s\n", bopt, mmapf_strerror(ret));
} else if (bloom_mmapf.mem == NULL) {
bail(1, "got NULL pointer trying to set up bloom filter\n");
if (boptn > 0) {
if (vopt) {
printf("Loading... ");
}
bloom = bloom_mmapf.mem;
for (int i = 0; i < boptn; i++) {
if (vopt) {
fprintf(stdout, "%s... ", bopts[i]);
}
if ((ret = mmapf(&bloom_mmapf[i], bopts[i], BLOOM_SIZE, MMAPF_RNDRD)) != MMAPF_OKAY) {
bail(1, "failed to open bloom filter '%s': %s\n", bopts[i], mmapf_strerror(ret));
} else if (bloom_mmapf[i].mem == NULL) {
bail(1, "got NULL pointer trying to set up bloom filter\n");
}
blooms[i] = bloom_mmapf[i].mem;
}
printf("\n");
}

if (fopt) {
if (!bopt) {
if (boptn == 0) {
bail(1, "The '-f' option must be used with a bloom filter\n");
}
if ((ffile = fopen(fopt, "r")) == NULL) {
Expand Down Expand Up @@ -757,7 +784,7 @@ int main(int argc, char **argv) {
batch_stopped = Bopt;
} else {
for (i = 0; i < Bopt; ++i) {
if ((batch_line_read[i] = getline(&batch_line[i], &batch_line_sz[i], ifile)-1) > -1) {
if ((batch_line_read[i] = getline(&batch_line[i], &batch_line_sz[i], ifile)) > -1) {
if (skipping) {
++raw_lines;
if (kopt && raw_lines < kopt) { --i; continue; }
Expand All @@ -766,7 +793,14 @@ int main(int argc, char **argv) {
} else {
break;
}
batch_line[i][batch_line_read[i]] = 0;
if (!Copt || xopt) {
if (batch_line[i][batch_line_read[i]-1] == '\n') {
batch_line[i][--batch_line_read[i]] = 0;
}
if (batch_line[i][batch_line_read[i]-1] == '\r') {
batch_line[i][--batch_line_read[i]] = 0;
}
}
if (xopt) {
if (batch_line_read[i] / 2 > unhexed_sz) {
unhexed_sz = batch_line_read[i];
Expand All @@ -793,41 +827,51 @@ int main(int argc, char **argv) {

// loop over the public keys
for (i = 0; i < batch_stopped; ++i) {
if (bloom) { /* crack mode */
if (boptn > 0) { /* crack mode */
// loop over pubkey hash functions
for (j = 0; pubhashfn[j].fn != NULL; ++j) {
pubhashfn[j].fn(&hash160, batch_upub[i]);

unsigned int bit;
bit = BH00(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH01(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH02(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH03(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH04(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH05(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH06(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH07(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH08(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH09(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH10(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH11(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH12(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH13(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH14(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH15(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH16(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH17(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH18(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH19(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }

if (!fopt || hsearchf(ffile, &hash160)) {
if (tty) { fprintf(ofile, "\033[0K"); }
// reformat/populate the line if required
if (Iopt) {
hex(batch_priv[i], 32, batch_line[i], 65);
for (int k = 0; k < boptn; k++) {
unsigned int bit;
bloom = blooms[k];
bit = BH00(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH01(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH02(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH03(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH04(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH05(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH06(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH07(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH08(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH09(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH10(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH11(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH12(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH13(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH14(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH15(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH16(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH17(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH18(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH19(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH20(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH21(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH22(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH23(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }
bit = BH24(hash160.ul); if (BLOOM_GET_BIT(bit) == 0) { continue; }

if (!fopt || hsearchf(ffile, &hash160)) {
if (tty) { fprintf(ofile, "\033[0K"); }
// reformat/populate the line if required
if (Iopt) {
hex(batch_priv[i], 32, batch_line[i], 65);
}
fprintresult(ofile, &hash160, pubhashfn[j].id, modestr, batch_line[i]);
++olines;
k = boptn; // End a for loop.
break;
}
fprintresult(ofile, &hash160, pubhashfn[j].id, modestr, batch_line[i]);
++olines;
}
}
} else { /* generate mode */
Expand Down
Loading

0 comments on commit 6287ef5

Please sign in to comment.