Repository navigation
fix(sdk): strip 0x prefix before padding and validate hex in fromHexString and hexToBytes - #405
Merged
architect-dev merged 3 commits intoOct 14, 2026
Conversation
…tring and hexToBytes
🦋 Changeset detectedLatest commit: 9cc66d0 The changes in this PR will be included in the next version bump. This PR includes changesets to release 9 packages
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
approved these changes
Oct 14, 2026
architect-dev
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the fix. I've tightened up the changeset and fixed the formatting. Will merge
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.
Summary
Fixes hex string decoding in
@cofhe/sdk:fromHexStringpreviously padded before stripping^0x, which transformed odd-length prefixed strings like'0x123'into'00x123'. Because it started with'00', the regex failed to strip0x, causingparseInt('xa', 16)to evaluate toNaNand silently decode to zero bytes ([0x00, 0x00, 0x23])./^[0-9a-fA-F]*$/to throw on invalid hex characters instead of silently converting them to0bytes.node/index.ts,web/index.ts, andzkProve.worker.tswith the canonical core utility, and delegatedhexToBytestofromHexString.Testing
packages/sdk/core/test/utils.test.tscovering odd/even lengths, with/without0x, empty strings, uppercase hex, and error throwing on non-hex characters.pnpm --filter @cofhe/sdk run check:types(0 errors) andpnpm --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
0xinput but may surface errors where invalid hex was previously accepted as zeros.Overview
Fixes hex decoding for FHE public keys and CRS by correcting
fromHexStringin@cofhe/sdkcore and routing all TFHE deserialization through it.fromHexStringnow strips0xbefore odd-length padding, fixing a regression where values like0x123were padded to00x123and decoded to wrong bytes. It also validates the hex body and throwsInvalid hex stringon non-hex digits instead of turning bad pairs into zero bytes viaparseInt/NaN. Errors intentionally omit the input string so keys or other sensitive hex are not echoed.hexToBytesis a thin wrapper aroundfromHexString. Duplicate localfromHexStringcopies 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.tscover 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.