mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Update swarm hash.
This commit is contained in:
parent
8d18003808
commit
aa11ad7d0a
@ -61,9 +61,56 @@ h256 swarmHashIntermediate(string const& _input, size_t _offset, size_t _length)
|
|||||||
return swarmHashSimple(ref, _length);
|
return swarmHashSimple(ref, _length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h256 bmtHash(bytesConstRef _data)
|
||||||
|
{
|
||||||
|
if (_data.size() <= 64)
|
||||||
|
return keccak256(_data);
|
||||||
|
|
||||||
|
size_t midPoint = _data.size() / 2;
|
||||||
|
return keccak256(
|
||||||
|
bmtHash(_data.cropped(0, midPoint)).asBytes() +
|
||||||
|
bmtHash(_data.cropped(midPoint)).asBytes()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
h256 dev::swarmHash(string const& _input)
|
h256 chunkHash(bytesConstRef const _data, bool _forceHigherLevel = false)
|
||||||
|
{
|
||||||
|
bytes dataToHash;
|
||||||
|
if (_data.size() < 0x1000)
|
||||||
|
dataToHash = _data.toBytes();
|
||||||
|
else if (_data.size() == 0x1000 && !_forceHigherLevel)
|
||||||
|
dataToHash = _data.toBytes();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
size_t maxRepresentedSize = 0x1000;
|
||||||
|
while (maxRepresentedSize * (0x1000 / 32) < _data.size())
|
||||||
|
maxRepresentedSize *= (0x1000 / 32);
|
||||||
|
// If remaining size is 0x1000, but maxRepresentedSize is not,
|
||||||
|
// we have to still do one level of the chunk hashes.
|
||||||
|
bool forceHigher = maxRepresentedSize > 0x1000;
|
||||||
|
for (size_t i = 0; i < _data.size(); i += maxRepresentedSize)
|
||||||
|
{
|
||||||
|
size_t size = std::min(maxRepresentedSize, _data.size() - i);
|
||||||
|
dataToHash += chunkHash(_data.cropped(i, size), forceHigher).asBytes();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dataToHash.resize(0x1000, 0);
|
||||||
|
return keccak256(toLittleEndian(_data.size()) + bmtHash(&dataToHash).asBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
h256 dev::bzzr0Hash(string const& _input)
|
||||||
{
|
{
|
||||||
return swarmHashIntermediate(_input, 0, _input.size());
|
return swarmHashIntermediate(_input, 0, _input.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
h256 dev::bzzr1Hash(bytes const& _input)
|
||||||
|
{
|
||||||
|
if (_input.empty())
|
||||||
|
return h256{};
|
||||||
|
return chunkHash(&_input);
|
||||||
|
}
|
||||||
|
@ -26,7 +26,15 @@
|
|||||||
namespace dev
|
namespace dev
|
||||||
{
|
{
|
||||||
|
|
||||||
/// Compute the "swarm hash" of @a _input
|
/// Compute the "swarm hash" of @a _input (OLD 0x1000-section version)
|
||||||
h256 swarmHash(std::string const& _input);
|
h256 bzzr0Hash(std::string const& _input);
|
||||||
|
|
||||||
|
/// Compute the "bzz hash" of @a _input (the NEW binary / BMT version)
|
||||||
|
h256 bzzr1Hash(bytes const& _input);
|
||||||
|
|
||||||
|
inline h256 bzzr1Hash(std::string const& _input)
|
||||||
|
{
|
||||||
|
return bzzr1Hash(asBytes(_input));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -784,7 +784,7 @@ h256 const& CompilerStack::Source::keccak256() const
|
|||||||
h256 const& CompilerStack::Source::swarmHash() const
|
h256 const& CompilerStack::Source::swarmHash() const
|
||||||
{
|
{
|
||||||
if (swarmHashCached == h256{})
|
if (swarmHashCached == h256{})
|
||||||
swarmHashCached = dev::swarmHash(scanner->source());
|
swarmHashCached = dev::bzzr0Hash(scanner->source());
|
||||||
return swarmHashCached;
|
return swarmHashCached;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1232,7 +1232,7 @@ private:
|
|||||||
bytes CompilerStack::createCBORMetadata(string const& _metadata, bool _experimentalMode)
|
bytes CompilerStack::createCBORMetadata(string const& _metadata, bool _experimentalMode)
|
||||||
{
|
{
|
||||||
MetadataCBOREncoder encoder;
|
MetadataCBOREncoder encoder;
|
||||||
encoder.pushBytes("bzzr0", dev::swarmHash(_metadata).asBytes());
|
encoder.pushBytes("bzzr0", dev::bzzr0Hash(_metadata).asBytes());
|
||||||
if (_experimentalMode)
|
if (_experimentalMode)
|
||||||
encoder.pushBool("experimental", true);
|
encoder.pushBool("experimental", true);
|
||||||
if (m_release)
|
if (m_release)
|
||||||
|
@ -22,6 +22,8 @@
|
|||||||
|
|
||||||
#include <test/Options.h>
|
#include <test/Options.h>
|
||||||
|
|
||||||
|
#include <libdevcore/Keccak256.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
namespace dev
|
namespace dev
|
||||||
@ -31,24 +33,68 @@ namespace test
|
|||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE(SwarmHash)
|
BOOST_AUTO_TEST_SUITE(SwarmHash)
|
||||||
|
|
||||||
string swarmHashHex(string const& _input)
|
string bzzr0HashHex(string const& _input)
|
||||||
{
|
{
|
||||||
return toHex(swarmHash(_input).asBytes());
|
return toHex(bzzr0Hash(_input).asBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
string bzzr1HashHex(bytes const& _input)
|
||||||
|
{
|
||||||
|
return toHex(bzzr1Hash(_input).asBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes sequence(size_t _length)
|
||||||
|
{
|
||||||
|
bytes data;
|
||||||
|
for (size_t i = 0; i < _length; i++)
|
||||||
|
data.push_back(uint8_t((i % 255) & 0xff));
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(test_zeros)
|
BOOST_AUTO_TEST_CASE(test_zeros)
|
||||||
{
|
{
|
||||||
BOOST_CHECK_EQUAL(swarmHashHex(string()), string("011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce"));
|
BOOST_CHECK_EQUAL(bzzr0HashHex(string()), string("011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce"));
|
||||||
BOOST_CHECK_EQUAL(swarmHashHex(string(0x1000 - 1, 0)), string("32f0faabc4265ac238cd945087133ce3d7e9bb2e536053a812b5373c54043adb"));
|
BOOST_CHECK_EQUAL(bzzr0HashHex(string(0x1000 - 1, 0)), string("32f0faabc4265ac238cd945087133ce3d7e9bb2e536053a812b5373c54043adb"));
|
||||||
BOOST_CHECK_EQUAL(swarmHashHex(string(0x1000, 0)), string("411dd45de7246e94589ff5888362c41e85bd3e582a92d0fda8f0e90b76439bec"));
|
BOOST_CHECK_EQUAL(bzzr0HashHex(string(0x1000, 0)), string("411dd45de7246e94589ff5888362c41e85bd3e582a92d0fda8f0e90b76439bec"));
|
||||||
BOOST_CHECK_EQUAL(swarmHashHex(string(0x1000 + 1, 0)), string("69754a0098432bbc2e84fe1205276870748a61a065ab6ef44d6a2e7b13ce044d"));
|
BOOST_CHECK_EQUAL(bzzr0HashHex(string(0x1000 + 1, 0)), string("69754a0098432bbc2e84fe1205276870748a61a065ab6ef44d6a2e7b13ce044d"));
|
||||||
BOOST_CHECK_EQUAL(swarmHashHex(string(0x2000 - 1, 0)), string("69ad3c581043404f775ffa8d6f1b25ad4a9ee812971190e90209c0966116a321"));
|
BOOST_CHECK_EQUAL(bzzr0HashHex(string(0x2000 - 1, 0)), string("69ad3c581043404f775ffa8d6f1b25ad4a9ee812971190e90209c0966116a321"));
|
||||||
BOOST_CHECK_EQUAL(swarmHashHex(string(0x2000, 0)), string("f00222373ff82d0a178dc6271c78953e9c88f74130a52d401f5ec51475f63c43"));
|
BOOST_CHECK_EQUAL(bzzr0HashHex(string(0x2000, 0)), string("f00222373ff82d0a178dc6271c78953e9c88f74130a52d401f5ec51475f63c43"));
|
||||||
BOOST_CHECK_EQUAL(swarmHashHex(string(0x2000 + 1, 0)), string("86d6773e79e02fd8145ee1aedba89ace0c15f2566db1249654000039a9a134bf"));
|
BOOST_CHECK_EQUAL(bzzr0HashHex(string(0x2000 + 1, 0)), string("86d6773e79e02fd8145ee1aedba89ace0c15f2566db1249654000039a9a134bf"));
|
||||||
BOOST_CHECK_EQUAL(swarmHashHex(string(0x80000, 0)), string("cc0854fe2c6b98e920d5c14b1a88e6d4223e55b8f78883f60939aa2485e361bf"));
|
BOOST_CHECK_EQUAL(bzzr0HashHex(string(0x80000, 0)), string("cc0854fe2c6b98e920d5c14b1a88e6d4223e55b8f78883f60939aa2485e361bf"));
|
||||||
BOOST_CHECK_EQUAL(swarmHashHex(string(0x80020, 0)), string("ee9ffca246e70d3704740ba4df450fa6988d14a1c2439c7e734c7a77a4eb6fd3"));
|
BOOST_CHECK_EQUAL(bzzr0HashHex(string(0x80020, 0)), string("ee9ffca246e70d3704740ba4df450fa6988d14a1c2439c7e734c7a77a4eb6fd3"));
|
||||||
BOOST_CHECK_EQUAL(swarmHashHex(string(0x800020, 0)), string("78b90b20c90559fb904535181a7c28929ea2f30a2329dbc25232de579709f12f"));
|
BOOST_CHECK_EQUAL(bzzr0HashHex(string(0x800020, 0)), string("78b90b20c90559fb904535181a7c28929ea2f30a2329dbc25232de579709f12f"));
|
||||||
BOOST_CHECK_EQUAL(swarmHashHex(string(2095104, 0)), string("a9958184589fc11b4027a4c233e777ebe2e99c66f96b74aef2a0638a94dd5439"));
|
BOOST_CHECK_EQUAL(bzzr0HashHex(string(2095104, 0)), string("a9958184589fc11b4027a4c233e777ebe2e99c66f96b74aef2a0638a94dd5439"));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(bzz_hash_short)
|
||||||
|
{
|
||||||
|
// Special case: 32 zero bytes
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(bytes()), toHex(bytes(32, 0)));
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(bytes(1, 0)), "fe60ba40b87599ddfb9e8947c1c872a4a1a5b56f7d1b80f0a646005b38db52a5");
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(bytes(31, 0)), "36fe2d14c5fe9ed380dc67afd9da6c5824bffcb01ac7972219c3cc3b1c8cd6b1");
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(asBytes("hello world")), "92672a471f4419b255d7cb0cf313474a6f5856fb347c5ece85fb706d644b630f");
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(bytes(64, 0)), "24090f674316c306ea2a98bdd08f042d6f776d0ae1c23b27fca52750a9c7d4e5");
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(bytes(65, 0)), "6ab1eaa91095215e30cacf47131d06ce5e9fc01611e406409705e190ee4440c6");
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(bytes(4096, 0)), "09ae927d0f3aaa37324df178928d3826820f3dd3388ce4aaebfc3af410bde23a");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(bzz_hash_large)
|
||||||
|
{
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(bytes(4097, 0)), "c082943c4cb8a97c67947f290f5421cf4c61d021eb303c8df77de6fe208df516");
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(bytes(4096 * 128, 0)), "392edbfc185187265cb5d50c2507965f2bb99ce8c255a24d3eb14257e40f2e33");
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(bytes(4096 * 128 * 2, 0)), "f89af84ac550cdaa79639d5f6a1591ff1c9b3cb5d1fc55651ca63d4f80375447");
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(bytes(4096 * 129, 0)), "8ab36734a15aef65221b024e47ee9a6bb727d8101eec42db364eef42285beb1d");
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(bytes(4096 * 130, 0)), "21eafb87f2a2a7e51d96212296f8e970103add0527f2fc61fae99f244847a42d");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(bzz_hash_nonzero)
|
||||||
|
{
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(sequence(65)), "541552bae05e9a63a6cb561f69edf36ffe073e441667dbf7a0e9a3864bb744ea");
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(sequence(4096)), "c10090961e7682a10890c334d759a28426647141213abda93b096b892824d2ef");
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(sequence(4096 * 128 + 31)), "e5c76afa931e33ac94bce2e754b1bb6407d07f738f67856783d93934ca8fc576");
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(sequence(4096 * 128 + 33)), "c2489aebad937b19384b61c7bd4ba494f9b14a710b2bcccce792856fb8fcfb3d");
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(sequence(4096 * 129)), "b7e298f61b1bf23e21d8f45bf545eb1d6c0c4eaaca7d2c2690fb86038404a6d6");
|
||||||
|
BOOST_CHECK_EQUAL(bzzr1HashHex(sequence(4096 * 130)), "59de730bf6c67a941f3b2ffa2f920acfaa1713695ad5deea12b4a121e5f23fa1");
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
@ -40,7 +40,7 @@ namespace test
|
|||||||
#define CHECK_DEPLOY_GAS(_gasNoOpt, _gasOpt) \
|
#define CHECK_DEPLOY_GAS(_gasNoOpt, _gasOpt) \
|
||||||
do \
|
do \
|
||||||
{ \
|
{ \
|
||||||
u256 bzzr0Cost = GasMeter::dataGas(dev::swarmHash(m_compiler.metadata(m_compiler.lastContractName())).asBytes(), true); \
|
u256 bzzr0Cost = GasMeter::dataGas(dev::bzzr0Hash(m_compiler.metadata(m_compiler.lastContractName())).asBytes(), true); \
|
||||||
u256 gasOpt{_gasOpt}; \
|
u256 gasOpt{_gasOpt}; \
|
||||||
u256 gasNoOpt{_gasNoOpt}; \
|
u256 gasNoOpt{_gasNoOpt}; \
|
||||||
u256 gas = m_optimiserSettings == OptimiserSettings::minimal() ? gasNoOpt : gasOpt; \
|
u256 gas = m_optimiserSettings == OptimiserSettings::minimal() ? gasNoOpt : gasOpt; \
|
||||||
|
@ -70,7 +70,7 @@ BOOST_AUTO_TEST_CASE(metadata_stamp)
|
|||||||
bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
|
bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
|
||||||
std::string const& metadata = compilerStack.metadata("test");
|
std::string const& metadata = compilerStack.metadata("test");
|
||||||
BOOST_CHECK(dev::test::isValidMetadata(metadata));
|
BOOST_CHECK(dev::test::isValidMetadata(metadata));
|
||||||
bytes hash = dev::swarmHash(metadata).asBytes();
|
bytes hash = dev::bzzr0Hash(metadata).asBytes();
|
||||||
BOOST_REQUIRE(hash.size() == 32);
|
BOOST_REQUIRE(hash.size() == 32);
|
||||||
auto const cborMetadata = requireParsedCBORMetadata(bytecode);
|
auto const cborMetadata = requireParsedCBORMetadata(bytecode);
|
||||||
BOOST_CHECK(cborMetadata.size() == 2);
|
BOOST_CHECK(cborMetadata.size() == 2);
|
||||||
@ -105,7 +105,7 @@ BOOST_AUTO_TEST_CASE(metadata_stamp_experimental)
|
|||||||
bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
|
bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
|
||||||
std::string const& metadata = compilerStack.metadata("test");
|
std::string const& metadata = compilerStack.metadata("test");
|
||||||
BOOST_CHECK(dev::test::isValidMetadata(metadata));
|
BOOST_CHECK(dev::test::isValidMetadata(metadata));
|
||||||
bytes hash = dev::swarmHash(metadata).asBytes();
|
bytes hash = dev::bzzr0Hash(metadata).asBytes();
|
||||||
BOOST_REQUIRE(hash.size() == 32);
|
BOOST_REQUIRE(hash.size() == 32);
|
||||||
auto const cborMetadata = requireParsedCBORMetadata(bytecode);
|
auto const cborMetadata = requireParsedCBORMetadata(bytecode);
|
||||||
BOOST_CHECK(cborMetadata.size() == 3);
|
BOOST_CHECK(cborMetadata.size() == 3);
|
||||||
|
Loading…
Reference in New Issue
Block a user