From 977ba29fcdddd64516182b34c7dd9abcccc1101a Mon Sep 17 00:00:00 2001 From: Milton Date: Thu, 14 Sep 2023 15:37:32 -0300 Subject: [PATCH 01/22] Add ec hints --- pkg/hints/ec_hint.go | 92 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 pkg/hints/ec_hint.go diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go new file mode 100644 index 00000000..33a6c162 --- /dev/null +++ b/pkg/hints/ec_hint.go @@ -0,0 +1,92 @@ +package hints + +import ( + "errors" + "math/big" + + "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/vm/memory" +) + +type BigInt3 struct { + limbs []lambdaworks.Felt +} + +type EcPoint struct { + X BigInt3 + Y BigInt3 +} + +func (val *BigInt3) Pack86() big.Int { + sum := big.NewInt(0) + for i := 0; i < 3; i++ { + felt := val.limbs[i] + signed := felt.ToSigned() + shifed := new(big.Int).Lsh(signed, uint(i*86)) + sum.Add(sum, shifed) + } + return *sum +} + +func FromBaseAddr(addr memory.Relocatable, virtual_machine 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))) + if err == nil { + limbs = append(limbs, felt) + } else { + return BigInt3{}, errors.New("Identifier has no member") + } + } + return BigInt3{limbs: limbs}, nil +} + +func FromVarName(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 + } + + x, err := FromBaseAddr(point_addr, virtual_machine) + if err != nil { + return EcPoint{}, err + } + + y, err := FromBaseAddr(point_addr.AddUint(3), virtual_machine) + if err != nil { + return EcPoint{}, err + } + + return EcPoint{X: x, Y: y}, nil +} + +/* +Implements main logic for `EC_NEGATE` and `EC_NEGATE_EMBEDDED_SECP` hints +*/ +func ec_negate(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) + if err != nil { + return err + } + + point_y, err := point.AddInt(3) + if err != nil { + return err + } + + y_bigint3, err := FromBaseAddr(point_y, virtual_machine) + if err != nil { + return err + } + + y := y_bigint3.Pack86() + value := new(big.Int).Neg(&y) + value.Mod(value, &secp_p) + + exec_scopes.AssignOrUpdateVariable("value", value) + exec_scopes.AssignOrUpdateVariable("SECP_P", secp_p) + return nil +} From 70a9db2e91ebcf550f2aef0bc071d141d5baf557 Mon Sep 17 00:00:00 2001 From: Milton Date: Fri, 15 Sep 2023 14:23:23 -0300 Subject: [PATCH 02/22] Implement hints --- pkg/hints/ec_hint.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index 33a6c162..57a35074 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -90,3 +90,28 @@ func ec_negate(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionSco exec_scopes.AssignOrUpdateVariable("SECP_P", secp_p) return nil } + +func EcNegateImportSecpP(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager) error { + secp_p, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007908834671663", 10) + return ec_negate(virtual_machine, exec_scopes, ids_data, *secp_p) +} + +/* +Implements hint: +%{ + from starkware.cairo.common.cairo_secp.secp_utils import pack + SECP_P = 2**255-19 + + y = pack(ids.point.y, PRIME) % SECP_P + # The modulo operation in python always returns a nonnegative number. + value = (-y) % SECP_P +%} +*/ + +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, 256) + secp_p.Sub(secp_p, big.NewInt(19)) + + return ec_negate(virtual_machine, exec_scopes, ids_data, *secp_p) +} From 864b7b87c9d30dfab2337c92d3595052ff31c201 Mon Sep 17 00:00:00 2001 From: Milton Date: Fri, 15 Sep 2023 15:13:26 -0300 Subject: [PATCH 03/22] Add the hints to the processor --- pkg/hints/ec_hint.go | 4 ++-- pkg/hints/hint_codes/ec_op_hints.go | 4 ++++ pkg/hints/{ => hint_codes}/math_hint_codes.go | 2 +- pkg/hints/{ => hint_codes}/memcpy_hint_codes.go | 2 +- pkg/hints/hint_processor.go | 5 +++++ pkg/hints/math_hints_test.go | 1 + pkg/hints/memcpy_hints_test.go | 1 + 7 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 pkg/hints/hint_codes/ec_op_hints.go rename pkg/hints/{ => hint_codes}/math_hint_codes.go (98%) rename pkg/hints/{ => hint_codes}/memcpy_hint_codes.go (82%) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index 57a35074..95e89096 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -91,7 +91,7 @@ func ec_negate(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionSco return nil } -func EcNegateImportSecpP(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager) error { +func ec_negate_import_secp_p(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager) error { secp_p, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007908834671663", 10) return ec_negate(virtual_machine, exec_scopes, ids_data, *secp_p) } @@ -108,7 +108,7 @@ Implements hint: %} */ -func EcNegateEmbeddedSecpP(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager) error { +func ec_negate_embedded_secp_p(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager) error { secp_p := big.NewInt(1) secp_p.Lsh(secp_p, 256) secp_p.Sub(secp_p, big.NewInt(19)) diff --git a/pkg/hints/hint_codes/ec_op_hints.go b/pkg/hints/hint_codes/ec_op_hints.go new file mode 100644 index 00000000..c2092a7f --- /dev/null +++ b/pkg/hints/hint_codes/ec_op_hints.go @@ -0,0 +1,4 @@ +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" diff --git a/pkg/hints/math_hint_codes.go b/pkg/hints/hint_codes/math_hint_codes.go similarity index 98% rename from pkg/hints/math_hint_codes.go rename to pkg/hints/hint_codes/math_hint_codes.go index 4eead668..0bf0f36e 100644 --- a/pkg/hints/math_hint_codes.go +++ b/pkg/hints/hint_codes/math_hint_codes.go @@ -1,4 +1,4 @@ -package hints +package hint_codes const ASSERT_NN = "from starkware.cairo.common.math_utils import assert_integer\nassert_integer(ids.a)\nassert 0 <= ids.a % PRIME < range_check_builtin.bound, f'a = {ids.a} is out of range.'" diff --git a/pkg/hints/memcpy_hint_codes.go b/pkg/hints/hint_codes/memcpy_hint_codes.go similarity index 82% rename from pkg/hints/memcpy_hint_codes.go rename to pkg/hints/hint_codes/memcpy_hint_codes.go index 71e356c5..68efc0de 100644 --- a/pkg/hints/memcpy_hint_codes.go +++ b/pkg/hints/hint_codes/memcpy_hint_codes.go @@ -1,4 +1,4 @@ -package hints +package hint_codes const ADD_SEGMENT = "memory[ap] = segments.add()" const VM_EXIT_SCOPE = "vm_exit_scope()" diff --git a/pkg/hints/hint_processor.go b/pkg/hints/hint_processor.go index e12c7194..acbe5175 100644 --- a/pkg/hints/hint_processor.go +++ b/pkg/hints/hint_processor.go @@ -3,6 +3,7 @@ package hints import ( "strings" + . "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_codes" . "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/parser" @@ -51,6 +52,10 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any, return vm_exit_scope(execScopes) case ASSERT_NOT_EQUAL: return assert_not_equal(data.Ids, vm) + case EC_NEGATE: + return ec_negate_import_secp_p(*vm, *execScopes, data.Ids) + case EC_NEGATE_EMBEDDED_SECP: + return ec_negate_embedded_secp_p(*vm, *execScopes, data.Ids) default: return errors.Errorf("Unknown Hint: %s", data.Code) } diff --git a/pkg/hints/math_hints_test.go b/pkg/hints/math_hints_test.go index efd08f43..3c76bcac 100644 --- a/pkg/hints/math_hints_test.go +++ b/pkg/hints/math_hints_test.go @@ -4,6 +4,7 @@ import ( "testing" . "github.com/lambdaclass/cairo-vm.go/pkg/hints" + . "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_codes" . "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" diff --git a/pkg/hints/memcpy_hints_test.go b/pkg/hints/memcpy_hints_test.go index 758b0717..19c0cde7 100644 --- a/pkg/hints/memcpy_hints_test.go +++ b/pkg/hints/memcpy_hints_test.go @@ -4,6 +4,7 @@ import ( "testing" . "github.com/lambdaclass/cairo-vm.go/pkg/hints" + . "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_codes" . "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" From 7df419225f828f5876869fe6191d71edac02ad5c Mon Sep 17 00:00:00 2001 From: Milton Date: Fri, 15 Sep 2023 16:48:03 -0300 Subject: [PATCH 04/22] Test pack86 function --- pkg/hints/ec_hint.go | 6 +++--- pkg/hints/ec_hint_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 pkg/hints/ec_hint_test.go diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index 95e89096..8cd5cdfc 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -12,7 +12,7 @@ import ( ) type BigInt3 struct { - limbs []lambdaworks.Felt + Limbs []lambdaworks.Felt } type EcPoint struct { @@ -23,7 +23,7 @@ type EcPoint struct { func (val *BigInt3) Pack86() big.Int { sum := big.NewInt(0) for i := 0; i < 3; i++ { - felt := val.limbs[i] + felt := val.Limbs[i] signed := felt.ToSigned() shifed := new(big.Int).Lsh(signed, uint(i*86)) sum.Add(sum, shifed) @@ -41,7 +41,7 @@ func FromBaseAddr(addr memory.Relocatable, virtual_machine vm.VirtualMachine) (B return BigInt3{}, errors.New("Identifier has no member") } } - return BigInt3{limbs: limbs}, nil + return BigInt3{Limbs: limbs}, nil } func FromVarName(name string, virtual_machine vm.VirtualMachine, ids_data hint_utils.IdsManager) (EcPoint, error) { diff --git a/pkg/hints/ec_hint_test.go b/pkg/hints/ec_hint_test.go new file mode 100644 index 00000000..76afdce6 --- /dev/null +++ b/pkg/hints/ec_hint_test.go @@ -0,0 +1,31 @@ +package hints_test + +import ( + "math/big" + "testing" + + "github.com/lambdaclass/cairo-vm.go/pkg/hints" + "github.com/lambdaclass/cairo-vm.go/pkg/lambdaworks" +) + +func TestBigInt3Pack86(t *testing.T) { + limbs1 := []lambdaworks.Felt{lambdaworks.FeltFromUint64(10), lambdaworks.FeltFromUint64(10), lambdaworks.FeltFromUint64(10)} + bigint := hints.BigInt3{Limbs: limbs1} + pack1 := bigint.Pack86() + + expected, _ := new(big.Int).SetString("59863107065073783529622931521771477038469668772249610", 10) + + if pack1.Cmp(expected) != 0 { + t.Errorf("Different pack from expected") + } + + limbs2 := []lambdaworks.Felt{lambdaworks.FeltFromDecString("773712524553362"), lambdaworks.FeltFromDecString("57408430697461422066401280"), lambdaworks.FeltFromDecString("1292469707114105")} + bigint2 := hints.BigInt3{Limbs: limbs2} + pack2 := bigint2.Pack86() + + expected2, _ := new(big.Int).SetString("7737125245533626718119526477371252455336267181195264773712524553362", 10) + + if pack2.Cmp(expected2) != 0 { + t.Errorf("Different pack from expected2") + } +} \ No newline at end of file From 1a663098db49b3056da639ff229a45364ea282b8 Mon Sep 17 00:00:00 2001 From: Milton Date: Fri, 15 Sep 2023 18:46:32 -0300 Subject: [PATCH 05/22] Test hint --- pkg/hints/ec_hint.go | 3 ++ pkg/hints/ec_hint_test.go | 74 ++++++++++++++++++++++++++++++++++--- pkg/hints/hint_processor.go | 4 ++ 3 files changed, 75 insertions(+), 6 deletions(-) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index 8cd5cdfc..16240678 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -2,6 +2,7 @@ package hints import ( "errors" + "fmt" "math/big" "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_utils" @@ -67,6 +68,7 @@ func FromVarName(name string, virtual_machine vm.VirtualMachine, ids_data hint_u Implements main logic for `EC_NEGATE` and `EC_NEGATE_EMBEDDED_SECP` hints */ func ec_negate(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager, secp_p big.Int) error { + fmt.Println("inside negate") point, err := ids_data.GetRelocatable("point", &virtual_machine) if err != nil { return err @@ -92,6 +94,7 @@ func ec_negate(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionSco } func ec_negate_import_secp_p(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager) error { + fmt.Println("ec negate sec") secp_p, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007908834671663", 10) return ec_negate(virtual_machine, exec_scopes, ids_data, *secp_p) } diff --git a/pkg/hints/ec_hint_test.go b/pkg/hints/ec_hint_test.go index 76afdce6..1b2a5186 100644 --- a/pkg/hints/ec_hint_test.go +++ b/pkg/hints/ec_hint_test.go @@ -4,13 +4,18 @@ import ( "math/big" "testing" - "github.com/lambdaclass/cairo-vm.go/pkg/hints" - "github.com/lambdaclass/cairo-vm.go/pkg/lambdaworks" + . "github.com/lambdaclass/cairo-vm.go/pkg/hints" + . "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_codes" + . "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" + ) func TestBigInt3Pack86(t *testing.T) { - limbs1 := []lambdaworks.Felt{lambdaworks.FeltFromUint64(10), lambdaworks.FeltFromUint64(10), lambdaworks.FeltFromUint64(10)} - bigint := hints.BigInt3{Limbs: limbs1} + limbs1 := []Felt{FeltFromUint64(10), FeltFromUint64(10), FeltFromUint64(10)} + bigint := BigInt3{Limbs: limbs1} pack1 := bigint.Pack86() expected, _ := new(big.Int).SetString("59863107065073783529622931521771477038469668772249610", 10) @@ -19,8 +24,8 @@ func TestBigInt3Pack86(t *testing.T) { t.Errorf("Different pack from expected") } - limbs2 := []lambdaworks.Felt{lambdaworks.FeltFromDecString("773712524553362"), lambdaworks.FeltFromDecString("57408430697461422066401280"), lambdaworks.FeltFromDecString("1292469707114105")} - bigint2 := hints.BigInt3{Limbs: limbs2} + limbs2 := []Felt{FeltFromDecString("773712524553362"), FeltFromDecString("57408430697461422066401280"), FeltFromDecString("1292469707114105")} + bigint2 := BigInt3{Limbs: limbs2} pack2 := bigint2.Pack86() expected2, _ := new(big.Int).SetString("7737125245533626718119526477371252455336267181195264773712524553362", 10) @@ -28,4 +33,61 @@ func TestBigInt3Pack86(t *testing.T) { if pack2.Cmp(expected2) != 0 { t.Errorf("Different pack from expected2") } +} + + + +// fn run_ec_negate_ok() { +// let hint_code = "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"; +// let mut vm = vm_with_range_check!(); + +// vm.segments = segments![((1, 3), 2645i32), ((1, 4), 454i32), ((1, 5), 206i32)]; +// //Initialize fp +// vm.run_context.fp = 1; +// //Create hint_data +// let ids_data = ids_data!["point"]; +// let mut exec_scopes = ExecutionScopes::new(); +// //Execute the hint +// assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); +// //Check 'value' is defined in the vm scope +// assert_matches!( +// exec_scopes.get::("value"), +// Ok(x) if x == bigint_str!( +// "115792089237316195423569751828682367333329274433232027476421668138471189901786" +// ) +// ); +// } + +func TestRunEcNegateOk(t *testing.T) { + vm := NewVirtualMachine() + vm.Segments.AddSegment() + vm.Segments.AddSegment() + vm.Segments.Memory.Insert(NewRelocatable(1,3), NewMaybeRelocatableFelt(FeltFromUint64(2645))) + vm.Segments.Memory.Insert(NewRelocatable(1,4), NewMaybeRelocatableFelt(FeltFromUint64(454))) + vm.Segments.Memory.Insert(NewRelocatable(1,5), NewMaybeRelocatableFelt(FeltFromUint64(206))) + + + idsManager := SetupIdsForTest( + map[string][]*MaybeRelocatable{ + "point": {NewMaybeRelocatableFelt(FeltFromDecString("-1"))}, + "ec_negative": {nil}, + }, + vm, + ) + hintProcessor := CairoVmHintProcessor{} + hintData := any(HintData{ + Ids: idsManager, + Code: EC_NEGATE, + }) + err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil) + if err != nil { + t.Errorf("Ec Negative hint test failed with error %s", err) + } + // Check ids.is_positive + value, err := idsManager.GetFelt("value", vm) + expected := FeltFromDecString("115792089237316195423569751828682367333329274433232027476421668138471189901786") + + if err != nil || value != expected { + t.Errorf("Ec Negative hint test incorrect value for ids.value") + } } \ No newline at end of file diff --git a/pkg/hints/hint_processor.go b/pkg/hints/hint_processor.go index acbe5175..e7a0eb3f 100644 --- a/pkg/hints/hint_processor.go +++ b/pkg/hints/hint_processor.go @@ -1,6 +1,7 @@ package hints import ( + "fmt" "strings" . "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_codes" @@ -53,6 +54,9 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any, case ASSERT_NOT_EQUAL: return assert_not_equal(data.Ids, vm) case EC_NEGATE: + fmt.Println("match case") + fmt.Println("vm: ", vm) + fmt.Println("escopes", execScopes) return ec_negate_import_secp_p(*vm, *execScopes, data.Ids) case EC_NEGATE_EMBEDDED_SECP: return ec_negate_embedded_secp_p(*vm, *execScopes, data.Ids) From 91ffa57fe1782cb1d88e64d9e2e77b69ea635d5b Mon Sep 17 00:00:00 2001 From: Milton Date: Mon, 18 Sep 2023 11:17:48 -0300 Subject: [PATCH 06/22] Delete debug info, Test ec negative op --- pkg/hints/ec_hint.go | 3 --- pkg/hints/ec_hint_test.go | 34 +++++++++++++++++----------------- pkg/hints/hint_processor.go | 4 ---- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index 16240678..8cd5cdfc 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -2,7 +2,6 @@ package hints import ( "errors" - "fmt" "math/big" "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_utils" @@ -68,7 +67,6 @@ func FromVarName(name string, virtual_machine vm.VirtualMachine, ids_data hint_u Implements main logic for `EC_NEGATE` and `EC_NEGATE_EMBEDDED_SECP` hints */ func ec_negate(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager, secp_p big.Int) error { - fmt.Println("inside negate") point, err := ids_data.GetRelocatable("point", &virtual_machine) if err != nil { return err @@ -94,7 +92,6 @@ func ec_negate(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionSco } func ec_negate_import_secp_p(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager) error { - fmt.Println("ec negate sec") secp_p, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007908834671663", 10) return ec_negate(virtual_machine, exec_scopes, ids_data, *secp_p) } diff --git a/pkg/hints/ec_hint_test.go b/pkg/hints/ec_hint_test.go index 1b2a5186..cbb22037 100644 --- a/pkg/hints/ec_hint_test.go +++ b/pkg/hints/ec_hint_test.go @@ -8,9 +8,9 @@ import ( . "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_codes" . "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/vm/memory" - ) func TestBigInt3Pack86(t *testing.T) { @@ -35,8 +35,6 @@ func TestBigInt3Pack86(t *testing.T) { } } - - // fn run_ec_negate_ok() { // let hint_code = "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"; // let mut vm = vm_with_range_check!(); @@ -62,14 +60,13 @@ func TestRunEcNegateOk(t *testing.T) { vm := NewVirtualMachine() vm.Segments.AddSegment() vm.Segments.AddSegment() - vm.Segments.Memory.Insert(NewRelocatable(1,3), NewMaybeRelocatableFelt(FeltFromUint64(2645))) - vm.Segments.Memory.Insert(NewRelocatable(1,4), NewMaybeRelocatableFelt(FeltFromUint64(454))) - vm.Segments.Memory.Insert(NewRelocatable(1,5), NewMaybeRelocatableFelt(FeltFromUint64(206))) - + vm.Segments.Memory.Insert(NewRelocatable(1, 3), NewMaybeRelocatableFelt(FeltFromUint64(2645))) + vm.Segments.Memory.Insert(NewRelocatable(1, 4), NewMaybeRelocatableFelt(FeltFromUint64(454))) + vm.Segments.Memory.Insert(NewRelocatable(1, 5), NewMaybeRelocatableFelt(FeltFromUint64(206))) idsManager := SetupIdsForTest( map[string][]*MaybeRelocatable{ - "point": {NewMaybeRelocatableFelt(FeltFromDecString("-1"))}, + "point": {NewMaybeRelocatableRelocatable(NewRelocatable(1, 0))}, "ec_negative": {nil}, }, vm, @@ -79,15 +76,18 @@ func TestRunEcNegateOk(t *testing.T) { Ids: idsManager, Code: EC_NEGATE, }) - err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil) + exec_scopes := types.NewExecutionScopes() + err := hintProcessor.ExecuteHint(vm, &hintData, nil, exec_scopes) if err != nil { t.Errorf("Ec Negative hint test failed with error %s", err) + } else { + // Check ids.is_positive + value, err := exec_scopes.Get("value") + val := value.(*big.Int) + expected, _ := new(big.Int).SetString("115792089237316195423569751828682367333329274433232027476421668138471189901786", 10) + + if err != nil || expected.Cmp(val) != 0 { + t.Errorf("Ec Negative hint test incorrect value for exec_scopes.value") + } } - // Check ids.is_positive - value, err := idsManager.GetFelt("value", vm) - expected := FeltFromDecString("115792089237316195423569751828682367333329274433232027476421668138471189901786") - - if err != nil || value != expected { - t.Errorf("Ec Negative hint test incorrect value for ids.value") - } -} \ No newline at end of file +} diff --git a/pkg/hints/hint_processor.go b/pkg/hints/hint_processor.go index e7a0eb3f..acbe5175 100644 --- a/pkg/hints/hint_processor.go +++ b/pkg/hints/hint_processor.go @@ -1,7 +1,6 @@ package hints import ( - "fmt" "strings" . "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_codes" @@ -54,9 +53,6 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any, case ASSERT_NOT_EQUAL: return assert_not_equal(data.Ids, vm) case EC_NEGATE: - fmt.Println("match case") - fmt.Println("vm: ", vm) - fmt.Println("escopes", execScopes) return ec_negate_import_secp_p(*vm, *execScopes, data.Ids) case EC_NEGATE_EMBEDDED_SECP: return ec_negate_embedded_secp_p(*vm, *execScopes, data.Ids) From 7bf190976a7591ffe1c7552be20e2f8cd8ca1472 Mon Sep 17 00:00:00 2001 From: Milton Date: Mon, 18 Sep 2023 13:04:36 -0300 Subject: [PATCH 07/22] Second hint test --- pkg/hints/ec_hint.go | 4 +- pkg/hints/ec_hint_test.go | 85 +++++++++++++++++++++++++++++---------- 2 files changed, 67 insertions(+), 22 deletions(-) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index 8cd5cdfc..065f9050 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -2,6 +2,7 @@ package hints import ( "errors" + "fmt" "math/big" "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_utils" @@ -68,6 +69,7 @@ Implements main logic for `EC_NEGATE` and `EC_NEGATE_EMBEDDED_SECP` hints */ func ec_negate(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) + fmt.Println("point ", point) if err != nil { return err } @@ -109,9 +111,9 @@ Implements hint: */ func ec_negate_embedded_secp_p(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager) error { + fmt.Println("inside hint") secp_p := big.NewInt(1) secp_p.Lsh(secp_p, 256) secp_p.Sub(secp_p, big.NewInt(19)) - return ec_negate(virtual_machine, exec_scopes, ids_data, *secp_p) } diff --git a/pkg/hints/ec_hint_test.go b/pkg/hints/ec_hint_test.go index cbb22037..cbf25a45 100644 --- a/pkg/hints/ec_hint_test.go +++ b/pkg/hints/ec_hint_test.go @@ -1,6 +1,7 @@ package hints_test import ( + "fmt" "math/big" "testing" @@ -35,27 +36,6 @@ func TestBigInt3Pack86(t *testing.T) { } } -// fn run_ec_negate_ok() { -// let hint_code = "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"; -// let mut vm = vm_with_range_check!(); - -// vm.segments = segments![((1, 3), 2645i32), ((1, 4), 454i32), ((1, 5), 206i32)]; -// //Initialize fp -// vm.run_context.fp = 1; -// //Create hint_data -// let ids_data = ids_data!["point"]; -// let mut exec_scopes = ExecutionScopes::new(); -// //Execute the hint -// assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); -// //Check 'value' is defined in the vm scope -// assert_matches!( -// exec_scopes.get::("value"), -// Ok(x) if x == bigint_str!( -// "115792089237316195423569751828682367333329274433232027476421668138471189901786" -// ) -// ); -// } - func TestRunEcNegateOk(t *testing.T) { vm := NewVirtualMachine() vm.Segments.AddSegment() @@ -71,6 +51,9 @@ func TestRunEcNegateOk(t *testing.T) { }, vm, ) + + point, _ := idsManager.Get("point", vm) + fmt.Println("Ids manager: ", point) hintProcessor := CairoVmHintProcessor{} hintData := any(HintData{ Ids: idsManager, @@ -91,3 +74,63 @@ func TestRunEcNegateOk(t *testing.T) { } } } + +func TestRunEcEmbeddedSecpOk(t *testing.T) { + vm := NewVirtualMachine() + vm.Segments.AddSegment() + vm.Segments.AddSegment() + vm.Segments.Memory.Insert(NewRelocatable(1, 3), NewMaybeRelocatableFelt(FeltFromUint64(2645))) + vm.Segments.Memory.Insert(NewRelocatable(1, 4), NewMaybeRelocatableFelt(FeltFromUint64(454))) + vm.Segments.Memory.Insert(NewRelocatable(1, 5), NewMaybeRelocatableFelt(FeltFromUint64(206))) + + y2 := big.NewInt(206) + y2.Lsh(y2, 86*2) + + y1 := big.NewInt(454) + y1.Lsh(y1, 86) + + y0 := big.NewInt(2645) + + y := new(big.Int) + y.Add(y, y2) + y.Add(y, y1) + y.Add(y, y0) + + //vm.RunContext.Fp = NewRelocatable(1,0) + + idsManager := SetupIdsForTest( + map[string][]*MaybeRelocatable{ + "point": {NewMaybeRelocatableRelocatable(NewRelocatable(1, 0))}, + "ec_negative": {nil}, + }, + vm, + ) + + point, _ := idsManager.Get("point", vm) + fmt.Println("Ids manager: ", point) + hintProcessor := CairoVmHintProcessor{} + hintData := any(HintData{ + Ids: idsManager, + Code: EC_NEGATE_EMBEDDED_SECP, + }) + exec_scopes := types.NewExecutionScopes() + err := hintProcessor.ExecuteHint(vm, &hintData, nil, exec_scopes) + if err != nil { + t.Errorf("Ec Negative Embedded Sec hint test failed with error %s", err) + } else { + // Check ids.is_positive + value, err := exec_scopes.Get("value") + val := value.(*big.Int) + + // expected value + minus_y := big.NewInt(1) + minus_y.Lsh(minus_y, 255) + minus_y.Sub(minus_y, big.NewInt(19)) + minus_y.Sub(minus_y, y) + + if err != nil || minus_y.Cmp(val) != 0 { + t.Errorf("Ec Negative hint test incorrect value for exec_scopes.value") + } + } + +} From a08c642fc3d7e5eb7bbd902009bb199d99adf1a2 Mon Sep 17 00:00:00 2001 From: Milton Date: Mon, 18 Sep 2023 13:51:26 -0300 Subject: [PATCH 08/22] Test embedded hint --- pkg/hints/ec_hint.go | 2 +- pkg/hints/ec_hint_test.go | 116 +++++++++++++++++++------------------- 2 files changed, 58 insertions(+), 60 deletions(-) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index d81f19f1..270822cd 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -112,7 +112,7 @@ Implements hint: func ec_negate_embedded_secp_p(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager) error { secp_p := big.NewInt(1) - secp_p.Lsh(secp_p, 256) + secp_p.Lsh(secp_p, 255) secp_p.Sub(secp_p, big.NewInt(19)) return ec_negate(virtual_machine, exec_scopes, ids_data, *secp_p) } diff --git a/pkg/hints/ec_hint_test.go b/pkg/hints/ec_hint_test.go index 656275e1..bed5fbe3 100644 --- a/pkg/hints/ec_hint_test.go +++ b/pkg/hints/ec_hint_test.go @@ -75,62 +75,60 @@ func TestRunEcNegateOk(t *testing.T) { } } -// func TestRunEcEmbeddedSecpOk(t *testing.T) { -// vm := NewVirtualMachine() -// vm.Segments.AddSegment() -// vm.Segments.AddSegment() -// vm.Segments.Memory.Insert(NewRelocatable(1, 3), NewMaybeRelocatableFelt(FeltFromUint64(2645))) -// vm.Segments.Memory.Insert(NewRelocatable(1, 4), NewMaybeRelocatableFelt(FeltFromUint64(454))) -// vm.Segments.Memory.Insert(NewRelocatable(1, 5), NewMaybeRelocatableFelt(FeltFromUint64(206))) - -// y2 := big.NewInt(206) -// y2.Lsh(y2, 86*2) - -// y1 := big.NewInt(454) -// y1.Lsh(y1, 86) - -// y0 := big.NewInt(2645) - -// y := new(big.Int) -// y.Add(y, y2) -// y.Add(y, y1) -// y.Add(y, y0) - -// //vm.RunContext.Fp = NewRelocatable(1,0) - -// idsManager := SetupIdsForTest( -// map[string][]*MaybeRelocatable{ -// "point": {NewMaybeRelocatableRelocatable(NewRelocatable(1, 0))}, -// "ec_negative": {nil}, -// }, -// vm, -// ) - -// point, _ := idsManager.Get("point", vm) -// fmt.Println("Ids manager: ", point) -// hintProcessor := CairoVmHintProcessor{} -// hintData := any(HintData{ -// Ids: idsManager, -// Code: EC_NEGATE_EMBEDDED_SECP, -// }) -// exec_scopes := types.NewExecutionScopes() -// err := hintProcessor.ExecuteHint(vm, &hintData, nil, exec_scopes) -// if err != nil { -// t.Errorf("Ec Negative Embedded Sec hint test failed with error %s", err) -// } else { -// // Check ids.is_positive -// value, err := exec_scopes.Get("value") -// val := value.(*big.Int) - -// // expected value -// minus_y := big.NewInt(1) -// minus_y.Lsh(minus_y, 255) -// minus_y.Sub(minus_y, big.NewInt(19)) -// minus_y.Sub(minus_y, y) - -// if err != nil || minus_y.Cmp(val) != 0 { -// t.Errorf("Ec Negative hint test incorrect value for exec_scopes.value") -// } -// } - -// } +func TestRunEcEmbeddedSecpOk(t *testing.T) { + vm := NewVirtualMachine() + vm.Segments.AddSegment() + vm.Segments.AddSegment() + vm.Segments.Memory.Insert(NewRelocatable(1, 3), NewMaybeRelocatableFelt(FeltFromUint64(2645))) + vm.Segments.Memory.Insert(NewRelocatable(1, 4), NewMaybeRelocatableFelt(FeltFromUint64(454))) + vm.Segments.Memory.Insert(NewRelocatable(1, 5), NewMaybeRelocatableFelt(FeltFromUint64(206))) + + y2 := big.NewInt(206) + y2.Lsh(y2, 86*2) + + y1 := big.NewInt(454) + y1.Lsh(y1, 86) + + y0 := big.NewInt(2645) + + y := new(big.Int) + y.Add(y, y2) + y.Add(y, y1) + y.Add(y, y0) + + vm.RunContext.Fp = NewRelocatable(1, 1) + + idsManager := SetupIdsForTest( + map[string][]*MaybeRelocatable{ + "point": {NewMaybeRelocatableRelocatable(NewRelocatable(1, 0))}, + "ec_negative": {nil}, + }, + vm, + ) + + hintProcessor := CairoVmHintProcessor{} + hintData := any(HintData{ + Ids: idsManager, + Code: EC_NEGATE_EMBEDDED_SECP, + }) + exec_scopes := types.NewExecutionScopes() + err := hintProcessor.ExecuteHint(vm, &hintData, nil, exec_scopes) + if err != nil { + t.Errorf("Ec Negative Embedded Sec hint test failed with error %s", err) + } else { + // Check ids.is_positive + value, err := exec_scopes.Get("value") + val := value.(*big.Int) + + // expected value + minus_y := big.NewInt(1) + minus_y.Lsh(minus_y, 255) + minus_y.Sub(minus_y, big.NewInt(19)) + minus_y.Sub(minus_y, y) + + if err != nil || minus_y.Cmp(val) != 0 { + t.Errorf("Ec Negative hint test incorrect value for exec_scopes.value") + } + } + +} From 6c6eed9613848d3d047e8b131a998e2e20717c93 Mon Sep 17 00:00:00 2001 From: Milton Date: Tue, 19 Sep 2023 07:48:03 -0300 Subject: [PATCH 09/22] Change to Camel case --- pkg/hints/ec_hint.go | 22 ++++++++++------------ pkg/hints/hint_processor.go | 4 ++-- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index 270822cd..8c83ff07 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -2,7 +2,6 @@ package hints import ( "errors" - "fmt" "math/big" "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_utils" @@ -32,7 +31,7 @@ func (val *BigInt3) Pack86() big.Int { return *sum } -func FromBaseAddr(addr memory.Relocatable, virtual_machine vm.VirtualMachine) (BigInt3, error) { +func BigInt3FromBaseAddr(addr memory.Relocatable, virtual_machine 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))) @@ -45,18 +44,18 @@ func FromBaseAddr(addr memory.Relocatable, virtual_machine vm.VirtualMachine) (B return BigInt3{Limbs: limbs}, nil } -func FromVarName(name string, virtual_machine vm.VirtualMachine, ids_data hint_utils.IdsManager) (EcPoint, error) { +func BigInt3FromVarName(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 } - x, err := FromBaseAddr(point_addr, virtual_machine) + x, err := BigInt3FromBaseAddr(point_addr, virtual_machine) if err != nil { return EcPoint{}, err } - y, err := FromBaseAddr(point_addr.AddUint(3), virtual_machine) + y, err := BigInt3FromBaseAddr(point_addr.AddUint(3), virtual_machine) if err != nil { return EcPoint{}, err } @@ -67,9 +66,8 @@ func FromVarName(name string, virtual_machine vm.VirtualMachine, ids_data hint_u /* Implements main logic for `EC_NEGATE` and `EC_NEGATE_EMBEDDED_SECP` hints */ -func ec_negate(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager, secp_p big.Int) error { +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) - fmt.Println("point ", point) if err != nil { return err } @@ -79,7 +77,7 @@ func ec_negate(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionSco return err } - y_bigint3, err := FromBaseAddr(point_y, virtual_machine) + y_bigint3, err := BigInt3FromBaseAddr(point_y, virtual_machine) if err != nil { return err } @@ -93,9 +91,9 @@ func ec_negate(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionSco return nil } -func ec_negate_import_secp_p(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager) error { +func ecNegateImportSecpP(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager) error { secp_p, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007908834671663", 10) - return ec_negate(virtual_machine, exec_scopes, ids_data, *secp_p) + return ecNegate(virtual_machine, exec_scopes, ids_data, *secp_p) } /* @@ -110,9 +108,9 @@ Implements hint: %} */ -func ec_negate_embedded_secp_p(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager) error { +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 ec_negate(virtual_machine, exec_scopes, ids_data, *secp_p) + return ecNegate(virtual_machine, exec_scopes, ids_data, *secp_p) } diff --git a/pkg/hints/hint_processor.go b/pkg/hints/hint_processor.go index dbd1c2ed..ccc26810 100644 --- a/pkg/hints/hint_processor.go +++ b/pkg/hints/hint_processor.go @@ -63,9 +63,9 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any, case ASSERT_NOT_EQUAL: return assert_not_equal(data.Ids, vm) case EC_NEGATE: - return ec_negate_import_secp_p(*vm, *execScopes, data.Ids) + return ecNegateImportSecpP(*vm, *execScopes, data.Ids) case EC_NEGATE_EMBEDDED_SECP: - return ec_negate_embedded_secp_p(*vm, *execScopes, data.Ids) + return ecNegateEmbeddedSecpP(*vm, *execScopes, data.Ids) case POW: return pow(data.Ids, vm) case SQRT: From ec9345c31f711995264a1257fb165ce4da796f30 Mon Sep 17 00:00:00 2001 From: Milton Date: Tue, 19 Sep 2023 10:24:58 -0300 Subject: [PATCH 10/22] Implement slope hints --- pkg/hints/ec_hint.go | 95 +++++++++++++++++++++++++++++ pkg/hints/hint_codes/ec_op_hints.go | 2 + pkg/hints/hint_processor.go | 4 ++ pkg/hints/hint_utils/secp_utils.go | 13 ++++ 4 files changed, 114 insertions(+) create mode 100644 pkg/hints/hint_utils/secp_utils.go diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index 8c83ff07..ec4e1c96 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -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" @@ -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 := BigInt3FromVarName(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 := BigInt3FromVarName(point0_alias, virtual_machine, ids_data) + if err != nil { + return err + } + point1, err := BigInt3FromVarName(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 +} diff --git a/pkg/hints/hint_codes/ec_op_hints.go b/pkg/hints/hint_codes/ec_op_hints.go index c2092a7f..254720c5 100644 --- a/pkg/hints/hint_codes/ec_op_hints.go +++ b/pkg/hints/hint_codes/ec_op_hints.go @@ -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)" diff --git a/pkg/hints/hint_processor.go b/pkg/hints/hint_processor.go index ccc26810..a15cd939 100644 --- a/pkg/hints/hint_processor.go +++ b/pkg/hints/hint_processor.go @@ -74,6 +74,10 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any, return memcpy_enter_scope(data.Ids, vm, execScopes) 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()) default: return errors.Errorf("Unknown Hint: %s", data.Code) } diff --git a/pkg/hints/hint_utils/secp_utils.go b/pkg/hints/hint_utils/secp_utils.go new file mode 100644 index 00000000..f531944c --- /dev/null +++ b/pkg/hints/hint_utils/secp_utils.go @@ -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 +} From ff7fb500c8dcf3f1a393200b1816141ac58412c4 Mon Sep 17 00:00:00 2001 From: Milton Date: Tue, 19 Sep 2023 10:26:38 -0300 Subject: [PATCH 11/22] Fix format --- pkg/hints/hint_codes/ec_op_hints.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/hints/hint_codes/ec_op_hints.go b/pkg/hints/hint_codes/ec_op_hints.go index 7feaf789..254720c5 100644 --- a/pkg/hints/hint_codes/ec_op_hints.go +++ b/pkg/hints/hint_codes/ec_op_hints.go @@ -4,4 +4,3 @@ const EC_NEGATE = "from starkware.cairo.common.cairo_secp.secp_utils import SECP 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)" - From 4e9afc502a967645bca70e1572c057e4d0ef28d5 Mon Sep 17 00:00:00 2001 From: Milton Date: Tue, 19 Sep 2023 10:27:28 -0300 Subject: [PATCH 12/22] Delete github conflict string --- pkg/hints/ec_hint.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index c967060d..5668fbaf 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -4,10 +4,7 @@ import ( "errors" "math/big" -<<<<<<< HEAD "github.com/lambdaclass/cairo-vm.go/pkg/builtins" -======= ->>>>>>> main "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" From cc53b652fc6c0fdc56953f63db6cdb1cf123f5c4 Mon Sep 17 00:00:00 2001 From: Milton Date: Tue, 19 Sep 2023 11:45:32 -0300 Subject: [PATCH 13/22] Tests hints --- pkg/hints/ec_hint.go | 7 ++- pkg/hints/ec_hint_test.go | 102 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 1 deletion(-) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index 5668fbaf..08747f37 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -2,6 +2,7 @@ package hints import ( "errors" + "fmt" "math/big" "github.com/lambdaclass/cairo-vm.go/pkg/builtins" @@ -46,7 +47,10 @@ func BigInt3FromBaseAddr(addr memory.Relocatable, virtual_machine vm.VirtualMach } func BigInt3FromVarName(name string, virtual_machine vm.VirtualMachine, ids_data hint_utils.IdsManager) (EcPoint, error) { + fmt.Println(name) + fmt.Println("ids data", ids_data) point_addr, err := ids_data.GetAddr(name, &virtual_machine) + fmt.Println(point_addr) if err != nil { return EcPoint{}, err } @@ -133,10 +137,12 @@ Implements hint: 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) + fmt.Println(ids_data) point, err := BigInt3FromVarName(point_alias, virtual_machine, ids_data) if err != nil { return err } + fmt.Println("after point") x := point.X.Pack86() y := point.Y.Pack86() @@ -209,4 +215,3 @@ func computeSlope(virtual_machine vm.VirtualMachine, exec_scopes types.Execution return nil } - diff --git a/pkg/hints/ec_hint_test.go b/pkg/hints/ec_hint_test.go index bed5fbe3..db15ef15 100644 --- a/pkg/hints/ec_hint_test.go +++ b/pkg/hints/ec_hint_test.go @@ -132,3 +132,105 @@ func TestRunEcEmbeddedSecpOk(t *testing.T) { } } + +// func TestComputeDoublingSlopeOk(t *testing.T) { +// vm := NewVirtualMachine() +// vm.Segments.AddSegment() +// vm.Segments.AddSegment() +// vm.Segments.Memory.Insert(NewRelocatable(1, 0), NewMaybeRelocatableFelt(FeltFromUint64(614323))) +// vm.Segments.Memory.Insert(NewRelocatable(1, 1), NewMaybeRelocatableFelt(FeltFromUint64(5456867))) +// vm.Segments.Memory.Insert(NewRelocatable(1, 2), NewMaybeRelocatableFelt(FeltFromUint64(101208))) +// vm.Segments.Memory.Insert(NewRelocatable(1, 3), NewMaybeRelocatableFelt(FeltFromUint64(773712524))) +// vm.Segments.Memory.Insert(NewRelocatable(1, 4), NewMaybeRelocatableFelt(FeltFromUint64(77371252))) +// vm.Segments.Memory.Insert(NewRelocatable(1, 5), NewMaybeRelocatableFelt(FeltFromUint64(5298795))) + +// vm.RunContext.Fp = NewRelocatable(1, 1) + +// idsManager := SetupIdsForTest( +// map[string][]*MaybeRelocatable{ +// "point": {NewMaybeRelocatableRelocatable(NewRelocatable(1, 0))}, +// }, +// 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.Segments.Memory.Insert(NewRelocatable(1, 0), NewMaybeRelocatableFelt(FeltFromUint64(134))) + vm.Segments.Memory.Insert(NewRelocatable(1, 1), NewMaybeRelocatableFelt(FeltFromUint64(5123))) + vm.Segments.Memory.Insert(NewRelocatable(1, 2), NewMaybeRelocatableFelt(FeltFromUint64(140))) + vm.Segments.Memory.Insert(NewRelocatable(1, 3), NewMaybeRelocatableFelt(FeltFromUint64(1232))) + vm.Segments.Memory.Insert(NewRelocatable(1, 4), NewMaybeRelocatableFelt(FeltFromUint64(4652))) + vm.Segments.Memory.Insert(NewRelocatable(1, 5), NewMaybeRelocatableFelt(FeltFromUint64(720))) + vm.Segments.Memory.Insert(NewRelocatable(1, 6), NewMaybeRelocatableFelt(FeltFromUint64(156))) + vm.Segments.Memory.Insert(NewRelocatable(1, 7), NewMaybeRelocatableFelt(FeltFromUint64(6545))) + vm.Segments.Memory.Insert(NewRelocatable(1, 8), NewMaybeRelocatableFelt(FeltFromUint64(100010))) + vm.Segments.Memory.Insert(NewRelocatable(1, 9), NewMaybeRelocatableFelt(FeltFromUint64(1123))) + vm.Segments.Memory.Insert(NewRelocatable(1, 10), NewMaybeRelocatableFelt(FeltFromUint64(1325))) + vm.Segments.Memory.Insert(NewRelocatable(1, 11), NewMaybeRelocatableFelt(FeltFromUint64(910))) + + vm.RunContext.Fp = NewRelocatable(1, 14) + + idsManager := SetupIdsForTest( + map[string][]*MaybeRelocatable{ + "point0": {NewMaybeRelocatableRelocatable(NewRelocatable(1, 0))}, + "point1": {NewMaybeRelocatableRelocatable(NewRelocatable(1, 6))}, + }, + 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") + } + } +} From a0d269bdc574e8e7931a15868eff246e4045bf95 Mon Sep 17 00:00:00 2001 From: Milton Date: Tue, 19 Sep 2023 12:49:39 -0300 Subject: [PATCH 14/22] Tests hints slopes --- pkg/hints/ec_hint.go | 4 -- pkg/hints/ec_hint_test.go | 126 ++++++++++++++++++++------------------ 2 files changed, 65 insertions(+), 65 deletions(-) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index 08747f37..4cc18ea9 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -47,10 +47,7 @@ func BigInt3FromBaseAddr(addr memory.Relocatable, virtual_machine vm.VirtualMach } func BigInt3FromVarName(name string, virtual_machine vm.VirtualMachine, ids_data hint_utils.IdsManager) (EcPoint, error) { - fmt.Println(name) - fmt.Println("ids data", ids_data) point_addr, err := ids_data.GetAddr(name, &virtual_machine) - fmt.Println(point_addr) if err != nil { return EcPoint{}, err } @@ -137,7 +134,6 @@ Implements hint: 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) - fmt.Println(ids_data) point, err := BigInt3FromVarName(point_alias, virtual_machine, ids_data) if err != nil { return err diff --git a/pkg/hints/ec_hint_test.go b/pkg/hints/ec_hint_test.go index db15ef15..70090666 100644 --- a/pkg/hints/ec_hint_test.go +++ b/pkg/hints/ec_hint_test.go @@ -133,80 +133,84 @@ func TestRunEcEmbeddedSecpOk(t *testing.T) { } -// func TestComputeDoublingSlopeOk(t *testing.T) { -// vm := NewVirtualMachine() -// vm.Segments.AddSegment() -// vm.Segments.AddSegment() -// vm.Segments.Memory.Insert(NewRelocatable(1, 0), NewMaybeRelocatableFelt(FeltFromUint64(614323))) -// vm.Segments.Memory.Insert(NewRelocatable(1, 1), NewMaybeRelocatableFelt(FeltFromUint64(5456867))) -// vm.Segments.Memory.Insert(NewRelocatable(1, 2), NewMaybeRelocatableFelt(FeltFromUint64(101208))) -// vm.Segments.Memory.Insert(NewRelocatable(1, 3), NewMaybeRelocatableFelt(FeltFromUint64(773712524))) -// vm.Segments.Memory.Insert(NewRelocatable(1, 4), NewMaybeRelocatableFelt(FeltFromUint64(77371252))) -// vm.Segments.Memory.Insert(NewRelocatable(1, 5), NewMaybeRelocatableFelt(FeltFromUint64(5298795))) - -// vm.RunContext.Fp = NewRelocatable(1, 1) - -// idsManager := SetupIdsForTest( -// map[string][]*MaybeRelocatable{ -// "point": {NewMaybeRelocatableRelocatable(NewRelocatable(1, 0))}, -// }, -// 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 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.Segments.Memory.Insert(NewRelocatable(1, 0), NewMaybeRelocatableFelt(FeltFromUint64(134))) - vm.Segments.Memory.Insert(NewRelocatable(1, 1), NewMaybeRelocatableFelt(FeltFromUint64(5123))) - vm.Segments.Memory.Insert(NewRelocatable(1, 2), NewMaybeRelocatableFelt(FeltFromUint64(140))) - vm.Segments.Memory.Insert(NewRelocatable(1, 3), NewMaybeRelocatableFelt(FeltFromUint64(1232))) - vm.Segments.Memory.Insert(NewRelocatable(1, 4), NewMaybeRelocatableFelt(FeltFromUint64(4652))) - vm.Segments.Memory.Insert(NewRelocatable(1, 5), NewMaybeRelocatableFelt(FeltFromUint64(720))) - vm.Segments.Memory.Insert(NewRelocatable(1, 6), NewMaybeRelocatableFelt(FeltFromUint64(156))) - vm.Segments.Memory.Insert(NewRelocatable(1, 7), NewMaybeRelocatableFelt(FeltFromUint64(6545))) - vm.Segments.Memory.Insert(NewRelocatable(1, 8), NewMaybeRelocatableFelt(FeltFromUint64(100010))) - vm.Segments.Memory.Insert(NewRelocatable(1, 9), NewMaybeRelocatableFelt(FeltFromUint64(1123))) - vm.Segments.Memory.Insert(NewRelocatable(1, 10), NewMaybeRelocatableFelt(FeltFromUint64(1325))) - vm.Segments.Memory.Insert(NewRelocatable(1, 11), NewMaybeRelocatableFelt(FeltFromUint64(910))) vm.RunContext.Fp = NewRelocatable(1, 14) idsManager := SetupIdsForTest( map[string][]*MaybeRelocatable{ - "point0": {NewMaybeRelocatableRelocatable(NewRelocatable(1, 0))}, - "point1": {NewMaybeRelocatableRelocatable(NewRelocatable(1, 6))}, + "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, From a27b814fee20bd0ac56b7e15ae28bba12f1717d7 Mon Sep 17 00:00:00 2001 From: Milton Date: Tue, 19 Sep 2023 12:52:40 -0300 Subject: [PATCH 15/22] Rename misleading name function --- pkg/hints/ec_hint.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index 4cc18ea9..d0bc80a4 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -46,7 +46,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 EcFromVarName(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 @@ -134,7 +134,7 @@ Implements hint: 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 := BigInt3FromVarName(point_alias, virtual_machine, ids_data) + point, err := EcFromVarName(point_alias, virtual_machine, ids_data) if err != nil { return err } @@ -176,11 +176,11 @@ func computeSlopeAndAssingSecpP(virtual_machine vm.VirtualMachine, exec_scopes t } func computeSlope(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager, point0_alias string, point1_alias string) error { - point0, err := BigInt3FromVarName(point0_alias, virtual_machine, ids_data) + point0, err := EcFromVarName(point0_alias, virtual_machine, ids_data) if err != nil { return err } - point1, err := BigInt3FromVarName(point1_alias, virtual_machine, ids_data) + point1, err := EcFromVarName(point1_alias, virtual_machine, ids_data) if err != nil { return err } From 9e3950249ae8ae73ebf8180a19f9595bc54d99e5 Mon Sep 17 00:00:00 2001 From: Milton Date: Tue, 19 Sep 2023 16:25:30 -0300 Subject: [PATCH 16/22] Fix function name --- pkg/hints/ec_hint.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index d0bc80a4..d114f900 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -46,7 +46,7 @@ func BigInt3FromBaseAddr(addr memory.Relocatable, virtual_machine vm.VirtualMach return BigInt3{Limbs: limbs}, nil } -func EcFromVarName(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 From 0fdad83680d7d7db6a7a97508a6aa1c6a1b6ee95 Mon Sep 17 00:00:00 2001 From: Milton Date: Tue, 19 Sep 2023 17:58:34 -0300 Subject: [PATCH 17/22] Fix error in function call --- pkg/hints/ec_hint.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index d114f900..4f291a32 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -134,7 +134,7 @@ Implements hint: 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 := EcFromVarName(point_alias, virtual_machine, ids_data) + point, err := EcPointFromVarName(point_alias, virtual_machine, ids_data) if err != nil { return err } @@ -176,11 +176,11 @@ func computeSlopeAndAssingSecpP(virtual_machine vm.VirtualMachine, exec_scopes t } func computeSlope(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager, point0_alias string, point1_alias string) error { - point0, err := EcFromVarName(point0_alias, virtual_machine, ids_data) + point0, err := EcPointFromVarName(point0_alias, virtual_machine, ids_data) if err != nil { return err } - point1, err := EcFromVarName(point1_alias, virtual_machine, ids_data) + point1, err := EcPointFromVarName(point1_alias, virtual_machine, ids_data) if err != nil { return err } From 78dd5d026797e3020448c6e2f63bd8ec87474c50 Mon Sep 17 00:00:00 2001 From: Milton Date: Tue, 19 Sep 2023 18:28:04 -0300 Subject: [PATCH 18/22] Delete debug info --- pkg/hints/ec_hint.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index 4f291a32..5df3d00c 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -138,7 +138,6 @@ func computeDoublingSlope(virtual_machine vm.VirtualMachine, exec_scopes types.E if err != nil { return err } - fmt.Println("after point") x := point.X.Pack86() y := point.Y.Pack86() From facfb8a23c74756459f7fe0b8be3c203c0a07222 Mon Sep 17 00:00:00 2001 From: Milton Date: Tue, 19 Sep 2023 18:29:59 -0300 Subject: [PATCH 19/22] Delete unused import --- pkg/hints/ec_hint.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index 5df3d00c..821fa266 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -2,7 +2,6 @@ package hints import ( "errors" - "fmt" "math/big" "github.com/lambdaclass/cairo-vm.go/pkg/builtins" From 6332e1c34970b950db0fb4771d838310344ea6ef Mon Sep 17 00:00:00 2001 From: Milton Date: Thu, 21 Sep 2023 08:57:22 -0300 Subject: [PATCH 20/22] Change name convention --- pkg/hints/ec_hint.go | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index 821fa266..16588087 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -8,7 +8,7 @@ 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/types" - "github.com/lambdaclass/cairo-vm.go/pkg/vm" + . "github.com/lambdaclass/cairo-vm.go/pkg/vm" "github.com/lambdaclass/cairo-vm.go/pkg/vm/memory" ) @@ -32,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 { @@ -45,18 +45,18 @@ func BigInt3FromBaseAddr(addr memory.Relocatable, virtual_machine vm.VirtualMach return BigInt3{Limbs: limbs}, nil } -func EcPointFromVarName(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, ids_data hint_utils.IdsManager) (EcPoint, error) { + point_addr, err := ids_data.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 } @@ -67,8 +67,8 @@ func EcPointFromVarName(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, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager, secp_p big.Int) error { + point, err := ids_data.GetRelocatable("point", &vm) if err != nil { return err } @@ -78,7 +78,7 @@ func ecNegate(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScop return err } - y_bigint3, err := BigInt3FromBaseAddr(point_y, virtual_machine) + y_bigint3, err := BigInt3FromBaseAddr(point_y, vm) if err != nil { return err } @@ -92,9 +92,9 @@ func ecNegate(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScop return nil } -func ecNegateImportSecpP(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager) error { +func ecNegateImportSecpP(vm VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager) error { secp_p, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007908834671663", 10) - return ecNegate(virtual_machine, exec_scopes, ids_data, *secp_p) + return ecNegate(vm, exec_scopes, ids_data, *secp_p) } /* @@ -109,11 +109,11 @@ Implements hint: %} */ -func ecNegateEmbeddedSecpP(virtual_machine vm.VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager) error { +func ecNegateEmbeddedSecpP(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) + return ecNegate(vm, exec_scopes, ids_data, *secp_p) } /* @@ -130,10 +130,10 @@ Implements hint: %} */ -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 { +func computeDoublingSlope(vm VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager, pointAlias 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) + point, err := EcPointFromVarName(pointAlias, vm, ids_data) if err != nil { return err } @@ -168,17 +168,17 @@ Implements hint: %} */ -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 { +func computeSlopeAndAssingSecpP(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) + return computeSlope(vm, 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) +func computeSlope(vm VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager, point0_alias string, point1_alias string) error { + point0, err := EcPointFromVarName(point0_alias, vm, ids_data) if err != nil { return err } - point1, err := EcPointFromVarName(point1_alias, virtual_machine, ids_data) + point1, err := EcPointFromVarName(point1_alias, vm, ids_data) if err != nil { return err } From 47a9ff54c79f4c7ddff3ce7c4ac2c981fd0d3a90 Mon Sep 17 00:00:00 2001 From: Milton Date: Thu, 21 Sep 2023 09:14:44 -0300 Subject: [PATCH 21/22] Change naming convention --- pkg/hints/ec_hint.go | 66 ++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index 16588087..34e094f8 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -5,9 +5,9 @@ import ( "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/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/types" . "github.com/lambdaclass/cairo-vm.go/pkg/vm" "github.com/lambdaclass/cairo-vm.go/pkg/vm/memory" ) @@ -45,8 +45,8 @@ func BigInt3FromBaseAddr(addr memory.Relocatable, vm VirtualMachine) (BigInt3, e return BigInt3{Limbs: limbs}, nil } -func EcPointFromVarName(name string, vm VirtualMachine, ids_data hint_utils.IdsManager) (EcPoint, error) { - point_addr, err := ids_data.GetAddr(name, &vm) +func EcPointFromVarName(name string, vm VirtualMachine, idsData IdsManager) (EcPoint, error) { + point_addr, err := idsData.GetAddr(name, &vm) if err != nil { return EcPoint{}, err } @@ -67,34 +67,34 @@ func EcPointFromVarName(name string, vm VirtualMachine, ids_data hint_utils.IdsM /* Implements main logic for `EC_NEGATE` and `EC_NEGATE_EMBEDDED_SECP` hints */ -func ecNegate(vm VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager, secp_p big.Int) error { - point, err := ids_data.GetRelocatable("point", &vm) +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, vm) + y_bigint3, err := BigInt3FromBaseAddr(pointY, vm) 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(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(vm, exec_scopes, ids_data, *secp_p) + return ecNegate(vm, execScopes, idsData, *secp_p) } /* @@ -109,11 +109,11 @@ Implements hint: %} */ -func ecNegateEmbeddedSecpP(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(vm, 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) } /* @@ -130,10 +130,10 @@ Implements hint: %} */ -func computeDoublingSlope(vm VirtualMachine, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager, pointAlias string, secp_p big.Int, alpha big.Int) error { - exec_scopes.AssignOrUpdateVariable("SECP_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, ids_data) + point, err := EcPointFromVarName(pointAlias, vm, idsData) if err != nil { return err } @@ -142,13 +142,13 @@ func computeDoublingSlope(vm VirtualMachine, exec_scopes types.ExecutionScopes, y := point.Y.Pack86() double_point := builtins.DoublePointB{X: x, Y: y} - value, err := builtins.EcDoubleSlope(double_point, alpha, secp_p) + value, err := builtins.EcDoubleSlope(double_point, alpha, SecpP) if err != nil { return err } - exec_scopes.AssignOrUpdateVariable("value", value) - exec_scopes.AssignOrUpdateVariable("slope", value) + execScopes.AssignOrUpdateVariable("value", value) + execScopes.AssignOrUpdateVariable("slope", value) return nil } @@ -168,22 +168,22 @@ Implements hint: %} */ -func computeSlopeAndAssingSecpP(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(vm, exec_scopes, ids_data, point0_alias, point1_alias) +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, exec_scopes types.ExecutionScopes, ids_data hint_utils.IdsManager, point0_alias string, point1_alias string) error { - point0, err := EcPointFromVarName(point0_alias, vm, ids_data) +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, ids_data) + point1, err := EcPointFromVarName(point1_alias, vm, idsData) if err != nil { return err } - secp_p, err := exec_scopes.Get("SECP_P") + secp_p, err := execScopes.Get("SECP_P") if err != nil { return err } @@ -204,8 +204,8 @@ func computeSlope(vm VirtualMachine, exec_scopes types.ExecutionScopes, ids_data return err } - exec_scopes.AssignOrUpdateVariable("value", value) - exec_scopes.AssignOrUpdateVariable("slope", value) + execScopes.AssignOrUpdateVariable("value", value) + execScopes.AssignOrUpdateVariable("slope", value) return nil } From 5c18154ae7b0c7632ea5cd719064ea534bcfecd9 Mon Sep 17 00:00:00 2001 From: Mariano Nicolini Date: Thu, 21 Sep 2023 12:23:30 -0300 Subject: [PATCH 22/22] Fix naming convention in variables --- pkg/hints/ec_hint.go | 32 +++++++++++++++--------------- pkg/hints/ec_hint_test.go | 26 ++++++++++++------------ pkg/hints/hint_utils/secp_utils.go | 4 ++-- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/pkg/hints/ec_hint.go b/pkg/hints/ec_hint.go index 34e094f8..922711f2 100644 --- a/pkg/hints/ec_hint.go +++ b/pkg/hints/ec_hint.go @@ -46,17 +46,17 @@ func BigInt3FromBaseAddr(addr memory.Relocatable, vm VirtualMachine) (BigInt3, e } func EcPointFromVarName(name string, vm VirtualMachine, idsData IdsManager) (EcPoint, error) { - point_addr, err := idsData.GetAddr(name, &vm) + pointAddr, err := idsData.GetAddr(name, &vm) if err != nil { return EcPoint{}, err } - x, err := BigInt3FromBaseAddr(point_addr, vm) + x, err := BigInt3FromBaseAddr(pointAddr, vm) if err != nil { return EcPoint{}, err } - y, err := BigInt3FromBaseAddr(point_addr.AddUint(3), vm) + y, err := BigInt3FromBaseAddr(pointAddr.AddUint(3), vm) if err != nil { return EcPoint{}, err } @@ -78,12 +78,12 @@ func ecNegate(vm VirtualMachine, execScopes ExecutionScopes, idsData IdsManager, return err } - y_bigint3, err := BigInt3FromBaseAddr(pointY, vm) + yBigint3, err := BigInt3FromBaseAddr(pointY, vm) if err != nil { return err } - y := y_bigint3.Pack86() + y := yBigint3.Pack86() value := new(big.Int).Neg(&y) value.Mod(value, &secpP) @@ -93,8 +93,8 @@ func ecNegate(vm VirtualMachine, execScopes ExecutionScopes, idsData IdsManager, } func ecNegateImportSecpP(vm VirtualMachine, execScopes ExecutionScopes, idsData IdsManager) error { - secp_p, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007908834671663", 10) - return ecNegate(vm, execScopes, idsData, *secp_p) + secpP, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007908834671663", 10) + return ecNegate(vm, execScopes, idsData, *secpP) } /* @@ -140,9 +140,9 @@ func computeDoublingSlope(vm VirtualMachine, execScopes ExecutionScopes, idsData x := point.X.Pack86() y := point.Y.Pack86() - double_point := builtins.DoublePointB{X: x, Y: y} + doublePoint := builtins.DoublePointB{X: x, Y: y} - value, err := builtins.EcDoubleSlope(double_point, alpha, SecpP) + value, err := builtins.EcDoubleSlope(doublePoint, alpha, SecpP) if err != nil { return err } @@ -168,26 +168,26 @@ Implements hint: %} */ -func computeSlopeAndAssingSecpP(vm VirtualMachine, execScopes ExecutionScopes, idsData IdsManager, point0Alias string, point1Alias string, secp_p big.Int) error { - execScopes.AssignOrUpdateVariable("SECP_P", secp_p) +func computeSlopeAndAssingSecpP(vm VirtualMachine, execScopes ExecutionScopes, idsData IdsManager, point0Alias string, point1Alias string, secpP big.Int) error { + execScopes.AssignOrUpdateVariable("SECP_P", secpP) 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) +func computeSlope(vm VirtualMachine, execScopes ExecutionScopes, idsData IdsManager, point0Alias string, point1Alias string) error { + point0, err := EcPointFromVarName(point0Alias, vm, idsData) if err != nil { return err } - point1, err := EcPointFromVarName(point1_alias, vm, idsData) + point1, err := EcPointFromVarName(point1Alias, vm, idsData) if err != nil { return err } - secp_p, err := execScopes.Get("SECP_P") + secpP, err := execScopes.Get("SECP_P") if err != nil { return err } - secp := secp_p.(big.Int) + secp := secpP.(big.Int) // build partial sum x0 := point0.X.Pack86() diff --git a/pkg/hints/ec_hint_test.go b/pkg/hints/ec_hint_test.go index 05cb78e4..4e5e0fbc 100644 --- a/pkg/hints/ec_hint_test.go +++ b/pkg/hints/ec_hint_test.go @@ -55,13 +55,13 @@ func TestRunEcNegateOk(t *testing.T) { Ids: idsManager, Code: EC_NEGATE, }) - exec_scopes := types.NewExecutionScopes() - err := hintProcessor.ExecuteHint(vm, &hintData, nil, exec_scopes) + execScopes := types.NewExecutionScopes() + err := hintProcessor.ExecuteHint(vm, &hintData, nil, execScopes) if err != nil { t.Errorf("Ec Negative hint test failed with error %s", err) } else { // Check ids.is_positive - value, err := exec_scopes.Get("value") + value, err := execScopes.Get("value") val := value.(*big.Int) expected, _ := new(big.Int).SetString("115792089237316195423569751828682367333329274433232027476421668138471189901786", 10) @@ -168,11 +168,11 @@ func TestComputeDoublingSlopeOk(t *testing.T) { slope := slope_res.(big.Int) // expected values - expected_val, _ := new(big.Int).SetString("40442433062102151071094722250325492738932110061897694430475034100717288403728", 10) + expectedVal, _ := new(big.Int).SetString("40442433062102151071094722250325492738932110061897694430475034100717288403728", 10) - expected_slope, _ := new(big.Int).SetString("40442433062102151071094722250325492738932110061897694430475034100717288403728", 10) + expectedSlope, _ := new(big.Int).SetString("40442433062102151071094722250325492738932110061897694430475034100717288403728", 10) - if expected_val.Cmp(&val) != 0 || expected_slope.Cmp(&slope) != 0 { + if expectedVal.Cmp(&val) != 0 || expectedSlope.Cmp(&slope) != 0 { t.Errorf("EC_DOUBLE_SLOPE_V1 hint test incorrect value for exec_scopes.value or exec_scopes.slope") } } @@ -213,23 +213,23 @@ func TestRunComputeSlopeOk(t *testing.T) { Code: COMPUTE_SLOPE_V1, }) - exec_scopes := types.NewExecutionScopes() - err := hintProcessor.ExecuteHint(vm, &hintData, nil, exec_scopes) + execScopes := types.NewExecutionScopes() + err := hintProcessor.ExecuteHint(vm, &hintData, nil, execScopes) if err != nil { t.Errorf("EC_DOUBLE_SLOPE_V1 hint test failed with error %s", err) } else { - value, _ := exec_scopes.Get("value") + value, _ := execScopes.Get("value") val := value.(big.Int) - slope_res, _ := exec_scopes.Get("slope") + slope_res, _ := execScopes.Get("slope") slope := slope_res.(big.Int) // expected values - expected_val, _ := new(big.Int).SetString("41419765295989780131385135514529906223027172305400087935755859001910844026631", 10) + expectedVal, _ := new(big.Int).SetString("41419765295989780131385135514529906223027172305400087935755859001910844026631", 10) - expected_slope, _ := new(big.Int).SetString("41419765295989780131385135514529906223027172305400087935755859001910844026631", 10) + expectedSlope, _ := new(big.Int).SetString("41419765295989780131385135514529906223027172305400087935755859001910844026631", 10) - if expected_val.Cmp(&val) != 0 || expected_slope.Cmp(&slope) != 0 { + if expectedVal.Cmp(&val) != 0 || expectedSlope.Cmp(&slope) != 0 { t.Errorf("EC_DOUBLE_SLOPE_V1 hint test incorrect value for exec_scopes.value or exec_scopes.slope") } } diff --git a/pkg/hints/hint_utils/secp_utils.go b/pkg/hints/hint_utils/secp_utils.go index f531944c..d7c9955b 100644 --- a/pkg/hints/hint_utils/secp_utils.go +++ b/pkg/hints/hint_utils/secp_utils.go @@ -3,8 +3,8 @@ package hint_utils import "math/big" func SECP_P() big.Int { - secp_p, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007908834671663", 10) - return *secp_p + secpP, _ := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007908834671663", 10) + return *secpP } func ALPHA() big.Int {