Permalink
Browse files

code to double check bloom matches against a full file

  • Loading branch information...
ryancdotorg committed Oct 19, 2015
1 parent c077332 commit db843196bc08b833a004d21d6af68d7cc27bf071
Showing with 129 additions and 6 deletions.
  1. +2 −2 Makefile
  2. +14 −4 blfchk.c
  3. +105 −0 hsearchf.c
  4. +8 −0 hsearchf.h
View
@@ -1,5 +1,5 @@
HEADERS = bloom.h crack.h hash160.h warpwallet.h
OBJECTS = brainflayer.o bloom.o hex2blf.o warpwallet.o hex.o mmapf.o
OBJECTS = brainflayer.o bloom.o hex2blf.o warpwallet.o hex.o mmapf.o hsearchf.o
BINARIES = brainflayer hex2blf blfchk ecmtabgen
LIBS = -lssl -lrt -lcrypto -lz -ldl -lgmp
CFLAGS = -O3 -flto -pedantic -std=gnu99 -Wall -Wextra -funsigned-char -Wno-pointer-sign -Wno-sign-compare
@@ -36,7 +36,7 @@ ec_pubkey_fast.o: ec_pubkey_fast.c secp256k1/include/secp256k1.h
%.o: %.c
$(COMPILE) -c $< -o $@
blfchk: blfchk.o hex.o bloom.o mmapf.o
blfchk: blfchk.o hex.o bloom.o mmapf.o hsearchf.o
$(COMPILE) -static $^ $(LIBS) -o $@
hex2blf: hex2blf.o hex.o bloom.o mmapf.o
View
@@ -20,19 +20,20 @@
#include "bloom.h"
#include "mmapf.h"
#include "hash160.h"
#include "hsearchf.h"
int main(int argc, char **argv) {
int ret;
hash160_t hash;
char *line = NULL;
size_t line_sz = 0;
unsigned char buf[128];
unsigned char *bloom, *bloomfile;
FILE *ifile = stdin, *ofile = stdout;
unsigned char *bloom, *bloomfile, *hashfile;
FILE *ifile = stdin, *ofile = stdout, *hfile = NULL;
mmapf_ctx bloom_mmapf;
if (argc != 2) {
fprintf(stderr, "Usage: %s BLOOM_FILTER_FILE\n", argv[0]);
if (argc < 2 || argc > 3) {
fprintf(stderr, "Usage: %s BLOOM_FILTER_FILE HASH_FILE\n", argv[0]);
return 1;
}
@@ -48,9 +49,18 @@ int main(int argc, char **argv) {
bloom = bloom_mmapf.mem;
if (argc == 3) {
hashfile = argv[2];
hfile = fopen(hashfile, "r");
}
while (getline(&line, &line_sz, ifile) > 0) {
unhex(line, strlen(line), hash.uc, sizeof(hash.uc));
if (bloom_chk_hash160(bloom, hash.ul)) {
if (hfile && !hsearchf(hfile, &hash)) {
//fprintf(ofile, "%s (false positive)\n", hex(hash.uc, sizeof(hash.uc), buf, sizeof(buf)));
continue;
}
fprintf(ofile, "%s\n", hex(hash.uc, sizeof(hash.uc), buf, sizeof(buf)));
}
}
View
@@ -0,0 +1,105 @@
/* Copyright (c) 2015 Ryan Castellucci, All Rights Reserved */
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <arpa/inet.h> /* for ntohl/htonl */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include "hex.h"
#include "hash160.h"
#include "hsearchf.h"
#define HASHLEN RIPEMD160_DIGEST_LENGTH
//#define USE_FUDGE
#define DO_MEMCMP() memcmp(candidate.uc, hash->uc, HASHLEN)
#define MAKE_GUESS(F) do { \
guess = entries * (ntohl(hash->ul[0]) / 4294967296.0 + (F)); \
} while (0)
#define READ_AT(X) do { \
if ((ret = fseek(f, (X) * HASHLEN, 0)) != 0) { return -1; } \
if ((ret = fread(candidate.uc, HASHLEN, 1, f)) != 1) { return -1; } \
} while (0)
// could mmapf and use bsearch, but meh
int hsearchf(FILE *f, hash160_t *hash) {
int ret;
//int i = 0;
size_t file_sz;
struct stat sb;
hash160_t candidate;
//unsigned char hexed[64];
int first, middle, last, entries;
if ((ret = fstat(fileno(f), &sb)) != 0) { return -1; }
file_sz = sb.st_size;
entries = file_sz / HASHLEN;
first = 0;
last = entries - 1;
#ifdef USE_FUDGE
// Most of the time, this is able to eliminate about nine reads because
// the values are fairly uniform. Worst case, we make two extra reads, so
// this is a win so long as it works at least a quarter of the time.
// Playing with the denominators on the MAKE_GUESS macros may give slight
// improvements.
int guess;
// make an initial guess at the location of the hash based on the target
MAKE_GUESS(1.0/+2048);
READ_AT(guess);
//fprintf(stderr, "entries %10u guess %9u %s\n", hash->ul[0], guess, hex(candidate.uc, HASHLEN, hexed, sizeof(hexed)));
ret = DO_MEMCMP();
if (ret == 0) {
return 1; // unlikely
} else if (ret < 0) {
first = guess;
MAKE_GUESS(1.0/+1024);
if (guess < entries) {
READ_AT(guess);
if (DO_MEMCMP() > 0) { last = guess; }
}
} else { // ret > 0
last = guess;
MAKE_GUESS(1.0/-32768);
if (guess > 0) {
READ_AT(guess);
if (DO_MEMCMP() < 0) { first = guess; }
}
}
#endif
middle = (first + last) / 2;
// binary search
while (first <= last) {
//fprintf(stderr, "target %s, checking entry %9u", hex(hash->uc, HASHLEN, hexed, sizeof(hexed)), middle);
READ_AT(middle);
ret = DO_MEMCMP();
//fprintf(stderr, " got %s %11d %2d\n", hex(candidate.uc, HASHLEN, hexed, sizeof(hexed)), ret, ++i);
if (ret == 0) {
return 1;
} else if (ret < 0) {
first = middle + 1;
} else { // ret > 0
last = middle - 1;
}
middle = (first + last) / 2;
}
return 0;
}
/* vim: set ts=2 sw=2 et ai si: */
View
@@ -0,0 +1,8 @@
/* Copyright (c) 2015 Ryan Castellucci, All Rights Reserved */
#ifndef __BRAINFLAYER_HSEARCHF_H_
#define __BRAINFLAYER_HSEARCHF_H_
int hsearchf(FILE *, hash160_t *);
/* vim: set ts=2 sw=2 et ai si: */
#endif /* __BRAINFLAYER_HSEARCHF_H_ */

0 comments on commit db84319

Please sign in to comment.