Skip to content
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 36 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
977ba29
Add ec hints
mmsc2 Sep 14, 2023
70a9db2
Implement hints
mmsc2 Sep 15, 2023
864b7b8
Add the hints to the processor
mmsc2 Sep 15, 2023
7df4192
Test pack86 function
mmsc2 Sep 15, 2023
1a66309
Test hint
mmsc2 Sep 15, 2023
91ffa57
Delete debug info, Test ec negative op
mmsc2 Sep 18, 2023
7bf1909
Second hint test
mmsc2 Sep 18, 2023
5d2d256
Merge main
mmsc2 Sep 18, 2023
a08c642
Test embedded hint
mmsc2 Sep 18, 2023
029fe50
Merge branch 'main' into EcHint
mmsc2 Sep 18, 2023
762a566
Merge branch 'main' into EcHint
mmsc2 Sep 18, 2023
6c6eed9
Change to Camel case
mmsc2 Sep 19, 2023
ec9345c
Implement slope hints
mmsc2 Sep 19, 2023
9d14678
Fix merge conflict
mmsc2 Sep 19, 2023
ff7fb50
Fix format
mmsc2 Sep 19, 2023
4e9afc5
Delete github conflict string
mmsc2 Sep 19, 2023
cc53b65
Tests hints
mmsc2 Sep 19, 2023
a0d269b
Tests hints slopes
mmsc2 Sep 19, 2023
c25e25c
Fix merge conflict
mmsc2 Sep 19, 2023
a27b814
Rename misleading name function
mmsc2 Sep 19, 2023
51fedf0
Merge branch 'main' into EcSlopeHint
mmsc2 Sep 19, 2023
9e39502
Fix function name
mmsc2 Sep 19, 2023
327299b
Fix format
mmsc2 Sep 19, 2023
0fdad83
Fix error in function call
mmsc2 Sep 19, 2023
ba81ca8
Merge branch 'main' into EcSlopeHint
entropidelic Sep 19, 2023
78dd5d0
Delete debug info
mmsc2 Sep 19, 2023
c023bea
Merge branch 'EcSlopeHint' of github.com:lambdaclass/cairo-vm.go into…
mmsc2 Sep 19, 2023
facfb8a
Delete unused import
mmsc2 Sep 19, 2023
f195395
Merge main
mmsc2 Sep 20, 2023
9831f40
Merge branch 'main' into EcSlopeHint
mmsc2 Sep 20, 2023
c9cfd08
Merge main
mmsc2 Sep 20, 2023
6332e1c
Change name convention
mmsc2 Sep 21, 2023
47a9ff5
Change naming convention
mmsc2 Sep 21, 2023
81b3430
Merge branch 'main' into EcSlopeHint
entropidelic Sep 21, 2023
7c08384
Merge branch 'EcSlopeHint' of github.com:lambdaclass/cairo-vm.go into…
mmsc2 Sep 21, 2023
5c18154
Fix naming convention in variables
entropidelic Sep 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 96 additions & 1 deletion pkg/hints/ec_hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"math/big"

"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"
Expand Down Expand Up @@ -44,7 +45,7 @@ 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) {
func EcPointFromVarName(name string, virtual_machine vm.VirtualMachine, ids_data hint_utils.IdsManager) (EcPoint, error) {
point_addr, err := ids_data.GetAddr(name, &virtual_machine)
if err != nil {
return EcPoint{}, err
Expand Down Expand Up @@ -114,3 +115,97 @@ func ecNegateEmbeddedSecpP(virtual_machine vm.VirtualMachine, exec_scopes types.
secp_p.Sub(secp_p, big.NewInt(19))
return ecNegate(virtual_machine, exec_scopes, ids_data, *secp_p)
}

/*
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(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager, point_alias string, secp_p big.Int, alpha big.Int) error {
exec_scopes.AssignOrUpdateVariable("SECP_P", secp_p)

point, err := EcPointFromVarName(point_alias, virtual_machine, ids_data)
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, secp_p)
if err != nil {
return err
}

exec_scopes.AssignOrUpdateVariable("value", value)
exec_scopes.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(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager, point0_alias string, point1_alias string, secp_p big.Int) error {
exec_scopes.AssignOrUpdateVariable("SECP_P", secp_p)
return computeSlope(virtual_machine, exec_scopes, ids_data, point0_alias, point1_alias)
}

func computeSlope(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager, point0_alias string, point1_alias string) error {
point0, err := EcPointFromVarName(point0_alias, virtual_machine, ids_data)
if err != nil {
return err
}
point1, err := EcPointFromVarName(point1_alias, virtual_machine, ids_data)
if err != nil {
return err
}

secp_p, err := exec_scopes.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
}

exec_scopes.AssignOrUpdateVariable("value", value)
exec_scopes.AssignOrUpdateVariable("slope", value)

return nil
}
106 changes: 106 additions & 0 deletions pkg/hints/ec_hint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,109 @@ func TestRunEcEmbeddedSecpOk(t *testing.T) {
}

}

func TestComputeDoublingSlopeOk(t *testing.T) {
vm := NewVirtualMachine()
vm.Segments.AddSegment()
vm.Segments.AddSegment()

vm.RunContext.Fp = NewRelocatable(1, 1)

idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"point": {
NewMaybeRelocatableFelt(FeltFromUint64(614323)),
NewMaybeRelocatableFelt(FeltFromUint64(5456867)),
NewMaybeRelocatableFelt(FeltFromUint64(101208)),
NewMaybeRelocatableFelt(FeltFromUint64(773712524)),
NewMaybeRelocatableFelt(FeltFromUint64(77371252)),
NewMaybeRelocatableFelt(FeltFromUint64(5298795)),
},
},
vm,
)

hintProcessor := CairoVmHintProcessor{}
hintData := any(HintData{
Ids: idsManager,
Code: EC_DOUBLE_SLOPE_V1,
})

exec_scopes := types.NewExecutionScopes()
err := hintProcessor.ExecuteHint(vm, &hintData, nil, exec_scopes)
if err != nil {
t.Errorf("EC_DOUBLE_SLOPE_V1 hint test failed with error %s", err)
} else {
value, _ := exec_scopes.Get("value")
val := value.(big.Int)

slope_res, _ := exec_scopes.Get("slope")
slope := slope_res.(big.Int)

// expected values
expected_val, _ := new(big.Int).SetString("40442433062102151071094722250325492738932110061897694430475034100717288403728", 10)

expected_slope, _ := new(big.Int).SetString("40442433062102151071094722250325492738932110061897694430475034100717288403728", 10)

if expected_val.Cmp(&val) != 0 || expected_slope.Cmp(&slope) != 0 {
t.Errorf("EC_DOUBLE_SLOPE_V1 hint test incorrect value for exec_scopes.value or exec_scopes.slope")
}
}
}

func TestRunComputeSlopeOk(t *testing.T) {
vm := NewVirtualMachine()
vm.Segments.AddSegment()
vm.Segments.AddSegment()

vm.RunContext.Fp = NewRelocatable(1, 14)

idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"point0": {
NewMaybeRelocatableFelt(FeltFromUint64(134)),
NewMaybeRelocatableFelt(FeltFromUint64(5123)),
NewMaybeRelocatableFelt(FeltFromUint64(140)),
NewMaybeRelocatableFelt(FeltFromUint64(1232)),
NewMaybeRelocatableFelt(FeltFromUint64(4652)),
NewMaybeRelocatableFelt(FeltFromUint64(720)),
},
"point1": {
NewMaybeRelocatableFelt(FeltFromUint64(156)),
NewMaybeRelocatableFelt(FeltFromUint64(6545)),
NewMaybeRelocatableFelt(FeltFromUint64(100010)),
NewMaybeRelocatableFelt(FeltFromUint64(1123)),
NewMaybeRelocatableFelt(FeltFromUint64(1325)),
NewMaybeRelocatableFelt(FeltFromUint64(910)),
},
},
vm,
)

hintProcessor := CairoVmHintProcessor{}
hintData := any(HintData{
Ids: idsManager,
Code: COMPUTE_SLOPE_V1,
})

exec_scopes := types.NewExecutionScopes()
err := hintProcessor.ExecuteHint(vm, &hintData, nil, exec_scopes)
if err != nil {
t.Errorf("EC_DOUBLE_SLOPE_V1 hint test failed with error %s", err)
} else {
value, _ := exec_scopes.Get("value")
val := value.(big.Int)

slope_res, _ := exec_scopes.Get("slope")
slope := slope_res.(big.Int)

// expected values
expected_val, _ := new(big.Int).SetString("41419765295989780131385135514529906223027172305400087935755859001910844026631", 10)

expected_slope, _ := new(big.Int).SetString("41419765295989780131385135514529906223027172305400087935755859001910844026631", 10)

if expected_val.Cmp(&val) != 0 || expected_slope.Cmp(&slope) != 0 {
t.Errorf("EC_DOUBLE_SLOPE_V1 hint test incorrect value for exec_scopes.value or exec_scopes.slope")
}
}
}
2 changes: 2 additions & 0 deletions pkg/hints/hint_codes/ec_op_hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ package hint_codes

const EC_NEGATE = "from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack\n\ny = pack(ids.point.y, PRIME) % SECP_P\n# The modulo operation in python always returns a nonnegative number.\nvalue = (-y) % SECP_P"
const EC_NEGATE_EMBEDDED_SECP = "from starkware.cairo.common.cairo_secp.secp_utils import pack\nSECP_P = 2**255-19\n\ny = pack(ids.point.y, PRIME) % SECP_P\n# The modulo operation in python always returns a nonnegative number.\nvalue = (-y) % SECP_P"
const EC_DOUBLE_SLOPE_V1 = "from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack\nfrom starkware.python.math_utils import ec_double_slope\n\n# Compute the slope.\nx = pack(ids.point.x, PRIME)\ny = pack(ids.point.y, PRIME)\nvalue = slope = ec_double_slope(point=(x, y), alpha=0, p=SECP_P)"
const COMPUTE_SLOPE_V1 = "from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack\nfrom starkware.python.math_utils import line_slope\n\n# Compute the slope.\nx0 = pack(ids.point0.x, PRIME)\ny0 = pack(ids.point0.y, PRIME)\nx1 = pack(ids.point1.x, PRIME)\ny1 = pack(ids.point1.y, PRIME)\nvalue = slope = line_slope(point1=(x0, y0), point2=(x1, y1), p=SECP_P)"
4 changes: 4 additions & 0 deletions pkg/hints/hint_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any,
return memset_step_loop(data.Ids, vm, execScopes, "continue_loop")
case VM_ENTER_SCOPE:
return vm_enter_scope(execScopes)
case COMPUTE_SLOPE_V1:
return computeSlopeAndAssingSecpP(*vm, *execScopes, data.Ids, "point0", "point1", SECP_P())
case EC_DOUBLE_SLOPE_V1:
return computeDoublingSlope(*vm, *execScopes, data.Ids, "point", SECP_P(), ALPHA())
case UNSAFE_KECCAK:
return unsafeKeccak(data.Ids, vm, *execScopes)
case UNSAFE_KECCAK_FINALIZE:
Expand Down
13 changes: 13 additions & 0 deletions pkg/hints/hint_utils/secp_utils.go
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
}
Loading