implement parallel pow hash algorithm, along with genesis block
This commit is contained in:
+7
-1
@@ -354,7 +354,13 @@ crypto_libbitcoin_crypto_base_a_SOURCES = \
|
||||
crypto/sha512.cpp \
|
||||
crypto/sha512.h \
|
||||
crypto/siphash.cpp \
|
||||
crypto/siphash.h
|
||||
crypto/siphash.h \
|
||||
crypto/parallel/architecture.h \
|
||||
crypto/parallel/common.h \
|
||||
crypto/parallel/parallel.cpp \
|
||||
crypto/parallel/parallel.h \
|
||||
crypto/parallel/sha512.cpp \
|
||||
crypto/parallel/sha512.h
|
||||
|
||||
if USE_ASM
|
||||
crypto_libbitcoin_crypto_base_a_SOURCES += crypto/sha256_sse4.cpp
|
||||
|
||||
+7
-8
@@ -44,11 +44,10 @@ static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesi
|
||||
* transaction cannot be spent since it did not originally exist in the
|
||||
* database.
|
||||
*
|
||||
* CBlock(hash=000000000019d6, ver=1, hashPrevBlock=00000000000000, hashMerkleRoot=4a5e1e, nTime=1231006505, nBits=1d00ffff, nNonce=2083236893, vtx=1)
|
||||
* CTransaction(hash=4a5e1e, ver=1, vin.size=1, vout.size=1, nLockTime=0)
|
||||
* CTxIn(COutPoint(000000, -1), coinbase 04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73)
|
||||
* CTxOut(nValue=50.00000000, scriptPubKey=0x5F1DF16B2B704C8A578D0B)
|
||||
* vMerkleTree: 4a5e1e
|
||||
* CBlock(hash=17e7131a4eb39658cca55c136f59d2cf795db379f3cb4fa640b171ee4ae90e9a, ver=0x00000001,
|
||||
* hashPrevBlock=0000000000000000000000000000000000000000000000000000000000000000,
|
||||
* hashMerkleRoot=dfd0e8579d4f22ce8fb4a7c2e372469d027e2a4954b6a3ef302c698052bdae1f,
|
||||
* nTime=1559078000, nBits=1f00ffff, nNonce=26552, vtx=1)
|
||||
*/
|
||||
static CBlock CreateGenesisBlock(uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
|
||||
{
|
||||
@@ -105,10 +104,10 @@ public:
|
||||
m_assumed_blockchain_size = 240;
|
||||
m_assumed_chain_state_size = 3;
|
||||
|
||||
genesis = CreateGenesisBlock(nTime, nNonce, 0x1f00ffff, 1, 0 * COIN);
|
||||
genesis = CreateGenesisBlock(1559078000, 26552, 0x1f00ffff, 1, 0 * COIN);
|
||||
consensus.hashGenesisBlock = genesis.GetHash();
|
||||
// assert(consensus.hashGenesisBlock == uint256S("0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"));
|
||||
// assert(genesis.hashMerkleRoot == uint256S("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));
|
||||
assert(consensus.hashGenesisBlock == uint256S("0x17e7131a4eb39658cca55c136f59d2cf795db379f3cb4fa640b171ee4ae90e9a"));
|
||||
assert(genesis.hashMerkleRoot == uint256S("0xdfd0e8579d4f22ce8fb4a7c2e372469d027e2a4954b6a3ef302c698052bdae1f"));
|
||||
|
||||
// vSeeds.emplace_back("");
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
// James Nobis <frt AT quelrod DOT net>
|
||||
// http://www.freerainbowtables.com/phpBB3/topic2465.html
|
||||
// "Feel free to consider this file to be in the public domain with no (C) attribution necessary."
|
||||
|
||||
#ifndef ARCHITECTURE_H
|
||||
#define ARCHITECTURE_H
|
||||
|
||||
#if defined (__GLIBC__)
|
||||
#include <endian.h>
|
||||
#if (__BYTE_ORDER == __LITTLE_ENDIAN)
|
||||
#define ARC_LITTLE_ENDIAN
|
||||
#elif (__BYTE_ORDER == __BIG_ENDIAN)
|
||||
#define ARC_BIG_ENDIAN
|
||||
#else
|
||||
#error "Unknown machine endianness"
|
||||
#endif
|
||||
#elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
|
||||
#define ARC_BIG_ENDIAN
|
||||
#elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
|
||||
#define ARC_LITTLE_ENDIAN
|
||||
#elif defined(__sparc) || defined(__sparc__) || \
|
||||
defined(_POWER) || defined(__powerpc__) || \
|
||||
defined(__ppc__) || defined(__hpux) || \
|
||||
defined(_MIPSEB) || defined(_POWER) || \
|
||||
defined(__s390__)
|
||||
#define ARC_BIG_ENDIAN
|
||||
#elif defined(__i386__) || defined(__alpha__) || \
|
||||
defined(__ia64) || defined(__ia64__) || \
|
||||
defined(_M_IX86) || defined(_M_IA64) || \
|
||||
defined(_M_ALPHA) || defined(__amd64) || \
|
||||
defined(__amd64__) || defined(_M_AMD64) || \
|
||||
defined(__x86_64) || defined(__x86_64__) || \
|
||||
defined(_M_X64)
|
||||
#define ARC_LITTLE_ENDIAN
|
||||
#else
|
||||
#error "Unknown machine endianness"
|
||||
#endif
|
||||
|
||||
#if defined(_M_X64) || defined(__x86_64__)
|
||||
#define ARC_x86_64
|
||||
#define ARC_x86
|
||||
#elif defined(_M_IX86) || defined(__i386__)
|
||||
#define ARC_x86_32
|
||||
#define ARC_x86
|
||||
#endif
|
||||
|
||||
#if defined(_LP64) || defined(__LP64__) || \
|
||||
defined(__ia64) || defined(__ia64__) || \
|
||||
defined(_M_IA64) || defined(__amd64) || \
|
||||
defined(__amd64__) || defined(_M_AMD64) || \
|
||||
defined(__x86_64) || defined(__x86_64__) || \
|
||||
defined(_M_X64) || defined(_WIN64)
|
||||
#define ARC_64
|
||||
#else
|
||||
#define ARC_32
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,80 @@
|
||||
// Copyright (c) 2014 Steve Thomas <steve AT tobtu DOT com>
|
||||
|
||||
#ifndef COMMON_H
|
||||
#define COMMON_H
|
||||
|
||||
#include "architecture.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#pragma warning(disable:4996)
|
||||
#endif
|
||||
|
||||
#define __STDC_CONSTANT_MACROS
|
||||
#define __STDC_LIMIT_MACROS
|
||||
#ifdef _WIN32
|
||||
#include "inttypes.h"
|
||||
#include <time.h>
|
||||
#include <windows.h>
|
||||
typedef LARGE_INTEGER TIMER_TYPE;
|
||||
#define TIMER_FUNC(t) QueryPerformanceCounter(&t)
|
||||
|
||||
inline double TIMER_DIFF(LARGE_INTEGER s, LARGE_INTEGER e)
|
||||
{
|
||||
LARGE_INTEGER f;
|
||||
QueryPerformanceFrequency(&f);
|
||||
return ((double) (e.QuadPart - s.QuadPart)) / f.QuadPart;
|
||||
}
|
||||
#else
|
||||
#include <inttypes.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#define TIMER_TYPE timeval
|
||||
#define TIMER_FUNC(t) gettimeofday(&t, NULL)
|
||||
#define TIMER_DIFF(s,e) ((e.tv_sec - s.tv_sec) + (e.tv_usec - s.tv_usec) / (double)1000000.0)
|
||||
#endif
|
||||
|
||||
#define SWAP_ENDIAN_32_(x) \
|
||||
( \
|
||||
((x) << 24) | \
|
||||
(((x) << 8) & 0x00ff0000) | \
|
||||
(((x) >> 8) & 0x0000ff00) | \
|
||||
((x) >> 24) \
|
||||
)
|
||||
#define SWAP_ENDIAN_32(x) SWAP_ENDIAN_32_(((uint32_t) (x)))
|
||||
#define SWAP_ENDIAN_64_(x) \
|
||||
( \
|
||||
((x) << 56) | \
|
||||
(((x) << 40) & UINT64_C(0x00ff000000000000)) | \
|
||||
(((x) << 24) & UINT64_C(0x0000ff0000000000)) | \
|
||||
(((x) << 8) & UINT64_C(0x000000ff00000000)) | \
|
||||
(((x) >> 8) & UINT64_C(0x00000000ff000000)) | \
|
||||
(((x) >> 24) & UINT64_C(0x0000000000ff0000)) | \
|
||||
(((x) >> 40) & UINT64_C(0x000000000000ff00)) | \
|
||||
((x) >> 56) \
|
||||
)
|
||||
#define SWAP_ENDIAN_64(x) SWAP_ENDIAN_64_(((uint64_t) (x)))
|
||||
|
||||
#ifdef ARC_LITTLE_ENDIAN
|
||||
#define READ_LITTLE_ENDIAN_32(n) (n)
|
||||
#define READ_BIG_ENDIAN_32(n) SWAP_ENDIAN_32(n)
|
||||
#define WRITE_LITTLE_ENDIAN_32(n) (n)
|
||||
#define WRITE_BIG_ENDIAN_32(n) SWAP_ENDIAN_32(n)
|
||||
|
||||
#define READ_LITTLE_ENDIAN_64(n) (n)
|
||||
#define READ_BIG_ENDIAN_64(n) SWAP_ENDIAN_64(n)
|
||||
#define WRITE_LITTLE_ENDIAN_64(n) (n)
|
||||
#define WRITE_BIG_ENDIAN_64(n) SWAP_ENDIAN_64(n)
|
||||
#else
|
||||
#define READ_LITTLE_ENDIAN_32(n) SWAP_ENDIAN_32(n)
|
||||
#define READ_BIG_ENDIAN_32(n) (n)
|
||||
#define WRITE_LITTLE_ENDIAN_32(n) SWAP_ENDIAN_32(n)
|
||||
#define WRITE_BIG_ENDIAN_32(n) (n)
|
||||
|
||||
#define READ_LITTLE_ENDIAN_64(n) SWAP_ENDIAN_64(n)
|
||||
#define READ_BIG_ENDIAN_64(n) (n)
|
||||
#define WRITE_LITTLE_ENDIAN_64(n) SWAP_ENDIAN_64(n)
|
||||
#define WRITE_BIG_ENDIAN_64(n) (n)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,147 @@
|
||||
// Copyright (c) 2014 Steve Thomas <steve AT tobtu DOT com>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "parallel.h"
|
||||
#include "sha512.h"
|
||||
|
||||
#define HASH_LENGTH Sha512::HASH_LENGTH
|
||||
|
||||
inline uint64_t calcLoopCount(uint32_t cost)
|
||||
{
|
||||
// floor((cost & 1 ? 2 : 3) * 2 ** floor((cost - 1) / 2))
|
||||
// 1, 2, 3, 4, 6, 8, 12, 16, ...
|
||||
if (cost == 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return ((uint64_t) ((cost & 1) ^ 3)) << ((cost - 1) >> 1);
|
||||
}
|
||||
|
||||
int PHS(void *out, size_t outlen, const void *in, size_t inlen, const void *salt, size_t saltlen, unsigned int t_cost, unsigned int m_cost)
|
||||
{
|
||||
uint64_t key [HASH_LENGTH / sizeof(uint64_t)];
|
||||
uint64_t tmp [HASH_LENGTH / sizeof(uint64_t)];
|
||||
uint64_t work[HASH_LENGTH / sizeof(uint64_t)];
|
||||
uint64_t parallelLoops;
|
||||
uint64_t sequentialLoops;
|
||||
Sha512 sha512;
|
||||
|
||||
if ((t_cost & 0xffff) > 106 || (t_cost >> 16) > 126 || outlen > HASH_LENGTH)
|
||||
{
|
||||
memset(out, 0, outlen);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// key = hash(hash(salt) || in)
|
||||
Sha512::hash(salt, saltlen, key);
|
||||
sha512.init();
|
||||
sha512.update(key, HASH_LENGTH);
|
||||
sha512.update(in, inlen);
|
||||
sha512.finish(key);
|
||||
|
||||
// Work
|
||||
parallelLoops = 3 * 5 * 128 * calcLoopCount(t_cost & 0xffff);
|
||||
sequentialLoops = calcLoopCount(t_cost >> 16);
|
||||
|
||||
for (uint64_t i = 0; i < sequentialLoops; i++)
|
||||
{
|
||||
// Clear work
|
||||
memset(work, 0, HASH_LENGTH);
|
||||
|
||||
for (uint64_t j = 0; j < parallelLoops; j++)
|
||||
{
|
||||
// work ^= hash(WRITE_BIG_ENDIAN_64(j) || key)
|
||||
uint64_t tmpJ = WRITE_BIG_ENDIAN_64(j);
|
||||
|
||||
sha512.init();
|
||||
sha512.update(&tmpJ, sizeof(tmpJ));
|
||||
sha512.update(key, HASH_LENGTH);
|
||||
sha512.finish(tmp);
|
||||
for (unsigned int k = 0; k < HASH_LENGTH / sizeof(uint64_t); k++)
|
||||
{
|
||||
work[k] ^= tmp[k];
|
||||
}
|
||||
}
|
||||
|
||||
// Finish
|
||||
// key = truncate(hash(hash(work || key)), outlen) || zeros(HASH_LENGTH - outlen)
|
||||
sha512.init();
|
||||
sha512.update(work, HASH_LENGTH);
|
||||
sha512.update(key, HASH_LENGTH);
|
||||
sha512.finish(key);
|
||||
Sha512::hash(key, HASH_LENGTH, key, outlen);
|
||||
memset(((uint8_t*) key) + outlen, 0, HASH_LENGTH - outlen);
|
||||
}
|
||||
|
||||
// Finish
|
||||
// out = key
|
||||
memcpy(out, key, outlen);
|
||||
|
||||
// Clean up
|
||||
// TODO: find a secure wipe function
|
||||
memset(key, 0, sizeof(key));
|
||||
memset(work, 0, sizeof(key));
|
||||
memset(tmp, 0, sizeof(key));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int parallelKdf(void *out, size_t outlen, const void *in, size_t inlen, const void *salt, size_t saltlen, unsigned int t_cost, unsigned int m_cost)
|
||||
{
|
||||
uint64_t key [HASH_LENGTH / sizeof(uint64_t)];
|
||||
uint64_t tmp [HASH_LENGTH / sizeof(uint64_t)];
|
||||
uint64_t work[HASH_LENGTH / sizeof(uint64_t)] = {0};
|
||||
uint64_t parallelLoops;
|
||||
Sha512 sha512;
|
||||
|
||||
if (t_cost > 106 || outlen > HASH_LENGTH)
|
||||
{
|
||||
memset(out, 0, outlen);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// key = hash(hash(salt) || in)
|
||||
Sha512::hash(salt, saltlen, key);
|
||||
sha512.init();
|
||||
sha512.update(key, HASH_LENGTH);
|
||||
sha512.update(in, inlen);
|
||||
sha512.finish(key);
|
||||
|
||||
// Work
|
||||
parallelLoops = 3 * 5 * 128 * calcLoopCount(t_cost & 0xffff);
|
||||
for (uint64_t i = 0; i < parallelLoops; i++)
|
||||
{
|
||||
// work ^= SHA512(WRITE_BIG_ENDIAN_64(i) || key)
|
||||
uint64_t tmpI = WRITE_BIG_ENDIAN_64(i);
|
||||
|
||||
sha512.init();
|
||||
sha512.update(&tmpI, sizeof(tmpI));
|
||||
sha512.update(key, HASH_LENGTH);
|
||||
sha512.finish(tmp);
|
||||
for (unsigned int j = 0; j < HASH_LENGTH / sizeof(uint64_t); j++)
|
||||
{
|
||||
work[j] ^= tmp[j];
|
||||
}
|
||||
}
|
||||
|
||||
// Finish
|
||||
for (int32_t i = 0, left = (int32_t) outlen; left > 0; left -= HASH_LENGTH, i++)
|
||||
{
|
||||
uint64_t tmpI = WRITE_BIG_ENDIAN_64(i);
|
||||
|
||||
// out = out || hash(WRITE_BIG_ENDIAN_64(i) || work || in)
|
||||
sha512.init();
|
||||
sha512.update(&tmpI, sizeof(tmpI));
|
||||
sha512.update(work, HASH_LENGTH);
|
||||
sha512.update(in, inlen);
|
||||
sha512.finish(out, outlen);
|
||||
out = ((uint8_t*) out) + HASH_LENGTH;
|
||||
}
|
||||
|
||||
// Clean up
|
||||
// TODO: find a secure wipe function
|
||||
memset(key, 0, sizeof(key));
|
||||
memset(work, 0, sizeof(key));
|
||||
memset(tmp, 0, sizeof(key));
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Copyright (c) 2014 Steve Thomas <steve AT tobtu DOT com>
|
||||
|
||||
#ifndef BATTCRYPT_H
|
||||
#define BATTCRYPT_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
int PHS(void *out, size_t outlen, const void *in, size_t inlen, const void *salt, size_t saltlen, unsigned int t_cost, unsigned int m_cost);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,203 @@
|
||||
// Copyright (c) 2014 Steve Thomas <steve AT tobtu DOT com>
|
||||
|
||||
#include <string.h>
|
||||
#include "sha512.h"
|
||||
|
||||
const uint64_t SHA512_CONSTS[80] = {
|
||||
UINT64_C(0x428a2f98d728ae22), UINT64_C(0x7137449123ef65cd), UINT64_C(0xb5c0fbcfec4d3b2f), UINT64_C(0xe9b5dba58189dbbc), UINT64_C(0x3956c25bf348b538),
|
||||
UINT64_C(0x59f111f1b605d019), UINT64_C(0x923f82a4af194f9b), UINT64_C(0xab1c5ed5da6d8118), UINT64_C(0xd807aa98a3030242), UINT64_C(0x12835b0145706fbe),
|
||||
UINT64_C(0x243185be4ee4b28c), UINT64_C(0x550c7dc3d5ffb4e2), UINT64_C(0x72be5d74f27b896f), UINT64_C(0x80deb1fe3b1696b1), UINT64_C(0x9bdc06a725c71235),
|
||||
UINT64_C(0xc19bf174cf692694), UINT64_C(0xe49b69c19ef14ad2), UINT64_C(0xefbe4786384f25e3), UINT64_C(0x0fc19dc68b8cd5b5), UINT64_C(0x240ca1cc77ac9c65),
|
||||
UINT64_C(0x2de92c6f592b0275), UINT64_C(0x4a7484aa6ea6e483), UINT64_C(0x5cb0a9dcbd41fbd4), UINT64_C(0x76f988da831153b5), UINT64_C(0x983e5152ee66dfab),
|
||||
UINT64_C(0xa831c66d2db43210), UINT64_C(0xb00327c898fb213f), UINT64_C(0xbf597fc7beef0ee4), UINT64_C(0xc6e00bf33da88fc2), UINT64_C(0xd5a79147930aa725),
|
||||
UINT64_C(0x06ca6351e003826f), UINT64_C(0x142929670a0e6e70), UINT64_C(0x27b70a8546d22ffc), UINT64_C(0x2e1b21385c26c926), UINT64_C(0x4d2c6dfc5ac42aed),
|
||||
UINT64_C(0x53380d139d95b3df), UINT64_C(0x650a73548baf63de), UINT64_C(0x766a0abb3c77b2a8), UINT64_C(0x81c2c92e47edaee6), UINT64_C(0x92722c851482353b),
|
||||
UINT64_C(0xa2bfe8a14cf10364), UINT64_C(0xa81a664bbc423001), UINT64_C(0xc24b8b70d0f89791), UINT64_C(0xc76c51a30654be30), UINT64_C(0xd192e819d6ef5218),
|
||||
UINT64_C(0xd69906245565a910), UINT64_C(0xf40e35855771202a), UINT64_C(0x106aa07032bbd1b8), UINT64_C(0x19a4c116b8d2d0c8), UINT64_C(0x1e376c085141ab53),
|
||||
UINT64_C(0x2748774cdf8eeb99), UINT64_C(0x34b0bcb5e19b48a8), UINT64_C(0x391c0cb3c5c95a63), UINT64_C(0x4ed8aa4ae3418acb), UINT64_C(0x5b9cca4f7763e373),
|
||||
UINT64_C(0x682e6ff3d6b2b8a3), UINT64_C(0x748f82ee5defb2fc), UINT64_C(0x78a5636f43172f60), UINT64_C(0x84c87814a1f0ab72), UINT64_C(0x8cc702081a6439ec),
|
||||
UINT64_C(0x90befffa23631e28), UINT64_C(0xa4506cebde82bde9), UINT64_C(0xbef9a3f7b2c67915), UINT64_C(0xc67178f2e372532b), UINT64_C(0xca273eceea26619c),
|
||||
UINT64_C(0xd186b8c721c0c207), UINT64_C(0xeada7dd6cde0eb1e), UINT64_C(0xf57d4f7fee6ed178), UINT64_C(0x06f067aa72176fba), UINT64_C(0x0a637dc5a2c898a6),
|
||||
UINT64_C(0x113f9804bef90dae), UINT64_C(0x1b710b35131c471b), UINT64_C(0x28db77f523047d84), UINT64_C(0x32caab7b40c72493), UINT64_C(0x3c9ebe0a15c9bebc),
|
||||
UINT64_C(0x431d67c49c100d4c), UINT64_C(0x4cc5d4becb3e42b6), UINT64_C(0x597f299cfc657e2a), UINT64_C(0x5fcb6fab3ad6faec), UINT64_C(0x6c44198c4a475817)};
|
||||
|
||||
void sha512Block(const uint64_t block[16], uint64_t state[8]);
|
||||
|
||||
void Sha512::hash(const void *message, size_t length, void *out, uint32_t outLength)
|
||||
{
|
||||
uint64_t block[16];
|
||||
uint64_t state[8] = {
|
||||
UINT64_C(0x6a09e667f3bcc908), UINT64_C(0xbb67ae8584caa73b), UINT64_C(0x3c6ef372fe94f82b), UINT64_C(0xa54ff53a5f1d36f1),
|
||||
UINT64_C(0x510e527fade682d1), UINT64_C(0x9b05688c2b3e6c1f), UINT64_C(0x1f83d9abfb41bd6b), UINT64_C(0x5be0cd19137e2179)};
|
||||
size_t left = length;
|
||||
|
||||
while (left >= 128)
|
||||
{
|
||||
sha512Block((const uint64_t*) message, state);
|
||||
message = ((const uint64_t*) message) + 16;
|
||||
left -= 128;
|
||||
}
|
||||
memcpy(block, message, left);
|
||||
((uint8_t*) block)[left] = 0x80;
|
||||
memset(((uint8_t*) block) + (left + 1), 0, 128 - (left + 1));
|
||||
if (left >= 128 - 16)
|
||||
{
|
||||
sha512Block(block, state);
|
||||
for (uint32_t i = 0; i < 14; i++)
|
||||
{
|
||||
block[i] = 0;
|
||||
}
|
||||
}
|
||||
uint64_t tmp;
|
||||
tmp = ((uint64_t) length) >> (64 - 3);
|
||||
block[14] = WRITE_BIG_ENDIAN_64(tmp);
|
||||
tmp = ((uint64_t) length) << 3;
|
||||
block[15] = WRITE_BIG_ENDIAN_64(tmp);
|
||||
sha512Block(block, state);
|
||||
if (outLength > 64)
|
||||
{
|
||||
outLength = 64;
|
||||
}
|
||||
for (uint32_t i = 0, end = outLength / 8; i < end; i++)
|
||||
{
|
||||
((uint64_t*) out)[i] = WRITE_BIG_ENDIAN_64(state[i]);
|
||||
}
|
||||
for (uint32_t i = outLength & ~7, shift = 56; i < outLength; i++, shift -= 8)
|
||||
{
|
||||
((uint8_t*) out)[i] = (uint8_t) (state[i / 8] >> shift);
|
||||
}
|
||||
}
|
||||
|
||||
void Sha512::init()
|
||||
{
|
||||
m_messageLengthHi = 0;
|
||||
m_messageLengthLo = 0;
|
||||
m_state[0] = UINT64_C(0x6a09e667f3bcc908);
|
||||
m_state[1] = UINT64_C(0xbb67ae8584caa73b);
|
||||
m_state[2] = UINT64_C(0x3c6ef372fe94f82b);
|
||||
m_state[3] = UINT64_C(0xa54ff53a5f1d36f1);
|
||||
m_state[4] = UINT64_C(0x510e527fade682d1);
|
||||
m_state[5] = UINT64_C(0x9b05688c2b3e6c1f);
|
||||
m_state[6] = UINT64_C(0x1f83d9abfb41bd6b);
|
||||
m_state[7] = UINT64_C(0x5be0cd19137e2179);
|
||||
}
|
||||
|
||||
void Sha512::update(const void *message, size_t length)
|
||||
{
|
||||
size_t pos = (size_t) m_messageLengthLo & 127;
|
||||
size_t left = length;
|
||||
|
||||
if (pos + left >= 128)
|
||||
{
|
||||
memcpy(((uint8_t*) m_block) + pos, message, 128 - pos);
|
||||
sha512Block(m_block, m_state);
|
||||
message = ((const uint8_t*) message) + 128 - pos;
|
||||
left -= 128 - pos;
|
||||
while (left >= 128)
|
||||
{
|
||||
sha512Block(((const uint64_t*) message), m_state);
|
||||
message = ((const uint8_t*) message) + 128;
|
||||
left -= 128;
|
||||
}
|
||||
memcpy(m_block, message, left);
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(((uint8_t*) m_block) + pos, message, left);
|
||||
}
|
||||
m_messageLengthLo += length;
|
||||
if (m_messageLengthLo < length)
|
||||
{
|
||||
m_messageLengthHi++;
|
||||
}
|
||||
}
|
||||
|
||||
void Sha512::finish(void *out, uint32_t outLength)
|
||||
{
|
||||
uint64_t lengthHi = m_messageLengthHi;
|
||||
uint64_t lengthLo = m_messageLengthLo;
|
||||
|
||||
((uint8_t*) m_block)[lengthLo % 128] = 0x80;
|
||||
memset(((uint8_t*) m_block) + (lengthLo % 128 + 1), 0, 128 - (lengthLo % 128 + 1));
|
||||
if (lengthLo % 128 >= 128 - 16)
|
||||
{
|
||||
sha512Block(m_block, m_state);
|
||||
for (uint32_t i = 0; i < 15; i++)
|
||||
{
|
||||
m_block[i] = 0;
|
||||
}
|
||||
}
|
||||
lengthHi = (lengthHi << 3) + (lengthLo >> (64 - 3));
|
||||
lengthLo *= 8;
|
||||
m_block[14] = WRITE_BIG_ENDIAN_64(lengthHi);
|
||||
m_block[15] = WRITE_BIG_ENDIAN_64(lengthLo);
|
||||
sha512Block(m_block, m_state);
|
||||
if (outLength > 64)
|
||||
{
|
||||
outLength = 64;
|
||||
}
|
||||
for (uint32_t i = 0, end = outLength / 8; i < end; i++)
|
||||
{
|
||||
((uint64_t*) out)[i] = WRITE_BIG_ENDIAN_64(m_state[i]);
|
||||
}
|
||||
for (uint32_t i = outLength & ~7, shift = 56; i < outLength; i++, shift -= 8)
|
||||
{
|
||||
((uint8_t*) out)[i] = (uint8_t) (m_state[i / 8] >> shift);
|
||||
}
|
||||
}
|
||||
|
||||
void sha512Block(const uint64_t block[16], uint64_t state[8])
|
||||
{
|
||||
#define ROTR(n,s) ((n >> s) | (n << (64 - s)))
|
||||
#define SHA512_STEP(a,b,c,d,e,f,g,h,w,i) \
|
||||
h += (ROTR(e, 14) ^ ROTR(e, 18) ^ ROTR(e, 41)) + ((e & f) ^ (~e & g)) + SHA512_CONSTS[i] + w[i]; \
|
||||
d += h; \
|
||||
h += (ROTR(a, 28) ^ ROTR(a, 34) ^ ROTR(a, 39)) + ((a & b) ^ (a & c) ^ (b & c));
|
||||
|
||||
uint64_t w[80];
|
||||
uint64_t a = state[0];
|
||||
uint64_t b = state[1];
|
||||
uint64_t c = state[2];
|
||||
uint64_t d = state[3];
|
||||
uint64_t e = state[4];
|
||||
uint64_t f = state[5];
|
||||
uint64_t g = state[6];
|
||||
uint64_t h = state[7];
|
||||
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
w[i] = READ_BIG_ENDIAN_64(block[i]);
|
||||
}
|
||||
for (int i = 16; i < 80; i++)
|
||||
{
|
||||
w[i] =
|
||||
w[i-16] +
|
||||
w[i- 7] +
|
||||
(ROTR(w[i-15], 1) ^ ROTR(w[i-15], 8) ^ (w[i-15] >> 7)) +
|
||||
(ROTR(w[i- 2], 19) ^ ROTR(w[i- 2], 61) ^ (w[i- 2] >> 6));
|
||||
}
|
||||
|
||||
for (int i = 0; i < 80; i += 8)
|
||||
{
|
||||
SHA512_STEP(a,b,c,d,e,f,g,h,w,i+0);
|
||||
SHA512_STEP(h,a,b,c,d,e,f,g,w,i+1);
|
||||
SHA512_STEP(g,h,a,b,c,d,e,f,w,i+2);
|
||||
SHA512_STEP(f,g,h,a,b,c,d,e,w,i+3);
|
||||
SHA512_STEP(e,f,g,h,a,b,c,d,w,i+4);
|
||||
SHA512_STEP(d,e,f,g,h,a,b,c,w,i+5);
|
||||
SHA512_STEP(c,d,e,f,g,h,a,b,w,i+6);
|
||||
SHA512_STEP(b,c,d,e,f,g,h,a,w,i+7);
|
||||
}
|
||||
|
||||
state[0] += a;
|
||||
state[1] += b;
|
||||
state[2] += c;
|
||||
state[3] += d;
|
||||
state[4] += e;
|
||||
state[5] += f;
|
||||
state[6] += g;
|
||||
state[7] += h;
|
||||
|
||||
#undef ROTR
|
||||
#undef SHA512_STEP
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright (c) 2014 Steve Thomas <steve AT tobtu DOT com>
|
||||
|
||||
#ifndef SHA512_H
|
||||
#define SHA512_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
class Sha512
|
||||
{
|
||||
public:
|
||||
Sha512() {}
|
||||
~Sha512() {}
|
||||
|
||||
static const unsigned int HASH_LENGTH = 64;
|
||||
|
||||
static void hash(const void *message, size_t length, void *out, uint32_t outLength = 64);
|
||||
void init();
|
||||
void update(const void *message, size_t length);
|
||||
void finish(void *out, uint32_t outLength = 64);
|
||||
|
||||
private:
|
||||
uint64_t m_messageLengthHi;
|
||||
uint64_t m_messageLengthLo;
|
||||
uint64_t m_state[8];
|
||||
uint64_t m_block[16];
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <crypto/common.h>
|
||||
#include <crypto/ripemd160.h>
|
||||
#include <crypto/sha256.h>
|
||||
#include <crypto/parallel/parallel.h>
|
||||
#include <prevector.h>
|
||||
#include <serialize.h>
|
||||
#include <uint256.h>
|
||||
|
||||
@@ -19,7 +19,7 @@ uint256 CBlockHeader::GetHash() const
|
||||
uint256 CBlockHeader::GetPoWHash() const
|
||||
{
|
||||
uint256 thash;
|
||||
scrypt_1024_1_1_256(BEGIN(nVersion), BEGIN(thash));
|
||||
PHS((void*)BEGIN(thash),32,(void*)BEGIN(nVersion),80,(void*)BEGIN(nVersion),80,4,4);
|
||||
return thash;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user