purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
construction_parent_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
7#include <algorithm>
8#include <limits>
9#include <utility>
10
12namespace {
13
14template <std::size_t N>
15void append(std::vector<unsigned char>& output,
16 const std::array<unsigned char, N>& bytes) {
17 output.insert(output.end(), bytes.begin(), bytes.end());
18}
19
20Status append_constants(
21 std::vector<unsigned char>& descriptors,
22 const ConstructionLeafParentConstants& constants,
23 const bppp::ScalarBytes& circuit_precommitment,
24 std::array<unsigned char, 32>& raw_generator_digest,
25 bool& have_raw_generator_digest) {
26 if (have_raw_generator_digest &&
27 raw_generator_digest != constants.raw_generator_digest) {
28 return unexpected_error(
30 "construction_parent:raw_generator_digest");
31 }
32 raw_generator_digest = constants.raw_generator_digest;
33 have_raw_generator_digest = true;
34 append(descriptors, constants.circuit_binding_digest);
35 append(descriptors, circuit_precommitment);
36 append(descriptors, constants.coefficient_commitment);
37 append(descriptors, constants.anchored_offset);
38 return {};
39}
40
41} // namespace
42
45 const ResolvedConstructionSetup& setup,
46 const ConstructionReceiptPayload& payload,
47 std::span<const ResolvedConstructionReceiptEntry> resolved_entries,
48 const ConstructionLeafAccumulator& leaves,
49 purify_secp_context* secp_context) {
50 if (payload.recovery_root() != setup.recovery_root()) {
52 "construction_parent:recovery_root");
53 }
54 const auto setup_entries = payload.setup_entries();
55 const auto anchor_entries = payload.anchor_entries();
56 if (setup_entries.size() != setup.roster().members().size()) {
58 "construction_parent:setup_count");
59 }
60 if (!size_fits_u32(setup_entries.size()) ||
61 !size_fits_u32(anchor_entries.size())) {
63 "construction_parent:leaf_count");
64 }
65
66 std::vector<RecoveryRootShare> setup_shares;
67 setup_shares.reserve(setup_entries.size());
68 for (const auto& entry : setup_entries) {
69 setup_shares.push_back(entry.share);
70 }
72 auto aggregate,
74 "construction_parent:setup_aggregate");
75 if (aggregate.recovery_root() != setup.recovery_root()) {
77 "construction_parent:setup_root");
78 }
80 auto statement_digest,
82 payload, setup.resolved_digest(), resolved_entries),
83 "construction_parent:statement");
84
85 std::size_t leaf_count = 0;
86 if (!checked_add_size(setup_entries.size(), anchor_entries.size(),
87 leaf_count) ||
88 leaf_count >
89 std::numeric_limits<std::size_t>::max() /
92 "construction_parent:descriptors");
93 }
94 std::vector<unsigned char> descriptors;
95 descriptors.reserve(leaf_count * kConstructionParentDescriptorSize);
96 std::array<unsigned char, 32> raw_generator_digest{};
97 bool have_raw_generator_digest = false;
98
99 for (std::size_t index = 0; index < setup_entries.size(); ++index) {
100 const auto& entry = setup_entries[index];
102 auto constants,
104 setup.roster(), setup.formation(), index, entry.share,
105 entry.circuit_precommitment, secp_context),
106 "construction_parent:setup_constants");
108 append_constants(descriptors, constants,
109 entry.circuit_precommitment,
110 raw_generator_digest,
111 have_raw_generator_digest),
112 "construction_parent:setup_descriptor");
113 }
114 for (std::size_t index = 0; index < anchor_entries.size(); ++index) {
115 const auto& resolved = resolved_entries[index];
116 const auto& entry = anchor_entries[index];
118 auto constants,
120 setup.roster(), setup.recovery_root(),
121 resolved.anchor_root, resolved.punishment_key,
122 resolved.context_digest, entry.ciphertext,
123 entry.circuit_precommitment, secp_context),
124 "construction_parent:anchor_constants");
126 append_constants(descriptors, constants,
127 entry.circuit_precommitment,
128 raw_generator_digest,
129 have_raw_generator_digest),
130 "construction_parent:anchor_descriptor");
131 }
132
134 statement_digest,
135 std::move(aggregate),
136 std::move(descriptors),
137 raw_generator_digest,
138 static_cast<std::uint32_t>(setup_entries.size()),
139 static_cast<std::uint32_t>(anchor_entries.size())};
140}
141
142} // namespace purify::accountable::detail
Result< ConstructionLeafParentConstants > derive_parent_constants(const RecoveryBoundSigningRoster &roster, const UInt512 &advertised_anchor_root, const AnchorPunishmentPublicKey &punishment_key, const AnchorEscrowContextDigest &context_digest, const AnchorEscrowCiphertext &ciphertext, const bppp::ScalarBytes &circuit_precommitment, purify_secp_context *secp_context) const
Result< ConstructionLeafParentConstants > derive_pending_anchor_parent_constants(const NonceHostBoundSigningRoster &roster, const UInt512 &recovery_root, const UInt512 &advertised_anchor_root, const AnchorPunishmentPublicKey &punishment_key, const AnchorEscrowContextDigest &context_digest, const AnchorEscrowCiphertext &ciphertext, const bppp::ScalarBytes &circuit_precommitment, purify_secp_context *secp_context) const
Canonical public payload for a coalition construction receipt.
std::span< const ConstructionReceiptSetupEntry > setup_entries() const noexcept
std::span< const ConstructionReceiptAnchorEntry > anchor_entries() const noexcept
std::span< const SigningRosterMember > members() const
const ResolvedConstructionSetupDigest & resolved_digest() const noexcept
const NonceHostBoundSigningRoster & roster() const noexcept
#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< RecoveryRootShareAggregate > aggregate_recovery_root_shares(std::span< const RecoveryRootShare > ordered_shares)
Result< ConstructionReceiptStatementDigest > construction_receipt_statement_digest(const ConstructionReceiptPayload &payload, const ResolvedConstructionSetupDigest &resolved_setup, std::span< const ResolvedConstructionReceiptEntry > resolved_entries)
Bind a canonical payload to its ordered, resolved anchor inputs.
std::array< unsigned char, 32 > ScalarBytes
Big-endian 32-byte scalar encoding.
Definition bppp.hpp:30
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
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
bool size_fits_u32(std::size_t value) noexcept
Definition common.hpp:79
bool checked_add_size(std::size_t lhs, std::size_t rhs, std::size_t &out) noexcept
Definition common.hpp:63