purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
bppp.hpp
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
9
10#pragma once
11
12#include <array>
13#include <cstddef>
14#include <memory>
15#include <span>
16#include <utility>
17#include <vector>
18
19#include "purify/api.hpp"
20
22
23namespace purify::bppp {
24
25namespace detail {
27}
28
30using ScalarBytes = std::array<unsigned char, 32>;
32using PointBytes = std::array<unsigned char, 33>;
34using GeneratorBytes = std::array<unsigned char, 33>;
35
46
55 const ScalarBytes& value,
56 purify_secp_context* secp_context);
57
68 const ScalarBytes& value,
69 purify_secp_context* secp_context,
70 const GeneratorBytes& value_gen,
71 const GeneratorBytes& blind_gen);
72
79 ScalarBytes out{};
82 return out;
83}
84
90inline std::vector<ScalarBytes> scalar_bytes(
91 std::span<const FieldElement> values) {
92 std::vector<ScalarBytes> out(values.size());
93 for (std::size_t i = 0; i < values.size(); ++i) {
95 out[i].data(),
97 }
98 return out;
99}
100
108 std::vector<PointBytes> generators;
109 std::vector<ScalarBytes> n_vec;
110 std::vector<ScalarBytes> l_vec;
111 std::vector<ScalarBytes> c_vec;
112};
113
116
120 std::vector<PointBytes> generators;
121 std::vector<ScalarBytes> c_vec;
122 std::size_t n_vec_len = 0;
126};
127
130public:
136
137protected:
139
140private:
142
143 [[nodiscard]] virtual std::shared_ptr<const void> find_public_data_impl(
144 const std::array<unsigned char, 32>& key) const = 0;
145 virtual void insert_public_data_impl(std::array<unsigned char, 32> key,
146 std::shared_ptr<const void> value) = 0;
147 [[nodiscard]] virtual purify_bppp_backend_resources* get_or_create_backend_resources_impl(
148 std::span<const PointBytes> generators,
149 purify_secp_context* secp_context) = 0;
150};
151
153namespace detail {
155 [[nodiscard]] static std::shared_ptr<const void> find_public_data(
156 const ExperimentalCircuitBackend* backend,
157 const std::array<unsigned char, 32>& key);
159 std::array<unsigned char, 32> key,
160 std::shared_ptr<const void> value);
163 std::span<const PointBytes> generators,
164 purify_secp_context* secp_context);
165};
166} // namespace detail
167
170public:
177
178 [[nodiscard]] bool empty() const noexcept;
179
180private:
181 struct Impl;
182 std::unique_ptr<Impl> impl_;
183
184 [[nodiscard]] std::shared_ptr<const void> find_public_data_impl(
185 const std::array<unsigned char, 32>& key) const override;
186 void insert_public_data_impl(std::array<unsigned char, 32> key,
187 std::shared_ptr<const void> value) override;
188 [[nodiscard]] purify_bppp_backend_resources* get_or_create_backend_resources_impl(
189 std::span<const PointBytes> generators,
190 purify_secp_context* secp_context) override;
191
193};
194
197public:
204
205 void clear();
206 [[nodiscard]] std::size_t size() const noexcept;
218 std::span<const PointBytes> generators) const;
220 [[nodiscard]] std::shared_ptr<const void> find_public_data(const std::array<unsigned char, 32>& key) const;
222 void insert_public_data(std::array<unsigned char, 32> key, std::shared_ptr<const void> value);
225 std::span<const PointBytes> generators,
226 purify_secp_context* secp_context);
227
228private:
229 struct Impl;
230 std::unique_ptr<Impl> impl_;
231
232 [[nodiscard]] std::shared_ptr<const void> find_public_data_impl(
233 const std::array<unsigned char, 32>& key) const override;
234 void insert_public_data_impl(std::array<unsigned char, 32> key,
235 std::shared_ptr<const void> value) override;
236 [[nodiscard]] purify_bppp_backend_resources* get_or_create_backend_resources_impl(
237 std::span<const PointBytes> generators,
238 purify_secp_context* secp_context) override;
239};
240
251 const PointBytes& commitment,
252 purify_secp_context* secp_context);
259bool verify_norm_arg(const NormArgProof& proof, purify_secp_context* secp_context);
260
266
279
287 const NativeBulletproofCircuit& circuit,
288 const BulletproofAssignmentData& assignment,
289 purify_secp_context* secp_context,
290 ExperimentalCircuitBackend* cache = nullptr);
291
300 const NativeBulletproofCircuit& circuit,
301 const BulletproofAssignmentData& assignment,
302 purify_secp_context* secp_context,
303 std::span<const unsigned char> statement_binding = {},
304 ExperimentalCircuitBackend* cache = nullptr);
305
314Result<ExperimentalCircuitNormArgProof> prove_experimental_circuit_norm_arg_to_commitment(
315 const NativeBulletproofCircuit& circuit,
316 const BulletproofAssignmentData& assignment,
317 const PointBytes& witness_commitment,
318 purify_secp_context* secp_context,
319 std::span<const unsigned char> statement_binding = {},
320 ExperimentalCircuitBackend* cache = nullptr);
321
329Result<bool> verify_experimental_circuit_norm_arg(
330 const NativeBulletproofCircuit& circuit,
331 const ExperimentalCircuitNormArgProof& proof,
332 purify_secp_context* secp_context,
333 std::span<const unsigned char> statement_binding = {},
334 ExperimentalCircuitBackend* cache = nullptr);
335
358Result<ExperimentalCircuitZkNormArgProof> prove_experimental_circuit_zk_norm_arg(
359 const NativeBulletproofCircuit& circuit,
360 const BulletproofAssignmentData& assignment,
361 const ScalarBytes& secret_prover_seed,
362 purify_secp_context* secp_context,
363 std::span<const unsigned char> statement_binding = {},
364 ExperimentalCircuitBackend* cache = nullptr);
365
374 const NativeBulletproofCircuit& circuit,
375 const ExperimentalCircuitZkNormArgProof& proof,
376 purify_secp_context* secp_context,
377 std::span<const unsigned char> statement_binding = {},
378 ExperimentalCircuitBackend* cache = nullptr);
379
395Result<ExperimentalCircuitZkNormArgProof> prove_experimental_circuit_zk_norm_arg_with_public_commitments(
396 const NativeBulletproofCircuit& circuit,
397 const BulletproofAssignmentData& assignment,
398 const ScalarBytes& secret_prover_seed,
399 std::span<const PointBytes> public_commitments,
400 purify_secp_context* secp_context,
401 std::span<const unsigned char> statement_binding = {},
402 ExperimentalCircuitBackend* cache = nullptr);
403
414 const NativeBulletproofCircuit& circuit,
415 const ExperimentalCircuitZkNormArgProof& proof,
416 std::span<const PointBytes> public_commitments,
417 purify_secp_context* secp_context,
418 std::span<const unsigned char> statement_binding = {},
419 ExperimentalCircuitBackend* cache = nullptr);
420
428
438 const ScalarBytes& blind,
439 purify_secp_context* secp_context);
440
452 const SecretKey& secret,
453 const ScalarBytes& blind,
454 purify_secp_context* secp_context,
455 const GeneratorBytes& value_gen,
456 const GeneratorBytes& blind_gen);
457
458} // namespace purify::bppp
High-level Purify key generation, evaluation, witness generation, and circuit helpers.
Field element modulo the backend scalar field used by this implementation.
Definition numeric.hpp:834
Move-only packed Purify secret stored in dedicated heap memory.
Definition secret.hpp:95
Common interface for reusable experimental BPPP backend state.
Definition bppp.hpp:129
ExperimentalCircuitBackend(const ExperimentalCircuitBackend &)=delete
ExperimentalCircuitBackend & operator=(const ExperimentalCircuitBackend &)=delete
ExperimentalCircuitBackend(ExperimentalCircuitBackend &&) noexcept=default
friend struct detail::ExperimentalCircuitBackendAccess
Definition bppp.hpp:141
Thread-local clone of one warmed experimental BPPP backend-resource line.
Definition bppp.hpp:169
ExperimentalCircuitCacheLine & operator=(const ExperimentalCircuitCacheLine &)=delete
ExperimentalCircuitCacheLine(const ExperimentalCircuitCacheLine &)=delete
ExperimentalCircuitCacheLine & operator=(ExperimentalCircuitCacheLine &&other) noexcept
ExperimentalCircuitCacheLine(ExperimentalCircuitCacheLine &&other) noexcept
ExperimentalCircuitCache & operator=(const ExperimentalCircuitCache &)=delete
ExperimentalCircuitCache(ExperimentalCircuitCache &&other) noexcept
Result< ExperimentalCircuitCacheLine > clone_line_for_thread(std::span< const PointBytes > generators) const
Clones one warmed backend-resource line for thread-local use.
Definition bppp.cpp:185
std::shared_ptr< const void > find_public_data(const std::array< unsigned char, 32 > &key) const
Looks up opaque cached reduction data by digest key.
Definition bppp.cpp:211
void insert_public_data(std::array< unsigned char, 32 > key, std::shared_ptr< const void > value)
Stores opaque cached reduction data by digest key.
Definition bppp.cpp:216
purify_bppp_backend_resources * get_or_create_backend_resources(std::span< const PointBytes > generators, purify_secp_context *secp_context)
Returns cached backend resources for this generator set, creating them on first use.
Definition bppp.cpp:223
ExperimentalCircuitCache & operator=(ExperimentalCircuitCache &&other) noexcept
std::size_t size() const noexcept
Definition bppp.cpp:179
ExperimentalCircuitCache(const ExperimentalCircuitCache &)=delete
Internal access helpers for experimental BPPP backend implementations.
Definition bppp.hpp:25
Result< NormArgProof > prove_norm_arg_to_commitment(const NormArgInputs &inputs, const PointBytes &commitment, purify_secp_context *secp_context)
Produces a standalone BPPP norm argument anchored to a caller-supplied public commitment.
Definition bppp.cpp:2083
Result< PointBytes > commit_norm_arg(const NormArgInputs &inputs, purify_secp_context *secp_context)
Computes the public BPPP commitment for a standalone norm-argument input bundle.
Definition bppp.cpp:2077
GeneratorBytes base_generator(purify_secp_context *secp_context)
Returns the serialized secp256k1 base generator used as the blind generator.
Definition bppp.cpp:1702
Result< PointBytes > commit_experimental_circuit_witness(const NativeBulletproofCircuit &circuit, const BulletproofAssignmentData &assignment, purify_secp_context *secp_context, ExperimentalCircuitBackend *cache=nullptr)
Commits to raw circuit coordinates before reduction challenges exist.
Definition bppp.cpp:2094
Result< bool > verify_experimental_circuit_zk_norm_arg_with_public_commitments(const NativeBulletproofCircuit &circuit, const ExperimentalCircuitZkNormArgProof &proof, std::span< const PointBytes > public_commitments, purify_secp_context *secp_context, std::span< const unsigned char > statement_binding={}, ExperimentalCircuitBackend *cache=nullptr)
Verifies an experimental masked circuit proof against explicit public commitment points.
Definition bppp.cpp:3827
Result< ExperimentalCircuitZkNormArgProof > prove_experimental_circuit_zk_norm_arg(const NativeBulletproofCircuit &circuit, const BulletproofAssignmentData &assignment, const ScalarBytes &secret_prover_seed, purify_secp_context *secp_context, std::span< const unsigned char > statement_binding={}, ExperimentalCircuitBackend *cache=nullptr)
Produces an experimental masked circuit proof over the reduced BPPP relation.
Definition bppp.cpp:3791
Result< ExperimentalCircuitZkNormArgProof > prove_experimental_circuit_zk_norm_arg_with_public_commitments(const NativeBulletproofCircuit &circuit, const BulletproofAssignmentData &assignment, const ScalarBytes &secret_prover_seed, std::span< const PointBytes > public_commitments, purify_secp_context *secp_context, std::span< const unsigned char > statement_binding={}, ExperimentalCircuitBackend *cache=nullptr)
Produces an experimental masked circuit proof bound to explicit public commitment points.
Definition bppp.cpp:3814
bool verify_norm_arg(const NormArgProof &proof, purify_secp_context *secp_context)
Verifies a standalone BPPP norm argument.
Definition bppp.cpp:2089
Result< ExperimentalCircuitNormArgProof > prove_experimental_circuit_norm_arg(const NativeBulletproofCircuit &circuit, const BulletproofAssignmentData &assignment, purify_secp_context *secp_context, std::span< const unsigned char > statement_binding={}, ExperimentalCircuitBackend *cache=nullptr)
Produces an anchored transparent circuit proof using the experimental circuit-to-BPPP reduction.
Definition bppp.cpp:2186
GeneratorBytes value_generator_h(purify_secp_context *secp_context)
Returns the serialized alternate generator used for committed values.
Definition bppp.cpp:1711
Result< CommittedPurifyWitness > commit_output_witness(const Bytes &message, const SecretKey &secret, const ScalarBytes &blind, purify_secp_context *secp_context)
Evaluates Purify, derives its witness, and commits to the output using Purify's default generators.
Definition bppp.cpp:3847
Result< NormArgProof > prove_norm_arg(const NormArgInputs &inputs, purify_secp_context *secp_context)
Produces a standalone BPPP norm argument.
Definition bppp.cpp:1818
Result< std::vector< PointBytes > > create_generators(std::size_t count, purify_secp_context *secp_context)
Expands the BPPP generator list.
Definition bppp.cpp:1721
std::array< unsigned char, 33 > PointBytes
Compressed 33-byte curve-point encoding.
Definition bppp.hpp:32
Result< bool > verify_experimental_circuit_zk_norm_arg(const NativeBulletproofCircuit &circuit, const ExperimentalCircuitZkNormArgProof &proof, purify_secp_context *secp_context, std::span< const unsigned char > statement_binding={}, ExperimentalCircuitBackend *cache=nullptr)
Verifies an experimental masked circuit proof produced by prove_experimental_circuit_zk_norm_arg.
Definition bppp.cpp:3803
std::array< unsigned char, 33 > GeneratorBytes
Serialized generator encoding used by the BPPP bridge.
Definition bppp.hpp:34
Result< PointBytes > pedersen_commit_char(const ScalarBytes &blind, const ScalarBytes &value, purify_secp_context *secp_context)
Computes a Pedersen commitment to an arbitrary 32-byte scalar value using Purify's default generators...
Definition bppp.cpp:1793
ScalarBytes scalar_bytes(const FieldElement &value)
Serializes a Purify field element into the scalar encoding expected by the BPPP bridge.
Definition bppp.hpp:78
std::array< unsigned char, 32 > ScalarBytes
Big-endian 32-byte scalar encoding.
Definition bppp.hpp:30
BigUInt< 8 > UInt512
512-bit unsigned integer used for private and packed public keys.
Definition numeric.hpp:821
Expected< T, Error > Result
Expected-returning convenience alias for Purify value-producing APIs.
Definition error.hpp:99
std::vector< unsigned char > Bytes
Dynamically sized byte string used for messages, serialized witnesses, and proofs.
Definition common.hpp:99
void purify_scalar_get_b32(unsigned char output32[32], const purify_scalar *value)
Serializes a scalar as 32 big-endian bytes.
struct purify_secp_context purify_secp_context
Caller-owned reusable secp256k1 context handle shared across Purify C and C++ APIs.
Columnar witness assignment compatible with the native Bulletproof circuit layout.
Native in-memory representation of a Bulletproof-style arithmetic circuit.
Purify witness bundle together with a Pedersen commitment to the output.
Definition bppp.hpp:422
BulletproofAssignmentData assignment
Definition bppp.hpp:425
Experimental transparent circuit proof backed by the standalone BPPP norm argument.
Definition bppp.hpp:262
Masked circuit proof with a pre-challenge raw-coordinate commitment.
Definition bppp.hpp:268
std::array< unsigned char, 32 > public_commitment_dlog_nonce
Even-Y Schnorr nonce x-coordinate for the batched public-commitment DLog proof.
Definition bppp.hpp:274
PointBytes a_commitment
Raw-coordinate commitment made before reduction challenges.
Definition bppp.hpp:270
ScalarBytes public_commitment_dlog_response
Schnorr response for the batched public-commitment DLog proof.
Definition bppp.hpp:276
Inputs for norm commitment computation and standalone BPPP proofs. Proving requires power-of-two vect...
Definition bppp.hpp:106
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
static purify_bppp_backend_resources * get_or_create_backend_resources(ExperimentalCircuitBackend *backend, std::span< const PointBytes > generators, purify_secp_context *secp_context)
Definition bppp.cpp:94
static std::shared_ptr< const void > find_public_data(const ExperimentalCircuitBackend *backend, const std::array< unsigned char, 32 > &key)
Definition bppp.cpp:78
static void insert_public_data(ExperimentalCircuitBackend *backend, std::array< unsigned char, 32 > key, std::shared_ptr< const void > value)
Definition bppp.cpp:85
static const purify_scalar & raw(const FieldElement &value) noexcept
Definition numeric.hpp:940