Split hex parsing on U256 into a second impl
What changed, and why it matters
This commit is a simple internal code reorganization. It moves some hex-parsing helper functions for a 256-bit unsigned integer (U256) from one place in the file to another, without changing what the code does or how it behaves. There is no security fix or vulnerability here.
No security action needed. Treat as normal refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change splits the impl U256 block in bitcoin/src/pow.rs into two adjacent impl U256 blocks. The hex parsing methods (from_hex, from_unprefixed_hex, and from_hex_internal) are moved out of the first impl block and into a second one. The method bodies, visibility (fn), signatures, and error types are unchanged. The commit message explains this is preparatory refactoring so that the hex parsing can later be moved into a units module and so that From impls on hex error types can be removed from outside of units. No functional change is intended.
Changed components
bitcoin/src/pow.rsInspect captured patch +32 / −30
diff --git a/bitcoin/src/pow.rs b/bitcoin/src/pow.rs
index 6a53bd7b..f2c6cd00 100644
--- a/bitcoin/src/pow.rs
+++ b/bitcoin/src/pow.rs
@@ -609,36 +609,6 @@ impl U256 {
const ONE: Self = Self(0, 1);
- /// Constructs a new `U256` from a prefixed hex string.
- fn from_hex(s: &str) -> Result<Self, PrefixedHexError> {
- let checked = parse_int::hex_remove_prefix(s)?;
- Ok(Self::from_hex_internal(checked)?)
- }
-
- /// Constructs a new `U256` from an unprefixed hex string.
- fn from_unprefixed_hex(s: &str) -> Result<Self, UnprefixedHexError> {
- let checked = parse_int::hex_check_unprefixed(s)?;
- Ok(Self::from_hex_internal(checked)?)
- }
-
- // Caller to ensure `s` does not contain a prefix.
- fn from_hex_internal(s: &str) -> Result<Self, ParseIntError> {
- let (high, low) = if s.len() <= 32 {
- let low = parse_int::hex_u128_unchecked(s)?;
- (0, low)
- } else {
- let high_len = s.len() - 32;
- let high_s = &s[..high_len];
- let low_s = &s[high_len..];
-
- let high = parse_int::hex_u128_unchecked(high_s)?;
- let low = parse_int::hex_u128_unchecked(low_s)?;
- (high, low)
- };
-
- Ok(Self(high, low))
- }
-
/// Constructs a new `U256` from a big-endian array of `u8`s.
fn from_be_bytes(a: [u8; 32]) -> Self {
let (high, low) = split_in_half(a);
@@ -1010,6 +980,38 @@ impl U256 {
}
}
+impl U256 {
+ /// Constructs a new `U256` from a prefixed hex string.
+ fn from_hex(s: &str) -> Result<Self, PrefixedHexError> {
+ let checked = parse_int::hex_remove_prefix(s)?;
+ Ok(Self::from_hex_internal(checked)?)
+ }
+
+ /// Constructs a new `U256` from an unprefixed hex string.
+ fn from_unprefixed_hex(s: &str) -> Result<Self, UnprefixedHexError> {
+ let checked = parse_int::hex_check_unprefixed(s)?;
+ Ok(Self::from_hex_internal(checked)?)
+ }
+
+ // Caller to ensure `s` does not contain a prefix.
+ fn from_hex_internal(s: &str) -> Result<Self, ParseIntError> {
+ let (high, low) = if s.len() <= 32 {
+ let low = parse_int::hex_u128_unchecked(s)?;
+ (0, low)
+ } else {
+ let high_len = s.len() - 32;
+ let high_s = &s[..high_len];
+ let low_s = &s[high_len..];
+
+ let high = parse_int::hex_u128_unchecked(high_s)?;
+ let low = parse_int::hex_u128_unchecked(low_s)?;
+ (high, low)
+ };
+
+ Ok(Self(high, low))
+ }
+}
+
impl<T: Into<u128>> From<T> for U256 {
fn from(x: T) -> Self { Self(0, x.into()) }
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.