@luxfhe/wasm 0.1.0 → 0.1.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/README.md ADDED
@@ -0,0 +1,198 @@
1
+ # @luxfi/tfhe-wasm
2
+
3
+ WebAssembly bindings for the Lux TFHE (Fully Homomorphic Encryption) library. Enables encrypted computation directly in the browser.
4
+
5
+ ## Features
6
+
7
+ - **Fully Homomorphic Encryption**: Compute on encrypted data without decrypting
8
+ - **Boolean Gates**: AND, OR, XOR, NOT, NAND, NOR, XNOR, MUX
9
+ - **Integer Operations**: Add, subtract, multiply, compare encrypted uint8 values
10
+ - **Browser-Ready**: Pure WebAssembly, works in all modern browsers
11
+ - **TypeScript**: Full type definitions included
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install @luxfi/tfhe-wasm
17
+ ```
18
+
19
+ ## Quick Start
20
+
21
+ ```typescript
22
+ import { loadWasm, createTFHE } from '@luxfi/tfhe-wasm';
23
+
24
+ // Load WASM module
25
+ await loadWasm('./node_modules/@luxfi/tfhe-wasm/pkg/tfhe.wasm');
26
+
27
+ // Create API instance
28
+ const tfhe = createTFHE();
29
+
30
+ // Initialize
31
+ await tfhe.init();
32
+
33
+ // Generate keys
34
+ const keys = await tfhe.generateKeys();
35
+ console.log('Keys generated');
36
+
37
+ // Encrypt values
38
+ const ct1 = await tfhe.encrypt(true);
39
+ const ct2 = await tfhe.encrypt(false);
40
+
41
+ // Compute on encrypted data
42
+ const result = await tfhe.and(ct1, ct2);
43
+
44
+ // Decrypt result
45
+ const decrypted = await tfhe.decrypt(result);
46
+ console.log('true AND false =', decrypted); // false
47
+ ```
48
+
49
+ ## API Reference
50
+
51
+ ### Initialization
52
+
53
+ ```typescript
54
+ // Load WASM (required first)
55
+ await loadWasm(wasmPath: string): Promise<void>
56
+
57
+ // Create TFHE instance
58
+ const tfhe = createTFHE(): TFHE
59
+
60
+ // Initialize TFHE parameters
61
+ await tfhe.init(): Promise<void>
62
+ ```
63
+
64
+ ### Key Management
65
+
66
+ ```typescript
67
+ // Generate new key pair
68
+ const keys = await tfhe.generateKeys(): Promise<KeyPair>
69
+ // keys.secretKey: base64-encoded secret key
70
+ // keys.publicKey: base64-encoded public key
71
+
72
+ // Load existing secret key
73
+ await tfhe.loadSecretKey(secretKey: string): Promise<void>
74
+ ```
75
+
76
+ ### Boolean Encryption
77
+
78
+ ```typescript
79
+ // Encrypt boolean
80
+ const ct = await tfhe.encrypt(value: boolean): Promise<Ciphertext>
81
+
82
+ // Decrypt to boolean
83
+ const value = await tfhe.decrypt(ct: Ciphertext): Promise<boolean>
84
+ ```
85
+
86
+ ### Boolean Operations
87
+
88
+ ```typescript
89
+ await tfhe.and(ct1, ct2): Promise<Ciphertext>
90
+ await tfhe.or(ct1, ct2): Promise<Ciphertext>
91
+ await tfhe.xor(ct1, ct2): Promise<Ciphertext>
92
+ await tfhe.not(ct): Promise<Ciphertext>
93
+ await tfhe.nand(ct1, ct2): Promise<Ciphertext>
94
+ await tfhe.nor(ct1, ct2): Promise<Ciphertext>
95
+ await tfhe.xnor(ct1, ct2): Promise<Ciphertext>
96
+ await tfhe.mux(sel, ctTrue, ctFalse): Promise<Ciphertext>
97
+ ```
98
+
99
+ ### Integer Encryption (uint8)
100
+
101
+ ```typescript
102
+ // Encrypt 8-bit integer (0-255)
103
+ const ct = await tfhe.encryptUint8(value: number): Promise<CiphertextUint8>
104
+
105
+ // Decrypt to 8-bit integer
106
+ const value = await tfhe.decryptUint8(ct: CiphertextUint8): Promise<number>
107
+ ```
108
+
109
+ ### Integer Operations
110
+
111
+ ```typescript
112
+ // Addition (a + b) mod 256
113
+ await tfhe.add(a, b): Promise<CiphertextUint8>
114
+
115
+ // Subtraction (a - b) mod 256
116
+ await tfhe.sub(a, b): Promise<CiphertextUint8>
117
+
118
+ // Multiplication (a * b) mod 256
119
+ await tfhe.mul(a, b): Promise<CiphertextUint8>
120
+
121
+ // Comparison (a < b)
122
+ await tfhe.compare(a, b): Promise<Ciphertext>
123
+ ```
124
+
125
+ ## Browser Usage
126
+
127
+ Include the WASM support files in your HTML:
128
+
129
+ ```html
130
+ <script src="wasm_exec.js"></script>
131
+ <script type="module">
132
+ import { loadWasm, createTFHE } from '@luxfi/tfhe-wasm';
133
+
134
+ const tfhe = createTFHE();
135
+ await loadWasm('./tfhe.wasm');
136
+ await tfhe.init();
137
+
138
+ // Use tfhe...
139
+ </script>
140
+ ```
141
+
142
+ ## Building from Source
143
+
144
+ Requirements:
145
+ - Go 1.23+
146
+ - Node.js 18+
147
+
148
+ ```bash
149
+ # Clone repository
150
+ git clone https://github.com/luxfi/tfhe-wasm
151
+ cd tfhe-wasm
152
+
153
+ # Install dependencies
154
+ make install
155
+
156
+ # Build WASM and TypeScript
157
+ make build
158
+
159
+ # Run tests
160
+ make test
161
+
162
+ # Serve example
163
+ make serve
164
+ ```
165
+
166
+ ## Performance Notes
167
+
168
+ TFHE is computationally intensive. Expected timings on modern hardware:
169
+
170
+ | Operation | Time |
171
+ |-----------|------|
172
+ | Key Generation | 5-30s |
173
+ | Boolean Gate | 10-50ms |
174
+ | 8-bit Addition | 500ms-2s |
175
+ | 8-bit Comparison | 1-5s |
176
+
177
+ For better performance, consider:
178
+ - Using Web Workers to avoid blocking the UI
179
+ - Caching encrypted values
180
+ - Batching operations where possible
181
+
182
+ ## Security
183
+
184
+ This library provides ~128-bit security using the PN10QP27 parameter set:
185
+ - LWE dimension: N=512
186
+ - Modulus: Q=12289
187
+
188
+ Keys should be treated as sensitive data. The secret key must never be exposed to untrusted parties.
189
+
190
+ ## License
191
+
192
+ BSD-3-Clause
193
+
194
+ ## Related
195
+
196
+ - [luxfi/tfhe](https://github.com/luxfi/tfhe) - Go TFHE library
197
+ - [luxfi/lattice](https://github.com/luxfi/lattice) - Lattice cryptography primitives
198
+ - [TFHE Paper](https://eprint.iacr.org/2018/421) - Original TFHE research
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @luxfi/tfhe-wasm
3
+ *
4
+ * WebAssembly bindings for the Lux TFHE (Fully Homomorphic Encryption) library.
5
+ * Enables encrypted computation directly in the browser.
6
+ */
7
+ import type { TFHE } from './types';
8
+ export * from './types';
9
+ export { loadWasm, isLoaded } from './init';
10
+ /**
11
+ * TFHEError is thrown when a TFHE operation fails.
12
+ */
13
+ export declare class TFHEError extends Error {
14
+ constructor(message: string);
15
+ }
16
+ /**
17
+ * Create a high-level TFHE instance.
18
+ * This wraps the raw WASM functions with proper error handling.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * import { loadWasm, createTFHE } from '@luxfi/tfhe-wasm';
23
+ *
24
+ * await loadWasm('./tfhe.wasm');
25
+ * const tfhe = createTFHE();
26
+ *
27
+ * await tfhe.init();
28
+ * const keys = await tfhe.generateKeys();
29
+ *
30
+ * const ct1 = await tfhe.encrypt(true);
31
+ * const ct2 = await tfhe.encrypt(false);
32
+ * const result = await tfhe.and(ct1, ct2);
33
+ * const decrypted = await tfhe.decrypt(result); // false
34
+ * ```
35
+ */
36
+ export declare function createTFHE(): TFHE;
37
+ export default createTFHE;
38
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../js/index.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,IAAI,EAKL,MAAM,SAAS,CAAC;AAEjB,cAAc,SAAS,CAAC;AACxB,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAE5C;;GAEG;AACH,qBAAa,SAAU,SAAQ,KAAK;gBACtB,OAAO,EAAE,MAAM;CAI5B;AAYD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,UAAU,IAAI,IAAI,CA6GjC;AAGD,eAAe,UAAU,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,136 @@
1
+ // Copyright (c) 2025, Lux Industries Inc
2
+ // SPDX-License-Identifier: BSD-3-Clause
3
+ export * from './types';
4
+ export { loadWasm, isLoaded } from './init';
5
+ /**
6
+ * TFHEError is thrown when a TFHE operation fails.
7
+ */
8
+ export class TFHEError extends Error {
9
+ constructor(message) {
10
+ super(message);
11
+ this.name = 'TFHEError';
12
+ }
13
+ }
14
+ /**
15
+ * Helper to unwrap TFHEResult and throw on error.
16
+ */
17
+ function unwrap(result) {
18
+ if (!result.success) {
19
+ throw new TFHEError(result.error || 'Unknown TFHE error');
20
+ }
21
+ return result.data;
22
+ }
23
+ /**
24
+ * Create a high-level TFHE instance.
25
+ * This wraps the raw WASM functions with proper error handling.
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * import { loadWasm, createTFHE } from '@luxfi/tfhe-wasm';
30
+ *
31
+ * await loadWasm('./tfhe.wasm');
32
+ * const tfhe = createTFHE();
33
+ *
34
+ * await tfhe.init();
35
+ * const keys = await tfhe.generateKeys();
36
+ *
37
+ * const ct1 = await tfhe.encrypt(true);
38
+ * const ct2 = await tfhe.encrypt(false);
39
+ * const result = await tfhe.and(ct1, ct2);
40
+ * const decrypted = await tfhe.decrypt(result); // false
41
+ * ```
42
+ */
43
+ export function createTFHE() {
44
+ const checkLoaded = () => {
45
+ if (typeof window === 'undefined' || typeof window.tfhe_init !== 'function') {
46
+ throw new TFHEError('TFHE WASM not loaded. Call loadWasm() first.');
47
+ }
48
+ };
49
+ return {
50
+ async init() {
51
+ checkLoaded();
52
+ unwrap(window.tfhe_init());
53
+ },
54
+ async generateKeys() {
55
+ checkLoaded();
56
+ return unwrap(window.tfhe_generateKeys());
57
+ },
58
+ async loadSecretKey(secretKey) {
59
+ checkLoaded();
60
+ unwrap(window.tfhe_loadSecretKey(secretKey));
61
+ },
62
+ async encrypt(value) {
63
+ checkLoaded();
64
+ return unwrap(window.tfhe_encrypt(value));
65
+ },
66
+ async decrypt(ct) {
67
+ checkLoaded();
68
+ return unwrap(window.tfhe_decrypt(ct));
69
+ },
70
+ async encryptUint8(value) {
71
+ checkLoaded();
72
+ if (value < 0 || value > 255) {
73
+ throw new TFHEError(`Value ${value} out of range for uint8 (0-255)`);
74
+ }
75
+ return unwrap(window.tfhe_encryptUint8(value));
76
+ },
77
+ async decryptUint8(cts) {
78
+ checkLoaded();
79
+ if (cts.length !== 8) {
80
+ throw new TFHEError(`Expected 8 ciphertexts, got ${cts.length}`);
81
+ }
82
+ return unwrap(window.tfhe_decryptUint8(cts));
83
+ },
84
+ async and(ct1, ct2) {
85
+ checkLoaded();
86
+ return unwrap(window.tfhe_and(ct1, ct2));
87
+ },
88
+ async or(ct1, ct2) {
89
+ checkLoaded();
90
+ return unwrap(window.tfhe_or(ct1, ct2));
91
+ },
92
+ async xor(ct1, ct2) {
93
+ checkLoaded();
94
+ return unwrap(window.tfhe_xor(ct1, ct2));
95
+ },
96
+ async not(ct) {
97
+ checkLoaded();
98
+ return unwrap(window.tfhe_not(ct));
99
+ },
100
+ async nand(ct1, ct2) {
101
+ checkLoaded();
102
+ return unwrap(window.tfhe_nand(ct1, ct2));
103
+ },
104
+ async nor(ct1, ct2) {
105
+ checkLoaded();
106
+ return unwrap(window.tfhe_nor(ct1, ct2));
107
+ },
108
+ async xnor(ct1, ct2) {
109
+ checkLoaded();
110
+ return unwrap(window.tfhe_xnor(ct1, ct2));
111
+ },
112
+ async mux(sel, ctTrue, ctFalse) {
113
+ checkLoaded();
114
+ return unwrap(window.tfhe_mux(sel, ctTrue, ctFalse));
115
+ },
116
+ async compare(a, b) {
117
+ checkLoaded();
118
+ return unwrap(window.tfhe_compare(a, b));
119
+ },
120
+ async add(a, b) {
121
+ checkLoaded();
122
+ return unwrap(window.tfhe_addUint8(a, b));
123
+ },
124
+ async sub(a, b) {
125
+ checkLoaded();
126
+ return unwrap(window.tfhe_subUint8(a, b));
127
+ },
128
+ async mul(a, b) {
129
+ checkLoaded();
130
+ return unwrap(window.tfhe_mulUint8(a, b));
131
+ },
132
+ };
133
+ }
134
+ // Default export for convenience
135
+ export default createTFHE;
136
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @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MAAM,CAAC,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;QAC5C,CAAC;QAED,KAAK,CAAC,GAAG,CAAC,CAAkB,EAAE,CAAkB;YAC9C,WAAW,EAAE,CAAC;YACd,OAAO,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;QAC5C,CAAC;QAED,KAAK,CAAC,GAAG,CAAC,CAAkB,EAAE,CAAkB;YAC9C,WAAW,EAAE,CAAC;YACd,OAAO,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;QAC5C,CAAC;KACF,CAAC;AACJ,CAAC;AAED,iCAAiC;AACjC,eAAe,UAAU,CAAC"}
package/dist/init.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Load and instantiate the Go WASM runtime.
3
+ * This must be called before using any TFHE functions.
4
+ *
5
+ * @param wasmPath Path to the tfhe.wasm file
6
+ * @returns Promise that resolves when WASM is loaded
7
+ */
8
+ export declare function loadWasm(wasmPath?: string): Promise<void>;
9
+ /**
10
+ * Check if TFHE WASM is loaded.
11
+ */
12
+ export declare function isLoaded(): boolean;
13
+ //# sourceMappingURL=init.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../js/init.ts"],"names":[],"mappings":"AAKA;;;;;;GAMG;AACH,wBAAsB,QAAQ,CAAC,QAAQ,GAAE,MAAsB,GAAG,OAAO,CAAC,IAAI,CAAC,CAgC9E;AAmBD;;GAEG;AACH,wBAAgB,QAAQ,IAAI,OAAO,CAElC"}
package/dist/init.js ADDED
@@ -0,0 +1,54 @@
1
+ // Copyright (c) 2025, Lux Industries Inc
2
+ // SPDX-License-Identifier: BSD-3-Clause
3
+ /**
4
+ * Load and instantiate the Go WASM runtime.
5
+ * This must be called before using any TFHE functions.
6
+ *
7
+ * @param wasmPath Path to the tfhe.wasm file
8
+ * @returns Promise that resolves when WASM is loaded
9
+ */
10
+ export async function loadWasm(wasmPath = './tfhe.wasm') {
11
+ // Check if Go runtime is available
12
+ if (typeof window === 'undefined' || typeof window.Go === 'undefined') {
13
+ throw new Error('Go runtime not found. Make sure to include wasm_exec.js before calling loadWasm().');
14
+ }
15
+ const go = new window.Go();
16
+ let wasmInstance;
17
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
18
+ // Modern browsers support streaming instantiation
19
+ const result = await WebAssembly.instantiateStreaming(fetch(wasmPath), go.importObject);
20
+ wasmInstance = result.instance;
21
+ }
22
+ else {
23
+ // Fallback for older browsers
24
+ const response = await fetch(wasmPath);
25
+ const bytes = await response.arrayBuffer();
26
+ const result = await WebAssembly.instantiate(bytes, go.importObject);
27
+ wasmInstance = result.instance;
28
+ }
29
+ // Run the Go program (non-blocking, sets up exports)
30
+ go.run(wasmInstance);
31
+ // Wait for TFHE to be ready
32
+ await waitForTFHE();
33
+ }
34
+ /**
35
+ * Wait for TFHE functions to be available.
36
+ * The Go WASM module registers functions asynchronously.
37
+ */
38
+ async function waitForTFHE(timeout = 5000) {
39
+ const start = Date.now();
40
+ while (Date.now() - start < timeout) {
41
+ if (typeof window.tfhe_init === 'function') {
42
+ return;
43
+ }
44
+ await new Promise(resolve => setTimeout(resolve, 10));
45
+ }
46
+ throw new Error('Timeout waiting for TFHE WASM to initialize');
47
+ }
48
+ /**
49
+ * Check if TFHE WASM is loaded.
50
+ */
51
+ export function isLoaded() {
52
+ return typeof window !== 'undefined' && typeof window.tfhe_init === 'function';
53
+ }
54
+ //# sourceMappingURL=init.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"init.js","sourceRoot":"","sources":["../js/init.ts"],"names":[],"mappings":"AAAA,yCAAyC;AACzC,wCAAwC;AAIxC;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,WAAmB,aAAa;IAC7D,mCAAmC;IACnC,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,OAAO,MA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D,CAAC;IAED,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;AACjE,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,QAAQ;IACtB,OAAO,OAAO,MAAM,KAAK,WAAW,IAAI,OAAO,MAAM,CAAC,SAAS,KAAK,UAAU,CAAC;AACjF,CAAC"}
@@ -0,0 +1,166 @@
1
+ /**
2
+ * Result from TFHE operations.
3
+ * All WASM functions return this structure.
4
+ */
5
+ export interface TFHEResult<T = unknown> {
6
+ success: boolean;
7
+ data?: T;
8
+ error?: string;
9
+ }
10
+ /**
11
+ * Key pair returned from generateKeys()
12
+ */
13
+ export interface KeyPair {
14
+ secretKey: string;
15
+ publicKey: string;
16
+ }
17
+ /**
18
+ * Encrypted bit - base64-encoded ciphertext
19
+ */
20
+ export type Ciphertext = string;
21
+ /**
22
+ * Encrypted 8-bit unsigned integer - array of 8 ciphertexts (LSB first)
23
+ */
24
+ export type CiphertextUint8 = [
25
+ Ciphertext,
26
+ Ciphertext,
27
+ Ciphertext,
28
+ Ciphertext,
29
+ Ciphertext,
30
+ Ciphertext,
31
+ Ciphertext,
32
+ Ciphertext
33
+ ];
34
+ /**
35
+ * Raw WASM functions exposed globally.
36
+ * These are low-level and return TFHEResult.
37
+ */
38
+ export interface TFHEWasmRaw {
39
+ tfhe_init(): TFHEResult<string>;
40
+ tfhe_generateKeys(): TFHEResult<KeyPair>;
41
+ tfhe_loadSecretKey(secretKey: string): TFHEResult<string>;
42
+ tfhe_encrypt(value: boolean): TFHEResult<Ciphertext>;
43
+ tfhe_decrypt(ct: Ciphertext): TFHEResult<boolean>;
44
+ tfhe_encryptUint8(value: number): TFHEResult<CiphertextUint8>;
45
+ tfhe_decryptUint8(cts: CiphertextUint8): TFHEResult<number>;
46
+ tfhe_and(ct1: Ciphertext, ct2: Ciphertext): TFHEResult<Ciphertext>;
47
+ tfhe_or(ct1: Ciphertext, ct2: Ciphertext): TFHEResult<Ciphertext>;
48
+ tfhe_xor(ct1: Ciphertext, ct2: Ciphertext): TFHEResult<Ciphertext>;
49
+ tfhe_not(ct: Ciphertext): TFHEResult<Ciphertext>;
50
+ tfhe_nand(ct1: Ciphertext, ct2: Ciphertext): TFHEResult<Ciphertext>;
51
+ tfhe_nor(ct1: Ciphertext, ct2: Ciphertext): TFHEResult<Ciphertext>;
52
+ tfhe_xnor(ct1: Ciphertext, ct2: Ciphertext): TFHEResult<Ciphertext>;
53
+ tfhe_mux(sel: Ciphertext, ctTrue: Ciphertext, ctFalse: Ciphertext): TFHEResult<Ciphertext>;
54
+ tfhe_compare(a: CiphertextUint8, b: CiphertextUint8): TFHEResult<Ciphertext>;
55
+ tfhe_addUint8(a: CiphertextUint8, b: CiphertextUint8): TFHEResult<CiphertextUint8>;
56
+ tfhe_subUint8(a: CiphertextUint8, b: CiphertextUint8): TFHEResult<CiphertextUint8>;
57
+ tfhe_mulUint8(a: CiphertextUint8, b: CiphertextUint8): TFHEResult<CiphertextUint8>;
58
+ }
59
+ /**
60
+ * High-level TFHE API with proper error handling.
61
+ */
62
+ export interface TFHE {
63
+ /**
64
+ * Initialize the TFHE library.
65
+ * Must be called before any other operations.
66
+ */
67
+ init(): Promise<void>;
68
+ /**
69
+ * Generate a new key pair.
70
+ * @returns The generated key pair
71
+ */
72
+ generateKeys(): Promise<KeyPair>;
73
+ /**
74
+ * Load a secret key from base64.
75
+ * This also regenerates the public key and bootstrap key.
76
+ * @param secretKey Base64-encoded secret key
77
+ */
78
+ loadSecretKey(secretKey: string): Promise<void>;
79
+ /**
80
+ * Encrypt a boolean value.
81
+ * @param value The boolean to encrypt
82
+ * @returns Encrypted ciphertext
83
+ */
84
+ encrypt(value: boolean): Promise<Ciphertext>;
85
+ /**
86
+ * Decrypt a ciphertext to a boolean.
87
+ * @param ct The ciphertext to decrypt
88
+ * @returns The decrypted boolean
89
+ */
90
+ decrypt(ct: Ciphertext): Promise<boolean>;
91
+ /**
92
+ * Encrypt an 8-bit unsigned integer.
93
+ * @param value The value to encrypt (0-255)
94
+ * @returns Array of 8 encrypted bits
95
+ */
96
+ encryptUint8(value: number): Promise<CiphertextUint8>;
97
+ /**
98
+ * Decrypt 8 ciphertexts to an 8-bit unsigned integer.
99
+ * @param cts Array of 8 encrypted bits
100
+ * @returns The decrypted value (0-255)
101
+ */
102
+ decryptUint8(cts: CiphertextUint8): Promise<number>;
103
+ /**
104
+ * Perform logical AND on two encrypted bits.
105
+ */
106
+ and(ct1: Ciphertext, ct2: Ciphertext): Promise<Ciphertext>;
107
+ /**
108
+ * Perform logical OR on two encrypted bits.
109
+ */
110
+ or(ct1: Ciphertext, ct2: Ciphertext): Promise<Ciphertext>;
111
+ /**
112
+ * Perform logical XOR on two encrypted bits.
113
+ */
114
+ xor(ct1: Ciphertext, ct2: Ciphertext): Promise<Ciphertext>;
115
+ /**
116
+ * Perform logical NOT on an encrypted bit.
117
+ */
118
+ not(ct: Ciphertext): Promise<Ciphertext>;
119
+ /**
120
+ * Perform logical NAND on two encrypted bits.
121
+ */
122
+ nand(ct1: Ciphertext, ct2: Ciphertext): Promise<Ciphertext>;
123
+ /**
124
+ * Perform logical NOR on two encrypted bits.
125
+ */
126
+ nor(ct1: Ciphertext, ct2: Ciphertext): Promise<Ciphertext>;
127
+ /**
128
+ * Perform logical XNOR on two encrypted bits.
129
+ */
130
+ xnor(ct1: Ciphertext, ct2: Ciphertext): Promise<Ciphertext>;
131
+ /**
132
+ * Multiplexer: if sel then ctTrue else ctFalse.
133
+ */
134
+ mux(sel: Ciphertext, ctTrue: Ciphertext, ctFalse: Ciphertext): Promise<Ciphertext>;
135
+ /**
136
+ * Compare two encrypted uint8 values.
137
+ * @returns Encrypted result: true if a < b
138
+ */
139
+ compare(a: CiphertextUint8, b: CiphertextUint8): Promise<Ciphertext>;
140
+ /**
141
+ * Add two encrypted uint8 values.
142
+ */
143
+ add(a: CiphertextUint8, b: CiphertextUint8): Promise<CiphertextUint8>;
144
+ /**
145
+ * Subtract two encrypted uint8 values (a - b).
146
+ */
147
+ sub(a: CiphertextUint8, b: CiphertextUint8): Promise<CiphertextUint8>;
148
+ /**
149
+ * Multiply two encrypted uint8 values.
150
+ * Returns lower 8 bits of result.
151
+ */
152
+ mul(a: CiphertextUint8, b: CiphertextUint8): Promise<CiphertextUint8>;
153
+ }
154
+ declare global {
155
+ interface Window extends TFHEWasmRaw {
156
+ Go: new () => GoInstance;
157
+ }
158
+ }
159
+ /**
160
+ * Go runtime instance for WASM.
161
+ */
162
+ export interface GoInstance {
163
+ importObject: WebAssembly.Imports;
164
+ run(instance: WebAssembly.Instance): Promise<void>;
165
+ }
166
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../js/types.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,MAAM,WAAW,UAAU,CAAC,CAAC,GAAG,OAAO;IACrC,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC;AAEhC;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,UAAU;IAAE,UAAU;IAAE,UAAU;IAAE,UAAU;IAC9C,UAAU;IAAE,UAAU;IAAE,UAAU;IAAE,UAAU;CAC/C,CAAC;AAEF;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,SAAS,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IAChC,iBAAiB,IAAI,UAAU,CAAC,OAAO,CAAC,CAAC;IACzC,kBAAkB,CAAC,SAAS,EAAE,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC;IAC1D,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,UAAU,CAAC,UAAU,CAAC,CAAC;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{};
4
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../js/types.ts"],"names":[],"mappings":"AAAA,yCAAyC;AACzC,wCAAwC"}