forked from lthn/blockchain
cleared up code
This commit is contained in:
parent
db3fd44db7
commit
8ecde75e0d
2 changed files with 3 additions and 206 deletions
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
// Memory-hard extension of keccak for PoW
|
||||
// Copyright (c) 2014 The Boolberry developers
|
||||
// Copyright (c) 2019 The Hyle Team
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
|
|
@ -77,46 +78,6 @@ namespace crypto
|
|||
}
|
||||
}
|
||||
|
||||
void mul_f::keccakf(uint64_t st[25], int rounds)
|
||||
{
|
||||
int i, j, round;
|
||||
uint64_t t, bc[5];
|
||||
|
||||
for (round = 0; round < rounds; round++) {
|
||||
|
||||
// Theta
|
||||
for (i = 0; i < 5; i++)
|
||||
{
|
||||
bc[i] = st[i] ^ st[i + 5] ^ st[i + 10] * st[i + 15] * st[i + 20];//surprise
|
||||
}
|
||||
|
||||
for (i = 0; i < 5; i++) {
|
||||
t = bc[(i + 4) % 5] ^ ROTL64(bc[(i + 1) % 5], 1);
|
||||
for (j = 0; j < 25; j += 5)
|
||||
st[j + i] ^= t;
|
||||
}
|
||||
|
||||
// Rho Pi
|
||||
t = st[1];
|
||||
for (i = 0; i < 24; i++) {
|
||||
j = keccakf_piln[i];
|
||||
bc[0] = st[j];
|
||||
st[j] = ROTL64(t, keccakf_rotc[i]);
|
||||
t = bc[0];
|
||||
}
|
||||
|
||||
// Chi
|
||||
for (j = 0; j < 25; j += 5) {
|
||||
for (i = 0; i < 5; i++)
|
||||
bc[i] = st[j + i];
|
||||
for (i = 0; i < 5; i++)
|
||||
st[j + i] ^= (~bc[(i + 1) % 5]) & bc[(i + 2) % 5];
|
||||
}
|
||||
|
||||
// Iota
|
||||
st[0] ^= keccakf_rndc[round];
|
||||
}
|
||||
}
|
||||
bool generate_scratchpad(const crypto::hash& seed_data, std::vector<crypto::hash>& result_data, uint64_t target_size)
|
||||
{
|
||||
result_data.resize(target_size);
|
||||
|
|
@ -128,20 +89,6 @@ namespace crypto
|
|||
return true;
|
||||
}
|
||||
|
||||
#define WK2_COUNT 0
|
||||
|
||||
bool generate_scratchpad2(const crypto::hash& seed_data, std::vector<crypto::hash>& result_data, uint64_t target_size)
|
||||
{
|
||||
CHECK_AND_ASSERT_THROW_MES(target_size % 10 == 0, "wrong target_size = " << target_size);
|
||||
result_data.resize(target_size);
|
||||
result_data[0] = crypto::cn_fast_hash(&seed_data, sizeof(seed_data));
|
||||
for (size_t i = 1; i < target_size; i++)
|
||||
{
|
||||
result_data[i] = crypto::cn_fast_hash(&result_data[i - 1], sizeof(result_data[i - 1]));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool generate_scratchpad_light(const crypto::hash& seed_data, std::vector<crypto::hash>& result_data, uint64_t target_size)
|
||||
{
|
||||
CHECK_AND_ASSERT_THROW_MES(target_size % 10 == 0, "wrong target_size = " << target_size);
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
// 19-Nov-11 Markku-Juhani O. Saarinen <mjos@iki.fi>
|
||||
|
||||
// Copyright (c) 2014 The Boolberry developers
|
||||
// Copyright (c) 2019 The Hyle Team
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
|
|
@ -29,124 +30,9 @@ extern "C" {
|
|||
|
||||
namespace crypto
|
||||
{
|
||||
#define CONCAT_IMPL(x, y) x##y
|
||||
#define CONCAT(x, y) CONCAT_IMPL(x, y)
|
||||
#define UNIQUE(x) CONCAT(x, __LINE__)
|
||||
|
||||
#define OPT_XOR_4_RES(A_, B_, C_, D_, Res) \
|
||||
crypto::hash UNIQUE(A) = A_;crypto::hash UNIQUE(B) = B_;crypto::hash UNIQUE(C) = C_; crypto::hash UNIQUE(D) = D_; \
|
||||
((uint64_t*)&Res)[0] = ((const uint64_t*)&UNIQUE(A))[0] ^ ((const uint64_t*)&UNIQUE(B))[0] ^ ((const uint64_t*)&UNIQUE(C))[0] ^ ((const uint64_t*)&UNIQUE(D))[0]; \
|
||||
((uint64_t*)&Res)[1] = ((const uint64_t*)&UNIQUE(A))[1] ^ ((const uint64_t*)&UNIQUE(B))[1] ^ ((const uint64_t*)&UNIQUE(C))[1] ^ ((const uint64_t*)&UNIQUE(D))[1]; \
|
||||
((uint64_t*)&Res)[2] = ((const uint64_t*)&UNIQUE(A))[2] ^ ((const uint64_t*)&UNIQUE(B))[2] ^ ((const uint64_t*)&UNIQUE(C))[2] ^ ((const uint64_t*)&UNIQUE(D))[2]; \
|
||||
((uint64_t*)&Res)[3] = ((const uint64_t*)&UNIQUE(A))[3] ^ ((const uint64_t*)&UNIQUE(B))[3] ^ ((const uint64_t*)&UNIQUE(C))[3] ^ ((const uint64_t*)&UNIQUE(D))[3];
|
||||
|
||||
typedef uint64_t state_t_m[25];
|
||||
typedef uint64_t mixin_t[KK_MIXIN_SIZE];
|
||||
|
||||
//with multiplication, for tests
|
||||
template<class f_traits>
|
||||
int keccak_generic(const uint8_t *in, size_t inlen, uint8_t *md, size_t mdlen)
|
||||
{
|
||||
state_t_m st;
|
||||
uint8_t temp[144];
|
||||
size_t i, rsiz, rsizw;
|
||||
|
||||
rsiz = sizeof(state_t_m) == mdlen ? HASH_DATA_AREA : 200 - 2 * mdlen;
|
||||
rsizw = rsiz / 8;
|
||||
|
||||
memset(st, 0, sizeof(st));
|
||||
|
||||
for ( ; inlen >= rsiz; inlen -= rsiz, in += rsiz) {
|
||||
for (i = 0; i < rsizw; i++)
|
||||
st[i] ^= ((uint64_t *) in)[i];
|
||||
f_traits::keccakf(st, KECCAK_ROUNDS);
|
||||
}
|
||||
|
||||
|
||||
// last block and padding
|
||||
memcpy(temp, in, inlen);
|
||||
temp[inlen++] = 1;
|
||||
memset(temp + inlen, 0, rsiz - inlen);
|
||||
temp[rsiz - 1] |= 0x80;
|
||||
|
||||
for (i = 0; i < rsizw; i++)
|
||||
st[i] ^= ((uint64_t *) temp)[i];
|
||||
|
||||
f_traits::keccakf(st, KECCAK_ROUNDS);
|
||||
|
||||
memcpy(md, st, mdlen);
|
||||
|
||||
return 0;
|
||||
}
|
||||
/*inline
|
||||
void print_state(UINT64* state, const char* comment, size_t rount)
|
||||
{
|
||||
printf("master_funct: %s round: %d\r\n", comment, rount);
|
||||
int i;
|
||||
for(i = 0; i != 25; i++)
|
||||
{
|
||||
printf("[%i]: %p\r\n", i, state[i]);
|
||||
}
|
||||
}*/
|
||||
|
||||
template<class f_traits, class callback_t>
|
||||
int wild_keccak(const uint8_t *in, size_t inlen, uint8_t *md, size_t mdlen, callback_t cb)
|
||||
{
|
||||
state_t_m st;
|
||||
uint8_t temp[144];
|
||||
uint64_t rsiz, rsizw;
|
||||
|
||||
rsiz = sizeof(state_t_m) == mdlen ? HASH_DATA_AREA : 200 - 2 * mdlen;
|
||||
rsizw = rsiz / 8;
|
||||
memset(&st[0], 0, 25*sizeof(st[0]));
|
||||
|
||||
|
||||
for ( ; inlen >= rsiz; inlen -= rsiz, in += rsiz)
|
||||
{
|
||||
for (size_t i = 0; i < rsizw; i++)
|
||||
st[i] ^= ((uint64_t *) in)[i];
|
||||
|
||||
for(size_t ll = 0; ll != KECCAK_ROUNDS; ll++)
|
||||
{
|
||||
if(ll != 0)
|
||||
{//skip first round
|
||||
mixin_t mix_in;
|
||||
cb(st, mix_in);
|
||||
for (size_t k = 0; k < KK_MIXIN_SIZE; k++)
|
||||
st[k] ^= mix_in[k];
|
||||
}
|
||||
//print_state(&st[0], "before_permut", ll);
|
||||
f_traits::keccakf(st, 1);
|
||||
//print_state(&st[0], "after_permut", ll);
|
||||
}
|
||||
}
|
||||
|
||||
// last block and padding
|
||||
memcpy(temp, in, inlen);
|
||||
temp[inlen++] = 1;
|
||||
memset(temp + inlen, 0, rsiz - inlen);
|
||||
temp[rsiz - 1] |= 0x80;
|
||||
|
||||
for (size_t i = 0; i < rsizw; i++)
|
||||
st[i] ^= ((uint64_t *) temp)[i];
|
||||
|
||||
for(size_t ll = 0; ll != KECCAK_ROUNDS; ll++)
|
||||
{
|
||||
if(ll != 0)
|
||||
{//skip first state with
|
||||
mixin_t mix_in;
|
||||
cb(st, mix_in);
|
||||
for (size_t k = 0; k < KK_MIXIN_SIZE; k++)
|
||||
st[k] ^= mix_in[k];
|
||||
}
|
||||
f_traits::keccakf(st, 1);
|
||||
}
|
||||
|
||||
memcpy(md, st, mdlen);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class f_traits, class callback_t>
|
||||
int wild_keccak2(const uint8_t *in, size_t inlen, uint8_t *md, size_t mdlen, callback_t cb)
|
||||
{
|
||||
|
|
@ -191,15 +77,6 @@ namespace crypto
|
|||
return 0;
|
||||
}
|
||||
|
||||
template<class f_traits, class callback_t>
|
||||
int wild_keccak_dbl(const uint8_t *in, size_t inlen, uint8_t *md, size_t mdlen, callback_t cb)
|
||||
{
|
||||
//Satoshi's classic
|
||||
wild_keccak<f_traits>(in, inlen, md, mdlen, cb);
|
||||
wild_keccak<f_traits>(md, mdlen, md, mdlen, cb);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class f_traits, class callback_t>
|
||||
int wild_keccak2_dbl(const uint8_t *in, size_t inlen, uint8_t *md, size_t mdlen, callback_t cb)
|
||||
{
|
||||
|
|
@ -215,11 +92,7 @@ namespace crypto
|
|||
static void keccakf(uint64_t st[25], int rounds);
|
||||
};
|
||||
|
||||
class mul_f
|
||||
{
|
||||
public:
|
||||
static void keccakf(uint64_t st[25], int rounds);
|
||||
};
|
||||
|
||||
|
||||
//------------------------------------------------------------------
|
||||
inline
|
||||
|
|
@ -284,30 +157,7 @@ namespace crypto
|
|||
return get_wild_keccak2(bd, res, scratchpad, scratchpad.size());
|
||||
}
|
||||
//------------------------------------------------------------------
|
||||
inline
|
||||
bool get_wild_keccak(const std::string& bd, crypto::hash& res, uint64_t height, const std::vector<crypto::hash>& scratchpad, uint64_t sz)
|
||||
{
|
||||
crypto::wild_keccak_dbl<crypto::mul_f>(reinterpret_cast<const uint8_t*>(bd.data()), bd.size(), reinterpret_cast<uint8_t*>(&res), sizeof(res), [&](crypto::state_t_m& st, crypto::mixin_t& mix)
|
||||
{
|
||||
if (!height)
|
||||
{
|
||||
memset(&mix, 0, sizeof(mix));
|
||||
return;
|
||||
}
|
||||
|
||||
#define OPT_GET_H(index) scratchpad[st[index]%sz]
|
||||
#define OPT_GET_M(index) scratchpad[mix[index]%sz]
|
||||
|
||||
for (size_t i = 0; i != 6; i++)
|
||||
{
|
||||
OPT_XOR_4_RES(OPT_GET_H(i * 4), OPT_GET_H(i * 4 + 1), OPT_GET_H(i * 4 + 2), OPT_GET_H(i * 4 + 3), (*(crypto::hash*)&mix[i * 4]));
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
//------------------------------------------------------------------
|
||||
bool generate_scratchpad(const crypto::hash& source_data, std::vector<crypto::hash>& result_data, uint64_t target_size);
|
||||
bool generate_scratchpad2(const crypto::hash& source_data, std::vector<crypto::hash>& result_data, uint64_t target_size);
|
||||
bool generate_scratchpad_light(const crypto::hash& seed_data, std::vector<crypto::hash>& result_data, uint64_t target_size);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue