| 75 | } |
| 76 | |
| 77 | h256 chunkHash(bytesConstRef const _data, bool _forceHigherLevel = false) |
| 78 | { |
| 79 | bytes dataToHash; |
| 80 | if (_data.size() < 0x1000) |
| 81 | dataToHash = _data.toBytes(); |
| 82 | else if (_data.size() == 0x1000 && !_forceHigherLevel) |
| 83 | dataToHash = _data.toBytes(); |
| 84 | else |
| 85 | { |
| 86 | size_t maxRepresentedSize = 0x1000; |
| 87 | while (maxRepresentedSize * (0x1000 / 32) < _data.size()) |
| 88 | 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 | bool forceHigher = maxRepresentedSize > 0x1000; |
| 92 | for (size_t i = 0; i < _data.size(); i += maxRepresentedSize) |
| 93 | { |
| 94 | size_t size = std::min(maxRepresentedSize, _data.size() - i); |
| 95 | dataToHash += chunkHash(_data.cropped(i, size), forceHigher).asBytes(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | dataToHash.resize(0x1000, 0); |
| 100 | return keccak256(toLittleEndian(_data.size()) + bmtHash(&dataToHash).asBytes()); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | } |