@enclave-e3/sdk 0.1.7 → 0.1.8

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.
package/dist/index.d.ts CHANGED
@@ -84,13 +84,9 @@ interface SDKConfig {
84
84
  */
85
85
  chainId?: number;
86
86
  /**
87
- * The protocol to use for the Enclave requests
87
+ * The threshold BFV parameters preset name to use for the Enclave requests
88
88
  */
89
- protocol: FheProtocol;
90
- /**
91
- * The protocol parameters to use for the Enclave requests
92
- */
93
- protocolParams?: ProtocolParams;
89
+ thresholdBfvParamsPresetName: ThresholdBfvParamsPresetName;
94
90
  }
95
91
  interface EventListenerConfig {
96
92
  fromBlock?: bigint;
@@ -271,26 +267,14 @@ interface VerifiableEncryptionResult {
271
267
  proof: ProofData;
272
268
  }
273
269
  /**
274
- * The protocol to use for the Enclave requests
275
- */
276
- declare enum FheProtocol {
277
- /**
278
- * The BFV protocol
279
- */
280
- BFV = "BFV",
281
- /**
282
- * The TrBFV protocol
283
- */
284
- TRBFV = "TRBFV"
285
- }
286
- /**
287
- * Protocol parameters for an Enclave program request
270
+ * BFV parameters for an Enclave program request
288
271
  * Example for BFV
289
- * 2048, // degree
290
- * 1032193, // plaintext_modulus
291
- * 0x3FFFFFFF000001, // moduli
272
+ * 512, // degree
273
+ * 10, // plaintext_modulus
274
+ * 0xffffee001, // moduli
275
+ * 0xffffc4001, // moduli
292
276
  */
293
- interface ProtocolParams {
277
+ interface BfvParams {
294
278
  /**
295
279
  * The degree of the polynomial
296
280
  */
@@ -308,36 +292,8 @@ interface ProtocolParams {
308
292
  */
309
293
  error1Variance: string | undefined;
310
294
  }
311
- type ProtocolParamsName = 'INSECURE_SET_2048_1032193_1' | 'INSECURE_SET_512_10_1' | 'INSECURE_SET_512_0XFFFFEE001_1' | 'SET_8192_1000_4' | 'SET_8192_144115188075855872_2';
312
- /**
313
- * Parameters for the BFV protocol
314
- */
315
- declare const BfvProtocolParams: {
316
- /**
317
- * Recommended parameters for BFV protocol
318
- * - Degree: 2048
319
- * - Plaintext modulus: 1032193
320
- * - Moduli:0x3FFFFFFF000001
321
- */
322
- BFV_NORMAL: {
323
- readonly degree: 2048;
324
- readonly plaintextModulus: 1032193n;
325
- readonly moduli: [18014398492704769n];
326
- readonly error1Variance: "10";
327
- };
328
- /**
329
- * Recommended parameters for TrBFV protocol
330
- * - Degree: 8192
331
- * - Plaintext modulus: 1000
332
- * - Moduli: [0x00800000022a0001, 0x00800000021a0001, 0x0080000002120001, 0x0080000001f60001]
333
- */
334
- BFV_THRESHOLD: {
335
- readonly degree: 8192;
336
- readonly plaintextModulus: 1000n;
337
- readonly moduli: [36028797055270913n, 36028797054222337n, 36028797053698049n, 36028797051863041n];
338
- readonly error1Variance: "10";
339
- };
340
- };
295
+ type ThresholdBfvParamsPresetName = 'INSECURE_THRESHOLD_BFV_512' | 'SECURE_THRESHOLD_BFV_8192';
296
+ declare const ThresholdBfvParamsPresetNames: readonly ["INSECURE_THRESHOLD_BFV_512", "SECURE_THRESHOLD_BFV_8192"];
341
297
  /**
342
298
  * The result of encrypting a value and generating a proof
343
299
  */
@@ -518,8 +474,7 @@ declare class EnclaveSDK {
518
474
  private eventListener;
519
475
  private contractClient;
520
476
  private initialized;
521
- private protocol;
522
- private protocolParams?;
477
+ private thresholdBfvParamsPresetName?;
523
478
  private publicClient;
524
479
  constructor(config: SDKConfig);
525
480
  /**
@@ -531,8 +486,8 @@ declare class EnclaveSDK {
531
486
  * @returns The public client
532
487
  */
533
488
  getPublicClient: () => PublicClient;
534
- getBfvParamsSet(name: ProtocolParamsName): Promise<ProtocolParams>;
535
- getProtocolParams(): Promise<ProtocolParams>;
489
+ getThresholdBfvParamsSet(): Promise<BfvParams>;
490
+ computePublicKeyCommitment(publicKey: Uint8Array): Promise<Uint8Array>;
536
491
  /**
537
492
  * Encrypt a number using the configured protocol
538
493
  * @param data - The number to encrypt
@@ -606,7 +561,7 @@ declare class EnclaveSDK {
606
561
  /**
607
562
  * Activate an E3 computation
608
563
  */
609
- activateE3(e3Id: bigint, publicKey: `0x${string}`, gasLimit?: bigint): Promise<Hash>;
564
+ activateE3(e3Id: bigint, gasLimit?: bigint): Promise<Hash>;
610
565
  /**
611
566
  * Publish input for an E3 computation
612
567
  */
@@ -671,8 +626,7 @@ declare class EnclaveSDK {
671
626
  };
672
627
  privateKey?: `0x${string}`;
673
628
  chainId: keyof typeof EnclaveSDK.chains;
674
- protocol: FheProtocol;
675
- protocolParams?: ProtocolParams;
629
+ thresholdBfvParamsPresetName: ThresholdBfvParamsPresetName;
676
630
  }): EnclaveSDK;
677
631
  }
678
632
 
@@ -743,9 +697,9 @@ declare class ContractClient {
743
697
  requestE3(threshold: [number, number], startWindow: [bigint, bigint], duration: bigint, e3Program: `0x${string}`, e3ProgramParams: `0x${string}`, computeProviderParams: `0x${string}`, customParams?: `0x${string}`, gasLimit?: bigint): Promise<Hash>;
744
698
  /**
745
699
  * Activate an E3 computation
746
- * activate(uint256 e3Id, bytes memory publicKey)
700
+ * activate(uint256 e3Id)
747
701
  */
748
- activateE3(e3Id: bigint, publicKey: `0x${string}`, gasLimit?: bigint): Promise<Hash>;
702
+ activateE3(e3Id: bigint, gasLimit?: bigint): Promise<Hash>;
749
703
  /**
750
704
  * Publish input for an E3 computation
751
705
  * publishInput(uint256 e3Id, bytes memory data)
@@ -797,12 +751,6 @@ declare function generateEventId(log: Log): string;
797
751
  * Get the current timestamp in seconds
798
752
  */
799
753
  declare function getCurrentTimestamp(): number;
800
- declare const BFV_PARAMS_SET: {
801
- readonly degree: 2048;
802
- readonly plaintext_modulus: 1032193;
803
- readonly moduli: readonly [18014398492704769n];
804
- readonly error1_variance: "10";
805
- };
806
754
  interface ComputeProviderParams {
807
755
  name: string;
808
756
  parallel: boolean;
@@ -820,7 +768,7 @@ declare const DEFAULT_E3_CONFIG: {
820
768
  * Encode BFV parameters for the smart contract
821
769
  * BFV (Brakerski-Fan-Vercauteren) is a type of fully homomorphic encryption
822
770
  */
823
- declare function encodeBfvParams(degree?: number, plaintext_modulus?: number, moduli?: readonly bigint[], error1_variance?: string): `0x${string}`;
771
+ declare function encodeBfvParams(params: BfvParams): `0x${string}`;
824
772
  /**
825
773
  * Encode compute provider parameters for the smart contract'
826
774
  * If mock is true, the compute provider parameters will return 32 bytes of 0x00
@@ -839,4 +787,4 @@ declare function calculateStartWindow(windowSize?: number): [bigint, bigint];
839
787
  */
840
788
  declare function decodePlaintextOutput(plaintextOutput: string): number | null;
841
789
 
842
- export { type AllEventTypes, BFV_PARAMS_SET, BfvProtocolParams, type CiphernodeAddedData, type CiphernodeRemovedData, type CiphertextOutputPublishedData, type CommitteeFinalizedData, type CommitteePublishedData, type CommitteeRequestedData, type ComputeProviderParams, ContractClient, type ContractInstances, DEFAULT_COMPUTE_PROVIDER_PARAMS, DEFAULT_E3_CONFIG, type E3, type E3ActivatedData, type E3RequestedData, type EnclaveEvent, type EnclaveEventData, EnclaveEventType, EnclaveSDK, type EncryptedValueAndPublicInputs, type EventCallback, type EventFilter, EventListener, type EventListenerConfig, FheProtocol, type PlaintextOutputPublishedData, type Polynomial, type ProtocolParams, type RegistryEventData, RegistryEventType, type SDKConfig, SDKError, type SDKEventEmitter, type VerifiableEncryptionResult, calculateStartWindow, convertToPolynomial, convertToPolynomialArray, decodePlaintextOutput, encodeBfvParams, encodeComputeProviderParams, encodeCustomParams, formatBigInt, formatEventName, generateEventId, generateProof, getCurrentTimestamp, isValidAddress, isValidHash, parseBigInt, parseEventData, sleep };
790
+ export { type AllEventTypes, type BfvParams, type CiphernodeAddedData, type CiphernodeRemovedData, type CiphertextOutputPublishedData, type CommitteeFinalizedData, type CommitteePublishedData, type CommitteeRequestedData, type ComputeProviderParams, ContractClient, type ContractInstances, DEFAULT_COMPUTE_PROVIDER_PARAMS, DEFAULT_E3_CONFIG, type E3, type E3ActivatedData, type E3RequestedData, type EnclaveEvent, type EnclaveEventData, EnclaveEventType, EnclaveSDK, type EncryptedValueAndPublicInputs, type EventCallback, type EventFilter, EventListener, type EventListenerConfig, type PlaintextOutputPublishedData, type Polynomial, type RegistryEventData, RegistryEventType, type SDKConfig, SDKError, type SDKEventEmitter, type ThresholdBfvParamsPresetName, ThresholdBfvParamsPresetNames, type VerifiableEncryptionResult, calculateStartWindow, convertToPolynomial, convertToPolynomialArray, decodePlaintextOutput, encodeBfvParams, encodeComputeProviderParams, encodeCustomParams, formatBigInt, formatEventName, generateEventId, generateProof, getCurrentTimestamp, isValidAddress, isValidHash, parseBigInt, parseEventData, sleep };
package/dist/index.js CHANGED
@@ -44,14 +44,6 @@ function generateEventId(log) {
44
44
  function getCurrentTimestamp() {
45
45
  return Math.floor(Date.now() / 1e3);
46
46
  }
47
- var INSECURE_SET_2048_1032193_1 = {
48
- degree: 2048,
49
- plaintext_modulus: 1032193,
50
- moduli: [0x3fffffff000001n],
51
- // BigInt for the modulus
52
- error1_variance: "10"
53
- };
54
- var BFV_PARAMS_SET = INSECURE_SET_2048_1032193_1;
55
47
  var DEFAULT_COMPUTE_PROVIDER_PARAMS = {
56
48
  name: "risc0",
57
49
  parallel: false,
@@ -67,7 +59,14 @@ var DEFAULT_E3_CONFIG = {
67
59
  payment_amount: "0"
68
60
  // 0 ETH in wei
69
61
  };
70
- function encodeBfvParams(degree = BFV_PARAMS_SET.degree, plaintext_modulus = BFV_PARAMS_SET.plaintext_modulus, moduli = BFV_PARAMS_SET.moduli, error1_variance = BFV_PARAMS_SET.error1_variance) {
62
+ function encodeBfvParams(params) {
63
+ const { degree, plaintextModulus, moduli, error1Variance } = params;
64
+ if (error1Variance === void 0) {
65
+ throw new SDKError(
66
+ "error1Variance is required in ProtocolParams. All BFV parameter sets must specify error1_variance.",
67
+ "MISSING_ERROR1_VARIANCE"
68
+ );
69
+ }
71
70
  return encodeAbiParameters(
72
71
  [
73
72
  {
@@ -84,9 +83,9 @@ function encodeBfvParams(degree = BFV_PARAMS_SET.degree, plaintext_modulus = BFV
84
83
  [
85
84
  {
86
85
  degree: BigInt(degree),
87
- plaintext_modulus: BigInt(plaintext_modulus),
86
+ plaintext_modulus: BigInt(plaintextModulus),
88
87
  moduli: [...moduli],
89
- error1_variance
88
+ error1_variance: error1Variance
90
89
  }
91
90
  ]
92
91
  );
@@ -242,9 +241,9 @@ var ContractClient = class {
242
241
  }
243
242
  /**
244
243
  * Activate an E3 computation
245
- * activate(uint256 e3Id, bytes memory publicKey)
244
+ * activate(uint256 e3Id)
246
245
  */
247
- async activateE3(e3Id, publicKey, gasLimit) {
246
+ async activateE3(e3Id, gasLimit) {
248
247
  if (!this.walletClient) {
249
248
  throw new SDKError("Wallet client required for write operations", "NO_WALLET");
250
249
  }
@@ -260,7 +259,7 @@ var ContractClient = class {
260
259
  address: this.addresses.enclave,
261
260
  abi: Enclave__factory.abi,
262
261
  functionName: "activate",
263
- args: [e3Id, publicKey],
262
+ args: [e3Id],
264
263
  account,
265
264
  gas: gasLimit
266
265
  });
@@ -631,37 +630,10 @@ var RegistryEventType = /* @__PURE__ */ ((RegistryEventType2) => {
631
630
  RegistryEventType2["INITIALIZED"] = "Initialized";
632
631
  return RegistryEventType2;
633
632
  })(RegistryEventType || {});
634
- var FheProtocol = /* @__PURE__ */ ((FheProtocol2) => {
635
- FheProtocol2["BFV"] = "BFV";
636
- FheProtocol2["TRBFV"] = "TRBFV";
637
- return FheProtocol2;
638
- })(FheProtocol || {});
639
- var BfvProtocolParams = {
640
- /**
641
- * Recommended parameters for BFV protocol
642
- * - Degree: 2048
643
- * - Plaintext modulus: 1032193
644
- * - Moduli:0x3FFFFFFF000001
645
- */
646
- BFV_NORMAL: {
647
- degree: 2048,
648
- plaintextModulus: 1032193n,
649
- moduli: [0x3fffffff000001n],
650
- error1Variance: "10"
651
- },
652
- /**
653
- * Recommended parameters for TrBFV protocol
654
- * - Degree: 8192
655
- * - Plaintext modulus: 1000
656
- * - Moduli: [0x00800000022a0001, 0x00800000021a0001, 0x0080000002120001, 0x0080000001f60001]
657
- */
658
- BFV_THRESHOLD: {
659
- degree: 8192,
660
- plaintextModulus: 1000n,
661
- moduli: [0x00800000022a0001n, 0x00800000021a0001n, 0x0080000002120001n, 0x0080000001f60001n],
662
- error1Variance: "10"
663
- }
664
- };
633
+ var ThresholdBfvParamsPresetNames = [
634
+ "INSECURE_THRESHOLD_BFV_512",
635
+ "SECURE_THRESHOLD_BFV_8192"
636
+ ];
665
637
 
666
638
  // src/enclave-sdk.ts
667
639
  import {
@@ -669,6 +641,7 @@ import {
669
641
  bfv_encrypt_vector,
670
642
  bfv_verifiable_encrypt_number,
671
643
  bfv_verifiable_encrypt_vector,
644
+ compute_pk_commitment,
672
645
  get_bfv_params
673
646
  } from "@enclave-e3/wasm";
674
647
 
@@ -755,15 +728,18 @@ var EnclaveSDK = class _EnclaveSDK {
755
728
  if (!isValidAddress(config.contracts.feeToken)) {
756
729
  throw new SDKError("Invalid FeeToken contract address", "INVALID_ADDRESS");
757
730
  }
758
- this.eventListener = new EventListener(config.publicClient);
759
- this.contractClient = new ContractClient(config.publicClient, config.walletClient, config.contracts);
760
- if (!Object.values(FheProtocol).includes(config.protocol)) {
761
- throw new SDKError(`Invalid protocol: ${config.protocol}`, "INVALID_PROTOCOL");
731
+ if (!config.thresholdBfvParamsPresetName) {
732
+ throw new SDKError("Threshold BFV parameters preset name is required", "MISSING_THRESHOLD_BFV_PARAMS_PRESET_NAME");
762
733
  }
763
- this.protocol = config.protocol;
764
- if (config.protocolParams) {
765
- this.protocolParams = config.protocolParams;
734
+ if (!Object.values(ThresholdBfvParamsPresetNames).includes(config.thresholdBfvParamsPresetName)) {
735
+ throw new SDKError(
736
+ `Invalid threshold BFV parameters preset name: ${config.thresholdBfvParamsPresetName}`,
737
+ "INVALID_THRESHOLD_BFV_PARAMS_PRESET_NAME"
738
+ );
766
739
  }
740
+ this.thresholdBfvParamsPresetName = config.thresholdBfvParamsPresetName;
741
+ this.eventListener = new EventListener(config.publicClient);
742
+ this.contractClient = new ContractClient(config.publicClient, config.walletClient, config.contracts);
767
743
  this.publicClient = config.publicClient;
768
744
  }
769
745
  static chains = {
@@ -775,8 +751,7 @@ var EnclaveSDK = class _EnclaveSDK {
775
751
  eventListener;
776
752
  contractClient;
777
753
  initialized = false;
778
- protocol;
779
- protocolParams;
754
+ thresholdBfvParamsPresetName;
780
755
  publicClient;
781
756
  /**
782
757
  * Initialize the SDK
@@ -798,9 +773,9 @@ var EnclaveSDK = class _EnclaveSDK {
798
773
  getPublicClient = () => {
799
774
  return this.publicClient;
800
775
  };
801
- async getBfvParamsSet(name) {
776
+ async getThresholdBfvParamsSet() {
802
777
  await initializeWasm();
803
- let params = get_bfv_params(name);
778
+ let params = get_bfv_params(this.thresholdBfvParamsPresetName);
804
779
  return {
805
780
  degree: Number(params.degree),
806
781
  // degree is returned as a bigint from wasm
@@ -809,17 +784,15 @@ var EnclaveSDK = class _EnclaveSDK {
809
784
  error1Variance: params.error1_variance
810
785
  };
811
786
  }
812
- async getProtocolParams() {
787
+ async computePublicKeyCommitment(publicKey) {
813
788
  await initializeWasm();
814
- if (this.protocolParams) {
815
- return this.protocolParams;
816
- }
817
- switch (this.protocol) {
818
- case "BFV" /* BFV */:
819
- return await this.getBfvParamsSet("INSECURE_SET_2048_1032193_1");
820
- case "TRBFV" /* TRBFV */:
821
- return await this.getBfvParamsSet("INSECURE_SET_512_10_1");
822
- }
789
+ const protocolParams = await this.getThresholdBfvParamsSet();
790
+ return compute_pk_commitment(
791
+ publicKey,
792
+ protocolParams.degree,
793
+ protocolParams.plaintextModulus,
794
+ BigUint64Array.from(protocolParams.moduli)
795
+ );
823
796
  }
824
797
  /**
825
798
  * Encrypt a number using the configured protocol
@@ -829,7 +802,7 @@ var EnclaveSDK = class _EnclaveSDK {
829
802
  */
830
803
  async encryptNumber(data, publicKey) {
831
804
  await initializeWasm();
832
- const protocolParams = await this.getProtocolParams();
805
+ const protocolParams = await this.getThresholdBfvParamsSet();
833
806
  return bfv_encrypt_number(
834
807
  data,
835
808
  publicKey,
@@ -846,7 +819,7 @@ var EnclaveSDK = class _EnclaveSDK {
846
819
  */
847
820
  async encryptVector(data, publicKey) {
848
821
  await initializeWasm();
849
- const protocolParams = await this.getProtocolParams();
822
+ const protocolParams = await this.getThresholdBfvParamsSet();
850
823
  return bfv_encrypt_vector(
851
824
  data,
852
825
  publicKey,
@@ -864,7 +837,7 @@ var EnclaveSDK = class _EnclaveSDK {
864
837
  */
865
838
  async encryptNumberAndGenInputs(data, publicKey) {
866
839
  await initializeWasm();
867
- const protocolParams = await this.getProtocolParams();
840
+ const protocolParams = await this.getThresholdBfvParamsSet();
868
841
  const [encryptedData, circuitInputs] = bfv_verifiable_encrypt_number(
869
842
  data,
870
843
  publicKey,
@@ -901,7 +874,7 @@ var EnclaveSDK = class _EnclaveSDK {
901
874
  */
902
875
  async encryptVectorAndGenInputs(data, publicKey) {
903
876
  await initializeWasm();
904
- const protocolParams = await this.getProtocolParams();
877
+ const protocolParams = await this.getThresholdBfvParamsSet();
905
878
  const [encryptedData, circuitInputs] = bfv_verifiable_encrypt_vector(
906
879
  data,
907
880
  publicKey,
@@ -975,11 +948,11 @@ var EnclaveSDK = class _EnclaveSDK {
975
948
  /**
976
949
  * Activate an E3 computation
977
950
  */
978
- async activateE3(e3Id, publicKey, gasLimit) {
951
+ async activateE3(e3Id, gasLimit) {
979
952
  if (!this.initialized) {
980
953
  await this.initialize();
981
954
  }
982
- return this.contractClient.activateE3(e3Id, publicKey, gasLimit);
955
+ return this.contractClient.activateE3(e3Id, gasLimit);
983
956
  }
984
957
  /**
985
958
  * Publish input for an E3 computation
@@ -1127,23 +1100,20 @@ var EnclaveSDK = class _EnclaveSDK {
1127
1100
  walletClient,
1128
1101
  contracts: options.contracts,
1129
1102
  chainId: options.chainId,
1130
- protocol: options.protocol,
1131
- protocolParams: options.protocolParams
1103
+ thresholdBfvParamsPresetName: options.thresholdBfvParamsPresetName
1132
1104
  });
1133
1105
  }
1134
1106
  };
1135
1107
  export {
1136
- BFV_PARAMS_SET,
1137
- BfvProtocolParams,
1138
1108
  ContractClient,
1139
1109
  DEFAULT_COMPUTE_PROVIDER_PARAMS,
1140
1110
  DEFAULT_E3_CONFIG,
1141
1111
  EnclaveEventType,
1142
1112
  EnclaveSDK,
1143
1113
  EventListener,
1144
- FheProtocol,
1145
1114
  RegistryEventType,
1146
1115
  SDKError,
1116
+ ThresholdBfvParamsPresetNames,
1147
1117
  calculateStartWindow,
1148
1118
  convertToPolynomial,
1149
1119
  convertToPolynomialArray,