Skip to content

fix(sdk): strip 0x prefix before padding and validate hex in fromHexString and hexToBytes - #405

Merged
architect-dev merged 3 commits into
FhenixProtocol:masterfrom
Lesnak1:fix/sdk-hex-utils-prefix-and-validation
Oct 14, 2026
Merged

architect-dev merged 3 commits into
FhenixProtocol:masterfrom
Lesnak1:fix/sdk-hex-utils-prefix-and-validation

Conversation

@Lesnak1

@Lesnak1 Lesnak1 commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes hex string decoding in @cofhe/sdk:

  1. Prefix stripping before padding: fromHexString previously padded before stripping ^0x, which transformed odd-length prefixed strings like '0x123' into '00x123'. Because it started with '00', the regex failed to strip 0x, causing parseInt('xa', 16) to evaluate to NaN and silently decode to zero bytes ([0x00, 0x00, 0x23]).
  2. Strict hex validation: Added validation against /^[0-9a-fA-F]*$/ to throw on invalid hex characters instead of silently converting them to 0 bytes.
  3. Canonicalization: Replaced duplicate private implementations in node/index.ts, web/index.ts, and zkProve.worker.ts with the canonical core utility, and delegated hexToBytes to fromHexString.

Testing

  • Added 21 unit tests in packages/sdk/core/test/utils.test.ts covering odd/even lengths, with/without 0x, empty strings, uppercase hex, and error throwing on non-hex characters.
  • Verified pnpm --filter @cofhe/sdk run check:types (0 errors) and pnpm --filter @cofhe/sdk run lint (0 errors).

Note

Medium Risk
Changes how network FHE keys and CRS hex are parsed before TFHE deserialize; fixes incorrect bytes for odd-length 0x input but may surface errors where invalid hex was previously accepted as zeros.

Overview
Fixes hex decoding for FHE public keys and CRS by correcting fromHexString in @cofhe/sdk core and routing all TFHE deserialization through it.

fromHexString now strips 0x before odd-length padding, fixing a regression where values like 0x123 were padded to 00x123 and decoded to wrong bytes. It also validates the hex body and throws Invalid hex string on non-hex digits instead of turning bad pairs into zero bytes via parseInt/NaN. Errors intentionally omit the input string so keys or other sensitive hex are not echoed.

hexToBytes is a thin wrapper around fromHexString. Duplicate local fromHexString copies were removed from the node, web, zkProve worker, and thread-pool test helper in favor of the shared core utility.

New unit tests in packages/sdk/core/test/utils.test.ts cover prefix/padding, validation, uppercase hex, empty input, and non-leaking error messages.

Reviewed by Cursor Bugbot for commit 9cc66d0. Bugbot is set up for automated code reviews on this repo. Configure here.

@changeset-bot

changeset-bot Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9cc66d0

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

This PR includes changesets to release 9 packages
Name Type
@cofhe/sdk Patch
@cofhe/abi Patch
@cofhe/hardhat-3-plugin Patch
@cofhe/hardhat-plugin Patch
@cofhe/mock-contracts Patch
@cofhe/react Patch
@cofhe/site Patch
@cofhe/example-react Patch
@cofhe/foundry-plugin 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

@architect-dev architect-dev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix. I've tightened up the changeset and fixed the formatting. Will merge

@architect-dev
architect-dev merged commit a34bef0 into FhenixProtocol:master Oct 14, 2026
13 checks passed
architect-dev added a commit that referenced this pull request Oct 16, 2026
- Bump @cofhe/sdk to major so the fixed group releases 1.0.0
- Merge the six mock-parity changesets into mocks-match-production
- Drop changesets for small internal fixes (#300, #329, #399, #401, #405)
- Shorten the CofheProvider breaking-change note
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.

2 participants