@zama-fhe/relayer-sdk 0.1.0-5 → 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 +5549 -5515
- package/bundle/fhevm.umd.cjs +12 -12
- 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 +85 -32
- package/lib/node.d.ts +4 -2
- package/lib/node.js +60 -27
- package/lib/tfhe_bg.wasm +0 -0
- package/lib/web.d.ts +4 -3
- package/lib/web.js +304 -269
- package/lib/workerHelpers.js +16 -15
- package/package.json +9 -6
package/lib/web.js
CHANGED
|
@@ -293,6 +293,22 @@ function passArray8ToWasm0$1(arg, malloc) {
|
|
|
293
293
|
WASM_VECTOR_LEN$1 = arg.length;
|
|
294
294
|
return ptr;
|
|
295
295
|
}
|
|
296
|
+
|
|
297
|
+
function init_panic_hook() {
|
|
298
|
+
wasm$1.init_panic_hook();
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* @param {TfheServerKey} server_key
|
|
303
|
+
*/
|
|
304
|
+
function set_server_key(server_key) {
|
|
305
|
+
_assertClass$1(server_key, TfheServerKey);
|
|
306
|
+
const ret = wasm$1.set_server_key(server_key.__wbg_ptr);
|
|
307
|
+
if (ret[1]) {
|
|
308
|
+
throw takeFromExternrefTable0$1(ret[0]);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
296
312
|
/**
|
|
297
313
|
* @param {ShortintCompactPublicKeyEncryptionParametersName} param
|
|
298
314
|
* @returns {string}
|
|
@@ -327,21 +343,6 @@ function shortint_params_name(param) {
|
|
|
327
343
|
}
|
|
328
344
|
}
|
|
329
345
|
|
|
330
|
-
function init_panic_hook() {
|
|
331
|
-
wasm$1.init_panic_hook();
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
/**
|
|
335
|
-
* @param {TfheServerKey} server_key
|
|
336
|
-
*/
|
|
337
|
-
function set_server_key(server_key) {
|
|
338
|
-
_assertClass$1(server_key, TfheServerKey);
|
|
339
|
-
const ret = wasm$1.set_server_key(server_key.__wbg_ptr);
|
|
340
|
-
if (ret[1]) {
|
|
341
|
-
throw takeFromExternrefTable0$1(ret[0]);
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
|
|
345
346
|
/**
|
|
346
347
|
* @param {number} num_threads
|
|
347
348
|
* @returns {Promise<any>}
|
|
@@ -24780,223 +24781,6 @@ var tfhe$1 = /*#__PURE__*/Object.freeze({
|
|
|
24780
24781
|
wbg_rayon_start_worker: wbg_rayon_start_worker
|
|
24781
24782
|
});
|
|
24782
24783
|
|
|
24783
|
-
const SERIALIZED_SIZE_LIMIT_CIPHERTEXT = BigInt(1024 * 1024 * 512);
|
|
24784
|
-
const SERIALIZED_SIZE_LIMIT_PK = BigInt(1024 * 1024 * 512);
|
|
24785
|
-
const SERIALIZED_SIZE_LIMIT_CRS = BigInt(1024 * 1024 * 512);
|
|
24786
|
-
const cleanURL = (url) => {
|
|
24787
|
-
if (!url)
|
|
24788
|
-
return '';
|
|
24789
|
-
return url.endsWith('/') ? url.slice(0, -1) : url;
|
|
24790
|
-
};
|
|
24791
|
-
const numberToHex = (num) => {
|
|
24792
|
-
let hex = num.toString(16);
|
|
24793
|
-
return hex.length % 2 ? '0' + hex : hex;
|
|
24794
|
-
};
|
|
24795
|
-
const fromHexString = (hexString) => {
|
|
24796
|
-
const arr = hexString.replace(/^(0x)/, '').match(/.{1,2}/g);
|
|
24797
|
-
if (!arr)
|
|
24798
|
-
return new Uint8Array();
|
|
24799
|
-
return Uint8Array.from(arr.map((byte) => parseInt(byte, 16)));
|
|
24800
|
-
};
|
|
24801
|
-
const toHexString = (bytes, with0x = false) => `${with0x ? '0x' : ''}${bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '')}`;
|
|
24802
|
-
const bytesToBigInt = function (byteArray) {
|
|
24803
|
-
if (!byteArray || byteArray?.length === 0) {
|
|
24804
|
-
return BigInt(0);
|
|
24805
|
-
}
|
|
24806
|
-
const hex = Array.from(byteArray)
|
|
24807
|
-
.map((b) => b.toString(16).padStart(2, '0')) // byte to hex
|
|
24808
|
-
.join('');
|
|
24809
|
-
return BigInt(`0x${hex}`);
|
|
24810
|
-
};
|
|
24811
|
-
|
|
24812
|
-
const keyurlCache = {};
|
|
24813
|
-
const getKeysFromRelayer = async (url, publicKeyId) => {
|
|
24814
|
-
if (keyurlCache[url]) {
|
|
24815
|
-
return keyurlCache[url];
|
|
24816
|
-
}
|
|
24817
|
-
try {
|
|
24818
|
-
const response = await fetch(`${url}/v1/keyurl`);
|
|
24819
|
-
if (!response.ok) {
|
|
24820
|
-
throw new Error(`HTTP error! status: ${response.status}`);
|
|
24821
|
-
}
|
|
24822
|
-
const data = await response.json();
|
|
24823
|
-
if (data) {
|
|
24824
|
-
let pubKeyUrl;
|
|
24825
|
-
// If no publicKeyId is provided, use the first one
|
|
24826
|
-
// Warning: if there are multiple keys available, the first one will most likely never be the
|
|
24827
|
-
// same between several calls (fetching the infos is non-deterministic)
|
|
24828
|
-
if (!publicKeyId) {
|
|
24829
|
-
pubKeyUrl = data.response.fhe_key_info[0].fhe_public_key.urls[0];
|
|
24830
|
-
publicKeyId = data.response.fhe_key_info[0].fhe_public_key.data_id;
|
|
24831
|
-
}
|
|
24832
|
-
else {
|
|
24833
|
-
// If a publicKeyId is provided, get the corresponding info
|
|
24834
|
-
const keyInfo = data.response.fhe_key_info.find((info) => info.fhe_public_key.data_id === publicKeyId);
|
|
24835
|
-
if (!keyInfo) {
|
|
24836
|
-
throw new Error(`Could not find FHE key info with data_id ${publicKeyId}`);
|
|
24837
|
-
}
|
|
24838
|
-
// TODO: Get a given party's public key url instead of the first one
|
|
24839
|
-
pubKeyUrl = keyInfo.fhe_public_key.urls[0];
|
|
24840
|
-
}
|
|
24841
|
-
const publicKeyResponse = await fetch(pubKeyUrl);
|
|
24842
|
-
if (!publicKeyResponse.ok) {
|
|
24843
|
-
throw new Error(`HTTP error! status: ${publicKeyResponse.status} on ${publicKeyResponse.url}`);
|
|
24844
|
-
}
|
|
24845
|
-
let publicKey;
|
|
24846
|
-
if (typeof publicKeyResponse.bytes === 'function') {
|
|
24847
|
-
// bytes is not widely supported yet
|
|
24848
|
-
publicKey = await publicKeyResponse.bytes();
|
|
24849
|
-
}
|
|
24850
|
-
else {
|
|
24851
|
-
publicKey = new Uint8Array(await publicKeyResponse.arrayBuffer());
|
|
24852
|
-
}
|
|
24853
|
-
const publicParamsUrl = data.response.crs['2048'].urls[0];
|
|
24854
|
-
const publicParamsId = data.response.crs['2048'].data_id;
|
|
24855
|
-
const publicParams2048Response = await fetch(publicParamsUrl);
|
|
24856
|
-
if (!publicParams2048Response.ok) {
|
|
24857
|
-
throw new Error(`HTTP error! status: ${publicParams2048Response.status} on ${publicParams2048Response.url}`);
|
|
24858
|
-
}
|
|
24859
|
-
const publicParams2048 = await publicParams2048Response.bytes();
|
|
24860
|
-
let pub_key;
|
|
24861
|
-
try {
|
|
24862
|
-
pub_key = TfheCompactPublicKey.safe_deserialize(publicKey, SERIALIZED_SIZE_LIMIT_PK);
|
|
24863
|
-
}
|
|
24864
|
-
catch (e) {
|
|
24865
|
-
throw new Error('Invalid public key (deserialization failed)', {
|
|
24866
|
-
cause: e,
|
|
24867
|
-
});
|
|
24868
|
-
}
|
|
24869
|
-
let crs;
|
|
24870
|
-
try {
|
|
24871
|
-
crs = CompactPkeCrs.safe_deserialize(new Uint8Array(publicParams2048), SERIALIZED_SIZE_LIMIT_CRS);
|
|
24872
|
-
}
|
|
24873
|
-
catch (e) {
|
|
24874
|
-
throw new Error('Invalid crs (deserialization failed)', {
|
|
24875
|
-
cause: e,
|
|
24876
|
-
});
|
|
24877
|
-
}
|
|
24878
|
-
const result = {
|
|
24879
|
-
publicKey: pub_key,
|
|
24880
|
-
publicKeyId,
|
|
24881
|
-
publicParams: {
|
|
24882
|
-
2048: {
|
|
24883
|
-
publicParams: crs,
|
|
24884
|
-
publicParamsId,
|
|
24885
|
-
},
|
|
24886
|
-
},
|
|
24887
|
-
};
|
|
24888
|
-
keyurlCache[url] = result;
|
|
24889
|
-
return result;
|
|
24890
|
-
}
|
|
24891
|
-
else {
|
|
24892
|
-
throw new Error('No public key available');
|
|
24893
|
-
}
|
|
24894
|
-
}
|
|
24895
|
-
catch (e) {
|
|
24896
|
-
throw new Error('Impossible to fetch public key: wrong relayer url.', {
|
|
24897
|
-
cause: e,
|
|
24898
|
-
});
|
|
24899
|
-
}
|
|
24900
|
-
};
|
|
24901
|
-
|
|
24902
|
-
const abiKmsVerifier = [
|
|
24903
|
-
'function getKmsSigners() view returns (address[])',
|
|
24904
|
-
'function getThreshold() view returns (uint256)',
|
|
24905
|
-
];
|
|
24906
|
-
const abiInputVerifier = [
|
|
24907
|
-
'function getCoprocessorSigners() view returns (address[])',
|
|
24908
|
-
'function getThreshold() view returns (uint256)',
|
|
24909
|
-
];
|
|
24910
|
-
const getProvider = (config) => {
|
|
24911
|
-
if (typeof config.network === 'string') {
|
|
24912
|
-
return new JsonRpcProvider(config.network);
|
|
24913
|
-
}
|
|
24914
|
-
else if (config.network) {
|
|
24915
|
-
return new BrowserProvider(config.network);
|
|
24916
|
-
}
|
|
24917
|
-
throw new Error('You must provide a network URL or a EIP1193 object (eg: window.ethereum)');
|
|
24918
|
-
};
|
|
24919
|
-
const getChainId = async (provider, config) => {
|
|
24920
|
-
if (config.chainId && typeof config.chainId === 'number') {
|
|
24921
|
-
return config.chainId;
|
|
24922
|
-
}
|
|
24923
|
-
else if (config.chainId && typeof config.chainId !== 'number') {
|
|
24924
|
-
throw new Error('chainId must be a number.');
|
|
24925
|
-
}
|
|
24926
|
-
else {
|
|
24927
|
-
const chainId = (await provider.getNetwork()).chainId;
|
|
24928
|
-
return Number(chainId);
|
|
24929
|
-
}
|
|
24930
|
-
};
|
|
24931
|
-
const getTfheCompactPublicKey = async (config) => {
|
|
24932
|
-
if (config.relayerUrl && !config.publicKey) {
|
|
24933
|
-
const inputs = await getKeysFromRelayer(cleanURL(config.relayerUrl));
|
|
24934
|
-
return { publicKey: inputs.publicKey, publicKeyId: inputs.publicKeyId };
|
|
24935
|
-
}
|
|
24936
|
-
else if (config.publicKey && config.publicKey.data && config.publicKey.id) {
|
|
24937
|
-
const buff = config.publicKey.data;
|
|
24938
|
-
try {
|
|
24939
|
-
return {
|
|
24940
|
-
publicKey: TfheCompactPublicKey.safe_deserialize(buff, SERIALIZED_SIZE_LIMIT_PK),
|
|
24941
|
-
publicKeyId: config.publicKey.id,
|
|
24942
|
-
};
|
|
24943
|
-
}
|
|
24944
|
-
catch (e) {
|
|
24945
|
-
throw new Error('Invalid public key (deserialization failed)', {
|
|
24946
|
-
cause: e,
|
|
24947
|
-
});
|
|
24948
|
-
}
|
|
24949
|
-
}
|
|
24950
|
-
else {
|
|
24951
|
-
throw new Error('You must provide a public key with its public key ID.');
|
|
24952
|
-
}
|
|
24953
|
-
};
|
|
24954
|
-
const getPublicParams = async (config) => {
|
|
24955
|
-
if (config.relayerUrl && !config.publicParams) {
|
|
24956
|
-
const inputs = await getKeysFromRelayer(cleanURL(config.relayerUrl));
|
|
24957
|
-
return inputs.publicParams;
|
|
24958
|
-
}
|
|
24959
|
-
else if (config.publicParams && config.publicParams['2048']) {
|
|
24960
|
-
const buff = config.publicParams['2048'].publicParams;
|
|
24961
|
-
try {
|
|
24962
|
-
return {
|
|
24963
|
-
2048: {
|
|
24964
|
-
publicParams: CompactPkeCrs.safe_deserialize(buff, SERIALIZED_SIZE_LIMIT_CRS),
|
|
24965
|
-
publicParamsId: config.publicParams['2048'].publicParamsId,
|
|
24966
|
-
},
|
|
24967
|
-
};
|
|
24968
|
-
}
|
|
24969
|
-
catch (e) {
|
|
24970
|
-
throw new Error('Invalid public key (deserialization failed)', {
|
|
24971
|
-
cause: e,
|
|
24972
|
-
});
|
|
24973
|
-
}
|
|
24974
|
-
}
|
|
24975
|
-
else {
|
|
24976
|
-
throw new Error('You must provide a valid CRS with its CRS ID.');
|
|
24977
|
-
}
|
|
24978
|
-
};
|
|
24979
|
-
const getKMSSigners = async (provider, config) => {
|
|
24980
|
-
const kmsContract = new Contract(config.kmsContractAddress, abiKmsVerifier, provider);
|
|
24981
|
-
const signers = await kmsContract.getKmsSigners();
|
|
24982
|
-
return signers;
|
|
24983
|
-
};
|
|
24984
|
-
const getKMSSignersThreshold = async (provider, config) => {
|
|
24985
|
-
const kmsContract = new Contract(config.kmsContractAddress, abiKmsVerifier, provider);
|
|
24986
|
-
const threshold = await kmsContract.getThreshold();
|
|
24987
|
-
return Number(threshold); // threshold is always supposed to fit in a number
|
|
24988
|
-
};
|
|
24989
|
-
const getCoprocessorSigners = async (provider, config) => {
|
|
24990
|
-
const inputContract = new Contract(config.inputVerifierContractAddress, abiInputVerifier, provider);
|
|
24991
|
-
const signers = await inputContract.getCoprocessorSigners();
|
|
24992
|
-
return signers;
|
|
24993
|
-
};
|
|
24994
|
-
const getCoprocessorSignersThreshold = async (provider, config) => {
|
|
24995
|
-
const inputContract = new Contract(config.inputVerifierContractAddress, abiInputVerifier, provider);
|
|
24996
|
-
const threshold = await inputContract.getThreshold();
|
|
24997
|
-
return Number(threshold); // threshold is always supposed to fit in a number
|
|
24998
|
-
};
|
|
24999
|
-
|
|
25000
24784
|
let wasm;
|
|
25001
24785
|
|
|
25002
24786
|
let WASM_VECTOR_LEN = 0;
|
|
@@ -25189,23 +24973,6 @@ function takeFromExternrefTable0(idx) {
|
|
|
25189
24973
|
return value;
|
|
25190
24974
|
}
|
|
25191
24975
|
|
|
25192
|
-
/**
|
|
25193
|
-
* Create a new [ServerIdAddr] structure that holds an ID and an address
|
|
25194
|
-
* which must be a valid EIP-55 address, notably prefixed with "0x".
|
|
25195
|
-
* @param {number} id
|
|
25196
|
-
* @param {string} addr
|
|
25197
|
-
* @returns {ServerIdAddr}
|
|
25198
|
-
*/
|
|
25199
|
-
function new_server_id_addr(id, addr) {
|
|
25200
|
-
const ptr0 = passStringToWasm0(addr, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
25201
|
-
const len0 = WASM_VECTOR_LEN;
|
|
25202
|
-
const ret = wasm.new_server_id_addr(id, ptr0, len0);
|
|
25203
|
-
if (ret[2]) {
|
|
25204
|
-
throw takeFromExternrefTable0(ret[1]);
|
|
25205
|
-
}
|
|
25206
|
-
return ServerIdAddr.__wrap(ret[0]);
|
|
25207
|
-
}
|
|
25208
|
-
|
|
25209
24976
|
function passArrayJsValueToWasm0(array, malloc) {
|
|
25210
24977
|
const ptr = malloc(array.length * 4, 4) >>> 0;
|
|
25211
24978
|
for (let i = 0; i < array.length; i++) {
|
|
@@ -26433,6 +26200,230 @@ async function __wbg_init(module_or_path) {
|
|
|
26433
26200
|
return __wbg_finalize_init(instance, module);
|
|
26434
26201
|
}
|
|
26435
26202
|
|
|
26203
|
+
const SERIALIZED_SIZE_LIMIT_CIPHERTEXT = BigInt(1024 * 1024 * 512);
|
|
26204
|
+
const SERIALIZED_SIZE_LIMIT_PK = BigInt(1024 * 1024 * 512);
|
|
26205
|
+
const SERIALIZED_SIZE_LIMIT_CRS = BigInt(1024 * 1024 * 512);
|
|
26206
|
+
const cleanURL = (url) => {
|
|
26207
|
+
if (!url)
|
|
26208
|
+
return '';
|
|
26209
|
+
return url.endsWith('/') ? url.slice(0, -1) : url;
|
|
26210
|
+
};
|
|
26211
|
+
const numberToHex = (num) => {
|
|
26212
|
+
let hex = num.toString(16);
|
|
26213
|
+
return hex.length % 2 ? '0' + hex : hex;
|
|
26214
|
+
};
|
|
26215
|
+
const fromHexString = (hexString) => {
|
|
26216
|
+
const arr = hexString.replace(/^(0x)/, '').match(/.{1,2}/g);
|
|
26217
|
+
if (!arr)
|
|
26218
|
+
return new Uint8Array();
|
|
26219
|
+
return Uint8Array.from(arr.map((byte) => parseInt(byte, 16)));
|
|
26220
|
+
};
|
|
26221
|
+
const toHexString = (bytes, with0x = false) => `${with0x ? '0x' : ''}${bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '')}`;
|
|
26222
|
+
const bytesToBigInt = function (byteArray) {
|
|
26223
|
+
if (!byteArray || byteArray?.length === 0) {
|
|
26224
|
+
return BigInt(0);
|
|
26225
|
+
}
|
|
26226
|
+
const hex = Array.from(byteArray)
|
|
26227
|
+
.map((b) => b.toString(16).padStart(2, '0')) // byte to hex
|
|
26228
|
+
.join('');
|
|
26229
|
+
return BigInt(`0x${hex}`);
|
|
26230
|
+
};
|
|
26231
|
+
|
|
26232
|
+
const keyurlCache = {};
|
|
26233
|
+
const getKeysFromRelayer = async (url, publicKeyId) => {
|
|
26234
|
+
if (keyurlCache[url]) {
|
|
26235
|
+
return keyurlCache[url];
|
|
26236
|
+
}
|
|
26237
|
+
try {
|
|
26238
|
+
const response = await fetch(`${url}/v1/keyurl`);
|
|
26239
|
+
if (!response.ok) {
|
|
26240
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
26241
|
+
}
|
|
26242
|
+
const data = await response.json();
|
|
26243
|
+
if (data) {
|
|
26244
|
+
let pubKeyUrl;
|
|
26245
|
+
// If no publicKeyId is provided, use the first one
|
|
26246
|
+
// Warning: if there are multiple keys available, the first one will most likely never be the
|
|
26247
|
+
// same between several calls (fetching the infos is non-deterministic)
|
|
26248
|
+
if (!publicKeyId) {
|
|
26249
|
+
pubKeyUrl = data.response.fhe_key_info[0].fhe_public_key.urls[0];
|
|
26250
|
+
publicKeyId = data.response.fhe_key_info[0].fhe_public_key.data_id;
|
|
26251
|
+
}
|
|
26252
|
+
else {
|
|
26253
|
+
// If a publicKeyId is provided, get the corresponding info
|
|
26254
|
+
const keyInfo = data.response.fhe_key_info.find((info) => info.fhe_public_key.data_id === publicKeyId);
|
|
26255
|
+
if (!keyInfo) {
|
|
26256
|
+
throw new Error(`Could not find FHE key info with data_id ${publicKeyId}`);
|
|
26257
|
+
}
|
|
26258
|
+
// TODO: Get a given party's public key url instead of the first one
|
|
26259
|
+
pubKeyUrl = keyInfo.fhe_public_key.urls[0];
|
|
26260
|
+
}
|
|
26261
|
+
const publicKeyResponse = await fetch(pubKeyUrl);
|
|
26262
|
+
if (!publicKeyResponse.ok) {
|
|
26263
|
+
throw new Error(`HTTP error! status: ${publicKeyResponse.status} on ${publicKeyResponse.url}`);
|
|
26264
|
+
}
|
|
26265
|
+
let publicKey;
|
|
26266
|
+
if (typeof publicKeyResponse.bytes === 'function') {
|
|
26267
|
+
// bytes is not widely supported yet
|
|
26268
|
+
publicKey = await publicKeyResponse.bytes();
|
|
26269
|
+
}
|
|
26270
|
+
else {
|
|
26271
|
+
publicKey = new Uint8Array(await publicKeyResponse.arrayBuffer());
|
|
26272
|
+
}
|
|
26273
|
+
const publicParamsUrl = data.response.crs['2048'].urls[0];
|
|
26274
|
+
const publicParamsId = data.response.crs['2048'].data_id;
|
|
26275
|
+
const publicParams2048Response = await fetch(publicParamsUrl);
|
|
26276
|
+
if (!publicParams2048Response.ok) {
|
|
26277
|
+
throw new Error(`HTTP error! status: ${publicParams2048Response.status} on ${publicParams2048Response.url}`);
|
|
26278
|
+
}
|
|
26279
|
+
let publicParams2048;
|
|
26280
|
+
if (typeof publicParams2048Response.bytes === 'function') {
|
|
26281
|
+
// bytes is not widely supported yet
|
|
26282
|
+
publicParams2048 = await publicParams2048Response.bytes();
|
|
26283
|
+
}
|
|
26284
|
+
else {
|
|
26285
|
+
publicParams2048 = new Uint8Array(await publicParams2048Response.arrayBuffer());
|
|
26286
|
+
}
|
|
26287
|
+
let pub_key;
|
|
26288
|
+
try {
|
|
26289
|
+
pub_key = TFHE.TfheCompactPublicKey.safe_deserialize(publicKey, SERIALIZED_SIZE_LIMIT_PK);
|
|
26290
|
+
}
|
|
26291
|
+
catch (e) {
|
|
26292
|
+
throw new Error('Invalid public key (deserialization failed)', {
|
|
26293
|
+
cause: e,
|
|
26294
|
+
});
|
|
26295
|
+
}
|
|
26296
|
+
let crs;
|
|
26297
|
+
try {
|
|
26298
|
+
crs = TFHE.CompactPkeCrs.safe_deserialize(new Uint8Array(publicParams2048), SERIALIZED_SIZE_LIMIT_CRS);
|
|
26299
|
+
}
|
|
26300
|
+
catch (e) {
|
|
26301
|
+
throw new Error('Invalid crs (deserialization failed)', {
|
|
26302
|
+
cause: e,
|
|
26303
|
+
});
|
|
26304
|
+
}
|
|
26305
|
+
const result = {
|
|
26306
|
+
publicKey: pub_key,
|
|
26307
|
+
publicKeyId,
|
|
26308
|
+
publicParams: {
|
|
26309
|
+
2048: {
|
|
26310
|
+
publicParams: crs,
|
|
26311
|
+
publicParamsId,
|
|
26312
|
+
},
|
|
26313
|
+
},
|
|
26314
|
+
};
|
|
26315
|
+
keyurlCache[url] = result;
|
|
26316
|
+
return result;
|
|
26317
|
+
}
|
|
26318
|
+
else {
|
|
26319
|
+
throw new Error('No public key available');
|
|
26320
|
+
}
|
|
26321
|
+
}
|
|
26322
|
+
catch (e) {
|
|
26323
|
+
throw new Error('Impossible to fetch public key: wrong relayer url.', {
|
|
26324
|
+
cause: e,
|
|
26325
|
+
});
|
|
26326
|
+
}
|
|
26327
|
+
};
|
|
26328
|
+
|
|
26329
|
+
const abiKmsVerifier = [
|
|
26330
|
+
'function getKmsSigners() view returns (address[])',
|
|
26331
|
+
'function getThreshold() view returns (uint256)',
|
|
26332
|
+
];
|
|
26333
|
+
const abiInputVerifier = [
|
|
26334
|
+
'function getCoprocessorSigners() view returns (address[])',
|
|
26335
|
+
'function getThreshold() view returns (uint256)',
|
|
26336
|
+
];
|
|
26337
|
+
const getProvider = (config) => {
|
|
26338
|
+
if (typeof config.network === 'string') {
|
|
26339
|
+
return new JsonRpcProvider(config.network);
|
|
26340
|
+
}
|
|
26341
|
+
else if (config.network) {
|
|
26342
|
+
return new BrowserProvider(config.network);
|
|
26343
|
+
}
|
|
26344
|
+
throw new Error('You must provide a network URL or a EIP1193 object (eg: window.ethereum)');
|
|
26345
|
+
};
|
|
26346
|
+
const getChainId = async (provider, config) => {
|
|
26347
|
+
if (config.chainId && typeof config.chainId === 'number') {
|
|
26348
|
+
return config.chainId;
|
|
26349
|
+
}
|
|
26350
|
+
else if (config.chainId && typeof config.chainId !== 'number') {
|
|
26351
|
+
throw new Error('chainId must be a number.');
|
|
26352
|
+
}
|
|
26353
|
+
else {
|
|
26354
|
+
const chainId = (await provider.getNetwork()).chainId;
|
|
26355
|
+
return Number(chainId);
|
|
26356
|
+
}
|
|
26357
|
+
};
|
|
26358
|
+
const getTfheCompactPublicKey = async (config) => {
|
|
26359
|
+
if (config.relayerUrl && !config.publicKey) {
|
|
26360
|
+
const inputs = await getKeysFromRelayer(cleanURL(config.relayerUrl));
|
|
26361
|
+
return { publicKey: inputs.publicKey, publicKeyId: inputs.publicKeyId };
|
|
26362
|
+
}
|
|
26363
|
+
else if (config.publicKey && config.publicKey.data && config.publicKey.id) {
|
|
26364
|
+
const buff = config.publicKey.data;
|
|
26365
|
+
try {
|
|
26366
|
+
return {
|
|
26367
|
+
publicKey: TFHE.TfheCompactPublicKey.safe_deserialize(buff, SERIALIZED_SIZE_LIMIT_PK),
|
|
26368
|
+
publicKeyId: config.publicKey.id,
|
|
26369
|
+
};
|
|
26370
|
+
}
|
|
26371
|
+
catch (e) {
|
|
26372
|
+
throw new Error('Invalid public key (deserialization failed)', {
|
|
26373
|
+
cause: e,
|
|
26374
|
+
});
|
|
26375
|
+
}
|
|
26376
|
+
}
|
|
26377
|
+
else {
|
|
26378
|
+
throw new Error('You must provide a public key with its public key ID.');
|
|
26379
|
+
}
|
|
26380
|
+
};
|
|
26381
|
+
const getPublicParams = async (config) => {
|
|
26382
|
+
if (config.relayerUrl && !config.publicParams) {
|
|
26383
|
+
const inputs = await getKeysFromRelayer(cleanURL(config.relayerUrl));
|
|
26384
|
+
return inputs.publicParams;
|
|
26385
|
+
}
|
|
26386
|
+
else if (config.publicParams && config.publicParams['2048']) {
|
|
26387
|
+
const buff = config.publicParams['2048'].publicParams;
|
|
26388
|
+
try {
|
|
26389
|
+
return {
|
|
26390
|
+
2048: {
|
|
26391
|
+
publicParams: TFHE.CompactPkeCrs.safe_deserialize(buff, SERIALIZED_SIZE_LIMIT_CRS),
|
|
26392
|
+
publicParamsId: config.publicParams['2048'].publicParamsId,
|
|
26393
|
+
},
|
|
26394
|
+
};
|
|
26395
|
+
}
|
|
26396
|
+
catch (e) {
|
|
26397
|
+
throw new Error('Invalid public key (deserialization failed)', {
|
|
26398
|
+
cause: e,
|
|
26399
|
+
});
|
|
26400
|
+
}
|
|
26401
|
+
}
|
|
26402
|
+
else {
|
|
26403
|
+
throw new Error('You must provide a valid CRS with its CRS ID.');
|
|
26404
|
+
}
|
|
26405
|
+
};
|
|
26406
|
+
const getKMSSigners = async (provider, config) => {
|
|
26407
|
+
const kmsContract = new Contract(config.kmsContractAddress, abiKmsVerifier, provider);
|
|
26408
|
+
const signers = await kmsContract.getKmsSigners();
|
|
26409
|
+
return signers;
|
|
26410
|
+
};
|
|
26411
|
+
const getKMSSignersThreshold = async (provider, config) => {
|
|
26412
|
+
const kmsContract = new Contract(config.kmsContractAddress, abiKmsVerifier, provider);
|
|
26413
|
+
const threshold = await kmsContract.getThreshold();
|
|
26414
|
+
return Number(threshold); // threshold is always supposed to fit in a number
|
|
26415
|
+
};
|
|
26416
|
+
const getCoprocessorSigners = async (provider, config) => {
|
|
26417
|
+
const inputContract = new Contract(config.inputVerifierContractAddress, abiInputVerifier, provider);
|
|
26418
|
+
const signers = await inputContract.getCoprocessorSigners();
|
|
26419
|
+
return signers;
|
|
26420
|
+
};
|
|
26421
|
+
const getCoprocessorSignersThreshold = async (provider, config) => {
|
|
26422
|
+
const inputContract = new Contract(config.inputVerifierContractAddress, abiInputVerifier, provider);
|
|
26423
|
+
const threshold = await inputContract.getThreshold();
|
|
26424
|
+
return Number(threshold); // threshold is always supposed to fit in a number
|
|
26425
|
+
};
|
|
26426
|
+
|
|
26436
26427
|
const NumEncryptedBits = {
|
|
26437
26428
|
0: 2, // ebool
|
|
26438
26429
|
2: 8, // euint8
|
|
@@ -26524,6 +26515,8 @@ function checkDeadlineValidity(startTimestamp, durationDays) {
|
|
|
26524
26515
|
}
|
|
26525
26516
|
const userDecryptRequest = (kmsSigners, gatewayChainId, chainId, verifyingContractAddress, aclContractAddress, relayerUrl, provider) => async (_handles, privateKey, publicKey, signature, contractAddresses, userAddress, startTimestamp, durationDays) => {
|
|
26526
26517
|
// Casting handles if string
|
|
26518
|
+
const signatureSanitized = signature.replace(/^(0x)/, '');
|
|
26519
|
+
const publicKeySanitized = publicKey.replace(/^(0x)/, '');
|
|
26527
26520
|
const handles = _handles.map((h) => ({
|
|
26528
26521
|
handle: typeof h.handle === 'string'
|
|
26529
26522
|
? toHexString(fromHexString(h.handle), true)
|
|
@@ -26565,8 +26558,8 @@ const userDecryptRequest = (kmsSigners, gatewayChainId, chainId, verifyingContra
|
|
|
26565
26558
|
contractsChainId: chainId.toString(), // Convert to string
|
|
26566
26559
|
contractAddresses: contractAddresses.map((c) => getAddress(c)),
|
|
26567
26560
|
userAddress: getAddress(userAddress),
|
|
26568
|
-
signature:
|
|
26569
|
-
publicKey:
|
|
26561
|
+
signature: signatureSanitized,
|
|
26562
|
+
publicKey: publicKeySanitized,
|
|
26570
26563
|
};
|
|
26571
26564
|
const options = {
|
|
26572
26565
|
method: 'POST',
|
|
@@ -26578,8 +26571,8 @@ const userDecryptRequest = (kmsSigners, gatewayChainId, chainId, verifyingContra
|
|
|
26578
26571
|
let pubKey;
|
|
26579
26572
|
let privKey;
|
|
26580
26573
|
try {
|
|
26581
|
-
pubKey = u8vec_to_ml_kem_pke_pk(fromHexString(publicKey));
|
|
26582
|
-
privKey = u8vec_to_ml_kem_pke_sk(fromHexString(privateKey));
|
|
26574
|
+
pubKey = TKMS.u8vec_to_ml_kem_pke_pk(fromHexString(publicKey));
|
|
26575
|
+
privKey = TKMS.u8vec_to_ml_kem_pke_sk(fromHexString(privateKey));
|
|
26583
26576
|
}
|
|
26584
26577
|
catch (e) {
|
|
26585
26578
|
throw new Error('Invalid public or private key', { cause: e });
|
|
@@ -26610,9 +26603,9 @@ const userDecryptRequest = (kmsSigners, gatewayChainId, chainId, verifyingContra
|
|
|
26610
26603
|
}
|
|
26611
26604
|
// assume the KMS Signers have the correct order
|
|
26612
26605
|
let indexedKmsSigners = kmsSigners.map((signer, index) => {
|
|
26613
|
-
return new_server_id_addr(index + 1, signer);
|
|
26606
|
+
return TKMS.new_server_id_addr(index + 1, signer);
|
|
26614
26607
|
});
|
|
26615
|
-
const client = new_client(indexedKmsSigners, userAddress, 'default');
|
|
26608
|
+
const client = TKMS.new_client(indexedKmsSigners, userAddress, 'default');
|
|
26616
26609
|
try {
|
|
26617
26610
|
const buffer = new ArrayBuffer(32);
|
|
26618
26611
|
const view = new DataView(buffer);
|
|
@@ -26626,13 +26619,13 @@ const userDecryptRequest = (kmsSigners, gatewayChainId, chainId, verifyingContra
|
|
|
26626
26619
|
salt: null,
|
|
26627
26620
|
};
|
|
26628
26621
|
const payloadForVerification = {
|
|
26629
|
-
signature,
|
|
26622
|
+
signature: signatureSanitized,
|
|
26630
26623
|
client_address: userAddress,
|
|
26631
|
-
enc_key:
|
|
26624
|
+
enc_key: publicKeySanitized,
|
|
26632
26625
|
ciphertext_handles: handles.map((h) => h.handle.replace(/^0x/, '')),
|
|
26633
26626
|
eip712_verifying_contract: verifyingContractAddress,
|
|
26634
26627
|
};
|
|
26635
|
-
const decryption = process_user_decryption_resp_from_js(client, payloadForVerification, eip712Domain, json.response, pubKey, privKey, true);
|
|
26628
|
+
const decryption = TKMS.process_user_decryption_resp_from_js(client, payloadForVerification, eip712Domain, json.response, pubKey, privKey, true);
|
|
26636
26629
|
const listBigIntDecryptions = decryption.map((d) => bytesToBigInt(d.bytes));
|
|
26637
26630
|
const results = buildUserDecryptedResult(handles.map((h) => h.handle), listBigIntDecryptions);
|
|
26638
26631
|
return results;
|
|
@@ -26667,7 +26660,7 @@ const createEncryptedInput = ({ aclContractAddress, chainId, tfheCompactPublicKe
|
|
|
26667
26660
|
}
|
|
26668
26661
|
const publicKey = tfheCompactPublicKey;
|
|
26669
26662
|
const bits = [];
|
|
26670
|
-
const builder = CompactCiphertextList.builder(publicKey);
|
|
26663
|
+
const builder = TFHE.CompactCiphertextList.builder(publicKey);
|
|
26671
26664
|
let ciphertextWithZKProof = new Uint8Array(); // updated in `_prove`
|
|
26672
26665
|
const checkLimit = (added) => {
|
|
26673
26666
|
if (bits.reduce((acc, val) => acc + Math.max(2, val), 0) + added > 2048) {
|
|
@@ -26798,7 +26791,7 @@ const createEncryptedInput = ({ aclContractAddress, chainId, tfheCompactPublicKe
|
|
|
26798
26791
|
auxData.set(buffUser, 20);
|
|
26799
26792
|
auxData.set(buffAcl, 40);
|
|
26800
26793
|
auxData.set(buffChainId, auxData.length - buffChainId.length);
|
|
26801
|
-
const encrypted = builder.build_with_proof_packed(pp, auxData, ZkComputeLoad.Verify);
|
|
26794
|
+
const encrypted = builder.build_with_proof_packed(pp, auxData, TFHE.ZkComputeLoad.Verify);
|
|
26802
26795
|
ciphertextWithZKProof = encrypted.safe_serialize(SERIALIZED_SIZE_LIMIT_CIPHERTEXT);
|
|
26803
26796
|
return ciphertextWithZKProof;
|
|
26804
26797
|
},
|
|
@@ -26844,9 +26837,9 @@ const computeHandles = (ciphertextWithZKProof, bitwidths, aclContractAddress, ch
|
|
|
26844
26837
|
if (BigInt(chainId) > MAX_UINT64) {
|
|
26845
26838
|
throw new Error('ChainId exceeds maximum allowed value (8 bytes)'); // fhevm assumes chainID is only taking up to 8 bytes
|
|
26846
26839
|
}
|
|
26847
|
-
const chainId8Bytes =
|
|
26840
|
+
const chainId8Bytes = fromHexString(hex).slice(24, 32);
|
|
26848
26841
|
dataInput[21] = encryptionIndex;
|
|
26849
|
-
|
|
26842
|
+
dataInput.set(chainId8Bytes, 22);
|
|
26850
26843
|
dataInput[30] = encryptionType;
|
|
26851
26844
|
dataInput[31] = ciphertextVersion;
|
|
26852
26845
|
return dataInput;
|
|
@@ -27198,7 +27191,7 @@ const publicDecryptRequest = (kmsSigners, thresholdSigners, gatewayChainId, veri
|
|
|
27198
27191
|
* @param durationDays - How many days the decryption permission remains valid
|
|
27199
27192
|
* @returns EIP712 typed data structure for user decryption
|
|
27200
27193
|
*/
|
|
27201
|
-
const createEIP712 = (
|
|
27194
|
+
const createEIP712 = (verifyingContract, contractsChainId) => (publicKey, contractAddresses, startTimestamp, durationDays, delegatedAccount) => {
|
|
27202
27195
|
if (delegatedAccount && !isAddress(delegatedAccount))
|
|
27203
27196
|
throw new Error('Invalid delegated account.');
|
|
27204
27197
|
if (!isAddress(verifyingContract)) {
|
|
@@ -27227,7 +27220,7 @@ const createEIP712 = (gatewayChainId, verifyingContract, contractsChainId) => (p
|
|
|
27227
27220
|
const domain = {
|
|
27228
27221
|
name: 'Decryption',
|
|
27229
27222
|
version: '1',
|
|
27230
|
-
chainId:
|
|
27223
|
+
chainId: contractsChainId,
|
|
27231
27224
|
verifyingContract,
|
|
27232
27225
|
};
|
|
27233
27226
|
if (delegatedAccount) {
|
|
@@ -27281,14 +27274,34 @@ const createEIP712 = (gatewayChainId, verifyingContract, contractsChainId) => (p
|
|
|
27281
27274
|
};
|
|
27282
27275
|
};
|
|
27283
27276
|
const generateKeypair = () => {
|
|
27284
|
-
const keypair = ml_kem_pke_keygen();
|
|
27277
|
+
const keypair = TKMS.ml_kem_pke_keygen();
|
|
27285
27278
|
return {
|
|
27286
|
-
publicKey: toHexString(ml_kem_pke_pk_to_u8vec(ml_kem_pke_get_pk(keypair))),
|
|
27287
|
-
privateKey: toHexString(ml_kem_pke_sk_to_u8vec(keypair)),
|
|
27279
|
+
publicKey: toHexString(TKMS.ml_kem_pke_pk_to_u8vec(TKMS.ml_kem_pke_get_pk(keypair))),
|
|
27280
|
+
privateKey: toHexString(TKMS.ml_kem_pke_sk_to_u8vec(keypair)),
|
|
27288
27281
|
};
|
|
27289
27282
|
};
|
|
27290
27283
|
|
|
27291
27284
|
global.fetch = fetchRetry(global.fetch, { retries: 5, retryDelay: 500 });
|
|
27285
|
+
const SepoliaConfig = {
|
|
27286
|
+
// ACL_CONTRACT_ADDRESS (FHEVM Host chain)
|
|
27287
|
+
aclContractAddress: '0x687820221192C5B662b25367F70076A37bc79b6c',
|
|
27288
|
+
// KMS_VERIFIER_CONTRACT_ADDRESS (FHEVM Host chain)
|
|
27289
|
+
kmsContractAddress: '0x1364cBBf2cDF5032C47d8226a6f6FBD2AFCDacAC',
|
|
27290
|
+
// INPUT_VERIFIER_CONTRACT_ADDRESS (FHEVM Host chain)
|
|
27291
|
+
inputVerifierContractAddress: '0xbc91f3daD1A5F19F8390c400196e58073B6a0BC4',
|
|
27292
|
+
// DECRYPTION_ADDRESS (Gateway chain)
|
|
27293
|
+
verifyingContractAddressDecryption: '0xb6E160B1ff80D67Bfe90A85eE06Ce0A2613607D1',
|
|
27294
|
+
// INPUT_VERIFICATION_ADDRESS (Gateway chain)
|
|
27295
|
+
verifyingContractAddressInputVerification: '0x7048C39f048125eDa9d678AEbaDfB22F7900a29F',
|
|
27296
|
+
// FHEVM Host chain id
|
|
27297
|
+
chainId: 11155111,
|
|
27298
|
+
// Gateway chain id
|
|
27299
|
+
gatewayChainId: 55815,
|
|
27300
|
+
// Optional RPC provider to host chain
|
|
27301
|
+
network: 'https://eth-sepolia.public.blastapi.io',
|
|
27302
|
+
// Relayer URL
|
|
27303
|
+
relayerUrl: 'https://relayer.testnet.zama.cloud',
|
|
27304
|
+
};
|
|
27292
27305
|
const createInstance = async (config) => {
|
|
27293
27306
|
const { verifyingContractAddressDecryption, verifyingContractAddressInputVerification, publicKey, kmsContractAddress, aclContractAddress, gatewayChainId, } = config;
|
|
27294
27307
|
if (!kmsContractAddress || !isAddress(kmsContractAddress)) {
|
|
@@ -27321,7 +27334,7 @@ const createInstance = async (config) => {
|
|
|
27321
27334
|
return {
|
|
27322
27335
|
createEncryptedInput: createRelayerEncryptedInput(aclContractAddress, verifyingContractAddressInputVerification, chainId, gatewayChainId, cleanURL(config.relayerUrl), publicKeyData.publicKey, publicParamsData, coprocessorSigners, thresholdCoprocessorSigners),
|
|
27323
27336
|
generateKeypair,
|
|
27324
|
-
createEIP712: createEIP712(
|
|
27337
|
+
createEIP712: createEIP712(verifyingContractAddressDecryption, chainId),
|
|
27325
27338
|
publicDecrypt: publicDecryptRequest(kmsSigners, thresholdKMSSigners, gatewayChainId, verifyingContractAddressDecryption, aclContractAddress, cleanURL(config.relayerUrl), provider),
|
|
27326
27339
|
userDecrypt: userDecryptRequest(kmsSigners, gatewayChainId, chainId, verifyingContractAddressDecryption, aclContractAddress, cleanURL(config.relayerUrl), provider),
|
|
27327
27340
|
getPublicKey: () => publicKeyData.publicKey
|
|
@@ -27365,4 +27378,26 @@ const initFhevm = async ({ tfheParams, kmsParams, thread, } = {}) => {
|
|
|
27365
27378
|
return true;
|
|
27366
27379
|
};
|
|
27367
27380
|
|
|
27368
|
-
|
|
27381
|
+
// ESM explicit named re-export is required.
|
|
27382
|
+
window.TFHE = {
|
|
27383
|
+
default: __wbg_init$1,
|
|
27384
|
+
initThreadPool,
|
|
27385
|
+
init_panic_hook,
|
|
27386
|
+
TfheCompactPublicKey: TfheCompactPublicKey,
|
|
27387
|
+
CompactPkeCrs: CompactPkeCrs,
|
|
27388
|
+
CompactCiphertextList: CompactCiphertextList,
|
|
27389
|
+
ZkComputeLoad: ZkComputeLoad,
|
|
27390
|
+
};
|
|
27391
|
+
window.TKMS = {
|
|
27392
|
+
default: __wbg_init,
|
|
27393
|
+
u8vec_to_ml_kem_pke_pk,
|
|
27394
|
+
u8vec_to_ml_kem_pke_sk,
|
|
27395
|
+
new_client,
|
|
27396
|
+
process_user_decryption_resp_from_js,
|
|
27397
|
+
ml_kem_pke_keygen,
|
|
27398
|
+
ml_kem_pke_pk_to_u8vec,
|
|
27399
|
+
ml_kem_pke_sk_to_u8vec,
|
|
27400
|
+
ml_kem_pke_get_pk,
|
|
27401
|
+
};
|
|
27402
|
+
|
|
27403
|
+
export { ENCRYPTION_TYPES, SepoliaConfig, createEIP712, createInstance, generateKeypair, initFhevm };
|