fast

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PageAddrSize = 12
	PageKeySize  = 64 - PageAddrSize
	PageSize     = 1 << PageAddrSize
	PageAddrMask = PageSize - 1
	MaxPageCount = 1 << PageKeySize
	PageKeyMask  = MaxPageCount - 1
	ProofLen     = 64 - 4
)

Note: 2**12 = 4 KiB, the minimum page-size in Unicorn for mmap as well as the Go runtime min phys page size.

View Source
const (
	VMStatusValid      = 0
	VMStatusInvalid    = 1
	VMStatusPanic      = 2
	VMStatusUnfinished = 3
)

Variables

This section is empty.

Functions

func HashPair

func HashPair(left, right [32]byte) [32]byte

func PatchVM

func PatchVM(f *elf.File, vmState *VMState) error

Types

type CachedPage

type CachedPage struct {
	Data *Page
	// intermediate nodes only
	Cache [PageSize / 32][32]byte
	// true if the intermediate node is valid
	Ok [PageSize / 32]bool
}

func (*CachedPage) GenerateProof added in v1.1.0

func (p *CachedPage) GenerateProof(addr uint64) [][32]byte

func (*CachedPage) Invalidate

func (p *CachedPage) Invalidate(pageAddr uint64)

func (*CachedPage) InvalidateFull

func (p *CachedPage) InvalidateFull()

func (*CachedPage) MerkleRoot

func (p *CachedPage) MerkleRoot() [32]byte

func (*CachedPage) MerkleizeNode added in v1.1.0

func (p *CachedPage) MerkleizeNode(addr, gindex uint64) [32]byte

func (*CachedPage) MerkleizeSubtree

func (p *CachedPage) MerkleizeSubtree(gindex uint64) [32]byte

type InstrumentedState

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

func NewInstrumentedState

func NewInstrumentedState(state *VMState, po PreimageOracle, stdOut, stdErr io.Writer) *InstrumentedState

func (*InstrumentedState) LastPreimage

func (m *InstrumentedState) LastPreimage() ([32]byte, []byte, uint64)

func (*InstrumentedState) Step

func (m *InstrumentedState) Step(proof bool) (wit *StepWitness, err error)

type L1 added in v1.1.0

type L1 = LargeRadixNode[L2]

type L2 added in v1.1.0

type L2 = *LargeRadixNode[L3]

type L3 added in v1.1.0

type L3 = *MediumRadixNode[L4]

type L4 added in v1.1.0

type L4 = *MediumRadixNode[L5]

type L5 added in v1.1.0

type L5 = *SmallRadixNode[L6]

type L6 added in v1.1.0

type L6 = *SmallRadixNode[L7]

type L7 added in v1.1.0

type L7 = *Memory

type LargeRadixNode added in v1.1.0

type LargeRadixNode[C RadixNode] struct {
	Children   [1 << 16]*C // Array of child nodes, indexed by 16-bit keys.
	Hashes     [1 << 16][32]byte
	HashExists [(1 << 16) / 64]uint64
	HashValid  [(1 << 16) / 64]uint64
	Depth      uint64
}

LargeRadixNode is a radix trie node with a branching factor of 16 bits.

func (*LargeRadixNode[C]) GenerateProof added in v1.1.0

func (n *LargeRadixNode[C]) GenerateProof(addr uint64, proofs [][32]byte)

func (*LargeRadixNode[C]) InvalidateNode added in v1.1.0

func (n *LargeRadixNode[C]) InvalidateNode(gindex uint64)

func (*LargeRadixNode[C]) MerkleizeNode added in v1.1.0

func (n *LargeRadixNode[C]) MerkleizeNode(addr, gindex uint64) [32]byte

type LocalContext

type LocalContext common.Hash

type MediumRadixNode added in v1.1.0

type MediumRadixNode[C RadixNode] struct {
	Children   [1 << 6]*C // Array of child nodes, indexed by 6-bit keys.
	Hashes     [1 << 6][32]byte
	HashExists uint64
	HashValid  uint64
	Depth      uint64
}

MediumRadixNode is a radix trie node with a branching factor of 6 bits.

func (*MediumRadixNode[C]) GenerateProof added in v1.1.0

func (n *MediumRadixNode[C]) GenerateProof(addr uint64, proofs [][32]byte)

func (*MediumRadixNode[C]) InvalidateNode added in v1.1.0

func (n *MediumRadixNode[C]) InvalidateNode(gindex uint64)

func (*MediumRadixNode[C]) MerkleizeNode added in v1.1.0

func (n *MediumRadixNode[C]) MerkleizeNode(addr, gindex uint64) [32]byte

type Memory

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

func NewMemory

func NewMemory() *Memory

func (*Memory) AllocPage

func (m *Memory) AllocPage(pageIndex uint64) *CachedPage

AllocPage allocates a new page at the specified page index in memory.

func (*Memory) Deserialize added in v1.1.0

func (m *Memory) Deserialize(in io.Reader) error

func (*Memory) ForEachPage

func (m *Memory) ForEachPage(fn func(pageIndex uint64, page *Page) error) error

func (*Memory) GenerateProof added in v1.1.0

func (m *Memory) GenerateProof(addr uint64, proofs [][32]byte)

func (*Memory) GetUnaligned

func (m *Memory) GetUnaligned(addr uint64, dest []byte)

func (*Memory) Invalidate

func (m *Memory) Invalidate(addr uint64)

Invalidate invalidates the cache along the path from the specified address up to the root. It ensures that any cached hashes are recomputed when needed.

func (*Memory) MarshalJSON

func (m *Memory) MarshalJSON() ([]byte, error)

func (*Memory) MerkleProof

func (m *Memory) MerkleProof(addr uint64) [ProofLen * 32]byte

MerkleProof generates the Merkle proof for the specified address in memory.

func (*Memory) MerkleRoot

func (m *Memory) MerkleRoot() [32]byte

MerkleRoot computes the Merkle root hash of the entire memory.

func (*Memory) MerkleizeNode added in v1.1.0

func (m *Memory) MerkleizeNode(addr, gindex uint64) [32]byte

func (*Memory) PageCount

func (m *Memory) PageCount() int

func (*Memory) ReadMemoryRange

func (m *Memory) ReadMemoryRange(addr uint64, count uint64) io.Reader

func (*Memory) Serialize added in v1.1.0

func (m *Memory) Serialize(out io.Writer) error

Serialize writes the memory in a simple binary format which can be read again using Deserialize The format is a simple concatenation of fields, with prefixed item count for repeating items and using big endian encoding for numbers.

len(PageCount) uint64 For each page (order is arbitrary):

page index          uint64
page Data           [PageSize]byte

func (*Memory) SetMemoryRange

func (m *Memory) SetMemoryRange(addr uint64, r io.Reader) error

func (*Memory) SetUnaligned

func (m *Memory) SetUnaligned(addr uint64, dat []byte)

func (*Memory) UnmarshalJSON

func (m *Memory) UnmarshalJSON(data []byte) error

func (*Memory) Usage

func (m *Memory) Usage() string

type Page

type Page [PageSize]byte

func (*Page) MarshalText

func (p *Page) MarshalText() ([]byte, error)

func (*Page) UnmarshalText

func (p *Page) UnmarshalText(dat []byte) error

type PreimageOracle

type PreimageOracle interface {
	Hint(v []byte)
	GetPreimage(k [32]byte) []byte
}

type RadixNode added in v1.1.0

type RadixNode interface {
	// GenerateProof generates the Merkle proof for the given address.
	GenerateProof(addr uint64, proofs [][32]byte)
	// MerkleizeNode computes the Merkle root hash for the node at the given generalized index.
	MerkleizeNode(addr, gindex uint64) [32]byte
}

RadixNode is an interface defining the operations for a node in a radix trie.

type SmallRadixNode added in v1.1.0

type SmallRadixNode[C RadixNode] struct {
	Children   [1 << 4]*C       // Array of child nodes, indexed by 4-bit keys.
	Hashes     [1 << 4][32]byte // Cached hashes for intermediate hash node.
	HashExists uint16           // Bitmask indicating if the intermediate hash exist (1 bit per intermediate node).
	HashValid  uint16           // Bitmask indicating if the intermediate hashes are valid (1 bit per intermediate node).
	Depth      uint64           // The depth of this node in the trie (number of bits from the root).
}

SmallRadixNode is a radix trie node with a branching factor of 4 bits.

func (*SmallRadixNode[C]) GenerateProof added in v1.1.0

func (n *SmallRadixNode[C]) GenerateProof(addr uint64, proofs [][32]byte)

GenerateProof generates the Merkle proof for the given address. It collects the necessary sibling hashes along the path to reconstruct the Merkle proof.

func (*SmallRadixNode[C]) InvalidateNode added in v1.1.0

func (n *SmallRadixNode[C]) InvalidateNode(gindex uint64)

InvalidateNode invalidates the hash cache along the path to the specified address. It marks the necessary intermediate hashes as invalid, forcing them to be recomputed when needed.

func (*SmallRadixNode[C]) MerkleizeNode added in v1.1.0

func (n *SmallRadixNode[C]) MerkleizeNode(addr, gindex uint64) [32]byte

MerkleizeNode computes the Merkle root hash for the node at the given generalized index. It recursively computes the hash of the subtree rooted at the given index. Note: The 'addr' parameter represents the partial address accumulated up to this node, not the full address. It represents the path taken in the trie to reach this node.

type SortedSymbols

type SortedSymbols []elf.Symbol

func Symbols

func Symbols(f *elf.File) (SortedSymbols, error)

func (SortedSymbols) FindSymbol

func (s SortedSymbols) FindSymbol(addr uint64) elf.Symbol

FindSymbol finds the symbol that intersects with the given addr, or nil if none exists

type StateWitness

type StateWitness []byte

func (StateWitness) StateHash

func (sw StateWitness) StateHash() (common.Hash, error)

type StepWitness

type StepWitness struct {
	// encoded state witness
	State []byte

	MemProof []byte

	PreimageKey    [32]byte // zeroed when no pre-image is accessed
	PreimageValue  []byte   // including the 8-byte length prefix
	PreimageOffset uint64
}

func (*StepWitness) EncodePreimageOracleInput

func (wit *StepWitness) EncodePreimageOracleInput(localContext LocalContext) ([]byte, error)

func (*StepWitness) EncodeStepInput

func (wit *StepWitness) EncodeStepInput(localContext LocalContext) ([]byte, error)

func (*StepWitness) HasPreimage

func (wit *StepWitness) HasPreimage() bool

type U256

type U256 = uint256.Int

type U64

type U64 = uint64

type UnrecognizedResourceErr

type UnrecognizedResourceErr struct {
	Resource U64
}

func (*UnrecognizedResourceErr) Error

func (e *UnrecognizedResourceErr) Error() string

type UnrecognizedSyscallErr

type UnrecognizedSyscallErr struct {
	SyscallNum U64
}

func (*UnrecognizedSyscallErr) Error

func (e *UnrecognizedSyscallErr) Error() string

type UnsupportedSyscallErr

type UnsupportedSyscallErr struct {
	SyscallNum U64
}

func (*UnsupportedSyscallErr) Error

func (e *UnsupportedSyscallErr) Error() string

type VMState

type VMState struct {
	Memory *Memory `json:"memory"`

	PreimageKey    common.Hash `json:"preimageKey"`
	PreimageOffset uint64      `json:"preimageOffset"`

	PC uint64 `json:"pc"`

	ExitCode uint8 `json:"exit"`
	Exited   bool  `json:"exited"`

	Step uint64 `json:"step"`

	Heap uint64 `json:"heap"` // for mmap to keep allocating new anon memory

	LoadReservation uint64 `json:"loadReservation"`

	Registers [32]uint64 `json:"registers"`

	// LastHint is optional metadata, and not part of the VM state itself.
	// It is used to remember the last pre-image hint,
	// so a VM can start from any state without fetching prior pre-images,
	// and instead just repeat the last hint on setup,
	// to make sure pre-image requests can be served.
	// The first 4 bytes are a uin32 length prefix.
	// Warning: the hint MAY NOT BE COMPLETE. I.e. this is buffered,
	// and should only be read when len(LastHint) > 4 && uint32(LastHint[:4]) <= len(LastHint[4:])
	LastHint hexutil.Bytes `json:"lastHint,omitempty"`

	// VMState must hold these values because if not, we must ask FPVM again to
	// compute these values.
	Witness   []byte      `json:"witness,omitempty"`
	StateHash common.Hash `json:"stateHash,omitempty"`
}

func LoadELF

func LoadELF(f *elf.File) (*VMState, error)

func LoadVMStateFromFile added in v1.1.0

func LoadVMStateFromFile(path string) (*VMState, error)

func NewVMState

func NewVMState() *VMState

func (*VMState) Deserialize added in v1.1.0

func (s *VMState) Deserialize(in io.Reader) error

func (*VMState) EncodeWitness

func (state *VMState) EncodeWitness() StateWitness

func (*VMState) GetStep

func (state *VMState) GetStep() uint64

func (*VMState) Instr

func (state *VMState) Instr() uint32

func (*VMState) Serialize added in v1.1.0

func (s *VMState) Serialize(out io.Writer) error

Serialize writes the state in a simple binary format which can be read again using Deserialize The format is a simple concatenation of fields, with prefixed item count for repeating items and using big endian encoding for numbers.

Memory As per Memory.Serialize PreimageKey [32]byte PreimageOffset uint64 PC uint64 ExitCode uint8 Exited bool - 0 for false, 1 for true Step uint64 Heap uint64 LoadReservation uint64 Registers. [32]uint64 len(LastHint) uint64 (0 when LastHint is nil) LastHint []byte len(Witness) uint64 (0 when Witness is nil) Witness []byte StateHash [32]byte

func (*VMState) SetWitnessAndStateHash

func (state *VMState) SetWitnessAndStateHash() error

Jump to

Keyboard shortcuts

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