purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
accountable_responses.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 <array>
9#include <cstdint>
10#include <optional>
11#include <utility>
12#include <vector>
13
14#include "bppp_bridge.h"
15#include "purify/secp_bridge.h"
16
17namespace purify::accountable {
18namespace {
19
20std::array<unsigned char, 8> encode_u64(std::size_t value) {
21 std::array<unsigned char, 8> out{};
22 const auto encoded = static_cast<std::uint64_t>(value);
23 for (std::size_t i = 0; i < out.size(); ++i) {
24 out[out.size() - 1 - i] =
25 static_cast<unsigned char>(encoded >> (8 * i));
26 }
27 return out;
28}
29
30template <typename Roster>
31Result<NonceHostCommitment> compute_nonce_host_commitment(
32 const Roster& roster,
33 std::span<const puresign_plusplus::PublicKey> ordered_hosts,
34 purify_secp_context* secp_context) {
36 require_secp_context(secp_context,
37 "accountable:nonce_hosts_context"),
38 "accountable:nonce_hosts_context");
39 if (roster.members().empty()) {
41 "accountable:nonce_hosts_empty");
42 }
43 if (ordered_hosts.size() != roster.members().size()) {
45 "accountable:nonce_hosts_count");
46 }
47 if (!size_fits_u64(ordered_hosts.size())) {
49 "accountable:nonce_hosts_count");
50 }
51
52 const auto count = encode_u64(ordered_hosts.size());
53 std::size_t encoded_size = 0;
54 if (!checked_mul_size(ordered_hosts.size(), 33 + 96, encoded_size) ||
55 !checked_add_size(32 + 32 + count.size(), encoded_size,
56 encoded_size)) {
58 "accountable:nonce_hosts_size");
59 }
60 Bytes preimage;
61 preimage.reserve(encoded_size);
62 preimage.insert(preimage.end(), roster.signing_key().begin(),
63 roster.signing_key().end());
64 preimage.insert(preimage.end(), roster.key_list_hash().begin(),
65 roster.key_list_hash().end());
66 preimage.insert(preimage.end(), count.begin(), count.end());
67
68 for (std::size_t i = 0; i < ordered_hosts.size(); ++i) {
69 const auto& member = roster.members()[i];
70 const auto& host = ordered_hosts[i];
71 if (member.public_key[0] != 0x02 ||
72 !std::equal(host.bip340_pubkey.begin(),
73 host.bip340_pubkey.end(),
74 member.public_key.begin() + 1)) {
76 "accountable:nonce_host_member_key");
77 }
79 "accountable:nonce_host_purify_key");
81 secp_context, host.bip340_pubkey.data()) == 0) {
83 "accountable:nonce_host_signing_key");
84 }
85 for (std::size_t j = 0; j < i; ++j) {
86 if (ordered_hosts[j].purify_pubkey == host.purify_pubkey) {
88 "accountable:duplicate_nonce_host");
89 }
90 }
91 const auto encoded_host = host.serialize();
92 preimage.insert(preimage.end(), member.public_key.begin(),
93 member.public_key.end());
94 preimage.insert(preimage.end(), encoded_host.begin(),
95 encoded_host.end());
96 }
97 static const TaggedHash hash(
98 "Purify/Accountable/NonceHostRoster/v1");
99 return hash.digest(preimage);
100}
101
102Result<FieldElement> roster_challenge(
103 const NonceHostBoundSigningRoster& roster,
104 const VerifiedCoalitionNonceSet& nonce_set,
105 const ConflictContext& context,
106 std::span<const unsigned char> canonical_body,
107 purify_secp_context* secp_context) {
108 if (context.signing_key != roster.signing_key() ||
109 context.host_commitment != roster.nonce_host_commitment() ||
110 context.coalition_core_digest != roster.coalition_core_digest() ||
111 nonce_set.signing_key() != roster.signing_key() ||
112 nonce_set.host_commitment() != roster.nonce_host_commitment() ||
113 nonce_set.coalition_core_digest() !=
114 roster.coalition_core_digest()) {
116 "accountable:response_roster_binding");
117 }
119 const auto& nonce_input,
120 nonce_evaluation_input(context, secp_context),
121 "accountable:response_nonce_input");
122 if (nonce_input != nonce_set.nonce_input()) {
124 "accountable:response_nonce_context");
125 }
126 return signature_challenge(context, canonical_body, secp_context);
127}
128
130 const std::array<unsigned char, 33>& key,
131 const FieldElement& weighted_challenge,
132 const ScalarSignature& response,
133 purify_secp_context* secp_context) {
134 const auto negative_weight = weighted_challenge.negate().to_bytes_be();
135 std::array<unsigned char, 33> key_term{};
136 if (purify_point_scale(secp_context, key.data(),
137 negative_weight.data(), key_term.data()) == 0) {
139 "accountable:response_key_term");
140 }
141 const auto scalar = response.serialize();
142 std::array<unsigned char, 33> nonce{};
144 secp_context, key_term.data(), scalar.data(),
145 nonce.data()) == 0) {
147 "accountable:response_nonce_at_infinity");
148 }
149 return nonce;
150}
151
152Status check_partial(
153 const SigningRosterMember& member,
154 const std::array<unsigned char, 33>& verified_nonce,
155 const FieldElement& challenge, const PartialResponse& partial,
156 purify_secp_context* secp_context) {
158 const auto& nonce,
159 response_nonce(member.public_key,
160 challenge * member.coefficient,
161 partial.response, secp_context),
162 "accountable:partial_nonce");
163 if (nonce != verified_nonce) {
165 "accountable:partial_response_equation");
166 }
167 return {};
168}
169
170} // namespace
171
173 const SigningRoster& roster,
174 std::span<const puresign_plusplus::PublicKey> ordered_hosts,
175 purify_secp_context* secp_context) {
176 return compute_nonce_host_commitment(roster, ordered_hosts,
177 secp_context);
178}
179
181 const NonceHostBoundSigningRoster& roster,
182 std::span<const puresign_plusplus::PublicKey> ordered_hosts,
183 purify_secp_context* secp_context) {
184 return compute_nonce_host_commitment(roster, ordered_hosts,
185 secp_context);
186}
187
189 SigningRoster roster, const CoalitionFormation& formation,
190 std::span<const puresign_plusplus::PublicKey> ordered_hosts,
191 purify_secp_context* secp_context) {
192 return bind_nonce_hosts(
193 std::move(roster), formation.core_digest(), ordered_hosts,
194 secp_context);
195}
196
198 SigningRoster roster,
200 std::span<const puresign_plusplus::PublicKey> ordered_hosts,
201 purify_secp_context* secp_context) {
203 const auto& commitment,
204 nonce_host_commitment(roster, ordered_hosts, secp_context),
205 "accountable:bind_nonce_hosts");
206 return NonceHostBoundSigningRoster(
207 std::move(roster), coalition_core_digest, commitment);
208}
209
211 const NonceHostBoundSigningRoster& roster,
212 const ConflictContext& context,
213 std::span<const puresign_plusplus::PublicKey> ordered_hosts,
214 std::span<const puresign_plusplus::NonceProof> ordered_proofs,
215 purify_secp_context* secp_context,
217 if (ordered_proofs.size() != ordered_hosts.size() ||
218 ordered_hosts.size() != roster.members().size()) {
220 "accountable:nonce_proof_count");
221 }
223 const auto& registered_hosts,
224 nonce_host_commitment(roster, ordered_hosts, secp_context),
225 "accountable:nonce_host_commitment");
226 if (registered_hosts != roster.nonce_host_commitment() ||
227 context.host_commitment != registered_hosts ||
228 context.signing_key != roster.signing_key() ||
229 context.coalition_core_digest != roster.coalition_core_digest()) {
231 "accountable:nonce_roster_binding");
232 }
234 const auto& nonce_input,
235 nonce_evaluation_input(context, secp_context),
236 "accountable:nonce_input");
238 auto proof_cache,
240 "accountable:nonce_proof_cache");
241
242 std::vector<VerifiedCoalitionNonceSet::Point> member_nonces;
243 member_nonces.reserve(ordered_proofs.size());
244 std::optional<VerifiedCoalitionNonceSet::Point> aggregate;
245 for (std::size_t i = 0; i < ordered_proofs.size(); ++i) {
247 const auto& accepted,
248 ordered_hosts[i].verify_topic_nonce_proof(
249 proof_cache, ordered_proofs[i], secp_context, cache),
250 "accountable:nonce_proof");
251 if (!accepted) {
253 "accountable:nonce_proof");
254 }
255 const auto point = ordered_proofs[i].commitment_point;
256 member_nonces.push_back(point);
257 if (!aggregate) {
258 aggregate = point;
259 } else if ((*aggregate)[0] != point[0] &&
260 std::equal(aggregate->begin() + 1, aggregate->end(),
261 point.begin() + 1)) {
262 // Exact opposite points cancel. A later member may make the final
263 // aggregate finite again.
264 aggregate.reset();
265 } else {
267 if (purify_point_add(secp_context, aggregate->data(),
268 point.data(), next.data()) == 0) {
270 "accountable:aggregate_nonce_sum");
271 }
272 aggregate = next;
273 }
274 }
275 if (!aggregate) {
277 "accountable:aggregate_nonce_infinity");
278 }
279 return VerifiedCoalitionNonceSet(
280 std::move(member_nonces), *aggregate, roster.signing_key(),
281 registered_hosts, roster.coalition_core_digest(), nonce_input);
282}
283
285 const NonceHostBoundSigningRoster& roster,
286 const VerifiedCoalitionNonceSet& nonce_set,
287 std::size_t member_index, const ConflictContext& context,
288 std::span<const unsigned char> canonical_body,
289 const PartialResponse& partial,
290 purify_secp_context* secp_context) {
291 if (member_index >= roster.members().size() ||
292 member_index >= nonce_set.member_nonces_.size()) {
294 "accountable:response_member_index");
295 }
297 const auto& challenge,
298 roster_challenge(roster, nonce_set, context, canonical_body,
299 secp_context),
300 "accountable:partial_challenge");
301 return check_partial(roster.members()[member_index],
302 nonce_set.member_nonces_[member_index], challenge,
303 partial, secp_context);
304}
305
307 const NonceHostBoundSigningRoster& roster,
308 const VerifiedCoalitionNonceSet& nonce_set,
309 const ConflictContext& context,
310 std::span<const unsigned char> canonical_body,
311 const ScalarSignature& response,
312 purify_secp_context* secp_context) {
314 roster_challenge(roster, nonce_set, context, canonical_body,
315 secp_context),
316 "accountable:scalar_response_context");
318 const auto& reconstructed,
319 reconstruct_nonce(context, canonical_body, response, secp_context),
320 "accountable:scalar_response_nonce");
321 if (reconstructed != nonce_set.aggregate_nonce_) {
323 "accountable:scalar_response_nonce");
324 }
326 response, reconstructed, roster.signing_key(),
328 Bytes(context.slot.begin(), context.slot.end()),
329 Bytes(canonical_body.begin(), canonical_body.end()));
330}
331
333 const NonceHostBoundSigningRoster& roster,
334 const VerifiedCoalitionNonceSet& nonce_set,
335 const ConflictContext& context,
336 std::span<const unsigned char> canonical_body,
337 std::span<const PartialResponse> partials,
338 purify_secp_context* secp_context) {
339 if (roster.members().empty()) {
341 "accountable:empty_response_roster");
342 }
343 if (partials.size() != roster.members().size() ||
344 partials.size() != nonce_set.member_nonces_.size()) {
346 "accountable:response_member_count");
347 }
349 const auto& challenge,
350 roster_challenge(roster, nonce_set, context, canonical_body,
351 secp_context),
352 "accountable:aggregate_challenge");
353 auto sum = FieldElement::zero();
354 for (std::size_t i = 0; i < partials.size(); ++i) {
356 check_partial(roster.members()[i], nonce_set.member_nonces_[i],
357 challenge, partials[i], secp_context),
358 "accountable:aggregate_partial");
359 sum = sum + FieldElement::from_bytes32(
360 partials[i].response.serialize());
361 }
363 const auto& response,
364 ScalarSignature::deserialize(sum.to_bytes_be()),
365 "accountable:aggregate_response");
366 return verify_scalar_response(roster, nonce_set, context,
367 canonical_body, response, secp_context);
368}
369
371 const VerifiedScalarResponse& first,
372 const VerifiedScalarResponse& second,
373 purify_secp_context* secp_context) {
374 if (first.signing_key_ != second.signing_key_ ||
375 first.host_commitment_ != second.host_commitment_ ||
376 first.coalition_core_digest_ != second.coalition_core_digest_ ||
377 first.slot_ != second.slot_ || first.nonce_ != second.nonce_) {
379 "accountable:verified_conflict_binding");
380 }
381 if (first.body_ == second.body_) {
383 "accountable:verified_conflict_body");
384 }
385 const ConflictContext context{
386 first.signing_key_, first.host_commitment_,
387 first.coalition_core_digest_, first.slot_};
389 const auto& recovered,
390 recover_same_slot_key(context, first.body_, first.response_,
391 second.body_, second.response_, secp_context),
392 "accountable:verified_conflict_recover");
393 return ExposedCoalitionKey(
394 FieldElement::from_bytes32(recovered), first.signing_key_,
395 first.host_commitment_, first.coalition_core_digest_);
396}
397
398} // 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])
int purify_point_add(purify_secp_context *context, const unsigned char lhs33[33], const unsigned char rhs33[33], unsigned char out33[33])
int purify_bppp_offset_commitment(purify_secp_context *context, const unsigned char commitment33[33], const unsigned char scalar32[32], unsigned char commitment_out33[33])
C ABI bridging Purify C++ code to secp256k1-zkp BPPP functionality.
static FieldElement from_bytes32(const std::array< unsigned char, 32 > &bytes)
Decodes a 32-byte big-endian field element.
Definition numeric.cpp:63
static FieldElement zero()
Returns the additive identity of the scalar field.
Definition numeric.cpp:32
const CoalitionCoreDigest & core_digest() const noexcept
An ordered signing roster bound to one core and nonce-host list.
std::span< const SigningRosterMember > members() const
const std::array< unsigned char, 32 > & signing_key() const
const NonceHostCommitment & nonce_host_commitment() const
const CoalitionCoreDigest & coalition_core_digest() const
Canonical 32-byte scalar response; carries no nonce or proof.
static Result< ScalarSignature > deserialize(std::span< const unsigned char > bytes)
Parse exactly 32 big-endian bytes below the group order, including zero.
Experimental public key aggregation for one ordered coalition roster.
const std::array< unsigned char, 32 > & nonce_input() const
Common interface for reusable experimental BPPP backend state.
Definition bppp.hpp:129
#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< std::array< unsigned char, 33 > > reconstruct_nonce(const ConflictContext &context, std::span< const unsigned char > canonical_body, const ScalarSignature &signature, purify_secp_context *secp_context)
Reconstruct the full nonce point as sigma*G - e*Q, rejecting infinity.
Result< NonceHostCommitment > nonce_host_commitment(const SigningRoster &roster, std::span< const puresign_plusplus::PublicKey > ordered_hosts, purify_secp_context *secp_context)
Result< VerifiedCoalitionNonceSet > verify_coalition_nonce_set(const NonceHostBoundSigningRoster &roster, const ConflictContext &context, std::span< const puresign_plusplus::PublicKey > ordered_hosts, std::span< const puresign_plusplus::NonceProof > ordered_proofs, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *cache=nullptr)
Result< ExposedCoalitionKey > recover_verified_same_slot_key(const VerifiedScalarResponse &first, const VerifiedScalarResponse &second, purify_secp_context *secp_context)
Result< std::array< unsigned char, 32 > > nonce_evaluation_input(const ConflictContext &context, purify_secp_context *secp_context)
Derive the candidate deterministic Purify input from the registered slot.
Result< NonceHostBoundSigningRoster > bind_nonce_hosts(SigningRoster roster, const CoalitionCoreDigest &coalition_core_digest, std::span< const puresign_plusplus::PublicKey > ordered_hosts, purify_secp_context *secp_context)
std::array< unsigned char, 32 > CoalitionCoreDigest
Status verify_partial_response(const NonceHostBoundSigningRoster &roster, const VerifiedCoalitionNonceSet &nonce_set, std::size_t member_index, const ConflictContext &context, std::span< const unsigned char > canonical_body, const PartialResponse &partial, purify_secp_context *secp_context)
Result< VerifiedScalarResponse > verify_scalar_response(const NonceHostBoundSigningRoster &roster, const VerifiedCoalitionNonceSet &nonce_set, const ConflictContext &context, std::span< const unsigned char > canonical_body, const ScalarSignature &response, purify_secp_context *secp_context)
Result< FieldElement > signature_challenge(const ConflictContext &context, std::span< const unsigned char > canonical_body, purify_secp_context *secp_context)
Compute the versioned Q/U/core/T/body challenge, without a nonce point.
Result< VerifiedScalarResponse > aggregate_responses(const NonceHostBoundSigningRoster &roster, const VerifiedCoalitionNonceSet &nonce_set, const ConflictContext &context, std::span< const unsigned char > canonical_body, std::span< const PartialResponse > partials, purify_secp_context *secp_context)
Result< std::array< unsigned char, 32 > > recover_same_slot_key(const ConflictContext &context, std::span< const unsigned char > first_body, const ScalarSignature &first_signature, std::span< const unsigned char > second_body, const ScalarSignature &second_signature, purify_secp_context *secp_context)
Recover the exact signing scalar exposed by two same-slot responses.
Result< NonceHostBoundSigningRoster > bind_formation_nonce_hosts(SigningRoster roster, const CoalitionFormation &formation, std::span< const puresign_plusplus::PublicKey > ordered_hosts, purify_secp_context *secp_context)
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 size_fits_u64(std::size_t value) noexcept
Definition common.hpp:83
bool checked_mul_size(std::size_t lhs, std::size_t rhs, std::size_t &out) noexcept
Definition common.hpp:71
Status validate_public_key(const UInt512 &packed)
Validates the packed public-key encoding range.
Definition curve.cpp:319
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
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
Narrow C ABI exposing secp256k1 scalar and HMAC helpers to the C++ headers.
int purify_bip340_validate_xonly_pubkey(purify_secp_context *context, const unsigned char xonly_pubkey32[32])
Returns nonzero when the x-only public key encoding parses successfully.
struct purify_secp_context purify_secp_context
Caller-owned reusable secp256k1 context handle shared across Purify C and C++ APIs.
Public context shared by conflicting statements in one slashable slot.
std::array< unsigned char, 32 > host_commitment
std::span< const unsigned char > slot
std::array< unsigned char, 32 > signing_key
CoalitionCoreDigest coalition_core_digest
An ordered individual signing key and its normalized public weight.
static Result< TopicProofCache > build(std::span< const unsigned char > topic)
Builds a reusable verifier template for one exact topic.
Definition bppp.cpp:389