Skip to content

Commit

Permalink
test_pos_compute
Browse files Browse the repository at this point in the history
  • Loading branch information
madMAx43v3r committed Nov 5, 2023
1 parent 8e81676 commit a81ddb3
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 102 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ add_library(mmx_db SHARED

add_library(mmx_pos SHARED
src/pos/mem_hash.cpp
src/pos/verify.cpp
)

add_library(mmx_modules SHARED
Expand Down Expand Up @@ -237,7 +238,7 @@ target_include_directories(mmx_chiapos
target_link_libraries(mmx_iface uint256_t secp256k1 bech32 bls vnx_base vnx_addons)

target_link_libraries(mmx_db mmx_iface)

target_link_libraries(mmx_pos mmx_iface)
target_link_libraries(mmx_vm mmx_db mmx_iface)

target_link_libraries(mmx_modules mmx_chiapos mmx_pos mmx_vm mmx_db mmx_iface)
Expand Down
2 changes: 1 addition & 1 deletion include/mmx/pos/mem_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void gen_mem_array(uint32_t* mem, const uint8_t* key, const uint64_t mem_size);
/*
* M = log2 number of iterations
* mem = array of size (32 << B)
* hash = array of size 32
* hash = array of size 64
*/
void calc_mem_hash(uint32_t* mem, uint8_t* hash, const int M, const int B);

Expand Down
51 changes: 51 additions & 0 deletions include/mmx/pos/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@
#define INCLUDE_MMX_POS_UTIL_H_

#include <cstdint>
#include <algorithm>

// compiler-specific byte swap macros.
#if defined(_MSC_VER)
#include <cstdlib>
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/byteswap-uint64-byteswap-ulong-byteswap-ushort?view=msvc-160
inline uint16_t bswap_16(uint16_t x) { return _byteswap_ushort(x); }
inline uint32_t bswap_32(uint32_t x) { return _byteswap_ulong(x); }
inline uint64_t bswap_64(uint64_t x) { return _byteswap_uint64(x); }
#elif defined(__clang__) || defined(__GNUC__)
inline uint16_t bswap_16(uint16_t x) { return __builtin_bswap16(x); }
inline uint32_t bswap_32(uint32_t x) { return __builtin_bswap32(x); }
inline uint64_t bswap_64(uint64_t x) { return __builtin_bswap64(x); }
#else
#error "unknown compiler, don't know how to swap bytes"
#endif

#define MMXPOS_QUARTERROUND(a, b, c, d) \
a = a + b; \
Expand All @@ -32,6 +48,41 @@ inline uint64_t rotl_64(const uint64_t v, int bits) {
return (v << bits) | (v >> (64 - bits));
}

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


} // pos
} // mmx
Expand Down
8 changes: 3 additions & 5 deletions include/mmx/pos/verify.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
namespace mmx {
namespace pos {

std::vector<std::pair<uint64_t, bytes_t<48>>>
compute(const std::vector<uint64_t>& X_values, std::vector<uint64_t>* X_out,
std::vector<std::pair<uint32_t, bytes_t<48>>>
compute(const std::vector<uint32_t>& X_values, std::vector<uint32_t>* X_out,
const uint8_t* id, const int ksize, const int xbits);

hash_t verify(const std::vector<uint64_t>& X_values, const uint8_t* id, const hash_t& challenge, const int ksize);
hash_t verify(const std::vector<uint32_t>& X_values, const hash_t& challenge, const uint8_t* id, const int ksize);


} // pos
} // mmx



#endif /* INCLUDE_MMX_POS_VERIFY_H_ */
10 changes: 1 addition & 9 deletions src/pos/mem_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ void calc_mem_hash(uint32_t* mem, uint8_t* hash, const int M, const int B)
for(int i = 0; i < N; ++i) {
state[i] = mem[index_mask * N + i];
}
// std::map<uint32_t, uint32_t> count;

for(int k = 0; k < num_iter; ++k)
{
Expand All @@ -78,7 +77,6 @@ void calc_mem_hash(uint32_t* mem, uint8_t* hash, const int M, const int B)
const auto bits = tmp % 32;
// const auto offset = ((tmp >> 5) & index_mask) * N;
const auto offset = tmp & (index_mask << 5);
// count[offset]++;

for(int i = 0; i < N; ++i) {
const int shift = (k + i) % N;
Expand All @@ -89,13 +87,7 @@ void calc_mem_hash(uint32_t* mem, uint8_t* hash, const int M, const int B)
}
}

// for(const auto& entry : count) {
// if(entry.second > uint32_t(3 << (M - B))) {
// std::cout << "WARN [" << entry.first << "] " << entry.second << std::endl;
// }
// }

for(int i = 0; i < 8; ++i) {
for(int i = 0; i < 16; ++i) {
for(int k = 0; k < 4; ++k) {
hash[i * 4 + k] = state[i] >> (24 - k * 8);
}
Expand Down
Loading

0 comments on commit a81ddb3

Please sign in to comment.