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:
26 using base_field = typename curve::BaseField;
31 using group = typename curve::Group;
33
34 static constexpr size_t NUM_PUBLIC_INPUTS = 6;
35
36 static Builder generate(uint256_t public_inputs[])
37 {
39
40 // IN CIRCUIT
41 // Create an input buffer from public inputs (treating each as a single byte)
42 byte_array_ct input_buffer(&builder, std::vector<uint8_t>());
43 for (size_t i = 0; i < NUM_PUBLIC_INPUTS; ++i) {
44 field_ct byte_value = public_witness_ct(&builder, public_inputs[i]);
45 // Constrain to be a single byte and create byte_array
46 byte_array_ct single_byte(byte_value, 1);
47 input_buffer.write(single_byte);
48 }
49
50 // This is the message that we would like to confirm
51 std::string message_string(NUM_PUBLIC_INPUTS, '\0');
52 for (size_t i = 0; i < NUM_PUBLIC_INPUTS; ++i) {
53 message_string[i] = static_cast<char>(static_cast<uint8_t>(public_inputs[i]));
54 }
55 auto message = byte_array_ct(&builder, message_string);
56
57 // Assert that the public inputs buffer matches the message we want
58 for (size_t i = 0; i < NUM_PUBLIC_INPUTS; ++i) {
59 input_buffer[i].assert_equal(message[i]);
60 }
61
62 // UNCONSTRAINED: create a random keypair to sign with
64 account.private_key = curve::ScalarFieldNative::random_element();
65 account.public_key = curve::GroupNative::one * account.private_key;
66
67 // UNCONSTRAINED: create a sig
68 crypto::ecdsa_signature signature = crypto::
69 ecdsa_construct_signature<crypto::Sha256Hasher, base_field_native, scalar_field_native, group_native>(
70 message_string, account);
71
72 // UNCONSTRAINED: verify the created signature
73 bool dry_run =
74 crypto::ecdsa_verify_signature<crypto::Sha256Hasher, base_field_native, scalar_field_native, group_native>(
75 message_string, account.public_key, signature);
76 if (!dry_run) {
77 throw_or_abort("[non circuit]: Sig verification failed");
78 }
79
80 // IN CIRCUIT: create a witness with the pub key in our circuit
81 group public_key = group::from_witness(&builder, account.public_key);
82
83 std::vector<uint8_t> rr(signature.r.begin(), signature.r.end());
84 std::vector<uint8_t> ss(signature.s.begin(), signature.s.end());
85
86 // IN CIRCUIT: create a witness with the sig in our circuit
88
89 // Compute H(m) natively and pass as witness (mirrors ACIR which takes pre-hashed message)
90 auto hash_arr = crypto::sha256(std::vector<uint8_t>(message_string.begin(), message_string.end()));
91 byte_array_ct hashed_message(&builder, std::vector<uint8_t>(hash_arr.begin(), hash_arr.end()));
92
93 // IN CIRCUIT: verify the signature
94 bool_ct signature_result = stdlib::ecdsa_verify_signature<Builder, curve, base_field, scalar_field, group>(
95 // hashed_message, public_key, sig);
96 hashed_message,
97 public_key,
98 sig);
99
100 // Assert the signature is true
101 signature_result.assert_equal(bool_ct(true));
102
104
105 return builder;
106 }
107};
108
109} // namespace bb
static constexpr size_t NUM_PUBLIC_INPUTS
stdlib::bool_t< Builder > bool_ct
typename curve::BaseFieldNative base_field_native
typename curve::GroupNative group_native
typename curve::ScalarFieldNative scalar_field_native
typename curve::Group group
stdlib::public_witness_t< Builder > public_witness_ct
stdlib::byte_array< Builder > byte_array_ct
typename curve::BaseField base_field
typename curve::ScalarField scalar_field
static Builder generate(uint256_t public_inputs[])
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
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
::bb::secp256k1::fq BaseFieldNative
Definition secp256k1.hpp:23
bigfield< CircuitType, typename ::bb::secp256k1::FrParams > ScalarField
Definition secp256k1.hpp:29
::bb::secp256k1::fr ScalarFieldNative
Definition secp256k1.hpp:22
bigfield< CircuitType, typename ::bb::secp256k1::FqParams > BaseField
Definition secp256k1.hpp:30
::bb::secp256k1::g1 GroupNative
Definition secp256k1.hpp:24
element< CircuitType, BaseField, ScalarField, GroupNative > Group
Definition secp256k1.hpp:31
void throw_or_abort(std::string const &err)