fix: Box/Header identity and Box.bytes use the retained wire bytes - #900
Open
mwaddip wants to merge 3 commits into
Open
fix: Box/Header identity and Box.bytes use the retained wire bytes#900mwaddip wants to merge 3 commits into
mwaddip wants to merge 3 commits into
Conversation
The reference impl retains the exact consumed input slice when parsing a box (`ErgoBox.sigmaSerializer.parse`, ErgoBox.scala:214-225) and computes `id = Blake2b256(bytes)` over it; `ErgoBox.equals` (:188-191) then compares ids. sigma-rust re-serialized the decoded box to compute the id and compared boxes field-wise, so a box carrying a non-canonically-encoded but accepted value (e.g. a `0x00`-lead "garbage identity" GroupElement, whose bytes 1..32 are discarded at parse and normalized on re-serialize) got the *canonical* id and compared equal to its canonical twin — where the reference impl gives distinct ids and EQ false. `sigma_parse` now captures the consumed slice and hashes it for `id`; `PartialEq` compares `box_id` only. Boxes built from fields are unaffected (no retained bytes => canonical serialization, as before). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
`ErgoHeader.sigmaSerializer.parse` (ErgoHeader.scala:167-180) retains the exact consumed input slice and computes `id = Blake2b256(bytes)` over it; `CHeader` equality is id-based. sigma-rust re-serialized the decoded header to compute the id, which normalizes non-canonically-encoded but accepted values (e.g. a `0x00`- lead "garbage identity" minerPk), so a header deserialized from such bytes got the canonical id and compared equal to its canonical twin — where the reference impl gives distinct ids and EQ false. `scorex_parse` now hashes the consumed slice for `id`. Canonical headers (every real block) are unaffected: the retained slice equals the re-serialization. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
`ExtractBytes` (`Box.bytes`) re-serialized the decoded box, which normalizes a non-canonically-encoded but accepted value (e.g. a 0x00-lead "garbage identity" GroupElement) back to canonical. The reference impl returns `ErgoBox._bytes` — the exact retained parse slice — so a script reading `SELF.bytes` (or hashing it) diverged on such inputs. Retain the consumed parse slice on `ErgoBox` (`serialized_bytes`, `None` for field-built boxes) and return it from a new `ErgoBox.bytes`; `ExtractBytes` uses it. `ExtractBytesWithNoRef` (`bytesWithoutRef`) is deliberately unchanged: the candidate has no retained slice in the reference impl and re-serializes canonically, so the accessors are asymmetric. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ErgoBox/Headerderivedidby re-serializing the decoded value (andBox.bytesre-serialized too). The reference impl retains the exact consumed parse slice and derivesid/ErgoBox.bytesfrom it;ErgoBox.equalsis id-based.A non-canonical-but-accepted encoding — e.g. a
0x00-lead "garbage identity" GroupElement (bytes 1..32 discarded at parse) — thus matched its canonical twin's id/bytes, forking container identity (SELF.id,SELF.bytes, box/header EQ).sigma_parse/scorex_parsenow retain the consumed slice and hash it forid;ErgoBoxequality is id-only.ExtractBytesWithNoRefis intentionally unchanged — the candidate has no retained slice and re-serializes canonically (asymmetric). Field-built boxes and canonical inputs are unchanged. Tests included.