Skip to content

Commit

Permalink
Merge pull request #99 from zama-ai/feature/eaddress
Browse files Browse the repository at this point in the history
Feature/eaddress
  • Loading branch information
leventdem authored Mar 18, 2024
2 parents a3167be + 70acdcc commit 5876042
Show file tree
Hide file tree
Showing 15 changed files with 683 additions and 158 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ replace(
## Regenerate protobuff files

To re-generate these files, install `protoc`, `protoc-gen-go` and `protoc-gen-go-grpc` and run protoc
`cd proto && protoc --go_out=../kms --go_opt=paths=source_relative --go-grpc_out=../kms --go-grpc_opt=paths=source_relative kms.proto && cd ..`.
`cd proto && protoc --go_out=../fhevm/kms --go_opt=paths=source_relative --go-grpc_out=../fhevm/kms --go-grpc_opt=paths=source_relative kms.proto && cd ..`.

## Documentation

Expand Down
90 changes: 49 additions & 41 deletions fhevm/kms/kms.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion fhevm/kms/kms_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions fhevm/operators_arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func fheAddRun(environment EVMEnvironment, caller common.Address, addr common.Ad
return importRandomCiphertext(environment, lhs.fheUintType()), nil
}

result, err := lhs.ciphertext.ScalarAdd(rhs.Uint64())
result, err := lhs.ciphertext.ScalarAdd(rhs)
if err != nil {
logger.Error("fheAdd failed", "err", err)
return nil, err
Expand Down Expand Up @@ -127,7 +127,7 @@ func fheSubRun(environment EVMEnvironment, caller common.Address, addr common.Ad
return importRandomCiphertext(environment, lhs.fheUintType()), nil
}

result, err := lhs.ciphertext.ScalarSub(rhs.Uint64())
result, err := lhs.ciphertext.ScalarSub(rhs)
if err != nil {
logger.Error("fheSub failed", "err", err)
return nil, err
Expand Down Expand Up @@ -193,7 +193,7 @@ func fheMulRun(environment EVMEnvironment, caller common.Address, addr common.Ad
return importRandomCiphertext(environment, lhs.fheUintType()), nil
}

result, err := lhs.ciphertext.ScalarMul(rhs.Uint64())
result, err := lhs.ciphertext.ScalarMul(rhs)
if err != nil {
logger.Error("fheMul failed", "err", err)
return nil, err
Expand Down Expand Up @@ -234,7 +234,7 @@ func fheDivRun(environment EVMEnvironment, caller common.Address, addr common.Ad
return importRandomCiphertext(environment, lhs.fheUintType()), nil
}

result, err := lhs.ciphertext.ScalarDiv(rhs.Uint64())
result, err := lhs.ciphertext.ScalarDiv(rhs)
if err != nil {
logger.Error("fheDiv failed", "err", err)
return nil, err
Expand Down Expand Up @@ -275,7 +275,7 @@ func fheRemRun(environment EVMEnvironment, caller common.Address, addr common.Ad
return importRandomCiphertext(environment, lhs.fheUintType()), nil
}

result, err := lhs.ciphertext.ScalarRem(rhs.Uint64())
result, err := lhs.ciphertext.ScalarRem(rhs)
if err != nil {
logger.Error("fheRem failed", "err", err)
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions fhevm/operators_bit.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func fheShlRun(environment EVMEnvironment, caller common.Address, addr common.Ad
return importRandomCiphertext(environment, lhs.fheUintType()), nil
}

result, err := lhs.ciphertext.ScalarShl(rhs.Uint64())
result, err := lhs.ciphertext.ScalarShl(rhs)
if err != nil {
logger.Error("fheShl failed", "err", err)
return nil, err
Expand Down Expand Up @@ -127,7 +127,7 @@ func fheShrRun(environment EVMEnvironment, caller common.Address, addr common.Ad
return importRandomCiphertext(environment, lhs.fheUintType()), nil
}

result, err := lhs.ciphertext.ScalarShr(rhs.Uint64())
result, err := lhs.ciphertext.ScalarShr(rhs)
if err != nil {
logger.Error("fheShr failed", "err", err)
return nil, err
Expand Down
16 changes: 8 additions & 8 deletions fhevm/operators_comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func fheLeRun(environment EVMEnvironment, caller common.Address, addr common.Add
return importRandomCiphertext(environment, tfhe.FheBool), nil
}

result, err := lhs.ciphertext.ScalarLe(rhs.Uint64())
result, err := lhs.ciphertext.ScalarLe(rhs)
if err != nil {
logger.Error("fheLe failed", "err", err)
return nil, err
Expand Down Expand Up @@ -128,7 +128,7 @@ func fheLtRun(environment EVMEnvironment, caller common.Address, addr common.Add
return importRandomCiphertext(environment, tfhe.FheBool), nil
}

result, err := lhs.ciphertext.ScalarLt(rhs.Uint64())
result, err := lhs.ciphertext.ScalarLt(rhs)
if err != nil {
logger.Error("fheLt failed", "err", err)
return nil, err
Expand Down Expand Up @@ -194,7 +194,7 @@ func fheEqRun(environment EVMEnvironment, caller common.Address, addr common.Add
return importRandomCiphertext(environment, tfhe.FheBool), nil
}

result, err := lhs.ciphertext.ScalarEq(rhs.Uint64())
result, err := lhs.ciphertext.ScalarEq(rhs)
if err != nil {
logger.Error("fheEq failed", "err", err)
return nil, err
Expand Down Expand Up @@ -260,7 +260,7 @@ func fheGeRun(environment EVMEnvironment, caller common.Address, addr common.Add
return importRandomCiphertext(environment, tfhe.FheBool), nil
}

result, err := lhs.ciphertext.ScalarGe(rhs.Uint64())
result, err := lhs.ciphertext.ScalarGe(rhs)
if err != nil {
logger.Error("fheGe failed", "err", err)
return nil, err
Expand Down Expand Up @@ -326,7 +326,7 @@ func fheGtRun(environment EVMEnvironment, caller common.Address, addr common.Add
return importRandomCiphertext(environment, tfhe.FheBool), nil
}

result, err := lhs.ciphertext.ScalarGt(rhs.Uint64())
result, err := lhs.ciphertext.ScalarGt(rhs)
if err != nil {
logger.Error("fheGt failed", "err", err)
return nil, err
Expand Down Expand Up @@ -392,7 +392,7 @@ func fheNeRun(environment EVMEnvironment, caller common.Address, addr common.Add
return importRandomCiphertext(environment, tfhe.FheBool), nil
}

result, err := lhs.ciphertext.ScalarNe(rhs.Uint64())
result, err := lhs.ciphertext.ScalarNe(rhs)
if err != nil {
logger.Error("fheNe failed", "err", err)
return nil, err
Expand Down Expand Up @@ -458,7 +458,7 @@ func fheMinRun(environment EVMEnvironment, caller common.Address, addr common.Ad
return importRandomCiphertext(environment, lhs.fheUintType()), nil
}

result, err := lhs.ciphertext.ScalarMin(rhs.Uint64())
result, err := lhs.ciphertext.ScalarMin(rhs)
if err != nil {
logger.Error("fheMin failed", "err", err)
return nil, err
Expand Down Expand Up @@ -524,7 +524,7 @@ func fheMaxRun(environment EVMEnvironment, caller common.Address, addr common.Ad
return importRandomCiphertext(environment, lhs.fheUintType()), nil
}

result, err := lhs.ciphertext.ScalarMax(rhs.Uint64())
result, err := lhs.ciphertext.ScalarMax(rhs)
if err != nil {
logger.Error("fheMax failed", "err", err)
return nil, err
Expand Down
24 changes: 16 additions & 8 deletions fhevm/operators_crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ func reencryptRun(environment EVMEnvironment, caller common.Address, addr common
fheType = kms.FheType_Euint32
case tfhe.FheUint64:
fheType = kms.FheType_Euint64
case tfhe.FheUint160:
fheType = kms.FheType_Euint160
}

pubKey := input[32:64]
Expand Down Expand Up @@ -165,7 +167,6 @@ func reencryptRun(environment EVMEnvironment, caller common.Address, addr common
return nil, errors.New(msg)
}


func decryptRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) {
input = input[:minInt(32, len(input))]

Expand Down Expand Up @@ -205,9 +206,7 @@ func decryptRun(environment EVMEnvironment, caller common.Address, addr common.A

// Always return a 32-byte big-endian integer.
ret := make([]byte, 32)
bigIntValue := big.NewInt(0)
bigIntValue.SetUint64(plaintext)
bigIntValue.FillBytes(ret)
plaintext.FillBytes(ret)
return ret, nil
}

Expand Down Expand Up @@ -237,7 +236,7 @@ func getCiphertextRun(environment EVMEnvironment, caller common.Address, addr co
return ciphertext.bytes, nil
}

func decryptValue(environment EVMEnvironment, ct *tfhe.TfheCiphertext) (uint64, error) {
func decryptValue(environment EVMEnvironment, ct *tfhe.TfheCiphertext) (*big.Int, error) {

logger := environment.GetLogger()
var fheType kms.FheType
Expand All @@ -254,6 +253,8 @@ func decryptValue(environment EVMEnvironment, ct *tfhe.TfheCiphertext) (uint64,
fheType = kms.FheType_Euint32
case tfhe.FheUint64:
fheType = kms.FheType_Euint64
case tfhe.FheUint160:
fheType = kms.FheType_Euint160
}

// TODO: generate merkle proof for some data
Expand All @@ -271,7 +272,7 @@ func decryptValue(environment EVMEnvironment, ct *tfhe.TfheCiphertext) (uint64,

conn, err := grpc.Dial(kms.KmsEndpointAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return 0, errors.New("kms unreachable")
return nil, errors.New("kms unreachable")
}
defer conn.Close()

Expand All @@ -283,10 +284,17 @@ func decryptValue(environment EVMEnvironment, ct *tfhe.TfheCiphertext) (uint64,
res, err := ep.Decrypt(ctx, decryptionRequest)
if err != nil {
logger.Error("decrypt failed", "err", err)
return 0, err
return nil, err
}

return uint64(res.Plaintext), err
// plaintext is a byte slice
plaintextBytes := res.Plaintext

// Variable to hold the resulting big.Int
plaintextBigInt := new(big.Int).SetBytes(plaintextBytes)

return plaintextBigInt, nil

}

func castRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error) {
Expand Down
1 change: 1 addition & 0 deletions fhevm/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func DefaultGasCosts() GasCosts {
tfhe.FheUint16: 44000 + AdjustFHEGas,
tfhe.FheUint32: 72000 + AdjustFHEGas,
tfhe.FheUint64: 76000 + AdjustFHEGas,
tfhe.FheUint160: 80000 + AdjustFHEGas,
},
FheLe: map[tfhe.FheUintType]uint64{
tfhe.FheUint4: 60000 + AdjustFHEGas,
Expand Down
Loading

0 comments on commit 5876042

Please sign in to comment.