@cofhe/sdk 0.4.0 → 0.5.1
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/CHANGELOG.md +38 -0
- package/adapters/{ethers5.test.ts → test/ethers5.test.ts} +2 -2
- package/adapters/{ethers6.test.ts → test/ethers6.test.ts} +2 -2
- package/adapters/{hardhat.hh2.test.ts → test/hardhat.hh2.test.ts} +2 -2
- package/adapters/{index.test.ts → test/index.test.ts} +1 -1
- package/adapters/{wagmi.test.ts → test/wagmi.test.ts} +1 -1
- package/chains/{chains.test.ts → test/chains.test.ts} +1 -1
- package/core/client.ts +11 -1
- package/core/clientTypes.ts +3 -1
- package/core/consts.ts +9 -0
- package/core/decrypt/cofheMocksDecryptForTx.ts +14 -3
- package/core/decrypt/decryptForTxBuilder.ts +16 -2
- package/core/decrypt/decryptForViewBuilder.ts +14 -7
- package/core/decrypt/polling.ts +14 -0
- package/core/decrypt/tnDecryptV2.ts +250 -110
- package/core/decrypt/tnSealOutputV2.ts +245 -104
- package/core/decrypt/verifyDecryptResult.ts +65 -0
- package/core/encrypt/cofheMocksZkVerifySign.ts +6 -6
- package/core/encrypt/zkPackProveVerify.ts +10 -19
- package/core/fetchKeys.ts +0 -2
- package/core/index.ts +9 -1
- package/core/keyStore.ts +5 -2
- package/core/permits.ts +5 -0
- package/core/{client.test.ts → test/client.test.ts} +7 -7
- package/core/{config.test.ts → test/config.test.ts} +1 -1
- package/core/test/decrypt.test.ts +252 -0
- package/core/test/decryptBuilders.test.ts +390 -0
- package/core/{encrypt → test}/encryptInputsBuilder.test.ts +61 -6
- package/core/{fetchKeys.test.ts → test/fetchKeys.test.ts} +3 -3
- package/core/{keyStore.test.ts → test/keyStore.test.ts} +5 -3
- package/core/{permits.test.ts → test/permits.test.ts} +42 -1
- package/core/test/pollCallbacks.test.ts +563 -0
- package/core/types.ts +13 -0
- package/dist/chains.d.cts +2 -2
- package/dist/chains.d.ts +2 -2
- package/dist/chunk-4FP4V35O.js +13 -0
- package/dist/{chunk-NWDKXBIP.js → chunk-MRCKUMOS.js} +62 -22
- package/dist/{chunk-MXND5SVN.js → chunk-S7OKGLFD.js} +485 -207
- package/dist/{clientTypes-kkrRdawm.d.ts → clientTypes-BSbwairE.d.cts} +23 -6
- package/dist/{clientTypes-ACVWbrXL.d.cts → clientTypes-DDmcgZ0a.d.ts} +23 -6
- package/dist/core.cjs +561 -244
- package/dist/core.d.cts +24 -6
- package/dist/core.d.ts +24 -6
- package/dist/core.js +3 -2
- package/dist/node.cjs +566 -246
- package/dist/node.d.cts +3 -3
- package/dist/node.d.ts +3 -3
- package/dist/node.js +14 -7
- package/dist/{permit-MZ502UBl.d.cts → permit-DnVMDT5h.d.cts} +34 -4
- package/dist/{permit-MZ502UBl.d.ts → permit-DnVMDT5h.d.ts} +34 -4
- package/dist/permits.cjs +66 -29
- package/dist/permits.d.cts +18 -13
- package/dist/permits.d.ts +18 -13
- package/dist/permits.js +2 -1
- package/dist/web.cjs +604 -256
- package/dist/web.d.cts +8 -4
- package/dist/web.d.ts +8 -4
- package/dist/web.js +49 -14
- package/dist/zkProve.worker.cjs +72 -64
- package/dist/zkProve.worker.js +71 -64
- package/node/index.ts +13 -4
- package/node/test/client.test.ts +25 -0
- package/node/test/config.test.ts +16 -0
- package/node/test/inherited.test.ts +244 -0
- package/node/test/tfheinit.test.ts +56 -0
- package/package.json +24 -22
- package/permits/permit.ts +31 -5
- package/permits/sealing.ts +1 -1
- package/permits/{localstorage.test.ts → test/localstorage.test.ts} +2 -2
- package/permits/{permit.test.ts → test/permit.test.ts} +35 -1
- package/permits/{sealing.test.ts → test/sealing.test.ts} +1 -1
- package/permits/{store.test.ts → test/store.test.ts} +2 -2
- package/permits/{validation.test.ts → test/validation.test.ts} +82 -6
- package/permits/types.ts +1 -1
- package/permits/validation.ts +42 -2
- package/web/const.ts +2 -0
- package/web/index.ts +40 -11
- package/web/storage.ts +18 -3
- package/web/{client.web.test.ts → test/client.web.test.ts} +13 -1
- package/web/test/config.web.test.ts +16 -0
- package/web/test/inherited.web.test.ts +245 -0
- package/web/test/tfheinit.web.test.ts +62 -0
- package/web/{worker.config.web.test.ts → test/worker.config.web.test.ts} +1 -1
- package/web/{worker.output.web.test.ts → test/worker.output.web.test.ts} +1 -1
- package/web/{workerManager.test.ts → test/workerManager.test.ts} +1 -1
- package/web/{workerManager.web.test.ts → test/workerManager.web.test.ts} +1 -1
- package/web/zkProve.worker.ts +94 -84
- package/node/client.test.ts +0 -147
- package/node/config.test.ts +0 -68
- package/node/encryptInputs.test.ts +0 -155
- package/web/config.web.test.ts +0 -69
- package/web/encryptInputs.web.test.ts +0 -172
- package/web/worker.builder.web.test.ts +0 -148
- /package/dist/{types-YiAC4gig.d.cts → types-C07FK-cL.d.cts} +0 -0
- /package/dist/{types-YiAC4gig.d.ts → types-C07FK-cL.d.ts} +0 -0
package/dist/web.d.cts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { C as CofheInputConfig, a as CofheConfig, b as CofheClient, E as EncryptableItem } from './clientTypes-
|
|
1
|
+
import { I as IStorage, C as CofheInputConfig, a as CofheConfig, b as CofheClient, E as EncryptableItem } from './clientTypes-BSbwairE.cjs';
|
|
2
2
|
import 'viem';
|
|
3
|
-
import './types-
|
|
3
|
+
import './types-C07FK-cL.cjs';
|
|
4
4
|
import 'zod';
|
|
5
|
-
import './permit-
|
|
5
|
+
import './permit-DnVMDT5h.cjs';
|
|
6
6
|
import 'zustand/vanilla';
|
|
7
7
|
|
|
8
|
+
declare function createSsrStorage(): IStorage;
|
|
9
|
+
|
|
8
10
|
/**
|
|
9
11
|
* Terminate the worker
|
|
10
12
|
*/
|
|
@@ -14,6 +16,8 @@ declare function terminateWorker(): void;
|
|
|
14
16
|
*/
|
|
15
17
|
declare function areWorkersAvailable(): boolean;
|
|
16
18
|
|
|
19
|
+
declare const hasDOM: boolean;
|
|
20
|
+
|
|
17
21
|
/**
|
|
18
22
|
* Creates a CoFHE configuration for web with IndexedDB storage as default
|
|
19
23
|
* @param config - The CoFHE input configuration (fheKeyStorage will default to IndexedDB if not provided)
|
|
@@ -35,4 +39,4 @@ declare function createCofheClient<TConfig extends CofheConfig>(config: TConfig)
|
|
|
35
39
|
*/
|
|
36
40
|
declare function createCofheClientWithCustomWorker(config: CofheConfig, customZkProveWorkerFn: (fheKeyHex: string, crsHex: string, items: EncryptableItem[], metadata: Uint8Array) => Promise<Uint8Array>): CofheClient;
|
|
37
41
|
|
|
38
|
-
export { areWorkersAvailable, createCofheClient, createCofheClientWithCustomWorker, createCofheConfig, terminateWorker };
|
|
42
|
+
export { areWorkersAvailable, createCofheClient, createCofheClientWithCustomWorker, createCofheConfig, createSsrStorage, hasDOM, terminateWorker };
|
package/dist/web.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { C as CofheInputConfig, a as CofheConfig, b as CofheClient, E as EncryptableItem } from './clientTypes-
|
|
1
|
+
import { I as IStorage, C as CofheInputConfig, a as CofheConfig, b as CofheClient, E as EncryptableItem } from './clientTypes-DDmcgZ0a.js';
|
|
2
2
|
import 'viem';
|
|
3
|
-
import './types-
|
|
3
|
+
import './types-C07FK-cL.js';
|
|
4
4
|
import 'zod';
|
|
5
|
-
import './permit-
|
|
5
|
+
import './permit-DnVMDT5h.js';
|
|
6
6
|
import 'zustand/vanilla';
|
|
7
7
|
|
|
8
|
+
declare function createSsrStorage(): IStorage;
|
|
9
|
+
|
|
8
10
|
/**
|
|
9
11
|
* Terminate the worker
|
|
10
12
|
*/
|
|
@@ -14,6 +16,8 @@ declare function terminateWorker(): void;
|
|
|
14
16
|
*/
|
|
15
17
|
declare function areWorkersAvailable(): boolean;
|
|
16
18
|
|
|
19
|
+
declare const hasDOM: boolean;
|
|
20
|
+
|
|
17
21
|
/**
|
|
18
22
|
* Creates a CoFHE configuration for web with IndexedDB storage as default
|
|
19
23
|
* @param config - The CoFHE input configuration (fheKeyStorage will default to IndexedDB if not provided)
|
|
@@ -35,4 +39,4 @@ declare function createCofheClient<TConfig extends CofheConfig>(config: TConfig)
|
|
|
35
39
|
*/
|
|
36
40
|
declare function createCofheClientWithCustomWorker(config: CofheConfig, customZkProveWorkerFn: (fheKeyHex: string, crsHex: string, items: EncryptableItem[], metadata: Uint8Array) => Promise<Uint8Array>): CofheClient;
|
|
37
41
|
|
|
38
|
-
export { areWorkersAvailable, createCofheClient, createCofheClientWithCustomWorker, createCofheConfig, terminateWorker };
|
|
42
|
+
export { areWorkersAvailable, createCofheClient, createCofheClientWithCustomWorker, createCofheConfig, createSsrStorage, hasDOM, terminateWorker };
|
package/dist/web.js
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
|
-
import { createCofheConfigBase, createCofheClientBase, fheTypeToString } from './chunk-
|
|
1
|
+
import { createCofheConfigBase, createCofheClientBase, fheTypeToString } from './chunk-S7OKGLFD.js';
|
|
2
2
|
import './chunk-TBLR7NNE.js';
|
|
3
|
-
import './chunk-
|
|
3
|
+
import './chunk-MRCKUMOS.js';
|
|
4
|
+
import { TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT } from './chunk-4FP4V35O.js';
|
|
4
5
|
import { constructClient } from 'iframe-shared-storage';
|
|
5
|
-
import init, { init_panic_hook, TfheCompactPublicKey, CompactPkeCrs, ProvenCompactCiphertextList } from 'tfhe';
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
// web/const.ts
|
|
8
|
+
var hasDOM = typeof globalThis?.document !== "undefined" && typeof globalThis?.window !== "undefined";
|
|
9
|
+
|
|
10
|
+
// web/storage.ts
|
|
11
|
+
var createWebStorage = (opts = { enableLog: false }) => {
|
|
12
|
+
if (!hasDOM)
|
|
13
|
+
throw new Error("createWebStorage can only be used in a browser environment");
|
|
8
14
|
const client = constructClient({
|
|
9
15
|
iframe: {
|
|
10
16
|
src: "https://iframe-shared-storage.vercel.app/hub.html",
|
|
11
17
|
messagingOptions: {
|
|
12
|
-
enableLog: "both"
|
|
18
|
+
enableLog: opts.enableLog ? "both" : void 0
|
|
13
19
|
},
|
|
14
20
|
iframeReadyTimeoutMs: 3e4,
|
|
15
21
|
// if the iframe is not initied during this interval AND a reuqest is made, such request will throw an error
|
|
@@ -32,6 +38,16 @@ var createWebStorage = () => {
|
|
|
32
38
|
}
|
|
33
39
|
};
|
|
34
40
|
};
|
|
41
|
+
function createSsrStorage() {
|
|
42
|
+
console.warn("using no-op server-side SSR storage");
|
|
43
|
+
return {
|
|
44
|
+
getItem: async () => null,
|
|
45
|
+
setItem: async () => {
|
|
46
|
+
},
|
|
47
|
+
removeItem: async () => {
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
35
51
|
|
|
36
52
|
// web/workerManager.ts
|
|
37
53
|
var ZkProveWorkerManager = class {
|
|
@@ -171,15 +187,25 @@ function terminateWorker() {
|
|
|
171
187
|
function areWorkersAvailable() {
|
|
172
188
|
return typeof Worker !== "undefined";
|
|
173
189
|
}
|
|
190
|
+
|
|
191
|
+
// web/index.ts
|
|
192
|
+
var tfheModule = null;
|
|
174
193
|
var tfheInitialized = false;
|
|
175
194
|
async function initTfhe() {
|
|
176
195
|
if (tfheInitialized)
|
|
177
196
|
return false;
|
|
178
|
-
await
|
|
179
|
-
await
|
|
197
|
+
tfheModule = await import('tfhe');
|
|
198
|
+
await tfheModule.default();
|
|
199
|
+
await tfheModule.init_panic_hook();
|
|
180
200
|
tfheInitialized = true;
|
|
181
201
|
return true;
|
|
182
202
|
}
|
|
203
|
+
function requireTfhe() {
|
|
204
|
+
if (!tfheModule) {
|
|
205
|
+
throw new Error("TFHE not initialized \u2014 call initTfhe() (or any client method that triggers it) first");
|
|
206
|
+
}
|
|
207
|
+
return tfheModule;
|
|
208
|
+
}
|
|
183
209
|
var fromHexString = (hexString) => {
|
|
184
210
|
const cleanString = hexString.length % 2 === 1 ? `0${hexString}` : hexString;
|
|
185
211
|
const arr = cleanString.replace(/^0x/, "").match(/.{1,2}/g);
|
|
@@ -187,16 +213,25 @@ var fromHexString = (hexString) => {
|
|
|
187
213
|
return new Uint8Array();
|
|
188
214
|
return new Uint8Array(arr.map((byte) => parseInt(byte, 16)));
|
|
189
215
|
};
|
|
216
|
+
var _deserializeTfhePublicKey = (buff) => {
|
|
217
|
+
return requireTfhe().TfheCompactPublicKey.safe_deserialize(
|
|
218
|
+
fromHexString(buff),
|
|
219
|
+
TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT
|
|
220
|
+
);
|
|
221
|
+
};
|
|
222
|
+
var _deserializeCompactPkeCrs = (buff) => {
|
|
223
|
+
return requireTfhe().CompactPkeCrs.safe_deserialize(fromHexString(buff), TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT);
|
|
224
|
+
};
|
|
190
225
|
var tfhePublicKeyDeserializer = (buff) => {
|
|
191
|
-
|
|
226
|
+
_deserializeTfhePublicKey(buff);
|
|
192
227
|
};
|
|
193
228
|
var compactPkeCrsDeserializer = (buff) => {
|
|
194
|
-
|
|
229
|
+
_deserializeCompactPkeCrs(buff);
|
|
195
230
|
};
|
|
196
231
|
var zkBuilderAndCrsGenerator = (fhe, crs) => {
|
|
197
|
-
const fhePublicKey =
|
|
198
|
-
const zkBuilder = ProvenCompactCiphertextList.builder(fhePublicKey);
|
|
199
|
-
const zkCrs =
|
|
232
|
+
const fhePublicKey = _deserializeTfhePublicKey(fhe);
|
|
233
|
+
const zkBuilder = requireTfhe().ProvenCompactCiphertextList.builder(fhePublicKey);
|
|
234
|
+
const zkCrs = _deserializeCompactPkeCrs(crs);
|
|
200
235
|
return { zkBuilder, zkCrs };
|
|
201
236
|
};
|
|
202
237
|
async function zkProveWithWorker(fheKeyHex, crsHex, items, metadata) {
|
|
@@ -211,7 +246,7 @@ function createCofheConfig(config) {
|
|
|
211
246
|
return createCofheConfigBase({
|
|
212
247
|
environment: "web",
|
|
213
248
|
...config,
|
|
214
|
-
fheKeyStorage: config.fheKeyStorage === null ? null : config.fheKeyStorage ?? createWebStorage()
|
|
249
|
+
fheKeyStorage: config.fheKeyStorage === null ? null : config.fheKeyStorage ?? (hasDOM ? createWebStorage() : createSsrStorage())
|
|
215
250
|
});
|
|
216
251
|
}
|
|
217
252
|
function createCofheClient(config) {
|
|
@@ -237,4 +272,4 @@ function createCofheClientWithCustomWorker(config, customZkProveWorkerFn) {
|
|
|
237
272
|
});
|
|
238
273
|
}
|
|
239
274
|
|
|
240
|
-
export { areWorkersAvailable, createCofheClient, createCofheClientWithCustomWorker, createCofheConfig, terminateWorker };
|
|
275
|
+
export { areWorkersAvailable, createCofheClient, createCofheClientWithCustomWorker, createCofheConfig, createSsrStorage, hasDOM, terminateWorker };
|
package/dist/zkProve.worker.cjs
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
// core/consts.ts
|
|
4
|
+
var TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT = BigInt(1 << 30);
|
|
5
|
+
|
|
3
6
|
// web/zkProve.worker.ts
|
|
4
7
|
var tfheModule = null;
|
|
5
8
|
var initialized = false;
|
|
@@ -24,70 +27,75 @@ function fromHexString(hexString) {
|
|
|
24
27
|
return new Uint8Array();
|
|
25
28
|
return new Uint8Array(arr.map((byte) => parseInt(byte, 16)));
|
|
26
29
|
}
|
|
27
|
-
self
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
try {
|
|
38
|
-
await initTfhe();
|
|
39
|
-
if (!tfheModule) {
|
|
40
|
-
throw new Error("TFHE module not initialized");
|
|
30
|
+
if (typeof self !== "undefined") {
|
|
31
|
+
self.onmessage = async (event) => {
|
|
32
|
+
const { id, type, fheKeyHex, crsHex, items, metadata } = event.data;
|
|
33
|
+
if (type !== "zkProve") {
|
|
34
|
+
self.postMessage({
|
|
35
|
+
id,
|
|
36
|
+
type: "error",
|
|
37
|
+
error: "Invalid message type"
|
|
38
|
+
});
|
|
39
|
+
return;
|
|
41
40
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const builder = tfheModule.ProvenCompactCiphertextList.builder(fheKey);
|
|
47
|
-
for (const item of items) {
|
|
48
|
-
switch (item.utype) {
|
|
49
|
-
case "bool":
|
|
50
|
-
builder.push_boolean(Boolean(item.data));
|
|
51
|
-
break;
|
|
52
|
-
case "uint8":
|
|
53
|
-
builder.push_u8(Number(item.data));
|
|
54
|
-
break;
|
|
55
|
-
case "uint16":
|
|
56
|
-
builder.push_u16(Number(item.data));
|
|
57
|
-
break;
|
|
58
|
-
case "uint32":
|
|
59
|
-
builder.push_u32(Number(item.data));
|
|
60
|
-
break;
|
|
61
|
-
case "uint64":
|
|
62
|
-
builder.push_u64(BigInt(item.data));
|
|
63
|
-
break;
|
|
64
|
-
case "uint128":
|
|
65
|
-
builder.push_u128(BigInt(item.data));
|
|
66
|
-
break;
|
|
67
|
-
case "uint160":
|
|
68
|
-
builder.push_u160(BigInt(item.data));
|
|
69
|
-
break;
|
|
70
|
-
default:
|
|
71
|
-
throw new Error(`Unsupported type: ${item.utype}`);
|
|
41
|
+
try {
|
|
42
|
+
await initTfhe();
|
|
43
|
+
if (!tfheModule) {
|
|
44
|
+
throw new Error("TFHE module not initialized");
|
|
72
45
|
}
|
|
46
|
+
const fheKeyBytes = fromHexString(fheKeyHex);
|
|
47
|
+
const crsBytes = fromHexString(crsHex);
|
|
48
|
+
const fheKey = tfheModule.TfheCompactPublicKey.safe_deserialize(
|
|
49
|
+
fheKeyBytes,
|
|
50
|
+
TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT
|
|
51
|
+
);
|
|
52
|
+
const crs = tfheModule.CompactPkeCrs.safe_deserialize(crsBytes, TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT);
|
|
53
|
+
const builder = tfheModule.ProvenCompactCiphertextList.builder(fheKey);
|
|
54
|
+
for (const item of items) {
|
|
55
|
+
switch (item.utype) {
|
|
56
|
+
case "bool":
|
|
57
|
+
builder.push_boolean(Boolean(item.data));
|
|
58
|
+
break;
|
|
59
|
+
case "uint8":
|
|
60
|
+
builder.push_u8(Number(item.data));
|
|
61
|
+
break;
|
|
62
|
+
case "uint16":
|
|
63
|
+
builder.push_u16(Number(item.data));
|
|
64
|
+
break;
|
|
65
|
+
case "uint32":
|
|
66
|
+
builder.push_u32(Number(item.data));
|
|
67
|
+
break;
|
|
68
|
+
case "uint64":
|
|
69
|
+
builder.push_u64(BigInt(item.data));
|
|
70
|
+
break;
|
|
71
|
+
case "uint128":
|
|
72
|
+
builder.push_u128(BigInt(item.data));
|
|
73
|
+
break;
|
|
74
|
+
case "uint160":
|
|
75
|
+
builder.push_u160(BigInt(item.data));
|
|
76
|
+
break;
|
|
77
|
+
default:
|
|
78
|
+
throw new Error(`Unsupported type: ${item.utype}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const metadataBytes = new Uint8Array(metadata);
|
|
82
|
+
const compactList = builder.build_with_proof_packed(crs, metadataBytes, 1);
|
|
83
|
+
const result = compactList.safe_serialize(TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT);
|
|
84
|
+
self.postMessage({
|
|
85
|
+
id,
|
|
86
|
+
type: "success",
|
|
87
|
+
result: Array.from(result)
|
|
88
|
+
});
|
|
89
|
+
} catch (error) {
|
|
90
|
+
self.postMessage({
|
|
91
|
+
id,
|
|
92
|
+
type: "error",
|
|
93
|
+
error: error instanceof Error ? error.message : String(error)
|
|
94
|
+
});
|
|
73
95
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
result: Array.from(result)
|
|
81
|
-
});
|
|
82
|
-
} catch (error) {
|
|
83
|
-
self.postMessage({
|
|
84
|
-
id,
|
|
85
|
-
type: "error",
|
|
86
|
-
error: error instanceof Error ? error.message : String(error)
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
self.postMessage({
|
|
91
|
-
id: "init",
|
|
92
|
-
type: "ready"
|
|
93
|
-
});
|
|
96
|
+
};
|
|
97
|
+
self.postMessage({
|
|
98
|
+
id: "init",
|
|
99
|
+
type: "ready"
|
|
100
|
+
});
|
|
101
|
+
}
|
package/dist/zkProve.worker.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT } from './chunk-4FP4V35O.js';
|
|
2
|
+
|
|
1
3
|
// web/zkProve.worker.ts
|
|
2
4
|
var tfheModule = null;
|
|
3
5
|
var initialized = false;
|
|
@@ -22,70 +24,75 @@ function fromHexString(hexString) {
|
|
|
22
24
|
return new Uint8Array();
|
|
23
25
|
return new Uint8Array(arr.map((byte) => parseInt(byte, 16)));
|
|
24
26
|
}
|
|
25
|
-
self
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
try {
|
|
36
|
-
await initTfhe();
|
|
37
|
-
if (!tfheModule) {
|
|
38
|
-
throw new Error("TFHE module not initialized");
|
|
27
|
+
if (typeof self !== "undefined") {
|
|
28
|
+
self.onmessage = async (event) => {
|
|
29
|
+
const { id, type, fheKeyHex, crsHex, items, metadata } = event.data;
|
|
30
|
+
if (type !== "zkProve") {
|
|
31
|
+
self.postMessage({
|
|
32
|
+
id,
|
|
33
|
+
type: "error",
|
|
34
|
+
error: "Invalid message type"
|
|
35
|
+
});
|
|
36
|
+
return;
|
|
39
37
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
38
|
+
try {
|
|
39
|
+
await initTfhe();
|
|
40
|
+
if (!tfheModule) {
|
|
41
|
+
throw new Error("TFHE module not initialized");
|
|
42
|
+
}
|
|
43
|
+
const fheKeyBytes = fromHexString(fheKeyHex);
|
|
44
|
+
const crsBytes = fromHexString(crsHex);
|
|
45
|
+
const fheKey = tfheModule.TfheCompactPublicKey.safe_deserialize(
|
|
46
|
+
fheKeyBytes,
|
|
47
|
+
TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT
|
|
48
|
+
);
|
|
49
|
+
const crs = tfheModule.CompactPkeCrs.safe_deserialize(crsBytes, TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT);
|
|
50
|
+
const builder = tfheModule.ProvenCompactCiphertextList.builder(fheKey);
|
|
51
|
+
for (const item of items) {
|
|
52
|
+
switch (item.utype) {
|
|
53
|
+
case "bool":
|
|
54
|
+
builder.push_boolean(Boolean(item.data));
|
|
55
|
+
break;
|
|
56
|
+
case "uint8":
|
|
57
|
+
builder.push_u8(Number(item.data));
|
|
58
|
+
break;
|
|
59
|
+
case "uint16":
|
|
60
|
+
builder.push_u16(Number(item.data));
|
|
61
|
+
break;
|
|
62
|
+
case "uint32":
|
|
63
|
+
builder.push_u32(Number(item.data));
|
|
64
|
+
break;
|
|
65
|
+
case "uint64":
|
|
66
|
+
builder.push_u64(BigInt(item.data));
|
|
67
|
+
break;
|
|
68
|
+
case "uint128":
|
|
69
|
+
builder.push_u128(BigInt(item.data));
|
|
70
|
+
break;
|
|
71
|
+
case "uint160":
|
|
72
|
+
builder.push_u160(BigInt(item.data));
|
|
73
|
+
break;
|
|
74
|
+
default:
|
|
75
|
+
throw new Error(`Unsupported type: ${item.utype}`);
|
|
76
|
+
}
|
|
70
77
|
}
|
|
78
|
+
const metadataBytes = new Uint8Array(metadata);
|
|
79
|
+
const compactList = builder.build_with_proof_packed(crs, metadataBytes, 1);
|
|
80
|
+
const result = compactList.safe_serialize(TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT);
|
|
81
|
+
self.postMessage({
|
|
82
|
+
id,
|
|
83
|
+
type: "success",
|
|
84
|
+
result: Array.from(result)
|
|
85
|
+
});
|
|
86
|
+
} catch (error) {
|
|
87
|
+
self.postMessage({
|
|
88
|
+
id,
|
|
89
|
+
type: "error",
|
|
90
|
+
error: error instanceof Error ? error.message : String(error)
|
|
91
|
+
});
|
|
71
92
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
result: Array.from(result)
|
|
79
|
-
});
|
|
80
|
-
} catch (error) {
|
|
81
|
-
self.postMessage({
|
|
82
|
-
id,
|
|
83
|
-
type: "error",
|
|
84
|
-
error: error instanceof Error ? error.message : String(error)
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
self.postMessage({
|
|
89
|
-
id: "init",
|
|
90
|
-
type: "ready"
|
|
91
|
-
});
|
|
93
|
+
};
|
|
94
|
+
self.postMessage({
|
|
95
|
+
id: "init",
|
|
96
|
+
type: "ready"
|
|
97
|
+
});
|
|
98
|
+
}
|
package/node/index.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
type CofheInputConfig,
|
|
9
9
|
type ZkBuilderAndCrsGenerator,
|
|
10
10
|
type FheKeyDeserializer,
|
|
11
|
+
TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT,
|
|
11
12
|
} from '@/core';
|
|
12
13
|
|
|
13
14
|
// Import node-specific storage (internal use only)
|
|
@@ -39,12 +40,20 @@ const fromHexString = (hexString: string): Uint8Array => {
|
|
|
39
40
|
return new Uint8Array(arr.map((byte) => parseInt(byte, 16)));
|
|
40
41
|
};
|
|
41
42
|
|
|
43
|
+
const _deserializeTfhePublicKey = (buff: string): TfheCompactPublicKey => {
|
|
44
|
+
return TfheCompactPublicKey.safe_deserialize(fromHexString(buff), TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const _deserializeCompactPkeCrs = (buff: string): CompactPkeCrs => {
|
|
48
|
+
return CompactPkeCrs.safe_deserialize(fromHexString(buff), TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT);
|
|
49
|
+
};
|
|
50
|
+
|
|
42
51
|
/**
|
|
43
52
|
* Serializer for TFHE public keys
|
|
44
53
|
* Validates that the buffer can be deserialized into a TfheCompactPublicKey
|
|
45
54
|
*/
|
|
46
55
|
const tfhePublicKeyDeserializer: FheKeyDeserializer = (buff: string): void => {
|
|
47
|
-
|
|
56
|
+
_deserializeTfhePublicKey(buff);
|
|
48
57
|
};
|
|
49
58
|
|
|
50
59
|
/**
|
|
@@ -52,7 +61,7 @@ const tfhePublicKeyDeserializer: FheKeyDeserializer = (buff: string): void => {
|
|
|
52
61
|
* Validates that the buffer can be deserialized into ZkCompactPkePublicParams
|
|
53
62
|
*/
|
|
54
63
|
const compactPkeCrsDeserializer: FheKeyDeserializer = (buff: string): void => {
|
|
55
|
-
|
|
64
|
+
_deserializeCompactPkeCrs(buff);
|
|
56
65
|
};
|
|
57
66
|
|
|
58
67
|
/**
|
|
@@ -60,9 +69,9 @@ const compactPkeCrsDeserializer: FheKeyDeserializer = (buff: string): void => {
|
|
|
60
69
|
* This is used internally by the SDK to create encrypted inputs
|
|
61
70
|
*/
|
|
62
71
|
const zkBuilderAndCrsGenerator: ZkBuilderAndCrsGenerator = (fhe: string, crs: string) => {
|
|
63
|
-
const fhePublicKey =
|
|
72
|
+
const fhePublicKey = _deserializeTfhePublicKey(fhe);
|
|
64
73
|
const zkBuilder = ProvenCompactCiphertextList.builder(fhePublicKey);
|
|
65
|
-
const zkCrs =
|
|
74
|
+
const zkCrs = _deserializeCompactPkeCrs(crs);
|
|
66
75
|
|
|
67
76
|
return { zkBuilder, zkCrs };
|
|
68
77
|
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type CofheClient } from '@/core';
|
|
2
|
+
import { arbSepolia as cofheArbSepolia } from '@/chains';
|
|
3
|
+
|
|
4
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
5
|
+
import { createCofheClient, createCofheConfig } from '../index.js';
|
|
6
|
+
|
|
7
|
+
describe('@cofhe/node - Client', () => {
|
|
8
|
+
let cofheClient: CofheClient;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
const config = createCofheConfig({
|
|
12
|
+
supportedChains: [cofheArbSepolia],
|
|
13
|
+
});
|
|
14
|
+
cofheClient = createCofheClient(config);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('should automatically use filesystem storage as default', () => {
|
|
18
|
+
expect(cofheClient.config.fheKeyStorage).toBeDefined();
|
|
19
|
+
expect(cofheClient.config.fheKeyStorage).not.toBeNull();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('should have the correct environment', () => {
|
|
23
|
+
expect(cofheClient.config.environment).toBe('node');
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { arbSepolia } from '@/chains';
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect } from 'vitest';
|
|
4
|
+
import { createCofheConfig } from '../index.js';
|
|
5
|
+
|
|
6
|
+
describe('@cofhe/node - Config', () => {
|
|
7
|
+
it('should automatically inject filesystem storage as default', () => {
|
|
8
|
+
const config = createCofheConfig({
|
|
9
|
+
supportedChains: [arbSepolia],
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
expect(config.fheKeyStorage).toBeDefined();
|
|
13
|
+
expect(config.fheKeyStorage).not.toBeNull();
|
|
14
|
+
expect(config.supportedChains).toEqual([arbSepolia]);
|
|
15
|
+
});
|
|
16
|
+
});
|