@cofhe/sdk 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/acps/acp.ts +10 -1
  3. package/acps/index.ts +11 -0
  4. package/acps/onchain-utils.ts +188 -49
  5. package/acps/registry.ts +73 -0
  6. package/acps/test/onchainAccessStatus.test.ts +116 -0
  7. package/acps/types.ts +16 -0
  8. package/chains/chains/arbitrum.ts +14 -0
  9. package/chains/chains/ethereum.ts +14 -0
  10. package/chains/index.ts +5 -1
  11. package/chains/test/chains.test.ts +24 -3
  12. package/core/acps.ts +51 -143
  13. package/core/client.ts +13 -8
  14. package/core/clientTypes.ts +29 -4
  15. package/core/config.ts +25 -1
  16. package/core/consts.ts +14 -2
  17. package/core/decrypt/verifyDecryptResult.ts +4 -3
  18. package/core/encrypt/encryptInputsBuilder.ts +6 -0
  19. package/core/encrypt/zkPackProveVerify.ts +7 -1
  20. package/core/error.ts +1 -0
  21. package/core/fetchKeys.ts +2 -2
  22. package/core/index.ts +9 -1
  23. package/core/keyStore.ts +23 -10
  24. package/core/taskManager.ts +48 -0
  25. package/core/test/acps.test.ts +91 -1
  26. package/core/test/client.test.ts +41 -0
  27. package/core/test/config.test.ts +16 -0
  28. package/core/test/decrypt.test.ts +11 -4
  29. package/core/test/encryptInputsBuilder.test.ts +1 -1
  30. package/core/test/fetchKeys.test.ts +23 -2
  31. package/core/test/keyStore.test.ts +18 -6
  32. package/core/test/taskManager.test.ts +57 -0
  33. package/core/types.ts +8 -0
  34. package/dist/acps.cjs +314 -35
  35. package/dist/acps.d.cts +251 -4
  36. package/dist/acps.d.ts +251 -4
  37. package/dist/acps.js +2 -2
  38. package/dist/chains.cjs +26 -0
  39. package/dist/chains.d.cts +45 -1
  40. package/dist/chains.d.ts +45 -1
  41. package/dist/chains.js +1 -1
  42. package/dist/{chunk-43USWPEH.js → chunk-GGI7Y6AW.js} +59 -264
  43. package/dist/chunk-KSOV5B4B.js +50 -0
  44. package/dist/{chunk-ESMZCFJY.js → chunk-NMKMCAI3.js} +4 -2
  45. package/dist/{chunk-Q7CBWGQX.js → chunk-UJYOBHYA.js} +374 -37
  46. package/dist/{chunk-N6IDQRRU.js → chunk-UKQAJK4N.js} +25 -1
  47. package/dist/{clientTypes-BN3nbzYM.d.ts → clientTypes-5YPPR16m.d.ts} +77 -14
  48. package/dist/{clientTypes-CYZjFznO.d.cts → clientTypes-D3aIXmUp.d.cts} +77 -14
  49. package/dist/core.cjs +271 -136
  50. package/dist/core.d.cts +29 -8
  51. package/dist/core.d.ts +29 -8
  52. package/dist/core.js +4 -4
  53. package/dist/node.cjs +266 -136
  54. package/dist/node.d.cts +2 -2
  55. package/dist/node.d.ts +2 -2
  56. package/dist/node.js +5 -5
  57. package/dist/{acp-Wi6isVQI.d.cts → onchain-utils-CUAK8glx.d.cts} +31 -1
  58. package/dist/{acp-Wi6isVQI.d.ts → onchain-utils-CUAK8glx.d.ts} +31 -1
  59. package/dist/web.cjs +360 -150
  60. package/dist/web.d.cts +49 -3
  61. package/dist/web.d.ts +49 -3
  62. package/dist/web.js +51 -20
  63. package/dist/zkProve.worker.cjs +69 -15
  64. package/dist/zkProve.worker.js +23 -17
  65. package/node/storage.ts +2 -2
  66. package/node/test/inherited.test.ts +5 -1
  67. package/node/test/storage-memory-fallback.test.ts +47 -0
  68. package/package.json +5 -3
  69. package/web/index.ts +100 -26
  70. package/web/test/countingZkProveWorker.ts +31 -0
  71. package/web/test/inherited.web.test.ts +5 -1
  72. package/web/test/mainThreadPool.web.test.ts +174 -0
  73. package/web/test/threadPoolBenchHelper.ts +140 -0
  74. package/web/test/threadPoolOff.web.test.ts +17 -0
  75. package/web/test/threadPoolOn.web.test.ts +80 -0
  76. package/web/test/workerInitRace.web.test.ts +79 -0
  77. package/web/tfheThreadPool.ts +140 -0
  78. package/web/workerManager.ts +4 -2
  79. package/web/zkProve.worker.ts +42 -17
@@ -1,10 +1,17 @@
1
1
  import { WalletClient, PublicClient, Hex } from 'viem';
2
2
  import { C as CofheChain } from './types-C07FK-cL.js';
3
3
  import { z } from 'zod';
4
- import { A as ACP, h as EIP712Domain, C as CreateSelfACPOptions, p as SelfACP, d as CreateSharingACPOptions, q as SharingACP, I as ImportSharedACPOptions, R as RecipientACP, v as ACPHashFields, S as SerializedACP, g as IncomingShare, i as ACPUtils } from './acp-Wi6isVQI.js';
4
+ import { A as ACP, h as EIP712Domain, C as CreateSelfACPOptions, v as SelfACP, d as CreateSharingACPOptions, w as SharingACP, I as ImportSharedACPOptions, R as RecipientACP, B as ACPHashFields, S as SerializedACP, z as ACPAccessStatus, g as IncomingShare, m as AclServedAddresses, i as ACPUtils } from './onchain-utils-CUAK8glx.js';
5
5
  import { StoreApi } from 'zustand/vanilla';
6
6
 
7
7
  type TfheInitializer = () => Promise<boolean>;
8
+ /**
9
+ * How many threads tfhe's rayon pool may use for ZK proof generation (web only).
10
+ * - `'auto'` (default): derive from `navigator.hardwareConcurrency`, capped
11
+ * - a positive number: request exactly that many
12
+ * - `false`: stay single-threaded
13
+ */
14
+ type TfheThreadsSetting = number | 'auto' | false;
8
15
  interface IStorage {
9
16
  getItem: (name: string) => Promise<any>;
10
17
  setItem: (name: string, value: any) => Promise<void>;
@@ -229,6 +236,25 @@ type CofheConfig = {
229
236
  * Default: true
230
237
  */
231
238
  useWorkers: boolean;
239
+ /**
240
+ * How many threads tfhe's rayon pool may use for ZK proof generation
241
+ * (web platform only; ignored on node/hardhat).
242
+ *
243
+ * Proving dominates the cost of `encryptInputs`, and tfhe's wasm is built with
244
+ * rayon, so spreading it across threads is a large win — roughly 5x on an
245
+ * 8-thread pool.
246
+ *
247
+ * Requires a cross-origin-isolated page (`Cross-Origin-Opener-Policy: same-origin`
248
+ * plus `Cross-Origin-Embedder-Policy: require-corp` or `credentialless`). When the
249
+ * page isn't isolated the pool is skipped and proving stays single-threaded, so
250
+ * this is safe to leave on.
251
+ *
252
+ * `'auto'` derives the count from `navigator.hardwareConcurrency` (capped, since
253
+ * the speedup flattens out); a number requests exactly that many; `false` keeps
254
+ * tfhe single-threaded.
255
+ * Default: 'auto'
256
+ */
257
+ tfheThreads: TfheThreadsSetting;
232
258
  /** ACP acp defaults, applied when creating acps (user options always win) */
233
259
  acp: {
234
260
  /**
@@ -310,6 +336,7 @@ declare const CofheConfigSchema: z.ZodObject<{
310
336
  removeItem: z.ZodCustom<(name: string) => Promise<void>, (name: string) => Promise<void>>;
311
337
  }, z.core.$strip>, z.ZodNull]>>;
312
338
  useWorkers: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
339
+ tfheThreads: z.ZodDefault<z.ZodOptional<z.ZodUnion<readonly [z.ZodNumber, z.ZodLiteral<"auto">, z.ZodLiteral<false>]>>>;
313
340
  acp: z.ZodDefault<z.ZodOptional<z.ZodObject<{
314
341
  defaultRevoker: z.ZodOptional<z.ZodCustom<Record<number, `0x${string}`>, Record<number, `0x${string}`>>>;
315
342
  defaultContractScopes: z.ZodOptional<z.ZodCustom<Record<number, `0x${string}`[]>, Record<number, `0x${string}`[]>>>;
@@ -665,6 +692,12 @@ interface ZkProveWorkerRequest {
665
692
  data: any;
666
693
  }>;
667
694
  metadata: number[];
695
+ /**
696
+ * Forwarded from `config.tfheThreads`. The worker applies it the first time it
697
+ * initialises tfhe — `initThreadPool` can only run once per wasm instance, so
698
+ * later requests carrying a different value have no effect.
699
+ */
700
+ tfheThreads?: TfheThreadsSetting;
668
701
  }
669
702
  /**
670
703
  * Message sent from worker back to main thread with proof result
@@ -712,14 +745,14 @@ type ChainRecord<T> = Record<string, T>;
712
745
  type SecurityZoneRecord<T> = Record<number, T>;
713
746
  type KeysStore = {
714
747
  fhe: ChainRecord<SecurityZoneRecord<string | undefined>>;
715
- crs: ChainRecord<string | undefined>;
748
+ crs: ChainRecord<SecurityZoneRecord<string | undefined>>;
716
749
  };
717
750
  type KeysStorage = {
718
751
  store: StoreApi<KeysStore>;
719
752
  getFheKey: (chainId: number | undefined, securityZone?: number) => string | undefined;
720
- getCrs: (chainId: number | undefined) => string | undefined;
753
+ getCrs: (chainId: number | undefined, securityZone?: number) => string | undefined;
721
754
  setFheKey: (chainId: number, securityZone: number, key: string) => void;
722
- setCrs: (chainId: number, crs: string) => void;
755
+ setCrs: (chainId: number, securityZone: number, crs: string) => void;
723
756
  clearKeysStorage: () => Promise<void>;
724
757
  rehydrateKeysStore: () => Promise<void>;
725
758
  };
@@ -753,6 +786,7 @@ type EncryptInputsBuilderParams<T extends EncryptableItem[]> = BaseBuilderParams
753
786
  zkBuilderAndCrsGenerator: ZkBuilderAndCrsGenerator | undefined;
754
787
  initTfhe: TfheInitializer | undefined;
755
788
  zkProveWorkerFn: ZkProveWorkerFunction | undefined;
789
+ beforeMainThreadProve?: (() => Promise<void>) | undefined;
756
790
  keysStorage: KeysStorage | undefined;
757
791
  };
758
792
  /**
@@ -779,6 +813,7 @@ declare class EncryptInputsBuilder<T extends EncryptableItem[]> extends BaseBuil
779
813
  private zkBuilderAndCrsGenerator;
780
814
  private initTfhe;
781
815
  private zkProveWorkerFn;
816
+ private beforeMainThreadProve;
782
817
  private keysStorage;
783
818
  private useWorker;
784
819
  private stepTimestamps;
@@ -971,10 +1006,15 @@ declare class EncryptInputsBuilder<T extends EncryptableItem[]> extends BaseBuil
971
1006
  execute(): Promise<HashPlusProofResult<T>>;
972
1007
  }
973
1008
 
974
- interface AclServedAddresses {
975
- defaultRevoker?: Hex;
976
- shareRegistry?: Hex;
977
- }
1009
+ /** Options for importing a shared acp. */
1010
+ type ImportActivationOptions = {
1011
+ /**
1012
+ * Make the imported acp the active one. Defaults to `true`.
1013
+ * With `false` the acp is only stored: decrypts that use the active acp keep using the current one,
1014
+ * and the imported acp is used only where it is passed explicitly (e.g. `.withACP(acp)`).
1015
+ */
1016
+ activate?: boolean;
1017
+ };
978
1018
  declare const acps: {
979
1019
  getSnapshot: () => {
980
1020
  acps: {
@@ -1070,7 +1110,7 @@ declare const acps: {
1070
1110
  }) => void) => () => void;
1071
1111
  createSelf: (options: CreateSelfACPOptions, publicClient: PublicClient, walletClient: WalletClient) => Promise<SelfACP>;
1072
1112
  createSharing: (options: CreateSharingACPOptions, publicClient: PublicClient, walletClient: WalletClient) => Promise<SharingACP>;
1073
- importShared: (options: ImportSharedACPOptions | string, publicClient: PublicClient, walletClient: WalletClient) => Promise<RecipientACP>;
1113
+ importShared: (options: ImportSharedACPOptions | string, publicClient: PublicClient, walletClient: WalletClient, { activate }?: ImportActivationOptions) => Promise<RecipientACP>;
1074
1114
  getOrCreateSelfACP: (publicClient: PublicClient, walletClient: WalletClient, chainId?: number, account?: string, options?: CreateSelfACPOptions) => Promise<ACP>;
1075
1115
  getOrCreateSharingACP: (publicClient: PublicClient, walletClient: WalletClient, options: CreateSharingACPOptions, chainId?: number, account?: string) => Promise<ACP>;
1076
1116
  getHash: (acp: ACPHashFields) => string;
@@ -1121,12 +1161,13 @@ declare const acps: {
1121
1161
  revokeACP: (acp: ACP, walletClient: WalletClient) => Promise<Hex>;
1122
1162
  revokeAllACPs: (walletClient: WalletClient, publicClient: PublicClient, revokerContract?: Hex) => Promise<Hex>;
1123
1163
  isACPRevoked: (acp: ACP, publicClient: PublicClient) => Promise<boolean>;
1164
+ checkAccess: (acp: ACP, publicClient: PublicClient, handle?: bigint | Hex) => Promise<ACPAccessStatus>;
1124
1165
  shareOnChain: (acp: ACP, walletClient: WalletClient, registry: Hex) => Promise<{
1125
1166
  txHash: Hex;
1126
1167
  shareId: Hex;
1127
1168
  }>;
1128
1169
  getIncomingShares: (publicClient: PublicClient, registry: Hex, recipient: Hex) => Promise<IncomingShare[]>;
1129
- importFromChain: (share: IncomingShare, publicClient: PublicClient, walletClient: WalletClient) => Promise<RecipientACP>;
1170
+ importFromChain: (share: IncomingShare, publicClient: PublicClient, walletClient: WalletClient, importOptions?: ImportActivationOptions) => Promise<RecipientACP>;
1130
1171
  removeShareOnChain: (shareId: Hex, walletClient: WalletClient, registry: Hex) => Promise<Hex>;
1131
1172
  computeShareId: (acp: ACP) => Hex;
1132
1173
  applyACPDefaults: <T extends {
@@ -1195,12 +1236,20 @@ type CofheClientACPsClients = {
1195
1236
  publicClient: PublicClient;
1196
1237
  walletClient: WalletClient;
1197
1238
  };
1239
+ /**
1240
+ * Options for `importShared`. `activate: false` stores the imported acp without making it the active
1241
+ * one (default `true`). Clients default to the connected ones; pass both to override.
1242
+ */
1243
+ type CofheClientImportSharedOptions = ImportActivationOptions & (CofheClientACPsClients | {
1244
+ publicClient?: undefined;
1245
+ walletClient?: undefined;
1246
+ });
1198
1247
  type CofheClientACPs = {
1199
1248
  getSnapshot: typeof acps.getSnapshot;
1200
1249
  subscribe: typeof acps.subscribe;
1201
1250
  createSelf: (options: CreateSelfACPOptions, clients?: CofheClientACPsClients) => Promise<SelfACP>;
1202
1251
  createSharing: (options: CreateSharingACPOptions, clients?: CofheClientACPsClients) => Promise<SharingACP>;
1203
- importShared: (options: ImportSharedACPOptions | string, clients?: CofheClientACPsClients) => Promise<RecipientACP>;
1252
+ importShared: (options: ImportSharedACPOptions | string, importOptions?: CofheClientImportSharedOptions) => Promise<RecipientACP>;
1204
1253
  getACP: (hash: string, chainId?: number, account?: string) => ACP | undefined;
1205
1254
  getACPs: (chainId?: number, account?: string) => Record<string, ACP>;
1206
1255
  getActiveACP: (chainId?: number, account?: string) => ACP | undefined;
@@ -1213,6 +1262,13 @@ type CofheClientACPs = {
1213
1262
  revokeACP: (acp: ACP) => Promise<`0x${string}`>;
1214
1263
  revokeAllACPs: (revokerContract?: `0x${string}`) => Promise<`0x${string}`>;
1215
1264
  isACPRevoked: (acp: ACP) => Promise<boolean>;
1265
+ /**
1266
+ * The ACP on-chain status as a value instead of a revert. Without a handle: 'valid', or
1267
+ * 'expired' / 'revoked' / an invalid signature. With a handle, a valid ACP reports
1268
+ * 'allowed', 'out-of-scope' or 'issuer-not-allowed'. For the issuer copy of a share (type
1269
+ * 'sharing', no recipient signature yet) the handle-less check covers expiry and revocation only.
1270
+ */
1271
+ checkAccess: (acp: ACP, handle?: bigint | `0x${string}`) => Promise<ACPAccessStatus>;
1216
1272
  /** Post a signed sharing ACP to the on-chain share registry (ACL-served, or config `acp.sharingRegistry`). */
1217
1273
  shareOnChain: (acp: ACP) => Promise<{
1218
1274
  txHash: `0x${string}`;
@@ -1220,8 +1276,8 @@ type CofheClientACPs = {
1220
1276
  }>;
1221
1277
  /** Importable shares addressed to the connected account (unexpired, not revoked). */
1222
1278
  getIncomingShares: () => Promise<IncomingShare[]>;
1223
- /** Import a share read from the registry: sign as recipient, store and activate. */
1224
- importFromChain: (share: IncomingShare) => Promise<RecipientACP>;
1279
+ /** Import a share read from the registry: sign as recipient, store and (unless `activate: false`) activate. */
1280
+ importFromChain: (share: IncomingShare, options?: ImportActivationOptions) => Promise<RecipientACP>;
1225
1281
  /** Recipient-side: remove a share from the registry (after import, or to decline). */
1226
1282
  dismissShare: (shareId: `0x${string}`) => Promise<`0x${string}`>;
1227
1283
  /** Issuer-side: retract a pending share from the registry. */
@@ -1238,6 +1294,13 @@ type CofheClientParams<TConfig extends CofheConfig> = {
1238
1294
  compactPkeCrsDeserializer: FheKeyDeserializer;
1239
1295
  initTfhe: TfheInitializer;
1240
1296
  zkProveWorkerFn?: ZkProveWorkerFunction;
1297
+ /**
1298
+ * Runs right before a ZK proof is generated on the calling thread, i.e. only
1299
+ * when the worker path is disabled, unavailable or has failed. Platforms use
1300
+ * it for setup that only pays off when that thread does the proving (web
1301
+ * starts tfhe's rayon thread pool here).
1302
+ */
1303
+ beforeMainThreadProve?: () => Promise<void>;
1241
1304
  };
1242
1305
 
1243
- export { EncryptInputsBuilder as $, type AnyExternalHash as A, type EncryptableToExternalHashMap as B, type CofheInputConfig as C, type ExternalItemHashes as D, type EncryptableItem as E, FheTypes as F, type DecryptPollCallbackFunction as G, type HashPlusProofResult as H, type IStorage as I, type DecryptPollCallbackContext as J, type DecryptEndpoint as K, type LiteralToPrimitive as L, type EncryptStepCallbackFunction as M, type EncryptStepCallbackContext as N, FheUintUTypes as O, type Primitive as P, FheAllUTypes as Q, Encryptable as R, isEncryptableItem as S, EncryptStep as T, type UnsealedItem as U, isLastEncryptionStep as V, fetchKeys as W, type FheKeyDeserializer as X, createKeysStore as Y, type KeysStorage as Z, type KeysStore as _, type CofheConfig as a, type EncryptInputsBuilderUnset as a0, DecryptForViewBuilder as a1, DecryptForTxBuilder as a2, type DecryptForTxResult as a3, type ZkBuilderAndCrsGenerator as a4, type ZkProveWorkerFunction as a5, type ZkProveWorkerRequest as a6, type ZkProveWorkerResponse as a7, zkProveWithWorker as a8, type CofheClient as b, type CofheClientParams as c, type CofheClientConnectionState as d, createCofheConfigBase as e, assertNoRenamedConfigKeys as f, getCofheConfigItem as g, type CofheInternalConfig as h, type CofheClientACPs as i, type EncryptableBool as j, type EncryptableUint8 as k, type EncryptableUint16 as l, type EncryptableUint32 as m, type EncryptableUint64 as n, type EncryptableUint128 as o, type EncryptableAddress as p, type EncryptedNumber as q, type FheTypeValue as r, type ExternalBoolHash as s, type ExternalUint8Hash as t, type ExternalUint16Hash as u, type ExternalUint32Hash as v, type ExternalUint64Hash as w, type ExternalUint128Hash as x, type ExternalAddressHash as y, type ExternalHashProof as z };
1306
+ export { type KeysStorage as $, type AnyExternalHash as A, type EncryptableToExternalHashMap as B, type CofheInputConfig as C, type ExternalItemHashes as D, type EncryptableItem as E, FheTypes as F, type DecryptPollCallbackFunction as G, type HashPlusProofResult as H, type IStorage as I, type DecryptPollCallbackContext as J, type DecryptEndpoint as K, type LiteralToPrimitive as L, type EncryptStepCallbackFunction as M, type EncryptStepCallbackContext as N, type TfheThreadsSetting as O, type Primitive as P, FheUintUTypes as Q, FheAllUTypes as R, Encryptable as S, type TfheInitializer as T, type UnsealedItem as U, isEncryptableItem as V, EncryptStep as W, isLastEncryptionStep as X, fetchKeys as Y, type FheKeyDeserializer as Z, createKeysStore as _, type CofheConfig as a, type KeysStore as a0, EncryptInputsBuilder as a1, type EncryptInputsBuilderUnset as a2, DecryptForViewBuilder as a3, DecryptForTxBuilder as a4, type DecryptForTxResult as a5, type ZkBuilderAndCrsGenerator as a6, type ZkProveWorkerFunction as a7, type ZkProveWorkerRequest as a8, type ZkProveWorkerResponse as a9, zkProveWithWorker as aa, type CofheClient as b, type CofheClientParams as c, type CofheClientConnectionState as d, createCofheConfigBase as e, assertNoRenamedConfigKeys as f, getCofheConfigItem as g, type CofheInternalConfig as h, type CofheClientACPs as i, type EncryptableBool as j, type EncryptableUint8 as k, type EncryptableUint16 as l, type EncryptableUint32 as m, type EncryptableUint64 as n, type EncryptableUint128 as o, type EncryptableAddress as p, type EncryptedNumber as q, type FheTypeValue as r, type ExternalBoolHash as s, type ExternalUint8Hash as t, type ExternalUint16Hash as u, type ExternalUint32Hash as v, type ExternalUint64Hash as w, type ExternalUint128Hash as x, type ExternalAddressHash as y, type ExternalHashProof as z };
@@ -1,10 +1,17 @@
1
1
  import { WalletClient, PublicClient, Hex } from 'viem';
2
2
  import { C as CofheChain } from './types-C07FK-cL.cjs';
3
3
  import { z } from 'zod';
4
- import { A as ACP, h as EIP712Domain, C as CreateSelfACPOptions, p as SelfACP, d as CreateSharingACPOptions, q as SharingACP, I as ImportSharedACPOptions, R as RecipientACP, v as ACPHashFields, S as SerializedACP, g as IncomingShare, i as ACPUtils } from './acp-Wi6isVQI.cjs';
4
+ import { A as ACP, h as EIP712Domain, C as CreateSelfACPOptions, v as SelfACP, d as CreateSharingACPOptions, w as SharingACP, I as ImportSharedACPOptions, R as RecipientACP, B as ACPHashFields, S as SerializedACP, z as ACPAccessStatus, g as IncomingShare, m as AclServedAddresses, i as ACPUtils } from './onchain-utils-CUAK8glx.cjs';
5
5
  import { StoreApi } from 'zustand/vanilla';
6
6
 
7
7
  type TfheInitializer = () => Promise<boolean>;
8
+ /**
9
+ * How many threads tfhe's rayon pool may use for ZK proof generation (web only).
10
+ * - `'auto'` (default): derive from `navigator.hardwareConcurrency`, capped
11
+ * - a positive number: request exactly that many
12
+ * - `false`: stay single-threaded
13
+ */
14
+ type TfheThreadsSetting = number | 'auto' | false;
8
15
  interface IStorage {
9
16
  getItem: (name: string) => Promise<any>;
10
17
  setItem: (name: string, value: any) => Promise<void>;
@@ -229,6 +236,25 @@ type CofheConfig = {
229
236
  * Default: true
230
237
  */
231
238
  useWorkers: boolean;
239
+ /**
240
+ * How many threads tfhe's rayon pool may use for ZK proof generation
241
+ * (web platform only; ignored on node/hardhat).
242
+ *
243
+ * Proving dominates the cost of `encryptInputs`, and tfhe's wasm is built with
244
+ * rayon, so spreading it across threads is a large win — roughly 5x on an
245
+ * 8-thread pool.
246
+ *
247
+ * Requires a cross-origin-isolated page (`Cross-Origin-Opener-Policy: same-origin`
248
+ * plus `Cross-Origin-Embedder-Policy: require-corp` or `credentialless`). When the
249
+ * page isn't isolated the pool is skipped and proving stays single-threaded, so
250
+ * this is safe to leave on.
251
+ *
252
+ * `'auto'` derives the count from `navigator.hardwareConcurrency` (capped, since
253
+ * the speedup flattens out); a number requests exactly that many; `false` keeps
254
+ * tfhe single-threaded.
255
+ * Default: 'auto'
256
+ */
257
+ tfheThreads: TfheThreadsSetting;
232
258
  /** ACP acp defaults, applied when creating acps (user options always win) */
233
259
  acp: {
234
260
  /**
@@ -310,6 +336,7 @@ declare const CofheConfigSchema: z.ZodObject<{
310
336
  removeItem: z.ZodCustom<(name: string) => Promise<void>, (name: string) => Promise<void>>;
311
337
  }, z.core.$strip>, z.ZodNull]>>;
312
338
  useWorkers: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
339
+ tfheThreads: z.ZodDefault<z.ZodOptional<z.ZodUnion<readonly [z.ZodNumber, z.ZodLiteral<"auto">, z.ZodLiteral<false>]>>>;
313
340
  acp: z.ZodDefault<z.ZodOptional<z.ZodObject<{
314
341
  defaultRevoker: z.ZodOptional<z.ZodCustom<Record<number, `0x${string}`>, Record<number, `0x${string}`>>>;
315
342
  defaultContractScopes: z.ZodOptional<z.ZodCustom<Record<number, `0x${string}`[]>, Record<number, `0x${string}`[]>>>;
@@ -665,6 +692,12 @@ interface ZkProveWorkerRequest {
665
692
  data: any;
666
693
  }>;
667
694
  metadata: number[];
695
+ /**
696
+ * Forwarded from `config.tfheThreads`. The worker applies it the first time it
697
+ * initialises tfhe — `initThreadPool` can only run once per wasm instance, so
698
+ * later requests carrying a different value have no effect.
699
+ */
700
+ tfheThreads?: TfheThreadsSetting;
668
701
  }
669
702
  /**
670
703
  * Message sent from worker back to main thread with proof result
@@ -712,14 +745,14 @@ type ChainRecord<T> = Record<string, T>;
712
745
  type SecurityZoneRecord<T> = Record<number, T>;
713
746
  type KeysStore = {
714
747
  fhe: ChainRecord<SecurityZoneRecord<string | undefined>>;
715
- crs: ChainRecord<string | undefined>;
748
+ crs: ChainRecord<SecurityZoneRecord<string | undefined>>;
716
749
  };
717
750
  type KeysStorage = {
718
751
  store: StoreApi<KeysStore>;
719
752
  getFheKey: (chainId: number | undefined, securityZone?: number) => string | undefined;
720
- getCrs: (chainId: number | undefined) => string | undefined;
753
+ getCrs: (chainId: number | undefined, securityZone?: number) => string | undefined;
721
754
  setFheKey: (chainId: number, securityZone: number, key: string) => void;
722
- setCrs: (chainId: number, crs: string) => void;
755
+ setCrs: (chainId: number, securityZone: number, crs: string) => void;
723
756
  clearKeysStorage: () => Promise<void>;
724
757
  rehydrateKeysStore: () => Promise<void>;
725
758
  };
@@ -753,6 +786,7 @@ type EncryptInputsBuilderParams<T extends EncryptableItem[]> = BaseBuilderParams
753
786
  zkBuilderAndCrsGenerator: ZkBuilderAndCrsGenerator | undefined;
754
787
  initTfhe: TfheInitializer | undefined;
755
788
  zkProveWorkerFn: ZkProveWorkerFunction | undefined;
789
+ beforeMainThreadProve?: (() => Promise<void>) | undefined;
756
790
  keysStorage: KeysStorage | undefined;
757
791
  };
758
792
  /**
@@ -779,6 +813,7 @@ declare class EncryptInputsBuilder<T extends EncryptableItem[]> extends BaseBuil
779
813
  private zkBuilderAndCrsGenerator;
780
814
  private initTfhe;
781
815
  private zkProveWorkerFn;
816
+ private beforeMainThreadProve;
782
817
  private keysStorage;
783
818
  private useWorker;
784
819
  private stepTimestamps;
@@ -971,10 +1006,15 @@ declare class EncryptInputsBuilder<T extends EncryptableItem[]> extends BaseBuil
971
1006
  execute(): Promise<HashPlusProofResult<T>>;
972
1007
  }
973
1008
 
974
- interface AclServedAddresses {
975
- defaultRevoker?: Hex;
976
- shareRegistry?: Hex;
977
- }
1009
+ /** Options for importing a shared acp. */
1010
+ type ImportActivationOptions = {
1011
+ /**
1012
+ * Make the imported acp the active one. Defaults to `true`.
1013
+ * With `false` the acp is only stored: decrypts that use the active acp keep using the current one,
1014
+ * and the imported acp is used only where it is passed explicitly (e.g. `.withACP(acp)`).
1015
+ */
1016
+ activate?: boolean;
1017
+ };
978
1018
  declare const acps: {
979
1019
  getSnapshot: () => {
980
1020
  acps: {
@@ -1070,7 +1110,7 @@ declare const acps: {
1070
1110
  }) => void) => () => void;
1071
1111
  createSelf: (options: CreateSelfACPOptions, publicClient: PublicClient, walletClient: WalletClient) => Promise<SelfACP>;
1072
1112
  createSharing: (options: CreateSharingACPOptions, publicClient: PublicClient, walletClient: WalletClient) => Promise<SharingACP>;
1073
- importShared: (options: ImportSharedACPOptions | string, publicClient: PublicClient, walletClient: WalletClient) => Promise<RecipientACP>;
1113
+ importShared: (options: ImportSharedACPOptions | string, publicClient: PublicClient, walletClient: WalletClient, { activate }?: ImportActivationOptions) => Promise<RecipientACP>;
1074
1114
  getOrCreateSelfACP: (publicClient: PublicClient, walletClient: WalletClient, chainId?: number, account?: string, options?: CreateSelfACPOptions) => Promise<ACP>;
1075
1115
  getOrCreateSharingACP: (publicClient: PublicClient, walletClient: WalletClient, options: CreateSharingACPOptions, chainId?: number, account?: string) => Promise<ACP>;
1076
1116
  getHash: (acp: ACPHashFields) => string;
@@ -1121,12 +1161,13 @@ declare const acps: {
1121
1161
  revokeACP: (acp: ACP, walletClient: WalletClient) => Promise<Hex>;
1122
1162
  revokeAllACPs: (walletClient: WalletClient, publicClient: PublicClient, revokerContract?: Hex) => Promise<Hex>;
1123
1163
  isACPRevoked: (acp: ACP, publicClient: PublicClient) => Promise<boolean>;
1164
+ checkAccess: (acp: ACP, publicClient: PublicClient, handle?: bigint | Hex) => Promise<ACPAccessStatus>;
1124
1165
  shareOnChain: (acp: ACP, walletClient: WalletClient, registry: Hex) => Promise<{
1125
1166
  txHash: Hex;
1126
1167
  shareId: Hex;
1127
1168
  }>;
1128
1169
  getIncomingShares: (publicClient: PublicClient, registry: Hex, recipient: Hex) => Promise<IncomingShare[]>;
1129
- importFromChain: (share: IncomingShare, publicClient: PublicClient, walletClient: WalletClient) => Promise<RecipientACP>;
1170
+ importFromChain: (share: IncomingShare, publicClient: PublicClient, walletClient: WalletClient, importOptions?: ImportActivationOptions) => Promise<RecipientACP>;
1130
1171
  removeShareOnChain: (shareId: Hex, walletClient: WalletClient, registry: Hex) => Promise<Hex>;
1131
1172
  computeShareId: (acp: ACP) => Hex;
1132
1173
  applyACPDefaults: <T extends {
@@ -1195,12 +1236,20 @@ type CofheClientACPsClients = {
1195
1236
  publicClient: PublicClient;
1196
1237
  walletClient: WalletClient;
1197
1238
  };
1239
+ /**
1240
+ * Options for `importShared`. `activate: false` stores the imported acp without making it the active
1241
+ * one (default `true`). Clients default to the connected ones; pass both to override.
1242
+ */
1243
+ type CofheClientImportSharedOptions = ImportActivationOptions & (CofheClientACPsClients | {
1244
+ publicClient?: undefined;
1245
+ walletClient?: undefined;
1246
+ });
1198
1247
  type CofheClientACPs = {
1199
1248
  getSnapshot: typeof acps.getSnapshot;
1200
1249
  subscribe: typeof acps.subscribe;
1201
1250
  createSelf: (options: CreateSelfACPOptions, clients?: CofheClientACPsClients) => Promise<SelfACP>;
1202
1251
  createSharing: (options: CreateSharingACPOptions, clients?: CofheClientACPsClients) => Promise<SharingACP>;
1203
- importShared: (options: ImportSharedACPOptions | string, clients?: CofheClientACPsClients) => Promise<RecipientACP>;
1252
+ importShared: (options: ImportSharedACPOptions | string, importOptions?: CofheClientImportSharedOptions) => Promise<RecipientACP>;
1204
1253
  getACP: (hash: string, chainId?: number, account?: string) => ACP | undefined;
1205
1254
  getACPs: (chainId?: number, account?: string) => Record<string, ACP>;
1206
1255
  getActiveACP: (chainId?: number, account?: string) => ACP | undefined;
@@ -1213,6 +1262,13 @@ type CofheClientACPs = {
1213
1262
  revokeACP: (acp: ACP) => Promise<`0x${string}`>;
1214
1263
  revokeAllACPs: (revokerContract?: `0x${string}`) => Promise<`0x${string}`>;
1215
1264
  isACPRevoked: (acp: ACP) => Promise<boolean>;
1265
+ /**
1266
+ * The ACP on-chain status as a value instead of a revert. Without a handle: 'valid', or
1267
+ * 'expired' / 'revoked' / an invalid signature. With a handle, a valid ACP reports
1268
+ * 'allowed', 'out-of-scope' or 'issuer-not-allowed'. For the issuer copy of a share (type
1269
+ * 'sharing', no recipient signature yet) the handle-less check covers expiry and revocation only.
1270
+ */
1271
+ checkAccess: (acp: ACP, handle?: bigint | `0x${string}`) => Promise<ACPAccessStatus>;
1216
1272
  /** Post a signed sharing ACP to the on-chain share registry (ACL-served, or config `acp.sharingRegistry`). */
1217
1273
  shareOnChain: (acp: ACP) => Promise<{
1218
1274
  txHash: `0x${string}`;
@@ -1220,8 +1276,8 @@ type CofheClientACPs = {
1220
1276
  }>;
1221
1277
  /** Importable shares addressed to the connected account (unexpired, not revoked). */
1222
1278
  getIncomingShares: () => Promise<IncomingShare[]>;
1223
- /** Import a share read from the registry: sign as recipient, store and activate. */
1224
- importFromChain: (share: IncomingShare) => Promise<RecipientACP>;
1279
+ /** Import a share read from the registry: sign as recipient, store and (unless `activate: false`) activate. */
1280
+ importFromChain: (share: IncomingShare, options?: ImportActivationOptions) => Promise<RecipientACP>;
1225
1281
  /** Recipient-side: remove a share from the registry (after import, or to decline). */
1226
1282
  dismissShare: (shareId: `0x${string}`) => Promise<`0x${string}`>;
1227
1283
  /** Issuer-side: retract a pending share from the registry. */
@@ -1238,6 +1294,13 @@ type CofheClientParams<TConfig extends CofheConfig> = {
1238
1294
  compactPkeCrsDeserializer: FheKeyDeserializer;
1239
1295
  initTfhe: TfheInitializer;
1240
1296
  zkProveWorkerFn?: ZkProveWorkerFunction;
1297
+ /**
1298
+ * Runs right before a ZK proof is generated on the calling thread, i.e. only
1299
+ * when the worker path is disabled, unavailable or has failed. Platforms use
1300
+ * it for setup that only pays off when that thread does the proving (web
1301
+ * starts tfhe's rayon thread pool here).
1302
+ */
1303
+ beforeMainThreadProve?: () => Promise<void>;
1241
1304
  };
1242
1305
 
1243
- export { EncryptInputsBuilder as $, type AnyExternalHash as A, type EncryptableToExternalHashMap as B, type CofheInputConfig as C, type ExternalItemHashes as D, type EncryptableItem as E, FheTypes as F, type DecryptPollCallbackFunction as G, type HashPlusProofResult as H, type IStorage as I, type DecryptPollCallbackContext as J, type DecryptEndpoint as K, type LiteralToPrimitive as L, type EncryptStepCallbackFunction as M, type EncryptStepCallbackContext as N, FheUintUTypes as O, type Primitive as P, FheAllUTypes as Q, Encryptable as R, isEncryptableItem as S, EncryptStep as T, type UnsealedItem as U, isLastEncryptionStep as V, fetchKeys as W, type FheKeyDeserializer as X, createKeysStore as Y, type KeysStorage as Z, type KeysStore as _, type CofheConfig as a, type EncryptInputsBuilderUnset as a0, DecryptForViewBuilder as a1, DecryptForTxBuilder as a2, type DecryptForTxResult as a3, type ZkBuilderAndCrsGenerator as a4, type ZkProveWorkerFunction as a5, type ZkProveWorkerRequest as a6, type ZkProveWorkerResponse as a7, zkProveWithWorker as a8, type CofheClient as b, type CofheClientParams as c, type CofheClientConnectionState as d, createCofheConfigBase as e, assertNoRenamedConfigKeys as f, getCofheConfigItem as g, type CofheInternalConfig as h, type CofheClientACPs as i, type EncryptableBool as j, type EncryptableUint8 as k, type EncryptableUint16 as l, type EncryptableUint32 as m, type EncryptableUint64 as n, type EncryptableUint128 as o, type EncryptableAddress as p, type EncryptedNumber as q, type FheTypeValue as r, type ExternalBoolHash as s, type ExternalUint8Hash as t, type ExternalUint16Hash as u, type ExternalUint32Hash as v, type ExternalUint64Hash as w, type ExternalUint128Hash as x, type ExternalAddressHash as y, type ExternalHashProof as z };
1306
+ export { type KeysStorage as $, type AnyExternalHash as A, type EncryptableToExternalHashMap as B, type CofheInputConfig as C, type ExternalItemHashes as D, type EncryptableItem as E, FheTypes as F, type DecryptPollCallbackFunction as G, type HashPlusProofResult as H, type IStorage as I, type DecryptPollCallbackContext as J, type DecryptEndpoint as K, type LiteralToPrimitive as L, type EncryptStepCallbackFunction as M, type EncryptStepCallbackContext as N, type TfheThreadsSetting as O, type Primitive as P, FheUintUTypes as Q, FheAllUTypes as R, Encryptable as S, type TfheInitializer as T, type UnsealedItem as U, isEncryptableItem as V, EncryptStep as W, isLastEncryptionStep as X, fetchKeys as Y, type FheKeyDeserializer as Z, createKeysStore as _, type CofheConfig as a, type KeysStore as a0, EncryptInputsBuilder as a1, type EncryptInputsBuilderUnset as a2, DecryptForViewBuilder as a3, DecryptForTxBuilder as a4, type DecryptForTxResult as a5, type ZkBuilderAndCrsGenerator as a6, type ZkProveWorkerFunction as a7, type ZkProveWorkerRequest as a8, type ZkProveWorkerResponse as a9, zkProveWithWorker as aa, type CofheClient as b, type CofheClientParams as c, type CofheClientConnectionState as d, createCofheConfigBase as e, assertNoRenamedConfigKeys as f, getCofheConfigItem as g, type CofheInternalConfig as h, type CofheClientACPs as i, type EncryptableBool as j, type EncryptableUint8 as k, type EncryptableUint16 as l, type EncryptableUint32 as m, type EncryptableUint64 as n, type EncryptableUint128 as o, type EncryptableAddress as p, type EncryptedNumber as q, type FheTypeValue as r, type ExternalBoolHash as s, type ExternalUint8Hash as t, type ExternalUint16Hash as u, type ExternalUint32Hash as v, type ExternalUint64Hash as w, type ExternalUint128Hash as x, type ExternalAddressHash as y, type ExternalHashProof as z };