purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
anchor_escrow.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
9
10#include <algorithm>
11#include <array>
12#include <cassert>
13#include <optional>
14#include <utility>
15#include <vector>
16
18#include "purify/secp_bridge.h"
20#include "secure_cleanup.hpp"
21
22namespace purify::accountable {
23namespace {
24
26
27const TaggedHash kRandomnessStatementHash(
28 "Purify/Accountable/AnchorEscrowRandomnessStatement/v4");
29const TaggedHash kRandomnessMasterHash(
30 "Purify/Accountable/AnchorEscrowRandomnessMaster/v4");
31const TaggedHash kEphemeralHash(
32 "Purify/Accountable/AnchorEscrowEphemeral/v4");
33const TaggedHash kProofSeedHash(
34 "Purify/Accountable/AnchorEscrowProofSeed/v4");
35
36Status require_anchor_v4_hash_suite(const char* label) {
37#if PURIFY_USE_LEGACY_FIELD_HASHES
39#else
40 (void)label;
41 return {};
42#endif
43}
44
45using ::purify::detail::WipeOnDestruction;
46
47using SecretBytes32 = ::purify::detail::Secret<bppp::ScalarBytes>;
48
49struct EscrowOpening {
50 const GeneratedKey& ephemeral;
51 const FieldElement& punishment_scalar;
52};
53
54PointBytes punishment_point(const AnchorPunishmentPublicKey& key) {
55 PointBytes out{};
56 out[0] = 0x02;
57 std::copy(key.xonly().begin(), key.xonly().end(), out.begin() + 1);
58 return out;
59}
60
61Status require_roster(const NonceHostBoundSigningRoster& roster,
62 const char* label) {
63 if (roster.members().empty()) {
65 }
66 return {};
67}
68
69Status require_distinct_anchor_root(
70 const UInt512& recovery_root,
71 const UInt512& advertised_anchor_root,
72 const char* label) {
73 if (advertised_anchor_root == recovery_root) {
75 }
76 return {};
77}
78
79Result<FieldElement> shared_pad(const SecretKey& secret,
80 const UInt512& peer) {
81 PURIFY_ASSIGN_OR_RETURN(auto scalars, unpack_secret(secret.packed()),
82 "anchor_escrow:shared_secret");
83 WipeOnDestruction scalars_guard(scalars);
84 PURIFY_ASSIGN_OR_RETURN(const auto& points,
86 "anchor_escrow:shared_peer");
88 auto first,
89 curve1().mul_secret_affine(points.first, scalars.first),
90 "anchor_escrow:shared_first");
91 WipeOnDestruction first_guard(first);
93 auto second,
94 curve2().mul_secret_affine(points.second, scalars.second),
95 "anchor_escrow:shared_second");
96 WipeOnDestruction second_guard(second);
97 return combine(first.x, second.x);
98}
99
100Result<AnchorEscrowCiphertext> encrypt_anchor(
101 const UInt512& recipient, const EscrowOpening& opening) {
103 auto pad,
104 shared_pad(opening.ephemeral.secret, recipient),
105 "anchor_escrow:encrypt_pad");
106 WipeOnDestruction pad_guard(pad);
108 out.ephemeral_key = opening.ephemeral.public_key;
109 out.masked_scalar = opening.punishment_scalar + pad;
110 return out;
111}
112
113Result<Expr> escrow_relation(
114 Transcript& transcript, const UInt512& recipient,
115 const AnchorEscrowCiphertext& ciphertext,
116 const EscrowOpening* opening) {
118 const auto& receiver,
119 unpack_public_points(recipient),
120 "anchor_escrow:recipient");
122 validate_public_points(ciphertext.ephemeral_key),
123 "anchor_escrow:ephemeral");
125 const auto ephemeral_coords,
126 unpack_public(ciphertext.ephemeral_key),
127 "anchor_escrow:ephemeral_coords");
128
129 std::array<std::vector<int>, 2> values{
130 std::vector<int>(255, -1), std::vector<int>(255, -1)};
131 WipeOnDestruction first_values_guard(values[0]);
132 WipeOnDestruction second_values_guard(values[1]);
133 if (opening != nullptr) {
135 auto pair,
136 unpack_secret(opening->ephemeral.secret.packed()),
137 "anchor_escrow:ephemeral_secret");
138 WipeOnDestruction pair_guard(pair);
140 values[0], key_to_bits(pair.first, half_n1()),
141 "anchor_escrow:ephemeral_first_bits");
143 values[1], key_to_bits(pair.second, half_n2()),
144 "anchor_escrow:ephemeral_second_bits");
145 }
146 std::array<std::vector<Expr>, 2> ephemeral_bits;
147 for (std::size_t component = 0; component < 2; ++component) {
148 auto& bits = ephemeral_bits[component];
149 bits.reserve(255);
150 for (const int& bit : values[component]) {
151 auto bit_value =
152 bit < 0
153 ? std::optional<FieldElement>{}
154 : std::optional<FieldElement>(
156 WipeOnDestruction bit_value_guard(bit_value);
157 bits.push_back(transcript.boolean(
158 transcript.secret(bit_value)));
159 }
160 }
161
162 auto punishment_value =
163 opening == nullptr
164 ? std::optional<FieldElement>{}
165 : std::optional<FieldElement>(opening->punishment_scalar);
166 WipeOnDestruction punishment_value_guard(punishment_value);
167 const Expr punishment = transcript.secret(punishment_value);
168 const Expr ephemeral_first = circuit_ec_multiply_x(
169 curve1(), transcript, generator1(), ephemeral_bits[0]);
170 const Expr ephemeral_second = circuit_ec_multiply_x(
171 curve2(), transcript, generator2(), ephemeral_bits[1]);
172 transcript.equal(
173 ephemeral_first,
174 Expr(FieldElement::from_uint256(ephemeral_coords.first)));
175 transcript.equal(
176 ephemeral_second,
177 Expr(FieldElement::from_uint256(ephemeral_coords.second)));
178 const Expr shared_first = circuit_ec_multiply_x(
179 curve1(), transcript, receiver.first, ephemeral_bits[0]);
180 const Expr shared_second = circuit_ec_multiply_x(
181 curve2(), transcript, receiver.second, ephemeral_bits[1]);
182 transcript.equal(
183 punishment + circuit_combine(
184 transcript, shared_first, shared_second),
185 Expr(ciphertext.masked_scalar));
186 return punishment;
187}
188
189Result<NativeBulletproofCircuit> public_escrow_circuit(
190 const UInt512& recipient, const AnchorEscrowCiphertext& ciphertext) {
191 Transcript transcript;
193 const auto output,
194 escrow_relation(transcript, recipient, ciphertext, nullptr),
195 "anchor_escrow:relation");
196 BulletproofTranscript bp;
197 PURIFY_RETURN_IF_ERROR(bp.from_transcript(transcript, 2 * 255, 8192),
198 "anchor_escrow:lower");
200 bp.bind_output_to_commitment(output),
201 "anchor_escrow:bind_punishment");
202 return bp.native_circuit();
203}
204
205Result<BulletproofAssignmentData> escrow_assignment(
206 const UInt512& recipient, const AnchorEscrowCiphertext& ciphertext,
207 const EscrowOpening& opening) {
208 Transcript transcript;
210 const auto output,
211 escrow_relation(transcript, recipient, ciphertext, &opening),
212 "anchor_escrow:witness_relation");
213 BulletproofTranscript bp;
214 PURIFY_RETURN_IF_ERROR(bp.from_transcript(transcript, 2 * 255, 8192),
215 "anchor_escrow:lower_witness");
217 bp.bind_output_to_commitment(output),
218 "anchor_escrow:bind_punishment_witness");
219 return bp.assignment_data(transcript.varmap(),
220 opening.punishment_scalar);
221}
222
223Bytes ciphertext_bytes(const AnchorEscrowCiphertext& ciphertext) {
224 Bytes out;
226 const auto ephemeral = ciphertext.ephemeral_key.to_bytes_be();
227 const auto masked = ciphertext.masked_scalar.to_bytes_be();
228 out.insert(out.end(), ephemeral.begin(), ephemeral.end());
229 out.insert(out.end(), masked.begin(), masked.end());
230 assert(out.size() == AnchorEscrowCiphertext::kSerializedSize);
231 return out;
232}
233
234PointBytes roster_point(const NonceHostBoundSigningRoster& roster) {
235 PointBytes out{};
236 out[0] = 0x02;
237 std::copy(roster.signing_key().begin(), roster.signing_key().end(),
238 out.begin() + 1);
239 return out;
240}
241
242Result<bppp::ScalarBytes> escrow_randomness_statement(
243 const RecoveryBoundSigningRoster& roster,
244 const UInt512& advertised_anchor_root,
245 const AnchorPunishmentPublicKey& punishment_key,
246 const AnchorEscrowContextDigest& context_digest) {
247 PURIFY_RETURN_IF_ERROR(require_roster(roster,
248 "anchor_escrow:empty_roster"),
249 "anchor_escrow:randomness_roster");
251 require_distinct_anchor_root(
252 roster.recovery_root(), advertised_anchor_root,
253 "anchor_escrow:recovery_root_alias"),
254 "anchor_escrow:randomness_anchor_root");
256 validate_public_points(advertised_anchor_root),
257 "anchor_escrow:randomness_anchor_root");
258 const auto recovery_root = roster.recovery_root().to_bytes_be();
259 const auto anchor_root = advertised_anchor_root.to_bytes_be();
260 const auto signing_key = roster_point(roster);
261 const auto punishment = punishment_point(punishment_key);
262 return kRandomnessStatementHash.digest_many<8>({
263 signing_key, roster.key_list_hash(), roster.coalition_core_digest(),
264 roster.nonce_host_commitment(), recovery_root, anchor_root,
265 punishment, context_digest,
266 });
267}
268
269Result<Bytes> escrow_binding(
270 const NonceHostBoundSigningRoster& roster,
271 const UInt512& recovery_root,
272 const UInt512& advertised_anchor_root,
273 const AnchorPunishmentPublicKey& punishment_key,
274 const AnchorEscrowContextDigest& context_digest,
275 const AnchorEscrowCiphertext& ciphertext) {
276 PURIFY_RETURN_IF_ERROR(require_roster(roster,
277 "anchor_escrow:empty_roster"),
278 "anchor_escrow:roster");
280 require_distinct_anchor_root(
281 recovery_root, advertised_anchor_root,
282 "anchor_escrow:recovery_root_alias"),
283 "anchor_escrow:binding_anchor_root");
285 validate_public_points(advertised_anchor_root),
286 "anchor_escrow:binding_anchor_root");
287 const auto recovery_root_bytes = recovery_root.to_bytes_be();
288 const auto anchor_root = advertised_anchor_root.to_bytes_be();
289 const auto encrypted = ciphertext_bytes(ciphertext);
290 const auto signing_key = roster_point(roster);
291 const auto punishment = punishment_point(punishment_key);
292 static const TaggedHash hash("Purify/Accountable/AnchorEscrow/v4");
293 const auto digest = hash.digest_many<9>({
294 signing_key, roster.key_list_hash(), roster.coalition_core_digest(),
295 roster.nonce_host_commitment(), recovery_root_bytes, anchor_root,
296 encrypted,
297 punishment, context_digest,
298 });
299 return Bytes(digest.begin(), digest.end());
300}
301
302Result<Bytes> escrow_binding(
303 const RecoveryBoundSigningRoster& roster,
304 const UInt512& advertised_anchor_root,
305 const AnchorPunishmentPublicKey& punishment_key,
306 const AnchorEscrowContextDigest& context_digest,
307 const AnchorEscrowCiphertext& ciphertext) {
308 return escrow_binding(roster, roster.recovery_root(),
309 advertised_anchor_root, punishment_key,
310 context_digest, ciphertext);
311}
312
313Result<AnchorPunishmentKey> decrypt_punishment(
314 const GeneratedKey& recipient,
315 const AnchorPunishmentPublicKey& punishment_key,
316 const AnchorEscrowCiphertext& ciphertext,
317 purify_secp_context* secp_context) {
319 auto pad,
320 shared_pad(recipient.secret, ciphertext.ephemeral_key),
321 "anchor_escrow:decrypt_pad");
322 WipeOnDestruction pad_guard(pad);
323 auto plaintext = (ciphertext.masked_scalar - pad).to_bytes_be();
324 WipeOnDestruction plaintext_guard(plaintext);
326 auto recovered,
327 AnchorPunishmentKey::from_secret(plaintext, secp_context),
328 "anchor_escrow:decrypt_plaintext");
329 if (recovered.bip340_key().seckey != plaintext) {
331 "anchor_escrow:noncanonical_plaintext");
332 }
333 if (recovered.public_key() != punishment_key) {
335 "anchor_escrow:wrong_plaintext");
336 }
337 return recovered;
338}
339
340} // namespace
341
343 const std::array<unsigned char, 32>& xonly,
344 purify_secp_context* secp_context) {
346 require_secp_context(secp_context,
347 "anchor_escrow:punishment_public_key"),
348 "anchor_escrow:punishment_public_key");
349 if (purify_bip340_validate_xonly_pubkey(secp_context, xonly.data()) ==
350 0) {
352 "anchor_escrow:punishment_public_key");
353 }
355}
356
358 std::array<unsigned char, 32> secret,
359 purify_secp_context* secp_context) {
360 WipeOnDestruction secret_guard(secret);
362 require_secp_context(secp_context,
363 "anchor_escrow:punishment_secret"),
364 "anchor_escrow:punishment_secret");
365 Bip340Key key;
366 key.seckey = secret;
368 secp_context, key.seckey.data(), key.xonly_pubkey.data()) == 0) {
370 "anchor_escrow:punishment_secret");
371 }
372 return AnchorPunishmentKey(std::move(key));
373}
374
377 const UInt512& recipient,
378 const AnchorEscrowCiphertext& ciphertext) {
380 require_anchor_v4_hash_suite("anchor_escrow:shape_hash_suite"),
381 "anchor_escrow:shape_hash_suite");
382 Transcript transcript;
384 const auto output,
385 escrow_relation(transcript, recipient, ciphertext, nullptr),
386 "anchor_escrow:shape_relation");
387 const auto relation_multiplications = transcript.muls().size();
389 PURIFY_RETURN_IF_ERROR(bp.from_transcript(transcript, 2 * 255, 8192),
390 "anchor_escrow:shape_lower");
392 bp.bind_output_to_commitment(output),
393 "anchor_escrow:shape_bind");
395 relation_multiplications, bp.native_circuit().n_gates};
396}
397
398Status AnchorEscrowRecord::validate_encoding(
399 purify_secp_context* secp_context) const {
401 require_secp_context(secp_context, "anchor_escrow:record"),
402 "anchor_escrow:record");
405 "anchor_escrow:ciphertext_ephemeral");
406 static constexpr std::size_t kRoundCount = 14;
407 static_assert(kNormProofSize == 65 * kRoundCount + 64);
409 raw_commitment_, mask_commitment_, t2_,
410 public_commitment_dlog_nonce_,
411 public_commitment_dlog_response_, norm_proof_};
412 return bppp::candidate::
415 kRoundCount, secp_context);
416}
417
419 purify_secp_context* secp_context) const {
420 PURIFY_RETURN_IF_ERROR(validate_encoding(secp_context),
421 "anchor_escrow:serialize");
422 Bytes out;
423 out.reserve(kSerializedSize);
424 out.push_back(kSerializationVersion);
425 const auto encrypted = ciphertext_bytes(ciphertext_);
426 out.insert(out.end(), encrypted.begin(), encrypted.end());
427 out.insert(out.end(), raw_commitment_.begin(), raw_commitment_.end());
428 out.insert(out.end(), mask_commitment_.begin(), mask_commitment_.end());
429 out.insert(out.end(), t2_.begin(), t2_.end());
430 out.insert(out.end(), public_commitment_dlog_nonce_.begin(),
431 public_commitment_dlog_nonce_.end());
432 out.insert(out.end(), public_commitment_dlog_response_.begin(),
433 public_commitment_dlog_response_.end());
434 out.insert(out.end(), norm_proof_.begin(), norm_proof_.end());
435 assert(out.size() == kSerializedSize);
436 return out;
437}
438
440 std::span<const unsigned char> encoded,
441 purify_secp_context* secp_context) {
442 if (encoded.size() != kSerializedSize) {
444 "anchor_escrow:deserialize_size");
445 }
446 if (encoded[0] != kSerializationVersion) {
448 "anchor_escrow:deserialize_version");
449 }
451 std::size_t offset = 1;
452 out.ciphertext_.ephemeral_key =
453 UInt512::from_bytes_be(encoded.data() + offset, 64);
454 offset += 64;
455 std::array<unsigned char, 32> masked{};
456 std::copy_n(encoded.begin() + offset, masked.size(), masked.begin());
457 offset += masked.size();
459 out.ciphertext_.masked_scalar,
461 "anchor_escrow:deserialize_masked");
462 std::copy_n(encoded.begin() + offset, out.raw_commitment_.size(),
463 out.raw_commitment_.begin());
464 offset += out.raw_commitment_.size();
465 std::copy_n(encoded.begin() + offset, out.mask_commitment_.size(),
466 out.mask_commitment_.begin());
467 offset += out.mask_commitment_.size();
468 std::copy_n(encoded.begin() + offset, out.t2_.size(),
469 out.t2_.begin());
470 offset += out.t2_.size();
471 std::copy_n(encoded.begin() + offset,
472 out.public_commitment_dlog_nonce_.size(),
473 out.public_commitment_dlog_nonce_.begin());
474 offset += out.public_commitment_dlog_nonce_.size();
475 std::copy_n(encoded.begin() + offset,
476 out.public_commitment_dlog_response_.size(),
477 out.public_commitment_dlog_response_.begin());
478 offset += out.public_commitment_dlog_response_.size();
479 out.norm_proof_.assign(encoded.begin() + offset, encoded.end());
480 PURIFY_RETURN_IF_ERROR(out.validate_encoding(secp_context),
481 "anchor_escrow:deserialize");
482 return out;
483}
484
486 const RecoveryBoundSigningRoster& roster,
487 const UInt512& advertised_anchor_root,
488 const AnchorPunishmentKey& punishment_key,
489 const AnchorEscrowContextDigest& context_digest,
490 purify_secp_context* secp_context,
493 require_anchor_v4_hash_suite("anchor_escrow:create_hash_suite"),
494 "anchor_escrow:create_hash_suite");
496 require_secp_context(secp_context, "anchor_escrow:create"),
497 "anchor_escrow:create");
498 SecretBytes32 auxiliary_randomness;
499 PURIFY_RETURN_IF_ERROR(fill_secure_random(auxiliary_randomness.value),
500 "anchor_escrow:auxiliary_randomness");
502 roster, advertised_anchor_root, punishment_key, context_digest,
503 auxiliary_randomness.value, secp_context, cache);
504}
505
507 AnchorEscrowAuxiliaryScalar&& other) noexcept
508 : bytes_(other.bytes_) {
509 other.clear();
510}
511
513 AnchorEscrowAuxiliaryScalar&& other) noexcept {
514 if (this != &other) {
515 clear();
516 bytes_ = other.bytes_;
517 other.clear();
518 }
519 return *this;
520}
521
525
526void AnchorEscrowAuxiliaryScalar::clear() noexcept {
527 detail::secure_clear_bytes(bytes_.data(), bytes_.size());
528}
529
532 auto scalar = FieldElement::try_from_bytes32(encoded);
533 if (!scalar.has_value()) {
535 "anchor_escrow:auxiliary_scalar_encoding");
536 }
537 WipeOnDestruction scalar_guard(*scalar);
538 if (scalar->is_zero()) {
540 "anchor_escrow:auxiliary_scalar_zero");
541 }
542 return AnchorEscrowAuxiliaryScalar{encoded};
543}
544
546 const RecoveryBoundSigningRoster& roster,
547 const UInt512& advertised_anchor_root,
548 const AnchorPunishmentKey& punishment_key,
549 const AnchorEscrowContextDigest& context_digest,
550 const AnchorEscrowAuxiliaryScalar& auxiliary_scalar,
551 purify_secp_context* secp_context,
554 roster, advertised_anchor_root, punishment_key, context_digest,
555 auxiliary_scalar.bytes_, secp_context, cache);
556}
557
559 const RecoveryBoundSigningRoster& roster,
560 const UInt512& advertised_anchor_root,
561 const AnchorPunishmentKey& punishment_key,
562 const AnchorEscrowContextDigest& context_digest,
563 const bppp::ScalarBytes& auxiliary_randomness,
564 purify_secp_context* secp_context,
567 require_anchor_v4_hash_suite(
568 "anchor_escrow:create_aux_hash_suite"),
569 "anchor_escrow:create_aux_hash_suite");
571 require_secp_context(secp_context, "anchor_escrow:create_aux"),
572 "anchor_escrow:create_aux");
573 PURIFY_RETURN_IF_ERROR(require_roster(roster,
574 "anchor_escrow:empty_roster"),
575 "anchor_escrow:create_roster");
577 const auto& randomness_statement,
578 escrow_randomness_statement(roster,
579 advertised_anchor_root,
580 punishment_key.public_key(),
581 context_digest),
582 "anchor_escrow:randomness_statement");
583 const auto& punishment_secret = punishment_key.bip340_key().seckey;
585 auto punishment_scalar,
586 FieldElement::try_from_bytes32(punishment_secret),
587 "anchor_escrow:punishment_scalar");
588 WipeOnDestruction punishment_scalar_guard(punishment_scalar);
589 SecretBytes32 randomness_master;
590 {
591 auto digest = kRandomnessMasterHash.digest_many<3>({
592 auxiliary_randomness, punishment_secret, randomness_statement});
593 WipeOnDestruction digest_guard(digest);
594 randomness_master.value = digest;
595 }
596 SecretBytes32 ephemeral_seed;
597 {
598 auto digest = kEphemeralHash.digest(randomness_master.value);
599 WipeOnDestruction digest_guard(digest);
600 ephemeral_seed.value = digest;
601 }
602 PURIFY_ASSIGN_OR_RETURN(auto ephemeral,
603 generate_key(ephemeral_seed.value),
604 "anchor_escrow:ephemeral");
605 EscrowOpening opening{
606 ephemeral,
607 punishment_scalar,
608 };
610 const auto& ciphertext,
611 encrypt_anchor(roster.recovery_root(), opening),
612 "anchor_escrow:encrypt");
614 const auto& circuit,
615 public_escrow_circuit(roster.recovery_root(), ciphertext),
616 "anchor_escrow:public_circuit");
618 auto assignment,
619 escrow_assignment(roster.recovery_root(), ciphertext, opening),
620 "anchor_escrow:assignment");
622 const auto& binding,
623 escrow_binding(roster, advertised_anchor_root,
624 punishment_key.public_key(), context_digest,
625 ciphertext),
626 "anchor_escrow:binding");
627 SecretBytes32 prover_seed;
628 {
629 auto digest = kProofSeedHash.digest_many<2>({
630 randomness_master.value, binding});
631 WipeOnDestruction digest_guard(digest);
632 prover_seed.value = digest;
633 }
634 const std::array<PointBytes, 1> outputs{
635 punishment_point(punishment_key.public_key())};
637 auto publication,
639 prove_circuit_zk_norm_arg_v4_with_public_commitments(
640 circuit, assignment, prover_seed.value, outputs,
641 secp_context, binding, cache),
642 "anchor_escrow:prove");
643 AnchorEscrowRecord record;
644 record.ciphertext_ = ciphertext;
645 record.raw_commitment_ = publication.proof.raw_commitment;
646 record.mask_commitment_ = publication.proof.mask_commitment;
647 record.t2_ = publication.proof.t2;
648 record.public_commitment_dlog_nonce_ =
649 publication.proof.public_commitment_dlog_nonce;
650 record.public_commitment_dlog_response_ =
651 publication.proof.public_commitment_dlog_response;
652 record.norm_proof_ = std::move(publication.proof.norm_proof);
653 PURIFY_RETURN_IF_ERROR(record.validate_encoding(secp_context),
654 "anchor_escrow:proof_encoding");
655 return record;
656}
657
659 const RecoveryBoundSigningRoster& roster,
660 const UInt512& advertised_anchor_root,
661 const AnchorPunishmentPublicKey& punishment_key,
662 const AnchorEscrowContextDigest& context_digest,
663 const AnchorEscrowRecord& record,
664 purify_secp_context* secp_context,
667 require_anchor_v4_hash_suite("anchor_escrow:verify_hash_suite"),
668 "anchor_escrow:verify_hash_suite");
670 require_secp_context(secp_context, "anchor_escrow:verify"),
671 "anchor_escrow:verify");
672 PURIFY_RETURN_IF_ERROR(require_roster(roster,
673 "anchor_escrow:empty_roster"),
674 "anchor_escrow:verify_roster");
675 PURIFY_RETURN_IF_ERROR(record.validate_encoding(secp_context),
676 "anchor_escrow:verify_record");
678 const auto& circuit,
679 public_escrow_circuit(roster.recovery_root(), record.ciphertext_),
680 "anchor_escrow:verify_circuit");
682 const auto& binding,
683 escrow_binding(roster, advertised_anchor_root, punishment_key,
684 context_digest, record.ciphertext_),
685 "anchor_escrow:verify_binding");
686 const std::array<PointBytes, 1> outputs{
687 punishment_point(punishment_key)};
689 record.raw_commitment_, record.mask_commitment_, record.t2_,
690 record.public_commitment_dlog_nonce_,
691 record.public_commitment_dlog_response_, record.norm_proof_};
693 auto publication,
695 reconstruct_circuit_zk_norm_arg_publication_v4_with_public_commitments(
696 circuit, std::move(proof), outputs, secp_context, binding),
697 "anchor_escrow:publication");
698 return bppp::candidate::
700 circuit, publication, outputs, secp_context, binding, cache);
701}
702
704 purify_secp_context* secp_context) {
705 static_assert(AnchorEscrowRecord::kNormProofSize == 65 * 14 + 64);
709 auto basis,
712 "Purify/Accountable/ConstructionLeafAccumulation/v1",
713 secp_context),
714 "construction_leaf_accumulation:basis");
715 return ConstructionLeafAccumulator(std::move(basis));
716}
717
720 const RecoveryBoundSigningRoster& roster,
721 const UInt512& advertised_anchor_root,
722 const AnchorPunishmentPublicKey& punishment_key,
723 const AnchorEscrowContextDigest& context_digest,
724 const AnchorEscrowCiphertext& ciphertext,
725 const bppp::ScalarBytes& circuit_precommitment,
726 purify_secp_context* secp_context) const {
728 roster, roster.recovery_root(), advertised_anchor_root,
729 punishment_key, context_digest, ciphertext,
730 circuit_precommitment, secp_context);
731}
732
735 const NonceHostBoundSigningRoster& roster,
736 const UInt512& recovery_root,
737 const UInt512& advertised_anchor_root,
738 const AnchorPunishmentPublicKey& punishment_key,
739 const AnchorEscrowContextDigest& context_digest,
740 const AnchorEscrowCiphertext& ciphertext,
741 const bppp::ScalarBytes& circuit_precommitment,
742 purify_secp_context* secp_context) const {
744 require_anchor_v4_hash_suite(
745 "anchor_escrow_parent:hash_suite"),
746 "anchor_escrow_parent:hash_suite");
748 require_secp_context(secp_context, "anchor_escrow_parent:context"),
749 "anchor_escrow_parent:context");
751 require_roster(roster, "anchor_escrow_parent:empty_roster"),
752 "anchor_escrow_parent:roster");
754 const auto precommitment,
756 circuit_precommitment),
757 "anchor_escrow_parent:precommitment");
759 const auto circuit,
760 public_escrow_circuit(recovery_root, ciphertext),
761 "anchor_escrow_parent:circuit");
763 const auto binding,
764 escrow_binding(roster, recovery_root, advertised_anchor_root,
765 punishment_key, context_digest, ciphertext),
766 "anchor_escrow_parent:binding");
767 const std::array<PointBytes, 1> outputs{
768 punishment_point(punishment_key)};
770 auto constants,
772 derive_circuit_norm_parent_constants_v4_with_public_commitments(
773 circuit, precommitment, outputs, secp_context, binding),
774 "anchor_escrow_parent:derive");
776 std::move(constants.circuit_binding_digest),
777 std::move(constants.raw_generator_digest),
778 std::move(constants.coefficient_commitment),
779 std::move(constants.anchored_offset)};
780}
781
784 const RecoveryBoundSigningRoster& roster,
785 const UInt512& advertised_anchor_root,
786 const AnchorPunishmentPublicKey& punishment_key,
787 const AnchorEscrowContextDigest& context_digest,
788 const AnchorEscrowRecord& record,
789 purify_secp_context* secp_context,
792 require_anchor_v4_hash_suite(
793 "anchor_escrow_accumulation:prepare_hash_suite"),
794 "anchor_escrow_accumulation:prepare_hash_suite");
796 require_secp_context(secp_context,
797 "anchor_escrow_accumulation:prepare"),
798 "anchor_escrow_accumulation:prepare");
800 require_roster(roster, "anchor_escrow_accumulation:empty_roster"),
801 "anchor_escrow_accumulation:roster");
802 PURIFY_RETURN_IF_ERROR(record.validate_encoding(secp_context),
803 "anchor_escrow_accumulation:record");
805 const auto& circuit,
806 public_escrow_circuit(roster.recovery_root(), record.ciphertext_),
807 "anchor_escrow_accumulation:circuit");
809 const auto& binding,
810 escrow_binding(roster, advertised_anchor_root, punishment_key,
811 context_digest, record.ciphertext_),
812 "anchor_escrow_accumulation:binding");
813 const std::array<PointBytes, 1> outputs{
814 punishment_point(punishment_key)};
816 record.raw_commitment_, record.mask_commitment_, record.t2_,
817 record.public_commitment_dlog_nonce_,
818 record.public_commitment_dlog_response_, record.norm_proof_};
820 auto publication,
822 reconstruct_circuit_zk_norm_arg_publication_v4_with_public_commitments(
823 circuit, std::move(proof), outputs, secp_context, binding),
824 "anchor_escrow_accumulation:publication");
825 const auto circuit_precommitment =
826 publication.precommitment.bytes();
828 auto prepared,
830 prepare_circuit_zk_norm_arg_v4_with_public_commitments(
831 circuit, publication, outputs, secp_context, binding,
832 cache),
833 "anchor_escrow_accumulation:outer");
834 if (!prepared.has_value()) {
836 "anchor_escrow_accumulation:outer");
837 }
839 auto norm,
841 prepare_v4_circuit_norm_for_algebraic_reduction(
842 basis_, std::move(*prepared), secp_context),
843 "anchor_escrow_accumulation:norm");
844 return PreparedConstructionLeaf(circuit_precommitment, std::move(norm));
845}
846
848 const RecoveryBoundSigningRoster& roster,
849 const FieldElement& exposed_signing_scalar,
850 const UInt512& advertised_anchor_root,
851 const AnchorPunishmentPublicKey& punishment_key,
852 const AnchorEscrowContextDigest& context_digest,
853 const AnchorEscrowRecord& record,
854 purify_secp_context* secp_context,
857 require_anchor_v4_hash_suite("anchor_escrow:recover_hash_suite"),
858 "anchor_escrow:recover_hash_suite");
860 const auto& recipient,
861 recover_registered_root(roster, exposed_signing_scalar,
862 secp_context),
863 "anchor_escrow:recover_recipient");
865 const auto& accepted,
866 verify_anchor_escrow(roster, advertised_anchor_root,
867 punishment_key, context_digest, record,
868 secp_context, cache),
869 "anchor_escrow:recover_verify");
870 if (!accepted) {
872 "anchor_escrow:invalid_proof");
873 }
875 recipient, punishment_key, record.ciphertext(), secp_context);
876}
877
880 const FieldElement& exposed_signing_scalar,
881 const UInt512& advertised_anchor_root,
882 const AnchorPunishmentPublicKey& punishment_key,
883 const AnchorEscrowContextDigest& context_digest,
884 const AnchorEscrowRecord& record,
885 purify_secp_context* secp_context,
888 require_anchor_v4_hash_suite(
889 "anchor_escrow:recover_distributed_hash_suite"),
890 "anchor_escrow:recover_distributed_hash_suite");
892 const auto& recipient,
894 setup.roster(), setup.aggregate(), exposed_signing_scalar,
895 secp_context),
896 "anchor_escrow:recover_distributed_recipient");
898 const auto accepted,
900 setup.roster(), advertised_anchor_root, punishment_key,
901 context_digest, record, secp_context, cache),
902 "anchor_escrow:recover_distributed_verify");
903 if (!accepted) {
904 return unexpected_error(
906 "anchor_escrow:recover_distributed_proof");
907 }
909 recipient, punishment_key, record.ciphertext(), secp_context);
910}
911
913 const GeneratedKey& recovered_recipient,
914 const AnchorPunishmentPublicKey& punishment_key,
915 const AnchorEscrowCiphertext& ciphertext,
916 purify_secp_context* secp_context) {
918 require_anchor_v4_hash_suite(
919 "anchor_escrow:recover_authenticated_hash_suite"),
920 "anchor_escrow:recover_authenticated_hash_suite");
921 return decrypt_punishment(recovered_recipient, punishment_key,
922 ciphertext, secp_context);
923}
924
925} // namespace purify::accountable
Lowering helper that converts a symbolic transcript into native Bulletproof witness and circuit forms...
Status from_transcript(const Transcript &transcript, std::size_t n_bits, std::size_t minimum_gate_count=1)
Imports a symbolic transcript with a fixed power-of-two gate floor.
Status bind_output_to_commitment(Expr output)
Binds a scalar expression to public commitment slot zero.
NativeBulletproofCircuit native_circuit() const
Builds the native sparse circuit object from the lowered assignments and constraints.
Field element modulo the backend scalar field used by this implementation.
Definition numeric.hpp:834
static FieldElement from_uint256(const UInt256 &value)
Converts a 256-bit unsigned integer into the scalar field representation.
Definition numeric.cpp:73
static Result< FieldElement > try_from_bytes32(const std::array< unsigned char, 32 > &bytes)
Decodes a canonical 32-byte big-endian field element.
Definition numeric.cpp:53
static FieldElement from_int(std::int64_t value)
Constructs a field element from a signed integer, reducing negatives modulo the field.
Definition numeric.cpp:46
Mutable transcript used to record symbolic multiplication, division, and boolean constraints.
Definition expr.hpp:215
const std::vector< MulConstraint > & muls() const
Returns the multiplication and division constraints accumulated so far.
Definition expr.hpp:255
AnchorEscrowAuxiliaryScalar & operator=(const AnchorEscrowAuxiliaryScalar &)=delete
static Result< AnchorEscrowAuxiliaryScalar > from_bytes(const Bytes &encoded)
Result< Bytes > serialize(purify_secp_context *secp_context) const
AnchorEscrowRecord(const AnchorEscrowRecord &)=default
const AnchorEscrowCiphertext & ciphertext() const noexcept
static constexpr std::size_t kSerializedSize
static constexpr std::size_t kNormProofSize
static constexpr unsigned char kSerializationVersion
static Result< AnchorEscrowRecord > deserialize(std::span< const unsigned char > encoded, purify_secp_context *secp_context)
const AnchorPunishmentPublicKey & public_key() const noexcept
AnchorPunishmentKey(const AnchorPunishmentKey &)=delete
static Result< AnchorPunishmentKey > from_secret(std::array< unsigned char, 32 > secret, purify_secp_context *secp_context)
const Bip340Key & bip340_key() const noexcept
AnchorPunishmentPublicKey(const AnchorPunishmentPublicKey &)=default
const std::array< unsigned char, 32 > & xonly() const noexcept
static Result< AnchorPunishmentPublicKey > create(const std::array< unsigned char, 32 > &xonly, purify_secp_context *secp_context)
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
static Result< ConstructionLeafAccumulator > create(purify_secp_context *secp_context)
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
ConstructionLeafAccumulator(const ConstructionLeafAccumulator &)=delete
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
An ordered signing roster bound to one core and nonce-host list.
A nonce-host-bound roster with an authorized recovery-root relation.
const RecoveryBoundSigningRoster & roster() const noexcept
const RecoveryRootShareAggregate & aggregate() const noexcept
Common interface for reusable experimental BPPP backend state.
Definition bppp.hpp:129
static Result< Basis > create(std::size_t n_vec_len, std::size_t l_vec_len, std::string_view semantic_domain, purify_secp_context *context)
static Result< CircuitPrecommitmentDigestV4 > parse(const ScalarBytes &encoded)
#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< GeneratedKey > recover_aggregated_recovery_root(const NonceHostBoundSigningRoster &roster, const RecoveryRootShareAggregate &aggregate, const FieldElement &exposed_signing_scalar, purify_secp_context *secp_context)
Result< AnchorPunishmentKey > recover_distributed_escrowed_anchor(const VerifiedDistributedRecoverySetup &setup, const FieldElement &exposed_signing_scalar, 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)
Result< AnchorEscrowRecord > create_anchor_escrow_deterministic(const RecoveryBoundSigningRoster &roster, const UInt512 &advertised_anchor_root, const AnchorPunishmentKey &punishment_key, const AnchorEscrowContextDigest &context_digest, const AnchorEscrowAuxiliaryScalar &auxiliary_scalar, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *cache=nullptr)
std::array< unsigned char, 32 > AnchorEscrowContextDigest
Result< AnchorPunishmentKey > recover_authenticated_anchor_ciphertext(const GeneratedKey &recovered_recipient, const AnchorPunishmentPublicKey &punishment_key, const AnchorEscrowCiphertext &ciphertext, purify_secp_context *secp_context)
Result< AnchorEscrowRecord > create_anchor_escrow_with_aux_randomness(const RecoveryBoundSigningRoster &roster, const UInt512 &advertised_anchor_root, const AnchorPunishmentKey &punishment_key, const AnchorEscrowContextDigest &context_digest, const bppp::ScalarBytes &auxiliary_randomness, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *cache)
Result< AnchorEscrowRecord > create_anchor_escrow(const RecoveryBoundSigningRoster &roster, const UInt512 &advertised_anchor_root, const AnchorPunishmentKey &punishment_key, const AnchorEscrowContextDigest &context_digest, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *cache=nullptr)
Result< AnchorPunishmentKey > recover_escrowed_anchor(const RecoveryBoundSigningRoster &roster, const FieldElement &exposed_signing_scalar, 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)
Result< AnchorEscrowCircuitShape > anchor_escrow_circuit_shape_for_testing(const UInt512 &recipient, const AnchorEscrowCiphertext &ciphertext)
Result< GeneratedKey > recover_registered_root(const RecoveryBoundSigningRoster &roster, const FieldElement &exposed_signing_scalar, purify_secp_context *secp_context)
Result< bool > verify_anchor_escrow(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)
Result< bool > verify_circuit_zk_norm_arg_v4_with_public_commitments(const NativeBulletproofCircuit &circuit, const CircuitZkNormArgPublicationV4 &publication, std::span< const PointBytes > public_commitments, purify_secp_context *secp_context, std::span< const unsigned char > statement_binding, ExperimentalCircuitBackend *cache=nullptr)
Definition bppp.cpp:3772
Status validate_circuit_zk_norm_arg_proof_v4_fixed_encoding(const CircuitZkNormArgProofV4 &proof, CircuitCommitmentModeV4 mode, std::size_t round_count, purify_secp_context *secp_context)
Definition bppp.cpp:3323
std::array< unsigned char, 33 > PointBytes
Compressed 33-byte curve-point encoding.
Definition bppp.hpp:32
std::array< unsigned char, 32 > ScalarBytes
Big-endian 32-byte scalar encoding.
Definition bppp.hpp:30
void secure_clear_bytes(void *data, std::size_t size) noexcept
Definition secret.hpp:22
Status require_secp_context(const purify_secp_context *context, const char *error_context)
Definition common.hpp:56
const UInt256 & half_n1()
Returns floor(order_n1 / 2).
Definition curve.cpp:219
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
BigUInt< 8 > UInt512
512-bit unsigned integer used for private and packed public keys.
Definition numeric.hpp:821
Result< std::vector< int > > key_to_bits(UInt256 n, const UInt256 &max_value)
Encodes a scalar into the signed 3-bit window bit schedule used by the circuit.
Definition curve.cpp:413
Result< GeneratedKey > generate_key()
Generates a random Purify keypair using the built-in OS RNG.
Definition api.cpp:117
Result< std::pair< UInt256, UInt256 > > unpack_public(const UInt512 &packed)
Splits a packed public key into its two x-coordinates.
Definition curve.cpp:360
const UInt256 & half_n2()
Returns floor(order_n2 / 2).
Definition curve.cpp:224
Expr circuit_combine(Transcript &transcript, const Expr &x1, const Expr &x2)
Builds the symbolic Purify output combiner over two x-coordinates.
const EllipticCurve & curve1()
Returns the first Purify curve instance.
Definition curve.cpp:261
Expr circuit_ec_multiply_x(const EllipticCurve &curve, Transcript &transcript, const JacobianPoint &point, const std::vector< Expr > &bits)
Builds the symbolic x-coordinate multiplication gadget for one curve point.
Expected< T, Error > Result
Expected-returning convenience alias for Purify value-producing APIs.
Definition error.hpp:99
const JacobianPoint & generator1()
Returns the fixed generator for the first curve.
Definition curve.cpp:282
Result< std::pair< UInt256, UInt256 > > unpack_secret(const UInt512 &z)
Splits a packed private key into its two per-curve secret scalars.
Definition curve.cpp:326
Expected< void, Error > Status
Expected-returning convenience alias for Purify status-only APIs.
Definition error.hpp:102
Result< std::pair< JacobianPoint, JacobianPoint > > unpack_public_points(const UInt512 &packed)
Decodes and validates both curve points in a packed public key.
Definition curve.cpp:370
std::vector< unsigned char > Bytes
Dynamically sized byte string used for messages, serialized witnesses, and proofs.
Definition common.hpp:99
const EllipticCurve & curve2()
Returns the second Purify curve instance.
Definition curve.cpp:266
const JacobianPoint & generator2()
Returns the fixed generator for the second curve.
Definition curve.cpp:293
FieldElement combine(const FieldElement &x1, const FieldElement &x2)
Applies the Purify curve-combination map to two x-coordinates.
Definition curve.cpp:405
Status fill_secure_random(std::span< unsigned char > bytes) noexcept
Fills a buffer with operating-system randomness.
Definition api.cpp:108
Status validate_public_points(const UInt512 &packed)
Validates both curve points in a packed public key.
Definition curve.cpp:393
Narrow C ABI exposing secp256k1 scalar and HMAC helpers to the C++ headers.
int purify_bip340_key_from_seckey(purify_secp_context *context, unsigned char seckey32[32], unsigned char xonly_pubkey32[32])
Canonicalizes a valid secp256k1 secret key for BIP340 and derives its x-only public key.
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.
static BigUInt from_bytes_be(const unsigned char *data, std::size_t size)
Definition numeric.hpp:238
Canonical BIP340 keypair derived deterministically from a packed Purify secret.
Definition api.hpp:38
std::array< unsigned char, 32 > seckey
Definition api.hpp:62
std::array< unsigned char, 32 > xonly_pubkey
Definition api.hpp:63
Derived Purify keypair bundle with an owned packed secret and its matching public key.
Definition api.hpp:24
static constexpr std::size_t kSerializedSize
void PURIFY_UINT_FN to_bytes_be(unsigned char out[PURIFY_UINT_WORDS *8], const uint64_t value[PURIFY_UINT_WORDS])
Definition uint_impl.h:227
int PURIFY_UINT_FN bit(const uint64_t value[PURIFY_UINT_WORDS], size_t index)
Definition uint_impl.h:133