Skip to content

Instantly share code, notes, and snippets.

@AndrejMitrovic
Created September 23, 2020 02:16
Show Gist options
  • Save AndrejMitrovic/356280d445bc6af551049bc25a3f056c to your computer and use it in GitHub Desktop.
Save AndrejMitrovic/356280d445bc6af551049bc25a3f056c to your computer and use it in GitHub Desktop.
diff --git a/source/scpp/src/crypto/SHA.cpp b/source/scpp/src/crypto/SHA.cpp
index 47293deec..835d0fa60 100644
--- a/source/scpp/src/crypto/SHA.cpp
+++ b/source/scpp/src/crypto/SHA.cpp
@@ -10,76 +10,76 @@
namespace stellar
{
-// Plain SHA256
-uint256
-sha256(ByteSlice const& bin)
+// Plain SHA512
+uint512
+sha512(ByteSlice const& bin)
{
- uint256 out;
- if (crypto_hash_sha256(out.data(), bin.data(), bin.size()) != 0)
+ uint512 out;
+ if (crypto_hash_sha512(out.data(), bin.data(), bin.size()) != 0)
{
- throw std::runtime_error("error from crypto_hash_sha256");
+ throw std::runtime_error("error from crypto_hash_sha512");
}
return out;
}
-class SHA256Impl : public SHA256, NonCopyable
+class SHA512Impl : public SHA512, NonCopyable
{
- crypto_hash_sha256_state mState;
+ crypto_hash_sha512_state mState;
bool mFinished;
public:
- SHA256Impl();
+ SHA512Impl();
void reset() override;
void add(ByteSlice const& bin) override;
- uint256 finish() override;
+ uint512 finish() override;
};
-std::unique_ptr<SHA256>
-SHA256::create()
+std::unique_ptr<SHA512>
+SHA512::create()
{
- return std::make_unique<SHA256Impl>();
+ return std::make_unique<SHA512Impl>();
}
-SHA256Impl::SHA256Impl() : mFinished(false)
+SHA512Impl::SHA512Impl() : mFinished(false)
{
reset();
}
void
-SHA256Impl::reset()
+SHA512Impl::reset()
{
- if (crypto_hash_sha256_init(&mState) != 0)
+ if (crypto_hash_sha512_init(&mState) != 0)
{
- throw std::runtime_error("error from crypto_hash_sha256_init");
+ throw std::runtime_error("error from crypto_hash_sha512_init");
}
mFinished = false;
}
void
-SHA256Impl::add(ByteSlice const& bin)
+SHA512Impl::add(ByteSlice const& bin)
{
if (mFinished)
{
- throw std::runtime_error("adding bytes to finished SHA256");
+ throw std::runtime_error("adding bytes to finished SHA512");
}
- if (crypto_hash_sha256_update(&mState, bin.data(), bin.size()) != 0)
+ if (crypto_hash_sha512_update(&mState, bin.data(), bin.size()) != 0)
{
- throw std::runtime_error("error from crypto_hash_sha256_update");
+ throw std::runtime_error("error from crypto_hash_sha512_update");
}
}
-uint256
-SHA256Impl::finish()
+uint512
+SHA512Impl::finish()
{
- uint256 out;
- assert(out.size() == crypto_hash_sha256_BYTES);
+ uint512 out;
+ assert(out.size() == crypto_hash_sha512_BYTES);
if (mFinished)
{
- throw std::runtime_error("finishing already-finished SHA256");
+ throw std::runtime_error("finishing already-finished SHA512");
}
- if (crypto_hash_sha256_final(&mState, out.data()) != 0)
+ if (crypto_hash_sha512_final(&mState, out.data()) != 0)
{
- throw std::runtime_error("error from crypto_hash_sha256_final");
+ throw std::runtime_error("error from crypto_hash_sha512_final");
}
return out;
}
diff --git a/source/scpp/src/crypto/SHA.h b/source/scpp/src/crypto/SHA.h
index 91ea4ef69..4b09ba638 100644
--- a/source/scpp/src/crypto/SHA.h
+++ b/source/scpp/src/crypto/SHA.h
@@ -11,17 +11,17 @@
namespace stellar
{
-// Plain SHA256
-uint256 sha256(ByteSlice const& bin);
+// Plain SHA512
+uint512 sha512(ByteSlice const& bin);
-// SHA256 in incremental mode, for large inputs.
-class SHA256
+// SHA512 in incremental mode, for large inputs.
+class SHA512
{
public:
- static std::unique_ptr<SHA256> create();
- virtual ~SHA256(){};
+ static std::unique_ptr<SHA512> create();
+ virtual ~SHA512(){};
virtual void reset() = 0;
virtual void add(ByteSlice const& bin) = 0;
- virtual uint256 finish() = 0;
+ virtual uint512 finish() = 0;
};
}
diff --git a/source/scpp/src/scp/LocalNode.cpp b/source/scpp/src/scp/LocalNode.cpp
index 4ce4d51fc..0a5643452 100644
--- a/source/scpp/src/scp/LocalNode.cpp
+++ b/source/scpp/src/scp/LocalNode.cpp
@@ -24,14 +24,14 @@ LocalNode::LocalNode(NodeID const& nodeID, bool isValidator,
: mNodeID(nodeID), mIsValidator(isValidator), mQSet(qSet), mSCP(scp)
{
normalizeQSet(mQSet);
- mQSetHash = sha256(xdr::xdr_to_opaque(mQSet));
+ mQSetHash = sha512(xdr::xdr_to_opaque(mQSet));
CLOG(INFO, "SCP") << "LocalNode::LocalNode"
<< "@" << KeyUtils::toShortString(mNodeID)
<< " qSet: " << hexAbbrev(mQSetHash);
mSingleQSet = std::make_shared<SCPQuorumSet>(buildSingletonQSet(mNodeID));
- gSingleQSetHash = sha256(xdr::xdr_to_opaque(*mSingleQSet));
+ gSingleQSetHash = sha512(xdr::xdr_to_opaque(*mSingleQSet));
}
SCPQuorumSet
@@ -46,7 +46,7 @@ LocalNode::buildSingletonQSet(NodeID const& nodeID)
void
LocalNode::updateQuorumSet(SCPQuorumSet const& qSet)
{
- mQSetHash = sha256(xdr::xdr_to_opaque(qSet));
+ mQSetHash = sha512(xdr::xdr_to_opaque(qSet));
mQSet = qSet;
}
diff --git a/source/scpp/src/scp/SCPDriver.cpp b/source/scpp/src/scp/SCPDriver.cpp
index 256b87dc5..542968829 100644
--- a/source/scpp/src/scp/SCPDriver.cpp
+++ b/source/scpp/src/scp/SCPDriver.cpp
@@ -18,7 +18,7 @@ namespace stellar
std::string
SCPDriver::getValueString(Value const& v) const
{
- uint256 valueHash = sha256(xdr::xdr_to_opaque(v));
+ uint512 valueHash = sha512(xdr::xdr_to_opaque(v));
return hexAbbrev(valueHash);
}
@@ -42,13 +42,13 @@ static const uint32 hash_K = 3;
static uint64
hashHelper(uint64 slotIndex, Value const& prev,
- std::function<void(SHA256*)> extra)
+ std::function<void(SHA512*)> extra)
{
- auto h = SHA256::create();
+ auto h = SHA512::create();
h->add(xdr::xdr_to_opaque(slotIndex));
h->add(xdr::xdr_to_opaque(prev));
extra(h.get());
- uint256 t = h->finish();
+ uint512 t = h->finish();
uint64 res = 0;
for (size_t i = 0; i < sizeof(res); i++)
{
@@ -61,7 +61,7 @@ uint64
SCPDriver::computeHashNode(uint64 slotIndex, Value const& prev, bool isPriority,
int32_t roundNumber, NodeID const& nodeID)
{
- return hashHelper(slotIndex, prev, [&](SHA256* h) {
+ return hashHelper(slotIndex, prev, [&](SHA512* h) {
h->add(xdr::xdr_to_opaque(isPriority ? hash_P : hash_N));
h->add(xdr::xdr_to_opaque(roundNumber));
h->add(xdr::xdr_to_opaque(nodeID));
@@ -72,7 +72,7 @@ uint64
SCPDriver::computeValueHash(uint64 slotIndex, Value const& prev,
int32_t roundNumber, Value const& value)
{
- return hashHelper(slotIndex, prev, [&](SHA256* h) {
+ return hashHelper(slotIndex, prev, [&](SHA512* h) {
h->add(xdr::xdr_to_opaque(hash_K));
h->add(xdr::xdr_to_opaque(roundNumber));
h->add(xdr::xdr_to_opaque(value));
diff --git a/source/scpp/src/util/HashOfHash.cpp b/source/scpp/src/util/HashOfHash.cpp
index 1f89a6823..284f1309a 100644
--- a/source/scpp/src/util/HashOfHash.cpp
+++ b/source/scpp/src/util/HashOfHash.cpp
@@ -12,4 +12,13 @@ hash<stellar::uint256>::operator()(stellar::uint256 const& x) const noexcept
return res;
}
+
+size_t
+hash<stellar::uint512>::operator()(stellar::uint512 const& x) const noexcept
+{
+ size_t res =
+ stellar::shortHash::computeHash(stellar::ByteSlice(x.data(), 8));
+
+ return res;
+}
}
diff --git a/source/scpp/src/util/HashOfHash.h b/source/scpp/src/util/HashOfHash.h
index b4bd400ab..b9a466e7c 100644
--- a/source/scpp/src/util/HashOfHash.h
+++ b/source/scpp/src/util/HashOfHash.h
@@ -7,4 +7,9 @@ template <> struct hash<stellar::uint256>
{
size_t operator()(stellar::uint256 const& x) const noexcept;
};
+
+template <> struct hash<stellar::uint512>
+{
+ size_t operator()(stellar::uint512 const& x) const noexcept;
+};
}
diff --git a/source/scpp/src/xdr/Stellar-types.h b/source/scpp/src/xdr/Stellar-types.h
index 28ecc823b..94bfd09cb 100644
--- a/source/scpp/src/xdr/Stellar-types.h
+++ b/source/scpp/src/xdr/Stellar-types.h
@@ -9,8 +9,11 @@
namespace stellar {
-using Hash = xdr::opaque_array<32>;
+// note: Hash was changed to 64-bytes in #737.
+// uint256 was also changed to reflect that.
+using Hash = xdr::opaque_array<64>;
using uint256 = xdr::opaque_array<32>;
+using uint512 = xdr::opaque_array<64>;
using uint32 = std::uint32_t;
using int32 = std::int32_t;
using uint64 = std::uint64_t;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment