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] 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