diff --git a/pkg/runners/zero/zero.go b/pkg/runners/zero/zero.go index 07832b9c8..e428e8b4c 100644 --- a/pkg/runners/zero/zero.go +++ b/pkg/runners/zero/zero.go @@ -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) } diff --git a/pkg/vm/builtins/output.go b/pkg/vm/builtins/output.go index 20477c598..bf8751ccd 100644 --- a/pkg/vm/builtins/output.go +++ b/pkg/vm/builtins/output.go @@ -2,6 +2,7 @@ package builtins import ( "errors" + "fmt" "github.com/NethermindEth/cairo-vm-go/pkg/vm/memory" ) @@ -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 } diff --git a/pkg/vm/builtins/output_test.go b/pkg/vm/builtins/output_test.go new file mode 100644 index 000000000..36982b6d0 --- /dev/null +++ b/pkg/vm/builtins/output_test.go @@ -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") + +} diff --git a/pkg/vm/memory/memory.go b/pkg/vm/memory/memory.go index f5d6c0629..1bc0b4d0a 100644 --- a/pkg/vm/memory/memory.go +++ b/pkg/vm/memory/memory.go @@ -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