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

refactor: TFHEExecutor #141

Merged
merged 5 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions contracts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node_modules
# Hardhat files
/cache
/artifacts
/types

# TypeChain files
/typechain
Expand Down
679 changes: 0 additions & 679 deletions contracts/contracts/TFHEExecutor.events.sol

This file was deleted.

67 changes: 33 additions & 34 deletions contracts/contracts/TFHEExecutor.sol

Large diffs are not rendered by default.

215 changes: 215 additions & 0 deletions contracts/contracts/TFHEExecutorWithEvents.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity ^0.8.24;

import {TFHEExecutor} from "./TFHEExecutor.sol";

contract TFHEExecutorWithEvents is TFHEExecutor {
event FheAdd(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheSub(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheMul(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheDiv(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheRem(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheBitAnd(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheBitOr(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheBitXor(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheShl(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheShr(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheRotl(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheRotr(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheEq(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheEqBytes(uint256 lhs, bytes rhs, bytes1 scalarByte, uint256 result);
event FheNe(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheNeBytes(uint256 lhs, bytes rhs, bytes1 scalarByte, uint256 result);
event FheGe(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheGt(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheLe(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheLt(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheMin(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheMax(uint256 lhs, uint256 rhs, bytes1 scalarByte, uint256 result);
event FheNeg(uint256 ct, uint256 result);
event FheNot(uint256 ct, uint256 result);
event VerifyCiphertext(
bytes32 inputHandle,
address userAddress,
bytes inputProof,
bytes1 inputType,
uint256 result
);
event Cast(uint256 ct, bytes1 toType, uint256 result);
event TrivialEncrypt(uint256 pt, bytes1 toType, uint256 result);
event TrivialEncryptBytes(bytes pt, bytes1 toType, uint256 result);
event FheIfThenElse(uint256 control, uint256 ifTrue, uint256 ifFalse, uint256 result);
event FheRand(bytes1 randType, uint256 result);
event FheRandBounded(uint256 upperBound, bytes1 randType, uint256 result);

/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}

function fheAdd(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheAdd(lhs, rhs, scalarByte);
emit FheAdd(lhs, rhs, scalarByte, result);
}

function fheSub(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheSub(lhs, rhs, scalarByte);
emit FheSub(lhs, rhs, scalarByte, result);
}

function fheMul(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheMul(lhs, rhs, scalarByte);
emit FheMul(lhs, rhs, scalarByte, result);
}

function fheDiv(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheDiv(lhs, rhs, scalarByte);
emit FheDiv(lhs, rhs, scalarByte, result);
}

function fheRem(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheRem(lhs, rhs, scalarByte);
emit FheRem(lhs, rhs, scalarByte, result);
}

function fheBitAnd(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheBitAnd(lhs, rhs, scalarByte);
emit FheBitAnd(lhs, rhs, scalarByte, result);
}

function fheBitOr(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheBitOr(lhs, rhs, scalarByte);
emit FheBitOr(lhs, rhs, scalarByte, result);
}

function fheBitXor(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheBitXor(lhs, rhs, scalarByte);
emit FheBitXor(lhs, rhs, scalarByte, result);
}

function fheShl(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheShl(lhs, rhs, scalarByte);
emit FheShl(lhs, rhs, scalarByte, result);
}

function fheShr(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheShr(lhs, rhs, scalarByte);
emit FheShr(lhs, rhs, scalarByte, result);
}

function fheRotl(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheRotl(lhs, rhs, scalarByte);
emit FheRotl(lhs, rhs, scalarByte, result);
}

function fheRotr(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheRotr(lhs, rhs, scalarByte);
emit FheRotr(lhs, rhs, scalarByte, result);
}

function fheEq(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheEq(lhs, rhs, scalarByte);
emit FheEq(lhs, rhs, scalarByte, result);
}

function fheEq(uint256 lhs, bytes memory rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheEq(lhs, rhs, scalarByte);
emit FheEqBytes(lhs, rhs, scalarByte, result);
}

function fheNe(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheNe(lhs, rhs, scalarByte);
emit FheNe(lhs, rhs, scalarByte, result);
}

function fheNe(uint256 lhs, bytes memory rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheNe(lhs, rhs, scalarByte);
emit FheNeBytes(lhs, rhs, scalarByte, result);
}

function fheGe(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheGe(lhs, rhs, scalarByte);
emit FheGe(lhs, rhs, scalarByte, result);
}

function fheGt(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheGt(lhs, rhs, scalarByte);
emit FheGt(lhs, rhs, scalarByte, result);
}

function fheLe(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheLe(lhs, rhs, scalarByte);
emit FheLe(lhs, rhs, scalarByte, result);
}

function fheLt(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheLt(lhs, rhs, scalarByte);
emit FheLt(lhs, rhs, scalarByte, result);
}

function fheMin(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheMin(lhs, rhs, scalarByte);
emit FheMin(lhs, rhs, scalarByte, result);
}

function fheMax(uint256 lhs, uint256 rhs, bytes1 scalarByte) public virtual override returns (uint256 result) {
result = super.fheMax(lhs, rhs, scalarByte);
emit FheMax(lhs, rhs, scalarByte, result);
}

function fheNeg(uint256 ct) public virtual override returns (uint256 result) {
result = super.fheNeg(ct);
emit FheNeg(ct, result);
}

function fheNot(uint256 ct) public virtual override returns (uint256 result) {
result = super.fheNot(ct);
emit FheNot(ct, result);
}

function verifyCiphertext(
bytes32 inputHandle,
address userAddress,
bytes memory inputProof,
bytes1 inputType
) public virtual override returns (uint256 result) {
result = super.verifyCiphertext(inputHandle, userAddress, inputProof, inputType);
emit VerifyCiphertext(inputHandle, userAddress, inputProof, inputType, result);
}

function cast(uint256 ct, bytes1 toType) public virtual override returns (uint256 result) {
result = super.cast(ct, toType);
emit Cast(ct, toType, result);
}

function trivialEncrypt(uint256 pt, bytes1 toType) public virtual override returns (uint256 result) {
result = super.trivialEncrypt(pt, toType);
emit TrivialEncrypt(pt, toType, result);
}

function trivialEncrypt(bytes memory pt, bytes1 toType) public virtual override returns (uint256 result) {
result = super.trivialEncrypt(pt, toType);
emit TrivialEncryptBytes(pt, toType, result);
}

function fheIfThenElse(
uint256 control,
uint256 ifTrue,
uint256 ifFalse
) public virtual override returns (uint256 result) {
result = super.fheIfThenElse(control, ifTrue, ifFalse);
emit FheIfThenElse(control, ifTrue, ifFalse, result);
}

function fheRand(bytes1 randType) public virtual override returns (uint256 result) {
result = super.fheRand(randType);
emit FheRand(randType, result);
}

function fheRandBounded(uint256 upperBound, bytes1 randType) public virtual override returns (uint256 result) {
result = super.fheRandBounded(upperBound, randType);
emit FheRandBounded(upperBound, randType, result);
}

function _authorizeUpgrade(address _newImplementation) internal virtual override onlyOwner {}
}
2 changes: 1 addition & 1 deletion contracts/tasks/taskDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ task('task:deployTFHEExecutor')
if (process.env.HARDHAT_TFHEEXECUTOR_EVENTS !== '1') {
factory = await ethers.getContractFactory('contracts/TFHEExecutor.sol:TFHEExecutor', deployer);
} else {
factory = await ethers.getContractFactory('contracts/TFHEExecutor.events.sol:TFHEExecutor', deployer);
factory = await ethers.getContractFactory('contracts/TFHEExecutorWithEvents.sol:TFHEExecutorWithEvents', deployer);
}
const exec = await upgrades.deployProxy(factory, [deployer.address], { initializer: 'initialize', kind: 'uups' });
await exec.waitForDeployment();
Expand Down
2 changes: 1 addition & 1 deletion contracts/test/coprocessorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ async function insertHandleFromEvent(event: FHEVMEvent) {

export function getFHEGasFromTxReceipt(receipt: ethers.TransactionReceipt): number {
if (process.env.HARDHAT_TFHEEXECUTOR_EVENTS !== '1') {
throw Error('FHEGas tracking is currently implemented only with TFHEExecutor.events.sol variant');
throw Error('FHEGas tracking is currently implemented only with TFHEExecutorWithEvents.sol variant');
}
if (receipt.status === 0) {
throw new Error('Transaction reverted');
Expand Down
4 changes: 2 additions & 2 deletions contracts/test/encryptedERC20/EncryptedERC20.FHEGas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('EncryptedERC20:FHEGas', function () {
const t2 = await tx.wait();
expect(t2?.status).to.eq(1);
if (process.env.HARDHAT_TFHEEXECUTOR_EVENTS === '1') {
// FHEGas tracking is currently implemented only with TFHEExecutor.events.sol variant
// FHEGas tracking is currently implemented only with TFHEExecutorWithEvents.sol variant
const FHEGasConsumedTransfer = getFHEGasFromTxReceipt(t2);
console.log('FHEGas Consumed in transfer', FHEGasConsumedTransfer);
console.log('Native Gas Consumed in transfer', t2.gasUsed);
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('EncryptedERC20:FHEGas', function () {
);
const t3 = await tx3.wait();
if (process.env.HARDHAT_TFHEEXECUTOR_EVENTS === '1') {
// FHEGas tracking is currently implemented only with TFHEExecutor.events.sol variant
// FHEGas tracking is currently implemented only with TFHEExecutorWithEvents.sol variant
const FHEGasConsumedTransferFrom = getFHEGasFromTxReceipt(t3);
console.log('FHEGas Consumed in transferFrom', FHEGasConsumedTransferFrom);
console.log('Native Gas Consumed in transferFrom', t3.gasUsed);
Expand Down