See the CHANGELOG.md

This commit is contained in:
AlbertoBSD
2021-10-21 21:05:13 +02:00
parent e743446a7e
commit a69ec16ab7
18 changed files with 2224 additions and 683 deletions

2
.gitignore vendored
View File

@@ -3,6 +3,8 @@ hexcharstoraw
*.bin *.bin
keyhunt keyhunt
KEYFOUNDKEYFOUND.txt KEYFOUNDKEYFOUND.txt
*.tbl
*.blm
# Prerequisites # Prerequisites
*.d *.d

View File

@@ -1,3 +1,10 @@
# Version 0.2.211018 Chocolate ¡Beta!
- Solved some bugs: https://github.com/albertobsd/keyhunt/issues/122 https://github.com/albertobsd/keyhunt/issues/111
- Files are going to be updated automatillyca
-- from keyhunt_bsgs_3_*.blm to keyhunt_bsgs_4*.blm
-- from keyhunt_bsgs_1_*.blm to keyhunt_bsgs_5*.blm
-- the program will notify you when time to delete the old files
# Version 0.2.211012 Chocolate ¡Beta! # Version 0.2.211012 Chocolate ¡Beta!
- Fixed the slow bP table generation. - Fixed the slow bP table generation.
-- This fix make obsolete the files keyhunt_bsgs_0_*.blm -- This fix make obsolete the files keyhunt_bsgs_0_*.blm

View File

@@ -1,5 +1,6 @@
default: default:
gcc -O3 -c bloom/bloom.c -o bloom.o g++ -O3 -c oldbloom/bloom.cpp -o oldbloom.o
g++ -O3 -c bloom/bloom.cpp -o bloom.o
g++ -O3 -c sha256/sha256.c -o sha256.o g++ -O3 -c sha256/sha256.c -o sha256.o
gcc -O3 -c base58/base58.c -o base58.o gcc -O3 -c base58/base58.c -o base58.o
gcc -O3 -c rmd160/rmd160.c -o rmd160.o gcc -O3 -c rmd160/rmd160.c -o rmd160.o
@@ -13,9 +14,7 @@ default:
g++ -m64 -mssse3 -Wno-unused-result -Wno-write-strings -O2 -c secp256k1/IntMod.cpp -o IntMod.o g++ -m64 -mssse3 -Wno-unused-result -Wno-write-strings -O2 -c secp256k1/IntMod.cpp -o IntMod.o
g++ -m64 -mssse3 -Wno-unused-result -Wno-write-strings -O2 -c secp256k1/Random.cpp -o Random.o g++ -m64 -mssse3 -Wno-unused-result -Wno-write-strings -O2 -c secp256k1/Random.cpp -o Random.o
g++ -m64 -mssse3 -Wno-unused-result -Wno-write-strings -O2 -c secp256k1/IntGroup.cpp -o IntGroup.o g++ -m64 -mssse3 -Wno-unused-result -Wno-write-strings -O2 -c secp256k1/IntGroup.cpp -o IntGroup.o
g++ -o keyhunt keyhunt.c base58.o rmd160.o sha256.o bloom.o xxhash.o util.o Int.o Point.o SECP256K1.o IntMod.o Random.o IntGroup.o sha3.o keccak.o -lm -lpthread g++ -o keyhunt keyhunt.cpp base58.o rmd160.o sha256.o bloom.o oldbloom.o xxhash.o util.o Int.o Point.o SECP256K1.o IntMod.o Random.o IntGroup.o sha3.o keccak.o -lm -lpthread
rm -r *.o rm -r *.o
clean: clean:
rm keyhunt rm keyhunt
rm bPfile

View File

@@ -7,6 +7,11 @@ Work for btc in this moment, only legacy Addresses that start with '1'
Ethereum addresses is a work in develop Ethereum addresses is a work in develop
## For regulars users
Please read the CHANGELOG.md to see the new changes
# Download # Download
To clone the repository: To clone the repository:
@@ -772,7 +777,7 @@ OK at this point maybe you want to use ALL your RAM memory to solve the puzzle 1
I already tested it with some **18 GB ** used with `-k 1024` and I get **~46 Petakeys/s per thread.** I already tested it with some **18 GB ** used with `-k 1024` and I get **~46 Petakeys/s per thread.**
with 6 threads with **6** threads
`./keyhunt -m bsgs -f tests/120.txt -b 120 -R -k 1024 -q -t 6` `./keyhunt -m bsgs -f tests/120.txt -b 120 -R -k 1024 -q -t 6`
@@ -947,35 +952,35 @@ To get optimal performance the k values need to be base 2^x values, this is 1,2,
### Valid n and k values ### Valid n and k values
``` ```
+---------------------------+-------------+---------------------------+ +---------------------------+-------------+---------------------------+
| bits | n in hexadecimal | k max value | Amount of RAM with k = 1 | | bits | n in hexadecimal | k max value | Amount of RAM with k = 1 |
| +-------------------------+-------------+---------------------------+ +---------------------------+-------------+---------------------------+
| 20 | 0x100000 | 1 (default) | | | 20 | 0x100000 | 1 (default) | |
| 22 | 0x400000 | 2 | | | 22 | 0x400000 | 2 | |
| 24 | 0x1000000 | 4 | | | 24 | 0x1000000 | 4 | |
| 26 | 0x4000000 | 8 | | | 26 | 0x4000000 | 8 | |
| 28 | 0x10000000 | 16 | | | 28 | 0x10000000 | 16 | |
| 30 | 0x40000000 | 32 | | | 30 | 0x40000000 | 32 | |
| 32 | 0x100000000 | 64 | | | 32 | 0x100000000 | 64 | |
| 34 | 0x400000000 | 128 | | | 34 | 0x400000000 | 128 | |
| 36 | 0x1000000000 | 256 | | | 36 | 0x1000000000 | 256 | |
| 38 | 0x4000000000 | 512 | | | 38 | 0x4000000000 | 512 | |
| 40 | 0x10000000000 | 1024 | | | 40 | 0x10000000000 | 1024 | |
| 42 | 0x40000000000 | 2048 | | | 42 | 0x40000000000 | 2048 | |
| 44 | 0x100000000000 | 4096 | ~17 MB | | 44 | 0x100000000000 | 4096 | ~17 MB |
| 46 | 0x400000000000 | 8192 | ~34 MB | | 46 | 0x400000000000 | 8192 | ~34 MB |
| 48 | 0x1000000000000 | 16384 | | | 48 | 0x1000000000000 | 16384 | |
| 50 | 0x4000000000000 | 32768 | | | 50 | 0x4000000000000 | 32768 | |
| 52 | 0x10000000000000 | 65536 | | | 52 | 0x10000000000000 | 65536 | |
| 54 | 0x40000000000000 | 131072 | | | 54 | 0x40000000000000 | 131072 | |
| 56 | 0x100000000000000 | 262144 | | | 56 | 0x100000000000000 | 262144 | |
| 58 | 0x400000000000000 | 524288 | | | 58 | 0x400000000000000 | 524288 | |
| 60 | 0x1000000000000000 | 1048576 | ~4.5 GB | | 60 | 0x1000000000000000 | 1048576 | ~4.5 GB |
| 62 | 0x4000000000000000 | 2097152 | ~9 GB | | 62 | 0x4000000000000000 | 2097152 | ~9 GB |
+---------------------------+-------------+---------------------------+ +---------------------------+-------------+---------------------------+
``` ```
*if you exceed the Max value of K the program can have a unknow behavior, the program can have a suboptimal performance, or in the wrong cases you can missing some hits and have an incorrect SPEED.* **if you exceed the Max value of K the program can have a unknow behavior, the program can have a suboptimal performance, or in the wrong cases you can missing some hits and have an incorrect SPEED.**
Of course you can use a bigger number N like 2^64 or 2^70 if you have enough memory for it. Of course you can use a bigger number N like 2^64 or 2^70 if you have enough memory for it.
@@ -1035,7 +1040,7 @@ Please note that number of threads was setting to 6 but only 4 threads were used
The next command also solve the Puzzle 63 with more threads The next command also solve the Puzzle 63 with more threads
``` ```
time ./keyhunt -m bsgs -t 6 -f tests/63.pub -n 0x400000000000000 -M -s 0 -S -k 16 -b 63 -B both time ./keyhunt -m bsgs -t 6 -f tests/63.pub -n 0x400000000000000 -M -s 0 -S -k 8 -b 63
``` ```
``` ```
@@ -1057,16 +1062,21 @@ Please check the video that i made to answer that https://youtu.be/MVby8mYNxbI
- Is available for Windows? - Is available for Windows?
R: It can be compiled with mingw, also it can be executed in the Ubuntu shell for windows 10 R: It can be compiled with mingw, also it can be executed in the Ubuntu shell for windows 10
Updated: Updated:
Yes, thanks to @WanderingPhilosopher Yes thanks to @kanhavishva
Available in: https://github.com/kanhavishva/keyhunt
Also, thanks to @WanderingPhilosopher
Available in: https://github.com/WanderingPhilosopher/keyhunt Available in: https://github.com/WanderingPhilosopher/keyhunt
Also thanks to @XopMC Also thanks to @XopMC
Available in: https://github.com/XopMC/keyhunt-win Available in: https://github.com/XopMC/keyhunt-win
- Why i need the bPfile.bin ? - Why i need the bPfile.bin ?
R: Usually you don't need it, but if you are going to run and stop the program several times is faster load the data from a file. R: Usually you don't need it, but if you are going to run and stop the program several times is faster load the data from a file.
NOTE: *bPfile.bin will discontinued in some future version, the current versios have the `-S` to SAVE the FILES or READ from IT if they already exist* **NOTE: bPfile.bin will discontinued in some future version, the current versios have the `-S` to SAVE the FILES or READ from IT if they already exist**
- The bPfile gives me extra speed? - The bPfile gives me extra speed?
R: No, is just to help to speed up a little the load process and no more, but the final speed of keys per second is going to be the same without this file. R: No, is just to help to speed up a little the load process and no more, but the final speed of keys per second is going to be the same without this file.
@@ -1075,13 +1085,14 @@ R: No, is just to help to speed up a little the load process and no more, but th
## Dependencies ## Dependencies
- pthread - pthread
Tested under Debian, Termux, Ubuntu Shell for windows 10 Tested under Debian, Ubuntu Shell for windows 10
## Thanks ## Thanks
This program was possible thanks to This program was possible thanks to
- IceLand - IceLand
- kanhavishva - kanhavishva
- JLP for part of his code
- All the group of CryptoHunters that made this program possible - All the group of CryptoHunters that made this program possible
- All the users that tested it, report bugs, requested improvements and shared his knowledge. - All the users that tested it, report bugs, requested improvements and shared his knowledge.

View File

@@ -1,393 +1,299 @@
/* /*
* Copyright (c) 2012-2019, Jyri J. Virkki * Copyright (c) 2012-2019, Jyri J. Virkki
* All rights reserved. * All rights reserved.
* *
* This file is under BSD license. See LICENSE file. * This file is under BSD license. See LICENSE file.
*/ */
/* /*
* Refer to bloom.h for documentation on the public interfaces. * Refer to bloom.h for documentation on the public interfaces.
*/ */
#include <assert.h> #include <assert.h>
#include <fcntl.h> #include <fcntl.h>
#include <math.h> #include <math.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <inttypes.h> #include <inttypes.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <pthread.h> #include <pthread.h>
#include "bloom.h" #include "bloom.h"
#include "../xxhash/xxhash.h" #include "../xxhash/xxhash.h"
#define MAKESTRING(n) STRING(n) #define MAKESTRING(n) STRING(n)
#define STRING(n) #n #define STRING(n) #n
#define BLOOM_MAGIC "libbloom2" #define BLOOM_MAGIC "libbloom2"
#define BLOOM_VERSION_MAJOR 2 #define BLOOM_VERSION_MAJOR 2
#define BLOOM_VERSION_MINOR 200 #define BLOOM_VERSION_MINOR 201
inline static int test_bit_set_bit(struct bloom * bloom, uint64_t bit, int set_bit) inline static int test_bit_set_bit(uint8_t *bf, uint64_t bit, int set_bit)
{ {
uint64_t byte = bit >> 3; uint64_t byte = bit >> 3;
uint8_t c; uint8_t c = bf[byte]; // expensive memory access
uint8_t mask = 1 << (bit % 8); uint8_t mask = 1 << (bit % 8);
pthread_mutex_lock(&bloom->mutex); if (c & mask) {
c = bloom->bf[byte]; // expensive memory access return 1;
pthread_mutex_unlock(&bloom->mutex); } else {
if (c & mask) { if (set_bit) {
return 1; bf[byte] = c | mask;
} else { }
if (set_bit) { return 0;
pthread_mutex_lock(&bloom->mutex); }
bloom->bf[byte] = c | mask; }
pthread_mutex_unlock(&bloom->mutex);
} inline static int test_bit(uint8_t *bf, uint64_t bit)
return 0; {
} uint64_t byte = bit >> 3;
} uint8_t c = bf[byte]; // expensive memory access
uint8_t mask = 1 << (bit % 8);
if (c & mask) {
static int bloom_check_add(struct bloom * bloom, const void * buffer, int len, int add) return 1;
{ } else {
if (bloom->ready == 0) { return 0;
printf("bloom at %p not initialized!\n", (void *)bloom); }
return -1; }
}
uint8_t hits = 0; static int bloom_check_add(struct bloom * bloom, const void * buffer, int len, int add)
uint64_t a = XXH64(buffer, len, 0x59f2815b16f81798); {
uint64_t b = XXH64(buffer, len, a); if (bloom->ready == 0) {
uint64_t x; printf("bloom at %p not initialized!\n", (void *)bloom);
uint8_t i; return -1;
int r; }
for (i = 0; i < bloom->hashes; i++) { uint8_t hits = 0;
x = (a + b*i) % bloom->bits; uint64_t a = XXH64(buffer, len, 0x59f2815b16f81798);
r = test_bit_set_bit(bloom, x, add); uint64_t b = XXH64(buffer, len, a);
uint64_t x;
if (r) { uint8_t i;
hits++; for (i = 0; i < bloom->hashes; i++) {
} else if (!add) { x = (a + b*i) % bloom->bits;
// Don't care about the presence of all the bits. Just our own. if (test_bit_set_bit(bloom->bf, x, add)) {
return 0; hits++;
} } else if (!add) {
} // Don't care about the presence of all the bits. Just our own.
if (hits == bloom->hashes) { return 0;
return 1; // 1 == element already in (or collision) }
} }
return 0; if (hits == bloom->hashes) {
} return 1; // 1 == element already in (or collision)
}
return 0;
// DEPRECATED - Please migrate to bloom_init2. }
int bloom_init(struct bloom * bloom, uint64_t entries, long double error)
{ // DEPRECATED - Please migrate to bloom_init2.
return bloom_init2(bloom, entries, error); int bloom_init(struct bloom * bloom, uint64_t entries, long double error)
} {
return bloom_init2(bloom, entries, error);
}
int bloom_init2(struct bloom * bloom, uint64_t entries, long double error)
{ int bloom_init2(struct bloom * bloom, uint64_t entries, long double error)
memset(bloom, 0, sizeof(struct bloom)); {
if (entries < 1000 || error <= 0 || error >= 1) { memset(bloom, 0, sizeof(struct bloom));
return 1; if (entries < 1000 || error <= 0 || error >= 1) {
} return 1;
}
bloom->entries = entries; bloom->entries = entries;
bloom->error = error; bloom->error = error;
long double num = -log(bloom->error); long double num = -log(bloom->error);
long double denom = 0.480453013918201; // ln(2)^2 long double denom = 0.480453013918201; // ln(2)^2
bloom->bpe = (num / denom); bloom->bpe = (num / denom);
long double dentries = (long double)entries; long double dentries = (long double)entries;
long double allbits = dentries * bloom->bpe; long double allbits = dentries * bloom->bpe;
bloom->bits = (uint64_t)allbits; bloom->bits = (uint64_t)allbits;
bloom->bytes = (uint64_t) bloom->bits / 8; bloom->bytes = (uint64_t) bloom->bits / 8;
if (bloom->bits % 8) { if (bloom->bits % 8) {
bloom->bytes +=1; bloom->bytes +=1;
} }
bloom->hashes = (uint8_t)ceil(0.693147180559945 * bloom->bpe); // ln(2) bloom->hashes = (uint8_t)ceil(0.693147180559945 * bloom->bpe); // ln(2)
bloom->bf = (uint8_t *)calloc(bloom->bytes, sizeof(uint8_t)); bloom->bf = (uint8_t *)calloc(bloom->bytes, sizeof(uint8_t));
if (bloom->bf == NULL) { // LCOV_EXCL_START if (bloom->bf == NULL) { // LCOV_EXCL_START
return 1; return 1;
} // LCOV_EXCL_STOP } // LCOV_EXCL_STOP
bloom->ready = 1; bloom->ready = 1;
bloom->major = BLOOM_VERSION_MAJOR; bloom->major = BLOOM_VERSION_MAJOR;
bloom->minor = BLOOM_VERSION_MINOR; bloom->minor = BLOOM_VERSION_MINOR;
return 0; return 0;
} }
int bloom_dummy(struct bloom * bloom, uint64_t entries, long double error) int bloom_check(struct bloom * bloom, const void * buffer, int len)
{ {
memset(bloom, 0, sizeof(struct bloom)); if (bloom->ready == 0) {
if (entries < 1000 || error <= 0 || error >= 1) { printf("bloom at %p not initialized!\n", (void *)bloom);
return 1; return -1;
} }
uint8_t hits = 0;
bloom->entries = entries; uint64_t a = XXH64(buffer, len, 0x59f2815b16f81798);
bloom->error = error; uint64_t b = XXH64(buffer, len, a);
uint64_t x;
long double num = -log(bloom->error); uint8_t i;
long double denom = 0.480453013918201; // ln(2)^2 int r;
bloom->bpe = (num / denom); for (i = 0; i < bloom->hashes; i++) {
x = (a + b*i) % bloom->bits;
long double dentries = (long double)entries; if (test_bit(bloom->bf, x)) {
long double allbits = dentries * bloom->bpe; hits++;
bloom->bits = (uint64_t)allbits; } else {
return 0;
bloom->bytes = (uint64_t) bloom->bits / 8; }
if (bloom->bits % 8) { }
bloom->bytes +=1; if (hits == bloom->hashes) {
} return 1; // 1 == element already in (or collision)
}
bloom->hashes = (uint8_t)ceil(0.693147180559945 * bloom->bpe); // ln(2) return 0;
/* }
bloom->bf = (uint8_t *)calloc(bloom->bytes, sizeof(uint8_t));
if (bloom->bf == NULL) { // LCOV_EXCL_START
return 1; int bloom_add(struct bloom * bloom, const void * buffer, int len)
} // LCOV_EXCL_STOP {
return bloom_check_add(bloom, buffer, len, 1);
bloom->ready = 1; }
bloom->major = BLOOM_VERSION_MAJOR;
bloom->minor = BLOOM_VERSION_MINOR; void bloom_print(struct bloom * bloom)
*/ {
return 0; printf("bloom at %p\n", (void *)bloom);
} if (!bloom->ready) { printf(" *** NOT READY ***\n"); }
printf(" ->version = %d.%d\n", bloom->major, bloom->minor);
int bloom_check(struct bloom * bloom, const void * buffer, int len) printf(" ->entries = %" PRIu64 "\n", bloom->entries);
{ printf(" ->error = %Lf\n", bloom->error);
return bloom_check_add(bloom, buffer, len, 0); printf(" ->bits = %" PRIu64 "\n", bloom->bits);
} printf(" ->bits per elem = %f\n", bloom->bpe);
printf(" ->bytes = %" PRIu64 "\n", bloom->bytes);
unsigned int KB = bloom->bytes / 1024;
int bloom_add(struct bloom * bloom, const void * buffer, int len) unsigned int MB = KB / 1024;
{ printf(" (%u KB, %u MB)\n", KB, MB);
int r; printf(" ->hash functions = %d\n", bloom->hashes);
r =bloom_check_add(bloom, buffer, len, 1); }
return r;
} void bloom_free(struct bloom * bloom)
{
if (bloom->ready) {
void bloom_print(struct bloom * bloom) free(bloom->bf);
{ }
printf("bloom at %p\n", (void *)bloom); bloom->ready = 0;
if (!bloom->ready) { printf(" *** NOT READY ***\n"); } }
printf(" ->version = %d.%d\n", bloom->major, bloom->minor);
printf(" ->entries = %"PRIu64"\n", bloom->entries); int bloom_reset(struct bloom * bloom)
printf(" ->error = %Lf\n", bloom->error); {
printf(" ->bits = %"PRIu64"\n", bloom->bits); if (!bloom->ready) return 1;
printf(" ->bits per elem = %f\n", bloom->bpe); memset(bloom->bf, 0, bloom->bytes);
printf(" ->bytes = %"PRIu64"\n", bloom->bytes); return 0;
unsigned int KB = bloom->bytes / 1024; }
unsigned int MB = KB / 1024; /*
printf(" (%u KB, %u MB)\n", KB, MB); int bloom_save(struct bloom * bloom, char * filename)
printf(" ->hash functions = %d\n", bloom->hashes); {
} if (filename == NULL || filename[0] == 0) {
return 1;
}
void bloom_free(struct bloom * bloom)
{ int fd = open(filename, O_WRONLY | O_CREAT, 0644);
if (bloom->ready) { if (fd < 0) {
free(bloom->bf); return 1;
} }
bloom->ready = 0;
} ssize_t out = write(fd, BLOOM_MAGIC, strlen(BLOOM_MAGIC));
if (out != strlen(BLOOM_MAGIC)) { goto save_error; } // LCOV_EXCL_LINE
int bloom_reset(struct bloom * bloom) uint16_t size = sizeof(struct bloom);
{ out = write(fd, &size, sizeof(uint16_t));
if (!bloom->ready) return 1; if (out != sizeof(uint16_t)) { goto save_error; } // LCOV_EXCL_LINE
memset(bloom->bf, 0, bloom->bytes);
return 0; out = write(fd, bloom, sizeof(struct bloom));
} if (out != sizeof(struct bloom)) { goto save_error; } // LCOV_EXCL_LINE
int bloom_loadcustom(struct bloom * bloom, char * filename) { out = write(fd, bloom->bf, bloom->bytes);
if (filename == NULL || filename[0] == 0) { if (out != bloom->bytes) { goto save_error; } // LCOV_EXCL_LINE
return 1;
} close(fd);
FILE *fd_str,*fd_dat; return 0;
char filename_str[70],filename_dat[70]; // LCOV_EXCL_START
memset(filename_str,0,70); save_error:
memset(filename_dat,0,70); close(fd);
snprintf(filename_str,68,"%s.blm",filename); return 1;
snprintf(filename_dat,68,"%s.dat",filename); // LCOV_EXCL_STOP
fd_str = fopen(filename_str,"rb"); }
fd_dat = fopen(filename_dat,"rb");
if (fd_str == NULL || fd_dat == NULL) {
return 1; int bloom_load(struct bloom * bloom, char * filename)
} {
if(fread(bloom,1,sizeof(struct bloom),fd_str) != sizeof(struct bloom) ) { int rv = 0;
fclose(fd_str);
fclose(fd_dat); if (filename == NULL || filename[0] == 0) { return 1; }
return 1; if (bloom == NULL) { return 2; }
}
bloom->bf = malloc(bloom->bytes); memset(bloom, 0, sizeof(struct bloom));
if(bloom->bf == NULL) {
memset(bloom, 0, sizeof(struct bloom)); int fd = open(filename, O_RDONLY);
return 1; if (fd < 0) { return 3; }
}
if(fread(bloom->bf,1, bloom->bytes,fd_dat) !=bloom->bytes) { char line[30];
free(bloom->bf); memset(line, 0, 30);
memset(bloom, 0, sizeof(struct bloom)); ssize_t in = read(fd, line, strlen(BLOOM_MAGIC));
fclose(fd_str);
fclose(fd_dat); if (in != strlen(BLOOM_MAGIC)) {
return 1; rv = 4;
} goto load_error;
fclose(fd_str); }
fclose(fd_dat);
return 0; if (strncmp(line, BLOOM_MAGIC, strlen(BLOOM_MAGIC))) {
} rv = 5;
goto load_error;
int bloom_savecustom(struct bloom * bloom, char * filename) { }
if (filename == NULL || filename[0] == 0) {
return 1; uint16_t size;
} in = read(fd, &size, sizeof(uint16_t));
FILE *fd_str,*fd_dat; if (in != sizeof(uint16_t)) {
char filename_str[70],filename_dat[70]; rv = 6;
memset(filename_str,0,70); goto load_error;
memset(filename_dat,0,70); }
snprintf(filename_str,68,"%s.blm",filename);
snprintf(filename_dat,68,"%s.dat",filename); if (size != sizeof(struct bloom)) {
fd_str = fopen(filename_str,"wb"); rv = 7;
fd_dat = fopen(filename_dat,"wb"); goto load_error;
if (fd_str == NULL || fd_dat == NULL) { }
return 1;
} in = read(fd, bloom, sizeof(struct bloom));
if (in != sizeof(struct bloom)) {
if(fwrite(bloom,1,sizeof(struct bloom),fd_str) != sizeof(struct bloom) ) { rv = 8;
fclose(fd_str); goto load_error;
fclose(fd_dat); }
fprintf(stderr,"fwrite bloom\n");
exit(0); bloom->bf = NULL;
return 1; if (bloom->major != BLOOM_VERSION_MAJOR) {
} rv = 9;
if(fwrite(bloom->bf,1, bloom->bytes,fd_dat) !=bloom->bytes) { goto load_error;
fclose(fd_str); }
fclose(fd_dat);
fprintf(stderr,"fwrite bloom->bf\n"); bloom->bf = (unsigned char *)malloc(bloom->bytes);
exit(0); if (bloom->bf == NULL) { rv = 10; goto load_error; } // LCOV_EXCL_LINE
return 1;
} in = read(fd, bloom->bf, bloom->bytes);
fclose(fd_str); if (in != bloom->bytes) {
fclose(fd_dat); rv = 11;
return 0; free(bloom->bf);
} bloom->bf = NULL;
goto load_error;
}
int bloom_save(struct bloom * bloom, char * filename)
{ close(fd);
if (filename == NULL || filename[0] == 0) { return rv;
return 1;
} load_error:
close(fd);
int fd = open(filename, O_WRONLY | O_CREAT, 0644); bloom->ready = 0;
if (fd < 0) { return rv;
return 1; }
} */
ssize_t out = write(fd, BLOOM_MAGIC, strlen(BLOOM_MAGIC)); const char * bloom_version()
if (out != strlen(BLOOM_MAGIC)) { goto save_error; } // LCOV_EXCL_LINE {
return MAKESTRING(BLOOM_VERSION);
uint16_t size = sizeof(struct bloom); }
out = write(fd, &size, sizeof(uint16_t));
if (out != sizeof(uint16_t)) { goto save_error; } // LCOV_EXCL_LINE
out = write(fd, bloom, sizeof(struct bloom));
if (out != sizeof(struct bloom)) { goto save_error; } // LCOV_EXCL_LINE
out = write(fd, bloom->bf, bloom->bytes);
if (out != bloom->bytes) { goto save_error; } // LCOV_EXCL_LINE
close(fd);
return 0;
// LCOV_EXCL_START
save_error:
close(fd);
return 1;
// LCOV_EXCL_STOP
}
int bloom_load(struct bloom * bloom, char * filename)
{
int rv = 0;
if (filename == NULL || filename[0] == 0) { return 1; }
if (bloom == NULL) { return 2; }
memset(bloom, 0, sizeof(struct bloom));
int fd = open(filename, O_RDONLY);
if (fd < 0) { return 3; }
char line[30];
memset(line, 0, 30);
ssize_t in = read(fd, line, strlen(BLOOM_MAGIC));
if (in != strlen(BLOOM_MAGIC)) {
rv = 4;
goto load_error;
}
if (strncmp(line, BLOOM_MAGIC, strlen(BLOOM_MAGIC))) {
rv = 5;
goto load_error;
}
uint16_t size;
in = read(fd, &size, sizeof(uint16_t));
if (in != sizeof(uint16_t)) {
rv = 6;
goto load_error;
}
if (size != sizeof(struct bloom)) {
rv = 7;
goto load_error;
}
in = read(fd, bloom, sizeof(struct bloom));
if (in != sizeof(struct bloom)) {
rv = 8;
goto load_error;
}
bloom->bf = NULL;
if (bloom->major != BLOOM_VERSION_MAJOR) {
rv = 9;
goto load_error;
}
bloom->bf = (unsigned char *)malloc(bloom->bytes);
if (bloom->bf == NULL) { rv = 10; goto load_error; } // LCOV_EXCL_LINE
in = read(fd, bloom->bf, bloom->bytes);
if (in != bloom->bytes) {
rv = 11;
free(bloom->bf);
bloom->bf = NULL;
goto load_error;
}
close(fd);
return rv;
load_error:
close(fd);
bloom->ready = 0;
return rv;
}
const char * bloom_version()
{
return MAKESTRING(BLOOM_VERSION);
}

View File

@@ -8,6 +8,10 @@
#ifndef _BLOOM_H #ifndef _BLOOM_H
#define _BLOOM_H #define _BLOOM_H
#ifdef _WIN64
#include <windows.h>
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@@ -37,18 +41,16 @@ struct bloom
uint8_t major; uint8_t major;
uint8_t minor; uint8_t minor;
double bpe; double bpe;
uint8_t checksum[32];
uint8_t checksum_backup[32];
uint8_t *bf; uint8_t *bf;
pthread_mutex_t mutex;
}; };
/* /*
Customs Customs
*/ */
int bloom_dummy(struct bloom * bloom, uint64_t entries, long double error); /*
int bloom_loadcustom(struct bloom * bloom, char * filename); int bloom_loadcustom(struct bloom * bloom, char * filename);
int bloom_savecustom(struct bloom * bloom, char * filename); int bloom_savecustom(struct bloom * bloom, char * filename);
*/
/** *************************************************************************** /** ***************************************************************************
@@ -184,7 +186,7 @@ int bloom_reset(struct bloom * bloom);
* 1 - on failure * 1 - on failure
* *
*/ */
int bloom_save(struct bloom * bloom, char * filename); //int bloom_save(struct bloom * bloom, char * filename);
/** *************************************************************************** /** ***************************************************************************
@@ -202,7 +204,7 @@ int bloom_save(struct bloom * bloom, char * filename);
* > 0 - on failure * > 0 - on failure
* *
*/ */
int bloom_load(struct bloom * bloom, char * filename); //int bloom_load(struct bloom * bloom, char * filename);
/** *************************************************************************** /** ***************************************************************************
@@ -217,4 +219,4 @@ const char * bloom_version();
} }
#endif #endif
#endif #endif

File diff suppressed because it is too large Load Diff

26
oldbloom/LICENSE Normal file
View File

@@ -0,0 +1,26 @@
Copyright (c) 2012,2015,2016,2017 Jyri J. Virkki
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

304
oldbloom/bloom.cpp Normal file
View File

@@ -0,0 +1,304 @@
/*
* Copyright (c) 2012-2019, Jyri J. Virkki
* All rights reserved.
*
* This file is under BSD license. See LICENSE file.
*/
/*
* Refer to bloom.h for documentation on the public interfaces.
*/
#include <assert.h>
#include <fcntl.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <inttypes.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include "oldbloom.h"
#include "../xxhash/xxhash.h"
#define MAKESTRING(n) STRING(n)
#define STRING(n) #n
#define BLOOM_MAGIC "libbloom2"
#define BLOOM_VERSION_MAJOR 2
#define BLOOM_VERSION_MINOR 200
inline static int oldtest_bit_set_bit(uint8_t *bf, uint64_t bit, int set_bit)
{
uint64_t byte = bit >> 3;
uint8_t c = bf[byte]; // expensive memory access
uint8_t mask = 1 << (bit % 8);
if (c & mask) {
return 1;
} else {
if (set_bit) {
bf[byte] = c | mask;
}
return 0;
}
}
inline static int oldtest_bit(uint8_t *bf, uint64_t bit)
{
uint64_t byte = bit >> 3;
uint8_t c = bf[byte]; // expensive memory access
uint8_t mask = 1 << (bit % 8);
if (c & mask) {
return 1;
} else {
return 0;
}
}
static int oldbloom_check_add(struct oldbloom * bloom, const void * buffer, int len, int add)
{
if (bloom->ready == 0) {
printf("bloom at %p not initialized!\n", (void *)bloom);
return -1;
}
uint8_t hits = 0;
uint64_t a = XXH64(buffer, len, 0x59f2815b16f81798);
uint64_t b = XXH64(buffer, len, a);
uint64_t x;
uint8_t i;
int r;
for (i = 0; i < bloom->hashes; i++) {
x = (a + b*i) % bloom->bits;
pthread_mutex_lock(&bloom->mutex);
r = oldtest_bit_set_bit(bloom->bf, x, add);
pthread_mutex_unlock(&bloom->mutex);
if (r) {
hits++;
} else if (!add) {
// Don't care about the presence of all the bits. Just our own.
return 0;
}
}
if (hits == bloom->hashes) {
return 1; // 1 == element already in (or collision)
}
return 0;
}
// DEPRECATED - Please migrate to bloom_init2.
int oldbloom_init(struct oldbloom * bloom, uint64_t entries, long double error)
{
return oldbloom_init2(bloom, entries, error);
}
int oldbloom_init2(struct oldbloom * bloom, uint64_t entries, long double error)
{
memset(bloom, 0, sizeof(struct oldbloom));
if (entries < 1000 || error <= 0 || error >= 1) {
return 1;
}
bloom->entries = entries;
bloom->error = error;
long double num = -log(bloom->error);
long double denom = 0.480453013918201; // ln(2)^2
bloom->bpe = (num / denom);
long double dentries = (long double)entries;
long double allbits = dentries * bloom->bpe;
bloom->bits = (uint64_t)allbits;
bloom->bytes = (uint64_t) bloom->bits / 8;
if (bloom->bits % 8) {
bloom->bytes +=1;
}
bloom->hashes = (uint8_t)ceil(0.693147180559945 * bloom->bpe); // ln(2)
bloom->bf = (uint8_t *)calloc(bloom->bytes, sizeof(uint8_t));
if (bloom->bf == NULL) { // LCOV_EXCL_START
return 1;
} // LCOV_EXCL_STOP
bloom->ready = 1;
bloom->major = BLOOM_VERSION_MAJOR;
bloom->minor = BLOOM_VERSION_MINOR;
return 0;
}
int oldbloom_check(struct oldbloom * bloom, const void * buffer, int len)
{
if (bloom->ready == 0) {
printf("bloom at %p not initialized!\n", (void *)bloom);
return -1;
}
uint8_t hits = 0;
uint64_t a = XXH64(buffer, len, 0x59f2815b16f81798);
uint64_t b = XXH64(buffer, len, a);
uint64_t x;
uint8_t i;
int r;
for (i = 0; i < bloom->hashes; i++) {
x = (a + b*i) % bloom->bits;
if (oldtest_bit(bloom->bf, x)) {
hits++;
} else {
return 0;
}
}
if (hits == bloom->hashes) {
return 1; // 1 == element already in (or collision)
}
return 0;
}
int oldbloom_add(struct oldbloom * bloom, const void * buffer, int len)
{
return oldbloom_check_add(bloom, buffer, len, 1);
}
void oldbloom_print(struct oldbloom * bloom)
{
printf("bloom at %p\n", (void *)bloom);
if (!bloom->ready) { printf(" *** NOT READY ***\n"); }
printf(" ->version = %d.%d\n", bloom->major, bloom->minor);
printf(" ->entries = %" PRIu64 "\n", bloom->entries);
printf(" ->error = %Lf\n", bloom->error);
printf(" ->bits = %" PRIu64 "\n", bloom->bits);
printf(" ->bits per elem = %f\n", bloom->bpe);
printf(" ->bytes = %" PRIu64 "\n", bloom->bytes);
unsigned int KB = bloom->bytes / 1024;
unsigned int MB = KB / 1024;
printf(" (%u KB, %u MB)\n", KB, MB);
printf(" ->hash functions = %d\n", bloom->hashes);
}
void oldbloom_free(struct oldbloom * bloom)
{
if (bloom->ready) {
free(bloom->bf);
}
bloom->ready = 0;
}
int oldbloom_reset(struct oldbloom * bloom)
{
if (!bloom->ready) return 1;
memset(bloom->bf, 0, bloom->bytes);
return 0;
}
/*
int oldbloom_save(struct oldbloom * bloom, char * filename)
{
if (filename == NULL || filename[0] == 0) {
return 1;
}
int fd = open(filename, O_WRONLY | O_CREAT, 0644);
if (fd < 0) {
return 1;
}
ssize_t out = write(fd, BLOOM_MAGIC, strlen(BLOOM_MAGIC));
if (out != strlen(BLOOM_MAGIC)) { goto save_error; } // LCOV_EXCL_LINE
uint16_t size = sizeof(struct oldbloom);
out = write(fd, &size, sizeof(uint16_t));
if (out != sizeof(uint16_t)) { goto save_error; } // LCOV_EXCL_LINE
out = write(fd, bloom, sizeof(struct oldbloom));
if (out != sizeof(struct oldbloom)) { goto save_error; } // LCOV_EXCL_LINE
out = write(fd, bloom->bf, bloom->bytes);
if (out != bloom->bytes) { goto save_error; } // LCOV_EXCL_LINE
close(fd);
return 0;
// LCOV_EXCL_START
save_error:
close(fd);
return 1;
// LCOV_EXCL_STOP
}
int oldbloom_load(struct oldbloom * bloom, char * filename)
{
int rv = 0;
if (filename == NULL || filename[0] == 0) { return 1; }
if (bloom == NULL) { return 2; }
memset(bloom, 0, sizeof(struct oldbloom));
int fd = open(filename, O_RDONLY);
if (fd < 0) { return 3; }
char line[30];
memset(line, 0, 30);
ssize_t in = read(fd, line, strlen(BLOOM_MAGIC));
if (in != strlen(BLOOM_MAGIC)) {
rv = 4;
goto load_error;
}
if (strncmp(line, BLOOM_MAGIC, strlen(BLOOM_MAGIC))) {
rv = 5;
goto load_error;
}
uint16_t size;
in = read(fd, &size, sizeof(uint16_t));
if (in != sizeof(uint16_t)) {
rv = 6;
goto load_error;
}
if (size != sizeof(struct oldbloom)) {
rv = 7;
goto load_error;
}
in = read(fd, bloom, sizeof(struct oldbloom));
if (in != sizeof(struct oldbloom)) {
rv = 8;
goto load_error;
}
bloom->bf = NULL;
if (bloom->major != BLOOM_VERSION_MAJOR) {
rv = 9;
goto load_error;
}
bloom->bf = (unsigned char *)malloc(bloom->bytes);
if (bloom->bf == NULL) { rv = 10; goto load_error; } // LCOV_EXCL_LINE
in = read(fd, bloom->bf, bloom->bytes);
if (in != bloom->bytes) {
rv = 11;
free(bloom->bf);
bloom->bf = NULL;
goto load_error;
}
close(fd);
return rv;
load_error:
close(fd);
bloom->ready = 0;
return rv;
}
*/
const char * oldbloom_version()
{
return MAKESTRING(BLOOM_VERSION);
}

227
oldbloom/bloom.h Normal file
View File

@@ -0,0 +1,227 @@
/*
* Copyright (c) 2012-2019, Jyri J. Virkki
* All rights reserved.
*
* This file is under BSD license. See LICENSE file.
*/
#ifndef _OLDBLOOM_H
#define _OLDBLOOM_H
#ifdef _WIN64
#include <windows.h>
#else
#endif
#ifdef __cplusplus
extern "C" {
#endif
/** ***************************************************************************
* Structure to keep track of one bloom filter. Caller needs to
* allocate this and pass it to the functions below. First call for
* every struct must be to bloom_init().
*
*/
struct oldbloom
{
// These fields are part of the public interface of this structure.
// Client code may read these values if desired. Client code MUST NOT
// modify any of these.
uint64_t entries;
uint64_t bits;
uint64_t bytes;
uint8_t hashes;
long double error;
// Fields below are private to the implementation. These may go away or
// change incompatibly at any moment. Client code MUST NOT access or rely
// on these.
uint8_t ready;
uint8_t major;
uint8_t minor;
double bpe;
uint8_t checksum[32];
uint8_t checksum_backup[32];
uint8_t *bf;
#ifdef _WIN64
HANDLE mutex;
#else
pthread_mutex_t mutex;
#endif
};
/*
Customs
*/
/*
int oldbloom_loadcustom(struct oldbloom * bloom, char * filename);
int oldbloom_savecustom(struct oldbloom * bloom, char * filename);
*/
/** ***************************************************************************
* Initialize the bloom filter for use.
*
* The filter is initialized with a bit field and number of hash functions
* according to the computations from the wikipedia entry:
* http://en.wikipedia.org/wiki/Bloom_filter
*
* Optimal number of bits is:
* bits = (entries * ln(error)) / ln(2)^2
*
* Optimal number of hash functions is:
* hashes = bpe * ln(2)
*
* Parameters:
* -----------
* bloom - Pointer to an allocated struct bloom (see above).
* entries - The expected number of entries which will be inserted.
* Must be at least 1000 (in practice, likely much larger).
* error - Probability of collision (as long as entries are not
* exceeded).
*
* Return:
* -------
* 0 - on success
* 1 - on failure
*
*/
int oldbloom_init2(struct oldbloom * bloom, uint64_t entries, long double error);
/**
* DEPRECATED.
* Kept for compatibility with libbloom v.1. To be removed in v3.0.
*
*/
int oldbloom_init(struct oldbloom * bloom, uint64_t entries, long double error);
/** ***************************************************************************
* Check if the given element is in the bloom filter. Remember this may
* return false positive if a collision occurred.
*
* Parameters:
* -----------
* bloom - Pointer to an allocated struct bloom (see above).
* buffer - Pointer to buffer containing element to check.
* len - Size of 'buffer'.
*
* Return:
* -------
* 0 - element is not present
* 1 - element is present (or false positive due to collision)
* -1 - bloom not initialized
*
*/
int oldbloom_check(struct oldbloom * bloom, const void * buffer, int len);
/** ***************************************************************************
* Add the given element to the bloom filter.
* The return code indicates if the element (or a collision) was already in,
* so for the common check+add use case, no need to call check separately.
*
* Parameters:
* -----------
* bloom - Pointer to an allocated struct bloom (see above).
* buffer - Pointer to buffer containing element to add.
* len - Size of 'buffer'.
*
* Return:
* -------
* 0 - element was not present and was added
* 1 - element (or a collision) had already been added previously
* -1 - bloom not initialized
*
*/
int oldbloom_add(struct oldbloom * bloom, const void * buffer, int len);
/** ***************************************************************************
* Print (to stdout) info about this bloom filter. Debugging aid.
*
*/
void oldbloom_print(struct oldbloom * bloom);
/** ***************************************************************************
* Deallocate internal storage.
*
* Upon return, the bloom struct is no longer usable. You may call bloom_init
* again on the same struct to reinitialize it again.
*
* Parameters:
* -----------
* bloom - Pointer to an allocated struct bloom (see above).
*
* Return: none
*
*/
void oldbloom_free(struct oldbloom * bloom);
/** ***************************************************************************
* Erase internal storage.
*
* Erases all elements. Upon return, the bloom struct returns to its initial
* (initialized) state.
*
* Parameters:
* -----------
* bloom - Pointer to an allocated struct bloom (see above).
*
* Return:
* 0 - on success
* 1 - on failure
*
*/
int oldbloom_reset(struct oldbloom * bloom);
/** ***************************************************************************
* Save a bloom filter to a file.
*
* Parameters:
* -----------
* bloom - Pointer to an allocated struct bloom (see above).
* filename - Create (or overwrite) bloom data to this file.
*
* Return:
* 0 - on success
* 1 - on failure
*
*/
//int oldbloom_save(struct oldbloom * bloom, char * filename);
/** ***************************************************************************
* Load a bloom filter from a file.
*
* This functions loads a file previously saved with bloom_save().
*
* Parameters:
* -----------
* bloom - Pointer to an allocated struct bloom (see above).
* filename - Load bloom filter data from this file.
*
* Return:
* 0 - on success
* > 0 - on failure
*
*/
//int oldbloom_load(struct oldbloom * bloom, char * filename);
/** ***************************************************************************
* Returns version string compiled into library.
*
* Return: version string
*
*/
const char * oldbloom_version();
#ifdef __cplusplus
}
#endif
#endif

227
oldbloom/oldbloom.h Normal file
View File

@@ -0,0 +1,227 @@
/*
* Copyright (c) 2012-2019, Jyri J. Virkki
* All rights reserved.
*
* This file is under BSD license. See LICENSE file.
*/
#ifndef _OLDBLOOM_H
#define _OLDBLOOM_H
#ifdef _WIN64
#include <windows.h>
#else
#endif
#ifdef __cplusplus
extern "C" {
#endif
/** ***************************************************************************
* Structure to keep track of one bloom filter. Caller needs to
* allocate this and pass it to the functions below. First call for
* every struct must be to bloom_init().
*
*/
struct oldbloom
{
// These fields are part of the public interface of this structure.
// Client code may read these values if desired. Client code MUST NOT
// modify any of these.
uint64_t entries;
uint64_t bits;
uint64_t bytes;
uint8_t hashes;
long double error;
// Fields below are private to the implementation. These may go away or
// change incompatibly at any moment. Client code MUST NOT access or rely
// on these.
uint8_t ready;
uint8_t major;
uint8_t minor;
double bpe;
uint8_t checksum[32];
uint8_t checksum_backup[32];
uint8_t *bf;
#ifdef _WIN64
HANDLE mutex;
#else
pthread_mutex_t mutex;
#endif
};
/*
Customs
*/
/*
int oldbloom_loadcustom(struct oldbloom * bloom, char * filename);
int oldbloom_savecustom(struct oldbloom * bloom, char * filename);
*/
/** ***************************************************************************
* Initialize the bloom filter for use.
*
* The filter is initialized with a bit field and number of hash functions
* according to the computations from the wikipedia entry:
* http://en.wikipedia.org/wiki/Bloom_filter
*
* Optimal number of bits is:
* bits = (entries * ln(error)) / ln(2)^2
*
* Optimal number of hash functions is:
* hashes = bpe * ln(2)
*
* Parameters:
* -----------
* bloom - Pointer to an allocated struct bloom (see above).
* entries - The expected number of entries which will be inserted.
* Must be at least 1000 (in practice, likely much larger).
* error - Probability of collision (as long as entries are not
* exceeded).
*
* Return:
* -------
* 0 - on success
* 1 - on failure
*
*/
int oldbloom_init2(struct oldbloom * bloom, uint64_t entries, long double error);
/**
* DEPRECATED.
* Kept for compatibility with libbloom v.1. To be removed in v3.0.
*
*/
int oldbloom_init(struct oldbloom * bloom, uint64_t entries, long double error);
/** ***************************************************************************
* Check if the given element is in the bloom filter. Remember this may
* return false positive if a collision occurred.
*
* Parameters:
* -----------
* bloom - Pointer to an allocated struct bloom (see above).
* buffer - Pointer to buffer containing element to check.
* len - Size of 'buffer'.
*
* Return:
* -------
* 0 - element is not present
* 1 - element is present (or false positive due to collision)
* -1 - bloom not initialized
*
*/
int oldbloom_check(struct oldbloom * bloom, const void * buffer, int len);
/** ***************************************************************************
* Add the given element to the bloom filter.
* The return code indicates if the element (or a collision) was already in,
* so for the common check+add use case, no need to call check separately.
*
* Parameters:
* -----------
* bloom - Pointer to an allocated struct bloom (see above).
* buffer - Pointer to buffer containing element to add.
* len - Size of 'buffer'.
*
* Return:
* -------
* 0 - element was not present and was added
* 1 - element (or a collision) had already been added previously
* -1 - bloom not initialized
*
*/
int oldbloom_add(struct oldbloom * bloom, const void * buffer, int len);
/** ***************************************************************************
* Print (to stdout) info about this bloom filter. Debugging aid.
*
*/
void oldbloom_print(struct oldbloom * bloom);
/** ***************************************************************************
* Deallocate internal storage.
*
* Upon return, the bloom struct is no longer usable. You may call bloom_init
* again on the same struct to reinitialize it again.
*
* Parameters:
* -----------
* bloom - Pointer to an allocated struct bloom (see above).
*
* Return: none
*
*/
void oldbloom_free(struct oldbloom * bloom);
/** ***************************************************************************
* Erase internal storage.
*
* Erases all elements. Upon return, the bloom struct returns to its initial
* (initialized) state.
*
* Parameters:
* -----------
* bloom - Pointer to an allocated struct bloom (see above).
*
* Return:
* 0 - on success
* 1 - on failure
*
*/
int oldbloom_reset(struct oldbloom * bloom);
/** ***************************************************************************
* Save a bloom filter to a file.
*
* Parameters:
* -----------
* bloom - Pointer to an allocated struct bloom (see above).
* filename - Create (or overwrite) bloom data to this file.
*
* Return:
* 0 - on success
* 1 - on failure
*
*/
//int oldbloom_save(struct oldbloom * bloom, char * filename);
/** ***************************************************************************
* Load a bloom filter from a file.
*
* This functions loads a file previously saved with bloom_save().
*
* Parameters:
* -----------
* bloom - Pointer to an allocated struct bloom (see above).
* filename - Load bloom filter data from this file.
*
* Return:
* 0 - on success
* > 0 - on failure
*
*/
//int oldbloom_load(struct oldbloom * bloom, char * filename);
/** ***************************************************************************
* Returns version string compiled into library.
*
* Return: version string
*
*/
const char * oldbloom_version();
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -920,7 +920,7 @@ void Int::GCD(Int *a) {
// ------------------------------------------------ // ------------------------------------------------
void Int::SetBase10(char *value) { void Int::SetBase10(const char *value) {
CLEAR(); CLEAR();
Int pw((uint64_t)1); Int pw((uint64_t)1);
Int c; Int c;
@@ -937,7 +937,7 @@ void Int::SetBase10(char *value) {
// ------------------------------------------------ // ------------------------------------------------
void Int::SetBase16(char *value) { void Int::SetBase16(const char *value) {
SetBaseN(16,"0123456789ABCDEF",value); SetBaseN(16,"0123456789ABCDEF",value);
} }
@@ -976,7 +976,7 @@ char * Int::GetC64Str(int nbDigit) {
tmp[1] = 0; tmp[1] = 0;
for (int i = 0; i< nbDigit; i++) { for (int i = 0; i< nbDigit; i++) {
if (bits64[i] != 0) { if (bits64[i] != 0) {
#ifdef WIN64 #ifdef _WIN64
sprintf(bStr, "0x%016I64XULL", bits64[i]); sprintf(bStr, "0x%016I64XULL", bits64[i]);
#else #else
sprintf(bStr, "0x%" PRIx64 "ULL", bits64[i]); sprintf(bStr, "0x%" PRIx64 "ULL", bits64[i]);
@@ -993,17 +993,16 @@ char * Int::GetC64Str(int nbDigit) {
// ------------------------------------------------ // ------------------------------------------------
void Int::SetBaseN(int n,char *charset,char *value) { void Int::SetBaseN(int n,const char *charset,const char *value) {
CLEAR(); CLEAR();
Int pw((uint64_t)1); Int pw((uint64_t)1);
Int nb((uint64_t)n); Int nb((uint64_t)n);
Int c; Int c;
int lgth = (int)strlen(value); int lgth = (int)strlen(value);
for(int i=lgth-1;i>=0;i--) { for(int i=lgth-1;i>=0;i--) {
char *p = strchr(charset,toupper(value[i])); char *p = strchr((char*)charset,toupper(value[i]));
if(!p) { if(!p) {
printf("Invalid charset !!\n"); printf("Invalid charset !!\n");
return; return;
} }
int id = (int)(p-charset); int id = (int)(p-charset);
@@ -1016,7 +1015,7 @@ void Int::SetBaseN(int n,char *charset,char *value) {
// ------------------------------------------------ // ------------------------------------------------
char* Int::GetBaseN(int n,char *charset) { char* Int::GetBaseN(int n,const char *charset) {
char *ret = (char*) calloc(1,1024); char *ret = (char*) calloc(1,1024);
Int N(this); Int N(this);

View File

@@ -140,9 +140,9 @@ public:
void SetInt64(uint64_t value); void SetInt64(uint64_t value);
void SetInt32(uint32_t value); void SetInt32(uint32_t value);
void Set(Int *a); void Set(Int *a);
void SetBase10(char *value); void SetBase10(const char *value);
void SetBase16(char *value); void SetBase16(const char *value);
void SetBaseN(int n,char *charset,char *value); void SetBaseN(int n,const char *charset,const char *value);
void SetByte(int n,unsigned char byte); void SetByte(int n,unsigned char byte);
void SetDWord(int n, uint32_t b); void SetDWord(int n, uint32_t b);
void SetQWord(int n,uint64_t b); void SetQWord(int n,uint64_t b);
@@ -161,7 +161,7 @@ public:
char* GetBase2(); char* GetBase2();
char* GetBase10(); char* GetBase10();
char* GetBase16(); char* GetBase16();
char* GetBaseN(int n,char *charset); char* GetBaseN(int n,const char *charset);
char* GetBlockStr(); char* GetBlockStr();
char* GetC64Str(int nbDigit); char* GetC64Str(int nbDigit);
@@ -195,7 +195,7 @@ private:
// Inline routines // Inline routines
#ifndef WIN64 #ifndef _WIN64
// Missing intrinsics // Missing intrinsics
static uint64_t inline _umul128(uint64_t a, uint64_t b, uint64_t *h) { static uint64_t inline _umul128(uint64_t a, uint64_t b, uint64_t *h) {

View File

@@ -855,7 +855,7 @@ void Int::MontgomeryMult(Int *a, Int *b) {
void Int::ModMulK1(Int *a, Int *b) { void Int::ModMulK1(Int *a, Int *b) {
#ifndef WIN64 #ifndef _WIN64
#if (__GNUC__ > 7) || (__GNUC__ == 7 && (__GNUC_MINOR__ > 2)) #if (__GNUC__ > 7) || (__GNUC__ == 7 && (__GNUC_MINOR__ > 2))
unsigned char c; unsigned char c;
#else #else
@@ -917,7 +917,7 @@ void Int::ModMulK1(Int *a, Int *b) {
void Int::ModMulK1(Int *a) { void Int::ModMulK1(Int *a) {
#ifndef WIN64 #ifndef _WIN64
#if (__GNUC__ > 7) || (__GNUC__ == 7 && (__GNUC_MINOR__ > 2)) #if (__GNUC__ > 7) || (__GNUC__ == 7 && (__GNUC_MINOR__ > 2))
unsigned char c; unsigned char c;
#else #else
@@ -977,7 +977,7 @@ void Int::ModMulK1(Int *a) {
void Int::ModSquareK1(Int *a) { void Int::ModSquareK1(Int *a) {
#ifndef WIN64 #ifndef _WIN64
#if (__GNUC__ > 7) || (__GNUC__ == 7 && (__GNUC_MINOR__ > 2)) #if (__GNUC__ > 7) || (__GNUC__ == 7 && (__GNUC_MINOR__ > 2))
unsigned char c; unsigned char c;
#else #else

View File

@@ -75,16 +75,3 @@ void Point::Reduce() {
bool Point::equals(Point &p) { bool Point::equals(Point &p) {
return x.IsEqual(&p.x) && y.IsEqual(&p.y) && z.IsEqual(&p.z); return x.IsEqual(&p.x) && y.IsEqual(&p.y) && z.IsEqual(&p.z);
} }
/*
std::string Point::toString() {
std::string ret;
ret = "X=" + x.GetBase16() + "\n";
ret += "Y=" + y.GetBase16() + "\n";
ret += "Z=" + z.GetBase16() + "\n";
return ret;
}
*/

View File

@@ -53,7 +53,7 @@ void rk_seed(unsigned long seed, rk_state *state)
#define UPPER_MASK 0x80000000UL #define UPPER_MASK 0x80000000UL
#define LOWER_MASK 0x7fffffffUL #define LOWER_MASK 0x7fffffffUL
#ifdef WIN32 #ifdef _WIN64
// Disable "unary minus operator applied to unsigned type, result still unsigned" warning. // Disable "unary minus operator applied to unsigned type, result still unsigned" warning.
#pragma warning(disable : 4146) #pragma warning(disable : 4146)
#endif #endif

View File

@@ -19,7 +19,7 @@
#include <cstring> #include <cstring>
#include "SECP256k1.h" #include "SECP256k1.h"
#include "Point.h" #include "Point.h"
#include "util.h" #include "../util.h"
Secp256K1::Secp256K1() { Secp256K1::Secp256K1() {
} }
@@ -104,6 +104,7 @@ Point Secp256K1::Negation(Point &p) {
Q.x.Set(&p.x); Q.x.Set(&p.x);
Q.y.Set(&this->P); Q.y.Set(&this->P);
Q.y.Sub(&p.y); Q.y.Sub(&p.y);
Q.z.SetInt32(1);
return Q; return Q;
} }
@@ -167,8 +168,8 @@ bool Secp256K1::ParsePublicKeyHex(char *str,Point &ret,bool &isCompressed) {
} }
char* Secp256K1::GetPublicKeyHex(bool compressed, Point &pubKey) { char* Secp256K1::GetPublicKeyHex(bool compressed, Point &pubKey) {
unsigned char publicKeyBytes[128]; unsigned char publicKeyBytes[65];
char *ret; char *ret = NULL;
if (!compressed) { if (!compressed) {
//Uncompressed public key //Uncompressed public key
publicKeyBytes[0] = 0x4; publicKeyBytes[0] = 0x4;
@@ -185,8 +186,24 @@ char* Secp256K1::GetPublicKeyHex(bool compressed, Point &pubKey) {
return ret; return ret;
} }
void Secp256K1::GetPublicKeyHex(bool compressed, Point &pubKey,char *dst){
unsigned char publicKeyBytes[65];
if (!compressed) {
//Uncompressed public key
publicKeyBytes[0] = 0x4;
pubKey.x.Get32Bytes(publicKeyBytes + 1);
pubKey.y.Get32Bytes(publicKeyBytes + 33);
tohex_dst((char*)publicKeyBytes,65,dst);
}
else {
// Compressed public key
publicKeyBytes[0] = pubKey.y.IsEven() ? 0x2 : 0x3;
pubKey.x.Get32Bytes(publicKeyBytes + 1);
tohex_dst((char*)publicKeyBytes,33,dst);
}
}
char* Secp256K1::GetPublicKeyRaw(bool compressed, Point &pubKey) { char* Secp256K1::GetPublicKeyRaw(bool compressed, Point &pubKey) {
unsigned char publicKeyBytes[128];
char *ret = (char*) malloc(65); char *ret = (char*) malloc(65);
if(ret == NULL) { if(ret == NULL) {
::fprintf(stderr,"Can't alloc memory\n"); ::fprintf(stderr,"Can't alloc memory\n");
@@ -206,6 +223,20 @@ char* Secp256K1::GetPublicKeyRaw(bool compressed, Point &pubKey) {
return ret; return ret;
} }
void Secp256K1::GetPublicKeyRaw(bool compressed, Point &pubKey,char *dst) {
if (!compressed) {
//Uncompressed public key
dst[0] = 0x4;
pubKey.x.Get32Bytes((unsigned char*) (dst + 1));
pubKey.y.Get32Bytes((unsigned char*) (dst + 33));
}
else {
// Compressed public key
dst[0] = pubKey.y.IsEven() ? 0x2 : 0x3;
pubKey.x.Get32Bytes((unsigned char*) (dst + 1));
}
}
Point Secp256K1::AddDirect(Point &p1,Point &p2) { Point Secp256K1::AddDirect(Point &p1,Point &p2) {
Int _s; Int _s;
Int _p; Int _p;
@@ -452,3 +483,27 @@ bool Secp256K1::EC(Point &p) {
_s.ModSub(&_p); _s.ModSub(&_p);
return _s.IsZero(); // ( ((pow2(y) - (pow3(x) + 7)) % P) == 0 ); return _s.IsZero(); // ( ((pow2(y) - (pow3(x) + 7)) % P) == 0 );
} }
Point Secp256K1::ScalarMultiplication(Point &P,Int *scalar) {
Point R,Q,T;
int no_of_bits, loop;
no_of_bits = scalar->GetBitLength();
R.Clear();
R.z.SetInt32(1);
if(!scalar->IsZero()) {
Q.Set(P);
if(scalar->GetBit(0) == 1) {
R.Set(P);
}
for(loop = 1; loop < no_of_bits; loop++) {
T = Double(Q);
Q.Set(T);
T.Set(R);
if(scalar->GetBit(loop)){
R = Add(T,Q);
}
}
}
R.Reduce();
return R;
}

View File

@@ -31,9 +31,15 @@ public:
Point ComputePublicKey(Int *privKey); Point ComputePublicKey(Int *privKey);
Point NextKey(Point &key); Point NextKey(Point &key);
bool EC(Point &p); bool EC(Point &p);
Point ScalarMultiplication(Point &P,Int *scalar);
char* GetPublicKeyHex(bool compressed, Point &p); char* GetPublicKeyHex(bool compressed, Point &p);
void GetPublicKeyHex(bool compressed, Point &pubKey,char *dst);
char* GetPublicKeyRaw(bool compressed, Point &p); char* GetPublicKeyRaw(bool compressed, Point &p);
void GetPublicKeyRaw(bool compressed, Point &pubKey,char *dst);
bool ParsePublicKeyHex(char *str,Point &p,bool &isCompressed); bool ParsePublicKeyHex(char *str,Point &p,bool &isCompressed);
Point Add(Point &p1, Point &p2); Point Add(Point &p1, Point &p2);