/src/solidity/libsolutil/FixedHash.h
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 FixedHash.h |
19 | | * @author Gav Wood <i@gavwood.com> |
20 | | * @date 2014 |
21 | | * |
22 | | * The FixedHash fixed-size "hash" container type. |
23 | | */ |
24 | | |
25 | | #pragma once |
26 | | |
27 | | #include <libsolutil/CommonData.h> |
28 | | #include <libsolutil/Numeric.h> |
29 | | |
30 | | #include <boost/functional/hash.hpp> |
31 | | #include <boost/io/ios_state.hpp> |
32 | | |
33 | | #include <array> |
34 | | #include <cstdint> |
35 | | #include <algorithm> |
36 | | |
37 | | namespace solidity::util |
38 | | { |
39 | | |
40 | | /// Fixed-size raw-byte array container type, with an API optimised for storing hashes. |
41 | | /// Transparently converts to/from the corresponding arithmetic type; this will |
42 | | /// assume the data contained in the hash is big-endian. |
43 | | template <unsigned N> |
44 | | class FixedHash |
45 | | { |
46 | | public: |
47 | | /// The corresponding arithmetic type. |
48 | | using Arith = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N * 8, N * 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>; |
49 | | |
50 | | /// The size of the container. |
51 | | enum { size = N }; |
52 | | static_assert(N != 0); |
53 | | |
54 | | /// Method to convert from a string. |
55 | | enum ConstructFromStringType { FromHex, FromBinary }; |
56 | | |
57 | | /// Method to convert from a string. |
58 | | enum ConstructFromHashType { AlignLeft, AlignRight, FailIfDifferent }; |
59 | | |
60 | | /// Construct an empty hash. |
61 | 48.6k | explicit FixedHash() { m_data.fill(0); } Unexecuted instantiation: solidity::util::FixedHash<20u>::FixedHash() solidity::util::FixedHash<32u>::FixedHash() Line | Count | Source | 61 | 48.6k | explicit FixedHash() { m_data.fill(0); } |
|
62 | | |
63 | | /// Construct from another hash, filling with zeroes or cropping as necessary. |
64 | | template <unsigned M> explicit FixedHash(FixedHash<M> const& _h, ConstructFromHashType _t = AlignLeft) |
65 | 716 | { |
66 | 716 | m_data.fill(0); |
67 | 716 | unsigned c = std::min(M, N); |
68 | 9.30k | for (unsigned i = 0; i < c; ++i) |
69 | 8.59k | m_data[_t == AlignRight ? N - 1 - i : i] = _h[_t == AlignRight ? M - 1 - i : i]; |
70 | 716 | } solidity::util::FixedHash<20u>::FixedHash<32u>(solidity::util::FixedHash<32u> const&, solidity::util::FixedHash<20u>::ConstructFromHashType) Line | Count | Source | 65 | 358 | { | 66 | 358 | m_data.fill(0); | 67 | 358 | unsigned c = std::min(M, N); | 68 | 7.51k | for (unsigned i = 0; i < c; ++i) | 69 | 7.16k | m_data[_t == AlignRight ? N - 1 - i : i] = _h[_t == AlignRight ? M - 1 - i : i]; | 70 | 358 | } |
solidity::util::FixedHash<4u>::FixedHash<32u>(solidity::util::FixedHash<32u> const&, solidity::util::FixedHash<4u>::ConstructFromHashType) Line | Count | Source | 65 | 358 | { | 66 | 358 | m_data.fill(0); | 67 | 358 | unsigned c = std::min(M, N); | 68 | 1.79k | for (unsigned i = 0; i < c; ++i) | 69 | 1.43k | m_data[_t == AlignRight ? N - 1 - i : i] = _h[_t == AlignRight ? M - 1 - i : i]; | 70 | 358 | } |
|
71 | | |
72 | | /// Convert from the corresponding arithmetic type. |
73 | 0 | FixedHash(Arith const& _arith) { toBigEndian(_arith, m_data); } |
74 | | |
75 | | /// Explicitly construct, copying from a byte array. |
76 | | explicit FixedHash(bytes const& _array, ConstructFromHashType _sizeMismatchBehavior = FailIfDifferent) |
77 | 716 | { |
78 | 716 | if (_array.size() == N) |
79 | 716 | memcpy(m_data.data(), _array.data(), _array.size()); |
80 | 0 | else |
81 | 0 | { |
82 | 0 | m_data.fill(0); |
83 | 0 | if (_sizeMismatchBehavior != FailIfDifferent) |
84 | 0 | { |
85 | 0 | auto bytesToCopy = std::min<size_t>(_array.size(), N); |
86 | 0 | for (size_t i = 0; i < bytesToCopy; ++i) |
87 | 0 | if (_sizeMismatchBehavior == AlignRight) |
88 | 0 | m_data[N - 1 - i] = _array[_array.size() - 1 - i]; |
89 | 0 | else |
90 | 0 | m_data[i] = _array[i]; |
91 | 0 | } |
92 | 0 | } |
93 | 716 | } Unexecuted instantiation: solidity::util::FixedHash<20u>::FixedHash(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&, solidity::util::FixedHash<20u>::ConstructFromHashType) solidity::util::FixedHash<32u>::FixedHash(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&, solidity::util::FixedHash<32u>::ConstructFromHashType) Line | Count | Source | 77 | 716 | { | 78 | 716 | if (_array.size() == N) | 79 | 716 | memcpy(m_data.data(), _array.data(), _array.size()); | 80 | 0 | else | 81 | 0 | { | 82 | 0 | m_data.fill(0); | 83 | 0 | if (_sizeMismatchBehavior != FailIfDifferent) | 84 | 0 | { | 85 | 0 | auto bytesToCopy = std::min<size_t>(_array.size(), N); | 86 | 0 | for (size_t i = 0; i < bytesToCopy; ++i) | 87 | 0 | if (_sizeMismatchBehavior == AlignRight) | 88 | 0 | m_data[N - 1 - i] = _array[_array.size() - 1 - i]; | 89 | 0 | else | 90 | 0 | m_data[i] = _array[i]; | 91 | 0 | } | 92 | 0 | } | 93 | 716 | } |
|
94 | | |
95 | | /// Explicitly construct, copying from a byte array. |
96 | | explicit FixedHash(bytesConstRef _b, ConstructFromHashType _t = FailIfDifferent) |
97 | 0 | { |
98 | 0 | if (_b.size() == N) |
99 | 0 | memcpy(m_data.data(), _b.data(), std::min<size_t>(_b.size(), N)); |
100 | 0 | else |
101 | 0 | { |
102 | 0 | m_data.fill(0); |
103 | 0 | if (_t != FailIfDifferent) |
104 | 0 | { |
105 | 0 | auto c = std::min<size_t>(_b.size(), N); |
106 | 0 | for (size_t i = 0; i < c; ++i) |
107 | 0 | if (_t == AlignRight) |
108 | 0 | m_data[N - 1 - i] = _b[_b.size() - 1 - i]; |
109 | 0 | else |
110 | 0 | m_data[i] = _b[i]; |
111 | 0 | } |
112 | 0 | } |
113 | 0 | } |
114 | | |
115 | | /// Explicitly construct, copying from a string. |
116 | | explicit FixedHash(std::string const& _s, ConstructFromStringType _t = FromHex, ConstructFromHashType _ht = FailIfDifferent): |
117 | | FixedHash(_t == FromHex ? fromHex(_s, WhenError::Throw) : solidity::util::asBytes(_s), _ht) |
118 | 0 | {} |
119 | | |
120 | | /// Convert to arithmetic type. |
121 | 1.07k | operator Arith() const { return fromBigEndian<Arith>(m_data); } solidity::util::FixedHash<32u>::operator boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<256u, 256u, (boost::multiprecision::cpp_integer_type)0, (boost::multiprecision::cpp_int_check_type)0, void>, (boost::multiprecision::expression_template_option)0>() const Line | Count | Source | 121 | 716 | operator Arith() const { return fromBigEndian<Arith>(m_data); } |
solidity::util::FixedHash<4u>::operator boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<32u, 32u, (boost::multiprecision::cpp_integer_type)0, (boost::multiprecision::cpp_int_check_type)0, void>, (boost::multiprecision::expression_template_option)0>() const Line | Count | Source | 121 | 358 | operator Arith() const { return fromBigEndian<Arith>(m_data); } |
|
122 | | |
123 | | // The obvious comparison operators. |
124 | 716 | bool operator==(FixedHash const& _c) const { return m_data == _c.m_data; } |
125 | | bool operator!=(FixedHash const& _c) const { return m_data != _c.m_data; } |
126 | | /// Required to sort objects of this type or use them as map keys. |
127 | 1.43k | bool operator<(FixedHash const& _c) const { |
128 | 7.16k | for (unsigned i = 0; i < N; ++i) |
129 | 5.72k | { |
130 | 5.72k | if (m_data[i] < _c.m_data[i]) |
131 | 0 | return true; |
132 | 5.72k | else if (m_data[i] > _c.m_data[i]) |
133 | 0 | return false; |
134 | 5.72k | } |
135 | 1.43k | return false; |
136 | 1.43k | } Unexecuted instantiation: solidity::util::FixedHash<32u>::operator<(solidity::util::FixedHash<32u> const&) const solidity::util::FixedHash<4u>::operator<(solidity::util::FixedHash<4u> const&) const Line | Count | Source | 127 | 1.43k | bool operator<(FixedHash const& _c) const { | 128 | 7.16k | for (unsigned i = 0; i < N; ++i) | 129 | 5.72k | { | 130 | 5.72k | if (m_data[i] < _c.m_data[i]) | 131 | 0 | return true; | 132 | 5.72k | else if (m_data[i] > _c.m_data[i]) | 133 | 0 | return false; | 134 | 5.72k | } | 135 | 1.43k | return false; | 136 | 1.43k | } |
|
137 | | |
138 | | /// @returns a particular byte from the hash. |
139 | 0 | uint8_t& operator[](unsigned _i) { return m_data[_i]; } |
140 | | /// @returns a particular byte from the hash. |
141 | 27.2k | uint8_t operator[](unsigned _i) const { return m_data[_i]; } solidity::util::FixedHash<20u>::operator[](unsigned int) const Line | Count | Source | 141 | 7.16k | uint8_t operator[](unsigned _i) const { return m_data[_i]; } |
solidity::util::FixedHash<32u>::operator[](unsigned int) const Line | Count | Source | 141 | 20.0k | uint8_t operator[](unsigned _i) const { return m_data[_i]; } |
|
142 | | |
143 | | /// @returns the hash as a user-readable hex string. |
144 | 358 | std::string hex() const { return toHex(asBytes()); } solidity::util::FixedHash<4u>::hex() const Line | Count | Source | 144 | 358 | std::string hex() const { return toHex(asBytes()); } |
Unexecuted instantiation: solidity::util::FixedHash<32u>::hex() const |
145 | | |
146 | | /// @returns a mutable byte vector_ref to the object's data. |
147 | | bytesRef ref() { return bytesRef(m_data.data(), N); } |
148 | | |
149 | | /// @returns a constant byte vector_ref to the object's data. |
150 | 0 | bytesConstRef ref() const { return bytesConstRef(m_data.data(), N); } |
151 | | |
152 | | /// @returns a mutable byte pointer to the object's data. |
153 | 47.2k | uint8_t* data() { return m_data.data(); } |
154 | | |
155 | | /// @returns a constant byte pointer to the object's data. |
156 | 93.0k | uint8_t const* data() const { return m_data.data(); } solidity::util::FixedHash<32u>::data() const Line | Count | Source | 156 | 92.3k | uint8_t const* data() const { return m_data.data(); } |
solidity::util::FixedHash<4u>::data() const Line | Count | Source | 156 | 716 | uint8_t const* data() const { return m_data.data(); } |
Unexecuted instantiation: solidity::util::FixedHash<20u>::data() const |
157 | | |
158 | | /// @returns a copy of the object's data as a byte vector. |
159 | 46.5k | bytes asBytes() const { return bytes(data(), data() + N); } solidity::util::FixedHash<32u>::asBytes() const Line | Count | Source | 159 | 46.1k | bytes asBytes() const { return bytes(data(), data() + N); } |
solidity::util::FixedHash<4u>::asBytes() const Line | Count | Source | 159 | 358 | bytes asBytes() const { return bytes(data(), data() + N); } |
Unexecuted instantiation: solidity::util::FixedHash<20u>::asBytes() const |
160 | | |
161 | | private: |
162 | | std::array<uint8_t, N> m_data; ///< The binary data. |
163 | | }; |
164 | | |
165 | | /// Stream I/O for the FixedHash class. |
166 | | template <unsigned N> |
167 | | inline std::ostream& operator<<(std::ostream& _out, FixedHash<N> const& _h) |
168 | 0 | { |
169 | 0 | boost::io::ios_all_saver guard(_out); |
170 | 0 | _out << std::noshowbase << std::hex << std::setfill('0'); |
171 | 0 | for (unsigned i = 0; i < N; ++i) |
172 | 0 | _out << std::setw(2) << (int)_h[i]; |
173 | 0 | _out << std::dec; |
174 | 0 | return _out; |
175 | 0 | } Unexecuted instantiation: std::__1::basic_ostream<char, std::__1::char_traits<char> >& solidity::util::operator<< <32u>(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, solidity::util::FixedHash<32u> const&) Unexecuted instantiation: std::__1::basic_ostream<char, std::__1::char_traits<char> >& solidity::util::operator<< <20u>(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, solidity::util::FixedHash<20u> const&) |
176 | | |
177 | | // Common types of FixedHash. |
178 | | using h256 = FixedHash<32>; |
179 | | using h160 = FixedHash<20>; |
180 | | |
181 | | } |