Skip to content

fix(sdk): cache the CRS per security zone - #322

Open
Dusk1e wants to merge 1 commit into
FhenixProtocol:masterfrom
Dusk1e:fix/crs-per-security-zone
Open

fix(sdk): cache the CRS per security zone#322
Dusk1e wants to merge 1 commit into
FhenixProtocol:masterfrom
Dusk1e:fix/crs-per-security-zone

Conversation

@Dusk1e

@Dusk1e Dusk1e commented Aug 13, 2026

Copy link
Copy Markdown

fetchCrs asks CoFHE for the CRS of a specific securityZone but caches it under the chain id alone, while fetchFhePublicKey right above it keys by chain and zone. So once you encrypt on zone 0 and then call setSecurityZone(1), encryptInputs reads zone 0's CRS back out of the store. A stale CRS deserializes fine, so it passes checkKeyValidity and gets used to build the proof, and the ZK verifier rejects it.

The zones really do have different CRS values. POST /GetCrs to testnet-cofhe.fhenix.zone with securityZone: 0 and securityZone: 1 returns two different 7,968,512 byte blobs (sha256 e0cf6cf5 vs 9e062a6f). GetNetworkPublicKey differs per zone too, which is why the FHE key was already cached per zone.

I keyed crs by chain and zone the same way fhe already is, and made setCrs take the zone in the same position setFheKey does. A crs persisted under the old shape holds a bare string per chain, so those entries are dropped on rehydrate and refetched per zone instead of being indexed as a string. Also fixed insertMockKeys in the tests, which took a securityZone, passed it to setFheKey, then hardcoded zone 0 for setCrs — the same conflation.

Added a keyStore test for zone isolation and a fetchKeys test that a zone 0 CRS is not reused for zone 1. The zone isolation test fails on master. Full node suite is 378 passing; the two failing files are the pre-existing @cofhe/test-setup resolution errors that fail on master too. tsc --noEmit reports the same 3 errors before and after, all from that same missing module.

`fetchCrs` requested the CRS for a given `securityZone` but cached it under the
chain id alone, while `fetchFhePublicKey` right above it keys by
`(chainId, securityZone)`. So after encrypting on one zone, `encryptInputs` on a
different zone read the first zone's CRS back out of the store. A stale CRS
deserializes fine, so it passed `checkKeyValidity` and was used to build the
proof, which the ZK verifier then rejects.

CoFHE does serve a distinct CRS per zone. `POST /GetCrs` against
testnet-cofhe.fhenix.zone with `securityZone: 0` and `securityZone: 1` returns
two different values (both 7,968,512 bytes, sha256 e0cf6cf5a2af9241 vs
9e062a6f0c8208d3). `GetNetworkPublicKey` differs per zone too, which is why the
FHE key is already cached per zone.

`KeysStore.crs` is now keyed by chain and zone like `KeysStore.fhe`, `getCrs`
takes an optional zone defaulting to 0, and `setCrs(chainId, securityZone, crs)`
matches `setFheKey`'s argument order. On rehydrate, a `crs` persisted under the
old shape (a bare string per chain) is dropped so the CRS is refetched per zone
instead of being indexed as a string.

Also fixes the `insertMockKeys` test helper, which took a `securityZone` and
passed it to `setFheKey` but hardcoded zone 0 for `setCrs` — the same conflation.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@changeset-bot

changeset-bot Bot commented Aug 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6ff3430

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 Minor
@cofhe/abi Minor
@cofhe/hardhat-3-plugin Minor
@cofhe/hardhat-plugin Minor
@cofhe/mock-contracts Minor
@cofhe/react Minor
@cofhe/site Minor
@cofhe/example-react Minor
@cofhe/foundry-plugin Minor

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

@vercel

vercel Bot commented Aug 13, 2026

Copy link
Copy Markdown

@Dusk1e is attempting to deploy a commit to the Fhenix Team on Vercel.

A member of the Team first needs to authorize it.

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