-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Replace uint256/uint160 with opaque blobs where possible (cont'd) #5490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Verified b7155a9's code in aith_uint256.{h,cpp} to be a pure copy of the original code in uint256.{h,cpp}, apart from some type name changes, and IMPORTANT 2 deleted trailing white spaces in the comments above SetCompact. |
Untested ACK, but the code (both blob and arithmetic versions of it) looks well tested. |
I've added a commit that (and left the rest untouched)
I'm not exactly sure about the latter. It is an improvement, but other options would be:
|
Let's not let this go too unrebasable... |
8d31cbe
to
adc105a
Compare
ACK. |
adc105a
to
3eb5909
Compare
Rebased for c444c62 |
@jgarzik @gavinandresen Care to review? |
This isn't catched by git, only by the compiler, but needs rebase for 0125988 as it introduces |
Also add a stub for arith_uint256 and its conversion functions, for now completely based on uint256. Eases step-by-step migration to blob.
Replace x=0 with .SetNull(), x==0 with IsNull(), x!=0 with !IsNull(). Replace uses of uint256(0) with uint256().
SignatureHash and its test function SignatureHashOld return uint256(1) as a special error signaling value. Return a local static constant with the same value instead.
If uint256() constructor takes a string, uint256(0) will become dangerous when uint256 does not take integers anymore (it will go through std::string(const char*) making a NULL string, and the explicit keyword is no help).
Also add conversion from/to uint256 where needed.
Introduce new opaque implementation of `uint256`, move old "arithmetic" implementation to `arith_uint256.
We never do 160-bit arithmetic.
- Methods that access the guts of arith_uint256 are removed, as these are incompatible between endians. Use uint256 instead - Serialization is no longer needed as arith_uint256's are never read or written - GetHash is never used on arith_uint256
Remove initialization from vector (as this is only used in the tests). Also implement SetHex and GetHex in terms of uint256, to avoid duplicate code as well as avoid endianness issues (as they work in term of bytes).
3eb5909
to
6bd0dc2
Compare
Rebased for 0125988. |
6bd0dc2 arith_uint256: remove initialization from byte vector (Wladimir J. van der Laan) 30007fd Remove now-unused methods from arith_uint256 and base_uint (Wladimir J. van der Laan) edc7204 Remove arith_uint160 (Wladimir J. van der Laan) dba2e91 Add tests for new uint256 (Wladimir J. van der Laan) 92cdb1a Add conversion functions arith_uint256<->uint_256 (Wladimir J. van der Laan) bfc6070 uint256->arith_uint256 blob256->uint256 (Wladimir J. van der Laan) 734f85c Use arith_uint256 where necessary (Wladimir J. van der Laan) 34cdc41 String conversions uint256 -> uint256S (Wladimir J. van der Laan) 2eae315 Replace uint256(1) with static constant (Wladimir J. van der Laan) 8076585 Replace GetLow64 with GetCheapHash (Wladimir J. van der Laan) 4f15249 Replace direct use of 0 with SetNull and IsNull (Wladimir J. van der Laan) 5d3064b Temporarily add SetNull/IsNull/GetCheapHash to base_uint (Wladimir J. van der Laan)
… (First) 400f6f4 Moving uint256(str) to proper uint256S(str). --> uint256 string parse. (furszy) b0163f6 x=0 moved to x=UINT256_ZERO (furszy) 14319cf [Backport] Replace uint256(1) with static constant. (furszy) d96eab9 Replace GetLow64 with GetCheapHash (furszy) 741f43c Replace direct use of 0 with SetNull and IsNull. (furszy) Pull request description: Initial PR towards a big back port [upstream#5490](bitcoin#5490). This is including: 1) [upstream@4f152496](bitcoin@4f15249) Replace x=0 with .SetNull(), x==0 with IsNull(), x!=0 with !IsNull(). Replace uses of uint256(0) with UINT256_ZERO. 2) [upstream@80765854](bitcoin@8076585) Replace GetLow64 with GetCheapHash 3) [upstream@2eae3157](bitcoin@2eae315) Replace uint256(1) with static constant 4) Moved `uint256(str)` string parse to the adequate `uint256S(str)`method. ACKs for top commit: random-zebra: ACK 400f6f4 and merging... Tree-SHA512: ed34b398ff00df8584efe079c764756d17bf5d7f1406aa6db84f82d9b544ae2b996c9b9e811d9d54989002a1f620ed798f77aa9e5b5d6c00a4de8310c5d6534e
…ere possible (First) 400f6f4 Moving uint256(str) to proper uint256S(str). --> uint256 string parse. (furszy) b0163f6 x=0 moved to x=UINT256_ZERO (furszy) 14319cf [Backport] Replace uint256(1) with static constant. (furszy) d96eab9 Replace GetLow64 with GetCheapHash (furszy) 741f43c Replace direct use of 0 with SetNull and IsNull. (furszy) Pull request description: Initial PR towards a big back port [upstream#5490](bitcoin#5490). This is including: 1) [upstream@4f152496](bitcoin@4f15249) Replace x=0 with .SetNull(), x==0 with IsNull(), x!=0 with !IsNull(). Replace uses of uint256(0) with UINT256_ZERO. 2) [upstream@80765854](bitcoin@8076585) Replace GetLow64 with GetCheapHash 3) [upstream@2eae3157](bitcoin@2eae315) Replace uint256(1) with static constant 4) Moved `uint256(str)` string parse to the adequate `uint256S(str)`method. ACKs for top commit: random-zebra: ACK 400f6f4 and merging... Tree-SHA512: ed34b398ff00df8584efe079c764756d17bf5d7f1406aa6db84f82d9b544ae2b996c9b9e811d9d54989002a1f620ed798f77aa9e5b5d6c00a4de8310c5d6534e
Second attempt at #5478. See discussion there for context.
This pull replaces almost all uses of
uint256
and all uses ofuint160
to use opaque byte blobs with only the following operations:IsNull()
compare to special all-zeros valueSetNull()
clear to special all-zeros value: Bitcoin needsIsNull()
/SetNull()
because we often use the all-zeroes value as a marker for errors and empty values.<
for sorting in maps.!=
==
test for (in)equalityGetHex
/SetHex
/ToString
because they're just so usefulbegin()
/end()
raw access to the datasize()
returns a fixed sizeGetSerializeSize()
Serialize
Unserialize
serialization just reads and writes the bytesGetCheapHash()
this is similar toGetLow64()
and returns part of the value as uint64_t, for cheap hashing when the contents are assumed distributed uniformy random.arith_uint256
(the old uint256, used for proof-of-work mainly), on the other hand, loses all functionality like raw bytes access and serialization. Its memory should not be accessed directly. This is necessary for #888 (big endian support).arith_uint160
(the old uint160) is completely removed as Bitcoin Core does no 160-bit integer arithmetic.For overall steps see commits.