Skip to content

Commit

Permalink
Returned Keccak hashing and burn BGL changes (#13)
Browse files Browse the repository at this point in the history
* Returned Keccak hashing and burn BGL changes

* Remove duplicate lines

* Fix interfaces refactoring

Co-authored-by: VanOrton <>
  • Loading branch information
van-orton authored May 19, 2021
1 parent a8eef8f commit 7af3521
Show file tree
Hide file tree
Showing 17 changed files with 321 additions and 1,500 deletions.
19 changes: 15 additions & 4 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ LIBBGL_CRYPTO_SHANI = crypto/libBGL_crypto_shani.a
LIBBGL_CRYPTO += $(LIBBGL_CRYPTO_SHANI)
endif

# Add SHA3 support
LIBBGL_CRYPTO_SHA3 = crypto/sha3/libBGL_crypto_sha3.a
LIBBGL_CRYPTO += $(LIBBGL_CRYPTO_SHA3)

$(LIBSECP256K1): $(wildcard secp256k1/src/*.h) $(wildcard secp256k1/src/*.c) $(wildcard secp256k1/include/*)
$(AM_V_at)$(MAKE) $(AM_MAKEFLAGS) -C $(@D) $(@F)

Expand Down Expand Up @@ -393,7 +397,6 @@ endif
libBGL_wallet_a_CPPFLAGS = $(AM_CPPFLAGS) $(BGL_INCLUDES) $(SQLITE_CFLAGS)
libBGL_wallet_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libBGL_wallet_a_SOURCES = \
interfaces/wallet.cpp \
wallet/coincontrol.cpp \
wallet/context.cpp \
wallet/crypter.cpp \
Expand All @@ -402,6 +405,7 @@ libBGL_wallet_a_SOURCES = \
wallet/external_signer_scriptpubkeyman.cpp \
wallet/feebumper.cpp \
wallet/fees.cpp \
wallet/interfaces.cpp \
wallet/load.cpp \
wallet/rpcdump.cpp \
wallet/rpcwallet.cpp \
Expand All @@ -425,6 +429,13 @@ libBGL_wallet_tool_a_SOURCES = \
wallet/wallettool.cpp \
$(BGL_CORE_H)

# SHA3 extension library
crypto_sha3_libBGL_crypto_sha3_a_CPPFLAGS = $(AM_CPPFLAGS)
crypto_sha3_libBGL_crypto_sha3_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
crypto_sha3_libBGL_crypto_sha3_a_SOURCES = \
crypto/sha3/sha3.c \
crypto/sha3/sha3.h

# crypto primitives library
crypto_libBGL_crypto_base_a_CPPFLAGS = $(AM_CPPFLAGS)
crypto_libBGL_crypto_base_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
Expand Down Expand Up @@ -591,13 +602,13 @@ libBGL_util_a_SOURCES = \
util/rbf.cpp \
util/readwritefile.cpp \
util/settings.cpp \
util/thread.cpp \
util/threadnames.cpp \
util/spanparsing.cpp \
util/strencodings.cpp \
util/string.cpp \
util/time.cpp \
util/tokenpipe.cpp \
util/thread.cpp \
$(BGL_CORE_H)

if USE_LIBEVENT
Expand Down Expand Up @@ -734,7 +745,7 @@ BGL_util_LDADD += $(BOOST_LIBS)
# BGLconsensus library #
if BUILD_BGL_LIBS
include_HEADERS = script/BGLconsensus.h
libBGLconsensus_la_SOURCES = support/cleanse.cpp $(crypto_libBGL_crypto_base_a_SOURCES) $(libBGL_consensus_a_SOURCES)
libBGLconsensus_la_SOURCES = support/cleanse.cpp $(crypto_libBGL_crypto_base_a_SOURCES) $(crypto_sha3_libBGL_crypto_sha3_a_SOURCES) $(libBGL_consensus_a_SOURCES)

if GLIBC_BACK_COMPAT
libBGLconsensus_la_SOURCES += compat/glibc_compat.cpp
Expand Down Expand Up @@ -875,4 +886,4 @@ endif

if ENABLE_QT_TESTS
include Makefile.qttest.include
endif
endif
4 changes: 2 additions & 2 deletions src/addrman.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ friend class CAddrManTest;
// can be ignored by older clients for backward compatibility.
uint256 asmap_checksum;
if (m_asmap.size() != 0) {
asmap_checksum = SerializeHash(m_asmap);
asmap_checksum = SerializeHashSHA256(m_asmap);
}
s << asmap_checksum;
}
Expand Down Expand Up @@ -519,7 +519,7 @@ friend class CAddrManTest;
// serialization.
uint256 supplied_asmap_checksum;
if (m_asmap.size() != 0) {
supplied_asmap_checksum = SerializeHash(m_asmap);
supplied_asmap_checksum = SerializeHashSHA256(m_asmap);
}
uint256 serialized_asmap_checksum;
if (format >= Format::V2_ASMAP) {
Expand Down
259 changes: 112 additions & 147 deletions src/chainparams.cpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/crypto/muhash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ void Num3072::ToBytes(unsigned char (&out)[BYTE_SIZE]) {
Num3072 MuHash3072::ToNum3072(Span<const unsigned char> in) {
unsigned char tmp[Num3072::BYTE_SIZE];

uint256 hashed_in = (CHashWriter(SER_DISK, 0) << in).GetSHA256();
uint256 hashed_in = (CHashWriterSHA256(SER_DISK, 0) << in).GetSHA256();
ChaCha20(hashed_in.data(), hashed_in.size()).Keystream(tmp, Num3072::BYTE_SIZE);
Num3072 out{tmp};

Expand All @@ -318,7 +318,7 @@ void MuHash3072::Finalize(uint256& out) noexcept
unsigned char data[Num3072::BYTE_SIZE];
m_numerator.ToBytes(data);

out = (CHashWriter(SER_DISK, 0) << data).GetSHA256();
out = (CHashWriterSHA256(SER_DISK, 0) << data).GetSHA256();
}

MuHash3072& MuHash3072::operator*=(const MuHash3072& mul) noexcept
Expand Down
4 changes: 2 additions & 2 deletions src/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ uint256 SHA256Uint256(const uint256& input)
return result;
}

CHashWriter TaggedHash(const std::string& tag)
CHashWriterSHA256 TaggedHash(const std::string& tag)
{
CHashWriter writer(SER_GETHASH, 0);
CHashWriterSHA256 writer(SER_GETHASH, 0);
uint256 taghash;
CSHA256().Write((const unsigned char*)tag.data(), tag.size()).Finalize(taghash.begin());
writer << taghash << taghash;
Expand Down
146 changes: 138 additions & 8 deletions src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
#include <string>
#include <vector>

#include "logging.h"

extern "C" {
#include <crypto/sha3/sha3.h>
}

typedef uint256 ChainCode;

/** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
Expand Down Expand Up @@ -45,7 +51,62 @@ class CHash256 {
}
};

/** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
/** A hasher class for BGL's 256-bit hash (single SHA-256). Used for tx ids/hashes */
class CHash256Single {
private:
CSHA256 sha;
public:
static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;

void Finalize(unsigned char hash[OUTPUT_SIZE]) {
sha.Finalize(hash);
}

CHash256Single& Write(const unsigned char *data, size_t len) {
sha.Write(data, len);
return *this;
}

CHash256Single& Reset() {
sha.Reset();
return *this;
}
};

/** A SHA3 hasher class specifically for blocks and transactions of BGL. */
class CHash256BlockOrTransaction {
private:
sha3_context sha3context;
public:
static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;

CHash256BlockOrTransaction() {
sha3_Init256(&sha3context);
sha3_SetFlags(&sha3context, SHA3_FLAGS_KECCAK);
}

void Finalize(unsigned char hash[OUTPUT_SIZE]) {
void const *buf;
buf = sha3_Finalize(&sha3context);

for (int i = 0; i < 32; i++)
{
hash[i] = ((const unsigned char *) buf)[i];
}
}

CHash256BlockOrTransaction& Write(const unsigned char *data, size_t len) {
sha3_Update(&sha3context, data, len);
return *this;
}

CHash256BlockOrTransaction& Reset() {
sha3_Init256(&sha3context);
return *this;
}
};

/** A hasher class for BGL's 160-bit hash (SHA-256 + RIPEMD-160). */
class CHash160 {
private:
CSHA256 sha;
Expand Down Expand Up @@ -96,11 +157,11 @@ inline uint160 Hash160(const T1& in1)
return result;
}

/** A writer stream (for serialization) that computes a 256-bit hash. */
/** A writer stream (for serialization) that computes a 256-bit Keccak hash. */
class CHashWriter
{
private:
CSHA256 ctx;
CHash256BlockOrTransaction ctx;

const int nType;
const int nVersion;
Expand All @@ -115,6 +176,61 @@ class CHashWriter
ctx.Write((const unsigned char*)pch, size);
}

/** Compute the double-SHA256 hash of all data written to this object.
*
* Invalidates this object.
*/
uint256 GetHash() {
uint256 result;
ctx.Finalize((unsigned char*)&result);
return result;
}

/** Compute the SHA256 hash of all data written to this object.
*
* Invalidates this object.
*/
//uint256 GetSHA256() {
// uint256 result;
// ctx.Finalize(result.begin());
// return result;
//}

/**
* Returns the first 64 bits from the resulting hash.
*/
inline uint64_t GetCheapHash() {
unsigned char result[CHash256::OUTPUT_SIZE];
ctx.Finalize(result);
return ReadLE64(result);
}

template<typename T>
CHashWriter& operator<<(const T& obj) {
// Serialize to this stream
::Serialize(*this, obj);
return (*this);
}
};

class CHashWriterSHA256
{
private:
CHash256Single ctx;

const int nType;
const int nVersion;
public:

CHashWriterSHA256(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}

int GetType() const { return nType; }
int GetVersion() const { return nVersion; }

void write(const char *pch, size_t size) {
ctx.Write((const unsigned char*)pch, size);
}

/** Compute the double-SHA256 hash of all data written to this object.
*
* Invalidates this object.
Expand All @@ -125,6 +241,11 @@ class CHashWriter
ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin());
return result;
}
// uint256 GetHash() {
// uint256 result;
// ctx.Finalize(result.begin());
// return result;
// }

/** Compute the SHA256 hash of all data written to this object.
*
Expand All @@ -140,12 +261,13 @@ class CHashWriter
* Returns the first 64 bits from the resulting hash.
*/
inline uint64_t GetCheapHash() {
uint256 result = GetHash();
return ReadLE64(result.begin());
unsigned char result[CHash256::OUTPUT_SIZE];
ctx.Finalize(result);
return ReadLE64(result);
}

template<typename T>
CHashWriter& operator<<(const T& obj) {
CHashWriterSHA256& operator<<(const T& obj) {
// Serialize to this stream
::Serialize(*this, obj);
return (*this);
Expand Down Expand Up @@ -189,13 +311,21 @@ class CHashVerifier : public CHashWriter

/** Compute the 256-bit hash of an object's serialization. */
template<typename T>
uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
uint256 SerializeHashKeccak(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
{
CHashWriter ss(nType, nVersion);
ss << obj;
return ss.GetHash();
}

template<typename T>
uint256 SerializeHashSHA256(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
{
CHashWriterSHA256 ss(nType, nVersion);
ss << obj;
return ss.GetSHA256();
}

/** Single-SHA256 a 32-byte input (represented as uint256). */
[[nodiscard]] uint256 SHA256Uint256(const uint256& input);

Expand All @@ -209,6 +339,6 @@ void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char he
* A tagged hash can be computed by feeding the message into this object, and
* then calling CHashWriter::GetSHA256().
*/
CHashWriter TaggedHash(const std::string& tag);
CHashWriterSHA256 TaggedHash(const std::string& tag);

#endif // BGL_HASH_H
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
InitError(strprintf(_("Could not parse asmap file %s"), asmap_path));
return false;
}
const uint256 asmap_version = SerializeHash(asmap);
const uint256 asmap_version = SerializeHashSHA256(asmap);
node.connman->SetAsmap(std::move(asmap));
LogPrintf("Using asmap version %s for IP bucketing\n", asmap_version.ToString());
} else {
Expand Down
Loading

0 comments on commit 7af3521

Please sign in to comment.