Skip to content

Commit

Permalink
Merge branch 'cuckarooz'
Browse files Browse the repository at this point in the history
adopt cuckarooz
  • Loading branch information
tromp committed Jul 4, 2020
2 parents e79fb8e + 59b4ff1 commit f06d706
Show file tree
Hide file tree
Showing 13 changed files with 5,009 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/cuckaroom/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,15 @@ oldcuda29: ../crypto/siphash.cuh compress.hpp graph.hpp mean.cu kernel.cuh Makef

cuda29: ../crypto/siphash.cuh compress.hpp graph.hpp meaner.cu Makefile
$(NVCC) -o $@ -DEDGEBITS=29 -arch sm_35 meaner.cu $(BLAKE_2B_SRC)

cuda66: ../crypto/siphash.cuh compress.hpp graph.hpp meaner66.cu Makefile
$(NVCC) -o $@ -DEDGEBITS=29 -arch sm_35 meaner66.cu $(BLAKE_2B_SRC)

cuda29v: ../crypto/siphash.cuh compress.hpp graph.hpp meaner.cu Makefile
$(NVCC) -o $@ -DEDGEBITS=29 -DVERBOSE -arch sm_35 meaner.cu $(BLAKE_2B_SRC)

old66v: ../crypto/siphash.cuh compress.hpp graph.hpp old66.cu Makefile
$(NVCC) -o $@ -DEDGEBITS=29 -DVERBOSE -arch sm_35 old66.cu $(BLAKE_2B_SRC)

cuda66v: ../crypto/siphash.cuh compress.hpp graph.hpp meaner66.cu Makefile
$(NVCC) -o $@ -DEDGEBITS=29 -DVERBOSE -arch sm_35 meaner66.cu $(BLAKE_2B_SRC)
62 changes: 62 additions & 0 deletions src/cuckarooz/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
.POSIX:
.SUFFIXES:

OPT ?= -O3

GCC_ARCH_FLAGS ?= -march=native
GPP_ARCH_FLAGS ?= -march=native

# -Wno-deprecated-declarations shuts up Apple OSX clang
FLAGS ?= -Wall -Wno-format -Wno-deprecated-declarations -D_POSIX_C_SOURCE=200112L $(OPT) -DPREFETCH -I. $(CPPFLAGS) -pthread
GPP ?= g++ $(GPP_ARCH_FLAGS) -std=c++11 $(FLAGS)
CFLAGS ?= -Wall -Wno-format -fomit-frame-pointer $(OPT)
GCC ?= gcc $(GCC_ARCH_FLAGS) -std=gnu11 $(CFLAGS)
BLAKE_2B_SRC ?= ../crypto/blake2b-ref.c
NVCC ?= nvcc -std=c++11

all : simpletest # cpu miner not working yet # meantest

simpletest: simple19
./simple19 -n 64

meantest: mean29x4
./mean29x4 -n 23 -t 4 -s

simple19: ../crypto/siphash.hpp cuckarooz.hpp bitmap.hpp graph.hpp simple.cpp Makefile
$(GPP) -o $@ -DPROOFSIZE=42 -DEDGEBITS=19 simple.cpp $(BLAKE_2B_SRC)

simple29: ../crypto/siphash.hpp cuckarooz.hpp bitmap.hpp graph.hpp simple.cpp Makefile
$(GPP) -o $@ -DPROOFSIZE=42 -DEDGEBITS=29 simple.cpp $(BLAKE_2B_SRC)

mean19x1: cuckarooz.hpp bitmap.hpp graph.hpp ../threads/barrier.hpp ../crypto/siphash.hpp mean.hpp mean.cpp Makefile
$(GPP) -o $@ -DXBITS=2 -DNSIPHASH=1 -DEDGEBITS=19 mean.cpp $(BLAKE_2B_SRC)

mean19x4: cuckarooz.hpp bitmap.hpp graph.hpp ../threads/barrier.hpp ../crypto/siphash.hpp mean.hpp mean.cpp Makefile
$(GPP) -o $@ -mno-avx2 -DXBITS=2 -DNSIPHASH=4 -DEDGEBITS=19 mean.cpp $(BLAKE_2B_SRC)

mean19x8: cuckarooz.hpp bitmap.hpp graph.hpp ../threads/barrier.hpp ../crypto/siphash.hpp mean.hpp mean.cpp Makefile
$(GPP) -o $@ -mavx2 -DXBITS=2 -DNSIPHASH=8 -DEDGEBITS=19 mean.cpp $(BLAKE_2B_SRC)

mean29x4: cuckarooz.hpp bitmap.hpp graph.hpp ../crypto/siphash.hpp mean.hpp mean.cpp Makefile
$(GPP) -o $@ -mno-avx2 -DNSIPHASH=4 -DEDGEBITS=29 mean.cpp $(BLAKE_2B_SRC)

mean29x8: cuckarooz.hpp bitmap.hpp graph.hpp ../threads/barrier.hpp ../crypto/siphash.hpp mean.hpp mean.cpp Makefile
$(GPP) -o $@ -mavx2 -DNSIPHASH=8 -DEDGEBITS=29 mean.cpp $(BLAKE_2B_SRC)

mean29x8s: cuckarooz.hpp bitmap.hpp graph.hpp ../threads/barrier.hpp ../crypto/siphash.hpp mean.hpp mean.cpp Makefile
$(GPP) -o $@ -mavx2 -DSAVEEDGES -DNSIPHASH=8 -DEDGEBITS=29 mean.cpp $(BLAKE_2B_SRC)

mean29x1: cuckarooz.hpp bitmap.hpp graph.hpp ../threads/barrier.hpp ../crypto/siphash.hpp mean.hpp mean.cpp Makefile
$(GPP) -o $@ -DNSIPHASH=1 -DEDGEBITS=29 mean.cpp $(BLAKE_2B_SRC)

mean30x1: cuckarooz.hpp bitmap.hpp graph.hpp ../threads/barrier.hpp ../crypto/siphash.hpp mean.hpp mean.cpp Makefile
$(GPP) -o $@ -DNSIPHASH=1 -DEXPANDROUND=10 -DCOMPRESSROUND=22 -DEDGEBITS=30 mean.cpp $(BLAKE_2B_SRC)

mean30x8: cuckarooz.hpp bitmap.hpp graph.hpp ../threads/barrier.hpp ../crypto/siphash.hpp mean.hpp mean.cpp Makefile
$(GPP) -o $@ -mavx2 -DNSIPHASH=8 -DEXPANDROUND=10 -DCOMPRESSROUND=22 -DEDGEBITS=30 mean.cpp $(BLAKE_2B_SRC)

cuda19: ../crypto/siphash.cuh compress.hpp graph.hpp mean.cu Makefile
$(NVCC) -o $@ -DEPS_A=4 -DEPS_B=3 -DIDXSHIFT=2 -DEDGEBITS=19 -arch sm_35 mean.cu $(BLAKE_2B_SRC)

cuda29: ../crypto/siphash.cuh compress.hpp graph.hpp mean.cu Makefile
$(NVCC) -o $@ -DEDGEBITS=29 -arch sm_35 mean.cu $(BLAKE_2B_SRC)
67 changes: 67 additions & 0 deletions src/cuckarooz/bitmap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
template <typename word_t>
class bitmap {
public:
word_t SIZE;
word_t BITMAP_WORDS;
#ifdef ATOMIC
typedef std::atomic<word_t> aword_t;
#else
typedef word_t aword_t;
#endif
aword_t *bits;
const u32 BITS_PER_WORD = sizeof(word_t) * 8;

bitmap(word_t size) {
SIZE = size;
BITMAP_WORDS = SIZE / BITS_PER_WORD;
bits = new aword_t[BITMAP_WORDS];
assert(bits != 0);
}
~bitmap() {
freebits();
}
void freebits() {
delete[] bits;
bits = 0;
}
void clear() {
assert(bits);
memset((word_t *)bits, 0, BITMAP_WORDS*sizeof(word_t));
}
void prefetch(u32 u) const {
#ifdef PREFETCH
__builtin_prefetch((const void *)(&bits[u/BITS_PER_WORD]), /*READ=*/0, /*TEMPORAL=*/0);
#endif
}
void set(u32 u) {
u32 idx = u / BITS_PER_WORD;
word_t bit = (word_t)1 << (u % BITS_PER_WORD);
#ifdef ATOMIC
std::atomic_fetch_or_explicit(&bits[idx], bit, std::memory_order_relaxed);
#else
bits[idx] |= bit;
#endif
}
void reset(u32 u) {
u32 idx = u / BITS_PER_WORD;
word_t bit = (word_t)1 << (u % BITS_PER_WORD);
#ifdef ATOMIC
std::atomic_fetch_and_explicit(&bits[idx], ~bit, std::memory_order_relaxed);
#else
bits[idx] &= ~bit;
#endif
}
bool test(u32 u) const {
u32 idx = u / BITS_PER_WORD;
u32 bit = u % BITS_PER_WORD;
#ifdef ATOMIC
return (bits[idx].load(std::memory_order_relaxed) >> bit) & 1;
#else
return (bits[idx] >> bit) & 1;
#endif
}
word_t block(u32 n) const {
u32 idx = n / BITS_PER_WORD;
return bits[idx];
}
};
75 changes: 75 additions & 0 deletions src/cuckarooz/compress.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <new>

// compressor for cuckarooz nodes where edgetrimming
// has left at most a fraction 2^-compressbits nodes in each partition
template <typename word_t>
class compressor {
public:
u32 NODEBITS;
u32 SHIFTBITS;
u32 SIZEBITS;
word_t SIZE;
word_t SIZE2;
word_t MASK;
word_t MASK2;
word_t nnodes;
const word_t NIL = ~(word_t)0;
word_t *nodes;
bool sharedmem;

compressor(u32 nodebits, u32 compressbits, char *bytes) {
NODEBITS = nodebits;
SHIFTBITS = compressbits;
SIZEBITS = NODEBITS-compressbits;
SIZE = (word_t)1 << SIZEBITS;
SIZE2 = (word_t)2 << SIZEBITS;
nodes = new (bytes) word_t[SIZE2];
sharedmem = true;
MASK = SIZE-1;
MASK2 = SIZE2-1;
}

compressor(u32 nodebits, u32 compressbits) {
NODEBITS = nodebits;
SHIFTBITS = compressbits;
SIZEBITS = NODEBITS-compressbits;
SIZE = (word_t)1 << SIZEBITS;
SIZE2 = (word_t)2 << SIZEBITS;
nodes = new word_t[SIZE2];
sharedmem = false;
MASK = SIZE-1;
MASK2 = SIZE2-1;
}

~compressor() {
if (!sharedmem)
delete[] nodes;
}

uint64_t bytes() {
return sizeof(word_t[SIZE2]);
}

void reset() {
memset(nodes, (char)NIL, sizeof(word_t[SIZE2]));
nnodes = 0;
}

word_t compress(word_t u) {
word_t ui = u >> SHIFTBITS;
for (; ; ui = (ui+1) & MASK2) {
word_t cu = nodes[ui];
if (cu == NIL) {
if (nnodes >= SIZE) {
print_log("NODE OVERFLOW at %x\n", u);
return 0;
}
nodes[ui] = u << SIZEBITS | nnodes;
return nnodes++;
}
if ((cu & ~MASK) == u << SIZEBITS) {
return cu & MASK;
}
}
}
};
59 changes: 59 additions & 0 deletions src/cuckarooz/cuckarooz.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Cuckarooz Cycle, a memory-hard proof-of-work
// Copyright (c) 2013-2020 John Tromp

#include "cuckarooz.h"
#include <inttypes.h> // for SCNx64 macro
#include <stdio.h> // printf/scanf
#include <stdlib.h> // exit
#include <unistd.h> // getopt
#include <assert.h> // d'uh

// arbitrary length of header hashed into siphash key
#define HEADERLEN 80

int main(int argc, char **argv) {
const char *header = "";
int nonce = 0;
int c;
while ((c = getopt (argc, argv, "h:n:")) != -1) {
switch (c) {
case 'h':
header = optarg;
break;
case 'n':
nonce = atoi(optarg);
break;
}
}
char headernonce[HEADERLEN];
u32 hdrlen = strlen(header);
memcpy(headernonce, header, hdrlen);
memset(headernonce+hdrlen, 0, sizeof(headernonce)-hdrlen);
((u32 *)headernonce)[HEADERLEN/sizeof(u32)-1] = htole32(nonce);
siphash_keys keys;
setheader(headernonce, sizeof(headernonce), &keys);
printf("nonce %d k0 k1 k2 k3 %llx %llx %llx %llx\n", nonce, keys.k0, keys.k1, keys.k2, keys.k3);
printf("Verifying size %d proof for cuckarooz%d(\"%s\",%d)\n",
PROOFSIZE, EDGEBITS, header, nonce);
for (int nsols=0; scanf(" Solution") == 0; nsols++) {
word_t nonces[PROOFSIZE];
for (int n = 0; n < PROOFSIZE; n++) {
uint64_t nonce;
int nscan = scanf(" %" SCNx64, &nonce);
assert(nscan == 1);
nonces[n] = nonce;
}
int pow_rc = verify(nonces, &keys);
if (pow_rc == POW_OK) {
printf("Verified with cyclehash ");
unsigned char cyclehash[32];
blake2b((void *)cyclehash, sizeof(cyclehash), (const void *)nonces, sizeof(nonces), 0, 0);
for (int i=0; i<32; i++)
printf("%02x", cyclehash[i]);
printf("\n");
} else {
printf("FAILED due to %s\n", errstr[pow_rc]);
}
}
return 0;
}
Loading

0 comments on commit f06d706

Please sign in to comment.