Skip to content

Commit 8e3c266

Browse files
committed
Merge bitcoin/bitcoin#24077: util: Make base_uint::GetHex() and base_uint::SetHex() not depend on uint256
a4f4f89 Replace uint256 specific implementations of base_uint::GetHex() and base_uint::SetHex() with proper ones that don't depend on uint256 and replace template methods instantiations of base_uint with template class instantiation (Samer Afach) Pull request description: The current implementations of `SetHex()` and `GetHex()` in `base_uint` use `arith_uint256`'s implementations. Which means, any attempt to create anything other than `arith_uint256` (say `arith_uint512`) and using any of these functions (which is what I needed in my application) will just not work and will cause compilation errors (besides the immediate linking errors due to templates being in source files instantiated only for 256) because there's no viable conversion from `arith_uint256` and any of the other possible types. Besides that these function will yield wrong results even if the conversion is possible depending on the size. This is fixed in this PR. ACKs for top commit: laanwj: re-ACK a4f4f89 Tree-SHA512: 92a930fb7ddec5a5565deae2386f7d2d84645f9e8532f8d0c0178367ae081019b32eedcb59cc11028bac0cb15d9883228e016a466b1ee8fc3c6377b4df1d4180
2 parents decde9b + a4f4f89 commit 8e3c266

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/arith_uint256.cpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,21 @@ double base_uint<BITS>::getdouble() const
146146
template <unsigned int BITS>
147147
std::string base_uint<BITS>::GetHex() const
148148
{
149-
return ArithToUint256(*this).GetHex();
149+
base_blob<BITS> b;
150+
for (int x = 0; x < this->WIDTH; ++x) {
151+
WriteLE32(b.begin() + x*4, this->pn[x]);
152+
}
153+
return b.GetHex();
150154
}
151155

152156
template <unsigned int BITS>
153157
void base_uint<BITS>::SetHex(const char* psz)
154158
{
155-
*this = UintToArith256(uint256S(psz));
159+
base_blob<BITS> b;
160+
b.SetHex(psz);
161+
for (int x = 0; x < this->WIDTH; ++x) {
162+
this->pn[x] = ReadLE32(b.begin() + x*4);
163+
}
156164
}
157165

158166
template <unsigned int BITS>
@@ -164,7 +172,7 @@ void base_uint<BITS>::SetHex(const std::string& str)
164172
template <unsigned int BITS>
165173
std::string base_uint<BITS>::ToString() const
166174
{
167-
return (GetHex());
175+
return GetHex();
168176
}
169177

170178
template <unsigned int BITS>
@@ -183,20 +191,7 @@ unsigned int base_uint<BITS>::bits() const
183191
}
184192

185193
// Explicit instantiations for base_uint<256>
186-
template base_uint<256>::base_uint(const std::string&);
187-
template base_uint<256>& base_uint<256>::operator<<=(unsigned int);
188-
template base_uint<256>& base_uint<256>::operator>>=(unsigned int);
189-
template base_uint<256>& base_uint<256>::operator*=(uint32_t b32);
190-
template base_uint<256>& base_uint<256>::operator*=(const base_uint<256>& b);
191-
template base_uint<256>& base_uint<256>::operator/=(const base_uint<256>& b);
192-
template int base_uint<256>::CompareTo(const base_uint<256>&) const;
193-
template bool base_uint<256>::EqualTo(uint64_t) const;
194-
template double base_uint<256>::getdouble() const;
195-
template std::string base_uint<256>::GetHex() const;
196-
template std::string base_uint<256>::ToString() const;
197-
template void base_uint<256>::SetHex(const char*);
198-
template void base_uint<256>::SetHex(const std::string&);
199-
template unsigned int base_uint<256>::bits() const;
194+
template class base_uint<256>;
200195

201196
// This implementation directly uses shifts instead of going
202197
// through an intermediate MPI representation.

src/arith_uint256.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,6 @@ class arith_uint256 : public base_uint<256> {
284284
uint256 ArithToUint256(const arith_uint256 &);
285285
arith_uint256 UintToArith256(const uint256 &);
286286

287+
extern template class base_uint<256>;
288+
287289
#endif // BITCOIN_ARITH_UINT256_H

0 commit comments

Comments
 (0)