diff options
author | chriseth <c@ethdev.com> | 2016-11-15 21:55:19 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2016-11-15 21:55:51 +0800 |
commit | 518fe2aab7b96dc9cd259049dec80be509860a43 (patch) | |
tree | 4e290aee6d7799a5c43bae9cd12d1e7265828b79 /libdevcore | |
parent | 0072160d7772b2f30c2c6af4428728cb31641696 (diff) | |
download | dexon-solidity-518fe2aab7b96dc9cd259049dec80be509860a43.tar dexon-solidity-518fe2aab7b96dc9cd259049dec80be509860a43.tar.gz dexon-solidity-518fe2aab7b96dc9cd259049dec80be509860a43.tar.bz2 dexon-solidity-518fe2aab7b96dc9cd259049dec80be509860a43.tar.lz dexon-solidity-518fe2aab7b96dc9cd259049dec80be509860a43.tar.xz dexon-solidity-518fe2aab7b96dc9cd259049dec80be509860a43.tar.zst dexon-solidity-518fe2aab7b96dc9cd259049dec80be509860a43.zip |
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()); } |