Skip to content

Commit ab3de9a

Browse files
authored
Merge pull request #204 from FhenixProtocol/feat/tfhe153-followup
Feat/tfhe153 followup
2 parents 6ec669c + 9750942 commit ab3de9a

8 files changed

Lines changed: 28 additions & 32 deletions

File tree

packages/sdk/core/consts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY =
2222
'0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d' as const;
2323

2424
/** Maximum total bits for ZK proof packing */
25-
export const TFHE_RS_ZK_MAX_BITS = 2048n as const;
25+
export const TFHE_RS_ZK_MAX_BITS = 2048 as const;
2626

2727
/** Size limit for safe_serialize/safe_deserialize (1 GB) */
2828
export const TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT = BigInt(1 << 30);

packages/sdk/core/encrypt/cofheMocksZkVerifySign.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type EncryptableItem, FheTypes } from '../types.js';
2-
import { MAX_ENCRYPTABLE_BITS, type VerifyResult } from './zkPackProveVerify.js';
2+
import { type VerifyResult } from './zkPackProveVerify.js';
33
import {
44
createWalletClient,
55
http,
@@ -14,7 +14,7 @@ import { MockZkVerifierAbi } from './MockZkVerifierAbi.js';
1414
import { hardhat } from 'viem/chains';
1515
import { CofheError, CofheErrorCode } from '../error.js';
1616
import { privateKeyToAccount, sign } from 'viem/accounts';
17-
import { MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY, MOCKS_ZK_VERIFIER_ADDRESS } from '../consts.js';
17+
import { MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY, MOCKS_ZK_VERIFIER_ADDRESS, TFHE_RS_ZK_MAX_BITS } from '../consts.js';
1818

1919
type EncryptableItemWithCtHash = EncryptableItem & {
2020
ctHash: bigint;
@@ -69,14 +69,14 @@ export async function cofheMocksCheckEncryptableBits(items: EncryptableItem[]):
6969
}
7070
}
7171
}
72-
if (totalBits > MAX_ENCRYPTABLE_BITS) {
72+
if (totalBits > TFHE_RS_ZK_MAX_BITS) {
7373
throw new CofheError({
7474
code: CofheErrorCode.ZkPackFailed,
75-
message: `Total bits ${totalBits} exceeds ${MAX_ENCRYPTABLE_BITS}`,
76-
hint: `Ensure that the total bits of the items to encrypt does not exceed ${MAX_ENCRYPTABLE_BITS}`,
75+
message: `Total bits ${totalBits} exceeds ${TFHE_RS_ZK_MAX_BITS}`,
76+
hint: `Ensure that the total bits of the items to encrypt does not exceed ${TFHE_RS_ZK_MAX_BITS}`,
7777
context: {
7878
totalBits,
79-
maxBits: MAX_ENCRYPTABLE_BITS,
79+
maxBits: TFHE_RS_ZK_MAX_BITS,
8080
items,
8181
},
8282
});

packages/sdk/core/encrypt/encryptInputsBuilder.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ class MockZkListBuilder {
122122

123123
const MockCrs = {
124124
free: () => {},
125-
serialize: () => new Uint8Array(),
126-
safe_serialize: () => new Uint8Array(),
125+
safe_serialize: (_serializedSizeLimit: bigint) => new Uint8Array(),
127126
};
128127

129128
// Setup fetch mock for http://localhost:3001/verify
@@ -223,7 +222,7 @@ class MockZkProvenList {
223222
this.metadata = metadata;
224223
}
225224

226-
serialize(): Uint8Array {
225+
safe_serialize(_serializedSizeLimit: bigint): Uint8Array {
227226
// Serialize this.items into JSON, then encode as Uint8Array (utf-8)
228227
const json = stringifyWithBigInt({ items: this.items, metadata: this.metadata });
229228
return new TextEncoder().encode(json);

packages/sdk/core/encrypt/zkPackProveVerify.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { TFHE_RS_ZK_MAX_BITS, TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT } from 'core/consts.js';
2-
import { CofheError, CofheErrorCode } from '../error.js';
3-
import { type EncryptableItem, FheTypes } from '../types.js';
4-
import { toBigIntOrThrow, validateBigIntInRange, toHexString, hexToBytes } from '../utils.js';
1+
import { TFHE_RS_ZK_MAX_BITS, TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT } from '../consts';
2+
import { CofheError, CofheErrorCode } from '../error';
3+
import { type EncryptableItem, FheTypes } from '../types';
4+
import { toBigIntOrThrow, validateBigIntInRange, toHexString, hexToBytes } from '../utils';
55

66
// ===== TYPES =====
77

@@ -90,67 +90,66 @@ export const MAX_UINT64: bigint = 18446744073709551615n; // 2^64 - 1
9090
export const MAX_UINT128: bigint = 340282366920938463463374607431768211455n; // 2^128 - 1
9191
export const MAX_UINT256: bigint = 115792089237316195423570985008687907853269984665640564039457584007913129640319n; // 2^256 - 1
9292
export const MAX_UINT160: bigint = 1461501637330902918203684832716283019655932542975n; // 2^160 - 1
93-
export const MAX_ENCRYPTABLE_BITS: number = 2048;
9493

9594
// ===== CORE FUNCTIONS =====
9695

9796
export const zkPack = (items: EncryptableItem[], builder: ZkCiphertextListBuilder): ZkCiphertextListBuilder => {
98-
let totalBits = 0n;
97+
let totalBits = 0;
9998
for (const item of items) {
10099
switch (item.utype) {
101100
case FheTypes.Bool: {
102101
builder.push_boolean(item.data);
103-
totalBits += 1n;
102+
totalBits += 1;
104103
break;
105104
}
106105
case FheTypes.Uint8: {
107106
const bint = toBigIntOrThrow(item.data);
108107
validateBigIntInRange(bint, MAX_UINT8);
109108
builder.push_u8(parseInt(bint.toString()));
110-
totalBits += 8n;
109+
totalBits += 8;
111110
break;
112111
}
113112
case FheTypes.Uint16: {
114113
const bint = toBigIntOrThrow(item.data);
115114
validateBigIntInRange(bint, MAX_UINT16);
116115
builder.push_u16(parseInt(bint.toString()));
117-
totalBits += 16n;
116+
totalBits += 16;
118117
break;
119118
}
120119
case FheTypes.Uint32: {
121120
const bint = toBigIntOrThrow(item.data);
122121
validateBigIntInRange(bint, MAX_UINT32);
123122
builder.push_u32(parseInt(bint.toString()));
124-
totalBits += 32n;
123+
totalBits += 32;
125124
break;
126125
}
127126
case FheTypes.Uint64: {
128127
const bint = toBigIntOrThrow(item.data);
129128
validateBigIntInRange(bint, MAX_UINT64);
130129
builder.push_u64(bint);
131-
totalBits += 64n;
130+
totalBits += 64;
132131
break;
133132
}
134133
case FheTypes.Uint128: {
135134
const bint = toBigIntOrThrow(item.data);
136135
validateBigIntInRange(bint, MAX_UINT128);
137136
builder.push_u128(bint);
138-
totalBits += 128n;
137+
totalBits += 128;
139138
break;
140139
}
141140
// [U256-DISABLED]
142141
// case FheTypes.Uint256: {
143142
// const bint = toBigIntOrThrow(item.data);
144143
// validateBigIntInRange(bint, MAX_UINT256);
145144
// builder.push_u256(bint);
146-
// totalBits += 256n;
145+
// totalBits += 256;
147146
// break;
148147
// }
149148
case FheTypes.Uint160: {
150149
const bint = toBigIntOrThrow(item.data);
151150
validateBigIntInRange(bint, MAX_UINT160);
152151
builder.push_u160(bint);
153-
totalBits += 160n;
152+
totalBits += 160;
154153
break;
155154
}
156155
default: {

packages/sdk/core/keyStore.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* eslint-disable no-undef */
33

44
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
5+
import { TFHE_RS_KEY_VERSION } from './consts.js';
56
import { createKeysStore, type KeysStore, type KeysStorage } from './keyStore.js';
67

78
// Mock the storage module
@@ -124,7 +125,7 @@ describe('KeyStore', () => {
124125
it('should clear keys storage', async () => {
125126
await keysStorage.clearKeysStorage();
126127

127-
expect(mockStorage.removeItem).toHaveBeenCalledWith('cofhesdk-keys');
128+
expect(mockStorage.removeItem).toHaveBeenCalledWith(`cofhesdk-keys-v${TFHE_RS_KEY_VERSION}`);
128129
});
129130

130131
it('should rehydrate keys store', async () => {

packages/sdk/core/keyStore.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ export type KeysStorage = {
2424
rehydrateKeysStore: () => Promise<void>;
2525
};
2626

27-
function getKeysStoreName(): string {
28-
return `cofhesdk-keys-v${TFHE_RS_KEY_VERSION}`;
29-
}
27+
const KEYSTORE_NAME = `cofhesdk-keys-v${TFHE_RS_KEY_VERSION}`;
3028

3129
function isValidPersistedState(state: unknown): state is KeysStore {
3230
if (state && typeof state === 'object') {
@@ -99,7 +97,7 @@ export function createKeysStore(storage: IStorage | null): KeysStorage {
9997

10098
const clearKeysStorage = async () => {
10199
if (storage) {
102-
await storage.removeItem(getKeysStoreName());
100+
await storage.removeItem(KEYSTORE_NAME);
103101
}
104102
// If no storage, this is a no-op
105103
};
@@ -130,7 +128,7 @@ function createStoreWithPersit(storage: IStorage) {
130128
onRehydrateStorage: () => (_state?, _error?) => {
131129
if (_error) throw new Error(`onRehydrateStorage: Error rehydrating keys store: ${_error}`);
132130
},
133-
name: getKeysStoreName(),
131+
name: KEYSTORE_NAME,
134132
storage: createJSONStorage(() => storage),
135133
merge: (persistedState, currentState) => {
136134
const persisted = isValidPersistedState(persistedState) ? persistedState : DEFAULT_KEYS_STORE;

packages/sdk/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"name": "@cofhe/sdk",
33
"version": "0.4.0",
4-
"hello": "world",
54
"type": "module",
65
"description": "SDK for Fhenix COFHE coprocessor interaction",
76
"main": "./dist/core.cjs",

packages/sdk/web/zkProve.worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/// <reference lib="webworker" />
77
/* eslint-disable no-undef */
88

9-
import { TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT } from 'core/consts.js';
9+
import { TFHE_RS_SAFE_SERIALIZATION_SIZE_LIMIT } from '../core/consts';
1010
import type { ZkProveWorkerRequest, ZkProveWorkerResponse } from '../core/encrypt/zkPackProveVerify.js';
1111

1212
// TFHE module (will be initialized on first use)

0 commit comments

Comments
 (0)