-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ExecutionResources struct * Fix: Remove vm argument from CairoRunner methods * Finish func + remove todos * Add unit test * Implement GenArg * Remove recursive processing * Add unit tests * Fix test values * Start fn * Add RunFromEntryPoint * Add test for RunFromEntryPoint * Add unit tests * Add comments * Add skeleton * Implement GetMemoryAccesses for BuiltinRunner * Start implementing RunSecurityChecks * Implement VerifyAutoDeductionsForAddr * Finish func + remove import cycle * Add fix + unit test * Fix logic, add test * Fix logic * Fix logic * Fix logic + add test * Add unit tests * Move RunSecurityChecks to external func in builtins package * Remove todos from BuiltinRunner interface * Finish verifySecureRunner * Add verifySecure to RunFromEntryPoint * Add unit tests * Add secure_run flag * Fix typo * Fix alias for new flag * Set secure_run to true in testProgram --------- Co-authored-by: Pedro Fontana <[email protected]>
- Loading branch information
Showing
17 changed files
with
435 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package builtins_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/lambdaclass/cairo-vm.go/pkg/builtins" | ||
"github.com/lambdaclass/cairo-vm.go/pkg/lambdaworks" | ||
"github.com/lambdaclass/cairo-vm.go/pkg/vm/memory" | ||
) | ||
|
||
func TestRunSecurityChecksEmptyMemory(t *testing.T) { | ||
builtin := builtins.NewBitwiseBuiltinRunner(256) | ||
segments := memory.NewMemorySegmentManager() | ||
err := builtins.RunSecurityChecksForBuiltin(builtin, &segments) | ||
if err != nil { | ||
t.Errorf("RunSecurityChecks failed with error: %s", err.Error()) | ||
} | ||
} | ||
|
||
func TestRunSecurityChecksMissingMemoryCells(t *testing.T) { | ||
builtin := builtins.NewBitwiseBuiltinRunner(256) | ||
segments := memory.NewMemorySegmentManager() | ||
|
||
builtin.InitializeSegments(&segments) | ||
builtinBase := builtin.Base() | ||
// A bitwise cell consists of 5 elements: 2 input cells & 3 output cells | ||
// In this test we insert cells 4-5 and leave the first input cell empty | ||
// This will fail the security checks, as the memory cell with offset 0 will be missing | ||
builtinSegment := []memory.MaybeRelocatable{ | ||
*memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint(1)), | ||
*memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint(2)), | ||
*memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint(3)), | ||
*memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint(4)), | ||
} | ||
segments.LoadData(builtinBase.AddUint(1), &builtinSegment) | ||
|
||
err := builtins.RunSecurityChecksForBuiltin(builtin, &segments) | ||
if err == nil { | ||
t.Errorf("RunSecurityChecks should have failed") | ||
} | ||
} | ||
|
||
func TestRunSecurityChecksMissingMemoryCellsNCheck(t *testing.T) { | ||
builtin := builtins.NewBitwiseBuiltinRunner(256) | ||
segments := memory.NewMemorySegmentManager() | ||
|
||
builtin.InitializeSegments(&segments) | ||
builtinBase := builtin.Base() | ||
// n = max(offsets) // cellsPerInstance + 1 | ||
// n = max[(0]) // 5 + 1 = 0 // 5 + 1 = 1 | ||
// len(offsets) // inputCells = 1 // 2 | ||
// This will fail the security checks, as n > len(offsets) // inputCells | ||
builtinSegment := []memory.MaybeRelocatable{ | ||
*memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint(1)), | ||
} | ||
segments.LoadData(builtinBase, &builtinSegment) | ||
|
||
err := builtins.RunSecurityChecksForBuiltin(builtin, &segments) | ||
if err == nil { | ||
t.Errorf("RunSecurityChecks should have failed") | ||
} | ||
} | ||
|
||
func TestRunSecurityChecksValidateOutputCellsNotDeducedOk(t *testing.T) { | ||
builtin := builtins.NewBitwiseBuiltinRunner(256) | ||
segments := memory.NewMemorySegmentManager() | ||
|
||
builtin.InitializeSegments(&segments) | ||
builtinBase := builtin.Base() | ||
// A bitwise cell consists of 5 elements: 2 input cells & 3 output cells | ||
// In this test we insert the input cells (1-2), but not the output cells (3-5) | ||
// This will cause the security checks to run the auto-deductions for those output cells | ||
builtinSegment := []memory.MaybeRelocatable{ | ||
*memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint(1)), | ||
*memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint(2)), | ||
} | ||
segments.LoadData(builtinBase, &builtinSegment) | ||
|
||
err := builtins.RunSecurityChecksForBuiltin(builtin, &segments) | ||
if err != nil { | ||
t.Errorf("RunSecurityChecks failed with error: %s", err.Error()) | ||
} | ||
} | ||
|
||
func TestRunSecurityChecksValidateOutputCellsNotDeducedErr(t *testing.T) { | ||
builtin := builtins.NewBitwiseBuiltinRunner(256) | ||
segments := memory.NewMemorySegmentManager() | ||
|
||
builtin.InitializeSegments(&segments) | ||
builtinBase := builtin.Base() | ||
// A bitwise cell consists of 5 elements: 2 input cells & 3 output cells | ||
// In this test we insert the input cells (1-2), but not the output cells (3-5) | ||
// This will cause the security checks to run the auto-deductions for those output cells | ||
// As we inserted an invalid value (PRIME -1) on the first input cell, this deduction will fail | ||
builtinSegment := []memory.MaybeRelocatable{ | ||
*memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromDecString("-1")), | ||
*memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint(2)), | ||
} | ||
segments.LoadData(builtinBase, &builtinSegment) | ||
|
||
err := builtins.RunSecurityChecksForBuiltin(builtin, &segments) | ||
if err == nil { | ||
t.Errorf("RunSecurityChecks should have failed") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.