memory

package
v0.0.0-...-3aa4f12 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var UnknownAddress = MemoryAddress{}
View Source
var UnknownValue = MemoryValue{}

Functions

This section is empty.

Types

type BuiltinRunner

type BuiltinRunner interface {
	fmt.Stringer
	CheckWrite(segment *Segment, offset uint64, value *MemoryValue) error
	InferValue(segment *Segment, offset uint64) error
	GetAllocatedSize(segmentUsedSize uint64, vmCurrentStep uint64) (uint64, error)
	GetCellsPerInstance() uint64
	GetStopPointer() uint64
	SetStopPointer(stopPointer uint64)
}

type Memory

type Memory struct {
	Segments []*Segment
	// TemporarySegments is a map of temporary segments, key is the segment index, value is the segment
	TemporarySegments []*Segment
}

Represents the whole VM memory divided into segments

func InitializeEmptyMemory

func InitializeEmptyMemory() *Memory

todo(rodro): can the amount of segments be known before hand?

func (*Memory) AllocateBuiltinSegment

func (memory *Memory) AllocateBuiltinSegment(builtinRunner BuiltinRunner) MemoryAddress

Allocate a Builtin segment

func (*Memory) AllocateEmptySegment

func (memory *Memory) AllocateEmptySegment() MemoryAddress

Allocates an empty segment and returns its index

func (*Memory) AllocateEmptyTemporarySegment

func (memory *Memory) AllocateEmptyTemporarySegment() MemoryAddress

Allocates an empty temporary segment and returns its index

func (*Memory) AllocateSegment

func (memory *Memory) AllocateSegment(data []*f.Element) (MemoryAddress, error)

Allocates a new segment providing its initial data and returns its index

func (*Memory) FindSegmentWithBuiltin

func (memory *Memory) FindSegmentWithBuiltin(builtinName string) (*Segment, bool)

It finds a segment with a given builtin name, it returns the segment and true if found

func (*Memory) GetConsecutiveMemoryValues

func (memory *Memory) GetConsecutiveMemoryValues(addr MemoryAddress, size uint64) ([]MemoryValue, error)

func (*Memory) KnownValue

func (memory *Memory) KnownValue(segment uint64, offset uint64) bool

Given a segment index and offset returns true if the value at that address is known

func (*Memory) KnownValueAtAddress

func (memory *Memory) KnownValueAtAddress(address *MemoryAddress) bool

Given an address returns true if it contains a known value

func (*Memory) Peek

func (memory *Memory) Peek(segmentIndex uint64, offset uint64) (MemoryValue, error)

Given a segment index and offset, returns the memory value at that position, without modifying it in any way. Errors if peeking from an unallocated segment

func (*Memory) PeekFromAddress

func (memory *Memory) PeekFromAddress(address *MemoryAddress) (MemoryValue, error)

Given an address returns the memory value at that position, without modifying it in any way. Errors if peeking from an unallocated segment

func (*Memory) Read

func (memory *Memory) Read(segmentIndex uint64, offset uint64) (MemoryValue, error)

Reads a memory value given the segment index and offset. Errors if reading from an unallocated segment or if reading an unknown memory value

func (*Memory) ReadAsAddress

func (memory *Memory) ReadAsAddress(address *MemoryAddress) (MemoryAddress, error)

Works the same as `Read` but `MemoryValue` is converted to `MemoryAddress` first

func (*Memory) ReadAsElement

func (memory *Memory) ReadAsElement(segmentIndex uint64, offset uint64) (f.Element, error)

Works the same as `Read` but `MemoryValue` is converted to `Element` first

func (*Memory) ReadFromAddress

func (memory *Memory) ReadFromAddress(address *MemoryAddress) (MemoryValue, error)

Reads a memory value given an address. Errors if reading from an unallocated segment or if reading an unknown memory value

func (*Memory) ReadFromAddressAsAddress

func (memory *Memory) ReadFromAddressAsAddress(address *MemoryAddress) (MemoryAddress, error)

Works the same as `ReadFromAddress` but `MemoryValue` is converted to `MemoryAddress` first

func (*Memory) ReadFromAddressAsElement

func (memory *Memory) ReadFromAddressAsElement(address *MemoryAddress) (f.Element, error)

Works the same as `ReadFromAddress` but `MemoryValue` is converted to `Element` first

func (*Memory) RelocationOffsets

func (memory *Memory) RelocationOffsets() ([]uint64, uint64)

It returns all segment offsets and max memory used

func (*Memory) ResolveAsBigInt3

func (memory *Memory) ResolveAsBigInt3(valAddr MemoryAddress) ([3]*f.Element, error)

func (*Memory) ResolveAsBigInt5

func (memory *Memory) ResolveAsBigInt5(valAddr MemoryAddress) ([5]*f.Element, error)

func (*Memory) ResolveAsEcPoint

func (memory *Memory) ResolveAsEcPoint(valAddr MemoryAddress) ([2]*f.Element, error)

func (*Memory) Write

func (memory *Memory) Write(segmentIndex uint64, offset uint64, value *MemoryValue) error

Writes to a given segment index and offset a new memory value. Errors if writing to an unallocated segment or if overwriting a different memory value

func (*Memory) WriteToAddress

func (memory *Memory) WriteToAddress(address *MemoryAddress, value *MemoryValue) error

Writes to a memory address a new memory value. Errors if writing to an unallocated segment or if overwriting a different memory value

func (*Memory) WriteToNthStructField

func (memory *Memory) WriteToNthStructField(addr MemoryAddress, value MemoryValue, field int16) error

func (*Memory) WriteUint256ToAddress

func (memory *Memory) WriteUint256ToAddress(addr MemoryAddress, low, high *f.Element) error

type MemoryAddress

type MemoryAddress struct {
	SegmentIndex uint64
	Offset       uint64
}

Represents a Memory Address of the Cairo VM. Because memory is split between different segments during execution, addresses has two locators: the segment they belong to and the location inside that segment

func (*MemoryAddress) Add

func (address *MemoryAddress) Add(lhs *MemoryAddress, rhs *f.Element) error

Adds a memory address and a field element

func (*MemoryAddress) AddOffset

func (address *MemoryAddress) AddOffset(offset int16) (MemoryAddress, error)

It crates a new memory address with the modified offset

func (*MemoryAddress) Cmp

func (address *MemoryAddress) Cmp(other *MemoryAddress) int

func (*MemoryAddress) Equal

func (address *MemoryAddress) Equal(other *MemoryAddress) bool

func (*MemoryAddress) Relocate

func (address *MemoryAddress) Relocate(segmentsOffset []uint64) *f.Element

func (MemoryAddress) String

func (address MemoryAddress) String() string

func (*MemoryAddress) Sub

func (address *MemoryAddress) Sub(lhs *MemoryAddress, rhs *f.Element) error

Subtracts a memory address and a field element

func (*MemoryAddress) SubAddress

func (address *MemoryAddress) SubAddress(other *MemoryAddress) (uint64, error)

type MemoryValue

type MemoryValue struct {
	Felt f.Element
	Kind memoryValueKind
}

Stores all posible types that can be stored in a Memory cell,

  • either a Felt value (an `f.Element`),
  • or a pointer to another Memory Cell (a `MemoryAddress`) both values share the same underlying memory, which is a f.Element

func EmptyMemoryValueAs

func EmptyMemoryValueAs(address bool) MemoryValue

func EmptyMemoryValueAsAddress

func EmptyMemoryValueAsAddress() MemoryValue

func EmptyMemoryValueAsFelt

func EmptyMemoryValueAsFelt() MemoryValue

func MemoryValueFromAny

func MemoryValueFromAny(anyType any) (MemoryValue, error)

func MemoryValueFromFieldElement

func MemoryValueFromFieldElement(felt *f.Element) MemoryValue

func MemoryValueFromInt

func MemoryValueFromInt[T constraints.Integer](v T) MemoryValue

func MemoryValueFromMemoryAddress

func MemoryValueFromMemoryAddress(address *MemoryAddress) MemoryValue

func MemoryValueFromSegmentAndOffset

func MemoryValueFromSegmentAndOffset[T constraints.Integer](segmentIndex, offset T) MemoryValue

creates a memory value from an index and an offset. If either is negative the result is undefined

func MemoryValueFromUint

func MemoryValueFromUint[T constraints.Unsigned](v T) MemoryValue

func (*MemoryValue) Add

func (mv *MemoryValue) Add(lhs, rhs *MemoryValue) error

Adds two memory values if the second one is a Felt

func (*MemoryValue) Any

func (mv *MemoryValue) Any() any

func (*MemoryValue) Div

func (mv *MemoryValue) Div(lhs, rhs *MemoryValue) error

func (*MemoryValue) Equal

func (mv *MemoryValue) Equal(other *MemoryValue) bool

func (*MemoryValue) FieldElement

func (mv *MemoryValue) FieldElement() (*f.Element, error)

func (*MemoryValue) IsAddress

func (mv *MemoryValue) IsAddress() bool

func (*MemoryValue) IsFelt

func (mv *MemoryValue) IsFelt() bool

func (*MemoryValue) IsZero

func (mv *MemoryValue) IsZero() bool

func (*MemoryValue) Known

func (mv *MemoryValue) Known() bool

func (*MemoryValue) MemoryAddress

func (mv *MemoryValue) MemoryAddress() (*MemoryAddress, error)

func (*MemoryValue) Mul

func (mv *MemoryValue) Mul(lhs, rhs *MemoryValue) error

func (MemoryValue) String

func (mv MemoryValue) String() string

func (*MemoryValue) Sub

func (mv *MemoryValue) Sub(lhs, rhs *MemoryValue) error

Subs two memory values if they're in the same segment or the rhs is a Felt.

func (*MemoryValue) Uint64

func (mv *MemoryValue) Uint64() (uint64, error)

Returns a MemoryValue holding a felt as uint if it fits

type NoBuiltin

type NoBuiltin struct{}

func (*NoBuiltin) CheckWrite

func (b *NoBuiltin) CheckWrite(segment *Segment, offset uint64, value *MemoryValue) error

func (*NoBuiltin) GetAllocatedSize

func (b *NoBuiltin) GetAllocatedSize(segmentUsedSize uint64, vmCurrentStep uint64) (uint64, error)

func (*NoBuiltin) GetCellsPerInstance

func (b *NoBuiltin) GetCellsPerInstance() uint64

func (*NoBuiltin) GetStopPointer

func (b *NoBuiltin) GetStopPointer() uint64

func (*NoBuiltin) InferValue

func (b *NoBuiltin) InferValue(segment *Segment, offset uint64) error

func (*NoBuiltin) SetStopPointer

func (b *NoBuiltin) SetStopPointer(stopPointer uint64)

func (*NoBuiltin) String

func (b *NoBuiltin) String() string

type PublicMemoryOffset

type PublicMemoryOffset struct {
	Address uint16
	Page    uint16
}

type Segment

type Segment struct {
	Data []MemoryValue
	// the max index where a value was written
	LastIndex           int
	BuiltinRunner       BuiltinRunner
	PublicMemoryOffsets []PublicMemoryOffset
}

func EmptySegment

func EmptySegment() *Segment

func EmptySegmentWithCapacity

func EmptySegmentWithCapacity(capacity int) *Segment

func EmptySegmentWithLength

func EmptySegmentWithLength(length int) *Segment

func (*Segment) Finalize

func (segment *Segment) Finalize(newSize uint64, publicMemoryOffsets []PublicMemoryOffset)

func (*Segment) IncreaseSegmentSize

func (segment *Segment) IncreaseSegmentSize(newSize uint64)

Increase a segment allocated space. Panics if the new size is smaller

func (*Segment) Len

func (segment *Segment) Len() uint64

returns the effective size of a segment length i.e the rightmost element index + 1

func (*Segment) Peek

func (segment *Segment) Peek(offset uint64) MemoryValue

func (*Segment) Read

func (segment *Segment) Read(offset uint64) (MemoryValue, error)

Reads a memory value from a specified offset at the segment

func (*Segment) RealLen

func (segment *Segment) RealLen() uint64

returns the real length that a segment has

func (*Segment) String

func (segment *Segment) String() string

func (*Segment) WithBuiltinRunner

func (segment *Segment) WithBuiltinRunner(builtinRunner BuiltinRunner) *Segment

func (*Segment) Write

func (segment *Segment) Write(offset uint64, value *MemoryValue) error

Writes a new memory value to a specified offset, errors in case of overwriting a different memory value

Jump to

Keyboard shortcuts

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