Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

Commit bf7ed04

Browse files
committed
refactor: support enum TFHEExecutor (WIP)
1 parent 910d092 commit bf7ed04

File tree

8 files changed

+1040
-912
lines changed

8 files changed

+1040
-912
lines changed

contracts/codegen/operatorsPrices.json

Lines changed: 292 additions & 292 deletions
Large diffs are not rendered by default.

contracts/codegen/payments.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export function generateSolidityFHEGasLimit(priceData: PriceData): string {
1616
import {Ownable2StepUpgradeable} from "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";
1717
import {tfheExecutorAdd} from "../addresses/TFHEExecutorAddress.sol";
1818
19+
import {FheType} from "../lib/TFHE.sol";
20+
1921
/**
2022
* @title FHEGasLimit
2123
* @notice This contract manages the amount of gas to be paid for FHE operations.
@@ -76,7 +78,7 @@ contract FHEGasLimit is UUPSUpgradeable, Ownable2StepUpgradeable {
7678
* @param resultType Result type.
7779
* @param scalarByte Scalar byte.
7880
*/
79-
function ${functionName}(uint8 resultType, bytes1 scalarByte) external virtual {
81+
function ${functionName}(FheType resultType, bytes1 scalarByte) external virtual {
8082
if(msg.sender != tfheExecutorAddress) revert CallerMustBeTFHEExecutorContract();
8183
_checkIfNewBlock();
8284
`;
@@ -85,7 +87,7 @@ contract FHEGasLimit is UUPSUpgradeable, Ownable2StepUpgradeable {
8587
* @notice Computes the gas required for ${operation.charAt(0).toUpperCase() + operation.slice(1)}.
8688
* @param resultType Result type.
8789
*/
88-
function ${functionName}(uint8 resultType) external virtual {
90+
function ${functionName}(FheType resultType) external virtual {
8991
if(msg.sender != tfheExecutorAddress) revert CallerMustBeTFHEExecutorContract();
9092
_checkIfNewBlock();
9193
`;
@@ -192,7 +194,7 @@ function generatePriceChecks(prices: { [key: string]: number }): string {
192194
return (
193195
Object.entries(prices)
194196
.map(
195-
([resultType, price]) => ` if (resultType == ${resultType}) {
197+
([resultType, price]) => ` if (resultType == FheType.${resultType}) {
196198
_updateFunding(${price});
197199
}`,
198200
)

contracts/codegen/templates.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ export function generateSolidityImplLib(operators: Operator[]): string {
8484
// SPDX-License-Identifier: BSD-3-Clause-Clear
8585
pragma solidity ^0.8.24;
8686
87+
import {FheType} from "./TFHE.sol";
88+
8789
${generateImplCoprocessorInterface(operators)}
8890
8991
${generateACLInterface()}
@@ -206,31 +208,31 @@ function coprocessorInterfaceCustomFunctions(): string {
206208
* @param inputType Input type.
207209
* @return result Result.
208210
*/
209-
function verifyCiphertext(bytes32 inputHandle, address callerAddress, bytes memory inputProof, bytes1 inputType) external returns (bytes32 result);
211+
function verifyCiphertext(bytes32 inputHandle, address callerAddress, bytes memory inputProof, FheType inputType) external returns (bytes32 result);
210212
211213
/**
212214
* @notice Performs the casting to a target type.
213215
* @param ct Value to cast.
214216
* @param toType Target type.
215217
* @return result Result value of the target type.
216218
*/
217-
function cast(bytes32 ct, bytes1 toType) external returns (bytes32 result);
219+
function cast(bytes32 ct, FheType toType) external returns (bytes32 result);
218220
219221
/**
220222
* @notice Does trivial encryption.
221223
* @param ct Value to encrypt.
222224
* @param toType Target type.
223225
* @return result Result value of the target type.
224226
*/
225-
function trivialEncrypt(uint256 ct, bytes1 toType) external returns (bytes32 result);
227+
function trivialEncrypt(uint256 ct, FheType toType) external returns (bytes32 result);
226228
227229
/**
228230
* @notice Does trivial encryption.
229231
* @param ct Value to encrypt.
230232
* @param toType Target type.
231233
* @return result Result value of the target type.
232234
*/
233-
function trivialEncrypt(bytes memory ct, bytes1 toType) external returns (bytes32 result);
235+
function trivialEncrypt(bytes memory ct, FheType toType) external returns (bytes32 result);
234236
235237
/**
236238
* @notice Computes FHEEq operation.
@@ -264,15 +266,15 @@ function coprocessorInterfaceCustomFunctions(): string {
264266
* @param randType Type for the random result.
265267
* @return result Result.
266268
*/
267-
function fheRand(bytes1 randType) external returns (bytes32 result);
269+
function fheRand(FheType randType) external returns (bytes32 result);
268270
269271
/**
270272
* @notice Computes FHERandBounded operation.
271273
* @param upperBound Upper bound value.
272274
* @param randType Type for the random result.
273275
* @return result Result.
274276
*/
275-
function fheRandBounded(uint256 upperBound, bytes1 randType) external returns (bytes32 result);
277+
function fheRandBounded(uint256 upperBound, FheType randType) external returns (bytes32 result);
276278
`;
277279
}
278280

@@ -767,7 +769,7 @@ function handleSolidityTFHECustomCastBetweenTwoEuint(inputFheType: FheType, outp
767769
* @dev Casts an encrypted integer from '${inputFheType.type.toLowerCase()}' to '${outputFheType.type.toLowerCase()}'.
768770
*/
769771
function as${outputFheType.type}(${inputFheType.type.toLowerCase()} value) internal returns (${outputFheType.type.toLowerCase()}) {
770-
return ${outputFheType.type.toLowerCase()}.wrap(Impl.cast(${inputFheType.type.toLowerCase()}.unwrap(value), uint8(FheType.${outputFheType.type})));
772+
return ${outputFheType.type.toLowerCase()}.wrap(Impl.cast(${inputFheType.type.toLowerCase()}.unwrap(value), FheType.${outputFheType.type}));
771773
}
772774
`;
773775
}
@@ -782,7 +784,7 @@ function handleSolidityTFHECustomCastBetweenEboolAndEuint(fheType: FheType): str
782784
* @dev Converts an 'ebool' to an '${fheType.type.toLowerCase()}'.
783785
*/
784786
function as${fheType.type}(ebool b) internal returns (${fheType.type.toLowerCase()}) {
785-
return ${fheType.type.toLowerCase()}.wrap(Impl.cast(ebool.unwrap(b), uint8(FheType.${fheType.type})));
787+
return ${fheType.type.toLowerCase()}.wrap(Impl.cast(ebool.unwrap(b), FheType.${fheType.type}));
786788
}
787789
`);
788790

@@ -838,7 +840,7 @@ function handleSolidityTFHEConvertPlaintextAndEinputToRespectiveType(fheType: Fh
838840
* @dev Convert an inputHandle with corresponding inputProof to an encrypted ${fheType.type.toLowerCase()} integer.
839841
*/
840842
function as${fheType.type}(einput inputHandle, bytes memory inputProof) internal returns (${fheType.type.toLowerCase()}) {
841-
return ${fheType.type.toLowerCase()}.wrap(Impl.verify(einput.unwrap(inputHandle), inputProof, uint8(FheType.${fheType.isAlias ? fheType.aliasType : fheType.type})));
843+
return ${fheType.type.toLowerCase()}.wrap(Impl.verify(einput.unwrap(inputHandle), inputProof, FheType.${fheType.isAlias ? fheType.aliasType : fheType.type}));
842844
}
843845
844846
`;
@@ -850,7 +852,7 @@ function handleSolidityTFHEConvertPlaintextAndEinputToRespectiveType(fheType: Fh
850852
* @dev Converts a plaintext boolean to an encrypted boolean.
851853
*/
852854
function asEbool(bool value) internal returns (ebool) {
853-
return ebool.wrap(Impl.trivialEncrypt(value? 1 : 0, uint8(FheType.Ebool)));
855+
return ebool.wrap(Impl.trivialEncrypt(value? 1 : 0, FheType.Ebool));
854856
}
855857
856858
`;
@@ -860,7 +862,7 @@ function handleSolidityTFHEConvertPlaintextAndEinputToRespectiveType(fheType: Fh
860862
* @dev Convert the plaintext bytes to a ${fheType.type.toLowerCase()} value.
861863
*/
862864
function as${fheType.type}(${fheType.clearMatchingType} value) internal returns (${fheType.type.toLowerCase()}) {
863-
return ${fheType.type.toLowerCase()}.wrap(Impl.trivialEncrypt(value, uint8(FheType.${fheType.isAlias ? fheType.aliasType : fheType.type})));
865+
return ${fheType.type.toLowerCase()}.wrap(Impl.trivialEncrypt(value, FheType.${fheType.isAlias ? fheType.aliasType : fheType.type}));
864866
}
865867
`;
866868
} else {
@@ -874,7 +876,7 @@ function handleSolidityTFHEConvertPlaintextAndEinputToRespectiveType(fheType: Fh
874876
* @dev Convert a plaintext value to an encrypted ${fheType.type.toLowerCase()} integer.
875877
*/
876878
function as${fheType.type}(${fheType.clearMatchingType} value) internal returns (${fheType.type.toLowerCase()}) {
877-
return ${fheType.type.toLowerCase()}.wrap(Impl.trivialEncrypt(uint256(${value}), uint8(FheType.${fheType.isAlias ? fheType.aliasType : fheType.type})));
879+
return ${fheType.type.toLowerCase()}.wrap(Impl.trivialEncrypt(uint256(${value}), FheType.${fheType.isAlias ? fheType.aliasType : fheType.type}));
878880
}
879881
880882
`;
@@ -1018,10 +1020,10 @@ function generateCustomMethodsForImpl(): string {
10181020
function verify(
10191021
bytes32 inputHandle,
10201022
bytes memory inputProof,
1021-
uint8 toType
1023+
FheType toType
10221024
) internal returns (bytes32 result) {
10231025
FHEVMConfigStruct storage $ = getFHEVMConfig();
1024-
result = ITFHEExecutor($.TFHEExecutorAddress).verifyCiphertext(inputHandle, msg.sender, inputProof, bytes1(toType));
1026+
result = ITFHEExecutor($.TFHEExecutorAddress).verifyCiphertext(inputHandle, msg.sender, inputProof, toType);
10251027
IACL($.ACLAddress).allowTransient(result, msg.sender);
10261028
}
10271029
@@ -1033,10 +1035,10 @@ function generateCustomMethodsForImpl(): string {
10331035
*/
10341036
function cast(
10351037
bytes32 ciphertext,
1036-
uint8 toType
1038+
FheType toType
10371039
) internal returns (bytes32 result) {
10381040
FHEVMConfigStruct storage $ = getFHEVMConfig();
1039-
result = ITFHEExecutor($.TFHEExecutorAddress).cast(ciphertext, bytes1(toType));
1041+
result = ITFHEExecutor($.TFHEExecutorAddress).cast(ciphertext, toType);
10401042
}
10411043
10421044
/**
@@ -1047,10 +1049,10 @@ function generateCustomMethodsForImpl(): string {
10471049
*/
10481050
function trivialEncrypt(
10491051
uint256 value,
1050-
uint8 toType
1052+
FheType toType
10511053
) internal returns (bytes32 result) {
10521054
FHEVMConfigStruct storage $ = getFHEVMConfig();
1053-
result = ITFHEExecutor($.TFHEExecutorAddress).trivialEncrypt(value, bytes1(toType));
1055+
result = ITFHEExecutor($.TFHEExecutorAddress).trivialEncrypt(value, toType);
10541056
}
10551057
10561058
/**
@@ -1061,10 +1063,10 @@ function generateCustomMethodsForImpl(): string {
10611063
*/
10621064
function trivialEncrypt(
10631065
bytes memory value,
1064-
uint8 toType
1066+
FheType toType
10651067
) internal returns (bytes32 result) {
10661068
FHEVMConfigStruct storage $ = getFHEVMConfig();
1067-
result = ITFHEExecutor($.TFHEExecutorAddress).trivialEncrypt(value, bytes1(toType));
1069+
result = ITFHEExecutor($.TFHEExecutorAddress).trivialEncrypt(value, toType);
10681070
}
10691071
10701072
/**
@@ -1103,14 +1105,14 @@ function generateCustomMethodsForImpl(): string {
11031105
result = ITFHEExecutor($.TFHEExecutorAddress).fheNe(lhs, rhs, scalarByte);
11041106
}
11051107
1106-
function rand(uint8 randType) internal returns(bytes32 result) {
1108+
function rand(FheType randType) internal returns(bytes32 result) {
11071109
FHEVMConfigStruct storage $ = getFHEVMConfig();
1108-
result = ITFHEExecutor($.TFHEExecutorAddress).fheRand(bytes1(randType));
1110+
result = ITFHEExecutor($.TFHEExecutorAddress).fheRand(randType);
11091111
}
11101112
1111-
function randBounded(uint256 upperBound, uint8 randType) internal returns(bytes32 result) {
1113+
function randBounded(uint256 upperBound, FheType randType) internal returns(bytes32 result) {
11121114
FHEVMConfigStruct storage $ = getFHEVMConfig();
1113-
result = ITFHEExecutor($.TFHEExecutorAddress).fheRandBounded(upperBound, bytes1(randType));
1115+
result = ITFHEExecutor($.TFHEExecutorAddress).fheRandBounded(upperBound, randType);
11141116
}
11151117
11161118
/**
@@ -1181,7 +1183,7 @@ function handleSolidityTFHERand(fheType: FheType): string {
11811183
* @dev Generates a random encrypted value.
11821184
*/
11831185
function rand${fheType.type}() internal returns (${fheType.type.toLowerCase()}) {
1184-
return ${fheType.type.toLowerCase()}.wrap(Impl.rand(uint8(FheType.${fheType.isAlias ? fheType.aliasType : fheType.type})));
1186+
return ${fheType.type.toLowerCase()}.wrap(Impl.rand(FheType.${fheType.isAlias ? fheType.aliasType : fheType.type}));
11851187
}
11861188
11871189
`;
@@ -1194,7 +1196,7 @@ function handleSolidityTFHERand(fheType: FheType): string {
11941196
* The upperBound must be a power of 2.
11951197
*/
11961198
function rand${fheType.type}(uint${fheType.bitLength} upperBound) internal returns (${fheType.type.toLowerCase()}) {
1197-
return ${fheType.type.toLowerCase()}.wrap(Impl.randBounded(upperBound, uint8(FheType.${fheType.isAlias ? fheType.aliasType : fheType.type})));
1199+
return ${fheType.type.toLowerCase()}.wrap(Impl.randBounded(upperBound, FheType.${fheType.isAlias ? fheType.aliasType : fheType.type}));
11981200
}
11991201
12001202
`;

0 commit comments

Comments
 (0)