Skip to content

Commit

Permalink
Merge pull request #207 from chfast/remove_progpow
Browse files Browse the repository at this point in the history
Remove deprecated ProgPoW implementation
  • Loading branch information
chfast authored Nov 9, 2021
2 parents 55440f9 + f9c5fba commit 36be977
Show file tree
Hide file tree
Showing 20 changed files with 23 additions and 1,038 deletions.
20 changes: 8 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![readme style standard](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)

> C/C++ implementation of Ethash and ProgPoW – the Ethereum Proof of Work algorithms
> C/C++ implementation of Ethash – the Ethereum Proof of Work algorithm

## Table of Contents
Expand Down Expand Up @@ -31,20 +31,16 @@ cmake --build .
See [ethash.hpp] for list of exported function and documentation.


## Test vectors

- [ProgPoW test vectors](test/unittests/progpow_test_vectors.hpp)


## Optimizations

This section decscribes the optimizations, modification and tweaks applied
This section describes the optimizations, modification and tweaks applied
in this library in relation to [Ethash reference implementation].

The library contains a set of micro-benchmarks. Build and run `bench` tool.
The library contains a set of micro-benchmarks.Build and run the `ethash-bench`
tool.

### Seed hash is computed on the fly.

Seed hash is sequence of keccak256 hashes applied the epoch number of times.
Time needed to compute seed hash is negligible comparing to time needed to build
light cache. Computing seed hash for epoch 10000 takes ~ 5 ms, building light
Expand All @@ -55,9 +51,9 @@ cache for epoch 1 takes ~ 500 ms.
Computing the size of full dataset and light cache requires finding the largest
prime number given an upper bound. For similar reasons as with seed hash, this
is computed on the fly. The procedure used is quite naive and forks well only
up to 40-bit number, so some additional improvement can be done in future.
up to 40-bit number, so some additional improvement can be done in the future.


## Maintainer

Paweł Bylica [@chfast]
Expand Down
1 change: 0 additions & 1 deletion include/ethash/ethash.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ 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;
};

Expand Down
9 changes: 0 additions & 9 deletions include/ethash/hash_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ union ethash_hash1024
char str[128];
};

union ethash_hash2048
{
union ethash_hash512 hash512s[4];
uint64_t word64s[32];
uint32_t word32s[64];
uint8_t bytes[256];
char str[256];
};

#ifdef __cplusplus
}
#endif
1 change: 0 additions & 1 deletion include/ethash/hash_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ namespace ethash
using hash256 = ethash_hash256;
using hash512 = ethash_hash512;
using hash1024 = ethash_hash1024;
using hash2048 = ethash_hash2048;
} // namespace ethash
50 changes: 0 additions & 50 deletions include/ethash/progpow.hpp

This file was deleted.

4 changes: 0 additions & 4 deletions lib/ethash/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@ set_target_properties(ethash PROPERTIES C_EXTENSIONS OFF CXX_EXTENSIONS OFF)
target_link_libraries(ethash PRIVATE ethash::keccak)
target_include_directories(ethash PUBLIC $<BUILD_INTERFACE:${include_dir}>$<INSTALL_INTERFACE:include>)
target_sources(ethash PRIVATE
bit_manipulation.h
builtins.h
endianness.hpp
${include_dir}/ethash/ethash.h
${include_dir}/ethash/ethash.hpp
ethash-internal.hpp
ethash.cpp
${include_dir}/ethash/hash_types.h
kiss99.hpp
primes.h
primes.c
${include_dir}/ethash/progpow.hpp
progpow.cpp
)


Expand Down
81 changes: 0 additions & 81 deletions lib/ethash/bit_manipulation.h

This file was deleted.

5 changes: 2 additions & 3 deletions lib/ethash/ethash-internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ extern "C" struct ethash_epoch_context_full : ethash_epoch_context
ethash_hash1024* full_dataset;

constexpr ethash_epoch_context_full(int epoch, int light_num_items, const ethash_hash512* light,
const uint32_t* l1, int dataset_num_items, ethash_hash1024* dataset) noexcept
: ethash_epoch_context{epoch, light_num_items, light, l1, dataset_num_items},
int dataset_num_items, ethash_hash1024* dataset) noexcept
: ethash_epoch_context{epoch, light_num_items, light, dataset_num_items},
full_dataset{dataset}
{}
};
Expand Down Expand Up @@ -51,7 +51,6 @@ void build_light_cache(hash512 cache[], int num_items, const hash256& seed) noex

hash512 calculate_dataset_item_512(const epoch_context& context, int64_t index) noexcept;
hash1024 calculate_dataset_item_1024(const epoch_context& context, uint32_t index) noexcept;
hash2048 calculate_dataset_item_2048(const epoch_context& context, uint32_t index) noexcept;

namespace generic
{
Expand Down
44 changes: 13 additions & 31 deletions lib/ethash/ethash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

#include "ethash-internal.hpp"

#include "bit_manipulation.h"
#include "primes.h"
#include <ethash/keccak.hpp>
#include <ethash/progpow.hpp>
#include <cstdlib>
#include <cstring>

Expand All @@ -30,7 +28,15 @@ static_assert(full_dataset_item_size == ETHASH_FULL_DATASET_ITEM_SIZE, "");

namespace
{
using ::fnv1;
/// The core transformation of the FNV-1 hash function.
/// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1_hash.
NO_SANITIZE("unsigned-integer-overflow")
inline uint32_t fnv1(uint32_t u, uint32_t v) noexcept
{
static const uint32_t fnv_prime = 0x01000193;
return (u * fnv_prime) ^ v;
}


inline hash512 fnv1(const hash512& u, const hash512& v) noexcept
{
Expand Down Expand Up @@ -133,8 +139,7 @@ epoch_context_full* create_epoch_context(
const int full_dataset_num_items = calculate_full_dataset_num_items(epoch_number);
const size_t light_cache_size = get_light_cache_size(light_cache_num_items);
const size_t full_dataset_size =
full ? static_cast<size_t>(full_dataset_num_items) * sizeof(hash1024) :
progpow::l1_cache_size;
full ? static_cast<size_t>(full_dataset_num_items) * sizeof(hash1024) : 0;

const size_t alloc_size = context_alloc_size + light_cache_size + full_dataset_size;

Expand All @@ -146,23 +151,18 @@ epoch_context_full* create_epoch_context(
const hash256 epoch_seed = calculate_epoch_seed(epoch_number);
build_fn(light_cache, light_cache_num_items, epoch_seed);

uint32_t* const l1_cache =
reinterpret_cast<uint32_t*>(alloc_data + context_alloc_size + light_cache_size);

hash1024* full_dataset = full ? reinterpret_cast<hash1024*>(l1_cache) : nullptr;
hash1024* const full_dataset =
full ? reinterpret_cast<hash1024*>(alloc_data + context_alloc_size + light_cache_size) :
nullptr;

epoch_context_full* const context = new (alloc_data) epoch_context_full{
epoch_number,
light_cache_num_items,
light_cache,
l1_cache,
full_dataset_num_items,
full_dataset,
};

auto* full_dataset_2048 = reinterpret_cast<hash2048*>(l1_cache);
for (uint32_t i = 0; i < progpow::l1_cache_size / sizeof(full_dataset_2048[0]); ++i)
full_dataset_2048[i] = calculate_dataset_item_2048(*context, i);
return context;
}
} // namespace generic
Expand Down Expand Up @@ -227,24 +227,6 @@ hash1024 calculate_dataset_item_1024(const epoch_context& context, uint32_t inde
return hash1024{{item0.final(), item1.final()}};
}

hash2048 calculate_dataset_item_2048(const epoch_context& context, uint32_t index) noexcept
{
item_state item0{context, int64_t(index) * 4};
item_state item1{context, int64_t(index) * 4 + 1};
item_state item2{context, int64_t(index) * 4 + 2};
item_state item3{context, int64_t(index) * 4 + 3};

for (uint32_t j = 0; j < full_dataset_item_parents; ++j)
{
item0.update(j);
item1.update(j);
item2.update(j);
item3.update(j);
}

return hash2048{{item0.final(), item1.final(), item2.final(), item3.final()}};
}

namespace
{
using lookup_fn = hash1024 (*)(const epoch_context&, uint32_t);
Expand Down
Loading

0 comments on commit 36be977

Please sign in to comment.