-
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
Ec slope hint #257
Merged
Merged
Ec slope hint #257
Changes from 35 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
977ba29
Add ec hints
mmsc2 70a9db2
Implement hints
mmsc2 864b7b8
Add the hints to the processor
mmsc2 7df4192
Test pack86 function
mmsc2 1a66309
Test hint
mmsc2 91ffa57
Delete debug info, Test ec negative op
mmsc2 7bf1909
Second hint test
mmsc2 5d2d256
Merge main
mmsc2 a08c642
Test embedded hint
mmsc2 029fe50
Merge branch 'main' into EcHint
mmsc2 762a566
Merge branch 'main' into EcHint
mmsc2 6c6eed9
Change to Camel case
mmsc2 ec9345c
Implement slope hints
mmsc2 9d14678
Fix merge conflict
mmsc2 ff7fb50
Fix format
mmsc2 4e9afc5
Delete github conflict string
mmsc2 cc53b65
Tests hints
mmsc2 a0d269b
Tests hints slopes
mmsc2 c25e25c
Fix merge conflict
mmsc2 a27b814
Rename misleading name function
mmsc2 51fedf0
Merge branch 'main' into EcSlopeHint
mmsc2 9e39502
Fix function name
mmsc2 327299b
Fix format
mmsc2 0fdad83
Fix error in function call
mmsc2 ba81ca8
Merge branch 'main' into EcSlopeHint
entropidelic 78dd5d0
Delete debug info
mmsc2 c023bea
Merge branch 'EcSlopeHint' of github.com:lambdaclass/cairo-vm.go into…
mmsc2 facfb8a
Delete unused import
mmsc2 f195395
Merge main
mmsc2 9831f40
Merge branch 'main' into EcSlopeHint
mmsc2 c9cfd08
Merge main
mmsc2 6332e1c
Change name convention
mmsc2 47a9ff5
Change naming convention
mmsc2 81b3430
Merge branch 'main' into EcSlopeHint
entropidelic 7c08384
Merge branch 'EcSlopeHint' of github.com:lambdaclass/cairo-vm.go into…
mmsc2 5c18154
Fix naming convention in variables
entropidelic 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 |
---|---|---|
|
@@ -4,10 +4,11 @@ import ( | |
"errors" | ||
"math/big" | ||
|
||
"github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_utils" | ||
"github.com/lambdaclass/cairo-vm.go/pkg/builtins" | ||
. "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/types" | ||
"github.com/lambdaclass/cairo-vm.go/pkg/vm" | ||
. "github.com/lambdaclass/cairo-vm.go/pkg/types" | ||
. "github.com/lambdaclass/cairo-vm.go/pkg/vm" | ||
"github.com/lambdaclass/cairo-vm.go/pkg/vm/memory" | ||
) | ||
|
||
|
@@ -31,10 +32,10 @@ func (val *BigInt3) Pack86() big.Int { | |
return *sum | ||
} | ||
|
||
func BigInt3FromBaseAddr(addr memory.Relocatable, virtual_machine vm.VirtualMachine) (BigInt3, error) { | ||
func BigInt3FromBaseAddr(addr memory.Relocatable, vm VirtualMachine) (BigInt3, error) { | ||
limbs := make([]lambdaworks.Felt, 0) | ||
for i := 0; i < 3; i++ { | ||
felt, err := virtual_machine.Segments.Memory.GetFelt(addr.AddUint(uint(i))) | ||
felt, err := vm.Segments.Memory.GetFelt(addr.AddUint(uint(i))) | ||
if err == nil { | ||
limbs = append(limbs, felt) | ||
} else { | ||
|
@@ -44,18 +45,18 @@ func BigInt3FromBaseAddr(addr memory.Relocatable, virtual_machine vm.VirtualMach | |
return BigInt3{Limbs: limbs}, nil | ||
} | ||
|
||
func BigInt3FromVarName(name string, virtual_machine vm.VirtualMachine, ids_data hint_utils.IdsManager) (EcPoint, error) { | ||
point_addr, err := ids_data.GetAddr(name, &virtual_machine) | ||
func EcPointFromVarName(name string, vm VirtualMachine, idsData IdsManager) (EcPoint, error) { | ||
point_addr, err := idsData.GetAddr(name, &vm) | ||
if err != nil { | ||
return EcPoint{}, err | ||
} | ||
|
||
x, err := BigInt3FromBaseAddr(point_addr, virtual_machine) | ||
x, err := BigInt3FromBaseAddr(point_addr, vm) | ||
if err != nil { | ||
return EcPoint{}, err | ||
} | ||
|
||
y, err := BigInt3FromBaseAddr(point_addr.AddUint(3), virtual_machine) | ||
y, err := BigInt3FromBaseAddr(point_addr.AddUint(3), vm) | ||
if err != nil { | ||
return EcPoint{}, err | ||
} | ||
|
@@ -66,34 +67,34 @@ func BigInt3FromVarName(name string, virtual_machine vm.VirtualMachine, ids_data | |
/* | ||
Implements main logic for `EC_NEGATE` and `EC_NEGATE_EMBEDDED_SECP` hints | ||
*/ | ||
func ecNegate(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager, secp_p big.Int) error { | ||
point, err := ids_data.GetRelocatable("point", &virtual_machine) | ||
func ecNegate(vm VirtualMachine, execScopes ExecutionScopes, idsData IdsManager, secpP big.Int) error { | ||
point, err := idsData.GetRelocatable("point", &vm) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
point_y, err := point.AddInt(3) | ||
pointY, err := point.AddInt(3) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
y_bigint3, err := BigInt3FromBaseAddr(point_y, virtual_machine) | ||
y_bigint3, err := BigInt3FromBaseAddr(pointY, vm) | ||
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. yBigint3 |
||
if err != nil { | ||
return err | ||
} | ||
|
||
y := y_bigint3.Pack86() | ||
value := new(big.Int).Neg(&y) | ||
value.Mod(value, &secp_p) | ||
value.Mod(value, &secpP) | ||
|
||
exec_scopes.AssignOrUpdateVariable("value", value) | ||
exec_scopes.AssignOrUpdateVariable("SECP_P", secp_p) | ||
execScopes.AssignOrUpdateVariable("value", value) | ||
execScopes.AssignOrUpdateVariable("SECP_P", secpP) | ||
return nil | ||
} | ||
|
||
func ecNegateImportSecpP(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager) error { | ||
func ecNegateImportSecpP(vm VirtualMachine, execScopes ExecutionScopes, idsData IdsManager) error { | ||
secp_p, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007908834671663", 10) | ||
return ecNegate(virtual_machine, exec_scopes, ids_data, *secp_p) | ||
return ecNegate(vm, execScopes, idsData, *secp_p) | ||
} | ||
|
||
/* | ||
|
@@ -108,9 +109,103 @@ Implements hint: | |
%} | ||
*/ | ||
|
||
func ecNegateEmbeddedSecpP(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager) error { | ||
secp_p := big.NewInt(1) | ||
secp_p.Lsh(secp_p, 255) | ||
secp_p.Sub(secp_p, big.NewInt(19)) | ||
return ecNegate(virtual_machine, exec_scopes, ids_data, *secp_p) | ||
func ecNegateEmbeddedSecpP(vm VirtualMachine, execScopes ExecutionScopes, idsData IdsManager) error { | ||
SecpP := big.NewInt(1) | ||
SecpP.Lsh(SecpP, 255) | ||
SecpP.Sub(SecpP, big.NewInt(19)) | ||
return ecNegate(vm, execScopes, idsData, *SecpP) | ||
} | ||
|
||
/* | ||
Implements hint: | ||
|
||
%{ | ||
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack | ||
from starkware.python.math_utils import ec_double_slope | ||
|
||
# Compute the slope. | ||
x = pack(ids.point.x, PRIME) | ||
y = pack(ids.point.y, PRIME) | ||
value = slope = ec_double_slope(point=(x, y), alpha=0, p=SECP_P) | ||
|
||
%} | ||
*/ | ||
func computeDoublingSlope(vm VirtualMachine, execScopes ExecutionScopes, idsData IdsManager, pointAlias string, SecpP big.Int, alpha big.Int) error { | ||
execScopes.AssignOrUpdateVariable("SECP_P", SecpP) | ||
|
||
point, err := EcPointFromVarName(pointAlias, vm, idsData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
x := point.X.Pack86() | ||
y := point.Y.Pack86() | ||
double_point := builtins.DoublePointB{X: x, Y: y} | ||
|
||
value, err := builtins.EcDoubleSlope(double_point, alpha, SecpP) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
execScopes.AssignOrUpdateVariable("value", value) | ||
execScopes.AssignOrUpdateVariable("slope", value) | ||
|
||
return nil | ||
} | ||
|
||
/* | ||
Implements hint: | ||
%{ | ||
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack | ||
from starkware.python.math_utils import line_slope | ||
|
||
# Compute the slope. | ||
x0 = pack(ids.point0.x, PRIME) | ||
y0 = pack(ids.point0.y, PRIME) | ||
x1 = pack(ids.point1.x, PRIME) | ||
y1 = pack(ids.point1.y, PRIME) | ||
value = slope = line_slope(point1=(x0, y0), point2=(x1, y1), p=SECP_P) | ||
%} | ||
*/ | ||
|
||
func computeSlopeAndAssingSecpP(vm VirtualMachine, execScopes ExecutionScopes, idsData IdsManager, point0Alias string, point1Alias string, secp_p big.Int) error { | ||
execScopes.AssignOrUpdateVariable("SECP_P", secp_p) | ||
return computeSlope(vm, execScopes, idsData, point0Alias, point1Alias) | ||
} | ||
|
||
func computeSlope(vm VirtualMachine, execScopes ExecutionScopes, idsData IdsManager, point0_alias string, point1_alias string) error { | ||
point0, err := EcPointFromVarName(point0_alias, vm, idsData) | ||
if err != nil { | ||
return err | ||
} | ||
point1, err := EcPointFromVarName(point1_alias, vm, idsData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
secp_p, err := execScopes.Get("SECP_P") | ||
if err != nil { | ||
return err | ||
} | ||
secp := secp_p.(big.Int) | ||
|
||
// build partial sum | ||
x0 := point0.X.Pack86() | ||
y0 := point0.Y.Pack86() | ||
point_a := builtins.PartialSumB{X: x0, Y: y0} | ||
|
||
// build double point | ||
x1 := point1.X.Pack86() | ||
y1 := point1.Y.Pack86() | ||
point_b := builtins.DoublePointB{X: x1, Y: y1} | ||
|
||
value, err := builtins.LineSlope(point_a, point_b, secp) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
execScopes.AssignOrUpdateVariable("value", value) | ||
execScopes.AssignOrUpdateVariable("slope", value) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package hint_utils | ||
|
||
import "math/big" | ||
|
||
func SECP_P() big.Int { | ||
secp_p, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007908834671663", 10) | ||
return *secp_p | ||
} | ||
|
||
func ALPHA() big.Int { | ||
alpha := big.NewInt(0) | ||
return *alpha | ||
} |
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.
pointAddr