Skip to content

Commit

Permalink
refactor: clean and separate the fhelib
Browse files Browse the repository at this point in the history
  • Loading branch information
youben11 committed Feb 14, 2024
1 parent 144ba98 commit 0be0db2
Show file tree
Hide file tree
Showing 14 changed files with 3,115 additions and 3,065 deletions.
364 changes: 187 additions & 177 deletions fhevm/contracts_test.go

Large diffs are not rendered by default.

110 changes: 7 additions & 103 deletions fhevm/evm.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package fhevm

import (
"encoding/binary"
"errors"
"fmt"
"math/big"

Expand Down Expand Up @@ -53,73 +51,12 @@ func (*DefaultLogger) Error(msg string, keyvals ...interface{}) {
fmt.Println("Error: "+msg, toString(keyvals...))
}

func makeKeccakSignature(input string) uint32 {
return binary.BigEndian.Uint32(crypto.Keccak256([]byte(input))[0:4])
}

func isScalarOp(input []byte) (bool, error) {
if len(input) != 65 {
return false, errors.New("input needs to contain two 256-bit sized values and 1 8-bit value")
}
isScalar := (input[64] == 1)
return isScalar, nil
}

func getVerifiedCiphertext(environment EVMEnvironment, ciphertextHash common.Hash) *verifiedCiphertext {
return getVerifiedCiphertextFromEVM(environment, ciphertextHash)
}

func get2VerifiedOperands(environment EVMEnvironment, input []byte) (lhs *verifiedCiphertext, rhs *verifiedCiphertext, err error) {
if len(input) != 65 {
return nil, nil, errors.New("input needs to contain two 256-bit sized values and 1 8-bit value")
}
lhs = getVerifiedCiphertext(environment, common.BytesToHash(input[0:32]))
if lhs == nil {
return nil, nil, errors.New("unverified ciphertext handle")
}
rhs = getVerifiedCiphertext(environment, common.BytesToHash(input[32:64]))
if rhs == nil {
return nil, nil, errors.New("unverified ciphertext handle")
}
err = nil
return
}

func get3VerifiedOperands(environment EVMEnvironment, input []byte) (first *verifiedCiphertext, second *verifiedCiphertext, third *verifiedCiphertext, err error) {
if len(input) != 96 {
return nil, nil, nil, errors.New("input needs to contain three 256-bit sized values")
}
first = getVerifiedCiphertext(environment, common.BytesToHash(input[0:32]))
if first == nil {
return nil, nil, nil, errors.New("unverified ciphertext handle")
}
second = getVerifiedCiphertext(environment, common.BytesToHash(input[32:64]))
if second == nil {
return nil, nil, nil, errors.New("unverified ciphertext handle")
}
third = getVerifiedCiphertext(environment, common.BytesToHash(input[64:96]))
if third == nil {
return nil, nil, nil, errors.New("unverified ciphertext handle")
}
err = nil
return
}

func getScalarOperands(environment EVMEnvironment, input []byte) (lhs *verifiedCiphertext, rhs *big.Int, err error) {
if len(input) != 65 {
return nil, nil, errors.New("input needs to contain two 256-bit sized values and 1 8-bit value")
}
lhs = getVerifiedCiphertext(environment, common.BytesToHash(input[0:32]))
if lhs == nil {
return nil, nil, errors.New("unverified ciphertext handle")
}
rhs = &big.Int{}
rhs.SetBytes(input[32:64])
return
}

func importCiphertextToEVMAtDepth(environment EVMEnvironment, ct *tfheCiphertext, depth int) *verifiedCiphertext {
existing, ok := environment.FhevmData().verifiedCiphertexts[ct.getHash()]
func importCiphertextToEVMAtDepth(environment EVMEnvironment, ct *TfheCiphertext, depth int) *verifiedCiphertext {
existing, ok := environment.FhevmData().verifiedCiphertexts[ct.GetHash()]
if ok {
existing.verifiedDepths.add(depth)
return existing
Expand All @@ -130,50 +67,30 @@ func importCiphertextToEVMAtDepth(environment EVMEnvironment, ct *tfheCiphertext
verifiedDepths,
ct,
}
environment.FhevmData().verifiedCiphertexts[ct.getHash()] = new
environment.FhevmData().verifiedCiphertexts[ct.GetHash()] = new
return new
}
}

func importCiphertextToEVM(environment EVMEnvironment, ct *tfheCiphertext) *verifiedCiphertext {
func importCiphertextToEVM(environment EVMEnvironment, ct *TfheCiphertext) *verifiedCiphertext {
return importCiphertextToEVMAtDepth(environment, ct, environment.GetDepth())
}

func importCiphertext(environment EVMEnvironment, ct *tfheCiphertext) *verifiedCiphertext {
func importCiphertext(environment EVMEnvironment, ct *TfheCiphertext) *verifiedCiphertext {
return importCiphertextToEVM(environment, ct)
}

func importRandomCiphertext(environment EVMEnvironment, t FheUintType) []byte {
nextCtHash := &environment.FhevmData().nextCiphertextHashOnGasEst
ctHashBytes := crypto.Keccak256(nextCtHash.Bytes())
handle := common.BytesToHash(ctHashBytes)
ct := new(tfheCiphertext)
ct := new(TfheCiphertext)
ct.fheUintType = t
ct.hash = &handle
importCiphertext(environment, ct)
temp := nextCtHash.Clone()
nextCtHash.Add(temp, uint256.NewInt(1))
return ct.getHash().Bytes()
}

func minInt(a int, b int) int {
if a < b {
return a
}
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
return ct.GetHash().Bytes()
}

func InitFhevm(accessibleState EVMEnvironment) {
Expand All @@ -187,19 +104,6 @@ func persistFhePubKeyHash(accessibleState EVMEnvironment) {
}
}

// 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
}

func Create(evm EVMEnvironment, caller common.Address, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
contractAddr = crypto.CreateAddress(caller, evm.GetNonce(caller))
protectedStorageAddr := fhevm_crypto.CreateProtectedStorageContractAddress(contractAddr)
Expand Down
Loading

0 comments on commit 0be0db2

Please sign in to comment.