From 518fe2aab7b96dc9cd259049dec80be509860a43 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 15 Nov 2016 14:55:19 +0100 Subject: Correct implementation of swarm hash. --- libdevcore/SwarmHash.cpp | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) (limited to 'libdevcore/SwarmHash.cpp') 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(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()); } -- cgit