Skip to content

Commit

Permalink
Add ecdsa signature in types. (#193)
Browse files Browse the repository at this point in the history
Rebase fixes.

Fix. (#195)

Fix `to_nt`
  • Loading branch information
suyash67 committed Mar 2, 2023
1 parent 283c840 commit 74c66a7
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 7 deletions.
38 changes: 38 additions & 0 deletions cpp/src/aztec/crypto/ecdsa/ecdsa.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
#include "../hashers/hashers.hpp"
#include <array>
#include <string>
#include <ecc/curves/secp256k1/secp256k1.hpp>

namespace crypto {
namespace ecdsa {
Expand All @@ -21,6 +23,42 @@ template <typename Hash, typename Fq, typename Fr, typename G1>
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 <typename B> inline void read(B& it, signature& sig)
{
read(it, sig.r);
read(it, sig.s);
}

template <typename B> inline void write(B& buf, signature const& sig)
{
write(buf, sig.r);
write(buf, sig.s);
}

template <typename B> inline void read(B& it, key_pair<secp256k1::fr, secp256k1::g1>& keypair)
{
read(it, keypair.private_key);
read(it, keypair.public_key);
}

template <typename B> inline void write(B& buf, key_pair<secp256k1::fr, secp256k1::g1> const& keypair)
{
write(buf, keypair.private_key);
write(buf, keypair.public_key);
}

} // namespace ecdsa
} // namespace crypto

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ WASM_EXPORT void* join_split__new_prover(uint8_t const* join_split_buf, bool moc
{
auto tx = from_buffer<join_split_tx>(join_split_buf);
auto prover = new_join_split_prover(tx, mock);
auto heapProver = new plonk::TurboProver(std::move(prover));
auto heapProver = new plonk::stdlib::types::Prover(std::move(prover));
return heapProver;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void init_verification_key(std::shared_ptr<bonk::VerifierMemReferenceString> con
verification_key = std::make_shared<bonk::verification_key>(std::move(vk_data), crs);
}

plonk::TurboProver new_join_split_prover(join_split_tx const& tx, bool mock)
plonk::stdlib::types::Prover new_join_split_prover(join_split_tx const& tx, bool mock)
{
Composer composer(proving_key, nullptr);
join_split_circuit(composer, tx);
Expand All @@ -89,7 +89,8 @@ plonk::TurboProver new_join_split_prover(join_split_tx const& tx, bool mock)

bool verify_proof(plonk::proof const& proof)
{
plonk::TurboVerifier verifier(verification_key, Composer::create_manifest(verification_key->num_public_inputs));
plonk::stdlib::types::Verifier verifier(verification_key,
Composer::create_manifest(verification_key->num_public_inputs));

std::unique_ptr<plonk::KateCommitmentScheme<plonk::turbo_settings>> kate_commitment_scheme =
std::make_unique<plonk::KateCommitmentScheme<plonk::turbo_settings>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void init_verification_key(std::unique_ptr<bonk::ReferenceStringFactory>&& crs_f
void init_verification_key(std::shared_ptr<bonk::VerifierMemReferenceString> const& crs,
bonk::verification_key_data&& vk_data);

plonk::TurboProver new_join_split_prover(join_split_tx const& tx, bool mock);
plonk::stdlib::types::Prover new_join_split_prover(join_split_tx const& tx, bool mock);

bool verify_proof(plonk::proof const& proof);

Expand Down
11 changes: 11 additions & 0 deletions cpp/src/aztec/stdlib/encryption/ecdsa/ecdsa.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <crypto/ecdsa/ecdsa.hpp>
#include "../../primitives/byte_array/byte_array.hpp"
#include "../../primitives/composers/composers_fwd.hpp"

Expand All @@ -16,6 +17,16 @@ template <typename Composer, typename Curve, typename Fq, typename Fr, typename
bool_t<Composer> verify_signature(const stdlib::byte_array<Composer>& message,
const G1& public_key,
const signature<Composer>& sig);

template <typename Composer>
static signature<Composer> from_witness(Composer* ctx, const crypto::ecdsa::signature& input)
{
byte_array x(ctx, input.r);
byte_array y(ctx, input.s);
signature<Composer> out(x, y);
return out;
}

} // namespace ecdsa
} // namespace stdlib
} // namespace plonk
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/aztec/stdlib/hash/sha256/sha256.bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ void generate_test_plonk_circuit(Composer& composer, size_t num_bytes)
}

Composer composers[NUM_HASHES];
plonk::TurboProver provers[NUM_HASHES];
plonk::TurboVerifier verifiers[NUM_HASHES];
plonk::stdlib::types::Prover provers[NUM_HASHES];
plonk::stdlib::types::Verifier verifiers[NUM_HASHES];
plonk::proof proofs[NUM_HASHES];

void construct_witnesses_bench(State& state) noexcept
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/aztec/stdlib/types/circuit_types.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <stdlib/primitives/address/address.hpp>
#include <stdlib/encryption/schnorr/schnorr.hpp>
#include <stdlib/encryption/ecdsa/ecdsa.hpp>
#include <stdlib/primitives/bigfield/bigfield.hpp>
#include <stdlib/primitives/biggroup/biggroup.hpp>
#include <stdlib/primitives/bit_array/bit_array.hpp>
Expand Down Expand Up @@ -53,6 +54,7 @@ template <typename Composer> struct CircuitTypes {
// typedef packed_byte_array<Composer> packed_byte_array;

// typedef stdlib::schnorr::signature_bits<Composer> signature;
typedef stdlib::ecdsa::signature<Composer> ecdsa_signature;

typedef stdlib::recursion::recursion_output<bn254> AggregationObject;
typedef stdlib::recursion::verification_key<bn254> VK;
Expand Down
32 changes: 32 additions & 0 deletions cpp/src/aztec/stdlib/types/convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ typename CT<Composer>::bn254_point to_ct(Composer& composer, typename NT::bn254_
return CT<Composer>::bn254_point::from_witness(&composer, e);
};

template <typename Composer>
typename CT<Composer>::ecdsa_signature to_ct(Composer& composer, typename NT::ecdsa_signature const& e)
{
return CT<Composer>::ecdsa_signature::template from_witness<Composer>(&composer, e);
};

template <typename Composer>
std::optional<typename CT<Composer>::boolean> to_ct(Composer& composer, std::optional<typename NT::boolean> const& e)
{
Expand All @@ -81,6 +87,13 @@ std::optional<typename CT<Composer>::grumpkin_point> to_ct(Composer& composer,
return e ? std::make_optional<typename CT<Composer>::grumpkin_point>(to_ct(composer, *e)) : std::nullopt;
};

template <typename Composer>
std::optional<typename CT<Composer>::ecdsa_signature> to_ct(Composer& composer,
std::optional<typename NT::ecdsa_signature> const& e)
{
return e ? std::make_optional<typename CT<Composer>::ecdsa_signature>(to_ct(&composer, e)) : std::nullopt;
};

template <typename Composer>
std::vector<typename CT<Composer>::fr> to_ct(Composer& composer, std::vector<typename NT::fr> const& vec)
{
Expand Down Expand Up @@ -155,6 +168,19 @@ template <typename Composer> typename NT::bn254_point to_nt(typename CT<Composer
return e.get_value();
};

template <typename Composer> typename NT::ecdsa_signature to_nt(typename CT<Composer>::ecdsa_signature const& e)
{
std::vector<uint8_t> r_bytes = e.r.get_value();
std::vector<uint8_t> s_bytes = e.s.get_value();

std::array<uint8_t, 32> r_array;
std::array<uint8_t, 32> s_array;
std::copy(r_bytes.begin(), r_bytes.end(), r_array.begin());
std::copy(s_bytes.begin(), s_bytes.end(), s_array.begin());

return NT::ecdsa_signature{ r_array, s_array };
};

template <typename Composer>
std::optional<typename NT::boolean> to_nt(std::optional<typename CT<Composer>::boolean> const& e)
{
Expand All @@ -178,6 +204,12 @@ std::optional<typename NT::grumpkin_point> to_nt(std::optional<typename CT<Compo
return e ? std::make_optional<typename NT::grumpkin_point>(to_nt<Composer>(*e)) : std::nullopt;
};

template <typename Composer>
std::optional<typename NT::ecdsa_signature> to_nt(std::optional<typename CT<Composer>::ecdsa_signature> const& e)
{
return e ? std::make_optional<typename NT::ecdsa_signature>(to_nt<Composer>(*e)) : std::nullopt;
};

template <typename Composer> std::vector<typename NT::fr> to_nt(std::vector<typename CT<Composer>::fr> const& vec)
{
auto ref_to_nt = [&](typename CT<Composer>::fr const& e) { return to_nt<Composer>(e); };
Expand Down
4 changes: 3 additions & 1 deletion cpp/src/aztec/stdlib/types/native_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
#include <crypto/pedersen_commitment/pedersen.hpp>
#include <crypto/generators/generator_data.hpp>
#include <crypto/schnorr/schnorr.hpp>
#include <crypto/ecdsa/ecdsa.hpp>
#include <ecc/curves/bn254/fq.hpp>
#include <ecc/curves/bn254/fr.hpp>
#include <ecc/curves/bn254/g1.hpp>
#include <ecc/curves/grumpkin/grumpkin.hpp>
#include <numeric/uint256/uint256.hpp>
#include <proof_system/verification_key/verification_key.hpp>
#include <plonk/proof_system/types/plonk_proof.hpp>
#include <plonk/proof_system/types/proof.hpp>
#include <stdlib/recursion/verifier/verifier.hpp>

// #include <stdlib/primitives/bit_array/bit_array.hpp>
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 74c66a7

Please sign in to comment.