Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
ecdsa_constraints.test.cpp
Go to the documentation of this file.
2#include "acir_format.hpp"
8
9#include <algorithm>
10#include <gtest/gtest.h>
11#include <vector>
12
13using namespace bb;
14using namespace bb::crypto;
15using namespace acir_format;
16
17template <class Curve> class EcdsaConstraintsTest : public ::testing::Test {
18 public:
19 using Builder = Curve::Builder;
20 using FrNative = Curve::fr;
21 using FqNative = Curve::fq;
22 using G1Native = Curve::g1;
24
25 // Reproducible test
26 static constexpr FrNative private_key =
27 FrNative("0xd67abee717b3fc725adf59e2cc8cd916435c348b277dd814a34e3ceb279436c2");
28
29 static size_t generate_ecdsa_constraint(EcdsaConstraint& ecdsa_constraint, WitnessVector& witness_values)
30 {
31 std::string message_string = "Instructions unclear, ask again later.";
32
33 // Hash the message
34 std::vector<uint8_t> message_buffer(message_string.begin(), message_string.end());
35 std::array<uint8_t, 32> hashed_message = Sha256Hasher::hash(message_buffer);
36
37 // Generate ECDSA key pair
39 account.private_key = private_key;
40 account.public_key = G1Native::one * account.private_key;
41
42 // Generate signature
43 ecdsa_signature signature =
44 ecdsa_construct_signature<Sha256Hasher, FqNative, FrNative, G1Native>(message_string, account);
45
46 // Serialize public key coordinates into bytes
47 std::array<uint8_t, 32> buffer_x;
48 std::array<uint8_t, 32> buffer_y;
49 FqNative::serialize_to_buffer(account.public_key.x, &buffer_x[0]);
50 FqNative::serialize_to_buffer(account.public_key.y, &buffer_y[0]);
51
52 // Create witness indices and witnesses
53 size_t num_variables = 0;
54
55 std::array<uint32_t, 32> hashed_message_indices =
56 add_to_witness_and_track_indices<uint8_t, 32>(witness_values, std::span(hashed_message));
57 num_variables += hashed_message_indices.size();
58
59 std::array<uint32_t, 32> pub_x_indices =
60 add_to_witness_and_track_indices<uint8_t, 32>(witness_values, std::span(buffer_x));
61 num_variables += pub_x_indices.size();
62
63 std::array<uint32_t, 32> pub_y_indices =
64 add_to_witness_and_track_indices<uint8_t, 32>(witness_values, std::span(buffer_y));
65 num_variables += pub_y_indices.size();
66
67 std::array<uint32_t, 32> r_indices =
68 add_to_witness_and_track_indices<uint8_t, 32>(witness_values, std::span(signature.r));
69 num_variables += r_indices.size();
70
71 std::array<uint32_t, 32> s_indices =
72 add_to_witness_and_track_indices<uint8_t, 32>(witness_values, std::span(signature.s));
73 num_variables += s_indices.size();
74
75 uint32_t result_index = static_cast<uint32_t>(num_variables);
76 bb::fr result = bb::fr::one();
77 witness_values.emplace_back(result);
78 num_variables += 1;
79
80 // Restructure vectors into array
81 std::array<uint32_t, 64> signature_indices;
82 std::ranges::copy(r_indices, signature_indices.begin());
83 std::ranges::copy(s_indices, signature_indices.begin() + 32);
84
85 ecdsa_constraint = EcdsaConstraint{ .hashed_message = hashed_message_indices,
86 .signature = signature_indices,
87 .pub_x_indices = pub_x_indices,
88 .pub_y_indices = pub_y_indices,
89 .result = result_index };
90
91 return num_variables;
92 }
93
95 {
96 EcdsaConstraint ecdsa_constraint;
97 WitnessVector witness_values;
98 size_t num_variables = generate_ecdsa_constraint(ecdsa_constraint, witness_values);
99 AcirFormat constraint_system = {
100 .varnum = static_cast<uint32_t>(num_variables),
101 .num_acir_opcodes = 1,
102 .public_inputs = {},
103 .original_opcode_indices = create_empty_original_opcode_indices(),
104 };
105
106 if constexpr (Curve::type == bb::CurveType::SECP256K1) {
107 constraint_system.ecdsa_k1_constraints = { ecdsa_constraint };
108 } else {
109 constraint_system.ecdsa_r1_constraints = { ecdsa_constraint };
110 }
111
112 mock_opcode_indices(constraint_system);
113
114 return { constraint_system, witness_values };
115 }
116
117 protected:
119};
120
121using CurveTypes = testing::Types<stdlib::secp256k1<MegaCircuitBuilder>,
125
127
128TYPED_TEST(EcdsaConstraintsTest, GenerateVKFromConstraints)
129{
130 using Flavor = TestFixture::Flavor;
131 using Builder = TestFixture::Builder;
132 using ProvingKey = DeciderProvingKey_<Flavor>;
134
135 auto [constraint_system, witness_values] = TestFixture::generate_constraint_system();
136
137 std::shared_ptr<VerificationKey> vk_from_witness;
138 {
139 AcirProgram program{ constraint_system, witness_values };
140 auto builder = create_circuit<Builder>(program);
141 info("Num gates: ", builder.get_estimated_num_finalized_gates());
142
143 auto proving_key = std::make_shared<ProvingKey>(builder);
144 vk_from_witness = std::make_shared<VerificationKey>(proving_key->get_precomputed());
145
146 // Validate the builder
147 EXPECT_TRUE(CircuitChecker::check(builder));
148 }
149
150 std::shared_ptr<VerificationKey> vk_from_constraint;
151 {
152 AcirProgram program{ constraint_system, /*witness=*/{} };
153 auto builder = create_circuit<Builder>(program);
154 auto proving_key = std::make_shared<ProvingKey>(builder);
155 vk_from_constraint = std::make_shared<VerificationKey>(proving_key->get_precomputed());
156 }
157
158 EXPECT_EQ(*vk_from_witness, *vk_from_constraint);
159}
acir_format::AcirFormatOriginalOpcodeIndices create_empty_original_opcode_indices()
void mock_opcode_indices(acir_format::AcirFormat &constraint_system)
static std::pair< AcirFormat, WitnessVector > generate_constraint_system()
static size_t generate_ecdsa_constraint(EcdsaConstraint &ecdsa_constraint, WitnessVector &witness_values)
std::conditional_t< std::is_same_v< Builder, UltraCircuitBuilder >, UltraFlavor, MegaFlavor > Flavor
static constexpr FrNative private_key
A DeciderProvingKey is normally constructed from a finalized circuit and it contains all the informat...
The verification key is responsible for storing the commitments to the precomputed (non-witnessk) pol...
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
void info(Args... args)
Definition log.hpp:70
AluTraceBuilder builder
Definition alu.test.cpp:123
UltraKeccakFlavor::VerificationKey VerificationKey
bb::SlabVector< bb::fr > WitnessVector
std::filesystem::path bb_crs_path()
void init_file_crs_factory(const std::filesystem::path &path)
TYPED_TEST_SUITE(BoomerangRecursiveVerifierTest, Flavors)
Entry point for Barretenberg command-line interface.
::testing::Types< curve::BN254, curve::Grumpkin > CurveTypes
TYPED_TEST(ShpleminiTest, CorrectnessOfMultivariateClaimBatching)
@ SECP256K1
Definition types.hpp:10
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::vector< EcdsaConstraint > ecdsa_r1_constraints
std::vector< EcdsaConstraint > ecdsa_k1_constraints
std::array< uint32_t, 32 > hashed_message
static auto hash(const B &message)
Definition hashers.hpp:36
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 constexpr field one()