Skip to content

Commit

Permalink
Simple Address Generation
Browse files Browse the repository at this point in the history
  • Loading branch information
MakisChristou committed Dec 16, 2020
1 parent fd56831 commit 9bacc03
Show file tree
Hide file tree
Showing 124 changed files with 30,185 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
CC=gcc
CFLAGS=-Ofast -Wall -Wno-unused-function -Wno-pointer-sign \
-I. -Isecp256k1 -Isecp256k1/include -funsafe-loop-optimizations
LDFLAGS=$(CFLAGS)
LDLIBS=-lm -lgmp

SHA256=sha256/sha256.o sha256/sha256-avx-asm.o sha256/sha256-avx2-asm.o \
sha256/sha256-ssse3-asm.o sha256/sha256-ni-asm.o

OBJS=vanitygen.o base58.o cpu.o rmd160.o segwit_addr.o $(SHA256)


all: vanitygen

install: all
cp --remove-destination -p vanitygen /usr/local/bin/

clean:
rm -f vanitygen *.o sha256/*.o

distclean: clean
$(MAKE) -C secp256k1 distclean


vanitygen: $(OBJS)

$(OBJS): Makefile *.h secp256k1/src/libsecp256k1-config.h secp256k1/src/ecmult_static_context.h

secp256k1/src/libsecp256k1-config.h:
(cd secp256k1;./autogen.sh;./configure)

secp256k1/src/ecmult_static_context.h:
$(MAKE) -C secp256k1 src/ecmult_static_context.h
151 changes: 151 additions & 0 deletions base58.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright 2012-2014 Luke Dashjr
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#include "externs.h"

static const int8_t b58digits_map[] = {
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1,
-1, 9,10,11,12,13,14,15, 16,-1,17,18,19,20,21,-1,
22,23,24,25,26,27,28,29, 30,31,32,-1,-1,-1,-1,-1,
-1,33,34,35,36,37,38,39, 40,41,42,43,-1,44,45,46,
47,48,49,50,51,52,53,54, 55,56,57,-1,-1,-1,-1,-1,
};

bool b58tobin(void *bin, size_t *binszp, const char *b58, size_t b58sz)
{
size_t binsz = *binszp;
const unsigned char *b58u = (void*)b58;
unsigned char *binu = bin;
size_t outisz = (binsz + 3) / 4;
uint32_t outi[outisz];
uint64_t t;
uint32_t c;
size_t i, j;
uint8_t bytesleft = binsz % 4;
uint32_t zeromask = bytesleft ? (0xffffffff << (bytesleft * 8)) : 0;
unsigned zerocount = 0;

if (!b58sz)
b58sz = strlen(b58);

memset(outi, 0, outisz * sizeof(*outi));

// Leading zeros, just count
for (i = 0; i < b58sz && b58u[i] == '1'; ++i)
++zerocount;

for ( ; i < b58sz; ++i)
{
if (b58u[i] & 0x80)
// High-bit set on invalid digit
return 0;
if (b58digits_map[b58u[i]] == -1)
// Invalid base58 digit
return 0;
c = (unsigned)b58digits_map[b58u[i]];
for (j = outisz; j--; )
{
t = ((uint64_t)outi[j]) * 58 + c;
c = (t & 0x3f00000000) >> 32;
outi[j] = t & 0xffffffff;
}
if (c)
// Output number too big (carry to the next int32)
return 0;
if (outi[0] & zeromask)
// Output number too big (last int32 filled too far)
return 0;
}

j = 0;
switch (bytesleft) {
case 3:
*(binu++) = (outi[0] & 0xff0000) >> 16;
case 2:
*(binu++) = (outi[0] & 0xff00) >> 8;
case 1:
*(binu++) = (outi[0] & 0xff);
++j;
default:
break;
}

for (; j < outisz; ++j)
{
*(binu++) = (outi[j] >> 0x18) & 0xff;
*(binu++) = (outi[j] >> 0x10) & 0xff;
*(binu++) = (outi[j] >> 8) & 0xff;
*(binu++) = (outi[j] >> 0) & 0xff;
}

// Count canonical base58 byte count
binu = bin;
for (i = 0; i < binsz; ++i)
{
if (binu[i])
break;
--*binszp;
}
*binszp += zerocount;

return 1;
}

static const char b58digits_ordered[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

bool b58enc(char *b58, const void *data, size_t binsz)
{
const uint8_t *bin = data;
int carry;
ssize_t i, j, high, zcount = 0;
size_t size;

while (zcount < binsz && !bin[zcount])
++zcount;

size = (binsz - zcount) * 138 / 100 + 1;
uint8_t buf[size];
memset(buf, 0, size);

for (i = zcount, high = size - 1; i < binsz; ++i, high = j)
{
for (carry = bin[i], j = size - 1; (j > high) || carry; --j)
{
carry += 256 * buf[j];
buf[j] = carry % 58;
carry /= 58;
}
}

for (j = 0; j < size && !buf[j]; ++j);

if (zcount)
memset(b58, '1', zcount);
for (i = zcount; j < size; ++i, ++j)
b58[i] = b58digits_ordered[buf[j]];
b58[i] = '\0';

return 1;
}
Binary file added base58.o
Binary file not shown.
62 changes: 62 additions & 0 deletions cpu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* cpu.c - CPU scheduler routines */

#include "externs.h"

static cpu_set_t *cpuset;
static int cpuset_ncpu;
static size_t cpuset_size;


// Return a count of the CPUs currently available to this process.
//
int get_num_cpus()
{
cpuset_ncpu=1024; /* Starting number of CPUs */

while(1) {
cpuset=CPU_ALLOC(cpuset_ncpu);
cpuset_size=CPU_ALLOC_SIZE(cpuset_ncpu);

if(!sched_getaffinity(0, cpuset_size, cpuset))
return CPU_COUNT_S(cpuset_size, cpuset);

if(errno == EINVAL) {
/* Loop, doubling the cpuset, until sched_getaffinity() succeeds */
CPU_FREE(cpuset);
cpuset_ncpu *= 2;
continue;
}

/* Unexpected error, but at least 1 CPU has to be available */
CPU_FREE(cpuset);
cpuset=NULL;
cpuset_ncpu=0;
cpuset_size=0;
return 1;
}
}

// Set this thread's CPU affinity to the Nth CPU in the list.
//
void set_working_cpu(int thread)
{
int i;

if(!cpuset_size)
return;

// The cpuset is already populated with the available CPUs on this system
// from the call to get_num_cpus(). Look for the Nth one.

for(i=0;;) {
if(CPU_ISSET_S(i, cpuset_size, cpuset) && !thread--) {
CPU_ZERO_S(cpuset_size, cpuset);
CPU_SET_S(i, cpuset_size, cpuset);
sched_setaffinity(0, cpuset_size, cpuset); /* Ignore any errors */
return;
}

if(++i >= cpuset_ncpu)
i=0; /* Wrap around */
}
}
Binary file added cpu.o
Binary file not shown.
121 changes: 121 additions & 0 deletions externs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* externs.h - System-specific declarations */

#define _GNU_SOURCE // Use the GNU C Library Extensions

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <fcntl.h>
#include <errno.h>
#include <sched.h>
#include <sys/signal.h>
#include <sys/prctl.h>
#include <sys/mman.h>
#include <sys/sysinfo.h>
#include <arpa/inet.h>

/* Define our own set of types */
#undef quad
#define quad long long
#define bool _Bool

typedef char s8;
typedef short s16;
typedef int s32;
typedef quad s64;

typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned quad u64;

#define align4 __attribute__((aligned(4)))
#define align8 __attribute__((aligned(8)))
#define align16 __attribute__((aligned(16)))

/* Path prediction */
#define likely(x) __builtin_expect((x), 1)
#define unlikely(x) __builtin_expect((x), 0)

/* Little/big-endian byte conversions */
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
# define le16(x) ((u16)(x))
# define le32(x) ((u32)(x))
# define le64(x) ((u64)(x))
# define be16(x) __builtin_bswap16(x)
# define be32(x) __builtin_bswap32(x)
# define be64(x) __builtin_bswap64(x)
#else
# define le16(x) __builtin_bswap16(x)
# define le32(x) __builtin_bswap32(x)
# define le64(x) __builtin_bswap64(x)
# define be16(x) ((u16)(x))
# define be32(x) ((u32)(x))
# define be64(x) ((u64)(x))
#endif

/* Swap the values of two integers, quadwords, doubles, etc. */
#define XCHG(a,b) (void)({ typeof(a) _temp=a; a=b; b=_temp; })
/* ...The same thing can be done using: a ^= b, b ^= a, a ^= b */

/* Rotate a 32-bit word right or left */
#define ROR(x,n) ({ u32 _x=(x), _n=(n); (_x >> _n) | (_x << (32-_n)); })
#define ROL(x,n) ({ u32 _x=(x), _n=(n); (_x << _n) | (_x >> (32-_n)); })

/* Generic min() and max() functions */
#undef min
#undef max
#define min(x,y) ({ typeof(x) _x=x; typeof(y) _y=y; (_x < _y)?_x:_y; })
#define max(x,y) ({ typeof(x) _x=x; typeof(y) _y=y; (_x > _y)?_x:_y; })

/* Optimal way of keeping a number within a set range */
#define RANGE(x,lo,hi) ({ typeof(x) _val=x, _lo=lo, _hi=hi; \
(_val < _lo)?_lo:(_val > _hi)?_hi:_val; })

/* Determines the number of elements in a static array */
#define NELEM(array) (int)(sizeof(array)/sizeof(array[0]))


/**** Module declarations ****************************************************/

/* libsecp256k1 */
#include "secp256k1.h"

/* base58.c */
extern bool b58tobin(void *bin, size_t *binszp, const char *b58, size_t b58sz);
extern bool b58enc(char *b58, const void *data, size_t binsz);

/* cpu.c */
extern int get_num_cpus(void);
extern void set_working_cpu(int thread);

/* rmd160.c */
extern void rmd160_init(void);
extern void rmd160_process(const char input_block[64]);
extern void rmd160_finish(char output[20]);
extern void rmd160_hash(char output[20], const char input[64]);

#define rmd160_prepare(block, sz) ({ \
int _sz=(sz); \
memset(block, 0, 64); \
block[_sz]=0x80; \
block[57]=(_sz*8) >> 8; /* Little-endian length in bits */ \
block[56]=(_sz*8) & 0xff; \
})

/* sha256.c */
extern void sha256_init(void);
extern void sha256_process(const char input_block[64]);
extern void sha256_finish(char output[32]);
extern void sha256_hash(char output[32], const char input[64]);
extern void sha256_register(bool verbose);

#define sha256_prepare(block, sz) ({ \
int _sz=(sz); \
memset(block, 0, 64); \
block[_sz]=0x80; \
block[62]=(_sz*8) >> 8; /* Big-endian length in bits */ \
block[63]=(_sz*8) & 0xff; \
})
Loading

0 comments on commit 9bacc03

Please sign in to comment.