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

feat: updated to new core contracts prerelease #674

Merged
merged 1 commit into from
Dec 23, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- run: source .env && npx hardhat task:computeTFHEExecutorAddress --private-key $PRIVATE_KEY_FHEVM_DEPLOYER
- run: source .env && npx hardhat task:computeKMSVerifierAddress --private-key $PRIVATE_KEY_FHEVM_DEPLOYER
- run: source .env && npx hardhat task:computeInputVerifierAddress --private-key $PRIVATE_KEY_FHEVM_DEPLOYER --use-address false
- run: source .env && npx hardhat task:computeFHEPaymentAddress --private-key $PRIVATE_KEY_FHEVM_DEPLOYER
- run: source .env && npx hardhat task:computeFHEGasLimitAddress --private-key $PRIVATE_KEY_FHEVM_DEPLOYER
- run: npm run compile
- uses: JS-DevTools/npm-publish@19c28f1ef146469e409470805ea4279d47c3d35c # v3.1.1
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publishprerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- run: source .env && npx hardhat task:computeTFHEExecutorAddress --private-key $PRIVATE_KEY_FHEVM_DEPLOYER
- run: source .env && npx hardhat task:computeKMSVerifierAddress --private-key $PRIVATE_KEY_FHEVM_DEPLOYER
- run: source .env && npx hardhat task:computeInputVerifierAddress --private-key $PRIVATE_KEY_FHEVM_DEPLOYER --use-address false
- run: source .env && npx hardhat task:computeFHEPaymentAddress --private-key $PRIVATE_KEY_FHEVM_DEPLOYER
- run: source .env && npx hardhat task:computeFHEGasLimitAddress --private-key $PRIVATE_KEY_FHEVM_DEPLOYER
- run: npm run compile
- uses: JS-DevTools/npm-publish@19c28f1ef146469e409470805ea4279d47c3d35c # v3.1.1
with:
Expand Down
1 change: 0 additions & 1 deletion codegen/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ function generateAllFiles() {
const ovShards = testgen.splitOverloadsToShards(overloads);
writeFileSync('lib/Impl.sol', t.implSol(operators));
writeFileSync('lib/TFHE.sol', tfheSolSource);
writeFileSync('payment/Payment.sol', t.paymentSol());
mkdirSync('examples/tests', { recursive: true });
ovShards.forEach((os) => {
writeFileSync(`examples/tests/TFHETestSuite${os.shardNumber}.sol`, testgen.generateSmartContract(os));
Expand Down
79 changes: 26 additions & 53 deletions codegen/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export function implSol(operators: Operator[]): string {

const coprocessorInterface = generateImplCoprocessorInterface(operators);
const aclInterface = generateACLInterface();
const inputVerifierInterface = generateInputVerifierInterface();

res.push(`
// SPDX-License-Identifier: BSD-3-Clause-Clear
Expand All @@ -80,6 +81,8 @@ ${coprocessorInterface}

${aclInterface}

${inputVerifierInterface}

/**
* @title Impl
* @notice This library is the core implementation for computing FHE operations (e.g. add, sub, xor).
Expand All @@ -98,8 +101,8 @@ library Impl {
FHEVMConfigStruct storage $ = getFHEVMConfig();
$.ACLAddress = fhevmConfig.ACLAddress;
$.TFHEExecutorAddress = fhevmConfig.TFHEExecutorAddress;
$.FHEPaymentAddress = fhevmConfig.FHEPaymentAddress;
$.KMSVerifierAddress = fhevmConfig.KMSVerifierAddress;
$.InputVerifierAddress = fhevmConfig.InputVerifierAddress;
}
`);

Expand Down Expand Up @@ -172,8 +175,8 @@ function generateImplCoprocessorInterface(operators: Operator[]): string {
struct FHEVMConfigStruct {
address ACLAddress;
address TFHEExecutorAddress;
address FHEPaymentAddress;
address KMSVerifierAddress;
address InputVerifierAddress;
}

/**
Expand Down Expand Up @@ -242,6 +245,18 @@ function generateACLInterface(): string {
`;
}

function generateInputVerifierInterface(): string {
return `
/**
* @title IInputVerifier
* @notice This interface contains the only function required from InputVerifier.
*/
interface IInputVerifier {
function cleanTransientStorage() external;
}
`;
}

export function tfheSol(
operators: Operator[],
supportedBits: number[],
Expand Down Expand Up @@ -746,9 +761,11 @@ function tfheAclMethods(supportedBits: number[]): string {
res.push(
`
// cleans the transient storage of ACL containing all the allowedTransient accounts
// also cleans transient storage of InputVerifier containing cached inputProofs
// to be used for integration with Account Abstraction or when bundling UserOps calling the FHEVMCoprocessor
function cleanTransientStorage() internal {
return Impl.cleanTransientStorage();
Impl.cleanTransientStorageACL();
Impl.cleanTransientStorageInputVerifier();
}

function isAllowed(ebool value, address account) internal view returns (bool) {
Expand Down Expand Up @@ -1518,63 +1535,19 @@ function implCustomMethods(): string {
IACL($.ACLAddress).allow(handle, account);
}

function cleanTransientStorage() internal {
function cleanTransientStorageACL() internal {
FHEVMConfigStruct storage $ = getFHEVMConfig();
IACL($.ACLAddress).cleanTransientStorage();
}

function cleanTransientStorageInputVerifier() internal {
FHEVMConfigStruct storage $ = getFHEVMConfig();
IInputVerifier($.InputVerifierAddress).cleanTransientStorage();
}

function isAllowed(uint256 handle, address account) internal view returns (bool) {
FHEVMConfigStruct storage $ = getFHEVMConfig();
return IACL($.ACLAddress).isAllowed(handle, account);
}
`;
}

export function paymentSol(): string {
const res: string = `// SPDX-License-Identifier: BSD-3-Clause-Clear

pragma solidity ^0.8.24;

import "../lib/Impl.sol";

interface IFHEPayment {
function depositETH(address account) external payable;
function withdrawETH(uint256 amount, address receiver) external;
function getAvailableDepositsETH(address account) external view returns(uint256);
}

library Payment {
function depositForAccount(address account, uint256 amount) internal {
FHEVMConfigStruct storage $ = Impl.getFHEVMConfig();
IFHEPayment($.FHEPaymentAddress).depositETH{value: amount}(account);
}

function depositForThis(uint256 amount) internal {
FHEVMConfigStruct storage $ = Impl.getFHEVMConfig();
IFHEPayment($.FHEPaymentAddress).depositETH{value: amount}(address(this));
}

function withdrawToAccount(address account, uint256 amount) internal {
FHEVMConfigStruct storage $ = Impl.getFHEVMConfig();
IFHEPayment($.FHEPaymentAddress).withdrawETH(amount, account);
}

function withdrawToThis(uint256 amount) internal {
FHEVMConfigStruct storage $ = Impl.getFHEVMConfig();
IFHEPayment($.FHEPaymentAddress).withdrawETH(amount, address(this));
}

function getDepositedBalanceOfAccount(address account) internal view returns (uint256) {
FHEVMConfigStruct storage $ = Impl.getFHEVMConfig();
return IFHEPayment($.FHEPaymentAddress).getAvailableDepositsETH(account);
}

function getDepositedBalanceOfThis() internal view returns (uint256) {
FHEVMConfigStruct storage $ = Impl.getFHEVMConfig();
return IFHEPayment($.FHEPaymentAddress).getAvailableDepositsETH(address(this));
}
}
`;

return res;
}
6 changes: 3 additions & 3 deletions config/ZamaFHEVMConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {FHEVMConfigStruct} from "../lib/Impl.sol";
* @title ZamaFHEVMConfig.
* @notice This library returns the TFHE config for different networks
* with the contract addresses for
* (1) ACL, (2) TFHEExecutor, (3) FHEPayment, (4) KMSVerifier,
* (1) ACL, (2) TFHEExecutor, (3) KMSVerifier, (4) InputVerifier
* which are deployed & maintained by Zama.
*/
library ZamaFHEVMConfig {
Expand All @@ -17,8 +17,8 @@ library ZamaFHEVMConfig {
FHEVMConfigStruct({
ACLAddress: 0xFee8407e2f5e3Ee68ad77cAE98c434e637f516e5,
TFHEExecutorAddress: 0x687408aB54661ba0b4aeF3a44156c616c6955E07,
FHEPaymentAddress: 0xFb03BE574d14C256D56F09a198B586bdfc0A9de2,
KMSVerifierAddress: 0x9D6891A6240D6130c54ae243d8005063D05fE14b
KMSVerifierAddress: 0x9D6891A6240D6130c54ae243d8005063D05fE14b,
InputVerifierAddress: 0x3a2DA6f1daE9eF988B48d9CF27523FA31a8eBE50
});
}

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/gas.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ FHE operations in fhEVM are computationally intensive, resulting in higher gas c
2. **FHEGas**:
- Represents gas consumed by FHE-specific computations.
- A new synthetic kind of gas consumed by FHE-specific computations.
- FHEGas is tracked in each block by the FHEPayment contract to prevent DDOS attacks.
- FHEGas is tracked in each block by the FHEGasLimit contract to prevent DDOS attacks.
- If too many FHE operations are requested in the same block, the transaction will revert once the FHEGas block limit is reached.
- FHEGas is consistent across both mocked and real fhEVM environments.

Expand Down
8 changes: 4 additions & 4 deletions examples/FHEVMConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
pragma solidity ^0.8.24;

import "fhevm-core-contracts/addresses/ACLAddress.sol";
import "fhevm-core-contracts/addresses/FHEPaymentAddress.sol";
import "fhevm-core-contracts/addresses/InputVerifierAddress.sol";
import "fhevm-core-contracts/addresses/KMSVerifierAddress.sol";
import "fhevm-core-contracts/addresses/TFHEExecutorAddress.sol";
import "../lib/Impl.sol";

/**
* @title FHEVMConfig
* @notice This library returns all addresses for the ACL, TFHEExecutor, FHEPayment,
* @notice This library returns all addresses for the ACL, TFHEExecutor, InputVerifier,
* and KMSVerifier contracts.
*/
library FHEVMConfig {
Expand All @@ -22,8 +22,8 @@ library FHEVMConfig {
FHEVMConfigStruct({
ACLAddress: aclAdd,
TFHEExecutorAddress: tfheExecutorAdd,
FHEPaymentAddress: fhePaymentAdd,
KMSVerifierAddress: kmsVerifierAdd
KMSVerifierAddress: kmsVerifierAdd,
InputVerifierAddress: inputVerifierAdd
});
}
}
6 changes: 2 additions & 4 deletions examples/GatewayConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import "fhevm-core-contracts/addresses/GatewayContractAddress.sol";

/**
* @title FHEVMConfig
* @notice This library returns all addresses for the ACL, TFHEExecutor, FHEPayment,
* and KMSVerifier contracts.
* @notice This library returns the GatewayContract address
*/
library GatewayConfig {
/**
* @notice This function returns a struct containing all contract addresses.
* @dev It returns an immutable struct.
* @notice This function returns a the gateway contract address.
*/
function defaultGatewayContract() internal pure returns (address) {
return GATEWAY_CONTRACT_PREDEPLOY_ADDRESS;
Expand Down
5 changes: 1 addition & 4 deletions examples/PaymentLimit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ pragma solidity ^0.8.24;

import "../lib/TFHE.sol";
import "./FHEVMConfig.sol";
import "@openzeppelin/contracts/access/Ownable2Step.sol";
import "../payment/Payment.sol";

/// @title PaymentLimit
/// @notice A contract to demonstrate FHE gas limits in different scenarios
contract PaymentLimit {
/// @notice Constructor that sets up FHE configuration and deposits initial value
/// @dev Payable to allow initial deposit
constructor() payable {
constructor() {
TFHE.setFHEVM(FHEVMConfig.defaultConfig());
Payment.depositForThis(msg.value);
}

/// @notice Performs a small number of FHE operations
Expand Down
4 changes: 2 additions & 2 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ task('test', async (_taskArgs, hre, runSuper) => {
await hre.run('task:computeTFHEExecutorAddress', { privateKey: privKeyFhevmDeployer });
await hre.run('task:computeKMSVerifierAddress', { privateKey: privKeyFhevmDeployer });
await hre.run('task:computeInputVerifierAddress', { privateKey: privKeyFhevmDeployer, useAddress: false });
await hre.run('task:computeFHEPaymentAddress', { privateKey: privKeyFhevmDeployer });
await hre.run('task:computeFHEGasLimitAddress', { privateKey: privKeyFhevmDeployer });
const sourceDir = path.resolve(__dirname, 'node_modules/fhevm-core-contracts/contracts');
const destinationDir = path.resolve(__dirname, 'fhevmTemp/contracts');
fs.copySync(sourceDir, destinationDir, { dereference: true });
Expand All @@ -123,7 +123,7 @@ task('test', async (_taskArgs, hre, runSuper) => {
await hre.run('task:deployTFHEExecutor', { privateKey: privKeyFhevmDeployer });
await hre.run('task:deployKMSVerifier', { privateKey: privKeyFhevmDeployer });
await hre.run('task:deployInputVerifier', { privateKey: privKeyFhevmDeployer });
await hre.run('task:deployFHEPayment', { privateKey: privKeyFhevmDeployer });
await hre.run('task:deployFHEGasLimit', { privateKey: privKeyFhevmDeployer });
await hre.run('task:addSigners', {
numSigners: process.env.NUM_KMS_SIGNERS!,
privateKey: privKeyFhevmDeployer,
Expand Down
19 changes: 16 additions & 3 deletions lib/Impl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import "./TFHE.sol";
struct FHEVMConfigStruct {
address ACLAddress;
address TFHEExecutorAddress;
address FHEPaymentAddress;
address KMSVerifierAddress;
address InputVerifierAddress;
}

/**
Expand Down Expand Up @@ -70,6 +70,14 @@ interface IACL {
function allowForDecryption(uint256[] memory handlesList) external;
}

/**
* @title IInputVerifier
* @notice This interface contains the only function required from InputVerifier.
*/
interface IInputVerifier {
Copy link
Contributor

@PacificYield PacificYield Dec 23, 2024

Choose a reason for hiding this comment

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

/**
 * @title IInputVerifier
 * @notice  This interface contains the only function required from InputVerifier.
 */

Maybe something like this

function cleanTransientStorage() external;
}

/**
* @title Impl
* @notice This library is the core implementation for computing FHE operations (e.g. add, sub, xor).
Expand All @@ -88,8 +96,8 @@ library Impl {
FHEVMConfigStruct storage $ = getFHEVMConfig();
$.ACLAddress = fhevmConfig.ACLAddress;
$.TFHEExecutorAddress = fhevmConfig.TFHEExecutorAddress;
$.FHEPaymentAddress = fhevmConfig.FHEPaymentAddress;
$.KMSVerifierAddress = fhevmConfig.KMSVerifierAddress;
$.InputVerifierAddress = fhevmConfig.InputVerifierAddress;
}

function add(uint256 lhs, uint256 rhs, bool scalar) internal returns (uint256 result) {
Expand Down Expand Up @@ -387,11 +395,16 @@ library Impl {
IACL($.ACLAddress).allow(handle, account);
}

function cleanTransientStorage() internal {
function cleanTransientStorageACL() internal {
FHEVMConfigStruct storage $ = getFHEVMConfig();
IACL($.ACLAddress).cleanTransientStorage();
}

function cleanTransientStorageInputVerifier() internal {
FHEVMConfigStruct storage $ = getFHEVMConfig();
IInputVerifier($.InputVerifierAddress).cleanTransientStorage();
}

function isAllowed(uint256 handle, address account) internal view returns (bool) {
FHEVMConfigStruct storage $ = getFHEVMConfig();
return IACL($.ACLAddress).isAllowed(handle, account);
Expand Down
4 changes: 3 additions & 1 deletion lib/TFHE.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10849,9 +10849,11 @@ library TFHE {
}

// cleans the transient storage of ACL containing all the allowedTransient accounts
// also cleans transient storage of InputVerifier containing cached inputProofs
// to be used for integration with Account Abstraction or when bundling UserOps calling the FHEVMCoprocessor
function cleanTransientStorage() internal {
return Impl.cleanTransientStorage();
Impl.cleanTransientStorageACL();
Impl.cleanTransientStorageInputVerifier();
}

function isAllowed(ebool value, address account) internal view returns (bool) {
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"eslint-config-prettier": "^8.5.0",
"ethers": "^6.8.0",
"fhevmjs": "^0.6.0-8",
"fhevm-core-contracts": "0.6.1",
"fhevm-core-contracts": "0.7.0-0",
"hardhat": "^2.22.10",
"hardhat-deploy": "^0.11.29",
"hardhat-gas-reporter": "^1.0.2",
Expand Down
Loading
Loading