Skip to content

Commit

Permalink
Chore(builtins): Add output builtin check (#132)
Browse files Browse the repository at this point in the history
Add output builtin check
  • Loading branch information
rodrigo-pino authored Oct 23, 2023
1 parent 16c29c6 commit 1feb71e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/runners/zero/zero.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ func (runner *ZeroRunner) Output() []*fp.Element {

for offset := uint64(0); offset < outputSegment.Len(); offset++ {
value := outputSegment.Peek(offset)
// todo(rodro): check if output can only contains field elements
// no need to check for an error here since only felts can be written
// to the output segment
valueFelt, _ := value.FieldElement()
output = append(output, valueFelt)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/vm/builtins/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package builtins

import (
"errors"
"fmt"

"github.com/NethermindEth/cairo-vm-go/pkg/vm/memory"
)
Expand All @@ -11,6 +12,9 @@ const OutputName = "output"
type Output struct{}

func (o *Output) CheckWrite(segment *memory.Segment, offset uint64, value *memory.MemoryValue) error {
if !value.IsFelt() {
return fmt.Errorf("expected a felt but got an address: %s", value)
}
return nil
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/vm/builtins/output_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package builtins

import (
"testing"

"github.com/NethermindEth/cairo-vm-go/pkg/vm/memory"
"github.com/stretchr/testify/require"
)

func TestOutput(t *testing.T) {
output := &Output{}
segment := memory.EmptySegmentWithLength(5).WithBuiltinRunner(output)

mv1 := memory.MemoryValueFromInt(5)
err := segment.Write(0, &mv1)
require.NoError(t, err)

mv2 := memory.MemoryValueFromSegmentAndOffset(1, 2)
err = segment.Write(1, &mv2)
require.ErrorContains(t, err, "expected a felt but got an address")

}
1 change: 0 additions & 1 deletion pkg/vm/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ func (segment *Segment) String() string {
return header
}

// todo(rodro): Check out temprary segments
// Represents the whole VM memory divided into segments
type Memory struct {
Segments []*Segment
Expand Down

0 comments on commit 1feb71e

Please sign in to comment.