@zama-fhe/relayer-sdk 0.1.0-6 → 0.1.0-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/bin/relayer.js +14 -7
- package/bin/utils.js +6 -2
- package/bundle/{fhevm.js → relayer-sdk-js.js} +2343 -2322
- package/bundle/{fhevm.umd.cjs → relayer-sdk-js.umd.cjs} +7 -7
- 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 +278 -247
- package/lib/workerHelpers.js +16 -15
- package/package.json +4 -4
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;
|
|
@@ -26433,6 +26217,230 @@ async function __wbg_init(module_or_path) {
|
|
|
26433
26217
|
return __wbg_finalize_init(instance, module);
|
|
26434
26218
|
}
|
|
26435
26219
|
|
|
26220
|
+
const SERIALIZED_SIZE_LIMIT_CIPHERTEXT = BigInt(1024 * 1024 * 512);
|
|
26221
|
+
const SERIALIZED_SIZE_LIMIT_PK = BigInt(1024 * 1024 * 512);
|
|
26222
|
+
const SERIALIZED_SIZE_LIMIT_CRS = BigInt(1024 * 1024 * 512);
|
|
26223
|
+
const cleanURL = (url) => {
|
|
26224
|
+
if (!url)
|
|
26225
|
+
return '';
|
|
26226
|
+
return url.endsWith('/') ? url.slice(0, -1) : url;
|
|
26227
|
+
};
|
|
26228
|
+
const numberToHex = (num) => {
|
|
26229
|
+
let hex = num.toString(16);
|
|
26230
|
+
return hex.length % 2 ? '0' + hex : hex;
|
|
26231
|
+
};
|
|
26232
|
+
const fromHexString = (hexString) => {
|
|
26233
|
+
const arr = hexString.replace(/^(0x)/, '').match(/.{1,2}/g);
|
|
26234
|
+
if (!arr)
|
|
26235
|
+
return new Uint8Array();
|
|
26236
|
+
return Uint8Array.from(arr.map((byte) => parseInt(byte, 16)));
|
|
26237
|
+
};
|
|
26238
|
+
const toHexString = (bytes, with0x = false) => `${with0x ? '0x' : ''}${bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '')}`;
|
|
26239
|
+
const bytesToBigInt = function (byteArray) {
|
|
26240
|
+
if (!byteArray || byteArray?.length === 0) {
|
|
26241
|
+
return BigInt(0);
|
|
26242
|
+
}
|
|
26243
|
+
const hex = Array.from(byteArray)
|
|
26244
|
+
.map((b) => b.toString(16).padStart(2, '0')) // byte to hex
|
|
26245
|
+
.join('');
|
|
26246
|
+
return BigInt(`0x${hex}`);
|
|
26247
|
+
};
|
|
26248
|
+
|
|
26249
|
+
const keyurlCache = {};
|
|
26250
|
+
const getKeysFromRelayer = async (url, publicKeyId) => {
|
|
26251
|
+
if (keyurlCache[url]) {
|
|
26252
|
+
return keyurlCache[url];
|
|
26253
|
+
}
|
|
26254
|
+
try {
|
|
26255
|
+
const response = await fetch(`${url}/v1/keyurl`);
|
|
26256
|
+
if (!response.ok) {
|
|
26257
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
26258
|
+
}
|
|
26259
|
+
const data = await response.json();
|
|
26260
|
+
if (data) {
|
|
26261
|
+
let pubKeyUrl;
|
|
26262
|
+
// If no publicKeyId is provided, use the first one
|
|
26263
|
+
// Warning: if there are multiple keys available, the first one will most likely never be the
|
|
26264
|
+
// same between several calls (fetching the infos is non-deterministic)
|
|
26265
|
+
if (!publicKeyId) {
|
|
26266
|
+
pubKeyUrl = data.response.fhe_key_info[0].fhe_public_key.urls[0];
|
|
26267
|
+
publicKeyId = data.response.fhe_key_info[0].fhe_public_key.data_id;
|
|
26268
|
+
}
|
|
26269
|
+
else {
|
|
26270
|
+
// If a publicKeyId is provided, get the corresponding info
|
|
26271
|
+
const keyInfo = data.response.fhe_key_info.find((info) => info.fhe_public_key.data_id === publicKeyId);
|
|
26272
|
+
if (!keyInfo) {
|
|
26273
|
+
throw new Error(`Could not find FHE key info with data_id ${publicKeyId}`);
|
|
26274
|
+
}
|
|
26275
|
+
// TODO: Get a given party's public key url instead of the first one
|
|
26276
|
+
pubKeyUrl = keyInfo.fhe_public_key.urls[0];
|
|
26277
|
+
}
|
|
26278
|
+
const publicKeyResponse = await fetch(pubKeyUrl);
|
|
26279
|
+
if (!publicKeyResponse.ok) {
|
|
26280
|
+
throw new Error(`HTTP error! status: ${publicKeyResponse.status} on ${publicKeyResponse.url}`);
|
|
26281
|
+
}
|
|
26282
|
+
let publicKey;
|
|
26283
|
+
if (typeof publicKeyResponse.bytes === 'function') {
|
|
26284
|
+
// bytes is not widely supported yet
|
|
26285
|
+
publicKey = await publicKeyResponse.bytes();
|
|
26286
|
+
}
|
|
26287
|
+
else {
|
|
26288
|
+
publicKey = new Uint8Array(await publicKeyResponse.arrayBuffer());
|
|
26289
|
+
}
|
|
26290
|
+
const publicParamsUrl = data.response.crs['2048'].urls[0];
|
|
26291
|
+
const publicParamsId = data.response.crs['2048'].data_id;
|
|
26292
|
+
const publicParams2048Response = await fetch(publicParamsUrl);
|
|
26293
|
+
if (!publicParams2048Response.ok) {
|
|
26294
|
+
throw new Error(`HTTP error! status: ${publicParams2048Response.status} on ${publicParams2048Response.url}`);
|
|
26295
|
+
}
|
|
26296
|
+
let publicParams2048;
|
|
26297
|
+
if (typeof publicParams2048Response.bytes === 'function') {
|
|
26298
|
+
// bytes is not widely supported yet
|
|
26299
|
+
publicParams2048 = await publicParams2048Response.bytes();
|
|
26300
|
+
}
|
|
26301
|
+
else {
|
|
26302
|
+
publicParams2048 = new Uint8Array(await publicParams2048Response.arrayBuffer());
|
|
26303
|
+
}
|
|
26304
|
+
let pub_key;
|
|
26305
|
+
try {
|
|
26306
|
+
pub_key = TFHE.TfheCompactPublicKey.safe_deserialize(publicKey, SERIALIZED_SIZE_LIMIT_PK);
|
|
26307
|
+
}
|
|
26308
|
+
catch (e) {
|
|
26309
|
+
throw new Error('Invalid public key (deserialization failed)', {
|
|
26310
|
+
cause: e,
|
|
26311
|
+
});
|
|
26312
|
+
}
|
|
26313
|
+
let crs;
|
|
26314
|
+
try {
|
|
26315
|
+
crs = TFHE.CompactPkeCrs.safe_deserialize(new Uint8Array(publicParams2048), SERIALIZED_SIZE_LIMIT_CRS);
|
|
26316
|
+
}
|
|
26317
|
+
catch (e) {
|
|
26318
|
+
throw new Error('Invalid crs (deserialization failed)', {
|
|
26319
|
+
cause: e,
|
|
26320
|
+
});
|
|
26321
|
+
}
|
|
26322
|
+
const result = {
|
|
26323
|
+
publicKey: pub_key,
|
|
26324
|
+
publicKeyId,
|
|
26325
|
+
publicParams: {
|
|
26326
|
+
2048: {
|
|
26327
|
+
publicParams: crs,
|
|
26328
|
+
publicParamsId,
|
|
26329
|
+
},
|
|
26330
|
+
},
|
|
26331
|
+
};
|
|
26332
|
+
keyurlCache[url] = result;
|
|
26333
|
+
return result;
|
|
26334
|
+
}
|
|
26335
|
+
else {
|
|
26336
|
+
throw new Error('No public key available');
|
|
26337
|
+
}
|
|
26338
|
+
}
|
|
26339
|
+
catch (e) {
|
|
26340
|
+
throw new Error('Impossible to fetch public key: wrong relayer url.', {
|
|
26341
|
+
cause: e,
|
|
26342
|
+
});
|
|
26343
|
+
}
|
|
26344
|
+
};
|
|
26345
|
+
|
|
26346
|
+
const abiKmsVerifier = [
|
|
26347
|
+
'function getKmsSigners() view returns (address[])',
|
|
26348
|
+
'function getThreshold() view returns (uint256)',
|
|
26349
|
+
];
|
|
26350
|
+
const abiInputVerifier = [
|
|
26351
|
+
'function getCoprocessorSigners() view returns (address[])',
|
|
26352
|
+
'function getThreshold() view returns (uint256)',
|
|
26353
|
+
];
|
|
26354
|
+
const getProvider = (config) => {
|
|
26355
|
+
if (typeof config.network === 'string') {
|
|
26356
|
+
return new JsonRpcProvider(config.network);
|
|
26357
|
+
}
|
|
26358
|
+
else if (config.network) {
|
|
26359
|
+
return new BrowserProvider(config.network);
|
|
26360
|
+
}
|
|
26361
|
+
throw new Error('You must provide a network URL or a EIP1193 object (eg: window.ethereum)');
|
|
26362
|
+
};
|
|
26363
|
+
const getChainId = async (provider, config) => {
|
|
26364
|
+
if (config.chainId && typeof config.chainId === 'number') {
|
|
26365
|
+
return config.chainId;
|
|
26366
|
+
}
|
|
26367
|
+
else if (config.chainId && typeof config.chainId !== 'number') {
|
|
26368
|
+
throw new Error('chainId must be a number.');
|
|
26369
|
+
}
|
|
26370
|
+
else {
|
|
26371
|
+
const chainId = (await provider.getNetwork()).chainId;
|
|
26372
|
+
return Number(chainId);
|
|
26373
|
+
}
|
|
26374
|
+
};
|
|
26375
|
+
const getTfheCompactPublicKey = async (config) => {
|
|
26376
|
+
if (config.relayerUrl && !config.publicKey) {
|
|
26377
|
+
const inputs = await getKeysFromRelayer(cleanURL(config.relayerUrl));
|
|
26378
|
+
return { publicKey: inputs.publicKey, publicKeyId: inputs.publicKeyId };
|
|
26379
|
+
}
|
|
26380
|
+
else if (config.publicKey && config.publicKey.data && config.publicKey.id) {
|
|
26381
|
+
const buff = config.publicKey.data;
|
|
26382
|
+
try {
|
|
26383
|
+
return {
|
|
26384
|
+
publicKey: TFHE.TfheCompactPublicKey.safe_deserialize(buff, SERIALIZED_SIZE_LIMIT_PK),
|
|
26385
|
+
publicKeyId: config.publicKey.id,
|
|
26386
|
+
};
|
|
26387
|
+
}
|
|
26388
|
+
catch (e) {
|
|
26389
|
+
throw new Error('Invalid public key (deserialization failed)', {
|
|
26390
|
+
cause: e,
|
|
26391
|
+
});
|
|
26392
|
+
}
|
|
26393
|
+
}
|
|
26394
|
+
else {
|
|
26395
|
+
throw new Error('You must provide a public key with its public key ID.');
|
|
26396
|
+
}
|
|
26397
|
+
};
|
|
26398
|
+
const getPublicParams = async (config) => {
|
|
26399
|
+
if (config.relayerUrl && !config.publicParams) {
|
|
26400
|
+
const inputs = await getKeysFromRelayer(cleanURL(config.relayerUrl));
|
|
26401
|
+
return inputs.publicParams;
|
|
26402
|
+
}
|
|
26403
|
+
else if (config.publicParams && config.publicParams['2048']) {
|
|
26404
|
+
const buff = config.publicParams['2048'].publicParams;
|
|
26405
|
+
try {
|
|
26406
|
+
return {
|
|
26407
|
+
2048: {
|
|
26408
|
+
publicParams: TFHE.CompactPkeCrs.safe_deserialize(buff, SERIALIZED_SIZE_LIMIT_CRS),
|
|
26409
|
+
publicParamsId: config.publicParams['2048'].publicParamsId,
|
|
26410
|
+
},
|
|
26411
|
+
};
|
|
26412
|
+
}
|
|
26413
|
+
catch (e) {
|
|
26414
|
+
throw new Error('Invalid public key (deserialization failed)', {
|
|
26415
|
+
cause: e,
|
|
26416
|
+
});
|
|
26417
|
+
}
|
|
26418
|
+
}
|
|
26419
|
+
else {
|
|
26420
|
+
throw new Error('You must provide a valid CRS with its CRS ID.');
|
|
26421
|
+
}
|
|
26422
|
+
};
|
|
26423
|
+
const getKMSSigners = async (provider, config) => {
|
|
26424
|
+
const kmsContract = new Contract(config.kmsContractAddress, abiKmsVerifier, provider);
|
|
26425
|
+
const signers = await kmsContract.getKmsSigners();
|
|
26426
|
+
return signers;
|
|
26427
|
+
};
|
|
26428
|
+
const getKMSSignersThreshold = async (provider, config) => {
|
|
26429
|
+
const kmsContract = new Contract(config.kmsContractAddress, abiKmsVerifier, provider);
|
|
26430
|
+
const threshold = await kmsContract.getThreshold();
|
|
26431
|
+
return Number(threshold); // threshold is always supposed to fit in a number
|
|
26432
|
+
};
|
|
26433
|
+
const getCoprocessorSigners = async (provider, config) => {
|
|
26434
|
+
const inputContract = new Contract(config.inputVerifierContractAddress, abiInputVerifier, provider);
|
|
26435
|
+
const signers = await inputContract.getCoprocessorSigners();
|
|
26436
|
+
return signers;
|
|
26437
|
+
};
|
|
26438
|
+
const getCoprocessorSignersThreshold = async (provider, config) => {
|
|
26439
|
+
const inputContract = new Contract(config.inputVerifierContractAddress, abiInputVerifier, provider);
|
|
26440
|
+
const threshold = await inputContract.getThreshold();
|
|
26441
|
+
return Number(threshold); // threshold is always supposed to fit in a number
|
|
26442
|
+
};
|
|
26443
|
+
|
|
26436
26444
|
const NumEncryptedBits = {
|
|
26437
26445
|
0: 2, // ebool
|
|
26438
26446
|
2: 8, // euint8
|
|
@@ -26580,8 +26588,8 @@ const userDecryptRequest = (kmsSigners, gatewayChainId, chainId, verifyingContra
|
|
|
26580
26588
|
let pubKey;
|
|
26581
26589
|
let privKey;
|
|
26582
26590
|
try {
|
|
26583
|
-
pubKey = u8vec_to_ml_kem_pke_pk(fromHexString(publicKey));
|
|
26584
|
-
privKey = u8vec_to_ml_kem_pke_sk(fromHexString(privateKey));
|
|
26591
|
+
pubKey = TKMS.u8vec_to_ml_kem_pke_pk(fromHexString(publicKey));
|
|
26592
|
+
privKey = TKMS.u8vec_to_ml_kem_pke_sk(fromHexString(privateKey));
|
|
26585
26593
|
}
|
|
26586
26594
|
catch (e) {
|
|
26587
26595
|
throw new Error('Invalid public or private key', { cause: e });
|
|
@@ -26612,9 +26620,9 @@ const userDecryptRequest = (kmsSigners, gatewayChainId, chainId, verifyingContra
|
|
|
26612
26620
|
}
|
|
26613
26621
|
// assume the KMS Signers have the correct order
|
|
26614
26622
|
let indexedKmsSigners = kmsSigners.map((signer, index) => {
|
|
26615
|
-
return new_server_id_addr(index + 1, signer);
|
|
26623
|
+
return TKMS.new_server_id_addr(index + 1, signer);
|
|
26616
26624
|
});
|
|
26617
|
-
const client = new_client(indexedKmsSigners, userAddress, 'default');
|
|
26625
|
+
const client = TKMS.new_client(indexedKmsSigners, userAddress, 'default');
|
|
26618
26626
|
try {
|
|
26619
26627
|
const buffer = new ArrayBuffer(32);
|
|
26620
26628
|
const view = new DataView(buffer);
|
|
@@ -26634,7 +26642,7 @@ const userDecryptRequest = (kmsSigners, gatewayChainId, chainId, verifyingContra
|
|
|
26634
26642
|
ciphertext_handles: handles.map((h) => h.handle.replace(/^0x/, '')),
|
|
26635
26643
|
eip712_verifying_contract: verifyingContractAddress,
|
|
26636
26644
|
};
|
|
26637
|
-
const decryption = process_user_decryption_resp_from_js(client, payloadForVerification, eip712Domain, json.response, pubKey, privKey, true);
|
|
26645
|
+
const decryption = TKMS.process_user_decryption_resp_from_js(client, payloadForVerification, eip712Domain, json.response, pubKey, privKey, true);
|
|
26638
26646
|
const listBigIntDecryptions = decryption.map((d) => bytesToBigInt(d.bytes));
|
|
26639
26647
|
const results = buildUserDecryptedResult(handles.map((h) => h.handle), listBigIntDecryptions);
|
|
26640
26648
|
return results;
|
|
@@ -26669,7 +26677,7 @@ const createEncryptedInput = ({ aclContractAddress, chainId, tfheCompactPublicKe
|
|
|
26669
26677
|
}
|
|
26670
26678
|
const publicKey = tfheCompactPublicKey;
|
|
26671
26679
|
const bits = [];
|
|
26672
|
-
const builder = CompactCiphertextList.builder(publicKey);
|
|
26680
|
+
const builder = TFHE.CompactCiphertextList.builder(publicKey);
|
|
26673
26681
|
let ciphertextWithZKProof = new Uint8Array(); // updated in `_prove`
|
|
26674
26682
|
const checkLimit = (added) => {
|
|
26675
26683
|
if (bits.reduce((acc, val) => acc + Math.max(2, val), 0) + added > 2048) {
|
|
@@ -26800,7 +26808,7 @@ const createEncryptedInput = ({ aclContractAddress, chainId, tfheCompactPublicKe
|
|
|
26800
26808
|
auxData.set(buffUser, 20);
|
|
26801
26809
|
auxData.set(buffAcl, 40);
|
|
26802
26810
|
auxData.set(buffChainId, auxData.length - buffChainId.length);
|
|
26803
|
-
const encrypted = builder.build_with_proof_packed(pp, auxData, ZkComputeLoad.Verify);
|
|
26811
|
+
const encrypted = builder.build_with_proof_packed(pp, auxData, TFHE.ZkComputeLoad.Verify);
|
|
26804
26812
|
ciphertextWithZKProof = encrypted.safe_serialize(SERIALIZED_SIZE_LIMIT_CIPHERTEXT);
|
|
26805
26813
|
return ciphertextWithZKProof;
|
|
26806
26814
|
},
|
|
@@ -26846,9 +26854,9 @@ const computeHandles = (ciphertextWithZKProof, bitwidths, aclContractAddress, ch
|
|
|
26846
26854
|
if (BigInt(chainId) > MAX_UINT64) {
|
|
26847
26855
|
throw new Error('ChainId exceeds maximum allowed value (8 bytes)'); // fhevm assumes chainID is only taking up to 8 bytes
|
|
26848
26856
|
}
|
|
26849
|
-
const chainId8Bytes =
|
|
26857
|
+
const chainId8Bytes = fromHexString(hex).slice(24, 32);
|
|
26850
26858
|
dataInput[21] = encryptionIndex;
|
|
26851
|
-
|
|
26859
|
+
dataInput.set(chainId8Bytes, 22);
|
|
26852
26860
|
dataInput[30] = encryptionType;
|
|
26853
26861
|
dataInput[31] = ciphertextVersion;
|
|
26854
26862
|
return dataInput;
|
|
@@ -27200,7 +27208,7 @@ const publicDecryptRequest = (kmsSigners, thresholdSigners, gatewayChainId, veri
|
|
|
27200
27208
|
* @param durationDays - How many days the decryption permission remains valid
|
|
27201
27209
|
* @returns EIP712 typed data structure for user decryption
|
|
27202
27210
|
*/
|
|
27203
|
-
const createEIP712 = (
|
|
27211
|
+
const createEIP712 = (verifyingContract, contractsChainId) => (publicKey, contractAddresses, startTimestamp, durationDays, delegatedAccount) => {
|
|
27204
27212
|
if (delegatedAccount && !isAddress(delegatedAccount))
|
|
27205
27213
|
throw new Error('Invalid delegated account.');
|
|
27206
27214
|
if (!isAddress(verifyingContract)) {
|
|
@@ -27229,7 +27237,7 @@ const createEIP712 = (gatewayChainId, verifyingContract, contractsChainId) => (p
|
|
|
27229
27237
|
const domain = {
|
|
27230
27238
|
name: 'Decryption',
|
|
27231
27239
|
version: '1',
|
|
27232
|
-
chainId:
|
|
27240
|
+
chainId: contractsChainId,
|
|
27233
27241
|
verifyingContract,
|
|
27234
27242
|
};
|
|
27235
27243
|
if (delegatedAccount) {
|
|
@@ -27283,10 +27291,10 @@ const createEIP712 = (gatewayChainId, verifyingContract, contractsChainId) => (p
|
|
|
27283
27291
|
};
|
|
27284
27292
|
};
|
|
27285
27293
|
const generateKeypair = () => {
|
|
27286
|
-
const keypair = ml_kem_pke_keygen();
|
|
27294
|
+
const keypair = TKMS.ml_kem_pke_keygen();
|
|
27287
27295
|
return {
|
|
27288
|
-
publicKey: toHexString(ml_kem_pke_pk_to_u8vec(ml_kem_pke_get_pk(keypair))),
|
|
27289
|
-
privateKey: toHexString(ml_kem_pke_sk_to_u8vec(keypair)),
|
|
27296
|
+
publicKey: toHexString(TKMS.ml_kem_pke_pk_to_u8vec(TKMS.ml_kem_pke_get_pk(keypair))),
|
|
27297
|
+
privateKey: toHexString(TKMS.ml_kem_pke_sk_to_u8vec(keypair)),
|
|
27290
27298
|
};
|
|
27291
27299
|
};
|
|
27292
27300
|
|
|
@@ -27343,7 +27351,7 @@ const createInstance = async (config) => {
|
|
|
27343
27351
|
return {
|
|
27344
27352
|
createEncryptedInput: createRelayerEncryptedInput(aclContractAddress, verifyingContractAddressInputVerification, chainId, gatewayChainId, cleanURL(config.relayerUrl), publicKeyData.publicKey, publicParamsData, coprocessorSigners, thresholdCoprocessorSigners),
|
|
27345
27353
|
generateKeypair,
|
|
27346
|
-
createEIP712: createEIP712(
|
|
27354
|
+
createEIP712: createEIP712(verifyingContractAddressDecryption, chainId),
|
|
27347
27355
|
publicDecrypt: publicDecryptRequest(kmsSigners, thresholdKMSSigners, gatewayChainId, verifyingContractAddressDecryption, aclContractAddress, cleanURL(config.relayerUrl), provider),
|
|
27348
27356
|
userDecrypt: userDecryptRequest(kmsSigners, gatewayChainId, chainId, verifyingContractAddressDecryption, aclContractAddress, cleanURL(config.relayerUrl), provider),
|
|
27349
27357
|
getPublicKey: () => publicKeyData.publicKey
|
|
@@ -27387,4 +27395,27 @@ const initFhevm = async ({ tfheParams, kmsParams, thread, } = {}) => {
|
|
|
27387
27395
|
return true;
|
|
27388
27396
|
};
|
|
27389
27397
|
|
|
27398
|
+
// ESM explicit named re-export is required.
|
|
27399
|
+
window.TFHE = {
|
|
27400
|
+
default: __wbg_init$1,
|
|
27401
|
+
initThreadPool,
|
|
27402
|
+
init_panic_hook,
|
|
27403
|
+
TfheCompactPublicKey: TfheCompactPublicKey,
|
|
27404
|
+
CompactPkeCrs: CompactPkeCrs,
|
|
27405
|
+
CompactCiphertextList: CompactCiphertextList,
|
|
27406
|
+
ZkComputeLoad: ZkComputeLoad,
|
|
27407
|
+
};
|
|
27408
|
+
window.TKMS = {
|
|
27409
|
+
default: __wbg_init,
|
|
27410
|
+
u8vec_to_ml_kem_pke_pk,
|
|
27411
|
+
u8vec_to_ml_kem_pke_sk,
|
|
27412
|
+
new_client,
|
|
27413
|
+
new_server_id_addr,
|
|
27414
|
+
process_user_decryption_resp_from_js,
|
|
27415
|
+
ml_kem_pke_keygen,
|
|
27416
|
+
ml_kem_pke_pk_to_u8vec,
|
|
27417
|
+
ml_kem_pke_sk_to_u8vec,
|
|
27418
|
+
ml_kem_pke_get_pk,
|
|
27419
|
+
};
|
|
27420
|
+
|
|
27390
27421
|
export { ENCRYPTION_TYPES, SepoliaConfig, createEIP712, createInstance, generateKeypair, initFhevm };
|
package/lib/workerHelpers.js
CHANGED
|
@@ -284,6 +284,22 @@ function passArray8ToWasm0(arg, malloc) {
|
|
|
284
284
|
WASM_VECTOR_LEN = arg.length;
|
|
285
285
|
return ptr;
|
|
286
286
|
}
|
|
287
|
+
|
|
288
|
+
function init_panic_hook() {
|
|
289
|
+
wasm.init_panic_hook();
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* @param {TfheServerKey} server_key
|
|
294
|
+
*/
|
|
295
|
+
function set_server_key(server_key) {
|
|
296
|
+
_assertClass(server_key, TfheServerKey);
|
|
297
|
+
const ret = wasm.set_server_key(server_key.__wbg_ptr);
|
|
298
|
+
if (ret[1]) {
|
|
299
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
287
303
|
/**
|
|
288
304
|
* @param {ShortintCompactPublicKeyEncryptionParametersName} param
|
|
289
305
|
* @returns {string}
|
|
@@ -318,21 +334,6 @@ function shortint_params_name(param) {
|
|
|
318
334
|
}
|
|
319
335
|
}
|
|
320
336
|
|
|
321
|
-
function init_panic_hook() {
|
|
322
|
-
wasm.init_panic_hook();
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
/**
|
|
326
|
-
* @param {TfheServerKey} server_key
|
|
327
|
-
*/
|
|
328
|
-
function set_server_key(server_key) {
|
|
329
|
-
_assertClass(server_key, TfheServerKey);
|
|
330
|
-
const ret = wasm.set_server_key(server_key.__wbg_ptr);
|
|
331
|
-
if (ret[1]) {
|
|
332
|
-
throw takeFromExternrefTable0(ret[0]);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
|
|
336
337
|
/**
|
|
337
338
|
* @param {number} num_threads
|
|
338
339
|
* @returns {Promise<any>}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zama-fhe/relayer-sdk",
|
|
3
|
-
"version": "0.1.0-
|
|
3
|
+
"version": "0.1.0-8",
|
|
4
4
|
"description": "fhevm Relayer SDK",
|
|
5
5
|
"main": "lib/node.js",
|
|
6
6
|
"types": "lib/node.d.ts",
|
|
@@ -61,13 +61,13 @@
|
|
|
61
61
|
},
|
|
62
62
|
"homepage": "https://github.com/zama-ai/relayer-sdk#readme",
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"commander": "^
|
|
64
|
+
"commander": "^14.0.0",
|
|
65
65
|
"ethers": "^6.13.4",
|
|
66
66
|
"fetch-retry": "^6.0.0",
|
|
67
67
|
"keccak": "^3.0.4",
|
|
68
|
-
"node-tfhe": "^1.1.
|
|
68
|
+
"node-tfhe": "^1.1.3",
|
|
69
69
|
"node-tkms": "0.11.0-rc17",
|
|
70
|
-
"tfhe": "^1.1.
|
|
70
|
+
"tfhe": "^1.1.3",
|
|
71
71
|
"tkms": "0.11.0-rc17",
|
|
72
72
|
"wasm-feature-detect": "^1.8.0"
|
|
73
73
|
},
|