From 8695c0bb24631b3906d23d204fe1229f0620883a Mon Sep 17 00:00:00 2001 From: David Date: Sat, 4 Nov 2023 11:41:21 +0800 Subject: [PATCH 1/3] Custom parameters for fhevm-go --- fhevm/evm.go | 2 +- fhevm/instructions.go | 4 +- fhevm/interface.go | 1 + fhevm/params.go | 209 ++++++++++++++++++++++++------------------ fhevm/precompiles.go | 134 +++++---------------------- fhevm/tfhe.go | 32 +++---- fhevm/tfhe_test.go | 84 ++++++++--------- 7 files changed, 208 insertions(+), 258 deletions(-) diff --git a/fhevm/evm.go b/fhevm/evm.go index e986d12..66e4dfd 100644 --- a/fhevm/evm.go +++ b/fhevm/evm.go @@ -111,7 +111,7 @@ func importCiphertext(environment EVMEnvironment, ct *tfheCiphertext) *verifiedC return importCiphertextToEVM(environment, ct) } -func importRandomCiphertext(environment EVMEnvironment, t fheUintType) []byte { +func importRandomCiphertext(environment EVMEnvironment, t FheUintType) []byte { nextCtHash := &environment.GetFhevmData().nextCiphertextHashOnGasEst ctHashBytes := crypto.Keccak256(nextCtHash.Bytes()) handle := common.BytesToHash(ctHashBytes) diff --git a/fhevm/instructions.go b/fhevm/instructions.go index 3f479e3..7ccef83 100644 --- a/fhevm/instructions.go +++ b/fhevm/instructions.go @@ -29,7 +29,7 @@ func contains(haystack []byte, needle []byte) bool { type ciphertextMetadata struct { refCount uint64 length uint64 - fheUintType fheUintType + fheUintType FheUintType } func (m ciphertextMetadata) serialize() [32]byte { @@ -45,7 +45,7 @@ func (m *ciphertextMetadata) deserialize(buf [32]byte) *ciphertextMetadata { u.SetBytes(buf[:]) m.refCount = u[0] m.length = u[1] - m.fheUintType = fheUintType(u[2]) + m.fheUintType = FheUintType(u[2]) return m } diff --git a/fhevm/interface.go b/fhevm/interface.go index 75c8285..92b6633 100644 --- a/fhevm/interface.go +++ b/fhevm/interface.go @@ -32,6 +32,7 @@ type EVMEnvironment interface { CreateContract2(caller common.Address, code []byte, codeHash common.Hash, gas uint64, value *big.Int, address common.Address) ([]byte, common.Address, uint64, error) GetFhevmData() *FhevmData + FhevmParams() *FhevmParams } type FhevmData struct { diff --git a/fhevm/params.go b/fhevm/params.go index 88ef8ca..e41dc62 100644 --- a/fhevm/params.go +++ b/fhevm/params.go @@ -18,102 +18,135 @@ // // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . - package fhevm -// This file contains default gas costs of fhEVM-related operations. -// Users can change the values based on specific requirements in their blockchain. - -// Base gas costs of existing EVM operations. Used for setting gas costs relative to them. -// These constants are used just for readability. const EvmNetSstoreInitGas uint64 = 20000 -const ColdSloadCostEIP2929 uint64 = 2100 - -var ( - // FHE operation costs depend on tfhe-rs performance and hardware acceleration. These values will most certainly change. - FheUint8AddSubGas uint64 = 83000 - FheUint16AddSubGas uint64 = 108000 - FheUint32AddSubGas uint64 = 130000 - FheUint8MulGas uint64 = 150000 - FheUint16MulGas uint64 = 200000 - FheUint32MulGas uint64 = 270000 - // Div and Rem currently only support a plaintext divisor and below gas costs reflect that case only. - FheUint8DivGas uint64 = 200000 - FheUint16DivGas uint64 = 250000 - FheUint32DivGas uint64 = 350000 - FheUint8RemGas uint64 = 200000 - FheUint16RemGas uint64 = 250000 - FheUint32RemGas uint64 = 350000 - FheUint8BitwiseGas uint64 = 20000 - FheUint16BitwiseGas uint64 = 21000 - FheUint32BitwiseGas uint64 = 22000 - FheUint8ShiftGas uint64 = 105000 - FheUint16ShiftGas uint64 = 128000 - FheUint32ShiftGas uint64 = 160000 - FheUint8LeGas uint64 = 61000 - FheUint16LeGas uint64 = 83000 - FheUint32LeGas uint64 = 109000 - FheUint8MinMaxGas uint64 = 108000 - FheUint16MinMaxGas uint64 = 134000 - FheUint32MinMaxGas uint64 = 150000 - FheUint8NegNotGas uint64 = 83000 - FheUint16NegNotGas uint64 = 108000 - FheUint32NegNotGas uint64 = 130000 - - // TODO: Costs will depend on the complexity of doing reencryption/decryption by the oracle. - FheUint8ReencryptGas uint64 = 320000 - FheUint16ReencryptGas uint64 = 320400 - FheUint32ReencryptGas uint64 = 320800 - FheUint8DecryptGas uint64 = 320000 - FheUint16DecryptGas uint64 = 320400 - FheUint32DecryptGas uint64 = 320800 - - // As of now, verification costs only cover ciphertext deserialization and assume there is no ZKPoK to verify. - FheUint8VerifyGas uint64 = 200 - FheUint16VerifyGas uint64 = 300 - FheUint32VerifyGas uint64 = 400 - - // TODO: Cost will depend on the complexity of doing decryption by the oracle. - FheUint8RequireGas uint64 = 320000 - FheUint16RequireGas uint64 = 320400 - FheUint32RequireGas uint64 = 320800 - - // TODO: As of now, only support FheUint8. All optimistic require predicates are - // downcast to FheUint8 at the solidity level. Eventually move to ebool. - // If there is at least one optimistic require, we need to decrypt it as it was a normal FHE require. - // For every subsequent optimistic require, we need to bitand it with the current require value - that - // works, because we assume requires have a value of 0 or 1. - FheUint8OptimisticRequireGas uint64 = FheUint8RequireGas - FheUint8OptimisticRequireBitandGas uint64 = FheUint8BitwiseGas - // TODO: These will change once we have an FHE-based random generaration. - FheUint8RandGas uint64 = EvmNetSstoreInitGas + 1000 - FheUint16RandGas uint64 = FheUint8RandGas + 1000 - FheUint32RandGas uint64 = FheUint16RandGas + 1000 - - // TODO: The values here are chosen somewhat arbitrarily (at least the 8 bit ones). Also, we don't - // take into account whether a ciphertext existed (either "current" or "original") for the given handle. - // Finally, costs are likely to change in the future. - FheUint8ProtectedStorageSstoreGas uint64 = EvmNetSstoreInitGas + 2000 - FheUint16ProtectedStorageSstoreGas uint64 = FheUint8ProtectedStorageSstoreGas * 2 - FheUint32ProtectedStorageSstoreGas uint64 = FheUint16ProtectedStorageSstoreGas * 2 - - // TODO: We don't take whether the slot is cold or warm into consideration. - FheUint8ProtectedStorageSloadGas uint64 = ColdSloadCostEIP2929 + 200 - FheUint16ProtectedStorageSloadGas uint64 = FheUint8ProtectedStorageSloadGas * 2 - FheUint32ProtectedStorageSloadGas uint64 = FheUint16ProtectedStorageSloadGas * 2 +func DefaultFhevmParams() FhevmParams { + return FhevmParams{ + GasCosts: DefaultGasCosts(), + } +} - FheCastGas uint64 = 100 +type FhevmParams struct { + GasCosts GasCosts +} - FhePubKeyGas uint64 = 2 +type GasCosts struct { + FheCast uint64 + FhePubKey uint64 + FheAddSub map[FheUintType]uint64 + FheDecrypt map[FheUintType]uint64 + FheBitwiseOp map[FheUintType]uint64 + FheMul map[FheUintType]uint64 + FheDiv map[FheUintType]uint64 + FheRem map[FheUintType]uint64 + FheShift map[FheUintType]uint64 + FheLe map[FheUintType]uint64 + FheMinMax map[FheUintType]uint64 + FheNegNot map[FheUintType]uint64 + FheReencrypt map[FheUintType]uint64 + FheTrivialEncrypt map[FheUintType]uint64 + FheRand map[FheUintType]uint64 + FheVerify map[FheUintType]uint64 + FheOptRequire map[FheUintType]uint64 + FheOptRequireBitAnd map[FheUintType]uint64 +} - FheUint8TrivialEncryptGas uint64 = 100 - FheUint16TrivialEncryptGas uint64 = 200 - FheUint32TrivialEncryptGas uint64 = 400 +func DefaultGasCosts() GasCosts { + return GasCosts{ + FheAddSub: map[FheUintType]uint64{ + FheUint8: 83000, + FheUint16: 108000, + FheUint32: 130000, + }, + FheDecrypt: map[FheUintType]uint64{ + FheUint8: 600, + FheUint16: 700, + FheUint32: 800, + }, + FheBitwiseOp: map[FheUintType]uint64{ + FheUint8: 20000, + FheUint16: 21000, + FheUint32: 22000, + }, + FheMul: map[FheUintType]uint64{ + FheUint8: 150000, + FheUint16: 200000, + FheUint32: 270000, + }, + FheDiv: map[FheUintType]uint64{ + FheUint8: 1370000, + FheUint16: 3500000, + FheUint32: 9120000, + }, + FheRem: map[FheUintType]uint64{ + FheUint8: 1370000, // TODO: check again rem gas + FheUint16: 3500000, + FheUint32: 9120000, + }, + FheShift: map[FheUintType]uint64{ + FheUint8: 105000, + FheUint16: 128000, + FheUint32: 160000, + }, + FheLe: map[FheUintType]uint64{ + FheUint8: 61000, + FheUint16: 83000, + FheUint32: 109000, + }, + FheMinMax: map[FheUintType]uint64{ + FheUint8: 108000, + FheUint16: 134000, + FheUint32: 150000, + }, + FheNegNot: map[FheUintType]uint64{ + FheUint8: 83000, + FheUint16: 108000, + FheUint32: 130000, + }, + // TODO: Costs will depend on the complexity of doing reencryption/decryption by the oracle. + FheReencrypt: map[FheUintType]uint64{ + FheUint8: 1000, + FheUint16: 1100, + FheUint32: 1200, + }, + // As of now, verification costs only cover ciphertext deserialization and assume there is no ZKPoK to verify. + FheVerify: map[FheUintType]uint64{ + FheUint8: 200, + FheUint16: 300, + FheUint32: 400, + }, + FheTrivialEncrypt: map[FheUintType]uint64{ + FheUint8: 100, + FheUint16: 200, + FheUint32: 300, + }, + // TODO: These will change once we have an FHE-based random generaration. + FheRand: map[FheUintType]uint64{ + FheUint8: EvmNetSstoreInitGas + 1000, + FheUint16: EvmNetSstoreInitGas + 2000, + FheUint32: EvmNetSstoreInitGas + 3000, + }, + // TODO: As of now, only support FheUint8. All optimistic require predicates are + // downcast to FheUint8 at the solidity level. Eventually move to ebool. + // If there is at least one optimistic require, we need to decrypt it as it was a normal FHE require. + // For every subsequent optimistic require, we need to bitand it with the current require value - that + // works, because we assume requires have a value of 0 or 1. + FheOptRequire: map[FheUintType]uint64{ + FheUint8: 170000, + FheUint16: 180000, + FheUint32: 190000, + }, + FheOptRequireBitAnd: map[FheUintType]uint64{ + FheUint8: 20000, + FheUint16: 20000, + FheUint32: 20000, + }, + } +} - // A byte of data attached to a transaction has fractional cost: 1 / TxDataFractionalGasFactor. - TxDataFractionalGasFactor uint64 = 4 -) +var TxDataFractionalGasFactor uint64 = 4 func TxDataFractionalGas(originalGas uint64) (fractionalGas uint64) { return originalGas / TxDataFractionalGasFactor diff --git a/fhevm/precompiles.go b/fhevm/precompiles.go index a87ba99..0963e33 100644 --- a/fhevm/precompiles.go +++ b/fhevm/precompiles.go @@ -285,90 +285,6 @@ func FheLibRun(environment EVMEnvironment, caller common.Address, addr common.Ad } } -var fheAddSubGasCosts = map[fheUintType]uint64{ - FheUint8: FheUint8AddSubGas, - FheUint16: FheUint16AddSubGas, - FheUint32: FheUint32AddSubGas, -} - -var fheDecryptGasCosts = map[fheUintType]uint64{ - FheUint8: FheUint8DecryptGas, - FheUint16: FheUint16DecryptGas, - FheUint32: FheUint32DecryptGas, -} - -var fheBitwiseOpGasCosts = map[fheUintType]uint64{ - FheUint8: FheUint8BitwiseGas, - FheUint16: FheUint16BitwiseGas, - FheUint32: FheUint32BitwiseGas, -} - -var fheMulGasCosts = map[fheUintType]uint64{ - FheUint8: FheUint8MulGas, - FheUint16: FheUint16MulGas, - FheUint32: FheUint32MulGas, -} - -var fheDivGasCosts = map[fheUintType]uint64{ - FheUint8: FheUint8DivGas, - FheUint16: FheUint16DivGas, - FheUint32: FheUint32DivGas, -} - -var fheRemGasCosts = map[fheUintType]uint64{ - FheUint8: FheUint8RemGas, - FheUint16: FheUint16RemGas, - FheUint32: FheUint32RemGas, -} - -var fheShiftGasCosts = map[fheUintType]uint64{ - FheUint8: FheUint8ShiftGas, - FheUint16: FheUint16ShiftGas, - FheUint32: FheUint32ShiftGas, -} - -var fheLeGasCosts = map[fheUintType]uint64{ - FheUint8: FheUint8LeGas, - FheUint16: FheUint16LeGas, - FheUint32: FheUint32LeGas, -} - -var fheMinMaxGasCosts = map[fheUintType]uint64{ - FheUint8: FheUint8MinMaxGas, - FheUint16: FheUint16MinMaxGas, - FheUint32: FheUint32MinMaxGas, -} - -var fheNegNotGasCosts = map[fheUintType]uint64{ - FheUint8: FheUint8NegNotGas, - FheUint16: FheUint16NegNotGas, - FheUint32: FheUint32NegNotGas, -} - -var fheReencryptGasCosts = map[fheUintType]uint64{ - FheUint8: FheUint8ReencryptGas, - FheUint16: FheUint16ReencryptGas, - FheUint32: FheUint32ReencryptGas, -} - -var fheVerifyGasCosts = map[fheUintType]uint64{ - FheUint8: FheUint8VerifyGas, - FheUint16: FheUint16VerifyGas, - FheUint32: FheUint32VerifyGas, -} - -var fheTrivialEncryptGasCosts = map[fheUintType]uint64{ - FheUint8: FheUint8TrivialEncryptGas, - FheUint16: FheUint16TrivialEncryptGas, - FheUint32: FheUint32TrivialEncryptGas, -} - -var fheRandGasCosts = map[fheUintType]uint64{ - FheUint8: FheUint8RandGas, - FheUint16: FheUint16RandGas, - FheUint32: FheUint32RandGas, -} - // Gas costs func fheAddSubRequiredGas(environment EVMEnvironment, input []byte) uint64 { logger := environment.GetLogger() @@ -396,7 +312,7 @@ func fheAddSubRequiredGas(environment EVMEnvironment, input []byte) uint64 { } } - return fheAddSubGasCosts[lhs.ciphertext.fheUintType] + return environment.FhevmParams().GasCosts.FheAddSub[lhs.ciphertext.fheUintType] } func fheMulRequiredGas(environment EVMEnvironment, input []byte) uint64 { @@ -424,7 +340,7 @@ func fheMulRequiredGas(environment EVMEnvironment, input []byte) uint64 { return 0 } } - return fheMulGasCosts[lhs.ciphertext.fheUintType] + return environment.FhevmParams().GasCosts.FheMul[lhs.ciphertext.fheUintType] } func fheLeRequiredGas(environment EVMEnvironment, input []byte) uint64 { @@ -452,7 +368,7 @@ func fheLeRequiredGas(environment EVMEnvironment, input []byte) uint64 { return 0 } } - return fheLeGasCosts[lhs.ciphertext.fheUintType] + return environment.FhevmParams().GasCosts.FheLe[lhs.ciphertext.fheUintType] } func fheLtRequiredGas(environment EVMEnvironment, input []byte) uint64 { @@ -505,7 +421,7 @@ func fheShlRequiredGas(environment EVMEnvironment, input []byte) uint64 { return 0 } } - return fheShiftGasCosts[lhs.ciphertext.fheUintType] + return environment.FhevmParams().GasCosts.FheShift[lhs.ciphertext.fheUintType] } func fheShrRequiredGas(environment EVMEnvironment, input []byte) uint64 { @@ -538,7 +454,7 @@ func fheMinRequiredGas(environment EVMEnvironment, input []byte) uint64 { return 0 } } - return fheMinMaxGasCosts[lhs.ciphertext.fheUintType] + return environment.FhevmParams().GasCosts.FheMinMax[lhs.ciphertext.fheUintType] } func fheMaxRequiredGas(environment EVMEnvironment, input []byte) uint64 { @@ -557,7 +473,7 @@ func fheNegRequiredGas(environment EVMEnvironment, input []byte) uint64 { logger.Error("fheNeg input not verified", "input", hex.EncodeToString(input)) return 0 } - return fheNegNotGasCosts[ct.ciphertext.fheUintType] + return environment.FhevmParams().GasCosts.FheNegNot[ct.ciphertext.fheUintType] } func fheNotRequiredGas(environment EVMEnvironment, input []byte) uint64 { @@ -583,7 +499,7 @@ func fheDivRequiredGas(environment EVMEnvironment, input []byte) uint64 { return 0 } } - return fheDivGasCosts[lhs.ciphertext.fheUintType] + return environment.FhevmParams().GasCosts.FheDiv[lhs.ciphertext.fheUintType] } func fheRemRequiredGas(environment EVMEnvironment, input []byte) uint64 { @@ -604,7 +520,7 @@ func fheRemRequiredGas(environment EVMEnvironment, input []byte) uint64 { return 0 } } - return fheRemGasCosts[lhs.ciphertext.fheUintType] + return environment.FhevmParams().GasCosts.FheRem[lhs.ciphertext.fheUintType] } func fheBitAndRequiredGas(environment EVMEnvironment, input []byte) uint64 { @@ -631,7 +547,7 @@ func fheBitAndRequiredGas(environment EVMEnvironment, input []byte) uint64 { logger.Error("Bitwise op RequiredGas() operand type mismatch", "lhs", lhs.ciphertext.fheUintType, "rhs", rhs.ciphertext.fheUintType) return 0 } - return fheBitwiseOpGasCosts[lhs.ciphertext.fheUintType] + return environment.FhevmParams().GasCosts.FheBitwiseOp[lhs.ciphertext.fheUintType] } func fheBitOrRequiredGas(environment EVMEnvironment, input []byte) uint64 { @@ -650,8 +566,8 @@ func fheRandRequiredGas(environment EVMEnvironment, input []byte) uint64 { logger.Error("fheRand RequiredGas() input len must be at least 1 byte and be a valid FheUint type", "input", hex.EncodeToString(input), "len", len(input)) return 0 } - t := fheUintType(input[0]) - return fheRandGasCosts[t] + t := FheUintType(input[0]) + return environment.FhevmParams().GasCosts.FheRand[t] } func verifyCiphertextRequiredGas(environment EVMEnvironment, input []byte) uint64 { @@ -661,8 +577,8 @@ func verifyCiphertextRequiredGas(environment EVMEnvironment, input []byte) uint6 "len", len(input)) return 0 } - ctType := fheUintType(input[len(input)-1]) - return fheVerifyGasCosts[ctType] + ctType := FheUintType(input[len(input)-1]) + return environment.FhevmParams().GasCosts.FheVerify[ctType] } func reencryptRequiredGas(environment EVMEnvironment, input []byte) uint64 { @@ -676,7 +592,7 @@ func reencryptRequiredGas(environment EVMEnvironment, input []byte) uint64 { logger.Error("reencrypt RequiredGas() input doesn't point to verified ciphertext", "input", hex.EncodeToString(input)) return 0 } - return fheReencryptGasCosts[ct.ciphertext.fheUintType] + return environment.FhevmParams().GasCosts.FheReencrypt[ct.ciphertext.fheUintType] } func optimisticRequireRequiredGas(environment EVMEnvironment, input []byte) uint64 { @@ -698,9 +614,9 @@ func optimisticRequireRequiredGas(environment EVMEnvironment, input []byte) uint return 0 } if len(environment.GetFhevmData().optimisticRequires) == 0 { - return FheUint8OptimisticRequireGas + return environment.FhevmParams().GasCosts.FheOptRequire[FheUint8] } - return FheUint8OptimisticRequireBitandGas + return environment.FhevmParams().GasCosts.FheOptRequireBitAnd[FheUint8] } func castRequiredGas(environment EVMEnvironment, input []byte) uint64 { @@ -710,7 +626,7 @@ func castRequiredGas(environment EVMEnvironment, input []byte) uint64 { "len", len(input)) return 0 } - return FheCastGas + return environment.FhevmParams().GasCosts.FheCast } func decryptRequiredGas(environment EVMEnvironment, input []byte) uint64 { @@ -724,11 +640,11 @@ func decryptRequiredGas(environment EVMEnvironment, input []byte) uint64 { logger.Error("decrypt RequiredGas() input doesn't point to verified ciphertext", "input", hex.EncodeToString(input)) return 0 } - return fheDecryptGasCosts[ct.ciphertext.fheUintType] + return environment.FhevmParams().GasCosts.FheDecrypt[ct.ciphertext.fheUintType] } func fhePubKeyRequiredGas(environment EVMEnvironment, input []byte) uint64 { - return FhePubKeyGas + return environment.FhevmParams().GasCosts.FhePubKey } func trivialEncryptRequiredGas(environment EVMEnvironment, input []byte) uint64 { @@ -737,8 +653,8 @@ func trivialEncryptRequiredGas(environment EVMEnvironment, input []byte) uint64 logger.Error("trivialEncrypt RequiredGas() input len must be 33 bytes", "input", hex.EncodeToString(input), "len", len(input)) return 0 } - encryptToType := fheUintType(input[32]) - return fheTrivialEncryptGasCosts[encryptToType] + encryptToType := FheUintType(input[32]) + return environment.FhevmParams().GasCosts.FheTrivialEncrypt[encryptToType] } // Implementations @@ -1855,7 +1771,7 @@ func fheRandRun(environment EVMEnvironment, caller common.Address, addr common.A return nil, errors.New(msg) } - t := fheUintType(input[0]) + t := FheUintType(input[0]) // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. if !environment.IsCommitting() { return importRandomCiphertext(environment, t), nil @@ -1922,7 +1838,7 @@ func verifyCiphertextRun(environment EVMEnvironment, caller common.Address, addr logger.Error(msg, "type", ctTypeByte) return nil, errors.New(msg) } - ctType := fheUintType(ctTypeByte) + ctType := FheUintType(ctTypeByte) expectedSize, found := compactFheCiphertextSize[ctType] if !found || expectedSize != uint(len(ctBytes)) { @@ -2122,7 +2038,7 @@ func castRun(environment EVMEnvironment, caller common.Address, addr common.Addr logger.Error("invalid type to cast to") return nil, errors.New("invalid type provided") } - castToType := fheUintType(input[32]) + castToType := FheUintType(input[32]) // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. if !environment.IsCommitting() && !environment.IsEthCall() { @@ -2180,7 +2096,7 @@ func trivialEncryptRun(environment EVMEnvironment, caller common.Address, addr c } valueToEncrypt := *new(big.Int).SetBytes(input[0:32]) - encryptToType := fheUintType(input[32]) + encryptToType := FheUintType(input[32]) ct := new(tfheCiphertext).trivialEncrypt(valueToEncrypt, encryptToType) diff --git a/fhevm/tfhe.go b/fhevm/tfhe.go index 07e76cc..dd08e05 100644 --- a/fhevm/tfhe.go +++ b/fhevm/tfhe.go @@ -1518,10 +1518,10 @@ func toBufferView(in []byte) C.BufferView { } // Expanded TFHE ciphertext sizes by type, in bytes. -var expandedFheCiphertextSize map[fheUintType]uint +var expandedFheCiphertextSize map[FheUintType]uint // Compact TFHE ciphertext sizes by type, in bytes. -var compactFheCiphertextSize map[fheUintType]uint +var compactFheCiphertextSize map[FheUintType]uint // server key: evaluation key var sks unsafe.Pointer @@ -1549,8 +1549,8 @@ func initGlobalKeysWithNewKeys() { } func initCiphertextSizes() { - expandedFheCiphertextSize = make(map[fheUintType]uint) - compactFheCiphertextSize = make(map[fheUintType]uint) + expandedFheCiphertextSize = make(map[FheUintType]uint) + compactFheCiphertextSize = make(map[FheUintType]uint) expandedFheCiphertextSize[FheUint8] = uint(len(new(tfheCiphertext).trivialEncrypt(*big.NewInt(0), FheUint8).serialize())) expandedFheCiphertextSize[FheUint16] = uint(len(new(tfheCiphertext).trivialEncrypt(*big.NewInt(0), FheUint16).serialize())) @@ -1610,7 +1610,7 @@ func init() { } } -func serialize(ptr unsafe.Pointer, t fheUintType) ([]byte, error) { +func serialize(ptr unsafe.Pointer, t FheUintType) ([]byte, error) { out := &C.Buffer{} var ret C.int switch t { @@ -1644,23 +1644,23 @@ func serializePublicKey(pks unsafe.Pointer) ([]byte, error) { } // Represents a TFHE ciphertext type, i.e. its bit capacity. -type fheUintType uint8 +type FheUintType uint8 const ( - FheUint8 fheUintType = 0 - FheUint16 fheUintType = 1 - FheUint32 fheUintType = 2 + FheUint8 FheUintType = 0 + FheUint16 FheUintType = 1 + FheUint32 FheUintType = 2 ) // Represents an expanded TFHE ciphertext. type tfheCiphertext struct { serialization []byte hash *common.Hash - fheUintType fheUintType + fheUintType FheUintType } // Deserializes a TFHE ciphertext. -func (ct *tfheCiphertext) deserialize(in []byte, t fheUintType) error { +func (ct *tfheCiphertext) deserialize(in []byte, t FheUintType) error { switch t { case FheUint8: ptr := C.deserialize_fhe_uint8(toBufferView((in))) @@ -1692,7 +1692,7 @@ func (ct *tfheCiphertext) deserialize(in []byte, t fheUintType) error { // Deserializes a compact TFHE ciphetext. // Note: After the compact TFHE ciphertext has been serialized, subsequent calls to serialize() // will produce non-compact ciphertext serialziations. -func (ct *tfheCiphertext) deserializeCompact(in []byte, t fheUintType) error { +func (ct *tfheCiphertext) deserializeCompact(in []byte, t FheUintType) error { switch t { case FheUint8: ptr := C.deserialize_compact_fhe_uint8(toBufferView((in))) @@ -1737,7 +1737,7 @@ func (ct *tfheCiphertext) deserializeCompact(in []byte, t fheUintType) error { // Encrypts a value as a TFHE ciphertext, using the compact public FHE key. // The resulting ciphertext is automaticaly expanded. -func (ct *tfheCiphertext) encrypt(value big.Int, t fheUintType) *tfheCiphertext { +func (ct *tfheCiphertext) encrypt(value big.Int, t FheUintType) *tfheCiphertext { var ptr unsafe.Pointer var err error switch t { @@ -1770,7 +1770,7 @@ func (ct *tfheCiphertext) encrypt(value big.Int, t fheUintType) *tfheCiphertext return ct } -func (ct *tfheCiphertext) trivialEncrypt(value big.Int, t fheUintType) *tfheCiphertext { +func (ct *tfheCiphertext) trivialEncrypt(value big.Int, t FheUintType) *tfheCiphertext { var ptr unsafe.Pointer var err error switch t { @@ -2460,7 +2460,7 @@ func (lhs *tfheCiphertext) not() (*tfheCiphertext, error) { }) } -func (ct *tfheCiphertext) castTo(castToType fheUintType) (*tfheCiphertext, error) { +func (ct *tfheCiphertext) castTo(castToType FheUintType) (*tfheCiphertext, error) { if ct.fheUintType == castToType { return nil, errors.New("casting to same type is not supported") } @@ -2645,7 +2645,7 @@ func isValidType(t byte) bool { return true } -func encryptAndSerializeCompact(value uint32, fheUintType fheUintType) []byte { +func encryptAndSerializeCompact(value uint32, fheUintType FheUintType) []byte { out := &C.Buffer{} switch fheUintType { case FheUint8: diff --git a/fhevm/tfhe_test.go b/fhevm/tfhe_test.go index a1200f1..10968b2 100644 --- a/fhevm/tfhe_test.go +++ b/fhevm/tfhe_test.go @@ -38,7 +38,7 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } -func TfheEncryptDecrypt(t *testing.T, fheUintType fheUintType) { +func TfheEncryptDecrypt(t *testing.T, fheUintType FheUintType) { var val big.Int switch fheUintType { case FheUint8: @@ -56,7 +56,7 @@ func TfheEncryptDecrypt(t *testing.T, fheUintType fheUintType) { } } -func TfheTrivialEncryptDecrypt(t *testing.T, fheUintType fheUintType) { +func TfheTrivialEncryptDecrypt(t *testing.T, fheUintType FheUintType) { var val big.Int switch fheUintType { case FheUint8: @@ -74,7 +74,7 @@ func TfheTrivialEncryptDecrypt(t *testing.T, fheUintType fheUintType) { } } -func TfheSerializeDeserialize(t *testing.T, fheUintType fheUintType) { +func TfheSerializeDeserialize(t *testing.T, fheUintType FheUintType) { var val big.Int switch fheUintType { case FheUint8: @@ -98,7 +98,7 @@ func TfheSerializeDeserialize(t *testing.T, fheUintType fheUintType) { } } -func TfheSerializeDeserializeCompact(t *testing.T, fheUintType fheUintType) { +func TfheSerializeDeserializeCompact(t *testing.T, fheUintType FheUintType) { var val uint32 switch fheUintType { case FheUint8: @@ -134,7 +134,7 @@ func TfheSerializeDeserializeCompact(t *testing.T, fheUintType fheUintType) { } } -func TfheTrivialSerializeDeserialize(t *testing.T, fheUintType fheUintType) { +func TfheTrivialSerializeDeserialize(t *testing.T, fheUintType FheUintType) { var val big.Int switch fheUintType { case FheUint8: @@ -158,7 +158,7 @@ func TfheTrivialSerializeDeserialize(t *testing.T, fheUintType fheUintType) { } } -func TfheDeserializeFailure(t *testing.T, fheUintType fheUintType) { +func TfheDeserializeFailure(t *testing.T, fheUintType FheUintType) { ct := new(tfheCiphertext) input := make([]byte, 1) input[0] = 42 @@ -168,7 +168,7 @@ func TfheDeserializeFailure(t *testing.T, fheUintType fheUintType) { } } -func TfheDeserializeCompact(t *testing.T, fheUintType fheUintType) { +func TfheDeserializeCompact(t *testing.T, fheUintType FheUintType) { var val uint32 switch fheUintType { case FheUint8: @@ -190,7 +190,7 @@ func TfheDeserializeCompact(t *testing.T, fheUintType fheUintType) { } } -func TfheDeserializeCompactFailure(t *testing.T, fheUintType fheUintType) { +func TfheDeserializeCompactFailure(t *testing.T, fheUintType FheUintType) { ct := new(tfheCiphertext) err := ct.deserializeCompact(make([]byte, 10), fheUintType) if err == nil { @@ -198,7 +198,7 @@ func TfheDeserializeCompactFailure(t *testing.T, fheUintType fheUintType) { } } -func TfheAdd(t *testing.T, fheUintType fheUintType) { +func TfheAdd(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -223,7 +223,7 @@ func TfheAdd(t *testing.T, fheUintType fheUintType) { } } -func TfheScalarAdd(t *testing.T, fheUintType fheUintType) { +func TfheScalarAdd(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -246,7 +246,7 @@ func TfheScalarAdd(t *testing.T, fheUintType fheUintType) { } } -func TfheSub(t *testing.T, fheUintType fheUintType) { +func TfheSub(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -271,7 +271,7 @@ func TfheSub(t *testing.T, fheUintType fheUintType) { } } -func TfheScalarSub(t *testing.T, fheUintType fheUintType) { +func TfheScalarSub(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -294,7 +294,7 @@ func TfheScalarSub(t *testing.T, fheUintType fheUintType) { } } -func TfheMul(t *testing.T, fheUintType fheUintType) { +func TfheMul(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -319,7 +319,7 @@ func TfheMul(t *testing.T, fheUintType fheUintType) { } } -func TfheScalarMul(t *testing.T, fheUintType fheUintType) { +func TfheScalarMul(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -342,7 +342,7 @@ func TfheScalarMul(t *testing.T, fheUintType fheUintType) { } } -func TfheScalarDiv(t *testing.T, fheUintType fheUintType) { +func TfheScalarDiv(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -365,7 +365,7 @@ func TfheScalarDiv(t *testing.T, fheUintType fheUintType) { } } -func TfheScalarRem(t *testing.T, fheUintType fheUintType) { +func TfheScalarRem(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -388,7 +388,7 @@ func TfheScalarRem(t *testing.T, fheUintType fheUintType) { } } -func TfheBitAnd(t *testing.T, fheUintType fheUintType) { +func TfheBitAnd(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -413,7 +413,7 @@ func TfheBitAnd(t *testing.T, fheUintType fheUintType) { } } -func TfheBitOr(t *testing.T, fheUintType fheUintType) { +func TfheBitOr(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -438,7 +438,7 @@ func TfheBitOr(t *testing.T, fheUintType fheUintType) { } } -func TfheBitXor(t *testing.T, fheUintType fheUintType) { +func TfheBitXor(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -463,7 +463,7 @@ func TfheBitXor(t *testing.T, fheUintType fheUintType) { } } -func TfheShl(t *testing.T, fheUintType fheUintType) { +func TfheShl(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -488,7 +488,7 @@ func TfheShl(t *testing.T, fheUintType fheUintType) { } } -func TfheScalarShl(t *testing.T, fheUintType fheUintType) { +func TfheScalarShl(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -511,7 +511,7 @@ func TfheScalarShl(t *testing.T, fheUintType fheUintType) { } } -func TfheShr(t *testing.T, fheUintType fheUintType) { +func TfheShr(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -536,7 +536,7 @@ func TfheShr(t *testing.T, fheUintType fheUintType) { } } -func TfheScalarShr(t *testing.T, fheUintType fheUintType) { +func TfheScalarShr(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -559,7 +559,7 @@ func TfheScalarShr(t *testing.T, fheUintType fheUintType) { } } -func TfheEq(t *testing.T, fheUintType fheUintType) { +func TfheEq(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -590,7 +590,7 @@ func TfheEq(t *testing.T, fheUintType fheUintType) { } } -func TfheScalarEq(t *testing.T, fheUintType fheUintType) { +func TfheScalarEq(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -619,7 +619,7 @@ func TfheScalarEq(t *testing.T, fheUintType fheUintType) { } } -func TfheNe(t *testing.T, fheUintType fheUintType) { +func TfheNe(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -650,7 +650,7 @@ func TfheNe(t *testing.T, fheUintType fheUintType) { } } -func TfheScalarNe(t *testing.T, fheUintType fheUintType) { +func TfheScalarNe(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -679,7 +679,7 @@ func TfheScalarNe(t *testing.T, fheUintType fheUintType) { } } -func TfheGe(t *testing.T, fheUintType fheUintType) { +func TfheGe(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -708,7 +708,7 @@ func TfheGe(t *testing.T, fheUintType fheUintType) { } } -func TfheScalarGe(t *testing.T, fheUintType fheUintType) { +func TfheScalarGe(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -730,7 +730,7 @@ func TfheScalarGe(t *testing.T, fheUintType fheUintType) { } } -func TfheGt(t *testing.T, fheUintType fheUintType) { +func TfheGt(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -759,7 +759,7 @@ func TfheGt(t *testing.T, fheUintType fheUintType) { } } -func TfheScalarGt(t *testing.T, fheUintType fheUintType) { +func TfheScalarGt(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -781,7 +781,7 @@ func TfheScalarGt(t *testing.T, fheUintType fheUintType) { } } -func TfheLe(t *testing.T, fheUintType fheUintType) { +func TfheLe(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -810,7 +810,7 @@ func TfheLe(t *testing.T, fheUintType fheUintType) { } } -func TfheScalarLe(t *testing.T, fheUintType fheUintType) { +func TfheScalarLe(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -832,7 +832,7 @@ func TfheScalarLe(t *testing.T, fheUintType fheUintType) { } } -func TfheLt(t *testing.T, fheUintType fheUintType) { +func TfheLt(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -861,7 +861,7 @@ func TfheLt(t *testing.T, fheUintType fheUintType) { } } -func TfheScalarLt(t *testing.T, fheUintType fheUintType) { +func TfheScalarLt(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -883,7 +883,7 @@ func TfheScalarLt(t *testing.T, fheUintType fheUintType) { } } -func TfheMin(t *testing.T, fheUintType fheUintType) { +func TfheMin(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -912,7 +912,7 @@ func TfheMin(t *testing.T, fheUintType fheUintType) { } } -func TfheScalarMin(t *testing.T, fheUintType fheUintType) { +func TfheScalarMin(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -934,7 +934,7 @@ func TfheScalarMin(t *testing.T, fheUintType fheUintType) { } } -func TfheMax(t *testing.T, fheUintType fheUintType) { +func TfheMax(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -963,7 +963,7 @@ func TfheMax(t *testing.T, fheUintType fheUintType) { } } -func TfheScalarMax(t *testing.T, fheUintType fheUintType) { +func TfheScalarMax(t *testing.T, fheUintType FheUintType) { var a, b big.Int switch fheUintType { case FheUint8: @@ -985,7 +985,7 @@ func TfheScalarMax(t *testing.T, fheUintType fheUintType) { } } -func TfheNeg(t *testing.T, fheUintType fheUintType) { +func TfheNeg(t *testing.T, fheUintType FheUintType) { var a big.Int var expected uint64 @@ -1009,7 +1009,7 @@ func TfheNeg(t *testing.T, fheUintType fheUintType) { } } -func TfheNot(t *testing.T, fheUintType fheUintType) { +func TfheNot(t *testing.T, fheUintType FheUintType) { var a big.Int var expected uint64 switch fheUintType { @@ -1033,7 +1033,7 @@ func TfheNot(t *testing.T, fheUintType fheUintType) { } } -func TfheCast(t *testing.T, fheUintTypeFrom fheUintType, fheUintTypeTo fheUintType) { +func TfheCast(t *testing.T, fheUintTypeFrom FheUintType, fheUintTypeTo FheUintType) { var a big.Int switch fheUintTypeFrom { case FheUint8: From e8168df9a341a4ee0d07b57b2fa8ad9f814e5eb3 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 8 Nov 2023 12:27:38 +0800 Subject: [PATCH 2/3] restore missing constants --- fhevm/params.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/fhevm/params.go b/fhevm/params.go index e41dc62..99ed23d 100644 --- a/fhevm/params.go +++ b/fhevm/params.go @@ -20,7 +20,27 @@ // along with the go-ethereum library. If not, see . package fhevm +// This file contains default gas costs of fhEVM-related operations. +// Users can change the values based on specific requirements in their blockchain. + +// Base gas costs of existing EVM operations. Used for setting gas costs relative to them. +// These constants are used just for readability. const EvmNetSstoreInitGas uint64 = 20000 +const ColdSloadCostEIP2929 uint64 = 2100 + +var ( + // TODO: The values here are chosen somewhat arbitrarily (at least the 8 bit ones). Also, we don't + // take into account whether a ciphertext existed (either "current" or "original") for the given handle. + // Finally, costs are likely to change in the future. + FheUint8ProtectedStorageSstoreGas uint64 = EvmNetSstoreInitGas + 2000 + FheUint16ProtectedStorageSstoreGas uint64 = FheUint8ProtectedStorageSstoreGas * 2 + FheUint32ProtectedStorageSstoreGas uint64 = FheUint16ProtectedStorageSstoreGas * 2 + + // TODO: We don't take whether the slot is cold or warm into consideration. + FheUint8ProtectedStorageSloadGas uint64 = ColdSloadCostEIP2929 + 200 + FheUint16ProtectedStorageSloadGas uint64 = FheUint8ProtectedStorageSloadGas * 2 + FheUint32ProtectedStorageSloadGas uint64 = FheUint16ProtectedStorageSloadGas * 2 +) func DefaultFhevmParams() FhevmParams { return FhevmParams{ From dbea2493e5891d1c2be1dee9d043c8e9a6ea5c0c Mon Sep 17 00:00:00 2001 From: David Date: Wed, 8 Nov 2023 12:34:48 +0800 Subject: [PATCH 3/3] Remove Get from method name --- fhevm/evm.go | 6 +++--- fhevm/instructions.go | 12 ++++++------ fhevm/interface.go | 2 +- fhevm/precompiles.go | 6 +++--- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/fhevm/evm.go b/fhevm/evm.go index 66e4dfd..12822fd 100644 --- a/fhevm/evm.go +++ b/fhevm/evm.go @@ -87,7 +87,7 @@ func getScalarOperands(environment EVMEnvironment, input []byte) (lhs *verifiedC } func importCiphertextToEVMAtDepth(environment EVMEnvironment, ct *tfheCiphertext, depth int) *verifiedCiphertext { - existing, ok := environment.GetFhevmData().verifiedCiphertexts[ct.getHash()] + existing, ok := environment.FhevmData().verifiedCiphertexts[ct.getHash()] if ok { existing.verifiedDepths.add(depth) return existing @@ -98,7 +98,7 @@ func importCiphertextToEVMAtDepth(environment EVMEnvironment, ct *tfheCiphertext verifiedDepths, ct, } - environment.GetFhevmData().verifiedCiphertexts[ct.getHash()] = new + environment.FhevmData().verifiedCiphertexts[ct.getHash()] = new return new } } @@ -112,7 +112,7 @@ func importCiphertext(environment EVMEnvironment, ct *tfheCiphertext) *verifiedC } func importRandomCiphertext(environment EVMEnvironment, t FheUintType) []byte { - nextCtHash := &environment.GetFhevmData().nextCiphertextHashOnGasEst + nextCtHash := &environment.FhevmData().nextCiphertextHashOnGasEst ctHashBytes := crypto.Keccak256(nextCtHash.Bytes()) handle := common.BytesToHash(ctHashBytes) ct := new(tfheCiphertext) diff --git a/fhevm/instructions.go b/fhevm/instructions.go index 7ccef83..643c104 100644 --- a/fhevm/instructions.go +++ b/fhevm/instructions.go @@ -133,7 +133,7 @@ func isVerifiedAtCurrentDepth(environment EVMEnvironment, ct *verifiedCiphertext // Returns a pointer to the ciphertext if the given hash points to a verified ciphertext. // Else, it returns nil. func getVerifiedCiphertextFromEVM(environment EVMEnvironment, ciphertextHash common.Hash) *verifiedCiphertext { - ct, ok := environment.GetFhevmData().verifiedCiphertexts[ciphertextHash] + ct, ok := environment.FhevmData().verifiedCiphertexts[ciphertextHash] if ok && isVerifiedAtCurrentDepth(environment, ct) { return ct } @@ -141,7 +141,7 @@ func getVerifiedCiphertextFromEVM(environment EVMEnvironment, ciphertextHash com } func verifyIfCiphertextHandle(handle common.Hash, env EVMEnvironment, contractAddress common.Address) error { - ct, ok := env.GetFhevmData().verifiedCiphertexts[handle] + ct, ok := env.FhevmData().verifiedCiphertexts[handle] if ok { // If already existing in memory, skip storage and import the same ciphertext at the current depth. // @@ -299,7 +299,7 @@ func OpSstore(pc *uint64, env EVMEnvironment, scope ScopeContext) ([]byte, error // Return a map from ciphertext hash -> depthSet before delegation. func DelegateCiphertextHandlesInArgs(env EVMEnvironment, args []byte) (verified map[common.Hash]*depthSet) { verified = make(map[common.Hash]*depthSet) - for key, verifiedCiphertext := range env.GetFhevmData().verifiedCiphertexts { + for key, verifiedCiphertext := range env.FhevmData().verifiedCiphertexts { if contains(args, key.Bytes()) && isVerifiedAtCurrentDepth(env, verifiedCiphertext) { if env.IsCommitting() { env.GetLogger().Info("delegateCiphertextHandlesInArgs", @@ -316,12 +316,12 @@ func DelegateCiphertextHandlesInArgs(env EVMEnvironment, args []byte) (verified func RestoreVerifiedDepths(env EVMEnvironment, verified map[common.Hash]*depthSet) { for k, v := range verified { - env.GetFhevmData().verifiedCiphertexts[k].verifiedDepths = v + env.FhevmData().verifiedCiphertexts[k].verifiedDepths = v } } func delegateCiphertextHandlesToCaller(env EVMEnvironment, ret []byte) { - for key, verifiedCiphertext := range env.GetFhevmData().verifiedCiphertexts { + for key, verifiedCiphertext := range env.FhevmData().verifiedCiphertexts { if contains(ret, key.Bytes()) && isVerifiedAtCurrentDepth(env, verifiedCiphertext) { if env.IsCommitting() { env.GetLogger().Info("opReturn making ciphertext available to caller", @@ -336,7 +336,7 @@ func delegateCiphertextHandlesToCaller(env EVMEnvironment, ret []byte) { } func RemoveVerifiedCipherextsAtCurrentDepth(env EVMEnvironment) { - for _, verifiedCiphertext := range env.GetFhevmData().verifiedCiphertexts { + for _, verifiedCiphertext := range env.FhevmData().verifiedCiphertexts { if env.IsCommitting() { env.GetLogger().Info("Run removing ciphertext from depth", "handle", verifiedCiphertext.ciphertext.getHash().Hex(), diff --git a/fhevm/interface.go b/fhevm/interface.go index 92b6633..dd814ac 100644 --- a/fhevm/interface.go +++ b/fhevm/interface.go @@ -31,7 +31,7 @@ type EVMEnvironment interface { CreateContract(caller common.Address, code []byte, gas uint64, value *big.Int, address common.Address) ([]byte, common.Address, uint64, error) CreateContract2(caller common.Address, code []byte, codeHash common.Hash, gas uint64, value *big.Int, address common.Address) ([]byte, common.Address, uint64, error) - GetFhevmData() *FhevmData + FhevmData() *FhevmData FhevmParams() *FhevmParams } diff --git a/fhevm/precompiles.go b/fhevm/precompiles.go index 0963e33..7a9535a 100644 --- a/fhevm/precompiles.go +++ b/fhevm/precompiles.go @@ -613,7 +613,7 @@ func optimisticRequireRequiredGas(environment EVMEnvironment, input []byte) uint "type", ct.ciphertext.fheUintType) return 0 } - if len(environment.GetFhevmData().optimisticRequires) == 0 { + if len(environment.FhevmData().optimisticRequires) == 0 { return environment.FhevmParams().GasCosts.FheOptRequire[FheUint8] } return environment.FhevmParams().GasCosts.FheOptRequireBitAnd[FheUint8] @@ -1950,7 +1950,7 @@ func optimisticRequireRun(environment EVMEnvironment, caller common.Address, add logger.Error(msg, "type", ct.ciphertext.fheUintType) return nil, errors.New(msg) } - environment.GetFhevmData().optimisticRequires = append(environment.GetFhevmData().optimisticRequires, ct.ciphertext) + environment.FhevmData().optimisticRequires = append(environment.FhevmData().optimisticRequires, ct.ciphertext) return nil, nil } @@ -2001,7 +2001,7 @@ func decryptValue(ct *tfheCiphertext) (uint64, error) { // That works, because we assume their values are either 0 or 1. If there is at least // one 0, the result will be 0 (false). func evaluateRemainingOptimisticRequires(environment EVMEnvironment) (bool, error) { - requires := environment.GetFhevmData().optimisticRequires + requires := environment.FhevmData().optimisticRequires len := len(requires) defer func() { requires = make([]*tfheCiphertext, 0) }() if len != 0 {