p2p: return count of bytes hashed sha2_checksum()
What changed, and why it matters
This is a small internal code cleanup in the rust-bitcoin library's peer-to-peer networking code. The sha2_checksum helper function now returns both the checksum and the number of bytes that were hashed, but the only place that calls it still ignores the byte count. There is no visible security fix or behavior change in this commit.
No security action required. Treat as a normal refactor. If reviewing a series of commits, check whether a later commit actually consumes the returned byte count and verify that usage for correctness.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit modifies p2p/src/message.rs so that sha2_checksum() returns a tuple (u64 bytes_hashed, [u8; 4] checksum) instead of just the checksum. The single call site in V1NetworkMessageDecoder destructures the tuple but only uses the checksum. The change appears to be preparatory plumbing for a future feature where the hashed byte count is needed when computing a message body from a header. No validation logic, cryptographic operation, or network behavior is altered.
Changed components
p2p/src/message.rssha2_checksum helperV1NetworkMessageDecoderInspect captured patch +6 / −3
diff --git a/p2p/src/message.rs b/p2p/src/message.rs
index ac8fae8f..92075181 100644
--- a/p2p/src/message.rs
+++ b/p2p/src/message.rs
@@ -1548,7 +1548,7 @@ impl encoding::Decoder for V1NetworkMessageDecoder {
..
} => {
let payload = payload_decoder.end()?;
- let expected_checksum = sha2_checksum(&payload);
+ let (_, expected_checksum) = sha2_checksum(&payload);
if checksum != expected_checksum {
return Err(V1NetworkMessageDecoderError(
V1NetworkMessageDecoderErrorInner::InvalidChecksum {
@@ -2330,12 +2330,15 @@ fn read_bytes_from_finite_reader<D: Read + ?Sized>(
}
/// Does a double-SHA256 on `data` and returns the first 4 bytes.
-fn sha2_checksum(data: &impl encoding::Encodable) -> [u8; 4] {
+fn sha2_checksum(data: &impl encoding::Encodable) -> (u64, [u8; 4]) {
let mut engine = sha256d::HashEngine::new();
hashes::encode_to_engine(data, &mut engine);
+ let bytes_hashed = engine.n_bytes_hashed();
let hash = engine.finalize();
let checksum = hash.to_byte_array();
- [checksum[0], checksum[1], checksum[2], checksum[3]]
+ let leading_bytes = [checksum[0], checksum[1], checksum[2], checksum[3]];
+
+ (bytes_hashed, leading_bytes)
}
/// Error types for network messages.
Why this scored 17/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.