From 51002f93124ed5d762312648840309e9bbb5d6b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Tue, 5 Mar 2024 02:37:11 +0100 Subject: [PATCH 1/9] chore: refactor operators --- fhevm/fhelib_required_gas.go | 520 --------- fhevm/fhelib_run.go | 1814 ----------------------------- fhevm/operators_arithmetic.go | 290 +++++ fhevm/operators_arithmetic_gas.go | 111 ++ fhevm/operators_bit.go | 358 ++++++ fhevm/operators_bit_gas.go | 115 ++ fhevm/operators_comparison.go | 574 +++++++++ fhevm/operators_comparison_gas.go | 141 +++ fhevm/operators_crypto.go | 457 ++++++++ fhevm/operators_crypto_gas.go | 123 ++ fhevm/operators_rand.go | 180 +++ fhevm/operators_rand_gas.go | 52 + 12 files changed, 2401 insertions(+), 2334 deletions(-) delete mode 100644 fhevm/fhelib_required_gas.go delete mode 100644 fhevm/fhelib_run.go create mode 100644 fhevm/operators_arithmetic.go create mode 100644 fhevm/operators_arithmetic_gas.go create mode 100644 fhevm/operators_bit.go create mode 100644 fhevm/operators_bit_gas.go create mode 100644 fhevm/operators_comparison.go create mode 100644 fhevm/operators_comparison_gas.go create mode 100644 fhevm/operators_crypto.go create mode 100644 fhevm/operators_crypto_gas.go create mode 100644 fhevm/operators_rand.go create mode 100644 fhevm/operators_rand_gas.go diff --git a/fhevm/fhelib_required_gas.go b/fhevm/fhelib_required_gas.go deleted file mode 100644 index a3513fb..0000000 --- a/fhevm/fhelib_required_gas.go +++ /dev/null @@ -1,520 +0,0 @@ -package fhevm - -import ( - "encoding/hex" - "fmt" - "math/bits" - - "github.com/ethereum/go-ethereum/common" - "github.com/holiman/uint256" -) - -func fheAddSubRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheAdd/Sub RequiredGas() can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - var lhs, rhs *verifiedCiphertext - if !isScalar { - lhs, rhs, err = get2VerifiedOperands(environment, input) - if err != nil { - logger.Error("fheAdd/Sub RequiredGas() ciphertext inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - if lhs.fheUintType() != rhs.fheUintType() { - logger.Error("fheAdd/Sub RequiredGas() operand type mismatch", "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return 0 - } - } else { - lhs, _, err = getScalarOperands(environment, input) - if err != nil { - logger.Error("fheAdd/Sub RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - } - - return environment.FhevmParams().GasCosts.FheAddSub[lhs.fheUintType()] -} - -func fheMulRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheMul RequiredGas() can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - var lhs, rhs *verifiedCiphertext - if !isScalar { - lhs, rhs, err = get2VerifiedOperands(environment, input) - if err != nil { - logger.Error("fheMul RequiredGas() ciphertext inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - if lhs.fheUintType() != rhs.fheUintType() { - logger.Error("fheMul RequiredGas() operand type mismatch", "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return 0 - } - return environment.FhevmParams().GasCosts.FheMul[lhs.fheUintType()] - } else { - lhs, _, err = getScalarOperands(environment, input) - if err != nil { - logger.Error("fheMul RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - return environment.FhevmParams().GasCosts.FheScalarMul[lhs.fheUintType()] - } -} - -func fheLeRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("comparison RequiredGas() can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - var lhs, rhs *verifiedCiphertext - if !isScalar { - lhs, rhs, err = get2VerifiedOperands(environment, input) - if err != nil { - logger.Error("comparison RequiredGas() ciphertext inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - if lhs.fheUintType() != rhs.fheUintType() { - logger.Error("comparison RequiredGas() operand type mismatch", "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return 0 - } - } else { - lhs, _, err = getScalarOperands(environment, input) - if err != nil { - logger.Error("comparison RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - } - return environment.FhevmParams().GasCosts.FheLe[lhs.fheUintType()] -} - -func fheLtRequiredGas(environment EVMEnvironment, input []byte) uint64 { - // Implement in terms of le, because le and lt costs are currently the same. - return fheLeRequiredGas(environment, input) -} - -func fheEqRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("comparison RequiredGas() can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - var lhs, rhs *verifiedCiphertext - if !isScalar { - lhs, rhs, err = get2VerifiedOperands(environment, input) - if err != nil { - logger.Error("comparison RequiredGas() ciphertext inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - if lhs.fheUintType() != rhs.fheUintType() { - logger.Error("comparison RequiredGas() operand type mismatch", "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return 0 - } - } else { - lhs, _, err = getScalarOperands(environment, input) - if err != nil { - logger.Error("comparison RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - } - return environment.FhevmParams().GasCosts.FheEq[lhs.fheUintType()] -} - -func fheGeRequiredGas(environment EVMEnvironment, input []byte) uint64 { - // Implement in terms of le, because comparison costs are currently the same. - return fheLeRequiredGas(environment, input) -} - -func fheGtRequiredGas(environment EVMEnvironment, input []byte) uint64 { - // Implement in terms of le, because comparison costs are currently the same. - return fheLeRequiredGas(environment, input) -} - -func fheNeRequiredGas(environment EVMEnvironment, input []byte) uint64 { - // Implement in terms of le, because comparison costs are currently the same. - return fheEqRequiredGas(environment, input) -} - -func fheShlRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheShift RequiredGas() can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - var lhs, rhs *verifiedCiphertext - if !isScalar { - lhs, rhs, err = get2VerifiedOperands(environment, input) - if err != nil { - logger.Error("fheShift RequiredGas() ciphertext inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - if lhs.fheUintType() != rhs.fheUintType() { - logger.Error("fheShift RequiredGas() operand type mismatch", "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return 0 - } - return environment.FhevmParams().GasCosts.FheShift[lhs.fheUintType()] - } else { - lhs, _, err = getScalarOperands(environment, input) - if err != nil { - logger.Error("fheShift RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - return environment.FhevmParams().GasCosts.FheScalarShift[lhs.fheUintType()] - } -} - -func fheShrRequiredGas(environment EVMEnvironment, input []byte) uint64 { - // Implement in terms of shl, because comparison costs are currently the same. - return fheShlRequiredGas(environment, input) -} - -func fheMinRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheMin/Max RequiredGas() can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - var lhs, rhs *verifiedCiphertext - if !isScalar { - lhs, rhs, err = get2VerifiedOperands(environment, input) - if err != nil { - logger.Error("fheMin/Max RequiredGas() ciphertext inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - if lhs.fheUintType() != rhs.fheUintType() { - logger.Error("fheMin/Max RequiredGas() operand type mismatch", "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return 0 - } - return environment.FhevmParams().GasCosts.FheMinMax[lhs.fheUintType()] - } else { - lhs, _, err = getScalarOperands(environment, input) - if err != nil { - logger.Error("fheMin/Max RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - return environment.FhevmParams().GasCosts.FheScalarMinMax[lhs.fheUintType()] - } -} - -func fheMaxRequiredGas(environment EVMEnvironment, input []byte) uint64 { - // Implement in terms of min, because costs are currently the same. - return fheMinRequiredGas(environment, input) -} - -func fheNegRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(32, len(input))] - - logger := environment.GetLogger() - if len(input) != 32 { - logger.Error("fheNeg input needs to contain one 256-bit sized value", "input", hex.EncodeToString(input)) - return 0 - } - ct := getVerifiedCiphertext(environment, common.BytesToHash(input[0:32])) - if ct == nil { - logger.Error("fheNeg input not verified", "input", hex.EncodeToString(input)) - return 0 - } - return environment.FhevmParams().GasCosts.FheNeg[ct.fheUintType()] -} - -func fheNotRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(32, len(input))] - - logger := environment.GetLogger() - if len(input) != 32 { - logger.Error("fheNot input needs to contain one 256-bit sized value", "input", hex.EncodeToString(input)) - return 0 - } - ct := getVerifiedCiphertext(environment, common.BytesToHash(input[0:32])) - if ct == nil { - logger.Error("fheNot input not verified", "input", hex.EncodeToString(input)) - return 0 - } - return environment.FhevmParams().GasCosts.FheNot[ct.fheUintType()] -} - -func fheDivRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheDiv RequiredGas() cannot detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - var lhs *verifiedCiphertext - if !isScalar { - logger.Error("fheDiv RequiredGas() only scalar in division is supported, two ciphertexts received", "input", hex.EncodeToString(input)) - return 0 - } else { - lhs, _, err = getScalarOperands(environment, input) - if err != nil { - logger.Error("fheDiv RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - return environment.FhevmParams().GasCosts.FheScalarDiv[lhs.fheUintType()] - } -} - -func fheRemRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheRem RequiredGas() cannot detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - var lhs *verifiedCiphertext - if !isScalar { - logger.Error("fheRem RequiredGas() only scalar in division is supported, two ciphertexts received", "input", hex.EncodeToString(input)) - return 0 - } else { - lhs, _, err = getScalarOperands(environment, input) - if err != nil { - logger.Error("fheRem RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - return environment.FhevmParams().GasCosts.FheScalarRem[lhs.fheUintType()] - } -} - -func fheBitAndRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("Bitwise op RequiredGas() can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - - if isScalar { - msg := "Bitwise op RequiredGas() scalar op not supported" - logger.Error(msg) - return 0 - } - - lhs, rhs, err := get2VerifiedOperands(environment, input) - if err != nil { - logger.Error("Bitwise op RequiredGas() inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - if lhs.fheUintType() != rhs.fheUintType() { - logger.Error("Bitwise op RequiredGas() operand type mismatch", "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return 0 - } - return environment.FhevmParams().GasCosts.FheBitwiseOp[lhs.fheUintType()] -} - -func fheBitOrRequiredGas(environment EVMEnvironment, input []byte) uint64 { - // Implement in terms of bitAnd, because bitwise op costs are currently the same. - return fheBitAndRequiredGas(environment, input) -} - -func fheBitXorRequiredGas(environment EVMEnvironment, input []byte) uint64 { - // Implement in terms of bitAnd, because bitwise op costs are currently the same. - return fheBitAndRequiredGas(environment, input) -} - -func fheRandRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(1, len(input))] - - logger := environment.GetLogger() - if len(input) != 1 || !isValidFheType(input[0]) { - 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 environment.FhevmParams().GasCosts.FheRand[t] -} - -func parseRandUpperBoundInput(input []byte) (randType FheUintType, upperBound *uint256.Int, err error) { - if len(input) != 33 || !isValidFheType(input[32]) { - return FheUint8, nil, fmt.Errorf("parseRandUpperBoundInput() invalid input len or type") - } - randType = FheUintType(input[32]) - upperBound = uint256.NewInt(0) - upperBound.SetBytes32(input) - // For now, we only support bounds of up to 64 bits. - if !upperBound.IsUint64() { - return FheUint8, nil, fmt.Errorf("parseRandUpperBoundInput() only supports bounds up to 64 bits") - } - upperBound64 := upperBound.Uint64() - oneBits := bits.OnesCount64(upperBound64) - if oneBits != 1 { - return FheUint8, nil, fmt.Errorf("parseRandUpperBoundInput() bound not a power of 2: %d", upperBound64) - } - return randType, upperBound, nil -} - -func fheRandBoundedRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(33, len(input))] - - logger := environment.GetLogger() - randType, _, err := parseRandUpperBoundInput(input) - if err != nil { - logger.Error("fheRandBounded RequiredGas() bound error", "input", hex.EncodeToString(input), "err", err) - return 0 - } - return environment.FhevmParams().GasCosts.FheRand[randType] -} - -func fheIfThenElseRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(96, len(input))] - - logger := environment.GetLogger() - first, second, third, err := get3VerifiedOperands(environment, input) - if err != nil { - logger.Error("IfThenElse op RequiredGas() inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return 0 - } - if first.fheUintType() != FheBool { - logger.Error("IfThenElse op RequiredGas() invalid type for condition", "first", first.fheUintType()) - return 0 - } - if second.fheUintType() != third.fheUintType() { - logger.Error("IfThenElse op RequiredGas() operand type mismatch", "second", second.fheUintType(), "third", third.fheUintType()) - return 0 - } - return environment.FhevmParams().GasCosts.FheIfThenElse[second.fheUintType()] -} - -func verifyCiphertextRequiredGas(environment EVMEnvironment, input []byte) uint64 { - if len(input) <= 1 { - environment.GetLogger().Error( - "verifyCiphertext RequiredGas() input needs to contain a ciphertext and one byte for its type", - "len", len(input)) - return 0 - } - ctType := FheUintType(input[len(input)-1]) - return environment.FhevmParams().GasCosts.FheVerify[ctType] -} - -func reencryptRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(64, len(input))] - - logger := environment.GetLogger() - if len(input) != 64 { - logger.Error("reencrypt RequiredGas() input len must be 64 bytes", "input", hex.EncodeToString(input), "len", len(input)) - return 0 - } - ct := getVerifiedCiphertext(environment, common.BytesToHash(input[0:32])) - if ct == nil { - logger.Error("reencrypt RequiredGas() input doesn't point to verified ciphertext", "input", hex.EncodeToString(input)) - return 0 - } - return environment.FhevmParams().GasCosts.FheReencrypt[ct.fheUintType()] -} - -func optimisticRequireRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(32, len(input))] - - logger := environment.GetLogger() - if len(input) != 32 { - logger.Error("optimisticRequire RequiredGas() input len must be 32 bytes", - "input", hex.EncodeToString(input), "len", len(input)) - return 0 - } - ct := getVerifiedCiphertext(environment, common.BytesToHash(input)) - if ct == nil { - logger.Error("optimisticRequire RequiredGas() input doesn't point to verified ciphertext", - "input", hex.EncodeToString(input)) - return 0 - } - if ct.fheUintType() != FheUint8 { - logger.Error("optimisticRequire RequiredGas() ciphertext type is not FheUint8", - "type", ct.fheUintType()) - return 0 - } - if len(environment.FhevmData().optimisticRequires) == 0 { - return environment.FhevmParams().GasCosts.FheOptRequire[FheUint8] - } - return environment.FhevmParams().GasCosts.FheOptRequireBitAnd[FheUint8] -} - -func getCiphertextRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(64, len(input))] - - logger := environment.GetLogger() - if len(input) != 64 { - logger.Error("getCiphertext RequiredGas() input len must be 64 bytes", - "input", hex.EncodeToString(input), "len", len(input)) - return 0 - } - - contractAddress := common.BytesToAddress(input[:32]) - handle := common.BytesToHash(input[32:]) - metadata := getCiphertextMetadataFromProtectedStorage(environment, contractAddress, handle) - if metadata == nil { - return GetNonExistentCiphertextGas - } - return environment.FhevmParams().GasCosts.FheGetCiphertext[metadata.fheUintType] -} - -func castRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(33, len(input))] - - if len(input) != 33 { - environment.GetLogger().Error( - "cast RequiredGas() input needs to contain a ciphertext and one byte for its type", - "len", len(input)) - return 0 - } - return environment.FhevmParams().GasCosts.FheCast -} - -func decryptRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(32, len(input))] - - logger := environment.GetLogger() - if len(input) != 32 { - logger.Error("decrypt RequiredGas() input len must be 32 bytes", "input", hex.EncodeToString(input), "len", len(input)) - return 0 - } - ct := getVerifiedCiphertext(environment, common.BytesToHash(input)) - if ct == nil { - logger.Error("decrypt RequiredGas() input doesn't point to verified ciphertext", "input", hex.EncodeToString(input)) - return 0 - } - return environment.FhevmParams().GasCosts.FheDecrypt[ct.fheUintType()] -} - -func fhePubKeyRequiredGas(environment EVMEnvironment, input []byte) uint64 { - return environment.FhevmParams().GasCosts.FhePubKey -} - -func trivialEncryptRequiredGas(environment EVMEnvironment, input []byte) uint64 { - input = input[:minInt(33, len(input))] - - logger := environment.GetLogger() - if len(input) != 33 { - logger.Error("trivialEncrypt RequiredGas() input len must be 33 bytes", "input", hex.EncodeToString(input), "len", len(input)) - return 0 - } - encryptToType := FheUintType(input[32]) - return environment.FhevmParams().GasCosts.FheTrivialEncrypt[encryptToType] -} diff --git a/fhevm/fhelib_run.go b/fhevm/fhelib_run.go deleted file mode 100644 index fa67f2b..0000000 --- a/fhevm/fhelib_run.go +++ /dev/null @@ -1,1814 +0,0 @@ -package fhevm - -import ( - "bytes" - "context" - "encoding/binary" - "encoding/hex" - "errors" - "fmt" - "math/big" - "math/bits" - "time" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" - "github.com/holiman/uint256" - fhevm_crypto "github.com/zama-ai/fhevm-go/crypto" - "github.com/zama-ai/fhevm-go/kms" - "go.opentelemetry.io/otel/trace" - "golang.org/x/crypto/chacha20" - "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" -) - -func fheAddRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheAdd can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if !isScalar { - lhs, rhs, err := get2VerifiedOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) - if err != nil { - logger.Error("fheAdd inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - if lhs.fheUintType() != rhs.fheUintType() { - msg := "fheAdd operand type mismatch" - logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.Add(rhs.ciphertext) - if err != nil { - logger.Error("fheAdd failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheAdd success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil - - } else { - lhs, rhs, err := getScalarOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) - if err != nil { - logger.Error("fheAdd scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.ScalarAdd(rhs.Uint64()) - if err != nil { - logger.Error("fheAdd failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheAdd scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) - return resultHash[:], nil - } -} - -func fheSubRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheSub can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if !isScalar { - lhs, rhs, err := get2VerifiedOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) - if err != nil { - logger.Error("fheSub inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - if lhs.fheUintType() != rhs.fheUintType() { - msg := "fheSub operand type mismatch" - logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.Sub(rhs.ciphertext) - if err != nil { - logger.Error("fheSub failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheSub success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil - - } else { - lhs, rhs, err := getScalarOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) - if err != nil { - logger.Error("fheSub scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.ScalarSub(rhs.Uint64()) - if err != nil { - logger.Error("fheSub failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheSub scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) - return resultHash[:], nil - } -} - -func fheMulRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheMul can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if !isScalar { - lhs, rhs, err := get2VerifiedOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) - if err != nil { - logger.Error("fheMul inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - if lhs.fheUintType() != rhs.fheUintType() { - msg := "fheMul operand type mismatch" - logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.Mul(rhs.ciphertext) - if err != nil { - logger.Error("fheMul failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheMul success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil - - } else { - lhs, rhs, err := getScalarOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) - if err != nil { - logger.Error("fheMul scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.ScalarMul(rhs.Uint64()) - if err != nil { - logger.Error("fheMul failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheMul scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) - return resultHash[:], nil - } -} - -func fheLeRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheLe can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if !isScalar { - lhs, rhs, err := get2VerifiedOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) - if err != nil { - logger.Error("fheLe inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - if lhs.fheUintType() != rhs.fheUintType() { - msg := "fheLe operand type mismatch" - logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil - } - - result, err := lhs.ciphertext.Le(rhs.ciphertext) - if err != nil { - logger.Error("fheLe failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheLe success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil - - } else { - lhs, rhs, err := getScalarOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) - if err != nil { - logger.Error("fheLe scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil - } - - result, err := lhs.ciphertext.ScalarLe(rhs.Uint64()) - if err != nil { - logger.Error("fheLe failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheLe scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) - return resultHash[:], nil - } -} - -func fheLtRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheLt can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if !isScalar { - lhs, rhs, err := get2VerifiedOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) - if err != nil { - logger.Error("fheLt inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - if lhs.fheUintType() != rhs.fheUintType() { - msg := "fheLt operand type mismatch" - logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil - } - - result, err := lhs.ciphertext.Lt(rhs.ciphertext) - if err != nil { - logger.Error("fheLt failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheLt success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil - - } else { - lhs, rhs, err := getScalarOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) - if err != nil { - logger.Error("fheLt scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil - } - - result, err := lhs.ciphertext.ScalarLt(rhs.Uint64()) - if err != nil { - logger.Error("fheLt failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheLt scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) - return resultHash[:], nil - } -} - -func fheEqRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheEq can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if !isScalar { - lhs, rhs, err := get2VerifiedOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) - if err != nil { - logger.Error("fheEq inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - if lhs.fheUintType() != rhs.fheUintType() { - msg := "fheEq operand type mismatch" - logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil - } - - result, err := lhs.ciphertext.Eq(rhs.ciphertext) - if err != nil { - logger.Error("fheEq failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheEq success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil - - } else { - lhs, rhs, err := getScalarOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) - if err != nil { - logger.Error("fheEq scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil - } - - result, err := lhs.ciphertext.ScalarEq(rhs.Uint64()) - if err != nil { - logger.Error("fheEq failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheEq scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) - return resultHash[:], nil - } -} - -func fheGeRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheGe can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if !isScalar { - lhs, rhs, err := get2VerifiedOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) - if err != nil { - logger.Error("fheGe inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - if lhs.fheUintType() != rhs.fheUintType() { - msg := "fheGe operand type mismatch" - logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil - } - - result, err := lhs.ciphertext.Ge(rhs.ciphertext) - if err != nil { - logger.Error("fheGe failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheGe success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil - - } else { - lhs, rhs, err := getScalarOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) - if err != nil { - logger.Error("fheGe scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil - } - - result, err := lhs.ciphertext.ScalarGe(rhs.Uint64()) - if err != nil { - logger.Error("fheGe failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheGe scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) - return resultHash[:], nil - } -} - -func fheGtRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheGt can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if !isScalar { - lhs, rhs, err := get2VerifiedOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) - if err != nil { - logger.Error("fheGt inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - if lhs.fheUintType() != rhs.fheUintType() { - msg := "fheGt operand type mismatch" - logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil - } - - result, err := lhs.ciphertext.Gt(rhs.ciphertext) - if err != nil { - logger.Error("fheGt failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheGt success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil - - } else { - lhs, rhs, err := getScalarOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) - if err != nil { - logger.Error("fheGt scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil - } - - result, err := lhs.ciphertext.ScalarGt(rhs.Uint64()) - if err != nil { - logger.Error("fheGt failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheGt scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) - return resultHash[:], nil - } -} - -func fheShlRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheShl can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if !isScalar { - lhs, rhs, err := get2VerifiedOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) - if err != nil { - logger.Error("fheShl inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - if lhs.fheUintType() != rhs.fheUintType() { - msg := "fheShl operand type mismatch" - logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.Shl(rhs.ciphertext) - if err != nil { - logger.Error("fheShl failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheShl success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil - - } else { - lhs, rhs, err := getScalarOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) - if err != nil { - logger.Error("fheShl scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.ScalarShl(rhs.Uint64()) - if err != nil { - logger.Error("fheShl failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheShl scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) - return resultHash[:], nil - } -} - -func fheShrRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheShr can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if !isScalar { - lhs, rhs, err := get2VerifiedOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) - if err != nil { - logger.Error("fheShr inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - if lhs.fheUintType() != rhs.fheUintType() { - msg := "fheShr operand type mismatch" - logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.Shr(rhs.ciphertext) - if err != nil { - logger.Error("fheShr failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheShr success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil - - } else { - lhs, rhs, err := getScalarOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) - if err != nil { - logger.Error("fheShr scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.ScalarShr(rhs.Uint64()) - if err != nil { - logger.Error("fheShr failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheShr scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) - return resultHash[:], nil - } -} - -func fheNeRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheNe can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if !isScalar { - lhs, rhs, err := get2VerifiedOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) - if err != nil { - logger.Error("fheNe inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - if lhs.fheUintType() != rhs.fheUintType() { - msg := "fheNe operand type mismatch" - logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil - } - - result, err := lhs.ciphertext.Ne(rhs.ciphertext) - if err != nil { - logger.Error("fheNe failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheNe success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil - - } else { - lhs, rhs, err := getScalarOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) - if err != nil { - logger.Error("fheNe scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil - } - - result, err := lhs.ciphertext.ScalarNe(rhs.Uint64()) - if err != nil { - logger.Error("fheNe failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheNe scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) - return resultHash[:], nil - } -} - -func fheMinRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheMin can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if !isScalar { - lhs, rhs, err := get2VerifiedOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) - if err != nil { - logger.Error("fheMin inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - if lhs.fheUintType() != rhs.fheUintType() { - msg := "fheMin operand type mismatch" - logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.Min(rhs.ciphertext) - if err != nil { - logger.Error("fheMin failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheMin success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil - - } else { - lhs, rhs, err := getScalarOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) - if err != nil { - logger.Error("fheMin scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.ScalarMin(rhs.Uint64()) - if err != nil { - logger.Error("fheMin failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheMin scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) - return resultHash[:], nil - } -} - -func fheMaxRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheMax can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if !isScalar { - lhs, rhs, err := get2VerifiedOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) - if err != nil { - logger.Error("fheMax inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - if lhs.fheUintType() != rhs.fheUintType() { - msg := "fheMax operand type mismatch" - logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.Max(rhs.ciphertext) - if err != nil { - logger.Error("fheMax failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheMax success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil - - } else { - lhs, rhs, err := getScalarOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) - if err != nil { - logger.Error("fheMax scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.ScalarMax(rhs.Uint64()) - if err != nil { - logger.Error("fheMax failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheMax scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) - return resultHash[:], nil - } -} - -func fheNegRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(32, len(input))] - - logger := environment.GetLogger() - - if len(input) != 32 { - msg := "fheMax input needs to contain one 256-bit sized value" - logger.Error(msg, "input", hex.EncodeToString(input)) - return nil, errors.New(msg) - - } - - ct := getVerifiedCiphertext(environment, common.BytesToHash(input[0:32])) - if ct == nil { - msg := "fheNeg input not verified" - logger.Error(msg, msg, "input", hex.EncodeToString(input)) - return nil, errors.New(msg) - } - otelDescribeOperandsFheTypes(runSpan, ct.fheUintType()) - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, ct.fheUintType()), nil - } - - result, err := ct.ciphertext.Neg() - if err != nil { - logger.Error("fheNeg failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheNeg success", "ct", ct.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil -} - -func fheNotRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(32, len(input))] - - logger := environment.GetLogger() - - if len(input) != 32 { - msg := "fheMax input needs to contain one 256-bit sized value" - logger.Error(msg, "input", hex.EncodeToString(input)) - return nil, errors.New(msg) - - } - - ct := getVerifiedCiphertext(environment, common.BytesToHash(input[0:32])) - if ct == nil { - msg := "fheNot input not verified" - logger.Error(msg, msg, "input", hex.EncodeToString(input)) - return nil, errors.New(msg) - } - otelDescribeOperandsFheTypes(runSpan, ct.fheUintType()) - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, ct.fheUintType()), nil - } - - result, err := ct.ciphertext.Not() - if err != nil { - logger.Error("fheNot failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheNot success", "ct", ct.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil -} - -func fheDivRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheDiv cannot detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if !isScalar { - err = errors.New("fheDiv supports only scalar input operation, two ciphertexts received") - logger.Error("fheDiv supports only scalar input operation, two ciphertexts received", "input", hex.EncodeToString(input)) - return nil, err - } else { - lhs, rhs, err := getScalarOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) - if err != nil { - logger.Error("fheDiv scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.ScalarDiv(rhs.Uint64()) - if err != nil { - logger.Error("fheDiv failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheDiv scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) - return resultHash[:], nil - } -} - -func fheRemRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheRem cannot detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if !isScalar { - err = errors.New("fheRem supports only scalar input operation, two ciphertexts received") - logger.Error("fheRem supports only scalar input operation, two ciphertexts received", "input", hex.EncodeToString(input)) - return nil, err - } else { - lhs, rhs, err := getScalarOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) - if err != nil { - logger.Error("fheRem scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.ScalarRem(rhs.Uint64()) - if err != nil { - logger.Error("fheRem failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheRem scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) - return resultHash[:], nil - } -} - -func fheBitAndRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheBitAnd can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if isScalar { - msg := "fheBitAnd scalar op not supported" - logger.Error(msg) - return nil, errors.New(msg) - } - - lhs, rhs, err := get2VerifiedOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) - if err != nil { - logger.Error("fheBitAnd inputs not verified", "err", err) - return nil, err - } - - if lhs.fheUintType() != rhs.fheUintType() { - msg := "fheBitAnd operand type mismatch" - logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.Bitand(rhs.ciphertext) - if err != nil { - logger.Error("fheBitAnd failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheBitAnd success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil -} - -func fheBitOrRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheBitOr can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if isScalar { - msg := "fheBitOr scalar op not supported" - logger.Error(msg) - return nil, errors.New(msg) - } - - lhs, rhs, err := get2VerifiedOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) - if err != nil { - logger.Error("fheBitOr inputs not verified", "err", err) - return nil, err - } - - if lhs.fheUintType() != rhs.fheUintType() { - msg := "fheBitOr operand type mismatch" - logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.Bitor(rhs.ciphertext) - if err != nil { - logger.Error("fheBitOr failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheBitOr success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil -} - -func fheBitXorRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(65, len(input))] - - logger := environment.GetLogger() - - isScalar, err := isScalarOp(input) - if err != nil { - logger.Error("fheBitXor can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if isScalar { - msg := "fheBitXor scalar op not supported" - logger.Error(msg) - return nil, errors.New(msg) - } - - lhs, rhs, err := get2VerifiedOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) - if err != nil { - logger.Error("fheBitXor inputs not verified", "err", err) - return nil, err - } - - if lhs.fheUintType() != rhs.fheUintType() { - msg := "fheBitXor operand type mismatch" - logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, lhs.fheUintType()), nil - } - - result, err := lhs.ciphertext.Bitxor(rhs.ciphertext) - if err != nil { - logger.Error("fheBitXor failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheBitXor success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil -} - -var globalRngSeed []byte - -var rngNonceKey [32]byte = uint256.NewInt(0).Bytes32() - -func init() { - if chacha20.NonceSizeX != 24 { - panic("expected 24 bytes for NonceSizeX") - } - - // TODO: Since the current implementation is not FHE-based and, hence, not private, - // we just initialize the global seed with non-random public data. We will change - // that once the FHE version is available. - globalRngSeed = make([]byte, chacha20.KeySize) - for i := range globalRngSeed { - globalRngSeed[i] = byte(1 + i) - } -} - -// Applies the upperBound (if set) to the rand value and returns the result. -// bitsInRand is the amount of random bits that are contained in rand. -// bitsInRand and upperBound must be powers of 2. -func applyUpperBound(rand uint64, bitsInRand int, upperBound *uint64) uint64 { - if upperBound == nil { - return rand - } else if *upperBound == 0 { - panic("sliceRandom called with upperBound of 0") - } - // Len64() returns the amount of bits needed to represent upperBound. Subtract 1 to get the - // amount of bits requested by the given upperBound as we want to return a value in the [0, upperBound) range. - // Note that upperBound is assumed to be a power of 2. - // - // For example, if upperBound = 128, then bits = 8 - 1 = 7 random bits to be returned. - // To get that amount of random bits from rand, subtract bits from bitsInRand, i.e. - // shift = 32 - 7 = 25. Shifting rand 25 positions would leave 7 of its random bits. - bits := bits.Len64(*upperBound) - 1 - shift := bitsInRand - bits - // If the shift ends up negative or 0, just return rand without any shifts. - if shift <= 0 { - return rand - } - return rand >> shift -} - -func generateRandom(environment EVMEnvironment, caller common.Address, resultType FheUintType, upperBound *uint64) ([]byte, error) { - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() { - return importRandomCiphertext(environment, resultType), nil - } - - // Get the RNG nonce. - protectedStorage := fhevm_crypto.CreateProtectedStorageContractAddress(caller) - currentRngNonceBytes := environment.GetState(protectedStorage, rngNonceKey).Bytes() - - // Increment the RNG nonce by 1. - nextRngNonce := uint256.NewInt(0).SetBytes(currentRngNonceBytes) - nextRngNonce = nextRngNonce.AddUint64(nextRngNonce, 1) - environment.SetState(protectedStorage, rngNonceKey, nextRngNonce.Bytes32()) - - // Compute the seed and use it to create a new cipher. - hasher := crypto.NewKeccakState() - hasher.Write(globalRngSeed) - hasher.Write(caller.Bytes()) - seed := common.Hash{} - _, err := hasher.Read(seed[:]) - if err != nil { - return nil, err - } - // The RNG nonce bytes are of size chacha20.NonceSizeX, which is assumed to be 24 bytes (see init() above). - // Since uint256.Int.z[0] is the least significant byte and since uint256.Int.Bytes32() serializes - // in order of z[3], z[2], z[1], z[0], we want to essentially ignore the first byte, i.e. z[3], because - // it will always be 0 as the nonce size is 24. - cipher, err := chacha20.NewUnauthenticatedCipher(seed.Bytes(), currentRngNonceBytes[32-chacha20.NonceSizeX:32]) - if err != nil { - return nil, err - } - - // XOR a byte array of 0s with the stream from the cipher and receive the result in the same array. - // Apply upperBound, if set. - var randUint uint64 - switch resultType { - case FheUint4: - randBytes := make([]byte, 1) - cipher.XORKeyStream(randBytes, randBytes) - randUint = uint64(randBytes[0]) - randUint = uint64(applyUpperBound(randUint, 4, upperBound)) - case FheUint8: - randBytes := make([]byte, 1) - cipher.XORKeyStream(randBytes, randBytes) - randUint = uint64(randBytes[0]) - randUint = uint64(applyUpperBound(randUint, 8, upperBound)) - case FheUint16: - randBytes := make([]byte, 2) - cipher.XORKeyStream(randBytes, randBytes) - randUint = uint64(binary.BigEndian.Uint16(randBytes)) - randUint = uint64(applyUpperBound(randUint, 16, upperBound)) - case FheUint32: - randBytes := make([]byte, 4) - cipher.XORKeyStream(randBytes, randBytes) - randUint = uint64(binary.BigEndian.Uint32(randBytes)) - randUint = uint64(applyUpperBound(randUint, 32, upperBound)) - case FheUint64: - randBytes := make([]byte, 8) - cipher.XORKeyStream(randBytes, randBytes) - randUint = uint64(binary.BigEndian.Uint64(randBytes)) - randUint = uint64(applyUpperBound(randUint, 64, upperBound)) - default: - return nil, fmt.Errorf("generateRandom() invalid type requested: %d", resultType) - } - - // Trivially encrypt the random integer. - randCt := new(TfheCiphertext) - randBigInt := big.NewInt(0) - randBigInt.SetUint64(randUint) - randCt.TrivialEncrypt(*randBigInt, resultType) - importCiphertext(environment, randCt) - - if err != nil { - return nil, err - } - ctHash := randCt.GetHash() - return ctHash[:], nil -} - -func fheRandRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(1, len(input))] - - logger := environment.GetLogger() - if environment.IsEthCall() { - msg := "fheRand cannot be called via EthCall, because it needs to mutate internal state" - logger.Error(msg) - return nil, errors.New(msg) - } - if len(input) != 1 || !isValidFheType(input[0]) { - msg := "fheRand input len must be at least 1 byte and be a valid FheUint type" - logger.Error(msg, "input", hex.EncodeToString(input), "len", len(input)) - return nil, errors.New(msg) - } - resultType := FheUintType(input[0]) - otelDescribeOperandsFheTypes(runSpan, resultType) - var noUpperBound *uint64 = nil - return generateRandom(environment, caller, resultType, noUpperBound) -} - -func fheRandBoundedRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(33, len(input))] - - logger := environment.GetLogger() - if environment.IsEthCall() { - msg := "fheRandBoundedRun cannot be called via EthCall, because it needs to mutate internal state" - logger.Error(msg) - return nil, errors.New(msg) - } - randType, bound, err := parseRandUpperBoundInput(input) - otelDescribeOperandsFheTypes(runSpan, randType) - if err != nil { - msg := "fheRandBounded bound error" - logger.Error(msg, "input", hex.EncodeToString(input), "err", err) - return nil, errors.New(msg) - } - bound64 := bound.Uint64() - return generateRandom(environment, caller, randType, &bound64) -} - -func fheIfThenElseRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(96, len(input))] - - logger := environment.GetLogger() - first, second, third, err := get3VerifiedOperands(environment, input) - otelDescribeOperands(runSpan, encryptedOperand(*first), encryptedOperand(*second), encryptedOperand(*third)) - if err != nil { - logger.Error("fheIfThenElse inputs not verified", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - - if second.fheUintType() != third.fheUintType() { - msg := "fheIfThenElse operand type mismatch" - logger.Error(msg, "second", second.fheUintType(), "third", third.fheUintType()) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, second.fheUintType()), nil - } - - result, err := first.ciphertext.IfThenElse(second.ciphertext, third.ciphertext) - if err != nil { - logger.Error("fheIfThenElse failed", "err", err) - return nil, err - } - importCiphertext(environment, result) - - resultHash := result.GetHash() - logger.Info("fheIfThenElse success", "first", first.hash().Hex(), "second", second.hash().Hex(), "third", third.hash().Hex(), "result", resultHash.Hex()) - return resultHash[:], nil -} - -func verifyCiphertextRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - logger := environment.GetLogger() - // first 32 bytes of the payload is offset, then 32 bytes are size of byte array - if len(input) <= 68 { - err := errors.New("verifyCiphertext(bytes) must contain at least 68 bytes for selector, byte offset and size") - logger.Error("fheLib precompile error", "err", err, "input", hex.EncodeToString(input)) - return nil, err - } - bytesPaddingSize := 32 - bytesSizeSlotSize := 32 - // read only last 4 bytes of padded number for byte array size - sizeStart := bytesPaddingSize + bytesSizeSlotSize - 4 - sizeEnd := sizeStart + 4 - bytesSize := binary.BigEndian.Uint32(input[sizeStart:sizeEnd]) - bytesStart := bytesPaddingSize + bytesSizeSlotSize - bytesEnd := bytesStart + int(bytesSize) - input = input[bytesStart:minInt(bytesEnd, len(input))] - - if len(input) <= 1 { - msg := "verifyCiphertext Run() input needs to contain a ciphertext and one byte for its type" - logger.Error(msg, "len", len(input)) - return nil, errors.New(msg) - } - - ctBytes := input[:len(input)-1] - ctTypeByte := input[len(input)-1] - if !isValidFheType(ctTypeByte) { - msg := "verifyCiphertext Run() ciphertext type is invalid" - logger.Error(msg, "type", ctTypeByte) - return nil, errors.New(msg) - } - ctType := FheUintType(ctTypeByte) - otelDescribeOperandsFheTypes(runSpan, ctType) - - expectedSize, found := GetCompactFheCiphertextSize(ctType) - if !found || expectedSize != uint(len(ctBytes)) { - msg := "verifyCiphertext Run() compact ciphertext size is invalid" - logger.Error(msg, "type", ctTypeByte, "size", len(ctBytes), "expectedSize", expectedSize) - return nil, errors.New(msg) - } - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, ctType), nil - } - - ct := new(TfheCiphertext) - err := ct.DeserializeCompact(ctBytes, ctType) - if err != nil { - logger.Error("verifyCiphertext failed to deserialize input ciphertext", - "err", err, - "len", len(ctBytes), - "ctBytes64", hex.EncodeToString(ctBytes[:minInt(len(ctBytes), 64)])) - return nil, err - } - ctHash := ct.GetHash() - importCiphertext(environment, ct) - if environment.IsCommitting() { - logger.Info("verifyCiphertext success", - "ctHash", ctHash.Hex(), - "ctBytes64", hex.EncodeToString(ctBytes[:minInt(len(ctBytes), 64)])) - } - return ctHash.Bytes(), nil -} - -func reencryptRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(64, len(input))] - // precompileBytes, err := reencryptRun(environment, caller, addr, bwCompatBytes, readOnly) - - logger := environment.GetLogger() - if !environment.IsEthCall() { - msg := "reencrypt only supported on EthCall" - logger.Error(msg) - return nil, errors.New(msg) - } - if len(input) != 64 { - msg := "reencrypt input len must be 64 bytes" - logger.Error(msg, "input", hex.EncodeToString(input), "len", len(input)) - return nil, errors.New(msg) - } - ct := getVerifiedCiphertext(environment, common.BytesToHash(input[0:32])) - if ct != nil { - otelDescribeOperandsFheTypes(runSpan, ct.fheUintType()) - // Make sure we don't decrypt before any optimistic requires are checked. - // optReqResult, optReqErr := evaluateRemainingOptimisticRequires(environment) - // if optReqErr != nil { - // return nil, optReqErr - // } else if !optReqResult { - // return nil, ErrExecutionReverted - // } - - var fheType kms.FheType - switch ct.fheUintType() { - case FheBool: - fheType = kms.FheType_Bool - case FheUint4: - fheType = kms.FheType_Euint4 - case FheUint8: - fheType = kms.FheType_Euint8 - case FheUint16: - fheType = kms.FheType_Euint16 - case FheUint32: - fheType = kms.FheType_Euint32 - case FheUint64: - fheType = kms.FheType_Euint64 - } - - pubKey := input[32:64] - - // TODO: generate merkle proof for some data - proof := &kms.Proof{ - Height: 3, - MerklePatriciaProof: []byte{}, - } - - reencryptionRequest := &kms.ReencryptionRequest{ - FheType: fheType, - Ciphertext: ct.serialization(), - Request: pubKey, // TODO: change according to the structure of `Request` - Proof: proof, - } - - conn, err := grpc.Dial(kms.KmsEndpointAddr, grpc.WithTransportCredentials(insecure.NewCredentials())) - if err != nil { - return nil, errors.New("kms unreachable") - } - defer conn.Close() - - ep := kms.NewKmsEndpointClient(conn) - - ctx, cancel := context.WithTimeout(context.Background(), time.Second) - defer cancel() - - res, err := ep.Reencrypt(ctx, reencryptionRequest) - if err != nil { - return nil, err - } - - // TODO: decide if `res.Signature` should be verified here - - var reencryptedValue = res.ReencryptedCiphertext - - logger.Info("reencrypt success", "input", hex.EncodeToString(input), "callerAddr", caller, "reencryptedValue", reencryptedValue, "len", len(reencryptedValue)) - reencryptedValue = toEVMBytes(reencryptedValue) - // pad according to abi specification, first add offset to the dynamic bytes argument - outputBytes := make([]byte, 32, len(reencryptedValue)+32) - outputBytes[31] = 0x20 - outputBytes = append(outputBytes, reencryptedValue...) - return padArrayTo32Multiple(outputBytes), nil - } - msg := "reencrypt unverified ciphertext handle" - logger.Error(msg, "input", hex.EncodeToString(input)) - return nil, errors.New(msg) -} - -func optimisticRequireRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(32, len(input))] - - logger := environment.GetLogger() - if len(input) != 32 { - msg := "optimisticRequire input len must be 32 bytes" - logger.Error(msg, "input", hex.EncodeToString(input), "len", len(input)) - return nil, errors.New(msg) - } - ct := getVerifiedCiphertext(environment, common.BytesToHash(input)) - if ct == nil { - msg := "optimisticRequire unverified handle" - logger.Error(msg, "input", hex.EncodeToString(input)) - return nil, errors.New(msg) - } - otelDescribeOperandsFheTypes(runSpan, ct.fheUintType()) - // If we are doing gas estimation, don't do anything as we would assume all requires are true. - if !environment.IsCommitting() && !environment.IsEthCall() { - return nil, nil - } - if ct.fheUintType() != FheUint8 { - msg := "optimisticRequire ciphertext type is not FheUint8" - logger.Error(msg, "type", ct.fheUintType()) - return nil, errors.New(msg) - } - environment.FhevmData().appendOptimisticRequires(ct.ciphertext) - return nil, nil -} - -func decryptRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(32, len(input))] - - logger := environment.GetLogger() - // if not gas estimation and not view function fail if decryptions are disabled in transactions - if environment.IsCommitting() && !environment.IsEthCall() && environment.FhevmParams().DisableDecryptionsInTransaction { - msg := "decryptions during transaction are disabled" - logger.Error(msg, "input", hex.EncodeToString(input)) - return nil, errors.New(msg) - } - if len(input) != 32 { - msg := "decrypt input len must be 32 bytes" - logger.Error(msg, "input", hex.EncodeToString(input), "len", len(input)) - return nil, errors.New(msg) - } - ct := getVerifiedCiphertext(environment, common.BytesToHash(input)) - if ct == nil { - msg := "decrypt unverified handle" - logger.Error(msg, "input", hex.EncodeToString(input)) - return nil, errors.New(msg) - } - otelDescribeOperandsFheTypes(runSpan, ct.fheUintType()) - - // If we are doing gas estimation, skip decryption and make sure we return the maximum possible value. - // We need that, because non-zero bytes cost more than zero bytes in some contexts (e.g. SSTORE or memory operations). - if !environment.IsCommitting() && !environment.IsEthCall() { - return bytes.Repeat([]byte{0xFF}, 32), nil - } - // Make sure we don't decrypt before any optimistic requires are checked. - optReqResult, optReqErr := evaluateRemainingOptimisticRequires(environment) - if optReqErr != nil { - return nil, optReqErr - } else if !optReqResult { - return nil, ErrExecutionReverted - } - - plaintext, err := decryptValue(environment, ct.ciphertext) - if err != nil { - logger.Error("decrypt failed", "err", err) - return nil, err - } - - logger.Info("decrypt success", "plaintext", plaintext) - - // Always return a 32-byte big-endian integer. - ret := make([]byte, 32) - bigIntValue := big.NewInt(0) - bigIntValue.SetUint64(plaintext) - bigIntValue.FillBytes(ret) - return ret, nil -} - -func getCiphertextRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(64, len(input))] - - logger := environment.GetLogger() - if !environment.IsEthCall() { - msg := "getCiphertext only supported on EthCall" - logger.Error(msg) - return nil, errors.New(msg) - } - if len(input) != 64 { - msg := "getCiphertext input len must be 64 bytes" - logger.Error(msg, "input", hex.EncodeToString(input), "len", len(input)) - return nil, errors.New(msg) - } - - contractAddress := common.BytesToAddress(input[:32]) - handle := common.BytesToHash(input[32:]) - - ciphertext := getCiphertextFromProtectedStoage(environment, contractAddress, handle) - if ciphertext == nil { - return make([]byte, 0), nil - } - otelDescribeOperandsFheTypes(runSpan, ciphertext.metadata.fheUintType) - return ciphertext.bytes, nil -} - -func decryptValue(environment EVMEnvironment, ct *TfheCiphertext) (uint64, error) { - - logger := environment.GetLogger() - var fheType kms.FheType - switch ct.Type() { - case FheBool: - fheType = kms.FheType_Bool - case FheUint4: - fheType = kms.FheType_Euint4 - case FheUint8: - fheType = kms.FheType_Euint8 - case FheUint16: - fheType = kms.FheType_Euint16 - case FheUint32: - fheType = kms.FheType_Euint32 - case FheUint64: - fheType = kms.FheType_Euint64 - } - - // TODO: generate merkle proof for some data - proof := &kms.Proof{ - Height: 4, - MerklePatriciaProof: []byte{}, - } - - decryptionRequest := &kms.DecryptionRequest{ - FheType: fheType, - Ciphertext: ct.Serialize(), - Request: []byte{}, // TODO: change according to the structure of `Request` - Proof: proof, - } - - conn, err := grpc.Dial(kms.KmsEndpointAddr, grpc.WithTransportCredentials(insecure.NewCredentials())) - if err != nil { - return 0, errors.New("kms unreachable") - } - defer conn.Close() - - ep := kms.NewKmsEndpointClient(conn) - - ctx, cancel := context.WithTimeout(context.Background(), time.Second) - defer cancel() - - res, err := ep.Decrypt(ctx, decryptionRequest) - if err != nil { - logger.Error("decrypt failed", "err", err) - return 0, err - } - - return uint64(res.Plaintext), err -} - -// If there are optimistic requires, check them by doing bitwise AND on all of them. -// 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.FhevmData().optimisticRequires - len := len(requires) - defer func() { environment.FhevmData().resetOptimisticRequires() }() - if len != 0 { - var cumulative *TfheCiphertext = requires[0] - var err error - for i := 1; i < len; i++ { - cumulative, err = cumulative.Bitand(requires[i]) - if err != nil { - environment.GetLogger().Error("evaluateRemainingOptimisticRequires bitand failed", "err", err) - return false, err - } - } - result, err := decryptValue(environment, cumulative) - return result != 0, err - } - return true, nil -} - -func castRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(33, len(input))] - - logger := environment.GetLogger() - if len(input) != 33 { - msg := "cast Run() input needs to contain a ciphertext and one byte for its type" - logger.Error(msg, "len", len(input)) - return nil, errors.New(msg) - } - - ct := getVerifiedCiphertext(environment, common.BytesToHash(input[0:32])) - if ct == nil { - logger.Error("cast input not verified") - return nil, errors.New("unverified ciphertext handle") - } - - if !isValidFheType(input[32]) { - logger.Error("invalid type to cast to") - return nil, errors.New("invalid type provided") - } - castToType := FheUintType(input[32]) - - otelDescribeOperandsFheTypes(runSpan, ct.fheUintType(), castToType) - - // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. - if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, castToType), nil - } - - res, err := ct.ciphertext.CastTo(castToType) - if err != nil { - msg := "cast Run() error casting ciphertext to" - logger.Error(msg, "type", castToType) - return nil, errors.New(msg) - } - - resHash := res.GetHash() - - importCiphertext(environment, res) - if environment.IsCommitting() { - logger.Info("cast success", - "ctHash", resHash.Hex(), - ) - } - - return resHash.Bytes(), nil -} - -var fhePubKeyHashPrecompile = common.BytesToAddress([]byte{93}) -var fhePubKeyHashSlot = common.Hash{} - -func fhePubKeyRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(1, len(input))] - - existing := environment.GetState(fhePubKeyHashPrecompile, fhePubKeyHashSlot) - if existing != GetPksHash() { - msg := "fhePubKey FHE public key hash doesn't match one stored in state" - environment.GetLogger().Error(msg, "existing", existing.Hex(), "pksHash", GetPksHash().Hex()) - return nil, errors.New(msg) - } - // serialize public key - pksBytes, err := serializePublicKey() - if err != nil { - return nil, err - } - // If we have a single byte with the value of 1, make as an EVM array. - if len(input) == 1 && input[0] == 1 { - pksBytes = toEVMBytes(pksBytes) - } - // pad according to abi specification, first add offset to the dynamic bytes argument - outputBytes := make([]byte, 32, len(pksBytes)+32) - outputBytes[31] = 0x20 - outputBytes = append(outputBytes, pksBytes...) - return padArrayTo32Multiple(outputBytes), nil -} - -func trivialEncryptRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { - input = input[:minInt(33, len(input))] - - logger := environment.GetLogger() - if len(input) != 33 { - msg := "trivialEncrypt input len must be 33 bytes" - logger.Error(msg, "input", hex.EncodeToString(input), "len", len(input)) - return nil, errors.New(msg) - } - - valueToEncrypt := *new(big.Int).SetBytes(input[0:32]) - encryptToType := FheUintType(input[32]) - otelDescribeOperandsFheTypes(runSpan, encryptToType) - - ct := new(TfheCiphertext).TrivialEncrypt(valueToEncrypt, encryptToType) - - ctHash := ct.GetHash() - importCiphertext(environment, ct) - if environment.IsCommitting() { - logger.Info("trivialEncrypt success", - "ctHash", ctHash.Hex(), - "valueToEncrypt", valueToEncrypt.Uint64()) - } - return ctHash.Bytes(), nil -} diff --git a/fhevm/operators_arithmetic.go b/fhevm/operators_arithmetic.go new file mode 100644 index 0000000..904b5f1 --- /dev/null +++ b/fhevm/operators_arithmetic.go @@ -0,0 +1,290 @@ +package fhevm + +import ( + "encoding/hex" + "errors" + + "github.com/ethereum/go-ethereum/common" + "go.opentelemetry.io/otel/trace" +) + + +func fheAddRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheAdd can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if !isScalar { + lhs, rhs, err := get2VerifiedOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) + if err != nil { + logger.Error("fheAdd inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + if lhs.fheUintType() != rhs.fheUintType() { + msg := "fheAdd operand type mismatch" + logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.Add(rhs.ciphertext) + if err != nil { + logger.Error("fheAdd failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheAdd success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil + + } else { + lhs, rhs, err := getScalarOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) + if err != nil { + logger.Error("fheAdd scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.ScalarAdd(rhs.Uint64()) + if err != nil { + logger.Error("fheAdd failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheAdd scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) + return resultHash[:], nil + } +} + +func fheSubRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheSub can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if !isScalar { + lhs, rhs, err := get2VerifiedOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) + if err != nil { + logger.Error("fheSub inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + if lhs.fheUintType() != rhs.fheUintType() { + msg := "fheSub operand type mismatch" + logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.Sub(rhs.ciphertext) + if err != nil { + logger.Error("fheSub failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheSub success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil + + } else { + lhs, rhs, err := getScalarOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) + if err != nil { + logger.Error("fheSub scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.ScalarSub(rhs.Uint64()) + if err != nil { + logger.Error("fheSub failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheSub scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) + return resultHash[:], nil + } +} + +func fheMulRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheMul can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if !isScalar { + lhs, rhs, err := get2VerifiedOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) + if err != nil { + logger.Error("fheMul inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + if lhs.fheUintType() != rhs.fheUintType() { + msg := "fheMul operand type mismatch" + logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.Mul(rhs.ciphertext) + if err != nil { + logger.Error("fheMul failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheMul success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil + + } else { + lhs, rhs, err := getScalarOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) + if err != nil { + logger.Error("fheMul scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.ScalarMul(rhs.Uint64()) + if err != nil { + logger.Error("fheMul failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheMul scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) + return resultHash[:], nil + } +} + +func fheDivRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheDiv cannot detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if !isScalar { + err = errors.New("fheDiv supports only scalar input operation, two ciphertexts received") + logger.Error("fheDiv supports only scalar input operation, two ciphertexts received", "input", hex.EncodeToString(input)) + return nil, err + } else { + lhs, rhs, err := getScalarOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) + if err != nil { + logger.Error("fheDiv scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.ScalarDiv(rhs.Uint64()) + if err != nil { + logger.Error("fheDiv failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheDiv scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) + return resultHash[:], nil + } +} + +func fheRemRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheRem cannot detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if !isScalar { + err = errors.New("fheRem supports only scalar input operation, two ciphertexts received") + logger.Error("fheRem supports only scalar input operation, two ciphertexts received", "input", hex.EncodeToString(input)) + return nil, err + } else { + lhs, rhs, err := getScalarOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) + if err != nil { + logger.Error("fheRem scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.ScalarRem(rhs.Uint64()) + if err != nil { + logger.Error("fheRem failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheRem scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) + return resultHash[:], nil + } +} diff --git a/fhevm/operators_arithmetic_gas.go b/fhevm/operators_arithmetic_gas.go new file mode 100644 index 0000000..015a848 --- /dev/null +++ b/fhevm/operators_arithmetic_gas.go @@ -0,0 +1,111 @@ +package fhevm + +import "encoding/hex" + +func fheAddSubRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheAdd/Sub RequiredGas() can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + var lhs, rhs *verifiedCiphertext + if !isScalar { + lhs, rhs, err = get2VerifiedOperands(environment, input) + if err != nil { + logger.Error("fheAdd/Sub RequiredGas() ciphertext inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + if lhs.fheUintType() != rhs.fheUintType() { + logger.Error("fheAdd/Sub RequiredGas() operand type mismatch", "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return 0 + } + } else { + lhs, _, err = getScalarOperands(environment, input) + if err != nil { + logger.Error("fheAdd/Sub RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + } + + return environment.FhevmParams().GasCosts.FheAddSub[lhs.fheUintType()] +} + +func fheMulRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheMul RequiredGas() can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + var lhs, rhs *verifiedCiphertext + if !isScalar { + lhs, rhs, err = get2VerifiedOperands(environment, input) + if err != nil { + logger.Error("fheMul RequiredGas() ciphertext inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + if lhs.fheUintType() != rhs.fheUintType() { + logger.Error("fheMul RequiredGas() operand type mismatch", "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return 0 + } + return environment.FhevmParams().GasCosts.FheMul[lhs.fheUintType()] + } else { + lhs, _, err = getScalarOperands(environment, input) + if err != nil { + logger.Error("fheMul RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + return environment.FhevmParams().GasCosts.FheScalarMul[lhs.fheUintType()] + } +} + +func fheDivRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheDiv RequiredGas() cannot detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + var lhs *verifiedCiphertext + if !isScalar { + logger.Error("fheDiv RequiredGas() only scalar in division is supported, two ciphertexts received", "input", hex.EncodeToString(input)) + return 0 + } else { + lhs, _, err = getScalarOperands(environment, input) + if err != nil { + logger.Error("fheDiv RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + return environment.FhevmParams().GasCosts.FheScalarDiv[lhs.fheUintType()] + } +} + +func fheRemRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheRem RequiredGas() cannot detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + var lhs *verifiedCiphertext + if !isScalar { + logger.Error("fheRem RequiredGas() only scalar in division is supported, two ciphertexts received", "input", hex.EncodeToString(input)) + return 0 + } else { + lhs, _, err = getScalarOperands(environment, input) + if err != nil { + logger.Error("fheRem RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + return environment.FhevmParams().GasCosts.FheScalarRem[lhs.fheUintType()] + } +} \ No newline at end of file diff --git a/fhevm/operators_bit.go b/fhevm/operators_bit.go new file mode 100644 index 0000000..2af1956 --- /dev/null +++ b/fhevm/operators_bit.go @@ -0,0 +1,358 @@ +package fhevm + +import ( + "encoding/hex" + "errors" + + "github.com/ethereum/go-ethereum/common" + "go.opentelemetry.io/otel/trace" +) + +func fheShlRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheShl can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if !isScalar { + lhs, rhs, err := get2VerifiedOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) + if err != nil { + logger.Error("fheShl inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + if lhs.fheUintType() != rhs.fheUintType() { + msg := "fheShl operand type mismatch" + logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.Shl(rhs.ciphertext) + if err != nil { + logger.Error("fheShl failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheShl success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil + + } else { + lhs, rhs, err := getScalarOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) + if err != nil { + logger.Error("fheShl scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.ScalarShl(rhs.Uint64()) + if err != nil { + logger.Error("fheShl failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheShl scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) + return resultHash[:], nil + } +} + +func fheShrRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheShr can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if !isScalar { + lhs, rhs, err := get2VerifiedOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) + if err != nil { + logger.Error("fheShr inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + if lhs.fheUintType() != rhs.fheUintType() { + msg := "fheShr operand type mismatch" + logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.Shr(rhs.ciphertext) + if err != nil { + logger.Error("fheShr failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheShr success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil + + } else { + lhs, rhs, err := getScalarOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) + if err != nil { + logger.Error("fheShr scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.ScalarShr(rhs.Uint64()) + if err != nil { + logger.Error("fheShr failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheShr scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) + return resultHash[:], nil + } +} + + +func fheNegRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(32, len(input))] + + logger := environment.GetLogger() + + if len(input) != 32 { + msg := "fheMax input needs to contain one 256-bit sized value" + logger.Error(msg, "input", hex.EncodeToString(input)) + return nil, errors.New(msg) + + } + + ct := getVerifiedCiphertext(environment, common.BytesToHash(input[0:32])) + if ct == nil { + msg := "fheNeg input not verified" + logger.Error(msg, msg, "input", hex.EncodeToString(input)) + return nil, errors.New(msg) + } + otelDescribeOperandsFheTypes(runSpan, ct.fheUintType()) + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, ct.fheUintType()), nil + } + + result, err := ct.ciphertext.Neg() + if err != nil { + logger.Error("fheNeg failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheNeg success", "ct", ct.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil +} + +func fheNotRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(32, len(input))] + + logger := environment.GetLogger() + + if len(input) != 32 { + msg := "fheMax input needs to contain one 256-bit sized value" + logger.Error(msg, "input", hex.EncodeToString(input)) + return nil, errors.New(msg) + + } + + ct := getVerifiedCiphertext(environment, common.BytesToHash(input[0:32])) + if ct == nil { + msg := "fheNot input not verified" + logger.Error(msg, msg, "input", hex.EncodeToString(input)) + return nil, errors.New(msg) + } + otelDescribeOperandsFheTypes(runSpan, ct.fheUintType()) + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, ct.fheUintType()), nil + } + + result, err := ct.ciphertext.Not() + if err != nil { + logger.Error("fheNot failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheNot success", "ct", ct.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil +} + + +func fheBitAndRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheBitAnd can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if isScalar { + msg := "fheBitAnd scalar op not supported" + logger.Error(msg) + return nil, errors.New(msg) + } + + lhs, rhs, err := get2VerifiedOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) + if err != nil { + logger.Error("fheBitAnd inputs not verified", "err", err) + return nil, err + } + + if lhs.fheUintType() != rhs.fheUintType() { + msg := "fheBitAnd operand type mismatch" + logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.Bitand(rhs.ciphertext) + if err != nil { + logger.Error("fheBitAnd failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheBitAnd success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil +} + +func fheBitOrRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheBitOr can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if isScalar { + msg := "fheBitOr scalar op not supported" + logger.Error(msg) + return nil, errors.New(msg) + } + + lhs, rhs, err := get2VerifiedOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) + if err != nil { + logger.Error("fheBitOr inputs not verified", "err", err) + return nil, err + } + + if lhs.fheUintType() != rhs.fheUintType() { + msg := "fheBitOr operand type mismatch" + logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.Bitor(rhs.ciphertext) + if err != nil { + logger.Error("fheBitOr failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheBitOr success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil +} + +func fheBitXorRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheBitXor can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if isScalar { + msg := "fheBitXor scalar op not supported" + logger.Error(msg) + return nil, errors.New(msg) + } + + lhs, rhs, err := get2VerifiedOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) + if err != nil { + logger.Error("fheBitXor inputs not verified", "err", err) + return nil, err + } + + if lhs.fheUintType() != rhs.fheUintType() { + msg := "fheBitXor operand type mismatch" + logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.Bitxor(rhs.ciphertext) + if err != nil { + logger.Error("fheBitXor failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheBitXor success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil +} diff --git a/fhevm/operators_bit_gas.go b/fhevm/operators_bit_gas.go new file mode 100644 index 0000000..b8e77fb --- /dev/null +++ b/fhevm/operators_bit_gas.go @@ -0,0 +1,115 @@ +package fhevm + +import ( + "encoding/hex" + + "github.com/ethereum/go-ethereum/common" +) + +func fheShlRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheShift RequiredGas() can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + var lhs, rhs *verifiedCiphertext + if !isScalar { + lhs, rhs, err = get2VerifiedOperands(environment, input) + if err != nil { + logger.Error("fheShift RequiredGas() ciphertext inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + if lhs.fheUintType() != rhs.fheUintType() { + logger.Error("fheShift RequiredGas() operand type mismatch", "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return 0 + } + return environment.FhevmParams().GasCosts.FheShift[lhs.fheUintType()] + } else { + lhs, _, err = getScalarOperands(environment, input) + if err != nil { + logger.Error("fheShift RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + return environment.FhevmParams().GasCosts.FheScalarShift[lhs.fheUintType()] + } +} + +func fheShrRequiredGas(environment EVMEnvironment, input []byte) uint64 { + // Implement in terms of shl, because comparison costs are currently the same. + return fheShlRequiredGas(environment, input) +} + + +func fheNegRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(32, len(input))] + + logger := environment.GetLogger() + if len(input) != 32 { + logger.Error("fheNeg input needs to contain one 256-bit sized value", "input", hex.EncodeToString(input)) + return 0 + } + ct := getVerifiedCiphertext(environment, common.BytesToHash(input[0:32])) + if ct == nil { + logger.Error("fheNeg input not verified", "input", hex.EncodeToString(input)) + return 0 + } + return environment.FhevmParams().GasCosts.FheNeg[ct.fheUintType()] +} + +func fheNotRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(32, len(input))] + + logger := environment.GetLogger() + if len(input) != 32 { + logger.Error("fheNot input needs to contain one 256-bit sized value", "input", hex.EncodeToString(input)) + return 0 + } + ct := getVerifiedCiphertext(environment, common.BytesToHash(input[0:32])) + if ct == nil { + logger.Error("fheNot input not verified", "input", hex.EncodeToString(input)) + return 0 + } + return environment.FhevmParams().GasCosts.FheNot[ct.fheUintType()] +} + +func fheBitAndRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("Bitwise op RequiredGas() can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + + if isScalar { + msg := "Bitwise op RequiredGas() scalar op not supported" + logger.Error(msg) + return 0 + } + + lhs, rhs, err := get2VerifiedOperands(environment, input) + if err != nil { + logger.Error("Bitwise op RequiredGas() inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + if lhs.fheUintType() != rhs.fheUintType() { + logger.Error("Bitwise op RequiredGas() operand type mismatch", "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return 0 + } + return environment.FhevmParams().GasCosts.FheBitwiseOp[lhs.fheUintType()] +} + +func fheBitOrRequiredGas(environment EVMEnvironment, input []byte) uint64 { + // Implement in terms of bitAnd, because bitwise op costs are currently the same. + return fheBitAndRequiredGas(environment, input) +} + +func fheBitXorRequiredGas(environment EVMEnvironment, input []byte) uint64 { + // Implement in terms of bitAnd, because bitwise op costs are currently the same. + return fheBitAndRequiredGas(environment, input) +} diff --git a/fhevm/operators_comparison.go b/fhevm/operators_comparison.go new file mode 100644 index 0000000..2426a3c --- /dev/null +++ b/fhevm/operators_comparison.go @@ -0,0 +1,574 @@ +package fhevm + +import ( + "encoding/hex" + "errors" + + "github.com/ethereum/go-ethereum/common" + "go.opentelemetry.io/otel/trace" +) + + +func fheLeRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheLe can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if !isScalar { + lhs, rhs, err := get2VerifiedOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) + if err != nil { + logger.Error("fheLe inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + if lhs.fheUintType() != rhs.fheUintType() { + msg := "fheLe operand type mismatch" + logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, FheBool), nil + } + + result, err := lhs.ciphertext.Le(rhs.ciphertext) + if err != nil { + logger.Error("fheLe failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheLe success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil + + } else { + lhs, rhs, err := getScalarOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) + if err != nil { + logger.Error("fheLe scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, FheBool), nil + } + + result, err := lhs.ciphertext.ScalarLe(rhs.Uint64()) + if err != nil { + logger.Error("fheLe failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheLe scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) + return resultHash[:], nil + } +} + +func fheLtRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheLt can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if !isScalar { + lhs, rhs, err := get2VerifiedOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) + if err != nil { + logger.Error("fheLt inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + if lhs.fheUintType() != rhs.fheUintType() { + msg := "fheLt operand type mismatch" + logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, FheBool), nil + } + + result, err := lhs.ciphertext.Lt(rhs.ciphertext) + if err != nil { + logger.Error("fheLt failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheLt success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil + + } else { + lhs, rhs, err := getScalarOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) + if err != nil { + logger.Error("fheLt scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, FheBool), nil + } + + result, err := lhs.ciphertext.ScalarLt(rhs.Uint64()) + if err != nil { + logger.Error("fheLt failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheLt scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) + return resultHash[:], nil + } +} + +func fheEqRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheEq can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if !isScalar { + lhs, rhs, err := get2VerifiedOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) + if err != nil { + logger.Error("fheEq inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + if lhs.fheUintType() != rhs.fheUintType() { + msg := "fheEq operand type mismatch" + logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, FheBool), nil + } + + result, err := lhs.ciphertext.Eq(rhs.ciphertext) + if err != nil { + logger.Error("fheEq failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheEq success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil + + } else { + lhs, rhs, err := getScalarOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) + if err != nil { + logger.Error("fheEq scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, FheBool), nil + } + + result, err := lhs.ciphertext.ScalarEq(rhs.Uint64()) + if err != nil { + logger.Error("fheEq failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheEq scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) + return resultHash[:], nil + } +} + +func fheGeRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheGe can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if !isScalar { + lhs, rhs, err := get2VerifiedOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) + if err != nil { + logger.Error("fheGe inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + if lhs.fheUintType() != rhs.fheUintType() { + msg := "fheGe operand type mismatch" + logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, FheBool), nil + } + + result, err := lhs.ciphertext.Ge(rhs.ciphertext) + if err != nil { + logger.Error("fheGe failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheGe success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil + + } else { + lhs, rhs, err := getScalarOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) + if err != nil { + logger.Error("fheGe scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, FheBool), nil + } + + result, err := lhs.ciphertext.ScalarGe(rhs.Uint64()) + if err != nil { + logger.Error("fheGe failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheGe scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) + return resultHash[:], nil + } +} + +func fheGtRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheGt can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if !isScalar { + lhs, rhs, err := get2VerifiedOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) + if err != nil { + logger.Error("fheGt inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + if lhs.fheUintType() != rhs.fheUintType() { + msg := "fheGt operand type mismatch" + logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, FheBool), nil + } + + result, err := lhs.ciphertext.Gt(rhs.ciphertext) + if err != nil { + logger.Error("fheGt failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheGt success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil + + } else { + lhs, rhs, err := getScalarOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) + if err != nil { + logger.Error("fheGt scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, FheBool), nil + } + + result, err := lhs.ciphertext.ScalarGt(rhs.Uint64()) + if err != nil { + logger.Error("fheGt failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheGt scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) + return resultHash[:], nil + } +} + +func fheNeRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheNe can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if !isScalar { + lhs, rhs, err := get2VerifiedOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) + if err != nil { + logger.Error("fheNe inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + if lhs.fheUintType() != rhs.fheUintType() { + msg := "fheNe operand type mismatch" + logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, FheBool), nil + } + + result, err := lhs.ciphertext.Ne(rhs.ciphertext) + if err != nil { + logger.Error("fheNe failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheNe success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil + + } else { + lhs, rhs, err := getScalarOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) + if err != nil { + logger.Error("fheNe scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, FheBool), nil + } + + result, err := lhs.ciphertext.ScalarNe(rhs.Uint64()) + if err != nil { + logger.Error("fheNe failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheNe scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) + return resultHash[:], nil + } +} + + +func fheMinRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheMin can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if !isScalar { + lhs, rhs, err := get2VerifiedOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) + if err != nil { + logger.Error("fheMin inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + if lhs.fheUintType() != rhs.fheUintType() { + msg := "fheMin operand type mismatch" + logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.Min(rhs.ciphertext) + if err != nil { + logger.Error("fheMin failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheMin success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil + + } else { + lhs, rhs, err := getScalarOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) + if err != nil { + logger.Error("fheMin scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.ScalarMin(rhs.Uint64()) + if err != nil { + logger.Error("fheMin failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheMin scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) + return resultHash[:], nil + } +} + +func fheMaxRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheMax can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if !isScalar { + lhs, rhs, err := get2VerifiedOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), encryptedOperand(*rhs)) + if err != nil { + logger.Error("fheMax inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + if lhs.fheUintType() != rhs.fheUintType() { + msg := "fheMax operand type mismatch" + logger.Error(msg, "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.Max(rhs.ciphertext) + if err != nil { + logger.Error("fheMax failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheMax success", "lhs", lhs.hash().Hex(), "rhs", rhs.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil + + } else { + lhs, rhs, err := getScalarOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*lhs), plainOperand(*rhs)) + if err != nil { + logger.Error("fheMax scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, lhs.fheUintType()), nil + } + + result, err := lhs.ciphertext.ScalarMax(rhs.Uint64()) + if err != nil { + logger.Error("fheMax failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheMax scalar success", "lhs", lhs.hash().Hex(), "rhs", rhs.Uint64(), "result", resultHash.Hex()) + return resultHash[:], nil + } +} + + +func fheIfThenElseRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(96, len(input))] + + logger := environment.GetLogger() + first, second, third, err := get3VerifiedOperands(environment, input) + otelDescribeOperands(runSpan, encryptedOperand(*first), encryptedOperand(*second), encryptedOperand(*third)) + if err != nil { + logger.Error("fheIfThenElse inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + + if second.fheUintType() != third.fheUintType() { + msg := "fheIfThenElse operand type mismatch" + logger.Error(msg, "second", second.fheUintType(), "third", third.fheUintType()) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, second.fheUintType()), nil + } + + result, err := first.ciphertext.IfThenElse(second.ciphertext, third.ciphertext) + if err != nil { + logger.Error("fheIfThenElse failed", "err", err) + return nil, err + } + importCiphertext(environment, result) + + resultHash := result.GetHash() + logger.Info("fheIfThenElse success", "first", first.hash().Hex(), "second", second.hash().Hex(), "third", third.hash().Hex(), "result", resultHash.Hex()) + return resultHash[:], nil +} diff --git a/fhevm/operators_comparison_gas.go b/fhevm/operators_comparison_gas.go new file mode 100644 index 0000000..ff19fe6 --- /dev/null +++ b/fhevm/operators_comparison_gas.go @@ -0,0 +1,141 @@ +package fhevm + +import "encoding/hex" + +func fheLeRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("comparison RequiredGas() can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + var lhs, rhs *verifiedCiphertext + if !isScalar { + lhs, rhs, err = get2VerifiedOperands(environment, input) + if err != nil { + logger.Error("comparison RequiredGas() ciphertext inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + if lhs.fheUintType() != rhs.fheUintType() { + logger.Error("comparison RequiredGas() operand type mismatch", "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return 0 + } + } else { + lhs, _, err = getScalarOperands(environment, input) + if err != nil { + logger.Error("comparison RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + } + return environment.FhevmParams().GasCosts.FheLe[lhs.fheUintType()] +} + +func fheLtRequiredGas(environment EVMEnvironment, input []byte) uint64 { + // Implement in terms of le, because le and lt costs are currently the same. + return fheLeRequiredGas(environment, input) +} + +func fheEqRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("comparison RequiredGas() can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + var lhs, rhs *verifiedCiphertext + if !isScalar { + lhs, rhs, err = get2VerifiedOperands(environment, input) + if err != nil { + logger.Error("comparison RequiredGas() ciphertext inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + if lhs.fheUintType() != rhs.fheUintType() { + logger.Error("comparison RequiredGas() operand type mismatch", "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return 0 + } + } else { + lhs, _, err = getScalarOperands(environment, input) + if err != nil { + logger.Error("comparison RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + } + return environment.FhevmParams().GasCosts.FheEq[lhs.fheUintType()] +} + +func fheGeRequiredGas(environment EVMEnvironment, input []byte) uint64 { + // Implement in terms of le, because comparison costs are currently the same. + return fheLeRequiredGas(environment, input) +} + +func fheGtRequiredGas(environment EVMEnvironment, input []byte) uint64 { + // Implement in terms of le, because comparison costs are currently the same. + return fheLeRequiredGas(environment, input) +} + +func fheNeRequiredGas(environment EVMEnvironment, input []byte) uint64 { + // Implement in terms of le, because comparison costs are currently the same. + return fheEqRequiredGas(environment, input) +} + + +func fheMinRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(65, len(input))] + + logger := environment.GetLogger() + isScalar, err := isScalarOp(input) + if err != nil { + logger.Error("fheMin/Max RequiredGas() can not detect if operator is meant to be scalar", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + var lhs, rhs *verifiedCiphertext + if !isScalar { + lhs, rhs, err = get2VerifiedOperands(environment, input) + if err != nil { + logger.Error("fheMin/Max RequiredGas() ciphertext inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + if lhs.fheUintType() != rhs.fheUintType() { + logger.Error("fheMin/Max RequiredGas() operand type mismatch", "lhs", lhs.fheUintType(), "rhs", rhs.fheUintType()) + return 0 + } + return environment.FhevmParams().GasCosts.FheMinMax[lhs.fheUintType()] + } else { + lhs, _, err = getScalarOperands(environment, input) + if err != nil { + logger.Error("fheMin/Max RequiredGas() scalar inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + return environment.FhevmParams().GasCosts.FheScalarMinMax[lhs.fheUintType()] + } +} + +func fheMaxRequiredGas(environment EVMEnvironment, input []byte) uint64 { + // Implement in terms of min, because costs are currently the same. + return fheMinRequiredGas(environment, input) +} + + +func fheIfThenElseRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(96, len(input))] + + logger := environment.GetLogger() + first, second, third, err := get3VerifiedOperands(environment, input) + if err != nil { + logger.Error("IfThenElse op RequiredGas() inputs not verified", "err", err, "input", hex.EncodeToString(input)) + return 0 + } + if first.fheUintType() != FheBool { + logger.Error("IfThenElse op RequiredGas() invalid type for condition", "first", first.fheUintType()) + return 0 + } + if second.fheUintType() != third.fheUintType() { + logger.Error("IfThenElse op RequiredGas() operand type mismatch", "second", second.fheUintType(), "third", third.fheUintType()) + return 0 + } + return environment.FhevmParams().GasCosts.FheIfThenElse[second.fheUintType()] +} diff --git a/fhevm/operators_crypto.go b/fhevm/operators_crypto.go new file mode 100644 index 0000000..682da6c --- /dev/null +++ b/fhevm/operators_crypto.go @@ -0,0 +1,457 @@ +package fhevm + +import ( + "bytes" + "context" + "encoding/binary" + "encoding/hex" + "errors" + "math/big" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/zama-ai/fhevm-go/kms" + "go.opentelemetry.io/otel/trace" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" +) + + +func verifyCiphertextRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + logger := environment.GetLogger() + // first 32 bytes of the payload is offset, then 32 bytes are size of byte array + if len(input) <= 68 { + err := errors.New("verifyCiphertext(bytes) must contain at least 68 bytes for selector, byte offset and size") + logger.Error("fheLib precompile error", "err", err, "input", hex.EncodeToString(input)) + return nil, err + } + bytesPaddingSize := 32 + bytesSizeSlotSize := 32 + // read only last 4 bytes of padded number for byte array size + sizeStart := bytesPaddingSize + bytesSizeSlotSize - 4 + sizeEnd := sizeStart + 4 + bytesSize := binary.BigEndian.Uint32(input[sizeStart:sizeEnd]) + bytesStart := bytesPaddingSize + bytesSizeSlotSize + bytesEnd := bytesStart + int(bytesSize) + input = input[bytesStart:minInt(bytesEnd, len(input))] + + if len(input) <= 1 { + msg := "verifyCiphertext Run() input needs to contain a ciphertext and one byte for its type" + logger.Error(msg, "len", len(input)) + return nil, errors.New(msg) + } + + ctBytes := input[:len(input)-1] + ctTypeByte := input[len(input)-1] + if !isValidFheType(ctTypeByte) { + msg := "verifyCiphertext Run() ciphertext type is invalid" + logger.Error(msg, "type", ctTypeByte) + return nil, errors.New(msg) + } + ctType := FheUintType(ctTypeByte) + otelDescribeOperandsFheTypes(runSpan, ctType) + + expectedSize, found := GetCompactFheCiphertextSize(ctType) + if !found || expectedSize != uint(len(ctBytes)) { + msg := "verifyCiphertext Run() compact ciphertext size is invalid" + logger.Error(msg, "type", ctTypeByte, "size", len(ctBytes), "expectedSize", expectedSize) + return nil, errors.New(msg) + } + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, ctType), nil + } + + ct := new(TfheCiphertext) + err := ct.DeserializeCompact(ctBytes, ctType) + if err != nil { + logger.Error("verifyCiphertext failed to deserialize input ciphertext", + "err", err, + "len", len(ctBytes), + "ctBytes64", hex.EncodeToString(ctBytes[:minInt(len(ctBytes), 64)])) + return nil, err + } + ctHash := ct.GetHash() + importCiphertext(environment, ct) + if environment.IsCommitting() { + logger.Info("verifyCiphertext success", + "ctHash", ctHash.Hex(), + "ctBytes64", hex.EncodeToString(ctBytes[:minInt(len(ctBytes), 64)])) + } + return ctHash.Bytes(), nil +} + +func reencryptRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(64, len(input))] + // precompileBytes, err := reencryptRun(environment, caller, addr, bwCompatBytes, readOnly) + + logger := environment.GetLogger() + if !environment.IsEthCall() { + msg := "reencrypt only supported on EthCall" + logger.Error(msg) + return nil, errors.New(msg) + } + if len(input) != 64 { + msg := "reencrypt input len must be 64 bytes" + logger.Error(msg, "input", hex.EncodeToString(input), "len", len(input)) + return nil, errors.New(msg) + } + ct := getVerifiedCiphertext(environment, common.BytesToHash(input[0:32])) + if ct != nil { + otelDescribeOperandsFheTypes(runSpan, ct.fheUintType()) + // Make sure we don't decrypt before any optimistic requires are checked. + // optReqResult, optReqErr := evaluateRemainingOptimisticRequires(environment) + // if optReqErr != nil { + // return nil, optReqErr + // } else if !optReqResult { + // return nil, ErrExecutionReverted + // } + + var fheType kms.FheType + switch ct.fheUintType() { + case FheBool: + fheType = kms.FheType_Bool + case FheUint4: + fheType = kms.FheType_Euint4 + case FheUint8: + fheType = kms.FheType_Euint8 + case FheUint16: + fheType = kms.FheType_Euint16 + case FheUint32: + fheType = kms.FheType_Euint32 + case FheUint64: + fheType = kms.FheType_Euint64 + } + + pubKey := input[32:64] + + // TODO: generate merkle proof for some data + proof := &kms.Proof{ + Height: 3, + MerklePatriciaProof: []byte{}, + } + + reencryptionRequest := &kms.ReencryptionRequest{ + FheType: fheType, + Ciphertext: ct.serialization(), + Request: pubKey, // TODO: change according to the structure of `Request` + Proof: proof, + } + + conn, err := grpc.Dial(kms.KmsEndpointAddr, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + return nil, errors.New("kms unreachable") + } + defer conn.Close() + + ep := kms.NewKmsEndpointClient(conn) + + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + + res, err := ep.Reencrypt(ctx, reencryptionRequest) + if err != nil { + return nil, err + } + + // TODO: decide if `res.Signature` should be verified here + + var reencryptedValue = res.ReencryptedCiphertext + + logger.Info("reencrypt success", "input", hex.EncodeToString(input), "callerAddr", caller, "reencryptedValue", reencryptedValue, "len", len(reencryptedValue)) + reencryptedValue = toEVMBytes(reencryptedValue) + // pad according to abi specification, first add offset to the dynamic bytes argument + outputBytes := make([]byte, 32, len(reencryptedValue)+32) + outputBytes[31] = 0x20 + outputBytes = append(outputBytes, reencryptedValue...) + return padArrayTo32Multiple(outputBytes), nil + } + msg := "reencrypt unverified ciphertext handle" + logger.Error(msg, "input", hex.EncodeToString(input)) + return nil, errors.New(msg) +} + +func optimisticRequireRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(32, len(input))] + + logger := environment.GetLogger() + if len(input) != 32 { + msg := "optimisticRequire input len must be 32 bytes" + logger.Error(msg, "input", hex.EncodeToString(input), "len", len(input)) + return nil, errors.New(msg) + } + ct := getVerifiedCiphertext(environment, common.BytesToHash(input)) + if ct == nil { + msg := "optimisticRequire unverified handle" + logger.Error(msg, "input", hex.EncodeToString(input)) + return nil, errors.New(msg) + } + otelDescribeOperandsFheTypes(runSpan, ct.fheUintType()) + // If we are doing gas estimation, don't do anything as we would assume all requires are true. + if !environment.IsCommitting() && !environment.IsEthCall() { + return nil, nil + } + if ct.fheUintType() != FheUint8 { + msg := "optimisticRequire ciphertext type is not FheUint8" + logger.Error(msg, "type", ct.fheUintType()) + return nil, errors.New(msg) + } + environment.FhevmData().appendOptimisticRequires(ct.ciphertext) + return nil, nil +} + +func decryptRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(32, len(input))] + + logger := environment.GetLogger() + // if not gas estimation and not view function fail if decryptions are disabled in transactions + if environment.IsCommitting() && !environment.IsEthCall() && environment.FhevmParams().DisableDecryptionsInTransaction { + msg := "decryptions during transaction are disabled" + logger.Error(msg, "input", hex.EncodeToString(input)) + return nil, errors.New(msg) + } + if len(input) != 32 { + msg := "decrypt input len must be 32 bytes" + logger.Error(msg, "input", hex.EncodeToString(input), "len", len(input)) + return nil, errors.New(msg) + } + ct := getVerifiedCiphertext(environment, common.BytesToHash(input)) + if ct == nil { + msg := "decrypt unverified handle" + logger.Error(msg, "input", hex.EncodeToString(input)) + return nil, errors.New(msg) + } + otelDescribeOperandsFheTypes(runSpan, ct.fheUintType()) + + // If we are doing gas estimation, skip decryption and make sure we return the maximum possible value. + // We need that, because non-zero bytes cost more than zero bytes in some contexts (e.g. SSTORE or memory operations). + if !environment.IsCommitting() && !environment.IsEthCall() { + return bytes.Repeat([]byte{0xFF}, 32), nil + } + // Make sure we don't decrypt before any optimistic requires are checked. + optReqResult, optReqErr := evaluateRemainingOptimisticRequires(environment) + if optReqErr != nil { + return nil, optReqErr + } else if !optReqResult { + return nil, ErrExecutionReverted + } + + plaintext, err := decryptValue(environment, ct.ciphertext) + if err != nil { + logger.Error("decrypt failed", "err", err) + return nil, err + } + + logger.Info("decrypt success", "plaintext", plaintext) + + // Always return a 32-byte big-endian integer. + ret := make([]byte, 32) + bigIntValue := big.NewInt(0) + bigIntValue.SetUint64(plaintext) + bigIntValue.FillBytes(ret) + return ret, nil +} + +func getCiphertextRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(64, len(input))] + + logger := environment.GetLogger() + if !environment.IsEthCall() { + msg := "getCiphertext only supported on EthCall" + logger.Error(msg) + return nil, errors.New(msg) + } + if len(input) != 64 { + msg := "getCiphertext input len must be 64 bytes" + logger.Error(msg, "input", hex.EncodeToString(input), "len", len(input)) + return nil, errors.New(msg) + } + + contractAddress := common.BytesToAddress(input[:32]) + handle := common.BytesToHash(input[32:]) + + ciphertext := getCiphertextFromProtectedStoage(environment, contractAddress, handle) + if ciphertext == nil { + return make([]byte, 0), nil + } + otelDescribeOperandsFheTypes(runSpan, ciphertext.metadata.fheUintType) + return ciphertext.bytes, nil +} + +func decryptValue(environment EVMEnvironment, ct *TfheCiphertext) (uint64, error) { + + logger := environment.GetLogger() + var fheType kms.FheType + switch ct.Type() { + case FheBool: + fheType = kms.FheType_Bool + case FheUint4: + fheType = kms.FheType_Euint4 + case FheUint8: + fheType = kms.FheType_Euint8 + case FheUint16: + fheType = kms.FheType_Euint16 + case FheUint32: + fheType = kms.FheType_Euint32 + case FheUint64: + fheType = kms.FheType_Euint64 + } + + // TODO: generate merkle proof for some data + proof := &kms.Proof{ + Height: 4, + MerklePatriciaProof: []byte{}, + } + + decryptionRequest := &kms.DecryptionRequest{ + FheType: fheType, + Ciphertext: ct.Serialize(), + Request: []byte{}, // TODO: change according to the structure of `Request` + Proof: proof, + } + + conn, err := grpc.Dial(kms.KmsEndpointAddr, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + return 0, errors.New("kms unreachable") + } + defer conn.Close() + + ep := kms.NewKmsEndpointClient(conn) + + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + + res, err := ep.Decrypt(ctx, decryptionRequest) + if err != nil { + logger.Error("decrypt failed", "err", err) + return 0, err + } + + return uint64(res.Plaintext), err +} + +// If there are optimistic requires, check them by doing bitwise AND on all of them. +// 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.FhevmData().optimisticRequires + len := len(requires) + defer func() { environment.FhevmData().resetOptimisticRequires() }() + if len != 0 { + var cumulative *TfheCiphertext = requires[0] + var err error + for i := 1; i < len; i++ { + cumulative, err = cumulative.Bitand(requires[i]) + if err != nil { + environment.GetLogger().Error("evaluateRemainingOptimisticRequires bitand failed", "err", err) + return false, err + } + } + result, err := decryptValue(environment, cumulative) + return result != 0, err + } + return true, nil +} + +func castRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(33, len(input))] + + logger := environment.GetLogger() + if len(input) != 33 { + msg := "cast Run() input needs to contain a ciphertext and one byte for its type" + logger.Error(msg, "len", len(input)) + return nil, errors.New(msg) + } + + ct := getVerifiedCiphertext(environment, common.BytesToHash(input[0:32])) + if ct == nil { + logger.Error("cast input not verified") + return nil, errors.New("unverified ciphertext handle") + } + + if !isValidFheType(input[32]) { + logger.Error("invalid type to cast to") + return nil, errors.New("invalid type provided") + } + castToType := FheUintType(input[32]) + + otelDescribeOperandsFheTypes(runSpan, ct.fheUintType(), castToType) + + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() && !environment.IsEthCall() { + return importRandomCiphertext(environment, castToType), nil + } + + res, err := ct.ciphertext.CastTo(castToType) + if err != nil { + msg := "cast Run() error casting ciphertext to" + logger.Error(msg, "type", castToType) + return nil, errors.New(msg) + } + + resHash := res.GetHash() + + importCiphertext(environment, res) + if environment.IsCommitting() { + logger.Info("cast success", + "ctHash", resHash.Hex(), + ) + } + + return resHash.Bytes(), nil +} + +var fhePubKeyHashPrecompile = common.BytesToAddress([]byte{93}) +var fhePubKeyHashSlot = common.Hash{} + +func fhePubKeyRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(1, len(input))] + + existing := environment.GetState(fhePubKeyHashPrecompile, fhePubKeyHashSlot) + if existing != GetPksHash() { + msg := "fhePubKey FHE public key hash doesn't match one stored in state" + environment.GetLogger().Error(msg, "existing", existing.Hex(), "pksHash", GetPksHash().Hex()) + return nil, errors.New(msg) + } + // serialize public key + pksBytes, err := serializePublicKey() + if err != nil { + return nil, err + } + // If we have a single byte with the value of 1, make as an EVM array. + if len(input) == 1 && input[0] == 1 { + pksBytes = toEVMBytes(pksBytes) + } + // pad according to abi specification, first add offset to the dynamic bytes argument + outputBytes := make([]byte, 32, len(pksBytes)+32) + outputBytes[31] = 0x20 + outputBytes = append(outputBytes, pksBytes...) + return padArrayTo32Multiple(outputBytes), nil +} + +func trivialEncryptRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(33, len(input))] + + logger := environment.GetLogger() + if len(input) != 33 { + msg := "trivialEncrypt input len must be 33 bytes" + logger.Error(msg, "input", hex.EncodeToString(input), "len", len(input)) + return nil, errors.New(msg) + } + + valueToEncrypt := *new(big.Int).SetBytes(input[0:32]) + encryptToType := FheUintType(input[32]) + otelDescribeOperandsFheTypes(runSpan, encryptToType) + + ct := new(TfheCiphertext).TrivialEncrypt(valueToEncrypt, encryptToType) + + ctHash := ct.GetHash() + importCiphertext(environment, ct) + if environment.IsCommitting() { + logger.Info("trivialEncrypt success", + "ctHash", ctHash.Hex(), + "valueToEncrypt", valueToEncrypt.Uint64()) + } + return ctHash.Bytes(), nil +} diff --git a/fhevm/operators_crypto_gas.go b/fhevm/operators_crypto_gas.go new file mode 100644 index 0000000..f1f45f9 --- /dev/null +++ b/fhevm/operators_crypto_gas.go @@ -0,0 +1,123 @@ +package fhevm + +import ( + "encoding/hex" + + "github.com/ethereum/go-ethereum/common" +) + +func verifyCiphertextRequiredGas(environment EVMEnvironment, input []byte) uint64 { + if len(input) <= 1 { + environment.GetLogger().Error( + "verifyCiphertext RequiredGas() input needs to contain a ciphertext and one byte for its type", + "len", len(input)) + return 0 + } + ctType := FheUintType(input[len(input)-1]) + return environment.FhevmParams().GasCosts.FheVerify[ctType] +} + +func reencryptRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(64, len(input))] + + logger := environment.GetLogger() + if len(input) != 64 { + logger.Error("reencrypt RequiredGas() input len must be 64 bytes", "input", hex.EncodeToString(input), "len", len(input)) + return 0 + } + ct := getVerifiedCiphertext(environment, common.BytesToHash(input[0:32])) + if ct == nil { + logger.Error("reencrypt RequiredGas() input doesn't point to verified ciphertext", "input", hex.EncodeToString(input)) + return 0 + } + return environment.FhevmParams().GasCosts.FheReencrypt[ct.fheUintType()] +} + +func optimisticRequireRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(32, len(input))] + + logger := environment.GetLogger() + if len(input) != 32 { + logger.Error("optimisticRequire RequiredGas() input len must be 32 bytes", + "input", hex.EncodeToString(input), "len", len(input)) + return 0 + } + ct := getVerifiedCiphertext(environment, common.BytesToHash(input)) + if ct == nil { + logger.Error("optimisticRequire RequiredGas() input doesn't point to verified ciphertext", + "input", hex.EncodeToString(input)) + return 0 + } + if ct.fheUintType() != FheUint8 { + logger.Error("optimisticRequire RequiredGas() ciphertext type is not FheUint8", + "type", ct.fheUintType()) + return 0 + } + if len(environment.FhevmData().optimisticRequires) == 0 { + return environment.FhevmParams().GasCosts.FheOptRequire[FheUint8] + } + return environment.FhevmParams().GasCosts.FheOptRequireBitAnd[FheUint8] +} + +func getCiphertextRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(64, len(input))] + + logger := environment.GetLogger() + if len(input) != 64 { + logger.Error("getCiphertext RequiredGas() input len must be 64 bytes", + "input", hex.EncodeToString(input), "len", len(input)) + return 0 + } + + contractAddress := common.BytesToAddress(input[:32]) + handle := common.BytesToHash(input[32:]) + metadata := getCiphertextMetadataFromProtectedStorage(environment, contractAddress, handle) + if metadata == nil { + return GetNonExistentCiphertextGas + } + return environment.FhevmParams().GasCosts.FheGetCiphertext[metadata.fheUintType] +} + +func castRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(33, len(input))] + + if len(input) != 33 { + environment.GetLogger().Error( + "cast RequiredGas() input needs to contain a ciphertext and one byte for its type", + "len", len(input)) + return 0 + } + return environment.FhevmParams().GasCosts.FheCast +} + +func decryptRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(32, len(input))] + + logger := environment.GetLogger() + if len(input) != 32 { + logger.Error("decrypt RequiredGas() input len must be 32 bytes", "input", hex.EncodeToString(input), "len", len(input)) + return 0 + } + ct := getVerifiedCiphertext(environment, common.BytesToHash(input)) + if ct == nil { + logger.Error("decrypt RequiredGas() input doesn't point to verified ciphertext", "input", hex.EncodeToString(input)) + return 0 + } + return environment.FhevmParams().GasCosts.FheDecrypt[ct.fheUintType()] +} + +func fhePubKeyRequiredGas(environment EVMEnvironment, input []byte) uint64 { + return environment.FhevmParams().GasCosts.FhePubKey +} + +func trivialEncryptRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(33, len(input))] + + logger := environment.GetLogger() + if len(input) != 33 { + logger.Error("trivialEncrypt RequiredGas() input len must be 33 bytes", "input", hex.EncodeToString(input), "len", len(input)) + return 0 + } + encryptToType := FheUintType(input[32]) + return environment.FhevmParams().GasCosts.FheTrivialEncrypt[encryptToType] +} diff --git a/fhevm/operators_rand.go b/fhevm/operators_rand.go new file mode 100644 index 0000000..dd64f94 --- /dev/null +++ b/fhevm/operators_rand.go @@ -0,0 +1,180 @@ +package fhevm + +import ( + "encoding/binary" + "encoding/hex" + "errors" + "fmt" + "math/big" + "math/bits" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + "github.com/holiman/uint256" + fhevm_crypto "github.com/zama-ai/fhevm-go/crypto" + "go.opentelemetry.io/otel/trace" + "golang.org/x/crypto/chacha20" +) + +var globalRngSeed []byte + +var rngNonceKey [32]byte = uint256.NewInt(0).Bytes32() + +func init() { + if chacha20.NonceSizeX != 24 { + panic("expected 24 bytes for NonceSizeX") + } + + // TODO: Since the current implementation is not FHE-based and, hence, not private, + // we just initialize the global seed with non-random public data. We will change + // that once the FHE version is available. + globalRngSeed = make([]byte, chacha20.KeySize) + for i := range globalRngSeed { + globalRngSeed[i] = byte(1 + i) + } +} + +// Applies the upperBound (if set) to the rand value and returns the result. +// bitsInRand is the amount of random bits that are contained in rand. +// bitsInRand and upperBound must be powers of 2. +func applyUpperBound(rand uint64, bitsInRand int, upperBound *uint64) uint64 { + if upperBound == nil { + return rand + } else if *upperBound == 0 { + panic("sliceRandom called with upperBound of 0") + } + // Len64() returns the amount of bits needed to represent upperBound. Subtract 1 to get the + // amount of bits requested by the given upperBound as we want to return a value in the [0, upperBound) range. + // Note that upperBound is assumed to be a power of 2. + // + // For example, if upperBound = 128, then bits = 8 - 1 = 7 random bits to be returned. + // To get that amount of random bits from rand, subtract bits from bitsInRand, i.e. + // shift = 32 - 7 = 25. Shifting rand 25 positions would leave 7 of its random bits. + bits := bits.Len64(*upperBound) - 1 + shift := bitsInRand - bits + // If the shift ends up negative or 0, just return rand without any shifts. + if shift <= 0 { + return rand + } + return rand >> shift +} + +func generateRandom(environment EVMEnvironment, caller common.Address, resultType FheUintType, upperBound *uint64) ([]byte, error) { + // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. + if !environment.IsCommitting() { + return importRandomCiphertext(environment, resultType), nil + } + + // Get the RNG nonce. + protectedStorage := fhevm_crypto.CreateProtectedStorageContractAddress(caller) + currentRngNonceBytes := environment.GetState(protectedStorage, rngNonceKey).Bytes() + + // Increment the RNG nonce by 1. + nextRngNonce := uint256.NewInt(0).SetBytes(currentRngNonceBytes) + nextRngNonce = nextRngNonce.AddUint64(nextRngNonce, 1) + environment.SetState(protectedStorage, rngNonceKey, nextRngNonce.Bytes32()) + + // Compute the seed and use it to create a new cipher. + hasher := crypto.NewKeccakState() + hasher.Write(globalRngSeed) + hasher.Write(caller.Bytes()) + seed := common.Hash{} + _, err := hasher.Read(seed[:]) + if err != nil { + return nil, err + } + // The RNG nonce bytes are of size chacha20.NonceSizeX, which is assumed to be 24 bytes (see init() above). + // Since uint256.Int.z[0] is the least significant byte and since uint256.Int.Bytes32() serializes + // in order of z[3], z[2], z[1], z[0], we want to essentially ignore the first byte, i.e. z[3], because + // it will always be 0 as the nonce size is 24. + cipher, err := chacha20.NewUnauthenticatedCipher(seed.Bytes(), currentRngNonceBytes[32-chacha20.NonceSizeX:32]) + if err != nil { + return nil, err + } + + // XOR a byte array of 0s with the stream from the cipher and receive the result in the same array. + // Apply upperBound, if set. + var randUint uint64 + switch resultType { + case FheUint4: + randBytes := make([]byte, 1) + cipher.XORKeyStream(randBytes, randBytes) + randUint = uint64(randBytes[0]) + randUint = uint64(applyUpperBound(randUint, 4, upperBound)) + case FheUint8: + randBytes := make([]byte, 1) + cipher.XORKeyStream(randBytes, randBytes) + randUint = uint64(randBytes[0]) + randUint = uint64(applyUpperBound(randUint, 8, upperBound)) + case FheUint16: + randBytes := make([]byte, 2) + cipher.XORKeyStream(randBytes, randBytes) + randUint = uint64(binary.BigEndian.Uint16(randBytes)) + randUint = uint64(applyUpperBound(randUint, 16, upperBound)) + case FheUint32: + randBytes := make([]byte, 4) + cipher.XORKeyStream(randBytes, randBytes) + randUint = uint64(binary.BigEndian.Uint32(randBytes)) + randUint = uint64(applyUpperBound(randUint, 32, upperBound)) + case FheUint64: + randBytes := make([]byte, 8) + cipher.XORKeyStream(randBytes, randBytes) + randUint = uint64(binary.BigEndian.Uint64(randBytes)) + randUint = uint64(applyUpperBound(randUint, 64, upperBound)) + default: + return nil, fmt.Errorf("generateRandom() invalid type requested: %d", resultType) + } + + // Trivially encrypt the random integer. + randCt := new(TfheCiphertext) + randBigInt := big.NewInt(0) + randBigInt.SetUint64(randUint) + randCt.TrivialEncrypt(*randBigInt, resultType) + importCiphertext(environment, randCt) + + if err != nil { + return nil, err + } + ctHash := randCt.GetHash() + return ctHash[:], nil +} + +func fheRandRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(1, len(input))] + + logger := environment.GetLogger() + if environment.IsEthCall() { + msg := "fheRand cannot be called via EthCall, because it needs to mutate internal state" + logger.Error(msg) + return nil, errors.New(msg) + } + if len(input) != 1 || !isValidFheType(input[0]) { + msg := "fheRand input len must be at least 1 byte and be a valid FheUint type" + logger.Error(msg, "input", hex.EncodeToString(input), "len", len(input)) + return nil, errors.New(msg) + } + resultType := FheUintType(input[0]) + otelDescribeOperandsFheTypes(runSpan, resultType) + var noUpperBound *uint64 = nil + return generateRandom(environment, caller, resultType, noUpperBound) +} + +func fheRandBoundedRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { + input = input[:minInt(33, len(input))] + + logger := environment.GetLogger() + if environment.IsEthCall() { + msg := "fheRandBoundedRun cannot be called via EthCall, because it needs to mutate internal state" + logger.Error(msg) + return nil, errors.New(msg) + } + randType, bound, err := parseRandUpperBoundInput(input) + otelDescribeOperandsFheTypes(runSpan, randType) + if err != nil { + msg := "fheRandBounded bound error" + logger.Error(msg, "input", hex.EncodeToString(input), "err", err) + return nil, errors.New(msg) + } + bound64 := bound.Uint64() + return generateRandom(environment, caller, randType, &bound64) +} diff --git a/fhevm/operators_rand_gas.go b/fhevm/operators_rand_gas.go new file mode 100644 index 0000000..c459a26 --- /dev/null +++ b/fhevm/operators_rand_gas.go @@ -0,0 +1,52 @@ +package fhevm + +import ( + "encoding/hex" + "fmt" + "math/bits" + + "github.com/holiman/uint256" +) + +func fheRandRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(1, len(input))] + + logger := environment.GetLogger() + if len(input) != 1 || !isValidFheType(input[0]) { + 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 environment.FhevmParams().GasCosts.FheRand[t] +} + +func parseRandUpperBoundInput(input []byte) (randType FheUintType, upperBound *uint256.Int, err error) { + if len(input) != 33 || !isValidFheType(input[32]) { + return FheUint8, nil, fmt.Errorf("parseRandUpperBoundInput() invalid input len or type") + } + randType = FheUintType(input[32]) + upperBound = uint256.NewInt(0) + upperBound.SetBytes32(input) + // For now, we only support bounds of up to 64 bits. + if !upperBound.IsUint64() { + return FheUint8, nil, fmt.Errorf("parseRandUpperBoundInput() only supports bounds up to 64 bits") + } + upperBound64 := upperBound.Uint64() + oneBits := bits.OnesCount64(upperBound64) + if oneBits != 1 { + return FheUint8, nil, fmt.Errorf("parseRandUpperBoundInput() bound not a power of 2: %d", upperBound64) + } + return randType, upperBound, nil +} + +func fheRandBoundedRequiredGas(environment EVMEnvironment, input []byte) uint64 { + input = input[:minInt(33, len(input))] + + logger := environment.GetLogger() + randType, _, err := parseRandUpperBoundInput(input) + if err != nil { + logger.Error("fheRandBounded RequiredGas() bound error", "input", hex.EncodeToString(input), "err", err) + return 0 + } + return environment.FhevmParams().GasCosts.FheRand[randType] +} From 4fe70428d878b1a7d85b16b2799b01006f64da1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Tue, 5 Mar 2024 03:57:52 +0100 Subject: [PATCH 2/9] chore: add tfhe package --- Makefile | 3 +- fhevm/contracts_test.go | 1296 ++++++++++++------------ fhevm/evm.go | 16 +- fhevm/instructions.go | 3 +- fhevm/instructions_test.go | 27 +- fhevm/interface.go | 9 +- fhevm/interpreter.go | 13 +- fhevm/operators_comparison.go | 25 +- fhevm/operators_comparison_gas.go | 8 +- fhevm/operators_crypto.go | 53 +- fhevm/operators_crypto_gas.go | 11 +- fhevm/operators_rand.go | 19 +- fhevm/operators_rand_gas.go | 17 +- fhevm/otel.go | 3 +- fhevm/params.go | 360 +++---- fhevm/protected_storage.go | 9 +- {fhevm => tfhe}/tfhe_ciphertext.go | 338 +++--- {fhevm => tfhe}/tfhe_key_management.go | 38 +- {fhevm => tfhe}/tfhe_test.go | 14 +- {fhevm => tfhe}/tfhe_wrappers.c | 0 {fhevm => tfhe}/tfhe_wrappers.go | 8 +- {fhevm => tfhe}/tfhe_wrappers.h | 0 22 files changed, 1153 insertions(+), 1117 deletions(-) rename {fhevm => tfhe}/tfhe_ciphertext.go (88%) rename {fhevm => tfhe}/tfhe_key_management.go (74%) rename {fhevm => tfhe}/tfhe_test.go (99%) rename {fhevm => tfhe}/tfhe_wrappers.c (100%) rename {fhevm => tfhe}/tfhe_wrappers.go (90%) rename {fhevm => tfhe}/tfhe_wrappers.h (100%) diff --git a/Makefile b/Makefile index d83dff6..0cd736f 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,8 @@ build: build-tfhe-rs-capi .PHONY: test test: build-tfhe-rs-capi - cd fhevm && go test -v . + cd fhevm && go test -v . \ + && cd ../tfhe && go test -v . .PHONY: build-tfhe-rs-capi build-tfhe-rs-capi: diff --git a/fhevm/contracts_test.go b/fhevm/contracts_test.go index 9e7fc84..6a0d6a5 100644 --- a/fhevm/contracts_test.go +++ b/fhevm/contracts_test.go @@ -5,7 +5,9 @@ import ( "encoding/binary" "encoding/hex" "errors" + "fmt" "math/big" + "os" "strings" "testing" @@ -13,8 +15,22 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" + "github.com/zama-ai/fhevm-go/tfhe" ) +// generate keys if not present +func setup() { + if !tfhe.AllGlobalKeysPresent() { + fmt.Println("INFO: initializing global keys in tests") + tfhe.InitGlobalKeysWithNewKeys() + } +} + +func TestMain(m *testing.M) { + setup() + os.Exit(m.Run()) +} + func init() { // register errors from geth so that tests recognize them RegisterErrors( @@ -66,7 +82,7 @@ func evaluateRemainingOptimisticRequiresWithoutKms(environment EVMEnvironment) ( len := len(requires) defer func() { environment.FhevmData().resetOptimisticRequires() }() if len != 0 { - var cumulative *TfheCiphertext = requires[0] + var cumulative *tfhe.TfheCiphertext = requires[0] var err error for i := 1; i < len; i++ { cumulative, err = cumulative.Bitand(requires[i]) @@ -168,20 +184,20 @@ func prepareInputForVerifyCiphertext(input []byte) []byte { return append(append(padding, size...), input...) } -func VerifyCiphertext(t *testing.T, fheUintType FheUintType) { +func VerifyCiphertext(t *testing.T, fheUintType tfhe.FheUintType) { var value uint64 switch fheUintType { - case FheBool: + case tfhe.FheBool: value = 1 - case FheUint4: + case tfhe.FheUint4: value = 4 - case FheUint8: + case tfhe.FheUint8: value = 234 - case FheUint16: + case tfhe.FheUint16: value = 4283 - case FheUint32: + case tfhe.FheUint32: value = 1333337 - case FheUint64: + case tfhe.FheUint64: value = 13333377777777777 } depth := 1 @@ -189,13 +205,13 @@ func VerifyCiphertext(t *testing.T, fheUintType FheUintType) { environment.depth = depth addr := common.Address{} readOnly := false - compact := encryptAndSerializeCompact(value, fheUintType) + compact := tfhe.EncryptAndSerializeCompact(value, fheUintType) input := prepareInputForVerifyCiphertext(append(compact, byte(fheUintType))) out, err := verifyCiphertextRun(environment, addr, addr, input, readOnly, nil) if err != nil { t.Fatalf(err.Error()) } - ct := new(TfheCiphertext) + ct := new(tfhe.TfheCiphertext) if err = ct.DeserializeCompact(compact, fheUintType); err != nil { t.Fatalf(err.Error()) } @@ -208,18 +224,18 @@ func VerifyCiphertext(t *testing.T, fheUintType FheUintType) { } } -func VerifyCiphertextBadType(t *testing.T, actualType FheUintType, metadataType FheUintType) { +func VerifyCiphertextBadType(t *testing.T, actualType tfhe.FheUintType, metadataType tfhe.FheUintType) { var value uint64 switch actualType { - case FheUint4: + case tfhe.FheUint4: value = 2 - case FheUint8: + case tfhe.FheUint8: value = 2 - case FheUint16: + case tfhe.FheUint16: value = 4283 - case FheUint32: + case tfhe.FheUint32: value = 1333337 - case FheUint64: + case tfhe.FheUint64: value = 13333377777777777 } depth := 1 @@ -227,7 +243,7 @@ func VerifyCiphertextBadType(t *testing.T, actualType FheUintType, metadataType environment.depth = depth addr := common.Address{} readOnly := false - compact := encryptAndSerializeCompact(value, actualType) + compact := tfhe.EncryptAndSerializeCompact(value, actualType) input := prepareInputForVerifyCiphertext(append(compact, byte(metadataType))) _, err := verifyCiphertextRun(environment, addr, addr, input, readOnly, nil) if err == nil { @@ -238,20 +254,20 @@ func VerifyCiphertextBadType(t *testing.T, actualType FheUintType, metadataType } } -func TrivialEncrypt(t *testing.T, fheUintType FheUintType) { +func TrivialEncrypt(t *testing.T, fheUintType tfhe.FheUintType) { var value big.Int switch fheUintType { - case FheBool: + case tfhe.FheBool: value = *big.NewInt(1) - case FheUint4: + case tfhe.FheUint4: value = *big.NewInt(2) - case FheUint8: + case tfhe.FheUint8: value = *big.NewInt(2) - case FheUint16: + case tfhe.FheUint16: value = *big.NewInt(4283) - case FheUint32: + case tfhe.FheUint32: value = *big.NewInt(1333337) - case FheUint64: + case tfhe.FheUint64: value = *big.NewInt(13333377777777777) } depth := 1 @@ -265,7 +281,7 @@ func TrivialEncrypt(t *testing.T, fheUintType FheUintType) { if err != nil { t.Fatalf(err.Error()) } - ct := new(TfheCiphertext).TrivialEncrypt(value, fheUintType) + ct := new(tfhe.TfheCiphertext).TrivialEncrypt(value, fheUintType) if common.BytesToHash(out) != ct.GetHash() { t.Fatalf("output hash in verifyCipertext is incorrect") } @@ -275,22 +291,22 @@ func TrivialEncrypt(t *testing.T, fheUintType FheUintType) { } } -func FheLibAdd(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibAdd(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777777777 lhs = 133377777777 @@ -324,22 +340,22 @@ func FheLibAdd(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibSub(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibSub(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777777777 lhs = 133377777777 } @@ -372,22 +388,22 @@ func FheLibSub(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibMul(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibMul(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 3 rhs = 2 - case FheUint8: + case tfhe.FheUint8: lhs = 3 rhs = 2 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777777 lhs = 1337 } @@ -420,22 +436,22 @@ func FheLibMul(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibLe(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibLe(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 1333777777777 lhs = 133377777777 } @@ -488,22 +504,22 @@ func FheLibLe(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibLt(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibLt(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777777777 lhs = 133377777777 } @@ -557,22 +573,22 @@ func FheLibLt(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibEq(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibEq(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777777777 lhs = 133377777777 } @@ -605,22 +621,22 @@ func FheLibEq(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibGe(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibGe(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777777777 lhs = 133377777777 } @@ -671,22 +687,22 @@ func FheLibGe(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibGt(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibGt(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777777777 lhs = 133377777777 } @@ -739,22 +755,22 @@ func FheLibGt(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibShl(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibShl(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 2 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 3 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777777777 lhs = 34 } @@ -787,22 +803,22 @@ func FheLibShl(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibShr(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibShr(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 3 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 3 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777777777 lhs = 34 } @@ -835,22 +851,22 @@ func FheLibShr(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibNe(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibNe(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777777777 lhs = 133377777777 } @@ -883,22 +899,22 @@ func FheLibNe(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibMin(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibMin(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777777777 lhs = 133377777777 } @@ -950,22 +966,22 @@ func FheLibMin(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibMax(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibMax(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777777777 lhs = 133377777777 } @@ -1017,22 +1033,22 @@ func FheLibMax(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibNeg(t *testing.T, fheUintType FheUintType) { +func FheLibNeg(t *testing.T, fheUintType tfhe.FheUintType) { var pt, expected uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: pt = 7 expected = uint64(16 - uint8(pt)) - case FheUint8: + case tfhe.FheUint8: pt = 2 expected = uint64(-uint8(pt)) - case FheUint16: + case tfhe.FheUint16: pt = 4283 expected = uint64(-uint16(pt)) - case FheUint32: + case tfhe.FheUint32: pt = 1333337 expected = uint64(-uint32(pt)) - case FheUint64: + case tfhe.FheUint64: pt = 13333377777777777 expected = uint64(-uint64(pt)) } @@ -1060,25 +1076,25 @@ func FheLibNeg(t *testing.T, fheUintType FheUintType) { } } -func FheLibNot(t *testing.T, fheUintType FheUintType) { +func FheLibNot(t *testing.T, fheUintType tfhe.FheUintType) { var pt, expected uint64 switch fheUintType { - case FheBool: + case tfhe.FheBool: pt = 1 expected = 0 - case FheUint4: + case tfhe.FheUint4: pt = 5 expected = uint64(15 - uint8(pt)) - case FheUint8: + case tfhe.FheUint8: pt = 2 expected = uint64(^uint8(pt)) - case FheUint16: + case tfhe.FheUint16: pt = 4283 expected = uint64(^uint16(pt)) - case FheUint32: + case tfhe.FheUint32: pt = 1333337 expected = uint64(^uint32(pt)) - case FheUint64: + case tfhe.FheUint64: pt = 13333377777777777 expected = uint64(^uint64(pt)) } @@ -1106,22 +1122,22 @@ func FheLibNot(t *testing.T, fheUintType FheUintType) { } } -func FheLibDiv(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibDiv(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 4 rhs = 2 - case FheUint8: + case tfhe.FheUint8: lhs = 4 rhs = 2 - case FheUint16: + case tfhe.FheUint16: lhs = 721 rhs = 1000 - case FheUint32: + case tfhe.FheUint32: lhs = 137 rhs = 17 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777777777 lhs = 1337 } @@ -1161,22 +1177,22 @@ func FheLibDiv(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibRem(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibRem(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 7 rhs = 3 - case FheUint8: + case tfhe.FheUint8: lhs = 7 rhs = 3 - case FheUint16: + case tfhe.FheUint16: lhs = 721 rhs = 1000 - case FheUint32: + case tfhe.FheUint32: lhs = 1337 rhs = 73 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777777777 lhs = 1337 } @@ -1215,25 +1231,25 @@ func FheLibRem(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibBitAnd(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibBitAnd(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheBool: + case tfhe.FheBool: lhs = 1 rhs = 0 - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777777777 lhs = 1337 } @@ -1272,25 +1288,25 @@ func FheLibBitAnd(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibBitOr(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibBitOr(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheBool: + case tfhe.FheBool: lhs = 1 rhs = 0 - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777777777 lhs = 1337 } @@ -1329,25 +1345,25 @@ func FheLibBitOr(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibBitXor(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLibBitXor(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheBool: + case tfhe.FheBool: lhs = 1 rhs = 0 - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777777777 lhs = 1337 } @@ -1386,7 +1402,7 @@ func FheLibBitXor(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLibRand(t *testing.T, fheUintType FheUintType) { +func FheLibRand(t *testing.T, fheUintType tfhe.FheUintType) { signature := "fheRand(bytes1)" depth := 1 environment := newTestEVMEnvironment() @@ -1417,30 +1433,30 @@ func FheLibRand(t *testing.T, fheUintType FheUintType) { t.Fatalf("decrypted value is not 64 bit") } switch fheUintType { - case FheUint4: + case tfhe.FheUint4: if decrypted.Uint64() > 0xF { t.Fatalf("random value is bigger than 0xFF (4 bits)") } - case FheUint8: + case tfhe.FheUint8: if decrypted.Uint64() > 0xFF { t.Fatalf("random value is bigger than 0xFF (8 bits)") } - case FheUint16: + case tfhe.FheUint16: if decrypted.Uint64() > 0xFFFF { t.Fatalf("random value is bigger than 0xFFFF (16 bits)") } - case FheUint32: + case tfhe.FheUint32: if decrypted.Uint64() > 0xFFFFFFFF { t.Fatalf("random value is bigger than 0xFFFFFFFF (32 bits)") } - case FheUint64: + case tfhe.FheUint64: if decrypted.Uint64() > 0xFFFFFFFFFFFFFFFF { t.Fatalf("random value is bigger than 0xFFFFFFFFFFFFFFFF (64 bits)") } } } -func FheLibRandBounded(t *testing.T, fheUintType FheUintType, upperBound64 uint64) { +func FheLibRandBounded(t *testing.T, fheUintType tfhe.FheUintType, upperBound64 uint64) { signature := "fheRandBounded(uint256,bytes1)" depth := 1 environment := newTestEVMEnvironment() @@ -1477,22 +1493,22 @@ func FheLibRandBounded(t *testing.T, fheUintType FheUintType, upperBound64 uint6 } } -func FheLibIfThenElse(t *testing.T, fheUintType FheUintType, condition uint64) { +func FheLibIfThenElse(t *testing.T, fheUintType tfhe.FheUintType, condition uint64) { var second, third uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: second = 2 third = 1 - case FheUint8: + case tfhe.FheUint8: second = 2 third = 1 - case FheUint16: + case tfhe.FheUint16: second = 4283 third = 1337 - case FheUint32: + case tfhe.FheUint32: second = 1333337 third = 133337 - case FheUint64: + case tfhe.FheUint64: second = 1333337777777 third = 133337 } @@ -1502,7 +1518,7 @@ func FheLibIfThenElse(t *testing.T, fheUintType FheUintType, condition uint64) { environment.depth = depth addr := common.Address{} readOnly := false - firstHash := verifyCiphertextInTestMemory(environment, condition, depth, FheBool).GetHash() + firstHash := verifyCiphertextInTestMemory(environment, condition, depth, tfhe.FheBool).GetHash() secondHash := verifyCiphertextInTestMemory(environment, second, depth, fheUintType).GetHash() thirdHash := verifyCiphertextInTestMemory(environment, third, depth, fheUintType).GetHash() input := toLibPrecompileInputNoScalar(signature, firstHash, secondHash, thirdHash) @@ -1521,18 +1537,18 @@ func FheLibIfThenElse(t *testing.T, fheUintType FheUintType, condition uint64) { } } -func LibTrivialEncrypt(t *testing.T, fheUintType FheUintType) { +func LibTrivialEncrypt(t *testing.T, fheUintType tfhe.FheUintType) { var value big.Int switch fheUintType { - case FheUint4: + case tfhe.FheUint4: value = *big.NewInt(2) - case FheUint8: + case tfhe.FheUint8: value = *big.NewInt(2) - case FheUint16: + case tfhe.FheUint16: value = *big.NewInt(4283) - case FheUint32: + case tfhe.FheUint32: value = *big.NewInt(1333337) - case FheUint64: + case tfhe.FheUint64: value = *big.NewInt(133333777777) } signature := "trivialEncrypt(uint256,bytes1)" @@ -1552,7 +1568,7 @@ func LibTrivialEncrypt(t *testing.T, fheUintType FheUintType) { if err != nil { t.Fatalf(err.Error()) } - ct := new(TfheCiphertext).TrivialEncrypt(value, fheUintType) + ct := new(tfhe.TfheCiphertext).TrivialEncrypt(value, fheUintType) if common.BytesToHash(out) != ct.GetHash() { t.Fatalf("output hash in verifyCipertext is incorrect") } @@ -1562,18 +1578,18 @@ func LibTrivialEncrypt(t *testing.T, fheUintType FheUintType) { } } -func LibDecrypt(t *testing.T, fheUintType FheUintType) { +func LibDecrypt(t *testing.T, fheUintType tfhe.FheUintType) { var value uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: value = 2 - case FheUint8: + case tfhe.FheUint8: value = 2 - case FheUint16: + case tfhe.FheUint16: value = 4283 - case FheUint32: + case tfhe.FheUint32: value = 1333337 - case FheUint64: + case tfhe.FheUint64: value = 133333777777 } signature := "decrypt(uint256)" @@ -1610,10 +1626,10 @@ func TestLibVerifyCiphertextInvalidType(t *testing.T) { environment.depth = depth addr := common.Address{} readOnly := false - invalidType := FheUintType(255) + invalidType := tfhe.FheUintType(255) input := make([]byte, 0) input = append(input, signatureBytes...) - compact := encryptAndSerializeCompact(0, FheUint32) + compact := tfhe.EncryptAndSerializeCompact(0, tfhe.FheUint32) input = append(input, compact...) input = append(input, byte(invalidType)) _, err := FheLibRun(environment, addr, addr, input, readOnly) @@ -1636,7 +1652,7 @@ func TestLibVerifyCiphertextInvalidType(t *testing.T) { // environment.depth = depth // environment.ethCall = true // toEncrypt := 7 -// fheUintType := FheUint8 +// fheUintType := tfhe.FheUint8 // encCiphertext := verifyCiphertextInTestMemory(environment, uint64(toEncrypt), depth, fheUintType).getHash() // addr := common.Address{} // readOnly := false @@ -1660,36 +1676,36 @@ func TestLibCast(t *testing.T) { environment.depth = depth environment.ethCall = true toEncrypt := 7 - fheUintType := FheUint8 + fheUintType := tfhe.FheUint8 encCiphertext := verifyCiphertextInTestMemory(environment, uint64(toEncrypt), depth, fheUintType).GetHash() addr := common.Address{} readOnly := false input := make([]byte, 0) input = append(input, signatureBytes...) input = append(input, encCiphertext.Bytes()...) - input = append(input, byte(FheUint32)) + input = append(input, byte(tfhe.FheUint32)) _, err := FheLibRun(environment, addr, addr, input, readOnly) if err != nil { t.Fatalf("Cast error: %s", err.Error()) } } -func FheAdd(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheAdd(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 133333777777 rhs = 133337 } @@ -1721,22 +1737,22 @@ func FheAdd(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheSub(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheSub(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 133333777777 rhs = 133337 } @@ -1768,22 +1784,22 @@ func FheSub(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheMul(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheMul(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 3 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 3 - case FheUint16: + case tfhe.FheUint16: lhs = 169 rhs = 5 - case FheUint32: + case tfhe.FheUint32: lhs = 137 rhs = 17 - case FheUint64: + case tfhe.FheUint64: lhs = 137777 rhs = 17 } @@ -1815,22 +1831,22 @@ func FheMul(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheDiv(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheDiv(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 6 rhs = 7 - case FheUint8: + case tfhe.FheUint8: lhs = 6 rhs = 7 - case FheUint16: + case tfhe.FheUint16: lhs = 721 rhs = 251 - case FheUint32: + case tfhe.FheUint32: lhs = 137 rhs = 65521 - case FheUint64: + case tfhe.FheUint64: lhs = 137777777 rhs = 65521 } @@ -1868,22 +1884,22 @@ func FheDiv(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheRem(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheRem(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 9 rhs = 5 - case FheUint8: + case tfhe.FheUint8: lhs = 9 rhs = 5 - case FheUint16: + case tfhe.FheUint16: lhs = 1773 rhs = 523 - case FheUint32: + case tfhe.FheUint32: lhs = 123765 rhs = 2179 - case FheUint64: + case tfhe.FheUint64: lhs = 1237651337 rhs = 2179 } @@ -1921,25 +1937,25 @@ func FheRem(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheBitAnd(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheBitAnd(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheBool: + case tfhe.FheBool: lhs = 1 rhs = 0 - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777 rhs = 133337 } @@ -1977,22 +1993,22 @@ func FheBitAnd(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheBitOr(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheBitOr(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777 rhs = 133337 } @@ -2030,22 +2046,22 @@ func FheBitOr(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheBitXor(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheBitXor(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777 rhs = 133337 } @@ -2083,22 +2099,22 @@ func FheBitXor(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheShl(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheShl(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 2 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 3 - case FheUint64: + case tfhe.FheUint64: lhs = 1333337777 rhs = 10 } @@ -2130,22 +2146,22 @@ func FheShl(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheShr(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheShr(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 3 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 3 - case FheUint64: + case tfhe.FheUint64: lhs = 133333777777 rhs = 10 } @@ -2177,22 +2193,22 @@ func FheShr(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheEq(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheEq(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777 rhs = 133337 } @@ -2224,22 +2240,22 @@ func FheEq(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheNe(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheNe(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777 rhs = 133337 } @@ -2271,22 +2287,22 @@ func FheNe(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheGe(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheGe(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777 rhs = 133337 } @@ -2336,22 +2352,22 @@ func FheGe(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheGt(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheGt(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777 rhs = 133337 } @@ -2403,22 +2419,22 @@ func FheGt(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLe(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLe(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 1333337777 rhs = 133337 } @@ -2470,22 +2486,22 @@ func FheLe(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheLt(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheLt(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777 rhs = 133337 } @@ -2538,22 +2554,22 @@ func FheLt(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheMin(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheMin(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 133333777777 rhs = 133337 } @@ -2604,22 +2620,22 @@ func FheMin(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheMax(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheMax(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777 rhs = 133337 } @@ -2670,22 +2686,22 @@ func FheMax(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheNeg(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheNeg(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var pt, expected uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: pt = 2 expected = uint64(-uint8(pt)) - case FheUint8: + case tfhe.FheUint8: pt = 2 expected = uint64(-uint8(pt)) - case FheUint16: + case tfhe.FheUint16: pt = 4283 expected = uint64(-uint16(pt)) - case FheUint32: + case tfhe.FheUint32: pt = 1333337 expected = uint64(-uint32(pt)) - case FheUint64: + case tfhe.FheUint64: pt = 133333777777 expected = uint64(-uint64(pt)) } @@ -2713,22 +2729,22 @@ func FheNeg(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheNot(t *testing.T, fheUintType FheUintType, scalar bool) { +func FheNot(t *testing.T, fheUintType tfhe.FheUintType, scalar bool) { var pt, expected uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: pt = 2 expected = uint64(^uint8(pt)) - case FheUint8: + case tfhe.FheUint8: pt = 2 expected = uint64(^uint8(pt)) - case FheUint16: + case tfhe.FheUint16: pt = 4283 expected = uint64(^uint16(pt)) - case FheUint32: + case tfhe.FheUint32: pt = 1333337 expected = uint64(^uint32(pt)) - case FheUint64: + case tfhe.FheUint64: pt = 1333337777777 expected = uint64(^uint64(pt)) } @@ -2756,22 +2772,22 @@ func FheNot(t *testing.T, fheUintType FheUintType, scalar bool) { } } -func FheIfThenElse(t *testing.T, fheUintType FheUintType, condition uint64) { +func FheIfThenElse(t *testing.T, fheUintType tfhe.FheUintType, condition uint64) { var lhs, rhs uint64 switch fheUintType { - case FheUint4: + case tfhe.FheUint4: lhs = 2 rhs = 1 - case FheUint8: + case tfhe.FheUint8: lhs = 2 rhs = 1 - case FheUint16: + case tfhe.FheUint16: lhs = 4283 rhs = 1337 - case FheUint32: + case tfhe.FheUint32: lhs = 1333337 rhs = 133337 - case FheUint64: + case tfhe.FheUint64: lhs = 13333377777 rhs = 133337 } @@ -2780,7 +2796,7 @@ func FheIfThenElse(t *testing.T, fheUintType FheUintType, condition uint64) { environment.depth = depth addr := common.Address{} readOnly := false - conditionHash := verifyCiphertextInTestMemory(environment, condition, depth, FheBool).GetHash() + conditionHash := verifyCiphertextInTestMemory(environment, condition, depth, tfhe.FheBool).GetHash() lhsHash := verifyCiphertextInTestMemory(environment, lhs, depth, fheUintType).GetHash() rhsHash := verifyCiphertextInTestMemory(environment, rhs, depth, fheUintType).GetHash() @@ -2799,20 +2815,20 @@ func FheIfThenElse(t *testing.T, fheUintType FheUintType, condition uint64) { } } -func Decrypt(t *testing.T, fheUintType FheUintType) { +func Decrypt(t *testing.T, fheUintType tfhe.FheUintType) { var value uint64 switch fheUintType { - case FheBool: + case tfhe.FheBool: value = 1 - case FheUint4: + case tfhe.FheUint4: value = 2 - case FheUint8: + case tfhe.FheUint8: value = 2 - case FheUint16: + case tfhe.FheUint16: value = 4283 - case FheUint32: + case tfhe.FheUint32: value = 1333337 - case FheUint64: + case tfhe.FheUint64: value = 133333777777777 } depth := 1 @@ -2834,7 +2850,7 @@ func Decrypt(t *testing.T, fheUintType FheUintType) { } } -func FheRand(t *testing.T, fheUintType FheUintType) { +func FheRand(t *testing.T, fheUintType tfhe.FheUintType) { depth := 1 environment := newTestEVMEnvironment() environment.depth = depth @@ -2863,8 +2879,8 @@ func TestVerifyCiphertextInvalidType(t *testing.T) { environment.depth = depth addr := common.Address{} readOnly := false - invalidType := FheUintType(255) - compact := encryptAndSerializeCompact(0, FheUint64) + invalidType := tfhe.FheUintType(255) + compact := tfhe.EncryptAndSerializeCompact(0, tfhe.FheUint64) input := prepareInputForVerifyCiphertext(append(compact, byte(invalidType))) _, err := verifyCiphertextRun(environment, addr, addr, input, readOnly, nil) if err == nil { @@ -2884,7 +2900,7 @@ func TestTrivialEncryptInvalidType(t *testing.T) { environment.depth = depth addr := common.Address{} readOnly := false - invalidType := FheUintType(255) + invalidType := tfhe.FheUintType(255) input := make([]byte, 32) input = append(input, byte(invalidType)) trivialEncryptRun(environment, addr, addr, input, readOnly, nil) @@ -2896,8 +2912,8 @@ func TestCastInvalidType(t *testing.T) { environment.depth = depth addr := common.Address{} readOnly := false - invalidType := FheUintType(255) - hash := verifyCiphertextInTestMemory(environment, 1, depth, FheUint8).GetHash() + invalidType := tfhe.FheUintType(255) + hash := verifyCiphertextInTestMemory(environment, 1, depth, tfhe.FheUint8).GetHash() input := make([]byte, 0) input = append(input, hash.Bytes()...) input = append(input, byte(invalidType)) @@ -2913,8 +2929,8 @@ func TestVerifyCiphertextInvalidSize(t *testing.T) { environment.depth = depth addr := common.Address{} readOnly := false - ctType := FheUint32 - compact := encryptAndSerializeCompact(0, ctType) + ctType := tfhe.FheUint32 + compact := tfhe.EncryptAndSerializeCompact(0, ctType) input := prepareInputForVerifyCiphertext(append(compact[:len(compact)-1], byte(ctType))) _, err := verifyCiphertextRun(environment, addr, addr, input, readOnly, nil) if err == nil { @@ -2923,78 +2939,78 @@ func TestVerifyCiphertextInvalidSize(t *testing.T) { } func TestVerifyCiphertext4(t *testing.T) { - VerifyCiphertext(t, FheUint4) + VerifyCiphertext(t, tfhe.FheUint4) } func TestVerifyCiphertext8(t *testing.T) { - VerifyCiphertext(t, FheUint8) + VerifyCiphertext(t, tfhe.FheUint8) } func TestVerifyCiphertext16(t *testing.T) { - VerifyCiphertext(t, FheUint16) + VerifyCiphertext(t, tfhe.FheUint16) } func TestVerifyCiphertext32(t *testing.T) { - VerifyCiphertext(t, FheUint32) + VerifyCiphertext(t, tfhe.FheUint32) } func TestVerifyCiphertext64(t *testing.T) { - VerifyCiphertext(t, FheUint64) + VerifyCiphertext(t, tfhe.FheUint64) } func TestTrivialEncrypt4(t *testing.T) { - TrivialEncrypt(t, FheUint4) + TrivialEncrypt(t, tfhe.FheUint4) } func TestTrivialEncrypt8(t *testing.T) { - TrivialEncrypt(t, FheUint8) + TrivialEncrypt(t, tfhe.FheUint8) } func TestTrivialEncrypt16(t *testing.T) { - TrivialEncrypt(t, FheUint16) + TrivialEncrypt(t, tfhe.FheUint16) } func TestTrivialEncrypt32(t *testing.T) { - TrivialEncrypt(t, FheUint32) + TrivialEncrypt(t, tfhe.FheUint32) } func TestTrivialEncrypt64(t *testing.T) { - TrivialEncrypt(t, FheUint64) + TrivialEncrypt(t, tfhe.FheUint64) } func TestVerifyCiphertext4BadType(t *testing.T) { - VerifyCiphertextBadType(t, FheUint4, FheUint8) - VerifyCiphertextBadType(t, FheUint4, FheUint16) - VerifyCiphertextBadType(t, FheUint4, FheUint32) - VerifyCiphertextBadType(t, FheUint4, FheUint64) + VerifyCiphertextBadType(t, tfhe.FheUint4, tfhe.FheUint8) + VerifyCiphertextBadType(t, tfhe.FheUint4, tfhe.FheUint16) + VerifyCiphertextBadType(t, tfhe.FheUint4, tfhe.FheUint32) + VerifyCiphertextBadType(t, tfhe.FheUint4, tfhe.FheUint64) } func TestVerifyCiphertext8BadType(t *testing.T) { - VerifyCiphertextBadType(t, FheUint8, FheUint4) - VerifyCiphertextBadType(t, FheUint8, FheUint16) - VerifyCiphertextBadType(t, FheUint8, FheUint32) - VerifyCiphertextBadType(t, FheUint8, FheUint64) + VerifyCiphertextBadType(t, tfhe.FheUint8, tfhe.FheUint4) + VerifyCiphertextBadType(t, tfhe.FheUint8, tfhe.FheUint16) + VerifyCiphertextBadType(t, tfhe.FheUint8, tfhe.FheUint32) + VerifyCiphertextBadType(t, tfhe.FheUint8, tfhe.FheUint64) } func TestVerifyCiphertext16BadType(t *testing.T) { - VerifyCiphertextBadType(t, FheUint16, FheUint4) - VerifyCiphertextBadType(t, FheUint16, FheUint8) - VerifyCiphertextBadType(t, FheUint16, FheUint32) - VerifyCiphertextBadType(t, FheUint16, FheUint64) + VerifyCiphertextBadType(t, tfhe.FheUint16, tfhe.FheUint4) + VerifyCiphertextBadType(t, tfhe.FheUint16, tfhe.FheUint8) + VerifyCiphertextBadType(t, tfhe.FheUint16, tfhe.FheUint32) + VerifyCiphertextBadType(t, tfhe.FheUint16, tfhe.FheUint64) } func TestVerifyCiphertext32BadType(t *testing.T) { - VerifyCiphertextBadType(t, FheUint32, FheUint4) - VerifyCiphertextBadType(t, FheUint32, FheUint8) - VerifyCiphertextBadType(t, FheUint32, FheUint16) - VerifyCiphertextBadType(t, FheUint32, FheUint64) + VerifyCiphertextBadType(t, tfhe.FheUint32, tfhe.FheUint4) + VerifyCiphertextBadType(t, tfhe.FheUint32, tfhe.FheUint8) + VerifyCiphertextBadType(t, tfhe.FheUint32, tfhe.FheUint16) + VerifyCiphertextBadType(t, tfhe.FheUint32, tfhe.FheUint64) } func TestVerifyCiphertext64BadType(t *testing.T) { - VerifyCiphertextBadType(t, FheUint64, FheUint4) - VerifyCiphertextBadType(t, FheUint64, FheUint8) - VerifyCiphertextBadType(t, FheUint64, FheUint16) - VerifyCiphertextBadType(t, FheUint64, FheUint32) + VerifyCiphertextBadType(t, tfhe.FheUint64, tfhe.FheUint4) + VerifyCiphertextBadType(t, tfhe.FheUint64, tfhe.FheUint8) + VerifyCiphertextBadType(t, tfhe.FheUint64, tfhe.FheUint16) + VerifyCiphertextBadType(t, tfhe.FheUint64, tfhe.FheUint32) } func TestVerifyCiphertextBadCiphertext(t *testing.T) { @@ -3014,940 +3030,940 @@ func TestVerifyCiphertextBadCiphertext(t *testing.T) { } func TestFheLibBitAndBool(t *testing.T) { - FheLibBitAnd(t, FheBool, false) + FheLibBitAnd(t, tfhe.FheBool, false) } func TestFheLibBitOrBool(t *testing.T) { - FheLibBitOr(t, FheBool, false) + FheLibBitOr(t, tfhe.FheBool, false) } func TestFheLibBitXorBool(t *testing.T) { - FheLibBitXor(t, FheBool, false) + FheLibBitXor(t, tfhe.FheBool, false) } func TestFheLibAdd4(t *testing.T) { - FheLibAdd(t, FheUint4, false) + FheLibAdd(t, tfhe.FheUint4, false) } func TestFheLibSub4(t *testing.T) { - FheLibSub(t, FheUint4, false) + FheLibSub(t, tfhe.FheUint4, false) } func TestFheLibMul4(t *testing.T) { - FheLibMul(t, FheUint4, false) + FheLibMul(t, tfhe.FheUint4, false) } func TestFheLibLe4(t *testing.T) { - FheLibLe(t, FheUint4, false) + FheLibLe(t, tfhe.FheUint4, false) } func TestFheLibLt4(t *testing.T) { - FheLibLt(t, FheUint4, false) + FheLibLt(t, tfhe.FheUint4, false) } func TestFheLibEq4(t *testing.T) { - FheLibEq(t, FheUint4, false) + FheLibEq(t, tfhe.FheUint4, false) } func TestFheLibGe4(t *testing.T) { - FheLibGe(t, FheUint4, false) + FheLibGe(t, tfhe.FheUint4, false) } func TestFheLibGt4(t *testing.T) { - FheLibGt(t, FheUint4, false) + FheLibGt(t, tfhe.FheUint4, false) } func TestFheLibShl4(t *testing.T) { - FheLibShl(t, FheUint4, false) + FheLibShl(t, tfhe.FheUint4, false) } func TestFheLibShr4(t *testing.T) { - FheLibShr(t, FheUint4, false) + FheLibShr(t, tfhe.FheUint4, false) } func TestFheLibNe4(t *testing.T) { - FheLibNe(t, FheUint4, false) + FheLibNe(t, tfhe.FheUint4, false) } func TestFheLibMin4(t *testing.T) { - FheLibMin(t, FheUint4, false) + FheLibMin(t, tfhe.FheUint4, false) } func TestFheLibMax4(t *testing.T) { - FheLibMax(t, FheUint4, false) + FheLibMax(t, tfhe.FheUint4, false) } func TestFheLibNeg4(t *testing.T) { - FheLibNeg(t, FheUint4) + FheLibNeg(t, tfhe.FheUint4) } func TestFheLibNotBool(t *testing.T) { - FheLibNot(t, FheBool) + FheLibNot(t, tfhe.FheBool) } func TestFheLibNot4(t *testing.T) { - FheLibNot(t, FheUint4) + FheLibNot(t, tfhe.FheUint4) } func TestFheLibDiv4(t *testing.T) { - FheLibDiv(t, FheUint4, true) + FheLibDiv(t, tfhe.FheUint4, true) } func TestFheLibRem4(t *testing.T) { - FheLibRem(t, FheUint4, true) + FheLibRem(t, tfhe.FheUint4, true) } func TestFheLibBitAnd4(t *testing.T) { - FheLibBitAnd(t, FheUint4, false) + FheLibBitAnd(t, tfhe.FheUint4, false) } func TestFheLibBitOr4(t *testing.T) { - FheLibBitOr(t, FheUint4, false) + FheLibBitOr(t, tfhe.FheUint4, false) } func TestFheLibBitXor4(t *testing.T) { - FheLibBitXor(t, FheUint4, false) + FheLibBitXor(t, tfhe.FheUint4, false) } func TestFheLibRand4(t *testing.T) { - FheLibRand(t, FheUint4) + FheLibRand(t, tfhe.FheUint4) } func TestFheLibAdd8(t *testing.T) { - FheLibAdd(t, FheUint8, false) + FheLibAdd(t, tfhe.FheUint8, false) } func TestFheLibSub8(t *testing.T) { - FheLibSub(t, FheUint8, false) + FheLibSub(t, tfhe.FheUint8, false) } func TestFheLibMul8(t *testing.T) { - FheLibMul(t, FheUint8, false) + FheLibMul(t, tfhe.FheUint8, false) } func TestFheLibLe8(t *testing.T) { - FheLibLe(t, FheUint8, false) + FheLibLe(t, tfhe.FheUint8, false) } func TestFheLibLt8(t *testing.T) { - FheLibLt(t, FheUint8, false) + FheLibLt(t, tfhe.FheUint8, false) } func TestFheLibEq8(t *testing.T) { - FheLibEq(t, FheUint8, false) + FheLibEq(t, tfhe.FheUint8, false) } func TestFheLibGe8(t *testing.T) { - FheLibGe(t, FheUint8, false) + FheLibGe(t, tfhe.FheUint8, false) } func TestFheLibGt8(t *testing.T) { - FheLibGt(t, FheUint8, false) + FheLibGt(t, tfhe.FheUint8, false) } func TestFheLibShl8(t *testing.T) { - FheLibShl(t, FheUint8, false) + FheLibShl(t, tfhe.FheUint8, false) } func TestFheLibShr8(t *testing.T) { - FheLibShr(t, FheUint8, false) + FheLibShr(t, tfhe.FheUint8, false) } func TestFheLibNe8(t *testing.T) { - FheLibNe(t, FheUint8, false) + FheLibNe(t, tfhe.FheUint8, false) } func TestFheLibMin8(t *testing.T) { - FheLibMin(t, FheUint8, false) + FheLibMin(t, tfhe.FheUint8, false) } func TestFheLibMax8(t *testing.T) { - FheLibMax(t, FheUint8, false) + FheLibMax(t, tfhe.FheUint8, false) } func TestFheLibNeg8(t *testing.T) { - FheLibNeg(t, FheUint8) + FheLibNeg(t, tfhe.FheUint8) } func TestFheLibNot8(t *testing.T) { - FheLibNot(t, FheUint8) + FheLibNot(t, tfhe.FheUint8) } func TestFheLibDiv8(t *testing.T) { - FheLibDiv(t, FheUint8, true) + FheLibDiv(t, tfhe.FheUint8, true) } func TestFheLibRem8(t *testing.T) { - FheLibRem(t, FheUint8, true) + FheLibRem(t, tfhe.FheUint8, true) } func TestFheLibBitAnd8(t *testing.T) { - FheLibBitAnd(t, FheUint8, false) + FheLibBitAnd(t, tfhe.FheUint8, false) } func TestFheLibBitOr8(t *testing.T) { - FheLibBitOr(t, FheUint8, false) + FheLibBitOr(t, tfhe.FheUint8, false) } func TestFheLibBitXor8(t *testing.T) { - FheLibBitXor(t, FheUint8, false) + FheLibBitXor(t, tfhe.FheUint8, false) } func TestFheLibRand8(t *testing.T) { - FheLibRand(t, FheUint8) + FheLibRand(t, tfhe.FheUint8) } func TestFheLibRand16(t *testing.T) { - FheLibRand(t, FheUint16) + FheLibRand(t, tfhe.FheUint16) } func TestFheLibRand32(t *testing.T) { - FheLibRand(t, FheUint32) + FheLibRand(t, tfhe.FheUint32) } func TestFheLibRand64(t *testing.T) { - FheLibRand(t, FheUint64) + FheLibRand(t, tfhe.FheUint64) } func TestFheLibRandBounded8(t *testing.T) { - FheLibRandBounded(t, FheUint8, 8) + FheLibRandBounded(t, tfhe.FheUint8, 8) } func TestFheLibRandBounded16(t *testing.T) { - FheLibRandBounded(t, FheUint16, 16) + FheLibRandBounded(t, tfhe.FheUint16, 16) } func TestFheLibRandBounded32(t *testing.T) { - FheLibRandBounded(t, FheUint32, 32) + FheLibRandBounded(t, tfhe.FheUint32, 32) } func TestFheLibRandBounded64(t *testing.T) { - FheLibRandBounded(t, FheUint64, 64) + FheLibRandBounded(t, tfhe.FheUint64, 64) } func TestFheLibIfThenElse8(t *testing.T) { - FheLibIfThenElse(t, FheUint8, 1) - FheLibIfThenElse(t, FheUint8, 0) + FheLibIfThenElse(t, tfhe.FheUint8, 1) + FheLibIfThenElse(t, tfhe.FheUint8, 0) } func TestFheLibIfThenElse16(t *testing.T) { - FheLibIfThenElse(t, FheUint16, 1) - FheLibIfThenElse(t, FheUint16, 0) + FheLibIfThenElse(t, tfhe.FheUint16, 1) + FheLibIfThenElse(t, tfhe.FheUint16, 0) } func TestFheLibIfThenElse32(t *testing.T) { - FheLibIfThenElse(t, FheUint32, 1) - FheLibIfThenElse(t, FheUint32, 0) + FheLibIfThenElse(t, tfhe.FheUint32, 1) + FheLibIfThenElse(t, tfhe.FheUint32, 0) } func TestFheLibIfThenElse64(t *testing.T) { - FheLibIfThenElse(t, FheUint64, 1) - FheLibIfThenElse(t, FheUint64, 0) + FheLibIfThenElse(t, tfhe.FheUint64, 1) + FheLibIfThenElse(t, tfhe.FheUint64, 0) } func TestFheLibTrivialEncrypt8(t *testing.T) { - LibTrivialEncrypt(t, FheUint8) + LibTrivialEncrypt(t, tfhe.FheUint8) } // TODO: can be enabled if mocking kms or running a kms during tests // func TestLibDecrypt8(t *testing.T) { -// LibDecrypt(t, FheUint8) +// LibDecrypt(t, tfhe.FheUint8) // } func TestFheAdd8(t *testing.T) { - FheAdd(t, FheUint8, false) + FheAdd(t, tfhe.FheUint8, false) } func TestFheAdd16(t *testing.T) { - FheAdd(t, FheUint16, false) + FheAdd(t, tfhe.FheUint16, false) } func TestFheAdd32(t *testing.T) { - FheAdd(t, FheUint32, false) + FheAdd(t, tfhe.FheUint32, false) } func TestFheAdd64(t *testing.T) { - FheAdd(t, FheUint64, false) + FheAdd(t, tfhe.FheUint64, false) } func TestFheScalarAdd8(t *testing.T) { - FheAdd(t, FheUint8, true) + FheAdd(t, tfhe.FheUint8, true) } func TestFheScalarAdd16(t *testing.T) { - FheAdd(t, FheUint16, true) + FheAdd(t, tfhe.FheUint16, true) } func TestFheScalarAdd32(t *testing.T) { - FheAdd(t, FheUint32, true) + FheAdd(t, tfhe.FheUint32, true) } func TestFheScalarAdd64(t *testing.T) { - FheAdd(t, FheUint64, true) + FheAdd(t, tfhe.FheUint64, true) } func TestFheSub8(t *testing.T) { - FheSub(t, FheUint8, false) + FheSub(t, tfhe.FheUint8, false) } func TestFheSub16(t *testing.T) { - FheSub(t, FheUint16, false) + FheSub(t, tfhe.FheUint16, false) } func TestFheSub32(t *testing.T) { - FheSub(t, FheUint32, false) + FheSub(t, tfhe.FheUint32, false) } func TestFheSub64(t *testing.T) { - FheSub(t, FheUint64, false) + FheSub(t, tfhe.FheUint64, false) } func TestFheScalarSub8(t *testing.T) { - FheSub(t, FheUint8, true) + FheSub(t, tfhe.FheUint8, true) } func TestFheScalarSub16(t *testing.T) { - FheSub(t, FheUint16, true) + FheSub(t, tfhe.FheUint16, true) } func TestFheScalarSub32(t *testing.T) { - FheSub(t, FheUint32, true) + FheSub(t, tfhe.FheUint32, true) } func TestFheScalarSub64(t *testing.T) { - FheSub(t, FheUint64, true) + FheSub(t, tfhe.FheUint64, true) } func TestFheMul8(t *testing.T) { - FheMul(t, FheUint8, false) + FheMul(t, tfhe.FheUint8, false) } func TestFheMul16(t *testing.T) { - FheMul(t, FheUint16, false) + FheMul(t, tfhe.FheUint16, false) } func TestFheMul32(t *testing.T) { - FheMul(t, FheUint32, false) + FheMul(t, tfhe.FheUint32, false) } func TestFheMul64(t *testing.T) { - FheMul(t, FheUint64, false) + FheMul(t, tfhe.FheUint64, false) } func TestFheScalarMul8(t *testing.T) { - FheMul(t, FheUint8, true) + FheMul(t, tfhe.FheUint8, true) } func TestFheScalarMul16(t *testing.T) { - FheMul(t, FheUint16, true) + FheMul(t, tfhe.FheUint16, true) } func TestFheScalarMul32(t *testing.T) { - FheMul(t, FheUint32, true) + FheMul(t, tfhe.FheUint32, true) } func TestFheScalarMul64(t *testing.T) { - FheMul(t, FheUint64, true) + FheMul(t, tfhe.FheUint64, true) } func TestFheDiv8(t *testing.T) { - FheDiv(t, FheUint8, false) + FheDiv(t, tfhe.FheUint8, false) } func TestFheDiv16(t *testing.T) { - FheDiv(t, FheUint16, false) + FheDiv(t, tfhe.FheUint16, false) } func TestFheDiv32(t *testing.T) { - FheDiv(t, FheUint32, false) + FheDiv(t, tfhe.FheUint32, false) } func TestFheDiv64(t *testing.T) { - FheDiv(t, FheUint64, false) + FheDiv(t, tfhe.FheUint64, false) } func TestFheScalarDiv8(t *testing.T) { - FheDiv(t, FheUint8, true) + FheDiv(t, tfhe.FheUint8, true) } func TestFheScalarDiv16(t *testing.T) { - FheDiv(t, FheUint16, true) + FheDiv(t, tfhe.FheUint16, true) } func TestFheScalarDiv32(t *testing.T) { - FheDiv(t, FheUint32, true) + FheDiv(t, tfhe.FheUint32, true) } func TestFheScalarDiv64(t *testing.T) { - FheDiv(t, FheUint64, true) + FheDiv(t, tfhe.FheUint64, true) } func TestFheRem8(t *testing.T) { - FheRem(t, FheUint8, false) + FheRem(t, tfhe.FheUint8, false) } func TestFheRem16(t *testing.T) { - FheRem(t, FheUint16, false) + FheRem(t, tfhe.FheUint16, false) } func TestFheRem32(t *testing.T) { - FheRem(t, FheUint32, false) + FheRem(t, tfhe.FheUint32, false) } func TestFheRem64(t *testing.T) { - FheRem(t, FheUint64, false) + FheRem(t, tfhe.FheUint64, false) } func TestFheScalarRem8(t *testing.T) { - FheRem(t, FheUint8, true) + FheRem(t, tfhe.FheUint8, true) } func TestFheScalarRem16(t *testing.T) { - FheRem(t, FheUint16, true) + FheRem(t, tfhe.FheUint16, true) } func TestFheScalarRem32(t *testing.T) { - FheRem(t, FheUint32, true) + FheRem(t, tfhe.FheUint32, true) } func TestFheScalarRem64(t *testing.T) { - FheRem(t, FheUint64, true) + FheRem(t, tfhe.FheUint64, true) } func TestFheBitAndBool(t *testing.T) { - FheBitAnd(t, FheBool, false) + FheBitAnd(t, tfhe.FheBool, false) } func TestFheBitAnd8(t *testing.T) { - FheBitAnd(t, FheUint8, false) + FheBitAnd(t, tfhe.FheUint8, false) } func TestFheBitAnd16(t *testing.T) { - FheBitAnd(t, FheUint16, false) + FheBitAnd(t, tfhe.FheUint16, false) } func TestFheBitAnd32(t *testing.T) { - FheBitAnd(t, FheUint32, false) + FheBitAnd(t, tfhe.FheUint32, false) } func TestFheBitAnd64(t *testing.T) { - FheBitAnd(t, FheUint64, false) + FheBitAnd(t, tfhe.FheUint64, false) } func TestFheScalarBitAnd8(t *testing.T) { - FheBitAnd(t, FheUint8, true) + FheBitAnd(t, tfhe.FheUint8, true) } func TestFheScalarBitAnd16(t *testing.T) { - FheBitAnd(t, FheUint16, true) + FheBitAnd(t, tfhe.FheUint16, true) } func TestFheScalarBitAnd32(t *testing.T) { - FheBitAnd(t, FheUint32, true) + FheBitAnd(t, tfhe.FheUint32, true) } func TestFheScalarBitAnd64(t *testing.T) { - FheBitAnd(t, FheUint64, true) + FheBitAnd(t, tfhe.FheUint64, true) } func TestFheBitOr8(t *testing.T) { - FheBitOr(t, FheUint8, false) + FheBitOr(t, tfhe.FheUint8, false) } func TestFheBitOr16(t *testing.T) { - FheBitOr(t, FheUint16, false) + FheBitOr(t, tfhe.FheUint16, false) } func TestFheBitOr32(t *testing.T) { - FheBitOr(t, FheUint32, false) + FheBitOr(t, tfhe.FheUint32, false) } func TestFheBitOr64(t *testing.T) { - FheBitOr(t, FheUint64, false) + FheBitOr(t, tfhe.FheUint64, false) } func TestFheScalarBitOr8(t *testing.T) { - FheBitOr(t, FheUint8, true) + FheBitOr(t, tfhe.FheUint8, true) } func TestFheScalarBitOr16(t *testing.T) { - FheBitOr(t, FheUint16, true) + FheBitOr(t, tfhe.FheUint16, true) } func TestFheScalarBitOr32(t *testing.T) { - FheBitOr(t, FheUint32, true) + FheBitOr(t, tfhe.FheUint32, true) } func TestFheScalarBitOr64(t *testing.T) { - FheBitOr(t, FheUint64, true) + FheBitOr(t, tfhe.FheUint64, true) } func TestFheBitXor8(t *testing.T) { - FheBitXor(t, FheUint8, false) + FheBitXor(t, tfhe.FheUint8, false) } func TestFheBitXor16(t *testing.T) { - FheBitXor(t, FheUint16, false) + FheBitXor(t, tfhe.FheUint16, false) } func TestFheBitXor32(t *testing.T) { - FheBitXor(t, FheUint32, false) + FheBitXor(t, tfhe.FheUint32, false) } func TestFheBitXor64(t *testing.T) { - FheBitXor(t, FheUint64, false) + FheBitXor(t, tfhe.FheUint64, false) } func TestFheScalarBitXor8(t *testing.T) { - FheBitXor(t, FheUint8, true) + FheBitXor(t, tfhe.FheUint8, true) } func TestFheScalarBitXor16(t *testing.T) { - FheBitXor(t, FheUint16, true) + FheBitXor(t, tfhe.FheUint16, true) } func TestFheScalarBitXor32(t *testing.T) { - FheBitXor(t, FheUint32, true) + FheBitXor(t, tfhe.FheUint32, true) } func TestFheScalarBitXor64(t *testing.T) { - FheBitXor(t, FheUint64, true) + FheBitXor(t, tfhe.FheUint64, true) } func TestFheShl4(t *testing.T) { - FheShl(t, FheUint4, false) + FheShl(t, tfhe.FheUint4, false) } func TestFheShl8(t *testing.T) { - FheShl(t, FheUint8, false) + FheShl(t, tfhe.FheUint8, false) } func TestFheShl16(t *testing.T) { - FheShl(t, FheUint16, false) + FheShl(t, tfhe.FheUint16, false) } func TestFheShl32(t *testing.T) { - FheShl(t, FheUint32, false) + FheShl(t, tfhe.FheUint32, false) } func TestFheShl64(t *testing.T) { - FheShl(t, FheUint64, false) + FheShl(t, tfhe.FheUint64, false) } func TestFheScalarShl8(t *testing.T) { - FheShl(t, FheUint8, true) + FheShl(t, tfhe.FheUint8, true) } func TestFheScalarShl16(t *testing.T) { - FheShl(t, FheUint16, true) + FheShl(t, tfhe.FheUint16, true) } func TestFheScalarShl32(t *testing.T) { - FheShl(t, FheUint32, true) + FheShl(t, tfhe.FheUint32, true) } func TestFheScalarShl64(t *testing.T) { - FheShl(t, FheUint64, true) + FheShl(t, tfhe.FheUint64, true) } func TestFheShr8(t *testing.T) { - FheShr(t, FheUint8, false) + FheShr(t, tfhe.FheUint8, false) } func TestFheShr16(t *testing.T) { - FheShr(t, FheUint16, false) + FheShr(t, tfhe.FheUint16, false) } func TestFheShr32(t *testing.T) { - FheShr(t, FheUint32, false) + FheShr(t, tfhe.FheUint32, false) } func TestFheShr64(t *testing.T) { - FheShr(t, FheUint64, false) + FheShr(t, tfhe.FheUint64, false) } func TestFheScalarShr8(t *testing.T) { - FheShr(t, FheUint8, true) + FheShr(t, tfhe.FheUint8, true) } func TestFheScalarShr16(t *testing.T) { - FheShr(t, FheUint16, true) + FheShr(t, tfhe.FheUint16, true) } func TestFheScalarShr32(t *testing.T) { - FheShr(t, FheUint32, true) + FheShr(t, tfhe.FheUint32, true) } func TestFheScalarShr64(t *testing.T) { - FheShr(t, FheUint64, true) + FheShr(t, tfhe.FheUint64, true) } func TestFheEq8(t *testing.T) { - FheEq(t, FheUint8, false) + FheEq(t, tfhe.FheUint8, false) } func TestFheEq16(t *testing.T) { - FheEq(t, FheUint16, false) + FheEq(t, tfhe.FheUint16, false) } func TestFheEq32(t *testing.T) { - FheEq(t, FheUint32, false) + FheEq(t, tfhe.FheUint32, false) } func TestFheEq64(t *testing.T) { - FheEq(t, FheUint64, false) + FheEq(t, tfhe.FheUint64, false) } func TestFheScalarEq8(t *testing.T) { - FheEq(t, FheUint8, true) + FheEq(t, tfhe.FheUint8, true) } func TestFheScalarEq16(t *testing.T) { - FheEq(t, FheUint16, true) + FheEq(t, tfhe.FheUint16, true) } func TestFheScalarEq32(t *testing.T) { - FheEq(t, FheUint32, true) + FheEq(t, tfhe.FheUint32, true) } func TestFheScalarEq64(t *testing.T) { - FheEq(t, FheUint64, true) + FheEq(t, tfhe.FheUint64, true) } func TestFheNe8(t *testing.T) { - FheNe(t, FheUint8, false) + FheNe(t, tfhe.FheUint8, false) } func TestFheNe16(t *testing.T) { - FheNe(t, FheUint16, false) + FheNe(t, tfhe.FheUint16, false) } func TestFheNe32(t *testing.T) { - FheNe(t, FheUint32, false) + FheNe(t, tfhe.FheUint32, false) } func TestFheNe64(t *testing.T) { - FheNe(t, FheUint64, false) + FheNe(t, tfhe.FheUint64, false) } func TestFheScalarNe8(t *testing.T) { - FheNe(t, FheUint8, true) + FheNe(t, tfhe.FheUint8, true) } func TestFheScalarNe16(t *testing.T) { - FheNe(t, FheUint16, true) + FheNe(t, tfhe.FheUint16, true) } func TestFheScalarNe32(t *testing.T) { - FheNe(t, FheUint32, true) + FheNe(t, tfhe.FheUint32, true) } func TestFheScalarNe64(t *testing.T) { - FheNe(t, FheUint64, true) + FheNe(t, tfhe.FheUint64, true) } func TestFheGe8(t *testing.T) { - FheGe(t, FheUint8, false) + FheGe(t, tfhe.FheUint8, false) } func TestFheGe16(t *testing.T) { - FheGe(t, FheUint16, false) + FheGe(t, tfhe.FheUint16, false) } func TestFheGe32(t *testing.T) { - FheGe(t, FheUint32, false) + FheGe(t, tfhe.FheUint32, false) } func TestFheGe64(t *testing.T) { - FheGe(t, FheUint64, false) + FheGe(t, tfhe.FheUint64, false) } func TestFheScalarGe8(t *testing.T) { - FheGe(t, FheUint8, true) + FheGe(t, tfhe.FheUint8, true) } func TestFheScalarGe16(t *testing.T) { - FheGe(t, FheUint16, true) + FheGe(t, tfhe.FheUint16, true) } func TestFheScalarGe32(t *testing.T) { - FheGe(t, FheUint32, true) + FheGe(t, tfhe.FheUint32, true) } func TestFheScalarGe64(t *testing.T) { - FheGe(t, FheUint64, true) + FheGe(t, tfhe.FheUint64, true) } func TestFheGt8(t *testing.T) { - FheGt(t, FheUint8, false) + FheGt(t, tfhe.FheUint8, false) } func TestFheGt16(t *testing.T) { - FheGt(t, FheUint16, false) + FheGt(t, tfhe.FheUint16, false) } func TestFheGt32(t *testing.T) { - FheGt(t, FheUint32, false) + FheGt(t, tfhe.FheUint32, false) } func TestFheGt64(t *testing.T) { - FheGt(t, FheUint64, false) + FheGt(t, tfhe.FheUint64, false) } func TestFheScalarGt8(t *testing.T) { - FheGt(t, FheUint8, true) + FheGt(t, tfhe.FheUint8, true) } func TestFheScalarGt16(t *testing.T) { - FheGt(t, FheUint16, true) + FheGt(t, tfhe.FheUint16, true) } func TestFheScalarGt32(t *testing.T) { - FheGt(t, FheUint32, true) + FheGt(t, tfhe.FheUint32, true) } func TestFheScalarGt64(t *testing.T) { - FheGt(t, FheUint64, true) + FheGt(t, tfhe.FheUint64, true) } func TestFheLe4(t *testing.T) { - FheLe(t, FheUint4, false) + FheLe(t, tfhe.FheUint4, false) } func TestFheLe8(t *testing.T) { - FheLe(t, FheUint8, false) + FheLe(t, tfhe.FheUint8, false) } func TestFheLe16(t *testing.T) { - FheLe(t, FheUint16, false) + FheLe(t, tfhe.FheUint16, false) } func TestFheLe32(t *testing.T) { - FheLe(t, FheUint32, false) + FheLe(t, tfhe.FheUint32, false) } func TestFheLe64(t *testing.T) { - FheLe(t, FheUint64, false) + FheLe(t, tfhe.FheUint64, false) } func TestFheScalarLe4(t *testing.T) { - FheLe(t, FheUint4, true) + FheLe(t, tfhe.FheUint4, true) } func TestFheScalarLe8(t *testing.T) { - FheLe(t, FheUint8, true) + FheLe(t, tfhe.FheUint8, true) } func TestFheScalarLe16(t *testing.T) { - FheLe(t, FheUint16, true) + FheLe(t, tfhe.FheUint16, true) } func TestFheScalarLe32(t *testing.T) { - FheLe(t, FheUint32, true) + FheLe(t, tfhe.FheUint32, true) } func TestFheScalarLe64(t *testing.T) { - FheLe(t, FheUint64, true) + FheLe(t, tfhe.FheUint64, true) } func TestFheLt8(t *testing.T) { - FheLt(t, FheUint8, false) + FheLt(t, tfhe.FheUint8, false) } func TestFheLt16(t *testing.T) { - FheLt(t, FheUint16, false) + FheLt(t, tfhe.FheUint16, false) } func TestFheLt32(t *testing.T) { - FheLt(t, FheUint32, false) + FheLt(t, tfhe.FheUint32, false) } func TestFheLt64(t *testing.T) { - FheLt(t, FheUint64, false) + FheLt(t, tfhe.FheUint64, false) } func TestFheScalarLt8(t *testing.T) { - FheLt(t, FheUint8, true) + FheLt(t, tfhe.FheUint8, true) } func TestFheScalarLt16(t *testing.T) { - FheLt(t, FheUint16, true) + FheLt(t, tfhe.FheUint16, true) } func TestFheScalarLt32(t *testing.T) { - FheLt(t, FheUint32, true) + FheLt(t, tfhe.FheUint32, true) } func TestFheScalarLt64(t *testing.T) { - FheLt(t, FheUint64, true) + FheLt(t, tfhe.FheUint64, true) } func TestFheMin8(t *testing.T) { - FheMin(t, FheUint8, false) + FheMin(t, tfhe.FheUint8, false) } func TestFheMin16(t *testing.T) { - FheMin(t, FheUint16, false) + FheMin(t, tfhe.FheUint16, false) } func TestFheMin32(t *testing.T) { - FheMin(t, FheUint32, false) + FheMin(t, tfhe.FheUint32, false) } func TestFheMin64(t *testing.T) { - FheMin(t, FheUint64, false) + FheMin(t, tfhe.FheUint64, false) } func TestFheScalarMin8(t *testing.T) { - FheMin(t, FheUint8, true) + FheMin(t, tfhe.FheUint8, true) } func TestFheScalarMin16(t *testing.T) { - FheMin(t, FheUint16, true) + FheMin(t, tfhe.FheUint16, true) } func TestFheScalarMin32(t *testing.T) { - FheMin(t, FheUint32, true) + FheMin(t, tfhe.FheUint32, true) } func TestFheScalarMin64(t *testing.T) { - FheMin(t, FheUint64, true) + FheMin(t, tfhe.FheUint64, true) } func TestFheMax4(t *testing.T) { - FheMax(t, FheUint4, false) + FheMax(t, tfhe.FheUint4, false) } func TestFheMax8(t *testing.T) { - FheMax(t, FheUint8, false) + FheMax(t, tfhe.FheUint8, false) } func TestFheMax16(t *testing.T) { - FheMax(t, FheUint16, false) + FheMax(t, tfhe.FheUint16, false) } func TestFheMax32(t *testing.T) { - FheMax(t, FheUint32, false) + FheMax(t, tfhe.FheUint32, false) } func TestFheMax64(t *testing.T) { - FheMax(t, FheUint64, false) + FheMax(t, tfhe.FheUint64, false) } func TestFheNeg8(t *testing.T) { - FheNeg(t, FheUint8, false) + FheNeg(t, tfhe.FheUint8, false) } func TestFheNeg16(t *testing.T) { - FheNeg(t, FheUint16, false) + FheNeg(t, tfhe.FheUint16, false) } func TestFheNeg32(t *testing.T) { - FheNeg(t, FheUint32, false) + FheNeg(t, tfhe.FheUint32, false) } func TestFheNeg64(t *testing.T) { - FheNeg(t, FheUint64, false) + FheNeg(t, tfhe.FheUint64, false) } func TestFheNot8(t *testing.T) { - FheNot(t, FheUint8, false) + FheNot(t, tfhe.FheUint8, false) } func TestFheNot16(t *testing.T) { - FheNot(t, FheUint16, false) + FheNot(t, tfhe.FheUint16, false) } func TestFheNot32(t *testing.T) { - FheNot(t, FheUint32, false) + FheNot(t, tfhe.FheUint32, false) } func TestFheNot64(t *testing.T) { - FheNot(t, FheUint64, false) + FheNot(t, tfhe.FheUint64, false) } func TestFheIfThenElse4(t *testing.T) { - FheIfThenElse(t, FheUint4, 1) - FheIfThenElse(t, FheUint4, 0) + FheIfThenElse(t, tfhe.FheUint4, 1) + FheIfThenElse(t, tfhe.FheUint4, 0) } func TestFheIfThenElse8(t *testing.T) { - FheIfThenElse(t, FheUint8, 1) - FheIfThenElse(t, FheUint8, 0) + FheIfThenElse(t, tfhe.FheUint8, 1) + FheIfThenElse(t, tfhe.FheUint8, 0) } func TestFheIfThenElse16(t *testing.T) { - FheIfThenElse(t, FheUint16, 1) - FheIfThenElse(t, FheUint16, 0) + FheIfThenElse(t, tfhe.FheUint16, 1) + FheIfThenElse(t, tfhe.FheUint16, 0) } func TestFheIfThenElse32(t *testing.T) { - FheIfThenElse(t, FheUint32, 1) - FheIfThenElse(t, FheUint32, 0) + FheIfThenElse(t, tfhe.FheUint32, 1) + FheIfThenElse(t, tfhe.FheUint32, 0) } func TestFheIfThenElse64(t *testing.T) { - FheIfThenElse(t, FheUint64, 1) - FheIfThenElse(t, FheUint64, 0) + FheIfThenElse(t, tfhe.FheUint64, 1) + FheIfThenElse(t, tfhe.FheUint64, 0) } func TestFheScalarMax4(t *testing.T) { - FheMax(t, FheUint4, true) + FheMax(t, tfhe.FheUint4, true) } func TestFheScalarMax8(t *testing.T) { - FheMax(t, FheUint8, true) + FheMax(t, tfhe.FheUint8, true) } func TestFheScalarMax16(t *testing.T) { - FheMax(t, FheUint16, true) + FheMax(t, tfhe.FheUint16, true) } func TestFheScalarMax32(t *testing.T) { - FheMax(t, FheUint32, true) + FheMax(t, tfhe.FheUint32, true) } func TestFheScalarMax64(t *testing.T) { - FheMax(t, FheUint64, true) + FheMax(t, tfhe.FheUint64, true) } func TestDecrypt8(t *testing.T) { - Decrypt(t, FheUint8) + Decrypt(t, tfhe.FheUint8) } func TestDecrypt16(t *testing.T) { - Decrypt(t, FheUint16) + Decrypt(t, tfhe.FheUint16) } func TestDecrypt32(t *testing.T) { - Decrypt(t, FheUint32) + Decrypt(t, tfhe.FheUint32) } func TestDecrypt64(t *testing.T) { - Decrypt(t, FheUint64) + Decrypt(t, tfhe.FheUint64) } func TestFheRand8(t *testing.T) { - FheRand(t, FheUint8) + FheRand(t, tfhe.FheUint8) } func TestFheRand16(t *testing.T) { - FheRand(t, FheUint16) + FheRand(t, tfhe.FheUint16) } func TestFheRand32(t *testing.T) { - FheRand(t, FheUint32) + FheRand(t, tfhe.FheUint32) } func TestFheRand64(t *testing.T) { - FheRand(t, FheUint64) + FheRand(t, tfhe.FheUint64) } func TestUnknownCiphertextHandle(t *testing.T) { depth := 1 environment := newTestEVMEnvironment() environment.depth = depth - hash := verifyCiphertextInTestMemory(environment, 2, depth, FheUint8).GetHash() + hash := verifyCiphertextInTestMemory(environment, 2, depth, tfhe.FheUint8).GetHash() ct := getVerifiedCiphertext(environment, hash) if ct == nil { @@ -3966,7 +3982,7 @@ func TestCiphertextNotVerifiedWithoutReturn(t *testing.T) { environment := newTestEVMEnvironment() environment.depth = 1 verifiedDepth := 2 - hash := verifyCiphertextInTestMemory(environment, 1, verifiedDepth, FheUint8).GetHash() + hash := verifyCiphertextInTestMemory(environment, 1, verifiedDepth, tfhe.FheUint8).GetHash() ct := getVerifiedCiphertext(environment, hash) if ct != nil { @@ -3978,7 +3994,7 @@ func TestCiphertextNotAutomaticallyDelegated(t *testing.T) { environment := newTestEVMEnvironment() environment.depth = 3 verifiedDepth := 2 - hash := verifyCiphertextInTestMemory(environment, 1, verifiedDepth, FheUint8).GetHash() + hash := verifyCiphertextInTestMemory(environment, 1, verifiedDepth, tfhe.FheUint8).GetHash() ct := getVerifiedCiphertext(environment, hash) if ct != nil { @@ -3989,7 +4005,7 @@ func TestCiphertextNotAutomaticallyDelegated(t *testing.T) { func TestCiphertextVerificationConditions(t *testing.T) { environment := newTestEVMEnvironment() verifiedDepth := 2 - hash := verifyCiphertextInTestMemory(environment, 1, verifiedDepth, FheUint8).GetHash() + hash := verifyCiphertextInTestMemory(environment, 1, verifiedDepth, tfhe.FheUint8).GetHash() environment.depth = verifiedDepth ctPtr := getVerifiedCiphertext(environment, hash) @@ -4059,7 +4075,7 @@ func TestFheRandBoundedInvalidType(t *testing.T) { } } -func FheRandBoundedInvalidBound(t *testing.T, fheUintType FheUintType, bound *uint256.Int) { +func FheRandBoundedInvalidBound(t *testing.T, fheUintType tfhe.FheUintType, bound *uint256.Int) { depth := 1 environment := newTestEVMEnvironment() environment.depth = depth @@ -4079,43 +4095,43 @@ func FheRandBoundedInvalidBound(t *testing.T, fheUintType FheUintType, bound *ui } func TestFheRandBoundedInvalidBound8(t *testing.T) { - FheRandBoundedInvalidBound(t, FheUint8, uint256.NewInt(0)) - FheRandBoundedInvalidBound(t, FheUint8, uint256.NewInt(3)) - FheRandBoundedInvalidBound(t, FheUint8, uint256.NewInt(98)) - FheRandBoundedInvalidBound(t, FheUint8, uint256.NewInt(0xFF)) + FheRandBoundedInvalidBound(t, tfhe.FheUint8, uint256.NewInt(0)) + FheRandBoundedInvalidBound(t, tfhe.FheUint8, uint256.NewInt(3)) + FheRandBoundedInvalidBound(t, tfhe.FheUint8, uint256.NewInt(98)) + FheRandBoundedInvalidBound(t, tfhe.FheUint8, uint256.NewInt(0xFF)) moreThan64Bits := uint256.NewInt(0xFFFFFFFFFFFFFFFF) moreThan64Bits.Add(moreThan64Bits, uint256.NewInt(1)) - FheRandBoundedInvalidBound(t, FheUint8, moreThan64Bits) + FheRandBoundedInvalidBound(t, tfhe.FheUint8, moreThan64Bits) } func TestFheRandBoundedInvalidBound16(t *testing.T) { - FheRandBoundedInvalidBound(t, FheUint16, uint256.NewInt(0)) - FheRandBoundedInvalidBound(t, FheUint16, uint256.NewInt(999)) - FheRandBoundedInvalidBound(t, FheUint16, uint256.NewInt(448)) - FheRandBoundedInvalidBound(t, FheUint16, uint256.NewInt(0xFFFF)) + FheRandBoundedInvalidBound(t, tfhe.FheUint16, uint256.NewInt(0)) + FheRandBoundedInvalidBound(t, tfhe.FheUint16, uint256.NewInt(999)) + FheRandBoundedInvalidBound(t, tfhe.FheUint16, uint256.NewInt(448)) + FheRandBoundedInvalidBound(t, tfhe.FheUint16, uint256.NewInt(0xFFFF)) moreThan64Bits := uint256.NewInt(0xFFFFFFFFFFFFFFFF) moreThan64Bits.Add(moreThan64Bits, uint256.NewInt(1)) - FheRandBoundedInvalidBound(t, FheUint16, moreThan64Bits) + FheRandBoundedInvalidBound(t, tfhe.FheUint16, moreThan64Bits) } func TestFheRandBoundedInvalidBound32(t *testing.T) { - FheRandBoundedInvalidBound(t, FheUint32, uint256.NewInt(0)) - FheRandBoundedInvalidBound(t, FheUint32, uint256.NewInt(111999)) - FheRandBoundedInvalidBound(t, FheUint32, uint256.NewInt(448884)) - FheRandBoundedInvalidBound(t, FheUint32, uint256.NewInt(0xFFFFFFFF)) + FheRandBoundedInvalidBound(t, tfhe.FheUint32, uint256.NewInt(0)) + FheRandBoundedInvalidBound(t, tfhe.FheUint32, uint256.NewInt(111999)) + FheRandBoundedInvalidBound(t, tfhe.FheUint32, uint256.NewInt(448884)) + FheRandBoundedInvalidBound(t, tfhe.FheUint32, uint256.NewInt(0xFFFFFFFF)) moreThan64Bits := uint256.NewInt(0xFFFFFFFFFFFFFFFF) moreThan64Bits.Add(moreThan64Bits, uint256.NewInt(1)) - FheRandBoundedInvalidBound(t, FheUint32, moreThan64Bits) + FheRandBoundedInvalidBound(t, tfhe.FheUint32, moreThan64Bits) } func TestFheRandBoundedInvalidBound64(t *testing.T) { - FheRandBoundedInvalidBound(t, FheUint64, uint256.NewInt(0)) - FheRandBoundedInvalidBound(t, FheUint64, uint256.NewInt(111999)) - FheRandBoundedInvalidBound(t, FheUint64, uint256.NewInt(448884)) - FheRandBoundedInvalidBound(t, FheUint64, uint256.NewInt(0xFFFFFFFFFFFFFFFF)) + FheRandBoundedInvalidBound(t, tfhe.FheUint64, uint256.NewInt(0)) + FheRandBoundedInvalidBound(t, tfhe.FheUint64, uint256.NewInt(111999)) + FheRandBoundedInvalidBound(t, tfhe.FheUint64, uint256.NewInt(448884)) + FheRandBoundedInvalidBound(t, tfhe.FheUint64, uint256.NewInt(0xFFFFFFFFFFFFFFFF)) moreThan64Bits := uint256.NewInt(0xFFFFFFFFFFFFFFFF) moreThan64Bits.Add(moreThan64Bits, uint256.NewInt(1)) - FheRandBoundedInvalidBound(t, FheUint32, moreThan64Bits) + FheRandBoundedInvalidBound(t, tfhe.FheUint32, moreThan64Bits) } func TestFheRandEthCall(t *testing.T) { @@ -4125,7 +4141,7 @@ func TestFheRandEthCall(t *testing.T) { environment.ethCall = true addr := common.Address{} readOnly := true - _, err := fheRandRun(environment, addr, addr, []byte{byte(FheUint8)}, readOnly, nil) + _, err := fheRandRun(environment, addr, addr, []byte{byte(tfhe.FheUint8)}, readOnly, nil) if err == nil { t.Fatalf("fheRand expected failure on EthCall") } @@ -4144,7 +4160,7 @@ func TestFheRandBoundedEthCall(t *testing.T) { input := make([]byte, 0) upperBound := uint256.NewInt(4).Bytes32() input = append(input, upperBound[:]...) - input = append(input, byte(FheUint8)) + input = append(input, byte(tfhe.FheUint8)) _, err := fheRandBoundedRun(environment, addr, addr, input, readOnly, nil) if err == nil { t.Fatalf("fheRandBounded expected failure on EthCall") @@ -4210,7 +4226,7 @@ func TestLibOneTrueOptimisticRequire(t *testing.T) { addr := common.Address{} readOnly := false input := make([]byte, 0) - hash := verifyCiphertextInTestMemory(environment, value, depth, FheUint8).GetHash() + hash := verifyCiphertextInTestMemory(environment, value, depth, tfhe.FheUint8).GetHash() input = append(input, signatureBytes...) input = append(input, hash.Bytes()...) out, err := FheLibRun(environment, addr, addr, input, readOnly) @@ -4237,7 +4253,7 @@ func TestOneFalseOptimisticRequire(t *testing.T) { environment.depth = depth addr := common.Address{} readOnly := false - hash := verifyCiphertextInTestMemory(environment, value, depth, FheUint8).GetHash() + hash := verifyCiphertextInTestMemory(environment, value, depth, tfhe.FheUint8).GetHash() out, err := optimisticRequireRun(environment, addr, addr, hash.Bytes(), readOnly, nil) if err != nil { t.Fatalf(err.Error()) @@ -4261,14 +4277,14 @@ func TestTwoTrueOptimisticRequires(t *testing.T) { environment.depth = depth addr := common.Address{} readOnly := false - hash := verifyCiphertextInTestMemory(environment, value, depth, FheUint8).GetHash() + hash := verifyCiphertextInTestMemory(environment, value, depth, tfhe.FheUint8).GetHash() out, err := optimisticRequireRun(environment, addr, addr, hash.Bytes(), readOnly, nil) if err != nil { t.Fatalf(err.Error()) } else if len(out) != 0 { t.Fatalf("require expected output len of 0, got %v", len(out)) } - hash = verifyCiphertextInTestMemory(environment, value, depth, FheUint8).GetHash() + hash = verifyCiphertextInTestMemory(environment, value, depth, tfhe.FheUint8).GetHash() out, err = optimisticRequireRun(environment, addr, addr, hash.Bytes(), readOnly, nil) if err != nil { t.Fatalf(err.Error()) @@ -4292,7 +4308,7 @@ func TestOptimisticRequireTwiceOnSameCiphertext(t *testing.T) { environment.depth = depth addr := common.Address{} readOnly := false - ct := verifyCiphertextInTestMemory(environment, value, depth, FheUint8) + ct := verifyCiphertextInTestMemory(environment, value, depth, tfhe.FheUint8) hash := ct.GetHash() out, err := optimisticRequireRun(environment, addr, addr, hash.Bytes(), readOnly, nil) if err != nil { @@ -4322,14 +4338,14 @@ func TestOneFalseAndOneTrueOptimisticRequire(t *testing.T) { environment.depth = depth addr := common.Address{} readOnly := false - hash := verifyCiphertextInTestMemory(environment, 0, depth, FheUint8).GetHash() + hash := verifyCiphertextInTestMemory(environment, 0, depth, tfhe.FheUint8).GetHash() out, err := optimisticRequireRun(environment, addr, addr, hash.Bytes(), readOnly, nil) if err != nil { t.Fatalf(err.Error()) } else if len(out) != 0 { t.Fatalf("require expected output len of 0, got %v", len(out)) } - hash = verifyCiphertextInTestMemory(environment, 1, depth, FheUint8).GetHash() + hash = verifyCiphertextInTestMemory(environment, 1, depth, tfhe.FheUint8).GetHash() out, err = optimisticRequireRun(environment, addr, addr, hash.Bytes(), readOnly, nil) if err != nil { t.Fatalf(err.Error()) @@ -4353,7 +4369,7 @@ func TestDecryptWithFalseOptimisticRequire(t *testing.T) { addr := common.Address{} readOnly := false // Call optimistic require with a false value and expect it succeeds. - hash := verifyCiphertextInTestMemory(environment, 0, depth, FheUint8).GetHash() + hash := verifyCiphertextInTestMemory(environment, 0, depth, tfhe.FheUint8).GetHash() out, err := optimisticRequireRun(environment, addr, addr, hash.Bytes(), readOnly, nil) if err != nil { t.Fatalf(err.Error()) @@ -4378,7 +4394,7 @@ func TestDecryptWithTrueOptimisticRequire(t *testing.T) { addr := common.Address{} readOnly := false // Call optimistic require with a false value and expect it succeeds. - hash := verifyCiphertextInTestMemory(environment, 1, depth, FheUint8).GetHash() + hash := verifyCiphertextInTestMemory(environment, 1, depth, tfhe.FheUint8).GetHash() out, err := optimisticRequireRun(environment, addr, addr, hash.Bytes(), readOnly, nil) if err != nil { t.Fatalf(err.Error()) @@ -4407,7 +4423,7 @@ func TestDecryptInTransactionDisabled(t *testing.T) { environment.fhevmParams.DisableDecryptionsInTransaction = true addr := common.Address{} readOnly := false - hash := verifyCiphertextInTestMemory(environment, 1, depth, FheUint8).GetHash() + hash := verifyCiphertextInTestMemory(environment, 1, depth, tfhe.FheUint8).GetHash() // Call decrypt and expect it to fail due to disabling of decryptions during commit _, err := decryptRunWithoutKms(environment, addr, addr, hash.Bytes(), readOnly) if err == nil { @@ -4440,7 +4456,7 @@ func TestFheLibGetCiphertextNonEthCall(t *testing.T) { depth := 1 environment.depth = depth plaintext := uint64(2) - ct := verifyCiphertextInTestMemory(environment, plaintext, depth, FheUint32) + ct := verifyCiphertextInTestMemory(environment, plaintext, depth, tfhe.FheUint32) ctHash := ct.GetHash() scope := newTestScopeConext() loc := uint256.NewInt(10) @@ -4477,7 +4493,7 @@ func TestFheLibGetCiphertextNonExistentHandle(t *testing.T) { depth := 1 environment.depth = depth plaintext := uint64(2) - ct := verifyCiphertextInTestMemory(environment, plaintext, depth, FheUint32) + ct := verifyCiphertextInTestMemory(environment, plaintext, depth, tfhe.FheUint32) ctHash := ct.GetHash() scope := newTestScopeConext() loc := uint256.NewInt(10) @@ -4520,7 +4536,7 @@ func TestFheLibGetCiphertextWrongContractAddress(t *testing.T) { depth := 1 environment.depth = depth plaintext := uint64(2) - ct := verifyCiphertextInTestMemory(environment, plaintext, depth, FheUint32) + ct := verifyCiphertextInTestMemory(environment, plaintext, depth, tfhe.FheUint32) ctHash := ct.GetHash() scope := newTestScopeConext() loc := uint256.NewInt(10) @@ -4557,7 +4573,7 @@ func TestFheLibGetCiphertextWrongContractAddress(t *testing.T) { } } -func FheLibGetCiphertext(t *testing.T, fheUintType FheUintType) { +func FheLibGetCiphertext(t *testing.T, fheUintType tfhe.FheUintType) { environment := newTestEVMEnvironment() pc := uint64(0) depth := 1 @@ -4592,12 +4608,12 @@ func FheLibGetCiphertext(t *testing.T, fheUintType FheUintType) { if err != nil { t.Fatalf(err.Error()) } - size, _ := GetExpandedFheCiphertextSize(fheUintType) + size, _ := tfhe.GetExpandedFheCiphertextSize(fheUintType) if size != uint(len(out)) { t.Fatalf("getCiphertext returned ciphertext size of %d, expected %d", len(out), size) } - outCt := new(TfheCiphertext) + outCt := new(tfhe.TfheCiphertext) err = outCt.Deserialize(out, fheUintType) if err != nil { t.Fatalf(err.Error()) @@ -4612,17 +4628,17 @@ func FheLibGetCiphertext(t *testing.T, fheUintType FheUintType) { } func TestFheLibGetCiphertext8(t *testing.T) { - FheLibGetCiphertext(t, FheUint8) + FheLibGetCiphertext(t, tfhe.FheUint8) } func TestFheLibGetCiphertext16(t *testing.T) { - FheLibGetCiphertext(t, FheUint16) + FheLibGetCiphertext(t, tfhe.FheUint16) } func TestFheLibGetCiphertext32(t *testing.T) { - FheLibGetCiphertext(t, FheUint32) + FheLibGetCiphertext(t, tfhe.FheUint32) } func TestFheLibGetCiphertext64(t *testing.T) { - FheLibGetCiphertext(t, FheUint64) + FheLibGetCiphertext(t, tfhe.FheUint64) } diff --git a/fhevm/evm.go b/fhevm/evm.go index 2125064..dc0f1a9 100644 --- a/fhevm/evm.go +++ b/fhevm/evm.go @@ -8,6 +8,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" fhevm_crypto "github.com/zama-ai/fhevm-go/crypto" + "github.com/zama-ai/fhevm-go/tfhe" ) var protectedStorageAddrCallerAddr common.Address @@ -56,7 +57,7 @@ func getVerifiedCiphertext(environment EVMEnvironment, ciphertextHash common.Has return getVerifiedCiphertextFromEVM(environment, ciphertextHash) } -func importCiphertextToEVMAtDepth(environment EVMEnvironment, ct *TfheCiphertext, depth int) *verifiedCiphertext { +func importCiphertextToEVMAtDepth(environment EVMEnvironment, ct *tfhe.TfheCiphertext, depth int) *verifiedCiphertext { existing, ok := environment.FhevmData().verifiedCiphertexts[ct.GetHash()] if ok { existing.verifiedDepths.add(depth) @@ -73,21 +74,21 @@ func importCiphertextToEVMAtDepth(environment EVMEnvironment, ct *TfheCiphertext } } -func importCiphertextToEVM(environment EVMEnvironment, ct *TfheCiphertext) *verifiedCiphertext { +func importCiphertextToEVM(environment EVMEnvironment, ct *tfhe.TfheCiphertext) *verifiedCiphertext { return importCiphertextToEVMAtDepth(environment, ct, environment.GetDepth()) } -func importCiphertext(environment EVMEnvironment, ct *TfheCiphertext) *verifiedCiphertext { +func importCiphertext(environment EVMEnvironment, ct *tfhe.TfheCiphertext) *verifiedCiphertext { return importCiphertextToEVM(environment, ct) } -func importRandomCiphertext(environment EVMEnvironment, t FheUintType) []byte { +func importRandomCiphertext(environment EVMEnvironment, t tfhe.FheUintType) []byte { nextCtHash := &environment.FhevmData().nextCiphertextHashOnGasEst ctHashBytes := crypto.Keccak256(nextCtHash.Bytes()) handle := common.BytesToHash(ctHashBytes) - ct := new(TfheCiphertext) - ct.fheUintType = t - ct.hash = &handle + ct := new(tfhe.TfheCiphertext) + ct.FheUintType = t + ct.Hash = &handle importCiphertext(environment, ct) temp := nextCtHash.Clone() nextCtHash.Add(temp, uint256.NewInt(1)) @@ -101,6 +102,7 @@ func InitFhevm(accessibleState EVMEnvironment) { func persistFhePubKeyHash(accessibleState EVMEnvironment) { existing := accessibleState.GetState(fhePubKeyHashPrecompile, fhePubKeyHashSlot) if newInt(existing[:]).IsZero() { + var pksHash = tfhe.GetPksHash() accessibleState.SetState(fhePubKeyHashPrecompile, fhePubKeyHashSlot, pksHash) } } diff --git a/fhevm/instructions.go b/fhevm/instructions.go index 1a90f0d..2ea94b1 100644 --- a/fhevm/instructions.go +++ b/fhevm/instructions.go @@ -9,6 +9,7 @@ import ( crypto "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" fhevm_crypto "github.com/zama-ai/fhevm-go/crypto" + "github.com/zama-ai/fhevm-go/tfhe" "go.opentelemetry.io/otel" ) @@ -51,7 +52,7 @@ func verifyIfCiphertextHandle(handle common.Hash, env EVMEnvironment, contractAd ciphertext := getCiphertextFromProtectedStoage(env, contractAddress, handle) if ciphertext != nil { - ct := new(TfheCiphertext) + ct := new(tfhe.TfheCiphertext) err := ct.Deserialize(ciphertext.bytes, ciphertext.metadata.fheUintType) if err != nil { msg := "opSload failed to deserialize a ciphertext" diff --git a/fhevm/instructions_test.go b/fhevm/instructions_test.go index 14d028e..3a35155 100644 --- a/fhevm/instructions_test.go +++ b/fhevm/instructions_test.go @@ -14,6 +14,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" fhevm_crypto "github.com/zama-ai/fhevm-go/crypto" + "github.com/zama-ai/fhevm-go/tfhe" ) func init() { @@ -39,10 +40,10 @@ func init() { ) } -func verifyCiphertextInTestMemory(environment EVMEnvironment, value uint64, depth int, t FheUintType) *TfheCiphertext { +func verifyCiphertextInTestMemory(environment EVMEnvironment, value uint64, depth int, t tfhe.FheUintType) *tfhe.TfheCiphertext { // Simulate as if the ciphertext is compact and comes externally. - ser := encryptAndSerializeCompact(uint64(value), t) - ct := new(TfheCiphertext) + ser := tfhe.EncryptAndSerializeCompact(uint64(value), t) + ct := new(tfhe.TfheCiphertext) err := ct.DeserializeCompact(ser, t) if err != nil { panic(err) @@ -50,7 +51,7 @@ func verifyCiphertextInTestMemory(environment EVMEnvironment, value uint64, dept return verifyTfheCiphertextInTestMemory(environment, ct, depth) } -func verifyTfheCiphertextInTestMemory(environment EVMEnvironment, ct *TfheCiphertext, depth int) *TfheCiphertext { +func verifyTfheCiphertextInTestMemory(environment EVMEnvironment, ct *tfhe.TfheCiphertext, depth int) *tfhe.TfheCiphertext { verifiedCiphertext := importCiphertextToEVMAtDepth(environment, ct, depth) return verifiedCiphertext.ciphertext } @@ -137,7 +138,7 @@ func (c testCallerAddress) Address() common.Address { func newTestScopeConext() TestScopeContextInterface { c := new(TestScopeContext) c.Memory = vm.NewMemory() - c.Memory.Resize(uint64(expandedFheCiphertextSize[FheUint8]) * 3) + c.Memory.Resize(uint64(tfhe.ExpandedFheCiphertextSize[tfhe.FheUint8]) * 3) c.Stack = newstack() c.Contract = vm.NewContract(testCallerAddress{}, testContractAddress{}, big.NewInt(10), 100000) return c @@ -242,7 +243,7 @@ func TestProtectedStorageSstoreSload(t *testing.T) { pc := uint64(0) depth := 1 environment.depth = depth - ct := verifyCiphertextInTestMemory(environment, 2, depth, FheUint32) + ct := verifyCiphertextInTestMemory(environment, 2, depth, tfhe.FheUint32) ctHash := ct.GetHash() scope := newTestScopeConext() loc := uint256.NewInt(10) @@ -281,7 +282,7 @@ func TestProtectedStorageGarbageCollectionNoFlaggedLocation(t *testing.T) { pc := uint64(0) depth := 1 environment.depth = depth - ctHash := verifyCiphertextInTestMemory(environment, 2, depth, FheUint8).GetHash() + ctHash := verifyCiphertextInTestMemory(environment, 2, depth, tfhe.FheUint8).GetHash() scope := newTestScopeConext() loc := uint256.NewInt(10) locHash := common.BytesToHash(loc.Bytes()) @@ -333,7 +334,7 @@ func TestProtectedStorageGarbageCollection(t *testing.T) { pc := uint64(0) depth := 1 environment.depth = depth - ctHash := verifyCiphertextInTestMemory(environment, 2, depth, FheUint8).GetHash() + ctHash := verifyCiphertextInTestMemory(environment, 2, depth, tfhe.FheUint8).GetHash() scope := newTestScopeConext() loc := uint256.NewInt(10) locHash := common.BytesToHash(loc.Bytes()) @@ -355,8 +356,8 @@ func TestProtectedStorageGarbageCollection(t *testing.T) { if metadata.refCount != 1 { t.Fatalf("metadata.refcount of ciphertext is not 1") } - if metadata.length != uint64(expandedFheCiphertextSize[FheUint8]) { - t.Fatalf("metadata.length (%v) != ciphertext len (%v)", metadata.length, uint64(expandedFheCiphertextSize[FheUint8])) + if metadata.length != uint64(tfhe.ExpandedFheCiphertextSize[tfhe.FheUint8]) { + t.Fatalf("metadata.length (%v) != ciphertext len (%v)", metadata.length, uint64(tfhe.ExpandedFheCiphertextSize[tfhe.FheUint8])) } ciphertextLocationsToCheck := (metadata.length + 32 - 1) / 32 startOfCiphertext := newInt(metadataKey.Bytes()) @@ -445,7 +446,7 @@ func TestOpReturnDelegation(t *testing.T) { pc := uint64(0) depth := 2 scope := newTestScopeConext() - ct := verifyCiphertextInTestMemory(environment, 2, depth, FheUint8) + ct := verifyCiphertextInTestMemory(environment, 2, depth, tfhe.FheUint8) ctHash := ct.GetHash() offset := uint256.NewInt(0) @@ -470,7 +471,7 @@ func TestOpReturnUnverifyIfNotReturned(t *testing.T) { pc := uint64(0) depth := 2 scope := newTestScopeConext() - ctHash := verifyCiphertextInTestMemory(environment, 2, depth, FheUint8).GetHash() + ctHash := verifyCiphertextInTestMemory(environment, 2, depth, tfhe.FheUint8).GetHash() offset := uint256.NewInt(0) len := uint256.NewInt(32) @@ -491,7 +492,7 @@ func TestOpReturnDoesNotUnverifyIfNotVerified(t *testing.T) { environment := newTestEVMEnvironment() pc := uint64(0) scope := newTestScopeConext() - ct := verifyCiphertextInTestMemory(environment, 2, 4, FheUint8) + ct := verifyCiphertextInTestMemory(environment, 2, 4, tfhe.FheUint8) ctHash := ct.GetHash() // Return from depth 3 to depth 2. However, ct is not verified at 3 and, hence, cannot diff --git a/fhevm/interface.go b/fhevm/interface.go index baa37d4..1698576 100644 --- a/fhevm/interface.go +++ b/fhevm/interface.go @@ -6,6 +6,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/holiman/uint256" + "github.com/zama-ai/fhevm-go/tfhe" ) type EVMEnvironment interface { @@ -47,24 +48,24 @@ type FhevmData struct { verifiedCiphertexts map[common.Hash]*verifiedCiphertext // All optimistic requires encountered up to that point in the txn execution - optimisticRequires []*TfheCiphertext + optimisticRequires []*tfhe.TfheCiphertext nextCiphertextHashOnGasEst uint256.Int } // Set the optimisticRequires array to an empty array func (data *FhevmData) resetOptimisticRequires() { - data.optimisticRequires = make([]*TfheCiphertext, 0) + data.optimisticRequires = make([]*tfhe.TfheCiphertext, 0) } // Append one ciphertext to the optimisticRequires array -func (data *FhevmData) appendOptimisticRequires(ct *TfheCiphertext) { +func (data *FhevmData) appendOptimisticRequires(ct *tfhe.TfheCiphertext) { data.optimisticRequires = append(data.optimisticRequires, ct) } func NewFhevmData() FhevmData { return FhevmData{ verifiedCiphertexts: make(map[common.Hash]*verifiedCiphertext), - optimisticRequires: make([]*TfheCiphertext, 0), + optimisticRequires: make([]*tfhe.TfheCiphertext, 0), } } diff --git a/fhevm/interpreter.go b/fhevm/interpreter.go index 5366343..ae7a6bf 100644 --- a/fhevm/interpreter.go +++ b/fhevm/interpreter.go @@ -2,6 +2,7 @@ package fhevm import ( "github.com/ethereum/go-ethereum/common" + "github.com/zama-ai/fhevm-go/tfhe" ) type ScopeContext interface { @@ -47,17 +48,17 @@ func (from *depthSet) clone() (to *depthSet) { type verifiedCiphertext struct { verifiedDepths *depthSet - ciphertext *TfheCiphertext + ciphertext *tfhe.TfheCiphertext } // Returns the type of the verified ciphertext -func (vc *verifiedCiphertext) fheUintType() FheUintType { - return vc.ciphertext.fheUintType +func (vc *verifiedCiphertext) fheUintType() tfhe.FheUintType { + return vc.ciphertext.FheUintType } // Returns the serialization of the verified ciphertext func (vc *verifiedCiphertext) serialization() []byte { - return vc.ciphertext.serialization + return vc.ciphertext.Serialization } // Returns the hash of the verified ciphertext @@ -70,12 +71,12 @@ type PrivilegedMemory struct { VerifiedCiphertexts map[common.Hash]*verifiedCiphertext // All optimistic requires encountered up to that point in the txn execution - optimisticRequires []*TfheCiphertext + optimisticRequires []*tfhe.TfheCiphertext } var PrivilegedMempory *PrivilegedMemory = &PrivilegedMemory{ make(map[common.Hash]*verifiedCiphertext), - make([]*TfheCiphertext, 0), + make([]*tfhe.TfheCiphertext, 0), } // Evaluate remaining optimistic requires when Interpreter.Run get an errStopToken diff --git a/fhevm/operators_comparison.go b/fhevm/operators_comparison.go index 2426a3c..e3a67c8 100644 --- a/fhevm/operators_comparison.go +++ b/fhevm/operators_comparison.go @@ -5,6 +5,7 @@ import ( "errors" "github.com/ethereum/go-ethereum/common" + "github.com/zama-ai/fhevm-go/tfhe" "go.opentelemetry.io/otel/trace" ) @@ -35,7 +36,7 @@ func fheLeRun(environment EVMEnvironment, caller common.Address, addr common.Add // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil + return importRandomCiphertext(environment, tfhe.FheBool), nil } result, err := lhs.ciphertext.Le(rhs.ciphertext) @@ -59,7 +60,7 @@ func fheLeRun(environment EVMEnvironment, caller common.Address, addr common.Add // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil + return importRandomCiphertext(environment, tfhe.FheBool), nil } result, err := lhs.ciphertext.ScalarLe(rhs.Uint64()) @@ -101,7 +102,7 @@ func fheLtRun(environment EVMEnvironment, caller common.Address, addr common.Add // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil + return importRandomCiphertext(environment, tfhe.FheBool), nil } result, err := lhs.ciphertext.Lt(rhs.ciphertext) @@ -125,7 +126,7 @@ func fheLtRun(environment EVMEnvironment, caller common.Address, addr common.Add // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil + return importRandomCiphertext(environment, tfhe.FheBool), nil } result, err := lhs.ciphertext.ScalarLt(rhs.Uint64()) @@ -167,7 +168,7 @@ func fheEqRun(environment EVMEnvironment, caller common.Address, addr common.Add // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil + return importRandomCiphertext(environment, tfhe.FheBool), nil } result, err := lhs.ciphertext.Eq(rhs.ciphertext) @@ -191,7 +192,7 @@ func fheEqRun(environment EVMEnvironment, caller common.Address, addr common.Add // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil + return importRandomCiphertext(environment, tfhe.FheBool), nil } result, err := lhs.ciphertext.ScalarEq(rhs.Uint64()) @@ -233,7 +234,7 @@ func fheGeRun(environment EVMEnvironment, caller common.Address, addr common.Add // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil + return importRandomCiphertext(environment, tfhe.FheBool), nil } result, err := lhs.ciphertext.Ge(rhs.ciphertext) @@ -257,7 +258,7 @@ func fheGeRun(environment EVMEnvironment, caller common.Address, addr common.Add // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil + return importRandomCiphertext(environment, tfhe.FheBool), nil } result, err := lhs.ciphertext.ScalarGe(rhs.Uint64()) @@ -299,7 +300,7 @@ func fheGtRun(environment EVMEnvironment, caller common.Address, addr common.Add // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil + return importRandomCiphertext(environment, tfhe.FheBool), nil } result, err := lhs.ciphertext.Gt(rhs.ciphertext) @@ -323,7 +324,7 @@ func fheGtRun(environment EVMEnvironment, caller common.Address, addr common.Add // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil + return importRandomCiphertext(environment, tfhe.FheBool), nil } result, err := lhs.ciphertext.ScalarGt(rhs.Uint64()) @@ -365,7 +366,7 @@ func fheNeRun(environment EVMEnvironment, caller common.Address, addr common.Add // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil + return importRandomCiphertext(environment, tfhe.FheBool), nil } result, err := lhs.ciphertext.Ne(rhs.ciphertext) @@ -389,7 +390,7 @@ func fheNeRun(environment EVMEnvironment, caller common.Address, addr common.Add // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. if !environment.IsCommitting() && !environment.IsEthCall() { - return importRandomCiphertext(environment, FheBool), nil + return importRandomCiphertext(environment, tfhe.FheBool), nil } result, err := lhs.ciphertext.ScalarNe(rhs.Uint64()) diff --git a/fhevm/operators_comparison_gas.go b/fhevm/operators_comparison_gas.go index ff19fe6..cd9f320 100644 --- a/fhevm/operators_comparison_gas.go +++ b/fhevm/operators_comparison_gas.go @@ -1,6 +1,10 @@ package fhevm -import "encoding/hex" +import ( + "encoding/hex" + + "github.com/zama-ai/fhevm-go/tfhe" +) func fheLeRequiredGas(environment EVMEnvironment, input []byte) uint64 { input = input[:minInt(65, len(input))] @@ -129,7 +133,7 @@ func fheIfThenElseRequiredGas(environment EVMEnvironment, input []byte) uint64 { logger.Error("IfThenElse op RequiredGas() inputs not verified", "err", err, "input", hex.EncodeToString(input)) return 0 } - if first.fheUintType() != FheBool { + if first.fheUintType() != tfhe.FheBool { logger.Error("IfThenElse op RequiredGas() invalid type for condition", "first", first.fheUintType()) return 0 } diff --git a/fhevm/operators_crypto.go b/fhevm/operators_crypto.go index 682da6c..f5717af 100644 --- a/fhevm/operators_crypto.go +++ b/fhevm/operators_crypto.go @@ -11,6 +11,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/zama-ai/fhevm-go/kms" + "github.com/zama-ai/fhevm-go/tfhe" "go.opentelemetry.io/otel/trace" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" @@ -43,15 +44,15 @@ func verifyCiphertextRun(environment EVMEnvironment, caller common.Address, addr ctBytes := input[:len(input)-1] ctTypeByte := input[len(input)-1] - if !isValidFheType(ctTypeByte) { + if !tfhe.IsValidFheType(ctTypeByte) { msg := "verifyCiphertext Run() ciphertext type is invalid" logger.Error(msg, "type", ctTypeByte) return nil, errors.New(msg) } - ctType := FheUintType(ctTypeByte) + ctType := tfhe.FheUintType(ctTypeByte) otelDescribeOperandsFheTypes(runSpan, ctType) - expectedSize, found := GetCompactFheCiphertextSize(ctType) + expectedSize, found := tfhe.GetCompactFheCiphertextSize(ctType) if !found || expectedSize != uint(len(ctBytes)) { msg := "verifyCiphertext Run() compact ciphertext size is invalid" logger.Error(msg, "type", ctTypeByte, "size", len(ctBytes), "expectedSize", expectedSize) @@ -63,7 +64,7 @@ func verifyCiphertextRun(environment EVMEnvironment, caller common.Address, addr return importRandomCiphertext(environment, ctType), nil } - ct := new(TfheCiphertext) + ct := new(tfhe.TfheCiphertext) err := ct.DeserializeCompact(ctBytes, ctType) if err != nil { logger.Error("verifyCiphertext failed to deserialize input ciphertext", @@ -110,17 +111,17 @@ func reencryptRun(environment EVMEnvironment, caller common.Address, addr common var fheType kms.FheType switch ct.fheUintType() { - case FheBool: + case tfhe.FheBool: fheType = kms.FheType_Bool - case FheUint4: + case tfhe.FheUint4: fheType = kms.FheType_Euint4 - case FheUint8: + case tfhe.FheUint8: fheType = kms.FheType_Euint8 - case FheUint16: + case tfhe.FheUint16: fheType = kms.FheType_Euint16 - case FheUint32: + case tfhe.FheUint32: fheType = kms.FheType_Euint32 - case FheUint64: + case tfhe.FheUint64: fheType = kms.FheType_Euint64 } @@ -192,7 +193,7 @@ func optimisticRequireRun(environment EVMEnvironment, caller common.Address, add if !environment.IsCommitting() && !environment.IsEthCall() { return nil, nil } - if ct.fheUintType() != FheUint8 { + if ct.fheUintType() != tfhe.FheUint8 { msg := "optimisticRequire ciphertext type is not FheUint8" logger.Error(msg, "type", ct.fheUintType()) return nil, errors.New(msg) @@ -279,22 +280,22 @@ func getCiphertextRun(environment EVMEnvironment, caller common.Address, addr co return ciphertext.bytes, nil } -func decryptValue(environment EVMEnvironment, ct *TfheCiphertext) (uint64, error) { +func decryptValue(environment EVMEnvironment, ct *tfhe.TfheCiphertext) (uint64, error) { logger := environment.GetLogger() var fheType kms.FheType switch ct.Type() { - case FheBool: + case tfhe.FheBool: fheType = kms.FheType_Bool - case FheUint4: + case tfhe.FheUint4: fheType = kms.FheType_Euint4 - case FheUint8: + case tfhe.FheUint8: fheType = kms.FheType_Euint8 - case FheUint16: + case tfhe.FheUint16: fheType = kms.FheType_Euint16 - case FheUint32: + case tfhe.FheUint32: fheType = kms.FheType_Euint32 - case FheUint64: + case tfhe.FheUint64: fheType = kms.FheType_Euint64 } @@ -339,7 +340,7 @@ func evaluateRemainingOptimisticRequires(environment EVMEnvironment) (bool, erro len := len(requires) defer func() { environment.FhevmData().resetOptimisticRequires() }() if len != 0 { - var cumulative *TfheCiphertext = requires[0] + var cumulative *tfhe.TfheCiphertext = requires[0] var err error for i := 1; i < len; i++ { cumulative, err = cumulative.Bitand(requires[i]) @@ -370,11 +371,11 @@ func castRun(environment EVMEnvironment, caller common.Address, addr common.Addr return nil, errors.New("unverified ciphertext handle") } - if !isValidFheType(input[32]) { + if !tfhe.IsValidFheType(input[32]) { logger.Error("invalid type to cast to") return nil, errors.New("invalid type provided") } - castToType := FheUintType(input[32]) + castToType := tfhe.FheUintType(input[32]) otelDescribeOperandsFheTypes(runSpan, ct.fheUintType(), castToType) @@ -409,13 +410,13 @@ func fhePubKeyRun(environment EVMEnvironment, caller common.Address, addr common input = input[:minInt(1, len(input))] existing := environment.GetState(fhePubKeyHashPrecompile, fhePubKeyHashSlot) - if existing != GetPksHash() { + if existing != tfhe.GetPksHash() { msg := "fhePubKey FHE public key hash doesn't match one stored in state" - environment.GetLogger().Error(msg, "existing", existing.Hex(), "pksHash", GetPksHash().Hex()) + environment.GetLogger().Error(msg, "existing", existing.Hex(), "pksHash", tfhe.GetPksHash().Hex()) return nil, errors.New(msg) } // serialize public key - pksBytes, err := serializePublicKey() + pksBytes, err := tfhe.SerializePublicKey() if err != nil { return nil, err } @@ -441,10 +442,10 @@ func trivialEncryptRun(environment EVMEnvironment, caller common.Address, addr c } valueToEncrypt := *new(big.Int).SetBytes(input[0:32]) - encryptToType := FheUintType(input[32]) + encryptToType := tfhe.FheUintType(input[32]) otelDescribeOperandsFheTypes(runSpan, encryptToType) - ct := new(TfheCiphertext).TrivialEncrypt(valueToEncrypt, encryptToType) + ct := new(tfhe.TfheCiphertext).TrivialEncrypt(valueToEncrypt, encryptToType) ctHash := ct.GetHash() importCiphertext(environment, ct) diff --git a/fhevm/operators_crypto_gas.go b/fhevm/operators_crypto_gas.go index f1f45f9..d38adb6 100644 --- a/fhevm/operators_crypto_gas.go +++ b/fhevm/operators_crypto_gas.go @@ -4,6 +4,7 @@ import ( "encoding/hex" "github.com/ethereum/go-ethereum/common" + "github.com/zama-ai/fhevm-go/tfhe" ) func verifyCiphertextRequiredGas(environment EVMEnvironment, input []byte) uint64 { @@ -13,7 +14,7 @@ func verifyCiphertextRequiredGas(environment EVMEnvironment, input []byte) uint6 "len", len(input)) return 0 } - ctType := FheUintType(input[len(input)-1]) + ctType := tfhe.FheUintType(input[len(input)-1]) return environment.FhevmParams().GasCosts.FheVerify[ctType] } @@ -48,15 +49,15 @@ func optimisticRequireRequiredGas(environment EVMEnvironment, input []byte) uint "input", hex.EncodeToString(input)) return 0 } - if ct.fheUintType() != FheUint8 { + if ct.fheUintType() != tfhe.FheUint8 { logger.Error("optimisticRequire RequiredGas() ciphertext type is not FheUint8", "type", ct.fheUintType()) return 0 } if len(environment.FhevmData().optimisticRequires) == 0 { - return environment.FhevmParams().GasCosts.FheOptRequire[FheUint8] + return environment.FhevmParams().GasCosts.FheOptRequire[tfhe.FheUint8] } - return environment.FhevmParams().GasCosts.FheOptRequireBitAnd[FheUint8] + return environment.FhevmParams().GasCosts.FheOptRequireBitAnd[tfhe.FheUint8] } func getCiphertextRequiredGas(environment EVMEnvironment, input []byte) uint64 { @@ -118,6 +119,6 @@ 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]) + encryptToType := tfhe.FheUintType(input[32]) return environment.FhevmParams().GasCosts.FheTrivialEncrypt[encryptToType] } diff --git a/fhevm/operators_rand.go b/fhevm/operators_rand.go index dd64f94..7620e9b 100644 --- a/fhevm/operators_rand.go +++ b/fhevm/operators_rand.go @@ -12,6 +12,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" fhevm_crypto "github.com/zama-ai/fhevm-go/crypto" + "github.com/zama-ai/fhevm-go/tfhe" "go.opentelemetry.io/otel/trace" "golang.org/x/crypto/chacha20" ) @@ -59,7 +60,7 @@ func applyUpperBound(rand uint64, bitsInRand int, upperBound *uint64) uint64 { return rand >> shift } -func generateRandom(environment EVMEnvironment, caller common.Address, resultType FheUintType, upperBound *uint64) ([]byte, error) { +func generateRandom(environment EVMEnvironment, caller common.Address, resultType tfhe.FheUintType, upperBound *uint64) ([]byte, error) { // If we are doing gas estimation, skip execution and insert a random ciphertext as a result. if !environment.IsCommitting() { return importRandomCiphertext(environment, resultType), nil @@ -96,27 +97,27 @@ func generateRandom(environment EVMEnvironment, caller common.Address, resultTyp // Apply upperBound, if set. var randUint uint64 switch resultType { - case FheUint4: + case tfhe.FheUint4: randBytes := make([]byte, 1) cipher.XORKeyStream(randBytes, randBytes) randUint = uint64(randBytes[0]) randUint = uint64(applyUpperBound(randUint, 4, upperBound)) - case FheUint8: + case tfhe.FheUint8: randBytes := make([]byte, 1) cipher.XORKeyStream(randBytes, randBytes) randUint = uint64(randBytes[0]) randUint = uint64(applyUpperBound(randUint, 8, upperBound)) - case FheUint16: + case tfhe.FheUint16: randBytes := make([]byte, 2) cipher.XORKeyStream(randBytes, randBytes) randUint = uint64(binary.BigEndian.Uint16(randBytes)) randUint = uint64(applyUpperBound(randUint, 16, upperBound)) - case FheUint32: + case tfhe.FheUint32: randBytes := make([]byte, 4) cipher.XORKeyStream(randBytes, randBytes) randUint = uint64(binary.BigEndian.Uint32(randBytes)) randUint = uint64(applyUpperBound(randUint, 32, upperBound)) - case FheUint64: + case tfhe.FheUint64: randBytes := make([]byte, 8) cipher.XORKeyStream(randBytes, randBytes) randUint = uint64(binary.BigEndian.Uint64(randBytes)) @@ -126,7 +127,7 @@ func generateRandom(environment EVMEnvironment, caller common.Address, resultTyp } // Trivially encrypt the random integer. - randCt := new(TfheCiphertext) + randCt := new(tfhe.TfheCiphertext) randBigInt := big.NewInt(0) randBigInt.SetUint64(randUint) randCt.TrivialEncrypt(*randBigInt, resultType) @@ -148,12 +149,12 @@ func fheRandRun(environment EVMEnvironment, caller common.Address, addr common.A logger.Error(msg) return nil, errors.New(msg) } - if len(input) != 1 || !isValidFheType(input[0]) { + if len(input) != 1 || !tfhe.IsValidFheType(input[0]) { msg := "fheRand input len must be at least 1 byte and be a valid FheUint type" logger.Error(msg, "input", hex.EncodeToString(input), "len", len(input)) return nil, errors.New(msg) } - resultType := FheUintType(input[0]) + resultType := tfhe.FheUintType(input[0]) otelDescribeOperandsFheTypes(runSpan, resultType) var noUpperBound *uint64 = nil return generateRandom(environment, caller, resultType, noUpperBound) diff --git a/fhevm/operators_rand_gas.go b/fhevm/operators_rand_gas.go index c459a26..455a012 100644 --- a/fhevm/operators_rand_gas.go +++ b/fhevm/operators_rand_gas.go @@ -6,35 +6,36 @@ import ( "math/bits" "github.com/holiman/uint256" + "github.com/zama-ai/fhevm-go/tfhe" ) func fheRandRequiredGas(environment EVMEnvironment, input []byte) uint64 { input = input[:minInt(1, len(input))] logger := environment.GetLogger() - if len(input) != 1 || !isValidFheType(input[0]) { + if len(input) != 1 || !tfhe.IsValidFheType(input[0]) { 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]) + t := tfhe.FheUintType(input[0]) return environment.FhevmParams().GasCosts.FheRand[t] } -func parseRandUpperBoundInput(input []byte) (randType FheUintType, upperBound *uint256.Int, err error) { - if len(input) != 33 || !isValidFheType(input[32]) { - return FheUint8, nil, fmt.Errorf("parseRandUpperBoundInput() invalid input len or type") +func parseRandUpperBoundInput(input []byte) (randType tfhe.FheUintType, upperBound *uint256.Int, err error) { + if len(input) != 33 || !tfhe.IsValidFheType(input[32]) { + return tfhe.FheUint8, nil, fmt.Errorf("parseRandUpperBoundInput() invalid input len or type") } - randType = FheUintType(input[32]) + randType = tfhe.FheUintType(input[32]) upperBound = uint256.NewInt(0) upperBound.SetBytes32(input) // For now, we only support bounds of up to 64 bits. if !upperBound.IsUint64() { - return FheUint8, nil, fmt.Errorf("parseRandUpperBoundInput() only supports bounds up to 64 bits") + return tfhe.FheUint8, nil, fmt.Errorf("parseRandUpperBoundInput() only supports bounds up to 64 bits") } upperBound64 := upperBound.Uint64() oneBits := bits.OnesCount64(upperBound64) if oneBits != 1 { - return FheUint8, nil, fmt.Errorf("parseRandUpperBoundInput() bound not a power of 2: %d", upperBound64) + return tfhe.FheUint8, nil, fmt.Errorf("parseRandUpperBoundInput() bound not a power of 2: %d", upperBound64) } return randType, upperBound, nil } diff --git a/fhevm/otel.go b/fhevm/otel.go index f2f682a..147122d 100644 --- a/fhevm/otel.go +++ b/fhevm/otel.go @@ -3,6 +3,7 @@ package fhevm import ( "math/big" + "github.com/zama-ai/fhevm-go/tfhe" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" ) @@ -42,7 +43,7 @@ func otelDescribeOperands(span trace.Span, operands ...operand) { span.SetAttributes(attribute.KeyValue{Key: operandTypeAttrName, Value: attribute.StringValue(operandTypes)}) } -func otelDescribeOperandsFheTypes(span trace.Span, types ...FheUintType) { +func otelDescribeOperandsFheTypes(span trace.Span, types ...tfhe.FheUintType) { if span == nil { return } diff --git a/fhevm/params.go b/fhevm/params.go index c403b95..d0ec2a4 100644 --- a/fhevm/params.go +++ b/fhevm/params.go @@ -1,5 +1,7 @@ package fhevm +import "github.com/zama-ai/fhevm-go/tfhe" + // This file contains default gas costs of fhEVM-related operations. // Users can change the values based on specific requirements in their blockchain. @@ -40,201 +42,201 @@ type FhevmParams struct { type GasCosts struct { FheCast uint64 FhePubKey uint64 - FheAddSub map[FheUintType]uint64 - FheDecrypt map[FheUintType]uint64 - FheBitwiseOp map[FheUintType]uint64 - FheMul map[FheUintType]uint64 - FheScalarMul map[FheUintType]uint64 - FheScalarDiv map[FheUintType]uint64 - FheScalarRem map[FheUintType]uint64 - FheShift map[FheUintType]uint64 - FheScalarShift map[FheUintType]uint64 - FheEq map[FheUintType]uint64 - FheLe map[FheUintType]uint64 - FheMinMax map[FheUintType]uint64 - FheScalarMinMax map[FheUintType]uint64 - FheNot map[FheUintType]uint64 - FheNeg map[FheUintType]uint64 - FheReencrypt map[FheUintType]uint64 - FheTrivialEncrypt map[FheUintType]uint64 - FheRand map[FheUintType]uint64 - FheIfThenElse map[FheUintType]uint64 - FheVerify map[FheUintType]uint64 - FheOptRequire map[FheUintType]uint64 - FheOptRequireBitAnd map[FheUintType]uint64 - FheGetCiphertext map[FheUintType]uint64 + FheAddSub map[tfhe.FheUintType]uint64 + FheDecrypt map[tfhe.FheUintType]uint64 + FheBitwiseOp map[tfhe.FheUintType]uint64 + FheMul map[tfhe.FheUintType]uint64 + FheScalarMul map[tfhe.FheUintType]uint64 + FheScalarDiv map[tfhe.FheUintType]uint64 + FheScalarRem map[tfhe.FheUintType]uint64 + FheShift map[tfhe.FheUintType]uint64 + FheScalarShift map[tfhe.FheUintType]uint64 + FheEq map[tfhe.FheUintType]uint64 + FheLe map[tfhe.FheUintType]uint64 + FheMinMax map[tfhe.FheUintType]uint64 + FheScalarMinMax map[tfhe.FheUintType]uint64 + FheNot map[tfhe.FheUintType]uint64 + FheNeg map[tfhe.FheUintType]uint64 + FheReencrypt map[tfhe.FheUintType]uint64 + FheTrivialEncrypt map[tfhe.FheUintType]uint64 + FheRand map[tfhe.FheUintType]uint64 + FheIfThenElse map[tfhe.FheUintType]uint64 + FheVerify map[tfhe.FheUintType]uint64 + FheOptRequire map[tfhe.FheUintType]uint64 + FheOptRequireBitAnd map[tfhe.FheUintType]uint64 + FheGetCiphertext map[tfhe.FheUintType]uint64 } func DefaultGasCosts() GasCosts { return GasCosts{ - FheAddSub: map[FheUintType]uint64{ - FheUint4: 55000 + AdjustFHEGas, - FheUint8: 84000 + AdjustFHEGas, - FheUint16: 123000 + AdjustFHEGas, - FheUint32: 152000 + AdjustFHEGas, - FheUint64: 178000 + AdjustFHEGas, - }, - FheDecrypt: map[FheUintType]uint64{ - FheUint4: 500000, - FheUint8: 500000, - FheUint16: 500000, - FheUint32: 500000, - FheUint64: 500000, - }, - FheBitwiseOp: map[FheUintType]uint64{ - FheBool: 16000 + AdjustFHEGas, - FheUint4: 22000 + AdjustFHEGas, - FheUint8: 24000 + AdjustFHEGas, - FheUint16: 24000 + AdjustFHEGas, - FheUint32: 25000 + AdjustFHEGas, - FheUint64: 28000 + AdjustFHEGas, - }, - FheMul: map[FheUintType]uint64{ - FheUint4: 140000 + AdjustFHEGas, - FheUint8: 187000 + AdjustFHEGas, - FheUint16: 252000 + AdjustFHEGas, - FheUint32: 349000 + AdjustFHEGas, - FheUint64: 631000 + AdjustFHEGas, - }, - FheScalarMul: map[FheUintType]uint64{ - FheUint4: 78000 + AdjustFHEGas, - FheUint8: 149000 + AdjustFHEGas, - FheUint16: 198000 + AdjustFHEGas, - FheUint32: 254000 + AdjustFHEGas, - FheUint64: 346000 + AdjustFHEGas, - }, - FheScalarDiv: map[FheUintType]uint64{ - FheUint4: 129000 + AdjustFHEGas, - FheUint8: 228000 + AdjustFHEGas, - FheUint16: 304000 + AdjustFHEGas, - FheUint32: 388000 + AdjustFHEGas, - FheUint64: 574000 + AdjustFHEGas, - }, - FheScalarRem: map[FheUintType]uint64{ - FheUint4: 276000 + AdjustFHEGas, - FheUint8: 450000 + AdjustFHEGas, - FheUint16: 612000 + AdjustFHEGas, - FheUint32: 795000 + AdjustFHEGas, - FheUint64: 1095000 + AdjustFHEGas, - }, - FheShift: map[FheUintType]uint64{ - FheUint4: 106000 + AdjustFHEGas, - FheUint8: 123000 + AdjustFHEGas, - FheUint16: 143000 + AdjustFHEGas, - FheUint32: 173000 + AdjustFHEGas, - FheUint64: 217000 + AdjustFHEGas, - }, - FheScalarShift: map[FheUintType]uint64{ - FheUint4: 25000 + AdjustFHEGas, - FheUint8: 25000 + AdjustFHEGas, - FheUint16: 25000 + AdjustFHEGas, - FheUint32: 25000 + AdjustFHEGas, - FheUint64: 28000 + AdjustFHEGas, - }, - FheEq: map[FheUintType]uint64{ - FheUint4: 41000 + AdjustFHEGas, - FheUint8: 43000 + AdjustFHEGas, - FheUint16: 44000 + AdjustFHEGas, - FheUint32: 72000 + AdjustFHEGas, - FheUint64: 76000 + AdjustFHEGas, - }, - FheLe: map[FheUintType]uint64{ - FheUint4: 60000 + AdjustFHEGas, - FheUint8: 72000 + AdjustFHEGas, - FheUint16: 95000 + AdjustFHEGas, - FheUint32: 118000 + AdjustFHEGas, - FheUint64: 146000 + AdjustFHEGas, - }, - FheMinMax: map[FheUintType]uint64{ - FheUint4: 106000 + AdjustFHEGas, - FheUint8: 118000 + AdjustFHEGas, - FheUint16: 143000 + AdjustFHEGas, - FheUint32: 173000 + AdjustFHEGas, - FheUint64: 200000 + AdjustFHEGas, - }, - FheScalarMinMax: map[FheUintType]uint64{ - FheUint4: 111000 + AdjustFHEGas, - FheUint8: 114000 + AdjustFHEGas, - FheUint16: 140000 + AdjustFHEGas, - FheUint32: 154000 + AdjustFHEGas, - FheUint64: 182000 + AdjustFHEGas, - }, - FheNot: map[FheUintType]uint64{ - FheUint4: 23000 + AdjustFHEGas, - FheUint8: 24000 + AdjustFHEGas, - FheUint16: 25000 + AdjustFHEGas, - FheUint32: 26000 + AdjustFHEGas, - FheUint64: 27000 + AdjustFHEGas, - }, - FheNeg: map[FheUintType]uint64{ - FheUint4: 50000 + AdjustFHEGas, - FheUint8: 85000 + AdjustFHEGas, - FheUint16: 121000 + AdjustFHEGas, - FheUint32: 150000 + AdjustFHEGas, - FheUint64: 189000 + AdjustFHEGas, + FheAddSub: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: 55000 + AdjustFHEGas, + tfhe.FheUint8: 84000 + AdjustFHEGas, + tfhe.FheUint16: 123000 + AdjustFHEGas, + tfhe.FheUint32: 152000 + AdjustFHEGas, + tfhe.FheUint64: 178000 + AdjustFHEGas, + }, + FheDecrypt: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: 500000, + tfhe.FheUint8: 500000, + tfhe.FheUint16: 500000, + tfhe.FheUint32: 500000, + tfhe.FheUint64: 500000, + }, + FheBitwiseOp: map[tfhe.FheUintType]uint64{ + tfhe.FheBool: 16000 + AdjustFHEGas, + tfhe.FheUint4: 22000 + AdjustFHEGas, + tfhe.FheUint8: 24000 + AdjustFHEGas, + tfhe.FheUint16: 24000 + AdjustFHEGas, + tfhe.FheUint32: 25000 + AdjustFHEGas, + tfhe.FheUint64: 28000 + AdjustFHEGas, + }, + FheMul: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: 140000 + AdjustFHEGas, + tfhe.FheUint8: 187000 + AdjustFHEGas, + tfhe.FheUint16: 252000 + AdjustFHEGas, + tfhe.FheUint32: 349000 + AdjustFHEGas, + tfhe.FheUint64: 631000 + AdjustFHEGas, + }, + FheScalarMul: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: 78000 + AdjustFHEGas, + tfhe.FheUint8: 149000 + AdjustFHEGas, + tfhe.FheUint16: 198000 + AdjustFHEGas, + tfhe.FheUint32: 254000 + AdjustFHEGas, + tfhe.FheUint64: 346000 + AdjustFHEGas, + }, + FheScalarDiv: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: 129000 + AdjustFHEGas, + tfhe.FheUint8: 228000 + AdjustFHEGas, + tfhe.FheUint16: 304000 + AdjustFHEGas, + tfhe.FheUint32: 388000 + AdjustFHEGas, + tfhe.FheUint64: 574000 + AdjustFHEGas, + }, + FheScalarRem: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: 276000 + AdjustFHEGas, + tfhe.FheUint8: 450000 + AdjustFHEGas, + tfhe.FheUint16: 612000 + AdjustFHEGas, + tfhe.FheUint32: 795000 + AdjustFHEGas, + tfhe.FheUint64: 1095000 + AdjustFHEGas, + }, + FheShift: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: 106000 + AdjustFHEGas, + tfhe.FheUint8: 123000 + AdjustFHEGas, + tfhe.FheUint16: 143000 + AdjustFHEGas, + tfhe.FheUint32: 173000 + AdjustFHEGas, + tfhe.FheUint64: 217000 + AdjustFHEGas, + }, + FheScalarShift: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: 25000 + AdjustFHEGas, + tfhe.FheUint8: 25000 + AdjustFHEGas, + tfhe.FheUint16: 25000 + AdjustFHEGas, + tfhe.FheUint32: 25000 + AdjustFHEGas, + tfhe.FheUint64: 28000 + AdjustFHEGas, + }, + FheEq: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: 41000 + AdjustFHEGas, + tfhe.FheUint8: 43000 + AdjustFHEGas, + tfhe.FheUint16: 44000 + AdjustFHEGas, + tfhe.FheUint32: 72000 + AdjustFHEGas, + tfhe.FheUint64: 76000 + AdjustFHEGas, + }, + FheLe: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: 60000 + AdjustFHEGas, + tfhe.FheUint8: 72000 + AdjustFHEGas, + tfhe.FheUint16: 95000 + AdjustFHEGas, + tfhe.FheUint32: 118000 + AdjustFHEGas, + tfhe.FheUint64: 146000 + AdjustFHEGas, + }, + FheMinMax: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: 106000 + AdjustFHEGas, + tfhe.FheUint8: 118000 + AdjustFHEGas, + tfhe.FheUint16: 143000 + AdjustFHEGas, + tfhe.FheUint32: 173000 + AdjustFHEGas, + tfhe.FheUint64: 200000 + AdjustFHEGas, + }, + FheScalarMinMax: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: 111000 + AdjustFHEGas, + tfhe.FheUint8: 114000 + AdjustFHEGas, + tfhe.FheUint16: 140000 + AdjustFHEGas, + tfhe.FheUint32: 154000 + AdjustFHEGas, + tfhe.FheUint64: 182000 + AdjustFHEGas, + }, + FheNot: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: 23000 + AdjustFHEGas, + tfhe.FheUint8: 24000 + AdjustFHEGas, + tfhe.FheUint16: 25000 + AdjustFHEGas, + tfhe.FheUint32: 26000 + AdjustFHEGas, + tfhe.FheUint64: 27000 + AdjustFHEGas, + }, + FheNeg: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: 50000 + AdjustFHEGas, + tfhe.FheUint8: 85000 + AdjustFHEGas, + tfhe.FheUint16: 121000 + AdjustFHEGas, + tfhe.FheUint32: 150000 + AdjustFHEGas, + tfhe.FheUint64: 189000 + AdjustFHEGas, }, // TODO: Costs will depend on the complexity of doing reencryption/decryption by the oracle. - FheReencrypt: map[FheUintType]uint64{ - FheBool: 1000, - FheUint4: 1000, - FheUint8: 1000, - FheUint16: 1100, - FheUint32: 1200, + FheReencrypt: map[tfhe.FheUintType]uint64{ + tfhe.FheBool: 1000, + tfhe.FheUint4: 1000, + tfhe.FheUint8: 1000, + tfhe.FheUint16: 1100, + tfhe.FheUint32: 1200, }, // As of now, verification costs only cover ciphertext deserialization and assume there is no ZKPoK to verify. - FheVerify: map[FheUintType]uint64{ - FheBool: 200, - FheUint4: 200, - FheUint8: 200, - FheUint16: 300, - FheUint32: 400, - FheUint64: 800, - }, - FheTrivialEncrypt: map[FheUintType]uint64{ - FheBool: 100, - FheUint4: 100, - FheUint8: 100, - FheUint16: 200, - FheUint32: 300, - FheUint64: 600, + FheVerify: map[tfhe.FheUintType]uint64{ + tfhe.FheBool: 200, + tfhe.FheUint4: 200, + tfhe.FheUint8: 200, + tfhe.FheUint16: 300, + tfhe.FheUint32: 400, + tfhe.FheUint64: 800, + }, + FheTrivialEncrypt: map[tfhe.FheUintType]uint64{ + tfhe.FheBool: 100, + tfhe.FheUint4: 100, + tfhe.FheUint8: 100, + tfhe.FheUint16: 200, + tfhe.FheUint32: 300, + tfhe.FheUint64: 600, }, // TODO: These will change once we have an FHE-based random generaration. - FheRand: map[FheUintType]uint64{ - FheUint4: EvmNetSstoreInitGas + 100000, - FheUint8: EvmNetSstoreInitGas + 100000, - FheUint16: EvmNetSstoreInitGas + 100000, - FheUint32: EvmNetSstoreInitGas + 100000, - FheUint64: EvmNetSstoreInitGas + 100000, - }, - FheIfThenElse: map[FheUintType]uint64{ - FheUint4: 35000 + AdjustFHEGas, - FheUint8: 37000 + AdjustFHEGas, - FheUint16: 37000 + AdjustFHEGas, - FheUint32: 40000 + AdjustFHEGas, - FheUint64: 43000 + AdjustFHEGas, + FheRand: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: EvmNetSstoreInitGas + 100000, + tfhe.FheUint8: EvmNetSstoreInitGas + 100000, + tfhe.FheUint16: EvmNetSstoreInitGas + 100000, + tfhe.FheUint32: EvmNetSstoreInitGas + 100000, + tfhe.FheUint64: EvmNetSstoreInitGas + 100000, + }, + FheIfThenElse: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: 35000 + AdjustFHEGas, + tfhe.FheUint8: 37000 + AdjustFHEGas, + tfhe.FheUint16: 37000 + AdjustFHEGas, + tfhe.FheUint32: 40000 + AdjustFHEGas, + tfhe.FheUint64: 43000 + AdjustFHEGas, }, // 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{ - FheUint4: 170000, - FheUint8: 170000, - FheUint16: 180000, - FheUint32: 190000, - }, - FheOptRequireBitAnd: map[FheUintType]uint64{ - FheUint4: 20000, - FheUint8: 20000, - FheUint16: 20000, - FheUint32: 20000, - }, - FheGetCiphertext: map[FheUintType]uint64{ - FheUint8: 12000, - FheUint16: 14000, - FheUint32: 18000, - FheUint64: 28000, + FheOptRequire: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: 170000, + tfhe.FheUint8: 170000, + tfhe.FheUint16: 180000, + tfhe.FheUint32: 190000, + }, + FheOptRequireBitAnd: map[tfhe.FheUintType]uint64{ + tfhe.FheUint4: 20000, + tfhe.FheUint8: 20000, + tfhe.FheUint16: 20000, + tfhe.FheUint32: 20000, + }, + FheGetCiphertext: map[tfhe.FheUintType]uint64{ + tfhe.FheUint8: 12000, + tfhe.FheUint16: 14000, + tfhe.FheUint32: 18000, + tfhe.FheUint64: 28000, }, } } diff --git a/fhevm/protected_storage.go b/fhevm/protected_storage.go index 0e2ad23..a709122 100644 --- a/fhevm/protected_storage.go +++ b/fhevm/protected_storage.go @@ -8,6 +8,7 @@ import ( crypto "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" fhevm_crypto "github.com/zama-ai/fhevm-go/crypto" + "github.com/zama-ai/fhevm-go/tfhe" ) // An arbitrary constant value to flag locations in protected storage. @@ -25,7 +26,7 @@ func minUint64(a, b uint64) uint64 { type ciphertextMetadata struct { refCount uint64 length uint64 - fheUintType FheUintType + fheUintType tfhe.FheUintType } func (m ciphertextMetadata) serialize() [32]byte { @@ -41,7 +42,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 = tfhe.FheUintType(u[2]) return m } @@ -117,8 +118,8 @@ func persistIfVerifiedCiphertext(flagHandleLocation common.Hash, handle common.H if metadataInt.IsZero() { // If no metadata, it means this ciphertext itself hasn't been persisted to protected storage yet. We do that as part of SSTORE. metadata.refCount = 1 - metadata.length = uint64(expandedFheCiphertextSize[verifiedCiphertext.ciphertext.fheUintType]) - metadata.fheUintType = verifiedCiphertext.ciphertext.fheUintType + metadata.length = uint64(tfhe.ExpandedFheCiphertextSize[verifiedCiphertext.ciphertext.FheUintType]) + metadata.fheUintType = verifiedCiphertext.ciphertext.FheUintType ciphertextSlot := newInt(metadataKey.Bytes()) ciphertextSlot.AddUint64(ciphertextSlot, 1) if env.IsCommitting() { diff --git a/fhevm/tfhe_ciphertext.go b/tfhe/tfhe_ciphertext.go similarity index 88% rename from fhevm/tfhe_ciphertext.go rename to tfhe/tfhe_ciphertext.go index 18f54d7..56b96c5 100644 --- a/fhevm/tfhe_ciphertext.go +++ b/tfhe/tfhe_ciphertext.go @@ -1,4 +1,4 @@ -package fhevm +package tfhe /* #include "tfhe_wrappers.h" @@ -44,7 +44,7 @@ func (t FheUintType) String() string { } } -func isValidFheType(t byte) bool { +func IsValidFheType(t byte) bool { if uint8(t) < uint8(FheBool) || uint8(t) > uint8(FheUint64) { return false } @@ -53,13 +53,13 @@ func isValidFheType(t byte) bool { // Represents an expanded TFHE ciphertext. type TfheCiphertext struct { - serialization []byte - hash *common.Hash - fheUintType FheUintType + Serialization []byte + Hash *common.Hash + FheUintType FheUintType } func (ct *TfheCiphertext) Type() FheUintType { - return ct.fheUintType + return ct.FheUintType } func boolBinaryNotSupportedOp(lhs unsafe.Pointer, rhs unsafe.Pointer) (unsafe.Pointer, error) { return nil, errors.New("Bool is not supported") @@ -115,8 +115,8 @@ func (ct *TfheCiphertext) Deserialize(in []byte, t FheUintType) error { default: panic("deserialize: unexpected ciphertext type") } - ct.fheUintType = t - ct.serialization = in + ct.FheUintType = t + ct.Serialization = in ct.computeHash() return nil } @@ -132,7 +132,7 @@ func (ct *TfheCiphertext) DeserializeCompact(in []byte, t FheUintType) error { return errors.New("compact FheBool ciphertext deserialization failed") } var err error - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_bool(ptr) if err != nil { return err @@ -143,7 +143,7 @@ func (ct *TfheCiphertext) DeserializeCompact(in []byte, t FheUintType) error { return errors.New("compact FheUint4 ciphertext deserialization failed") } var err error - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_uint4(ptr) if err != nil { return err @@ -154,7 +154,7 @@ func (ct *TfheCiphertext) DeserializeCompact(in []byte, t FheUintType) error { return errors.New("compact FheUint8 ciphertext deserialization failed") } var err error - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_uint8(ptr) if err != nil { return err @@ -165,7 +165,7 @@ func (ct *TfheCiphertext) DeserializeCompact(in []byte, t FheUintType) error { return errors.New("compact FheUint16 ciphertext deserialization failed") } var err error - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_uint16(ptr) if err != nil { return err @@ -176,7 +176,7 @@ func (ct *TfheCiphertext) DeserializeCompact(in []byte, t FheUintType) error { return errors.New("compact FheUint32 ciphertext deserialization failed") } var err error - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_uint32(ptr) if err != nil { return err @@ -187,7 +187,7 @@ func (ct *TfheCiphertext) DeserializeCompact(in []byte, t FheUintType) error { return errors.New("compact FheUint64 ciphertext deserialization failed") } var err error - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_uint64(ptr) if err != nil { return err @@ -195,7 +195,7 @@ func (ct *TfheCiphertext) DeserializeCompact(in []byte, t FheUintType) error { default: panic("deserializeCompact: unexpected ciphertext type") } - ct.fheUintType = t + ct.FheUintType = t ct.computeHash() return nil } @@ -212,42 +212,42 @@ func (ct *TfheCiphertext) Encrypt(value big.Int, t FheUintType) *TfheCiphertext val = true } ptr = C.public_key_encrypt_fhe_bool(pks, C.bool(val)) - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_bool(ptr) if err != nil { panic(err) } case FheUint4: ptr = C.public_key_encrypt_fhe_uint4(pks, C.uint8_t(value.Uint64())) - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_uint4(ptr) if err != nil { panic(err) } case FheUint8: ptr = C.public_key_encrypt_fhe_uint8(pks, C.uint8_t(value.Uint64())) - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_uint8(ptr) if err != nil { panic(err) } case FheUint16: ptr = C.public_key_encrypt_fhe_uint16(pks, C.uint16_t(value.Uint64())) - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_uint16(ptr) if err != nil { panic(err) } case FheUint32: ptr = C.public_key_encrypt_fhe_uint32(pks, C.uint32_t(value.Uint64())) - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_uint32(ptr) if err != nil { panic(err) } case FheUint64: ptr = C.public_key_encrypt_fhe_uint64(pks, C.uint64_t(value.Uint64())) - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_uint64(ptr) if err != nil { panic(err) @@ -255,7 +255,7 @@ func (ct *TfheCiphertext) Encrypt(value big.Int, t FheUintType) *TfheCiphertext default: panic("encrypt: unexpected ciphertext type") } - ct.fheUintType = t + ct.FheUintType = t ct.computeHash() return ct } @@ -270,42 +270,42 @@ func (ct *TfheCiphertext) TrivialEncrypt(value big.Int, t FheUintType) *TfheCiph val = true } ptr = C.trivial_encrypt_fhe_bool(sks, C.bool(val)) - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_bool(ptr) if err != nil { panic(err) } case FheUint4: ptr = C.trivial_encrypt_fhe_uint4(sks, C.uint8_t(value.Uint64())) - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_uint4(ptr) if err != nil { panic(err) } case FheUint8: ptr = C.trivial_encrypt_fhe_uint8(sks, C.uint8_t(value.Uint64())) - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_uint8(ptr) if err != nil { panic(err) } case FheUint16: ptr = C.trivial_encrypt_fhe_uint16(sks, C.uint16_t(value.Uint64())) - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_uint16(ptr) if err != nil { panic(err) } case FheUint32: ptr = C.trivial_encrypt_fhe_uint32(sks, C.uint32_t(value.Uint64())) - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_uint32(ptr) if err != nil { panic(err) } case FheUint64: ptr = C.trivial_encrypt_fhe_uint64(sks, C.uint64_t(value.Uint64())) - ct.serialization, err = serialize(ptr, t) + ct.Serialization, err = serialize(ptr, t) C.destroy_fhe_uint64(ptr) if err != nil { panic(err) @@ -313,13 +313,13 @@ func (ct *TfheCiphertext) TrivialEncrypt(value big.Int, t FheUintType) *TfheCiph default: panic("trivialEncrypt: unexpected ciphertext type") } - ct.fheUintType = t + ct.FheUintType = t ct.computeHash() return ct } func (ct *TfheCiphertext) Serialize() []byte { - return ct.serialization + return ct.Serialization } func (ct *TfheCiphertext) executeUnaryCiphertextOperation(rhs *TfheCiphertext, @@ -331,11 +331,11 @@ func (ct *TfheCiphertext) executeUnaryCiphertextOperation(rhs *TfheCiphertext, op64 func(ct unsafe.Pointer) (unsafe.Pointer, error)) (*TfheCiphertext, error) { res := new(TfheCiphertext) - res.fheUintType = ct.fheUintType + res.FheUintType = ct.FheUintType res_ser := &C.DynamicBuffer{} - switch ct.fheUintType { + switch ct.FheUintType { case FheBool: - ct_ptr := C.deserialize_fhe_bool(toDynamicBufferView((ct.serialization))) + ct_ptr := C.deserialize_fhe_bool(toDynamicBufferView((ct.Serialization))) if ct_ptr == nil { return nil, errors.New("Bool unary op deserialization failed") } @@ -352,10 +352,10 @@ func (ct *TfheCiphertext) executeUnaryCiphertextOperation(rhs *TfheCiphertext, if ret != 0 { return nil, errors.New("Bool unary op serialization failed") } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint4: - ct_ptr := C.deserialize_fhe_uint4(toDynamicBufferView((ct.serialization))) + ct_ptr := C.deserialize_fhe_uint4(toDynamicBufferView((ct.Serialization))) if ct_ptr == nil { return nil, errors.New("8 bit unary op deserialization failed") } @@ -372,10 +372,10 @@ func (ct *TfheCiphertext) executeUnaryCiphertextOperation(rhs *TfheCiphertext, if ret != 0 { return nil, errors.New("8 bit unary op serialization failed") } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint8: - ct_ptr := C.deserialize_fhe_uint8(toDynamicBufferView((ct.serialization))) + ct_ptr := C.deserialize_fhe_uint8(toDynamicBufferView((ct.Serialization))) if ct_ptr == nil { return nil, errors.New("8 bit unary op deserialization failed") } @@ -392,10 +392,10 @@ func (ct *TfheCiphertext) executeUnaryCiphertextOperation(rhs *TfheCiphertext, if ret != 0 { return nil, errors.New("8 bit unary op serialization failed") } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint16: - ct_ptr := C.deserialize_fhe_uint16(toDynamicBufferView((ct.serialization))) + ct_ptr := C.deserialize_fhe_uint16(toDynamicBufferView((ct.Serialization))) if ct_ptr == nil { return nil, errors.New("16 bit unary op deserialization failed") } @@ -412,10 +412,10 @@ func (ct *TfheCiphertext) executeUnaryCiphertextOperation(rhs *TfheCiphertext, if ret != 0 { return nil, errors.New("16 bit unary op serialization failed") } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint32: - ct_ptr := C.deserialize_fhe_uint32(toDynamicBufferView((ct.serialization))) + ct_ptr := C.deserialize_fhe_uint32(toDynamicBufferView((ct.Serialization))) if ct_ptr == nil { return nil, errors.New("32 bit unary op deserialization failed") } @@ -432,10 +432,10 @@ func (ct *TfheCiphertext) executeUnaryCiphertextOperation(rhs *TfheCiphertext, if ret != 0 { return nil, errors.New("32 bit unary op serialization failed") } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint64: - ct_ptr := C.deserialize_fhe_uint64(toDynamicBufferView((ct.serialization))) + ct_ptr := C.deserialize_fhe_uint64(toDynamicBufferView((ct.Serialization))) if ct_ptr == nil { return nil, errors.New("64 bit unary op deserialization failed") } @@ -452,7 +452,7 @@ func (ct *TfheCiphertext) executeUnaryCiphertextOperation(rhs *TfheCiphertext, if ret != 0 { return nil, errors.New("64 bit unary op serialization failed") } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) default: panic("unary op unexpected ciphertext type") @@ -469,24 +469,24 @@ func (lhs *TfheCiphertext) executeBinaryCiphertextOperation(rhs *TfheCiphertext, op32 func(lhs unsafe.Pointer, rhs unsafe.Pointer) (unsafe.Pointer, error), op64 func(lhs unsafe.Pointer, rhs unsafe.Pointer) (unsafe.Pointer, error), returnBool bool) (*TfheCiphertext, error) { - if lhs.fheUintType != rhs.fheUintType { + if lhs.FheUintType != rhs.FheUintType { return nil, errors.New("binary operations are only well-defined for identical types") } res := new(TfheCiphertext) if(returnBool) { - res.fheUintType = FheBool + res.FheUintType = FheBool } else { - res.fheUintType = lhs.fheUintType + res.FheUintType = lhs.FheUintType } res_ser := &C.DynamicBuffer{} - switch lhs.fheUintType { + switch lhs.FheUintType { case FheBool: - lhs_ptr := C.deserialize_fhe_bool(toDynamicBufferView((lhs.serialization))) + lhs_ptr := C.deserialize_fhe_bool(toDynamicBufferView((lhs.Serialization))) if lhs_ptr == nil { return nil, errors.New("bool binary op deserialization failed") } - rhs_ptr := C.deserialize_fhe_bool(toDynamicBufferView((rhs.serialization))) + rhs_ptr := C.deserialize_fhe_bool(toDynamicBufferView((rhs.Serialization))) if rhs_ptr == nil { C.destroy_fhe_bool(lhs_ptr) return nil, errors.New("bool binary op deserialization failed") @@ -505,14 +505,14 @@ func (lhs *TfheCiphertext) executeBinaryCiphertextOperation(rhs *TfheCiphertext, if ret != 0 { return nil, errors.New("bool binary op serialization failed") } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint4: - lhs_ptr := C.deserialize_fhe_uint4(toDynamicBufferView((lhs.serialization))) + lhs_ptr := C.deserialize_fhe_uint4(toDynamicBufferView((lhs.Serialization))) if lhs_ptr == nil { return nil, errors.New("4 bit binary op deserialization failed") } - rhs_ptr := C.deserialize_fhe_uint4(toDynamicBufferView((rhs.serialization))) + rhs_ptr := C.deserialize_fhe_uint4(toDynamicBufferView((rhs.Serialization))) if rhs_ptr == nil { C.destroy_fhe_uint4(lhs_ptr) return nil, errors.New("4 bit binary op deserialization failed") @@ -539,14 +539,14 @@ func (lhs *TfheCiphertext) executeBinaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("4 bit binary op serialization failed") } } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint8: - lhs_ptr := C.deserialize_fhe_uint8(toDynamicBufferView((lhs.serialization))) + lhs_ptr := C.deserialize_fhe_uint8(toDynamicBufferView((lhs.Serialization))) if lhs_ptr == nil { return nil, errors.New("8 bit binary op deserialization failed") } - rhs_ptr := C.deserialize_fhe_uint8(toDynamicBufferView((rhs.serialization))) + rhs_ptr := C.deserialize_fhe_uint8(toDynamicBufferView((rhs.Serialization))) if rhs_ptr == nil { C.destroy_fhe_uint8(lhs_ptr) return nil, errors.New("8 bit binary op deserialization failed") @@ -573,14 +573,14 @@ func (lhs *TfheCiphertext) executeBinaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("8 bit binary op serialization failed") } } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint16: - lhs_ptr := C.deserialize_fhe_uint16(toDynamicBufferView((lhs.serialization))) + lhs_ptr := C.deserialize_fhe_uint16(toDynamicBufferView((lhs.Serialization))) if lhs_ptr == nil { return nil, errors.New("16 bit binary op deserialization failed") } - rhs_ptr := C.deserialize_fhe_uint16(toDynamicBufferView((rhs.serialization))) + rhs_ptr := C.deserialize_fhe_uint16(toDynamicBufferView((rhs.Serialization))) if rhs_ptr == nil { C.destroy_fhe_uint16(lhs_ptr) return nil, errors.New("16 bit binary op deserialization failed") @@ -607,14 +607,14 @@ func (lhs *TfheCiphertext) executeBinaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("8 bit binary op serialization failed") } } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint32: - lhs_ptr := C.deserialize_fhe_uint32(toDynamicBufferView((lhs.serialization))) + lhs_ptr := C.deserialize_fhe_uint32(toDynamicBufferView((lhs.Serialization))) if lhs_ptr == nil { return nil, errors.New("32 bit binary op deserialization failed") } - rhs_ptr := C.deserialize_fhe_uint32(toDynamicBufferView((rhs.serialization))) + rhs_ptr := C.deserialize_fhe_uint32(toDynamicBufferView((rhs.Serialization))) if rhs_ptr == nil { C.destroy_fhe_uint32(lhs_ptr) return nil, errors.New("32 bit binary op deserialization failed") @@ -642,14 +642,14 @@ func (lhs *TfheCiphertext) executeBinaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("32 bit binary op serialization failed") } } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint64: - lhs_ptr := C.deserialize_fhe_uint64(toDynamicBufferView((lhs.serialization))) + lhs_ptr := C.deserialize_fhe_uint64(toDynamicBufferView((lhs.Serialization))) if lhs_ptr == nil { return nil, errors.New("64 bit binary op deserialization failed") } - rhs_ptr := C.deserialize_fhe_uint64(toDynamicBufferView((rhs.serialization))) + rhs_ptr := C.deserialize_fhe_uint64(toDynamicBufferView((rhs.Serialization))) if rhs_ptr == nil { C.destroy_fhe_uint64(lhs_ptr) return nil, errors.New("64 bit binary op deserialization failed") @@ -676,7 +676,7 @@ func (lhs *TfheCiphertext) executeBinaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("64 bit binary op serialization failed") } } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) default: panic("binary op unexpected ciphertext type") @@ -691,25 +691,25 @@ func (first *TfheCiphertext) executeTernaryCiphertextOperation(lhs *TfheCipherte op16 func(first unsafe.Pointer, lhs unsafe.Pointer, rhs unsafe.Pointer) unsafe.Pointer, op32 func(first unsafe.Pointer, lhs unsafe.Pointer, rhs unsafe.Pointer) unsafe.Pointer, op64 func(first unsafe.Pointer, lhs unsafe.Pointer, rhs unsafe.Pointer) unsafe.Pointer) (*TfheCiphertext, error) { - if lhs.fheUintType != rhs.fheUintType { + if lhs.FheUintType != rhs.FheUintType { return nil, errors.New("ternary operations are only well-defined for identical types") } res := new(TfheCiphertext) - res.fheUintType = lhs.fheUintType + res.FheUintType = lhs.FheUintType res_ser := &C.DynamicBuffer{} - switch lhs.fheUintType { + switch lhs.FheUintType { case FheUint4: - lhs_ptr := C.deserialize_fhe_uint4(toDynamicBufferView((lhs.serialization))) + lhs_ptr := C.deserialize_fhe_uint4(toDynamicBufferView((lhs.Serialization))) if lhs_ptr == nil { return nil, errors.New("4 bit binary op deserialization failed") } - rhs_ptr := C.deserialize_fhe_uint4(toDynamicBufferView((rhs.serialization))) + rhs_ptr := C.deserialize_fhe_uint4(toDynamicBufferView((rhs.Serialization))) if rhs_ptr == nil { C.destroy_fhe_uint4(lhs_ptr) return nil, errors.New("4 bit binary op deserialization failed") } - first_ptr := C.deserialize_fhe_bool(toDynamicBufferView((first.serialization))) + first_ptr := C.deserialize_fhe_bool(toDynamicBufferView((first.Serialization))) if first_ptr == nil { C.destroy_fhe_uint4(lhs_ptr) C.destroy_fhe_uint4(rhs_ptr) @@ -726,19 +726,19 @@ func (first *TfheCiphertext) executeTernaryCiphertextOperation(lhs *TfheCipherte if ret != 0 { return nil, errors.New("4 bit binary op serialization failed") } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint8: - lhs_ptr := C.deserialize_fhe_uint8(toDynamicBufferView((lhs.serialization))) + lhs_ptr := C.deserialize_fhe_uint8(toDynamicBufferView((lhs.Serialization))) if lhs_ptr == nil { return nil, errors.New("8 bit binary op deserialization failed") } - rhs_ptr := C.deserialize_fhe_uint8(toDynamicBufferView((rhs.serialization))) + rhs_ptr := C.deserialize_fhe_uint8(toDynamicBufferView((rhs.Serialization))) if rhs_ptr == nil { C.destroy_fhe_uint8(lhs_ptr) return nil, errors.New("8 bit binary op deserialization failed") } - first_ptr := C.deserialize_fhe_bool(toDynamicBufferView((first.serialization))) + first_ptr := C.deserialize_fhe_bool(toDynamicBufferView((first.Serialization))) if first_ptr == nil { C.destroy_fhe_uint8(lhs_ptr) C.destroy_fhe_uint8(rhs_ptr) @@ -755,19 +755,19 @@ func (first *TfheCiphertext) executeTernaryCiphertextOperation(lhs *TfheCipherte if ret != 0 { return nil, errors.New("8 bit binary op serialization failed") } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint16: - lhs_ptr := C.deserialize_fhe_uint16(toDynamicBufferView((lhs.serialization))) + lhs_ptr := C.deserialize_fhe_uint16(toDynamicBufferView((lhs.Serialization))) if lhs_ptr == nil { return nil, errors.New("16 bit binary op deserialization failed") } - rhs_ptr := C.deserialize_fhe_uint16(toDynamicBufferView((rhs.serialization))) + rhs_ptr := C.deserialize_fhe_uint16(toDynamicBufferView((rhs.Serialization))) if rhs_ptr == nil { C.destroy_fhe_uint16(lhs_ptr) return nil, errors.New("16 bit binary op deserialization failed") } - first_ptr := C.deserialize_fhe_bool(toDynamicBufferView((first.serialization))) + first_ptr := C.deserialize_fhe_bool(toDynamicBufferView((first.Serialization))) if first_ptr == nil { C.destroy_fhe_uint16(lhs_ptr) C.destroy_fhe_uint16(rhs_ptr) @@ -784,19 +784,19 @@ func (first *TfheCiphertext) executeTernaryCiphertextOperation(lhs *TfheCipherte if ret != 0 { return nil, errors.New("16 bit binary op serialization failed") } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint32: - lhs_ptr := C.deserialize_fhe_uint32(toDynamicBufferView((lhs.serialization))) + lhs_ptr := C.deserialize_fhe_uint32(toDynamicBufferView((lhs.Serialization))) if lhs_ptr == nil { return nil, errors.New("32 bit binary op deserialization failed") } - rhs_ptr := C.deserialize_fhe_uint32(toDynamicBufferView((rhs.serialization))) + rhs_ptr := C.deserialize_fhe_uint32(toDynamicBufferView((rhs.Serialization))) if rhs_ptr == nil { C.destroy_fhe_uint32(lhs_ptr) return nil, errors.New("32 bit binary op deserialization failed") } - first_ptr := C.deserialize_fhe_bool(toDynamicBufferView((first.serialization))) + first_ptr := C.deserialize_fhe_bool(toDynamicBufferView((first.Serialization))) if first_ptr == nil { C.destroy_fhe_uint32(lhs_ptr) C.destroy_fhe_uint32(rhs_ptr) @@ -813,19 +813,19 @@ func (first *TfheCiphertext) executeTernaryCiphertextOperation(lhs *TfheCipherte if ret != 0 { return nil, errors.New("32 bit binary op serialization failed") } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint64: - lhs_ptr := C.deserialize_fhe_uint64(toDynamicBufferView((lhs.serialization))) + lhs_ptr := C.deserialize_fhe_uint64(toDynamicBufferView((lhs.Serialization))) if lhs_ptr == nil { return nil, errors.New("64 bit binary op deserialization failed") } - rhs_ptr := C.deserialize_fhe_uint64(toDynamicBufferView((rhs.serialization))) + rhs_ptr := C.deserialize_fhe_uint64(toDynamicBufferView((rhs.Serialization))) if rhs_ptr == nil { C.destroy_fhe_uint64(lhs_ptr) return nil, errors.New("64 bit binary op deserialization failed") } - first_ptr := C.deserialize_fhe_bool(toDynamicBufferView((first.serialization))) + first_ptr := C.deserialize_fhe_bool(toDynamicBufferView((first.Serialization))) if first_ptr == nil { C.destroy_fhe_uint64(lhs_ptr) C.destroy_fhe_uint64(rhs_ptr) @@ -842,7 +842,7 @@ func (first *TfheCiphertext) executeTernaryCiphertextOperation(lhs *TfheCipherte if ret != 0 { return nil, errors.New("64 bit binary op serialization failed") } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) default: panic("ternary op unexpected ciphertext type") @@ -861,14 +861,14 @@ func (lhs *TfheCiphertext) executeBinaryScalarOperation(rhs uint64, returnBool bool) (*TfheCiphertext, error) { res := new(TfheCiphertext) if(returnBool) { - res.fheUintType = FheBool + res.FheUintType = FheBool } else { - res.fheUintType = lhs.fheUintType + res.FheUintType = lhs.FheUintType } res_ser := &C.DynamicBuffer{} - switch lhs.fheUintType { + switch lhs.FheUintType { case FheBool: - lhs_ptr := C.deserialize_fhe_bool(toDynamicBufferView((lhs.serialization))) + lhs_ptr := C.deserialize_fhe_bool(toDynamicBufferView((lhs.Serialization))) if lhs_ptr == nil { return nil, errors.New("Bool scalar op deserialization failed") } @@ -886,10 +886,10 @@ func (lhs *TfheCiphertext) executeBinaryScalarOperation(rhs uint64, if ret != 0 { return nil, errors.New("Bool scalar op serialization failed") } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint4: - lhs_ptr := C.deserialize_fhe_uint4(toDynamicBufferView((lhs.serialization))) + lhs_ptr := C.deserialize_fhe_uint4(toDynamicBufferView((lhs.Serialization))) if lhs_ptr == nil { return nil, errors.New("4 bit scalar op deserialization failed") } @@ -915,10 +915,10 @@ func (lhs *TfheCiphertext) executeBinaryScalarOperation(rhs uint64, return nil, errors.New("4 bit scalar op serialization failed") } } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint8: - lhs_ptr := C.deserialize_fhe_uint8(toDynamicBufferView((lhs.serialization))) + lhs_ptr := C.deserialize_fhe_uint8(toDynamicBufferView((lhs.Serialization))) if lhs_ptr == nil { return nil, errors.New("8 bit scalar op deserialization failed") } @@ -944,10 +944,10 @@ func (lhs *TfheCiphertext) executeBinaryScalarOperation(rhs uint64, return nil, errors.New("8 bit scalar op serialization failed") } } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint16: - lhs_ptr := C.deserialize_fhe_uint16(toDynamicBufferView((lhs.serialization))) + lhs_ptr := C.deserialize_fhe_uint16(toDynamicBufferView((lhs.Serialization))) if lhs_ptr == nil { return nil, errors.New("16 bit scalar op deserialization failed") } @@ -973,10 +973,10 @@ func (lhs *TfheCiphertext) executeBinaryScalarOperation(rhs uint64, return nil, errors.New("16 bit scalar op serialization failed") } } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint32: - lhs_ptr := C.deserialize_fhe_uint32(toDynamicBufferView((lhs.serialization))) + lhs_ptr := C.deserialize_fhe_uint32(toDynamicBufferView((lhs.Serialization))) if lhs_ptr == nil { return nil, errors.New("32 bit scalar op deserialization failed") } @@ -1002,10 +1002,10 @@ func (lhs *TfheCiphertext) executeBinaryScalarOperation(rhs uint64, return nil, errors.New("32 bit scalar op serialization failed") } } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) case FheUint64: - lhs_ptr := C.deserialize_fhe_uint64(toDynamicBufferView((lhs.serialization))) + lhs_ptr := C.deserialize_fhe_uint64(toDynamicBufferView((lhs.Serialization))) if lhs_ptr == nil { return nil, errors.New("64 bit scalar op deserialization failed") } @@ -1031,7 +1031,7 @@ func (lhs *TfheCiphertext) executeBinaryScalarOperation(rhs uint64, return nil, errors.New("64 bit scalar op serialization failed") } } - res.serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) + res.Serialization = C.GoBytes(unsafe.Pointer(res_ser.pointer), C.int(res_ser.length)) C.destroy_dynamic_buffer(res_ser) default: panic("scalar op unexpected ciphertext type") @@ -1729,18 +1729,18 @@ func (condition *TfheCiphertext) IfThenElse(lhs *TfheCiphertext, rhs *TfheCipher } func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error) { - if ct.fheUintType == castToType { + if ct.FheUintType == castToType { return nil, errors.New("casting to same type is not supported") } res := new(TfheCiphertext) - res.fheUintType = castToType + res.FheUintType = castToType - switch ct.fheUintType { + switch ct.FheUintType { case FheBool: switch castToType { case FheUint4: - from_ptr := C.deserialize_fhe_bool(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_bool(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheBool ciphertext") } @@ -1750,13 +1750,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheBool to FheUint8") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint4(to_ptr) if err != nil { return nil, err } case FheUint8: - from_ptr := C.deserialize_fhe_bool(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_bool(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheBool ciphertext") } @@ -1766,13 +1766,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheBool to FheUint8") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint8(to_ptr) if err != nil { return nil, err } case FheUint16: - from_ptr := C.deserialize_fhe_bool(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_bool(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheBool ciphertext") } @@ -1782,13 +1782,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheBool to FheUint16") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint16(to_ptr) if err != nil { return nil, err } case FheUint32: - from_ptr := C.deserialize_fhe_bool(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_bool(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheBool ciphertext") } @@ -1798,13 +1798,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheBool to FheUint32") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint32(to_ptr) if err != nil { return nil, err } case FheUint64: - from_ptr := C.deserialize_fhe_bool(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_bool(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheBool ciphertext") } @@ -1814,7 +1814,7 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheBool to FheUint64") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint64(to_ptr) if err != nil { return nil, err @@ -1825,7 +1825,7 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error case FheUint4: switch castToType { case FheUint8: - from_ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint4 ciphertext") } @@ -1835,13 +1835,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint4 to FheUint16") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint8(to_ptr) if err != nil { return nil, err } case FheUint16: - from_ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint4 ciphertext") } @@ -1851,13 +1851,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint4 to FheUint16") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint16(to_ptr) if err != nil { return nil, err } case FheUint32: - from_ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint4 ciphertext") } @@ -1867,13 +1867,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint4 to FheUint32") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint32(to_ptr) if err != nil { return nil, err } case FheUint64: - from_ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint4 ciphertext") } @@ -1883,7 +1883,7 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint4 to FheUint64") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint64(to_ptr) if err != nil { return nil, err @@ -1894,7 +1894,7 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error case FheUint8: switch castToType { case FheUint4: - from_ptr := C.deserialize_fhe_uint8(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint8(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint8 ciphertext") } @@ -1904,13 +1904,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint8 to FheUint4") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint4(to_ptr) if err != nil { return nil, err } case FheUint16: - from_ptr := C.deserialize_fhe_uint8(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint8(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint8 ciphertext") } @@ -1920,13 +1920,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint8 to FheUint16") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint16(to_ptr) if err != nil { return nil, err } case FheUint32: - from_ptr := C.deserialize_fhe_uint8(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint8(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint8 ciphertext") } @@ -1936,13 +1936,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint8 to FheUint32") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint32(to_ptr) if err != nil { return nil, err } case FheUint64: - from_ptr := C.deserialize_fhe_uint8(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint8(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint8 ciphertext") } @@ -1952,7 +1952,7 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint8 to FheUint64") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint64(to_ptr) if err != nil { return nil, err @@ -1963,7 +1963,7 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error case FheUint16: switch castToType { case FheUint4: - from_ptr := C.deserialize_fhe_uint16(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint16(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint16 ciphertext") } @@ -1973,13 +1973,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint16 to FheUint4") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint4(to_ptr) if err != nil { return nil, err } case FheUint8: - from_ptr := C.deserialize_fhe_uint16(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint16(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint16 ciphertext") } @@ -1989,13 +1989,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint16 to FheUint8") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint8(to_ptr) if err != nil { return nil, err } case FheUint32: - from_ptr := C.deserialize_fhe_uint16(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint16(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint16 ciphertext") } @@ -2005,13 +2005,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint16 to FheUint32") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint32(to_ptr) if err != nil { return nil, err } case FheUint64: - from_ptr := C.deserialize_fhe_uint16(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint16(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint16 ciphertext") } @@ -2021,7 +2021,7 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint16 to FheUint64") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint64(to_ptr) if err != nil { return nil, err @@ -2032,7 +2032,7 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error case FheUint32: switch castToType { case FheUint4: - from_ptr := C.deserialize_fhe_uint32(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint32(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint32 ciphertext") } @@ -2042,13 +2042,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint32 to FheUint4") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint4(to_ptr) if err != nil { return nil, err } case FheUint8: - from_ptr := C.deserialize_fhe_uint32(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint32(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint32 ciphertext") } @@ -2058,13 +2058,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint32 to FheUint8") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint8(to_ptr) if err != nil { return nil, err } case FheUint16: - from_ptr := C.deserialize_fhe_uint32(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint32(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint32 ciphertext") } @@ -2074,13 +2074,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint32 to FheUint16") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint16(to_ptr) if err != nil { return nil, err } case FheUint64: - from_ptr := C.deserialize_fhe_uint32(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint32(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint32 ciphertext") } @@ -2090,7 +2090,7 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint32 to FheUint64") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint64(to_ptr) if err != nil { return nil, err @@ -2101,7 +2101,7 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error case FheUint64: switch castToType { case FheUint4: - from_ptr := C.deserialize_fhe_uint64(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint64(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint64 ciphertext") } @@ -2111,13 +2111,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint64 to FheUint4") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint4(to_ptr) if err != nil { return nil, err } case FheUint8: - from_ptr := C.deserialize_fhe_uint64(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint64(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint64 ciphertext") } @@ -2127,13 +2127,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint64 to FheUint8") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint8(to_ptr) if err != nil { return nil, err } case FheUint16: - from_ptr := C.deserialize_fhe_uint64(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint64(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint64 ciphertext") } @@ -2143,13 +2143,13 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint64 to FheUint16") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint16(to_ptr) if err != nil { return nil, err } case FheUint32: - from_ptr := C.deserialize_fhe_uint64(toDynamicBufferView(ct.serialization)) + from_ptr := C.deserialize_fhe_uint64(toDynamicBufferView(ct.Serialization)) if from_ptr == nil { return nil, errors.New("castTo failed to deserialize FheUint64 ciphertext") } @@ -2159,7 +2159,7 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error return nil, errors.New("castTo failed to cast FheUint64 to FheUint32") } var err error - res.serialization, err = serialize(to_ptr, castToType) + res.Serialization, err = serialize(to_ptr, castToType) C.destroy_fhe_uint32(to_ptr) if err != nil { return nil, err @@ -2178,9 +2178,9 @@ func (ct *TfheCiphertext) Decrypt() (big.Int, error) { } var value uint64 var ret C.int - switch ct.fheUintType { + switch ct.FheUintType { case FheBool: - ptr := C.deserialize_fhe_bool(toDynamicBufferView(ct.serialization)) + ptr := C.deserialize_fhe_bool(toDynamicBufferView(ct.Serialization)) if ptr == nil { return *new(big.Int).SetUint64(0), errors.New("failed to deserialize FheBool") } @@ -2193,7 +2193,7 @@ func (ct *TfheCiphertext) Decrypt() (big.Int, error) { value = 0 } case FheUint4: - ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.serialization)) + ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.Serialization)) if ptr == nil { return *new(big.Int).SetUint64(0), errors.New("failed to deserialize FheUint4") } @@ -2202,7 +2202,7 @@ func (ct *TfheCiphertext) Decrypt() (big.Int, error) { C.destroy_fhe_uint4(ptr) value = uint64(result) case FheUint8: - ptr := C.deserialize_fhe_uint8(toDynamicBufferView(ct.serialization)) + ptr := C.deserialize_fhe_uint8(toDynamicBufferView(ct.Serialization)) if ptr == nil { return *new(big.Int).SetUint64(0), errors.New("failed to deserialize FheUint8") } @@ -2211,7 +2211,7 @@ func (ct *TfheCiphertext) Decrypt() (big.Int, error) { C.destroy_fhe_uint8(ptr) value = uint64(result) case FheUint16: - ptr := C.deserialize_fhe_uint16(toDynamicBufferView(ct.serialization)) + ptr := C.deserialize_fhe_uint16(toDynamicBufferView(ct.Serialization)) if ptr == nil { return *new(big.Int).SetUint64(0), errors.New("failed to deserialize FheUint16") } @@ -2220,7 +2220,7 @@ func (ct *TfheCiphertext) Decrypt() (big.Int, error) { C.destroy_fhe_uint16(ptr) value = uint64(result) case FheUint32: - ptr := C.deserialize_fhe_uint32(toDynamicBufferView(ct.serialization)) + ptr := C.deserialize_fhe_uint32(toDynamicBufferView(ct.Serialization)) if ptr == nil { return *new(big.Int).SetUint64(0), errors.New("failed to deserialize FheUint32") } @@ -2229,7 +2229,7 @@ func (ct *TfheCiphertext) Decrypt() (big.Int, error) { C.destroy_fhe_uint32(ptr) value = uint64(result) case FheUint64: - ptr := C.deserialize_fhe_uint64(toDynamicBufferView(ct.serialization)) + ptr := C.deserialize_fhe_uint64(toDynamicBufferView(ct.Serialization)) if ptr == nil { return *new(big.Int).SetUint64(0), errors.New("failed to deserialize FheUint64") } @@ -2247,14 +2247,14 @@ func (ct *TfheCiphertext) Decrypt() (big.Int, error) { } func (ct *TfheCiphertext) computeHash() { - hash := common.BytesToHash(crypto.Keccak256(ct.serialization)) - ct.hash = &hash + hash := common.BytesToHash(crypto.Keccak256(ct.Serialization)) + ct.Hash = &hash } func (ct *TfheCiphertext) GetHash() common.Hash { - if ct.hash != nil { - return *ct.hash + if ct.Hash != nil { + return *ct.Hash } ct.computeHash() - return *ct.hash + return *ct.Hash } diff --git a/fhevm/tfhe_key_management.go b/tfhe/tfhe_key_management.go similarity index 74% rename from fhevm/tfhe_key_management.go rename to tfhe/tfhe_key_management.go index 0f2045e..b079285 100644 --- a/fhevm/tfhe_key_management.go +++ b/tfhe/tfhe_key_management.go @@ -1,4 +1,4 @@ -package fhevm +package tfhe /* #include "tfhe_wrappers.h" @@ -17,10 +17,10 @@ import ( ) // Expanded TFHE ciphertext sizes by type, in bytes. -var expandedFheCiphertextSize map[FheUintType]uint +var ExpandedFheCiphertextSize map[FheUintType]uint func GetExpandedFheCiphertextSize(t FheUintType) (size uint, found bool) { - size, found = expandedFheCiphertextSize[t] + size, found = ExpandedFheCiphertextSize[t] return } @@ -53,32 +53,32 @@ func generateFhevmKeys() (unsafe.Pointer, unsafe.Pointer, unsafe.Pointer) { return keys.sks, keys.cks, keys.pks } -func allGlobalKeysPresent() bool { +func AllGlobalKeysPresent() bool { return sks != nil && cks != nil && pks != nil } -func initGlobalKeysWithNewKeys() { +func InitGlobalKeysWithNewKeys() { sks, cks, pks = generateFhevmKeys() initCiphertextSizes() } func initCiphertextSizes() { - expandedFheCiphertextSize = make(map[FheUintType]uint) + ExpandedFheCiphertextSize = make(map[FheUintType]uint) compactFheCiphertextSize = make(map[FheUintType]uint) - expandedFheCiphertextSize[FheBool] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheBool).Serialize())) - expandedFheCiphertextSize[FheUint4] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheUint4).Serialize())) - expandedFheCiphertextSize[FheUint8] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheUint8).Serialize())) - expandedFheCiphertextSize[FheUint16] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheUint16).Serialize())) - expandedFheCiphertextSize[FheUint32] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheUint32).Serialize())) - expandedFheCiphertextSize[FheUint64] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheUint64).Serialize())) - - compactFheCiphertextSize[FheBool] = uint(len(encryptAndSerializeCompact(0, FheBool))) - compactFheCiphertextSize[FheUint4] = uint(len(encryptAndSerializeCompact(0, FheUint4))) - compactFheCiphertextSize[FheUint8] = uint(len(encryptAndSerializeCompact(0, FheUint8))) - compactFheCiphertextSize[FheUint16] = uint(len(encryptAndSerializeCompact(0, FheUint16))) - compactFheCiphertextSize[FheUint32] = uint(len(encryptAndSerializeCompact(0, FheUint32))) - compactFheCiphertextSize[FheUint64] = uint(len(encryptAndSerializeCompact(0, FheUint64))) + ExpandedFheCiphertextSize[FheBool] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheBool).Serialize())) + ExpandedFheCiphertextSize[FheUint4] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheUint4).Serialize())) + ExpandedFheCiphertextSize[FheUint8] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheUint8).Serialize())) + ExpandedFheCiphertextSize[FheUint16] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheUint16).Serialize())) + ExpandedFheCiphertextSize[FheUint32] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheUint32).Serialize())) + ExpandedFheCiphertextSize[FheUint64] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheUint64).Serialize())) + + compactFheCiphertextSize[FheBool] = uint(len(EncryptAndSerializeCompact(0, FheBool))) + compactFheCiphertextSize[FheUint4] = uint(len(EncryptAndSerializeCompact(0, FheUint4))) + compactFheCiphertextSize[FheUint8] = uint(len(EncryptAndSerializeCompact(0, FheUint8))) + compactFheCiphertextSize[FheUint16] = uint(len(EncryptAndSerializeCompact(0, FheUint16))) + compactFheCiphertextSize[FheUint32] = uint(len(EncryptAndSerializeCompact(0, FheUint32))) + compactFheCiphertextSize[FheUint64] = uint(len(EncryptAndSerializeCompact(0, FheUint64))) } func InitGlobalKeysFromFiles(keysDir string) error { diff --git a/fhevm/tfhe_test.go b/tfhe/tfhe_test.go similarity index 99% rename from fhevm/tfhe_test.go rename to tfhe/tfhe_test.go index f3eba01..167440c 100644 --- a/fhevm/tfhe_test.go +++ b/tfhe/tfhe_test.go @@ -1,4 +1,4 @@ -package fhevm +package tfhe import ( "bytes" @@ -11,9 +11,9 @@ import ( // generate keys if not present func setup() { - if !allGlobalKeysPresent() { + if !AllGlobalKeysPresent() { fmt.Println("INFO: initializing global keys in tests") - initGlobalKeysWithNewKeys() + InitGlobalKeysWithNewKeys() } } @@ -117,7 +117,7 @@ func TfheSerializeDeserializeCompact(t *testing.T, fheUintType FheUintType) { val = 13333377777777777 } - ser := encryptAndSerializeCompact(val, fheUintType) + ser := EncryptAndSerializeCompact(val, fheUintType) ct1 := new(TfheCiphertext) err := ct1.DeserializeCompact(ser, fheUintType) if err != nil { @@ -198,7 +198,7 @@ func TfheDeserializeCompact(t *testing.T, fheUintType FheUintType) { case FheUint64: val = 13333377777777777 } - ser := encryptAndSerializeCompact(val, fheUintType) + ser := EncryptAndSerializeCompact(val, fheUintType) ct := new(TfheCiphertext) err := ct.DeserializeCompact(ser, fheUintType) if err != nil { @@ -1331,8 +1331,8 @@ func TfheCast(t *testing.T, fheUintTypeFrom FheUintType, fheUintTypeTo FheUintTy t.Fatal(err) } - if ctRes.fheUintType != fheUintTypeTo { - t.Fatalf("type %d != type %d", ctA.fheUintType, fheUintTypeTo) + if ctRes.FheUintType != fheUintTypeTo { + t.Fatalf("type %d != type %d", ctA.FheUintType, fheUintTypeTo) } res, err := ctRes.Decrypt() expected := a.Uint64() % modulus diff --git a/fhevm/tfhe_wrappers.c b/tfhe/tfhe_wrappers.c similarity index 100% rename from fhevm/tfhe_wrappers.c rename to tfhe/tfhe_wrappers.c diff --git a/fhevm/tfhe_wrappers.go b/tfhe/tfhe_wrappers.go similarity index 90% rename from fhevm/tfhe_wrappers.go rename to tfhe/tfhe_wrappers.go index 4d6b509..4228f2e 100644 --- a/fhevm/tfhe_wrappers.go +++ b/tfhe/tfhe_wrappers.go @@ -1,8 +1,8 @@ -package fhevm +package tfhe /* #cgo linux CFLAGS: -O3 -I../tfhe-rs/target/release -I../tfhe-rs/target/release/deps -#cgo linux LDFLAGS: -L../tfhe-rs/target/release -l:libtfhe.a -L../tfhe-rs/target/release/deps -l:libtfhe_c_api_dynamic_buffer.a -lm +#cgo linux LDFLAGS: -L../tfhe-rs/target/release -l:liba -L../tfhe-rs/target/release/deps -l:libtfhe_c_api_dynamic_buffer.a -lm #cgo darwin CFLAGS: -O3 -I../tfhe-rs/target/release -I../tfhe-rs/target/release/deps #cgo darwin LDFLAGS: -framework Security -L../tfhe-rs/target/release -ltfhe -L../tfhe-rs/target/release/deps -ltfhe_c_api_dynamic_buffer -lm @@ -51,7 +51,7 @@ func serialize(ptr unsafe.Pointer, t FheUintType) ([]byte, error) { return ser, nil } -func serializePublicKey() ([]byte, error) { +func SerializePublicKey() ([]byte, error) { if pks == nil { return nil, errors.New("serialize: no public key available") } @@ -65,7 +65,7 @@ func serializePublicKey() ([]byte, error) { return ser, nil } -func encryptAndSerializeCompact(value uint64, fheUintType FheUintType) []byte { +func EncryptAndSerializeCompact(value uint64, fheUintType FheUintType) []byte { out := &C.DynamicBuffer{} switch fheUintType { case FheBool: diff --git a/fhevm/tfhe_wrappers.h b/tfhe/tfhe_wrappers.h similarity index 100% rename from fhevm/tfhe_wrappers.h rename to tfhe/tfhe_wrappers.h From 9095289b1f2622816eb636fa97c95e8dbd841d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Tue, 5 Mar 2024 04:07:15 +0100 Subject: [PATCH 3/9] chore: move packages --- Makefile | 4 ++-- {crypto => packages/crypto}/crypto.go | 0 {fhevm => packages/fhevm}/contract.go | 0 {fhevm => packages/fhevm}/contracts_test.go | 0 {fhevm => packages/fhevm}/errors.go | 0 {fhevm => packages/fhevm}/evm.go | 0 {fhevm => packages/fhevm}/fhelib.go | 0 {fhevm => packages/fhevm}/instructions.go | 0 {fhevm => packages/fhevm}/instructions_test.go | 0 {fhevm => packages/fhevm}/interface.go | 0 {fhevm => packages/fhevm}/interpreter.go | 0 {fhevm => packages/fhevm}/memory.go | 0 {fhevm => packages/fhevm}/operators_arithmetic.go | 0 {fhevm => packages/fhevm}/operators_arithmetic_gas.go | 0 {fhevm => packages/fhevm}/operators_bit.go | 0 {fhevm => packages/fhevm}/operators_bit_gas.go | 0 {fhevm => packages/fhevm}/operators_comparison.go | 0 {fhevm => packages/fhevm}/operators_comparison_gas.go | 0 {fhevm => packages/fhevm}/operators_crypto.go | 0 {fhevm => packages/fhevm}/operators_crypto_gas.go | 0 {fhevm => packages/fhevm}/operators_rand.go | 0 {fhevm => packages/fhevm}/operators_rand_gas.go | 0 {fhevm => packages/fhevm}/otel.go | 0 {fhevm => packages/fhevm}/params.go | 0 {fhevm => packages/fhevm}/precompiles.go | 0 {fhevm => packages/fhevm}/protected_storage.go | 0 {fhevm => packages/fhevm}/stack.go | 0 go.mod => packages/go.mod | 0 go.sum => packages/go.sum | 0 {kms => packages/kms}/constants.go | 0 {kms => packages/kms}/kms.pb.go | 0 {kms => packages/kms}/kms_grpc.pb.go | 0 {tfhe => packages/tfhe}/tfhe_ciphertext.go | 0 {tfhe => packages/tfhe}/tfhe_key_management.go | 0 {tfhe => packages/tfhe}/tfhe_test.go | 0 {tfhe => packages/tfhe}/tfhe_wrappers.c | 0 {tfhe => packages/tfhe}/tfhe_wrappers.go | 8 ++++---- {tfhe => packages/tfhe}/tfhe_wrappers.h | 0 38 files changed, 6 insertions(+), 6 deletions(-) rename {crypto => packages/crypto}/crypto.go (100%) rename {fhevm => packages/fhevm}/contract.go (100%) rename {fhevm => packages/fhevm}/contracts_test.go (100%) rename {fhevm => packages/fhevm}/errors.go (100%) rename {fhevm => packages/fhevm}/evm.go (100%) rename {fhevm => packages/fhevm}/fhelib.go (100%) rename {fhevm => packages/fhevm}/instructions.go (100%) rename {fhevm => packages/fhevm}/instructions_test.go (100%) rename {fhevm => packages/fhevm}/interface.go (100%) rename {fhevm => packages/fhevm}/interpreter.go (100%) rename {fhevm => packages/fhevm}/memory.go (100%) rename {fhevm => packages/fhevm}/operators_arithmetic.go (100%) rename {fhevm => packages/fhevm}/operators_arithmetic_gas.go (100%) rename {fhevm => packages/fhevm}/operators_bit.go (100%) rename {fhevm => packages/fhevm}/operators_bit_gas.go (100%) rename {fhevm => packages/fhevm}/operators_comparison.go (100%) rename {fhevm => packages/fhevm}/operators_comparison_gas.go (100%) rename {fhevm => packages/fhevm}/operators_crypto.go (100%) rename {fhevm => packages/fhevm}/operators_crypto_gas.go (100%) rename {fhevm => packages/fhevm}/operators_rand.go (100%) rename {fhevm => packages/fhevm}/operators_rand_gas.go (100%) rename {fhevm => packages/fhevm}/otel.go (100%) rename {fhevm => packages/fhevm}/params.go (100%) rename {fhevm => packages/fhevm}/precompiles.go (100%) rename {fhevm => packages/fhevm}/protected_storage.go (100%) rename {fhevm => packages/fhevm}/stack.go (100%) rename go.mod => packages/go.mod (100%) rename go.sum => packages/go.sum (100%) rename {kms => packages/kms}/constants.go (100%) rename {kms => packages/kms}/kms.pb.go (100%) rename {kms => packages/kms}/kms_grpc.pb.go (100%) rename {tfhe => packages/tfhe}/tfhe_ciphertext.go (100%) rename {tfhe => packages/tfhe}/tfhe_key_management.go (100%) rename {tfhe => packages/tfhe}/tfhe_test.go (100%) rename {tfhe => packages/tfhe}/tfhe_wrappers.c (100%) rename {tfhe => packages/tfhe}/tfhe_wrappers.go (83%) rename {tfhe => packages/tfhe}/tfhe_wrappers.h (100%) diff --git a/Makefile b/Makefile index 0cd736f..885b5f2 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ .PHONY: build build: build-tfhe-rs-capi - cd fhevm && go build . + cd packages/fhevm && go build . .PHONY: test test: build-tfhe-rs-capi - cd fhevm && go test -v . \ + cd packages/fhevm && go test -v . \ && cd ../tfhe && go test -v . .PHONY: build-tfhe-rs-capi diff --git a/crypto/crypto.go b/packages/crypto/crypto.go similarity index 100% rename from crypto/crypto.go rename to packages/crypto/crypto.go diff --git a/fhevm/contract.go b/packages/fhevm/contract.go similarity index 100% rename from fhevm/contract.go rename to packages/fhevm/contract.go diff --git a/fhevm/contracts_test.go b/packages/fhevm/contracts_test.go similarity index 100% rename from fhevm/contracts_test.go rename to packages/fhevm/contracts_test.go diff --git a/fhevm/errors.go b/packages/fhevm/errors.go similarity index 100% rename from fhevm/errors.go rename to packages/fhevm/errors.go diff --git a/fhevm/evm.go b/packages/fhevm/evm.go similarity index 100% rename from fhevm/evm.go rename to packages/fhevm/evm.go diff --git a/fhevm/fhelib.go b/packages/fhevm/fhelib.go similarity index 100% rename from fhevm/fhelib.go rename to packages/fhevm/fhelib.go diff --git a/fhevm/instructions.go b/packages/fhevm/instructions.go similarity index 100% rename from fhevm/instructions.go rename to packages/fhevm/instructions.go diff --git a/fhevm/instructions_test.go b/packages/fhevm/instructions_test.go similarity index 100% rename from fhevm/instructions_test.go rename to packages/fhevm/instructions_test.go diff --git a/fhevm/interface.go b/packages/fhevm/interface.go similarity index 100% rename from fhevm/interface.go rename to packages/fhevm/interface.go diff --git a/fhevm/interpreter.go b/packages/fhevm/interpreter.go similarity index 100% rename from fhevm/interpreter.go rename to packages/fhevm/interpreter.go diff --git a/fhevm/memory.go b/packages/fhevm/memory.go similarity index 100% rename from fhevm/memory.go rename to packages/fhevm/memory.go diff --git a/fhevm/operators_arithmetic.go b/packages/fhevm/operators_arithmetic.go similarity index 100% rename from fhevm/operators_arithmetic.go rename to packages/fhevm/operators_arithmetic.go diff --git a/fhevm/operators_arithmetic_gas.go b/packages/fhevm/operators_arithmetic_gas.go similarity index 100% rename from fhevm/operators_arithmetic_gas.go rename to packages/fhevm/operators_arithmetic_gas.go diff --git a/fhevm/operators_bit.go b/packages/fhevm/operators_bit.go similarity index 100% rename from fhevm/operators_bit.go rename to packages/fhevm/operators_bit.go diff --git a/fhevm/operators_bit_gas.go b/packages/fhevm/operators_bit_gas.go similarity index 100% rename from fhevm/operators_bit_gas.go rename to packages/fhevm/operators_bit_gas.go diff --git a/fhevm/operators_comparison.go b/packages/fhevm/operators_comparison.go similarity index 100% rename from fhevm/operators_comparison.go rename to packages/fhevm/operators_comparison.go diff --git a/fhevm/operators_comparison_gas.go b/packages/fhevm/operators_comparison_gas.go similarity index 100% rename from fhevm/operators_comparison_gas.go rename to packages/fhevm/operators_comparison_gas.go diff --git a/fhevm/operators_crypto.go b/packages/fhevm/operators_crypto.go similarity index 100% rename from fhevm/operators_crypto.go rename to packages/fhevm/operators_crypto.go diff --git a/fhevm/operators_crypto_gas.go b/packages/fhevm/operators_crypto_gas.go similarity index 100% rename from fhevm/operators_crypto_gas.go rename to packages/fhevm/operators_crypto_gas.go diff --git a/fhevm/operators_rand.go b/packages/fhevm/operators_rand.go similarity index 100% rename from fhevm/operators_rand.go rename to packages/fhevm/operators_rand.go diff --git a/fhevm/operators_rand_gas.go b/packages/fhevm/operators_rand_gas.go similarity index 100% rename from fhevm/operators_rand_gas.go rename to packages/fhevm/operators_rand_gas.go diff --git a/fhevm/otel.go b/packages/fhevm/otel.go similarity index 100% rename from fhevm/otel.go rename to packages/fhevm/otel.go diff --git a/fhevm/params.go b/packages/fhevm/params.go similarity index 100% rename from fhevm/params.go rename to packages/fhevm/params.go diff --git a/fhevm/precompiles.go b/packages/fhevm/precompiles.go similarity index 100% rename from fhevm/precompiles.go rename to packages/fhevm/precompiles.go diff --git a/fhevm/protected_storage.go b/packages/fhevm/protected_storage.go similarity index 100% rename from fhevm/protected_storage.go rename to packages/fhevm/protected_storage.go diff --git a/fhevm/stack.go b/packages/fhevm/stack.go similarity index 100% rename from fhevm/stack.go rename to packages/fhevm/stack.go diff --git a/go.mod b/packages/go.mod similarity index 100% rename from go.mod rename to packages/go.mod diff --git a/go.sum b/packages/go.sum similarity index 100% rename from go.sum rename to packages/go.sum diff --git a/kms/constants.go b/packages/kms/constants.go similarity index 100% rename from kms/constants.go rename to packages/kms/constants.go diff --git a/kms/kms.pb.go b/packages/kms/kms.pb.go similarity index 100% rename from kms/kms.pb.go rename to packages/kms/kms.pb.go diff --git a/kms/kms_grpc.pb.go b/packages/kms/kms_grpc.pb.go similarity index 100% rename from kms/kms_grpc.pb.go rename to packages/kms/kms_grpc.pb.go diff --git a/tfhe/tfhe_ciphertext.go b/packages/tfhe/tfhe_ciphertext.go similarity index 100% rename from tfhe/tfhe_ciphertext.go rename to packages/tfhe/tfhe_ciphertext.go diff --git a/tfhe/tfhe_key_management.go b/packages/tfhe/tfhe_key_management.go similarity index 100% rename from tfhe/tfhe_key_management.go rename to packages/tfhe/tfhe_key_management.go diff --git a/tfhe/tfhe_test.go b/packages/tfhe/tfhe_test.go similarity index 100% rename from tfhe/tfhe_test.go rename to packages/tfhe/tfhe_test.go diff --git a/tfhe/tfhe_wrappers.c b/packages/tfhe/tfhe_wrappers.c similarity index 100% rename from tfhe/tfhe_wrappers.c rename to packages/tfhe/tfhe_wrappers.c diff --git a/tfhe/tfhe_wrappers.go b/packages/tfhe/tfhe_wrappers.go similarity index 83% rename from tfhe/tfhe_wrappers.go rename to packages/tfhe/tfhe_wrappers.go index 4228f2e..f713d6b 100644 --- a/tfhe/tfhe_wrappers.go +++ b/packages/tfhe/tfhe_wrappers.go @@ -1,10 +1,10 @@ package tfhe /* -#cgo linux CFLAGS: -O3 -I../tfhe-rs/target/release -I../tfhe-rs/target/release/deps -#cgo linux LDFLAGS: -L../tfhe-rs/target/release -l:liba -L../tfhe-rs/target/release/deps -l:libtfhe_c_api_dynamic_buffer.a -lm -#cgo darwin CFLAGS: -O3 -I../tfhe-rs/target/release -I../tfhe-rs/target/release/deps -#cgo darwin LDFLAGS: -framework Security -L../tfhe-rs/target/release -ltfhe -L../tfhe-rs/target/release/deps -ltfhe_c_api_dynamic_buffer -lm +#cgo linux CFLAGS: -O3 -I../../tfhe-rs/target/release -I../../tfhe-rs/target/release/deps +#cgo linux LDFLAGS: -L../../tfhe-rs/target/release -l:liba -L../../tfhe-rs/target/release/deps -l:libtfhe_c_api_dynamic_buffer.a -lm +#cgo darwin CFLAGS: -O3 -I../../tfhe-rs/target/release -I../../tfhe-rs/target/release/deps +#cgo darwin LDFLAGS: -framework Security -L../../tfhe-rs/target/release -ltfhe -L../../tfhe-rs/target/release/deps -ltfhe_c_api_dynamic_buffer -lm #include "tfhe_wrappers.h" diff --git a/tfhe/tfhe_wrappers.h b/packages/tfhe/tfhe_wrappers.h similarity index 100% rename from tfhe/tfhe_wrappers.h rename to packages/tfhe/tfhe_wrappers.h From c28dd9b0f1be14e326157009ffed68d20da0f2c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Tue, 5 Mar 2024 11:48:40 +0100 Subject: [PATCH 4/9] fix: typo --- packages/go.mod | 2 +- packages/tfhe/tfhe_wrappers.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/go.mod b/packages/go.mod index 23b4884..0209c2e 100644 --- a/packages/go.mod +++ b/packages/go.mod @@ -6,6 +6,7 @@ require ( github.com/ethereum/go-ethereum v1.12.0 github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c go.opentelemetry.io/otel v1.23.1 + go.opentelemetry.io/otel/trace v1.23.1 golang.org/x/crypto v0.16.0 ) @@ -13,7 +14,6 @@ require ( github.com/go-logr/logr v1.4.1 // indirect github.com/go-logr/stdr v1.2.2 // indirect go.opentelemetry.io/otel/metric v1.23.1 // indirect - go.opentelemetry.io/otel/trace v1.23.1 // indirect golang.org/x/net v0.14.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect ) diff --git a/packages/tfhe/tfhe_wrappers.go b/packages/tfhe/tfhe_wrappers.go index f713d6b..c546e7b 100644 --- a/packages/tfhe/tfhe_wrappers.go +++ b/packages/tfhe/tfhe_wrappers.go @@ -2,7 +2,7 @@ package tfhe /* #cgo linux CFLAGS: -O3 -I../../tfhe-rs/target/release -I../../tfhe-rs/target/release/deps -#cgo linux LDFLAGS: -L../../tfhe-rs/target/release -l:liba -L../../tfhe-rs/target/release/deps -l:libtfhe_c_api_dynamic_buffer.a -lm +#cgo linux LDFLAGS: -L../../tfhe-rs/target/release -l:libtfhe.a -L../../tfhe-rs/target/release/deps -l:libtfhe_c_api_dynamic_buffer.a -lm #cgo darwin CFLAGS: -O3 -I../../tfhe-rs/target/release -I../../tfhe-rs/target/release/deps #cgo darwin LDFLAGS: -framework Security -L../../tfhe-rs/target/release -ltfhe -L../../tfhe-rs/target/release/deps -ltfhe_c_api_dynamic_buffer -lm From 1b958f6cb797d04658f47938cd005e489d67a6a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Tue, 5 Mar 2024 13:26:46 +0100 Subject: [PATCH 5/9] fix: move go.mod at root --- Makefile | 4 ++-- packages/go.mod => go.mod | 0 packages/go.sum => go.sum | 0 {packages => pkg}/crypto/crypto.go | 0 {packages => pkg}/fhevm/contract.go | 0 {packages => pkg}/fhevm/contracts_test.go | 2 +- {packages => pkg}/fhevm/errors.go | 0 {packages => pkg}/fhevm/evm.go | 4 ++-- {packages => pkg}/fhevm/fhelib.go | 0 {packages => pkg}/fhevm/instructions.go | 4 ++-- {packages => pkg}/fhevm/instructions_test.go | 4 ++-- {packages => pkg}/fhevm/interface.go | 2 +- {packages => pkg}/fhevm/interpreter.go | 2 +- {packages => pkg}/fhevm/memory.go | 0 {packages => pkg}/fhevm/operators_arithmetic.go | 0 {packages => pkg}/fhevm/operators_arithmetic_gas.go | 0 {packages => pkg}/fhevm/operators_bit.go | 0 {packages => pkg}/fhevm/operators_bit_gas.go | 0 {packages => pkg}/fhevm/operators_comparison.go | 2 +- {packages => pkg}/fhevm/operators_comparison_gas.go | 2 +- {packages => pkg}/fhevm/operators_crypto.go | 4 ++-- {packages => pkg}/fhevm/operators_crypto_gas.go | 2 +- {packages => pkg}/fhevm/operators_rand.go | 4 ++-- {packages => pkg}/fhevm/operators_rand_gas.go | 2 +- {packages => pkg}/fhevm/otel.go | 2 +- {packages => pkg}/fhevm/params.go | 2 +- {packages => pkg}/fhevm/precompiles.go | 0 {packages => pkg}/fhevm/protected_storage.go | 4 ++-- {packages => pkg}/fhevm/stack.go | 0 {packages => pkg}/kms/constants.go | 0 {packages => pkg}/kms/kms.pb.go | 0 {packages => pkg}/kms/kms_grpc.pb.go | 0 {packages => pkg}/tfhe/tfhe_ciphertext.go | 0 {packages => pkg}/tfhe/tfhe_key_management.go | 0 {packages => pkg}/tfhe/tfhe_test.go | 0 {packages => pkg}/tfhe/tfhe_wrappers.c | 0 {packages => pkg}/tfhe/tfhe_wrappers.go | 0 {packages => pkg}/tfhe/tfhe_wrappers.h | 0 proto/kms.proto | 2 +- 39 files changed, 24 insertions(+), 24 deletions(-) rename packages/go.mod => go.mod (100%) rename packages/go.sum => go.sum (100%) rename {packages => pkg}/crypto/crypto.go (100%) rename {packages => pkg}/fhevm/contract.go (100%) rename {packages => pkg}/fhevm/contracts_test.go (99%) rename {packages => pkg}/fhevm/errors.go (100%) rename {packages => pkg}/fhevm/evm.go (97%) rename {packages => pkg}/fhevm/fhelib.go (100%) rename {packages => pkg}/fhevm/instructions.go (98%) rename {packages => pkg}/fhevm/instructions_test.go (99%) rename {packages => pkg}/fhevm/interface.go (98%) rename {packages => pkg}/fhevm/interpreter.go (98%) rename {packages => pkg}/fhevm/memory.go (100%) rename {packages => pkg}/fhevm/operators_arithmetic.go (100%) rename {packages => pkg}/fhevm/operators_arithmetic_gas.go (100%) rename {packages => pkg}/fhevm/operators_bit.go (100%) rename {packages => pkg}/fhevm/operators_bit_gas.go (100%) rename {packages => pkg}/fhevm/operators_comparison.go (99%) rename {packages => pkg}/fhevm/operators_comparison_gas.go (99%) rename {packages => pkg}/fhevm/operators_crypto.go (99%) rename {packages => pkg}/fhevm/operators_crypto_gas.go (99%) rename {packages => pkg}/fhevm/operators_rand.go (98%) rename {packages => pkg}/fhevm/operators_rand_gas.go (97%) rename {packages => pkg}/fhevm/otel.go (96%) rename {packages => pkg}/fhevm/params.go (99%) rename {packages => pkg}/fhevm/precompiles.go (100%) rename {packages => pkg}/fhevm/protected_storage.go (98%) rename {packages => pkg}/fhevm/stack.go (100%) rename {packages => pkg}/kms/constants.go (100%) rename {packages => pkg}/kms/kms.pb.go (100%) rename {packages => pkg}/kms/kms_grpc.pb.go (100%) rename {packages => pkg}/tfhe/tfhe_ciphertext.go (100%) rename {packages => pkg}/tfhe/tfhe_key_management.go (100%) rename {packages => pkg}/tfhe/tfhe_test.go (100%) rename {packages => pkg}/tfhe/tfhe_wrappers.c (100%) rename {packages => pkg}/tfhe/tfhe_wrappers.go (100%) rename {packages => pkg}/tfhe/tfhe_wrappers.h (100%) diff --git a/Makefile b/Makefile index 885b5f2..934ca4d 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ .PHONY: build build: build-tfhe-rs-capi - cd packages/fhevm && go build . + cd pkg/fhevm && go build . .PHONY: test test: build-tfhe-rs-capi - cd packages/fhevm && go test -v . \ + cd pkg/fhevm && go test -v . \ && cd ../tfhe && go test -v . .PHONY: build-tfhe-rs-capi diff --git a/packages/go.mod b/go.mod similarity index 100% rename from packages/go.mod rename to go.mod diff --git a/packages/go.sum b/go.sum similarity index 100% rename from packages/go.sum rename to go.sum diff --git a/packages/crypto/crypto.go b/pkg/crypto/crypto.go similarity index 100% rename from packages/crypto/crypto.go rename to pkg/crypto/crypto.go diff --git a/packages/fhevm/contract.go b/pkg/fhevm/contract.go similarity index 100% rename from packages/fhevm/contract.go rename to pkg/fhevm/contract.go diff --git a/packages/fhevm/contracts_test.go b/pkg/fhevm/contracts_test.go similarity index 99% rename from packages/fhevm/contracts_test.go rename to pkg/fhevm/contracts_test.go index 6a0d6a5..31c2ca3 100644 --- a/packages/fhevm/contracts_test.go +++ b/pkg/fhevm/contracts_test.go @@ -15,7 +15,7 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" - "github.com/zama-ai/fhevm-go/tfhe" + "github.com/zama-ai/fhevm-go/pkg/tfhe" ) // generate keys if not present diff --git a/packages/fhevm/errors.go b/pkg/fhevm/errors.go similarity index 100% rename from packages/fhevm/errors.go rename to pkg/fhevm/errors.go diff --git a/packages/fhevm/evm.go b/pkg/fhevm/evm.go similarity index 97% rename from packages/fhevm/evm.go rename to pkg/fhevm/evm.go index dc0f1a9..b2901f2 100644 --- a/packages/fhevm/evm.go +++ b/pkg/fhevm/evm.go @@ -7,8 +7,8 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" - fhevm_crypto "github.com/zama-ai/fhevm-go/crypto" - "github.com/zama-ai/fhevm-go/tfhe" + fhevm_crypto "github.com/zama-ai/fhevm-go/pkg/crypto" + "github.com/zama-ai/fhevm-go/pkg/tfhe" ) var protectedStorageAddrCallerAddr common.Address diff --git a/packages/fhevm/fhelib.go b/pkg/fhevm/fhelib.go similarity index 100% rename from packages/fhevm/fhelib.go rename to pkg/fhevm/fhelib.go diff --git a/packages/fhevm/instructions.go b/pkg/fhevm/instructions.go similarity index 98% rename from packages/fhevm/instructions.go rename to pkg/fhevm/instructions.go index 2ea94b1..0bc9281 100644 --- a/packages/fhevm/instructions.go +++ b/pkg/fhevm/instructions.go @@ -8,8 +8,8 @@ import ( "github.com/ethereum/go-ethereum/common" crypto "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" - fhevm_crypto "github.com/zama-ai/fhevm-go/crypto" - "github.com/zama-ai/fhevm-go/tfhe" + fhevm_crypto "github.com/zama-ai/fhevm-go/pkg/crypto" + "github.com/zama-ai/fhevm-go/pkg/tfhe" "go.opentelemetry.io/otel" ) diff --git a/packages/fhevm/instructions_test.go b/pkg/fhevm/instructions_test.go similarity index 99% rename from packages/fhevm/instructions_test.go rename to pkg/fhevm/instructions_test.go index 3a35155..fcf323c 100644 --- a/packages/fhevm/instructions_test.go +++ b/pkg/fhevm/instructions_test.go @@ -13,8 +13,8 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" - fhevm_crypto "github.com/zama-ai/fhevm-go/crypto" - "github.com/zama-ai/fhevm-go/tfhe" + fhevm_crypto "github.com/zama-ai/fhevm-go/pkg/crypto" + "github.com/zama-ai/fhevm-go/pkg/tfhe" ) func init() { diff --git a/packages/fhevm/interface.go b/pkg/fhevm/interface.go similarity index 98% rename from packages/fhevm/interface.go rename to pkg/fhevm/interface.go index 1698576..9d9522c 100644 --- a/packages/fhevm/interface.go +++ b/pkg/fhevm/interface.go @@ -6,7 +6,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/holiman/uint256" - "github.com/zama-ai/fhevm-go/tfhe" + "github.com/zama-ai/fhevm-go/pkg/tfhe" ) type EVMEnvironment interface { diff --git a/packages/fhevm/interpreter.go b/pkg/fhevm/interpreter.go similarity index 98% rename from packages/fhevm/interpreter.go rename to pkg/fhevm/interpreter.go index ae7a6bf..dfb7c7c 100644 --- a/packages/fhevm/interpreter.go +++ b/pkg/fhevm/interpreter.go @@ -2,7 +2,7 @@ package fhevm import ( "github.com/ethereum/go-ethereum/common" - "github.com/zama-ai/fhevm-go/tfhe" + "github.com/zama-ai/fhevm-go/pkg/tfhe" ) type ScopeContext interface { diff --git a/packages/fhevm/memory.go b/pkg/fhevm/memory.go similarity index 100% rename from packages/fhevm/memory.go rename to pkg/fhevm/memory.go diff --git a/packages/fhevm/operators_arithmetic.go b/pkg/fhevm/operators_arithmetic.go similarity index 100% rename from packages/fhevm/operators_arithmetic.go rename to pkg/fhevm/operators_arithmetic.go diff --git a/packages/fhevm/operators_arithmetic_gas.go b/pkg/fhevm/operators_arithmetic_gas.go similarity index 100% rename from packages/fhevm/operators_arithmetic_gas.go rename to pkg/fhevm/operators_arithmetic_gas.go diff --git a/packages/fhevm/operators_bit.go b/pkg/fhevm/operators_bit.go similarity index 100% rename from packages/fhevm/operators_bit.go rename to pkg/fhevm/operators_bit.go diff --git a/packages/fhevm/operators_bit_gas.go b/pkg/fhevm/operators_bit_gas.go similarity index 100% rename from packages/fhevm/operators_bit_gas.go rename to pkg/fhevm/operators_bit_gas.go diff --git a/packages/fhevm/operators_comparison.go b/pkg/fhevm/operators_comparison.go similarity index 99% rename from packages/fhevm/operators_comparison.go rename to pkg/fhevm/operators_comparison.go index e3a67c8..22a6b24 100644 --- a/packages/fhevm/operators_comparison.go +++ b/pkg/fhevm/operators_comparison.go @@ -5,7 +5,7 @@ import ( "errors" "github.com/ethereum/go-ethereum/common" - "github.com/zama-ai/fhevm-go/tfhe" + "github.com/zama-ai/fhevm-go/pkg/tfhe" "go.opentelemetry.io/otel/trace" ) diff --git a/packages/fhevm/operators_comparison_gas.go b/pkg/fhevm/operators_comparison_gas.go similarity index 99% rename from packages/fhevm/operators_comparison_gas.go rename to pkg/fhevm/operators_comparison_gas.go index cd9f320..33076b6 100644 --- a/packages/fhevm/operators_comparison_gas.go +++ b/pkg/fhevm/operators_comparison_gas.go @@ -3,7 +3,7 @@ package fhevm import ( "encoding/hex" - "github.com/zama-ai/fhevm-go/tfhe" + "github.com/zama-ai/fhevm-go/pkg/tfhe" ) func fheLeRequiredGas(environment EVMEnvironment, input []byte) uint64 { diff --git a/packages/fhevm/operators_crypto.go b/pkg/fhevm/operators_crypto.go similarity index 99% rename from packages/fhevm/operators_crypto.go rename to pkg/fhevm/operators_crypto.go index f5717af..34b287f 100644 --- a/packages/fhevm/operators_crypto.go +++ b/pkg/fhevm/operators_crypto.go @@ -10,8 +10,8 @@ import ( "time" "github.com/ethereum/go-ethereum/common" - "github.com/zama-ai/fhevm-go/kms" - "github.com/zama-ai/fhevm-go/tfhe" + "github.com/zama-ai/fhevm-go/pkg/kms" + "github.com/zama-ai/fhevm-go/pkg/tfhe" "go.opentelemetry.io/otel/trace" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" diff --git a/packages/fhevm/operators_crypto_gas.go b/pkg/fhevm/operators_crypto_gas.go similarity index 99% rename from packages/fhevm/operators_crypto_gas.go rename to pkg/fhevm/operators_crypto_gas.go index d38adb6..e02ac53 100644 --- a/packages/fhevm/operators_crypto_gas.go +++ b/pkg/fhevm/operators_crypto_gas.go @@ -4,7 +4,7 @@ import ( "encoding/hex" "github.com/ethereum/go-ethereum/common" - "github.com/zama-ai/fhevm-go/tfhe" + "github.com/zama-ai/fhevm-go/pkg/tfhe" ) func verifyCiphertextRequiredGas(environment EVMEnvironment, input []byte) uint64 { diff --git a/packages/fhevm/operators_rand.go b/pkg/fhevm/operators_rand.go similarity index 98% rename from packages/fhevm/operators_rand.go rename to pkg/fhevm/operators_rand.go index 7620e9b..9795983 100644 --- a/packages/fhevm/operators_rand.go +++ b/pkg/fhevm/operators_rand.go @@ -11,8 +11,8 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" - fhevm_crypto "github.com/zama-ai/fhevm-go/crypto" - "github.com/zama-ai/fhevm-go/tfhe" + fhevm_crypto "github.com/zama-ai/fhevm-go/pkg/crypto" + "github.com/zama-ai/fhevm-go/pkg/tfhe" "go.opentelemetry.io/otel/trace" "golang.org/x/crypto/chacha20" ) diff --git a/packages/fhevm/operators_rand_gas.go b/pkg/fhevm/operators_rand_gas.go similarity index 97% rename from packages/fhevm/operators_rand_gas.go rename to pkg/fhevm/operators_rand_gas.go index 455a012..fc921fc 100644 --- a/packages/fhevm/operators_rand_gas.go +++ b/pkg/fhevm/operators_rand_gas.go @@ -6,7 +6,7 @@ import ( "math/bits" "github.com/holiman/uint256" - "github.com/zama-ai/fhevm-go/tfhe" + "github.com/zama-ai/fhevm-go/pkg/tfhe" ) func fheRandRequiredGas(environment EVMEnvironment, input []byte) uint64 { diff --git a/packages/fhevm/otel.go b/pkg/fhevm/otel.go similarity index 96% rename from packages/fhevm/otel.go rename to pkg/fhevm/otel.go index 147122d..73f21c0 100644 --- a/packages/fhevm/otel.go +++ b/pkg/fhevm/otel.go @@ -3,7 +3,7 @@ package fhevm import ( "math/big" - "github.com/zama-ai/fhevm-go/tfhe" + "github.com/zama-ai/fhevm-go/pkg/tfhe" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" ) diff --git a/packages/fhevm/params.go b/pkg/fhevm/params.go similarity index 99% rename from packages/fhevm/params.go rename to pkg/fhevm/params.go index d0ec2a4..45a6b40 100644 --- a/packages/fhevm/params.go +++ b/pkg/fhevm/params.go @@ -1,6 +1,6 @@ package fhevm -import "github.com/zama-ai/fhevm-go/tfhe" +import "github.com/zama-ai/fhevm-go/pkg/tfhe" // This file contains default gas costs of fhEVM-related operations. // Users can change the values based on specific requirements in their blockchain. diff --git a/packages/fhevm/precompiles.go b/pkg/fhevm/precompiles.go similarity index 100% rename from packages/fhevm/precompiles.go rename to pkg/fhevm/precompiles.go diff --git a/packages/fhevm/protected_storage.go b/pkg/fhevm/protected_storage.go similarity index 98% rename from packages/fhevm/protected_storage.go rename to pkg/fhevm/protected_storage.go index a709122..b0f3f0d 100644 --- a/packages/fhevm/protected_storage.go +++ b/pkg/fhevm/protected_storage.go @@ -7,8 +7,8 @@ import ( "github.com/ethereum/go-ethereum/common" crypto "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" - fhevm_crypto "github.com/zama-ai/fhevm-go/crypto" - "github.com/zama-ai/fhevm-go/tfhe" + fhevm_crypto "github.com/zama-ai/fhevm-go/pkg/crypto" + "github.com/zama-ai/fhevm-go/pkg/tfhe" ) // An arbitrary constant value to flag locations in protected storage. diff --git a/packages/fhevm/stack.go b/pkg/fhevm/stack.go similarity index 100% rename from packages/fhevm/stack.go rename to pkg/fhevm/stack.go diff --git a/packages/kms/constants.go b/pkg/kms/constants.go similarity index 100% rename from packages/kms/constants.go rename to pkg/kms/constants.go diff --git a/packages/kms/kms.pb.go b/pkg/kms/kms.pb.go similarity index 100% rename from packages/kms/kms.pb.go rename to pkg/kms/kms.pb.go diff --git a/packages/kms/kms_grpc.pb.go b/pkg/kms/kms_grpc.pb.go similarity index 100% rename from packages/kms/kms_grpc.pb.go rename to pkg/kms/kms_grpc.pb.go diff --git a/packages/tfhe/tfhe_ciphertext.go b/pkg/tfhe/tfhe_ciphertext.go similarity index 100% rename from packages/tfhe/tfhe_ciphertext.go rename to pkg/tfhe/tfhe_ciphertext.go diff --git a/packages/tfhe/tfhe_key_management.go b/pkg/tfhe/tfhe_key_management.go similarity index 100% rename from packages/tfhe/tfhe_key_management.go rename to pkg/tfhe/tfhe_key_management.go diff --git a/packages/tfhe/tfhe_test.go b/pkg/tfhe/tfhe_test.go similarity index 100% rename from packages/tfhe/tfhe_test.go rename to pkg/tfhe/tfhe_test.go diff --git a/packages/tfhe/tfhe_wrappers.c b/pkg/tfhe/tfhe_wrappers.c similarity index 100% rename from packages/tfhe/tfhe_wrappers.c rename to pkg/tfhe/tfhe_wrappers.c diff --git a/packages/tfhe/tfhe_wrappers.go b/pkg/tfhe/tfhe_wrappers.go similarity index 100% rename from packages/tfhe/tfhe_wrappers.go rename to pkg/tfhe/tfhe_wrappers.go diff --git a/packages/tfhe/tfhe_wrappers.h b/pkg/tfhe/tfhe_wrappers.h similarity index 100% rename from packages/tfhe/tfhe_wrappers.h rename to pkg/tfhe/tfhe_wrappers.h diff --git a/proto/kms.proto b/proto/kms.proto index dff572e..2cd6a98 100644 --- a/proto/kms.proto +++ b/proto/kms.proto @@ -1,6 +1,6 @@ syntax = "proto3"; package kms; -option go_package = "github.com/zama-ai/fhevm-go/kms"; +option go_package = "github.com/zama-ai/fhevm-go/pkg/kms"; service KmsEndpoint { rpc Validate_and_decrypt(DecryptionRequest) returns (DecryptionResponse); From 3af30de4f68fe8d82cc51ba9455c8744137ac37d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Tue, 5 Mar 2024 13:30:38 +0100 Subject: [PATCH 6/9] chore: format --- pkg/fhevm/operators_arithmetic.go | 1 - pkg/fhevm/operators_arithmetic_gas.go | 2 +- pkg/fhevm/operators_bit.go | 2 - pkg/fhevm/operators_bit_gas.go | 1 - pkg/fhevm/operators_comparison.go | 3 - pkg/fhevm/operators_comparison_gas.go | 2 - pkg/fhevm/operators_crypto.go | 1 - pkg/fhevm/params.go | 8 +- pkg/kms/constants.go | 2 +- pkg/tfhe/tfhe_ciphertext.go | 236 +++++++++++++------------- pkg/tfhe/tfhe_key_management.go | 2 +- pkg/tfhe/tfhe_wrappers.go | 2 +- 12 files changed, 126 insertions(+), 136 deletions(-) diff --git a/pkg/fhevm/operators_arithmetic.go b/pkg/fhevm/operators_arithmetic.go index 904b5f1..87c99e9 100644 --- a/pkg/fhevm/operators_arithmetic.go +++ b/pkg/fhevm/operators_arithmetic.go @@ -8,7 +8,6 @@ import ( "go.opentelemetry.io/otel/trace" ) - func fheAddRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { input = input[:minInt(65, len(input))] diff --git a/pkg/fhevm/operators_arithmetic_gas.go b/pkg/fhevm/operators_arithmetic_gas.go index 015a848..33916cc 100644 --- a/pkg/fhevm/operators_arithmetic_gas.go +++ b/pkg/fhevm/operators_arithmetic_gas.go @@ -108,4 +108,4 @@ func fheRemRequiredGas(environment EVMEnvironment, input []byte) uint64 { } return environment.FhevmParams().GasCosts.FheScalarRem[lhs.fheUintType()] } -} \ No newline at end of file +} diff --git a/pkg/fhevm/operators_bit.go b/pkg/fhevm/operators_bit.go index 2af1956..502f3e7 100644 --- a/pkg/fhevm/operators_bit.go +++ b/pkg/fhevm/operators_bit.go @@ -140,7 +140,6 @@ func fheShrRun(environment EVMEnvironment, caller common.Address, addr common.Ad } } - func fheNegRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { input = input[:minInt(32, len(input))] @@ -215,7 +214,6 @@ func fheNotRun(environment EVMEnvironment, caller common.Address, addr common.Ad return resultHash[:], nil } - func fheBitAndRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { input = input[:minInt(65, len(input))] diff --git a/pkg/fhevm/operators_bit_gas.go b/pkg/fhevm/operators_bit_gas.go index b8e77fb..bf9ea00 100644 --- a/pkg/fhevm/operators_bit_gas.go +++ b/pkg/fhevm/operators_bit_gas.go @@ -42,7 +42,6 @@ func fheShrRequiredGas(environment EVMEnvironment, input []byte) uint64 { return fheShlRequiredGas(environment, input) } - func fheNegRequiredGas(environment EVMEnvironment, input []byte) uint64 { input = input[:minInt(32, len(input))] diff --git a/pkg/fhevm/operators_comparison.go b/pkg/fhevm/operators_comparison.go index 22a6b24..48b335a 100644 --- a/pkg/fhevm/operators_comparison.go +++ b/pkg/fhevm/operators_comparison.go @@ -9,7 +9,6 @@ import ( "go.opentelemetry.io/otel/trace" ) - func fheLeRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { input = input[:minInt(65, len(input))] @@ -406,7 +405,6 @@ func fheNeRun(environment EVMEnvironment, caller common.Address, addr common.Add } } - func fheMinRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { input = input[:minInt(65, len(input))] @@ -539,7 +537,6 @@ func fheMaxRun(environment EVMEnvironment, caller common.Address, addr common.Ad } } - func fheIfThenElseRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { input = input[:minInt(96, len(input))] diff --git a/pkg/fhevm/operators_comparison_gas.go b/pkg/fhevm/operators_comparison_gas.go index 33076b6..0e3fba0 100644 --- a/pkg/fhevm/operators_comparison_gas.go +++ b/pkg/fhevm/operators_comparison_gas.go @@ -86,7 +86,6 @@ func fheNeRequiredGas(environment EVMEnvironment, input []byte) uint64 { return fheEqRequiredGas(environment, input) } - func fheMinRequiredGas(environment EVMEnvironment, input []byte) uint64 { input = input[:minInt(65, len(input))] @@ -123,7 +122,6 @@ func fheMaxRequiredGas(environment EVMEnvironment, input []byte) uint64 { return fheMinRequiredGas(environment, input) } - func fheIfThenElseRequiredGas(environment EVMEnvironment, input []byte) uint64 { input = input[:minInt(96, len(input))] diff --git a/pkg/fhevm/operators_crypto.go b/pkg/fhevm/operators_crypto.go index 34b287f..5beb698 100644 --- a/pkg/fhevm/operators_crypto.go +++ b/pkg/fhevm/operators_crypto.go @@ -17,7 +17,6 @@ import ( "google.golang.org/grpc/credentials/insecure" ) - func verifyCiphertextRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) { logger := environment.GetLogger() // first 32 bytes of the payload is offset, then 32 bytes are size of byte array diff --git a/pkg/fhevm/params.go b/pkg/fhevm/params.go index 45a6b40..b18d157 100644 --- a/pkg/fhevm/params.go +++ b/pkg/fhevm/params.go @@ -84,7 +84,7 @@ func DefaultGasCosts() GasCosts { tfhe.FheUint64: 500000, }, FheBitwiseOp: map[tfhe.FheUintType]uint64{ - tfhe.FheBool: 16000 + AdjustFHEGas, + tfhe.FheBool: 16000 + AdjustFHEGas, tfhe.FheUint4: 22000 + AdjustFHEGas, tfhe.FheUint8: 24000 + AdjustFHEGas, tfhe.FheUint16: 24000 + AdjustFHEGas, @@ -177,7 +177,7 @@ func DefaultGasCosts() GasCosts { }, // TODO: Costs will depend on the complexity of doing reencryption/decryption by the oracle. FheReencrypt: map[tfhe.FheUintType]uint64{ - tfhe.FheBool: 1000, + tfhe.FheBool: 1000, tfhe.FheUint4: 1000, tfhe.FheUint8: 1000, tfhe.FheUint16: 1100, @@ -185,7 +185,7 @@ func DefaultGasCosts() GasCosts { }, // As of now, verification costs only cover ciphertext deserialization and assume there is no ZKPoK to verify. FheVerify: map[tfhe.FheUintType]uint64{ - tfhe.FheBool: 200, + tfhe.FheBool: 200, tfhe.FheUint4: 200, tfhe.FheUint8: 200, tfhe.FheUint16: 300, @@ -193,7 +193,7 @@ func DefaultGasCosts() GasCosts { tfhe.FheUint64: 800, }, FheTrivialEncrypt: map[tfhe.FheUintType]uint64{ - tfhe.FheBool: 100, + tfhe.FheBool: 100, tfhe.FheUint4: 100, tfhe.FheUint8: 100, tfhe.FheUint16: 200, diff --git a/pkg/kms/constants.go b/pkg/kms/constants.go index 108414e..5538daf 100644 --- a/pkg/kms/constants.go +++ b/pkg/kms/constants.go @@ -3,4 +3,4 @@ package kms import "os" // URL of the KMS gRPC endpoint -var KmsEndpointAddr = os.Getenv("KMS_ENDPOINT_ADDR") \ No newline at end of file +var KmsEndpointAddr = os.Getenv("KMS_ENDPOINT_ADDR") diff --git a/pkg/tfhe/tfhe_ciphertext.go b/pkg/tfhe/tfhe_ciphertext.go index 56b96c5..ad39c0b 100644 --- a/pkg/tfhe/tfhe_ciphertext.go +++ b/pkg/tfhe/tfhe_ciphertext.go @@ -17,7 +17,7 @@ import ( type FheUintType uint8 const ( - FheBool FheUintType = 0 + FheBool FheUintType = 0 FheUint4 FheUintType = 1 FheUint8 FheUintType = 2 FheUint16 FheUintType = 3 @@ -340,8 +340,8 @@ func (ct *TfheCiphertext) executeUnaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("Bool unary op deserialization failed") } res_ptr, err := opBool(ct_ptr) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_bool(ct_ptr) if res_ptr == nil { @@ -360,8 +360,8 @@ func (ct *TfheCiphertext) executeUnaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("8 bit unary op deserialization failed") } res_ptr, err := op4(ct_ptr) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_uint4(ct_ptr) if res_ptr == nil { @@ -380,8 +380,8 @@ func (ct *TfheCiphertext) executeUnaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("8 bit unary op deserialization failed") } res_ptr, err := op8(ct_ptr) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_uint8(ct_ptr) if res_ptr == nil { @@ -400,8 +400,8 @@ func (ct *TfheCiphertext) executeUnaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("16 bit unary op deserialization failed") } res_ptr, err := op16(ct_ptr) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_uint16(ct_ptr) if res_ptr == nil { @@ -420,8 +420,8 @@ func (ct *TfheCiphertext) executeUnaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("32 bit unary op deserialization failed") } res_ptr, err := op16(ct_ptr) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_uint32(ct_ptr) if res_ptr == nil { @@ -440,8 +440,8 @@ func (ct *TfheCiphertext) executeUnaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("64 bit unary op deserialization failed") } res_ptr, err := op64(ct_ptr) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_uint64(ct_ptr) if res_ptr == nil { @@ -474,7 +474,7 @@ func (lhs *TfheCiphertext) executeBinaryCiphertextOperation(rhs *TfheCiphertext, } res := new(TfheCiphertext) - if(returnBool) { + if returnBool { res.FheUintType = FheBool } else { res.FheUintType = lhs.FheUintType @@ -492,8 +492,8 @@ func (lhs *TfheCiphertext) executeBinaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("bool binary op deserialization failed") } res_ptr, err := opBool(lhs_ptr, rhs_ptr) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_bool(lhs_ptr) C.destroy_fhe_bool(rhs_ptr) @@ -518,15 +518,15 @@ func (lhs *TfheCiphertext) executeBinaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("4 bit binary op deserialization failed") } res_ptr, err := op4(lhs_ptr, rhs_ptr) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_uint4(lhs_ptr) C.destroy_fhe_uint4(rhs_ptr) if res_ptr == nil { return nil, errors.New("4 bit binary op failed") } - if (returnBool) { + if returnBool { ret := C.serialize_fhe_bool(res_ptr, res_ser) C.destroy_fhe_bool(res_ptr) if ret != 0 { @@ -552,15 +552,15 @@ func (lhs *TfheCiphertext) executeBinaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("8 bit binary op deserialization failed") } res_ptr, err := op8(lhs_ptr, rhs_ptr) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_uint8(lhs_ptr) C.destroy_fhe_uint8(rhs_ptr) if res_ptr == nil { return nil, errors.New("8 bit binary op failed") } - if (returnBool) { + if returnBool { ret := C.serialize_fhe_bool(res_ptr, res_ser) C.destroy_fhe_bool(res_ptr) if ret != 0 { @@ -586,15 +586,15 @@ func (lhs *TfheCiphertext) executeBinaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("16 bit binary op deserialization failed") } res_ptr, err := op16(lhs_ptr, rhs_ptr) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_uint16(lhs_ptr) C.destroy_fhe_uint16(rhs_ptr) if res_ptr == nil { return nil, errors.New("16 bit binary op failed") } - if (returnBool) { + if returnBool { ret := C.serialize_fhe_bool(res_ptr, res_ser) C.destroy_fhe_bool(res_ptr) if ret != 0 { @@ -620,8 +620,8 @@ func (lhs *TfheCiphertext) executeBinaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("32 bit binary op deserialization failed") } res_ptr, err := op32(lhs_ptr, rhs_ptr) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_uint32(lhs_ptr) C.destroy_fhe_uint32(rhs_ptr) @@ -629,7 +629,7 @@ func (lhs *TfheCiphertext) executeBinaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("32 bit binary op failed") } - if (returnBool) { + if returnBool { ret := C.serialize_fhe_bool(res_ptr, res_ser) C.destroy_fhe_bool(res_ptr) if ret != 0 { @@ -655,15 +655,15 @@ func (lhs *TfheCiphertext) executeBinaryCiphertextOperation(rhs *TfheCiphertext, return nil, errors.New("64 bit binary op deserialization failed") } res_ptr, err := op64(lhs_ptr, rhs_ptr) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_uint64(lhs_ptr) C.destroy_fhe_uint64(rhs_ptr) if res_ptr == nil { return nil, errors.New("64 bit binary op failed") } - if (returnBool) { + if returnBool { ret := C.serialize_fhe_bool(res_ptr, res_ser) C.destroy_fhe_bool(res_ptr) if ret != 0 { @@ -860,7 +860,7 @@ func (lhs *TfheCiphertext) executeBinaryScalarOperation(rhs uint64, op64 func(lhs unsafe.Pointer, rhs C.uint64_t) (unsafe.Pointer, error), returnBool bool) (*TfheCiphertext, error) { res := new(TfheCiphertext) - if(returnBool) { + if returnBool { res.FheUintType = FheBool } else { res.FheUintType = lhs.FheUintType @@ -874,8 +874,8 @@ func (lhs *TfheCiphertext) executeBinaryScalarOperation(rhs uint64, } scalar := C.bool(rhs == 1) res_ptr, err := opBool(lhs_ptr, scalar) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_bool(lhs_ptr) if res_ptr == nil { @@ -895,14 +895,14 @@ func (lhs *TfheCiphertext) executeBinaryScalarOperation(rhs uint64, } scalar := C.uint8_t(rhs) res_ptr, err := op4(lhs_ptr, scalar) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_uint4(lhs_ptr) if res_ptr == nil { return nil, errors.New("4 bit scalar op failed") } - if (returnBool) { + if returnBool { ret := C.serialize_fhe_bool(res_ptr, res_ser) C.destroy_fhe_bool(res_ptr) if ret != 0 { @@ -924,14 +924,14 @@ func (lhs *TfheCiphertext) executeBinaryScalarOperation(rhs uint64, } scalar := C.uint8_t(rhs) res_ptr, err := op8(lhs_ptr, scalar) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_uint8(lhs_ptr) if res_ptr == nil { return nil, errors.New("8 bit scalar op failed") } - if (returnBool) { + if returnBool { ret := C.serialize_fhe_bool(res_ptr, res_ser) C.destroy_fhe_bool(res_ptr) if ret != 0 { @@ -953,14 +953,14 @@ func (lhs *TfheCiphertext) executeBinaryScalarOperation(rhs uint64, } scalar := C.uint16_t(rhs) res_ptr, err := op16(lhs_ptr, scalar) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_uint16(lhs_ptr) if res_ptr == nil { return nil, errors.New("16 bit scalar op failed") } - if (returnBool) { + if returnBool { ret := C.serialize_fhe_bool(res_ptr, res_ser) C.destroy_fhe_bool(res_ptr) if ret != 0 { @@ -982,14 +982,14 @@ func (lhs *TfheCiphertext) executeBinaryScalarOperation(rhs uint64, } scalar := C.uint32_t(rhs) res_ptr, err := op32(lhs_ptr, scalar) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_uint32(lhs_ptr) if res_ptr == nil { return nil, errors.New("32 bit scalar op failed") } - if (returnBool) { + if returnBool { ret := C.serialize_fhe_bool(res_ptr, res_ser) C.destroy_fhe_bool(res_ptr) if ret != 0 { @@ -1011,14 +1011,14 @@ func (lhs *TfheCiphertext) executeBinaryScalarOperation(rhs uint64, } scalar := C.uint64_t(rhs) res_ptr, err := op64(lhs_ptr, scalar) - if (err != nil) { - return nil, err; + if err != nil { + return nil, err } C.destroy_fhe_uint64(lhs_ptr) if res_ptr == nil { return nil, errors.New("64 bit scalar op failed") } - if (returnBool) { + if returnBool { ret := C.serialize_fhe_bool(res_ptr, res_ser) C.destroy_fhe_bool(res_ptr) if ret != 0 { @@ -1822,75 +1822,75 @@ func (ct *TfheCiphertext) CastTo(castToType FheUintType) (*TfheCiphertext, error default: panic("castTo: unexpected type to cast to") } - case FheUint4: - switch castToType { - case FheUint8: - from_ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.Serialization)) - if from_ptr == nil { - return nil, errors.New("castTo failed to deserialize FheUint4 ciphertext") - } - to_ptr := C.cast_4_8(from_ptr, sks) - C.destroy_fhe_uint4(from_ptr) - if to_ptr == nil { - return nil, errors.New("castTo failed to cast FheUint4 to FheUint16") - } - var err error - res.Serialization, err = serialize(to_ptr, castToType) - C.destroy_fhe_uint8(to_ptr) - if err != nil { - return nil, err - } - case FheUint16: - from_ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.Serialization)) - if from_ptr == nil { - return nil, errors.New("castTo failed to deserialize FheUint4 ciphertext") - } - to_ptr := C.cast_4_16(from_ptr, sks) - C.destroy_fhe_uint4(from_ptr) - if to_ptr == nil { - return nil, errors.New("castTo failed to cast FheUint4 to FheUint16") - } - var err error - res.Serialization, err = serialize(to_ptr, castToType) - C.destroy_fhe_uint16(to_ptr) - if err != nil { - return nil, err - } - case FheUint32: - from_ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.Serialization)) - if from_ptr == nil { - return nil, errors.New("castTo failed to deserialize FheUint4 ciphertext") - } - to_ptr := C.cast_4_32(from_ptr, sks) - C.destroy_fhe_uint4(from_ptr) - if to_ptr == nil { - return nil, errors.New("castTo failed to cast FheUint4 to FheUint32") - } - var err error - res.Serialization, err = serialize(to_ptr, castToType) - C.destroy_fhe_uint32(to_ptr) - if err != nil { - return nil, err - } - case FheUint64: - from_ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.Serialization)) - if from_ptr == nil { - return nil, errors.New("castTo failed to deserialize FheUint4 ciphertext") - } - to_ptr := C.cast_4_64(from_ptr, sks) - C.destroy_fhe_uint4(from_ptr) - if to_ptr == nil { - return nil, errors.New("castTo failed to cast FheUint4 to FheUint64") - } - var err error - res.Serialization, err = serialize(to_ptr, castToType) - C.destroy_fhe_uint64(to_ptr) - if err != nil { - return nil, err - } - default: - panic("castTo: unexpected type to cast to") + case FheUint4: + switch castToType { + case FheUint8: + from_ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.Serialization)) + if from_ptr == nil { + return nil, errors.New("castTo failed to deserialize FheUint4 ciphertext") + } + to_ptr := C.cast_4_8(from_ptr, sks) + C.destroy_fhe_uint4(from_ptr) + if to_ptr == nil { + return nil, errors.New("castTo failed to cast FheUint4 to FheUint16") + } + var err error + res.Serialization, err = serialize(to_ptr, castToType) + C.destroy_fhe_uint8(to_ptr) + if err != nil { + return nil, err + } + case FheUint16: + from_ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.Serialization)) + if from_ptr == nil { + return nil, errors.New("castTo failed to deserialize FheUint4 ciphertext") } + to_ptr := C.cast_4_16(from_ptr, sks) + C.destroy_fhe_uint4(from_ptr) + if to_ptr == nil { + return nil, errors.New("castTo failed to cast FheUint4 to FheUint16") + } + var err error + res.Serialization, err = serialize(to_ptr, castToType) + C.destroy_fhe_uint16(to_ptr) + if err != nil { + return nil, err + } + case FheUint32: + from_ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.Serialization)) + if from_ptr == nil { + return nil, errors.New("castTo failed to deserialize FheUint4 ciphertext") + } + to_ptr := C.cast_4_32(from_ptr, sks) + C.destroy_fhe_uint4(from_ptr) + if to_ptr == nil { + return nil, errors.New("castTo failed to cast FheUint4 to FheUint32") + } + var err error + res.Serialization, err = serialize(to_ptr, castToType) + C.destroy_fhe_uint32(to_ptr) + if err != nil { + return nil, err + } + case FheUint64: + from_ptr := C.deserialize_fhe_uint4(toDynamicBufferView(ct.Serialization)) + if from_ptr == nil { + return nil, errors.New("castTo failed to deserialize FheUint4 ciphertext") + } + to_ptr := C.cast_4_64(from_ptr, sks) + C.destroy_fhe_uint4(from_ptr) + if to_ptr == nil { + return nil, errors.New("castTo failed to cast FheUint4 to FheUint64") + } + var err error + res.Serialization, err = serialize(to_ptr, castToType) + C.destroy_fhe_uint64(to_ptr) + if err != nil { + return nil, err + } + default: + panic("castTo: unexpected type to cast to") + } case FheUint8: switch castToType { case FheUint4: @@ -2187,7 +2187,7 @@ func (ct *TfheCiphertext) Decrypt() (big.Int, error) { var result C.bool ret = C.decrypt_fhe_bool(cks, ptr, &result) C.destroy_fhe_bool(ptr) - if (result) { + if result { value = 1 } else { value = 0 diff --git a/pkg/tfhe/tfhe_key_management.go b/pkg/tfhe/tfhe_key_management.go index b079285..9be572a 100644 --- a/pkg/tfhe/tfhe_key_management.go +++ b/pkg/tfhe/tfhe_key_management.go @@ -67,7 +67,7 @@ func initCiphertextSizes() { compactFheCiphertextSize = make(map[FheUintType]uint) ExpandedFheCiphertextSize[FheBool] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheBool).Serialize())) - ExpandedFheCiphertextSize[FheUint4] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheUint4).Serialize())) + ExpandedFheCiphertextSize[FheUint4] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheUint4).Serialize())) ExpandedFheCiphertextSize[FheUint8] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheUint8).Serialize())) ExpandedFheCiphertextSize[FheUint16] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheUint16).Serialize())) ExpandedFheCiphertextSize[FheUint32] = uint(len(new(TfheCiphertext).TrivialEncrypt(*big.NewInt(0), FheUint32).Serialize())) diff --git a/pkg/tfhe/tfhe_wrappers.go b/pkg/tfhe/tfhe_wrappers.go index c546e7b..a017be5 100644 --- a/pkg/tfhe/tfhe_wrappers.go +++ b/pkg/tfhe/tfhe_wrappers.go @@ -71,7 +71,7 @@ func EncryptAndSerializeCompact(value uint64, fheUintType FheUintType) []byte { case FheBool: val := false if value == 1 { - val = true + val = true } C.public_key_encrypt_and_serialize_fhe_bool_list(pks, C.bool(val), out) case FheUint4: From 42c283d3740ccfb4f007d343d9994c7b43e4adfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Tue, 5 Mar 2024 13:36:21 +0100 Subject: [PATCH 7/9] fix: run all tests with one command --- Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 934ca4d..117267c 100644 --- a/Makefile +++ b/Makefile @@ -4,8 +4,7 @@ build: build-tfhe-rs-capi .PHONY: test test: build-tfhe-rs-capi - cd pkg/fhevm && go test -v . \ - && cd ../tfhe && go test -v . + cd pkg && go test -v ./... .PHONY: build-tfhe-rs-capi build-tfhe-rs-capi: From 214dc8fb6dc9e7061ddd595ed5e06bac85cec34e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Tue, 5 Mar 2024 15:29:37 +0100 Subject: [PATCH 8/9] fix: move to fhevm to stay compatible --- Makefile | 2 +- {pkg/fhevm => fhevm}/contract.go | 0 {pkg/fhevm => fhevm}/contracts_test.go | 2 +- {pkg => fhevm}/crypto/crypto.go | 0 {pkg/fhevm => fhevm}/errors.go | 0 {pkg/fhevm => fhevm}/evm.go | 4 ++-- {pkg/fhevm => fhevm}/fhelib.go | 0 {pkg/fhevm => fhevm}/instructions.go | 4 ++-- {pkg/fhevm => fhevm}/instructions_test.go | 4 ++-- {pkg/fhevm => fhevm}/interface.go | 2 +- {pkg/fhevm => fhevm}/interpreter.go | 2 +- {pkg => fhevm}/kms/constants.go | 0 {pkg => fhevm}/kms/kms.pb.go | 0 {pkg => fhevm}/kms/kms_grpc.pb.go | 0 {pkg/fhevm => fhevm}/memory.go | 0 {pkg/fhevm => fhevm}/operators_arithmetic.go | 0 {pkg/fhevm => fhevm}/operators_arithmetic_gas.go | 0 {pkg/fhevm => fhevm}/operators_bit.go | 0 {pkg/fhevm => fhevm}/operators_bit_gas.go | 0 {pkg/fhevm => fhevm}/operators_comparison.go | 2 +- {pkg/fhevm => fhevm}/operators_comparison_gas.go | 2 +- {pkg/fhevm => fhevm}/operators_crypto.go | 4 ++-- {pkg/fhevm => fhevm}/operators_crypto_gas.go | 2 +- {pkg/fhevm => fhevm}/operators_rand.go | 4 ++-- {pkg/fhevm => fhevm}/operators_rand_gas.go | 2 +- {pkg/fhevm => fhevm}/otel.go | 2 +- {pkg/fhevm => fhevm}/params.go | 2 +- {pkg/fhevm => fhevm}/precompiles.go | 0 {pkg/fhevm => fhevm}/protected_storage.go | 4 ++-- {pkg/fhevm => fhevm}/stack.go | 0 {pkg => fhevm}/tfhe/tfhe_ciphertext.go | 0 {pkg => fhevm}/tfhe/tfhe_key_management.go | 0 {pkg => fhevm}/tfhe/tfhe_test.go | 0 {pkg => fhevm}/tfhe/tfhe_wrappers.c | 0 {pkg => fhevm}/tfhe/tfhe_wrappers.go | 0 {pkg => fhevm}/tfhe/tfhe_wrappers.h | 0 proto/kms.proto | 2 +- 37 files changed, 23 insertions(+), 23 deletions(-) rename {pkg/fhevm => fhevm}/contract.go (100%) rename {pkg/fhevm => fhevm}/contracts_test.go (99%) rename {pkg => fhevm}/crypto/crypto.go (100%) rename {pkg/fhevm => fhevm}/errors.go (100%) rename {pkg/fhevm => fhevm}/evm.go (97%) rename {pkg/fhevm => fhevm}/fhelib.go (100%) rename {pkg/fhevm => fhevm}/instructions.go (98%) rename {pkg/fhevm => fhevm}/instructions_test.go (99%) rename {pkg/fhevm => fhevm}/interface.go (98%) rename {pkg/fhevm => fhevm}/interpreter.go (98%) rename {pkg => fhevm}/kms/constants.go (100%) rename {pkg => fhevm}/kms/kms.pb.go (100%) rename {pkg => fhevm}/kms/kms_grpc.pb.go (100%) rename {pkg/fhevm => fhevm}/memory.go (100%) rename {pkg/fhevm => fhevm}/operators_arithmetic.go (100%) rename {pkg/fhevm => fhevm}/operators_arithmetic_gas.go (100%) rename {pkg/fhevm => fhevm}/operators_bit.go (100%) rename {pkg/fhevm => fhevm}/operators_bit_gas.go (100%) rename {pkg/fhevm => fhevm}/operators_comparison.go (99%) rename {pkg/fhevm => fhevm}/operators_comparison_gas.go (99%) rename {pkg/fhevm => fhevm}/operators_crypto.go (99%) rename {pkg/fhevm => fhevm}/operators_crypto_gas.go (99%) rename {pkg/fhevm => fhevm}/operators_rand.go (98%) rename {pkg/fhevm => fhevm}/operators_rand_gas.go (97%) rename {pkg/fhevm => fhevm}/otel.go (96%) rename {pkg/fhevm => fhevm}/params.go (99%) rename {pkg/fhevm => fhevm}/precompiles.go (100%) rename {pkg/fhevm => fhevm}/protected_storage.go (98%) rename {pkg/fhevm => fhevm}/stack.go (100%) rename {pkg => fhevm}/tfhe/tfhe_ciphertext.go (100%) rename {pkg => fhevm}/tfhe/tfhe_key_management.go (100%) rename {pkg => fhevm}/tfhe/tfhe_test.go (100%) rename {pkg => fhevm}/tfhe/tfhe_wrappers.c (100%) rename {pkg => fhevm}/tfhe/tfhe_wrappers.go (100%) rename {pkg => fhevm}/tfhe/tfhe_wrappers.h (100%) diff --git a/Makefile b/Makefile index 117267c..3b0ae7f 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ build: build-tfhe-rs-capi .PHONY: test test: build-tfhe-rs-capi - cd pkg && go test -v ./... + cd fhevm && go test -v ./... .PHONY: build-tfhe-rs-capi build-tfhe-rs-capi: diff --git a/pkg/fhevm/contract.go b/fhevm/contract.go similarity index 100% rename from pkg/fhevm/contract.go rename to fhevm/contract.go diff --git a/pkg/fhevm/contracts_test.go b/fhevm/contracts_test.go similarity index 99% rename from pkg/fhevm/contracts_test.go rename to fhevm/contracts_test.go index 31c2ca3..58203df 100644 --- a/pkg/fhevm/contracts_test.go +++ b/fhevm/contracts_test.go @@ -15,7 +15,7 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" - "github.com/zama-ai/fhevm-go/pkg/tfhe" + "github.com/zama-ai/fhevm-go/fhevm/tfhe" ) // generate keys if not present diff --git a/pkg/crypto/crypto.go b/fhevm/crypto/crypto.go similarity index 100% rename from pkg/crypto/crypto.go rename to fhevm/crypto/crypto.go diff --git a/pkg/fhevm/errors.go b/fhevm/errors.go similarity index 100% rename from pkg/fhevm/errors.go rename to fhevm/errors.go diff --git a/pkg/fhevm/evm.go b/fhevm/evm.go similarity index 97% rename from pkg/fhevm/evm.go rename to fhevm/evm.go index b2901f2..d998c78 100644 --- a/pkg/fhevm/evm.go +++ b/fhevm/evm.go @@ -7,8 +7,8 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" - fhevm_crypto "github.com/zama-ai/fhevm-go/pkg/crypto" - "github.com/zama-ai/fhevm-go/pkg/tfhe" + fhevm_crypto "github.com/zama-ai/fhevm-go/fhevm/crypto" + "github.com/zama-ai/fhevm-go/fhevm/tfhe" ) var protectedStorageAddrCallerAddr common.Address diff --git a/pkg/fhevm/fhelib.go b/fhevm/fhelib.go similarity index 100% rename from pkg/fhevm/fhelib.go rename to fhevm/fhelib.go diff --git a/pkg/fhevm/instructions.go b/fhevm/instructions.go similarity index 98% rename from pkg/fhevm/instructions.go rename to fhevm/instructions.go index 0bc9281..4732ebe 100644 --- a/pkg/fhevm/instructions.go +++ b/fhevm/instructions.go @@ -8,8 +8,8 @@ import ( "github.com/ethereum/go-ethereum/common" crypto "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" - fhevm_crypto "github.com/zama-ai/fhevm-go/pkg/crypto" - "github.com/zama-ai/fhevm-go/pkg/tfhe" + fhevm_crypto "github.com/zama-ai/fhevm-go/fhevm/crypto" + "github.com/zama-ai/fhevm-go/fhevm/tfhe" "go.opentelemetry.io/otel" ) diff --git a/pkg/fhevm/instructions_test.go b/fhevm/instructions_test.go similarity index 99% rename from pkg/fhevm/instructions_test.go rename to fhevm/instructions_test.go index fcf323c..b85f6d7 100644 --- a/pkg/fhevm/instructions_test.go +++ b/fhevm/instructions_test.go @@ -13,8 +13,8 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" - fhevm_crypto "github.com/zama-ai/fhevm-go/pkg/crypto" - "github.com/zama-ai/fhevm-go/pkg/tfhe" + fhevm_crypto "github.com/zama-ai/fhevm-go/fhevm/crypto" + "github.com/zama-ai/fhevm-go/fhevm/tfhe" ) func init() { diff --git a/pkg/fhevm/interface.go b/fhevm/interface.go similarity index 98% rename from pkg/fhevm/interface.go rename to fhevm/interface.go index 9d9522c..bc8941c 100644 --- a/pkg/fhevm/interface.go +++ b/fhevm/interface.go @@ -6,7 +6,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/holiman/uint256" - "github.com/zama-ai/fhevm-go/pkg/tfhe" + "github.com/zama-ai/fhevm-go/fhevm/tfhe" ) type EVMEnvironment interface { diff --git a/pkg/fhevm/interpreter.go b/fhevm/interpreter.go similarity index 98% rename from pkg/fhevm/interpreter.go rename to fhevm/interpreter.go index dfb7c7c..d3a1155 100644 --- a/pkg/fhevm/interpreter.go +++ b/fhevm/interpreter.go @@ -2,7 +2,7 @@ package fhevm import ( "github.com/ethereum/go-ethereum/common" - "github.com/zama-ai/fhevm-go/pkg/tfhe" + "github.com/zama-ai/fhevm-go/fhevm/tfhe" ) type ScopeContext interface { diff --git a/pkg/kms/constants.go b/fhevm/kms/constants.go similarity index 100% rename from pkg/kms/constants.go rename to fhevm/kms/constants.go diff --git a/pkg/kms/kms.pb.go b/fhevm/kms/kms.pb.go similarity index 100% rename from pkg/kms/kms.pb.go rename to fhevm/kms/kms.pb.go diff --git a/pkg/kms/kms_grpc.pb.go b/fhevm/kms/kms_grpc.pb.go similarity index 100% rename from pkg/kms/kms_grpc.pb.go rename to fhevm/kms/kms_grpc.pb.go diff --git a/pkg/fhevm/memory.go b/fhevm/memory.go similarity index 100% rename from pkg/fhevm/memory.go rename to fhevm/memory.go diff --git a/pkg/fhevm/operators_arithmetic.go b/fhevm/operators_arithmetic.go similarity index 100% rename from pkg/fhevm/operators_arithmetic.go rename to fhevm/operators_arithmetic.go diff --git a/pkg/fhevm/operators_arithmetic_gas.go b/fhevm/operators_arithmetic_gas.go similarity index 100% rename from pkg/fhevm/operators_arithmetic_gas.go rename to fhevm/operators_arithmetic_gas.go diff --git a/pkg/fhevm/operators_bit.go b/fhevm/operators_bit.go similarity index 100% rename from pkg/fhevm/operators_bit.go rename to fhevm/operators_bit.go diff --git a/pkg/fhevm/operators_bit_gas.go b/fhevm/operators_bit_gas.go similarity index 100% rename from pkg/fhevm/operators_bit_gas.go rename to fhevm/operators_bit_gas.go diff --git a/pkg/fhevm/operators_comparison.go b/fhevm/operators_comparison.go similarity index 99% rename from pkg/fhevm/operators_comparison.go rename to fhevm/operators_comparison.go index 48b335a..713f759 100644 --- a/pkg/fhevm/operators_comparison.go +++ b/fhevm/operators_comparison.go @@ -5,7 +5,7 @@ import ( "errors" "github.com/ethereum/go-ethereum/common" - "github.com/zama-ai/fhevm-go/pkg/tfhe" + "github.com/zama-ai/fhevm-go/fhevm/tfhe" "go.opentelemetry.io/otel/trace" ) diff --git a/pkg/fhevm/operators_comparison_gas.go b/fhevm/operators_comparison_gas.go similarity index 99% rename from pkg/fhevm/operators_comparison_gas.go rename to fhevm/operators_comparison_gas.go index 0e3fba0..563bfad 100644 --- a/pkg/fhevm/operators_comparison_gas.go +++ b/fhevm/operators_comparison_gas.go @@ -3,7 +3,7 @@ package fhevm import ( "encoding/hex" - "github.com/zama-ai/fhevm-go/pkg/tfhe" + "github.com/zama-ai/fhevm-go/fhevm/tfhe" ) func fheLeRequiredGas(environment EVMEnvironment, input []byte) uint64 { diff --git a/pkg/fhevm/operators_crypto.go b/fhevm/operators_crypto.go similarity index 99% rename from pkg/fhevm/operators_crypto.go rename to fhevm/operators_crypto.go index 5beb698..1f14a03 100644 --- a/pkg/fhevm/operators_crypto.go +++ b/fhevm/operators_crypto.go @@ -10,8 +10,8 @@ import ( "time" "github.com/ethereum/go-ethereum/common" - "github.com/zama-ai/fhevm-go/pkg/kms" - "github.com/zama-ai/fhevm-go/pkg/tfhe" + "github.com/zama-ai/fhevm-go/fhevm/kms" + "github.com/zama-ai/fhevm-go/fhevm/tfhe" "go.opentelemetry.io/otel/trace" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" diff --git a/pkg/fhevm/operators_crypto_gas.go b/fhevm/operators_crypto_gas.go similarity index 99% rename from pkg/fhevm/operators_crypto_gas.go rename to fhevm/operators_crypto_gas.go index e02ac53..676a549 100644 --- a/pkg/fhevm/operators_crypto_gas.go +++ b/fhevm/operators_crypto_gas.go @@ -4,7 +4,7 @@ import ( "encoding/hex" "github.com/ethereum/go-ethereum/common" - "github.com/zama-ai/fhevm-go/pkg/tfhe" + "github.com/zama-ai/fhevm-go/fhevm/tfhe" ) func verifyCiphertextRequiredGas(environment EVMEnvironment, input []byte) uint64 { diff --git a/pkg/fhevm/operators_rand.go b/fhevm/operators_rand.go similarity index 98% rename from pkg/fhevm/operators_rand.go rename to fhevm/operators_rand.go index 9795983..8252b94 100644 --- a/pkg/fhevm/operators_rand.go +++ b/fhevm/operators_rand.go @@ -11,8 +11,8 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" - fhevm_crypto "github.com/zama-ai/fhevm-go/pkg/crypto" - "github.com/zama-ai/fhevm-go/pkg/tfhe" + fhevm_crypto "github.com/zama-ai/fhevm-go/fhevm/crypto" + "github.com/zama-ai/fhevm-go/fhevm/tfhe" "go.opentelemetry.io/otel/trace" "golang.org/x/crypto/chacha20" ) diff --git a/pkg/fhevm/operators_rand_gas.go b/fhevm/operators_rand_gas.go similarity index 97% rename from pkg/fhevm/operators_rand_gas.go rename to fhevm/operators_rand_gas.go index fc921fc..8bc4986 100644 --- a/pkg/fhevm/operators_rand_gas.go +++ b/fhevm/operators_rand_gas.go @@ -6,7 +6,7 @@ import ( "math/bits" "github.com/holiman/uint256" - "github.com/zama-ai/fhevm-go/pkg/tfhe" + "github.com/zama-ai/fhevm-go/fhevm/tfhe" ) func fheRandRequiredGas(environment EVMEnvironment, input []byte) uint64 { diff --git a/pkg/fhevm/otel.go b/fhevm/otel.go similarity index 96% rename from pkg/fhevm/otel.go rename to fhevm/otel.go index 73f21c0..26e4104 100644 --- a/pkg/fhevm/otel.go +++ b/fhevm/otel.go @@ -3,7 +3,7 @@ package fhevm import ( "math/big" - "github.com/zama-ai/fhevm-go/pkg/tfhe" + "github.com/zama-ai/fhevm-go/fhevm/tfhe" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" ) diff --git a/pkg/fhevm/params.go b/fhevm/params.go similarity index 99% rename from pkg/fhevm/params.go rename to fhevm/params.go index b18d157..dc1d425 100644 --- a/pkg/fhevm/params.go +++ b/fhevm/params.go @@ -1,6 +1,6 @@ package fhevm -import "github.com/zama-ai/fhevm-go/pkg/tfhe" +import "github.com/zama-ai/fhevm-go/fhevm/tfhe" // This file contains default gas costs of fhEVM-related operations. // Users can change the values based on specific requirements in their blockchain. diff --git a/pkg/fhevm/precompiles.go b/fhevm/precompiles.go similarity index 100% rename from pkg/fhevm/precompiles.go rename to fhevm/precompiles.go diff --git a/pkg/fhevm/protected_storage.go b/fhevm/protected_storage.go similarity index 98% rename from pkg/fhevm/protected_storage.go rename to fhevm/protected_storage.go index b0f3f0d..7165200 100644 --- a/pkg/fhevm/protected_storage.go +++ b/fhevm/protected_storage.go @@ -7,8 +7,8 @@ import ( "github.com/ethereum/go-ethereum/common" crypto "github.com/ethereum/go-ethereum/crypto" "github.com/holiman/uint256" - fhevm_crypto "github.com/zama-ai/fhevm-go/pkg/crypto" - "github.com/zama-ai/fhevm-go/pkg/tfhe" + fhevm_crypto "github.com/zama-ai/fhevm-go/fhevm/crypto" + "github.com/zama-ai/fhevm-go/fhevm/tfhe" ) // An arbitrary constant value to flag locations in protected storage. diff --git a/pkg/fhevm/stack.go b/fhevm/stack.go similarity index 100% rename from pkg/fhevm/stack.go rename to fhevm/stack.go diff --git a/pkg/tfhe/tfhe_ciphertext.go b/fhevm/tfhe/tfhe_ciphertext.go similarity index 100% rename from pkg/tfhe/tfhe_ciphertext.go rename to fhevm/tfhe/tfhe_ciphertext.go diff --git a/pkg/tfhe/tfhe_key_management.go b/fhevm/tfhe/tfhe_key_management.go similarity index 100% rename from pkg/tfhe/tfhe_key_management.go rename to fhevm/tfhe/tfhe_key_management.go diff --git a/pkg/tfhe/tfhe_test.go b/fhevm/tfhe/tfhe_test.go similarity index 100% rename from pkg/tfhe/tfhe_test.go rename to fhevm/tfhe/tfhe_test.go diff --git a/pkg/tfhe/tfhe_wrappers.c b/fhevm/tfhe/tfhe_wrappers.c similarity index 100% rename from pkg/tfhe/tfhe_wrappers.c rename to fhevm/tfhe/tfhe_wrappers.c diff --git a/pkg/tfhe/tfhe_wrappers.go b/fhevm/tfhe/tfhe_wrappers.go similarity index 100% rename from pkg/tfhe/tfhe_wrappers.go rename to fhevm/tfhe/tfhe_wrappers.go diff --git a/pkg/tfhe/tfhe_wrappers.h b/fhevm/tfhe/tfhe_wrappers.h similarity index 100% rename from pkg/tfhe/tfhe_wrappers.h rename to fhevm/tfhe/tfhe_wrappers.h diff --git a/proto/kms.proto b/proto/kms.proto index 2cd6a98..7a709ba 100644 --- a/proto/kms.proto +++ b/proto/kms.proto @@ -1,6 +1,6 @@ syntax = "proto3"; package kms; -option go_package = "github.com/zama-ai/fhevm-go/pkg/kms"; +option go_package = "github.com/zama-ai/fhevm-go/fhevm/kms"; service KmsEndpoint { rpc Validate_and_decrypt(DecryptionRequest) returns (DecryptionResponse); From 7ad04112601d0e16b8efcbe0907a5ad4d9ced4c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Tue, 5 Mar 2024 15:41:56 +0100 Subject: [PATCH 9/9] fix: typo --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3b0ae7f..e82456c 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ .PHONY: build build: build-tfhe-rs-capi - cd pkg/fhevm && go build . + cd fhevm && go build . .PHONY: test test: build-tfhe-rs-capi