From 0a313c8569f1b581b52acd4cbc7b6c78f4601cf2 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Sat, 21 Oct 2023 19:00:30 +0200 Subject: [PATCH] Moved out merkle tree hash code --- CMakeLists.txt | 2 + src/merkle.cpp | 62 +++++++++++++++++++++++++++++ src/merkle.h | 24 +++++++++++ src/pool_block.cpp | 36 ++--------------- tests/CMakeLists.txt | 2 + tests/src/merkle_tests.cpp | 81 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 174 insertions(+), 33 deletions(-) create mode 100644 src/merkle.cpp create mode 100644 src/merkle.h create mode 100644 tests/src/merkle_tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b9e1db3..b285e16b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,6 +76,7 @@ set(HEADERS src/keccak.h src/log.h src/mempool.h + src/merkle.h src/p2p_server.h src/p2pool.h src/p2pool_api.h @@ -106,6 +107,7 @@ set(SOURCES src/main.cpp src/memory_leak_debug.cpp src/mempool.cpp + src/merkle.cpp src/p2p_server.cpp src/p2pool.cpp src/p2pool_api.cpp diff --git a/src/merkle.cpp b/src/merkle.cpp new file mode 100644 index 00000000..df5f8ef5 --- /dev/null +++ b/src/merkle.cpp @@ -0,0 +1,62 @@ +/* + * This file is part of the Monero P2Pool + * Copyright (c) 2021-2023 SChernykh + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "common.h" +#include "keccak.h" +#include "merkle.h" + +namespace p2pool { + +void merkle_hash(const std::vector& hashes, hash& root) +{ + const size_t count = hashes.size(); + const uint8_t* h = hashes[0].h; + + if (count == 1) { + root = hashes[0]; + } + else if (count == 2) { + keccak(h, HASH_SIZE * 2, root.h); + } + else { + size_t cnt = 1; + do { cnt <<= 1; } while (cnt <= count); + cnt >>= 1; + + std::vector tmp_ints(cnt); + + const size_t k = cnt * 2 - count; + memcpy(tmp_ints.data(), h, k * HASH_SIZE); + + for (size_t i = k, j = k; j < cnt; i += 2, ++j) { + keccak(h + i * HASH_SIZE, HASH_SIZE * 2, tmp_ints[j].h); + } + + while (cnt > 2) { + cnt >>= 1; + for (size_t i = 0, j = 0; j < cnt; i += 2, ++j) { + keccak(tmp_ints[i].h, HASH_SIZE * 2, tmp_ints[j].h); + } + } + + keccak(tmp_ints[0].h, HASH_SIZE * 2, root.h); + } +} + +} // namespace p2pool diff --git a/src/merkle.h b/src/merkle.h new file mode 100644 index 00000000..1bf6f8db --- /dev/null +++ b/src/merkle.h @@ -0,0 +1,24 @@ +/* + * This file is part of the Monero P2Pool + * Copyright (c) 2021-2023 SChernykh + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +namespace p2pool { + +void merkle_hash(const std::vector& hashes, hash& root); + +} // namespace p2pool diff --git a/src/pool_block.cpp b/src/pool_block.cpp index 24089f32..75d3a44c 100644 --- a/src/pool_block.cpp +++ b/src/pool_block.cpp @@ -21,6 +21,7 @@ #include "side_chain.h" #include "pow_hash.h" #include "crypto.h" +#include "merkle.h" LOG_CATEGORY(PoolBlock) @@ -311,39 +312,8 @@ bool PoolBlock::get_pow_hash(RandomX_Hasher_Base* hasher, uint64_t height, const keccak(reinterpret_cast(hashes), HASH_SIZE * 3, tmp.h); memcpy(h, tmp.h, HASH_SIZE); - if (count == 1) { - memcpy(blob + blob_size, h, HASH_SIZE); - } - else if (count == 2) { - keccak(h, HASH_SIZE * 2, tmp.h); - memcpy(blob + blob_size, tmp.h, HASH_SIZE); - } - else { - size_t i, j, cnt; - - for (i = 0, cnt = 1; cnt <= count; ++i, cnt <<= 1) {} - - cnt >>= 1; - - std::vector tmp_ints(cnt * HASH_SIZE); - memcpy(tmp_ints.data(), h, (cnt * 2 - count) * HASH_SIZE); - - for (i = cnt * 2 - count, j = cnt * 2 - count; j < cnt; i += 2, ++j) { - keccak(h + i * HASH_SIZE, HASH_SIZE * 2, tmp.h); - memcpy(tmp_ints.data() + j * HASH_SIZE, tmp.h, HASH_SIZE); - } - - while (cnt > 2) { - cnt >>= 1; - for (i = 0, j = 0; j < cnt; i += 2, ++j) { - keccak(tmp_ints.data() + i * HASH_SIZE, HASH_SIZE * 2, tmp.h); - memcpy(tmp_ints.data() + j * HASH_SIZE, tmp.h, HASH_SIZE); - } - } - - keccak(tmp_ints.data(), HASH_SIZE * 2, tmp.h); - memcpy(blob + blob_size, tmp.h, HASH_SIZE); - } + merkle_hash(m_transactions, tmp); + memcpy(blob + blob_size, tmp.h, HASH_SIZE); } blob_size += HASH_SIZE; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 41d7f545..c787b6f6 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -33,6 +33,7 @@ set(SOURCES src/difficulty_type_tests.cpp src/hash_tests.cpp src/keccak_tests.cpp + src/merkle_tests.cpp src/log_tests.cpp src/main.cpp src/pool_block_tests.cpp @@ -50,6 +51,7 @@ set(SOURCES ../src/log.cpp ../src/memory_leak_debug.cpp ../src/mempool.cpp + ../src/merkle.cpp ../src/miner.cpp ../src/p2p_server.cpp ../src/p2pool.cpp diff --git a/tests/src/merkle_tests.cpp b/tests/src/merkle_tests.cpp new file mode 100644 index 00000000..8ef709fc --- /dev/null +++ b/tests/src/merkle_tests.cpp @@ -0,0 +1,81 @@ +/* + * This file is part of the Monero P2Pool + * Copyright (c) 2021-2023 SChernykh + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "common.h" +#include "keccak.h" +#include "merkle.h" +#include "gtest/gtest.h" + +namespace p2pool { + +TEST(merkle, root_hash) +{ + hash input[5]; + uint8_t data[] = "data 0"; + + for (size_t i = 0; i < 5; ++i, ++data[sizeof(data) - 2]) { + keccak(data, sizeof(data) - 1, input[i].h); + } + + hash root; + std::vector hashes(1, input[0]); + + // 1 leaf + merkle_hash(hashes, root); + ASSERT_EQ(root, input[0]); + + // 2 leaves + hashes.push_back(input[1]); + merkle_hash(hashes, root); + + hash check[8]; + keccak(input[0].h, HASH_SIZE * 2, check[0].h); + ASSERT_EQ(root, check[0]); + + // 3 leaves + hashes.push_back(input[2]); + merkle_hash(hashes, root); + + keccak(input[1].h, HASH_SIZE * 2, check[1].h); + check[0] = input[0]; + keccak(check[0].h, HASH_SIZE * 2, check[0].h); + ASSERT_EQ(root, check[0]); + + // 4 leaves + hashes.push_back(input[3]); + merkle_hash(hashes, root); + + keccak(input[0].h, HASH_SIZE * 2, check[0].h); + keccak(input[2].h, HASH_SIZE * 2, check[1].h); + keccak(check[0].h, HASH_SIZE * 2, check[0].h); + ASSERT_EQ(root, check[0]); + + // 5 leaves + hashes.push_back(input[4]); + merkle_hash(hashes, root); + + check[0] = input[0]; + check[1] = input[1]; + check[2] = input[2]; + keccak(input[3].h, HASH_SIZE * 2, check[3].h); + keccak(check[0].h, HASH_SIZE * 2, check[0].h); + keccak(check[2].h, HASH_SIZE * 2, check[1].h); + keccak(check[0].h, HASH_SIZE * 2, check[0].h); + ASSERT_EQ(root, check[0]); +} + +}