purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
recovery_root.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
11
12#include <algorithm>
13#include <array>
14#include <cassert>
15#include <cstdint>
16#include <optional>
17#include <utility>
18#include <vector>
19
21#include "purify/secp_bridge.h"
23#include "secure_cleanup.hpp"
24
25namespace purify::accountable {
26namespace {
27
29using ::purify::detail::WipeOnDestruction;
30
31using SecretScalarBytes = ::purify::detail::Secret<bppp::ScalarBytes>;
32
33Status require_recovery_contribution_hash_suite(const char* label) {
34#if PURIFY_USE_LEGACY_FIELD_HASHES
36#else
37 (void)label;
38 return {};
39#endif
40}
41
42struct CenteredScalarRelation {
43 Expr sign;
44 Expr magnitude;
45 std::vector<Expr> recoded_bits;
46 std::size_t n_bits = 0;
47};
48
49struct ContributionRelation {
50 Expr output;
51 std::size_t n_bits = 0;
52};
53
54UInt256 half_secp_order() {
55 return prime_p().shifted_right(1);
56}
57
58struct CanonicalScalar {
59 UInt256 magnitude{};
60 bool negated = false;
61};
62
63CanonicalScalar canonical_scalar(const FieldElement& scalar) {
64 CanonicalScalar out{scalar.to_uint256(), false};
65 FieldElement negative_scalar = scalar.negate();
66 WipeOnDestruction negative_scalar_guard(negative_scalar);
67 UInt256 negative = negative_scalar.to_uint256();
68 WipeOnDestruction negative_guard(negative);
69 out.negated = half_secp_order() < out.magnitude;
70 out.magnitude.conditional_assign(negative, out.negated);
71 return out;
72}
73
74UInt256 magnitude(const FieldElement& scalar) {
75 auto canonical = canonical_scalar(scalar);
76 WipeOnDestruction canonical_guard(canonical);
77 return canonical.magnitude;
78}
79
80std::array<unsigned char, 8> encode_u64(std::size_t value) {
81 std::array<unsigned char, 8> out{};
82 const auto encoded = static_cast<std::uint64_t>(value);
83 for (std::size_t i = 0; i < out.size(); ++i) {
84 out[out.size() - 1 - i] =
85 static_cast<unsigned char>(encoded >> (8 * i));
86 }
87 return out;
88}
89
90PointBytes roster_point(const NonceHostBoundSigningRoster& roster) {
91 PointBytes out{};
92 out[0] = 0x02;
93 std::copy(roster.signing_key().begin(), roster.signing_key().end(),
94 out.begin() + 1);
95 return out;
96}
97
98Result<PointBytes> secp_point(const FieldElement& value,
99 purify_secp_context* context) {
100 const bppp::ScalarBytes zero{};
101 const auto generator = bppp::base_generator(context);
102 SecretScalarBytes encoded_value;
104 encoded_value.value.data(),
106 return bppp::pedersen_commit_char(zero, encoded_value.value, context,
107 generator, generator);
108}
109
110Result<PointBytes> weighted_member_point(
111 const NonceHostBoundSigningRoster& roster, std::size_t member_index,
112 purify_secp_context* secp_context) {
113 if (member_index >= roster.members().size()) {
115 "recovery_contribution:member_index");
116 }
117 const auto& member = roster.members()[member_index];
118 const auto coefficient = member.coefficient.to_bytes_be();
119 PointBytes weighted{};
120 if (purify_point_scale(secp_context, member.public_key.data(),
121 coefficient.data(), weighted.data()) == 0) {
123 "recovery_contribution:weighted_key");
124 }
125 return weighted;
126}
127
128Result<AffinePoint> decode_companion_point(
129 std::span<const unsigned char> encoded, const EllipticCurve& curve,
130 const char* label) {
131 if (encoded.size() != 33) {
133 }
134 if (encoded[0] != 0x02 && encoded[0] != 0x03) {
136 }
137 std::array<unsigned char, 32> x_bytes{};
138 std::copy(encoded.begin() + 1, encoded.end(), x_bytes.begin());
139 PURIFY_ASSIGN_OR_RETURN(const auto x,
140 FieldElement::try_from_bytes32(x_bytes), label);
141 auto point = curve.lift_x(x);
142 if (!point || point->infinity) {
144 }
145 auto affine = curve.affine(*point);
146 if (affine.y.is_odd() != (encoded[0] == 0x03)) {
147 affine.y = affine.y.negate();
148 }
149 const JacobianPoint exact{
150 affine.x, affine.y, FieldElement::one(), false};
151 if (!curve.mul(exact, curve.order()).infinity) {
153 }
154 return affine;
155}
156
157Status validate_recovery_root_share_count(std::size_t member_count) {
158 if (member_count == 0) {
160 "recovery_share_aggregate:empty");
161 }
162 if (member_count > RecoveryRootShareAggregate::kMaxMembers) {
164 "recovery_share_aggregate:member_count");
165 }
166 return {};
167}
168
169UInt256 reduce_mod(const UInt256& value, const UInt256& modulus) {
170 return divmod_same(value, modulus).second;
171}
172
173UInt256 add_mod(UInt256 lhs, const UInt256& rhs,
174 const UInt256& modulus) {
175 assert(lhs < modulus && rhs < modulus);
176 auto distance = modulus;
177 distance.sub_assign(rhs);
178 if (lhs < distance) {
179 lhs.add_assign(rhs);
180 } else {
181 lhs.sub_assign(distance);
182 }
183 return lhs;
184}
185
186UInt256 subtract_mod(UInt256 lhs, const UInt256& rhs,
187 const UInt256& modulus) {
188 assert(lhs < modulus && rhs < modulus);
189 if (!(lhs < rhs)) {
190 lhs.sub_assign(rhs);
191 return lhs;
192 }
193 auto difference = rhs;
194 difference.sub_assign(lhs);
195 auto out = modulus;
196 out.sub_assign(difference);
197 return out;
198}
199
200Result<UInt256> centered_residue(UInt256 residue,
201 const UInt256& modulus,
202 const UInt256& half_modulus,
203 const char* label) {
204 if (residue.is_zero()) {
206 }
207 auto negative = modulus;
208 negative.sub_assign(residue);
209 residue.conditional_assign(negative, half_modulus < residue);
210 return residue;
211}
212
213bool same_point(const JacobianPoint& candidate,
214 const AffinePoint& expected,
215 const EllipticCurve& curve) {
216 if (candidate.infinity) return false;
217 const auto affine = curve.affine(candidate);
218 return affine.x == expected.x && affine.y == expected.y;
219}
220
221void append_companion_point(Bytes& out, const AffinePoint& point) {
222 assert(!point.infinity && "serialized companion point must be finite");
223 out.push_back(static_cast<unsigned char>(point.y.is_odd() ? 0x03 : 0x02));
224 const auto x = point.x.to_bytes_be();
225 out.insert(out.end(), x.begin(), x.end());
226}
227std::vector<Expr> ordinary_bits(Transcript& transcript,
228 const std::optional<UInt256>& value) {
229 std::vector<Expr> bits;
230 bits.reserve(255);
231 for (std::size_t i = 0; i < 255; ++i) {
232 auto bit = value
233 ? std::optional<FieldElement>(
234 FieldElement::from_u64(value->bit(i)))
235 : std::nullopt;
236 WipeOnDestruction bit_guard(bit);
237 bits.push_back(transcript.boolean(transcript.secret(bit)));
238 }
239 return bits;
240}
241
242Expr exceeds_bound(Transcript& transcript, const std::vector<Expr>& bits,
243 const UInt256& bound) {
244 Expr equal_prefix(1);
245 Expr exceeds(0);
246 for (std::size_t i = bits.size(); i != 0; --i) {
247 const Expr same_and_one =
248 transcript.mul(equal_prefix, bits[i - 1]);
249 if (bound.bit(i - 1)) {
250 equal_prefix = same_and_one;
251 } else {
252 exceeds = exceeds + same_and_one;
253 equal_prefix = equal_prefix - same_and_one;
254 }
255 }
256 return exceeds;
257}
258
259std::vector<Expr> signed_window_bits(Transcript& transcript,
260 const std::vector<Expr>& ordinary) {
261 auto bits = ordinary;
262 for (std::size_t i = 3; i < bits.size(); i += 3) {
263 bits[i] = Expr(1) - ordinary[i];
264 for (std::size_t offset : {1U, 2U}) {
265 bits[i - offset] =
266 Expr(1) - ordinary[i] - ordinary[i - offset] +
267 2 * transcript.mul(ordinary[i], ordinary[i - offset]);
268 }
269 }
270 return bits;
271}
272
273Result<CenteredScalarRelation> centered_scalar_relation(
274 Transcript& transcript,
275 const FieldElement* scalar = nullptr) {
276 if (scalar && scalar->is_zero()) {
278 "recovery_root:zero_witness");
279 }
280 std::optional<UInt256> value;
281 std::optional<FieldElement> sign_value;
282 WipeOnDestruction value_guard(value);
283 WipeOnDestruction sign_value_guard(sign_value);
284 if (scalar) {
285 auto canonical = canonical_scalar(*scalar);
286 WipeOnDestruction canonical_guard(canonical);
287 value = canonical.magnitude;
288 value->sub_assign(UInt256::one());
289 sign_value = FieldElement::from_u64(canonical.negated);
290 }
291 const auto bits = ordinary_bits(transcript, value);
292 const Expr sign = transcript.boolean(transcript.secret(sign_value));
293 auto bound = half_secp_order();
294 bound.sub_assign(UInt256::one());
295 transcript.equal(exceeds_bound(transcript, bits, bound), Expr(0));
296 Expr scalar_value(1);
297 FieldElement power = FieldElement::one();
298 for (const auto& bit : bits) {
299 scalar_value = scalar_value + power * bit;
300 power = power + power;
301 }
302 return CenteredScalarRelation{
303 sign, scalar_value, signed_window_bits(transcript, bits),
304 bits.size() + 1};
305}
306
307Result<CircuitMainResult> setup_relation(
308 Transcript& transcript,
309 const FieldElement* scalar = nullptr) {
311 auto centered, centered_scalar_relation(transcript, scalar),
312 "recovery_root:centered_scalar");
313 const Expr first = circuit_ec_multiply_x(
314 curve1(), transcript, generator1(), centered.recoded_bits);
315 const Expr second = circuit_ec_multiply_x(
316 curve2(), transcript, generator2(), centered.recoded_bits);
317 return CircuitMainResult{
318 transcript.mul(Expr(1) - 2 * centered.sign,
319 centered.magnitude),
320 first, second, centered.n_bits};
321}
322
323Result<ContributionRelation> contribution_relation(
324 Transcript& transcript, const RecoveryRootShare& share,
325 const FieldElement* scalar = nullptr) {
327 auto centered, centered_scalar_relation(transcript, scalar),
328 "recovery_contribution:centered_scalar");
329 auto first = circuit_ec_multiply_point(
330 curve1(), transcript, generator1(), centered.recoded_bits);
331 auto second = circuit_ec_multiply_point(
332 curve2(), transcript, generator2(), centered.recoded_bits);
333 first = circuit_optionally_negate_ec(first, transcript, centered.sign);
334 second = circuit_optionally_negate_ec(second, transcript,
335 centered.sign);
336 transcript.equal(first.first, Expr(share.first().x));
337 transcript.equal(first.second, Expr(share.first().y));
338 transcript.equal(second.first, Expr(share.second().x));
339 transcript.equal(second.second, Expr(share.second().y));
340 return ContributionRelation{
341 transcript.mul(Expr(1) - 2 * centered.sign,
342 centered.magnitude),
343 centered.n_bits};
344}
345
346Result<NativeBulletproofCircuit> public_setup_circuit(const UInt512& root) {
348 "recovery_root:root");
349 Transcript transcript;
350 PURIFY_ASSIGN_OR_RETURN(const auto& relation, setup_relation(transcript),
351 "recovery_root:relation");
352 BulletproofTranscript bp;
353 PURIFY_RETURN_IF_ERROR(bp.from_transcript(transcript, relation.n_bits),
354 "recovery_root:lower");
356 bp.add_pubkey_and_out(root, relation.p1x, relation.p2x, relation.out),
357 "recovery_root:bind");
358 return bp.native_circuit();
359}
360
361Result<BulletproofAssignmentData> setup_assignment(
362 const UInt512& root, const FieldElement& scalar) {
363 Transcript transcript;
364 PURIFY_ASSIGN_OR_RETURN(const auto& relation,
365 setup_relation(transcript, &scalar),
366 "recovery_root:witness");
367 BulletproofTranscript bp;
368 PURIFY_RETURN_IF_ERROR(bp.from_transcript(transcript, relation.n_bits),
369 "recovery_root:lower");
371 bp.add_pubkey_and_out(root, relation.p1x, relation.p2x, relation.out),
372 "recovery_root:bind");
373 return bp.assignment_data(transcript.varmap(), scalar);
374}
375
376Result<NativeBulletproofCircuit> contribution_circuit(
377 const RecoveryRootShare& share,
378 std::size_t* relation_multiplications = nullptr) {
379 Transcript transcript;
381 const auto relation, contribution_relation(transcript, share),
382 "recovery_contribution:relation");
383 if (relation_multiplications != nullptr) {
384 *relation_multiplications = transcript.muls().size();
385 }
386 BulletproofTranscript bp;
387 PURIFY_RETURN_IF_ERROR(bp.from_transcript(transcript, relation.n_bits,
388 8192),
389 "recovery_contribution:lower");
391 bp.bind_output_to_commitment(relation.output),
392 "recovery_contribution:bind_output");
393 return bp.native_circuit();
394}
395
396Result<BulletproofAssignmentData> contribution_assignment(
397 const RecoveryRootShare& share, const FieldElement& effective_scalar) {
398 Transcript transcript;
400 const auto relation,
401 contribution_relation(transcript, share, &effective_scalar),
402 "recovery_contribution:witness_relation");
403 BulletproofTranscript bp;
404 PURIFY_RETURN_IF_ERROR(bp.from_transcript(transcript, relation.n_bits,
405 8192),
406 "recovery_contribution:lower_witness");
408 bp.bind_output_to_commitment(relation.output),
409 "recovery_contribution:bind_output_witness");
410 return bp.assignment_data(transcript.varmap(), effective_scalar);
411}
412
413Result<Bytes> setup_binding(const NonceHostBoundSigningRoster& roster,
414 const UInt512& root) {
415 if (roster.members().empty()) {
417 "recovery_root:empty_roster");
418 }
419 if (!size_fits_u64(roster.members().size())) {
421 "recovery_root:roster_size");
422 }
423 const auto root_bytes = root.to_bytes_be();
424 const auto count = encode_u64(roster.members().size());
425 const auto key = roster_point(roster);
426 static const TaggedHash hash(
427 "Purify/Accountable/RecoveryRootSetup/v2");
428 const auto digest = hash.digest_many<6>({
429 roster.key_list_hash(), key, count, root_bytes,
430 roster.coalition_core_digest(), roster.nonce_host_commitment(),
431 });
432 return Bytes(digest.begin(), digest.end());
433}
434
435Result<Bytes> contribution_binding(
436 const NonceHostBoundSigningRoster& roster,
437 const CoalitionFormation& formation, std::size_t member_index,
438 const RecoveryRootShare& share) {
439 if (roster.members().empty()) {
441 "recovery_contribution:empty_roster");
442 }
443 if (member_index >= roster.members().size()) {
445 "recovery_contribution:member_index");
446 }
447 if (formation.core_digest() != roster.coalition_core_digest()) {
449 "recovery_contribution:formation_core");
450 }
451 if (!size_fits_u64(roster.members().size()) ||
452 !size_fits_u64(member_index)) {
454 "recovery_contribution:roster_size");
455 }
456 const auto& member = roster.members()[member_index];
457 const auto count = encode_u64(roster.members().size());
458 const auto index = encode_u64(member_index);
459 const auto aggregate_key = roster_point(roster);
460 const auto coefficient = member.coefficient.to_bytes_be();
461 const std::array<unsigned char, 1> aggregate_parity{
462 static_cast<unsigned char>(roster.aggregate_was_negated())};
463 const auto encoded_share = share.serialize();
464 static const TaggedHash hash(
465 "Purify/Accountable/RecoveryRootContribution/v1");
466 const auto digest = hash.digest_many<11>({
467 roster.key_list_hash(), aggregate_key, count, index,
468 member.public_key, coefficient, aggregate_parity, encoded_share,
469 formation.permit_id(), roster.coalition_core_digest(),
470 roster.nonce_host_commitment(),
471 });
472 return Bytes(digest.begin(), digest.end());
473}
474
475} // namespace
476
478 Bytes out;
479 out.reserve(kSerializedSize);
480 append_companion_point(out, first_);
481 append_companion_point(out, second_);
482 assert(out.size() == kSerializedSize);
483 return out;
484}
485
487 std::span<const unsigned char> encoded) {
488 if (encoded.size() != kSerializedSize) {
490 "recovery_contribution:share_size");
491 }
493 auto first,
494 decode_companion_point(encoded.first<33>(), curve1(),
495 "recovery_contribution:first_share"),
496 "recovery_contribution:first_share");
498 auto second,
499 decode_companion_point(encoded.subspan<33, 33>(), curve2(),
500 "recovery_contribution:second_share"),
501 "recovery_contribution:second_share");
502 return RecoveryRootShare(std::move(first), std::move(second));
503}
504
506 const SigningRoster& roster, std::size_t member_index,
507 const FieldElement& member_secret,
508 purify_secp_context* secp_context) {
511 secp_context, "recovery_contribution:derive_share"),
512 "recovery_contribution:derive_share");
513 if (member_secret.is_zero()) {
514 return unexpected_error(
516 "recovery_contribution:zero_member_secret");
517 }
518 if (member_index >= roster.members().size()) {
519 return unexpected_error(
521 "recovery_contribution:member_index");
522 }
524 const auto member_point,
525 secp_point(member_secret, secp_context),
526 "recovery_contribution:member_point");
527 if (member_point != roster.members()[member_index].public_key) {
528 return unexpected_error(
530 "recovery_contribution:member_secret");
531 }
532 auto effective_scalar =
533 roster.members()[member_index].coefficient * member_secret;
534 WipeOnDestruction effective_scalar_guard(effective_scalar);
535 if (effective_scalar.is_zero()) {
536 return unexpected_error(
538 "recovery_contribution:zero_effective_scalar");
539 }
540 auto centered = canonical_scalar(effective_scalar);
541 WipeOnDestruction centered_guard(centered);
543 auto first,
544 curve1().mul_secret_affine(generator1(), centered.magnitude),
545 "recovery_contribution:first_share");
547 auto second,
548 curve2().mul_secret_affine(generator2(), centered.magnitude),
549 "recovery_contribution:second_share");
550 first.y.conditional_assign(first.y.negate(), centered.negated);
551 second.y.conditional_assign(second.y.negate(), centered.negated);
552 return RecoveryRootShare(std::move(first), std::move(second));
553}
554
556 std::span<const RecoveryRootShare> ordered_shares) {
558 validate_recovery_root_share_count(ordered_shares.size()),
559 "recovery_share_aggregate:member_count");
562 for (const auto& share : ordered_shares) {
563 first_sum = curve1().add_mixed(first_sum, share.first());
564 second_sum = curve2().add_mixed(second_sum, share.second());
565 }
566 if (first_sum.infinity || second_sum.infinity) {
568 "recovery_share_aggregate:infinite_sum");
569 }
570 auto first_affine = curve1().affine(first_sum);
571 auto second_affine = curve2().affine(second_sum);
572 const auto root = pack_public(first_affine.x.to_uint256(),
573 second_affine.x.to_uint256());
574 return RecoveryRootShareAggregate(
575 ordered_shares.size(), std::move(first_affine),
576 std::move(second_affine), root);
577}
578
580 const NonceHostBoundSigningRoster& roster,
581 const RecoveryRootShareAggregate& aggregate,
582 const FieldElement& exposed_signing_scalar,
583 purify_secp_context* secp_context) {
586 secp_context, "recovery_share_aggregate:recover"),
587 "recovery_share_aggregate:recover");
588 if (aggregate.member_count() != roster.members().size()) {
590 "recovery_share_aggregate:member_count");
591 }
592 if (exposed_signing_scalar.is_zero()) {
594 "recovery_share_aggregate:zero_scalar");
595 }
597 const auto exposed_point,
598 secp_point(exposed_signing_scalar, secp_context),
599 "recovery_share_aggregate:signing_point");
600 if (exposed_point != roster_point(roster)) {
602 "recovery_share_aggregate:signing_scalar");
603 }
604
605 const auto scalar = exposed_signing_scalar.to_uint256();
606 const auto first_step = curve1().mul(generator1(), prime_p());
607 const auto second_step = curve2().mul(generator2(), prime_p());
608 if (first_step.infinity || second_step.infinity) {
610 "recovery_share_aggregate:carry_step");
611 }
612 auto first_candidate = curve1().mul(generator1(), scalar);
613 auto second_candidate = curve2().mul(generator2(), scalar);
614 auto first_residue = reduce_mod(scalar, order_n1());
615 WipeOnDestruction first_residue_guard(first_residue);
616 auto second_residue = reduce_mod(scalar, order_n2());
617 WipeOnDestruction second_residue_guard(second_residue);
618 const auto first_step_residue = reduce_mod(prime_p(), order_n1());
619 const auto second_step_residue = reduce_mod(prime_p(), order_n2());
620
621 // These roster-only bounds contain N candidates for even N and N + 1
622 // for odd N. Start at -ceil(N / 2), then advance through
623 // floor((N - 1) / 2).
624 const auto negative_steps =
625 aggregate.member_count() / 2 + aggregate.member_count() % 2;
626 const auto negative_first_step = curve1().negate(first_step);
627 const auto negative_second_step = curve2().negate(second_step);
628 for (std::size_t i = 0; i < negative_steps; ++i) {
629 first_candidate = curve1().add(first_candidate,
630 negative_first_step);
631 second_candidate = curve2().add(second_candidate,
632 negative_second_step);
633 first_residue = subtract_mod(
634 std::move(first_residue), first_step_residue, order_n1());
635 second_residue = subtract_mod(
636 std::move(second_residue), second_step_residue, order_n2());
637 }
638
639 std::optional<std::pair<UInt256, UInt256>> match;
640 WipeOnDestruction match_guard(match);
641 std::size_t match_count = 0;
642 const auto candidate_advances = aggregate.member_count() -
643 (aggregate.member_count() % 2 == 0 ? 1 : 0);
644 for (std::size_t candidate_index = 0;; ++candidate_index) {
645 if (same_point(first_candidate, aggregate.first_sum(), curve1()) &&
646 same_point(second_candidate, aggregate.second_sum(), curve2())) {
647 ++match_count;
648 match = std::pair{first_residue, second_residue};
649 }
650 if (candidate_index == candidate_advances) break;
651 first_candidate = curve1().add(first_candidate, first_step);
652 second_candidate = curve2().add(second_candidate, second_step);
653 first_residue = add_mod(
654 std::move(first_residue), first_step_residue, order_n1());
655 second_residue = add_mod(
656 std::move(second_residue), second_step_residue, order_n2());
657 }
658 if (match_count != 1 || !match) {
659 return unexpected_error(
661 match_count == 0
662 ? "recovery_share_aggregate:missing_carry"
663 : "recovery_share_aggregate:ambiguous_carry");
664 }
665
667 auto first_secret,
668 centered_residue(std::move(match->first), order_n1(), half_n1(),
669 "recovery_share_aggregate:first_zero"),
670 "recovery_share_aggregate:first_secret");
671 WipeOnDestruction first_secret_guard(first_secret);
673 auto second_secret,
674 centered_residue(std::move(match->second), order_n2(), half_n2(),
675 "recovery_share_aggregate:second_zero"),
676 "recovery_share_aggregate:second_secret");
677 WipeOnDestruction second_secret_guard(second_secret);
679 auto packed, pack_secret(first_secret, second_secret),
680 "recovery_share_aggregate:pack");
681 WipeOnDestruction packed_guard(packed);
683 auto secret, SecretKey::from_packed(packed),
684 "recovery_share_aggregate:secret");
685 PURIFY_ASSIGN_OR_RETURN(auto recovered, derive_key(std::move(secret)),
686 "recovery_share_aggregate:derive");
687 if (recovered.public_key != aggregate.recovery_root()) {
689 "recovery_share_aggregate:exact_root");
690 }
691 return recovered;
692}
693
694Status RecoveryRootContribution::validate_encoding(
695 purify_secp_context* secp_context) const {
697 require_secp_context(secp_context,
698 "recovery_contribution:proof_encoding"),
699 "recovery_contribution:proof_encoding");
700 static constexpr std::size_t kRoundCount = 14;
701 static_assert(kNormProofSize == 65 * kRoundCount + 64);
703 raw_commitment_, mask_commitment_, t2_,
704 public_commitment_dlog_nonce_,
705 public_commitment_dlog_response_, norm_proof_};
706 return bppp::candidate::
709 kRoundCount, secp_context);
710}
711
713 purify_secp_context* secp_context) const {
714 PURIFY_RETURN_IF_ERROR(validate_encoding(secp_context),
715 "recovery_contribution:serialize");
716 Bytes out;
717 out.reserve(kSerializedSize);
718 out.push_back(kSerializationVersion);
719 const auto encoded_share = share_.serialize();
720 out.insert(out.end(), encoded_share.begin(), encoded_share.end());
721 out.insert(out.end(), raw_commitment_.begin(), raw_commitment_.end());
722 out.insert(out.end(), mask_commitment_.begin(), mask_commitment_.end());
723 out.insert(out.end(), t2_.begin(), t2_.end());
724 out.insert(out.end(), public_commitment_dlog_nonce_.begin(),
725 public_commitment_dlog_nonce_.end());
726 out.insert(out.end(), public_commitment_dlog_response_.begin(),
727 public_commitment_dlog_response_.end());
728 out.insert(out.end(), norm_proof_.begin(), norm_proof_.end());
729 assert(out.size() == kSerializedSize);
730 return out;
731}
732
734 std::span<const unsigned char> encoded,
735 purify_secp_context* secp_context) {
736 static constexpr std::size_t kProofOffset =
738 static constexpr std::size_t kNormOffset = kProofOffset + 162;
739 static_assert(kNormOffset + kNormProofSize == kSerializedSize);
740 if (encoded.size() != kSerializedSize) {
742 "recovery_contribution:deserialize_size");
743 }
744 if (encoded[0] != kSerializationVersion) {
746 "recovery_contribution:deserialize_version");
747 }
749 auto share,
751 encoded.subspan(1, RecoveryRootShare::kSerializedSize)),
752 "recovery_contribution:deserialize_share");
753 RecoveryRootContribution out(std::move(share));
754 std::size_t offset = kProofOffset;
755 std::copy_n(encoded.begin() + offset, out.raw_commitment_.size(),
756 out.raw_commitment_.begin());
757 offset += out.raw_commitment_.size();
758 std::copy_n(encoded.begin() + offset, out.mask_commitment_.size(),
759 out.mask_commitment_.begin());
760 offset += out.mask_commitment_.size();
761 std::copy_n(encoded.begin() + offset, out.t2_.size(), out.t2_.begin());
762 offset += out.t2_.size();
763 std::copy_n(encoded.begin() + offset,
764 out.public_commitment_dlog_nonce_.size(),
765 out.public_commitment_dlog_nonce_.begin());
766 offset += out.public_commitment_dlog_nonce_.size();
767 std::copy_n(encoded.begin() + offset,
768 out.public_commitment_dlog_response_.size(),
769 out.public_commitment_dlog_response_.begin());
770 offset += out.public_commitment_dlog_response_.size();
771 assert(offset == kNormOffset);
772 out.norm_proof_.assign(encoded.begin() + offset, encoded.end());
773 PURIFY_RETURN_IF_ERROR(out.validate_encoding(secp_context),
774 "recovery_contribution:deserialize");
775 return out;
776}
777
778namespace testing {
779
782 const RecoveryRootShare& share) {
784 require_recovery_contribution_hash_suite(
785 "recovery_contribution:shape_hash_suite"),
786 "recovery_contribution:shape_hash_suite");
787 std::size_t relation_multiplications = 0;
789 const auto circuit,
790 contribution_circuit(share, &relation_multiplications),
791 "recovery_contribution:shape");
793 relation_multiplications, circuit.n_gates};
794}
795
797 std::size_t member_count) {
798 return validate_recovery_root_share_count(member_count);
799}
800
802 const RecoveryRootShare& circuit_share,
803 const RecoveryRootShare& assignment_share,
804 const FieldElement& effective_scalar) {
806 const auto circuit, contribution_circuit(circuit_share),
807 "recovery_contribution:test_circuit");
809 const auto assignment,
810 contribution_assignment(assignment_share, effective_scalar),
811 "recovery_contribution:test_assignment");
812 return circuit.evaluate(assignment);
813}
814
815} // namespace testing
816
818 const NonceHostBoundSigningRoster& roster,
819 const CoalitionFormation& formation, std::size_t member_index,
820 const FieldElement& member_secret,
821 purify_secp_context* secp_context,
824 require_secp_context(secp_context, "recovery_contribution:prove"),
825 "recovery_contribution:prove");
827 require_recovery_contribution_hash_suite(
828 "recovery_contribution:prove_hash_suite"),
829 "recovery_contribution:prove_hash_suite");
831 auto share,
833 roster.signing_roster(), member_index, member_secret,
834 secp_context),
835 "recovery_contribution:derive_share");
836 RecoveryRootContribution contribution(std::move(share));
837 auto effective_scalar =
838 roster.members()[member_index].coefficient * member_secret;
839 WipeOnDestruction effective_scalar_guard(effective_scalar);
841 const auto circuit, contribution_circuit(contribution.share_),
842 "recovery_contribution:circuit");
844 auto assignment,
845 contribution_assignment(contribution.share_, effective_scalar),
846 "recovery_contribution:assignment");
848 const auto binding,
849 contribution_binding(roster, formation, member_index,
850 contribution.share_),
851 "recovery_contribution:binding");
852 SecretScalarBytes effective_bytes;
854 effective_bytes.value.data(),
856 static const TaggedHash prover_seed_hash(
857 "Purify/Accountable/RecoveryRootContributionProverSeed/v1");
858 SecretScalarBytes prover_seed;
859 prover_seed.value = prover_seed_hash.digest_many<2>({
860 effective_bytes.value, binding});
862 const auto weighted,
863 weighted_member_point(roster, member_index, secp_context),
864 "recovery_contribution:weighted_key");
865 const std::array<PointBytes, 1> outputs{weighted};
867 auto publication,
869 prove_circuit_zk_norm_arg_v4_with_public_commitments(
870 circuit, assignment, prover_seed.value, outputs,
871 secp_context, binding, cache),
872 "recovery_contribution:prove_circuit");
873 contribution.raw_commitment_ = publication.proof.raw_commitment;
874 contribution.mask_commitment_ = publication.proof.mask_commitment;
875 contribution.t2_ = publication.proof.t2;
876 contribution.public_commitment_dlog_nonce_ =
877 publication.proof.public_commitment_dlog_nonce;
878 contribution.public_commitment_dlog_response_ =
879 publication.proof.public_commitment_dlog_response;
880 contribution.norm_proof_ = std::move(publication.proof.norm_proof);
881 PURIFY_RETURN_IF_ERROR(contribution.validate_encoding(secp_context),
882 "recovery_contribution:proof_encoding");
883 return contribution;
884}
885
887 const NonceHostBoundSigningRoster& roster,
888 const CoalitionFormation& formation, std::size_t member_index,
889 const RecoveryRootContribution& contribution,
890 purify_secp_context* secp_context,
893 require_secp_context(secp_context, "recovery_contribution:verify"),
894 "recovery_contribution:verify");
896 require_recovery_contribution_hash_suite(
897 "recovery_contribution:verify_hash_suite"),
898 "recovery_contribution:verify_hash_suite");
899 PURIFY_RETURN_IF_ERROR(contribution.validate_encoding(secp_context),
900 "recovery_contribution:proof_encoding");
902 const auto circuit, contribution_circuit(contribution.share_),
903 "recovery_contribution:circuit");
905 const auto binding,
906 contribution_binding(roster, formation, member_index,
907 contribution.share_),
908 "recovery_contribution:binding");
910 const auto weighted,
911 weighted_member_point(roster, member_index, secp_context),
912 "recovery_contribution:weighted_key");
913 const std::array<PointBytes, 1> outputs{weighted};
915 contribution.raw_commitment_, contribution.mask_commitment_,
916 contribution.t2_, contribution.public_commitment_dlog_nonce_,
917 contribution.public_commitment_dlog_response_,
918 contribution.norm_proof_};
920 auto publication,
922 reconstruct_circuit_zk_norm_arg_publication_v4_with_public_commitments(
923 circuit, std::move(proof), outputs, secp_context, binding),
924 "recovery_contribution:publication");
925 return bppp::candidate::
927 circuit, publication, outputs, secp_context, binding, cache);
928}
929
933 const CoalitionFormation& formation,
934 std::span<const RecoveryRootContribution> ordered_contributions,
935 purify_secp_context* secp_context,
937 if (ordered_contributions.size() != roster.members().size()) {
938 return unexpected_error(
940 "recovery_contribution_set:member_count");
941 }
942 if (formation.core_digest() != roster.coalition_core_digest()) {
943 return unexpected_error(
945 "recovery_contribution_set:formation_core");
946 }
947
948 std::vector<RecoveryRootShare> ordered_shares;
949 ordered_shares.reserve(ordered_contributions.size());
950 for (std::size_t i = 0; i < ordered_contributions.size(); ++i) {
952 const auto accepted,
954 roster, formation, i, ordered_contributions[i],
955 secp_context, cache),
956 "recovery_contribution_set:verify");
957 if (!accepted) {
958 return unexpected_error(
960 "recovery_contribution_set:proof");
961 }
962 ordered_shares.push_back(ordered_contributions[i].share());
963 }
965 auto aggregate,
966 aggregate_recovery_root_shares(ordered_shares),
967 "recovery_contribution_set:aggregate");
969 std::move(roster), std::move(aggregate));
970}
971
974 const NonceHostBoundSigningRoster& roster,
975 const CoalitionFormation& formation, std::size_t member_index,
976 const RecoveryRootShare& share,
977 const bppp::ScalarBytes& circuit_precommitment,
978 purify_secp_context* secp_context) const {
980 require_recovery_contribution_hash_suite(
981 "recovery_contribution_parent:hash_suite"),
982 "recovery_contribution_parent:hash_suite");
985 secp_context, "recovery_contribution_parent:context"),
986 "recovery_contribution_parent:context");
988 const auto precommitment,
990 circuit_precommitment),
991 "recovery_contribution_parent:precommitment");
993 const auto circuit, contribution_circuit(share),
994 "recovery_contribution_parent:circuit");
996 const auto binding,
997 contribution_binding(roster, formation, member_index, share),
998 "recovery_contribution_parent:binding");
1000 const auto weighted,
1001 weighted_member_point(roster, member_index, secp_context),
1002 "recovery_contribution_parent:weighted_key");
1003 const std::array<PointBytes, 1> outputs{weighted};
1005 auto constants,
1007 derive_circuit_norm_parent_constants_v4_with_public_commitments(
1008 circuit, precommitment, outputs, secp_context, binding),
1009 "recovery_contribution_parent:derive");
1011 std::move(constants.circuit_binding_digest),
1012 std::move(constants.raw_generator_digest),
1013 std::move(constants.coefficient_commitment),
1014 std::move(constants.anchored_offset)};
1015}
1016
1019 const NonceHostBoundSigningRoster& roster,
1020 const CoalitionFormation& formation, std::size_t member_index,
1021 const RecoveryRootContribution& contribution,
1022 purify_secp_context* secp_context,
1023 bppp::ExperimentalCircuitBackend* cache) const {
1025 require_recovery_contribution_hash_suite(
1026 "recovery_contribution_accumulation:hash_suite"),
1027 "recovery_contribution_accumulation:hash_suite");
1030 secp_context, "recovery_contribution_accumulation:context"),
1031 "recovery_contribution_accumulation:context");
1033 contribution.validate_encoding(secp_context),
1034 "recovery_contribution_accumulation:record");
1036 const auto circuit, contribution_circuit(contribution.share_),
1037 "recovery_contribution_accumulation:circuit");
1039 const auto binding,
1040 contribution_binding(roster, formation, member_index,
1041 contribution.share_),
1042 "recovery_contribution_accumulation:binding");
1044 const auto weighted,
1045 weighted_member_point(roster, member_index, secp_context),
1046 "recovery_contribution_accumulation:weighted_key");
1047 const std::array<PointBytes, 1> outputs{weighted};
1049 contribution.raw_commitment_, contribution.mask_commitment_,
1050 contribution.t2_, contribution.public_commitment_dlog_nonce_,
1051 contribution.public_commitment_dlog_response_,
1052 contribution.norm_proof_};
1054 auto publication,
1056 reconstruct_circuit_zk_norm_arg_publication_v4_with_public_commitments(
1057 circuit, std::move(proof), outputs, secp_context, binding),
1058 "recovery_contribution_accumulation:publication");
1059 const auto circuit_precommitment = publication.precommitment.bytes();
1061 auto prepared,
1063 prepare_circuit_zk_norm_arg_v4_with_public_commitments(
1064 circuit, publication, outputs, secp_context, binding,
1065 cache),
1066 "recovery_contribution_accumulation:outer");
1067 if (!prepared.has_value()) {
1068 return unexpected_error(
1070 "recovery_contribution_accumulation:outer");
1071 }
1073 auto norm,
1075 prepare_v4_circuit_norm_for_algebraic_reduction(
1076 basis_, std::move(*prepared), secp_context),
1077 "recovery_contribution_accumulation:norm");
1078 return PreparedConstructionLeaf(circuit_precommitment,
1079 std::move(norm));
1080}
1081
1082Status RecoveryRootSetupProof::validate_encoding(
1083 purify_secp_context* secp_context) const {
1085 require_secp_context(secp_context, "recovery_root:proof_encoding"),
1086 "recovery_root:proof_encoding");
1087 static constexpr std::size_t kRoundCount = 12;
1088 static_assert(kNormProofSize == 65 * kRoundCount + 64);
1090 raw_commitment_, mask_commitment_, t2_,
1091 public_commitment_dlog_nonce_,
1092 public_commitment_dlog_response_, norm_proof_};
1093 return bppp::candidate::
1096 kRoundCount, secp_context);
1097}
1098
1100 purify_secp_context* secp_context) const {
1101 PURIFY_RETURN_IF_ERROR(validate_encoding(secp_context),
1102 "recovery_root:serialize");
1103 Bytes out;
1104 out.reserve(kSerializedSize);
1105 out.push_back(kSerializationVersion);
1106 out.insert(out.end(), raw_commitment_.begin(), raw_commitment_.end());
1107 out.insert(out.end(), mask_commitment_.begin(), mask_commitment_.end());
1108 out.insert(out.end(), t2_.begin(), t2_.end());
1109 out.insert(out.end(), public_commitment_dlog_nonce_.begin(),
1110 public_commitment_dlog_nonce_.end());
1111 out.insert(out.end(),
1112 public_commitment_dlog_response_.begin(),
1113 public_commitment_dlog_response_.end());
1114 out.insert(out.end(), norm_proof_.begin(), norm_proof_.end());
1115 assert(out.size() == kSerializedSize);
1116 return out;
1117}
1118
1120 std::span<const unsigned char> encoded,
1121 purify_secp_context* secp_context) {
1122 constexpr std::size_t header_size = 163;
1123 static_assert(header_size + kNormProofSize == kSerializedSize);
1124 if (encoded.size() != kSerializedSize) {
1126 "recovery_root:deserialize_header");
1127 }
1128 if (encoded[0] != kSerializationVersion) {
1130 "recovery_root:deserialize_version");
1131 }
1133 std::copy_n(encoded.begin() + 1, 33,
1134 out.raw_commitment_.begin());
1135 std::copy_n(encoded.begin() + 34, 33,
1136 out.mask_commitment_.begin());
1137 std::copy_n(encoded.begin() + 67, 32,
1138 out.t2_.begin());
1139 std::copy_n(encoded.begin() + 99, 32,
1140 out.public_commitment_dlog_nonce_.begin());
1141 std::copy_n(encoded.begin() + 131, 32,
1142 out.public_commitment_dlog_response_.begin());
1143 out.norm_proof_.assign(encoded.begin() + header_size, encoded.end());
1144 PURIFY_RETURN_IF_ERROR(out.validate_encoding(secp_context),
1145 "recovery_root:deserialize");
1146 return out;
1147}
1148
1150 const FieldElement& signing_scalar) {
1151 if (signing_scalar.is_zero()) {
1153 "recovery_root:zero_scalar");
1154 }
1155 auto first = magnitude(signing_scalar);
1156 WipeOnDestruction first_guard(first);
1157 auto second = first;
1158 WipeOnDestruction second_guard(second);
1159 auto reduced_second = order_n2();
1160 WipeOnDestruction reduced_second_guard(reduced_second);
1161 reduced_second.sub_assign(first);
1162 second.conditional_assign(reduced_second, half_n2() < second);
1163 PURIFY_ASSIGN_OR_RETURN(auto packed, pack_secret(first, second),
1164 "recovery_root:pack");
1165 WipeOnDestruction packed_guard(packed);
1167 "recovery_root:secret");
1168 return derive_key(std::move(secret));
1169}
1170
1172 const NonceHostBoundSigningRoster& roster,
1173 const UInt512& recovery_root, const FieldElement& signing_scalar,
1174 purify_secp_context* secp_context,
1177 require_secp_context(secp_context, "recovery_root:prove"),
1178 "recovery_root:prove");
1179 if (signing_scalar.is_zero()) {
1181 "recovery_root:zero_signing_scalar");
1182 }
1183 PURIFY_ASSIGN_OR_RETURN(const auto& scalar_point,
1184 secp_point(signing_scalar, secp_context),
1185 "recovery_root:signing_point");
1186 if (scalar_point != roster_point(roster)) {
1188 "recovery_root:signing_scalar");
1189 }
1190 PURIFY_ASSIGN_OR_RETURN(const auto& expected_root,
1191 derive_recovery_root(signing_scalar),
1192 "recovery_root:derive");
1193 if (expected_root.public_key != recovery_root) {
1195 "recovery_root:expected_root");
1196 }
1197 PURIFY_ASSIGN_OR_RETURN(const auto& circuit,
1198 public_setup_circuit(recovery_root),
1199 "recovery_root:public_circuit");
1200 PURIFY_ASSIGN_OR_RETURN(auto assignment,
1201 setup_assignment(recovery_root, signing_scalar),
1202 "recovery_root:assignment");
1203 PURIFY_ASSIGN_OR_RETURN(const auto& binding,
1204 setup_binding(roster, recovery_root),
1205 "recovery_root:binding");
1206 SecretScalarBytes encoded_scalar;
1208 encoded_scalar.value.data(),
1210 SecretScalarBytes secret_prover_seed;
1211 static const TaggedHash prover_seed_hash(
1212 "Purify/Accountable/RecoveryRootProverSeed/v2");
1213 auto prover_seed = prover_seed_hash.digest_many<2>(
1214 {encoded_scalar.value, binding});
1215 WipeOnDestruction prover_seed_guard(prover_seed);
1216 secret_prover_seed.value = prover_seed;
1217 const std::array<PointBytes, 1> outputs{roster_point(roster)};
1219 auto publication,
1221 prove_circuit_zk_norm_arg_v4_with_public_commitments(
1222 circuit, assignment, secret_prover_seed.value, outputs,
1223 secp_context, binding,
1224 cache),
1225 "recovery_root:prove_circuit");
1227 proof.raw_commitment_ = publication.proof.raw_commitment;
1228 proof.mask_commitment_ = publication.proof.mask_commitment;
1229 proof.t2_ = publication.proof.t2;
1230 proof.public_commitment_dlog_nonce_ =
1231 publication.proof.public_commitment_dlog_nonce;
1232 proof.public_commitment_dlog_response_ =
1233 publication.proof.public_commitment_dlog_response;
1234 proof.norm_proof_ = std::move(publication.proof.norm_proof);
1235 return proof;
1236}
1237
1240 purify_secp_context* secp_context) {
1241 static_assert(RecoveryRootSetupProof::kNormProofSize == 65 * 12 + 64);
1243 auto basis,
1246 "Purify/Accountable/RecoveryRootSetupAccumulation/v2",
1247 secp_context),
1248 "recovery_root_accumulation:basis");
1249 return RecoveryRootSetupAccumulator(std::move(basis));
1250}
1251
1254 const NonceHostBoundSigningRoster& roster,
1255 const UInt512& recovery_root,
1256 const RecoveryRootSetupProof& proof,
1257 purify_secp_context* secp_context,
1258 bppp::ExperimentalCircuitBackend* cache) const {
1260 require_secp_context(secp_context,
1261 "recovery_root_accumulation:prepare"),
1262 "recovery_root_accumulation:prepare");
1263 PURIFY_RETURN_IF_ERROR(proof.validate_encoding(secp_context),
1264 "recovery_root_accumulation:proof");
1266 const auto& circuit, public_setup_circuit(recovery_root),
1267 "recovery_root_accumulation:circuit");
1269 const auto& binding, setup_binding(roster, recovery_root),
1270 "recovery_root_accumulation:binding");
1271 const std::array<PointBytes, 1> outputs{roster_point(roster)};
1273 proof.raw_commitment_, proof.mask_commitment_, proof.t2_,
1274 proof.public_commitment_dlog_nonce_,
1275 proof.public_commitment_dlog_response_, proof.norm_proof_};
1277 auto publication,
1279 reconstruct_circuit_zk_norm_arg_publication_v4_with_public_commitments(
1280 circuit, std::move(circuit_proof), outputs, secp_context,
1281 binding),
1282 "recovery_root_accumulation:publication");
1284 auto prepared,
1286 prepare_circuit_zk_norm_arg_v4_with_public_commitments(
1287 circuit, publication, outputs, secp_context, binding, cache),
1288 "recovery_root_accumulation:outer");
1289 if (!prepared.has_value()) {
1291 "recovery_root_accumulation:outer");
1292 }
1295 basis_, std::move(*prepared), secp_context);
1296}
1297
1299 NonceHostBoundSigningRoster roster, const UInt512& recovery_root,
1300 const RecoveryRootSetupProof& proof,
1301 purify_secp_context* secp_context) {
1303 require_secp_context(secp_context, "recovery_root:verify"),
1304 "recovery_root:verify");
1305 PURIFY_RETURN_IF_ERROR(proof.validate_encoding(secp_context),
1306 "recovery_root:verify_encoding");
1307 PURIFY_ASSIGN_OR_RETURN(const auto& circuit,
1308 public_setup_circuit(recovery_root),
1309 "recovery_root:public_circuit");
1310 PURIFY_ASSIGN_OR_RETURN(const auto& binding,
1311 setup_binding(roster, recovery_root),
1312 "recovery_root:binding");
1313 const std::array<PointBytes, 1> outputs{roster_point(roster)};
1315 proof.raw_commitment_, proof.mask_commitment_, proof.t2_,
1316 proof.public_commitment_dlog_nonce_,
1317 proof.public_commitment_dlog_response_, proof.norm_proof_};
1319 auto publication,
1321 reconstruct_circuit_zk_norm_arg_publication_v4_with_public_commitments(
1322 circuit, std::move(circuit_proof), outputs, secp_context,
1323 binding),
1324 "recovery_root:publication");
1326 const auto& root_valid,
1328 verify_circuit_zk_norm_arg_v4_with_public_commitments(
1329 circuit, publication, outputs, secp_context, binding),
1330 "recovery_root:verify_circuit");
1331 if (!root_valid) {
1333 "recovery_root:setup_proof");
1334 }
1335 return RecoveryBoundSigningRoster(std::move(roster), recovery_root);
1336}
1337
1339 const RecoveryBoundSigningRoster& roster,
1340 const FieldElement& exposed_signing_scalar,
1341 purify_secp_context* secp_context) {
1343 require_secp_context(secp_context, "recovery_root:recover"),
1344 "recovery_root:recover");
1345 PURIFY_ASSIGN_OR_RETURN(const auto& point,
1346 secp_point(exposed_signing_scalar, secp_context),
1347 "recovery_root:recovered_point");
1348 PointBytes expected{};
1349 expected[0] = 0x02;
1350 std::copy(roster.signing_key().begin(), roster.signing_key().end(),
1351 expected.begin() + 1);
1352 if (point != expected) {
1354 "recovery_root:exact_signing_key");
1355 }
1356 PURIFY_ASSIGN_OR_RETURN(auto root,
1357 derive_recovery_root(exposed_signing_scalar),
1358 "recovery_root:derive_exposed");
1359 if (root.public_key != roster.recovery_root()) {
1361 "recovery_root:exact_registered_root");
1362 }
1363 return root;
1364}
1365
1366} // namespace purify::accountable
Experimental public transcripts for scalar-only accountable signatures.
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.
JacobianPoint mul(const JacobianPoint &point, const UInt256 &scalar) const
Multiplies a point by a scalar using double-and-add.
Definition curve.cpp:148
AffinePoint affine(const JacobianPoint &point) const
Converts a Jacobian point to affine coordinates.
Definition curve.cpp:91
JacobianPoint add_mixed(const JacobianPoint &lhs, const AffinePoint &rhs) const
Adds an affine point to a Jacobian point.
Definition curve.cpp:130
JacobianPoint negate(const JacobianPoint &point) const
Negates a point without changing its projective scale.
Definition curve.cpp:99
JacobianPoint add(const JacobianPoint &lhs, const JacobianPoint &rhs) const
Adds two Jacobian points.
Definition curve.cpp:139
Field element modulo the backend scalar field used by this implementation.
Definition numeric.hpp:834
static FieldElement from_u64(std::uint64_t value)
Constructs a field element from an unsigned 64-bit integer.
Definition numeric.cpp:40
static FieldElement one()
Returns the multiplicative identity of the scalar field.
Definition numeric.cpp:36
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
UInt256 to_uint256() const
Exports the field element as a canonical 256-bit unsigned integer.
Definition numeric.cpp:79
bool is_zero() const
Returns true when the element is zero.
Definition numeric.cpp:104
static Result< SecretKey > from_packed(const UInt512 &packed)
Constructs a validated secret key from packed Purify secret bytes.
Definition secret.hpp:109
Reusable BIP340-style tagged SHA-256 helper.
Definition curve.hpp:111
std::array< unsigned char, 32 > digest_many(const std::array< std::span< const unsigned char >, Segments > &segments) const
Definition curve.hpp:123
const CoalitionCoreDigest & core_digest() const noexcept
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< 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
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 SigningRoster & signing_roster() const noexcept
A nonce-host-bound roster with an authorized recovery-root relation.
Result< Bytes > serialize(purify_secp_context *secp_context) const
static Result< RecoveryRootContribution > deserialize(std::span< const unsigned char > encoded, purify_secp_context *secp_context)
RecoveryRootContribution(const RecoveryRootContribution &)=default
const RecoveryRootShare & share() const noexcept
static constexpr unsigned char kSerializationVersion
RecoveryRootSetupAccumulator(const RecoveryRootSetupAccumulator &)=delete
Result< bppp::accumulation::AlgebraicPreparedNorm > prepare(const NonceHostBoundSigningRoster &roster, const UInt512 &recovery_root, const RecoveryRootSetupProof &proof, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *cache=nullptr) const
static Result< RecoveryRootSetupAccumulator > create(purify_secp_context *secp_context)
const bppp::accumulation::Basis & basis() const noexcept
static constexpr std::size_t kSerializedSize
RecoveryRootSetupProof(const RecoveryRootSetupProof &)=default
static Result< RecoveryRootSetupProof > deserialize(std::span< const unsigned char > encoded, purify_secp_context *secp_context)
Result< Bytes > serialize(purify_secp_context *secp_context) const
static constexpr std::size_t kNormProofSize
static constexpr unsigned char kSerializationVersion
const AffinePoint & first_sum() const noexcept
const AffinePoint & second_sum() const noexcept
const UInt512 & recovery_root() const noexcept
const AffinePoint & second() const noexcept
static Result< RecoveryRootShare > deserialize(std::span< const unsigned char > encoded)
static constexpr std::size_t kSerializedSize
const AffinePoint & first() const noexcept
Experimental public key aggregation for one ordered coalition roster.
std::span< const SigningRosterMember > members() const
Members in their original order; effective weights include parity.
const RecoveryBoundSigningRoster & roster() const noexcept
const RecoveryRootShareAggregate & aggregate() const noexcept
VerifiedDistributedRecoverySetup(const VerifiedDistributedRecoverySetup &)=delete
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< RecoveryRootContributionCircuitShape > recovery_root_contribution_circuit_shape_for_testing(const RecoveryRootShare &share)
Result< bool > recovery_root_contribution_assignment_satisfies_for_testing(const RecoveryRootShare &circuit_share, const RecoveryRootShare &assignment_share, const FieldElement &effective_scalar)
Status recovery_root_share_count_valid_for_testing(std::size_t member_count)
Result< GeneratedKey > recover_aggregated_recovery_root(const NonceHostBoundSigningRoster &roster, const RecoveryRootShareAggregate &aggregate, const FieldElement &exposed_signing_scalar, purify_secp_context *secp_context)
Result< bool > verify_recovery_root_contribution(const NonceHostBoundSigningRoster &roster, const CoalitionFormation &formation, std::size_t member_index, const RecoveryRootContribution &contribution, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *cache=nullptr)
Result< RecoveryRootSetupProof > prove_recovery_root_setup(const NonceHostBoundSigningRoster &roster, const UInt512 &recovery_root, const FieldElement &signing_scalar, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *cache=nullptr)
Result< RecoveryBoundSigningRoster > verify_recovery_root_setup(NonceHostBoundSigningRoster roster, const UInt512 &recovery_root, const RecoveryRootSetupProof &proof, purify_secp_context *secp_context)
Result< RecoveryRootShareAggregate > aggregate_recovery_root_shares(std::span< const RecoveryRootShare > ordered_shares)
Result< RecoveryRootShare > derive_recovery_root_share(const SigningRoster &roster, std::size_t member_index, const FieldElement &member_secret, purify_secp_context *secp_context)
Result< GeneratedKey > derive_recovery_root(const FieldElement &signing_scalar)
Result< RecoveryRootContribution > prove_recovery_root_contribution(const NonceHostBoundSigningRoster &roster, const CoalitionFormation &formation, std::size_t member_index, const FieldElement &member_secret, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *cache=nullptr)
Result< GeneratedKey > recover_registered_root(const RecoveryBoundSigningRoster &roster, const FieldElement &exposed_signing_scalar, purify_secp_context *secp_context)
Result< VerifiedDistributedRecoverySetup > verify_distributed_recovery_setup(NonceHostBoundSigningRoster roster, const CoalitionFormation &formation, std::span< const RecoveryRootContribution > ordered_contributions, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *cache=nullptr)
Result< AlgebraicPreparedNorm > prepare_v4_circuit_norm_for_algebraic_reduction(const Basis &basis, candidate::PreparedCircuitZkNormArgV4 prepared, purify_secp_context *context)
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
GeneratorBytes base_generator(purify_secp_context *secp_context)
Returns the serialized secp256k1 base generator used as the blind generator.
Definition bppp.cpp:1702
std::array< unsigned char, 33 > PointBytes
Compressed 33-byte curve-point encoding.
Definition bppp.hpp:32
Result< PointBytes > pedersen_commit_char(const ScalarBytes &blind, const ScalarBytes &value, purify_secp_context *secp_context)
Computes a Pedersen commitment to an arbitrary 32-byte scalar value using Purify's default generators...
Definition bppp.cpp:1793
std::array< unsigned char, 32 > ScalarBytes
Big-endian 32-byte scalar encoding.
Definition bppp.hpp:30
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
const UInt256 & prime_p()
Returns the Purify base-field modulus.
Definition curve.cpp:204
bool size_fits_u64(std::size_t value) noexcept
Definition common.hpp:83
const UInt256 & half_n2()
Returns floor(order_n2 / 2).
Definition curve.cpp:224
BigUInt< 4 > UInt256
256-bit unsigned integer used for field elements and curve orders.
Definition numeric.hpp:817
const UInt256 & order_n2()
Returns the subgroup order for the second curve.
Definition curve.cpp:214
Result< GeneratedKey > derive_key(const SecretKey &secret)
Derives the packed public key corresponding to a packed secret.
Definition api.cpp:147
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
std::pair< BigUInt< Words >, BigUInt< Words > > divmod_same(const BigUInt< Words > &numerator, const BigUInt< Words > &denominator)
Performs long division where numerator and denominator have the same width.
Definition numeric.hpp:788
const JacobianPoint & generator1()
Returns the fixed generator for the first curve.
Definition curve.cpp:282
UInt512 pack_public(const UInt256 &x1, const UInt256 &x2)
Packs two x-coordinates into the reference 512-bit public-key encoding.
Definition curve.cpp:399
Result< UInt512 > pack_secret(const UInt256 &first, const UInt256 &second)
Packs scalars in [1, half_n1] and [1, half_n2].
Definition curve.cpp:339
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
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
const UInt256 & order_n1()
Returns the subgroup order for the first curve.
Definition curve.cpp:209
ExprPoint circuit_ec_multiply_point(const EllipticCurve &curve, Transcript &transcript, const JacobianPoint &point, const std::vector< Expr > &bits)
Builds the symbolic affine-point multiplication gadget for one curve point.
ExprPoint circuit_optionally_negate_ec(const ExprPoint &point, Transcript &transcript, const Expr &negate_bit)
Conditionally negates an elliptic-curve point encoded as symbolic affine expressions.
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.
void purify_scalar_get_b32(unsigned char output32[32], const purify_scalar *value)
Serializes a scalar as 32 big-endian bytes.
struct purify_secp_context purify_secp_context
Caller-owned reusable secp256k1 context handle shared across Purify C and C++ APIs.
static BigUInt one()
Definition numeric.hpp:216
BigUInt shifted_right(std::size_t bits) const
Returns a copy shifted right by the requested bit count.
Definition numeric.hpp:571
static JacobianPoint infinity_point()
Returns the point at infinity in Jacobian coordinates.
Definition curve.cpp:82
static const purify_scalar & raw(const FieldElement &value) noexcept
Definition numeric.hpp:940
int PURIFY_UINT_FN bit(const uint64_t value[PURIFY_UINT_WORDS], size_t index)
Definition uint_impl.h:133