@zama-fhe/relayer-sdk 0.1.0-6 → 0.1.0-7
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/bundle/fhevm.js +3691 -3677
- package/bundle/fhevm.umd.cjs +9 -9
- package/bundle/tfhe_bg.wasm +0 -0
- package/bundle/workerHelpers.js +1 -1
- package/bundle.d.ts +1 -0
- package/bundle.js +3 -12
- package/lib/node.cjs +58 -28
- package/lib/node.d.ts +2 -2
- package/lib/node.js +33 -22
- package/lib/tfhe_bg.wasm +0 -0
- package/lib/web.d.ts +2 -3
- package/lib/web.js +277 -264
- package/lib/workerHelpers.js +16 -15
- package/package.json +3 -3
package/bundle.d.ts
CHANGED
package/bundle.js
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
if (window && window.fhevm) {
|
|
5
|
-
return window.fhevm[functionName](...params);
|
|
6
|
-
}
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
const initFhevm = waitForFunction('initFhevm');
|
|
10
|
-
const createInstance = waitForFunction('createInstance');
|
|
11
|
-
|
|
12
|
-
export { initFhevm, createInstance };
|
|
1
|
+
export const initFhevm = window.fhevm.initFhevm;
|
|
2
|
+
export const createInstance = window.fhevm.createInstance;
|
|
3
|
+
export const SepoliaConfig = window.fhevm.SepoliaConfig;
|
package/lib/node.cjs
CHANGED
|
@@ -1,11 +1,31 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var TFHEPkg = require('node-tfhe');
|
|
4
|
+
var TKMSPkg = require('node-tkms');
|
|
3
5
|
var ethers = require('ethers');
|
|
4
|
-
var nodeTfhe = require('node-tfhe');
|
|
5
|
-
var nodeTkms = require('node-tkms');
|
|
6
6
|
var createHash = require('keccak');
|
|
7
7
|
var fetchRetry = require('fetch-retry');
|
|
8
8
|
|
|
9
|
+
function _interopNamespaceDefault(e) {
|
|
10
|
+
var n = Object.create(null);
|
|
11
|
+
if (e) {
|
|
12
|
+
Object.keys(e).forEach(function (k) {
|
|
13
|
+
if (k !== 'default') {
|
|
14
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
15
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return e[k]; }
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
n.default = e;
|
|
23
|
+
return Object.freeze(n);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var TFHEPkg__namespace = /*#__PURE__*/_interopNamespaceDefault(TFHEPkg);
|
|
27
|
+
var TKMSPkg__namespace = /*#__PURE__*/_interopNamespaceDefault(TKMSPkg);
|
|
28
|
+
|
|
9
29
|
const SERIALIZED_SIZE_LIMIT_CIPHERTEXT = BigInt(1024 * 1024 * 512);
|
|
10
30
|
const SERIALIZED_SIZE_LIMIT_PK = BigInt(1024 * 1024 * 512);
|
|
11
31
|
const SERIALIZED_SIZE_LIMIT_CRS = BigInt(1024 * 1024 * 512);
|
|
@@ -82,10 +102,17 @@ const getKeysFromRelayer = async (url, publicKeyId) => {
|
|
|
82
102
|
if (!publicParams2048Response.ok) {
|
|
83
103
|
throw new Error(`HTTP error! status: ${publicParams2048Response.status} on ${publicParams2048Response.url}`);
|
|
84
104
|
}
|
|
85
|
-
|
|
105
|
+
let publicParams2048;
|
|
106
|
+
if (typeof publicParams2048Response.bytes === 'function') {
|
|
107
|
+
// bytes is not widely supported yet
|
|
108
|
+
publicParams2048 = await publicParams2048Response.bytes();
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
publicParams2048 = new Uint8Array(await publicParams2048Response.arrayBuffer());
|
|
112
|
+
}
|
|
86
113
|
let pub_key;
|
|
87
114
|
try {
|
|
88
|
-
pub_key =
|
|
115
|
+
pub_key = TFHE.TfheCompactPublicKey.safe_deserialize(publicKey, SERIALIZED_SIZE_LIMIT_PK);
|
|
89
116
|
}
|
|
90
117
|
catch (e) {
|
|
91
118
|
throw new Error('Invalid public key (deserialization failed)', {
|
|
@@ -94,7 +121,7 @@ const getKeysFromRelayer = async (url, publicKeyId) => {
|
|
|
94
121
|
}
|
|
95
122
|
let crs;
|
|
96
123
|
try {
|
|
97
|
-
crs =
|
|
124
|
+
crs = TFHE.CompactPkeCrs.safe_deserialize(new Uint8Array(publicParams2048), SERIALIZED_SIZE_LIMIT_CRS);
|
|
98
125
|
}
|
|
99
126
|
catch (e) {
|
|
100
127
|
throw new Error('Invalid crs (deserialization failed)', {
|
|
@@ -163,7 +190,7 @@ const getTfheCompactPublicKey = async (config) => {
|
|
|
163
190
|
const buff = config.publicKey.data;
|
|
164
191
|
try {
|
|
165
192
|
return {
|
|
166
|
-
publicKey:
|
|
193
|
+
publicKey: TFHE.TfheCompactPublicKey.safe_deserialize(buff, SERIALIZED_SIZE_LIMIT_PK),
|
|
167
194
|
publicKeyId: config.publicKey.id,
|
|
168
195
|
};
|
|
169
196
|
}
|
|
@@ -187,7 +214,7 @@ const getPublicParams = async (config) => {
|
|
|
187
214
|
try {
|
|
188
215
|
return {
|
|
189
216
|
2048: {
|
|
190
|
-
publicParams:
|
|
217
|
+
publicParams: TFHE.CompactPkeCrs.safe_deserialize(buff, SERIALIZED_SIZE_LIMIT_CRS),
|
|
191
218
|
publicParamsId: config.publicParams['2048'].publicParamsId,
|
|
192
219
|
},
|
|
193
220
|
};
|
|
@@ -370,8 +397,8 @@ const userDecryptRequest = (kmsSigners, gatewayChainId, chainId, verifyingContra
|
|
|
370
397
|
let pubKey;
|
|
371
398
|
let privKey;
|
|
372
399
|
try {
|
|
373
|
-
pubKey =
|
|
374
|
-
privKey =
|
|
400
|
+
pubKey = TKMS.u8vec_to_ml_kem_pke_pk(fromHexString(publicKey));
|
|
401
|
+
privKey = TKMS.u8vec_to_ml_kem_pke_sk(fromHexString(privateKey));
|
|
375
402
|
}
|
|
376
403
|
catch (e) {
|
|
377
404
|
throw new Error('Invalid public or private key', { cause: e });
|
|
@@ -402,9 +429,9 @@ const userDecryptRequest = (kmsSigners, gatewayChainId, chainId, verifyingContra
|
|
|
402
429
|
}
|
|
403
430
|
// assume the KMS Signers have the correct order
|
|
404
431
|
let indexedKmsSigners = kmsSigners.map((signer, index) => {
|
|
405
|
-
return
|
|
432
|
+
return TKMS.new_server_id_addr(index + 1, signer);
|
|
406
433
|
});
|
|
407
|
-
const client =
|
|
434
|
+
const client = TKMS.new_client(indexedKmsSigners, userAddress, 'default');
|
|
408
435
|
try {
|
|
409
436
|
const buffer = new ArrayBuffer(32);
|
|
410
437
|
const view = new DataView(buffer);
|
|
@@ -424,7 +451,7 @@ const userDecryptRequest = (kmsSigners, gatewayChainId, chainId, verifyingContra
|
|
|
424
451
|
ciphertext_handles: handles.map((h) => h.handle.replace(/^0x/, '')),
|
|
425
452
|
eip712_verifying_contract: verifyingContractAddress,
|
|
426
453
|
};
|
|
427
|
-
const decryption =
|
|
454
|
+
const decryption = TKMS.process_user_decryption_resp_from_js(client, payloadForVerification, eip712Domain, json.response, pubKey, privKey, true);
|
|
428
455
|
const listBigIntDecryptions = decryption.map((d) => bytesToBigInt(d.bytes));
|
|
429
456
|
const results = buildUserDecryptedResult(handles.map((h) => h.handle), listBigIntDecryptions);
|
|
430
457
|
return results;
|
|
@@ -459,7 +486,7 @@ const createEncryptedInput = ({ aclContractAddress, chainId, tfheCompactPublicKe
|
|
|
459
486
|
}
|
|
460
487
|
const publicKey = tfheCompactPublicKey;
|
|
461
488
|
const bits = [];
|
|
462
|
-
const builder =
|
|
489
|
+
const builder = TFHE.CompactCiphertextList.builder(publicKey);
|
|
463
490
|
let ciphertextWithZKProof = new Uint8Array(); // updated in `_prove`
|
|
464
491
|
const checkLimit = (added) => {
|
|
465
492
|
if (bits.reduce((acc, val) => acc + Math.max(2, val), 0) + added > 2048) {
|
|
@@ -590,7 +617,7 @@ const createEncryptedInput = ({ aclContractAddress, chainId, tfheCompactPublicKe
|
|
|
590
617
|
auxData.set(buffUser, 20);
|
|
591
618
|
auxData.set(buffAcl, 40);
|
|
592
619
|
auxData.set(buffChainId, auxData.length - buffChainId.length);
|
|
593
|
-
const encrypted = builder.build_with_proof_packed(pp, auxData,
|
|
620
|
+
const encrypted = builder.build_with_proof_packed(pp, auxData, TFHE.ZkComputeLoad.Verify);
|
|
594
621
|
ciphertextWithZKProof = encrypted.safe_serialize(SERIALIZED_SIZE_LIMIT_CIPHERTEXT);
|
|
595
622
|
return ciphertextWithZKProof;
|
|
596
623
|
},
|
|
@@ -636,9 +663,9 @@ const computeHandles = (ciphertextWithZKProof, bitwidths, aclContractAddress, ch
|
|
|
636
663
|
if (BigInt(chainId) > MAX_UINT64) {
|
|
637
664
|
throw new Error('ChainId exceeds maximum allowed value (8 bytes)'); // fhevm assumes chainID is only taking up to 8 bytes
|
|
638
665
|
}
|
|
639
|
-
const chainId8Bytes =
|
|
666
|
+
const chainId8Bytes = fromHexString(hex).slice(24, 32);
|
|
640
667
|
dataInput[21] = encryptionIndex;
|
|
641
|
-
|
|
668
|
+
dataInput.set(chainId8Bytes, 22);
|
|
642
669
|
dataInput[30] = encryptionType;
|
|
643
670
|
dataInput[31] = ciphertextVersion;
|
|
644
671
|
return dataInput;
|
|
@@ -990,7 +1017,7 @@ const publicDecryptRequest = (kmsSigners, thresholdSigners, gatewayChainId, veri
|
|
|
990
1017
|
* @param durationDays - How many days the decryption permission remains valid
|
|
991
1018
|
* @returns EIP712 typed data structure for user decryption
|
|
992
1019
|
*/
|
|
993
|
-
const createEIP712 = (
|
|
1020
|
+
const createEIP712 = (verifyingContract, contractsChainId) => (publicKey, contractAddresses, startTimestamp, durationDays, delegatedAccount) => {
|
|
994
1021
|
if (delegatedAccount && !ethers.isAddress(delegatedAccount))
|
|
995
1022
|
throw new Error('Invalid delegated account.');
|
|
996
1023
|
if (!ethers.isAddress(verifyingContract)) {
|
|
@@ -1019,7 +1046,7 @@ const createEIP712 = (gatewayChainId, verifyingContract, contractsChainId) => (p
|
|
|
1019
1046
|
const domain = {
|
|
1020
1047
|
name: 'Decryption',
|
|
1021
1048
|
version: '1',
|
|
1022
|
-
chainId:
|
|
1049
|
+
chainId: contractsChainId,
|
|
1023
1050
|
verifyingContract,
|
|
1024
1051
|
};
|
|
1025
1052
|
if (delegatedAccount) {
|
|
@@ -1073,10 +1100,10 @@ const createEIP712 = (gatewayChainId, verifyingContract, contractsChainId) => (p
|
|
|
1073
1100
|
};
|
|
1074
1101
|
};
|
|
1075
1102
|
const generateKeypair = () => {
|
|
1076
|
-
const keypair =
|
|
1103
|
+
const keypair = TKMS.ml_kem_pke_keygen();
|
|
1077
1104
|
return {
|
|
1078
|
-
publicKey: toHexString(
|
|
1079
|
-
privateKey: toHexString(
|
|
1105
|
+
publicKey: toHexString(TKMS.ml_kem_pke_pk_to_u8vec(TKMS.ml_kem_pke_get_pk(keypair))),
|
|
1106
|
+
privateKey: toHexString(TKMS.ml_kem_pke_sk_to_u8vec(keypair)),
|
|
1080
1107
|
};
|
|
1081
1108
|
};
|
|
1082
1109
|
|
|
@@ -1133,7 +1160,7 @@ const createInstance = async (config) => {
|
|
|
1133
1160
|
return {
|
|
1134
1161
|
createEncryptedInput: createRelayerEncryptedInput(aclContractAddress, verifyingContractAddressInputVerification, chainId, gatewayChainId, cleanURL(config.relayerUrl), publicKeyData.publicKey, publicParamsData, coprocessorSigners, thresholdCoprocessorSigners),
|
|
1135
1162
|
generateKeypair,
|
|
1136
|
-
createEIP712: createEIP712(
|
|
1163
|
+
createEIP712: createEIP712(verifyingContractAddressDecryption, chainId),
|
|
1137
1164
|
publicDecrypt: publicDecryptRequest(kmsSigners, thresholdKMSSigners, gatewayChainId, verifyingContractAddressDecryption, aclContractAddress, cleanURL(config.relayerUrl), provider),
|
|
1138
1165
|
userDecrypt: userDecryptRequest(kmsSigners, gatewayChainId, chainId, verifyingContractAddressDecryption, aclContractAddress, cleanURL(config.relayerUrl), provider),
|
|
1139
1166
|
getPublicKey: () => publicKeyData.publicKey
|
|
@@ -1155,15 +1182,15 @@ const createInstance = async (config) => {
|
|
|
1155
1182
|
};
|
|
1156
1183
|
|
|
1157
1184
|
const createTfheKeypair = () => {
|
|
1158
|
-
const block_params = new
|
|
1159
|
-
const casting_params = new
|
|
1160
|
-
const config =
|
|
1185
|
+
const block_params = new TFHEPkg.ShortintParameters(TFHEPkg.ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128);
|
|
1186
|
+
const casting_params = new TFHEPkg.ShortintCompactPublicKeyEncryptionParameters(TFHEPkg.ShortintCompactPublicKeyEncryptionParametersName.V1_0_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128);
|
|
1187
|
+
const config = TFHEPkg.TfheConfigBuilder.default()
|
|
1161
1188
|
.use_custom_parameters(block_params)
|
|
1162
1189
|
.use_dedicated_compact_public_key_parameters(casting_params)
|
|
1163
1190
|
.build();
|
|
1164
|
-
let clientKey =
|
|
1165
|
-
let publicKey =
|
|
1166
|
-
const crs =
|
|
1191
|
+
let clientKey = TFHEPkg.TfheClientKey.generate(config);
|
|
1192
|
+
let publicKey = TFHEPkg.TfheCompactPublicKey.new(clientKey);
|
|
1193
|
+
const crs = TFHEPkg.CompactPkeCrs.from_config(config, 4 * 512);
|
|
1167
1194
|
return { clientKey, publicKey, crs };
|
|
1168
1195
|
};
|
|
1169
1196
|
const createTfhePublicKey = () => {
|
|
@@ -1171,6 +1198,9 @@ const createTfhePublicKey = () => {
|
|
|
1171
1198
|
return toHexString(publicKey.serialize());
|
|
1172
1199
|
};
|
|
1173
1200
|
|
|
1201
|
+
global.TFHE = TFHEPkg__namespace;
|
|
1202
|
+
global.TKMS = TKMSPkg__namespace;
|
|
1203
|
+
|
|
1174
1204
|
exports.ENCRYPTION_TYPES = ENCRYPTION_TYPES;
|
|
1175
1205
|
exports.SepoliaConfig = SepoliaConfig;
|
|
1176
1206
|
exports.createEIP712 = createEIP712;
|
package/lib/node.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ import { TfheCompactPublicKey } from 'node-tfhe';
|
|
|
15
15
|
* @param durationDays - How many days the decryption permission remains valid
|
|
16
16
|
* @returns EIP712 typed data structure for user decryption
|
|
17
17
|
*/
|
|
18
|
-
export declare const createEIP712: (
|
|
18
|
+
export declare const createEIP712: (verifyingContract: string, contractsChainId: number) => (publicKey: string | Uint8Array, contractAddresses: string[], startTimestamp: string | number, durationDays: string | number, delegatedAccount?: string) => EIP712;
|
|
19
19
|
|
|
20
20
|
export declare const createInstance: (config: FhevmInstanceConfig) => Promise<FhevmInstance>;
|
|
21
21
|
|
|
@@ -110,7 +110,7 @@ export declare type HandleContractPair = {
|
|
|
110
110
|
contractAddress: string;
|
|
111
111
|
};
|
|
112
112
|
|
|
113
|
-
export declare type PublicParams<T = CompactPkeCrs> = {
|
|
113
|
+
export declare type PublicParams<T = TFHE['CompactPkeCrs']> = {
|
|
114
114
|
[key in EncryptionTypes]?: {
|
|
115
115
|
publicParams: T;
|
|
116
116
|
publicParamsId: string;
|
package/lib/node.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import * as TFHEPkg from 'node-tfhe';
|
|
2
|
+
import { ShortintParameters, ShortintParametersName, ShortintCompactPublicKeyEncryptionParameters, ShortintCompactPublicKeyEncryptionParametersName, TfheConfigBuilder, TfheClientKey, TfheCompactPublicKey, CompactPkeCrs } from 'node-tfhe';
|
|
3
|
+
import * as TKMSPkg from 'node-tkms';
|
|
1
4
|
import { JsonRpcProvider, BrowserProvider, Contract, ethers, getAddress, isAddress, AbiCoder } from 'ethers';
|
|
2
|
-
import { TfheCompactPublicKey, CompactPkeCrs, CompactCiphertextList, ZkComputeLoad, ShortintParameters, ShortintParametersName, ShortintCompactPublicKeyEncryptionParameters, ShortintCompactPublicKeyEncryptionParametersName, TfheConfigBuilder, TfheClientKey } from 'node-tfhe';
|
|
3
|
-
import { u8vec_to_ml_kem_pke_pk, u8vec_to_ml_kem_pke_sk, new_server_id_addr, new_client, process_user_decryption_resp_from_js, ml_kem_pke_keygen, ml_kem_pke_pk_to_u8vec, ml_kem_pke_get_pk, ml_kem_pke_sk_to_u8vec } from 'node-tkms';
|
|
4
5
|
import createHash from 'keccak';
|
|
5
6
|
import fetchRetry from 'fetch-retry';
|
|
6
7
|
|
|
@@ -80,10 +81,17 @@ const getKeysFromRelayer = async (url, publicKeyId) => {
|
|
|
80
81
|
if (!publicParams2048Response.ok) {
|
|
81
82
|
throw new Error(`HTTP error! status: ${publicParams2048Response.status} on ${publicParams2048Response.url}`);
|
|
82
83
|
}
|
|
83
|
-
|
|
84
|
+
let publicParams2048;
|
|
85
|
+
if (typeof publicParams2048Response.bytes === 'function') {
|
|
86
|
+
// bytes is not widely supported yet
|
|
87
|
+
publicParams2048 = await publicParams2048Response.bytes();
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
publicParams2048 = new Uint8Array(await publicParams2048Response.arrayBuffer());
|
|
91
|
+
}
|
|
84
92
|
let pub_key;
|
|
85
93
|
try {
|
|
86
|
-
pub_key = TfheCompactPublicKey.safe_deserialize(publicKey, SERIALIZED_SIZE_LIMIT_PK);
|
|
94
|
+
pub_key = TFHE.TfheCompactPublicKey.safe_deserialize(publicKey, SERIALIZED_SIZE_LIMIT_PK);
|
|
87
95
|
}
|
|
88
96
|
catch (e) {
|
|
89
97
|
throw new Error('Invalid public key (deserialization failed)', {
|
|
@@ -92,7 +100,7 @@ const getKeysFromRelayer = async (url, publicKeyId) => {
|
|
|
92
100
|
}
|
|
93
101
|
let crs;
|
|
94
102
|
try {
|
|
95
|
-
crs = CompactPkeCrs.safe_deserialize(new Uint8Array(publicParams2048), SERIALIZED_SIZE_LIMIT_CRS);
|
|
103
|
+
crs = TFHE.CompactPkeCrs.safe_deserialize(new Uint8Array(publicParams2048), SERIALIZED_SIZE_LIMIT_CRS);
|
|
96
104
|
}
|
|
97
105
|
catch (e) {
|
|
98
106
|
throw new Error('Invalid crs (deserialization failed)', {
|
|
@@ -161,7 +169,7 @@ const getTfheCompactPublicKey = async (config) => {
|
|
|
161
169
|
const buff = config.publicKey.data;
|
|
162
170
|
try {
|
|
163
171
|
return {
|
|
164
|
-
publicKey: TfheCompactPublicKey.safe_deserialize(buff, SERIALIZED_SIZE_LIMIT_PK),
|
|
172
|
+
publicKey: TFHE.TfheCompactPublicKey.safe_deserialize(buff, SERIALIZED_SIZE_LIMIT_PK),
|
|
165
173
|
publicKeyId: config.publicKey.id,
|
|
166
174
|
};
|
|
167
175
|
}
|
|
@@ -185,7 +193,7 @@ const getPublicParams = async (config) => {
|
|
|
185
193
|
try {
|
|
186
194
|
return {
|
|
187
195
|
2048: {
|
|
188
|
-
publicParams: CompactPkeCrs.safe_deserialize(buff, SERIALIZED_SIZE_LIMIT_CRS),
|
|
196
|
+
publicParams: TFHE.CompactPkeCrs.safe_deserialize(buff, SERIALIZED_SIZE_LIMIT_CRS),
|
|
189
197
|
publicParamsId: config.publicParams['2048'].publicParamsId,
|
|
190
198
|
},
|
|
191
199
|
};
|
|
@@ -368,8 +376,8 @@ const userDecryptRequest = (kmsSigners, gatewayChainId, chainId, verifyingContra
|
|
|
368
376
|
let pubKey;
|
|
369
377
|
let privKey;
|
|
370
378
|
try {
|
|
371
|
-
pubKey = u8vec_to_ml_kem_pke_pk(fromHexString(publicKey));
|
|
372
|
-
privKey = u8vec_to_ml_kem_pke_sk(fromHexString(privateKey));
|
|
379
|
+
pubKey = TKMS.u8vec_to_ml_kem_pke_pk(fromHexString(publicKey));
|
|
380
|
+
privKey = TKMS.u8vec_to_ml_kem_pke_sk(fromHexString(privateKey));
|
|
373
381
|
}
|
|
374
382
|
catch (e) {
|
|
375
383
|
throw new Error('Invalid public or private key', { cause: e });
|
|
@@ -400,9 +408,9 @@ const userDecryptRequest = (kmsSigners, gatewayChainId, chainId, verifyingContra
|
|
|
400
408
|
}
|
|
401
409
|
// assume the KMS Signers have the correct order
|
|
402
410
|
let indexedKmsSigners = kmsSigners.map((signer, index) => {
|
|
403
|
-
return new_server_id_addr(index + 1, signer);
|
|
411
|
+
return TKMS.new_server_id_addr(index + 1, signer);
|
|
404
412
|
});
|
|
405
|
-
const client = new_client(indexedKmsSigners, userAddress, 'default');
|
|
413
|
+
const client = TKMS.new_client(indexedKmsSigners, userAddress, 'default');
|
|
406
414
|
try {
|
|
407
415
|
const buffer = new ArrayBuffer(32);
|
|
408
416
|
const view = new DataView(buffer);
|
|
@@ -422,7 +430,7 @@ const userDecryptRequest = (kmsSigners, gatewayChainId, chainId, verifyingContra
|
|
|
422
430
|
ciphertext_handles: handles.map((h) => h.handle.replace(/^0x/, '')),
|
|
423
431
|
eip712_verifying_contract: verifyingContractAddress,
|
|
424
432
|
};
|
|
425
|
-
const decryption = process_user_decryption_resp_from_js(client, payloadForVerification, eip712Domain, json.response, pubKey, privKey, true);
|
|
433
|
+
const decryption = TKMS.process_user_decryption_resp_from_js(client, payloadForVerification, eip712Domain, json.response, pubKey, privKey, true);
|
|
426
434
|
const listBigIntDecryptions = decryption.map((d) => bytesToBigInt(d.bytes));
|
|
427
435
|
const results = buildUserDecryptedResult(handles.map((h) => h.handle), listBigIntDecryptions);
|
|
428
436
|
return results;
|
|
@@ -457,7 +465,7 @@ const createEncryptedInput = ({ aclContractAddress, chainId, tfheCompactPublicKe
|
|
|
457
465
|
}
|
|
458
466
|
const publicKey = tfheCompactPublicKey;
|
|
459
467
|
const bits = [];
|
|
460
|
-
const builder = CompactCiphertextList.builder(publicKey);
|
|
468
|
+
const builder = TFHE.CompactCiphertextList.builder(publicKey);
|
|
461
469
|
let ciphertextWithZKProof = new Uint8Array(); // updated in `_prove`
|
|
462
470
|
const checkLimit = (added) => {
|
|
463
471
|
if (bits.reduce((acc, val) => acc + Math.max(2, val), 0) + added > 2048) {
|
|
@@ -588,7 +596,7 @@ const createEncryptedInput = ({ aclContractAddress, chainId, tfheCompactPublicKe
|
|
|
588
596
|
auxData.set(buffUser, 20);
|
|
589
597
|
auxData.set(buffAcl, 40);
|
|
590
598
|
auxData.set(buffChainId, auxData.length - buffChainId.length);
|
|
591
|
-
const encrypted = builder.build_with_proof_packed(pp, auxData, ZkComputeLoad.Verify);
|
|
599
|
+
const encrypted = builder.build_with_proof_packed(pp, auxData, TFHE.ZkComputeLoad.Verify);
|
|
592
600
|
ciphertextWithZKProof = encrypted.safe_serialize(SERIALIZED_SIZE_LIMIT_CIPHERTEXT);
|
|
593
601
|
return ciphertextWithZKProof;
|
|
594
602
|
},
|
|
@@ -634,9 +642,9 @@ const computeHandles = (ciphertextWithZKProof, bitwidths, aclContractAddress, ch
|
|
|
634
642
|
if (BigInt(chainId) > MAX_UINT64) {
|
|
635
643
|
throw new Error('ChainId exceeds maximum allowed value (8 bytes)'); // fhevm assumes chainID is only taking up to 8 bytes
|
|
636
644
|
}
|
|
637
|
-
const chainId8Bytes =
|
|
645
|
+
const chainId8Bytes = fromHexString(hex).slice(24, 32);
|
|
638
646
|
dataInput[21] = encryptionIndex;
|
|
639
|
-
|
|
647
|
+
dataInput.set(chainId8Bytes, 22);
|
|
640
648
|
dataInput[30] = encryptionType;
|
|
641
649
|
dataInput[31] = ciphertextVersion;
|
|
642
650
|
return dataInput;
|
|
@@ -988,7 +996,7 @@ const publicDecryptRequest = (kmsSigners, thresholdSigners, gatewayChainId, veri
|
|
|
988
996
|
* @param durationDays - How many days the decryption permission remains valid
|
|
989
997
|
* @returns EIP712 typed data structure for user decryption
|
|
990
998
|
*/
|
|
991
|
-
const createEIP712 = (
|
|
999
|
+
const createEIP712 = (verifyingContract, contractsChainId) => (publicKey, contractAddresses, startTimestamp, durationDays, delegatedAccount) => {
|
|
992
1000
|
if (delegatedAccount && !isAddress(delegatedAccount))
|
|
993
1001
|
throw new Error('Invalid delegated account.');
|
|
994
1002
|
if (!isAddress(verifyingContract)) {
|
|
@@ -1017,7 +1025,7 @@ const createEIP712 = (gatewayChainId, verifyingContract, contractsChainId) => (p
|
|
|
1017
1025
|
const domain = {
|
|
1018
1026
|
name: 'Decryption',
|
|
1019
1027
|
version: '1',
|
|
1020
|
-
chainId:
|
|
1028
|
+
chainId: contractsChainId,
|
|
1021
1029
|
verifyingContract,
|
|
1022
1030
|
};
|
|
1023
1031
|
if (delegatedAccount) {
|
|
@@ -1071,10 +1079,10 @@ const createEIP712 = (gatewayChainId, verifyingContract, contractsChainId) => (p
|
|
|
1071
1079
|
};
|
|
1072
1080
|
};
|
|
1073
1081
|
const generateKeypair = () => {
|
|
1074
|
-
const keypair = ml_kem_pke_keygen();
|
|
1082
|
+
const keypair = TKMS.ml_kem_pke_keygen();
|
|
1075
1083
|
return {
|
|
1076
|
-
publicKey: toHexString(ml_kem_pke_pk_to_u8vec(ml_kem_pke_get_pk(keypair))),
|
|
1077
|
-
privateKey: toHexString(ml_kem_pke_sk_to_u8vec(keypair)),
|
|
1084
|
+
publicKey: toHexString(TKMS.ml_kem_pke_pk_to_u8vec(TKMS.ml_kem_pke_get_pk(keypair))),
|
|
1085
|
+
privateKey: toHexString(TKMS.ml_kem_pke_sk_to_u8vec(keypair)),
|
|
1078
1086
|
};
|
|
1079
1087
|
};
|
|
1080
1088
|
|
|
@@ -1131,7 +1139,7 @@ const createInstance = async (config) => {
|
|
|
1131
1139
|
return {
|
|
1132
1140
|
createEncryptedInput: createRelayerEncryptedInput(aclContractAddress, verifyingContractAddressInputVerification, chainId, gatewayChainId, cleanURL(config.relayerUrl), publicKeyData.publicKey, publicParamsData, coprocessorSigners, thresholdCoprocessorSigners),
|
|
1133
1141
|
generateKeypair,
|
|
1134
|
-
createEIP712: createEIP712(
|
|
1142
|
+
createEIP712: createEIP712(verifyingContractAddressDecryption, chainId),
|
|
1135
1143
|
publicDecrypt: publicDecryptRequest(kmsSigners, thresholdKMSSigners, gatewayChainId, verifyingContractAddressDecryption, aclContractAddress, cleanURL(config.relayerUrl), provider),
|
|
1136
1144
|
userDecrypt: userDecryptRequest(kmsSigners, gatewayChainId, chainId, verifyingContractAddressDecryption, aclContractAddress, cleanURL(config.relayerUrl), provider),
|
|
1137
1145
|
getPublicKey: () => publicKeyData.publicKey
|
|
@@ -1169,4 +1177,7 @@ const createTfhePublicKey = () => {
|
|
|
1169
1177
|
return toHexString(publicKey.serialize());
|
|
1170
1178
|
};
|
|
1171
1179
|
|
|
1180
|
+
global.TFHE = TFHEPkg;
|
|
1181
|
+
global.TKMS = TKMSPkg;
|
|
1182
|
+
|
|
1172
1183
|
export { ENCRYPTION_TYPES, SepoliaConfig, createEIP712, createInstance, createTfheKeypair, createTfhePublicKey, generateKeypair };
|
package/lib/tfhe_bg.wasm
CHANGED
|
Binary file
|
package/lib/web.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { CompactPkeCrs } from 'node-tfhe';
|
|
2
1
|
import { Eip1193Provider } from 'ethers';
|
|
3
2
|
import { InitInput as KMSInput } from 'tkms';
|
|
4
3
|
import { InitInput as TFHEInput } from 'tfhe';
|
|
@@ -15,7 +14,7 @@ import { InitInput as TFHEInput } from 'tfhe';
|
|
|
15
14
|
* @param durationDays - How many days the decryption permission remains valid
|
|
16
15
|
* @returns EIP712 typed data structure for user decryption
|
|
17
16
|
*/
|
|
18
|
-
export declare const createEIP712: (
|
|
17
|
+
export declare const createEIP712: (verifyingContract: string, contractsChainId: number) => (publicKey: string | Uint8Array, contractAddresses: string[], startTimestamp: string | number, durationDays: string | number, delegatedAccount?: string) => EIP712;
|
|
19
18
|
|
|
20
19
|
export declare const createInstance: (config: FhevmInstanceConfig) => Promise<FhevmInstance>;
|
|
21
20
|
|
|
@@ -110,7 +109,7 @@ export declare const initFhevm: ({ tfheParams, kmsParams, thread, }?: {
|
|
|
110
109
|
|
|
111
110
|
export { KMSInput }
|
|
112
111
|
|
|
113
|
-
export declare type PublicParams<T = CompactPkeCrs> = {
|
|
112
|
+
export declare type PublicParams<T = TFHE['CompactPkeCrs']> = {
|
|
114
113
|
[key in EncryptionTypes]?: {
|
|
115
114
|
publicParams: T;
|
|
116
115
|
publicParamsId: string;
|