Skip to content

Commit

Permalink
Add keccak-tiny, thanks David Leon Gil RIP
Browse files Browse the repository at this point in the history
  • Loading branch information
albertobsd committed Dec 18, 2020
1 parent 1f1a9bd commit aea52ce
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ default:
gcc -O3 -c sha256/sha256.c -o sha256.o
gcc -O3 -c base58/base58.c -o base58.o
gcc -O3 -c rmd160/rmd160.c -o rmd160.o
gcc -O3 -c keccak/keccak-tiny.c -o keccak.o -D"memset_s(W,WL,V,OL)=memset(W,V,OL)"
gcc -O3 -c keyhunt.c -o keyhunt.o
gcc -o keyhunt keyhunt.o base58.o rmd160.o sha256.o bloom.o murmurhash2.o -lgmp -lm -lpthread
gcc -o keyhunt keyhunt.o base58.o rmd160.o sha256.o bloom.o murmurhash2.o keccak.o -lgmp -lm -lpthread
clean:
rm -r *.o

163 changes: 163 additions & 0 deletions keccak/keccak-tiny.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/** libkeccak-tiny
*
* A single-file implementation of SHA-3 and SHAKE.
*
* Implementor: David Leon Gil
* License: CC0, attribution kindly requested. Blame taken too,
* but not liability.
*/
#include "keccak-tiny.h"

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/******** The Keccak-f[1600] permutation ********/

/*** Constants. ***/
static const uint8_t rho[24] = \
{ 1, 3, 6, 10, 15, 21,
28, 36, 45, 55, 2, 14,
27, 41, 56, 8, 25, 43,
62, 18, 39, 61, 20, 44};
static const uint8_t pi[24] = \
{10, 7, 11, 17, 18, 3,
5, 16, 8, 21, 24, 4,
15, 23, 19, 13, 12, 2,
20, 14, 22, 9, 6, 1};
static const uint64_t RC[24] = \
{1ULL, 0x8082ULL, 0x800000000000808aULL, 0x8000000080008000ULL,
0x808bULL, 0x80000001ULL, 0x8000000080008081ULL, 0x8000000000008009ULL,
0x8aULL, 0x88ULL, 0x80008009ULL, 0x8000000aULL,
0x8000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL, 0x8000000000008003ULL,
0x8000000000008002ULL, 0x8000000000000080ULL, 0x800aULL, 0x800000008000000aULL,
0x8000000080008081ULL, 0x8000000000008080ULL, 0x80000001ULL, 0x8000000080008008ULL};

/*** Helper macros to unroll the permutation. ***/
#define rol(x, s) (((x) << s) | ((x) >> (64 - s)))
#define REPEAT6(e) e e e e e e
#define REPEAT24(e) REPEAT6(e e e e)
#define REPEAT5(e) e e e e e
#define FOR5(v, s, e) \
v = 0; \
REPEAT5(e; v += s;)

/*** Keccak-f[1600] ***/
static inline void keccakf(void* state) {
uint64_t* a = (uint64_t*)state;
uint64_t b[5] = {0};
uint64_t t = 0;
uint8_t x, y;

for (int i = 0; i < 24; i++) {
// Theta
FOR5(x, 1,
b[x] = 0;
FOR5(y, 5,
b[x] ^= a[x + y]; ))
FOR5(x, 1,
FOR5(y, 5,
a[y + x] ^= b[(x + 4) % 5] ^ rol(b[(x + 1) % 5], 1); ))
// Rho and pi
t = a[1];
x = 0;
REPEAT24(b[0] = a[pi[x]];
a[pi[x]] = rol(t, rho[x]);
t = b[0];
x++; )
// Chi
FOR5(y,
5,
FOR5(x, 1,
b[x] = a[y + x];)
FOR5(x, 1,
a[y + x] = b[x] ^ ((~b[(x + 1) % 5]) & b[(x + 2) % 5]); ))
// Iota
a[0] ^= RC[i];
}
}

/******** The FIPS202-defined functions. ********/

/*** Some helper macros. ***/

#define _(S) do { S } while (0)
#define FOR(i, ST, L, S) \
_(for (size_t i = 0; i < L; i += ST) { S; })
#define mkapply_ds(NAME, S) \
static inline void NAME(uint8_t* dst, \
const uint8_t* src, \
size_t len) { \
FOR(i, 1, len, S); \
}
#define mkapply_sd(NAME, S) \
static inline void NAME(const uint8_t* src, \
uint8_t* dst, \
size_t len) { \
FOR(i, 1, len, S); \
}

mkapply_ds(xorin, dst[i] ^= src[i]) // xorin
mkapply_sd(setout, dst[i] = src[i]) // setout

#define P keccakf
#define Plen 200

// Fold P*F over the full blocks of an input.
#define foldP(I, L, F) \
while (L >= rate) { \
F(a, I, rate); \
P(a); \
I += rate; \
L -= rate; \
}

/** The sponge-based hash construction. **/
static inline int hash(uint8_t* out, size_t outlen,
const uint8_t* in, size_t inlen,
size_t rate, uint8_t delim) {
if ((out == NULL) || ((in == NULL) && inlen != 0) || (rate >= Plen)) {
return -1;
}
uint8_t a[Plen] = {0};
// Absorb input.
foldP(in, inlen, xorin);
// Xor in the DS and pad frame.
a[inlen] ^= delim;
a[rate - 1] ^= 0x80;
// Xor in the last block.
xorin(a, in, inlen);
// Apply P
P(a);
// Squeeze output.
foldP(out, outlen, setout);
setout(a, out, outlen);
memset_s(a, 200, 0, 200);
return 0;
}

/*** Helper macros to define SHA3 and SHAKE instances. ***/
#define defshake(bits) \
int shake##bits(uint8_t* out, size_t outlen, \
const uint8_t* in, size_t inlen) { \
return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x1f); \
}
#define defsha3(bits) \
int sha3_##bits(uint8_t* out, size_t outlen, \
const uint8_t* in, size_t inlen) { \
if (outlen > (bits/8)) { \
return -1; \
} \
return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x06); \
}

/*** FIPS202 SHAKE VOFs ***/
defshake(128)
defshake(256)

/*** FIPS202 SHA3 FOFs ***/
defsha3(224)
defsha3(256)
defsha3(384)
defsha3(512)
19 changes: 19 additions & 0 deletions keccak/keccak-tiny.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef KECCAK_FIPS202_H
#define KECCAK_FIPS202_H
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdint.h>
#include <stdlib.h>

#define decshake(bits) \
int shake##bits(uint8_t*, size_t, const uint8_t*, size_t);

#define decsha3(bits) \
int sha3_##bits(uint8_t*, size_t, const uint8_t*, size_t);

decshake(128)
decshake(256)
decsha3(224)
decsha3(256)
decsha3(384)
decsha3(512)
#endif
99 changes: 89 additions & 10 deletions keyhunt.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ gcc -o keyhunt keyhunt.o base58.o rmd160.o sha256.o bloom.o murmurhash2.o -lgmp
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include "keccak/keccak-tiny.h"
#include "base58/libbase58.h"
#include "rmd160/rmd160.h"
#include "sha256/sha256.h"
Expand Down Expand Up @@ -66,6 +67,7 @@ void *thread_process_range(void *vargp);

void init_doublingG(struct Point *P);
char *pubkeytopubaddress(char *pkey,int length);
char *pubkeytopubaddress_eth(char *pkey,int length);
char *bit_range_str_min;
char *bit_range_str_max;

Expand Down Expand Up @@ -299,7 +301,7 @@ int main(int argc, char **argv) {
}
}
if(FLAGMODE == 1 && FLAGCRYPTO == CRYPTO_NONE) { //When none crypto is defined the default search is for Bitcoin
FLAGCRYPTO == CRYPTO_BTC;
FLAGCRYPTO = CRYPTO_BTC;
printf("Setting search for btc adddress\n");
}
if(FLAGFILE == 0) {
Expand Down Expand Up @@ -644,11 +646,27 @@ void init_doublingG(struct Point *P) {
i++;
}
}
char *pubkeytopubaddress_eth(char *pkey,int length) {
char *temp,*pubaddress = calloc(MAXLENGTHADDRESS,1);
char *digest = malloc(32);
if(digest == NULL || pubaddress == NULL) {
fprintf(stderr,"error malloc()\n");
exit(0);
}
pubaddress[0] = '0';
pubaddress[1] = 'x';
shake256(digest, 256, pkey, length);
temp = tohex(digest+12,20);
strcpy(pubaddress+2,temp);
free(temp);
free(digest);
return pubaddress;
}

char *pubkeytopubaddress(char *pkey,int length) {
char *pubaddress = calloc(MAXLENGTHADDRESS,1);
char *pubaddress = calloc(MAXLENGTHADDRESS+10,1);
char *digest = malloc(60);
long unsigned int pubaddress_size = MAXLENGTHADDRESS;
long unsigned int pubaddress_size = MAXLENGTHADDRESS+10;
if(pubaddress == NULL || digest == NULL) {
fprintf(stderr,"error malloc()\n");
exit(0);
Expand All @@ -665,7 +683,9 @@ char *pubkeytopubaddress(char *pkey,int length) {
//digest [0 +RMD160 20 bytes+SHA256 32 bytes+....0]
sha256(digest+21, 32, digest+21);
//digest [0 +RMD160 20 bytes+SHA256 32 bytes+....0]
b58enc(pubaddress,&pubaddress_size,digest,25);
if(!b58enc(pubaddress,&pubaddress_size,digest,25)){
fprintf(stderr,"error b58enc\n");
}
free(digest);
return pubaddress; // pubaddress need to be free by te caller funtion
}
Expand Down Expand Up @@ -767,11 +787,15 @@ void *thread_process(void *vargp) {
public_key_compressed[0] = 0x03;
}
if(FLAGMODE ) { // FLAGMODE == 1 search for address but for what crypto ?
if( FLAGCRYPTO & CRYPTO_BTC != 0) {
if( (FLAGCRYPTO & CRYPTO_BTC) != 0) {
mpz_export((public_key_uncompressed+1),&longtemp,1,8,1,0,R.x);
mpz_export((public_key_uncompressed+33),&longtemp,1,8,1,0,R.y);
public_address_compressed = pubkeytopubaddress(public_key_compressed,33);
public_address_uncompressed = pubkeytopubaddress(public_key_uncompressed,65);
/*
printf("Testing for %s\n",public_address_compressed);
printf("Testing for %s\n",public_address_uncompressed);
*/
if(FLAGVANITY) {
if(strncmp(public_address_uncompressed,vanity,len_vanity) == 0) {
hextemp = malloc(65);
Expand Down Expand Up @@ -836,9 +860,35 @@ void *thread_process(void *vargp) {
free(public_address_compressed);
free(public_address_uncompressed);
}
if( FLAGCRYPTO & CRYPTO_ETH != 0) {

}
//printf("Resultado %i\n",FLAGCRYPTO & CRYPTO_ETH);
if( (FLAGCRYPTO & CRYPTO_ETH) != 0) {
/*
mpz_export((public_key_uncompressed+1),&longtemp,1,8,1,0,R.x);
mpz_export((public_key_uncompressed+33),&longtemp,1,8,1,0,R.y);
public_address_uncompressed = pubkeytopubaddress_eth(public_key_uncompressed+1,64);
//printf("Testing for %s\n",public_address_uncompressed);
r = bloom_check(&bloom,public_address_uncompressed,MAXLENGTHADDRESS);
if(r) {
r = searchbinary(DATABUFFER,public_address_uncompressed,MAXLENGTHADDRESS,N);
if(r) {
hextemp = malloc(65);
mpz_get_str(hextemp,16,random_key_mpz);
public_key_uncompressed_hex = tohex(public_key_uncompressed+1,64);
pthread_mutex_lock(&write_keys);
keys = fopen("keys.txt","a+");
if(keys != NULL) {
fprintf(keys,"PrivKey: %s\npubkey: %s\naddress: %s\n",hextemp,public_key_uncompressed_hex,public_address_uncompressed);
fclose(keys);
}
printf("HIT!! PrivKey: %s\npubkey: %s\naddress: %s\n",hextemp,public_key_uncompressed_hex,public_address_uncompressed);
pthread_mutex_unlock(&write_keys);
free(public_key_uncompressed_hex);
free(hextemp);
}
free(public_address_uncompressed);
}
*/
}
}
else { //FLAGMODE == 0
r = bloom_check(&bloom,public_key_compressed+1,MAXLENGTHADDRESS);
Expand Down Expand Up @@ -930,11 +980,15 @@ void *thread_process_range(void *vargp) {
public_key_compressed[0] = 0x03;
}
if(FLAGMODE) { // FLAGMODE == 1
if( FLAGCRYPTO & CRYPTO_BTC != 0) {
if( (FLAGCRYPTO & CRYPTO_BTC) != 0) {
mpz_export((public_key_uncompressed+1),&longtemp,1,8,1,0,R.x);
mpz_export((public_key_uncompressed+33),&longtemp,1,8,1,0,R.y);
public_address_compressed = pubkeytopubaddress(public_key_compressed,33);
public_address_uncompressed = pubkeytopubaddress(public_key_uncompressed,65);
/*
printf("Testing for %s\n",public_address_compressed);
printf("Testing for %s\n",public_address_uncompressed);
*/
if(FLAGVANITY) {
if(strncmp(public_address_uncompressed,vanity,len_vanity) == 0) {
hextemp = malloc(65);
Expand Down Expand Up @@ -999,7 +1053,32 @@ void *thread_process_range(void *vargp) {
free(public_address_uncompressed);
}
if( ( FLAGCRYPTO & CRYPTO_ETH ) != 0) {

/*
mpz_export((public_key_uncompressed+1),&longtemp,1,8,1,0,R.x);
mpz_export((public_key_uncompressed+33),&longtemp,1,8,1,0,R.y);
public_address_uncompressed = pubkeytopubaddress_eth(public_key_uncompressed+1,64);
//printf("Testing for %s\n",public_address_uncompressed);
r = bloom_check(&bloom,public_address_uncompressed,MAXLENGTHADDRESS);
if(r) {
r = searchbinary(DATABUFFER,public_address_uncompressed,MAXLENGTHADDRESS,N);
if(r) {
hextemp = malloc(65);
mpz_get_str(hextemp,16,key_mpz);
public_key_uncompressed_hex = tohex(public_key_uncompressed+1,64);
pthread_mutex_lock(&write_keys);
keys = fopen("keys.txt","a+");
if(keys != NULL) {
fprintf(keys,"PrivKey: %s\npubkey: %s\naddress: %s\n",hextemp,public_key_uncompressed_hex,public_address_uncompressed);
fclose(keys);
}
printf("HIT!! PrivKey: %s\npubkey: %s\naddress: %s\n",hextemp,public_key_uncompressed_hex,public_address_uncompressed);
pthread_mutex_unlock(&write_keys);
free(public_key_uncompressed_hex);
free(hextemp);
}
free(public_address_uncompressed);
}
*/
}
}
else { // FLAGMODE == 0
Expand Down

0 comments on commit aea52ce

Please sign in to comment.