Skip to content

Commit 3a12b9d

Browse files
authored
Merge pull request #1630 from evoskuil/master
Optimize point hash to avoid loss of 1 bit of sha256 entropy.
2 parents 27ff4cd + 6899097 commit 3a12b9d

File tree

4 files changed

+13
-32
lines changed

4 files changed

+13
-32
lines changed

include/bitcoin/system/chain/point.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ struct hash<bc::system::chain::point>
134134
{
135135
size_t operator()(const bc::system::chain::point& value) const NOEXCEPT
136136
{
137-
return bc::system::hash_combine(value.index(),
138-
bc::system::unique_hash(value.hash()));
137+
// Right should always be the lower entropy value (i.e. point index).
138+
return bc::system::hash_combine(bc::system::unique_hash(value.hash()),
139+
value.index());
139140
}
140141
};
141142

include/bitcoin/system/impl/hash/functions.ipp

+2-22
Original file line numberDiff line numberDiff line change
@@ -237,30 +237,10 @@ INLINE constexpr size_t djb2_hash(const data_slice& data) NOEXCEPT
237237
}
238238

239239
// Combine hash values, such as djb2_hash or unique_hash outputs.
240+
// Right should always be the lower entropy value (e.g. point index).
240241
INLINE constexpr size_t hash_combine(size_t left, size_t right) NOEXCEPT
241242
{
242-
//// This leads to mixing null points (850k identical) with other buckets.
243-
////constexpr auto prime1 = possible_narrow_cast<size_t>(0x9e3779b97f4a7c15_u64);
244-
////constexpr auto prime2 = possible_narrow_cast<size_t>(0x517cc1b727220a95_u64);
245-
////
246-
////auto first = left;
247-
////first ^= shift_right(first, 23);
248-
////first *= prime1;
249-
////first ^= shift_right(first, 19);
250-
////
251-
////auto second = right;
252-
////second ^= shift_right(second, 13);
253-
////second *= prime2;
254-
////second ^= shift_right(second, 31);
255-
////
256-
////// seed parameter, defaults to zero.
257-
////first ^= second;
258-
////first += seed;
259-
////first ^= shift_right(first, 17);
260-
////first *= prime1;
261-
////first ^= shift_right(first, 29);
262-
263-
////return first;
243+
// Shift ensures left == right does not always return zero.
264244
return bit_xor(left, shift_left(right));
265245
}
266246

src/wallet/keys/hd_private.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ hd_private hd_private::from_entropy(const data_slice& entropy,
128128
static const auto magic = to_chunk("Bitcoin seed");
129129
const auto intermediate = split(hmac<sha512>::code(entropy, magic));
130130

131-
return hd_private(intermediate.first, intermediate.second, prefixes);
131+
return { intermediate.first, intermediate.second, prefixes };
132132
}
133133

134134
hd_private hd_private::from_key(const hd_key& key,
@@ -162,7 +162,7 @@ hd_private hd_private::from_key(const hd_key& key, uint64_t prefixes) NOEXCEPT
162162
child
163163
};
164164

165-
return hd_private(secret, chain, lineage);
165+
return { secret, chain, lineage };
166166
}
167167

168168
hd_private hd_private::from_string(const std::string& encoded,
@@ -231,8 +231,8 @@ hd_key hd_private::to_hd_key() const NOEXCEPT
231231

232232
hd_public hd_private::to_public() const NOEXCEPT
233233
{
234-
return hd_public(((hd_public)*this).to_hd_key(),
235-
hd_public::to_prefix(lineage_.prefixes));
234+
const auto key = static_cast<hd_public>(*this).to_hd_key();
235+
return { key, hd_public::to_prefix(lineage_.prefixes) };
236236
}
237237

238238
hd_private hd_private::derive_private(uint32_t index) const NOEXCEPT
@@ -261,7 +261,7 @@ hd_private hd_private::derive_private(uint32_t index) const NOEXCEPT
261261
index
262262
};
263263

264-
return hd_private(child, intermediate.second, lineage);
264+
return { child, intermediate.second, lineage };
265265
}
266266

267267
hd_public hd_private::derive_public(uint32_t index) const NOEXCEPT

src/wallet/keys/hd_public.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ hd_public hd_public::from_key(const hd_key& key, uint32_t prefix) NOEXCEPT
132132
child
133133
};
134134

135-
return hd_public(compressed, chain, lineage);
135+
return { compressed, chain, lineage };
136136
}
137137

138138
hd_public hd_public::from_string(const std::string& encoded,
@@ -142,7 +142,7 @@ hd_public hd_public::from_string(const std::string& encoded,
142142
if (!decode_base58(key, encoded))
143143
return {};
144144

145-
return hd_public(from_key(key, prefix));
145+
return { from_key(key, prefix) };
146146
}
147147

148148
// Cast operators.
@@ -227,7 +227,7 @@ hd_public hd_public::derive_public(uint32_t index) const NOEXCEPT
227227
index
228228
};
229229

230-
return hd_public(child, intermediate.second, lineage);
230+
return { child, intermediate.second, lineage };
231231
}
232232

233233
// Helpers.

0 commit comments

Comments
 (0)