diff options
author | chriseth <chris@ethereum.org> | 2016-11-16 19:25:20 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-16 19:25:20 +0800 |
commit | c811691861eb51520d9fd51d56770f14990b0320 (patch) | |
tree | 519822545c88474eff6cc076dfbbd069c7ef5a8d /libdevcore | |
parent | 192a81892dca3762c050f159da202c8c8f90f72c (diff) | |
parent | 518fe2aab7b96dc9cd259049dec80be509860a43 (diff) | |
download | dexon-solidity-c811691861eb51520d9fd51d56770f14990b0320.tar.gz dexon-solidity-c811691861eb51520d9fd51d56770f14990b0320.tar.zst dexon-solidity-c811691861eb51520d9fd51d56770f14990b0320.zip |
Merge pull request #1379 from ethereum/swarmHashCorrection
Correct implementation of swarm hash.
Diffstat (limited to 'libdevcore')
-rw-r--r-- | libdevcore/SwarmHash.cpp | 38 |
1 files changed, 16 insertions, 22 deletions
diff --git a/libdevcore/SwarmHash.cpp b/libdevcore/SwarmHash.cpp index 583d84b1..e7b844eb 100644 --- a/libdevcore/SwarmHash.cpp +++ b/libdevcore/SwarmHash.cpp @@ -38,32 +38,26 @@ h256 swarmHashSimple(bytesConstRef _data, size_t _size) return keccak256(toLittleEndian(_size) + _data.toBytes()); } -h256 dev::swarmHash(bytes const& _input) +h256 swarmHashIntermediate(bytes const& _input, size_t _offset, size_t _length) { - bytes data = _input; - size_t lastChunkSize = 0; - size_t level = 0; - do + if (_length <= 0x1000) + return swarmHashSimple(bytesConstRef(_input.data() + _offset, _length), _length); + else { bytes innerNodes; - size_t i = 0; - do + size_t maxRepresentedSize = 0x1000; + while (maxRepresentedSize * (0x1000 / 32) < _length) + maxRepresentedSize *= (0x1000 / 32); + for (size_t i = 0; i < _length; i += maxRepresentedSize) { - size_t bytes = std::min<size_t>(0x1000, data.size() - i); - size_t size = bytes << (7 * level); - if (i + 0x1000 >= data.size()) - { - // last node - size = level == 0 ? bytes : ((bytes - 32) << (7 * level)) + lastChunkSize; - lastChunkSize = size; - } - innerNodes += swarmHashSimple(bytesConstRef(_input.data() + i, bytes), size).asBytes(); - i += 0x1000; + size_t size = std::min(maxRepresentedSize, _length - i); + innerNodes += swarmHashIntermediate(_input, _offset + i, size).asBytes(); } - while (i < data.size()); - data = std::move(innerNodes); - level++; + return swarmHashSimple(bytesConstRef(&innerNodes), _length); } - while (data.size() > 32); - return h256(data); +} + +h256 dev::swarmHash(bytes const& _input) +{ + return swarmHashIntermediate(_input, 0, _input.size()); } |