@enclave-e3/sdk 0.1.7 → 0.1.9

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/README.md CHANGED
@@ -217,7 +217,7 @@ await sdk.requestE3({
217
217
  });
218
218
 
219
219
  // Activate an E3 computation
220
- await sdk.activateE3(e3Id: bigint, publicKey: `0x${string}`, gasLimit?: bigint);
220
+ await sdk.activateE3(e3Id: bigint, gasLimit?: bigint);
221
221
 
222
222
  // Publish input data
223
223
  await sdk.publishInput(e3Id: bigint, data: `0x${string}`, gasLimit?: bigint);
package/dist/index.cjs CHANGED
@@ -30,20 +30,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
- BFV_PARAMS_SET: () => BFV_PARAMS_SET,
34
- BfvProtocolParams: () => BfvProtocolParams,
35
33
  ContractClient: () => ContractClient,
36
34
  DEFAULT_COMPUTE_PROVIDER_PARAMS: () => DEFAULT_COMPUTE_PROVIDER_PARAMS,
37
35
  DEFAULT_E3_CONFIG: () => DEFAULT_E3_CONFIG,
38
36
  EnclaveEventType: () => EnclaveEventType,
39
37
  EnclaveSDK: () => EnclaveSDK,
40
38
  EventListener: () => EventListener,
41
- FheProtocol: () => FheProtocol,
42
39
  RegistryEventType: () => RegistryEventType,
43
40
  SDKError: () => SDKError,
41
+ ThresholdBfvParamsPresetNames: () => ThresholdBfvParamsPresetNames,
44
42
  calculateStartWindow: () => calculateStartWindow,
45
- convertToPolynomial: () => convertToPolynomial,
46
- convertToPolynomialArray: () => convertToPolynomialArray,
47
43
  decodePlaintextOutput: () => decodePlaintextOutput,
48
44
  encodeBfvParams: () => encodeBfvParams,
49
45
  encodeComputeProviderParams: () => encodeComputeProviderParams,
@@ -107,14 +103,6 @@ function generateEventId(log) {
107
103
  function getCurrentTimestamp() {
108
104
  return Math.floor(Date.now() / 1e3);
109
105
  }
110
- var INSECURE_SET_2048_1032193_1 = {
111
- degree: 2048,
112
- plaintext_modulus: 1032193,
113
- moduli: [0x3fffffff000001n],
114
- // BigInt for the modulus
115
- error1_variance: "10"
116
- };
117
- var BFV_PARAMS_SET = INSECURE_SET_2048_1032193_1;
118
106
  var DEFAULT_COMPUTE_PROVIDER_PARAMS = {
119
107
  name: "risc0",
120
108
  parallel: false,
@@ -130,7 +118,14 @@ var DEFAULT_E3_CONFIG = {
130
118
  payment_amount: "0"
131
119
  // 0 ETH in wei
132
120
  };
133
- 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) {
121
+ function encodeBfvParams(params) {
122
+ const { degree, plaintextModulus, moduli, error1Variance } = params;
123
+ if (error1Variance === void 0) {
124
+ throw new SDKError(
125
+ "error1Variance is required in ProtocolParams. All BFV parameter sets must specify error1_variance.",
126
+ "MISSING_ERROR1_VARIANCE"
127
+ );
128
+ }
134
129
  return (0, import_viem.encodeAbiParameters)(
135
130
  [
136
131
  {
@@ -147,9 +142,9 @@ function encodeBfvParams(degree = BFV_PARAMS_SET.degree, plaintext_modulus = BFV
147
142
  [
148
143
  {
149
144
  degree: BigInt(degree),
150
- plaintext_modulus: BigInt(plaintext_modulus),
145
+ plaintext_modulus: BigInt(plaintextModulus),
151
146
  moduli: [...moduli],
152
- error1_variance
147
+ error1_variance: error1Variance
153
148
  }
154
149
  ]
155
150
  );
@@ -305,9 +300,9 @@ var ContractClient = class {
305
300
  }
306
301
  /**
307
302
  * Activate an E3 computation
308
- * activate(uint256 e3Id, bytes memory publicKey)
303
+ * activate(uint256 e3Id)
309
304
  */
310
- async activateE3(e3Id, publicKey, gasLimit) {
305
+ async activateE3(e3Id, gasLimit) {
311
306
  if (!this.walletClient) {
312
307
  throw new SDKError("Wallet client required for write operations", "NO_WALLET");
313
308
  }
@@ -323,7 +318,7 @@ var ContractClient = class {
323
318
  address: this.addresses.enclave,
324
319
  abi: import_types.Enclave__factory.abi,
325
320
  functionName: "activate",
326
- args: [e3Id, publicKey],
321
+ args: [e3Id],
327
322
  account,
328
323
  gas: gasLimit
329
324
  });
@@ -694,37 +689,10 @@ var RegistryEventType = /* @__PURE__ */ ((RegistryEventType2) => {
694
689
  RegistryEventType2["INITIALIZED"] = "Initialized";
695
690
  return RegistryEventType2;
696
691
  })(RegistryEventType || {});
697
- var FheProtocol = /* @__PURE__ */ ((FheProtocol2) => {
698
- FheProtocol2["BFV"] = "BFV";
699
- FheProtocol2["TRBFV"] = "TRBFV";
700
- return FheProtocol2;
701
- })(FheProtocol || {});
702
- var BfvProtocolParams = {
703
- /**
704
- * Recommended parameters for BFV protocol
705
- * - Degree: 2048
706
- * - Plaintext modulus: 1032193
707
- * - Moduli:0x3FFFFFFF000001
708
- */
709
- BFV_NORMAL: {
710
- degree: 2048,
711
- plaintextModulus: 1032193n,
712
- moduli: [0x3fffffff000001n],
713
- error1Variance: "10"
714
- },
715
- /**
716
- * Recommended parameters for TrBFV protocol
717
- * - Degree: 8192
718
- * - Plaintext modulus: 1000
719
- * - Moduli: [0x00800000022a0001, 0x00800000021a0001, 0x0080000002120001, 0x0080000001f60001]
720
- */
721
- BFV_THRESHOLD: {
722
- degree: 8192,
723
- plaintextModulus: 1000n,
724
- moduli: [0x00800000022a0001n, 0x00800000021a0001n, 0x0080000002120001n, 0x0080000001f60001n],
725
- error1Variance: "10"
726
- }
727
- };
692
+ var ThresholdBfvParamsPresetNames = [
693
+ "INSECURE_THRESHOLD_512",
694
+ "SECURE_THRESHOLD_8192"
695
+ ];
728
696
 
729
697
  // src/enclave-sdk.ts
730
698
  var import_wasm = require("@enclave-e3/wasm");
@@ -732,66 +700,10 @@ var import_wasm = require("@enclave-e3/wasm");
732
700
  // src/greco.ts
733
701
  var import_bb = require("@aztec/bb.js");
734
702
  var import_noir_js = require("@noir-lang/noir_js");
735
- var defaultParams = {
736
- bounds: {
737
- pk_bounds: ["34359701504", "34359615488"],
738
- e0_bound: "20",
739
- e1_bound: "20",
740
- u_bound: "1",
741
- r1_low_bounds: ["261", "258"],
742
- r1_up_bounds: ["260", "258"],
743
- r2_bounds: ["34359701504", "34359615488"],
744
- p1_bounds: ["256", "256"],
745
- p2_bounds: ["34359701504", "34359615488"],
746
- k1_low_bound: "5",
747
- k1_up_bound: "4"
748
- },
749
- crypto: {
750
- q_mod_t: "3",
751
- qis: ["68719403009", "68719230977"],
752
- k0is: ["61847462708", "20615769293"]
753
- }
754
- };
755
- var convertToPolynomial = (stringArray) => {
756
- return {
757
- coefficients: stringArray
758
- };
759
- };
760
- var convertToPolynomialArray = (stringArrays) => {
761
- return stringArrays.map(convertToPolynomial);
762
- };
763
703
  var generateProof = async (circuitInputs, circuit) => {
764
704
  const noir = new import_noir_js.Noir(circuit);
765
705
  const backend = new import_bb.UltraHonkBackend(circuit.bytecode, { threads: 4 });
766
- const pk0is_poly = convertToPolynomialArray(circuitInputs.pk0is);
767
- const pk1is_poly = convertToPolynomialArray(circuitInputs.pk1is);
768
- const ct0is_poly = convertToPolynomialArray(circuitInputs.ct0is);
769
- const ct1is_poly = convertToPolynomialArray(circuitInputs.ct1is);
770
- const u_poly = convertToPolynomial(circuitInputs.u);
771
- const e0_poly = convertToPolynomial(circuitInputs.e0);
772
- const e1_poly = convertToPolynomial(circuitInputs.e1);
773
- const e0is_poly = convertToPolynomialArray(circuitInputs.e0is);
774
- const k1_poly = convertToPolynomial(circuitInputs.k1);
775
- const r1is_poly = convertToPolynomialArray(circuitInputs.r1is);
776
- const r2is_poly = convertToPolynomialArray(circuitInputs.r2is);
777
- const p1is_poly = convertToPolynomialArray(circuitInputs.p1is);
778
- const p2is_poly = convertToPolynomialArray(circuitInputs.p2is);
779
- const { witness } = await noir.execute({
780
- params: defaultParams,
781
- pk0is: pk0is_poly,
782
- pk1is: pk1is_poly,
783
- ct0is: ct0is_poly,
784
- ct1is: ct1is_poly,
785
- u: u_poly,
786
- e0: e0_poly,
787
- e1: e1_poly,
788
- e0is: e0is_poly,
789
- k1: k1_poly,
790
- r1is: r1is_poly,
791
- r2is: r2is_poly,
792
- p1is: p1is_poly,
793
- p2is: p2is_poly
794
- });
706
+ const { witness } = await noir.execute(circuitInputs);
795
707
  return await backend.generateProof(witness, { keccakZK: true });
796
708
  };
797
709
 
@@ -812,15 +724,18 @@ var EnclaveSDK = class _EnclaveSDK {
812
724
  if (!isValidAddress(config.contracts.feeToken)) {
813
725
  throw new SDKError("Invalid FeeToken contract address", "INVALID_ADDRESS");
814
726
  }
815
- this.eventListener = new EventListener(config.publicClient);
816
- this.contractClient = new ContractClient(config.publicClient, config.walletClient, config.contracts);
817
- if (!Object.values(FheProtocol).includes(config.protocol)) {
818
- throw new SDKError(`Invalid protocol: ${config.protocol}`, "INVALID_PROTOCOL");
727
+ if (!config.thresholdBfvParamsPresetName) {
728
+ throw new SDKError("Threshold BFV parameters preset name is required", "MISSING_THRESHOLD_BFV_PARAMS_PRESET_NAME");
819
729
  }
820
- this.protocol = config.protocol;
821
- if (config.protocolParams) {
822
- this.protocolParams = config.protocolParams;
730
+ if (!Object.values(ThresholdBfvParamsPresetNames).includes(config.thresholdBfvParamsPresetName)) {
731
+ throw new SDKError(
732
+ `Invalid threshold BFV parameters preset name: ${config.thresholdBfvParamsPresetName}`,
733
+ "INVALID_THRESHOLD_BFV_PARAMS_PRESET_NAME"
734
+ );
823
735
  }
736
+ this.thresholdBfvParamsPresetName = config.thresholdBfvParamsPresetName;
737
+ this.eventListener = new EventListener(config.publicClient);
738
+ this.contractClient = new ContractClient(config.publicClient, config.walletClient, config.contracts);
824
739
  this.publicClient = config.publicClient;
825
740
  }
826
741
  static chains = {
@@ -832,8 +747,7 @@ var EnclaveSDK = class _EnclaveSDK {
832
747
  eventListener;
833
748
  contractClient;
834
749
  initialized = false;
835
- protocol;
836
- protocolParams;
750
+ thresholdBfvParamsPresetName;
837
751
  publicClient;
838
752
  /**
839
753
  * Initialize the SDK
@@ -855,9 +769,9 @@ var EnclaveSDK = class _EnclaveSDK {
855
769
  getPublicClient = () => {
856
770
  return this.publicClient;
857
771
  };
858
- async getBfvParamsSet(name) {
772
+ async getThresholdBfvParamsSet() {
859
773
  await (0, import_init.default)();
860
- let params = (0, import_wasm.get_bfv_params)(name);
774
+ let params = (0, import_wasm.get_bfv_params)(this.thresholdBfvParamsPresetName);
861
775
  return {
862
776
  degree: Number(params.degree),
863
777
  // degree is returned as a bigint from wasm
@@ -866,17 +780,20 @@ var EnclaveSDK = class _EnclaveSDK {
866
780
  error1Variance: params.error1_variance
867
781
  };
868
782
  }
869
- async getProtocolParams() {
783
+ async generatePublicKey() {
870
784
  await (0, import_init.default)();
871
- if (this.protocolParams) {
872
- return this.protocolParams;
873
- }
874
- switch (this.protocol) {
875
- case "BFV" /* BFV */:
876
- return await this.getBfvParamsSet("INSECURE_SET_2048_1032193_1");
877
- case "TRBFV" /* TRBFV */:
878
- return await this.getBfvParamsSet("INSECURE_SET_512_10_1");
879
- }
785
+ const protocolParams = await this.getThresholdBfvParamsSet();
786
+ return (0, import_wasm.generate_public_key)(protocolParams.degree, protocolParams.plaintextModulus, BigUint64Array.from(protocolParams.moduli));
787
+ }
788
+ async computePublicKeyCommitment(publicKey) {
789
+ await (0, import_init.default)();
790
+ const protocolParams = await this.getThresholdBfvParamsSet();
791
+ return (0, import_wasm.compute_pk_commitment)(
792
+ publicKey,
793
+ protocolParams.degree,
794
+ protocolParams.plaintextModulus,
795
+ BigUint64Array.from(protocolParams.moduli)
796
+ );
880
797
  }
881
798
  /**
882
799
  * Encrypt a number using the configured protocol
@@ -886,7 +803,7 @@ var EnclaveSDK = class _EnclaveSDK {
886
803
  */
887
804
  async encryptNumber(data, publicKey) {
888
805
  await (0, import_init.default)();
889
- const protocolParams = await this.getProtocolParams();
806
+ const protocolParams = await this.getThresholdBfvParamsSet();
890
807
  return (0, import_wasm.bfv_encrypt_number)(
891
808
  data,
892
809
  publicKey,
@@ -903,7 +820,7 @@ var EnclaveSDK = class _EnclaveSDK {
903
820
  */
904
821
  async encryptVector(data, publicKey) {
905
822
  await (0, import_init.default)();
906
- const protocolParams = await this.getProtocolParams();
823
+ const protocolParams = await this.getThresholdBfvParamsSet();
907
824
  return (0, import_wasm.bfv_encrypt_vector)(
908
825
  data,
909
826
  publicKey,
@@ -921,7 +838,7 @@ var EnclaveSDK = class _EnclaveSDK {
921
838
  */
922
839
  async encryptNumberAndGenInputs(data, publicKey) {
923
840
  await (0, import_init.default)();
924
- const protocolParams = await this.getProtocolParams();
841
+ const protocolParams = await this.getThresholdBfvParamsSet();
925
842
  const [encryptedData, circuitInputs] = (0, import_wasm.bfv_verifiable_encrypt_number)(
926
843
  data,
927
844
  publicKey,
@@ -929,10 +846,9 @@ var EnclaveSDK = class _EnclaveSDK {
929
846
  protocolParams.plaintextModulus,
930
847
  BigUint64Array.from(protocolParams.moduli)
931
848
  );
932
- const publicInputs = JSON.parse(circuitInputs);
933
849
  return {
934
850
  encryptedData,
935
- publicInputs
851
+ circuitInputs: JSON.parse(circuitInputs)
936
852
  };
937
853
  }
938
854
  /**
@@ -943,7 +859,7 @@ var EnclaveSDK = class _EnclaveSDK {
943
859
  * @returns The encrypted number and the proof
944
860
  */
945
861
  async encryptNumberAndGenProof(data, publicKey, circuit) {
946
- const { publicInputs, encryptedData } = await this.encryptNumberAndGenInputs(data, publicKey);
862
+ const { circuitInputs: publicInputs, encryptedData } = await this.encryptNumberAndGenInputs(data, publicKey);
947
863
  const proof = await generateProof(publicInputs, circuit);
948
864
  return {
949
865
  encryptedData,
@@ -958,7 +874,7 @@ var EnclaveSDK = class _EnclaveSDK {
958
874
  */
959
875
  async encryptVectorAndGenInputs(data, publicKey) {
960
876
  await (0, import_init.default)();
961
- const protocolParams = await this.getProtocolParams();
877
+ const protocolParams = await this.getThresholdBfvParamsSet();
962
878
  const [encryptedData, circuitInputs] = (0, import_wasm.bfv_verifiable_encrypt_vector)(
963
879
  data,
964
880
  publicKey,
@@ -966,10 +882,9 @@ var EnclaveSDK = class _EnclaveSDK {
966
882
  protocolParams.plaintextModulus,
967
883
  BigUint64Array.from(protocolParams.moduli)
968
884
  );
969
- const publicInputs = JSON.parse(circuitInputs);
970
885
  return {
971
886
  encryptedData,
972
- publicInputs
887
+ circuitInputs: JSON.parse(circuitInputs)
973
888
  };
974
889
  }
975
890
  /**
@@ -980,7 +895,7 @@ var EnclaveSDK = class _EnclaveSDK {
980
895
  * @returns The encrypted vector and the proof
981
896
  */
982
897
  async encryptVectorAndGenProof(data, publicKey, circuit) {
983
- const { publicInputs, encryptedData } = await this.encryptVectorAndGenInputs(data, publicKey);
898
+ const { circuitInputs: publicInputs, encryptedData } = await this.encryptVectorAndGenInputs(data, publicKey);
984
899
  const proof = await generateProof(publicInputs, circuit);
985
900
  return {
986
901
  encryptedData,
@@ -1032,11 +947,11 @@ var EnclaveSDK = class _EnclaveSDK {
1032
947
  /**
1033
948
  * Activate an E3 computation
1034
949
  */
1035
- async activateE3(e3Id, publicKey, gasLimit) {
950
+ async activateE3(e3Id, gasLimit) {
1036
951
  if (!this.initialized) {
1037
952
  await this.initialize();
1038
953
  }
1039
- return this.contractClient.activateE3(e3Id, publicKey, gasLimit);
954
+ return this.contractClient.activateE3(e3Id, gasLimit);
1040
955
  }
1041
956
  /**
1042
957
  * Publish input for an E3 computation
@@ -1184,27 +1099,22 @@ var EnclaveSDK = class _EnclaveSDK {
1184
1099
  walletClient,
1185
1100
  contracts: options.contracts,
1186
1101
  chainId: options.chainId,
1187
- protocol: options.protocol,
1188
- protocolParams: options.protocolParams
1102
+ thresholdBfvParamsPresetName: options.thresholdBfvParamsPresetName
1189
1103
  });
1190
1104
  }
1191
1105
  };
1192
1106
  // Annotate the CommonJS export names for ESM import in node:
1193
1107
  0 && (module.exports = {
1194
- BFV_PARAMS_SET,
1195
- BfvProtocolParams,
1196
1108
  ContractClient,
1197
1109
  DEFAULT_COMPUTE_PROVIDER_PARAMS,
1198
1110
  DEFAULT_E3_CONFIG,
1199
1111
  EnclaveEventType,
1200
1112
  EnclaveSDK,
1201
1113
  EventListener,
1202
- FheProtocol,
1203
1114
  RegistryEventType,
1204
1115
  SDKError,
1116
+ ThresholdBfvParamsPresetNames,
1205
1117
  calculateStartWindow,
1206
- convertToPolynomial,
1207
- convertToPolynomialArray,
1208
1118
  decodePlaintextOutput,
1209
1119
  encodeBfvParams,
1210
1120
  encodeComputeProviderParams,