Skip to content

Commit

Permalink
Merge pull request #961 from madMAx43v3r/k34-support
Browse files Browse the repository at this point in the history
K34 support
  • Loading branch information
madMAx43v3r committed Oct 23, 2021
2 parents 0cf1586 + 04d1898 commit 8332d62
Show file tree
Hide file tree
Showing 10 changed files with 322 additions and 116 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Expand Up @@ -107,6 +107,9 @@ add_executable(test_phase_4 test/test_phase_4.cpp)
add_executable(check_phase_1 test/check_phase_1.cpp)

add_executable(chia_plot src/chia_plot.cpp)
add_executable(chia_plot_k34 src/chia_plot.cpp)

target_compile_definitions(chia_plot_k34 PUBLIC CHIA_K34=1)

target_link_libraries(test_copy chia_plotter)
target_link_libraries(test_disk_sort chia_plotter)
Expand All @@ -119,3 +122,4 @@ target_link_libraries(test_phase_4 chia_plotter)
target_link_libraries(check_phase_1 chia_plotter)

target_link_libraries(chia_plot chia_plotter bls sodium)
target_link_libraries(chia_plot_k34 chia_plotter bls sodium)
57 changes: 57 additions & 0 deletions include/chia/bits.hpp
Expand Up @@ -554,4 +554,61 @@ using Bits = BitsGeneric<SmallVector>;
using ParkBits = BitsGeneric<ParkVector>;
using LargeBits = BitsGeneric<LargeVector>;


inline
int write_bits(uint64_t* dst, const uint64_t value, const int bit_offset, const int num_bits)
{
assert(num_bits <= 64);
const int free_bits = 64 - (bit_offset % 64);
if(free_bits >= num_bits) {
dst[bit_offset / 64] |= bswap_64(value << (free_bits - num_bits));
} else {
const int suffix_size = num_bits - free_bits;
const uint64_t suffix = value & ((uint64_t(1) << suffix_size) - 1);
dst[bit_offset / 64] |= bswap_64(value >> suffix_size); // prefix (high bits)
dst[bit_offset / 64 + 1] |= bswap_64(suffix << (64 - suffix_size)); // suffix (low bits)
}
return bit_offset + num_bits;
}

inline
int append_bits(uint64_t* dst, const uint64_t* src, const int bit_offset, const int num_bits)
{
int i = 0;
int offset = bit_offset;
int num_left = num_bits;
while(num_left > 0) {
int bits = 64;
uint64_t value = bswap_64(src[i]);
if(num_left < 64) {
bits = num_left;
value >>= (64 - num_left);
}
offset = write_bits(dst, value, offset, bits);
num_left -= bits;
i++;
}
return offset;
}

inline
int slice_bits(uint64_t* dst, const uint64_t* src, const int bit_offset, const int num_bits)
{
int count = 0;
int offset = bit_offset;
int num_left = num_bits;
while(num_left > 0) {
const int shift = offset % 64;
const int bits = std::min(num_left, 64 - shift);
uint64_t value = bswap_64(src[offset / 64]) << shift;
if(bits < 64) {
value >>= (64 - bits);
}
count = write_bits(dst, value, count, bits);
offset += bits;
num_left -= bits;
}
return count;
}

#endif // SRC_CPP_BITS_HPP_
17 changes: 17 additions & 0 deletions include/chia/entries.h
Expand Up @@ -8,9 +8,26 @@
#ifndef INCLUDE_CHIA_ENTRIES_H_
#define INCLUDE_CHIA_ENTRIES_H_

#include <chia/util.hpp>

#include <cstdio>
#include <cstdint>

#ifdef CHIA_K34
constexpr int KMAX = 34;
constexpr int PMAX = 35;
constexpr int KBYTES = 5;
typedef uint64_t uintkx_t;
typedef uint128_t uintlp_t;
#else
#define CHIA_K32
constexpr int KMAX = 32;
constexpr int PMAX = 32;
constexpr int KBYTES = 4;
typedef uint32_t uintkx_t;
typedef uint64_t uintlp_t;
#endif


template<typename T>
bool write_entry(FILE* file, const T& entry) {
Expand Down
143 changes: 104 additions & 39 deletions include/chia/phase1.h
Expand Up @@ -30,69 +30,128 @@ struct input_t {
};

struct entry_1 {
uint64_t y; // 38 bit
uint32_t x; // 32 bit
uint64_t y; // 38 bit / 40 bit
uintkx_t x; // 32 bit / 34 bit

static constexpr uint32_t pos = 0; // dummy
static constexpr uintkx_t pos = 0; // dummy
static constexpr uint16_t off = 0; // dummy
static constexpr size_t disk_size = 9;
static constexpr size_t disk_size = 5 + KBYTES;

size_t read(const uint8_t* buf) {
y = 0;
memcpy(&y, buf, 5);
memcpy(&x, buf + 5, 4);
if(sizeof(x) > KBYTES) {
x = 0;
}
memcpy(&x, buf + 5, KBYTES); // 32 bit / 40 bit
return disk_size;
}
size_t write(uint8_t* buf) const {
memcpy(buf, &y, 5);
memcpy(buf + 5, &x, 4);
memcpy(buf + 5, &x, KBYTES);
return disk_size;
}
};

struct entry_x {
uint64_t y; // 38 bit
uint32_t pos; // 32 bit
uint64_t y; // 38 bit / 40 bit
uintkx_t pos; // 32 bit / 35 bit
uint16_t off; // 10 bit
};

template<int N>
struct entry_xm : entry_x {
std::array<uint8_t, N * 4> meta;

static constexpr size_t disk_size = 10 + N * 4;
std::array<uint8_t, N> meta;

#ifdef CHIA_K34
static constexpr size_t disk_size = 11 + N;

size_t read(const uint8_t* buf) {
y = 0;
memcpy(&y, buf, 5);
memcpy(&pos, buf + 5, 5);
pos &= 0x3FFFFFFFFF; // 38 bit
memcpy(&off, buf + 9, 2);
off >>= 6;
memcpy(meta.data(), buf + 11, meta.size());
return disk_size;
}
size_t write(uint8_t* buf) const {
memcpy(buf, &y, 5);
memcpy(buf + 5, &pos, 5);
{
const auto tmp = (off << 6) | buf[9];
memcpy(buf + 9, &tmp, 2);
}
memcpy(buf + 11, meta.data(), meta.size());
return disk_size;
}
#else
static constexpr size_t disk_size = 10 + N;

size_t read(const uint8_t* buf) {
memcpy(&y, buf, 5);
y &= 0x3FFFFFFFFFull;
off = 0;
off |= buf[4] >> 6;
off |= uint16_t(buf[5]) << 2;
memcpy(&pos, buf + 6, 4);
memcpy(meta.data(), buf + 10, sizeof(meta));
memcpy(meta.data(), buf + 10, meta.size());
return disk_size;
}
size_t write(uint8_t* buf) const {
memcpy(buf, &y, 5);
buf[4] = (off << 6) | (buf[4] & 0x3F);
buf[5] = off >> 2;
memcpy(buf + 6, &pos, 4);
memcpy(buf + 10, meta.data(), sizeof(meta));
memcpy(buf + 10, meta.data(), meta.size());
return disk_size;
}
#endif
};

typedef entry_xm<2> entry_2;
typedef entry_xm<4> entry_3;
typedef entry_xm<4> entry_4;
typedef entry_xm<3> entry_5;
typedef entry_xm<2> entry_6;
#ifdef CHIA_K34
typedef entry_xm<9> entry_2;
typedef entry_xm<17> entry_3;
typedef entry_xm<17> entry_4;
typedef entry_xm<13> entry_5;
typedef entry_xm<9> entry_6;
#else
typedef entry_xm<8> entry_2;
typedef entry_xm<16> entry_3;
typedef entry_xm<16> entry_4;
typedef entry_xm<12> entry_5;
typedef entry_xm<8> entry_6;
#endif

struct entry_7 {
uint32_t y; // 32 bit
uint32_t pos; // 32 bit
uintkx_t y; // 32 bit / 34 bit
uintkx_t pos; // 32 bit / 35 bit
uint16_t off; // 10 bit

#ifdef CHIA_K34
static constexpr size_t disk_size = 11;

void assign(const entry_7& entry) {
*this = entry;
}
size_t read(const uint8_t* buf) {
memcpy(&y, buf, 5);
y &= 0xFFFFFFFFF; // 36 bit
memcpy(&pos, buf + 4, 5);
pos >>= 4;
pos &= 0xFFFFFFFFF; // 36 bit
memcpy(&off, buf + 9, 2);
return disk_size;
}
size_t write(uint8_t* buf) const {
memcpy(buf, &y, 5);
const auto tmp = (pos << 4) | buf[4];
memcpy(buf + 4, &tmp, 5);
memcpy(buf + 9, &off, 2);
return disk_size;
}
#else
static constexpr size_t disk_size = 10;

void assign(const entry_7& entry) {
Expand All @@ -110,28 +169,32 @@ struct entry_7 {
memcpy(buf + 8, &off, 2);
return disk_size;
}
#endif
};

struct tmp_entry_1 {
uint32_t x; // 32 bit
uintkx_t x; // 32 bit / 34 bit

static constexpr size_t disk_size = 4;
static constexpr size_t disk_size = KBYTES;

void assign(const entry_1& entry) {
x = entry.x;
}
size_t read(const uint8_t* buf) {
memcpy(&x, buf, 4);
if(sizeof(x) > KBYTES) {
x = 0;
}
memcpy(&x, buf, KBYTES);
return disk_size;
}
size_t write(uint8_t* buf) const {
memcpy(buf, &x, 4);
memcpy(buf, &x, KBYTES);
return disk_size;
}
};

struct tmp_entry_x {
uint32_t pos; // 32 bit
uintkx_t pos; // 32 bit / 35 bit
uint16_t off; // 10 bit

static constexpr size_t disk_size = 6;
Expand All @@ -141,13 +204,19 @@ struct tmp_entry_x {
off = entry.off;
}
size_t read(const uint8_t* buf) {
memcpy(&pos, buf, 4);
memcpy(&pos, buf, KBYTES);
pos &= 0x3FFFFFFFFF; // 38 bit
memcpy(&off, buf + 4, 2);
off >>= 6;
return disk_size;
}
size_t write(uint8_t* buf) const {
memcpy(buf, &pos, 4);
memcpy(buf + 4, &off, 2);
memcpy(buf, &pos, KBYTES);
if(KBYTES < 5) {
buf[4] = 0;
}
const auto tmp = (off << 6) | buf[4];
memcpy(buf + 4, &tmp, 2);
return disk_size;
}
};
Expand All @@ -161,30 +230,26 @@ struct get_y {

template<typename T>
struct get_meta {
void operator()(const T& entry, uint128_t* value) {
*value = 0;
memcpy(value, entry.meta.data(), sizeof(entry.meta));
void operator()(const T& entry, uint64_t* bytes, const int k) {
memcpy(bytes, entry.meta.data(), entry.meta.size());
}
};

template<>
struct get_meta<entry_1> {
void operator()(const entry_1& entry, uint128_t* value) {
*value = entry.x;
}
void operator()(const entry_1& entry, uint64_t* bytes, const int k);
};

template<typename T>
struct set_meta {
void operator()(T& entry, const uint128_t value, const size_t num_bytes) {
entry.meta = {};
memcpy(entry.meta.data(), &value, num_bytes);
void operator()(T& entry, const uint64_t* bytes, const size_t num_bytes) {
memcpy(entry.meta.data(), bytes, num_bytes);
}
};

template<>
struct set_meta<entry_7> {
void operator()(entry_7& entry, const uint128_t value, const size_t num_bytes) {
void operator()(entry_7& entry, const uint64_t* bytes, const size_t num_bytes) {
// no meta data
}
};
Expand All @@ -193,7 +258,7 @@ template<typename T>
struct match_t {
T left;
T right;
uint32_t pos = 0;
uintkx_t pos = 0;
uint16_t off = 0;
};

Expand Down

0 comments on commit 8332d62

Please sign in to comment.