32
32
namespace dev
33
33
{
34
34
35
- // / Compile-time calculation of Log2 of constant values.
36
- template <unsigned N> struct StaticLog2 { enum { result = 1 + StaticLog2<N/2 >::result }; };
37
- template <> struct StaticLog2 <1 > { enum { result = 0 }; };
38
-
39
35
extern std::random_device s_fixedHashEngine;
40
36
41
37
// / Fixed-size raw-byte array container type, with an API optimised for storing hashes.
@@ -110,9 +106,6 @@ class FixedHash
110
106
// Big-endian increment.
111
107
FixedHash& operator ++() { for (unsigned i = size; i > 0 && !++m_data[--i]; ) {} return *this ; }
112
108
113
- // / @returns true if all one-bits in @a _c are set in this object.
114
- bool contains (FixedHash const & _c) const { return (*this & _c) == _c; }
115
-
116
109
// / @returns a particular byte from the hash.
117
110
byte& operator [](unsigned _i) { return m_data[_i]; }
118
111
// / @returns a particular byte from the hash.
@@ -121,9 +114,6 @@ class FixedHash
121
114
// / @returns an abridged version of the hash as a user-readable hex string.
122
115
std::string abridged () const { return toHex (ref ().cropped (0 , 4 )) + " \342\200\246 " ; }
123
116
124
- // / @returns a version of the hash as a user-readable hex string that leaves out the middle part.
125
- std::string abridgedMiddle () const { return toHex (ref ().cropped (0 , 4 )) + " \342\200\246 " + toHex (ref ().cropped (N - 4 )); }
126
-
127
117
// / @returns the hash as a user-readable hex string.
128
118
std::string hex () const { return toHex (ref ()); }
129
119
@@ -139,15 +129,6 @@ class FixedHash
139
129
// / @returns a constant byte pointer to the object's data.
140
130
byte const * data () const { return m_data.data (); }
141
131
142
- // / @returns a copy of the object's data as a byte vector.
143
- bytes asBytes () const { return bytes (data (), data () + N); }
144
-
145
- // / @returns a mutable reference to the object's data as an STL array.
146
- std::array<byte, N>& asArray () { return m_data; }
147
-
148
- // / @returns a constant reference to the object's data as an STL array.
149
- std::array<byte, N> const & asArray () const { return m_data; }
150
-
151
132
// / Populate with random data.
152
133
template <class Engine >
153
134
void randomize (Engine& _eng)
@@ -165,136 +146,12 @@ class FixedHash
165
146
size_t operator ()(FixedHash const & _value) const { return boost::hash_range (_value.m_data .cbegin (), _value.m_data .cend ()); }
166
147
};
167
148
168
- template <unsigned P, unsigned M> inline FixedHash& shiftBloom (FixedHash<M> const & _h)
169
- {
170
- return (*this |= _h.template bloomPart <P, N>());
171
- }
172
-
173
- template <unsigned P, unsigned M> inline bool containsBloom (FixedHash<M> const & _h)
174
- {
175
- return contains (_h.template bloomPart <P, N>());
176
- }
177
-
178
- template <unsigned P, unsigned M> inline FixedHash<M> bloomPart () const
179
- {
180
- unsigned const c_bloomBits = M * 8 ;
181
- unsigned const c_mask = c_bloomBits - 1 ;
182
- unsigned const c_bloomBytes = (StaticLog2<c_bloomBits>::result + 7 ) / 8 ;
183
-
184
- static_assert ((M & (M - 1 )) == 0 , " M must be power-of-two" );
185
- static_assert (P * c_bloomBytes <= N, " out of range" );
186
-
187
- FixedHash<M> ret;
188
- byte const * p = data ();
189
- for (unsigned i = 0 ; i < P; ++i)
190
- {
191
- unsigned index = 0 ;
192
- for (unsigned j = 0 ; j < c_bloomBytes; ++j, ++p)
193
- index = (index << 8 ) | *p;
194
- index &= c_mask;
195
- ret[M - 1 - index / 8 ] |= (1 << (index % 8 ));
196
- }
197
- return ret;
198
- }
199
-
200
- // / Returns the index of the first bit set to one, or size() * 8 if no bits are set.
201
- inline unsigned firstBitSet () const
202
- {
203
- unsigned ret = 0 ;
204
- for (auto d: m_data)
205
- if (d)
206
- for (;; ++ret, d <<= 1 )
207
- if (d & 0x80 )
208
- return ret;
209
- else {}
210
- else
211
- ret += 8 ;
212
- return ret;
213
- }
214
-
215
149
void clear () { m_data.fill (0 ); }
216
150
217
151
private:
218
152
std::array<byte, N> m_data; // /< The binary data.
219
153
};
220
154
221
- template <unsigned T>
222
- class SecureFixedHash : private FixedHash <T>
223
- {
224
- public:
225
- using ConstructFromHashType = typename FixedHash<T>::ConstructFromHashType;
226
- using ConstructFromStringType = typename FixedHash<T>::ConstructFromStringType;
227
- using ConstructFromPointerType = typename FixedHash<T>::ConstructFromPointerType;
228
- SecureFixedHash () = default ;
229
- explicit SecureFixedHash (bytes const & _b, ConstructFromHashType _t = FixedHash<T>::FailIfDifferent): FixedHash<T>(_b, _t) {}
230
- explicit SecureFixedHash (bytesConstRef _b, ConstructFromHashType _t = FixedHash<T>::FailIfDifferent): FixedHash<T>(_b, _t) {}
231
- explicit SecureFixedHash (bytesSec const & _b, ConstructFromHashType _t = FixedHash<T>::FailIfDifferent): FixedHash<T>(_b.ref(), _t) {}
232
- template <unsigned M> explicit SecureFixedHash (FixedHash<M> const & _h, ConstructFromHashType _t = FixedHash<T>::AlignLeft): FixedHash<T>(_h, _t) {}
233
- template <unsigned M> explicit SecureFixedHash (SecureFixedHash<M> const & _h, ConstructFromHashType _t = FixedHash<T>::AlignLeft): FixedHash<T>(_h.makeInsecure(), _t) {}
234
- explicit SecureFixedHash (std::string const & _s, ConstructFromStringType _t = FixedHash<T>::FromHex, ConstructFromHashType _ht = FixedHash<T>::FailIfDifferent): FixedHash<T>(_s, _t, _ht) {}
235
- explicit SecureFixedHash (bytes const * _d, ConstructFromPointerType _t): FixedHash<T>(_d, _t) {}
236
- ~SecureFixedHash () { ref ().cleanse (); }
237
-
238
- SecureFixedHash<T>& operator =(SecureFixedHash<T> const & _c)
239
- {
240
- if (&_c == this )
241
- return *this ;
242
- ref ().cleanse ();
243
- FixedHash<T>::operator =(static_cast <FixedHash<T> const &>(_c));
244
- return *this ;
245
- }
246
-
247
- using FixedHash<T>::size;
248
-
249
- bytesSec asBytesSec () const { return bytesSec (ref ()); }
250
-
251
- FixedHash<T> const & makeInsecure () const { return static_cast <FixedHash<T> const &>(*this ); }
252
- FixedHash<T>& writable () { clear (); return static_cast <FixedHash<T>&>(*this ); }
253
-
254
- using FixedHash<T>::operator bool ;
255
-
256
- // The obvious comparison operators.
257
- bool operator ==(SecureFixedHash const & _c) const { return static_cast <FixedHash<T> const &>(*this ).operator ==(static_cast <FixedHash<T> const &>(_c)); }
258
- bool operator !=(SecureFixedHash const & _c) const { return static_cast <FixedHash<T> const &>(*this ).operator !=(static_cast <FixedHash<T> const &>(_c)); }
259
- bool operator <(SecureFixedHash const & _c) const { return static_cast <FixedHash<T> const &>(*this ).operator <(static_cast <FixedHash<T> const &>(_c)); }
260
- bool operator >=(SecureFixedHash const & _c) const { return static_cast <FixedHash<T> const &>(*this ).operator >=(static_cast <FixedHash<T> const &>(_c)); }
261
- bool operator <=(SecureFixedHash const & _c) const { return static_cast <FixedHash<T> const &>(*this ).operator <=(static_cast <FixedHash<T> const &>(_c)); }
262
- bool operator >(SecureFixedHash const & _c) const { return static_cast <FixedHash<T> const &>(*this ).operator >(static_cast <FixedHash<T> const &>(_c)); }
263
-
264
- using FixedHash<T>::operator ==;
265
- using FixedHash<T>::operator !=;
266
- using FixedHash<T>::operator <;
267
- using FixedHash<T>::operator >=;
268
- using FixedHash<T>::operator <=;
269
- using FixedHash<T>::operator >;
270
-
271
- // The obvious binary operators.
272
- SecureFixedHash& operator ^=(FixedHash<T> const & _c) { static_cast <FixedHash<T>&>(*this ).operator ^=(_c); return *this ; }
273
- SecureFixedHash operator ^(FixedHash<T> const & _c) const { return SecureFixedHash (*this ) ^= _c; }
274
- SecureFixedHash& operator |=(FixedHash<T> const & _c) { static_cast <FixedHash<T>&>(*this ).operator ^=(_c); return *this ; }
275
- SecureFixedHash operator |(FixedHash<T> const & _c) const { return SecureFixedHash (*this ) |= _c; }
276
- SecureFixedHash& operator &=(FixedHash<T> const & _c) { static_cast <FixedHash<T>&>(*this ).operator ^=(_c); return *this ; }
277
- SecureFixedHash operator &(FixedHash<T> const & _c) const { return SecureFixedHash (*this ) &= _c; }
278
-
279
- SecureFixedHash& operator ^=(SecureFixedHash const & _c) { static_cast <FixedHash<T>&>(*this ).operator ^=(static_cast <FixedHash<T> const &>(_c)); return *this ; }
280
- SecureFixedHash operator ^(SecureFixedHash const & _c) const { return SecureFixedHash (*this ) ^= _c; }
281
- SecureFixedHash& operator |=(SecureFixedHash const & _c) { static_cast <FixedHash<T>&>(*this ).operator ^=(static_cast <FixedHash<T> const &>(_c)); return *this ; }
282
- SecureFixedHash operator |(SecureFixedHash const & _c) const { return SecureFixedHash (*this ) |= _c; }
283
- SecureFixedHash& operator &=(SecureFixedHash const & _c) { static_cast <FixedHash<T>&>(*this ).operator ^=(static_cast <FixedHash<T> const &>(_c)); return *this ; }
284
- SecureFixedHash operator &(SecureFixedHash const & _c) const { return SecureFixedHash (*this ) &= _c; }
285
- SecureFixedHash operator ~() const { auto r = ~static_cast <FixedHash<T> const &>(*this ); return static_cast <SecureFixedHash const &>(r); }
286
-
287
- using FixedHash<T>::abridged;
288
- using FixedHash<T>::abridgedMiddle;
289
-
290
- bytesConstRef ref () const { return FixedHash<T>::ref (); }
291
- byte const * data () const { return FixedHash<T>::data (); }
292
-
293
- using FixedHash<T>::firstBitSet;
294
-
295
- void clear () { ref ().cleanse (); }
296
- };
297
-
298
155
// / Fast equality operator for h256.
299
156
template <> inline bool FixedHash<32 >::operator ==(FixedHash<32 > const & _other) const
300
157
{
@@ -321,14 +178,6 @@ inline std::ostream& operator<<(std::ostream& _out, FixedHash<N> const& _h)
321
178
return _out;
322
179
}
323
180
324
- // / Stream I/O for the SecureFixedHash class.
325
- template <unsigned N>
326
- inline std::ostream& operator <<(std::ostream& _out, SecureFixedHash<N> const & _h)
327
- {
328
- _out << " SecureFixedHash#" << std::hex << typename FixedHash<N>::hash ()(_h.makeInsecure ()) << std::dec;
329
- return _out;
330
- }
331
-
332
181
// Common types of FixedHash.
333
182
using h2048 = FixedHash<256 >;
334
183
using h1024 = FixedHash<128 >;
@@ -346,26 +195,6 @@ using h160Set = std::set<h160>;
346
195
using h256Hash = std::unordered_set<h256>;
347
196
using h160Hash = std::unordered_set<h160>;
348
197
349
- // / Convert the given value into h160 (160-bit unsigned integer) using the right 20 bytes.
350
- inline h160 right160 (h256 const & _t)
351
- {
352
- h160 ret;
353
- memcpy (ret.data (), _t.data () + 12 , 20 );
354
- return ret;
355
- }
356
-
357
- // / Convert the given value into h160 (160-bit unsigned integer) using the left 20 bytes.
358
- inline h160 left160 (h256 const & _t)
359
- {
360
- h160 ret;
361
- memcpy (&ret[0 ], _t.data (), 20 );
362
- return ret;
363
- }
364
-
365
- h128 fromUUID (std::string const & _uuid);
366
-
367
- std::string toUUID (h128 const & _uuid);
368
-
369
198
inline std::string toString (h256s const & _bs)
370
199
{
371
200
std::ostringstream out;
0 commit comments