From c744c9402c6f7a808931e245049d3a526ad7b119 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Tue, 12 Sep 2023 18:55:47 -0300 Subject: [PATCH 01/14] Merge and add basic builtin --- pkg/hints/hint_processor.go | 2 ++ pkg/hints/math_hint_codes.go | 2 ++ pkg/hints/math_hints.go | 10 ++++++++++ pkg/hints/math_hints_test.go | 20 ++++++++++++++++++++ 4 files changed, 34 insertions(+) diff --git a/pkg/hints/hint_processor.go b/pkg/hints/hint_processor.go index 082a8e80..e2e18a3d 100644 --- a/pkg/hints/hint_processor.go +++ b/pkg/hints/hint_processor.go @@ -40,6 +40,8 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any, switch data.Code { case ASSERT_NN: return assert_nn(data.Ids, vm) + case VERIFY_ECDSA_SIGNATURE: + return verify_edsa_signature(data.Ids, vm) default: return errors.New("Unknown Hint") } diff --git a/pkg/hints/math_hint_codes.go b/pkg/hints/math_hint_codes.go index 022bd9e2..32479717 100644 --- a/pkg/hints/math_hint_codes.go +++ b/pkg/hints/math_hint_codes.go @@ -1,3 +1,5 @@ package hints 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.'" + +const VERIFY_ECDSA_SIGNATURE = "ecdsa_builtin.add_signature(ids.ecdsa_ptr.address_, (ids.signature_r, ids.signature_s))" diff --git a/pkg/hints/math_hints.go b/pkg/hints/math_hints.go index c8e00c26..20fcbac7 100644 --- a/pkg/hints/math_hints.go +++ b/pkg/hints/math_hints.go @@ -25,3 +25,13 @@ func assert_nn(ids IdsManager, vm *VirtualMachine) error { } return nil } + +func verify_edsa_signature(ids IdsManager, vm *VirtualMachine) error { + r, err := ids.GetFelt("signature_r", vm) + s, err := ids.GetFelt("signature_s", vm) + ecdsa_ptr, err := ids.GetRelocatable("ecdsa_ptr", vm) + + // vm.get_signature_builtin? + + return nil +} diff --git a/pkg/hints/math_hints_test.go b/pkg/hints/math_hints_test.go index 19f64062..45b9defb 100644 --- a/pkg/hints/math_hints_test.go +++ b/pkg/hints/math_hints_test.go @@ -49,3 +49,23 @@ func TestIsNNHintFail(t *testing.T) { t.Errorf("ASSERT_NN hint should have failed") } } + +func TestDummy(t *testing.T) { + vm := NewVirtualMachine() + vm.Segments.AddSegment() + idsManager := SetupIdsForTest( + map[string][]*MaybeRelocatable{ + "a": {NewMaybeRelocatableFelt(FeltFromDecString("-1"))}, + }, + vm, + ) + hintProcessor := CairoVmHintProcessor{} + hintData := any(HintData{ + Ids: idsManager, + Code: ASSERT_NN, + }) + err := hintProcessor.ExecuteHint(vm, &hintData, nil) + if err == nil { + t.Errorf("ASSERT_NN hint should have failed") + } +} From 8f55a62280bc51e9b3a34cddd2260fad36efa878 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Wed, 13 Sep 2023 10:52:03 -0300 Subject: [PATCH 02/14] WIP --- pkg/hints/math_hints.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/hints/math_hints.go b/pkg/hints/math_hints.go index aa51d623..ec2765e1 100644 --- a/pkg/hints/math_hints.go +++ b/pkg/hints/math_hints.go @@ -50,6 +50,6 @@ func verify_edsa_signature(ids IdsManager, vm *VirtualMachine) error { r, err := ids.GetFelt("signature_r", vm) s, err := ids.GetFelt("signature_s", vm) ecdsa_ptr, err := ids.GetRelocatable("ecdsa_ptr", vm) - + return nil // vm.get_signature_builtin? } From 51f4474a60ccc5099335379e577d18d1df4f28ef Mon Sep 17 00:00:00 2001 From: MauroFab Date: Wed, 13 Sep 2023 18:24:36 -0300 Subject: [PATCH 03/14] Initial version --- pkg/builtins/signature.go | 5 +- pkg/hints/hint_processor.go | 2 +- pkg/hints/math_hints.go | 33 ++++++++++-- pkg/hints/math_hints_test.go | 97 ++++++++++++++++++++++++++++++++++++ pkg/vm/memory/memory_test.go | 2 +- 5 files changed, 129 insertions(+), 10 deletions(-) diff --git a/pkg/builtins/signature.go b/pkg/builtins/signature.go index 036ec3a5..413af9b1 100644 --- a/pkg/builtins/signature.go +++ b/pkg/builtins/signature.go @@ -95,10 +95,9 @@ func (r *SignatureBuiltinRunner) AddValidationRule(mem *memory.Memory) { } // Helper function to AddSignature -func AddSignature( - signatureBuiltin *SignatureBuiltinRunner, +func (r *SignatureBuiltinRunner) AddSignature( address memory.Relocatable, signature Signature, ) { - signatureBuiltin.signatures[address] = signature + r.signatures[address] = signature } diff --git a/pkg/hints/hint_processor.go b/pkg/hints/hint_processor.go index 54998d95..ed580f83 100644 --- a/pkg/hints/hint_processor.go +++ b/pkg/hints/hint_processor.go @@ -43,7 +43,7 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any, case ASSERT_NN: return assert_nn(data.Ids, vm) case VERIFY_ECDSA_SIGNATURE: - return verify_edsa_signature(data.Ids, vm) + return verify_ecdsa_signature(data.Ids, vm) case IS_POSITIVE: return is_positive(data.Ids, vm) case ASSERT_NOT_ZERO: diff --git a/pkg/hints/math_hints.go b/pkg/hints/math_hints.go index 78354ba5..f0af3deb 100644 --- a/pkg/hints/math_hints.go +++ b/pkg/hints/math_hints.go @@ -65,10 +65,33 @@ func assert_not_zero(ids IdsManager, vm *VirtualMachine) error { return nil } -func verify_edsa_signature(ids IdsManager, vm *VirtualMachine) error { - r, err := ids.GetFelt("signature_r", vm) - s, err := ids.GetFelt("signature_s", vm) - ecdsa_ptr, err := ids.GetRelocatable("ecdsa_ptr", vm) +func verify_ecdsa_signature(ids IdsManager, vm *VirtualMachine) error { + r, err_get_r := ids.GetFelt("signature_r", vm) + if err_get_r != nil { + return err_get_r + } + + s, err_get_s := ids.GetFelt("signature_s", vm) + if err_get_s != nil { + return err_get_s + } + + ecdsa_ptr, err_get_ecdsa := ids.GetRelocatable("ecdsa_ptr", vm) + if err_get_ecdsa != nil { + return err_get_ecdsa + } + + signature_builtin_interface, err_get_builtin := vm.GetBuiltinRunner("signature") + if err_get_builtin != nil { + return err_get_builtin + } + + signature_builtin := (*signature_builtin_interface).(*builtins.SignatureBuiltinRunner) + + signature := builtins.Signature{ + R: r, + S: s, + } + signature_builtin.AddSignature(ecdsa_ptr, signature) return nil - // vm.get_signature_builtin? } diff --git a/pkg/hints/math_hints_test.go b/pkg/hints/math_hints_test.go index d4008e29..464aaa2a 100644 --- a/pkg/hints/math_hints_test.go +++ b/pkg/hints/math_hints_test.go @@ -3,10 +3,13 @@ package hints_test import ( "testing" + "github.com/lambdaclass/cairo-vm.go/pkg/builtins" . "github.com/lambdaclass/cairo-vm.go/pkg/hints" . "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_utils" + "github.com/lambdaclass/cairo-vm.go/pkg/lambdaworks" . "github.com/lambdaclass/cairo-vm.go/pkg/lambdaworks" . "github.com/lambdaclass/cairo-vm.go/pkg/vm" + "github.com/lambdaclass/cairo-vm.go/pkg/vm/memory" . "github.com/lambdaclass/cairo-vm.go/pkg/vm/memory" ) @@ -142,6 +145,26 @@ func TestAssertNotZeroHintOk(t *testing.T) { } } +/* + vm.segments = segments![ + ((1, 0), (0, 0)), + ( + (1, 1), + ( + "3086480810278599376317923499561306189851900463386393948998357832163236918254", + 10 + ) + ), + ( + (1, 2), + ( + "598673427589502599949712887611119751108407514580626464031881322743364689811", + 10 + ) + ) + ]; +*/ + func TestAssertNotZeroHintFail(t *testing.T) { vm := NewVirtualMachine() vm.Segments.AddSegment() @@ -161,3 +184,77 @@ func TestAssertNotZeroHintFail(t *testing.T) { t.Errorf("ASSERT_NOT_ZERO hint should have failed") } } + +func TestVerifyValidSignature(t *testing.T) { + vm := NewVirtualMachine() + signature_builtin := builtins.NewSignatureBuiltinRunner() + vm.BuiltinRunners = append(vm.BuiltinRunners, signature_builtin) + + hintProcessor := CairoVmHintProcessor{} + vm.Segments.AddSegment() + + r_felt := lambdaworks.FeltFromDecString("3086480810278599376317923499561306189851900463386393948998357832163236918254") + s_felt := lambdaworks.FeltFromDecString("598673427589502599949712887611119751108407514580626464031881322743364689811") + zero := memory.NewMaybeRelocatableFelt(lambdaworks.FeltZero()) + r := memory.NewMaybeRelocatableFelt(r_felt) + s := memory.NewMaybeRelocatableFelt(s_felt) + + vm.RunContext.Fp = memory.NewRelocatable(1, 3) + + idsManager := SetupIdsForTest( + map[string][]*MaybeRelocatable{ + "ecdsa_ptr": {zero}, + "signature_r": {r}, + "signature_s": {s}, + }, + vm, + ) + + hintData := any(HintData{ + Ids: idsManager, + Code: VERIFY_ECDSA_SIGNATURE, + }) + + err := hintProcessor.ExecuteHint(vm, &hintData, nil) + + if err != nil { + t.Errorf("Verify signature hint for correct signature failed") + } +} + +func VerifySignatureInvalidEcdsaPointer(t *testing.T) { + vm := NewVirtualMachine() + signature_builtin := builtins.NewSignatureBuiltinRunner() + vm.BuiltinRunners = append(vm.BuiltinRunners, signature_builtin) + + hintProcessor := CairoVmHintProcessor{} + vm.Segments.AddSegment() + + r_felt := lambdaworks.FeltFromDecString("3086480810278599376317923499561306189851900463386393948998357832163236918254") + s_felt := lambdaworks.FeltFromDecString("598673427589502599949712887611119751108407514580626464031881322743364689811") + three := memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint64(3)) + r := memory.NewMaybeRelocatableFelt(r_felt) + s := memory.NewMaybeRelocatableFelt(s_felt) + + vm.RunContext.Fp = memory.NewRelocatable(1, 3) + + idsManager := SetupIdsForTest( + map[string][]*MaybeRelocatable{ + "ecdsa_ptr": {three}, + "signature_r": {r}, + "signature_s": {s}, + }, + vm, + ) + + hintData := any(HintData{ + Ids: idsManager, + Code: VERIFY_ECDSA_SIGNATURE, + }) + + err := hintProcessor.ExecuteHint(vm, &hintData, nil) + + if err == nil { + t.Errorf("Verified a signature with an invalid pointer") + } +} diff --git a/pkg/vm/memory/memory_test.go b/pkg/vm/memory/memory_test.go index 7c903b16..4b1a05a3 100644 --- a/pkg/vm/memory/memory_test.go +++ b/pkg/vm/memory/memory_test.go @@ -437,7 +437,7 @@ func TestValidateMemoryForValidSignature(t *testing.T) { S: signature_s_felt, } - builtins.AddSignature(signature_builtin, signature_address, signature) + signature_builtin.AddSignature(signature_address, signature) pub_key_address := memory.NewRelocatable(1, 0) message_hash_address := memory.NewRelocatable(1, 1) From 343852bd2d19ce700941041a2efd1cc286501d87 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Wed, 13 Sep 2023 18:25:49 -0300 Subject: [PATCH 04/14] Remove unused comments --- pkg/hints/math_hints_test.go | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/pkg/hints/math_hints_test.go b/pkg/hints/math_hints_test.go index 464aaa2a..4741547f 100644 --- a/pkg/hints/math_hints_test.go +++ b/pkg/hints/math_hints_test.go @@ -145,26 +145,6 @@ func TestAssertNotZeroHintOk(t *testing.T) { } } -/* - vm.segments = segments![ - ((1, 0), (0, 0)), - ( - (1, 1), - ( - "3086480810278599376317923499561306189851900463386393948998357832163236918254", - 10 - ) - ), - ( - (1, 2), - ( - "598673427589502599949712887611119751108407514580626464031881322743364689811", - 10 - ) - ) - ]; -*/ - func TestAssertNotZeroHintFail(t *testing.T) { vm := NewVirtualMachine() vm.Segments.AddSegment() From 814f6cd8886e1f8aaa66749c4764299553ee0409 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Wed, 13 Sep 2023 18:46:04 -0300 Subject: [PATCH 05/14] Add signature program --- cairo_programs/signature.cairo | 24 ++++++++++++++++++++++++ pkg/vm/cairo_run/cairo_run_test.go | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 cairo_programs/signature.cairo diff --git a/cairo_programs/signature.cairo b/cairo_programs/signature.cairo new file mode 100644 index 00000000..b6f4a0d4 --- /dev/null +++ b/cairo_programs/signature.cairo @@ -0,0 +1,24 @@ +%builtins range_check + +from starkware.cairo.common.cairo_secp.signature import div_mod_n, get_point_from_x +from starkware.cairo.common.cairo_secp.bigint import BigInt3 + +func main{range_check_ptr: felt}() { + let a: BigInt3 = BigInt3(100, 99, 98); + let b: BigInt3 = BigInt3(10, 9, 8); + let (res) = div_mod_n(a, b); + assert res.d0 = 3413472211745629263979533; + assert res.d1 = 17305268010345238170172332; + assert res.d2 = 11991751872105858217578135; + + let x: BigInt3 = BigInt3(100, 99, 98); + let v: felt = 10; + let (point) = get_point_from_x(x, v); + assert point.x.d0 = 100; + assert point.x.d1 = 99; + assert point.x.d2 = 98; + assert point.y.d0 = 50471654703173585387369794; + assert point.y.d1 = 68898944762041070370364387; + assert point.y.d2 = 16932612780945290933872774; + return (); +} diff --git a/pkg/vm/cairo_run/cairo_run_test.go b/pkg/vm/cairo_run/cairo_run_test.go index 6a9942ba..d700253d 100644 --- a/pkg/vm/cairo_run/cairo_run_test.go +++ b/pkg/vm/cairo_run/cairo_run_test.go @@ -100,8 +100,8 @@ func TestAbsValue(t *testing.T) { t.Errorf("Program execution failed with error: %s", err) } } -func TestAssertNotZeroHint(t *testing.T) { - _, err := cairo_run.CairoRun("../../../cairo_programs/assert_not_zero.json", "small", false) +func TestSignature(t *testing.T) { + _, err := cairo_run.CairoRun("../../../cairo_programs/signature.json", "small", false) if err != nil { t.Errorf("Program execution failed with error: %s", err) } From f3d9e2bde8223d93f76a0830fe7c1ccaf4522568 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Wed, 13 Sep 2023 19:07:23 -0300 Subject: [PATCH 06/14] Fix parser --- pkg/hints/hint_processor.go | 1 + pkg/parser/parser.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/hints/hint_processor.go b/pkg/hints/hint_processor.go index ed580f83..bf6e173e 100644 --- a/pkg/hints/hint_processor.go +++ b/pkg/hints/hint_processor.go @@ -34,6 +34,7 @@ func (p *CairoVmHintProcessor) CompileHint(hintParams *parser.HintParams, refere func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any, constants *map[string]Felt) error { data, ok := (*hintData).(HintData) + println(data.Code) if !ok { return errors.New("Wrong Hint Data") } diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index 0b59ea67..55781f70 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -73,7 +73,7 @@ type HintParams struct { } type CompiledJson struct { - Attributes []string `json:"attributes"` + Attributes interface{} `json:"attributes"` Builtins []string `json:"builtins"` CompilerVersion string `json:"compiler_version"` Data []string `json:"data"` From 8b19ef9a7ebec204025934f1aa00da3ca58a6200 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Wed, 13 Sep 2023 19:16:01 -0300 Subject: [PATCH 07/14] Merge --- pkg/hints/hint_processor.go | 1 - pkg/hints/math_hints.go | 2 +- pkg/hints/math_hints_test.go | 4 ++-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/hints/hint_processor.go b/pkg/hints/hint_processor.go index 01671a5e..4d6b28d6 100644 --- a/pkg/hints/hint_processor.go +++ b/pkg/hints/hint_processor.go @@ -35,7 +35,6 @@ func (p *CairoVmHintProcessor) CompileHint(hintParams *parser.HintParams, refere func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any, constants *map[string]Felt, execScopes *types.ExecutionScopes) error { data, ok := (*hintData).(HintData) - println(data.Code) if !ok { return errors.New("Wrong Hint Data") } diff --git a/pkg/hints/math_hints.go b/pkg/hints/math_hints.go index f0af3deb..b54126b7 100644 --- a/pkg/hints/math_hints.go +++ b/pkg/hints/math_hints.go @@ -76,7 +76,7 @@ func verify_ecdsa_signature(ids IdsManager, vm *VirtualMachine) error { return err_get_s } - ecdsa_ptr, err_get_ecdsa := ids.GetRelocatable("ecdsa_ptr", vm) + ecdsa_ptr, err_get_ecdsa := ids.GetAddr("ecdsa_ptr", vm) if err_get_ecdsa != nil { return err_get_ecdsa } diff --git a/pkg/hints/math_hints_test.go b/pkg/hints/math_hints_test.go index 2ed74486..81ec0f4c 100644 --- a/pkg/hints/math_hints_test.go +++ b/pkg/hints/math_hints_test.go @@ -195,7 +195,7 @@ func TestVerifyValidSignature(t *testing.T) { Code: VERIFY_ECDSA_SIGNATURE, }) - err := hintProcessor.ExecuteHint(vm, &hintData, nil) + err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil) if err != nil { t.Errorf("Verify signature hint for correct signature failed") @@ -232,7 +232,7 @@ func VerifySignatureInvalidEcdsaPointer(t *testing.T) { Code: VERIFY_ECDSA_SIGNATURE, }) - err := hintProcessor.ExecuteHint(vm, &hintData, nil) + err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil) if err == nil { t.Errorf("Verified a signature with an invalid pointer") From d62c05a9c3b8561b2430b9d1bb32f531b833fd0a Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 18 Sep 2023 13:09:05 -0300 Subject: [PATCH 08/14] Replace test file --- cairo_programs/common_signature.cairo | 14 ++++++++++++++ cairo_programs/signature.cairo | 24 ------------------------ pkg/hints/math_hint_codes.go | 1 + pkg/vm/cairo_run/cairo_run_test.go | 4 ++-- 4 files changed, 17 insertions(+), 26 deletions(-) create mode 100644 cairo_programs/common_signature.cairo delete mode 100644 cairo_programs/signature.cairo diff --git a/cairo_programs/common_signature.cairo b/cairo_programs/common_signature.cairo new file mode 100644 index 00000000..46cc2fac --- /dev/null +++ b/cairo_programs/common_signature.cairo @@ -0,0 +1,14 @@ +%builtins ecdsa +from starkware.cairo.common.serialize import serialize_word +from starkware.cairo.common.cairo_builtins import SignatureBuiltin +from starkware.cairo.common.signature import verify_ecdsa_signature + +func main{ecdsa_ptr: SignatureBuiltin*}() { + verify_ecdsa_signature( + 2718, + 1735102664668487605176656616876767369909409133946409161569774794110049207117, + 3086480810278599376317923499561306189851900463386393948998357832163236918254, + 598673427589502599949712887611119751108407514580626464031881322743364689811, + ); + return (); +} diff --git a/cairo_programs/signature.cairo b/cairo_programs/signature.cairo deleted file mode 100644 index b6f4a0d4..00000000 --- a/cairo_programs/signature.cairo +++ /dev/null @@ -1,24 +0,0 @@ -%builtins range_check - -from starkware.cairo.common.cairo_secp.signature import div_mod_n, get_point_from_x -from starkware.cairo.common.cairo_secp.bigint import BigInt3 - -func main{range_check_ptr: felt}() { - let a: BigInt3 = BigInt3(100, 99, 98); - let b: BigInt3 = BigInt3(10, 9, 8); - let (res) = div_mod_n(a, b); - assert res.d0 = 3413472211745629263979533; - assert res.d1 = 17305268010345238170172332; - assert res.d2 = 11991751872105858217578135; - - let x: BigInt3 = BigInt3(100, 99, 98); - let v: felt = 10; - let (point) = get_point_from_x(x, v); - assert point.x.d0 = 100; - assert point.x.d1 = 99; - assert point.x.d2 = 98; - assert point.y.d0 = 50471654703173585387369794; - assert point.y.d1 = 68898944762041070370364387; - assert point.y.d2 = 16932612780945290933872774; - return (); -} diff --git a/pkg/hints/math_hint_codes.go b/pkg/hints/math_hint_codes.go index 7000a183..e52ffaa6 100644 --- a/pkg/hints/math_hint_codes.go +++ b/pkg/hints/math_hint_codes.go @@ -3,6 +3,7 @@ package hints 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.'" const VERIFY_ECDSA_SIGNATURE = "ecdsa_builtin.add_signature(ids.ecdsa_ptr.address_, (ids.signature_r, ids.signature_s))" + const IS_POSITIVE = "from starkware.cairo.common.math_utils import is_positive\nids.is_positive = 1 if is_positive(\n value=ids.value, prime=PRIME, rc_bound=range_check_builtin.bound) else 0" const ASSERT_NOT_ZERO = "from starkware.cairo.common.math_utils import assert_integer\nassert_integer(ids.value)\nassert ids.value % PRIME != 0, f'assert_not_zero failed: {ids.value} = 0.'" diff --git a/pkg/vm/cairo_run/cairo_run_test.go b/pkg/vm/cairo_run/cairo_run_test.go index 25756bfa..f010df66 100644 --- a/pkg/vm/cairo_run/cairo_run_test.go +++ b/pkg/vm/cairo_run/cairo_run_test.go @@ -123,9 +123,9 @@ func TestAbsValue(t *testing.T) { t.Errorf("Program execution failed with error: %s", err) } } -func TestSignature(t *testing.T) { +func TestCommonSignature(t *testing.T) { cairoRunConfig := cairo_run.CairoRunConfig{DisableTracePadding: false, ProofMode: false, Layout: "all_cairo"} - _, err := cairo_run.CairoRun("../../../cairo_programs/signature.json", cairoRunConfig) + _, err := cairo_run.CairoRun("../../../cairo_programs/common_signature.json", cairoRunConfig) if err != nil { t.Errorf("Program execution failed with error: %s", err) } From 14960568e32ce09119e4ac90326278ca1982c7a2 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 18 Sep 2023 13:11:52 -0300 Subject: [PATCH 09/14] Fix signature builtin name --- pkg/builtins/signature.go | 2 +- pkg/hints/math_hints.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/builtins/signature.go b/pkg/builtins/signature.go index 5d758061..5e577b61 100644 --- a/pkg/builtins/signature.go +++ b/pkg/builtins/signature.go @@ -8,7 +8,7 @@ import ( "github.com/pkg/errors" ) -const SIGNATURE_BUILTIN_NAME = "signature" +const SIGNATURE_BUILTIN_NAME = "ecdsa" // Notice changing this to any other number breaks the code const SIGNATURE_CELLS_PER_INSTANCE = 2 diff --git a/pkg/hints/math_hints.go b/pkg/hints/math_hints.go index 567eae87..ccf9b31c 100644 --- a/pkg/hints/math_hints.go +++ b/pkg/hints/math_hints.go @@ -82,7 +82,7 @@ func verify_ecdsa_signature(ids IdsManager, vm *VirtualMachine) error { return err_get_ecdsa } - signature_builtin_interface, err_get_builtin := vm.GetBuiltinRunner("signature") + signature_builtin_interface, err_get_builtin := vm.GetBuiltinRunner(builtins.SIGNATURE_BUILTIN_NAME) if err_get_builtin != nil { return err_get_builtin } From 92cfa3c56db868c428d4279c92e7b9f366e8a0ca Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 18 Sep 2023 13:13:38 -0300 Subject: [PATCH 10/14] Remove value from writeable address --- pkg/hints/math_hints_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/hints/math_hints_test.go b/pkg/hints/math_hints_test.go index 2d4e219d..6c8bab7f 100644 --- a/pkg/hints/math_hints_test.go +++ b/pkg/hints/math_hints_test.go @@ -175,7 +175,6 @@ func TestVerifyValidSignature(t *testing.T) { r_felt := lambdaworks.FeltFromDecString("3086480810278599376317923499561306189851900463386393948998357832163236918254") s_felt := lambdaworks.FeltFromDecString("598673427589502599949712887611119751108407514580626464031881322743364689811") - zero := memory.NewMaybeRelocatableFelt(lambdaworks.FeltZero()) r := memory.NewMaybeRelocatableFelt(r_felt) s := memory.NewMaybeRelocatableFelt(s_felt) @@ -183,7 +182,7 @@ func TestVerifyValidSignature(t *testing.T) { idsManager := SetupIdsForTest( map[string][]*MaybeRelocatable{ - "ecdsa_ptr": {zero}, + "ecdsa_ptr": {nil}, "signature_r": {r}, "signature_s": {s}, }, From bae2c528d31f280559a865fb9eb2a935883da066 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 18 Sep 2023 13:14:51 -0300 Subject: [PATCH 11/14] Add segment --- pkg/hints/math_hints_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/hints/math_hints_test.go b/pkg/hints/math_hints_test.go index 6c8bab7f..24cc60d5 100644 --- a/pkg/hints/math_hints_test.go +++ b/pkg/hints/math_hints_test.go @@ -167,6 +167,7 @@ func TestAssertNotZeroHintFail(t *testing.T) { func TestVerifyValidSignature(t *testing.T) { vm := NewVirtualMachine() + vm.Segments.AddSegment() signature_builtin := builtins.NewSignatureBuiltinRunner(2048) vm.BuiltinRunners = append(vm.BuiltinRunners, signature_builtin) @@ -197,7 +198,7 @@ func TestVerifyValidSignature(t *testing.T) { err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil) if err != nil { - t.Errorf("Verify signature hint for correct signature failed") + t.Errorf("Verify signature hint for correct signature failed with error: %s", err) } } From 5656e584645cd1f49ff4715a2bab14f5a8d42060 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 18 Sep 2023 13:16:01 -0300 Subject: [PATCH 12/14] Remove unused import --- cairo_programs/common_signature.cairo | 1 - 1 file changed, 1 deletion(-) diff --git a/cairo_programs/common_signature.cairo b/cairo_programs/common_signature.cairo index 46cc2fac..f9243948 100644 --- a/cairo_programs/common_signature.cairo +++ b/cairo_programs/common_signature.cairo @@ -1,5 +1,4 @@ %builtins ecdsa -from starkware.cairo.common.serialize import serialize_word from starkware.cairo.common.cairo_builtins import SignatureBuiltin from starkware.cairo.common.signature import verify_ecdsa_signature From 8572acc6864c6f192b1ffa939edeffe46048a82a Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 18 Sep 2023 14:33:25 -0300 Subject: [PATCH 13/14] Fix test name --- pkg/hints/math_hints_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/hints/math_hints_test.go b/pkg/hints/math_hints_test.go index 24cc60d5..0bb9bc28 100644 --- a/pkg/hints/math_hints_test.go +++ b/pkg/hints/math_hints_test.go @@ -202,7 +202,7 @@ func TestVerifyValidSignature(t *testing.T) { } } -func VerifySignatureInvalidEcdsaPointer(t *testing.T) { +func TestVerifySignatureInvalidEcdsaPointer(t *testing.T) { vm := NewVirtualMachine() signature_builtin := builtins.NewSignatureBuiltinRunner(2048) vm.BuiltinRunners = append(vm.BuiltinRunners, signature_builtin) From e4a2d14b94587a486b6af769449d92a0a077e266 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 19 Sep 2023 12:39:34 -0300 Subject: [PATCH 14/14] fix fmt --- pkg/hints/math_hints.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/hints/math_hints.go b/pkg/hints/math_hints.go index d0331426..acf46369 100644 --- a/pkg/hints/math_hints.go +++ b/pkg/hints/math_hints.go @@ -95,7 +95,7 @@ func verify_ecdsa_signature(ids IdsManager, vm *VirtualMachine) error { S: s, } signature_builtin.AddSignature(ecdsa_ptr, signature) - return nil + return nil } // Implements hint:from starkware.cairo.common.math.cairo