2016-02-28 12:15:53 -08:00
|
|
|
// Copyright (c) 2016 Jack Grigg
|
|
|
|
// Copyright (c) 2016 The Zcash developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_EQUIHASH_H
|
|
|
|
#define BITCOIN_EQUIHASH_H
|
|
|
|
|
|
|
|
#include "crypto/sha256.h"
|
|
|
|
#include "utilstrencodings.h"
|
|
|
|
|
|
|
|
#include "sodium.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-05-04 20:00:44 -07:00
|
|
|
#include <boost/static_assert.hpp>
|
|
|
|
|
2016-02-28 12:15:53 -08:00
|
|
|
typedef crypto_generichash_blake2b_state eh_HashState;
|
|
|
|
typedef uint32_t eh_index;
|
2016-04-18 18:41:06 -07:00
|
|
|
typedef uint8_t eh_trunc;
|
2016-02-28 12:15:53 -08:00
|
|
|
|
|
|
|
class StepRow
|
|
|
|
{
|
2016-04-15 06:14:37 -07:00
|
|
|
protected:
|
2016-02-28 12:15:53 -08:00
|
|
|
unsigned char* hash;
|
|
|
|
unsigned int len;
|
|
|
|
|
|
|
|
public:
|
|
|
|
StepRow(unsigned int n, const eh_HashState& base_state, eh_index i);
|
|
|
|
~StepRow();
|
|
|
|
|
|
|
|
StepRow(const StepRow& a);
|
|
|
|
|
|
|
|
bool IsZero();
|
|
|
|
std::string GetHex() { return HexStr(hash, hash+len); }
|
|
|
|
|
|
|
|
friend inline bool operator==(const StepRow& a, const StepRow& b) { return memcmp(a.hash, b.hash, a.len) == 0; }
|
|
|
|
friend inline bool operator<(const StepRow& a, const StepRow& b) { return memcmp(a.hash, b.hash, a.len) < 0; }
|
|
|
|
|
|
|
|
friend bool HasCollision(StepRow& a, StepRow& b, int l);
|
|
|
|
};
|
|
|
|
|
|
|
|
bool HasCollision(StepRow& a, StepRow& b, int l);
|
2016-04-15 06:14:37 -07:00
|
|
|
|
|
|
|
class FullStepRow : public StepRow
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::vector<eh_index> indices;
|
|
|
|
|
|
|
|
public:
|
|
|
|
FullStepRow(unsigned int n, const eh_HashState& base_state, eh_index i);
|
|
|
|
~FullStepRow() { }
|
|
|
|
|
|
|
|
FullStepRow(const FullStepRow& a) : StepRow {a}, indices(a.indices) { }
|
2016-05-04 22:05:27 -07:00
|
|
|
FullStepRow(const FullStepRow& a, const FullStepRow& b, int trim);
|
2016-04-15 06:14:37 -07:00
|
|
|
FullStepRow& operator=(const FullStepRow& a);
|
|
|
|
|
2016-05-04 22:05:27 -07:00
|
|
|
inline bool IndicesBefore(const FullStepRow& a) const { return indices[0] < a.indices[0]; }
|
2016-04-15 06:14:37 -07:00
|
|
|
std::vector<eh_index> GetSolution() { return std::vector<eh_index>(indices); }
|
|
|
|
|
|
|
|
friend bool DistinctIndices(const FullStepRow& a, const FullStepRow& b);
|
2016-04-18 18:41:06 -07:00
|
|
|
friend bool IsValidBranch(const FullStepRow& a, const unsigned int ilen, const eh_trunc t);
|
2016-04-15 06:14:37 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
bool DistinctIndices(const FullStepRow& a, const FullStepRow& b);
|
2016-04-18 18:41:06 -07:00
|
|
|
bool IsValidBranch(const FullStepRow& a, const unsigned int ilen, const eh_trunc t);
|
|
|
|
|
|
|
|
class TruncatedStepRow : public StepRow
|
|
|
|
{
|
|
|
|
private:
|
2016-05-03 18:47:49 -07:00
|
|
|
unsigned int lenIndices;
|
2016-04-18 18:41:06 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
TruncatedStepRow(unsigned int n, const eh_HashState& base_state, eh_index i, unsigned int ilen);
|
|
|
|
~TruncatedStepRow() { }
|
|
|
|
|
2016-05-03 18:47:49 -07:00
|
|
|
TruncatedStepRow(const TruncatedStepRow& a);
|
2016-05-04 22:05:27 -07:00
|
|
|
TruncatedStepRow(const TruncatedStepRow& a, const TruncatedStepRow& b, int trim);
|
2016-04-18 18:41:06 -07:00
|
|
|
TruncatedStepRow& operator=(const TruncatedStepRow& a);
|
|
|
|
|
2016-05-03 18:47:49 -07:00
|
|
|
inline bool IndicesBefore(const TruncatedStepRow& a) const { return memcmp(hash+len, a.hash+a.len, lenIndices) < 0; }
|
|
|
|
eh_trunc* GetPartialSolution(eh_index soln_size) const;
|
2016-04-18 18:41:06 -07:00
|
|
|
};
|
2016-02-28 12:15:53 -08:00
|
|
|
|
2016-05-04 20:00:44 -07:00
|
|
|
template<unsigned int N, unsigned int K>
|
2016-02-28 12:15:53 -08:00
|
|
|
class Equihash
|
|
|
|
{
|
|
|
|
private:
|
2016-05-04 20:00:44 -07:00
|
|
|
BOOST_STATIC_ASSERT(K < N);
|
|
|
|
BOOST_STATIC_ASSERT(N % 8 == 0);
|
|
|
|
BOOST_STATIC_ASSERT((N/(K+1)) % 8 == 0);
|
|
|
|
BOOST_STATIC_ASSERT((N/(K+1)) + 1 < 8*sizeof(eh_index));
|
2016-02-28 12:15:53 -08:00
|
|
|
|
|
|
|
public:
|
2016-05-04 20:00:44 -07:00
|
|
|
enum { CollisionBitLength=N/(K+1) };
|
|
|
|
enum { CollisionByteLength=CollisionBitLength/8 };
|
2016-02-28 12:15:53 -08:00
|
|
|
|
2016-05-04 20:00:44 -07:00
|
|
|
Equihash() { }
|
2016-02-28 12:15:53 -08:00
|
|
|
|
|
|
|
int InitialiseState(eh_HashState& base_state);
|
|
|
|
std::set<std::vector<eh_index>> BasicSolve(const eh_HashState& base_state);
|
2016-04-18 18:41:06 -07:00
|
|
|
std::set<std::vector<eh_index>> OptimisedSolve(const eh_HashState& base_state);
|
2016-02-28 12:15:53 -08:00
|
|
|
bool IsValidSolution(const eh_HashState& base_state, std::vector<eh_index> soln);
|
|
|
|
};
|
|
|
|
|
2016-05-04 20:00:44 -07:00
|
|
|
static Equihash<96,5> Eh965;
|
|
|
|
static Equihash<48,5> Eh485;
|
|
|
|
|
|
|
|
#define EhInitialiseState(n, k, base_state) \
|
|
|
|
if (n == 96 && k == 5) { \
|
|
|
|
Eh965.InitialiseState(base_state); \
|
|
|
|
} else if (n == 48 && k == 5) { \
|
|
|
|
Eh485.InitialiseState(base_state); \
|
|
|
|
} else { \
|
|
|
|
throw std::invalid_argument("Unsupported Equihash parameters"); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define EhBasicSolve(n, k, base_state, solns) \
|
|
|
|
if (n == 96 && k == 5) { \
|
|
|
|
solns = Eh965.BasicSolve(base_state); \
|
|
|
|
} else if (n == 48 && k == 5) { \
|
|
|
|
solns = Eh485.BasicSolve(base_state); \
|
|
|
|
} else { \
|
|
|
|
throw std::invalid_argument("Unsupported Equihash parameters"); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define EhOptimisedSolve(n, k, base_state, solns) \
|
|
|
|
if (n == 96 && k == 5) { \
|
|
|
|
solns = Eh965.OptimisedSolve(base_state); \
|
|
|
|
} else if (n == 48 && k == 5) { \
|
|
|
|
solns = Eh485.OptimisedSolve(base_state); \
|
|
|
|
} else { \
|
|
|
|
throw std::invalid_argument("Unsupported Equihash parameters"); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define EhIsValidSolution(n, k, base_state, soln, ret) \
|
|
|
|
if (n == 96 && k == 5) { \
|
|
|
|
ret = Eh965.IsValidSolution(base_state, soln); \
|
|
|
|
} else if (n == 48 && k == 5) { \
|
|
|
|
ret = Eh485.IsValidSolution(base_state, soln); \
|
|
|
|
} else { \
|
|
|
|
throw std::invalid_argument("Unsupported Equihash parameters"); \
|
|
|
|
}
|
|
|
|
|
2016-02-28 12:15:53 -08:00
|
|
|
#endif // BITCOIN_EQUIHASH_H
|