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

Add MemorySegmentManager.GetFeltRange #272

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 14 additions & 0 deletions pkg/vm/memory/segments.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,17 @@ func (m *MemorySegmentManager) Finalize(size *uint, segmentIndex uint, publicMem
m.PublicMemoryOffsets[segmentIndex] = emptyList
}
}

// Gets a range of Felt memory values from addr to addr + size
// Fails if there if any of the values inside the range is missing (memory gap), or is not a Felt
fmoletta marked this conversation as resolved.
Show resolved Hide resolved
func (m *MemorySegmentManager) GetFeltRange(start Relocatable, size uint) ([]lambdaworks.Felt, error) {
feltRange := make([]lambdaworks.Felt, 0, size)
for i := uint(0); i < size; i++ {
val, err := m.Memory.GetFelt(start.AddUint(i))
if err != nil {
return nil, err
}
feltRange = append(feltRange, val)
}
return feltRange, nil
}
60 changes: 60 additions & 0 deletions pkg/vm/memory/segments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,63 @@ func TestGetMemoryHoles(t *testing.T) {
t.Errorf("Get Memory Holes Returned the wrong value. Expected: 2, got %d", result)
}
}

func TestGetFeltRangeOk(t *testing.T) {
segments := memory.NewMemorySegmentManager()
segments.AddSegment()
segments.Memory.Insert(memory.NewRelocatable(0, 1), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(1)))
segments.Memory.Insert(memory.NewRelocatable(0, 2), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(2)))
segments.Memory.Insert(memory.NewRelocatable(0, 3), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(3)))
segments.Memory.Insert(memory.NewRelocatable(0, 4), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(4)))
segments.Memory.Insert(memory.NewRelocatable(0, 5), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(5)))
segments.Memory.Insert(memory.NewRelocatable(0, 6), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(6)))
segments.Memory.Insert(memory.NewRelocatable(0, 7), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(7)))

feltRange, err := segments.GetFeltRange(memory.NewRelocatable(0, 2), 4)
expectedFeltRange := []lambdaworks.Felt{
lambdaworks.FeltFromUint64(2),
lambdaworks.FeltFromUint64(3),
lambdaworks.FeltFromUint64(4),
lambdaworks.FeltFromUint64(5),
}

if err != nil || !reflect.DeepEqual(feltRange, expectedFeltRange) {
t.Errorf("GetFeltRange failed or returned wrong value")
}
}

func TestGetFeltRangeGap(t *testing.T) {
segments := memory.NewMemorySegmentManager()
segments.AddSegment()
segments.Memory.Insert(memory.NewRelocatable(0, 1), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(1)))
segments.Memory.Insert(memory.NewRelocatable(0, 2), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(2)))
//segments.Memory.Insert(memory.NewRelocatable(0, 3), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(3)))
segments.Memory.Insert(memory.NewRelocatable(0, 4), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(4)))
segments.Memory.Insert(memory.NewRelocatable(0, 5), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(5)))
segments.Memory.Insert(memory.NewRelocatable(0, 6), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(6)))
segments.Memory.Insert(memory.NewRelocatable(0, 7), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(7)))

_, err := segments.GetFeltRange(memory.NewRelocatable(0, 2), 4)

if err == nil {
t.Errorf("GetFeltRange should have failed")
}
}

func TestGetFeltRangeRelocatable(t *testing.T) {
segments := memory.NewMemorySegmentManager()
segments.AddSegment()
segments.Memory.Insert(memory.NewRelocatable(0, 1), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(1)))
segments.Memory.Insert(memory.NewRelocatable(0, 2), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(2)))
segments.Memory.Insert(memory.NewRelocatable(0, 3), memory.NewMaybeRelocatableRelocatable(memory.NewRelocatable(0, 0)))
segments.Memory.Insert(memory.NewRelocatable(0, 4), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(4)))
segments.Memory.Insert(memory.NewRelocatable(0, 5), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(5)))
segments.Memory.Insert(memory.NewRelocatable(0, 6), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(6)))
segments.Memory.Insert(memory.NewRelocatable(0, 7), memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(7)))

_, err := segments.GetFeltRange(memory.NewRelocatable(0, 2), 4)

if err == nil {
t.Errorf("GetFeltRange should have failed")
}
}