/src/solidity/libsolutil/SwarmHash.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | This file is part of solidity. |
3 | | |
4 | | solidity is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation, either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | solidity is distributed in the hope that it will be useful, |
10 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | GNU General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU General Public License |
15 | | along with solidity. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | /** @file SwarmHash.cpp |
19 | | */ |
20 | | |
21 | | #include <libsolutil/SwarmHash.h> |
22 | | |
23 | | #include <libsolutil/Keccak256.h> |
24 | | |
25 | | using namespace solidity; |
26 | | using namespace solidity::util; |
27 | | |
28 | | namespace |
29 | | { |
30 | | |
31 | | bytes toLittleEndian(uint64_t _size) |
32 | 17.2k | { |
33 | 17.2k | bytes encoded(8); |
34 | 154k | for (size_t i = 0; i < 8; ++i) |
35 | 137k | encoded[i] = (_size >> (8 * i)) & 0xff; |
36 | 17.2k | return encoded; |
37 | 17.2k | } |
38 | | |
39 | | h256 swarmHashSimple(bytesConstRef _data, size_t _size) |
40 | 0 | { |
41 | 0 | return keccak256(toLittleEndian(_size) + _data.toBytes()); |
42 | 0 | } |
43 | | |
44 | | h256 swarmHashIntermediate(std::string const& _input, size_t _offset, size_t _length) |
45 | 0 | { |
46 | 0 | bytesConstRef ref; |
47 | 0 | bytes innerNodes; |
48 | 0 | if (_length <= 0x1000) |
49 | 0 | ref = bytesConstRef(_input).cropped(_offset, _length); |
50 | 0 | else |
51 | 0 | { |
52 | 0 | size_t maxRepresentedSize = 0x1000; |
53 | 0 | while (maxRepresentedSize * (0x1000 / 32) < _length) |
54 | 0 | maxRepresentedSize *= (0x1000 / 32); |
55 | 0 | for (size_t i = 0; i < _length; i += maxRepresentedSize) |
56 | 0 | { |
57 | 0 | size_t size = std::min(maxRepresentedSize, _length - i); |
58 | 0 | innerNodes += swarmHashIntermediate(_input, _offset + i, size).asBytes(); |
59 | 0 | } |
60 | 0 | ref = bytesConstRef(&innerNodes); |
61 | 0 | } |
62 | 0 | return swarmHashSimple(ref, _length); |
63 | 0 | } |
64 | | |
65 | | h256 bmtHash(bytesConstRef _data) |
66 | 2.18M | { |
67 | 2.18M | if (_data.size() <= 64) |
68 | 1.10M | return keccak256(_data); |
69 | | |
70 | 1.08M | size_t midPoint = _data.size() / 2; |
71 | 1.08M | return keccak256( |
72 | 1.08M | bmtHash(_data.cropped(0, midPoint)).asBytes() + |
73 | 1.08M | bmtHash(_data.cropped(midPoint)).asBytes() |
74 | 1.08M | ); |
75 | 2.18M | } |
76 | | |
77 | | h256 chunkHash(bytesConstRef const _data, bool _forceHigherLevel = false) |
78 | 17.2k | { |
79 | 17.2k | bytes dataToHash; |
80 | 17.2k | if (_data.size() < 0x1000) |
81 | 14.9k | dataToHash = _data.toBytes(); |
82 | 2.28k | else if (_data.size() == 0x1000 && !_forceHigherLevel) |
83 | 1.97k | dataToHash = _data.toBytes(); |
84 | 310 | else |
85 | 310 | { |
86 | 310 | size_t maxRepresentedSize = 0x1000; |
87 | 311 | while (maxRepresentedSize * (0x1000 / 32) < _data.size()) |
88 | 1 | maxRepresentedSize *= (0x1000 / 32); |
89 | | // If remaining size is 0x1000, but maxRepresentedSize is not, |
90 | | // we have to still do one level of the chunk hashes. |
91 | 310 | bool forceHigher = maxRepresentedSize > 0x1000; |
92 | 2.59k | for (size_t i = 0; i < _data.size(); i += maxRepresentedSize) |
93 | 2.28k | { |
94 | 2.28k | size_t size = std::min(maxRepresentedSize, _data.size() - i); |
95 | 2.28k | dataToHash += chunkHash(_data.cropped(i, size), forceHigher).asBytes(); |
96 | 2.28k | } |
97 | 310 | } |
98 | | |
99 | 17.2k | dataToHash.resize(0x1000, 0); |
100 | 17.2k | return keccak256(toLittleEndian(_data.size()) + bmtHash(&dataToHash).asBytes()); |
101 | 17.2k | } |
102 | | |
103 | | |
104 | | } |
105 | | |
106 | | h256 solidity::util::bzzr0Hash(std::string const& _input) |
107 | 0 | { |
108 | 0 | return swarmHashIntermediate(_input, 0, _input.size()); |
109 | 0 | } |
110 | | |
111 | | |
112 | | h256 solidity::util::bzzr1Hash(bytes const& _input) |
113 | 14.9k | { |
114 | 14.9k | if (_input.empty()) |
115 | 0 | return h256{}; |
116 | 14.9k | return chunkHash(&_input); |
117 | 14.9k | } |