Skip to content

fix(serializer): reject out-of-range SByte values on serialization - #223

Open
Kshot3000 wants to merge 1 commit into
fleet-sdk:masterfrom
Kshot3000:fix/sbyte-range-on-serialize
Open

Kshot3000 wants to merge 1 commit into
fleet-sdk:masterfrom
Kshot3000:fix/sbyte-range-on-serialize

Conversation

@Kshot3000

@Kshot3000 Kshot3000 commented Oct 8, 2026 •

Copy link
Copy Markdown

Summary

SByte was the only signed primitive without a serialize-time range check. Every other signed writer throws a RangeError for out-of-range values — writeI16, writeI32, writeI64 and writeI256 in SigmaByteWriter, plus the VLQ writers — but the data serializer wrote byte constants through the raw write() method:

case descriptors.byte.code:
  return writer.write(data as number);

A plain number has no byte range in JS, and the underlying Uint8Array assignment silently wraps, so an out-of-range SByte constant did not round-trip:

  • SByte(200) serialized to the byte 0xc8, which readI8 parses back as -56
  • SByte(128) parsed back as -128
  • SByte(256) serialized to 0x00 and parsed back as 0
  • SByte(511) became -1, SByte(-129) became 127, non-integers were truncated

In Scala the Byte type itself makes these values unrepresentable, so this gap only exists in the JS port.

Fix

  • Add SigmaByteWriter.writeI8, mirroring writeI16: it throws RangeError ("out of range for an 8-bit integer") outside MIN_I8..MAX_I8 and writes negative values as their two's complement byte, which is exactly the conversion readI8 applies when reading.
  • The data serializer's byte case now calls writeI8. In-range encodings are byte-identical to before (the existing byteVectors, including -128 → 0280 and 127 → 027f, pass unchanged).

Deliberate non-change: SColl[Byte] is unaffected — its elements are carried as a Uint8Array of raw bytes and serialized with writeBytes, not through the per-element signed-byte path.

Tests

Tests-first: on pristine master, SByte(200) parses back as -56 and SByte(256) as 0 (probe spec, both assertions failed), and the new specs fail with the fix stashed (3 failures) while passing with it.

  • sigmaByteWriter.spec: new "Should put Byte" vectors (0, 1, 76, MAX_I8, -1, MIN_I8) and byte cases added to the existing out-of-range test
  • sigmaConstant.spec: serializing 128, 200, 255, 256, -129 and -256 as SByte now throws

Full monorepo suite: 1134/1134 tests pass (66 files); serializer package 375/375. Biome check clean on the changed files.


Submitted by @Kshot9000 — Ergo ecosystem work. If this is useful, tips in ERG are welcome: 9fcM5RWnAjmP4vx5bnW6yohB6H9bLq8sJbaPLHtwZLtQPB32Pvy

@changeset-bot

changeset-bot Bot commented Oct 8, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c0cdbcc

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