0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-10 10:52:31 -05:00
bitcoin-bitcoin-core/src
Ryan Ofsky 1a7d20509f
Merge bitcoin/bitcoin#30526: doc: Correct uint256 hex string endianness
73e3fa10b4 doc + test: Correct uint256 hex string endianness (Hodlinator)

Pull request description:

  This PR is a follow-up to #30436.

  Only changes test-code and modifies/adds comments.

  Byte order of hex string representation was wrongfully documented as little-endian, but are in fact closer to "big-endian" (endianness is a memory-order concept rather than a numeric concept). `[arith_]uint256` both store their data in arrays with little-endian byte order (`arith_uint256` has host byte order within each `uint32_t` element).

  **uint256_tests.cpp** - Avoid using variable from the left side of the condition in the right side. Credits to @maflcko: https://github.com/bitcoin/bitcoin/pull/30436#discussion_r1688273553

  **setup_common.cpp** - Skip needless ArithToUint256-conversion. Credits to @stickies-v: https://github.com/bitcoin/bitcoin/pull/30436#discussion_r1688621638

  ---

  <details>
  <summary>

  ## Logical reasoning for endianness

  </summary>

  1. Comparing an `arith_uint256` (`base_uint<256>`) to a `uint64_t` compares the beginning of the array, and verifies the remaining elements are zero.
  ```C++
  template <unsigned int BITS>
  bool base_uint<BITS>::EqualTo(uint64_t b) const
  {
      for (int i = WIDTH - 1; i >= 2; i--) {
          if (pn[i])
              return false;
      }
      if (pn[1] != (b >> 32))
          return false;
      if (pn[0] != (b & 0xfffffffful))
          return false;
      return true;
  }
  ```
  ...that is consistent with little endian ordering of the array.

  2. They have the same endianness (but `arith_*` has host-ordering of each `uint32_t` element):
  ```C++
  arith_uint256 UintToArith256(const uint256 &a)
  {
      arith_uint256 b;
      for(int x=0; x<b.WIDTH; ++x)
          b.pn[x] = ReadLE32(a.begin() + x*4);
      return b;
  }
  ```

  ### String conversions

  The reversal of order which happens when converting hex-strings <=> uint256 means strings are actually closer to big-endian, see the end of `base_blob<BITS>::SetHexDeprecated`:
  ```C++
      unsigned char* p1 = m_data.data();
      unsigned char* pend = p1 + WIDTH;
      while (digits > 0 && p1 < pend) {
          *p1 = ::HexDigit(trimmed[--digits]);
          if (digits > 0) {
              *p1 |= ((unsigned char)::HexDigit(trimmed[--digits]) << 4);
              p1++;
          }
      }
  ```
  Same reversal here:
  ```C++
  template <unsigned int BITS>
  std::string base_blob<BITS>::GetHex() const
  {
      uint8_t m_data_rev[WIDTH];
      for (int i = 0; i < WIDTH; ++i) {
          m_data_rev[i] = m_data[WIDTH - 1 - i];
      }
      return HexStr(m_data_rev);
  }
  ```
  It now makes sense to me that `SetHexDeprecated`, upon receiving a shorter hex string that requires zero-padding, would pad as if the missing hex chars where towards the end of the little-endian byte array, as they are the most significant bytes. "Big-endian" string representation is also consistent with the case where `SetHexDeprecated` receives too many hex digits and discards the leftmost ones, as a form of integer narrowing takes place.

  ### How I got it wrong in #30436

  Previously I used the less than (`<`) comparison to prove endianness, but for `uint256` it uses `memcmp` and thereby gives priority to the *lower* bytes at the beginning of the array.
  ```C++
      constexpr int Compare(const base_blob& other) const { return std::memcmp(m_data.data(), other.m_data.data(), WIDTH); }
  ```

  `arith_uint256` is different in that it begins by comparing the bytes from the end, as it is using little endian representation, where the bytes toward the end are more significant.
  ```C++
  template <unsigned int BITS>
  int base_uint<BITS>::CompareTo(const base_uint<BITS>& b) const
  {
      for (int i = WIDTH - 1; i >= 0; i--) {
          if (pn[i] < b.pn[i])
              return -1;
          if (pn[i] > b.pn[i])
              return 1;
      }
      return 0;
  }
  ```
  (The commit documents that `base_blob::Compare()` is doing lexicographic ordering unlike the `arith_*`-variant which is doing numeric ordering).

  </details>

ACKs for top commit:
  paplorinc:
    ACK 73e3fa10b4
  ryanofsky:
    Code review ACK 73e3fa10b4

Tree-SHA512: 121630c37ab01aa7f7097f10322ab37da3cbc0696a6bbdbf2bbd6db180dc5938c7ed91003aaa2df7cf4a4106f973f5118ba541b5e077cf3588aa641bbd528f4e
2024-08-04 22:27:10 -04:00
..
bench bench: add cluster linearization improvement benchmark 2024-07-25 10:16:40 -04:00
common random: get rid of GetRand by inlining 2024-07-01 12:39:53 -04:00
compat netbase: refactor CreateSock() to accept sa_family_t 2024-03-01 13:13:07 -05:00
config
consensus refactor: Rename CTransaction::nVersion to version 2024-06-07 13:55:23 -04:00
crc32c Update crc32c subtree to latest upstream master 2024-02-27 18:28:19 +00:00
crypto refactor: fix missing includes 2024-07-24 15:57:01 +01:00
index tidy: modernize-use-equals-default 2024-07-08 11:12:01 +02:00
init logging: clarify -debug and -debugexclude descriptions 2024-08-04 06:43:01 +02:00
interfaces Merge bitcoin-core/gui#626: Showing Local Addresses in Node Window 2024-08-02 14:19:02 +01:00
ipc multiprocess: Add type conversion code for UniValue types 2023-11-28 12:35:50 -05:00
kernel kernel: Only setup kernel context globals once 2024-07-30 18:07:21 +02:00
leveldb Update leveldb-subtree subtree to latest upstream 2024-06-13 13:17:57 +01:00
logging Use steady clock for logging timer 2023-01-31 18:48:50 +01:00
minisketch Update minisketch subtree to latest master 2024-06-12 14:38:39 +01:00
node Merge bitcoin-core/gui#626: Showing Local Addresses in Node Window 2024-08-02 14:19:02 +01:00
policy policy: stop 3rd party wtxid malleability of anchor spend 2024-07-30 14:06:58 -04:00
primitives tidy: modernize-use-equals-default 2024-07-08 11:12:01 +02:00
qt Merge bitcoin-core/gui#826: OptionsDialog: Allow Maximize of window 2024-08-04 16:29:31 +01:00
rpc Merge bitcoin/bitcoin#29798: Logging cleanup 2024-08-04 21:05:08 -04:00
script policy: make anchor spend standard 2024-07-30 14:06:58 -04:00
secp256k1 Update secp256k1 subtree to latest master 2024-06-25 15:01:00 +01:00
support cleanse: Use SecureZeroMemory for mingw-w64 (release) builds 2024-07-24 09:57:49 +01:00
test Merge bitcoin/bitcoin#30526: doc: Correct uint256 hex string endianness 2024-08-04 22:27:10 -04:00
univalue test: Fix MSVC warning C4101 "unreferenced local variable" 2024-07-16 22:40:25 +01:00
util refactor: remove TxidFromString 2024-07-31 16:47:39 +01:00
wallet policy: Add OP_1 <0x4e73> as a standard output type 2024-07-30 14:06:58 -04:00
zmq tidy: modernize-use-equals-default 2024-07-08 11:12:01 +02:00
.bear-tidy-config tidy: modernize-use-emplace 2023-10-12 11:27:19 +02:00
.clang-format refactor: Print verbose serialize compiler error messages 2023-12-15 15:20:54 +01:00
.clang-tidy Merge bitcoin/bitcoin#30234: Enable clang-tidy checks for self-assignment 2024-07-11 19:21:05 +01:00
addrdb.cpp random: get rid of GetRand by inlining 2024-07-01 12:39:53 -04:00
addrdb.h Use serialization parameters for CAddress serialization 2023-09-05 10:13:25 +02:00
addresstype.cpp policy: Add OP_1 <0x4e73> as a standard output type 2024-07-30 14:06:58 -04:00
addresstype.h policy: Add OP_1 <0x4e73> as a standard output type 2024-07-30 14:06:58 -04:00
addrman.cpp random: add RandomMixin::randbits with compile-known bits 2024-07-01 10:26:46 -04:00
addrman.h doc, test: Test and explain service flag handling 2024-01-15 16:19:53 -05:00
addrman_impl.h Remove timedata 2024-04-10 17:01:27 +02:00
arith_uint256.cpp refactor: Remove unused and fragile string interface from arith_uint256 2023-11-21 17:37:25 +01:00
arith_uint256.h doc + test: Correct uint256 hex string endianness 2024-08-03 21:59:54 +02:00
attributes.h Introduce platform-agnostic ALWAYS_INLINE macro 2023-05-04 20:57:51 +01:00
banman.cpp Merge bitcoin/bitcoin#24097: Replace RecursiveMutex m_cs_banned with Mutex, and rename it 2023-11-02 14:09:27 -04:00
banman.h net_processing: remove Misbehavior score and increments 2024-05-30 08:35:18 -04:00
base58.cpp util: Move util/string.h functions to util namespace 2024-05-16 10:16:08 -05:00
base58.h
bech32.cpp Reduce memory copying operations in bech32 encode 2024-06-05 13:18:13 +02:00
bech32.h refactor: Model the bech32 charlimit as an Enum 2024-05-13 12:07:47 +02:00
bip324.cpp net: merge V2Transport constructors, move key gen 2023-09-10 16:11:52 -04:00
bip324.h net: merge V2Transport constructors, move key gen 2023-09-10 16:11:52 -04:00
bitcoin-chainstate.cpp Merge bitcoin/bitcoin#30386: Early logging improvements 2024-07-26 08:06:08 -04:00
bitcoin-cli-res.rc
bitcoin-cli.cpp Merge bitcoin/bitcoin#29015: kernel: Streamline util library 2024-06-12 17:12:54 -04:00
bitcoin-tx-res.rc
bitcoin-tx.cpp refactor: Replace ParseHashStr with FromHex 2024-07-24 17:40:18 +02:00
bitcoin-util-res.rc
bitcoin-util.cpp scripted-diff: Add IWYU pragma keep to bitcoin-config.h includes 2024-05-01 08:33:04 +02:00
bitcoin-wallet-res.rc
bitcoin-wallet.cpp Merge bitcoin/bitcoin#29015: kernel: Streamline util library 2024-06-12 17:12:54 -04:00
bitcoind-res.rc
bitcoind.cpp refactor: remove warnings globals 2024-06-13 11:20:49 +01:00
blockencodings.cpp test: Make blockencodings_tests deterministic 2024-06-19 22:56:30 +01:00
blockencodings.h tidy: modernize-use-equals-default 2024-07-08 11:12:01 +02:00
blockfilter.cpp util: Move util/string.h functions to util namespace 2024-05-16 10:16:08 -05:00
blockfilter.h Remove unused includes from blockfilter.h 2023-08-17 18:28:15 +02:00
chain.cpp
chain.h tidy: modernize-use-equals-default 2024-07-08 11:12:01 +02:00
chainparams.cpp util: Move util/string.h functions to util namespace 2024-05-16 10:16:08 -05:00
chainparams.h test: Throw error when -signetchallenge is non-hex 2023-05-25 19:24:05 +02:00
chainparamsbase.cpp refactor: Use ChainType enum exhaustively 2023-05-10 10:39:58 +02:00
chainparamsbase.h refactor: Use ChainType enum exhaustively 2023-05-10 10:39:58 +02:00
chainparamsseeds.h seeds: Update testnet seeds 2024-03-04 19:53:30 -05:00
checkqueue.h refactor: Make CCheckQueue non-copyable and non-movable explicitly 2023-10-03 10:52:17 +01:00
clientversion.cpp util: Move util/string.h functions to util namespace 2024-05-16 10:16:08 -05:00
clientversion.h scripted-diff: Add IWYU pragma keep to bitcoin-config.h includes 2024-05-01 08:33:04 +02:00
cluster_linearize.h clusterlin: permit passing in existing linearization to Linearize 2024-07-25 10:16:40 -04:00
coins.cpp log: use error level for critical log messages 2024-06-10 13:46:56 +02:00
coins.h tidy: modernize-use-equals-default 2024-07-08 11:12:01 +02:00
compressor.cpp Clean up things that include script/standard.h 2023-08-14 17:38:27 -04:00
compressor.h refactor: Rename CTransaction::nVersion to version 2024-06-07 13:55:23 -04:00
core_io.h refactor: Replace ParseHashStr with FromHex 2024-07-24 17:40:18 +02:00
core_memusage.h
core_read.cpp refactor: Replace ParseHashStr with FromHex 2024-07-24 17:40:18 +02:00
core_write.cpp refactor: Rename CTransaction::nVersion to version 2024-06-07 13:55:23 -04:00
cuckoocache.h validation: Don't error if maxsigcachesize exceeds uint32::max 2024-07-04 22:35:29 +02:00
dbwrapper.cpp dbwrapper: Use DataStream for batch operations 2023-09-12 12:07:39 +02:00
dbwrapper.h dbwrapper: Use DataStream for batch operations 2023-09-12 12:07:39 +02:00
deploymentinfo.cpp Decouple RegTestChainParams from ArgsManager 2023-03-15 16:42:42 +01:00
deploymentinfo.h Decouple RegTestChainParams from ArgsManager 2023-03-15 16:42:42 +01:00
deploymentstatus.cpp
deploymentstatus.h
dummywallet.cpp wallet, test: Be able to always swap BDB endianness 2024-05-16 15:03:13 -04:00
external_signer.cpp external_signer: replace boost::process with cpp-subprocess 2024-03-27 14:16:37 +00:00
external_signer.h refactor: Move system from util to common library 2023-05-20 12:08:13 +02:00
flatfile.cpp refactor: Add FlatFileSeq member variables in BlockManager 2024-07-24 09:39:35 +02:00
flatfile.h refactor: Add FlatFileSeq member variables in BlockManager 2024-07-24 09:39:35 +02:00
hash.cpp crypto, hash: replace custom rotl32 with std::rotl 2024-01-05 17:12:38 +01:00
hash.h Include version.h in fewer places 2023-11-16 11:36:22 +10:00
headerssync.cpp random: get rid of GetRand by inlining 2024-07-01 12:39:53 -04:00
headerssync.h tidy: modernize-use-equals-default 2024-07-08 11:12:01 +02:00
httprpc.cpp Merge bitcoin/bitcoin#28167: init: Add option for rpccookie permissions (replace 26088) 2024-06-27 17:35:08 -04:00
httprpc.h
httpserver.cpp rest: don't copy data when sending binary response 2024-06-26 06:47:30 +03:00
httpserver.h Merge bitcoin/bitcoin#30406: refactor: modernize-use-equals-default 2024-07-11 19:08:46 +01:00
i2p.cpp Merge bitcoin/bitcoin#29833: i2p: fix and improve logs 2024-06-26 15:28:26 -04:00
i2p.h i2p: log errors properly according to their severity 2024-06-12 16:19:50 -03:00
indirectmap.h
init.cpp Merge bitcoin/bitcoin#22729: Make it possible to disable Tor binds and abort startup on bind failure 2024-07-16 16:27:24 -04:00
init.h refactor: fix missing includes 2024-07-24 15:57:01 +01:00
key.cpp refactor: remove unused CKey::Negate method 2024-06-03 16:59:43 +02:00
key.h refactor: add self-assign checks to classes which violate the clang-tidy check 2024-06-14 10:27:03 +00:00
key_io.cpp policy: Add OP_1 <0x4e73> as a standard output type 2024-07-30 14:06:58 -04:00
key_io.h Clean up things that include script/standard.h 2023-08-14 17:38:27 -04:00
logging.cpp rpc: make logging method reject "0" category and correct the help text 2024-08-04 06:43:00 +02:00
logging.h logging, refactor: minor encapsulation improvement and use BCLog::NONE instead of 0 2024-08-04 06:42:59 +02:00
Makefile.am clusterlin: introduce cluster_linearize.h with Cluster and DepGraph types 2024-07-25 10:16:37 -04:00
Makefile.bench.include bench: Candidate finding and linearization benchmarks 2024-07-25 10:16:40 -04:00
Makefile.crc32c.include
Makefile.leveldb.include
Makefile.minisketch.include build: remove minisketch clz check 2024-04-12 14:28:34 +02:00
Makefile.qt.include build, test, doc: Temporarily remove Android-related stuff 2024-05-06 11:29:14 +01:00
Makefile.qt_locale.include qt: 27.0 translations update 2024-03-04 10:02:26 +00:00
Makefile.qttest.include
Makefile.test.include [fuzz] Harness for version handshake 2024-07-31 13:25:52 +01:00
Makefile.test_fuzz.include fuzz: move MockedDescriptorConverter to fuzz/util 2023-11-20 15:57:50 -03:00
Makefile.test_util.include tests: framework for testing DepGraph class 2024-07-25 10:16:37 -04:00
Makefile.univalue.include
mapport.cpp upnp: add compatibility for miniupnpc 2.2.8 2024-06-18 12:24:48 +00:00
mapport.h Remove configure-time setting of DEFAULT_UPNP 2023-01-28 15:24:13 +00:00
memusage.h add std::list to memusage 2023-09-13 11:37:45 +01:00
merkleblock.cpp [clang-tidy] Enable the misc-no-recursion check 2024-04-07 14:04:45 +01:00
merkleblock.h tidy: modernize-use-equals-default 2024-07-08 11:12:01 +02:00
net.cpp Merge bitcoin-core/gui#626: Showing Local Addresses in Node Window 2024-08-02 14:19:02 +01:00
net.h Merge bitcoin-core/gui#626: Showing Local Addresses in Node Window 2024-08-02 14:19:02 +01:00
net_permissions.cpp util: move fees.h and error.h to common/messages.h 2024-05-16 10:16:08 -05:00
net_permissions.h Accept "in" and "out" flags to -whitelist to allow whitelisting manual connections 2024-02-28 10:05:56 -03:00
net_processing.cpp scripted-diff: Rename lazily initialized bloom filters 2024-07-31 13:23:46 +01:00
net_processing.h tidy: modernize-use-equals-default 2024-07-08 11:12:01 +02:00
net_types.cpp rpc: avoid copying into UniValue 2024-05-20 16:48:19 +00:00
net_types.h tidy: modernize-use-equals-default 2024-07-08 11:12:01 +02:00
netaddress.cpp util: Move util/string.h functions to util namespace 2024-05-16 10:16:08 -05:00
netaddress.h random: get rid of GetRand by inlining 2024-07-01 12:39:53 -04:00
netbase.cpp Merge bitcoin/bitcoin#30245: net: Allow -proxy=[::1] on nodes with IPV6 lo only 2024-07-18 17:51:16 -04:00
netbase.h netbase: extend CreateSock() to support creating arbitrary sockets 2024-06-14 14:23:50 +02:00
netgroup.cpp Merge bitcoin/bitcoin#27581: net: Continuous ASMap health check 2023-12-06 11:22:42 -05:00
netgroup.h net: Add continuous ASMap health check logging 2023-12-02 22:03:08 +01:00
netmessagemaker.h refactor: NetMsg::Make() without nVersion 2023-11-20 14:02:27 +01:00
noui.cpp node: Use log levels in noui_ThreadSafeMessageBox 2024-03-21 16:41:16 +01:00
noui.h
outputtype.cpp fix incorrect multisig redeem script size limit for segwit 2024-05-03 14:20:44 -03:00
outputtype.h fix incorrect multisig redeem script size limit for segwit 2024-05-03 14:20:44 -03:00
pow.cpp
pow.h
prevector.h tidy: modernize-use-equals-default 2024-07-08 11:12:01 +02:00
protocol.cpp protocol: make message types constexpr 2024-02-28 18:03:23 +01:00
protocol.h Merge bitcoin/bitcoin#29421: net: make the list of known message types a compile time constant 2024-05-21 13:59:33 -04:00
psbt.cpp util: add TransactionError includes and namespace declarations 2024-05-16 10:16:08 -05:00
psbt.h Merge bitcoin/bitcoin#30406: refactor: modernize-use-equals-default 2024-07-11 19:08:46 +01:00
pubkey.cpp crypto: add NUMS_H const 2024-05-14 10:24:31 +02:00
pubkey.h crypto: add NUMS_H const 2024-05-14 10:24:31 +02:00
random.cpp random: replace construct/assign with explicit Reseed() 2024-07-01 12:39:57 -04:00
random.h random: drop ad-hoc Shuffle in favor of std::shuffle 2024-07-06 09:06:36 -04:00
randomenv.cpp Fix MSVC warning C4273 "inconsistent dll linkage" 2024-07-19 22:01:01 +01:00
randomenv.h
rest.cpp refactor: Replace ParseHashStr with FromHex 2024-07-24 17:40:18 +02:00
rest.h
reverse_iterator.h
scheduler.cpp scripted-diff: Rename SingleThreadedSchedulerClient to SerialTaskRunner 2024-02-15 14:43:14 +01:00
scheduler.h kernel: Remove dependency on CScheduler 2024-02-16 17:12:52 +01:00
serialize.h tidy: modernize-use-equals-default 2024-07-08 11:12:01 +02:00
signet.cpp refactor: Rename CTransaction::nVersion to version 2024-06-07 13:55:23 -04:00
signet.h
span.h Allow int8_t optimized vector serialization 2023-12-22 09:58:19 +01:00
streams.cpp Add AutoFile::seek and tell 2024-04-01 14:37:24 -04:00
streams.h tidy: modernize-use-equals-default 2024-07-08 11:12:01 +02:00
sync.cpp util: avoid using thread_local variable that has a destructor 2024-05-16 18:16:46 +02:00
sync.h tidy: modernize-use-equals-default 2024-07-08 11:12:01 +02:00
threadsafety.h tidy: modernize-use-equals-default 2024-07-08 11:12:01 +02:00
tinyformat.h tidy: modernize-use-equals-default 2024-07-08 11:12:01 +02:00
torcontrol.cpp util: Move util/string.h functions to util namespace 2024-05-16 10:16:08 -05:00
torcontrol.h doc, refactor: Changing -torcontrol help to specify that a default port is used 2023-08-17 23:58:47 -05:00
txdb.cpp Merge bitcoin/bitcoin#28195: blockstorage: Drop legacy -txindex check 2023-09-05 11:37:35 +01:00
txdb.h move-only: Move CBlockTreeDB to node/blockstorage 2023-08-01 15:27:33 +02:00
txmempool.cpp random: get rid of GetRand by inlining 2024-07-01 12:39:53 -04:00
txmempool.h doc: use proper doxygen formatting for CTxMemPool::cs 2024-07-23 12:21:41 +02:00
txorphanage.cpp [refactor] delete EraseTxNoLock, just use EraseTx 2024-07-16 10:21:41 +01:00
txorphanage.h [doc] TxOrphanage is no longer thread-safe 2024-07-24 10:38:35 +01:00
txrequest.cpp random: get rid of GetRand by inlining 2024-07-01 12:39:53 -04:00
txrequest.h
uint256.cpp scripted-diff: Rename SetHex to SetHexDeprecated 2024-07-24 09:15:34 +02:00
uint256.h doc + test: Correct uint256 hex string endianness 2024-08-03 21:59:54 +02:00
undo.h Include version.h in fewer places 2023-11-16 11:36:22 +10:00
validation.cpp [refactor] change ActiveTipChange to use CBlockIndex ref instead of ptr 2024-07-25 11:01:22 +01:00
validation.h refactor: Make m_last_notified_header private 2024-07-17 09:12:28 +02:00
validationinterface.cpp [refactor] change ActiveTipChange to use CBlockIndex ref instead of ptr 2024-07-25 11:01:22 +01:00
validationinterface.h [refactor] change ActiveTipChange to use CBlockIndex ref instead of ptr 2024-07-25 11:01:22 +01:00
versionbits.cpp
versionbits.h
walletinitinterface.h tidy: modernize-use-equals-default 2024-07-08 11:12:01 +02:00