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 4 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
63 changes: 50 additions & 13 deletions fhevm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ func makeKeccakSignature(input string) uint32 {
return binary.BigEndian.Uint32(crypto.Keccak256([]byte(input))[0:4])
}

func isScalarOp(environment *EVMEnvironment, input []byte) (bool, error) {
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 getVerifiedCiphertext(environment EVMEnvironment, ciphertextHash common.Hash) *verifiedCiphertext {
return getVerifiedCiphertextFromEVM(environment, ciphertextHash)
}

func get2VerifiedOperands(environment *EVMEnvironment, input []byte) (lhs *verifiedCiphertext, rhs *verifiedCiphertext, err error) {
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")
}
Expand All @@ -72,7 +72,7 @@ func get2VerifiedOperands(environment *EVMEnvironment, input []byte) (lhs *verif
return
}

func getScalarOperands(environment *EVMEnvironment, input []byte) (lhs *verifiedCiphertext, rhs *big.Int, err error) {
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")
}
Expand All @@ -85,8 +85,8 @@ func getScalarOperands(environment *EVMEnvironment, input []byte) (lhs *verified
return
}

func importCiphertextToEVMAtDepth(environment *EVMEnvironment, ct *tfheCiphertext, depth int) *verifiedCiphertext {
existing, ok := (*environment).GetFhevmData().verifiedCiphertexts[ct.getHash()]
func importCiphertextToEVMAtDepth(environment EVMEnvironment, ct *tfheCiphertext, depth int) *verifiedCiphertext {
existing, ok := environment.GetFhevmData().verifiedCiphertexts[ct.getHash()]
if ok {
existing.verifiedDepths.add(depth)
return existing
Expand All @@ -97,21 +97,21 @@ func importCiphertextToEVMAtDepth(environment *EVMEnvironment, ct *tfheCiphertex
verifiedDepths,
ct,
}
(*environment).GetFhevmData().verifiedCiphertexts[ct.getHash()] = new
environment.GetFhevmData().verifiedCiphertexts[ct.getHash()] = new
return new
}
}

func importCiphertextToEVM(environment *EVMEnvironment, ct *tfheCiphertext) *verifiedCiphertext {
return importCiphertextToEVMAtDepth(environment, ct, (*environment).GetDepth())
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).GetFhevmData().nextCiphertextHashOnGasEst
func importRandomCiphertext(environment EVMEnvironment, t fheUintType) []byte {
nextCtHash := &environment.GetFhevmData().nextCiphertextHashOnGasEst
ctHashBytes := crypto.Keccak256(nextCtHash.Bytes())
handle := common.BytesToHash(ctHashBytes)
ct := new(tfheCiphertext)
Expand All @@ -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
}
Loading
Loading