Skip to content

Commit

Permalink
chore: updated to fhevm 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jatZama committed Apr 23, 2024
1 parent 828792c commit b4dba1f
Show file tree
Hide file tree
Showing 15 changed files with 1,058 additions and 87 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export INFURA_API_KEY="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
export MNEMONIC="adapt mosquito move limb mobile illegal tree voyage juice mosquito burger raise father hope layer"
export PRIVATE_KEY_ORACLE_DEPLOYER="717fd99986df414889fd8b51069d4f90a50af72e542c58ee065f5883779099c6"
export PRIVATE_KEY_ORACLE_OWNER="717fd99986df414889fd8b51069d4f90a50af72e542c58ee065f5883779099c6"
export PRIVATE_KEY_ORACLE_RELAYER="7ec931411ad75a7c201469a385d6f18a325d4923f9f213bd882bbea87e160b67"

# Block explorer API keys
export ARBISCAN_API_KEY="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
Expand Down
166 changes: 166 additions & 0 deletions contracts/asyncDecrypt/TestAsyncDecrypt.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// SPDX-License-Identifier: BSD-3-Clause-Clear

pragma solidity ^0.8.20;

import "fhevm/lib/TFHE.sol";
import "fhevm/oracle/OracleCaller.sol";

contract TestAsyncDecrypt is OracleCaller {
ebool xBool;
euint4 xUint4;
euint8 xUint8;
euint16 xUint16;
euint32 xUint32;
euint64 xUint64;
eaddress xAddress;

bool public yBool;
uint8 public yUint4;
uint8 public yUint8;
uint16 public yUint16;
uint32 public yUint32;
uint64 public yUint64;
address public yAddress;

constructor() {
xBool = TFHE.asEbool(true);
xUint4 = TFHE.asEuint4(4);
xUint8 = TFHE.asEuint8(42);
xUint16 = TFHE.asEuint16(16);
xUint32 = TFHE.asEuint32(32);
xUint64 = TFHE.asEuint64(64);
xAddress = TFHE.asEaddress(0x8ba1f109551bD432803012645Ac136ddd64DBA72);
}

function requestBoolAboveDelay() public {
// should revert
ebool[] memory cts = new ebool[](1);
cts[0] = xBool;
Oracle.requestDecryption(cts, this.callbackBool.selector, 0, block.timestamp + 2 days);
}

function requestBool() public {
ebool[] memory cts = new ebool[](1);
cts[0] = xBool;
Oracle.requestDecryption(cts, this.callbackBool.selector, 0, block.timestamp + 100);
}

function requestFakeBool() public {
ebool[] memory cts = new ebool[](1);
cts[0] = ebool.wrap(42);
Oracle.requestDecryption(cts, this.callbackBool.selector, 0, block.timestamp + 100); // this should revert because previous ebool is not honestly obtained
}

function callbackBool(uint256 /*requestID*/, bool decryptedInput) public onlyOracle returns (bool) {
yBool = decryptedInput;
return yBool;
}

function requestUint4() public {
euint4[] memory cts = new euint4[](1);
cts[0] = xUint4;
Oracle.requestDecryption(cts, this.callbackUint4.selector, 0, block.timestamp + 100);
}

function requestFakeUint4() public {
euint4[] memory cts = new euint4[](1);
cts[0] = euint4.wrap(42);
Oracle.requestDecryption(cts, this.callbackUint4.selector, 0, block.timestamp + 100); // this should revert because previous ebool is not honestly obtained
}

function callbackUint4(uint256 /*requestID*/, uint8 decryptedInput) public onlyOracle returns (uint8) {
yUint4 = decryptedInput;
return decryptedInput;
}

function requestUint8() public {
euint8[] memory cts = new euint8[](1);
cts[0] = xUint8;
Oracle.requestDecryption(cts, this.callbackUint8.selector, 0, block.timestamp + 100);
}

function requestFakeUint8() public {
euint8[] memory cts = new euint8[](1);
cts[0] = euint8.wrap(42);
Oracle.requestDecryption(cts, this.callbackUint8.selector, 0, block.timestamp + 100); // this should revert because previous ebool is not honestly obtained
}

function callbackUint8(uint256 /*requestID*/, uint8 decryptedInput) public onlyOracle returns (uint8) {
yUint8 = decryptedInput;
return decryptedInput;
}

function requestUint16() public {
euint16[] memory cts = new euint16[](1);
cts[0] = xUint16;
Oracle.requestDecryption(cts, this.callbackUint16.selector, 0, block.timestamp + 100);
}

function requestFakeUint16() public {
euint16[] memory cts = new euint16[](1);
cts[0] = euint16.wrap(42);
Oracle.requestDecryption(cts, this.callbackUint16.selector, 0, block.timestamp + 100); // this should revert because previous ebool is not honestly obtained
}

function callbackUint16(uint256 /*requestID*/, uint16 decryptedInput) public onlyOracle returns (uint16) {
yUint16 = decryptedInput;
return decryptedInput;
}

function requestUint32(uint32 input1, uint32 input2) public {
euint32[] memory cts = new euint32[](1);
cts[0] = xUint32;
uint256 requestID = Oracle.requestDecryption(cts, this.callbackUint32.selector, 0, block.timestamp + 100);
addParamsUint(requestID, input1);
addParamsUint(requestID, input2);
}

function requestFakeUint32() public {
euint32[] memory cts = new euint32[](1);
cts[0] = euint32.wrap(42);
Oracle.requestDecryption(cts, this.callbackUint32.selector, 0, block.timestamp + 100); // this should revert because previous ebool is not honestly obtained
}

function callbackUint32(uint256 requestID, uint32 decryptedInput) public onlyOracle returns (uint32) {
uint256[] memory params = getParamsUint(requestID);
unchecked {
uint32 result = uint32(params[0]) + uint32(params[1]) + decryptedInput;
yUint32 = result;
return result;
}
}

function requestUint64() public {
euint64[] memory cts = new euint64[](1);
cts[0] = xUint64;
Oracle.requestDecryption(cts, this.callbackUint64.selector, 0, block.timestamp + 100);
}

function requestFakeUint64() public {
euint64[] memory cts = new euint64[](1);
cts[0] = euint64.wrap(42);
Oracle.requestDecryption(cts, this.callbackUint64.selector, 0, block.timestamp + 100); // this should revert because previous ebool is not honestly obtained
}

function callbackUint64(uint256 /*requestID*/, uint64 decryptedInput) public onlyOracle returns (uint64) {
yUint64 = decryptedInput;
return decryptedInput;
}

function requestAddress() public {
eaddress[] memory cts = new eaddress[](1);
cts[0] = xAddress;
Oracle.requestDecryption(cts, this.callbackAddress.selector, 0, block.timestamp + 100);
}

function requestFakeAddress() public {
eaddress[] memory cts = new eaddress[](1);
cts[0] = eaddress.wrap(42);
Oracle.requestDecryption(cts, this.callbackAddress.selector, 0, block.timestamp + 100); // this should revert because previous ebool is not honestly obtained
}

function callbackAddress(uint256 /*requestID*/, address decryptedInput) public onlyOracle returns (address) {
yAddress = decryptedInput;
return decryptedInput;
}
}
18 changes: 0 additions & 18 deletions deploy/deploy.ts

This file was deleted.

48 changes: 38 additions & 10 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "@nomicfoundation/hardhat-toolbox";
import { config as dotenvConfig } from "dotenv";
import dotenv from "dotenv";
import * as fs from "fs";
import "hardhat-deploy";
import "hardhat-preprocessor";
Expand All @@ -9,12 +9,10 @@ import { task } from "hardhat/config";
import type { NetworkUserConfig } from "hardhat/types";
import { resolve } from "path";
import * as path from "path";
import "solidity-docgen";

import "./tasks/accounts";
import "./tasks/deployERC20";
import "./tasks/getEthereumAddress";
import "./tasks/mint";
import "./tasks/taskOracleRelayer";

function getAllSolidityFiles(dir: string, fileList: string[] = []): string[] {
fs.readdirSync(dir).forEach((file) => {
Expand Down Expand Up @@ -48,7 +46,7 @@ task("coverage-mock", "Run coverage after running pre-process task").setAction(a
});

const dotenvConfigPath: string = process.env.DOTENV_CONFIG_PATH || "./.env";
dotenvConfig({ path: resolve(__dirname, dotenvConfigPath) });
dotenv.config({ path: resolve(__dirname, dotenvConfigPath) });

const mnemonic: string | undefined = process.env.MNEMONIC;
if (!mnemonic) {
Expand Down Expand Up @@ -99,17 +97,47 @@ function getChainConfig(chain: keyof typeof chainIds): NetworkUserConfig {
};
}

task("test", async (taskArgs, hre, runSuper) => {
// Run modified test task

if (network === "hardhat") {
// in fhevm mode all this block is done when launching the node via `pnmp fhevm:start`
const privKeyDeployer = process.env.PRIVATE_KEY_ORACLE_DEPLOYER;
const privKeyOwner = process.env.PRIVATE_KEY_ORACLE_OWNER;
const privKeyRelayer = process.env.PRIVATE_KEY_ORACLE_RELAYER;
const deployerAddress = new hre.ethers.Wallet(privKeyDeployer!).address;
const ownerAddress = new hre.ethers.Wallet(privKeyOwner!).address;
const relayerAddress = new hre.ethers.Wallet(privKeyRelayer!).address;

await hre.run("task:computePredeployAddress", { privateKey: privKeyDeployer });

const bal = "0x1000000000000000000000000000000000000000";
const p1 = hre.network.provider.send("hardhat_setBalance", [deployerAddress, bal]);
const p2 = hre.network.provider.send("hardhat_setBalance", [ownerAddress, bal]);
const p3 = hre.network.provider.send("hardhat_setBalance", [relayerAddress, bal]);
await Promise.all([p1, p2, p3]);
await hre.run("compile");
await hre.run("task:deployOracle", { privateKey: privKeyDeployer, ownerAddress: ownerAddress });

const parsedEnv = dotenv.parse(fs.readFileSync("node_modules/fhevm/oracle/.env.oracle"));
const oraclePredeployAddress = parsedEnv.ORACLE_CONTRACT_PREDEPLOY_ADDRESS;

await hre.run("task:addRelayer", {
privateKey: privKeyOwner,
oracleAddress: oraclePredeployAddress,
relayerAddress: relayerAddress,
});
}

await runSuper();
});

const config: HardhatUserConfig = {
docgen: {
pages: "files",
},
preprocess: {
eachLine: () => ({
transform: (line: string) => {
if (network === "hardhat") {
// checks if HARDHAT_NETWORK env variable is set to "hardhat" to use the remapping for the mocked version of TFHE.sol
if (line.match(/".*.sol";$/)) {
// match all lines with `"<any-import-path>.sol";`
for (const [from, to] of getRemappings()) {
if (line.includes(from)) {
line = line.replace(from, to);
Expand Down
22 changes: 22 additions & 0 deletions launch-fhevm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

PRIVATE_KEY_ORACLE_DEPLOYER=$(grep PRIVATE_KEY_ORACLE_DEPLOYER .env | cut -d '"' -f 2)

npx hardhat task:computePredeployAddress --private-key "$PRIVATE_KEY_ORACLE_DEPLOYER"

PRIVATE_KEY_ORACLE_RELAYER=$(grep PRIVATE_KEY_ORACLE_RELAYER .env | cut -d '"' -f 2)

ORACLE_CONTRACT_PREDEPLOY_ADDRESS=$(grep ORACLE_CONTRACT_PREDEPLOY_ADDRESS node_modules/fhevm/oracle/.env.oracle | cut -d '=' -f2)

docker run -d -i -p 8545:8545 --rm --name fhevm \
-e PRIVATE_KEY_ORACLE_RELAYER="$PRIVATE_KEY_ORACLE_RELAYER" \
-e ORACLE_CONTRACT_PREDEPLOY_ADDRESS="$ORACLE_CONTRACT_PREDEPLOY_ADDRESS" \
ghcr.io/zama-ai/ethermint-dev-node:v0.4.2

sleep 10

npx hardhat compile

npx hardhat task:launchFhevm

docker attach fhevm
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
"eslint": "^8.57.0",
"eslint-config-prettier": "^8.10.0",
"ethers": "^6.11.1",
"fhevm": "0.4.0-5",
"fhevmjs": "0.4.0-7",
"fhevm": "^0.4.0",
"fhevmjs": "^0.4.0-7",
"fs-extra": "^10.1.0",
"hardhat": "^2.21.0",
"hardhat": "2.21.0",
"hardhat-deploy": "^0.11.45",
"hardhat-gas-reporter": "^1.0.10",
"hardhat-preprocessor": "^0.1.5",
Expand All @@ -42,7 +42,7 @@
"rimraf": "^4.4.1",
"solhint": "^3.6.2",
"solhint-plugin-prettier": "^0.0.5",
"solidity-coverage": "^0.8.11",
"solidity-coverage": "0.8.6",
"solidity-docgen": "0.6.0-beta.36",
"ts-generator": "^0.1.1",
"ts-node": "^10.9.2",
Expand Down Expand Up @@ -81,10 +81,8 @@
"test:mock": "HARDHAT_NETWORK=hardhat npx hardhat test --network hardhat",
"coverage:mock": "HARDHAT_NETWORK=hardhat npx hardhat coverage-mock --network hardhat",
"task:getEthereumAddress": "hardhat task:getEthereumAddress",
"task:mint": "hardhat task:mint",
"task:deployERC20": "hardhat task:deployERC20",
"task:accounts": "hardhat task:accounts",
"fhevm:start": "docker run -i -p 8545:8545 -p 8546:8546 --rm --name fhevm ghcr.io/zama-ai/ethermint-dev-node:v0.3.0-5",
"fhevm:start": "./launch-fhevm.sh",
"fhevm:stop": "docker rm -f fhevm",
"fhevm:restart": "fhevm:stop && fhevm:start",
"fhevm:faucet": "npm run fhevm:faucet:alice && sleep 5 && npm run fhevm:faucet:bob && sleep 5 && npm run fhevm:faucet:carol && sleep 5 && npm run fhevm:faucet:dave && sleep 5 && npm run fhevm:faucet:eve",
Expand Down
Loading

0 comments on commit b4dba1f

Please sign in to comment.