Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to tfhe-rs 1.0.0 #164

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions HOW-TO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Dependency update

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for doing it! To be reviewed by @immortal-tofu please, later, even if the PR is merged

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes there is also tkms to update to be fully tfhers 1.0.0.
And I can prerelease without merging.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I can prerelease without merging.

Awesome, but at the same time, it means our release / prerelease are not done on "real commits"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are real commit but not on main branch (they will be when Clement is back). There's even a prerelease tag to be sure we track them. Is this an issue for you ? A release is often done on release branch BTW not on main, so if you have rc/fix etc it happens outside of main.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eg, what happens if we squash commits? To me, we should do release on things which are merged, not on commits which are still in a PR


## Updating TFHE-RS

Steps:

1. Modify `package.json`.
2. Optional update `tkms` packages
3. Regen lock file `package-lock.json`, by running `npm install`
4. Update the code if needed
5. Update the keys if needed (e.g. for crypto-parameters changes, serialization changes), by running `./generateKeys.js`
6. Either push a PR to verify everything is green or test locally
a. npm test
b. npm run build
5 changes: 3 additions & 2 deletions config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ export default [
],
},
{
input: `./node_modules/tfhe/snippets/${wasmBindgenRayon}/src/workerHelpers.worker.js`,
input: `./node_modules/tfhe/snippets/${wasmBindgenRayon}/src/workerHelpers.js`,
output: {
file: 'lib/workerHelpers.worker.js',
file: 'lib/workerHelpers.js',
inlineDynamicImports: true,
name: 'worker',
format: 'esm',
},
Expand Down
2 changes: 1 addition & 1 deletion config/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default defineConfig({
],
worker: {
format: 'iife',
plugins: [ignoreURL(basePath)],
plugins: () => [ignoreURL(basePath)],
rollupOptions: {
output: {
entryFileNames: '[name].js',
Expand Down
6 changes: 3 additions & 3 deletions config/vite.replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ export const changeLoadingWorker = (basePath) => ({
// Only apply transformations to .js files (or other specific conditions)
if (id.endsWith('.js')) {
const searchValue =
/const worker = new Worker\(\s*new URL\(['"]\.?\/?workerHelpers\.worker\.js['"],\s*import\.meta\.url\),\s*\{\s*type:\s*'module',?\s*\},?\s*\);/;
/const worker = new Worker\(\s*new URL\(['"]\.?\/?workerHelpers.js['"],\s*import\.meta\.url\),\s*\{\s*type:\s*'module',?\s*\},?\s*\);/;

const replacement = `let worker;
try {
worker = new Worker(
new URL('./workerHelpers.worker.js', import.meta.url),
new URL('./workerHelpers.js', import.meta.url),
{
type: 'module'
}
);
} catch (e) {
const r = await fetch('${basePath}workerHelpers.worker.js');
const r = await fetch('${basePath}workerHelpers.js');
const blob = await r.blob();
const blobUrl = URL.createObjectURL(blob);
worker = new Worker(blobUrl);
Expand Down
14 changes: 7 additions & 7 deletions generateKeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export const SERIALIZED_SIZE_LIMIT_CRS = BigInt(1024 * 1024 * 512);

const createTfheKeypair = () => {
const block_params = new ShortintParameters(
ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_PBS_KS,
ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
);
const casting_params = new ShortintCompactPublicKeyEncryptionParameters(
ShortintCompactPublicKeyEncryptionParametersName.SHORTINT_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64,
ShortintCompactPublicKeyEncryptionParametersName.V1_0_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
);
const config = TfheConfigBuilder.default()
.use_custom_parameters(block_params)
Expand All @@ -41,27 +41,27 @@ const createTfheKeypair = () => {
const crs0 = CompactPkeCrs.from_config(config, 4 * 32);
fs.writeFileSync(
'src/test/keys/crs128.bin',
crs0.public_params().safe_serialize(SERIALIZED_SIZE_LIMIT_CRS),
crs0.safe_serialize(SERIALIZED_SIZE_LIMIT_CRS),
);
const crs1 = CompactPkeCrs.from_config(config, 4 * 64);
fs.writeFileSync(
'src/test/keys/crs256.bin',
crs1.public_params().safe_serialize(SERIALIZED_SIZE_LIMIT_CRS),
crs1.safe_serialize(SERIALIZED_SIZE_LIMIT_CRS),
);
const crs2 = CompactPkeCrs.from_config(config, 4 * 128);
fs.writeFileSync(
'src/test/keys/crs512.bin',
crs2.public_params().safe_serialize(SERIALIZED_SIZE_LIMIT_CRS),
crs2.safe_serialize(SERIALIZED_SIZE_LIMIT_CRS),
);
const crs3 = CompactPkeCrs.from_config(config, 4 * 256);
fs.writeFileSync(
'src/test/keys/crs1024.bin',
crs3.public_params().safe_serialize(SERIALIZED_SIZE_LIMIT_CRS),
crs3.safe_serialize(SERIALIZED_SIZE_LIMIT_CRS),
);
const crs4 = CompactPkeCrs.from_config(config, 4 * 512);
fs.writeFileSync(
'src/test/keys/crs2048.bin',
crs4.public_params().safe_serialize(SERIALIZED_SIZE_LIMIT_CRS),
crs4.safe_serialize(SERIALIZED_SIZE_LIMIT_CRS),
);
};

Expand Down
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
"ethers": "^6.13.4",
"fetch-mock": "^11.1.3",
"keccak": "^3.0.4",
"node-tfhe": "^0.10.0",
"node-tfhe": "^1.0.0",
"node-tkms": "0.10.0-rc1",
"tfhe": "^0.9.1",
"tfhe": "^1.0.0",
"tkms": "0.10.0-rc1",
"wasm-feature-detect": "^1.8.0"
},
Expand Down
4 changes: 2 additions & 2 deletions src/sdk/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
SERIALIZED_SIZE_LIMIT_PK,
SERIALIZED_SIZE_LIMIT_CRS,
} from '../utils';
import { CompactPkePublicParams, TfheCompactPublicKey } from 'node-tfhe';
import { CompactPkeCrs, TfheCompactPublicKey } from 'node-tfhe';
import { abi } from '../abi/kmsVerifier.json';

export type FhevmInstanceConfig = {
Expand Down Expand Up @@ -96,7 +96,7 @@ export const getPublicParams = async (
try {
return {
2048: {
publicParams: CompactPkePublicParams.safe_deserialize(
publicParams: CompactPkeCrs.safe_deserialize(
buff,
SERIALIZED_SIZE_LIMIT_CRS,
),
Expand Down
4 changes: 2 additions & 2 deletions src/sdk/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import createKeccakHash from 'keccak';
import {
TfheCompactPublicKey,
CompactCiphertextList,
CompactPkePublicParams,
CompactPkeCrs,
ZkComputeLoad,
} from 'node-tfhe';

Expand Down Expand Up @@ -73,7 +73,7 @@ const checkEncryptedValue = (value: number | bigint, bits: number) => {
}
};

export type PublicParams<T = CompactPkePublicParams> = {
export type PublicParams<T = CompactPkeCrs> = {
[key in EncryptionTypes]?: { publicParams: T; publicParamsId: string };
};

Expand Down
4 changes: 2 additions & 2 deletions src/sdk/network.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CompactPkePublicParams, TfheCompactPublicKey } from 'node-tfhe';
import { CompactPkeCrs, TfheCompactPublicKey } from 'node-tfhe';
import { SERIALIZED_SIZE_LIMIT_PK, SERIALIZED_SIZE_LIMIT_CRS } from '../utils';

export type GatewayKeysItem = {
Expand Down Expand Up @@ -89,7 +89,7 @@ export const getKeysFromGateway = async (
publicKeyId,
publicParams: {
2048: {
publicParams: CompactPkePublicParams.safe_deserialize(
publicParams: CompactPkeCrs.safe_deserialize(
new Uint8Array(publicParams2048),
SERIALIZED_SIZE_LIMIT_CRS,
),
Expand Down
8 changes: 2 additions & 6 deletions src/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
CompactPkePublicParams,
TfheClientKey,
TfheCompactPublicKey,
} from 'node-tfhe';
import { CompactPkeCrs, TfheClientKey, TfheCompactPublicKey } from 'node-tfhe';
import fs from 'fs';
import { SERIALIZED_SIZE_LIMIT_CRS, SERIALIZED_SIZE_LIMIT_PK } from '../utils';

Expand All @@ -22,7 +18,7 @@ export const publicKey = TfheCompactPublicKey.safe_deserialize(
);
export const publicParams = {
2048: {
publicParams: CompactPkePublicParams.safe_deserialize(
publicParams: CompactPkeCrs.safe_deserialize(
params2048,
SERIALIZED_SIZE_LIMIT_CRS,
),
Expand Down
Binary file modified src/test/keys/crs2048.bin
Binary file not shown.
Binary file modified src/test/keys/privateKey.bin
Binary file not shown.
Binary file modified src/test/keys/publicKey.bin
Binary file not shown.
4 changes: 2 additions & 2 deletions src/tfhe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { toHexString } from './utils';

export const createTfheKeypair = () => {
const block_params = new ShortintParameters(
ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_PBS_KS,
ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
);
const casting_params = new ShortintCompactPublicKeyEncryptionParameters(
ShortintCompactPublicKeyEncryptionParametersName.SHORTINT_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64,
ShortintCompactPublicKeyEncryptionParametersName.V1_0_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
);
const config = TfheConfigBuilder.default()
.use_custom_parameters(block_params)
Expand Down