-
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 needed args in ExecuteHint * Add pow hint * Finish unit test * Add sqrt hint * Add cairo config to tests --------- Co-authored-by: juan.mv <[email protected]> Co-authored-by: Mariano A. Nicolini <[email protected]>
- Loading branch information
1 parent
9887892
commit 4135dd1
Showing
10 changed files
with
181 additions
and
0 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,18 @@ | ||
%builtins range_check | ||
|
||
from starkware.cairo.common.pow import pow | ||
|
||
func main{range_check_ptr: felt}() { | ||
let (x) = pow(2, 3); | ||
assert x = 8; | ||
let (y) = pow(10, 6); | ||
assert y = 1000000; | ||
let (z) = pow(152, 25); | ||
assert z = 3516330588649452857943715400722794159857838650852114432; | ||
let (u) = pow(-2, 3); | ||
assert (u) = -8; | ||
let (v) = pow(-25, 31); | ||
assert (v) = -21684043449710088680149056017398834228515625; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
%builtins range_check | ||
|
||
from starkware.cairo.common.math import sqrt | ||
|
||
func main{range_check_ptr: felt}() { | ||
let result_a = sqrt(0); | ||
assert result_a = 0; | ||
|
||
let result_b = sqrt(2402); | ||
assert result_b = 49; | ||
|
||
let result_c = sqrt(361850278866613121369732278309507010562); | ||
assert result_c = 19022362599493605525; | ||
|
||
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
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,3 @@ | ||
package hints | ||
|
||
const POW = "ids.locs.bit = (ids.prev_locs.exp % PRIME) & 1" |
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,22 @@ | ||
package hints | ||
|
||
import ( | ||
. "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_utils" | ||
. "github.com/lambdaclass/cairo-vm.go/pkg/lambdaworks" | ||
. "github.com/lambdaclass/cairo-vm.go/pkg/vm" | ||
. "github.com/lambdaclass/cairo-vm.go/pkg/vm/memory" | ||
) | ||
|
||
// Implements hint: | ||
// %{ ids.locs.bit = (ids.prev_locs.exp % PRIME) & 1 %} | ||
func pow(ids IdsManager, vm *VirtualMachine) error { | ||
prev_locs_exp_addr, err := ids.GetAddr("prev_locs", vm) | ||
prev_locs_exp, _ := vm.Segments.Memory.GetFelt(prev_locs_exp_addr.AddUint(4)) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
ids.Insert("locs", NewMaybeRelocatableFelt(prev_locs_exp.And(FeltOne())), vm) | ||
return nil | ||
} |
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,42 @@ | ||
package hints_test | ||
|
||
import ( | ||
. "github.com/lambdaclass/cairo-vm.go/pkg/hints" | ||
. "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_utils" | ||
. "github.com/lambdaclass/cairo-vm.go/pkg/lambdaworks" | ||
. "github.com/lambdaclass/cairo-vm.go/pkg/vm" | ||
. "github.com/lambdaclass/cairo-vm.go/pkg/vm/memory" | ||
"testing" | ||
) | ||
|
||
func TestPowHintOk(t *testing.T) { | ||
vm := NewVirtualMachine() | ||
vm.Segments.AddSegment() | ||
vm.Segments.Memory.Insert(NewRelocatable(0, 4), NewMaybeRelocatableFelt(FeltFromUint64(5))) | ||
idsManager := SetupIdsForTest( | ||
map[string][]*MaybeRelocatable{ | ||
"prev_locs": {NewMaybeRelocatableRelocatable(NewRelocatable(0, 0))}, | ||
"locs": {nil}, | ||
}, | ||
vm, | ||
) | ||
hintProcessor := CairoVmHintProcessor{} | ||
hintData := any(HintData{ | ||
Ids: idsManager, | ||
Code: POW, | ||
}) | ||
|
||
err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil) | ||
if err != nil { | ||
t.Errorf("POW hint test failed with error %s", err) | ||
} | ||
|
||
locs, err := idsManager.GetFelt("locs", vm) | ||
if err != nil { | ||
t.Errorf("Failed to get locs.bit with error: %s", err) | ||
} | ||
|
||
if locs != FeltFromUint64(1) { | ||
t.Errorf("locs.bit: %d != 1", locs) | ||
} | ||
} |
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