fix(FhenixHRE): handle hex-encoded ciphertexts correctly in hardhatMockDecrypt#24
Open
amathxbt wants to merge 1 commit intoFhenixProtocol:masterfrom
Open
Conversation
The previous implementation split the ciphertext string into characters
and called charCodeAt(0) on each, treating the result as a byte. This
works only for ASCII strings but fails for:
1. Hex-encoded values (e.g. "0x1a2b3c...") returned by real sealoutput
calls - each hex digit pair was interpreted as two char codes instead
of a single byte, producing a wildly wrong bigint.
2. Bytes with values > 127 - charCodeAt returns code points not byte
values, and the high bit gets set incorrectly.
The fix detects 0x-prefixed or raw hex strings and parses them directly
with BigInt("0x..."), falling back to the byte-array path for plaintext
mock values.
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
hardhatMockDecryptinFhenixHardhatRuntimeEnvironment.tsconverts a ciphertext string to abigintby splitting it into characters and callingcharCodeAt(0)on each, treating the result as a byte value. This is incorrect for hex-encoded ciphertexts (the format returned by FHEsealoutputon real networks) and for any byte value > 127.Bug
Consequences:
& 0xffmask, which is a portability hazard when the JS engine returns a multi-byte code point).Fix
Detect hex-encoded inputs (0x-prefix or valid even-length hex string) and parse directly with
BigInt("0x..."). Fall back to the byte-array path for plain mock strings, now with an explicit& 0xffmask for safety.