Skip to content
This repository has been archived by the owner on Dec 23, 2024. It is now read-only.

Commit

Permalink
fix: add more tests and various fix
Browse files Browse the repository at this point in the history
  • Loading branch information
immortal-tofu committed Feb 27, 2024
1 parent fed51ca commit 03639f0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
4 changes: 4 additions & 0 deletions fhevm/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func DefaultGasCosts() GasCosts {
FheUint64: 500000,
},
FheBitwiseOp: map[FheUintType]uint64{
FheBool: 23000 + AdjustFHEGas,
FheUint4: 24000 + AdjustFHEGas,
FheUint8: 24000 + AdjustFHEGas,
FheUint16: 24000 + AdjustFHEGas,
Expand Down Expand Up @@ -163,20 +164,23 @@ func DefaultGasCosts() GasCosts {
},
// 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,
},
// 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,
Expand Down
4 changes: 4 additions & 0 deletions fhevm/precompiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,8 @@ func reencryptRun(environment EVMEnvironment, caller common.Address, addr common

var fheType kms.FheType
switch ct.ciphertext.fheUintType {
case FheBool:
fheType = kms.FheType_Bool
case FheUint4:
fheType = kms.FheType_Euint4
case FheUint8:
Expand Down Expand Up @@ -2356,6 +2358,8 @@ func decryptValue(environment EVMEnvironment, ct *tfheCiphertext) (uint64, error
logger := environment.GetLogger()
var fheType kms.FheType
switch ct.fheUintType {
case FheBool:
fheType = kms.FheType_Bool
case FheUint4:
fheType = kms.FheType_Euint4
case FheUint8:
Expand Down
8 changes: 4 additions & 4 deletions fhevm/tfhe_ciphertext.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (ct *tfheCiphertext) deserialize(in []byte, t FheUintType) error {
case FheBool:
ptr := C.deserialize_fhe_bool(toDynamicBufferView((in)))
if ptr == nil {
return errors.New("FheUint8 ciphertext deserialization failed")
return errors.New("FheBool ciphertext deserialization failed")
}
C.destroy_fhe_bool(ptr)
case FheUint4:
Expand Down Expand Up @@ -115,7 +115,7 @@ func (ct *tfheCiphertext) deserializeCompact(in []byte, t FheUintType) error {
}
var err error
ct.serialization, err = serialize(ptr, t)
C.destroy_fhe_uint8(ptr)
C.destroy_fhe_uint4(ptr)
if err != nil {
return err
}
Expand Down Expand Up @@ -184,14 +184,14 @@ func (ct *tfheCiphertext) encrypt(value big.Int, t FheUintType) *tfheCiphertext
}
ptr = C.public_key_encrypt_fhe_bool(pks, C.bool(val))
ct.serialization, err = serialize(ptr, t)
C.destroy_fhe_uint8(ptr)
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)
C.destroy_fhe_uint8(ptr)
C.destroy_fhe_uint4(ptr)
if err != nil {
panic(err)
}
Expand Down
44 changes: 44 additions & 0 deletions fhevm/tfhe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func TestMain(m *testing.M) {
func TfheEncryptDecrypt(t *testing.T, fheUintType FheUintType) {
var val big.Int
switch fheUintType {
case FheBool:
val.SetUint64(1)
case FheUint4:
val.SetUint64(2)
case FheUint8:
Expand All @@ -47,6 +49,8 @@ func TfheEncryptDecrypt(t *testing.T, fheUintType FheUintType) {
func TfheTrivialEncryptDecrypt(t *testing.T, fheUintType FheUintType) {
var val big.Int
switch fheUintType {
case FheBool:
val.SetUint64(1)
case FheUint4:
val.SetUint64(2)
case FheUint8:
Expand All @@ -69,6 +73,8 @@ func TfheTrivialEncryptDecrypt(t *testing.T, fheUintType FheUintType) {
func TfheSerializeDeserialize(t *testing.T, fheUintType FheUintType) {
var val big.Int
switch fheUintType {
case FheBool:
val = *big.NewInt(1)
case FheUint4:
val = *big.NewInt(2)
case FheUint8:
Expand Down Expand Up @@ -97,6 +103,8 @@ func TfheSerializeDeserialize(t *testing.T, fheUintType FheUintType) {
func TfheSerializeDeserializeCompact(t *testing.T, fheUintType FheUintType) {
var val uint64
switch fheUintType {
case FheBool:
val = 1
case FheUint4:
val = 2
case FheUint8:
Expand Down Expand Up @@ -137,6 +145,8 @@ func TfheSerializeDeserializeCompact(t *testing.T, fheUintType FheUintType) {
func TfheTrivialSerializeDeserialize(t *testing.T, fheUintType FheUintType) {
var val big.Int
switch fheUintType {
case FheBool:
val = *big.NewInt(1)
case FheUint4:
val = *big.NewInt(2)
case FheUint8:
Expand Down Expand Up @@ -175,6 +185,8 @@ func TfheDeserializeFailure(t *testing.T, fheUintType FheUintType) {
func TfheDeserializeCompact(t *testing.T, fheUintType FheUintType) {
var val uint64
switch fheUintType {
case FheBool:
val = 1
case FheUint4:
val = 2
case FheUint8:
Expand Down Expand Up @@ -1331,6 +1343,10 @@ func TfheCast(t *testing.T, fheUintTypeFrom FheUintType, fheUintTypeTo FheUintTy
}
}

func TestTfheEncryptDecryptBool(t *testing.T) {
TfheEncryptDecrypt(t, FheBool)
}

func TestTfheEncryptDecrypt4(t *testing.T) {
TfheEncryptDecrypt(t, FheUint4)
}
Expand All @@ -1351,6 +1367,10 @@ func TestTfheEncryptDecrypt64(t *testing.T) {
TfheEncryptDecrypt(t, FheUint64)
}

func TestTfheTrivialEncryptDecryptBool(t *testing.T) {
TfheTrivialEncryptDecrypt(t, FheBool)
}

func TestTfheTrivialEncryptDecrypt4(t *testing.T) {
TfheTrivialEncryptDecrypt(t, FheUint4)
}
Expand All @@ -1371,6 +1391,10 @@ func TestTfheTrivialEncryptDecrypt64(t *testing.T) {
TfheTrivialEncryptDecrypt(t, FheUint64)
}

func TestTfheSerializeDeserializeBool(t *testing.T) {
TfheSerializeDeserialize(t, FheBool)
}

func TestTfheSerializeDeserialize4(t *testing.T) {
TfheSerializeDeserialize(t, FheUint4)
}
Expand All @@ -1391,6 +1415,10 @@ func TestTfheSerializeDeserialize64(t *testing.T) {
TfheSerializeDeserialize(t, FheUint64)
}

func TestTfheSerializeDeserializeCompactBool(t *testing.T) {
TfheSerializeDeserializeCompact(t, FheBool)
}

func TestTfheSerializeDeserializeCompact4(t *testing.T) {
TfheSerializeDeserializeCompact(t, FheUint4)
}
Expand All @@ -1407,6 +1435,10 @@ func TestTfheSerializeDeserializeCompact64(t *testing.T) {
TfheSerializeDeserializeCompact(t, FheUint64)
}

func TestTfheTrivialSerializeDeserializeBool(t *testing.T) {
TfheTrivialSerializeDeserialize(t, FheBool)
}

func TestTfheTrivialSerializeDeserialize4(t *testing.T) {
TfheTrivialSerializeDeserialize(t, FheUint4)
}
Expand All @@ -1427,6 +1459,10 @@ func TestTfheTrivialSerializeDeserialize64(t *testing.T) {
TfheTrivialSerializeDeserialize(t, FheUint64)
}

func TestTfheDeserializeFailureBool(t *testing.T) {
TfheDeserializeFailure(t, FheBool)
}

func TestTfheDeserializeFailure4(t *testing.T) {
TfheDeserializeFailure(t, FheUint4)
}
Expand All @@ -1447,6 +1483,10 @@ func TestTfheDeserializeFailure64(t *testing.T) {
TfheDeserializeFailure(t, FheUint64)
}

func TestTfheDeserializeCompactBool(t *testing.T) {
TfheDeserializeCompact(t, FheBool)
}

func TestTfheDeserializeCompact4(t *testing.T) {
TfheDeserializeCompact(t, FheUint4)
}
Expand All @@ -1467,6 +1507,10 @@ func TestTfheDeserializeCompact64(t *testing.T) {
TfheDeserializeCompact(t, FheUint64)
}

func TestTfheDeserializeCompactFailureBool(t *testing.T) {
TfheDeserializeCompactFailure(t, FheBool)
}

func TestTfheDeserializeCompactFailure4(t *testing.T) {
TfheDeserializeCompactFailure(t, FheUint4)
}
Expand Down

0 comments on commit 03639f0

Please sign in to comment.