Skip to content

fix(sdk): match the TaskManager's signature checks in verifyDecryptResult - #464

Merged
alexshchur merged 3 commits into
FhenixProtocol:masterfrom
erkancamli:fix/verify-decrypt-result-signature-encoding
Oct 28, 2026
Merged

alexshchur merged 3 commits into
FhenixProtocol:masterfrom
erkancamli:fix/verify-decrypt-result-signature-encoding

Conversation

@erkancamli

@erkancamli erkancamli commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

client.verifyDecryptResult(ctHash, plaintext, signature) is meant for decrypt results that come from somewhere other than your own decryptForTx call (a backend, a cache, another client), and the docs say a true result "means the chain’s verifier considers this signature a valid attestation". It can return true for signatures the TaskManager rejects.

TaskManager._verifyDecryptResult in cofhe-contracts recovers the signer with OpenZeppelin's ECDSA.tryRecover(messageHash, signature), which only accepts a 65 byte signature with v of 27 or 28 and s in the lower half of the curve order. MockTaskManager does the same. verifyDecryptResult.ts recovers with viem's recoverAddress, which also accepts v of 0 or 1 and a high s. So the same valid signature re-encoded in either form:

  • returns true from the SDK's verifyDecryptResult;
  • makes verifyDecryptResult and publishDecryptResult on the TaskManager revert with InvalidSignature(), and verifyDecryptResultSafe return false.

An app that checks results from an untrusted source with the SDK before relaying or publishing them can be handed one that passes the check and then reverts on chain. Anyone holding a genuine result can produce the high s form, and code that serializes the recovery id as 0/1 produces the other. This does not allow a forged plaintext: both forms still recover the configured signer for the same (ctHash, plaintext). Results taken straight from decryptForTx are not affected, since normalizeTnSignature already re-serializes the threshold network signature with v of 27 or 28.

This adds the same checks before recovery: 65 bytes of hex, v of 27 or 28, and s at most the bound used by ECDSA.tryRecover. Malformed input still returns false rather than throwing. The JSDoc and the docs page mention the rule.

Tests: core/test/verifyDecryptResult.test.ts adds a case for v of 0/1 and one for a high s, both built from the configured signer's own signature. Both fail on master (expected true to equal false) and pass with this change. A malformed signature case covers the input check. For the on-chain side I compiled TaskManager from cofhe-contracts (bbd0196, OpenZeppelin 5.2.0) and this repo's MockTaskManager, deployed both on an in-process Hardhat network with a signer set, and called verifyDecryptResult / verifyDecryptResultSafe with the canonical, v 0/1 and high s forms of one signature: true for the canonical one, InvalidSignature() / false for the other two, on both contracts. On the same bytes the SDK returns true for all three on master, and true, false, false with this change. In the sdk node test project (run offline) the only failures are the existing network-dependent ones, identical with and without this change. eslint and prettier are clean on the changed files. A changeset is included.

This only touches the SDK; #414 covers the mock's batch decrypt result functions.


Note

Medium Risk
Changes decrypt-result trust checks for off-chain sources; stricter rejection may block re-encoded signatures that previously passed the SDK but still fail on-chain anyway.

Overview
verifyDecryptResult now rejects signature encodings that OpenZeppelin ECDSA.tryRecover (and the on-chain TaskManager) would reject, even when viem recovery would still match the configured signer.

Before recovery, the SDK requires a 65-byte hex signature with v of 27 or 28 and a low s (≤ secp256k1 half-order). Alternate encodings of the same valid attestation—v 0/1 or high s—return false instead of true, closing the gap where off-chain checks passed but verifyDecryptResult / publishDecryptResult reverted with InvalidSignature.

Tests cover those encodings plus malformed input; docs and JSDoc note the formatting rule. decryptForTx outputs are unchanged when already normalized.

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

@changeset-bot

changeset-bot Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 02275e0

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

@kutluhaneth46 kutluhaneth46 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checked this against TaskManager / OpenZeppelin ECDSA.tryRecover constraints (65-byte r||s||v, v ∈ {27,28}, low-s).

Ran locally on this branch:
pnpm --filter @cofhe/sdk test -- --run verifyDecryptResult → 6/6 passed (incl. v=0/1, high-s, malformed).

Nice alignment: SDK verifyDecryptResult returning false for encodings that would revert on-chain avoids a false-positive “verified” path. Looks ready from the SDK side.

erkancamli and others added 3 commits October 28, 2026 10:50
…sult

TaskManager.verifyDecryptResult checks the signature with OpenZeppelin's
ECDSA.tryRecover, which only accepts 65 bytes with v = 27 or 28 and a
low s value. The SDK's local verifyDecryptResult used viem's
recoverAddress, which also accepts v = 0 or 1 and high s values. So a
valid signature in either form returned true locally, while
verifyDecryptResult and publishDecryptResult on chain revert with
InvalidSignature for it.

Apply the same checks before recovering the signer.
@alexshchur
alexshchur force-pushed the fix/verify-decrypt-result-signature-encoding branch from 43b7971 to 02275e0 Compare October 28, 2026 08:58
@alexshchur
alexshchur merged commit 8fb65ec into FhenixProtocol:master Oct 28, 2026
13 checks passed
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.

4 participants