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

Organize hintrunner/operand.go #39

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Changes from all 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
52 changes: 26 additions & 26 deletions pkg/hintrunner/operand.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ type CellRefer interface {

type ApCellRef int16

type FpCellRef int16

func (ap ApCellRef) Get(vm *VM.VirtualMachine) (*memory.Cell, error) {
res, overflow := safemath.SafeOffset(vm.Context.Ap, int16(ap))
if overflow {
Expand All @@ -41,6 +39,8 @@ func (ap ApCellRef) Get(vm *VM.VirtualMachine) (*memory.Cell, error) {
return vm.MemoryManager.Memory.Peek(VM.ExecutionSegment, res)
}

type FpCellRef int16

func (fp FpCellRef) Get(vm *VM.VirtualMachine) (*memory.Cell, error) {
res, overflow := safemath.SafeOffset(vm.Context.Fp, int16(fp))
if overflow {
Expand All @@ -55,34 +55,10 @@ func (fp FpCellRef) Get(vm *VM.VirtualMachine) (*memory.Cell, error) {
//
// All ResOperand definitions

type ResOperander interface {
Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, error)
}

type Deref struct {
deref CellRefer
}

type DoubleDeref struct {
deref CellRefer
offset int16
}

type Immediate big.Int

type Operator uint8

const (
Add Operator = iota
Mul
)

type BinaryOp struct {
operator Operator
lhs CellRefer
rhs ResOperander // (except DoubleDeref and BinaryOp)
}

func (deref Deref) Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, error) {
cell, err := deref.deref.Get(vm)
if err != nil {
Expand All @@ -91,6 +67,11 @@ func (deref Deref) Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, error) {
return cell.Read(), nil
}

type DoubleDeref struct {
deref CellRefer
offset int16
}

func (dderef DoubleDeref) Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, error) {
cell, err := dderef.deref.Get(vm)
if err != nil {
Expand Down Expand Up @@ -121,6 +102,8 @@ func (dderef DoubleDeref) Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, e
return value, nil
}

type Immediate big.Int

// todo(rodro): Specs from Starkware stablish this can be uint256 and not a felt.
// Should we respect that, or go straight to felt?
func (imm Immediate) Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, error) {
Expand All @@ -133,6 +116,23 @@ func (imm Immediate) Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, error)
return memory.MemoryValueFromFieldElement(felt), nil
}

type Operator uint8

const (
Add Operator = iota
Mul
)

type ResOperander interface {
Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, error)
}
joshklop marked this conversation as resolved.
Show resolved Hide resolved

type BinaryOp struct {
operator Operator
lhs CellRefer
rhs ResOperander // (except DoubleDeref and BinaryOp)
}

func (bop BinaryOp) Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, error) {
cell, err := bop.lhs.Get(vm)
if err != nil {
Expand Down