Skip to content

fix: Box/Header identity and Box.bytes use the retained wire bytes - #900

Open
mwaddip wants to merge 3 commits into
ergoplatform:developfrom
mwaddip:fix/box-header-eq-id-basis
Open

fix: Box/Header identity and Box.bytes use the retained wire bytes#900
mwaddip wants to merge 3 commits into
ergoplatform:developfrom
mwaddip:fix/box-header-eq-id-basis

Conversation

@mwaddip

@mwaddip mwaddip commented Jun 10, 2026

Copy link
Copy Markdown

ErgoBox/Header derived id by re-serializing the decoded value (and Box.bytes re-serialized too). The reference impl retains the exact consumed parse slice and derives id/ErgoBox.bytes from it; ErgoBox.equals is 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_parse now retain the consumed slice and hash it for id; ErgoBox equality is id-only. ExtractBytesWithNoRef is intentionally unchanged — the candidate has no retained slice and re-serializes canonically (asymmetric). Field-built boxes and canonical inputs are unchanged. Tests included.

mwaddip and others added 3 commits June 10, 2026 19:11
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>
@mwaddip mwaddip changed the title fix: Box/Header id and equality use the retained wire bytes fix: Box/Header identity and Box.bytes use the retained wire bytes Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant