purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
construction_parent_prover_input.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
8
9#include <algorithm>
10#include <array>
11#include <cassert>
12#include <cstdint>
13#include <vector>
14
16
17namespace purify::accountable {
18namespace {
19
20constexpr std::array<unsigned char, 16> kWit4Magic{
21 'P', 'u', 'r', 'i', 'f', 'y', '/', 'N',
22 'o', 'v', 'a', '/', 'W', 'i', 't', '4'};
23constexpr std::size_t kDescriptorSize =
25constexpr std::size_t kReductionProofSize = 1'022;
26constexpr std::size_t kStepProofSize = 1'056;
27constexpr std::size_t kHeaderSize =
28 kWit4Magic.size() + 32 + 32 + 64 + 4 + 4;
29constexpr std::size_t kSetupTranscriptOffset =
31constexpr std::size_t kAnchorTranscriptOffset =
33constexpr std::size_t kSetupRecordSize =
35constexpr std::size_t kAnchorRecordSize =
37constexpr std::size_t kSetupWitnessSize =
38 kDescriptorSize + 33 + kSetupRecordSize +
39 kReductionProofSize + kStepProofSize;
40constexpr std::size_t kAnchorWitnessSize =
41 kDescriptorSize + 33 + kAnchorRecordSize +
42 kReductionProofSize + kStepProofSize;
43
44static_assert(kSetupTranscriptOffset == 67);
45static_assert(kAnchorTranscriptOffset == 97);
46static_assert(kSetupRecordSize == 1'137);
47static_assert(kAnchorRecordSize == kSetupRecordSize);
48static_assert(kSetupWitnessSize == 3'378);
49static_assert(kAnchorWitnessSize == kSetupWitnessSize);
50static_assert(kSetupRecordSize ==
52 kSetupTranscriptOffset);
53static_assert(kAnchorRecordSize ==
55 kAnchorTranscriptOffset);
56
57template <std::size_t N>
58void append(Bytes& output, const std::array<unsigned char, N>& bytes) {
59 output.insert(output.end(), bytes.begin(), bytes.end());
60}
61
62void append(Bytes& output, std::span<const unsigned char> bytes) {
63 output.insert(output.end(), bytes.begin(), bytes.end());
64}
65
66void append_compact_record(Bytes& output,
67 std::span<const unsigned char> full_record,
68 std::size_t transcript_offset) {
69 assert(!full_record.empty());
70 assert(transcript_offset <= full_record.size());
71 output.push_back(full_record.front());
72 append(output, full_record.subspan(transcript_offset));
73}
74
75void append_u32_be(Bytes& output, std::uint32_t value) {
76 output.push_back(static_cast<unsigned char>((value >> 24) & 0xffU));
77 output.push_back(static_cast<unsigned char>((value >> 16) & 0xffU));
78 output.push_back(static_cast<unsigned char>((value >> 8) & 0xffU));
79 output.push_back(static_cast<unsigned char>(value & 0xffU));
80}
81
82bool same_roster(const NonceHostBoundSigningRoster& left,
83 const NonceHostBoundSigningRoster& right) {
84 if (left.signing_key() != right.signing_key() ||
85 left.key_list_hash() != right.key_list_hash() ||
86 left.aggregate_was_negated() != right.aggregate_was_negated() ||
87 left.coalition_core_digest() != right.coalition_core_digest() ||
88 left.nonce_host_commitment() != right.nonce_host_commitment() ||
89 left.members().size() != right.members().size()) {
90 return false;
91 }
92 for (std::size_t index = 0; index < left.members().size(); ++index) {
93 if (left.members()[index].public_key !=
94 right.members()[index].public_key ||
95 left.members()[index].coefficient !=
96 right.members()[index].coefficient) {
97 return false;
98 }
99 }
100 return true;
101}
102
103Result<bppp::PointBytes> effective_member_point(
104 const NonceHostBoundSigningRoster& roster, std::size_t member_index,
105 purify_secp_context* secp_context) {
106 if (member_index >= roster.members().size()) {
108 "construction_parent_witness:member_index");
109 }
110 const auto& member = roster.members()[member_index];
111 const auto coefficient = member.coefficient.to_bytes_be();
112 bppp::PointBytes weighted{};
113 if (purify_point_scale(secp_context, member.public_key.data(),
114 coefficient.data(), weighted.data()) == 0) {
116 "construction_parent_witness:weighted_member");
117 }
118 return weighted;
119}
120
121bool same_ciphertext(const AnchorEscrowCiphertext& left,
122 const AnchorEscrowCiphertext& right) {
123 return left.ephemeral_key == right.ephemeral_key &&
124 left.masked_scalar == right.masked_scalar;
125}
126
127std::span<const unsigned char> descriptor_at(
129 std::size_t index) {
130 return std::span<const unsigned char>{inputs.descriptors}
131 .subspan(index * kDescriptorSize, kDescriptorSize);
132}
133
134} // namespace
135
137 const ResolvedConstructionSetup& resolved_setup,
138 const VerifiedDistributedRecoverySetup& verified_setup,
139 const ConstructionReceiptPayload& payload,
140 std::span<const ResolvedConstructionReceiptEntry> resolved_entries,
141 std::span<const ConstructionParentSetupWitness> setup_witnesses,
142 std::span<const ConstructionParentAnchorWitness> anchor_witnesses,
143 const ConstructionReceiptAccumulator& accumulator,
144 purify_secp_context* secp_context,
147 require_secp_context(secp_context,
148 "construction_parent_witness:context"),
149 "construction_parent_witness:context");
151 auto inputs,
153 resolved_setup, payload, resolved_entries,
154 accumulator.leaves(), secp_context),
155 "construction_parent_witness:public_inputs");
156
157 const auto setup_entries = payload.setup_entries();
158 const auto anchor_entries = payload.anchor_entries();
159 if (setup_witnesses.size() != setup_entries.size() ||
160 anchor_witnesses.size() != anchor_entries.size()) {
162 "construction_parent_witness:witness_count");
163 }
164 if (verified_setup.roster().recovery_root() !=
165 resolved_setup.recovery_root() ||
166 verified_setup.aggregate().recovery_root() !=
167 resolved_setup.recovery_root() ||
168 !same_roster(verified_setup.roster(),
169 resolved_setup.roster())) {
171 "construction_parent_witness:verified_setup");
172 }
173
174 std::size_t setup_size = 0;
175 std::size_t anchor_size = 0;
176 std::size_t body_size = 0;
177 std::size_t expected_size = 0;
178 constexpr std::size_t kFinalRootSize =
180 if (!checked_mul_size(setup_witnesses.size(), kSetupWitnessSize,
181 setup_size) ||
182 !checked_mul_size(anchor_witnesses.size(), kAnchorWitnessSize,
183 anchor_size) ||
184 !checked_add_size(setup_size, anchor_size, body_size) ||
185 !checked_add_size(kHeaderSize, body_size, expected_size) ||
186 !checked_add_size(expected_size, kFinalRootSize,
187 expected_size)) {
189 "construction_parent_witness:encoded_size");
190 }
191
192 std::array<unsigned char, kFinalRootSize> empty_root{};
194 auto current,
196 accumulator.leaves().basis(), empty_root, secp_context),
197 "construction_parent_witness:empty_root");
198
199 for (std::size_t index = 0; index < setup_witnesses.size(); ++index) {
200 const auto& witness = setup_witnesses[index];
201 const auto& entry = setup_entries[index];
202 if (witness.contribution.share().serialize() !=
203 entry.share.serialize()) {
205 "construction_parent_witness:setup_share");
206 }
208 auto prepared,
209 accumulator.leaves().prepare(
210 resolved_setup.roster(), resolved_setup.formation(),
211 index, witness.contribution, secp_context, cache),
212 "construction_parent_witness:prepare_setup");
213 if (prepared.circuit_precommitment() !=
214 entry.circuit_precommitment) {
215 return unexpected_error(
217 "construction_parent_witness:setup_precommitment");
218 }
220 auto leaf,
222 accumulator.leaves().basis(), prepared.norm(),
223 witness.reduction, secp_context),
224 "construction_parent_witness:verify_setup_reduction");
225 const std::array claims{current, leaf};
227 current,
229 accumulator.leaves().basis(), claims, witness.step,
230 secp_context),
231 "construction_parent_witness:verify_setup_step");
232 }
233
234 for (std::size_t index = 0; index < anchor_witnesses.size(); ++index) {
235 const auto& witness = anchor_witnesses[index];
236 const auto& entry = anchor_entries[index];
237 const auto& resolved = resolved_entries[index];
238 if (!same_ciphertext(witness.record.ciphertext(),
239 entry.ciphertext)) {
241 "construction_parent_witness:ciphertext");
242 }
244 auto prepared,
245 accumulator.leaves().prepare(
246 verified_setup.roster(), resolved.anchor_root,
247 resolved.punishment_key, resolved.context_digest,
248 witness.record, secp_context, cache),
249 "construction_parent_witness:prepare_anchor");
250 if (prepared.circuit_precommitment() !=
251 entry.circuit_precommitment) {
252 return unexpected_error(
254 "construction_parent_witness:anchor_precommitment");
255 }
257 auto leaf,
259 accumulator.leaves().basis(), prepared.norm(),
260 witness.reduction, secp_context),
261 "construction_parent_witness:verify_anchor_reduction");
262 const std::array claims{current, leaf};
264 current,
266 accumulator.leaves().basis(), claims, witness.step,
267 secp_context),
268 "construction_parent_witness:verify_anchor_step");
269 }
270
273 accumulator.leaves().basis(), current, secp_context),
274 "construction_parent_witness:decide");
275
276 Bytes encoded;
277 encoded.reserve(expected_size);
278 append(encoded, kWit4Magic);
279 append(encoded, inputs.statement_digest);
280 append(encoded, resolved_setup.resolved_digest().bytes());
281 append(encoded, resolved_setup.recovery_root().to_bytes_be());
282 append_u32_be(encoded, inputs.setup_count);
283 append_u32_be(encoded, inputs.anchor_count);
284
285 for (std::size_t index = 0; index < setup_witnesses.size(); ++index) {
286 const auto& witness = setup_witnesses[index];
288 auto weighted,
289 effective_member_point(resolved_setup.roster(), index,
290 secp_context),
291 "construction_parent_witness:effective_member");
293 auto contribution_bytes,
294 witness.contribution.serialize(secp_context),
295 "construction_parent_witness:serialize_contribution");
297 auto reduction_bytes,
299 accumulator.leaves().basis(), witness.reduction,
300 secp_context),
301 "construction_parent_witness:serialize_setup_reduction");
303 auto step_bytes,
305 accumulator.leaves().basis(), witness.step,
306 secp_context),
307 "construction_parent_witness:serialize_setup_step");
308 if (contribution_bytes.size() !=
310 reduction_bytes.size() != kReductionProofSize ||
311 step_bytes.size() != kStepProofSize) {
313 "construction_parent_witness:setup_size");
314 }
315
316 append(encoded, descriptor_at(inputs, index));
317 append(encoded, weighted);
318 append_compact_record(encoded, contribution_bytes,
319 kSetupTranscriptOffset);
320 append(encoded, reduction_bytes);
321 append(encoded, step_bytes);
322 }
323
324 for (std::size_t index = 0; index < anchor_witnesses.size(); ++index) {
325 const auto& witness = anchor_witnesses[index];
326 const auto& resolved = resolved_entries[index];
327 bppp::PointBytes punishment{};
328 punishment[0] = 0x02;
329 std::copy(resolved.punishment_key.xonly().begin(),
330 resolved.punishment_key.xonly().end(),
331 punishment.begin() + 1);
333 auto record_bytes,
334 witness.record.serialize(secp_context),
335 "construction_parent_witness:serialize_anchor_record");
337 auto reduction_bytes,
339 accumulator.leaves().basis(), witness.reduction,
340 secp_context),
341 "construction_parent_witness:serialize_anchor_reduction");
343 auto step_bytes,
345 accumulator.leaves().basis(), witness.step,
346 secp_context),
347 "construction_parent_witness:serialize_anchor_step");
348 if (record_bytes.size() != AnchorEscrowRecord::kSerializedSize ||
349 reduction_bytes.size() != kReductionProofSize ||
350 step_bytes.size() != kStepProofSize) {
352 "construction_parent_witness:anchor_size");
353 }
354
355 append(encoded,
356 descriptor_at(inputs, setup_witnesses.size() + index));
357 append(encoded, punishment);
358 append_compact_record(encoded, record_bytes,
359 kAnchorTranscriptOffset);
360 append(encoded, reduction_bytes);
361 append(encoded, step_bytes);
362 }
363
365 auto final_root,
367 accumulator.leaves().basis(), current, secp_context),
368 "construction_parent_witness:serialize_root");
369 if (final_root.size() != kFinalRootSize) {
371 "construction_parent_witness:root_size");
372 }
373 append(encoded, final_root);
374 if (encoded.size() != expected_size) {
376 "construction_parent_witness:final_size");
377 }
378 return encoded;
379}
380
381} // namespace purify::accountable
int purify_point_scale(purify_secp_context *context, const unsigned char point33[33], const unsigned char scalar32[32], unsigned char out33[33])
C ABI bridging Purify C++ code to secp256k1-zkp BPPP functionality.
static constexpr std::size_t kSerializedSize
static constexpr std::size_t kProofPayloadSize
const bppp::accumulation::Basis & basis() const noexcept
Result< PreparedConstructionLeaf > prepare(const RecoveryBoundSigningRoster &roster, const UInt512 &advertised_anchor_root, const AnchorPunishmentPublicKey &punishment_key, const AnchorEscrowContextDigest &context_digest, const AnchorEscrowRecord &record, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *cache=nullptr) const
const ConstructionLeafAccumulator & leaves() const noexcept
Canonical public payload for a coalition construction receipt.
std::span< const ConstructionReceiptSetupEntry > setup_entries() const noexcept
std::span< const ConstructionReceiptAnchorEntry > anchor_entries() const noexcept
An ordered signing roster bound to one core and nonce-host list.
static constexpr std::size_t kProofSerializedSize
const UInt512 & recovery_root() const noexcept
static constexpr std::size_t kSerializedSize
const ResolvedConstructionSetupDigestBytes & bytes() const noexcept
const ResolvedConstructionSetupDigest & resolved_digest() const noexcept
const NonceHostBoundSigningRoster & roster() const noexcept
const RecoveryBoundSigningRoster & roster() const noexcept
const RecoveryRootShareAggregate & aggregate() const noexcept
Common interface for reusable experimental BPPP backend state.
Definition bppp.hpp:129
Accepted public witness for one construction-parent proof.
#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< ResolvedConstructionParentPublicInputs > resolve_construction_parent_public_inputs(const ResolvedConstructionSetup &setup, const ConstructionReceiptPayload &payload, std::span< const ResolvedConstructionReceiptEntry > resolved_entries, const ConstructionLeafAccumulator &leaves, purify_secp_context *secp_context)
Result< Bytes > serialize_accepted_construction_parent_witness(const ResolvedConstructionSetup &resolved_setup, const VerifiedDistributedRecoverySetup &verified_setup, const ConstructionReceiptPayload &payload, std::span< const ResolvedConstructionReceiptEntry > resolved_entries, std::span< const ConstructionParentSetupWitness > setup_witnesses, std::span< const ConstructionParentAnchorWitness > anchor_witnesses, const ConstructionReceiptAccumulator &accumulator, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *cache=nullptr)
Result< Bytes > serialize_root_claim(const Basis &basis, const PendingClaim &claim, purify_secp_context *context)
Result< PendingClaim > parse_root_claim(const Basis &basis, std::span< const unsigned char > encoded, purify_secp_context *context)
Result< PendingClaim > verify_algebraic_norm_reduction(const Basis &basis, const AlgebraicPreparedNorm &norm, const AlgebraicNormReductionProof &proof, purify_secp_context *context)
Status decide(const Basis &basis, const PendingClaim &claim, purify_secp_context *context)
Result< Bytes > serialize_algebraic_norm_reduction(const Basis &basis, const AlgebraicNormReductionProof &proof, purify_secp_context *context)
Result< Bytes > serialize_algebraic_step(const Basis &basis, const AlgebraicStepProof &proof, purify_secp_context *context)
Result< PendingClaim > verify_algebraic_step(const Basis &basis, std::span< const PendingClaim > claims, const AlgebraicStepProof &proof, purify_secp_context *context)
std::array< unsigned char, 33 > PointBytes
Compressed 33-byte curve-point encoding.
Definition bppp.hpp:32
Status require_secp_context(const purify_secp_context *context, const char *error_context)
Definition common.hpp:56
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
bool checked_mul_size(std::size_t lhs, std::size_t rhs, std::size_t &out) noexcept
Definition common.hpp:71
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
bool checked_add_size(std::size_t lhs, std::size_t rhs, std::size_t &out) noexcept
Definition common.hpp:63
struct purify_secp_context purify_secp_context
Caller-owned reusable secp256k1 context handle shared across Purify C and C++ APIs.
std::array< unsigned char, Words *8 > to_bytes_be() const
Serializes the value to a fixed-width big-endian byte array.
Definition numeric.hpp:640
static constexpr std::size_t kSerializedSize