Skip to content

Commit 3793476

Browse files
committed
fix: resolve 6 _GLIBCXX_DEBUG runtime violations in barretenberg debug build
- batched_affine_addition.cpp: fix span::subspan OOB (passed count instead of remaining count) - scalar_multiplication.hpp: avoid indexing empty vector when all MSM scalars are zero - hmac.hpp: use secure_erase instead of `= {}` which empties vector for KeccakHasher - gemini_impl.hpp: clamp multilinear_challenge access to span size in high-degree attack tests - msm_builder.hpp: guard parallel_for_range when num_point_adds_and_doubles is 0 - poseidon2.test.cpp: fix off-by-one in hash padding collision test (hashes[3] on size-3 vector)
1 parent 4598628 commit 3793476

6 files changed

Lines changed: 25 additions & 20 deletions

File tree

barretenberg/cpp/src/barretenberg/commitment_schemes/gemini/gemini_impl.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ std::vector<typename GeminiProver_<Curve>::Polynomial> GeminiProver_<Curve>::com
137137
// size of the previous polynomial/2
138138
const size_t n_l = 1 << (log_n - l - 1);
139139

140-
// Opening point is the same for all
141-
const Fr u_l = multilinear_challenge[l];
140+
// Opening point is the same for all; use zero for rounds beyond the challenge size
141+
const Fr u_l = l < virtual_log_n ? multilinear_challenge[l] : Fr(0);
142142

143143
// A_l_fold = Aₗ₊₁(X) = (1-uₗ)⋅even(Aₗ)(X) + uₗ⋅odd(Aₗ)(X)
144144
auto A_l_fold = fold_polynomials[l].data();
@@ -161,7 +161,7 @@ std::vector<typename GeminiProver_<Curve>::Polynomial> GeminiProver_<Curve>::com
161161
// value at every point, (f(X) - f(x)) / (X - x) = 0, so these contribute nothing to the Shplonk quotient Q(X).
162162
// On the verifier side, padding_indicator_array zeros their contributions independently.
163163
const auto& last = fold_polynomials.back();
164-
const Fr u_last = multilinear_challenge[log_n - 1];
164+
const Fr u_last = (log_n - 1) < virtual_log_n ? multilinear_challenge[log_n - 1] : Fr(0);
165165
const Fr final_eval = last.at(0) + u_last * (last.at(1) - last.at(0));
166166
Polynomial const_fold(1);
167167
const_fold.at(0) = final_eval;

barretenberg/cpp/src/barretenberg/crypto/hmac/hmac.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ Fr deterministic_nonce_rfc6979(const MessageContainer& message, const KeyContain
136136
secure_erase(message_buffer);
137137
// Round trip reduces the hash modulo Fr::modulus
138138
Fr hashed_message_fr = Fr::serialize_from_buffer(hashed_message.data());
139-
hashed_message = {};
140-
Fr::serialize_to_buffer(hashed_message_fr, &hashed_message[0]);
139+
secure_erase(hashed_message);
140+
Fr::serialize_to_buffer(hashed_message_fr, hashed_message.data());
141141

142142
// Concatenate the private key and the hashed message
143143
std::vector<uint8_t> seed_material;

barretenberg/cpp/src/barretenberg/ecc/batched_affine_addition/batched_affine_addition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ std::span<typename BatchedAffineAddition<Curve>::Fq> BatchedAffineAddition<
143143
// Define scratch space for batched inverse computations and eventual storage of denominators
144144
BB_ASSERT_GTE(add_sequences.scratch_space.size(), 2 * total_num_pairs);
145145
std::span<Fq> denominators = add_sequences.scratch_space.subspan(0, total_num_pairs);
146-
std::span<Fq> differences = add_sequences.scratch_space.subspan(total_num_pairs, 2 * total_num_pairs);
146+
std::span<Fq> differences = add_sequences.scratch_space.subspan(total_num_pairs, total_num_pairs);
147147

148148
// Compute and store successive products of differences (x_2 - x_1)
149149
Fq accumulator = 1;

barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/scalar_multiplication.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,15 @@ template <typename Curve> class MSM {
118118
std::span<uint64_t> point_schedule_buffer,
119119
const MSMWorkUnit& work_unit) noexcept
120120
{
121+
const auto& indices = all_indices[work_unit.batch_msm_index];
122+
// Avoid indexing into an empty vector when all scalars are zero (work_unit.size == 0)
123+
std::span<const uint32_t> scalar_indices =
124+
work_unit.size > 0 ? std::span<const uint32_t>{ &indices[work_unit.start_index], work_unit.size }
125+
: std::span<const uint32_t>{};
121126
return MSMData{
122127
.scalars = all_scalars[work_unit.batch_msm_index],
123128
.points = all_points[work_unit.batch_msm_index],
124-
.scalar_indices =
125-
std::span<const uint32_t>{ &all_indices[work_unit.batch_msm_index][work_unit.start_index],
126-
work_unit.size },
129+
.scalar_indices = scalar_indices,
127130
.point_schedule = point_schedule_buffer,
128131
};
129132
}

barretenberg/cpp/src/barretenberg/eccvm/msm_builder.hpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -424,16 +424,18 @@ class ECCVMMSMMBuilder {
424424

425425
// inverse_trace is used to compute the value of the `collision_inverse` column in the ECCVM.
426426
std::vector<FF> inverse_trace(num_point_adds_and_doubles);
427-
parallel_for_range(num_point_adds_and_doubles, [&](size_t start, size_t end) {
428-
for (size_t operation_idx = start; operation_idx < end; ++operation_idx) {
429-
if (is_double_or_add[operation_idx]) {
430-
inverse_trace[operation_idx] = (p1_trace[operation_idx].y + p1_trace[operation_idx].y);
431-
} else {
432-
inverse_trace[operation_idx] = (p2_trace[operation_idx].x - p1_trace[operation_idx].x);
427+
if (num_point_adds_and_doubles > 0) {
428+
parallel_for_range(num_point_adds_and_doubles, [&](size_t start, size_t end) {
429+
for (size_t operation_idx = start; operation_idx < end; ++operation_idx) {
430+
if (is_double_or_add[operation_idx]) {
431+
inverse_trace[operation_idx] = (p1_trace[operation_idx].y + p1_trace[operation_idx].y);
432+
} else {
433+
inverse_trace[operation_idx] = (p2_trace[operation_idx].x - p1_trace[operation_idx].x);
434+
}
433435
}
434-
}
435-
FF::batch_invert(&inverse_trace[start], end - start);
436-
});
436+
FF::batch_invert(&inverse_trace[start], end - start);
437+
});
438+
}
437439

438440
// complete the computation of the ECCVM execution trace, by adding the affine intermediate point data
439441
// i.e. row.accumulator_x, row.accumulator_y, row.add_state[0...3].collision_inverse,

barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ template <typename Builder> class StdlibPoseidon2 : public testing::Test {
249249
}
250250

251251
// The domain separation IV depends on the input size, therefore, the hashes must not coincide.
252+
EXPECT_NE(hashes[0], hashes[1]);
252253
EXPECT_NE(hashes[1], hashes[2]);
253-
EXPECT_NE(hashes[2], hashes[3]);
254-
EXPECT_NE(hashes[1], hashes[3]);
254+
EXPECT_NE(hashes[0], hashes[2]);
255255
}
256256

257257
// Test vectors and the expected values are taken from https://github.com/zemse/poseidon2-evm

0 commit comments

Comments
 (0)