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;
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);
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());
62void append(
Bytes& output, std::span<const unsigned char> bytes) {
63 output.insert(output.end(), bytes.begin(), bytes.end());
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));
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));
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()) {
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) {
106 if (member_index >= roster.members().size()) {
108 "construction_parent_witness:member_index");
110 const auto& member = roster.members()[member_index];
111 const auto coefficient = member.coefficient.to_bytes_be();
114 coefficient.data(), weighted.data()) == 0) {
116 "construction_parent_witness:weighted_member");
123 return left.ephemeral_key == right.ephemeral_key &&
124 left.masked_scalar == right.masked_scalar;
127std::span<const unsigned char> descriptor_at(
130 return std::span<const unsigned char>{inputs.descriptors}
131 .subspan(index * kDescriptorSize, kDescriptorSize);
140 std::span<const ResolvedConstructionReceiptEntry> resolved_entries,
141 std::span<const ConstructionParentSetupWitness> setup_witnesses,
142 std::span<const ConstructionParentAnchorWitness> anchor_witnesses,
148 "construction_parent_witness:context"),
149 "construction_parent_witness:context");
153 resolved_setup, payload, resolved_entries,
154 accumulator.
leaves(), secp_context),
155 "construction_parent_witness:public_inputs");
159 if (setup_witnesses.size() != setup_entries.size() ||
160 anchor_witnesses.size() != anchor_entries.size()) {
162 "construction_parent_witness:witness_count");
168 !same_roster(verified_setup.
roster(),
169 resolved_setup.
roster())) {
171 "construction_parent_witness:verified_setup");
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 =
189 "construction_parent_witness:encoded_size");
192 std::array<unsigned char, kFinalRootSize> empty_root{};
196 accumulator.
leaves().
basis(), empty_root, secp_context),
197 "construction_parent_witness:empty_root");
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");
211 index, witness.contribution, secp_context, cache),
212 "construction_parent_witness:prepare_setup");
213 if (prepared.circuit_precommitment() !=
214 entry.circuit_precommitment) {
217 "construction_parent_witness:setup_precommitment");
223 witness.reduction, secp_context),
224 "construction_parent_witness:verify_setup_reduction");
225 const std::array claims{current, leaf};
229 accumulator.
leaves().
basis(), claims, witness.step,
231 "construction_parent_witness:verify_setup_step");
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(),
241 "construction_parent_witness:ciphertext");
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) {
254 "construction_parent_witness:anchor_precommitment");
260 witness.reduction, secp_context),
261 "construction_parent_witness:verify_anchor_reduction");
262 const std::array claims{current, leaf};
266 accumulator.
leaves().
basis(), claims, witness.step,
268 "construction_parent_witness:verify_anchor_step");
273 accumulator.
leaves().
basis(), current, secp_context),
274 "construction_parent_witness:decide");
277 encoded.reserve(expected_size);
278 append(encoded, kWit4Magic);
279 append(encoded, inputs.statement_digest);
282 append_u32_be(encoded, inputs.setup_count);
283 append_u32_be(encoded, inputs.anchor_count);
285 for (std::size_t index = 0; index < setup_witnesses.size(); ++index) {
286 const auto& witness = setup_witnesses[index];
289 effective_member_point(resolved_setup.
roster(), index,
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,
301 "construction_parent_witness:serialize_setup_reduction");
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");
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);
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];
328 punishment[0] = 0x02;
329 std::copy(resolved.punishment_key.xonly().begin(),
330 resolved.punishment_key.xonly().end(),
331 punishment.begin() + 1);
334 witness.record.serialize(secp_context),
335 "construction_parent_witness:serialize_anchor_record");
337 auto reduction_bytes,
341 "construction_parent_witness:serialize_anchor_reduction");
347 "construction_parent_witness:serialize_anchor_step");
349 reduction_bytes.size() != kReductionProofSize ||
350 step_bytes.size() != kStepProofSize) {
352 "construction_parent_witness:anchor_size");
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);
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");
373 append(encoded, final_root);
374 if (encoded.size() != expected_size) {
376 "construction_parent_witness:final_size");
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
static constexpr std::size_t kRootClaimSerializedSize
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.
const UInt512 & recovery_root() const
static constexpr std::size_t kSerializedSize
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 UInt512 & recovery_root() const noexcept
const NonceHostBoundSigningRoster & roster() const noexcept
const CoalitionFormation & formation() const noexcept
const RecoveryBoundSigningRoster & roster() const noexcept
const RecoveryRootShareAggregate & aggregate() const noexcept
Common interface for reusable experimental BPPP backend state.
#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.
constexpr std::size_t kConstructionParentDescriptorSize
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.
Status require_secp_context(const purify_secp_context *context, const char *error_context)
constexpr Unexpected< Error > unexpected_error(ErrorCode code, const char *context=nullptr)
Constructs an unexpected Error value from a machine-readable code.
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.
std::vector< unsigned char > Bytes
Dynamically sized byte string used for messages, serialized witnesses, and proofs.
bool checked_add_size(std::size_t lhs, std::size_t rhs, std::size_t &out) noexcept
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.
static constexpr std::size_t kSerializedSize