purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
bench_norm_accumulation.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Judica, Inc.
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or https://opensource.org/license/mit/.
4
5// Public accumulation of synthetic standalone norm proofs. This is neither a
6// construction-receipt proof nor a benchmark of the experimental circuit layer.
7
8#include <charconv>
9#include <chrono>
10#include <iomanip>
11#include <iostream>
12#include <string_view>
13
15
16namespace {
17
18using Clock = std::chrono::steady_clock;
21namespace accumulation = purify::bppp::accumulation;
22
23struct Options {
24 std::size_t claims = 0;
25 std::size_t norm_size = 0;
26 std::size_t linear_size = 0;
27};
28
29void usage(std::ostream& out, const char* executable) {
30 out << "Usage: " << executable << " --claims N --norm-size G --linear-size H\n"
31 << " 1 <= N <= 1000; G is a power of two <= 32768; H is a power of two <= G.\n"
32 << " Synthetic standalone norm proofs with different public c-vectors.\n"
33 << " Original inputs and authenticated accumulation transitions remain required.\n";
34}
35
36bool parse_options(int argc, char** argv, Options& options) {
37 for (int i = 1; i < argc; ++i) {
38 const std::string_view option(argv[i]);
39 std::size_t* target = nullptr;
40 if (option == "--claims") target = &options.claims;
41 if (option == "--norm-size") target = &options.norm_size;
42 if (option == "--linear-size") target = &options.linear_size;
43 if (target == nullptr || *target != 0 || i + 1 == argc) return false;
44 const std::string_view value(argv[++i]);
45 const auto parsed = std::from_chars(value.data(), value.data() + value.size(), *target);
46 if (parsed.ec != std::errc() || parsed.ptr != value.data() + value.size() || *target == 0) {
47 return false;
48 }
49 }
50 return options.claims <= 1000 && options.claims != 0 && options.norm_size <= 32768 &&
51 purify::is_power_of_two_size(options.norm_size) &&
52 purify::is_power_of_two_size(options.linear_size) && options.linear_size <= options.norm_size;
53}
54
55double elapsed_ms(Clock::time_point start) {
56 return std::chrono::duration<double, std::milli>(Clock::now() - start).count();
57}
58
59template <typename T>
60bool succeeded(const purify::Result<T>& result, std::string_view stage) {
61 if (result.has_value()) return true;
62 std::cerr << "Failed at " << stage << ": " << result.error().name() << '\n';
63 return false;
64}
65
66struct PrivateWitness {
67 purify::bppp::NormArgInputs inputs;
68
69 ~PrivateWitness() {
70 purify::detail::secure_clear_bytes(inputs.n_vec.data(), inputs.n_vec.size() * sizeof(ScalarBytes));
71 purify::detail::secure_clear_bytes(inputs.l_vec.data(), inputs.l_vec.size() * sizeof(ScalarBytes));
72 }
73};
74
75purify::Status sample_witness(std::vector<ScalarBytes>& values) {
76 // Dimensions are bounded by the CLI. One bulk RNG call avoids measuring
77 // a system-call-per-coordinate workload instead of the proof protocol.
79 reinterpret_cast<unsigned char*>(values.data()), values.size() * sizeof(ScalarBytes))),
80 "benchmark:random_witness");
81 for (auto& value : values) {
82 for (;;) {
83 const auto parsed = FieldElement::try_from_bytes32(value);
84 if (parsed && !parsed->is_zero()) break;
85 PURIFY_RETURN_IF_ERROR(purify::fill_secure_random(value), "benchmark:resample_witness");
86 }
87 }
88 return {};
89}
90
91// The generator stream is shared through Basis. Every leaf retains its own
92// canonical public coefficient vector for authenticated statement replay.
93struct PublishedNorm {
94 ScalarBytes rho;
95 purify::bppp::PointBytes commitment;
96 std::vector<ScalarBytes> c_vec;
97 purify::Bytes proof;
98};
99
100purify::Result<PublishedNorm> publish_norm(const accumulation::Basis& basis, std::size_t index,
101 purify_secp_context* context) {
102 PrivateWitness witness;
103 witness.inputs.rho = FieldElement::from_u64(index + 3).to_bytes_be();
104 witness.inputs.generators = basis.norm_generators();
105 witness.inputs.c_vec.reserve(basis.l_vec_len());
106 for (std::size_t i = 0; i < basis.l_vec_len(); ++i) {
107 witness.inputs.c_vec.push_back(
108 FieldElement::from_u64(17 + index * basis.l_vec_len() + i).to_bytes_be());
109 }
110 witness.inputs.n_vec.resize(basis.n_vec_len());
111 witness.inputs.l_vec.resize(basis.l_vec_len());
112 PURIFY_RETURN_IF_ERROR(sample_witness(witness.inputs.n_vec), "benchmark:n_witness");
113 PURIFY_RETURN_IF_ERROR(sample_witness(witness.inputs.l_vec), "benchmark:l_witness");
114 PURIFY_ASSIGN_OR_RETURN(auto published, purify::bppp::prove_norm_arg(witness.inputs, context),
115 "benchmark:publish_norm");
116 // The private input vectors are erased as this function exits. Only the
117 // completed public norm proof escapes into the subsequent public stages.
118 return PublishedNorm{published.rho, published.commitment,
119 std::move(published.c_vec), std::move(published.proof)};
120}
121
122int benchmark(const Options& options) {
123 auto context = purify::make_secp_context();
124 if (!context) {
125 std::cerr << "Could not create secp256k1 context\n";
126 return 1;
127 }
128 auto stage = Clock::now();
129 auto basis = accumulation::Basis::create(options.norm_size, options.linear_size,
130 "Purify/Benchmark/StandaloneNorm/V1", context.get());
131 if (!succeeded(basis, "common deterministic basis")) return 1;
132 const double basis_ms = elapsed_ms(stage);
133
134 stage = Clock::now();
135 std::vector<PublishedNorm> published;
136 published.reserve(options.claims);
137 std::size_t child_proof_bytes = 0;
138 std::size_t public_leaf_record_bytes = 0;
139 std::size_t coefficient_bytes = 0;
140 for (std::size_t i = 0; i < options.claims; ++i) {
141 auto leaf = publish_norm(*basis, i, context.get());
142 if (!succeeded(leaf, "independent leaf publication")) return 1;
143 child_proof_bytes += leaf->proof.size();
144 const auto c_bytes = leaf->c_vec.size() * sizeof(ScalarBytes);
145 coefficient_bytes += c_bytes;
146 public_leaf_record_bytes += leaf->rho.size() + leaf->commitment.size() + c_bytes + leaf->proof.size();
147 published.push_back(std::move(*leaf));
148 if ((i + 1) % 10 == 0 || i + 1 == options.claims) {
149 std::cerr << "Published " << i + 1 << '/' << options.claims << " standalone norm proofs\n";
150 }
151 }
152 const double leaf_publication_ms = elapsed_ms(stage);
153
154 std::vector<accumulation::PendingClaim> claims;
155 claims.reserve(published.size());
156 std::vector<purify::Bytes> reduction_transcripts;
157 reduction_transcripts.reserve(published.size());
158 purify::bppp::NormArgProof public_input;
159 public_input.n_vec_len = basis->n_vec_len();
160 public_input.generators = basis->norm_generators();
161 std::size_t polynomial_terms = 0;
162 std::size_t polynomial_scalars = 0;
163 std::size_t coefficient_commitment_bytes = 0;
164 std::size_t reduction_proof_bytes = 0;
165 double prepare_ms = 0;
166 double prove_reduction_ms = 0;
167 double reduction_codec_ms = 0;
168 double verify_reduction_ms = 0;
169 for (const auto& leaf : published) {
170 stage = Clock::now();
171 public_input.rho = leaf.rho;
172 public_input.commitment = leaf.commitment;
173 public_input.c_vec = leaf.c_vec;
174 public_input.proof = leaf.proof;
175 auto prepared = accumulation::prepare_norm(*basis, public_input, context.get());
176 if (!succeeded(prepared, "public norm preparation and coefficient authentication")) return 1;
177 prepare_ms += elapsed_ms(stage);
178 coefficient_commitment_bytes += prepared->coefficient_commitment().size();
179
180 stage = Clock::now();
181 auto reduction = accumulation::prove_norm_reduction(*basis, *prepared, context.get());
182 if (!succeeded(reduction, "public coefficient opening")) return 1;
183 prove_reduction_ms += elapsed_ms(stage);
184
185 stage = Clock::now();
186 auto encoded_reduction = accumulation::serialize_norm_reduction(*basis, *reduction, context.get());
187 if (!succeeded(encoded_reduction, "norm reduction serialization")) return 1;
188 auto parsed_reduction = accumulation::parse_norm_reduction(*basis, *encoded_reduction, context.get());
189 if (!succeeded(parsed_reduction, "norm reduction parsing")) return 1;
190 reduction_codec_ms += elapsed_ms(stage);
191
192 stage = Clock::now();
193 auto claim = accumulation::verify_norm_reduction(*basis, *prepared, *parsed_reduction, context.get());
194 if (!succeeded(claim, "short norm reduction verification")) return 1;
195 verify_reduction_ms += elapsed_ms(stage);
196 polynomial_terms += claim->polynomial().size();
197 for (const auto& term : claim->polynomial()) polynomial_scalars += 1 + term.factors.size();
198 claims.push_back(std::move(*claim));
199 reduction_proof_bytes += encoded_reduction->size();
200 reduction_transcripts.push_back(std::move(*encoded_reduction));
201 if (reduction_transcripts.size() % 10 == 0 || reduction_transcripts.size() == published.size()) {
202 std::cerr << "Verified " << reduction_transcripts.size() << '/' << published.size()
203 << " public norm reductions\n";
204 }
205 }
206
207 stage = Clock::now();
208 auto proof = accumulation::prove_step(*basis, claims, context.get());
209 if (!succeeded(proof, "public accumulation proving")) return 1;
210 const double prove_step_ms = elapsed_ms(stage);
211
212 stage = Clock::now();
213 auto encoded = accumulation::serialize_step(*basis, *proof, context.get());
214 if (!succeeded(encoded, "canonical step serialization")) return 1;
215 auto parsed = accumulation::parse_step(*basis, *encoded, context.get());
216 if (!succeeded(parsed, "canonical step parsing")) return 1;
217 const double codec_ms = elapsed_ms(stage);
218
219 stage = Clock::now();
220 auto pending = accumulation::verify_step(*basis, claims, *parsed, context.get());
221 if (!succeeded(pending, "short accumulation verification")) return 1;
222 const double verify_step_ms = elapsed_ms(stage);
223
224 stage = Clock::now();
225 auto decided = accumulation::decide(*basis, *pending, context.get());
226 if (!succeeded(decided, "mandatory final decider")) return 1;
227 const double decide_ms = elapsed_ms(stage);
228
229 std::cout << std::fixed << std::setprecision(3)
230 << "{\n"
231 << " \"scope\": \"synthetic_standalone_norm_accumulation\",\n"
232 << " \"common_c_vector_required\": false,\n"
233 << " \"private_witnesses_needed_for_accumulation\": false,\n"
234 << " \"original_inputs_and_authenticated_transitions_required\": true,\n"
235 << " \"compressed_construction_receipt\": false,\n"
236 << " \"claims\": " << options.claims << ",\n"
237 << " \"norm_size\": " << options.norm_size << ",\n"
238 << " \"linear_size\": " << options.linear_size << ",\n"
239 << " \"basis_dimension\": " << basis->dimension() << ",\n"
240 << " \"step_proof_bytes\": " << encoded->size() << ",\n"
241 << " \"child_norm_proof_bytes\": " << child_proof_bytes << ",\n"
242 << " \"retained_public_leaf_record_bytes\": " << public_leaf_record_bytes << ",\n"
243 << " \"retained_public_coefficient_bytes\": " << coefficient_bytes << ",\n"
244 << " \"derived_coefficient_commitment_bytes\": " << coefficient_commitment_bytes << ",\n"
245 << " \"retained_coefficient_opening_proof_bytes\": " << reduction_proof_bytes << ",\n"
246 << " \"retained_coefficient_opening_count\": " << reduction_transcripts.size() << ",\n"
247 << " \"public_claim_descriptor_count\": " << claims.size() << ",\n"
248 << " \"public_polynomial_term_count\": " << polynomial_terms << ",\n"
249 << " \"public_polynomial_scalar_count\": " << polynomial_scalars << ",\n"
250 << " \"timings_ms\": {\n"
251 << " \"basis\": " << basis_ms << ",\n"
252 << " \"leaf_publication\": " << leaf_publication_ms << ",\n"
253 << " \"prepare_and_authenticate_coefficients\": " << prepare_ms << ",\n"
254 << " \"prove_norm_reductions\": " << prove_reduction_ms << ",\n"
255 << " \"reduction_codec_roundtrip\": " << reduction_codec_ms << ",\n"
256 << " \"verify_norm_reductions\": " << verify_reduction_ms << ",\n"
257 << " \"prove_step\": " << prove_step_ms << ",\n"
258 << " \"codec_roundtrip\": " << codec_ms << ",\n"
259 << " \"verify_step\": " << verify_step_ms << ",\n"
260 << " \"decide\": " << decide_ms << "\n"
261 << " }\n}\n";
262 return 0;
263}
264
265} // namespace
266
267int main(int argc, char** argv) {
268 if (argc == 2 && std::string_view(argv[1]) == "--help") {
269 usage(std::cout, argv[0]);
270 return 0;
271 }
272 Options options;
273 if (!parse_options(argc, argv, options)) {
274 usage(std::cerr, argv[0]);
275 return 1;
276 }
277 return benchmark(options);
278}
int main(int argc, char **argv)
bool has_value() const noexcept
Definition expected.hpp:170
Field element modulo the backend scalar field used by this implementation.
Definition numeric.hpp:834
#define PURIFY_RETURN_IF_ERROR(expr, context)
Evaluates an expected-like expression and returns the wrapped error on failure.
Definition error.hpp:329
#define PURIFY_ASSIGN_OR_RETURN(lhs, expr, context)
Evaluates an expected-like expression, binds the value to lhs, and propagates errors.
Definition error.hpp:338
Result< NormArgProof > prove_norm_arg(const NormArgInputs &inputs, purify_secp_context *secp_context)
Produces a standalone BPPP norm argument.
Definition bppp.cpp:1818
std::array< unsigned char, 33 > PointBytes
Compressed 33-byte curve-point encoding.
Definition bppp.hpp:32
std::array< unsigned char, 32 > ScalarBytes
Big-endian 32-byte scalar encoding.
Definition bppp.hpp:30
void secure_clear_bytes(void *data, std::size_t size) noexcept
Definition secret.hpp:22
bool is_power_of_two_size(std::size_t value) noexcept
Definition common.hpp:94
SecpContextPtr make_secp_context() noexcept
Definition common.hpp:52
Expected< T, Error > Result
Expected-returning convenience alias for Purify value-producing APIs.
Definition error.hpp:99
Expected< void, Error > Status
Expected-returning convenience alias for Purify status-only APIs.
Definition error.hpp:102
std::vector< unsigned char > Bytes
Dynamically sized byte string used for messages, serialized witnesses, and proofs.
Definition common.hpp:99
Status fill_secure_random(std::span< unsigned char > bytes) noexcept
Fills a buffer with operating-system randomness.
Definition api.cpp:108
constexpr std::string_view name() const noexcept
Definition error.hpp:310
std::vector< ScalarBytes > l_vec
Definition bppp.hpp:110
std::vector< PointBytes > generators
Definition bppp.hpp:108
std::vector< ScalarBytes > n_vec
Definition bppp.hpp:109
std::vector< ScalarBytes > c_vec
Definition bppp.hpp:111
Standalone BPPP norm-argument proof bundle with all verifier-side inputs.
Definition bppp.hpp:118
std::vector< PointBytes > generators
Definition bppp.hpp:120
std::vector< ScalarBytes > c_vec
Definition bppp.hpp:121
PointBytes commitment
Finite compressed initial point; infinity is not accepted.
Definition bppp.hpp:124