Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cast/decrypt and hardcoded keys #12

Merged
merged 6 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions fhevm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,40 @@ func minInt(a int, b int) int {
}
return b
}

// Return a memory with a layout that matches the `bytes` EVM type, namely:
// - 32 byte integer in big-endian order as length
// - the actual bytes in the `bytes` value
// - add zero byte padding until nearest multiple of 32
func toEVMBytes(input []byte) []byte {
arrLen := uint64(len(input))
lenBytes32 := uint256.NewInt(arrLen).Bytes32()
ret := make([]byte, 0, arrLen+32)
ret = append(ret, lenBytes32[:]...)
ret = append(ret, input...)
return ret
}

func InitFhevm(accessibleState EVMEnvironment) {
persistFhePubKeyHash(accessibleState)
}

func persistFhePubKeyHash(accessibleState EVMEnvironment) {
existing := accessibleState.GetState(fhePubKeyHashPrecompile, fhePubKeyHashSlot)
if newInt(existing[:]).IsZero() {
accessibleState.SetState(fhePubKeyHashPrecompile, fhePubKeyHashSlot, pksHash)
}
}

// apply padding to slice to the multiple of 32
func padArrayTo32Multiple(input []byte) []byte {
modRes := len(input) % 32
if modRes > 0 {
padding := 32 - modRes
for padding > 0 {
padding--
input = append(input, 0x0)
}
}
return input
}
37 changes: 37 additions & 0 deletions fhevm/precompiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var ErrExecutionReverted = errors.New("execution reverted")
var signatureFheAdd = makeKeccakSignature("fheAdd(uint256,uint256,bytes1)")
var signatureCast = makeKeccakSignature("cast(uint256,bytes1)")
var signatureDecrypt = makeKeccakSignature("decrypt(uint256)")
var signatureFhePubKey = makeKeccakSignature("fhePubKey(bytes1)")

func FheLibRequiredGas(environment EVMEnvironment, input []byte) uint64 {
logger := environment.GetLogger()
Expand All @@ -43,6 +44,9 @@ func FheLibRequiredGas(environment EVMEnvironment, input []byte) uint64 {
case signatureDecrypt:
bwCompatBytes := input[4:minInt(36, len(input))]
return decryptRequiredGas(environment, bwCompatBytes)
case signatureFhePubKey:
bwCompatBytes := input[4:minInt(5, len(input))]
return fhePubKeyRequiredGas(environment, bwCompatBytes)
default:
err := errors.New("precompile method not found")
logger.Error("fheLib precompile error", "err", err, "input", hex.EncodeToString(input))
Expand All @@ -68,6 +72,17 @@ func FheLibRun(environment EVMEnvironment, caller common.Address, addr common.Ad
case signatureDecrypt:
bwCompatBytes := input[4:minInt(36, len(input))]
return decryptRun(environment, caller, addr, bwCompatBytes, readOnly)
case signatureFhePubKey:
bwCompatBytes := input[4:minInt(5, len(input))]
precompileBytes, err := fhePubKeyRun(environment, caller, addr, bwCompatBytes, readOnly)
if err != nil {
return precompileBytes, err
}
// pad according to abi specification, first add offset to the dynamic bytes argument
outputBytes := make([]byte, 32, len(precompileBytes)+32)
outputBytes[31] = 0x20
outputBytes = append(outputBytes, precompileBytes...)
return padArrayTo32Multiple(outputBytes), nil
default:
err := errors.New("precompile method not found")
logger.Error("fheLib precompile error", "err", err, "input", hex.EncodeToString(input))
Expand Down Expand Up @@ -141,6 +156,10 @@ func decryptRequiredGas(environment EVMEnvironment, input []byte) uint64 {
return fheDecryptGasCosts[ct.ciphertext.fheUintType]
}

func fhePubKeyRequiredGas(accessibleState EVMEnvironment, input []byte) uint64 {
return params.FhePubKeyGas
}

// Implementations
func fheAddRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool) ([]byte, error) {
logger := environment.GetLogger()
Expand Down Expand Up @@ -313,3 +332,21 @@ func castRun(environment EVMEnvironment, caller common.Address, addr common.Addr

return resHash.Bytes(), nil
}

var fhePubKeyHashPrecompile = common.BytesToAddress([]byte{93})
var fhePubKeyHashSlot = common.Hash{}

func fhePubKeyRun(accessibleState EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool) ([]byte, error) {
existing := accessibleState.GetState(fhePubKeyHashPrecompile, fhePubKeyHashSlot)
if existing != pksHash {
msg := "fhePubKey FHE public key hash doesn't match one stored in state"
accessibleState.GetLogger().Error(msg, "existing", existing.Hex(), "pksHash", pksHash.Hex())
return nil, errors.New(msg)
}
// If we have a single byte with the value of 1, return as an EVM array. Otherwise, returh the raw bytes.
if len(input) == 1 && input[0] == 1 {
return toEVMBytes(pksBytes), nil
} else {
return pksBytes, nil
}
}
2 changes: 1 addition & 1 deletion fhevm/tfhe.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package fhevm

/*
#cgo CFLAGS: -O3 -I../tfhe-rs/target/release
#cgo LDFLAGS: -L../tfhe-rs/target/release -l:libtfhe.a -lm
#cgo LDFLAGS: -L../tfhe-rs/target/release -ltfhe -lm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to use #ifdef directive to conditionally link dynamically on mac only

Suggested change
#cgo LDFLAGS: -L../tfhe-rs/target/release -ltfhe -lm
// FIXME: link statically on mac as well
#ifdef __APPLE__
#cgo LDFLAGS: -L../tfhe-rs/target/release -ltfhe -lm
#else
#cgo LDFLAGS: -L../tfhe-rs/target/release -l:libtfhe.a -lm
#endif

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it locally, and it doesn't seem to work. The #ifdef isn't evaluated to get those flags

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


#include <tfhe.h>

Expand Down
Loading