purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
bench_aggregate_nonce.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// Centralized arithmetic/proof baseline. This process holds every witness;
6// it implements no MPC protocol, distributed signing, or network transport.
7
8#include <array>
9#include <charconv>
10#include <chrono>
11#include <cstddef>
12#include <iomanip>
13#include <iostream>
14#include <optional>
15#include <span>
16#include <string_view>
17#include <utility>
18#include <vector>
19
20#include "purify.hpp"
21#include "purify/bppp.hpp"
22
23namespace {
24
25using Clock = std::chrono::steady_clock;
26
27double elapsed_ms(Clock::time_point start) {
28 return std::chrono::duration<double, std::milli>(Clock::now() - start).count();
29}
30
31void usage(std::ostream& output, std::string_view executable) {
32 output << "Usage: " << executable << " --members N\n"
33 << " N must be between 1 and 100. Each run creates one proof.\n"
34 << " Centralized imported arithmetic-proof baseline, not MPC.\n";
35}
36
37bool parse_members(std::string_view text, std::size_t& members) {
38 const auto parsed = std::from_chars(text.data(), text.data() + text.size(), members);
39 return parsed.ec == std::errc() && parsed.ptr == text.data() + text.size()
40 && members >= 1 && members <= 100;
41}
42
43template <typename T>
44bool succeeded(const purify::Result<T>& result, std::string_view stage) {
45 if (result.has_value()) return true;
46 std::cerr << "Failed at " << stage << ": " << result.error().name() << '\n';
47 return false;
48}
49
50struct ProverNonce {
52
53 ~ProverNonce() {
54 purify::detail::secure_clear_bytes(bytes.data(), bytes.size());
55 }
56};
57
58int benchmark(std::size_t members) {
59 std::cerr << "CENTRALIZED BASELINE: all witnesses are held by one process; no MPC.\n"
60 << "Backend: imported legacy Bulletproof arithmetic circuit; members="
61 << members << '\n';
62
63 const auto start = Clock::now();
64 auto context = purify::make_secp_context();
65 if (context == nullptr) {
66 std::cerr << "Failed to create secp context\n";
67 return 1;
68 }
69
70 const auto topic = purify::bytes_from_ascii("purify.aggregate-nonce.benchmark.v1");
71 auto stage = Clock::now();
72 auto topic_cache = purify::puresign::TopicProofCache::build(topic);
73 if (!succeeded(topic_cache, "topic template")) return 1;
74 const double template_ms = elapsed_ms(stage);
75
76 std::vector<purify::GeneratedKey> keys;
77 keys.reserve(members);
78 stage = Clock::now();
79 for (std::size_t i = 0; i < members; ++i) {
80 auto key = purify::generate_key();
81 if (!succeeded(key, "member key generation")) return 1;
82 keys.push_back(std::move(*key));
83 }
84 const double keygen_ms = elapsed_ms(stage);
85
86 std::vector<purify::UInt512> hosts;
87 std::vector<purify::BulletproofAssignmentData> assignments;
88 hosts.reserve(members);
89 assignments.reserve(members);
90 stage = Clock::now();
91 for (const auto& key : keys) {
92 auto witness = purify::prove_assignment_data(topic_cache->eval_input, key.secret);
93 if (!succeeded(witness, "member witness generation")) return 1;
94 hosts.push_back(witness->public_key);
95 assignments.push_back(std::move(witness->assignment));
96 }
97 const double witness_ms = elapsed_ms(stage);
98 keys.clear();
99
100 stage = Clock::now();
101 auto circuit = topic_cache->circuit_template.instantiate_sum(hosts);
102 if (!succeeded(circuit, "aggregate circuit construction")) return 1;
103 const double circuit_ms = elapsed_ms(stage);
104
105 stage = Clock::now();
106 auto assignment = topic_cache->circuit_template.sum_assignments(hosts, assignments);
107 if (!succeeded(assignment, "aggregate witness validation and composition")) return 1;
108 const double composition_ms = elapsed_ms(stage);
109 // Composition has copied the local witnesses. Release those duplicate
110 // columns before measuring proof generation at the target circuit size.
111 assignments.clear();
112
113 const auto value_generator = purify::bppp::base_generator(context.get());
114 const auto binding = purify::bytes_from_ascii("Purify/AggregateNonce/Benchmark/Arithmetic/V1");
115 ProverNonce nonce;
116 const auto entropy = purify::fill_secure_random(nonce.bytes);
117 if (!succeeded(entropy, "private prover randomness")) return 1;
118
120 std::cerr << "Generating aggregate proof\n";
121 stage = Clock::now();
123 *circuit, *assignment, nonce.bytes, value_generator, context.get(),
124 binding, {}, &prover_cache);
125 if (!succeeded(proof, "aggregate proof generation")) return 1;
126 const double prove_ms = elapsed_ms(stage);
127 prover_cache.clear();
128
129 stage = Clock::now();
130 auto serialized = proof->serialize();
131 if (!succeeded(serialized, "proof serialization")) return 1;
132 auto parsed = purify::ExperimentalBulletproofProof::deserialize(*serialized);
133 if (!succeeded(parsed, "proof deserialization")) return 1;
134 const double serialization_ms = elapsed_ms(stage);
135
136 // A distinct cache makes verification include cold verifier-resource setup.
138 std::cerr << "Verifying serialized aggregate proof\n";
139 stage = Clock::now();
141 *circuit, *parsed, value_generator, context.get(), binding, &verifier_cache);
142 if (!succeeded(verified, "aggregate proof verification")) return 1;
143 if (!*verified) {
144 std::cerr << "Aggregate proof verification rejected the generated proof\n";
145 return 1;
146 }
147 const double verify_ms = elapsed_ms(stage);
148
149 std::cout << std::fixed << std::setprecision(3)
150 << "{\n"
151 << " \"backend\": \"imported_legacy_arithmetic\",\n"
152 << " \"centralized\": true,\n"
153 << " \"mpc\": false,\n"
154 << " \"cold_prover_and_verifier_resources\": true,\n"
155 << " \"members\": " << members << ",\n"
156 << " \"gates\": " << circuit->n_gates << ",\n"
157 << " \"constraints\": " << circuit->c.size() << ",\n"
158 << " \"public_commitments\": " << circuit->n_commitments << ",\n"
159 << " \"raw_proof_bytes\": " << proof->proof.size() << ",\n"
160 << " \"commitment_bytes\": " << (proof->commitments.size() * 33) << ",\n"
161 << " \"framed_proof_bytes\": " << serialized->size() << ",\n"
162 << " \"template_ms\": " << template_ms << ",\n"
163 << " \"keygen_ms\": " << keygen_ms << ",\n"
164 << " \"witness_ms\": " << witness_ms << ",\n"
165 << " \"circuit_ms\": " << circuit_ms << ",\n"
166 << " \"composition_ms\": " << composition_ms << ",\n"
167 << " \"prove_ms\": " << prove_ms << ",\n"
168 << " \"serialization_ms\": " << serialization_ms << ",\n"
169 << " \"verify_ms\": " << verify_ms << ",\n"
170 << " \"total_ms\": " << elapsed_ms(start) << ",\n"
171 << " \"verified\": true\n"
172 << "}\n";
173 return 0;
174}
175
176} // namespace
177
178int main(int argc, char** argv) {
179 if (argc == 2 && std::string_view(argv[1]) == "--help") {
180 usage(std::cout, argv[0]);
181 return 0;
182 }
183 std::size_t members = 0;
184 if (argc != 3 || std::string_view(argv[1]) != "--members" || !parse_members(argv[2], members)) {
185 usage(std::cerr, argv[0]);
186 return 1;
187 }
188 return benchmark(members);
189}
int main(int argc, char **argv)
C++ wrappers for the BPPP functionality used by Purify.
bool has_value() const noexcept
Definition expected.hpp:170
Caller-owned cache for reusable legacy Bulletproof backend resources keyed by gate count.
GeneratorBytes base_generator(purify_secp_context *secp_context)
Returns the serialized secp256k1 base generator used as the blind generator.
Definition bppp.cpp:1702
void secure_clear_bytes(void *data, std::size_t size) noexcept
Definition secret.hpp:22
Result< ExperimentalBulletproofProof > prove_experimental_circuit(const NativeBulletproofCircuit &circuit, const BulletproofAssignmentData &assignment, const BulletproofScalarBytes &nonce, const BulletproofGeneratorBytes &value_generator, purify_secp_context *secp_context, std::span< const unsigned char > statement_binding={}, std::span< const BulletproofScalarBytes > blinds={}, ExperimentalBulletproofBackendCache *backend_cache=nullptr)
Proves a native circuit with the experimental imported Bulletproof circuit backend.
Result< BulletproofWitnessData > prove_assignment_data(const Bytes &message, const SecretKey &secret)
Computes the native Purify witness for a message and secret.
Definition api.cpp:243
Result< GeneratedKey > generate_key()
Generates a random Purify keypair using the built-in OS RNG.
Definition api.cpp:117
Result< bool > verify_experimental_circuit(const NativeBulletproofCircuit &circuit, const ExperimentalBulletproofProof &proof, const BulletproofGeneratorBytes &value_generator, purify_secp_context *secp_context, std::span< const unsigned char > statement_binding={}, ExperimentalBulletproofBackendCache *backend_cache=nullptr)
Verifies a proof produced by prove_experimental_circuit against the same native circuit and ordered o...
Bytes bytes_from_ascii(std::string_view input)
Encodes an ASCII string as a byte vector.
Definition curve.cpp:168
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
std::array< unsigned char, 32 > BulletproofScalarBytes
Status fill_secure_random(std::span< unsigned char > bytes) noexcept
Fills a buffer with operating-system randomness.
Definition api.cpp:108
Umbrella header that re-exports the public Purify API.
constexpr std::string_view name() const noexcept
Definition error.hpp:310
static Result< ExperimentalBulletproofProof > deserialize(std::span< const unsigned char > bytes)
static Result< TopicProofCache > build(std::span< const unsigned char > topic)
Builds a reusable verifier template for one exact topic.
Definition legacy.cpp:798