Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
ecdsa_circuit.hpp
Go to the documentation of this file.
1
2#pragma once
14
15namespace bb {
17 public:
24
25 static constexpr size_t NUM_PUBLIC_INPUTS = 6;
26
27 static Builder generate(uint256_t public_inputs[])
28 {
30
31 // IN CIRCUIT
32 // Create an input buffer the same size as our inputs
33 typename curve::byte_array_ct input_buffer(&builder, NUM_PUBLIC_INPUTS);
34 for (size_t i = 0; i < NUM_PUBLIC_INPUTS; ++i) {
35 input_buffer[i] = public_witness_ct(&builder, public_inputs[i]);
36 }
37
38 // This is the message that we would like to confirm
39 std::string message_string(NUM_PUBLIC_INPUTS, '\0');
40 for (size_t i = 0; i < NUM_PUBLIC_INPUTS; ++i) {
41 message_string[i] = static_cast<char>(static_cast<uint8_t>(public_inputs[i]));
42 }
43 auto message = typename curve::byte_array_ct(&builder, message_string);
44
45 // Assert that the public inputs buffer matches the message we want
46 for (size_t i = 0; i < NUM_PUBLIC_INPUTS; ++i) {
47 input_buffer[i].assert_equal(message[i]);
48 }
49
50 // UNCONSTRAINED: create a random keypair to sign with
53 account.public_key = curve::g1::one * account.private_key;
54
55 // UNCONSTRAINED: create a sig
56 crypto::ecdsa_signature signature = crypto::
57 ecdsa_construct_signature<crypto::Sha256Hasher, typename curve::fq, typename curve::fr, typename curve::g1>(
58 message_string, account);
59
60 // UNCONSTRAINED: verify the created signature
61 bool dry_run = crypto::
62 ecdsa_verify_signature<crypto::Sha256Hasher, typename curve::fq, typename curve::fr, typename curve::g1>(
63 message_string, account.public_key, signature);
64 if (!dry_run) {
65 throw_or_abort("[non circuit]: Sig verification failed");
66 }
67
68 // IN CIRCUIT: create a witness with the pub key in our circuit
69 typename curve::g1_bigfr_ct public_key = curve::g1_bigfr_ct::from_witness(&builder, account.public_key);
70
71 std::vector<uint8_t> rr(signature.r.begin(), signature.r.end());
72 std::vector<uint8_t> ss(signature.s.begin(), signature.s.end());
73
74 // IN CIRCUIT: create a witness with the sig in our circuit
76 typename curve::byte_array_ct(&builder, ss) };
77
78 stdlib::byte_array<Builder> hashed_message =
80
81 // IN CIRCUIT: verify the signature
82 typename curve::bool_ct signature_result = stdlib::ecdsa_verify_signature<Builder,
83 curve,
84 typename curve::fq_ct,
85 typename curve::bigfr_ct,
86 typename curve::g1_bigfr_ct>(
87 // hashed_message, public_key, sig);
88 hashed_message,
89 public_key,
90 sig);
91
92 // Assert the signature is true
93 signature_result.assert_equal(bool_ct(true));
94
95 return builder;
96 }
97};
98
99} // namespace bb
static constexpr size_t NUM_PUBLIC_INPUTS
stdlib::bool_t< Builder > bool_ct
bb::UltraCircuitBuilder Builder
stdlib::public_witness_t< Builder > public_witness_ct
stdlib::secp256k1< Builder > curve
static Builder generate(uint256_t public_inputs[])
static constexpr element one
Definition group.hpp:46
static byte_array< Builder > hash(const byte_array_ct &input)
Definition sha256.cpp:308
Implements boolean logic in-circuit.
Definition bool.hpp:59
Represents a dynamic array of bytes in-circuit.
AluTraceBuilder builder
Definition alu.test.cpp:123
bool_t< Builder > ecdsa_verify_signature(const stdlib::byte_array< Builder > &hashed_message, const G1 &public_key, const ecdsa_signature< Builder > &sig)
Verify ECDSA signature. Returns bool_t(true/false) depending on whether the signature is valid or not...
Entry point for Barretenberg command-line interface.
UltraCircuitBuilder_< UltraExecutionTraceBlocks > UltraCircuitBuilder
G1::affine_element public_key
Definition ecdsa.hpp:20
std::array< uint8_t, 32 > r
Definition ecdsa.hpp:26
std::array< uint8_t, 32 > s
Definition ecdsa.hpp:27
static field random_element(numeric::RNG *engine=nullptr) noexcept
byte_array< Builder > byte_array_ct
Definition secp256k1.hpp:28
bigfield< Builder, typename ::bb::secp256k1::FqParams > fq_ct
Definition secp256k1.hpp:31
element< Builder, fq_ct, bigfr_ct, g1 > g1_bigfr_ct
Definition secp256k1.hpp:34
bigfield< Builder, typename ::bb::secp256k1::FrParams > bigfr_ct
Definition secp256k1.hpp:32
void throw_or_abort(std::string const &err)