From 0424a11bca06889d7c0b42f850e7959bb79d3e9e Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 19 Sep 2023 17:55:55 -0300 Subject: [PATCH 1/3] Add MemorySegmentManager.GetFeltRange --- pkg/vm/memory/segments.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/vm/memory/segments.go b/pkg/vm/memory/segments.go index d22d62f7..659304f8 100644 --- a/pkg/vm/memory/segments.go +++ b/pkg/vm/memory/segments.go @@ -172,3 +172,15 @@ func (m *MemorySegmentManager) Finalize(size *uint, segmentIndex uint, publicMem m.PublicMemoryOffsets[segmentIndex] = emptyList } } + +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 +} From 15ed7c3fea460e0e743947afed2ac57dcfc6b499 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 19 Sep 2023 18:04:48 -0300 Subject: [PATCH 2/3] Add unit test + comment --- pkg/vm/memory/segments.go | 2 ++ pkg/vm/memory/segments_test.go | 60 ++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/pkg/vm/memory/segments.go b/pkg/vm/memory/segments.go index 659304f8..bf13a066 100644 --- a/pkg/vm/memory/segments.go +++ b/pkg/vm/memory/segments.go @@ -173,6 +173,8 @@ func (m *MemorySegmentManager) Finalize(size *uint, segmentIndex uint, publicMem } } +// 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 func (m *MemorySegmentManager) GetFeltRange(start Relocatable, size uint) ([]lambdaworks.Felt, error) { feltRange := make([]lambdaworks.Felt, 0, size) for i := uint(0); i < size; i++ { diff --git a/pkg/vm/memory/segments_test.go b/pkg/vm/memory/segments_test.go index d5967b74..cdca3585 100644 --- a/pkg/vm/memory/segments_test.go +++ b/pkg/vm/memory/segments_test.go @@ -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") + } +} From d98526f3057c0efdb064b64d3cc26a008b842b39 Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Wed, 20 Sep 2023 10:26:39 -0300 Subject: [PATCH 3/3] Update segments.go --- pkg/vm/memory/segments.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vm/memory/segments.go b/pkg/vm/memory/segments.go index bf13a066..732ee0b7 100644 --- a/pkg/vm/memory/segments.go +++ b/pkg/vm/memory/segments.go @@ -174,7 +174,7 @@ func (m *MemorySegmentManager) Finalize(size *uint, segmentIndex uint, publicMem } // 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 +// Fails if any of the values inside the range is missing (memory gap), or is not a Felt func (m *MemorySegmentManager) GetFeltRange(start Relocatable, size uint) ([]lambdaworks.Felt, error) { feltRange := make([]lambdaworks.Felt, 0, size) for i := uint(0); i < size; i++ {