Coverage Report

Created: 2022-08-24 06:31

/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 std;
26
using namespace solidity;
27
using namespace solidity::util;
28
29
namespace
30
{
31
32
bytes toLittleEndian(uint64_t _size)
33
0
{
34
0
  bytes encoded(8);
35
0
  for (size_t i = 0; i < 8; ++i)
36
0
    encoded[i] = (_size >> (8 * i)) & 0xff;
37
0
  return encoded;
38
0
}
39
40
h256 swarmHashSimple(bytesConstRef _data, size_t _size)
41
0
{
42
0
  return keccak256(toLittleEndian(_size) + _data.toBytes());
43
0
}
44
45
h256 swarmHashIntermediate(string const& _input, size_t _offset, size_t _length)
46
0
{
47
0
  bytesConstRef ref;
48
0
  bytes innerNodes;
49
0
  if (_length <= 0x1000)
50
0
    ref = bytesConstRef(_input).cropped(_offset, _length);
51
0
  else
52
0
  {
53
0
    size_t maxRepresentedSize = 0x1000;
54
0
    while (maxRepresentedSize * (0x1000 / 32) < _length)
55
0
      maxRepresentedSize *= (0x1000 / 32);
56
0
    for (size_t i = 0; i < _length; i += maxRepresentedSize)
57
0
    {
58
0
      size_t size = std::min(maxRepresentedSize, _length - i);
59
0
      innerNodes += swarmHashIntermediate(_input, _offset + i, size).asBytes();
60
0
    }
61
0
    ref = bytesConstRef(&innerNodes);
62
0
  }
63
0
  return swarmHashSimple(ref, _length);
64
0
}
65
66
h256 bmtHash(bytesConstRef _data)
67
0
{
68
0
  if (_data.size() <= 64)
69
0
    return keccak256(_data);
70
71
0
  size_t midPoint = _data.size() / 2;
72
0
  return keccak256(
73
0
    bmtHash(_data.cropped(0, midPoint)).asBytes() +
74
0
    bmtHash(_data.cropped(midPoint)).asBytes()
75
0
  );
76
0
}
77
78
h256 chunkHash(bytesConstRef const _data, bool _forceHigherLevel = false)
79
0
{
80
0
  bytes dataToHash;
81
0
  if (_data.size() < 0x1000)
82
0
    dataToHash = _data.toBytes();
83
0
  else if (_data.size() == 0x1000 && !_forceHigherLevel)
84
0
    dataToHash = _data.toBytes();
85
0
  else
86
0
  {
87
0
    size_t maxRepresentedSize = 0x1000;
88
0
    while (maxRepresentedSize * (0x1000 / 32) < _data.size())
89
0
      maxRepresentedSize *= (0x1000 / 32);
90
    // If remaining size is 0x1000, but maxRepresentedSize is not,
91
    // we have to still do one level of the chunk hashes.
92
0
    bool forceHigher = maxRepresentedSize > 0x1000;
93
0
    for (size_t i = 0; i < _data.size(); i += maxRepresentedSize)
94
0
    {
95
0
      size_t size = std::min(maxRepresentedSize, _data.size() - i);
96
0
      dataToHash += chunkHash(_data.cropped(i, size), forceHigher).asBytes();
97
0
    }
98
0
  }
99
100
0
  dataToHash.resize(0x1000, 0);
101
0
  return keccak256(toLittleEndian(_data.size()) + bmtHash(&dataToHash).asBytes());
102
0
}
103
104
105
}
106
107
h256 solidity::util::bzzr0Hash(string const& _input)
108
0
{
109
0
  return swarmHashIntermediate(_input, 0, _input.size());
110
0
}
111
112
113
h256 solidity::util::bzzr1Hash(bytes const& _input)
114
0
{
115
0
  if (_input.empty())
116
0
    return h256{};
117
0
  return chunkHash(&_input);
118
0
}