fhevm

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2024 License: BSD-3-Clause-Clear Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const AdjustFHEGas uint64 = 10000
View Source
const ColdSloadCostEIP2929 uint64 = 2100
View Source
const EvmNetSstoreInitGas uint64 = 20000

Base gas costs of existing EVM operations. Used for setting gas costs relative to them. These constants are used just for readability.

View Source
const GetNonExistentCiphertextGas uint64 = 1000

Variables

View Source
var (
	ErrOutOfGas                    error
	ErrCodeStoreOutOfGas           error
	ErrDepth                       error
	ErrInsufficientBalance         error
	ErrContractAddressCollision    error
	ErrExecutionReverted           error
	ErrMaxInitCodeSizeExceeded     error
	ErrMaxCodeSizeExceeded         error
	ErrInvalidJump                 error
	ErrWriteProtection             error
	ErrReturnDataOutOfBounds       error
	ErrGasUintOverflow             error
	ErrInvalidCode                 error
	ErrNonceUintOverflow           error
	ErrAddrProhibited              error
	ErrInvalidCoinbase             error
	ErrSenderAddressNotAllowListed error
)

List of EVM execution errors needed by the fhEVM

View Source
var (
	// TODO: The values here are chosen somewhat arbitrarily (at least the 8 bit ones). Also, we don't
	// take into account whether a ciphertext existed (either "current" or "original") for the given handle.
	// Finally, costs are likely to change in the future.
	FheUint8ProtectedStorageSstoreGas  uint64 = EvmNetSstoreInitGas + 2000
	FheUint16ProtectedStorageSstoreGas uint64 = FheUint8ProtectedStorageSstoreGas * 2
	FheUint32ProtectedStorageSstoreGas uint64 = FheUint16ProtectedStorageSstoreGas * 2

	// TODO: We don't take whether the slot is cold or warm into consideration.
	FheUint8ProtectedStorageSloadGas  uint64 = ColdSloadCostEIP2929 + 200
	FheUint16ProtectedStorageSloadGas uint64 = FheUint8ProtectedStorageSloadGas * 2
	FheUint32ProtectedStorageSloadGas uint64 = FheUint16ProtectedStorageSloadGas * 2
)
View Source
var TxDataFractionalGasFactor uint64 = 4

Functions

func Create

func Create(evm EVMEnvironment, caller common.Address, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error)

func Create2

func Create2(evm EVMEnvironment, caller common.Address, code []byte, gas uint64, endowment *big.Int, salt *uint256.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error)

func DelegateCiphertextHandlesInArgs

func DelegateCiphertextHandlesInArgs(env EVMEnvironment, args []byte) (verified map[common.Hash]*depthSet)

If there are ciphertext handles in the arguments to a call, delegate them to the callee. Return a map from ciphertext hash -> depthSet before delegation.

func FheLibRequiredGas

func FheLibRequiredGas(environment EVMEnvironment, input []byte) uint64

func FheLibRun

func FheLibRun(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool) (ret []byte, err error)

func InitFhevm

func InitFhevm(accessibleState EVMEnvironment)

func OpReturn

func OpReturn(pc *uint64, env EVMEnvironment, scope ScopeContext) []byte

func OpSelfdestruct

func OpSelfdestruct(pc *uint64, env EVMEnvironment, scope ScopeContext) (beneficiary uint256.Int, balance *big.Int)

func OpSload

func OpSload(pc *uint64, env EVMEnvironment, scope ScopeContext) ([]byte, error)

This function is a modified copy from https://github.com/ethereum/go-ethereum

func OpSstore

func OpSstore(pc *uint64, env EVMEnvironment, scope ScopeContext) ([]byte, error)

func RegisterErrors

func RegisterErrors(
	outOfGasError error,
	codeStoreOutOfGasError error,
	depthError error,
	insufficientBalanceError error,
	contractAddressCollisionError error,
	executionRevertedError error,
	maxInitCodeSizeExceededError error,
	maxCodeSizeExceededError error,
	invalidJumpError error,
	writeProtectionError error,
	returnDataOutOfBoundsError error,
	gasUintOverflowError error,
	invalidCodeError error,
	nonceUintOverflowError error,
	addrProhibitedError error,
	invalidCoinbaseError error,
	senderAddressNotAllowListedError error)

Register package errors with other custom errors.

This is useful in cases where returned errors need to be recognized by the framework using fhevm-go, without much code changes in the framework.

func RemoveVerifiedCipherextsAtCurrentDepth

func RemoveVerifiedCipherextsAtCurrentDepth(env EVMEnvironment)

func RestoreVerifiedDepths

func RestoreVerifiedDepths(env EVMEnvironment, verified map[common.Hash]*depthSet)

func SetProtectedStorageAddrCallerAddr

func SetProtectedStorageAddrCallerAddr(addr []byte)

Set the addr to be used as caller when creating protected storage contracts

func TxDataFractionalGas

func TxDataFractionalGas(originalGas uint64) (fractionalGas uint64)

Types

type Contract

type Contract interface {
	Address() common.Address
}

type DefaultLogger

type DefaultLogger struct {
	// contains filtered or unexported fields
}

A default Logger implementation that logs to stdout.

func (*DefaultLogger) Debug

func (l *DefaultLogger) Debug(msg string, keyvals ...interface{})

func (*DefaultLogger) Error

func (l *DefaultLogger) Error(msg string, keyvals ...interface{})

func (*DefaultLogger) Info

func (l *DefaultLogger) Info(msg string, keyvals ...interface{})

type EVMEnvironment

type EVMEnvironment interface {
	// StateDB related functions
	GetState(common.Address, common.Hash) common.Hash
	SetState(common.Address, common.Hash, common.Hash)
	GetNonce(common.Address) uint64
	AddBalance(common.Address, *big.Int)
	GetBalance(common.Address) *big.Int

	Suicide(common.Address) bool

	// EVM call stack depth
	GetDepth() int

	// EVM Logger
	GetLogger() Logger

	// TODO: clarify meaning of the following
	IsCommitting() bool
	IsEthCall() bool
	IsReadOnly() bool

	CreateContract(caller common.Address, code []byte, gas uint64, value *big.Int, address common.Address) ([]byte, common.Address, uint64, error)
	CreateContract2(caller common.Address, code []byte, codeHash common.Hash, gas uint64, value *big.Int, address common.Address) ([]byte, common.Address, uint64, error)

	FhevmData() *FhevmData
	FhevmParams() *FhevmParams

	// This should return the context used for OpenTelemetry in the current EVM.
	// It should be considered the root context for every op that runs in the EVM, and all spans created from this context
	// would be child spans for what has been already created using the context.
	// Implementations returning nil would disable OpenTelemetry on the fhEVM
	OtelContext() context.Context
}

type FheLibMethod

type FheLibMethod struct {
	// contains filtered or unexported fields
}

A method available in the fhelib precompile that can run and estimate gas

func GetFheLibMethod

func GetFheLibMethod(signature uint32) (fheLibMethod *FheLibMethod, found bool)

func (*FheLibMethod) Name

func (fheLibMethod *FheLibMethod) Name() string

func (*FheLibMethod) RequiredGas

func (fheLibMethod *FheLibMethod) RequiredGas(environment EVMEnvironment, input []byte) uint64

func (*FheLibMethod) Run

func (fheLibMethod *FheLibMethod) Run(environment EVMEnvironment, caller common.Address, addr common.Address, input []byte, readOnly bool, runSpan trace.Span) ([]byte, error)

func (*FheLibMethod) Signature

func (fheLibMethod *FheLibMethod) Signature() uint32

Return the computed signature by concatenating the name and the arg types of the method

type FhevmData

type FhevmData struct {
	// contains filtered or unexported fields
}

func NewFhevmData

func NewFhevmData() FhevmData

type FhevmParams

type FhevmParams struct {
	GasCosts                        GasCosts
	DisableDecryptionsInTransaction bool
}

func DefaultFhevmParams

func DefaultFhevmParams() FhevmParams

type GasCosts

type GasCosts struct {
	FheCast           uint64
	FhePubKey         uint64
	FheAddSub         map[tfhe.FheUintType]uint64
	FheDecrypt        map[tfhe.FheUintType]uint64
	FheBitwiseOp      map[tfhe.FheUintType]uint64
	FheMul            map[tfhe.FheUintType]uint64
	FheScalarMul      map[tfhe.FheUintType]uint64
	FheScalarDiv      map[tfhe.FheUintType]uint64
	FheScalarRem      map[tfhe.FheUintType]uint64
	FheShift          map[tfhe.FheUintType]uint64
	FheScalarShift    map[tfhe.FheUintType]uint64
	FheEq             map[tfhe.FheUintType]uint64
	FheLe             map[tfhe.FheUintType]uint64
	FheMinMax         map[tfhe.FheUintType]uint64
	FheScalarMinMax   map[tfhe.FheUintType]uint64
	FheNot            map[tfhe.FheUintType]uint64
	FheNeg            map[tfhe.FheUintType]uint64
	FheReencrypt      map[tfhe.FheUintType]uint64
	FheTrivialEncrypt map[tfhe.FheUintType]uint64
	FheRand           map[tfhe.FheUintType]uint64
	FheIfThenElse     map[tfhe.FheUintType]uint64
	FheVerify         map[tfhe.FheUintType]uint64
	FheGetCiphertext  map[tfhe.FheUintType]uint64
}

func DefaultGasCosts

func DefaultGasCosts() GasCosts

type Logger

type Logger interface {
	Debug(msg string, keyvals ...interface{})
	Info(msg string, keyvals ...interface{})
	Error(msg string, keyvals ...interface{})
}

A Logger interface for the EVM.

func NewDefaultLogger

func NewDefaultLogger() Logger

type Memory

type Memory interface {
	Set(offset, size uint64, value []byte)
	Set32(offset uint64, val *uint256.Int)
	Resize(size uint64)
	GetCopy(offset, size int64) (cpy []byte)
	GetPtr(offset, size int64) []byte
	Len() int
	Data() []byte
}

type PrivilegedMemory

type PrivilegedMemory struct {
	// A map from a ciphertext hash to itself and stack depths at which it is verified
	VerifiedCiphertexts map[common.Hash]*verifiedCiphertext
}
var PrivilegedMempory *PrivilegedMemory = &PrivilegedMemory{
	make(map[common.Hash]*verifiedCiphertext),
}

type ScopeContext

type ScopeContext interface {
	GetMemory() Memory
	GetStack() Stack
	GetContract() Contract
}

type Stack

type Stack interface {
	Pop() uint256.Int
	Peek() *uint256.Int
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL