From 778cee6bfdfcc55bd982413b93b7488ced00e758 Mon Sep 17 00:00:00 2001 From: vezenovm Date: Mon, 6 Mar 2023 15:14:46 -0700 Subject: [PATCH 1/7] upstream changes to fix proving key serialization in turbo_proofs --- .vscode/settings.json | 3 ++- .../aztec/dsl/turbo_proofs/turbo_proofs.cpp | 20 ++++++------------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 84927c8002..131a2ad190 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,7 @@ "*.tf": "terraform", "*.tfvars": "terraform", "Makefile.*": "makefile", - "iosfwd": "cpp" + "iosfwd": "cpp", + "vector": "cpp" } } diff --git a/cpp/src/aztec/dsl/turbo_proofs/turbo_proofs.cpp b/cpp/src/aztec/dsl/turbo_proofs/turbo_proofs.cpp index 48e3f58549..23f54a048d 100644 --- a/cpp/src/aztec/dsl/turbo_proofs/turbo_proofs.cpp +++ b/cpp/src/aztec/dsl/turbo_proofs/turbo_proofs.cpp @@ -27,21 +27,13 @@ size_t turbo_init_proving_key(uint8_t const* constraint_system_buf, uint8_t cons auto crs_factory = std::make_unique(); auto composer = create_circuit(constraint_system, std::move(crs_factory)); auto proving_key = composer.compute_proving_key(); - - // Computing the size of the serialized key is non trivial. We know it's ~331mb. - // Allocate a buffer large enough to hold it, and abort if we overflow. - // This is to keep memory usage down. - size_t total_buf_len = 350 * 1024 * 1024; - auto raw_buf = (uint8_t*)malloc(total_buf_len); - auto raw_buf_end = raw_buf; - write(raw_buf_end, *proving_key); + + auto buffer = to_buffer(*proving_key); + auto raw_buf = (uint8_t*)malloc(buffer.size()); + memcpy(raw_buf, (void*)buffer.data(), buffer.size()); *pk_buf = raw_buf; - auto len = static_cast(raw_buf_end - raw_buf); - if (len > total_buf_len) { - info("Buffer overflow serializing proving key."); - std::abort(); - } - return len; + + return buffer.size(); } size_t turbo_init_verification_key(void* pippenger, uint8_t const* g2x, uint8_t const* pk_buf, uint8_t const** vk_buf) From 8b4a304d3be7d52da3c0df6832687344c8785b70 Mon Sep 17 00:00:00 2001 From: vezenovm Date: Mon, 6 Mar 2023 15:18:43 -0700 Subject: [PATCH 2/7] go back to old .vscode settings --- .vscode/settings.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 131a2ad190..84927c8002 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,7 +3,6 @@ "*.tf": "terraform", "*.tfvars": "terraform", "Makefile.*": "makefile", - "iosfwd": "cpp", - "vector": "cpp" + "iosfwd": "cpp" } } From 231fcf93c05a58f5ae6ebaefc2e48351a94f203e Mon Sep 17 00:00:00 2001 From: vezenovm Date: Mon, 6 Mar 2023 15:47:01 -0700 Subject: [PATCH 3/7] add signature_result and constrain it correctly --- cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp b/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp index 38a8cb3359..1c15fb2b1d 100644 --- a/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp +++ b/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp @@ -105,6 +105,13 @@ void create_ecdsa_verify_constraints(plonk::TurboComposer& composer, const Ecdsa auto pub_key = secp256k1_ct::g1_ct(pub_key_x_fq, pub_key_y_fq); + //TODO: crypto-dev to verify calculation of the signature result is correct + bool_ct signature_result = stdlib::ecdsa:: + verify_signature( + message, pub_key, sig); + + auto result_bool = composer.add_variable(signature_result.get_value() == true); + composer.assert_equal(false, input.result); } From 55e25b660f6180bf3a994258e0ae033de88af95d Mon Sep 17 00:00:00 2001 From: vezenovm Date: Mon, 6 Mar 2023 15:47:06 -0700 Subject: [PATCH 4/7] add signature_result and constrain it correctly --- cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp b/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp index 1c15fb2b1d..c7fe6c4e30 100644 --- a/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp +++ b/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp @@ -105,14 +105,14 @@ void create_ecdsa_verify_constraints(plonk::TurboComposer& composer, const Ecdsa auto pub_key = secp256k1_ct::g1_ct(pub_key_x_fq, pub_key_y_fq); - //TODO: crypto-dev to verify calculation of the signature result is correct + //TODO: crypto-dev to verify calculation and constraining of the signature result is correct bool_ct signature_result = stdlib::ecdsa:: verify_signature( message, pub_key, sig); auto result_bool = composer.add_variable(signature_result.get_value() == true); - composer.assert_equal(false, input.result); + composer.assert_equal(result_bool, input.result); } } // namespace acir_format From cf46d5fb19a9fd49c88871755dc9daa863524526 Mon Sep 17 00:00:00 2001 From: vezenovm Date: Mon, 6 Mar 2023 16:37:46 -0700 Subject: [PATCH 5/7] change generics used on ecdsa verify_sig acir_format --- cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp b/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp index c7fe6c4e30..5282145be5 100644 --- a/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp +++ b/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp @@ -105,9 +105,9 @@ void create_ecdsa_verify_constraints(plonk::TurboComposer& composer, const Ecdsa auto pub_key = secp256k1_ct::g1_ct(pub_key_x_fq, pub_key_y_fq); - //TODO: crypto-dev to verify calculation and constraining of the signature result is correct - bool_ct signature_result = stdlib::ecdsa:: - verify_signature( + // TODO: crypto-dev to verify calculation and constraining of the signature result is correct + bool_ct signature_result = + stdlib::ecdsa::verify_signature( message, pub_key, sig); auto result_bool = composer.add_variable(signature_result.get_value() == true); From 084a7bb9cdab6ebc880e72c836828886ed28b610 Mon Sep 17 00:00:00 2001 From: vezenovm Date: Mon, 6 Mar 2023 16:45:18 -0700 Subject: [PATCH 6/7] use bigfr_ct --- cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp b/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp index 5282145be5..229ba1fee7 100644 --- a/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp +++ b/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp @@ -106,8 +106,8 @@ void create_ecdsa_verify_constraints(plonk::TurboComposer& composer, const Ecdsa auto pub_key = secp256k1_ct::g1_ct(pub_key_x_fq, pub_key_y_fq); // TODO: crypto-dev to verify calculation and constraining of the signature result is correct - bool_ct signature_result = - stdlib::ecdsa::verify_signature( + bool_ct signature_result = stdlib::ecdsa:: + verify_signature( message, pub_key, sig); auto result_bool = composer.add_variable(signature_result.get_value() == true); From fe81244a823a885a676bbff1066f803b1c26c450 Mon Sep 17 00:00:00 2001 From: vezenovm Date: Mon, 6 Mar 2023 17:16:22 -0700 Subject: [PATCH 7/7] remove declaration of pub_key in ecdsa acir_format, TODO check with crypto team how to accurately ecdsa verify_sig with TurboComposer --- .../aztec/dsl/acir_format/ecdsa_secp256k1.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp b/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp index 229ba1fee7..a00edd767f 100644 --- a/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp +++ b/cpp/src/aztec/dsl/acir_format/ecdsa_secp256k1.cpp @@ -103,16 +103,20 @@ void create_ecdsa_verify_constraints(plonk::TurboComposer& composer, const Ecdsa stdlib::ecdsa::signature sig{ stdlib::byte_array(&composer, rr), stdlib::byte_array(&composer, ss) }; - auto pub_key = secp256k1_ct::g1_ct(pub_key_x_fq, pub_key_y_fq); + pub_key_x_fq.assert_is_in_field(); + pub_key_y_fq.assert_is_in_field(); - // TODO: crypto-dev to verify calculation and constraining of the signature result is correct - bool_ct signature_result = stdlib::ecdsa:: - verify_signature( - message, pub_key, sig); + // TODO: crypto-dev to fix calculation and constraining of the signature result is correct + // the above line is currently a placeholder as unused variabels are not allowed in the build + // auto pub_key = secp256k1_ct::g1_ct(pub_key_x_fq, pub_key_y_fq); + // bool_ct signature_result = stdlib::ecdsa:: + // verify_signature( + // message, pub_key, sig); - auto result_bool = composer.add_variable(signature_result.get_value() == true); + // auto result_bool = composer.add_variable(signature_result.get_value() == true); - composer.assert_equal(result_bool, input.result); + composer.assert_equal(false, input.result); } } // namespace acir_format