-
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.
Merge branch 'main' of github.com:lambdaclass/cairo-vm.go into HEAD
- Loading branch information
Showing
29 changed files
with
1,019 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
%builtins range_check bitwise | ||
|
||
from starkware.cairo.common.alloc import alloc | ||
from starkware.cairo.common.cairo_blake2s.blake2s import blake2s | ||
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin | ||
|
||
// Computes the hash of "Hello World" | ||
func main{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}() { | ||
alloc_locals; | ||
let inputs: felt* = alloc(); | ||
assert inputs[0] = 'Hell'; | ||
assert inputs[1] = 'o Wo'; | ||
assert inputs[2] = 'rld'; | ||
let (local blake2s_ptr_start) = alloc(); | ||
let blake2s_ptr = blake2s_ptr_start; | ||
let (output) = blake2s{range_check_ptr=range_check_ptr, blake2s_ptr=blake2s_ptr}(inputs, 9); | ||
assert output.low = 219917655069954262743903159041439073909; | ||
assert output.high = 296157033687865319468534978667166017272; | ||
return (); | ||
} |
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
Oops, something went wrong.