purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
bppp.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Judica, Inc.
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or https://opensource.org/license/mit/.
4
9
12
13#include <algorithm>
14#include <array>
15#include <span>
16#include <string_view>
17#include <type_traits>
18#include <vector>
19
20#include "detail/common.hpp"
23#include "purify/secp_bridge.h"
24#include "secure_cleanup.hpp"
25
27
28namespace api_impl {
29
30Result<Signature> sign_message_with_prepared(const SecretKey& secret, std::span<const unsigned char> message,
31 PreparedNonce prepared, purify_secp_context* secp_context);
32Result<Signature> sign_with_prepared_topic(const SecretKey& secret, std::span<const unsigned char> message,
33 PreparedNonce prepared, purify_secp_context* secp_context);
34
35} // namespace api_impl
36
37namespace {
38
39constexpr std::string_view kMessageNonceTag = "PureSign/Nonce/Message/";
40constexpr std::string_view kTopicNonceTag = "PureSign/Nonce/Topic/";
41constexpr std::string_view kMessageBindingTag = "PureSign/Binding/Message";
42constexpr std::string_view kTopicBindingTag = "PureSign/Binding/Topic";
43constexpr std::string_view kMessageProofTag = "PureSign/BPPP/Proof/Message/V4";
44constexpr std::string_view kTopicProofTag = "PureSign/BPPP/Proof/Topic/V4";
45const TaggedHash kCertificateNonceProofHash(
46 "Purify/Accountable/CertificateNonceProof/v1");
47const TaggedHash kCertificateNonceLeafHash(
48 "Purify/Accountable/CertificateNonceLeaf/v1");
49
50using Scope = PreparedNonce::Scope;
52
53const TaggedHash& binding_tagged_hash(Scope scope) {
54 static const TaggedHash kMessageBindingTaggedHash(kMessageBindingTag);
55 static const TaggedHash kTopicBindingTaggedHash(kTopicBindingTag);
56 return scope == Scope::Message ? kMessageBindingTaggedHash : kTopicBindingTaggedHash;
57}
58
59const TaggedHash& proof_tagged_hash(Scope scope) {
60 static const TaggedHash kMessageProofTaggedHash(kMessageProofTag);
61 static const TaggedHash kTopicProofTaggedHash(kTopicProofTag);
62 return scope == Scope::Message ? kMessageProofTaggedHash : kTopicProofTaggedHash;
63}
64
65const UInt256& secp256k1_order() {
66 static const UInt256 value =
67 UInt256::from_hex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141");
68 return value;
69}
70
71Status validate_puresign_field_alignment() {
72 if (prime_p() != secp256k1_order()) {
73 return unexpected_error(ErrorCode::InternalMismatch, "puresign_plusplus:field_order_mismatch");
74 }
75 return {};
76}
77
78const unsigned char* nullable_data(std::span<const unsigned char> input) {
79 return input.empty() ? nullptr : input.data();
80}
81
82XOnly32 binding_digest(const TaggedHash& tag, std::span<const unsigned char> input) {
83 XOnly32 out{};
84 out = tag.digest(input);
85 return out;
86}
87
88std::string_view proof_tag_for_scope(Scope scope) {
89 return scope == Scope::Message ? kMessageProofTag : kTopicProofTag;
90}
91
92Bytes proof_statement_binding(Scope scope) {
93 return bytes_from_ascii(proof_tag_for_scope(scope));
94}
95
96using SecretProofSeed = ::purify::detail::Secret<Scalar32>;
97using SecretPackedKeyBytes =
98 ::purify::detail::Secret<std::array<unsigned char, 64>>;
99
100void derive_secret_prover_seed(const SecretKey& secret, Scope scope,
101 std::span<const unsigned char> eval_input,
102 SecretProofSeed& out) {
103 SecretPackedKeyBytes secret_bytes;
104 ::purify::detail::BigUIntCBridge<8>::to_bytes_be(
105 secret_bytes.value.data(), secret.packed().limbs.data());
106 std::array digest_segments{
107 std::span<const unsigned char>(secret_bytes.value.data(), secret_bytes.value.size()),
108 eval_input,
109 };
110 Scalar32 digest = proof_tagged_hash(scope).digest_many(digest_segments);
111 out.value = digest;
112 detail::secure_clear_bytes(digest.data(), digest.size());
113}
114
115Status validate_public_key_bundle(const PublicKey& public_key, purify_secp_context* secp_context) {
116 PURIFY_RETURN_IF_ERROR(validate_public_key(public_key.purify_pubkey),
117 "puresign_plusplus:validate_public_key_bundle:purify_pubkey");
118 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "puresign_plusplus:validate_public_key_bundle:secp_context"),
119 "puresign_plusplus:validate_public_key_bundle:secp_context");
120 if (purify_bip340_validate_xonly_pubkey(secp_context, public_key.bip340_pubkey.data()) == 0) {
122 "puresign_plusplus:validate_public_key_bundle:bip340_pubkey");
123 }
124 return {};
125}
126
128 return detail::validate_message_proof_cache(cache, kMessageNonceTag);
129}
130
132 return detail::validate_topic_proof_cache(cache, kTopicNonceTag);
133}
134
135struct DerivedNonceData {
136 Scope scope = Scope::Message;
137 Scalar32 scalar{};
138 Nonce nonce{};
139 XOnly32 signer_pubkey{};
140 XOnly32 binding_digest{};
141 Bytes eval_input;
142
143 DerivedNonceData() = default;
144 DerivedNonceData(const DerivedNonceData&) = delete;
145 DerivedNonceData& operator=(const DerivedNonceData&) = delete;
146 DerivedNonceData(DerivedNonceData&& other) noexcept
147 : scope(other.scope), scalar(other.scalar), nonce(other.nonce),
148 signer_pubkey(other.signer_pubkey), binding_digest(other.binding_digest),
149 eval_input(std::move(other.eval_input)) {
150 other.clear();
151 }
152 DerivedNonceData& operator=(DerivedNonceData&& other) noexcept {
153 if (this != &other) {
154 clear();
155 scope = other.scope;
156 scalar = other.scalar;
157 nonce = other.nonce;
158 signer_pubkey = other.signer_pubkey;
159 binding_digest = other.binding_digest;
160 eval_input = std::move(other.eval_input);
161 other.clear();
162 }
163 return *this;
164 }
165 ~DerivedNonceData() { clear(); }
166
167private:
168 void clear() noexcept {
169 detail::secure_clear_bytes(scalar.data(), scalar.size());
170 }
171};
172
173Result<DerivedNonceData> derive_nonce_data(const SecretKey& secret,
174 Scope scope,
175 std::span<const unsigned char> input,
176 purify_secp_context* secp_context) {
177 PURIFY_RETURN_IF_ERROR(validate_puresign_field_alignment(), "derive_nonce_data:validate_puresign_field_alignment");
178 PURIFY_ASSIGN_OR_RETURN(const auto& signer, derive_bip340_key(secret, secp_context), "derive_nonce_data:derive_bip340_key");
179
180 const std::string_view nonce_tag = scope == Scope::Message ? kMessageNonceTag : kTopicNonceTag;
181 const TaggedHash& binding_hash = binding_tagged_hash(scope);
182
183 DerivedNonceData out{};
184 out.scope = scope;
185 out.signer_pubkey = signer.xonly_pubkey;
186 out.binding_digest = binding_digest(binding_hash, input);
187 out.eval_input = detail::tagged_eval_input(nonce_tag, input);
188
189 auto nonce_value = eval(secret, out.eval_input);
190 if (!nonce_value.has_value()) {
191 return unexpected_error(nonce_value.error(), "derive_nonce_data:eval");
192 }
193 if (nonce_value->is_zero()) {
194 detail::secure_clear_bytes(&*nonce_value, sizeof(*nonce_value));
196 "derive_nonce_data:zero_nonce");
197 }
198 static_assert(std::is_trivially_destructible_v<FieldElement>);
200 out.scalar.data(),
202 detail::secure_clear_bytes(&*nonce_value, sizeof(*nonce_value));
203
204 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "derive_nonce_data:secp_context"),
205 "derive_nonce_data:secp_context");
206 if (purify_bip340_nonce_from_scalar(secp_context, out.scalar.data(), out.nonce.xonly.data()) == 0) {
207 return unexpected_error(ErrorCode::BackendRejectedInput, "derive_nonce_data:bip340_nonce_from_scalar");
208 }
209 return out;
210}
211
212Result<DerivedNonceData> prepare_nonce_data_impl(const SecretKey& secret,
213 Scope scope,
214 std::span<const unsigned char> input,
215 purify_secp_context* secp_context) {
216 if (scope == Scope::Topic && input.empty()) {
217 return unexpected_error(ErrorCode::EmptyInput, "prepare_nonce_data_impl:empty_topic");
218 }
219 PURIFY_ASSIGN_OR_RETURN(auto nonce_data, derive_nonce_data(secret, scope, input, secp_context),
220 "prepare_nonce_data_impl:derive_nonce_data");
221 return nonce_data;
222}
223
224Result<bppp::PointBytes> commitment_point_from_scalar(const FieldElement& scalar,
225 purify_secp_context* secp_context) {
226 Scalar32 blind{};
227 return bppp::pedersen_commit_char(blind, bppp::scalar_bytes(scalar), secp_context,
228 bppp::base_generator(secp_context), bppp::base_generator(secp_context));
229}
230
231Result<bool> nonce_proof_matches_nonce(const NonceProof& nonce_proof,
232 purify_secp_context* secp_context) {
233 XOnly32 xonly{};
234 int parity = 0;
235 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "nonce_proof_matches_nonce:secp_context"),
236 "nonce_proof_matches_nonce:secp_context");
237 if (purify_bip340_xonly_from_point(secp_context, nonce_proof.commitment_point.data(), xonly.data(), &parity) == 0) {
239 "nonce_proof_matches_nonce:invalid_commitment_point");
240 }
241 (void)parity;
242 return xonly == nonce_proof.nonce.xonly;
243}
244
245Result<NonceProof> build_nonce_proof_from_template(const SecretKey& secret,
246 Scope scope,
247 const Nonce& expected_nonce,
248 std::span<const unsigned char> eval_input,
249 const NativeBulletproofCircuitTemplate& circuit_template,
250 purify_secp_context* secp_context,
251 bppp::ExperimentalCircuitBackend* circuit_cache) {
252 auto witness_result = prove_assignment_data(detail::copy_bytes(eval_input), secret);
253 if (!witness_result.has_value()) {
254 return unexpected_error(witness_result.error(),
255 "build_nonce_proof_from_template:prove_assignment_data");
256 }
257 BulletproofWitnessData& witness = *witness_result;
258 PURIFY_ASSIGN_OR_RETURN(auto partial_ok, circuit_template.partial_evaluate(witness.assignment),
259 "build_nonce_proof_from_template:partial_evaluate");
260 if (!partial_ok) {
261 return unexpected_error(ErrorCode::BindingMismatch, "build_nonce_proof_from_template:partial_evaluate_false");
262 }
263 PURIFY_ASSIGN_OR_RETURN(auto final_ok, circuit_template.final_evaluate(witness.assignment, witness.public_key),
264 "build_nonce_proof_from_template:final_evaluate");
265 if (!final_ok) {
266 return unexpected_error(ErrorCode::BindingMismatch, "build_nonce_proof_from_template:final_evaluate_false");
267 }
268 PURIFY_ASSIGN_OR_RETURN(const auto& circuit, circuit_template.instantiate(witness.public_key),
269 "build_nonce_proof_from_template:instantiate");
270 PURIFY_RETURN_IF_ERROR(detail::validate_proof_cache_circuit(circuit, "build_nonce_proof_from_template:circuit_shape"),
271 "build_nonce_proof_from_template:validate_proof_cache_circuit");
272
273 PURIFY_ASSIGN_OR_RETURN(auto commitment_point, commitment_point_from_scalar(witness.assignment.commitments.front(), secp_context),
274 "build_nonce_proof_from_template:commitment_point_from_scalar");
275 SecretProofSeed secret_prover_seed;
276 derive_secret_prover_seed(secret, scope, eval_input, secret_prover_seed);
277 Bytes statement_binding = proof_statement_binding(scope);
278 std::array<bppp::PointBytes, 1> public_commitments{commitment_point};
280 auto publication,
281 bppp::candidate::
283 circuit, witness.assignment, secret_prover_seed.value,
284 public_commitments, secp_context, statement_binding, circuit_cache),
285 "build_nonce_proof_from_template:prove_circuit_zk_norm_arg_v4_with_public_commitments");
286
287 NonceProof out{};
288 out.nonce = expected_nonce;
289 out.commitment_point = commitment_point;
290 out.raw_commitment = publication.proof.raw_commitment;
291 out.mask_commitment = publication.proof.mask_commitment;
292 out.t2 = publication.proof.t2;
293 out.public_commitment_dlog_nonce =
294 publication.proof.public_commitment_dlog_nonce;
295 out.public_commitment_dlog_response =
296 publication.proof.public_commitment_dlog_response;
297 out.norm_proof = std::move(publication.proof.norm_proof);
298 PURIFY_ASSIGN_OR_RETURN(auto match, nonce_proof_matches_nonce(out, secp_context),
299 "build_nonce_proof_from_template:nonce_proof_matches_nonce");
300 if (!match) {
301 return unexpected_error(ErrorCode::InternalMismatch, "build_nonce_proof_from_template:nonce_mismatch");
302 }
303 return out;
304}
305
306Result<NonceProof> build_nonce_proof(const SecretKey& secret,
307 Scope scope,
308 std::span<const unsigned char> input,
309 const Nonce& expected_nonce,
310 purify_secp_context* secp_context,
311 bppp::ExperimentalCircuitBackend* circuit_cache) {
312 const std::string_view nonce_tag = scope == Scope::Message ? kMessageNonceTag : kTopicNonceTag;
313 PURIFY_ASSIGN_OR_RETURN(const auto& circuit_template, verifier_circuit_template(detail::tagged_eval_input(nonce_tag, input)),
314 "build_nonce_proof:verifier_circuit_template");
315 Bytes eval_input = detail::tagged_eval_input(nonce_tag, input);
316 return build_nonce_proof_from_template(secret, scope, expected_nonce, eval_input, circuit_template, secp_context,
317 circuit_cache);
318}
319
320Result<bool> verify_nonce_proof_with_circuit_impl(
321 const UInt512& nonce_host_public_key,
322 const NativeBulletproofCircuit& circuit,
323 const NonceProof& nonce_proof,
324 Scope scope,
325 purify_secp_context* secp_context,
326 const char* context,
327 bppp::ExperimentalCircuitBackend* circuit_cache) {
329 validate_public_key(nonce_host_public_key), context);
331 PURIFY_ASSIGN_OR_RETURN(auto match, nonce_proof_matches_nonce(nonce_proof, secp_context), context);
332 if (!match) {
333 return false;
334 }
335 Bytes statement_binding = proof_statement_binding(scope);
336 std::array<bppp::PointBytes, 1> public_commitments{nonce_proof.commitment_point};
337 bppp::candidate::CircuitZkNormArgProofV4 proof{
338 nonce_proof.raw_commitment,
339 nonce_proof.mask_commitment,
340 nonce_proof.t2,
341 nonce_proof.public_commitment_dlog_nonce,
342 nonce_proof.public_commitment_dlog_response,
343 nonce_proof.norm_proof,
344 };
346 auto publication,
347 bppp::candidate::
349 circuit, std::move(proof), public_commitments, secp_context,
350 statement_binding),
351 context);
352 return bppp::candidate::
354 circuit, publication, public_commitments, secp_context,
355 statement_binding, circuit_cache);
356}
357
358Result<bool> verify_nonce_proof_with_circuit(
359 const PublicKey& public_key,
360 const NativeBulletproofCircuit& circuit,
361 const NonceProof& nonce_proof,
362 Scope scope,
363 purify_secp_context* secp_context,
364 const char* context,
365 bppp::ExperimentalCircuitBackend* circuit_cache) {
367 validate_public_key_bundle(public_key, secp_context), context);
368 return verify_nonce_proof_with_circuit_impl(
369 public_key.purify_pubkey, circuit, nonce_proof, scope,
370 secp_context, context, circuit_cache);
371}
372
373} // namespace
374
378 "MessageProofCache::build:verifier_circuit_template");
380 "MessageProofCache::build:integrity_digest");
381 MessageProofCache cache{};
383 cache.eval_input = std::move(eval_input);
384 cache.circuit_template = std::move(circuit_template);
385 cache.template_digest = std::move(template_digest);
386 return cache;
387}
388
389Result<TopicProofCache> TopicProofCache::build(std::span<const unsigned char> topic) {
390 if (topic.empty()) {
391 return unexpected_error(ErrorCode::EmptyInput, "TopicProofCache::build:empty_topic");
392 }
395 "TopicProofCache::build:verifier_circuit_template");
397 "TopicProofCache::build:integrity_digest");
398 TopicProofCache cache{};
400 cache.eval_input = std::move(eval_input);
401 cache.circuit_template = std::move(circuit_template);
402 cache.template_digest = std::move(template_digest);
403 return cache;
404}
405
406PreparedNonce::PreparedNonce(Scope scope, const Scalar32& scalar, const Nonce& nonce,
407 const XOnly32& signer_pubkey, const XOnly32& binding_digest)
408 : scope_(scope), scalar_(scalar), nonce_(nonce),
409 signer_pubkey_(signer_pubkey), binding_digest_(binding_digest),
410 available_(true) {}
411
413 const XOnly32& signer_pubkey, const XOnly32& binding_digest) {
414 return PreparedNonce(scope, scalar, nonce, signer_pubkey, binding_digest);
415}
416
418 : scope_(other.scope_), scalar_(other.scalar_), nonce_(other.nonce_),
419 signer_pubkey_(other.signer_pubkey_), binding_digest_(other.binding_digest_),
420 available_(other.available_) {
421 other.clear();
422}
423
425 if (this != &other) {
426 clear();
427 scope_ = other.scope_;
428 scalar_ = other.scalar_;
429 nonce_ = other.nonce_;
430 signer_pubkey_ = other.signer_pubkey_;
431 binding_digest_ = other.binding_digest_;
432 available_ = other.available_;
433 other.clear();
434 }
435 return *this;
436}
437
439 clear();
440}
441
442void PreparedNonce::clear() noexcept {
443 detail::secure_clear_bytes(scalar_.data(), scalar_.size());
444 available_ = false;
445}
446
448 std::span<const unsigned char> message,
449 purify_secp_context* secp_context) && {
450 if (!available_) {
452 "PreparedNonce::sign_message:consumed");
453 }
454 available_ = false;
455 WipeOnDestruction burn(scalar_);
456 if (scope_ != Scope::Message) {
457 return unexpected_error(ErrorCode::BindingMismatch, "PreparedNonce::sign_message:scope");
458 }
459 if (signer_pubkey_ != signer.xonly_pubkey) {
460 return unexpected_error(ErrorCode::BindingMismatch, "PreparedNonce::sign_message:signer_pubkey");
461 }
462 if (binding_digest_ != binding_digest(binding_tagged_hash(Scope::Message), message)) {
463 return unexpected_error(ErrorCode::BindingMismatch, "PreparedNonce::sign_message:message_binding");
464 }
465
466 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "PreparedNonce::sign_message:secp_context"),
467 "PreparedNonce::sign_message:secp_context");
468 Signature out{};
469 if (purify_bip340_sign_with_fixed_nonce(secp_context, out.bytes.data(), nullable_data(message), message.size(),
470 signer.seckey.data(), scalar_.data()) == 0) {
471 return unexpected_error(ErrorCode::BackendRejectedInput, "PreparedNonce::sign_message:sign_with_fixed_nonce");
472 }
473 if (out.nonce().xonly != nonce_.xonly) {
474 return unexpected_error(ErrorCode::InternalMismatch, "PreparedNonce::sign_message:nonce_mismatch");
475 }
476 if (purify_bip340_verify(secp_context, out.bytes.data(), nullable_data(message), message.size(),
477 signer.xonly_pubkey.data()) == 0) {
478 return unexpected_error(ErrorCode::InternalMismatch, "PreparedNonce::sign_message:self_verify");
479 }
480 return out;
481}
482
484 std::span<const unsigned char> message,
485 purify_secp_context* secp_context) && {
486 if (!available_) {
488 "PreparedNonce::sign_topic_message:consumed");
489 }
490 available_ = false;
491 WipeOnDestruction burn(scalar_);
492 if (scope_ != Scope::Topic) {
493 return unexpected_error(ErrorCode::BindingMismatch, "PreparedNonce::sign_topic_message:scope");
494 }
495 if (signer_pubkey_ != signer.xonly_pubkey) {
496 return unexpected_error(ErrorCode::BindingMismatch, "PreparedNonce::sign_topic_message:signer_pubkey");
497 }
498
499 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "PreparedNonce::sign_topic_message:secp_context"),
500 "PreparedNonce::sign_topic_message:secp_context");
501 Signature out{};
502 if (purify_bip340_sign_with_fixed_nonce(secp_context, out.bytes.data(), nullable_data(message), message.size(),
503 signer.seckey.data(), scalar_.data()) == 0) {
505 "PreparedNonce::sign_topic_message:sign_with_fixed_nonce");
506 }
507 if (out.nonce().xonly != nonce_.xonly) {
508 return unexpected_error(ErrorCode::InternalMismatch, "PreparedNonce::sign_topic_message:nonce_mismatch");
509 }
510 if (purify_bip340_verify(secp_context, out.bytes.data(), nullable_data(message), message.size(),
511 signer.xonly_pubkey.data()) == 0) {
512 return unexpected_error(ErrorCode::InternalMismatch, "PreparedNonce::sign_topic_message:self_verify");
513 }
514 return out;
515}
516
520
522 if (!prepared_.available()) {
524 "PreparedNonceWithProof::raw_scalar:consumed");
525 }
526 if (proof_.commitment_point[0] != 0x02 &&
527 proof_.commitment_point[0] != 0x03) {
529 "PreparedNonceWithProof::raw_scalar:point");
530 }
531 if (!std::equal(proof_.commitment_point.begin() + 1,
532 proof_.commitment_point.end(),
533 prepared_.public_nonce().xonly.begin())) {
534 return unexpected_error(
536 "PreparedNonceWithProof::raw_scalar:nonce_mismatch");
537 }
538 PURIFY_ASSIGN_OR_RETURN(auto out, prepared_.scalar(),
539 "PreparedNonceWithProof::raw_scalar:scalar");
540 if (proof_.commitment_point[0] == 0x03) {
542 int overflow = 0;
543 purify_scalar_set_b32(&scalar, out.data(), &overflow);
544 if (overflow != 0 || purify_scalar_is_zero(&scalar) != 0) {
546 detail::secure_clear_bytes(out.data(), out.size());
547 return unexpected_error(
549 "PreparedNonceWithProof::raw_scalar:normalized_scalar");
550 }
552 purify_scalar_get_b32(out.data(), &scalar);
554 }
555 return out;
556}
557
559 std::span<const unsigned char> message,
560 purify_secp_context* secp_context) && {
561 NonceProof nonce_proof = std::move(proof_);
562 PURIFY_ASSIGN_OR_RETURN(auto signature,
563 api_impl::sign_message_with_prepared(secret, message, std::move(prepared_), secp_context),
564 "PreparedNonceWithProof::sign_message:sign_message_with_prepared");
565 if (signature.nonce().xonly != nonce_proof.nonce.xonly) {
566 return unexpected_error(ErrorCode::InternalMismatch, "PreparedNonceWithProof::sign_message:nonce_mismatch");
567 }
568 return ProvenSignature{signature, std::move(nonce_proof)};
569}
570
572 std::span<const unsigned char> message,
573 purify_secp_context* secp_context) && {
574 NonceProof nonce_proof = std::move(proof_);
575 PURIFY_ASSIGN_OR_RETURN(auto signature,
576 api_impl::sign_with_prepared_topic(secret, message, std::move(prepared_), secp_context),
577 "PreparedNonceWithProof::sign_topic_message:sign_with_prepared_topic");
578 if (signature.nonce().xonly != nonce_proof.nonce.xonly) {
579 return unexpected_error(ErrorCode::InternalMismatch, "PreparedNonceWithProof::sign_topic_message:nonce_mismatch");
580 }
581 return ProvenSignature{signature, std::move(nonce_proof)};
582}
583
584namespace api_impl {
585
587 PURIFY_ASSIGN_OR_RETURN(const auto& purify_key, derive_key(secret), "derive_public_key:derive_key");
588 PURIFY_ASSIGN_OR_RETURN(const auto& bip340_key, derive_bip340_key(secret, secp_context), "derive_public_key:derive_bip340_key");
589 return PublicKey{purify_key.public_key, bip340_key.xonly_pubkey};
590}
591
592Result<MessageProofCache> build_message_proof_cache(std::span<const unsigned char> message) {
593 return MessageProofCache::build(message);
594}
595
596Result<TopicProofCache> build_topic_proof_cache(std::span<const unsigned char> topic) {
597 return TopicProofCache::build(topic);
598}
599
600Result<PreparedNonce> prepare_message_nonce(const SecretKey& secret, std::span<const unsigned char> message,
601 purify_secp_context* secp_context) {
602 PURIFY_ASSIGN_OR_RETURN(const auto& nonce_data, prepare_nonce_data_impl(secret, Scope::Message, message, secp_context),
603 "prepare_message_nonce:prepare_nonce_data_impl");
604 return PreparedNonce::from_parts(Scope::Message, nonce_data.scalar, nonce_data.nonce,
605 nonce_data.signer_pubkey, nonce_data.binding_digest);
606}
607
609 std::span<const unsigned char> message,
610 purify_secp_context* secp_context,
611 bppp::ExperimentalCircuitBackend* circuit_cache) {
612 PURIFY_ASSIGN_OR_RETURN(const auto& nonce_data, prepare_nonce_data_impl(secret, Scope::Message, message, secp_context),
613 "prepare_message_nonce_with_proof:prepare_nonce_data_impl");
614 PreparedNonce prepared = PreparedNonce::from_parts(Scope::Message, nonce_data.scalar,
615 nonce_data.nonce, nonce_data.signer_pubkey,
616 nonce_data.binding_digest);
617 PURIFY_ASSIGN_OR_RETURN(auto proof, build_nonce_proof(secret, Scope::Message, message, prepared.public_nonce(), secp_context,
618 circuit_cache),
619 "prepare_message_nonce_with_proof:build_nonce_proof");
620 return PreparedNonceWithProof::from_parts(std::move(prepared), std::move(proof));
621}
622
624 const MessageProofCache& cache,
625 purify_secp_context* secp_context,
626 bppp::ExperimentalCircuitBackend* circuit_cache) {
627 PURIFY_RETURN_IF_ERROR(validate_message_proof_cache(cache),
628 "prepare_message_nonce_with_proof:validate_message_proof_cache");
629 PURIFY_ASSIGN_OR_RETURN(const auto& nonce_data, prepare_nonce_data_impl(secret, Scope::Message, cache.message, secp_context),
630 "prepare_message_nonce_with_proof:prepare_nonce_data_impl");
631 PreparedNonce prepared = PreparedNonce::from_parts(Scope::Message, nonce_data.scalar,
632 nonce_data.nonce, nonce_data.signer_pubkey,
633 nonce_data.binding_digest);
635 auto proof,
636 build_nonce_proof_from_template(secret, Scope::Message, prepared.public_nonce(),
637 cache.eval_input, cache.circuit_template, secp_context,
638 circuit_cache != nullptr ? circuit_cache : &cache.backend_cache),
639 "prepare_message_nonce_with_proof:build_nonce_proof_from_template");
640 return PreparedNonceWithProof::from_parts(std::move(prepared), std::move(proof));
641}
642
643Result<PreparedNonce> prepare_topic_nonce(const SecretKey& secret, std::span<const unsigned char> topic,
644 purify_secp_context* secp_context) {
645 PURIFY_ASSIGN_OR_RETURN(const auto& nonce_data, prepare_nonce_data_impl(secret, Scope::Topic, topic, secp_context),
646 "prepare_topic_nonce:prepare_nonce_data_impl");
647 return PreparedNonce::from_parts(Scope::Topic, nonce_data.scalar, nonce_data.nonce,
648 nonce_data.signer_pubkey, nonce_data.binding_digest);
649}
650
652 std::span<const unsigned char> topic,
653 purify_secp_context* secp_context,
654 bppp::ExperimentalCircuitBackend* circuit_cache) {
655 PURIFY_ASSIGN_OR_RETURN(const auto& nonce_data, prepare_nonce_data_impl(secret, Scope::Topic, topic, secp_context),
656 "prepare_topic_nonce_with_proof:prepare_nonce_data_impl");
657 PreparedNonce prepared = PreparedNonce::from_parts(Scope::Topic, nonce_data.scalar,
658 nonce_data.nonce, nonce_data.signer_pubkey,
659 nonce_data.binding_digest);
660 PURIFY_ASSIGN_OR_RETURN(auto proof, build_nonce_proof(secret, Scope::Topic, topic, prepared.public_nonce(), secp_context,
661 circuit_cache),
662 "prepare_topic_nonce_with_proof:build_nonce_proof");
663 return PreparedNonceWithProof::from_parts(std::move(prepared), std::move(proof));
664}
665
667 const TopicProofCache& cache,
668 purify_secp_context* secp_context,
669 bppp::ExperimentalCircuitBackend* circuit_cache) {
670 PURIFY_RETURN_IF_ERROR(validate_topic_proof_cache(cache),
671 "prepare_topic_nonce_with_proof:validate_topic_proof_cache");
672 PURIFY_ASSIGN_OR_RETURN(const auto& nonce_data, prepare_nonce_data_impl(secret, Scope::Topic, cache.topic, secp_context),
673 "prepare_topic_nonce_with_proof:prepare_nonce_data_impl");
674 PreparedNonce prepared = PreparedNonce::from_parts(Scope::Topic, nonce_data.scalar,
675 nonce_data.nonce, nonce_data.signer_pubkey,
676 nonce_data.binding_digest);
678 auto proof,
679 build_nonce_proof_from_template(secret, Scope::Topic, prepared.public_nonce(),
680 cache.eval_input, cache.circuit_template, secp_context,
681 circuit_cache != nullptr ? circuit_cache : &cache.backend_cache),
682 "prepare_topic_nonce_with_proof:build_nonce_proof_from_template");
683 return PreparedNonceWithProof::from_parts(std::move(prepared), std::move(proof));
684}
685
686Result<Signature> sign_message(const SecretKey& secret, std::span<const unsigned char> message,
687 purify_secp_context* secp_context) {
688 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_message_nonce(secret, message, secp_context), "sign_message:prepare_message_nonce");
689 return sign_message_with_prepared(secret, message, std::move(prepared), secp_context);
690}
691
692Result<Signature> sign_message_with_prepared(const SecretKey& secret, std::span<const unsigned char> message,
693 PreparedNonce prepared, purify_secp_context* secp_context) {
694 PURIFY_ASSIGN_OR_RETURN(const auto& signer, derive_bip340_key(secret, secp_context), "sign_message_with_prepared:derive_bip340_key");
695 return std::move(prepared).sign_message(signer, message, secp_context);
696}
697
699 std::span<const unsigned char> message,
700 PreparedNonceWithProof&& prepared,
701 purify_secp_context* secp_context) {
702 return std::move(prepared).sign_message(secret, message, secp_context);
703}
704
705Result<Signature> sign_with_topic(const SecretKey& secret, std::span<const unsigned char> message,
706 std::span<const unsigned char> topic,
707 purify_secp_context* secp_context) {
708 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_topic_nonce(secret, topic, secp_context), "sign_with_topic:prepare_topic_nonce");
709 return sign_with_prepared_topic(secret, message, std::move(prepared), secp_context);
710}
711
712Result<Signature> sign_with_prepared_topic(const SecretKey& secret, std::span<const unsigned char> message,
713 PreparedNonce prepared, purify_secp_context* secp_context) {
714 PURIFY_ASSIGN_OR_RETURN(const auto& signer, derive_bip340_key(secret, secp_context), "sign_with_prepared_topic:derive_bip340_key");
715 return std::move(prepared).sign_topic_message(signer, message, secp_context);
716}
717
719 std::span<const unsigned char> message,
720 PreparedNonceWithProof&& prepared,
721 purify_secp_context* secp_context) {
722 return std::move(prepared).sign_topic_message(secret, message, secp_context);
723}
724
725Result<ProvenSignature> sign_message_with_proof(const SecretKey& secret, std::span<const unsigned char> message,
726 purify_secp_context* secp_context,
727 bppp::ExperimentalCircuitBackend* circuit_cache) {
728 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_message_nonce_with_proof(secret, message, secp_context, circuit_cache),
729 "sign_message_with_proof:prepare_message_nonce_with_proof");
730 return sign_message_with_prepared_proof(secret, message, std::move(prepared), secp_context);
731}
732
734 purify_secp_context* secp_context,
735 bppp::ExperimentalCircuitBackend* circuit_cache) {
736 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_message_nonce_with_proof(secret, cache, secp_context, circuit_cache),
737 "sign_message_with_proof:prepare_message_nonce_with_proof");
738 return sign_message_with_prepared_proof(secret, cache.message, std::move(prepared), secp_context);
739}
740
741Result<ProvenSignature> sign_with_topic_proof(const SecretKey& secret, std::span<const unsigned char> message,
742 std::span<const unsigned char> topic,
743 purify_secp_context* secp_context,
744 bppp::ExperimentalCircuitBackend* circuit_cache) {
745 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_topic_nonce_with_proof(secret, topic, secp_context, circuit_cache),
746 "sign_with_topic_proof:prepare_topic_nonce_with_proof");
747 return sign_with_prepared_topic_proof(secret, message, std::move(prepared), secp_context);
748}
749
750Result<ProvenSignature> sign_with_topic_proof(const SecretKey& secret, std::span<const unsigned char> message,
751 const TopicProofCache& cache,
752 purify_secp_context* secp_context,
753 bppp::ExperimentalCircuitBackend* circuit_cache) {
754 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_topic_nonce_with_proof(secret, cache, secp_context, circuit_cache),
755 "sign_with_topic_proof:prepare_topic_nonce_with_proof");
756 return sign_with_prepared_topic_proof(secret, message, std::move(prepared), secp_context);
757}
758
759Result<bool> verify_signature(const PublicKey& public_key, std::span<const unsigned char> message,
760 const Signature& signature, purify_secp_context* secp_context) {
761 PURIFY_RETURN_IF_ERROR(validate_public_key(public_key.purify_pubkey), "verify_signature:validate_public_key");
762 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "verify_signature:secp_context"),
763 "verify_signature:secp_context");
764 if (purify_bip340_validate_xonly_pubkey(secp_context, public_key.bip340_pubkey.data()) == 0) {
765 return unexpected_error(ErrorCode::BackendRejectedInput, "verify_signature:bip340_validate_xonly_pubkey");
766 }
767 if (purify_bip340_validate_signature(secp_context, signature.bytes.data()) == 0) {
768 return unexpected_error(ErrorCode::BackendRejectedInput, "verify_signature:bip340_validate_signature");
769 }
770 return purify_bip340_verify(secp_context, signature.bytes.data(), nullable_data(message), message.size(),
771 public_key.bip340_pubkey.data()) != 0;
772}
773
774Result<bool> verify_message_nonce_proof(const PublicKey& public_key, std::span<const unsigned char> message,
775 const NonceProof& nonce_proof,
776 purify_secp_context* secp_context,
777 bppp::ExperimentalCircuitBackend* circuit_cache) {
778 PURIFY_ASSIGN_OR_RETURN(const auto& circuit,
779 verifier_circuit(detail::tagged_eval_input(kMessageNonceTag, message), public_key.purify_pubkey),
780 "verify_message_nonce_proof:verifier_circuit");
781 return verify_nonce_proof_with_circuit(public_key, circuit, nonce_proof, Scope::Message, secp_context,
782 "verify_message_nonce_proof:verify_nonce_proof_with_circuit",
783 circuit_cache);
784}
785
787 const NonceProof& nonce_proof,
788 purify_secp_context* secp_context,
789 bppp::ExperimentalCircuitBackend* circuit_cache) {
790 PURIFY_RETURN_IF_ERROR(validate_message_proof_cache(cache), "verify_message_nonce_proof:validate_message_proof_cache");
791 PURIFY_ASSIGN_OR_RETURN(const auto& circuit, cache.circuit_template.instantiate(public_key.purify_pubkey),
792 "verify_message_nonce_proof:instantiate");
793 return verify_nonce_proof_with_circuit(public_key, circuit, nonce_proof, Scope::Message, secp_context,
794 "verify_message_nonce_proof:verify_nonce_proof_with_circuit",
795 circuit_cache != nullptr ? circuit_cache : &cache.backend_cache);
796}
797
798Result<bool> verify_topic_nonce_proof(const PublicKey& public_key, std::span<const unsigned char> topic,
799 const NonceProof& nonce_proof,
800 purify_secp_context* secp_context,
801 bppp::ExperimentalCircuitBackend* circuit_cache) {
802 if (topic.empty()) {
803 return unexpected_error(ErrorCode::EmptyInput, "verify_topic_nonce_proof:empty_topic");
804 }
805 PURIFY_ASSIGN_OR_RETURN(const auto& circuit,
806 verifier_circuit(detail::tagged_eval_input(kTopicNonceTag, topic), public_key.purify_pubkey),
807 "verify_topic_nonce_proof:verifier_circuit");
808 return verify_nonce_proof_with_circuit(public_key, circuit, nonce_proof, Scope::Topic, secp_context,
809 "verify_topic_nonce_proof:verify_nonce_proof_with_circuit",
810 circuit_cache);
811}
812
814 const NonceProof& nonce_proof,
815 purify_secp_context* secp_context,
816 bppp::ExperimentalCircuitBackend* circuit_cache) {
817 PURIFY_RETURN_IF_ERROR(validate_topic_proof_cache(cache), "verify_topic_nonce_proof:validate_topic_proof_cache");
818 PURIFY_ASSIGN_OR_RETURN(const auto& circuit, cache.circuit_template.instantiate(public_key.purify_pubkey),
819 "verify_topic_nonce_proof:instantiate");
820 return verify_nonce_proof_with_circuit(public_key, circuit, nonce_proof, Scope::Topic, secp_context,
821 "verify_topic_nonce_proof:verify_nonce_proof_with_circuit",
822 circuit_cache != nullptr ? circuit_cache : &cache.backend_cache);
823}
824
826 std::span<const unsigned char> message,
827 const ProvenSignature& signature,
828 purify_secp_context* secp_context,
829 bppp::ExperimentalCircuitBackend* circuit_cache) {
830 PURIFY_ASSIGN_OR_RETURN(auto sig_ok, verify_signature(public_key, message, signature.signature, secp_context),
831 "verify_message_signature_with_proof:verify_signature");
832 if (!sig_ok) {
833 return false;
834 }
835 if (signature.signature.nonce().xonly != signature.nonce_proof.nonce.xonly) {
836 return false;
837 }
838 return verify_message_nonce_proof(public_key, message, signature.nonce_proof, secp_context, circuit_cache);
839}
840
842 const ProvenSignature& signature,
843 purify_secp_context* secp_context,
844 bppp::ExperimentalCircuitBackend* circuit_cache) {
845 PURIFY_ASSIGN_OR_RETURN(auto sig_ok, verify_signature(public_key, cache.message, signature.signature, secp_context),
846 "verify_message_signature_with_proof:verify_signature");
847 if (!sig_ok) {
848 return false;
849 }
850 if (signature.signature.nonce().xonly != signature.nonce_proof.nonce.xonly) {
851 return false;
852 }
853 return verify_message_nonce_proof(cache, public_key, signature.nonce_proof, secp_context, circuit_cache);
854}
855
857 std::span<const unsigned char> message,
858 std::span<const unsigned char> topic,
859 const ProvenSignature& signature,
860 purify_secp_context* secp_context,
861 bppp::ExperimentalCircuitBackend* circuit_cache) {
862 PURIFY_ASSIGN_OR_RETURN(auto sig_ok, verify_signature(public_key, message, signature.signature, secp_context),
863 "verify_topic_signature_with_proof:verify_signature");
864 if (!sig_ok) {
865 return false;
866 }
867 if (signature.signature.nonce().xonly != signature.nonce_proof.nonce.xonly) {
868 return false;
869 }
870 return verify_topic_nonce_proof(public_key, topic, signature.nonce_proof, secp_context, circuit_cache);
871}
872
874 std::span<const unsigned char> message,
875 const ProvenSignature& signature,
876 purify_secp_context* secp_context,
877 bppp::ExperimentalCircuitBackend* circuit_cache) {
878 PURIFY_ASSIGN_OR_RETURN(auto sig_ok, verify_signature(public_key, message, signature.signature, secp_context),
879 "verify_topic_signature_with_proof:verify_signature");
880 if (!sig_ok) {
881 return false;
882 }
883 if (signature.signature.nonce().xonly != signature.nonce_proof.nonce.xonly) {
884 return false;
885 }
886 return verify_topic_nonce_proof(cache, public_key, signature.nonce_proof, secp_context, circuit_cache);
887}
888
889} // namespace api_impl
890
892 return api_impl::derive_public_key(secret, secp_context);
893}
894
895Result<bool> PublicKey::verify_signature(std::span<const unsigned char> message, const Signature& signature,
896 purify_secp_context* secp_context) const {
897 return api_impl::verify_signature(*this, message, signature, secp_context);
898}
899
900Result<bool> PublicKey::verify_message_nonce_proof(std::span<const unsigned char> message,
901 const NonceProof& nonce_proof,
902 purify_secp_context* secp_context,
903 bppp::ExperimentalCircuitBackend* circuit_cache) const {
904 return api_impl::verify_message_nonce_proof(*this, message, nonce_proof, secp_context, circuit_cache);
905}
906
908 purify_secp_context* secp_context,
909 bppp::ExperimentalCircuitBackend* circuit_cache) const {
910 return api_impl::verify_message_nonce_proof(cache, *this, nonce_proof, secp_context, circuit_cache);
911}
912
913Result<bool> PublicKey::verify_topic_nonce_proof(std::span<const unsigned char> topic,
914 const NonceProof& nonce_proof,
915 purify_secp_context* secp_context,
916 bppp::ExperimentalCircuitBackend* circuit_cache) const {
917 return api_impl::verify_topic_nonce_proof(*this, topic, nonce_proof, secp_context, circuit_cache);
918}
919
921 purify_secp_context* secp_context,
922 bppp::ExperimentalCircuitBackend* circuit_cache) const {
923 return api_impl::verify_topic_nonce_proof(cache, *this, nonce_proof, secp_context, circuit_cache);
924}
925
927 const ProvenSignature& signature,
928 purify_secp_context* secp_context,
929 bppp::ExperimentalCircuitBackend* circuit_cache) const {
930 return api_impl::verify_message_signature_with_proof(*this, message, signature, secp_context, circuit_cache);
931}
932
934 const ProvenSignature& signature,
935 purify_secp_context* secp_context,
936 bppp::ExperimentalCircuitBackend* circuit_cache) const {
937 return api_impl::verify_message_signature_with_proof(cache, *this, signature, secp_context, circuit_cache);
938}
939
940Result<bool> PublicKey::verify_topic_signature_with_proof(std::span<const unsigned char> message,
941 std::span<const unsigned char> topic,
942 const ProvenSignature& signature,
943 purify_secp_context* secp_context,
944 bppp::ExperimentalCircuitBackend* circuit_cache) const {
945 return api_impl::verify_topic_signature_with_proof(*this, message, topic, signature, secp_context, circuit_cache);
946}
947
949 std::span<const unsigned char> message,
950 const ProvenSignature& signature,
951 purify_secp_context* secp_context,
952 bppp::ExperimentalCircuitBackend* circuit_cache) const {
953 return api_impl::verify_topic_signature_with_proof(cache, *this, message, signature, secp_context, circuit_cache);
954}
955
957 PURIFY_ASSIGN_OR_RETURN(auto owned_secret, secret.clone(), "KeyPair::from_secret:clone");
958 return KeyPair::from_secret(std::move(owned_secret), secp_context);
959}
960
962 PURIFY_ASSIGN_OR_RETURN(auto public_key, PublicKey::from_secret(secret, secp_context), "KeyPair::from_secret:from_secret");
963 return KeyPair(std::move(secret), std::move(public_key));
964}
965
966Result<PreparedNonce> KeyPair::prepare_message_nonce(std::span<const unsigned char> message,
967 purify_secp_context* secp_context) const {
968 return api_impl::prepare_message_nonce(secret_, message, secp_context);
969}
970
972 std::span<const unsigned char> message,
973 purify_secp_context* secp_context,
974 bppp::ExperimentalCircuitBackend* circuit_cache) const {
975 return api_impl::prepare_message_nonce_with_proof(secret_, message, secp_context, circuit_cache);
976}
977
979 const MessageProofCache& cache,
980 purify_secp_context* secp_context,
981 bppp::ExperimentalCircuitBackend* circuit_cache) const {
982 return api_impl::prepare_message_nonce_with_proof(secret_, cache, secp_context, circuit_cache);
983}
984
985Result<PreparedNonce> KeyPair::prepare_topic_nonce(std::span<const unsigned char> topic,
986 purify_secp_context* secp_context) const {
987 return api_impl::prepare_topic_nonce(secret_, topic, secp_context);
988}
989
991 std::span<const unsigned char> topic,
992 purify_secp_context* secp_context,
993 bppp::ExperimentalCircuitBackend* circuit_cache) const {
994 return api_impl::prepare_topic_nonce_with_proof(secret_, topic, secp_context, circuit_cache);
995}
996
998 const TopicProofCache& cache,
999 purify_secp_context* secp_context,
1000 bppp::ExperimentalCircuitBackend* circuit_cache) const {
1001 return api_impl::prepare_topic_nonce_with_proof(secret_, cache, secp_context, circuit_cache);
1002}
1003
1004Result<Signature> KeyPair::sign_message(std::span<const unsigned char> message,
1005 purify_secp_context* secp_context) const {
1006 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_message_nonce(message, secp_context), "KeyPair::sign_message:prepare_message_nonce");
1007 return sign_message_with_prepared(message, std::move(prepared), secp_context);
1008}
1009
1010Result<Signature> KeyPair::sign_message_with_prepared(std::span<const unsigned char> message,
1011 PreparedNonce&& prepared,
1012 purify_secp_context* secp_context) const {
1014 secret_, message, std::move(prepared), secp_context);
1015}
1016
1018 PreparedNonceWithProof&& prepared,
1019 purify_secp_context* secp_context) const {
1020 return std::move(prepared).sign_message(secret_, message, secp_context);
1021}
1022
1023Result<Signature> KeyPair::sign_with_topic(std::span<const unsigned char> message,
1024 std::span<const unsigned char> topic,
1025 purify_secp_context* secp_context) const {
1026 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_topic_nonce(topic, secp_context), "KeyPair::sign_with_topic:prepare_topic_nonce");
1027 return sign_with_prepared_topic(message, std::move(prepared), secp_context);
1028}
1029
1030Result<Signature> KeyPair::sign_with_prepared_topic(std::span<const unsigned char> message,
1031 PreparedNonce&& prepared,
1032 purify_secp_context* secp_context) const {
1034 secret_, message, std::move(prepared), secp_context);
1035}
1036
1038 PreparedNonceWithProof&& prepared,
1039 purify_secp_context* secp_context) const {
1040 return std::move(prepared).sign_topic_message(secret_, message, secp_context);
1041}
1042
1043Result<ProvenSignature> KeyPair::sign_message_with_proof(std::span<const unsigned char> message,
1044 purify_secp_context* secp_context,
1045 bppp::ExperimentalCircuitBackend* circuit_cache) const {
1046 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_message_nonce_with_proof(message, secp_context, circuit_cache),
1047 "KeyPair::sign_message_with_proof:prepare_message_nonce_with_proof");
1048 return sign_message_with_prepared_proof(message, std::move(prepared), secp_context);
1049}
1050
1052 purify_secp_context* secp_context,
1053 bppp::ExperimentalCircuitBackend* circuit_cache) const {
1054 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_message_nonce_with_proof(cache, secp_context, circuit_cache),
1055 "KeyPair::sign_message_with_proof:prepare_message_nonce_with_proof");
1056 return sign_message_with_prepared_proof(cache.message, std::move(prepared), secp_context);
1057}
1058
1059Result<ProvenSignature> KeyPair::sign_with_topic_proof(std::span<const unsigned char> message,
1060 std::span<const unsigned char> topic,
1061 purify_secp_context* secp_context,
1062 bppp::ExperimentalCircuitBackend* circuit_cache) const {
1063 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_topic_nonce_with_proof(topic, secp_context, circuit_cache),
1064 "KeyPair::sign_with_topic_proof:prepare_topic_nonce_with_proof");
1065 return sign_with_prepared_topic_proof(message, std::move(prepared), secp_context);
1066}
1067
1068Result<ProvenSignature> KeyPair::sign_with_topic_proof(std::span<const unsigned char> message,
1069 const TopicProofCache& cache,
1070 purify_secp_context* secp_context,
1071 bppp::ExperimentalCircuitBackend* circuit_cache) const {
1072 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_topic_nonce_with_proof(cache, secp_context, circuit_cache),
1073 "KeyPair::sign_with_topic_proof:prepare_topic_nonce_with_proof");
1074 return sign_with_prepared_topic_proof(message, std::move(prepared), secp_context);
1075}
1076
1078 std::span<const unsigned char> topic,
1079 const NonceProof& nonce_proof,
1080 purify_secp_context* secp_context,
1081 bppp::ExperimentalCircuitBackend* circuit_cache) const {
1082 if (topic.empty()) {
1083 return unexpected_error(
1084 ErrorCode::EmptyInput, "NonceHost::verify_topic:empty_topic");
1085 }
1087 const auto circuit,
1089 detail::tagged_eval_input(kTopicNonceTag, topic), public_key_),
1090 "NonceHost::verify_topic:circuit");
1091 return verify_nonce_proof_with_circuit_impl(
1092 public_key_, circuit, nonce_proof, Scope::Topic, secp_context,
1093 "NonceHost::verify_topic:proof", circuit_cache);
1094}
1095
1097 const TopicProofCache& cache,
1098 const NonceProof& nonce_proof,
1099 purify_secp_context* secp_context,
1100 bppp::ExperimentalCircuitBackend* circuit_cache) const {
1102 validate_topic_proof_cache(cache),
1103 "NonceHost::verify_topic:cache");
1105 const auto circuit,
1106 cache.circuit_template.instantiate(public_key_),
1107 "NonceHost::verify_topic:circuit");
1108 return verify_nonce_proof_with_circuit_impl(
1109 public_key_, circuit, nonce_proof, Scope::Topic, secp_context,
1110 "NonceHost::verify_topic:proof",
1111 circuit_cache != nullptr ? circuit_cache : &cache.backend_cache);
1112}
1113
1116 purify_secp_context* secp_context) {
1117 static_assert(NonceProof::kNormProofSize == 65 * 12 + 64);
1119 auto basis,
1122 "Purify/Accountable/CertificateNonceLeafAccumulation/v1",
1123 secp_context),
1124 "certificate_nonce_leaf_accumulation:basis");
1125 return CertificateNonceLeafAccumulator(std::move(basis));
1126}
1127
1130 const PublicKey& public_key,
1131 const TopicProofCache& cache,
1132 const NonceProof& proof,
1133 const bppp::ScalarBytes& circuit_precommitment,
1134 purify_secp_context* secp_context) const {
1136 const auto matches, nonce_proof_matches_nonce(proof, secp_context),
1137 "certificate_nonce_leaf_parent:nonce");
1138 if (!matches) {
1139 return unexpected_error(
1141 "certificate_nonce_leaf_parent:nonce");
1142 }
1144 public_key, cache, circuit_precommitment,
1145 proof.commitment_point, secp_context);
1146}
1147
1150 const PublicKey& public_key,
1151 const TopicProofCache& cache,
1152 const bppp::ScalarBytes& circuit_precommitment,
1153 const bppp::PointBytes& nonce_point,
1154 purify_secp_context* secp_context) const {
1156 validate_public_key_bundle(public_key, secp_context),
1157 "certificate_nonce_leaf_parent:public_key");
1159 validate_topic_proof_cache(cache),
1160 "certificate_nonce_leaf_parent:cache");
1162 const auto circuit,
1163 cache.circuit_template.instantiate(public_key.purify_pubkey),
1164 "certificate_nonce_leaf_parent:circuit");
1167 circuit, "certificate_nonce_leaf_parent:circuit_shape"),
1168 "certificate_nonce_leaf_parent:circuit_shape");
1170 const auto precommitment,
1172 circuit_precommitment),
1173 "certificate_nonce_leaf_parent:precommitment");
1174 const Bytes statement_binding = proof_statement_binding(Scope::Topic);
1175 const std::array<bppp::PointBytes, 1> public_commitments{
1176 nonce_point};
1178 auto constants,
1180 derive_circuit_norm_parent_constants_v4_with_public_commitments(
1181 circuit, precommitment, public_commitments, secp_context,
1182 statement_binding),
1183 "certificate_nonce_leaf_parent:derive");
1185 std::move(constants.circuit_binding_digest),
1186 std::move(constants.raw_generator_digest),
1187 std::move(constants.coefficient_commitment),
1188 std::move(constants.anchored_offset),
1189 };
1190}
1191
1194 const PublicKey& public_key,
1195 const TopicProofCache& cache,
1196 const NonceProof& proof,
1197 purify_secp_context* secp_context,
1198 bppp::ExperimentalCircuitBackend* circuit_cache) const {
1200 validate_public_key_bundle(public_key, secp_context),
1201 "certificate_nonce_leaf:public_key");
1203 validate_topic_proof_cache(cache),
1204 "certificate_nonce_leaf:cache");
1206 const auto matches, nonce_proof_matches_nonce(proof, secp_context),
1207 "certificate_nonce_leaf:nonce");
1208 if (!matches) {
1209 return unexpected_error(
1210 ErrorCode::BindingMismatch, "certificate_nonce_leaf:nonce");
1211 }
1213 const auto circuit,
1214 cache.circuit_template.instantiate(public_key.purify_pubkey),
1215 "certificate_nonce_leaf:circuit");
1218 circuit, "certificate_nonce_leaf:circuit_shape"),
1219 "certificate_nonce_leaf:circuit_shape");
1220 const Bytes statement_binding = proof_statement_binding(Scope::Topic);
1221 const std::array<bppp::PointBytes, 1> public_commitments{
1222 proof.commitment_point};
1224 proof.raw_commitment,
1225 proof.mask_commitment,
1226 proof.t2,
1229 proof.norm_proof,
1230 };
1232 auto publication,
1234 reconstruct_circuit_zk_norm_arg_publication_v4_with_public_commitments(
1235 circuit, std::move(circuit_proof), public_commitments,
1236 secp_context, statement_binding),
1237 "certificate_nonce_leaf:publication");
1238 const auto circuit_precommitment = publication.precommitment.bytes();
1240 auto prepared,
1242 prepare_circuit_zk_norm_arg_v4_with_public_commitments(
1243 circuit, publication, public_commitments, secp_context,
1244 statement_binding,
1245 circuit_cache != nullptr ? circuit_cache
1246 : &cache.backend_cache),
1247 "certificate_nonce_leaf:outer");
1248 if (!prepared.has_value()) {
1249 return unexpected_error(
1250 ErrorCode::EquationMismatch, "certificate_nonce_leaf:outer");
1251 }
1252 const auto& transcript_input = prepared->transcript_input();
1253 CertificateNonceLeafParentConstants parent_constants{
1254 prepared->circuit_binding_digest(),
1255 transcript_input.raw_generator_digest,
1256 transcript_input.coefficient_commitment,
1257 transcript_input.anchored_offset};
1259 auto norm,
1261 prepare_v4_circuit_norm_for_algebraic_reduction(
1262 basis_, std::move(*prepared), secp_context),
1263 "certificate_nonce_leaf:norm");
1265 const auto proof_bytes, proof.serialize(secp_context),
1266 "certificate_nonce_leaf:proof_bytes");
1267 const auto proof_digest =
1268 kCertificateNonceProofHash.digest(proof_bytes);
1269 const auto nonce_host = public_key.purify_pubkey.to_bytes_be();
1270 const auto prepared_binding = norm.prepared_binding().to_bytes_be();
1271 const auto binding_digest = kCertificateNonceLeafHash.digest_many<6>({
1272 nonce_host, cache.topic, proof_digest, circuit_precommitment,
1273 proof.commitment_point, prepared_binding,
1274 });
1276 circuit_precommitment, proof.commitment_point, nonce_host,
1277 cache.topic, proof_digest, binding_digest,
1278 std::move(parent_constants), std::move(norm));
1279}
1280
1281} // namespace purify::puresign_plusplus
Result< NativeBulletproofCircuit > instantiate(const UInt512 &pubkey) const
Move-only packed Purify secret stored in dedicated heap memory.
Definition secret.hpp:95
Result< SecretKey > clone() const
Creates a second owned copy of this secret key.
Definition secret.hpp:128
Reusable BIP340-style tagged SHA-256 helper.
Definition curve.hpp:111
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)
Result< CertificateNonceLeafParentConstants > derive_parent_constants_from_public_data(const PublicKey &public_key, const TopicProofCache &cache, const bppp::ScalarBytes &circuit_precommitment, const bppp::PointBytes &nonce_point, purify_secp_context *secp_context) const
Definition bppp.cpp:1149
Result< CertificateNonceLeafParentConstants > derive_parent_constants(const PublicKey &public_key, const TopicProofCache &cache, const NonceProof &proof, const bppp::ScalarBytes &circuit_precommitment, purify_secp_context *secp_context) const
Definition bppp.cpp:1129
static Result< CertificateNonceLeafAccumulator > create(purify_secp_context *secp_context)
Definition bppp.cpp:1115
Result< PreparedCertificateNonceLeaf > prepare(const PublicKey &public_key, const TopicProofCache &cache, const NonceProof &proof, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache=nullptr) const
Definition bppp.cpp:1193
CertificateNonceLeafAccumulator(const CertificateNonceLeafAccumulator &)=delete
Result< PreparedNonce > prepare_message_nonce(std::span< const unsigned char > message, purify_secp_context *secp_context) const
Deterministically prepares a message-bound nonce.
Definition bppp.cpp:966
Result< PreparedNonce > prepare_topic_nonce(std::span< const unsigned char > topic, purify_secp_context *secp_context) const
Deterministically prepares a topic-bound nonce.
Definition bppp.cpp:985
Result< ProvenSignature > sign_message_with_proof(std::span< const unsigned char > message, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache=nullptr) const
Signs a message and returns the signature bundled with its BPPP nonce proof.
Definition bppp.cpp:1043
const PublicKey & public_key() const noexcept
Returns the public key bundle associated with this signer.
Definition bppp.hpp:533
Result< Signature > sign_with_prepared_topic(std::span< const unsigned char > message, PreparedNonce &&prepared, purify_secp_context *secp_context) const
Signs a message using an already prepared topic-bound nonce.
Definition bppp.cpp:1030
static Result< KeyPair > from_secret(const SecretKey &secret, purify_secp_context *secp_context)
Derives a PureSign++ signing key pair from one packed Purify secret.
Definition bppp.cpp:956
Result< PreparedNonceWithProof > prepare_message_nonce_with_proof(std::span< const unsigned char > message, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache=nullptr) const
Deterministically prepares a message-bound nonce together with its BPPP proof.
Definition bppp.cpp:971
Result< ProvenSignature > sign_with_prepared_topic_proof(std::span< const unsigned char > message, PreparedNonceWithProof &&prepared, purify_secp_context *secp_context) const
Signs a message using an already prepared topic-bound nonce proof bundle.
Definition bppp.cpp:1037
Result< Signature > sign_message_with_prepared(std::span< const unsigned char > message, PreparedNonce &&prepared, purify_secp_context *secp_context) const
Signs a message using an already prepared message-bound nonce.
Definition bppp.cpp:1010
Result< ProvenSignature > sign_with_topic_proof(std::span< const unsigned char > message, std::span< const unsigned char > topic, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache=nullptr) const
Signs a message with a topic-bound nonce proof.
Definition bppp.cpp:1059
Result< Signature > sign_message(std::span< const unsigned char > message, purify_secp_context *secp_context) const
Signs a message with a deterministically derived message-bound nonce.
Definition bppp.cpp:1004
Result< PreparedNonceWithProof > prepare_topic_nonce_with_proof(std::span< const unsigned char > topic, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache=nullptr) const
Deterministically prepares a topic-bound nonce together with its BPPP proof.
Definition bppp.cpp:990
KeyPair(const KeyPair &)=delete
Result< Signature > sign_with_topic(std::span< const unsigned char > message, std::span< const unsigned char > topic, purify_secp_context *secp_context) const
Signs a message using a topic-bound deterministic nonce.
Definition bppp.cpp:1023
Result< ProvenSignature > sign_message_with_prepared_proof(std::span< const unsigned char > message, PreparedNonceWithProof &&prepared, purify_secp_context *secp_context) const
Signs a message using an already prepared message-bound nonce proof bundle.
Definition bppp.cpp:1017
Result< bool > verify_topic_nonce_proof(std::span< const unsigned char > topic, const NonceProof &nonce_proof, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache=nullptr) const
Definition bppp.cpp:1077
Result< Scalar32 > scalar() const
Explicitly exports the secret nonce scalar from the wrapped prepared nonce.
Definition bppp.hpp:455
PreparedNonceWithProof(const PreparedNonceWithProof &)=delete
const NonceProof & proof() const noexcept
Returns the public nonce proof carried by this bundle.
Definition bppp.hpp:447
Result< ProvenSignature > sign_message(const SecretKey &secret, std::span< const unsigned char > message, purify_secp_context *secp_context) &&
Consumes this message-bound prepared proof bundle and signs the message.
Definition bppp.cpp:558
static PreparedNonceWithProof from_parts(PreparedNonce prepared, NonceProof proof)
Bundles a prepared nonce with its matching BPPP-backed public nonce proof.
Definition bppp.cpp:517
Result< Scalar32 > raw_scalar() const
Explicitly exports the scalar for the proof's full raw point.
Definition bppp.cpp:521
Result< ProvenSignature > sign_topic_message(const SecretKey &secret, std::span< const unsigned char > message, purify_secp_context *secp_context) &&
Consumes this topic-bound prepared proof bundle and signs the message.
Definition bppp.cpp:571
Move-only prepared nonce bound to either a message or a topic.
Definition bppp.hpp:333
Result< Signature > sign_message(const Bip340Key &signer, std::span< const unsigned char > message, purify_secp_context *secp_context) &&
Consumes this message-bound nonce and signs the matching message.
Definition bppp.cpp:447
Result< Scalar32 > scalar() const
Explicitly exports the secret nonce scalar.
Definition bppp.hpp:367
const Nonce & public_nonce() const noexcept
Returns the public nonce corresponding to this prepared secret nonce scalar.
Definition bppp.hpp:351
PreparedNonce & operator=(const PreparedNonce &)=delete
PreparedNonce(const PreparedNonce &)=delete
Result< Signature > sign_topic_message(const Bip340Key &signer, std::span< const unsigned char > message, purify_secp_context *secp_context) &&
Consumes this topic-bound nonce and signs a message under that topic binding.
Definition bppp.cpp:483
static PreparedNonce from_parts(Scope scope, const Scalar32 &scalar, const Nonce &nonce, const XOnly32 &signer_pubkey, const XOnly32 &binding_digest)
Builds a prepared nonce from already-derived nonce components.
Definition bppp.cpp:412
#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< CircuitZkNormArgPublicationV4 > prove_circuit_zk_norm_arg_v4_with_public_commitments(const NativeBulletproofCircuit &circuit, const BulletproofAssignmentData &assignment, const ScalarBytes &secret_prover_seed, std::span< const PointBytes > public_commitments, purify_secp_context *secp_context, std::span< const unsigned char > statement_binding, ExperimentalCircuitBackend *cache=nullptr)
Definition bppp.cpp:3638
Result< CircuitZkNormArgPublicationV4 > reconstruct_circuit_zk_norm_arg_publication_v4_with_public_commitments(const NativeBulletproofCircuit &circuit, CircuitZkNormArgProofV4 proof, std::span< const PointBytes > public_commitments, purify_secp_context *secp_context, std::span< const unsigned char > statement_binding)
Definition bppp.cpp:3362
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
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
ScalarBytes scalar_bytes(const FieldElement &value)
Serializes a Purify field element into the scalar encoding expected by the BPPP bridge.
Definition bppp.hpp:78
std::array< unsigned char, 32 > ScalarBytes
Big-endian 32-byte scalar encoding.
Definition bppp.hpp:30
const UInt256 & secp256k1_order()
Definition c_api.cpp:90
Bytes tagged_eval_input(std::string_view tag, std::span< const unsigned char > input)
Definition common.hpp:21
Status validate_topic_proof_cache(const CacheLike &cache, std::string_view nonce_tag)
Definition common.hpp:92
void secure_clear_bytes(void *data, std::size_t size) noexcept
Definition secret.hpp:22
Status validate_message_proof_cache(const CacheLike &cache, std::string_view nonce_tag)
Definition common.hpp:78
Bytes copy_bytes(std::span< const unsigned char > input)
Definition common.hpp:17
Status validate_proof_cache_circuit(const CircuitLike &circuit, const char *context)
Definition common.hpp:64
Result< PublicKey > derive_public_key(const SecretKey &secret, purify_secp_context *secp_context)
Definition bppp.cpp:586
Result< PreparedNonce > prepare_message_nonce(const SecretKey &secret, std::span< const unsigned char > message, purify_secp_context *secp_context)
Definition bppp.cpp:600
Result< bool > verify_topic_nonce_proof(const PublicKey &public_key, std::span< const unsigned char > topic, const NonceProof &nonce_proof, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache)
Definition bppp.cpp:798
Result< PreparedNonce > prepare_topic_nonce(const SecretKey &secret, std::span< const unsigned char > topic, purify_secp_context *secp_context)
Definition bppp.cpp:643
Result< PreparedNonceWithProof > prepare_message_nonce_with_proof(const SecretKey &secret, std::span< const unsigned char > message, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache)
Definition bppp.cpp:608
Result< ProvenSignature > sign_message_with_proof(const SecretKey &secret, std::span< const unsigned char > message, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache)
Definition bppp.cpp:725
Result< bool > verify_message_nonce_proof(const PublicKey &public_key, std::span< const unsigned char > message, const NonceProof &nonce_proof, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache)
Definition bppp.cpp:774
Result< ProvenSignature > sign_message_with_prepared_proof(const SecretKey &secret, std::span< const unsigned char > message, PreparedNonceWithProof &&prepared, purify_secp_context *secp_context)
Definition bppp.cpp:698
Result< ProvenSignature > sign_with_topic_proof(const SecretKey &secret, std::span< const unsigned char > message, std::span< const unsigned char > topic, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache)
Definition bppp.cpp:741
Result< MessageProofCache > build_message_proof_cache(std::span< const unsigned char > message)
Definition bppp.cpp:592
Result< Signature > sign_message_with_prepared(const SecretKey &secret, std::span< const unsigned char > message, PreparedNonce prepared, purify_secp_context *secp_context)
Definition bppp.cpp:692
Result< bool > verify_topic_signature_with_proof(const PublicKey &public_key, std::span< const unsigned char > message, std::span< const unsigned char > topic, const ProvenSignature &signature, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache)
Definition bppp.cpp:856
Result< PreparedNonceWithProof > prepare_topic_nonce_with_proof(const SecretKey &secret, std::span< const unsigned char > topic, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache)
Definition bppp.cpp:651
Result< Signature > sign_with_prepared_topic(const SecretKey &secret, std::span< const unsigned char > message, PreparedNonce prepared, purify_secp_context *secp_context)
Definition bppp.cpp:712
Result< Signature > sign_with_topic(const SecretKey &secret, std::span< const unsigned char > message, std::span< const unsigned char > topic, purify_secp_context *secp_context)
Definition bppp.cpp:705
Result< ProvenSignature > sign_with_prepared_topic_proof(const SecretKey &secret, std::span< const unsigned char > message, PreparedNonceWithProof &&prepared, purify_secp_context *secp_context)
Definition bppp.cpp:718
Result< bool > verify_signature(const PublicKey &public_key, std::span< const unsigned char > message, const Signature &signature, purify_secp_context *secp_context)
Definition bppp.cpp:759
Result< Signature > sign_message(const SecretKey &secret, std::span< const unsigned char > message, purify_secp_context *secp_context)
Definition bppp.cpp:686
Result< TopicProofCache > build_topic_proof_cache(std::span< const unsigned char > topic)
Definition bppp.cpp:596
Result< bool > verify_message_signature_with_proof(const PublicKey &public_key, std::span< const unsigned char > message, const ProvenSignature &signature, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache)
Definition bppp.cpp:825
std::array< unsigned char, 32 > XOnly32
Definition bppp.hpp:24
std::array< unsigned char, 32 > Scalar32
Definition bppp.hpp:23
Status require_secp_context(const purify_secp_context *context, const char *error_context)
Definition common.hpp:56
constexpr Unexpected< Error > unexpected_error(ErrorCode code, const char *context=nullptr)
Constructs an unexpected Error value from a machine-readable code.
Definition error.hpp:293
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
Result< NativeBulletproofCircuitTemplate > verifier_circuit_template(const Bytes &message)
Builds a reusable public-key-agnostic verifier-circuit template for a message.
Definition api.cpp:217
Result< BulletproofWitnessData > prove_assignment_data(const Bytes &message, const SecretKey &secret)
Computes the native Purify witness for a message and secret.
Definition api.cpp:243
Result< NativeBulletproofCircuit > verifier_circuit(const Bytes &message, const UInt512 &pubkey)
Builds the native verifier circuit for a message and public key.
Definition api.cpp:211
Bytes bytes_from_ascii(std::string_view input)
Encodes an ASCII string as a byte vector.
Definition curve.cpp:168
BigUInt< 4 > UInt256
256-bit unsigned integer used for field elements and curve orders.
Definition numeric.hpp:817
Status validate_public_key(const UInt512 &packed)
Validates the packed public-key encoding range.
Definition curve.cpp:319
Result< Bip340Key > derive_bip340_key(const SecretKey &secret, purify_secp_context *secp_context)
Derives a canonical BIP340 signing keypair from an owned Purify secret.
Definition api.cpp:166
Result< GeneratedKey > derive_key(const SecretKey &secret)
Derives the packed public key corresponding to a packed secret.
Definition api.cpp:147
Expected< T, Error > Result
Expected-returning convenience alias for Purify value-producing APIs.
Definition error.hpp:99
Result< FieldElement > eval(const SecretKey &secret, const Bytes &message)
Evaluates the Purify PRF for an owned secret key and message.
Definition api.cpp:182
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
Experimental BPPP-backed PureSign proof(R) helpers.
Narrow C ABI exposing secp256k1 scalar and HMAC helpers to the C++ headers.
int purify_scalar_is_zero(const purify_scalar *value)
Returns nonzero when the scalar is zero.
int purify_bip340_xonly_from_point(purify_secp_context *context, const unsigned char point33[33], unsigned char xonly32[32], int *parity_out)
Converts a compressed secp256k1 point into its x-only public key encoding.
int purify_bip340_nonce_from_scalar(purify_secp_context *context, unsigned char scalar32[32], unsigned char xonly_nonce32[32])
Canonicalizes a valid secp256k1 nonce scalar for BIP340 and derives its x-only public nonce.
int purify_bip340_sign_with_fixed_nonce(purify_secp_context *context, unsigned char sig64[64], const unsigned char *msg, size_t msglen, const unsigned char seckey32[32], const unsigned char nonce32[32])
Signs a message with a caller-supplied BIP340 nonce scalar.
int purify_bip340_validate_signature(purify_secp_context *context, const unsigned char sig64[64])
Returns nonzero when the 64-byte BIP340 signature has a syntactically valid encoding.
int purify_bip340_validate_xonly_pubkey(purify_secp_context *context, const unsigned char xonly_pubkey32[32])
Returns nonzero when the x-only public key encoding parses successfully.
void purify_scalar_negate(purify_scalar *out, const purify_scalar *value)
Computes the additive inverse of a scalar.
void purify_scalar_get_b32(unsigned char output32[32], const purify_scalar *value)
Serializes a scalar as 32 big-endian bytes.
int purify_bip340_verify(purify_secp_context *context, const unsigned char sig64[64], const unsigned char *msg, size_t msglen, const unsigned char xonly_pubkey32[32])
Verifies a BIP340 signature against a serialized x-only public key.
void purify_scalar_set_b32(purify_scalar *out, const unsigned char input32[32], int *overflow)
Parses a big-endian 32-byte scalar.
struct purify_secp_context purify_secp_context
Caller-owned reusable secp256k1 context handle shared across Purify C and C++ APIs.
static BigUInt from_hex(std::string_view hex)
Definition numeric.hpp:283
std::array< unsigned char, Words *8 > to_bytes_be() const
Serializes the value to a fixed-width big-endian byte array.
Definition numeric.hpp:640
Canonical BIP340 keypair derived deterministically from a packed Purify secret.
Definition api.hpp:38
static const purify_scalar & raw(const FieldElement &value) noexcept
Definition numeric.hpp:940
Cacheable message-bound nonce-proof template for the BPPP-backed PureSign++ proof(R) flow.
Definition bppp.hpp:256
bppp::ExperimentalCircuitCache backend_cache
Definition bppp.hpp:261
NativeBulletproofCircuitTemplate circuit_template
Definition bppp.hpp:259
static Result< MessageProofCache > build(std::span< const unsigned char > message)
Builds a reusable verifier template for one exact message.
Definition bppp.cpp:375
Result< Bytes > serialize(purify_secp_context *secp_context) const
Definition bppp.cpp:148
bppp::ScalarBytes public_commitment_dlog_response
Definition bppp.hpp:306
static constexpr std::size_t kNormProofSize
Definition bppp.hpp:297
Public BIP340 nonce point in x-only form.
Definition bppp.hpp:220
static Result< PublicKey > from_secret(const SecretKey &secret, purify_secp_context *secp_context)
Derives both public identities from one packed Purify secret.
Definition bppp.cpp:891
Result< bool > verify_message_nonce_proof(std::span< const unsigned char > message, const NonceProof &nonce_proof, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache=nullptr) const
Verifies a message-bound BPPP nonce proof against this public key.
Definition bppp.cpp:900
Result< bool > verify_topic_nonce_proof(std::span< const unsigned char > topic, const NonceProof &nonce_proof, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache=nullptr) const
Verifies a topic-bound BPPP nonce proof against this public key.
Definition bppp.cpp:913
Result< bool > verify_signature(std::span< const unsigned char > message, const Signature &signature, purify_secp_context *secp_context) const
Verifies a plain BIP340 signature against this bundle's x-only public key.
Definition bppp.cpp:895
Result< bool > verify_message_signature_with_proof(std::span< const unsigned char > message, const ProvenSignature &signature, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache=nullptr) const
Verifies a message signature bundled with its BPPP nonce proof.
Definition bppp.cpp:926
Result< bool > verify_topic_signature_with_proof(std::span< const unsigned char > message, std::span< const unsigned char > topic, const ProvenSignature &signature, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache=nullptr) const
Verifies a topic-bound signature bundled with its BPPP nonce proof.
Definition bppp.cpp:940
Standard 64-byte BIP340 signature.
Definition bppp.hpp:233
Nonce nonce() const
Returns the x-only public nonce encoded in the first 32 signature bytes.
Definition bppp.cpp:117
Cacheable topic-bound nonce-proof template for the BPPP-backed PureSign++ proof(R) flow.
Definition bppp.hpp:278
bppp::ExperimentalCircuitCache backend_cache
Definition bppp.hpp:283
NativeBulletproofCircuitTemplate circuit_template
Definition bppp.hpp:281
static Result< TopicProofCache > build(std::span< const unsigned char > topic)
Builds a reusable verifier template for one exact topic.
Definition bppp.cpp:389
Opaque scalar storage compatible with secp256k1-zkp internal scalar storage.
Definition secp_bridge.h:22