-
Notifications
You must be signed in to change notification settings - Fork 13
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
Assorted hints2 #305
Open
mmsc2
wants to merge
10
commits into
main
Choose a base branch
from
AssortedHints2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Assorted hints2 #305
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1429c01
Safe Div hint
mmsc2 1d5ef6e
Merge branch 'main' into AssortedHints2
mmsc2 0aaf96e
Div safe test
mmsc2 e6f8acc
Fix broken test
mmsc2 7522950
Bigint pack div mod test
mmsc2 465cb15
Fix bigint pack div mod test
mmsc2 d34b0f2
Delete debug info
mmsc2 f76eeaa
Assert le felt v 0 6 hint
mmsc2 6a0b68c
Assert le felt v 0 8 hint and tests
mmsc2 3e1cb06
Merge branch 'main' into AssortedHints2
mmsc2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -6,7 +6,9 @@ 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/math_utils" | ||
. "github.com/lambdaclass/cairo-vm.go/pkg/types" | ||
. "github.com/lambdaclass/cairo-vm.go/pkg/utils" | ||
. "github.com/lambdaclass/cairo-vm.go/pkg/vm" | ||
"github.com/lambdaclass/cairo-vm.go/pkg/vm/memory" | ||
) | ||
|
@@ -20,8 +22,8 @@ Implements hint: | |
%} | ||
*/ | ||
|
||
func NondetBigInt3(virtual_machine VirtualMachine, execScopes ExecutionScopes, idsData IdsManager) error { | ||
resRelloc, err := idsData.GetAddr("res", &virtual_machine) | ||
func NondetBigInt3(vm VirtualMachine, execScopes ExecutionScopes, idsData IdsManager) error { | ||
resRelloc, err := idsData.GetAddr("res", &vm) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -47,10 +49,142 @@ func NondetBigInt3(virtual_machine VirtualMachine, execScopes ExecutionScopes, i | |
arg = append(arg, *m) | ||
} | ||
|
||
_, loadErr := virtual_machine.Segments.LoadData(resRelloc, &arg) | ||
_, loadErr := vm.Segments.LoadData(resRelloc, &arg) | ||
if loadErr != nil { | ||
return loadErr | ||
} | ||
|
||
return nil | ||
} | ||
|
||
/// Implements hint: | ||
/// ```python | ||
/// k = safe_div(res * y - x, p) | ||
/// value = k if k > 0 else 0 - k | ||
/// ids.flag = 1 if k > 0 else 0 | ||
/// ``` | ||
|
||
func SafeDivBigint(vm *VirtualMachine, execScopes *ExecutionScopes, idsData IdsManager) error { | ||
resUncast, err := execScopes.Get("res") | ||
if err != nil { | ||
return err | ||
} | ||
res, ok := resUncast.(big.Int) | ||
if !ok { | ||
return errors.New("Could not cast res value in SafeDivBigint") | ||
} | ||
|
||
yUncast, err := execScopes.Get("y") | ||
if err != nil { | ||
return err | ||
} | ||
y, ok := yUncast.(big.Int) | ||
if !ok { | ||
return errors.New("Could not cast y value in SafeDivBigint") | ||
} | ||
|
||
xUncast, err := execScopes.Get("x") | ||
if err != nil { | ||
return err | ||
} | ||
x, ok := xUncast.(big.Int) | ||
if !ok { | ||
return errors.New("Could not cast x value in SafeDivBigint") | ||
} | ||
|
||
pUncast, err := execScopes.Get("p") | ||
if err != nil { | ||
return err | ||
} | ||
p, ok := pUncast.(big.Int) | ||
if !ok { | ||
return errors.New("Could not cast p value in SafeDivBigint") | ||
} | ||
|
||
param_x := new(big.Int).Mul(&res, &y) | ||
param_x.Sub(param_x, &x) | ||
|
||
k, err := SafeDivBig(param_x, &p) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var value big.Int | ||
var flag lambdaworks.Felt | ||
|
||
// check if k is positive | ||
if k.Cmp(big.NewInt(0)) == 1 { | ||
value = *k | ||
flag = lambdaworks.FeltFromUint(1) | ||
} else { | ||
value = *new(big.Int).Neg(k) | ||
flag = lambdaworks.FeltFromUint(0) | ||
} | ||
|
||
execScopes.AssignOrUpdateVariable("k", *k) | ||
execScopes.AssignOrUpdateVariable("value", value) | ||
|
||
val := memory.NewMaybeRelocatableFelt(flag) | ||
idsData.Insert("flag", val, vm) | ||
|
||
return nil | ||
} | ||
|
||
func calculateX(vm *VirtualMachine, idsData IdsManager) (big.Int, error) { | ||
x_bigint5, err := LimbsFromVarName(5, "x", idsData, vm) | ||
if err != nil { | ||
return big.Int{}, err | ||
} | ||
// pack only takes the first three limbs | ||
x0 := x_bigint5[0] | ||
x1 := x_bigint5[1] | ||
x2 := x_bigint5[2] | ||
|
||
var limbs = []lambdaworks.Felt{x0, x1, x2} | ||
|
||
x_lower := BigInt3{Limbs: limbs} | ||
x_lower_bigint := x_lower.Pack86() | ||
|
||
d3 := x_bigint5[3].ToSigned() | ||
d4 := x_bigint5[4].ToSigned() | ||
|
||
base := BASE() | ||
d3.Mul(d3, base.Exp(&base, big.NewInt(3), nil)) | ||
d4.Mul(d4, base.Exp(&base, big.NewInt(4), nil)) | ||
|
||
x_lower_bigint.Add(&x_lower_bigint, d3) | ||
x_lower_bigint.Add(&x_lower_bigint, d4) | ||
|
||
return x_lower_bigint, nil | ||
} | ||
|
||
func bigintPackDivMod(vm *VirtualMachine, execScopes *ExecutionScopes, idsData IdsManager) error { | ||
|
||
pUnpack, err := BigInt3FromVarName("P", idsData, vm) | ||
if err != nil { | ||
return err | ||
} | ||
p := pUnpack.Pack86() | ||
|
||
x, err := calculateX(vm, idsData) | ||
if err != nil { | ||
return err | ||
} | ||
x1 := x | ||
|
||
yUnpacked, err := BigInt3FromVarName("y", idsData, vm) | ||
if err != nil { | ||
return err | ||
} | ||
y := yUnpacked.Pack86() | ||
|
||
res, _ := math_utils.DivMod(&x1, &y, &p) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dont ignore the error |
||
|
||
execScopes.AssignOrUpdateVariable("res", *res) | ||
execScopes.AssignOrUpdateVariable("value", *res) | ||
execScopes.AssignOrUpdateVariable("x", x) | ||
execScopes.AssignOrUpdateVariable("y", y) | ||
execScopes.AssignOrUpdateVariable("p", p) | ||
|
||
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
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle this error