Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for macOS compilation #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ distclean: clean

vanitygen: $(OBJS)

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

UNAME_S := $(shell uname -s)

# If on macOS, please install secp256k1 using (home)brew instead.
$(OBJS): Makefile *.h $(ifneq ($(UNAME_S),Darwin),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
$(MAKE) -C secp256k1 src/ecmult_static_context.h
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ Address: bc1q0000erwyfzl9m6y9yr8sun52p6tvmrzp5jpwtt

## Important Notes
When using this software double check the generated addresses using other implementations to make sure that everyting is working correctly. Do NOT send mainnet coins to generated addresses unless you make sure that the address matches the coresponding private key. I would recommend this open source [tool](https://segwitaddress.org/bech32/) for double checking. Also, please do not use the addresses shown in this readme file as you will (probably) get robbed.

## Notes for macOS
If building on macOS, please install the secp256k1 package using (home)brew, as the version for macOS is not packaged into this repository.
71 changes: 71 additions & 0 deletions cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,77 @@

#include "externs.h"

// below code taken and modified from https://www.hybridkernel.com/2015/01/18/binding_threads_to_cores_osx.html
// this code is needed because Apple does not support Linux's APIs.
#ifdef __APPLE__
#include <sys/sysctl.h>
#include <pthread.h>
#include <mach/thread_act.h>
#define SYSCTL_CORE_COUNT "machdep.cpu.core_count"

typedef struct cpu_set {
uint32_t count;
} cpu_set_t;

static inline void
CPU_ZERO_S(size_t setsize, cpu_set_t *cs) { cs->count = 0; }

static inline void
CPU_SET_S(int num, size_t setsize, cpu_set_t *cs) { cs->count |= (1 << num); }

static inline int
CPU_ISSET(int num, cpu_set_t *cs) { return (cs->count & (1 << num)); }

static inline int
CPU_ISSET_S(int num, size_t setsize, cpu_set_t *cs) { return (cs->count & (1 << num)); }

cpu_set_t *
CPU_ALLOC(int num) { return (cpu_set_t *)malloc(sizeof(cpu_set_t)); }

void
CPU_FREE(cpu_set_t *set) { free(set); }

size_t
CPU_ALLOC_SIZE(int num) { return sizeof(cpu_set_t); }

static inline int
CPU_COUNT_S(size_t setsize, cpu_set_t *set) { return set->count; }

int sched_getaffinity(pid_t pid, size_t cpu_size, cpu_set_t *cpu_set)
{
int32_t core_count = 0;
size_t len = sizeof(core_count);
int ret = sysctlbyname(SYSCTL_CORE_COUNT, &core_count, &len, 0, 0);
if (ret) {
printf("error while get core count %d\n", ret);
return -1;
}
cpu_set->count = 0;
for (int i = 0; i < core_count; i++) {
cpu_set->count |= (1 << i);
}

return 0;
}

int sched_setaffinity(pthread_t thread, size_t cpu_size,
cpu_set_t *cpu_set)
{
thread_port_t mach_thread;
int core = 0;

for (core = 0; core < 8 * cpu_size; core++) {
if (CPU_ISSET(core, cpu_set)) break;
}
printf("binding to core %d\n", core);
thread_affinity_policy_data_t policy = { core };
mach_thread = pthread_mach_thread_np(thread);
thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY,
(thread_policy_t)&policy, 1);
return 0;
}
#endif

static cpu_set_t *cpuset;
static int cpuset_ncpu;
static size_t cpuset_size;
Expand Down
8 changes: 6 additions & 2 deletions externs.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
#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>

// these headers are unavailable for macOS
#ifndef __APPLE__
#include <sys/prctl.h>
#include <sys/sysinfo.h>
#endif

/* Define our own set of types */
#undef quad
#define quad long long
Expand Down
11 changes: 6 additions & 5 deletions vanitygen.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ double get_difficulty(char* pattern, char* hrp)

}

extern void check_pattern(char* pattern);

// Use getopt to parse cli arguments
void parse_arguments(int argc, char** argv)
Expand Down Expand Up @@ -181,7 +182,7 @@ void check_pattern(char* pattern)
// Address generation code
void* vanity_engine(void *vargp)
{
int threadid = (int *)vargp;
int threadid = *(int *)vargp;

// Declare Secp256k1 Stuff
secp256k1_context *sec_ctx;
Expand Down Expand Up @@ -227,8 +228,8 @@ void* vanity_engine(void *vargp)

// Generate private key
if((fd=open("/dev/urandom", O_RDONLY|O_NOCTTY)) == -1) {
perror("/dev/urandom");
return;
perror("/dev/urandom");
return NULL;
}

// Stolen from supervanitygen
Expand All @@ -238,7 +239,7 @@ void* vanity_engine(void *vargp)
if(len != -1)
errno=EAGAIN;
perror("/dev/urandom");
return;
return NULL;
}
} while(privkey[0]+1 < 2); /* Ensure only valid private keys */

Expand Down Expand Up @@ -372,7 +373,7 @@ int main(int argc, char** argv)
int status;

for(i=0;i<noOfThread;i++)
pthread_create (&thread_id[i], NULL , &vanity_engine, i);
pthread_create (&thread_id[i], NULL , &vanity_engine, (void*)&i);

for(i=0;i<noOfThread;i++)
pthread_join(thread_id[i],NULL);
Expand Down