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
16
17namespace bb {
19 public:
27
28 static constexpr size_t NUM_PUBLIC_INPUTS = 6;
29
30 static Builder generate(uint256_t public_inputs[])
31 {
33
34 // IN CIRCUIT
35 // Create an input buffer from public inputs (treating each as a single byte)
36 typename curve::byte_array_ct input_buffer(&builder, std::vector<uint8_t>());
37 for (size_t i = 0; i < NUM_PUBLIC_INPUTS; ++i) {
38 field_ct byte_value = public_witness_ct(&builder, public_inputs[i]);
39 // Constrain to be a single byte and create byte_array
40 typename curve::byte_array_ct single_byte(byte_value, 1);
41 input_buffer.write(single_byte);
42 }
43
44 // This is the message that we would like to confirm
45 std::string message_string(NUM_PUBLIC_INPUTS, '\0');
46 for (size_t i = 0; i < NUM_PUBLIC_INPUTS; ++i) {
47 message_string[i] = static_cast<char>(static_cast<uint8_t>(public_inputs[i]));
48 }
49 auto message = typename curve::byte_array_ct(&builder, message_string);
50
51 // Assert that the public inputs buffer matches the message we want
52 for (size_t i = 0; i < NUM_PUBLIC_INPUTS; ++i) {
53 input_buffer[i].assert_equal(message[i]);
54 }
55
56 // UNCONSTRAINED: create a random keypair to sign with
59 account.public_key = curve::g1::one * account.private_key;
60
61 // UNCONSTRAINED: create a sig
62 crypto::ecdsa_signature signature = crypto::
63 ecdsa_construct_signature<crypto::Sha256Hasher, typename curve::fq, typename curve::fr, typename curve::g1>(
64 message_string, account);
65
66 // UNCONSTRAINED: verify the created signature
67 bool dry_run = crypto::
68 ecdsa_verify_signature<crypto::Sha256Hasher, typename curve::fq, typename curve::fr, typename curve::g1>(
69 message_string, account.public_key, signature);
70 if (!dry_run) {
71 throw_or_abort("[non circuit]: Sig verification failed");
72 }
73
74 // IN CIRCUIT: create a witness with the pub key in our circuit
75 typename curve::g1_bigfr_ct public_key = curve::g1_bigfr_ct::from_witness(&builder, account.public_key);
76
77 std::vector<uint8_t> rr(signature.r.begin(), signature.r.end());
78 std::vector<uint8_t> ss(signature.s.begin(), signature.s.end());
79
80 // IN CIRCUIT: create a witness with the sig in our circuit
82 typename curve::byte_array_ct(&builder, ss) };
83
84 // Compute H(m) natively and pass as witness (mirrors ACIR which takes pre-hashed message)
85 auto hash_arr = crypto::sha256(std::vector<uint8_t>(message_string.begin(), message_string.end()));
86 stdlib::byte_array<Builder> hashed_message(&builder, std::vector<uint8_t>(hash_arr.begin(), hash_arr.end()));
87
88 // IN CIRCUIT: verify the signature
89 typename curve::bool_ct signature_result = stdlib::ecdsa_verify_signature<Builder,
90 curve,
91 typename curve::fq_ct,
92 typename curve::bigfr_ct,
93 typename curve::g1_bigfr_ct>(
94 // hashed_message, public_key, sig);
95 hashed_message,
96 public_key,
97 sig);
98
99 // Assert the signature is true
100 signature_result.assert_equal(bool_ct(true));
101
103
104 return builder;
105 }
106};
107
108} // 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:60
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:433
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.
Manages the data that is propagated on the public inputs of an application/function circuit.
static void add_default(Builder &builder)
Add default public inputs when they are not present.
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:24
std::array< uint8_t, 32 > r
Definition ecdsa.hpp:31
std::array< uint8_t, 32 > s
Definition ecdsa.hpp:32
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)