Skip to content

Commit

Permalink
Added get_aux_slot
Browse files Browse the repository at this point in the history
  • Loading branch information
SChernykh committed Oct 24, 2023
1 parent da45871 commit d42e7a9
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 deletions.
8 changes: 8 additions & 0 deletions external/src/crypto/sha256.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#ifndef SHA256_H
#define SHA256_H

#ifdef __cplusplus
extern "C" {
#endif

/*************************** HEADER FILES ***************************/
#include <stdint.h>

Expand All @@ -30,4 +34,8 @@ void sha256_final(SHA256_CTX *ctx, uint8_t* hash);

void sha256(const void* data, uint32_t len, uint8_t* hash);

#ifdef __cplusplus
}
#endif

#endif // SHA256_H
21 changes: 21 additions & 0 deletions src/merkle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "keccak.h"
#include "merkle.h"
#include "keccak.h"
#include "sha256.h"

namespace p2pool {

Expand Down Expand Up @@ -271,4 +272,24 @@ bool verify_merkle_proof(hash h, const std::vector<hash>& proof, size_t index, s
return (h == root);
}

uint32_t get_aux_slot(const hash &id, uint32_t nonce, uint32_t n_aux_chains)
{
if (n_aux_chains <= 1) {
return 0;
}

constexpr uint8_t HASH_KEY_MM_SLOT = 'm';

uint8_t buf[HASH_SIZE + sizeof(uint32_t) + 1];

memcpy(buf, &id, HASH_SIZE);
memcpy(buf + HASH_SIZE, &nonce, sizeof(uint32_t));
buf[HASH_SIZE + sizeof(uint32_t)] = HASH_KEY_MM_SLOT;

hash res;
sha256(buf, sizeof(buf), res.h);

return *reinterpret_cast<uint32_t*>(res.h) % n_aux_chains;
}

} // namespace p2pool
3 changes: 3 additions & 0 deletions src/merkle.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ namespace p2pool {

void merkle_hash(const std::vector<hash>& hashes, hash& root);
void merkle_hash_full_tree(const std::vector<hash>& hashes, std::vector<std::vector<hash>>& tree);

bool get_merkle_proof(const std::vector<std::vector<hash>>& tree, const hash& h, std::vector<std::pair<bool, hash>>& proof);
bool verify_merkle_proof(hash h, const std::vector<std::pair<bool, hash>>& proof, const hash& root);
bool verify_merkle_proof(hash h, const std::vector<hash>& proof, size_t index, size_t count, const hash& root);

uint32_t get_aux_slot(const hash &id, uint32_t nonce, uint32_t n_aux_chains);

} // namespace p2pool
2 changes: 1 addition & 1 deletion tests/src/check_win7.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@
count += 1
api = line[29:].strip()
if api not in allowedAPIs:
print('API call "{}" is not checked for Windows 7 compatibility. Check it and then add it to the list in tests/src/check_imports.py'.format(api))
print('API call "{}" is not checked for Windows 7 compatibility. Check it and then add it to the list in tests/src/check_win7.py'.format(api))
exit_code = 1

if exit_code == 0:
Expand Down
19 changes: 19 additions & 0 deletions tests/src/merkle_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,23 @@ TEST(merkle, tree)
check_full_tree();
}

TEST(merkle, aux_slot)
{
hash id;

ASSERT_EQ(get_aux_slot(id, 0, 0), 0U);
ASSERT_EQ(get_aux_slot(id, 0, 1), 0U);
ASSERT_EQ(get_aux_slot(id, 0, 2), 0U);
ASSERT_EQ(get_aux_slot(id, 0, 3), 0U);
ASSERT_EQ(get_aux_slot(id, 0, 4), 0U);
ASSERT_EQ(get_aux_slot(id, 0, 5), 1U);
ASSERT_EQ(get_aux_slot(id, 0, 6), 0U);
ASSERT_EQ(get_aux_slot(id, 0, 7), 5U);
ASSERT_EQ(get_aux_slot(id, 0, 8), 0U);
ASSERT_EQ(get_aux_slot(id, 0, 9), 6U);

ASSERT_EQ(get_aux_slot(id, 0, std::numeric_limits<uint32_t>::max()), 2389612776U);
ASSERT_EQ(get_aux_slot(id, 1, std::numeric_limits<uint32_t>::max()), 1080669337U);
}

}
7 changes: 2 additions & 5 deletions tests/src/sha256_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@
*/

#include "common.h"
#include "gtest/gtest.h"

extern "C" {
#include "sha256.h"
}
#include "gtest/gtest.h"

namespace p2pool {

TEST(sha256, hashing)
{
auto check = [](const char* input, const char* output) {
hash h;
sha256(input, strlen(input), h.h);
sha256(input, static_cast<uint32_t>(strlen(input)), h.h);

char buf[128];
log::Stream s(buf);
Expand Down

0 comments on commit d42e7a9

Please sign in to comment.