29using ::purify::detail::WipeOnDestruction;
31using SecretScalarBytes = ::purify::detail::Secret<bppp::ScalarBytes>;
33Status require_recovery_contribution_hash_suite(
const char* label) {
34#if PURIFY_USE_LEGACY_FIELD_HASHES
42struct CenteredScalarRelation {
45 std::vector<Expr> recoded_bits;
46 std::size_t n_bits = 0;
49struct ContributionRelation {
51 std::size_t n_bits = 0;
58struct CanonicalScalar {
63CanonicalScalar canonical_scalar(
const FieldElement& scalar) {
64 CanonicalScalar out{scalar.to_uint256(),
false};
65 FieldElement negative_scalar = scalar.negate();
67 UInt256 negative = negative_scalar.to_uint256();
69 out.negated = half_secp_order() < out.magnitude;
70 out.magnitude.conditional_assign(negative, out.negated);
74UInt256 magnitude(
const FieldElement& scalar) {
75 auto canonical = canonical_scalar(scalar);
77 return canonical.magnitude;
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));
93 std::copy(roster.signing_key().begin(), roster.signing_key().end(),
102 SecretScalarBytes encoded_value;
104 encoded_value.value.data(),
107 generator, generator);
113 if (member_index >= roster.members().size()) {
115 "recovery_contribution:member_index");
117 const auto& member = roster.members()[member_index];
118 const auto coefficient = member.coefficient.to_bytes_be();
121 coefficient.data(), weighted.data()) == 0) {
123 "recovery_contribution:weighted_key");
129 std::span<const unsigned char> encoded,
const EllipticCurve& curve,
131 if (encoded.size() != 33) {
134 if (encoded[0] != 0x02 && encoded[0] != 0x03) {
137 std::array<unsigned char, 32> x_bytes{};
138 std::copy(encoded.begin() + 1, encoded.end(), x_bytes.begin());
141 auto point = curve.lift_x(x);
142 if (!point || point->infinity) {
145 auto affine = curve.affine(*point);
146 if (affine.y.is_odd() != (encoded[0] == 0x03)) {
147 affine.y = affine.y.negate();
149 const JacobianPoint exact{
151 if (!curve.mul(exact, curve.order()).infinity) {
157Status validate_recovery_root_share_count(std::size_t member_count) {
158 if (member_count == 0) {
160 "recovery_share_aggregate:empty");
164 "recovery_share_aggregate:member_count");
175 assert(lhs < modulus && rhs < modulus);
176 auto distance = modulus;
177 distance.sub_assign(rhs);
178 if (lhs < distance) {
181 lhs.sub_assign(distance);
188 assert(lhs < modulus && rhs < modulus);
193 auto difference = rhs;
194 difference.sub_assign(lhs);
196 out.sub_assign(difference);
204 if (residue.is_zero()) {
207 auto negative = modulus;
208 negative.sub_assign(residue);
209 residue.conditional_assign(negative, half_modulus < residue);
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;
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());
227std::vector<Expr> ordinary_bits(Transcript& transcript,
228 const std::optional<UInt256>& value) {
229 std::vector<Expr> bits;
231 for (std::size_t i = 0; i < 255; ++i) {
233 ? std::optional<FieldElement>(
237 bits.push_back(transcript.boolean(transcript.secret(
bit)));
242Expr exceeds_bound(Transcript& transcript,
const std::vector<Expr>& bits,
244 Expr equal_prefix(1);
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;
252 exceeds = exceeds + same_and_one;
253 equal_prefix = equal_prefix - same_and_one;
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}) {
266 Expr(1) - ordinary[i] - ordinary[i - offset] +
267 2 * transcript.mul(ordinary[i], ordinary[i - offset]);
274 Transcript& transcript,
275 const FieldElement* scalar =
nullptr) {
276 if (scalar && scalar->is_zero()) {
278 "recovery_root:zero_witness");
280 std::optional<UInt256> value;
281 std::optional<FieldElement> sign_value;
285 auto canonical = canonical_scalar(*scalar);
287 value = canonical.magnitude;
291 const auto bits = ordinary_bits(transcript, value);
292 const Expr sign = transcript.boolean(transcript.secret(sign_value));
293 auto bound = half_secp_order();
295 transcript.equal(exceeds_bound(transcript, bits, bound), Expr(0));
296 Expr scalar_value(1);
298 for (
const auto&
bit : bits) {
299 scalar_value = scalar_value + power *
bit;
300 power = power + power;
302 return CenteredScalarRelation{
303 sign, scalar_value, signed_window_bits(transcript, bits),
308 Transcript& transcript,
309 const FieldElement* scalar =
nullptr) {
311 auto centered, centered_scalar_relation(transcript, scalar),
312 "recovery_root:centered_scalar");
317 return CircuitMainResult{
318 transcript.mul(Expr(1) - 2 * centered.sign,
320 first, second, centered.n_bits};
325 const FieldElement* scalar =
nullptr) {
327 auto centered, centered_scalar_relation(transcript, scalar),
328 "recovery_contribution:centered_scalar");
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,
348 "recovery_root:root");
349 Transcript transcript;
351 "recovery_root:relation");
352 BulletproofTranscript bp;
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();
362 const UInt512& root,
const FieldElement& scalar) {
363 Transcript transcript;
365 setup_relation(transcript, &scalar),
366 "recovery_root:witness");
367 BulletproofTranscript bp;
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);
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();
386 BulletproofTranscript bp;
389 "recovery_contribution:lower");
391 bp.bind_output_to_commitment(relation.output),
392 "recovery_contribution:bind_output");
393 return bp.native_circuit();
398 Transcript transcript;
401 contribution_relation(transcript, share, &effective_scalar),
402 "recovery_contribution:witness_relation");
403 BulletproofTranscript bp;
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);
415 if (roster.members().empty()) {
417 "recovery_root:empty_roster");
421 "recovery_root:roster_size");
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(),
432 return Bytes(digest.begin(), digest.end());
439 if (roster.members().empty()) {
441 "recovery_contribution:empty_roster");
443 if (member_index >= roster.members().size()) {
445 "recovery_contribution:member_index");
447 if (formation.core_digest() != roster.coalition_core_digest()) {
449 "recovery_contribution:formation_core");
454 "recovery_contribution:roster_size");
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(),
472 return Bytes(digest.begin(), digest.end());
480 append_companion_point(out, first_);
481 append_companion_point(out, second_);
487 std::span<const unsigned char> encoded) {
490 "recovery_contribution:share_size");
494 decode_companion_point(encoded.first<33>(),
curve1(),
495 "recovery_contribution:first_share"),
496 "recovery_contribution:first_share");
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));
511 secp_context,
"recovery_contribution:derive_share"),
512 "recovery_contribution:derive_share");
516 "recovery_contribution:zero_member_secret");
518 if (member_index >= roster.
members().size()) {
521 "recovery_contribution:member_index");
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) {
530 "recovery_contribution:member_secret");
532 auto effective_scalar =
533 roster.
members()[member_index].coefficient * member_secret;
535 if (effective_scalar.is_zero()) {
538 "recovery_contribution:zero_effective_scalar");
540 auto centered = canonical_scalar(effective_scalar);
545 "recovery_contribution:first_share");
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));
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) {
568 "recovery_share_aggregate:infinite_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);
586 secp_context,
"recovery_share_aggregate:recover"),
587 "recovery_share_aggregate:recover");
590 "recovery_share_aggregate:member_count");
592 if (exposed_signing_scalar.
is_zero()) {
594 "recovery_share_aggregate:zero_scalar");
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");
605 const auto scalar = exposed_signing_scalar.
to_uint256();
608 if (first_step.infinity || second_step.infinity) {
610 "recovery_share_aggregate:carry_step");
614 auto first_residue = reduce_mod(scalar,
order_n1());
616 auto second_residue = reduce_mod(scalar,
order_n2());
624 const auto negative_steps =
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());
639 std::optional<std::pair<UInt256, UInt256>> match;
641 std::size_t match_count = 0;
642 const auto candidate_advances = aggregate.
member_count() -
644 for (std::size_t candidate_index = 0;; ++candidate_index) {
648 match = std::pair{first_residue, second_residue};
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());
658 if (match_count != 1 || !match) {
662 ?
"recovery_share_aggregate:missing_carry"
663 :
"recovery_share_aggregate:ambiguous_carry");
669 "recovery_share_aggregate:first_zero"),
670 "recovery_share_aggregate:first_secret");
675 "recovery_share_aggregate:second_zero"),
676 "recovery_share_aggregate:second_secret");
679 auto packed,
pack_secret(first_secret, second_secret),
680 "recovery_share_aggregate:pack");
684 "recovery_share_aggregate:secret");
686 "recovery_share_aggregate:derive");
689 "recovery_share_aggregate:exact_root");
694Status RecoveryRootContribution::validate_encoding(
698 "recovery_contribution:proof_encoding"),
699 "recovery_contribution:proof_encoding");
700 static constexpr std::size_t kRoundCount = 14;
703 raw_commitment_, mask_commitment_, t2_,
704 public_commitment_dlog_nonce_,
705 public_commitment_dlog_response_, norm_proof_};
709 kRoundCount, secp_context);
715 "recovery_contribution:serialize");
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());
734 std::span<const unsigned char> encoded,
736 static constexpr std::size_t kProofOffset =
738 static constexpr std::size_t kNormOffset = kProofOffset + 162;
742 "recovery_contribution:deserialize_size");
746 "recovery_contribution:deserialize_version");
752 "recovery_contribution:deserialize_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());
774 "recovery_contribution:deserialize");
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;
790 contribution_circuit(share, &relation_multiplications),
791 "recovery_contribution:shape");
793 relation_multiplications, circuit.n_gates};
797 std::size_t member_count) {
798 return validate_recovery_root_share_count(member_count);
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);
825 "recovery_contribution:prove");
827 require_recovery_contribution_hash_suite(
828 "recovery_contribution:prove_hash_suite"),
829 "recovery_contribution:prove_hash_suite");
835 "recovery_contribution:derive_share");
837 auto effective_scalar =
838 roster.
members()[member_index].coefficient * member_secret;
841 const auto circuit, contribution_circuit(contribution.share_),
842 "recovery_contribution:circuit");
845 contribution_assignment(contribution.share_, effective_scalar),
846 "recovery_contribution:assignment");
849 contribution_binding(roster, formation, member_index,
850 contribution.share_),
851 "recovery_contribution:binding");
852 SecretScalarBytes effective_bytes;
854 effective_bytes.value.data(),
857 "Purify/Accountable/RecoveryRootContributionProverSeed/v1");
858 SecretScalarBytes prover_seed;
859 prover_seed.value = prover_seed_hash.
digest_many<2>({
860 effective_bytes.value, binding});
863 weighted_member_point(roster, member_index, secp_context),
864 "recovery_contribution:weighted_key");
865 const std::array<PointBytes, 1> outputs{weighted};
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);
882 "recovery_contribution:proof_encoding");
894 "recovery_contribution:verify");
896 require_recovery_contribution_hash_suite(
897 "recovery_contribution:verify_hash_suite"),
898 "recovery_contribution:verify_hash_suite");
900 "recovery_contribution:proof_encoding");
902 const auto circuit, contribution_circuit(contribution.share_),
903 "recovery_contribution:circuit");
906 contribution_binding(roster, formation, member_index,
907 contribution.share_),
908 "recovery_contribution:binding");
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_};
922 reconstruct_circuit_zk_norm_arg_publication_v4_with_public_commitments(
923 circuit, std::move(proof), outputs, secp_context, binding),
924 "recovery_contribution:publication");
927 circuit, publication, outputs, secp_context, binding, cache);
934 std::span<const RecoveryRootContribution> ordered_contributions,
937 if (ordered_contributions.size() !=
roster.members().size()) {
940 "recovery_contribution_set:member_count");
945 "recovery_contribution_set:formation_core");
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) {
954 roster, formation, i, ordered_contributions[i],
955 secp_context, cache),
956 "recovery_contribution_set:verify");
960 "recovery_contribution_set:proof");
962 ordered_shares.push_back(ordered_contributions[i].share());
967 "recovery_contribution_set:aggregate");
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");
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};
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)};
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");
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_};
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();
1063 prepare_circuit_zk_norm_arg_v4_with_public_commitments(
1064 circuit, publication, outputs, secp_context, binding,
1066 "recovery_contribution_accumulation:outer");
1067 if (!prepared.has_value()) {
1070 "recovery_contribution_accumulation:outer");
1075 prepare_v4_circuit_norm_for_algebraic_reduction(
1076 basis_, std::move(*prepared), secp_context),
1077 "recovery_contribution_accumulation:norm");
1082Status RecoveryRootSetupProof::validate_encoding(
1086 "recovery_root:proof_encoding");
1087 static constexpr std::size_t kRoundCount = 12;
1090 raw_commitment_, mask_commitment_, t2_,
1091 public_commitment_dlog_nonce_,
1092 public_commitment_dlog_response_, norm_proof_};
1096 kRoundCount, secp_context);
1102 "recovery_root:serialize");
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());
1120 std::span<const unsigned char> encoded,
1122 constexpr std::size_t header_size = 163;
1126 "recovery_root:deserialize_header");
1130 "recovery_root:deserialize_version");
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,
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());
1145 "recovery_root:deserialize");
1151 if (signing_scalar.
is_zero()) {
1153 "recovery_root:zero_scalar");
1155 auto first = magnitude(signing_scalar);
1157 auto second = first;
1161 reduced_second.sub_assign(first);
1162 second.conditional_assign(reduced_second,
half_n2() < second);
1164 "recovery_root:pack");
1167 "recovery_root:secret");
1178 "recovery_root:prove");
1179 if (signing_scalar.
is_zero()) {
1181 "recovery_root:zero_signing_scalar");
1184 secp_point(signing_scalar, secp_context),
1185 "recovery_root:signing_point");
1186 if (scalar_point != roster_point(roster)) {
1188 "recovery_root:signing_scalar");
1192 "recovery_root:derive");
1193 if (expected_root.public_key != recovery_root) {
1195 "recovery_root:expected_root");
1198 public_setup_circuit(recovery_root),
1199 "recovery_root:public_circuit");
1201 setup_assignment(recovery_root, signing_scalar),
1202 "recovery_root:assignment");
1204 setup_binding(roster, recovery_root),
1205 "recovery_root:binding");
1206 SecretScalarBytes encoded_scalar;
1208 encoded_scalar.value.data(),
1210 SecretScalarBytes secret_prover_seed;
1212 "Purify/Accountable/RecoveryRootProverSeed/v2");
1213 auto prover_seed = prover_seed_hash.
digest_many<2>(
1214 {encoded_scalar.value, binding});
1216 secret_prover_seed.value = prover_seed;
1217 const std::array<PointBytes, 1> outputs{roster_point(roster)};
1221 prove_circuit_zk_norm_arg_v4_with_public_commitments(
1222 circuit, assignment, secret_prover_seed.value, outputs,
1223 secp_context, binding,
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);
1246 "Purify/Accountable/RecoveryRootSetupAccumulation/v2",
1248 "recovery_root_accumulation:basis");
1261 "recovery_root_accumulation:prepare"),
1262 "recovery_root_accumulation:prepare");
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_};
1279 reconstruct_circuit_zk_norm_arg_publication_v4_with_public_commitments(
1280 circuit, std::move(circuit_proof), outputs, secp_context,
1282 "recovery_root_accumulation:publication");
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");
1295 basis_, std::move(*prepared), secp_context);
1304 "recovery_root:verify");
1306 "recovery_root:verify_encoding");
1309 "recovery_root:public_circuit");
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_};
1321 reconstruct_circuit_zk_norm_arg_publication_v4_with_public_commitments(
1322 circuit, std::move(circuit_proof), outputs, secp_context,
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");
1333 "recovery_root:setup_proof");
1335 return RecoveryBoundSigningRoster(std::move(roster),
recovery_root);
1344 "recovery_root:recover");
1346 secp_point(exposed_signing_scalar, secp_context),
1347 "recovery_root:recovered_point");
1348 PointBytes expected{};
1351 expected.begin() + 1);
1352 if (point != expected) {
1354 "recovery_root:exact_signing_key");
1358 "recovery_root:derive_exposed");
1361 "recovery_root:exact_registered_root");
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.
AffinePoint affine(const JacobianPoint &point) const
Converts a Jacobian point to affine coordinates.
JacobianPoint add_mixed(const JacobianPoint &lhs, const AffinePoint &rhs) const
Adds an affine point to a Jacobian point.
JacobianPoint negate(const JacobianPoint &point) const
Negates a point without changing its projective scale.
JacobianPoint add(const JacobianPoint &lhs, const JacobianPoint &rhs) const
Adds two Jacobian points.
Field element modulo the backend scalar field used by this implementation.
static FieldElement from_u64(std::uint64_t value)
Constructs a field element from an unsigned 64-bit integer.
static FieldElement one()
Returns the multiplicative identity of the scalar field.
static Result< FieldElement > try_from_bytes32(const std::array< unsigned char, 32 > &bytes)
Decodes a canonical 32-byte big-endian field element.
UInt256 to_uint256() const
Exports the field element as a canonical 256-bit unsigned integer.
bool is_zero() const
Returns true when the element is zero.
static Result< SecretKey > from_packed(const UInt512 &packed)
Constructs a validated secret key from packed Purify secret bytes.
Reusable BIP340-style tagged SHA-256 helper.
std::array< unsigned char, 32 > digest_many(const std::array< std::span< const unsigned char >, Segments > &segments) const
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.
const UInt512 & recovery_root() const
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 std::size_t kNormProofSize
static constexpr unsigned char kSerializationVersion
static constexpr std::size_t kSerializedSize
static constexpr std::size_t kNormVectorLength
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
static constexpr std::size_t kMaxMembers
const UInt512 & recovery_root() const noexcept
std::size_t member_count() 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.
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.
#define PURIFY_ASSIGN_OR_RETURN(lhs, expr, context)
Evaluates an expected-like expression, binds the value to lhs, and propagates errors.
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)
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)
GeneratorBytes base_generator(purify_secp_context *secp_context)
Returns the serialized secp256k1 base generator used as the blind generator.
std::array< unsigned char, 33 > PointBytes
Compressed 33-byte curve-point encoding.
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...
std::array< unsigned char, 32 > ScalarBytes
Big-endian 32-byte scalar encoding.
Status require_secp_context(const purify_secp_context *context, const char *error_context)
const UInt256 & half_n1()
Returns floor(order_n1 / 2).
constexpr Unexpected< Error > unexpected_error(ErrorCode code, const char *context=nullptr)
Constructs an unexpected Error value from a machine-readable code.
BigUInt< 8 > UInt512
512-bit unsigned integer used for private and packed public keys.
const UInt256 & prime_p()
Returns the Purify base-field modulus.
bool size_fits_u64(std::size_t value) noexcept
const UInt256 & half_n2()
Returns floor(order_n2 / 2).
BigUInt< 4 > UInt256
256-bit unsigned integer used for field elements and curve orders.
const UInt256 & order_n2()
Returns the subgroup order for the second curve.
Result< GeneratedKey > derive_key(const SecretKey &secret)
Derives the packed public key corresponding to a packed secret.
const EllipticCurve & curve1()
Returns the first Purify curve instance.
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.
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.
const JacobianPoint & generator1()
Returns the fixed generator for the first curve.
UInt512 pack_public(const UInt256 &x1, const UInt256 &x2)
Packs two x-coordinates into the reference 512-bit public-key encoding.
Result< UInt512 > pack_secret(const UInt256 &first, const UInt256 &second)
Packs scalars in [1, half_n1] and [1, half_n2].
Expected< void, Error > Status
Expected-returning convenience alias for Purify status-only APIs.
std::vector< unsigned char > Bytes
Dynamically sized byte string used for messages, serialized witnesses, and proofs.
const EllipticCurve & curve2()
Returns the second Purify curve instance.
const JacobianPoint & generator2()
Returns the fixed generator for the second curve.
const UInt256 & order_n1()
Returns the subgroup order for the first curve.
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.
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.
BigUInt shifted_right(std::size_t bits) const
Returns a copy shifted right by the requested bit count.
static JacobianPoint infinity_point()
Returns the point at infinity in Jacobian coordinates.
static const purify_scalar & raw(const FieldElement &value) noexcept
int PURIFY_UINT_FN bit(const uint64_t value[PURIFY_UINT_WORDS], size_t index)