purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
nonce_batch.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
6
7#include <bit>
8#include <limits>
9#include <utility>
10
11namespace purify {
12namespace {
13
14Result<std::size_t> checked_sum(std::size_t lhs, std::size_t rhs) {
15 if (rhs > std::numeric_limits<std::size_t>::max() - lhs) {
16 return unexpected_error(ErrorCode::Overflow, "nonce_batch:dimension_sum");
17 }
18 return lhs + rhs;
19}
20
21Result<std::size_t> padded_gates(std::size_t gates) {
22 constexpr std::size_t max_power =
23 std::size_t{1} << (std::numeric_limits<std::size_t>::digits - 1);
24 if (gates == 0 || gates > max_power) {
25 return unexpected_error(ErrorCode::Overflow, "nonce_batch:padded_gates");
26 }
27 return std::bit_ceil(gates);
28}
29
30void append_rows(std::vector<NativeBulletproofCircuitRow>& destination,
31 std::vector<NativeBulletproofCircuitRow>& source,
32 std::size_t constraint_offset) {
33 for (auto& row : source) {
34 for (auto& term : row.entries) {
35 term.idx += constraint_offset;
36 }
37 destination.push_back(std::move(row));
38 }
39}
40
41} // namespace
42
44 std::span<const NonceBatchGroup> groups) {
45 if (groups.empty()) {
46 return unexpected_error(ErrorCode::InvalidDimensions, "nonce_batch_circuit:empty");
47 }
48
49 NativeBulletproofCircuit result(0, groups.size(), 0);
50 std::size_t group_index = 0;
51 for (const auto& group : groups) {
52 PURIFY_ASSIGN_OR_RETURN(auto circuit_template, verifier_circuit_template(group.evaluation_input),
53 "nonce_batch_circuit:template");
54 PURIFY_ASSIGN_OR_RETURN(auto circuit, circuit_template.instantiate_sum(group.hosts),
55 "nonce_batch_circuit:group");
56 PURIFY_ASSIGN_OR_RETURN(const auto next_gates, checked_sum(result.n_gates, circuit.n_gates),
57 "nonce_batch_circuit:gates");
58 PURIFY_RETURN_IF_ERROR(checked_sum(result.c.size(), circuit.c.size()),
59 "nonce_batch_circuit:constraints");
60 const std::size_t constraint_offset = result.c.size();
61 result.c.insert(result.c.end(), circuit.c.begin(), circuit.c.end());
62 append_rows(result.wl, circuit.wl, constraint_offset);
63 append_rows(result.wr, circuit.wr, constraint_offset);
64 append_rows(result.wo, circuit.wo, constraint_offset);
65 for (const auto& term : circuit.wv[0].entries) {
66 result.wv[group_index].add(constraint_offset + term.idx, term.scalar);
67 }
68 result.n_gates = next_gates;
69 ++group_index;
70 }
71
72 PURIFY_ASSIGN_OR_RETURN(const auto gates, padded_gates(result.n_gates),
73 "nonce_batch_circuit:padding");
74 result.n_gates = gates;
75 result.wl.resize(gates);
76 result.wr.resize(gates);
77 result.wo.resize(gates);
78 return result;
79}
80
82 std::span<const NonceBatchGroup> groups,
83 std::span<const std::vector<BulletproofAssignmentData>> assignments) {
84 if (groups.empty()) {
85 return unexpected_error(ErrorCode::InvalidDimensions, "nonce_batch_assignments:empty");
86 }
87 if (groups.size() != assignments.size()) {
88 return unexpected_error(ErrorCode::SizeMismatch, "nonce_batch_assignments:groups");
89 }
90
92 result.commitments.reserve(groups.size());
93 for (std::size_t group_index = 0; group_index < groups.size(); ++group_index) {
94 const auto& group = groups[group_index];
95 PURIFY_ASSIGN_OR_RETURN(auto circuit_template, verifier_circuit_template(group.evaluation_input),
96 "nonce_batch_assignments:template");
97 PURIFY_ASSIGN_OR_RETURN(auto assignment,
98 circuit_template.sum_assignments(group.hosts, assignments[group_index]),
99 "nonce_batch_assignments:group");
100 PURIFY_RETURN_IF_ERROR(checked_sum(result.left.size(), assignment.left.size()),
101 "nonce_batch_assignments:gates");
102 result.left.insert(result.left.end(), assignment.left.begin(), assignment.left.end());
103 result.right.insert(result.right.end(), assignment.right.begin(), assignment.right.end());
104 result.output.insert(result.output.end(), assignment.output.begin(), assignment.output.end());
105 result.commitments.push_back(assignment.commitments[0]);
106 }
107
108 PURIFY_ASSIGN_OR_RETURN(const auto gates, padded_gates(result.left.size()),
109 "nonce_batch_assignments:padding");
110 result.left.resize(gates, FieldElement::zero());
111 result.right.resize(gates, FieldElement::zero());
112 result.output.resize(gates, FieldElement::zero());
113 return result;
114}
115
116} // namespace purify
static FieldElement zero()
Returns the additive identity of the scalar field.
Definition numeric.cpp:32
#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
constexpr Unexpected< Error > unexpected_error(ErrorCode code, const char *context=nullptr)
Constructs an unexpected Error value from a machine-readable code.
Definition error.hpp:293
Result< NativeBulletproofCircuitTemplate > verifier_circuit_template(const Bytes &message)
Builds a reusable public-key-agnostic verifier-circuit template for a message.
Definition api.cpp:217
Expected< T, Error > Result
Expected-returning convenience alias for Purify value-producing APIs.
Definition error.hpp:99
Result< BulletproofAssignmentData > nonce_batch_assignments(std::span< const NonceBatchGroup > groups, std::span< const std::vector< BulletproofAssignmentData > > assignments)
Validates and combines member assignments for nonce_batch_circuit.
Result< NativeBulletproofCircuit > nonce_batch_circuit(std::span< const NonceBatchGroup > groups)
Builds a public verifier circuit with one output commitment per group.
Columnar witness assignment compatible with the native Bulletproof circuit layout.
BulletproofWitnessColumn left
BulletproofWitnessColumn output
BulletproofWitnessColumn commitments
BulletproofWitnessColumn right
Native in-memory representation of a Bulletproof-style arithmetic circuit.
std::vector< NativeBulletproofCircuitRow > wv
std::vector< NativeBulletproofCircuitRow > wr
std::vector< NativeBulletproofCircuitRow > wl
std::vector< NativeBulletproofCircuitRow > wo
std::vector< FieldElement > c