21const TaggedHash kConstructionReceiptStatementHash(
22 "Purify/Accountable/ConstructionReceiptStatement/v5");
28Status validate_circuit_precommitment(
33 "construction_receipt:circuit_precommitment");
34 if (scalar.is_zero()) {
37 "construction_receipt:circuit_precommitment_zero");
42Status validate_anchor_entries(
43 std::span<const ConstructionReceiptAnchorEntry> entries) {
44 if (entries.empty()) {
46 "construction_receipt:anchor_entries");
50 "construction_receipt:anchor_count");
54 "construction_receipt:ciphertext");
56 validate_circuit_precommitment(
57 entry.circuit_precommitment),
58 "construction_receipt:circuit_precommitment");
63Status validate_setup_entries(
65 std::span<const ConstructionReceiptSetupEntry> entries) {
66 if (entries.empty()) {
68 "construction_receipt:setup_entries");
72 "construction_receipt:setup_count");
74 std::vector<RecoveryRootShare> shares;
75 shares.reserve(entries.size());
78 validate_circuit_precommitment(
79 entry.circuit_precommitment),
80 "construction_receipt:setup_precommitment");
81 shares.push_back(entry.share);
86 "construction_receipt:setup_aggregate");
87 if (aggregate.recovery_root() != recovery_root) {
90 "construction_receipt:setup_recovery_root");
95void append_u32_be(
Bytes& out, std::uint32_t value) {
96 out.push_back(
static_cast<unsigned char>((value >> 24) & 0xffU));
97 out.push_back(
static_cast<unsigned char>((value >> 16) & 0xffU));
98 out.push_back(
static_cast<unsigned char>((value >> 8) & 0xffU));
99 out.push_back(
static_cast<unsigned char>(value & 0xffU));
102std::uint32_t read_u32_be(std::span<const unsigned char, 4> encoded) {
103 return (
static_cast<std::uint32_t
>(encoded[0]) << 24) |
104 (
static_cast<std::uint32_t
>(encoded[1]) << 16) |
105 (
static_cast<std::uint32_t
>(encoded[2]) << 8) |
106 static_cast<std::uint32_t
>(encoded[3]);
109template <std::
size_t Size>
110void append_array(
Bytes& out,
const std::array<unsigned char, Size>& value) {
111 out.insert(out.end(), value.begin(), value.end());
114void append_ciphertext(
Bytes& out,
116 append_array(out, ciphertext.ephemeral_key.to_bytes_be());
117 append_array(out, ciphertext.masked_scalar.to_bytes_be());
121 std::span<const unsigned char, AnchorEscrowCiphertext::kSerializedSize>
124 std::size_t offset = 0;
128 std::array<unsigned char, 32> encoded_scalar{};
129 std::copy_n(encoded.data() + offset,
130 encoded_scalar.size(), encoded_scalar.begin());
132 ciphertext.masked_scalar,
134 "construction_receipt:masked_scalar");
136 assert(offset == encoded.size());
138 "construction_receipt:ciphertext");
143 std::size_t setup_count, std::size_t anchor_count) {
144 std::size_t entry_count = 0;
150 "construction_receipt:entry_count");
152 std::size_t setup_size = 0;
153 std::size_t anchor_size = 0;
154 std::size_t entries_size = 0;
155 std::size_t encoded_size = 0;
169 "construction_receipt:encoded_size");
179 if (std::all_of(digest.begin(), digest.end(),
180 [](
unsigned char byte) { return byte == 0; })) {
183 "construction_receipt:resolved_setup_digest_zero");
185 return ResolvedConstructionSetupDigest(digest);
194 "construction_receipt:recovery_root");
196 const auto encoded_size,
199 "construction_receipt:encoded_size");
203 "construction_receipt:setup_entries");
206 "construction_receipt:anchor_entries");
207 return ConstructionReceiptPayload(
216 append_array(encoded, recovery_root_.to_bytes_be());
220 encoded,
static_cast<std::uint32_t
>(setup_entries_.size()));
222 encoded,
static_cast<std::uint32_t
>(anchor_entries_.size()));
224 const auto share = entry.share.serialize();
225 encoded.insert(encoded.end(), share.begin(), share.end());
226 append_array(encoded, entry.circuit_precommitment);
229 append_ciphertext(encoded, entry.ciphertext);
230 append_array(encoded, entry.circuit_precommitment);
237 std::span<const unsigned char> encoded) {
240 "construction_receipt:header");
244 "construction_receipt:version");
247 std::size_t offset = 1;
253 "construction_receipt:recovery_root");
255 const std::span<const unsigned char, 4> encoded_setup_count(
256 encoded.data() + offset, 4);
257 const std::uint32_t setup_count =
258 read_u32_be(encoded_setup_count);
260 const std::span<const unsigned char, 4> encoded_anchor_count(
261 encoded.data() + offset, 4);
262 const std::uint32_t anchor_count =
263 read_u32_be(encoded_anchor_count);
265 if (setup_count == 0 || anchor_count == 0) {
267 "construction_receipt:entry_count");
270 const std::size_t expected_size,
271 encoded_size_for_counts(
272 static_cast<std::size_t
>(setup_count),
273 static_cast<std::size_t
>(anchor_count)),
274 "construction_receipt:encoded_size");
275 if (encoded.size() != expected_size) {
277 "construction_receipt:encoded_size");
282 for (std::uint32_t i = 0; i < setup_count; ++i) {
286 encoded.subspan(offset,
288 "construction_receipt:setup_share");
291 std::copy_n(encoded.begin() + offset,
292 precommitment.size(), precommitment.begin());
293 offset += precommitment.size();
295 validate_circuit_precommitment(precommitment),
296 "construction_receipt:setup_precommitment");
302 for (std::uint32_t i = 0; i < anchor_count; ++i) {
304 const std::span<
const unsigned char,
306 encoded_ciphertext(encoded.data() + offset,
309 parse_ciphertext(encoded_ciphertext),
310 "construction_receipt:ciphertext");
313 encoded.begin() + offset,
318 validate_circuit_precommitment(
320 "construction_receipt:circuit_precommitment");
323 assert(offset == encoded.size());
326 "construction_receipt:setup_entries");
329 "construction_receipt:anchor_entries");
330 return ConstructionReceiptPayload(
339 std::span<const ResolvedConstructionReceiptEntry> resolved_entries) {
342 "construction_receipt:resolved_count");
346 statement.reserve(statement.size() +
348 resolved_entries.size() * (32 + 64 + 33 + 32));
349 append_array(statement, resolved_setup.
bytes());
350 for (std::size_t i = 0; i < resolved_entries.size(); ++i) {
353 for (std::size_t prior = 0; prior < i; ++prior) {
354 if (resolved_entries[prior].anchor_reference ==
358 "construction_receipt:duplicate_anchor_reference");
360 if (resolved_entries[prior].anchor_root ==
364 "construction_receipt:duplicate_anchor_root");
366 if (resolved_entries[prior].punishment_key ==
370 "construction_receipt:duplicate_punishment_key");
375 "construction_receipt:anchor_root");
378 std::array<unsigned char, 33> punishment{};
379 punishment[0] = 0x02;
382 punishment.begin() + 1);
383 append_array(statement, punishment);
386 return kConstructionReceiptStatementHash.digest(statement);
393Result<ConstructionReceiptAccumulator>
398 "construction_accumulation:leaf_basis");
408 if (
roster.members().empty() ||
412 "construction_setup:roster_count");
416 "construction_setup:recovery_root");
420 "construction_setup:formation_core");
432 "construction_envelope:parent_proof");
434 return ConstructionReceiptEnvelope(std::move(
payload),
440 std::span<const unsigned char> encoded) {
441 constexpr std::size_t minimum_size =
444 if (encoded.size() < minimum_size) {
446 "construction_envelope:encoded_size");
448 const std::size_t payload_size =
453 encoded.first(payload_size)),
454 "construction_envelope:payload");
460 Bytes encoded = payload_.serialize();
462 encoded.insert(encoded.end(), parent_proof_.begin(),
463 parent_proof_.end());
469 return payload_.serialized_size() + parent_proof_.size();
static Result< FieldElement > try_from_bytes32(const std::array< unsigned char, 32 > &bytes)
Decodes a canonical 32-byte big-endian field element.
const std::array< unsigned char, 32 > & xonly() const noexcept
static Result< ConstructionLeafAccumulator > create(purify_secp_context *secp_context)
ConstructionReceiptAccumulator(const ConstructionReceiptAccumulator &)=delete
const ConstructionLeafAccumulator & leaves() const noexcept
static Result< ConstructionReceiptAccumulator > create(purify_secp_context *secp_context)
static Result< ConstructionReceiptEnvelope > create(ConstructionReceiptPayload payload, Bytes parent_proof)
std::size_t serialized_size() const noexcept
static Result< ConstructionReceiptEnvelope > deserialize(std::span< const unsigned char > encoded)
const Bytes & parent_proof() const noexcept
const ConstructionReceiptPayload & payload() const noexcept
static constexpr std::size_t kParentProofSize
Canonical public payload for a coalition construction receipt.
std::span< const ConstructionReceiptSetupEntry > setup_entries() const noexcept
static constexpr std::size_t kAnchorEntrySize
std::span< const ConstructionReceiptAnchorEntry > anchor_entries() const noexcept
std::size_t serialized_size() const noexcept
static Result< ConstructionReceiptPayload > create(UInt512 recovery_root, std::vector< ConstructionReceiptSetupEntry > setup_entries, std::vector< ConstructionReceiptAnchorEntry > anchor_entries)
const UInt512 & recovery_root() const noexcept
static constexpr unsigned char kSerializationVersion
static Result< ConstructionReceiptPayload > deserialize(std::span< const unsigned char > encoded)
static constexpr std::size_t kSetupEntrySize
static constexpr std::size_t kFixedHeaderSize
An ordered signing roster bound to one core and nonce-host list.
static Result< RecoveryRootShare > deserialize(std::span< const unsigned char > encoded)
static constexpr std::size_t kSerializedSize
Immutable registry-record digest selected by a recovery root.
static Result< ResolvedConstructionSetupDigest > create(const ResolvedConstructionSetupDigestBytes &digest)
const ResolvedConstructionSetupDigestBytes & bytes() const noexcept
static constexpr std::size_t kSerializedSize
static Result< ResolvedConstructionSetup > create(NonceHostBoundSigningRoster roster, CoalitionFormation formation, UInt512 recovery_root, ResolvedConstructionSetupDigest resolved_digest)
ResolvedConstructionSetup(const ResolvedConstructionSetup &)=delete
const ResolvedConstructionSetupDigest & resolved_digest() const noexcept
const UInt512 & recovery_root() const noexcept
const NonceHostBoundSigningRoster & roster() const noexcept
const CoalitionFormation & formation() const noexcept
Canonical public statement for a coalition construction receipt.
Canonical transport for a construction receipt parent proof.
Elliptic-curve helpers, fixed parameters, and hash-to-curve utilities for Purify.
#define PURIFY_RETURN_IF_ERROR(expr, context)
Evaluates an expected-like expression and returns the wrapped error on failure.
#define PURIFY_ASSIGN_OR_RETURN(lhs, expr, context)
Evaluates an expected-like expression, binds the value to lhs, and propagates errors.
std::array< unsigned char, 32 > ResolvedConstructionSetupDigestBytes
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.
constexpr Unexpected< Error > unexpected_error(ErrorCode code, const char *context=nullptr)
Constructs an unexpected Error value from a machine-readable code.
BigUInt< 8 > UInt512
512-bit unsigned integer used for private and packed public keys.
bool checked_mul_size(std::size_t lhs, std::size_t rhs, std::size_t &out) noexcept
Expected< T, Error > Result
Expected-returning convenience alias for Purify value-producing APIs.
Expected< void, Error > Status
Expected-returning convenience alias for Purify status-only APIs.
std::vector< unsigned char > Bytes
Dynamically sized byte string used for messages, serialized witnesses, and proofs.
bool size_fits_u32(std::size_t value) noexcept
bool checked_add_size(std::size_t lhs, std::size_t rhs, std::size_t &out) noexcept
Status validate_public_points(const UInt512 &packed)
Validates both curve points in a packed public key.
static BigUInt from_bytes_be(const unsigned char *data, std::size_t size)
std::array< unsigned char, Words *8 > to_bytes_be() const
Serializes the value to a fixed-width big-endian byte array.
static constexpr std::size_t kSerializedSize
AnchorEscrowCiphertext ciphertext
bppp::ScalarBytes circuit_precommitment
Registry-resolved public inputs for one ordered anchor entry.
AnchorPunishmentPublicKey punishment_key
AnchorReference anchor_reference
AnchorEscrowContextDigest context_digest