You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
293 lines
8.0 KiB
293 lines
8.0 KiB
10 years ago
|
/*
|
||
10 years ago
|
This file is part of ethash.
|
||
10 years ago
|
|
||
10 years ago
|
ethash is free software: you can redistribute it and/or modify
|
||
10 years ago
|
it under the terms of the GNU General Public License as published by
|
||
|
the Free Software Foundation, either version 3 of the License, or
|
||
|
(at your option) any later version.
|
||
|
|
||
10 years ago
|
ethash is distributed in the hope that it will be useful,
|
||
10 years ago
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
10 years ago
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
10 years ago
|
GNU General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU General Public License
|
||
10 years ago
|
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||
10 years ago
|
*/
|
||
10 years ago
|
/** @file internal.c
|
||
10 years ago
|
* @author Tim Hughes <tim@twistedfury.com>
|
||
|
* @author Matthew Wampler-Doty
|
||
|
* @date 2015
|
||
|
*/
|
||
|
|
||
|
#include <assert.h>
|
||
|
#include <inttypes.h>
|
||
|
#include <stddef.h>
|
||
|
#include "ethash.h"
|
||
|
#include "fnv.h"
|
||
|
#include "endian.h"
|
||
|
#include "internal.h"
|
||
|
#include "data_sizes.h"
|
||
|
#include "sha3.h"
|
||
|
|
||
10 years ago
|
uint64_t ethash_get_datasize(uint64_t const block_number)
|
||
10 years ago
|
{
|
||
10 years ago
|
assert(block_number / ETHASH_EPOCH_LENGTH < 2048);
|
||
|
return dag_sizes[block_number / ETHASH_EPOCH_LENGTH];
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
uint64_t ethash_get_cachesize(uint64_t const block_number)
|
||
10 years ago
|
{
|
||
10 years ago
|
assert(block_number / ETHASH_EPOCH_LENGTH < 2048);
|
||
|
return cache_sizes[block_number / ETHASH_EPOCH_LENGTH];
|
||
10 years ago
|
}
|
||
|
|
||
|
// Follows Sergio's "STRICT MEMORY HARD HASHING FUNCTIONS" (2014)
|
||
|
// https://bitslog.files.wordpress.com/2013/12/memohash-v0-3.pdf
|
||
|
// SeqMemoHash(s, R, N)
|
||
10 years ago
|
bool static ethash_compute_cache_nodes(
|
||
|
node* const nodes,
|
||
10 years ago
|
uint64_t cache_size,
|
||
10 years ago
|
ethash_h256_t const* seed
|
||
|
)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (cache_size % sizeof(node) != 0) {
|
||
10 years ago
|
return false;
|
||
|
}
|
||
10 years ago
|
uint32_t const num_nodes = (uint32_t) (cache_size / sizeof(node));
|
||
10 years ago
|
|
||
|
SHA3_512(nodes[0].bytes, (uint8_t*)seed, 32);
|
||
|
|
||
10 years ago
|
for (uint32_t i = 1; i != num_nodes; ++i) {
|
||
10 years ago
|
SHA3_512(nodes[i].bytes, nodes[i - 1].bytes, 64);
|
||
|
}
|
||
|
|
||
10 years ago
|
for (uint32_t j = 0; j != ETHASH_CACHE_ROUNDS; j++) {
|
||
|
for (uint32_t i = 0; i != num_nodes; i++) {
|
||
10 years ago
|
uint32_t const idx = nodes[i].words[0] % num_nodes;
|
||
|
node data;
|
||
|
data = nodes[(num_nodes - 1 + i) % num_nodes];
|
||
10 years ago
|
for (uint32_t w = 0; w != NODE_WORDS; ++w) {
|
||
10 years ago
|
data.words[w] ^= nodes[idx].words[w];
|
||
|
}
|
||
|
SHA3_512(nodes[i].bytes, data.bytes, sizeof(data));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// now perform endian conversion
|
||
|
fix_endian_arr32(nodes->words, num_nodes * NODE_WORDS);
|
||
|
return true;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void ethash_calculate_dag_item(
|
||
|
node* const ret,
|
||
10 years ago
|
uint32_t node_index,
|
||
10 years ago
|
ethash_light_t const light
|
||
10 years ago
|
)
|
||
10 years ago
|
{
|
||
10 years ago
|
uint32_t num_parent_nodes = (uint32_t) (light->cache_size / sizeof(node));
|
||
|
node const* cache_nodes = (node const *) light->cache;
|
||
10 years ago
|
node const* init = &cache_nodes[node_index % num_parent_nodes];
|
||
10 years ago
|
memcpy(ret, init, sizeof(node));
|
||
|
ret->words[0] ^= node_index;
|
||
|
SHA3_512(ret->bytes, ret->bytes, sizeof(node));
|
||
10 years ago
|
#if defined(_M_X64) && ENABLE_SSE
|
||
10 years ago
|
__m128i const fnv_prime = _mm_set1_epi32(FNV_PRIME);
|
||
|
__m128i xmm0 = ret->xmm[0];
|
||
|
__m128i xmm1 = ret->xmm[1];
|
||
|
__m128i xmm2 = ret->xmm[2];
|
||
|
__m128i xmm3 = ret->xmm[3];
|
||
10 years ago
|
#endif
|
||
|
|
||
10 years ago
|
for (uint32_t i = 0; i != ETHASH_DATASET_PARENTS; ++i) {
|
||
10 years ago
|
uint32_t parent_index = fnv_hash(node_index ^ i, ret->words[i % NODE_WORDS]) % num_parent_nodes;
|
||
10 years ago
|
node const *parent = &cache_nodes[parent_index];
|
||
10 years ago
|
|
||
10 years ago
|
#if defined(_M_X64) && ENABLE_SSE
|
||
10 years ago
|
{
|
||
|
xmm0 = _mm_mullo_epi32(xmm0, fnv_prime);
|
||
|
xmm1 = _mm_mullo_epi32(xmm1, fnv_prime);
|
||
|
xmm2 = _mm_mullo_epi32(xmm2, fnv_prime);
|
||
|
xmm3 = _mm_mullo_epi32(xmm3, fnv_prime);
|
||
|
xmm0 = _mm_xor_si128(xmm0, parent->xmm[0]);
|
||
|
xmm1 = _mm_xor_si128(xmm1, parent->xmm[1]);
|
||
|
xmm2 = _mm_xor_si128(xmm2, parent->xmm[2]);
|
||
|
xmm3 = _mm_xor_si128(xmm3, parent->xmm[3]);
|
||
|
|
||
|
// have to write to ret as values are used to compute index
|
||
|
ret->xmm[0] = xmm0;
|
||
|
ret->xmm[1] = xmm1;
|
||
|
ret->xmm[2] = xmm2;
|
||
|
ret->xmm[3] = xmm3;
|
||
|
}
|
||
|
#else
|
||
|
{
|
||
|
for (unsigned w = 0; w != NODE_WORDS; ++w) {
|
||
|
ret->words[w] = fnv_hash(ret->words[w], parent->words[w]);
|
||
|
}
|
||
|
}
|
||
10 years ago
|
#endif
|
||
10 years ago
|
}
|
||
|
SHA3_512(ret->bytes, ret->bytes, sizeof(node));
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
static bool ethash_hash(
|
||
10 years ago
|
ethash_return_value_t* ret,
|
||
10 years ago
|
node const* full_nodes,
|
||
10 years ago
|
ethash_light_t const light,
|
||
10 years ago
|
uint64_t full_size,
|
||
10 years ago
|
ethash_h256_t const header_hash,
|
||
10 years ago
|
uint64_t const nonce
|
||
10 years ago
|
)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (full_size % MIX_WORDS != 0) {
|
||
10 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
// pack hash and nonce together into first 40 bytes of s_mix
|
||
|
assert(sizeof(node) * 8 == 512);
|
||
|
node s_mix[MIX_NODES + 1];
|
||
10 years ago
|
memcpy(s_mix[0].bytes, &header_hash, 32);
|
||
10 years ago
|
fix_endian64(s_mix[0].double_words[4], nonce);
|
||
|
|
||
|
// compute sha3-512 hash and replicate across mix
|
||
|
SHA3_512(s_mix->bytes, s_mix->bytes, 40);
|
||
|
fix_endian_arr32(s_mix[0].words, 16);
|
||
|
|
||
10 years ago
|
node* const mix = s_mix + 1;
|
||
10 years ago
|
for (uint32_t w = 0; w != MIX_WORDS; ++w) {
|
||
10 years ago
|
mix->words[w] = s_mix[0].words[w % NODE_WORDS];
|
||
|
}
|
||
|
|
||
10 years ago
|
unsigned const page_size = sizeof(uint32_t) * MIX_WORDS;
|
||
10 years ago
|
unsigned const num_full_pages = (unsigned) (full_size / page_size);
|
||
10 years ago
|
|
||
10 years ago
|
for (unsigned i = 0; i != ETHASH_ACCESSES; ++i) {
|
||
10 years ago
|
uint32_t const index = fnv_hash(s_mix->words[0] ^ i, mix->words[i % MIX_WORDS]) % num_full_pages;
|
||
10 years ago
|
|
||
|
for (unsigned n = 0; n != MIX_NODES; ++n) {
|
||
10 years ago
|
node const* dag_node;
|
||
10 years ago
|
if (full_nodes) {
|
||
|
dag_node = &full_nodes[MIX_NODES * index + n];
|
||
|
} else {
|
||
|
node tmp_node;
|
||
10 years ago
|
ethash_calculate_dag_item(&tmp_node, index * MIX_NODES + n, light);
|
||
10 years ago
|
dag_node = &tmp_node;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
#if defined(_M_X64) && ENABLE_SSE
|
||
10 years ago
|
{
|
||
|
__m128i fnv_prime = _mm_set1_epi32(FNV_PRIME);
|
||
|
__m128i xmm0 = _mm_mullo_epi32(fnv_prime, mix[n].xmm[0]);
|
||
|
__m128i xmm1 = _mm_mullo_epi32(fnv_prime, mix[n].xmm[1]);
|
||
|
__m128i xmm2 = _mm_mullo_epi32(fnv_prime, mix[n].xmm[2]);
|
||
|
__m128i xmm3 = _mm_mullo_epi32(fnv_prime, mix[n].xmm[3]);
|
||
|
mix[n].xmm[0] = _mm_xor_si128(xmm0, dag_node->xmm[0]);
|
||
|
mix[n].xmm[1] = _mm_xor_si128(xmm1, dag_node->xmm[1]);
|
||
|
mix[n].xmm[2] = _mm_xor_si128(xmm2, dag_node->xmm[2]);
|
||
|
mix[n].xmm[3] = _mm_xor_si128(xmm3, dag_node->xmm[3]);
|
||
|
}
|
||
|
#else
|
||
|
{
|
||
|
for (unsigned w = 0; w != NODE_WORDS; ++w) {
|
||
|
mix[n].words[w] = fnv_hash(mix[n].words[w], dag_node->words[w]);
|
||
|
}
|
||
|
}
|
||
10 years ago
|
#endif
|
||
10 years ago
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
// compress mix
|
||
10 years ago
|
for (uint32_t w = 0; w != MIX_WORDS; w += 4) {
|
||
10 years ago
|
uint32_t reduction = mix->words[w + 0];
|
||
|
reduction = reduction * FNV_PRIME ^ mix->words[w + 1];
|
||
|
reduction = reduction * FNV_PRIME ^ mix->words[w + 2];
|
||
|
reduction = reduction * FNV_PRIME ^ mix->words[w + 3];
|
||
|
mix->words[w / 4] = reduction;
|
||
|
}
|
||
|
|
||
|
fix_endian_arr32(mix->words, MIX_WORDS / 4);
|
||
|
memcpy(&ret->mix_hash, mix->bytes, 32);
|
||
|
// final Keccak hash
|
||
|
SHA3_256(&ret->result, s_mix->bytes, 64 + 32); // Keccak-256(s + compressed_mix)
|
||
|
return true;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
ethash_h256_t ethash_get_seedhash(uint64_t block_number)
|
||
10 years ago
|
{
|
||
10 years ago
|
ethash_h256_t ret;
|
||
|
ethash_h256_reset(&ret);
|
||
10 years ago
|
uint64_t const epochs = block_number / ETHASH_EPOCH_LENGTH;
|
||
10 years ago
|
for (uint32_t i = 0; i < epochs; ++i)
|
||
10 years ago
|
SHA3_256(&ret, (uint8_t*)&ret, 32);
|
||
|
return ret;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
ethash_light_t ethash_light_new_internal(uint64_t cache_size, ethash_h256_t const* seed)
|
||
10 years ago
|
{
|
||
10 years ago
|
struct ethash_light *ret;
|
||
|
ret = calloc(sizeof(*ret), 1);
|
||
|
if (!ret) {
|
||
|
return NULL;
|
||
|
}
|
||
10 years ago
|
ret->cache = malloc((size_t)cache_size);
|
||
10 years ago
|
if (!ret->cache) {
|
||
|
goto fail_free_light;
|
||
|
}
|
||
10 years ago
|
node* nodes = (node*)ret->cache;
|
||
|
if (!ethash_compute_cache_nodes(nodes, cache_size, seed)) {
|
||
|
goto fail_free_cache_mem;
|
||
|
}
|
||
|
ret->cache_size = cache_size;
|
||
10 years ago
|
return ret;
|
||
10 years ago
|
|
||
10 years ago
|
fail_free_cache_mem:
|
||
|
free(ret->cache);
|
||
10 years ago
|
fail_free_light:
|
||
10 years ago
|
free(ret);
|
||
|
return NULL;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
ethash_light_t ethash_light_new(uint64_t block_number)
|
||
10 years ago
|
{
|
||
10 years ago
|
ethash_h256_t seedhash = ethash_get_seedhash(block_number);
|
||
10 years ago
|
ethash_light_t ret;
|
||
|
ret = ethash_light_new_internal(ethash_get_cachesize(block_number), &seedhash);
|
||
|
ret->block_number = block_number;
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
10 years ago
|
void ethash_light_delete(ethash_light_t light)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (light->cache) {
|
||
10 years ago
|
free(light->cache);
|
||
10 years ago
|
}
|
||
|
free(light);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
ethash_return_value_t ethash_light_compute_internal(
|
||
10 years ago
|
ethash_light_t light,
|
||
10 years ago
|
uint64_t full_size,
|
||
10 years ago
|
ethash_h256_t const header_hash,
|
||
10 years ago
|
uint64_t nonce
|
||
10 years ago
|
)
|
||
10 years ago
|
{
|
||
10 years ago
|
ethash_return_value_t ret;
|
||
|
ret.success = true;
|
||
|
if (!ethash_hash(&ret, NULL, light, full_size, header_hash, nonce)) {
|
||
|
ret.success = false;
|
||
|
}
|
||
|
return ret;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
ethash_return_value_t ethash_light_compute(
|
||
|
ethash_light_t light,
|
||
10 years ago
|
ethash_h256_t const header_hash,
|
||
10 years ago
|
uint64_t nonce
|
||
10 years ago
|
)
|
||
10 years ago
|
{
|
||
10 years ago
|
uint64_t full_size = ethash_get_datasize(light->block_number);
|
||
10 years ago
|
return ethash_light_compute_internal(light, full_size, header_hash, nonce);
|
||
10 years ago
|
}
|