This commit is contained in:
Dobromir Popov
2025-09-07 15:03:47 +03:00
parent 00cda24e71
commit 2d2653551b
132 changed files with 34281 additions and 5 deletions

View File

@@ -0,0 +1,99 @@
/* ethash: C/C++ implementation of Ethash, the Ethereum Proof of Work algorithm.
* Copyright 2018 Pawel Bylica.
* Licensed under the Apache License, Version 2.0. See the LICENSE file.
*/
#pragma once
#include <ethash/hash_types.h>
#include <stdint.h>
#ifdef __cplusplus
#define NOEXCEPT noexcept
#else
#define NOEXCEPT
#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
* The Ethash algorithm revision implemented as specified in the Ethash spec
* https://github.com/ethereum/wiki/wiki/Ethash.
*/
#define ETHASH_REVISION "23"
#define ETHASH_EPOCH_LENGTH 30000
#define ETHASH_LIGHT_CACHE_ITEM_SIZE 64
#define ETHASH_FULL_DATASET_ITEM_SIZE 128
#define ETHASH_NUM_DATASET_ACCESSES 64
struct ethash_epoch_context
{
const int epoch_number;
const int light_cache_num_items;
const union ethash_hash512* const light_cache;
const uint32_t* const l1_cache;
const int full_dataset_num_items;
};
struct ethash_epoch_context_full;
/**
* Calculates the number of items in the light cache for given epoch.
*
* This function will search for a prime number matching the criteria given
* by the Ethash so the execution time is not constant. It takes ~ 0.01 ms.
*
* @param epoch_number The epoch number.
* @return The number items in the light cache.
*/
int ethash_calculate_light_cache_num_items(int epoch_number) NOEXCEPT;
/**
* Calculates the number of items in the full dataset for given epoch.
*
* This function will search for a prime number matching the criteria given
* by the Ethash so the execution time is not constant. It takes ~ 0.05 ms.
*
* @param epoch_number The epoch number.
* @return The number items in the full dataset.
*/
int ethash_calculate_full_dataset_num_items(int epoch_number) NOEXCEPT;
/**
* Calculates the epoch seed hash.
* @param epoch_number The epoch number.
* @return The epoch seed hash.
*/
union ethash_hash256 ethash_calculate_epoch_seed(int epoch_number) NOEXCEPT;
struct ethash_epoch_context* ethash_create_epoch_context(int epoch_number) NOEXCEPT;
/**
* Creates the epoch context with the full dataset initialized.
*
* The memory for the full dataset is only allocated and marked as "not-generated".
* The items of the full dataset are generated on the fly when hit for the first time.
*
* The memory allocated in the context MUST be freed with ethash_destroy_epoch_context_full().
*
* @param epoch_number The epoch number.
* @return Pointer to the context or null in case of memory allocation failure.
*/
struct ethash_epoch_context_full* ethash_create_epoch_context_full(int epoch_number) NOEXCEPT;
void ethash_destroy_epoch_context(struct ethash_epoch_context* context) NOEXCEPT;
void ethash_destroy_epoch_context_full(struct ethash_epoch_context_full* context) NOEXCEPT;
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,160 @@
// ethash: C/C++ implementation of Ethash, the Ethereum Proof of Work algorithm.
// Copyright 2018 Pawel Bylica.
// Licensed under the Apache License, Version 2.0. See the LICENSE file.
/// @file
///
/// API design decisions:
///
/// 1. Signed integer type is used whenever the size of the type is not
/// restricted by the Ethash specification.
/// See http://www.aristeia.com/Papers/C++ReportColumns/sep95.pdf.
/// See https://stackoverflow.com/questions/10168079/why-is-size-t-unsigned/.
/// See https://github.com/Microsoft/GSL/issues/171.
#pragma once
#include <ethash/ethash.h>
#include <ethash/hash_types.hpp>
#include <cstdint>
#include <cstring>
#include <memory>
namespace ethash
{
constexpr auto revision = ETHASH_REVISION;
static constexpr int epoch_length = ETHASH_EPOCH_LENGTH;
static constexpr int light_cache_item_size = ETHASH_LIGHT_CACHE_ITEM_SIZE;
static constexpr int full_dataset_item_size = ETHASH_FULL_DATASET_ITEM_SIZE;
static constexpr int num_dataset_accesses = ETHASH_NUM_DATASET_ACCESSES;
using epoch_context = ethash_epoch_context;
using epoch_context_full = ethash_epoch_context_full;
/// Constructs a 256-bit hash from an array of bytes.
///
/// @param bytes A pointer to array of at least 32 bytes.
/// @return The constructed hash.
inline hash256 hash256_from_bytes(const uint8_t bytes[32]) noexcept
{
hash256 h;
std::memcpy(&h, bytes, sizeof(h));
return h;
}
struct result
{
hash256 final_hash;
hash256 mix_hash;
};
struct search_result
{
bool solution_found = false;
uint64_t nonce = 0;
hash256 final_hash = {};
hash256 mix_hash = {};
search_result() noexcept = default;
search_result(result res, uint64_t nonce) noexcept
: solution_found(true), nonce(nonce), final_hash(res.final_hash), mix_hash(res.mix_hash)
{}
};
/// Alias for ethash_calculate_light_cache_num_items().
static constexpr auto calculate_light_cache_num_items = ethash_calculate_light_cache_num_items;
/// Alias for ethash_calculate_full_dataset_num_items().
static constexpr auto calculate_full_dataset_num_items = ethash_calculate_full_dataset_num_items;
/// Alias for ethash_calculate_epoch_seed().
static constexpr auto calculate_epoch_seed = ethash_calculate_epoch_seed;
/// Calculates the epoch number out of the block number.
inline constexpr int get_epoch_number(int block_number) noexcept
{
return block_number / epoch_length;
}
/**
* Coverts the number of items of a light cache to size in bytes.
*
* @param num_items The number of items in the light cache.
* @return The size of the light cache in bytes.
*/
inline constexpr size_t get_light_cache_size(int num_items) noexcept
{
return static_cast<size_t>(num_items) * light_cache_item_size;
}
/**
* Coverts the number of items of a full dataset to size in bytes.
*
* @param num_items The number of items in the full dataset.
* @return The size of the full dataset in bytes.
*/
inline constexpr uint64_t get_full_dataset_size(int num_items) noexcept
{
return static_cast<uint64_t>(num_items) * full_dataset_item_size;
}
/// Owned unique pointer to an epoch context.
using epoch_context_ptr = std::unique_ptr<epoch_context, decltype(&ethash_destroy_epoch_context)>;
using epoch_context_full_ptr =
std::unique_ptr<epoch_context_full, decltype(&ethash_destroy_epoch_context_full)>;
/// Creates Ethash epoch context.
///
/// This is a wrapper for ethash_create_epoch_number C function that returns
/// the context as a smart pointer which handles the destruction of the context.
inline epoch_context_ptr create_epoch_context(int epoch_number) noexcept
{
return {ethash_create_epoch_context(epoch_number), ethash_destroy_epoch_context};
}
inline epoch_context_full_ptr create_epoch_context_full(int epoch_number) noexcept
{
return {ethash_create_epoch_context_full(epoch_number), ethash_destroy_epoch_context_full};
}
result hash(const epoch_context& context, const hash256& header_hash, uint64_t nonce) noexcept;
result hash(const epoch_context_full& context, const hash256& header_hash, uint64_t nonce) noexcept;
bool verify_final_hash(const hash256& header_hash, const hash256& mix_hash, uint64_t nonce,
const hash256& boundary) noexcept;
bool verify(const epoch_context& context, const hash256& header_hash, const hash256& mix_hash,
uint64_t nonce, const hash256& boundary) noexcept;
search_result search_light(const epoch_context& context, const hash256& header_hash,
const hash256& boundary, uint64_t start_nonce, size_t iterations) noexcept;
search_result search(const epoch_context_full& context, const hash256& header_hash,
const hash256& boundary, uint64_t start_nonce, size_t iterations) noexcept;
/// Tries to find the epoch number matching the given seed hash.
///
/// Mining pool protocols (many variants of stratum and "getwork") send out
/// seed hash instead of epoch number to workers. This function tries to recover
/// the epoch number from this seed hash.
///
/// @param seed Ethash seed hash.
/// @return The epoch number or -1 if not found.
int find_epoch_number(const hash256& seed) noexcept;
/// Get global shared epoch context.
const epoch_context& get_global_epoch_context(int epoch_number);
/// Get global shared epoch context with full dataset initialized.
const epoch_context_full& get_global_epoch_context_full(int epoch_number);
} // namespace ethash

View File

@@ -0,0 +1,46 @@
/* ethash: C/C++ implementation of Ethash, the Ethereum Proof of Work algorithm.
* Copyright 2018 Pawel Bylica.
* Licensed under the Apache License, Version 2.0. See the LICENSE file.
*/
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
union ethash_hash256
{
uint64_t word64s[4];
uint32_t word32s[8];
uint8_t bytes[32];
};
union ethash_hash512
{
uint64_t word64s[8];
uint32_t word32s[16];
uint8_t bytes[64];
};
union ethash_hash1024
{
union ethash_hash512 hash512s[2];
uint64_t word64s[16];
uint32_t word32s[32];
uint8_t bytes[128];
};
union ethash_hash2048
{
union ethash_hash512 hash512s[4];
uint64_t word64s[32];
uint32_t word32s[64];
uint8_t bytes[256];
};
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,15 @@
// ethash: C/C++ implementation of Ethash, the Ethereum Proof of Work algorithm.
// Copyright 2018 Pawel Bylica.
// Licensed under the Apache License, Version 2.0. See the LICENSE file.
#pragma once
#include <ethash/hash_types.h>
namespace ethash
{
using hash256 = ethash_hash256;
using hash512 = ethash_hash512;
using hash1024 = ethash_hash1024;
using hash2048 = ethash_hash2048;
} // namespace ethash

View File

@@ -0,0 +1,49 @@
/* ethash: C/C++ implementation of Ethash, the Ethereum Proof of Work algorithm.
* Copyright 2018 Pawel Bylica.
* Licensed under the Apache License, Version 2.0. See the LICENSE file.
*/
#pragma once
#include <ethash/hash_types.h>
#include <stddef.h>
#ifdef __cplusplus
#define NOEXCEPT noexcept
#else
#define NOEXCEPT
#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
* The Keccak-f[1600] function.
*
* The implementation of the Keccak-f function with 1600-bit width of the permutation (b).
* The size of the state is also 1600 bit what gives 25 64-bit words.
*
* @param state The state of 25 64-bit words on which the permutation is to be performed.
*/
void ethash_keccakf1600(uint64_t state[25]) NOEXCEPT;
/**
* The Keccak-f[800] function.
*
* The implementation of the Keccak-f function with 800-bit width of the permutation (b).
* The size of the state is also 800 bit what gives 25 32-bit words.
*
* @param state The state of 25 32-bit words on which the permutation is to be performed.
*/
void ethash_keccakf800(uint32_t state[25]) NOEXCEPT;
union ethash_hash256 ethash_keccak256(const uint8_t* data, size_t size) NOEXCEPT;
union ethash_hash256 ethash_keccak256_32(const uint8_t data[32]) NOEXCEPT;
union ethash_hash512 ethash_keccak512(const uint8_t* data, size_t size) NOEXCEPT;
union ethash_hash512 ethash_keccak512_64(const uint8_t data[64]) NOEXCEPT;
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,35 @@
// ethash: C/C++ implementation of Ethash, the Ethereum Proof of Work algorithm.
// Copyright 2018 Pawel Bylica.
// Licensed under the Apache License, Version 2.0. See the LICENSE file.
#pragma once
#include <ethash/keccak.h>
#include <ethash/hash_types.hpp>
namespace ethash
{
inline hash256 keccak256(const uint8_t* data, size_t size) noexcept
{
return ethash_keccak256(data, size);
}
inline hash256 keccak256(const hash256& input) noexcept
{
return ethash_keccak256_32(input.bytes);
}
inline hash512 keccak512(const uint8_t* data, size_t size) noexcept
{
return ethash_keccak512(data, size);
}
inline hash512 keccak512(const hash512& input) noexcept
{
return ethash_keccak512_64(input.bytes);
}
static constexpr auto keccak256_32 = ethash_keccak256_32;
static constexpr auto keccak512_64 = ethash_keccak512_64;
} // namespace ethash

View File

@@ -0,0 +1,47 @@
// ethash: C/C++ implementation of Ethash, the Ethereum Proof of Work algorithm.
// Copyright 2018 Pawel Bylica.
// Licensed under the Apache License, Version 2.0. See the LICENSE file.
/// @file
///
/// ProgPoW API
///
/// This file provides the public API for ProgPoW as the Ethash API extension.
#include <ethash/ethash.hpp>
namespace progpow
{
using namespace ethash; // Include ethash namespace.
/// The ProgPoW algorithm revision implemented as specified in the spec
/// https://github.com/ifdefelse/ProgPOW#change-history.
constexpr auto revision = "0.9.2";
constexpr int period_length = 50;
constexpr uint32_t num_regs = 32;
constexpr size_t num_lanes = 16;
constexpr int num_cache_accesses = 12;
constexpr int num_math_operations = 20;
constexpr size_t l1_cache_size = 16 * 1024;
constexpr size_t l1_cache_num_items = l1_cache_size / sizeof(uint32_t);
result hash(const epoch_context& context, int block_number, const hash256& header_hash,
uint64_t nonce) noexcept;
result hash(const epoch_context_full& context, int block_number, const hash256& header_hash,
uint64_t nonce) noexcept;
bool verify(const epoch_context& context, int block_number, const hash256& header_hash,
const hash256& mix_hash, uint64_t nonce, const hash256& boundary) noexcept;
search_result search_light(const epoch_context& context, int block_number,
const hash256& header_hash, const hash256& boundary, uint64_t start_nonce,
size_t iterations) noexcept;
search_result search(const epoch_context_full& context, int block_number,
const hash256& header_hash, const hash256& boundary, uint64_t start_nonce,
size_t iterations) noexcept;
} // namespace progpow

View File

@@ -0,0 +1,18 @@
/* ethash: C/C++ implementation of Ethash, the Ethereum Proof of Work algorithm.
* Copyright 2019 Pawel Bylica.
* Licensed under the Apache License, Version 2.0.
*/
#pragma once
/** The ethash library version. */
#define ETHASH_VERSION "0.4.3"
#ifdef __cplusplus
namespace ethash
{
/// The ethash library version.
constexpr auto version = ETHASH_VERSION;
} // namespace ethash
#endif