Skip to content

Commit c29739c

Browse files
authored
feat: merge-train/barretenberg (#16729)
BEGIN_COMMIT_OVERRIDE chore: audit ECCVM msm relation (#16532) feat: add storage budget control for FileBackedMemory (#16690) fix(bb): fix honk_solidity_key_gen debug build (#16744) chore: wasm fix (#16751) END_COMMIT_OVERRIDE
2 parents 6111ef9 + 57954e3 commit c29739c

16 files changed

Lines changed: 848 additions & 488 deletions

File tree

barretenberg/cpp/src/barretenberg/api/api.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class API {
2222
bool write_vk{ false }; // should we addditionally write the verification key when writing the proof
2323
bool include_gates_per_opcode{ false }; // should we include gates_per_opcode in the gates command output
2424
bool slow_low_memory{ false }; // use file backed memory for polynomials
25+
std::string storage_budget; // storage budget for file backed memory (e.g. "500m", "2g")
2526
bool update_inputs{ false }; // update inputs when check fails
2627

2728
bool optimized_solidity_verifier{ false }; // should we use the optimized sol verifier? (temp)
@@ -40,6 +41,7 @@ class API {
4041
<< " write_vk " << flags.write_vk << "\n"
4142
<< " include_gates_per_opcode " << flags.include_gates_per_opcode << "\n"
4243
<< " slow_low_memory " << flags.slow_low_memory << "\n"
44+
<< " storage_budget " << flags.storage_budget << "\n"
4345
<< "]" << std::endl;
4446
return os;
4547
}

barretenberg/cpp/src/barretenberg/bb/cli.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ int parse_and_run_cli_command(int argc, char* argv[])
265265
"--slow_low_memory", flags.slow_low_memory, "Enable low memory mode (can be 2x slower or more).");
266266
};
267267

268+
const auto add_storage_budget_option = [&](CLI::App* subcommand) {
269+
return subcommand->add_option("--storage_budget",
270+
flags.storage_budget,
271+
"Storage budget for FileBackedMemory (e.g. '500m', '2g'). When exceeded, falls "
272+
"back to RAM (requires --slow_low_memory).");
273+
};
274+
268275
const auto add_update_inputs_flag = [&](CLI::App* subcommand) {
269276
return subcommand->add_flag("--update_inputs", flags.update_inputs, "Update inputs if vk check fails.");
270277
};
@@ -345,6 +352,7 @@ int parse_and_run_cli_command(int argc, char* argv[])
345352
add_slow_low_memory_flag(prove);
346353
add_print_bench_flag(prove);
347354
add_bench_out_option(prove);
355+
add_storage_budget_option(prove);
348356

349357
prove->add_flag("--verify", "Verify the proof natively, resulting in a boolean output. Useful for testing.");
350358

@@ -551,6 +559,9 @@ int parse_and_run_cli_command(int argc, char* argv[])
551559
verbose_logging = debug_logging || flags.verbose;
552560
slow_low_memory = flags.slow_low_memory;
553561
#ifndef __wasm__
562+
if (!flags.storage_budget.empty()) {
563+
storage_budget = parse_size_string(flags.storage_budget);
564+
}
554565
if (print_bench || !bench_out.empty()) {
555566
bb::detail::use_bb_bench = true;
556567
}

barretenberg/cpp/src/barretenberg/benchmark/decrypt_bench/CMakeLists.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
if (NOT FUZZING)
1+
if (NOT FUZZING)
22
add_executable(
33
decrypt_bench
44
main.cpp
@@ -10,4 +10,16 @@ if (NOT FUZZING)
1010
ecc
1111
common
1212
)
13-
endif()
13+
if(CHECK_CIRCUIT_STACKTRACES OR ENABLE_STACKTRACES)
14+
target_link_libraries(
15+
decrypt_bench
16+
PUBLIC
17+
Backward::Interface
18+
)
19+
target_link_options(
20+
decrypt_bench
21+
PRIVATE
22+
-ldw -lelf
23+
)
24+
endif()
25+
endif()

barretenberg/cpp/src/barretenberg/eccvm/README.md

Lines changed: 334 additions & 324 deletions
Large diffs are not rendered by default.

barretenberg/cpp/src/barretenberg/eccvm/eccvm_builder_types.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ template <typename CycleGroup> struct ScalarMul {
2828
typename CycleGroup::affine_element base_point;
2929
std::array<int, NUM_WNAF_DIGITS_PER_SCALAR>
3030
wnaf_digits; // [a_{n-1}, a_{n-1}, ..., a_{0}], where each a_i ∈ {-2ʷ⁻¹ + 1, -2ʷ⁻¹ + 3, ..., 2ʷ⁻¹ - 3, 2ʷ⁻¹ -
31-
// 1} ∪ {0}. (here, w = `NUM_WNAF_DIGIT_BITS`). in particular, a_i is an odd integer with
31+
// 1}. (here, w = `NUM_WNAF_DIGIT_BITS`). in particular, a_i is an odd integer with
3232
// absolute value less than 2ʷ. Represents the number `scalar` = ∑ᵢ aᵢ 2⁴ⁱ - `wnaf_skew`.
3333
bool wnaf_skew; // necessary to represent _even_ integers
3434
// size bumped by 1 to record base_point.dbl()

barretenberg/cpp/src/barretenberg/eccvm/eccvm_circuit_builder.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class ECCVMCircuitBuilder {
182182
msm.resize(msm_sizes[i]);
183183
}
184184
// populate result using the auxiliary vectors `msm_opqueue_index` and `msm_mul_index`, together with
185-
// `eccvm_ops`. this first pass will *not* get the pc (program counter) correct. we explain why when we set it
185+
// `eccvm_ops`. this first pass will *not* get the pc (point counter) correct. we explain why when we set it
186186
// correctly.
187187
parallel_for_range(msm_opqueue_index.size(), [&](size_t start, size_t end) {
188188
for (size_t i = start; i < end; i++) {

barretenberg/cpp/src/barretenberg/eccvm/eccvm_flavor.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,10 @@ class ECCVMFlavor {
549549
* msm_lambda2: temp variable used for ecc point addition algorithm if msm_add2 = 1
550550
* msm_lambda3: temp variable used for ecc point addition algorithm if msm_add3 = 1
551551
* msm_lambda4: temp variable used for ecc point addition algorithm if msm_add4 = 1
552+
* msm_slice1: wNAF digit/slice for first add
553+
* msm_slice2: wNAF digit/slice for second add
554+
* msm_slice3: wNAF digit/slice for third add
555+
* msm_slice4: wNAF digit/slice for fourth add
552556
* msm_collision_x1: used to ensure incomplete ecc addition exceptions not triggered if msm_add1 = 1
553557
* msm_collision_x2: used to ensure incomplete ecc addition exceptions not triggered if msm_add2 = 1
554558
* msm_collision_x3: used to ensure incomplete ecc addition exceptions not triggered if msm_add3 = 1

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ class ECCVMMSMMBuilder {
2727
static constexpr size_t NUM_WNAF_DIGITS_PER_SCALAR = bb::eccvm::NUM_WNAF_DIGITS_PER_SCALAR;
2828

2929
struct alignas(64) MSMRow {
30-
uint32_t pc = 0; // counter over all half-length (128 bit) scalar muls used to compute the required MSMs
31-
uint32_t msm_size = 0; // the number of points that will be scaled and summed
32-
uint32_t msm_count = 0; // number of multiplications processed so far in current MSM round
30+
uint32_t pc = 0; // decreasing point-counter, over all half-length (128 bit) scalar muls used to compute
31+
// the required MSMs. however, this value is _constant_ on a given MSM and more precisely
32+
// refers to the number of half-length scalar muls completed up until we have started
33+
// the current MSM.
34+
uint32_t msm_size = 0; // the number of points in (a.k.a. the length of) the MSM in whose computation
35+
// this VM row participates
36+
uint32_t msm_count = 0; // number of multiplications processed so far (not including this row) in current MSM
37+
// round (a.k.a. wNAF digit slot). this specifically refers to the number of wNAF-digit
38+
// * point scalar products we have looked up and accumulated.
3339
uint32_t msm_round = 0; // current "round" of MSM, in {0, ..., 32 = `NUM_WNAF_DIGITS_PER_SCALAR`}. With the
3440
// Straus algorithm, we proceed wNAF digit by wNAF digit, from left to right. (final
3541
// round deals with the `skew` bit.)
@@ -73,7 +79,7 @@ class ECCVMMSMMBuilder {
7379
* @brief Computes the row values for the Straus MSM columns of the ECCVM.
7480
*
7581
* For a detailed description of the Straus algorithm and its relation to the ECCVM, please see
76-
* https://hackmd.io/@aztec-network/rJ5xhuCsn
82+
* https://hackmd.io/@aztec-network/rJ5xhuCsn or, alternatively, the [ECCVM readme](README.md).
7783
*
7884
* @param msms A vector of vectors of `ScalarMul`s, a.k.a. a vector of `MSM`s.
7985
* @param point_table_read_counts Table of read counts to be populated.
@@ -96,17 +102,23 @@ class ECCVMMSMMBuilder {
96102
// row = point_idx * rows_per_point_table + (some function of the slice value)
97103
//
98104
// Illustration:
99-
// Block Structure Table structure:
100-
// | 0 | 1 | | Block_{0} | <-- pc = total_number_of_muls
101-
// | - | - | | Block_{1} | <-- pc = total_number_of_muls-(num muls in msm 0)
102-
// 1 | # | # | -1 | ... | ...
103-
// 3 | # | # | -3 | Block_{total_number_of_muls-1} | <-- pc = num muls in last msm
105+
// Block Structure:
106+
// | 0 | 1 |
107+
// | - | - |
108+
// 1 | # | # | -1
109+
// 3 | # | # | -3
104110
// 5 | # | # | -5
105111
// 7 | # | # | -7
106112
// 9 | # | # | -9
107113
// 11 | # | # | -11
108114
// 13 | # | # | -13
109115
// 15 | # | # | -15
116+
//
117+
// Table structure:
118+
// | Block_{0} | <-- pc = total_number_of_muls
119+
// | Block_{1} | <-- pc = total_number_of_muls-(num muls in msm 0)
120+
// | ... | ...
121+
// | Block_{total_number_of_muls-1} | <-- pc = num muls in last msm
110122

111123
const size_t num_rows_in_read_counts_table =
112124
static_cast<size_t>(total_number_of_muls) *
@@ -146,7 +158,7 @@ class ECCVMMSMMBuilder {
146158
std::vector<size_t> msm_row_counts;
147159
msm_row_counts.reserve(msms.size() + 1);
148160
msm_row_counts.push_back(1);
149-
// compute the program counter (i.e. the index among all single scalar muls) that each multiscalar
161+
// compute the point counter (i.e. the index among all single scalar muls) that each multiscalar
150162
// multiplication will start at.
151163
std::vector<size_t> pc_values;
152164
pc_values.reserve(msms.size() + 1);
@@ -203,7 +215,7 @@ class ECCVMMSMMBuilder {
203215
bool add = num_points_in_row > relative_point_idx;
204216
const size_t point_idx = offset + relative_point_idx;
205217
if (add) {
206-
// pc starts at total_number_of_muls and decreases non-uniformly to 0
218+
// `pc` starts at total_number_of_muls and decreases non-uniformly to 0.
207219
// -15 maps to the 1st point in the lookup table (array element 0)
208220
// -1 maps to the point in the lookup table that corresponds to the negation of the
209221
// original input point (i.e. the point we need to add into the accumulator if wnaf_skew

barretenberg/cpp/src/barretenberg/eccvm/transcript_builder.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ class ECCVMTranscriptBuilder {
109109

110110
// maintains the state of the VM at any given "time" (i.e., at any given value of pc).
111111
struct VMState {
112-
uint32_t pc = 0; // decreasing program counter that tracks the total number of multiplications that our virtual
113-
// machine has left to compute.
112+
uint32_t pc = 0; // decreasing point counter that tracks the total number of multiplications that our virtual
113+
// machine has left to compute.
114114
uint32_t count = 0; // Number of muls in the current MSM _excluding the current row_.
115115
Element accumulator = CycleGroup::affine_point_at_infinity; // accumulator for all group operations.
116116
Element msm_accumulator =
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,78 @@
11
#include "barretenberg/polynomials/backing_memory.hpp"
2+
#include "barretenberg/common/throw_or_abort.hpp"
3+
#include <atomic>
4+
#include <cctype>
5+
#include <cstdlib>
6+
#include <limits>
7+
#include <string>
28

39
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
410
bool slow_low_memory =
511
std::getenv("BB_SLOW_LOW_MEMORY") == nullptr ? false : std::string(std::getenv("BB_SLOW_LOW_MEMORY")) == "1";
12+
13+
// Storage budget is disabled for WASM builds
14+
#ifndef __wasm__
15+
16+
// Parse storage size string (e.g., "500m", "2g", "1024k")
17+
size_t parse_size_string(const std::string& size_str)
18+
{
19+
if (size_str.empty()) {
20+
return std::numeric_limits<size_t>::max();
21+
}
22+
23+
try {
24+
std::string str = size_str;
25+
26+
// Convert to lowercase for case-insensitive comparison
27+
char suffix = static_cast<char>(std::tolower(static_cast<unsigned char>(str.back())));
28+
size_t multiplier = 1;
29+
30+
// Check for unit suffix
31+
if (suffix == 'k') {
32+
multiplier = 1024ULL;
33+
str.pop_back();
34+
} else if (suffix == 'm') {
35+
multiplier = 1024ULL * 1024ULL;
36+
str.pop_back();
37+
} else if (suffix == 'g') {
38+
multiplier = 1024ULL * 1024ULL * 1024ULL;
39+
str.pop_back();
40+
} else if (std::isdigit(static_cast<unsigned char>(suffix)) == 0) {
41+
// Invalid suffix
42+
throw_or_abort("Invalid storage size format: '" + size_str + "'. Use format like '500m', '2g', or '1024k'");
43+
}
44+
45+
// Check if remaining string is a valid number
46+
if (str.empty()) {
47+
throw_or_abort("Invalid storage size format: '" + size_str + "'. No numeric value provided");
48+
}
49+
50+
size_t value = std::stoull(str);
51+
return value * multiplier;
52+
} catch (const std::invalid_argument&) {
53+
throw_or_abort("Invalid storage size format: '" + size_str + "'. Not a valid number");
54+
} catch (const std::out_of_range&) {
55+
throw_or_abort("Invalid storage size format: '" + size_str + "'. Value out of range");
56+
}
57+
}
58+
59+
namespace {
60+
// Parse storage budget from environment variable (supports k/m/g suffixes like Docker)
61+
size_t parse_storage_budget()
62+
{
63+
const char* env_val = std::getenv("BB_STORAGE_BUDGET");
64+
if (env_val == nullptr) {
65+
return std::numeric_limits<size_t>::max(); // No limit by default
66+
}
67+
68+
return parse_size_string(std::string(env_val));
69+
}
70+
} // namespace
71+
72+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
73+
size_t storage_budget = parse_storage_budget();
74+
75+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
76+
std::atomic<size_t> current_storage_usage{ 0 };
77+
78+
#endif // __wasm__

0 commit comments

Comments
 (0)