diff --git a/cpp/src/aztec/crypto/ecdsa/ecdsa.hpp b/cpp/src/aztec/crypto/ecdsa/ecdsa.hpp index fb9d7644fc..bce89a7243 100644 --- a/cpp/src/aztec/crypto/ecdsa/ecdsa.hpp +++ b/cpp/src/aztec/crypto/ecdsa/ecdsa.hpp @@ -1,6 +1,8 @@ +#pragma once #include "../hashers/hashers.hpp" #include #include +#include namespace crypto { namespace ecdsa { @@ -21,6 +23,42 @@ template bool verify_signature(const std::string& message, const typename G1::affine_element& public_key, const signature& signature); + +inline bool operator==(signature const& lhs, signature const& rhs) +{ + return lhs.r == rhs.r && lhs.s == rhs.s; +} + +inline std::ostream& operator<<(std::ostream& os, signature const& sig) +{ + os << "{ " << sig.r << ", " << sig.s << " }"; + return os; +} + +template inline void read(B& it, signature& sig) +{ + read(it, sig.r); + read(it, sig.s); +} + +template inline void write(B& buf, signature const& sig) +{ + write(buf, sig.r); + write(buf, sig.s); +} + +template inline void read(B& it, key_pair& keypair) +{ + read(it, keypair.private_key); + read(it, keypair.public_key); +} + +template inline void write(B& buf, key_pair const& keypair) +{ + write(buf, keypair.private_key); + write(buf, keypair.public_key); +} + } // namespace ecdsa } // namespace crypto diff --git a/cpp/src/aztec/stdlib/encryption/ecdsa/ecdsa.hpp b/cpp/src/aztec/stdlib/encryption/ecdsa/ecdsa.hpp index bdd67f46b4..56528c2bde 100644 --- a/cpp/src/aztec/stdlib/encryption/ecdsa/ecdsa.hpp +++ b/cpp/src/aztec/stdlib/encryption/ecdsa/ecdsa.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include "../../primitives/byte_array/byte_array.hpp" #include "../../primitives/composers/composers_fwd.hpp" @@ -16,6 +17,16 @@ template verify_signature(const stdlib::byte_array& message, const G1& public_key, const signature& sig); + +template +static signature from_witness(Composer* ctx, const crypto::ecdsa::signature& input) +{ + byte_array x(ctx, input.r); + byte_array y(ctx, input.s); + signature out(x, y); + return out; +} + } // namespace ecdsa } // namespace stdlib } // namespace plonk diff --git a/cpp/src/aztec/stdlib/types/circuit_types.hpp b/cpp/src/aztec/stdlib/types/circuit_types.hpp index ac224c0f35..68b2257f43 100644 --- a/cpp/src/aztec/stdlib/types/circuit_types.hpp +++ b/cpp/src/aztec/stdlib/types/circuit_types.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include #include #include @@ -53,6 +54,7 @@ template struct CircuitTypes { // typedef packed_byte_array packed_byte_array; // typedef stdlib::schnorr::signature_bits signature; + typedef stdlib::ecdsa::signature ecdsa_signature; typedef stdlib::recursion::recursion_output AggregationObject; typedef stdlib::recursion::verification_key VK; diff --git a/cpp/src/aztec/stdlib/types/convert.hpp b/cpp/src/aztec/stdlib/types/convert.hpp index 5a341f3873..ad03b05d75 100644 --- a/cpp/src/aztec/stdlib/types/convert.hpp +++ b/cpp/src/aztec/stdlib/types/convert.hpp @@ -56,6 +56,12 @@ typename CT::bn254_point to_ct(Composer& composer, typename NT::bn254_ return CT::bn254_point::from_witness(&composer, e); }; +template +typename CT::ecdsa_signature to_ct(Composer& composer, typename NT::ecdsa_signature const& e) +{ + return CT::ecdsa_signature::from_witness(&composer, e); +}; + template std::optional::boolean> to_ct(Composer& composer, std::optional const& e) { @@ -81,6 +87,13 @@ std::optional::grumpkin_point> to_ct(Composer& composer, return e ? std::make_optional::grumpkin_point>(to_ct(composer, *e)) : std::nullopt; }; +template +std::optional::ecdsa_signature> to_ct(Composer& composer, + std::optional const& e) +{ + return e ? std::make_optional::ecdsa_signature>(to_ct(&composer, e)) : std::nullopt; +}; + template std::vector::fr> to_ct(Composer& composer, std::vector const& vec) { @@ -155,6 +168,13 @@ template typename NT::bn254_point to_nt(typename CT typename NT::ecdsa_signature to_nt(typename CT::ecdsa_signature const& e) +{ + std::vector r_bytes = e.r.get_value(); + std::vector s_bytes = e.s.get_value(); + return NT::ecdsa_signature{ r_bytes, s_bytes }; +}; + template std::optional to_nt(std::optional::boolean> const& e) { @@ -178,6 +198,12 @@ std::optional to_nt(std::optional(to_nt(*e)) : std::nullopt; }; +template +std::optional to_nt(std::optional::ecdsa_signature> const& e) +{ + return e ? std::make_optional(to_nt(*e)) : std::nullopt; +}; + template std::vector to_nt(std::vector::fr> const& vec) { auto ref_to_nt = [&](typename CT::fr const& e) { return to_nt(e); }; diff --git a/cpp/src/aztec/stdlib/types/native_types.hpp b/cpp/src/aztec/stdlib/types/native_types.hpp index f45a0269d9..60691d105c 100644 --- a/cpp/src/aztec/stdlib/types/native_types.hpp +++ b/cpp/src/aztec/stdlib/types/native_types.hpp @@ -3,13 +3,14 @@ #include #include #include +#include #include #include #include #include #include #include -#include +#include #include // #include @@ -54,6 +55,7 @@ struct NativeTypes { // typedef packed_byte_array packed_byte_array; // typedef crypto::schnorr::signature signature; + typedef crypto::ecdsa::signature ecdsa_signature; typedef stdlib::recursion::native_recursion_output AggregationObject; typedef bonk::verification_key_data VKData;