Skip to content

Commit a4c79f3

Browse files
authored
feat: merge-train/barretenberg (#17833)
BEGIN_COMMIT_OVERRIDE chore: Set variable can only be used when writing vks (#17820) chore: DSL testing classes (#17834) END_COMMIT_OVERRIDE
2 parents 14a80e2 + 34b71f2 commit a4c79f3

File tree

5 files changed

+649
-0
lines changed

5 files changed

+649
-0
lines changed
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
// === AUDIT STATUS ===
2+
// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3+
// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4+
// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5+
// =====================
6+
7+
#pragma once
8+
9+
#include "barretenberg/dsl/acir_format/acir_format.hpp"
10+
#include "barretenberg/dsl/acir_format/acir_format_mocks.hpp"
11+
#include "barretenberg/stdlib/primitives/field/field.hpp"
12+
#include "gtest/gtest.h"
13+
#include <vector>
14+
15+
namespace acir_format {
16+
17+
using namespace bb;
18+
using namespace bb::stdlib;
19+
20+
/**
21+
* @brief Add a constraint element to the appropriate vector in AcirFormat
22+
*
23+
* @details Uses constexpr if to determine the correct vector based on constraint type. RecursionConstraints are not
24+
* handled in this function.
25+
*
26+
* @param acir_format The AcirFormat object to add the constraint to
27+
* @param constraint The constraint to add
28+
*/
29+
template <typename ConstraintType>
30+
void add_constraint_to_acir_format(AcirFormat& acir_format, const ConstraintType& constraint)
31+
{
32+
if constexpr (std::is_same_v<ConstraintType, LogicConstraint>) {
33+
acir_format.logic_constraints.push_back(constraint);
34+
} else if constexpr (std::is_same_v<ConstraintType, RangeConstraint>) {
35+
acir_format.range_constraints.push_back(constraint);
36+
} else if constexpr (std::is_same_v<ConstraintType, AES128Constraint>) {
37+
acir_format.aes128_constraints.push_back(constraint);
38+
} else if constexpr (std::is_same_v<ConstraintType, Sha256Compression>) {
39+
acir_format.sha256_compression.push_back(constraint);
40+
} else if constexpr (std::is_same_v<ConstraintType, EcdsaConstraint>) {
41+
if (constraint.type == bb::CurveType::SECP256K1) {
42+
acir_format.ecdsa_k1_constraints.push_back(constraint);
43+
} else {
44+
acir_format.ecdsa_r1_constraints.push_back(constraint);
45+
}
46+
} else if constexpr (std::is_same_v<ConstraintType, Blake2sConstraint>) {
47+
acir_format.blake2s_constraints.push_back(constraint);
48+
} else if constexpr (std::is_same_v<ConstraintType, Blake3Constraint>) {
49+
acir_format.blake3_constraints.push_back(constraint);
50+
} else if constexpr (std::is_same_v<ConstraintType, Keccakf1600>) {
51+
acir_format.keccak_permutations.push_back(constraint);
52+
} else if constexpr (std::is_same_v<ConstraintType, Poseidon2Constraint>) {
53+
acir_format.poseidon2_constraints.push_back(constraint);
54+
} else if constexpr (std::is_same_v<ConstraintType, MultiScalarMul>) {
55+
acir_format.multi_scalar_mul_constraints.push_back(constraint);
56+
} else if constexpr (std::is_same_v<ConstraintType, EcAdd>) {
57+
acir_format.ec_add_constraints.push_back(constraint);
58+
} else if constexpr (std::is_same_v<ConstraintType, RecursionConstraint>) {
59+
throw_or_abort("Recursion constraints are not currently supported.");
60+
} else if constexpr (std::is_same_v<ConstraintType, BlockConstraint>) {
61+
acir_format.block_constraints.push_back(constraint);
62+
} else if constexpr (std::is_same_v<ConstraintType, AcirFormat::PolyTripleConstraint>) {
63+
acir_format.poly_triple_constraints.push_back(constraint);
64+
} else if constexpr (std::is_same_v<ConstraintType, bb::mul_quad_<bb::curve::BN254::ScalarField>>) {
65+
acir_format.quad_constraints.push_back(constraint);
66+
} else if constexpr (std::is_same_v<ConstraintType, std::vector<bb::mul_quad_<bb::curve::BN254::ScalarField>>>) {
67+
acir_format.big_quad_constraints.push_back(constraint);
68+
} else {
69+
throw_or_abort("Unsupported constraint type");
70+
}
71+
}
72+
73+
/**
74+
* @brief Concept defining the requirements for the Base template parameter of TestClass
75+
*
76+
* @details Base must provide:
77+
* - A struct Tampering, which specifies how to tamper with the witness values so to make the constraints
78+
* unsatisfied. Tampering must specify an enum class Mode, which details the different tampering modes, and two
79+
* functions get_all() and get_labels() to iterate over all the possible tampering modes.
80+
* - Type aliases: Builder and AcirConstraint, specifying the Builder and constraint we are working with.
81+
* - Static methods: generate_constraints (to generate valid constraints with predicate set to witness true), tampering
82+
* (to tamper with the witness values to produce unsatisfied constraints).
83+
*/
84+
template <typename T>
85+
concept TestBase = requires {
86+
// Required type aliases
87+
typename T::Builder;
88+
typename T::AcirConstraint;
89+
typename T::Tampering;
90+
typename T::Tampering::Mode;
91+
92+
// Ensure Tampering::Mode is enum
93+
requires std::is_enum_v<typename T::Tampering::Mode>;
94+
95+
// Ensure that Tampering::Mode has a None value
96+
{ T::Tampering::Mode::None };
97+
98+
// Tampering must provide static methods for test iteration
99+
{ T::Tampering::get_all() } -> std::same_as<std::vector<typename T::Tampering::Mode>>;
100+
{ T::Tampering::get_labels() } -> std::same_as<std::vector<std::string>>;
101+
102+
// Required static constraint manipulation methods
103+
requires requires(typename T::AcirConstraint& constraint,
104+
WitnessVector& witness_values,
105+
const typename T::Tampering::Mode& tampering_mode) {
106+
/**
107+
* @brief Generate valid constraints with predicate set to a witness holding the value true.
108+
*
109+
*/
110+
{ T::generate_constraints(constraint, witness_values) } -> std::same_as<void>;
111+
112+
/**
113+
* @brief Tamper with the witness values to test that invalid witnesses produce unsatisfied constraints.
114+
*
115+
*/
116+
{ T::tampering(constraint, witness_values, tampering_mode) } -> std::same_as<void>;
117+
};
118+
};
119+
120+
template <TestBase Base> class TestClass {
121+
using Builder = Base::Builder;
122+
using AcirConstraint = Base::AcirConstraint;
123+
using Tampering = Base::Tampering;
124+
using TamperingMode = Base::Tampering::Mode;
125+
126+
/**
127+
* @brief Generate constraints and witness values based on the tampering mode.
128+
*/
129+
static std::pair<AcirConstraint, WitnessVector> generate_constraints(
130+
const TamperingMode& tampering_mode = TamperingMode::None)
131+
{
132+
AcirConstraint constraint;
133+
WitnessVector witness_values;
134+
Base::generate_constraints(constraint, witness_values);
135+
Base::tampering(constraint, witness_values, tampering_mode);
136+
137+
return { constraint, witness_values };
138+
}
139+
140+
/**
141+
* @brief General purpose testing function. It generates the test based on the tampering mode.
142+
*/
143+
static std::tuple<bool, bool, std::string> test_constraints(const TamperingMode& tampering_mode)
144+
{
145+
auto [constraint, witness_values] = generate_constraints(tampering_mode);
146+
147+
AcirFormat constraint_system = {
148+
.varnum = static_cast<uint32_t>(witness_values.size()),
149+
.num_acir_opcodes = 1,
150+
.public_inputs = {},
151+
.original_opcode_indices = create_empty_original_opcode_indices(),
152+
};
153+
154+
add_constraint_to_acir_format(constraint_system, constraint);
155+
156+
mock_opcode_indices(constraint_system);
157+
158+
AcirProgram program{ constraint_system, witness_values };
159+
auto builder = create_circuit<Builder>(program);
160+
161+
return { CircuitChecker::check(builder), builder.failed(), builder.err() };
162+
}
163+
164+
/**
165+
* @brief Test vk generation is independent of the witness values supplied.
166+
*
167+
* @tparam Flavor
168+
*/
169+
template <typename Flavor> static void test_vk_independence()
170+
{
171+
using ProverInstance = ProverInstance_<Flavor>;
172+
using VerificationKey = Flavor::VerificationKey;
173+
174+
// Generate the constraint system
175+
auto [constraint, witness_values] = generate_constraints();
176+
177+
AcirFormat constraint_system = {
178+
.varnum = static_cast<uint32_t>(witness_values.size()),
179+
.num_acir_opcodes = 1,
180+
.public_inputs = {},
181+
.original_opcode_indices = create_empty_original_opcode_indices(),
182+
};
183+
184+
add_constraint_to_acir_format(constraint_system, constraint);
185+
186+
mock_opcode_indices(constraint_system);
187+
188+
// Construct the vks
189+
std::shared_ptr<VerificationKey> vk_from_witness;
190+
{
191+
AcirProgram program{ constraint_system, witness_values };
192+
auto builder = create_circuit<Builder>(program);
193+
info("Num gates: ", builder.get_estimated_num_finalized_gates());
194+
195+
auto prover_instance = std::make_shared<ProverInstance>(builder);
196+
vk_from_witness = std::make_shared<VerificationKey>(prover_instance->get_precomputed());
197+
198+
// Validate the builder
199+
EXPECT_TRUE(CircuitChecker::check(builder));
200+
}
201+
202+
std::shared_ptr<VerificationKey> vk_from_constraint;
203+
{
204+
AcirProgram program{ constraint_system, /*witness=*/{} };
205+
auto builder = create_circuit<Builder>(program);
206+
auto prover_instance = std::make_shared<ProverInstance>(builder);
207+
vk_from_constraint = std::make_shared<VerificationKey>(prover_instance->get_precomputed());
208+
}
209+
210+
EXPECT_EQ(*vk_from_witness, *vk_from_constraint) << "Mismatch in the vks";
211+
}
212+
213+
/**
214+
* @brief Test all tampering modes.
215+
*
216+
* @return std::vector<std::string> List of error messages from the builder for each tampering mode.
217+
*/
218+
static std::vector<std::string> test_tampering()
219+
{
220+
std::vector<std::string> error_msgs;
221+
for (auto [mode, label] : zip_view(Tampering::get_all(), Tampering::get_labels())) {
222+
auto [circuit_checker_result, builder_failed, builder_err] = test_constraints(mode);
223+
error_msgs.emplace_back(builder_err);
224+
225+
if (mode != Tampering::Mode::None) {
226+
EXPECT_FALSE(circuit_checker_result)
227+
<< "Circuit checker succeeded unexpectedly for tampering mode " + label;
228+
EXPECT_TRUE(builder_failed) << "Builder succeeded unexpectedly for tampering mode " + label;
229+
} else {
230+
EXPECT_TRUE(circuit_checker_result)
231+
<< "Circuit checker failed unexpectedly for tampering mode " + label;
232+
EXPECT_FALSE(builder_failed) << "Builder failed unexpectedly for tampering mode " + label;
233+
}
234+
}
235+
236+
return error_msgs;
237+
}
238+
};
239+
240+
} // namespace acir_format

0 commit comments

Comments
 (0)