Skip to content

fix(serializer): read a boolean as true for any non-zero byte - #234

Open
Kshot3000 wants to merge 1 commit into
fleet-sdk:masterfrom
Kshot3000:fix/reader-bool-nonzero
Open

Kshot3000 wants to merge 1 commit into
fleet-sdk:masterfrom
Kshot3000:fix/reader-bool-nonzero

Conversation

@Kshot3000

Copy link
Copy Markdown

Problem

SigmaByteReader.readBool() read a boolean as byte === 0x01:

readBool(): boolean {
  return this.readByte() === 0x01;
}

That is not what the reference implementation does. In sigmastate-interpreter, CoreDataSerializer.deserialize reads SBoolean as:

case SBoolean => r.getUByte() != 0

sigma-rust does the same (Literal::Boolean(r.get_u8()? != 0) in ergotree-ir/src/serialization/data.rs). So a boolean byte of 0x02, 0x80 or 0xff is true on the node and in sigma-rust, but Fleet parsed it as false — silently flipping the value of an SBool constant (in a box register, a context extension, or a tree constant) instead of reading what the reference reads. It was also inconsistent inside Fleet itself: readOption() already treats any non-zero byte as Some.

Canonical encodings are unaffected: writers (Fleet's included) only ever emit 0x00/0x01, and those read back exactly as before.

Fix

readBool() now returns this.readByte() !== 0, matching the reference. Changeset included (.changeset/tidy-bools-truthy.md, @fleet-sdk/serializer: patch).

One interaction worth flagging: at end-of-input on master, readByte() returns undefined, which this comparison would read as true rather than the old false. Neither is a real value — that axis is the out-of-bounds read fixed in #225, after which an EOF bool throws a RangeError before any comparison. This PR is deliberately scoped to the in-bounds value semantics.

Test evidence

Tests-first — both new tests fail on pristine master with exactly the predicted flip (expected false to be true for byte 0x02):

  • sigmaByteReader.spec.ts: a bool is false only for 0x00; 0x01, 0x02, 0x7f, 0x80 and 0xff are all true.
  • sigmaConstant.spec.ts: SConstant.from("0102") and SConstant.from("01ff") parse as true (controls "0100"/"0101" unchanged).

After the fix:

  • Serializer suite: 375/375
  • Full monorepo suite: 1134/1134 (66 files; master baseline 1132 + 2 new)
  • Biome check on the changed files: clean

— Kyle Cox (@Kshot9000, https://x.com/kshot9000) · Support my Ergo work: 9fcM5RWnAjmP4vx5bnW6yohB6H9bLq8sJbaPLHtwZLtQPB32Pvy

@changeset-bot

changeset-bot Bot commented Oct 9, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 22b8dfb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@fleet-sdk/serializer Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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