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
15
16namespace bb {
18 public:
25
26 static constexpr size_t NUM_PUBLIC_INPUTS = 6;
27
28 static Builder generate(uint256_t public_inputs[])
29 {
31
32 // IN CIRCUIT
33 // Create an input buffer from public inputs (treating each as a single byte)
34 typename curve::byte_array_ct input_buffer(&builder, std::vector<uint8_t>());
35 for (size_t i = 0; i < NUM_PUBLIC_INPUTS; ++i) {
36 field_ct byte_value = public_witness_ct(&builder, public_inputs[i]);
37 // Constrain to be a single byte and create byte_array
38 typename curve::byte_array_ct single_byte(byte_value, 1);
39 input_buffer.write(single_byte);
40 }
41
42 // This is the message that we would like to confirm
43 std::string message_string(NUM_PUBLIC_INPUTS, '\0');
44 for (size_t i = 0; i < NUM_PUBLIC_INPUTS; ++i) {
45 message_string[i] = static_cast<char>(static_cast<uint8_t>(public_inputs[i]));
46 }
47 auto message = typename curve::byte_array_ct(&builder, message_string);
48
49 // Assert that the public inputs buffer matches the message we want
50 for (size_t i = 0; i < NUM_PUBLIC_INPUTS; ++i) {
51 input_buffer[i].assert_equal(message[i]);
52 }
53
54 // UNCONSTRAINED: create a random keypair to sign with
57 account.public_key = curve::g1::one * account.private_key;
58
59 // UNCONSTRAINED: create a sig
60 crypto::ecdsa_signature signature = crypto::
61 ecdsa_construct_signature<crypto::Sha256Hasher, typename curve::fq, typename curve::fr, typename curve::g1>(
62 message_string, account);
63
64 // UNCONSTRAINED: verify the created signature
65 bool dry_run = crypto::
66 ecdsa_verify_signature<crypto::Sha256Hasher, typename curve::fq, typename curve::fr, typename curve::g1>(
67 message_string, account.public_key, signature);
68 if (!dry_run) {
69 throw_or_abort("[non circuit]: Sig verification failed");
70 }
71
72 // IN CIRCUIT: create a witness with the pub key in our circuit
73 typename curve::g1_bigfr_ct public_key = curve::g1_bigfr_ct::from_witness(&builder, account.public_key);
74
75 std::vector<uint8_t> rr(signature.r.begin(), signature.r.end());
76 std::vector<uint8_t> ss(signature.s.begin(), signature.s.end());
77
78 // IN CIRCUIT: create a witness with the sig in our circuit
80 typename curve::byte_array_ct(&builder, ss) };
81
82 // Compute H(m) natively and pass as witness (mirrors ACIR which takes pre-hashed message)
83 auto hash_arr = crypto::sha256(std::vector<uint8_t>(message_string.begin(), message_string.end()));
84 stdlib::byte_array<Builder> hashed_message(&builder, std::vector<uint8_t>(hash_arr.begin(), hash_arr.end()));
85
86 // IN CIRCUIT: verify the signature
87 typename curve::bool_ct signature_result = stdlib::ecdsa_verify_signature<Builder,
88 curve,
89 typename curve::fq_ct,
90 typename curve::bigfr_ct,
91 typename curve::g1_bigfr_ct>(
92 // hashed_message, public_key, sig);
93 hashed_message,
94 public_key,
95 sig);
96
97 // Assert the signature is true
98 signature_result.assert_equal(bool_ct(true));
99
100 return builder;
101 }
102};
103
104} // 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
Implements boolean logic in-circuit.
Definition bool.hpp:59
void assert_equal(const bool_t &rhs, std::string const &msg="bool_t::assert_equal") const
Implements copy constraint for bool_t elements.
Definition bool.cpp:421
Represents a dynamic array of bytes in-circuit.
byte_array & write(byte_array const &other)
Appends the contents of another byte_array (other) to the end of this one.
AluTraceBuilder builder
Definition alu.test.cpp:124
Sha256Hash sha256(const ByteContainer &input)
SHA-256 hash function (FIPS 180-4)
Definition sha256.cpp:150
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.
Definition api.hpp:5
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:42
bigfield< Builder, typename ::bb::secp256k1::FqParams > fq_ct
Definition secp256k1.hpp:45
element< Builder, fq_ct, bigfr_ct, g1 > g1_bigfr_ct
Definition secp256k1.hpp:48
bigfield< Builder, typename ::bb::secp256k1::FrParams > bigfr_ct
Definition secp256k1.hpp:46
void throw_or_abort(std::string const &err)