Skip to content

Commit

Permalink
Merge pull request #78 from zama-ai/keccak
Browse files Browse the repository at this point in the history
Keccak
  • Loading branch information
immortal-tofu authored Jun 26, 2024
2 parents a3f3365 + b619b4a commit a109fb9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 77 deletions.
95 changes: 32 additions & 63 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
@@ -1,6 +1,6 @@
{
"name": "fhevmjs",
"version": "0.5.0-11",
"version": "0.5.0-12",
"description": "fhEVM SDK for blockchain using TFHE",
"main": "lib/node.js",
"types": "lib/node/node.d.ts",
Expand Down Expand Up @@ -47,9 +47,9 @@
"commander": "^11.0.0",
"crypto-js": "^4.1.1",
"ethers": "^6.6.4",
"keccak": "^3.0.4",
"node-fetch": "^2.7.0",
"node-tfhe": "^0.6.3",
"sha3": "^2.1.4",
"tfhe": "^0.6.3",
"web3-validator": "^2.0.6"
},
Expand Down
26 changes: 19 additions & 7 deletions src/init.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import initSDK, { InitOutput } from 'tfhe';
import wasm from 'tfhe/tfhe_bg.wasm';
import initTFHE, { InitInput as TFHEInput } from 'tfhe';
import wasmTFHE from 'tfhe/tfhe_bg.wasm';

let initialized: InitOutput;
import initKMS, { InitInput as KMSInput } from './kms/web/kms_lib.js';
import wasmKMS from './kms/web/kms_lib_bg.wasm';

type InitFhevm = typeof initSDK;
let initialized = false;

export const initFhevm: InitFhevm = async (params) => {
export const initFhevm = async ({
tfheParams,
kmsParams,
}: {
tfheParams?: TFHEInput;
kmsParams?: KMSInput;
} = {}) => {
if (!initialized) {
initialized = await initSDK(params || wasm());
await initTFHE(tfheParams || wasmTFHE());
await initKMS(
kmsParams ||
(wasmKMS as unknown as () => Promise<WebAssembly.Instance>)(),
);
initialized = true;
}
return initialized;
return true;
};
8 changes: 3 additions & 5 deletions src/sdk/encrypt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isAddress } from 'web3-validator';
import createKeccakHash from 'keccak';
import { Keccak } from 'sha3';
import {
TfheCompactPublicKey,
CompactFheUint160List,
Expand Down Expand Up @@ -167,9 +167,7 @@ export const createEncryptedInput =
}

const inputProof = encrypted.serialize();
const hash = createKeccakHash('keccak256')
.update(Buffer.from(inputProof))
.digest();
const hash = new Keccak(256).update(Buffer.from(inputProof)).digest();
// const encrypted = ProvenCompactFheUint160List.encrypt_with_compact_public_key(
// values,
// publicZkParams,
Expand All @@ -180,7 +178,7 @@ export const createEncryptedInput =
const dataWithIndex = new Uint8Array(hash.length + 1);
dataWithIndex.set(hash, 0);
dataWithIndex.set([i], hash.length);
const finalHash = createKeccakHash('keccak256')
const finalHash = new Keccak(256)
.update(Buffer.from(dataWithIndex))
.digest();
const dataInput = new Uint8Array(32);
Expand Down

0 comments on commit a109fb9

Please sign in to comment.