purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
legacy.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
11
12#include <algorithm>
13#include <cstring>
14
16#include "detail/common.hpp"
17#include "purify/bppp.hpp"
18#include "purify/curve.hpp"
19#include "secure_cleanup.hpp"
20
21namespace purify::puresign {
22
23namespace api_impl {
24
25Result<Signature> sign_message_with_prepared(const SecretKey& secret, std::span<const unsigned char> message,
26 PreparedNonce prepared, purify_secp_context* secp_context);
27Result<Signature> sign_with_prepared_topic(const SecretKey& secret, std::span<const unsigned char> message,
28 PreparedNonce prepared, purify_secp_context* secp_context);
29
30} // namespace api_impl
31
32namespace {
33
35
36constexpr std::string_view kMessageNonceTag = "PureSign/Nonce/Message/";
37constexpr std::string_view kTopicNonceTag = "PureSign/Nonce/Topic/";
38constexpr std::string_view kMessageBindingTag = "PureSign/Binding/Message";
39constexpr std::string_view kTopicBindingTag = "PureSign/Binding/Topic";
40constexpr std::string_view kMessageProofTag = "PureSign/Proof/Message/V1";
41constexpr std::string_view kTopicProofTag = "PureSign/Proof/Topic/V1";
42const TaggedHash kMessageBindingTaggedHash(kMessageBindingTag);
43const TaggedHash kTopicBindingTaggedHash(kTopicBindingTag);
44const TaggedHash kMessageProofTaggedHash(kMessageProofTag);
45const TaggedHash kTopicProofTaggedHash(kTopicProofTag);
46
47const TaggedHash& binding_tagged_hash(PreparedNonce::Scope scope) {
48 return scope == PreparedNonce::Scope::Message ? kMessageBindingTaggedHash : kTopicBindingTaggedHash;
49}
50
51const TaggedHash& proof_tagged_hash(PreparedNonce::Scope scope) {
52 return scope == PreparedNonce::Scope::Message ? kMessageProofTaggedHash : kTopicProofTaggedHash;
53}
54
55const UInt256& secp256k1_order() {
56 static const UInt256 value =
57 UInt256::from_hex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141");
58 return value;
59}
60
61Status validate_puresign_field_alignment() {
62 if (prime_p() != secp256k1_order()) {
63 return unexpected_error(ErrorCode::InternalMismatch, "puresign:field_order_mismatch");
64 }
65 return {};
66}
67
68Status validate_public_key_bundle(const PublicKey& public_key, purify_secp_context* secp_context) {
69 PURIFY_RETURN_IF_ERROR(validate_public_key(public_key.purify_pubkey),
70 "puresign:validate_public_key_bundle:purify_pubkey");
71 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "puresign:validate_public_key_bundle:secp_context"),
72 "puresign:validate_public_key_bundle:secp_context");
73 if (purify_bip340_validate_xonly_pubkey(secp_context, public_key.bip340_pubkey.data()) == 0) {
74 return unexpected_error(ErrorCode::BackendRejectedInput, "puresign:validate_public_key_bundle:bip340_pubkey");
75 }
76 return {};
77}
78
79const unsigned char* nullable_data(std::span<const unsigned char> input) {
80 return input.empty() ? nullptr : input.data();
81}
82
83XOnly32 binding_digest(const TaggedHash& tag, std::span<const unsigned char> input) {
84 XOnly32 out{};
85 out = tag.digest(input);
86 return out;
87}
88
89std::string_view proof_tag_for_scope(PreparedNonce::Scope scope) {
90 return scope == PreparedNonce::Scope::Message ? kMessageProofTag : kTopicProofTag;
91}
92
93Bytes proof_statement_binding(PreparedNonce::Scope scope) {
94 return bytes_from_ascii(proof_tag_for_scope(scope));
95}
96
97Scalar32 derive_proof_nonce_seed(const SecretKey& secret, PreparedNonce::Scope scope, std::span<const unsigned char> eval_input) {
98 std::array<unsigned char, 64> secret_bytes = secret.packed().to_bytes_be();
99 Scalar32 out{};
100 std::array digest_segments{
101 std::span<const unsigned char>(secret_bytes.data(), secret_bytes.size()),
102 eval_input,
103 };
104 out = proof_tagged_hash(scope).digest_many(digest_segments);
105 detail::secure_clear_bytes(secret_bytes.data(), secret_bytes.size());
106 return out;
107}
108
109Result<bool> nonce_proof_matches_nonce(const NonceProof& nonce_proof, purify_secp_context* secp_context) {
110 XOnly32 xonly{};
111 int parity = 0;
112 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "nonce_proof_matches_nonce:secp_context"),
113 "nonce_proof_matches_nonce:secp_context");
114 if (nonce_proof.proof.commitments.size() != 1) {
115 return unexpected_error(ErrorCode::SizeMismatch, "nonce_proof_matches_nonce:commitments_size");
116 }
117 if (purify_bip340_xonly_from_point(secp_context, nonce_proof.proof.commitments.front().data(), xonly.data(), &parity) == 0) {
118 return unexpected_error(ErrorCode::BackendRejectedInput, "nonce_proof_matches_nonce:invalid_commitment");
119 }
120 (void)parity;
121 return xonly == nonce_proof.nonce.xonly;
122}
123
125 return detail::validate_message_proof_cache(cache, kMessageNonceTag);
126}
127
129 return detail::validate_topic_proof_cache(cache, kTopicNonceTag);
130}
131
132struct DerivedNonceData {
134 Scalar32 scalar{};
135 Nonce nonce{};
136 XOnly32 signer_pubkey{};
137 XOnly32 binding_digest{};
138 Bytes eval_input;
139};
140
141Result<NonceProof> build_nonce_proof_from_template(const SecretKey& secret, const DerivedNonceData& nonce_data,
142 const NativeBulletproofCircuitTemplate& circuit_template,
143 purify_secp_context* secp_context,
144 ExperimentalBulletproofBackendCache* backend_cache = nullptr) {
145 PURIFY_ASSIGN_OR_RETURN(const auto& witness, prove_assignment_data(nonce_data.eval_input, secret),
146 "build_nonce_proof_from_template:prove_assignment_data");
147 PURIFY_ASSIGN_OR_RETURN(auto partial_ok, circuit_template.partial_evaluate(witness.assignment),
148 "build_nonce_proof_from_template:partial_evaluate");
149 if (!partial_ok) {
150 return unexpected_error(ErrorCode::BindingMismatch, "build_nonce_proof_from_template:partial_evaluate_false");
151 }
152 PURIFY_ASSIGN_OR_RETURN(auto final_ok, circuit_template.final_evaluate(witness.assignment, witness.public_key),
153 "build_nonce_proof_from_template:final_evaluate");
154 if (!final_ok) {
155 return unexpected_error(ErrorCode::BindingMismatch, "build_nonce_proof_from_template:final_evaluate_false");
156 }
157 PURIFY_ASSIGN_OR_RETURN(const auto& circuit, circuit_template.instantiate_packed(witness.public_key),
158 "build_nonce_proof_from_template:instantiate_packed");
159 PURIFY_RETURN_IF_ERROR(detail::validate_proof_cache_circuit(circuit, "build_nonce_proof_from_template:circuit_shape"),
160 "build_nonce_proof_from_template:validate_proof_cache_circuit");
161
162 Scalar32 proof_nonce = derive_proof_nonce_seed(secret, nonce_data.scope, nonce_data.eval_input);
163 Bytes statement_binding = proof_statement_binding(nonce_data.scope);
165 auto proof,
166 prove_experimental_circuit_assume_valid(circuit, witness.assignment, proof_nonce,
167 bppp::base_generator(secp_context), secp_context,
168 statement_binding, {},
169 backend_cache),
170 "build_nonce_proof_from_template:prove_experimental_circuit");
171
172 NonceProof out{};
173 out.nonce = nonce_data.nonce;
174 out.proof = std::move(proof);
175 PURIFY_ASSIGN_OR_RETURN(auto match, nonce_proof_matches_nonce(out, secp_context),
176 "build_nonce_proof_from_template:nonce_proof_matches_nonce");
177 if (!match) {
178 return unexpected_error(ErrorCode::InternalMismatch, "build_nonce_proof_from_template:nonce_mismatch");
179 }
180 return out;
181}
182
183Result<DerivedNonceData> derive_nonce_data(const SecretKey& secret, PreparedNonce::Scope scope,
184 std::span<const unsigned char> input,
185 purify_secp_context* secp_context) {
186 PURIFY_RETURN_IF_ERROR(validate_puresign_field_alignment(), "derive_nonce_data:validate_puresign_field_alignment");
187 PURIFY_ASSIGN_OR_RETURN(const auto& signer, derive_bip340_key(secret, secp_context), "derive_nonce_data:derive_bip340_key");
188
189 const std::string_view nonce_tag = scope == PreparedNonce::Scope::Message ? kMessageNonceTag : kTopicNonceTag;
190 const TaggedHash& binding_hash = binding_tagged_hash(scope);
191
192 DerivedNonceData out{};
193 out.scope = scope;
194 out.signer_pubkey = signer.xonly_pubkey;
195 out.binding_digest = binding_digest(binding_hash, input);
196
197 out.eval_input = detail::tagged_eval_input(nonce_tag, input);
198 PURIFY_ASSIGN_OR_RETURN(const auto& nonce_value, eval(secret, out.eval_input), "derive_nonce_data:eval");
199 out.scalar = nonce_value.to_bytes_be();
200 if (std::all_of(out.scalar.begin(), out.scalar.end(), [](unsigned char byte) { return byte == 0; })) {
201 /* This rejects the unique invalid Schnorr nonce. Since Purify outputs a field element
202 * modulo the secp256k1 group order, the failure probability is exactly 1/n, which is
203 * negligible for n ~= 2^256.
204 */
205 return unexpected_error(ErrorCode::BackendRejectedInput, "derive_nonce_data:zero_nonce");
206 }
207
208 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "derive_nonce_data:secp_context"),
209 "derive_nonce_data:secp_context");
210 if (purify_bip340_nonce_from_scalar(secp_context, out.scalar.data(), out.nonce.xonly.data()) == 0) {
211 return unexpected_error(ErrorCode::BackendRejectedInput, "derive_nonce_data:bip340_nonce_from_scalar");
212 }
213 return out;
214}
215
216Result<NonceProof> build_nonce_proof(const SecretKey& secret,
217 const DerivedNonceData& nonce_data,
218 purify_secp_context* secp_context) {
219 PURIFY_ASSIGN_OR_RETURN(const auto& circuit_template, verifier_circuit_template(nonce_data.eval_input),
220 "build_nonce_proof:verifier_circuit_template");
221 return build_nonce_proof_from_template(secret, nonce_data, circuit_template, secp_context);
222}
223
224Result<DerivedNonceData> prepare_nonce_data_impl(const SecretKey& secret, PreparedNonce::Scope scope,
225 std::span<const unsigned char> input,
226 purify_secp_context* secp_context) {
227 if (scope == PreparedNonce::Scope::Topic && input.empty()) {
228 return unexpected_error(ErrorCode::EmptyInput, "prepare_nonce_data_impl:empty_topic");
229 }
230 PURIFY_ASSIGN_OR_RETURN(auto nonce_data, derive_nonce_data(secret, scope, input, secp_context),
231 "prepare_nonce_data_impl:derive_nonce_data");
232 return nonce_data;
233}
234
235Result<bool> verify_nonce_proof_with_circuit(const PublicKey& public_key, const NativeBulletproofCircuit& circuit,
236 const NonceProof& nonce_proof, PreparedNonce::Scope scope,
237 purify_secp_context* secp_context,
238 const char* context,
239 ExperimentalBulletproofBackendCache* backend_cache = nullptr) {
240 PURIFY_RETURN_IF_ERROR(validate_public_key_bundle(public_key, secp_context), context);
242 PURIFY_ASSIGN_OR_RETURN(auto match, nonce_proof_matches_nonce(nonce_proof, secp_context), context);
243 if (!match) {
244 return false;
245 }
246 Bytes statement_binding = proof_statement_binding(scope);
247 return verify_experimental_circuit(circuit, nonce_proof.proof, bppp::base_generator(secp_context), secp_context, statement_binding,
248 backend_cache);
249}
250
251Result<bool> verify_nonce_proof_with_circuit(const PublicKey& public_key,
252 const NativeBulletproofCircuit::PackedWithSlack& circuit,
253 const NonceProof& nonce_proof, PreparedNonce::Scope scope,
254 purify_secp_context* secp_context,
255 const char* context,
256 ExperimentalBulletproofBackendCache* backend_cache = nullptr) {
257 PURIFY_RETURN_IF_ERROR(validate_public_key_bundle(public_key, secp_context), context);
259 PURIFY_ASSIGN_OR_RETURN(auto match, nonce_proof_matches_nonce(nonce_proof, secp_context), context);
260 if (!match) {
261 return false;
262 }
263 Bytes statement_binding = proof_statement_binding(scope);
264 return verify_experimental_circuit(circuit, nonce_proof.proof, bppp::base_generator(secp_context), secp_context, statement_binding,
265 backend_cache);
266}
267
268} // namespace
269
270PreparedNonce::PreparedNonce(Scope scope, const Scalar32& scalar, const Nonce& nonce,
271 const XOnly32& signer_pubkey, const XOnly32& binding_digest)
272 : scope_(scope), scalar_(scalar), nonce_(nonce),
273 signer_pubkey_(signer_pubkey), binding_digest_(binding_digest),
274 available_(true) {}
275
277 const XOnly32& signer_pubkey, const XOnly32& binding_digest) {
278 return PreparedNonce(scope, scalar, nonce, signer_pubkey, binding_digest);
279}
280
282 : scope_(other.scope_), scalar_(other.scalar_), nonce_(other.nonce_),
283 signer_pubkey_(other.signer_pubkey_), binding_digest_(other.binding_digest_),
284 available_(other.available_) {
285 other.clear();
286}
287
289 if (this != &other) {
290 clear();
291 scope_ = other.scope_;
292 scalar_ = other.scalar_;
293 nonce_ = other.nonce_;
294 signer_pubkey_ = other.signer_pubkey_;
295 binding_digest_ = other.binding_digest_;
296 available_ = other.available_;
297 other.clear();
298 }
299 return *this;
300}
301
303 clear();
304}
305
306void PreparedNonce::clear() noexcept {
307 detail::secure_clear_bytes(scalar_.data(), scalar_.size());
308 available_ = false;
309}
310
312 std::span<const unsigned char> message,
313 purify_secp_context* secp_context) && {
314 if (!available_) {
316 "PreparedNonce::sign_message:consumed");
317 }
318 available_ = false;
319 WipeOnDestruction burn(scalar_);
320 if (scope_ != Scope::Message) {
321 return unexpected_error(ErrorCode::BindingMismatch, "PreparedNonce::sign_message:scope");
322 }
323 if (signer_pubkey_ != signer.xonly_pubkey) {
324 return unexpected_error(ErrorCode::BindingMismatch, "PreparedNonce::sign_message:signer_pubkey");
325 }
326 if (binding_digest_ != binding_digest(kMessageBindingTaggedHash, message)) {
327 return unexpected_error(ErrorCode::BindingMismatch, "PreparedNonce::sign_message:message_binding");
328 }
329
330 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "PreparedNonce::sign_message:secp_context"),
331 "PreparedNonce::sign_message:secp_context");
332 Signature out{};
333 if (purify_bip340_sign_with_fixed_nonce(secp_context, out.bytes.data(), nullable_data(message), message.size(),
334 signer.seckey.data(), scalar_.data()) == 0) {
335 return unexpected_error(ErrorCode::BackendRejectedInput, "PreparedNonce::sign_message:sign_with_fixed_nonce");
336 }
337 if (out.nonce().xonly != nonce_.xonly) {
338 return unexpected_error(ErrorCode::InternalMismatch, "PreparedNonce::sign_message:nonce_mismatch");
339 }
340 if (purify_bip340_verify(secp_context, out.bytes.data(), nullable_data(message), message.size(),
341 signer.xonly_pubkey.data()) == 0) {
342 return unexpected_error(ErrorCode::InternalMismatch, "PreparedNonce::sign_message:self_verify");
343 }
344 return out;
345}
346
348 std::span<const unsigned char> message,
349 purify_secp_context* secp_context) && {
350 if (!available_) {
352 "PreparedNonce::sign_topic_message:consumed");
353 }
354 available_ = false;
355 WipeOnDestruction burn(scalar_);
356 if (scope_ != Scope::Topic) {
357 return unexpected_error(ErrorCode::BindingMismatch, "PreparedNonce::sign_topic_message:scope");
358 }
359 if (signer_pubkey_ != signer.xonly_pubkey) {
360 return unexpected_error(ErrorCode::BindingMismatch, "PreparedNonce::sign_topic_message:signer_pubkey");
361 }
362
363 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "PreparedNonce::sign_topic_message:secp_context"),
364 "PreparedNonce::sign_topic_message:secp_context");
365 Signature out{};
366 if (purify_bip340_sign_with_fixed_nonce(secp_context, out.bytes.data(), nullable_data(message), message.size(),
367 signer.seckey.data(), scalar_.data()) == 0) {
369 "PreparedNonce::sign_topic_message:sign_with_fixed_nonce");
370 }
371 if (out.nonce().xonly != nonce_.xonly) {
372 return unexpected_error(ErrorCode::InternalMismatch, "PreparedNonce::sign_topic_message:nonce_mismatch");
373 }
374 if (purify_bip340_verify(secp_context, out.bytes.data(), nullable_data(message), message.size(),
375 signer.xonly_pubkey.data()) == 0) {
376 return unexpected_error(ErrorCode::InternalMismatch, "PreparedNonce::sign_topic_message:self_verify");
377 }
378 return out;
379}
380
384
386 std::span<const unsigned char> message,
387 purify_secp_context* secp_context) && {
388 NonceProof nonce_proof = std::move(proof_);
389 PURIFY_ASSIGN_OR_RETURN(auto signature,
390 api_impl::sign_message_with_prepared(secret, message, std::move(prepared_), secp_context),
391 "PreparedNonceWithProof::sign_message:sign_message_with_prepared");
392 if (signature.nonce().xonly != nonce_proof.nonce.xonly) {
393 return unexpected_error(ErrorCode::InternalMismatch, "PreparedNonceWithProof::sign_message:nonce_mismatch");
394 }
395 return ProvenSignature{signature, std::move(nonce_proof)};
396}
397
399 std::span<const unsigned char> message,
400 purify_secp_context* secp_context) && {
401 NonceProof nonce_proof = std::move(proof_);
402 PURIFY_ASSIGN_OR_RETURN(auto signature,
403 api_impl::sign_with_prepared_topic(secret, message, std::move(prepared_), secp_context),
404 "PreparedNonceWithProof::sign_topic_message:sign_with_prepared_topic");
405 if (signature.nonce().xonly != nonce_proof.nonce.xonly) {
406 return unexpected_error(ErrorCode::InternalMismatch, "PreparedNonceWithProof::sign_topic_message:nonce_mismatch");
407 }
408 return ProvenSignature{signature, std::move(nonce_proof)};
409}
410
411namespace api_impl {
412
414 PURIFY_ASSIGN_OR_RETURN(const auto& purify_key, derive_key(secret), "derive_public_key:derive_key");
415 PURIFY_ASSIGN_OR_RETURN(const auto& bip340_key, derive_bip340_key(secret, secp_context), "derive_public_key:derive_bip340_key");
416 return PublicKey{purify_key.public_key, bip340_key.xonly_pubkey};
417}
418
419Result<MessageProofCache> build_message_proof_cache(std::span<const unsigned char> message) {
420 Bytes eval_input = detail::tagged_eval_input(kMessageNonceTag, message);
421 PURIFY_ASSIGN_OR_RETURN(auto circuit_template, verifier_circuit_template(eval_input),
422 "build_message_proof_cache:verifier_circuit_template");
423 PURIFY_ASSIGN_OR_RETURN(auto template_digest, circuit_template.integrity_digest(),
424 "build_message_proof_cache:integrity_digest");
425 MessageProofCache cache{};
426 cache.message = detail::copy_bytes(message);
427 cache.eval_input = std::move(eval_input);
428 cache.circuit_template = std::move(circuit_template);
429 cache.template_digest = std::move(template_digest);
430 return cache;
431}
432
433Result<TopicProofCache> build_topic_proof_cache(std::span<const unsigned char> topic) {
434 if (topic.empty()) {
435 return unexpected_error(ErrorCode::EmptyInput, "build_topic_proof_cache:empty_topic");
436 }
437 Bytes eval_input = detail::tagged_eval_input(kTopicNonceTag, topic);
438 PURIFY_ASSIGN_OR_RETURN(auto circuit_template, verifier_circuit_template(eval_input),
439 "build_topic_proof_cache:verifier_circuit_template");
440 PURIFY_ASSIGN_OR_RETURN(auto template_digest, circuit_template.integrity_digest(),
441 "build_topic_proof_cache:integrity_digest");
442 TopicProofCache cache{};
443 cache.topic = detail::copy_bytes(topic);
444 cache.eval_input = std::move(eval_input);
445 cache.circuit_template = std::move(circuit_template);
446 cache.template_digest = std::move(template_digest);
447 return cache;
448}
449
450Result<PreparedNonce> prepare_message_nonce(const SecretKey& secret, std::span<const unsigned char> message,
451 purify_secp_context* secp_context) {
452 PURIFY_ASSIGN_OR_RETURN(const auto& nonce_data,
453 prepare_nonce_data_impl(secret, PreparedNonce::Scope::Message, message, secp_context),
454 "prepare_message_nonce:prepare_nonce_data_impl");
455 return PreparedNonce::from_parts(PreparedNonce::Scope::Message, nonce_data.scalar, nonce_data.nonce,
456 nonce_data.signer_pubkey, nonce_data.binding_digest);
457}
458
460 std::span<const unsigned char> message,
461 purify_secp_context* secp_context) {
462 PURIFY_ASSIGN_OR_RETURN(const auto& nonce_data,
463 prepare_nonce_data_impl(secret, PreparedNonce::Scope::Message, message, secp_context),
464 "prepare_message_nonce_with_proof:prepare_nonce_data_impl");
466 nonce_data.nonce, nonce_data.signer_pubkey,
467 nonce_data.binding_digest);
468 PURIFY_ASSIGN_OR_RETURN(auto proof, build_nonce_proof(secret, nonce_data, secp_context),
469 "prepare_message_nonce_with_proof:build_nonce_proof");
470 return PreparedNonceWithProof::from_parts(std::move(prepared), std::move(proof));
471}
472
474 const MessageProofCache& cache,
475 purify_secp_context* secp_context) {
476 PURIFY_RETURN_IF_ERROR(validate_message_proof_cache(cache),
477 "prepare_message_nonce_with_proof:validate_message_proof_cache");
478 PURIFY_ASSIGN_OR_RETURN(const auto& nonce_data,
479 prepare_nonce_data_impl(secret, PreparedNonce::Scope::Message, cache.message, secp_context),
480 "prepare_message_nonce_with_proof:prepare_nonce_data_impl");
482 nonce_data.nonce, nonce_data.signer_pubkey,
483 nonce_data.binding_digest);
484 PURIFY_ASSIGN_OR_RETURN(auto proof,
485 build_nonce_proof_from_template(secret, nonce_data, cache.circuit_template, secp_context,
486 &cache.backend_cache),
487 "prepare_message_nonce_with_proof:build_nonce_proof_from_template");
488 return PreparedNonceWithProof::from_parts(std::move(prepared), std::move(proof));
489}
490
491Result<PreparedNonce> prepare_topic_nonce(const SecretKey& secret, std::span<const unsigned char> topic,
492 purify_secp_context* secp_context) {
493 PURIFY_ASSIGN_OR_RETURN(const auto& nonce_data,
494 prepare_nonce_data_impl(secret, PreparedNonce::Scope::Topic, topic, secp_context),
495 "prepare_topic_nonce:prepare_nonce_data_impl");
496 return PreparedNonce::from_parts(PreparedNonce::Scope::Topic, nonce_data.scalar, nonce_data.nonce,
497 nonce_data.signer_pubkey, nonce_data.binding_digest);
498}
499
501 std::span<const unsigned char> topic,
502 purify_secp_context* secp_context) {
503 PURIFY_ASSIGN_OR_RETURN(const auto& nonce_data,
504 prepare_nonce_data_impl(secret, PreparedNonce::Scope::Topic, topic, secp_context),
505 "prepare_topic_nonce_with_proof:prepare_nonce_data_impl");
507 nonce_data.nonce, nonce_data.signer_pubkey,
508 nonce_data.binding_digest);
509 PURIFY_ASSIGN_OR_RETURN(auto proof, build_nonce_proof(secret, nonce_data, secp_context),
510 "prepare_topic_nonce_with_proof:build_nonce_proof");
511 return PreparedNonceWithProof::from_parts(std::move(prepared), std::move(proof));
512}
513
515 const TopicProofCache& cache,
516 purify_secp_context* secp_context) {
517 PURIFY_RETURN_IF_ERROR(validate_topic_proof_cache(cache),
518 "prepare_topic_nonce_with_proof:validate_topic_proof_cache");
519 PURIFY_ASSIGN_OR_RETURN(const auto& nonce_data,
520 prepare_nonce_data_impl(secret, PreparedNonce::Scope::Topic, cache.topic, secp_context),
521 "prepare_topic_nonce_with_proof:prepare_nonce_data_impl");
523 nonce_data.nonce, nonce_data.signer_pubkey,
524 nonce_data.binding_digest);
525 PURIFY_ASSIGN_OR_RETURN(auto proof,
526 build_nonce_proof_from_template(secret, nonce_data, cache.circuit_template, secp_context,
527 &cache.backend_cache),
528 "prepare_topic_nonce_with_proof:build_nonce_proof_from_template");
529 return PreparedNonceWithProof::from_parts(std::move(prepared), std::move(proof));
530}
531
532Result<Signature> sign_message(const SecretKey& secret, std::span<const unsigned char> message,
533 purify_secp_context* secp_context) {
534 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_message_nonce(secret, message, secp_context), "sign_message:prepare_message_nonce");
535 return sign_message_with_prepared(secret, message, std::move(prepared), secp_context);
536}
537
538Result<Signature> sign_message_with_prepared(const SecretKey& secret, std::span<const unsigned char> message,
539 PreparedNonce prepared, purify_secp_context* secp_context) {
540 PURIFY_ASSIGN_OR_RETURN(const auto& signer, derive_bip340_key(secret, secp_context), "sign_message_with_prepared:derive_bip340_key");
541 return std::move(prepared).sign_message(signer, message, secp_context);
542}
543
544Result<ProvenSignature> sign_message_with_prepared_proof(const SecretKey& secret, std::span<const unsigned char> message,
545 PreparedNonceWithProof&& prepared,
546 purify_secp_context* secp_context) {
547 return std::move(prepared).sign_message(secret, message, secp_context);
548}
549
550Result<Signature> sign_with_topic(const SecretKey& secret, std::span<const unsigned char> message,
551 std::span<const unsigned char> topic,
552 purify_secp_context* secp_context) {
553 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_topic_nonce(secret, topic, secp_context), "sign_with_topic:prepare_topic_nonce");
554 return sign_with_prepared_topic(secret, message, std::move(prepared), secp_context);
555}
556
557Result<Signature> sign_with_prepared_topic(const SecretKey& secret, std::span<const unsigned char> message,
558 PreparedNonce prepared, purify_secp_context* secp_context) {
559 PURIFY_ASSIGN_OR_RETURN(const auto& signer, derive_bip340_key(secret, secp_context), "sign_with_prepared_topic:derive_bip340_key");
560 return std::move(prepared).sign_topic_message(signer, message, secp_context);
561}
562
563Result<ProvenSignature> sign_with_prepared_topic_proof(const SecretKey& secret, std::span<const unsigned char> message,
564 PreparedNonceWithProof&& prepared,
565 purify_secp_context* secp_context) {
566 return std::move(prepared).sign_topic_message(secret, message, secp_context);
567}
568
569Result<ProvenSignature> sign_message_with_proof(const SecretKey& secret, std::span<const unsigned char> message,
570 purify_secp_context* secp_context) {
571 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_message_nonce_with_proof(secret, message, secp_context),
572 "sign_message_with_proof:prepare_message_nonce_with_proof");
573 return sign_message_with_prepared_proof(secret, message, std::move(prepared), secp_context);
574}
575
577 purify_secp_context* secp_context) {
578 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_message_nonce_with_proof(secret, cache, secp_context),
579 "sign_message_with_proof:prepare_message_nonce_with_proof_cache");
580 return sign_message_with_prepared_proof(secret, cache.message, std::move(prepared), secp_context);
581}
582
583Result<ProvenSignature> sign_with_topic_proof(const SecretKey& secret, std::span<const unsigned char> message,
584 std::span<const unsigned char> topic,
585 purify_secp_context* secp_context) {
586 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_topic_nonce_with_proof(secret, topic, secp_context),
587 "sign_with_topic_proof:prepare_topic_nonce_with_proof");
588 return sign_with_prepared_topic_proof(secret, message, std::move(prepared), secp_context);
589}
590
591Result<ProvenSignature> sign_with_topic_proof(const SecretKey& secret, std::span<const unsigned char> message,
592 const TopicProofCache& cache,
593 purify_secp_context* secp_context) {
594 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_topic_nonce_with_proof(secret, cache, secp_context),
595 "sign_with_topic_proof:prepare_topic_nonce_with_proof_cache");
596 return sign_with_prepared_topic_proof(secret, message, std::move(prepared), secp_context);
597}
598
599Result<bool> verify_signature(const PublicKey& public_key, std::span<const unsigned char> message,
600 const Signature& signature, purify_secp_context* secp_context) {
601 PURIFY_RETURN_IF_ERROR(validate_public_key(public_key.purify_pubkey), "verify_signature:validate_public_key");
602 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "verify_signature:secp_context"),
603 "verify_signature:secp_context");
604 if (purify_bip340_validate_xonly_pubkey(secp_context, public_key.bip340_pubkey.data()) == 0) {
605 return unexpected_error(ErrorCode::BackendRejectedInput, "verify_signature:bip340_validate_xonly_pubkey");
606 }
607 if (purify_bip340_validate_signature(secp_context, signature.bytes.data()) == 0) {
608 return unexpected_error(ErrorCode::BackendRejectedInput, "verify_signature:bip340_validate_signature");
609 }
610 return purify_bip340_verify(secp_context, signature.bytes.data(), nullable_data(message), message.size(),
611 public_key.bip340_pubkey.data()) != 0;
612}
613
614Result<bool> verify_message_nonce_proof(const PublicKey& public_key, std::span<const unsigned char> message,
615 const NonceProof& nonce_proof,
616 purify_secp_context* secp_context,
618 PURIFY_ASSIGN_OR_RETURN(const auto& circuit,
619 verifier_circuit(detail::tagged_eval_input(kMessageNonceTag, message), public_key.purify_pubkey),
620 "verify_message_nonce_proof:verifier_circuit");
621 return verify_nonce_proof_with_circuit(public_key, circuit, nonce_proof, PreparedNonce::Scope::Message, secp_context,
622 "verify_message_nonce_proof:verify_nonce_proof_with_circuit",
623 circuit_cache);
624}
625
627 const NonceProof& nonce_proof,
628 purify_secp_context* secp_context,
630 PURIFY_RETURN_IF_ERROR(validate_message_proof_cache(cache), "verify_message_nonce_proof:validate_message_proof_cache");
631 PURIFY_ASSIGN_OR_RETURN(const auto& circuit, cache.circuit_template.instantiate_packed(public_key.purify_pubkey),
632 "verify_message_nonce_proof:instantiate_packed");
633 return verify_nonce_proof_with_circuit(public_key, circuit, nonce_proof, PreparedNonce::Scope::Message, secp_context,
634 "verify_message_nonce_proof:verify_nonce_proof_with_circuit",
635 circuit_cache != nullptr ? circuit_cache : &cache.backend_cache);
636}
637
638Result<bool> verify_topic_nonce_proof(const PublicKey& public_key, std::span<const unsigned char> topic,
639 const NonceProof& nonce_proof,
640 purify_secp_context* secp_context,
642 if (topic.empty()) {
643 return unexpected_error(ErrorCode::EmptyInput, "verify_topic_nonce_proof:empty_topic");
644 }
645 PURIFY_ASSIGN_OR_RETURN(const auto& circuit,
646 verifier_circuit(detail::tagged_eval_input(kTopicNonceTag, topic), public_key.purify_pubkey),
647 "verify_topic_nonce_proof:verifier_circuit");
648 return verify_nonce_proof_with_circuit(public_key, circuit, nonce_proof, PreparedNonce::Scope::Topic, secp_context,
649 "verify_topic_nonce_proof:verify_nonce_proof_with_circuit",
650 circuit_cache);
651}
652
654 const NonceProof& nonce_proof,
655 purify_secp_context* secp_context,
657 PURIFY_RETURN_IF_ERROR(validate_topic_proof_cache(cache), "verify_topic_nonce_proof:validate_topic_proof_cache");
658 PURIFY_ASSIGN_OR_RETURN(const auto& circuit, cache.circuit_template.instantiate_packed(public_key.purify_pubkey),
659 "verify_topic_nonce_proof:instantiate_packed");
660 return verify_nonce_proof_with_circuit(public_key, circuit, nonce_proof, PreparedNonce::Scope::Topic, secp_context,
661 "verify_topic_nonce_proof:verify_nonce_proof_with_circuit",
662 circuit_cache != nullptr ? circuit_cache : &cache.backend_cache);
663}
664
665Result<bool> verify_message_signature_with_proof(const PublicKey& public_key, std::span<const unsigned char> message,
666 const ProvenSignature& signature,
667 purify_secp_context* secp_context,
669 PURIFY_ASSIGN_OR_RETURN(auto sig_ok, verify_signature(public_key, message, signature.signature, secp_context),
670 "verify_message_signature_with_proof:verify_signature");
671 if (!sig_ok) {
672 return false;
673 }
674 if (signature.signature.nonce().xonly != signature.nonce_proof.nonce.xonly) {
675 return false;
676 }
677 return verify_message_nonce_proof(public_key, message, signature.nonce_proof, secp_context, circuit_cache);
678}
679
681 const ProvenSignature& signature,
682 purify_secp_context* secp_context,
684 PURIFY_ASSIGN_OR_RETURN(auto sig_ok, verify_signature(public_key, cache.message, signature.signature, secp_context),
685 "verify_message_signature_with_proof:verify_signature");
686 if (!sig_ok) {
687 return false;
688 }
689 if (signature.signature.nonce().xonly != signature.nonce_proof.nonce.xonly) {
690 return false;
691 }
692 return verify_message_nonce_proof(cache, public_key, signature.nonce_proof, secp_context, circuit_cache);
693}
694
695Result<bool> verify_topic_signature_with_proof(const PublicKey& public_key, std::span<const unsigned char> message,
696 std::span<const unsigned char> topic,
697 const ProvenSignature& signature,
698 purify_secp_context* secp_context,
700 PURIFY_ASSIGN_OR_RETURN(auto sig_ok, verify_signature(public_key, message, signature.signature, secp_context),
701 "verify_topic_signature_with_proof:verify_signature");
702 if (!sig_ok) {
703 return false;
704 }
705 if (signature.signature.nonce().xonly != signature.nonce_proof.nonce.xonly) {
706 return false;
707 }
708 return verify_topic_nonce_proof(public_key, topic, signature.nonce_proof, secp_context, circuit_cache);
709}
710
712 std::span<const unsigned char> message,
713 const ProvenSignature& signature,
714 purify_secp_context* secp_context,
716 PURIFY_ASSIGN_OR_RETURN(auto sig_ok, verify_signature(public_key, message, signature.signature, secp_context),
717 "verify_topic_signature_with_proof:verify_signature");
718 if (!sig_ok) {
719 return false;
720 }
721 if (signature.signature.nonce().xonly != signature.nonce_proof.nonce.xonly) {
722 return false;
723 }
724 return verify_topic_nonce_proof(cache, public_key, signature.nonce_proof, secp_context, circuit_cache);
725}
726
727} // namespace api_impl
728
730 return api_impl::derive_public_key(secret, secp_context);
731}
732
733Result<bool> PublicKey::verify_signature(std::span<const unsigned char> message, const Signature& signature,
734 purify_secp_context* secp_context) const {
735 return api_impl::verify_signature(*this, message, signature, secp_context);
736}
737
738Result<bool> PublicKey::verify_message_nonce_proof(std::span<const unsigned char> message,
739 const NonceProof& nonce_proof,
740 purify_secp_context* secp_context,
741 ExperimentalBulletproofBackendCache* circuit_cache) const {
742 return api_impl::verify_message_nonce_proof(*this, message, nonce_proof, secp_context, circuit_cache);
743}
744
746 purify_secp_context* secp_context,
747 ExperimentalBulletproofBackendCache* circuit_cache) const {
748 return api_impl::verify_message_nonce_proof(cache, *this, nonce_proof, secp_context, circuit_cache);
749}
750
751Result<bool> PublicKey::verify_topic_nonce_proof(std::span<const unsigned char> topic,
752 const NonceProof& nonce_proof,
753 purify_secp_context* secp_context,
754 ExperimentalBulletproofBackendCache* circuit_cache) const {
755 return api_impl::verify_topic_nonce_proof(*this, topic, nonce_proof, secp_context, circuit_cache);
756}
757
759 purify_secp_context* secp_context,
760 ExperimentalBulletproofBackendCache* circuit_cache) const {
761 return api_impl::verify_topic_nonce_proof(cache, *this, nonce_proof, secp_context, circuit_cache);
762}
763
765 const ProvenSignature& signature,
766 purify_secp_context* secp_context,
767 ExperimentalBulletproofBackendCache* circuit_cache) const {
768 return api_impl::verify_message_signature_with_proof(*this, message, signature, secp_context, circuit_cache);
769}
770
772 const ProvenSignature& signature,
773 purify_secp_context* secp_context,
774 ExperimentalBulletproofBackendCache* circuit_cache) const {
775 return api_impl::verify_message_signature_with_proof(cache, *this, signature, secp_context, circuit_cache);
776}
777
778Result<bool> PublicKey::verify_topic_signature_with_proof(std::span<const unsigned char> message,
779 std::span<const unsigned char> topic,
780 const ProvenSignature& signature,
781 purify_secp_context* secp_context,
782 ExperimentalBulletproofBackendCache* circuit_cache) const {
783 return api_impl::verify_topic_signature_with_proof(*this, message, topic, signature, secp_context, circuit_cache);
784}
785
787 std::span<const unsigned char> message,
788 const ProvenSignature& signature,
789 purify_secp_context* secp_context,
790 ExperimentalBulletproofBackendCache* circuit_cache) const {
791 return api_impl::verify_topic_signature_with_proof(cache, *this, message, signature, secp_context, circuit_cache);
792}
793
797
801
803 PURIFY_ASSIGN_OR_RETURN(auto owned_secret, secret.clone(), "KeyPair::from_secret:clone");
804 return KeyPair::from_secret(std::move(owned_secret), secp_context);
805}
806
808 PURIFY_ASSIGN_OR_RETURN(auto public_key, PublicKey::from_secret(secret, secp_context), "KeyPair::from_secret:from_secret");
809 return KeyPair(std::move(secret), std::move(public_key));
810}
811
812Result<PreparedNonce> KeyPair::prepare_message_nonce(std::span<const unsigned char> message,
813 purify_secp_context* secp_context) const {
814 return api_impl::prepare_message_nonce(secret_, message, secp_context);
815}
816
818 purify_secp_context* secp_context) const {
819 return api_impl::prepare_message_nonce_with_proof(secret_, message, secp_context);
820}
821
826
827Result<PreparedNonce> KeyPair::prepare_topic_nonce(std::span<const unsigned char> topic,
828 purify_secp_context* secp_context) const {
829 return api_impl::prepare_topic_nonce(secret_, topic, secp_context);
830}
831
833 purify_secp_context* secp_context) const {
834 return api_impl::prepare_topic_nonce_with_proof(secret_, topic, secp_context);
835}
836
841
842Result<Signature> KeyPair::sign_message(std::span<const unsigned char> message,
843 purify_secp_context* secp_context) const {
844 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_message_nonce(message, secp_context), "KeyPair::sign_message:prepare_message_nonce");
845 return sign_message_with_prepared(message, std::move(prepared), secp_context);
846}
847
848Result<Signature> KeyPair::sign_message_with_prepared(std::span<const unsigned char> message,
849 PreparedNonce&& prepared,
850 purify_secp_context* secp_context) const {
852 secret_, message, std::move(prepared), secp_context);
853}
854
856 PreparedNonceWithProof&& prepared,
857 purify_secp_context* secp_context) const {
858 return std::move(prepared).sign_message(secret_, message, secp_context);
859}
860
861Result<Signature> KeyPair::sign_with_topic(std::span<const unsigned char> message,
862 std::span<const unsigned char> topic,
863 purify_secp_context* secp_context) const {
864 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_topic_nonce(topic, secp_context), "KeyPair::sign_with_topic:prepare_topic_nonce");
865 return sign_with_prepared_topic(message, std::move(prepared), secp_context);
866}
867
868Result<Signature> KeyPair::sign_with_prepared_topic(std::span<const unsigned char> message,
869 PreparedNonce&& prepared,
870 purify_secp_context* secp_context) const {
872 secret_, message, std::move(prepared), secp_context);
873}
874
876 PreparedNonceWithProof&& prepared,
877 purify_secp_context* secp_context) const {
878 return std::move(prepared).sign_topic_message(secret_, message, secp_context);
879}
880
881Result<ProvenSignature> KeyPair::sign_message_with_proof(std::span<const unsigned char> message,
882 purify_secp_context* secp_context) const {
883 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_message_nonce_with_proof(message, secp_context),
884 "KeyPair::sign_message_with_proof:prepare_message_nonce_with_proof");
885 return sign_message_with_prepared_proof(message, std::move(prepared), secp_context);
886}
887
889 purify_secp_context* secp_context) const {
890 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_message_nonce_with_proof(cache, secp_context),
891 "KeyPair::sign_message_with_proof:prepare_message_nonce_with_proof");
892 return sign_message_with_prepared_proof(cache.message, std::move(prepared), secp_context);
893}
894
895Result<ProvenSignature> KeyPair::sign_with_topic_proof(std::span<const unsigned char> message,
896 std::span<const unsigned char> topic,
897 purify_secp_context* secp_context) const {
898 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_topic_nonce_with_proof(topic, secp_context),
899 "KeyPair::sign_with_topic_proof:prepare_topic_nonce_with_proof");
900 return sign_with_prepared_topic_proof(message, std::move(prepared), secp_context);
901}
902
903Result<ProvenSignature> KeyPair::sign_with_topic_proof(std::span<const unsigned char> message,
904 const TopicProofCache& cache,
905 purify_secp_context* secp_context) const {
906 PURIFY_ASSIGN_OR_RETURN(auto prepared, prepare_topic_nonce_with_proof(cache, secp_context),
907 "KeyPair::sign_with_topic_proof:prepare_topic_nonce_with_proof");
908 return sign_with_prepared_topic_proof(message, std::move(prepared), secp_context);
909}
910
911} // namespace purify::puresign
C++ wrappers for the BPPP functionality used by Purify.
Caller-owned cache for reusable legacy Bulletproof backend resources keyed by gate count.
Result< NativeBulletproofCircuit::PackedWithSlack > instantiate_packed(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
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 legacy.cpp:868
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 legacy.cpp:848
static Result< KeyPair > from_secret(const SecretKey &secret, purify_secp_context *secp_context)
Derives a signing key pair from one packed Purify secret.
Definition legacy.cpp:802
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 legacy.cpp:861
Result< PreparedNonce > prepare_message_nonce(std::span< const unsigned char > message, purify_secp_context *secp_context) const
Deterministically prepares a message-bound nonce.
Definition legacy.cpp:812
Result< PreparedNonceWithProof > prepare_message_nonce_with_proof(std::span< const unsigned char > message, purify_secp_context *secp_context) const
Deterministically prepares a message-bound nonce together with its proof.
Definition legacy.cpp:817
Result< PreparedNonce > prepare_topic_nonce(std::span< const unsigned char > topic, purify_secp_context *secp_context) const
Deterministically prepares a topic-bound nonce.
Definition legacy.cpp:827
Result< PreparedNonceWithProof > prepare_topic_nonce_with_proof(std::span< const unsigned char > topic, purify_secp_context *secp_context) const
Deterministically prepares a topic-bound nonce together with its proof.
Definition legacy.cpp:832
KeyPair(const KeyPair &)=delete
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 legacy.cpp:842
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 legacy.cpp:855
Result< ProvenSignature > sign_with_topic_proof(std::span< const unsigned char > message, std::span< const unsigned char > topic, purify_secp_context *secp_context) const
Signs a message with a topic-bound nonce proof.
Definition legacy.cpp:895
const PublicKey & public_key() const noexcept
Returns the public key bundle associated with this signer.
Definition legacy.hpp:489
Result< ProvenSignature > sign_message_with_proof(std::span< const unsigned char > message, purify_secp_context *secp_context) const
Signs a message and returns the signature bundled with its nonce proof.
Definition legacy.cpp:881
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 legacy.cpp:875
Move-only prepared nonce bundled with its public statement proof.
Definition legacy.hpp:387
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 legacy.cpp:398
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 legacy.cpp:385
const NonceProof & proof() const noexcept
Returns the public nonce proof carried by this bundle.
Definition legacy.hpp:413
static PreparedNonceWithProof from_parts(PreparedNonce prepared, NonceProof proof)
Bundles a prepared nonce with its matching public nonce proof.
Definition legacy.cpp:381
PreparedNonceWithProof(const PreparedNonceWithProof &)=delete
Move-only prepared nonce bound to either a message or a topic.
Definition legacy.hpp:292
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 legacy.cpp:276
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 legacy.cpp:311
PreparedNonce & operator=(const PreparedNonce &)=delete
Result< Scalar32 > scalar() const
Explicitly exports the secret nonce scalar.
Definition legacy.hpp:326
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 legacy.cpp:347
PreparedNonce(const PreparedNonce &)=delete
Elliptic-curve helpers, fixed parameters, and hash-to-curve utilities for Purify.
#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
Legacy Bulletproof-backed Purify-derived BIP340 signing helpers with prepared nonces.
GeneratorBytes base_generator(purify_secp_context *secp_context)
Returns the serialized secp256k1 base generator used as the blind generator.
Definition bppp.cpp:1702
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< PreparedNonce > prepare_topic_nonce(const SecretKey &secret, std::span< const unsigned char > topic, purify_secp_context *secp_context)
Definition legacy.cpp:491
Result< Signature > sign_message_with_prepared(const SecretKey &secret, std::span< const unsigned char > message, PreparedNonce prepared, purify_secp_context *secp_context)
Definition legacy.cpp:538
Result< ProvenSignature > sign_message_with_prepared_proof(const SecretKey &secret, std::span< const unsigned char > message, PreparedNonceWithProof &&prepared, purify_secp_context *secp_context)
Definition legacy.cpp:544
Result< ProvenSignature > sign_message_with_proof(const SecretKey &secret, std::span< const unsigned char > message, purify_secp_context *secp_context)
Definition legacy.cpp:569
Result< PreparedNonceWithProof > prepare_topic_nonce_with_proof(const SecretKey &secret, std::span< const unsigned char > topic, purify_secp_context *secp_context)
Definition legacy.cpp:500
Result< Signature > sign_message(const SecretKey &secret, std::span< const unsigned char > message, purify_secp_context *secp_context)
Definition legacy.cpp:532
Result< ProvenSignature > sign_with_prepared_topic_proof(const SecretKey &secret, std::span< const unsigned char > message, PreparedNonceWithProof &&prepared, purify_secp_context *secp_context)
Definition legacy.cpp:563
Result< TopicProofCache > build_topic_proof_cache(std::span< const unsigned char > topic)
Definition legacy.cpp:433
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, ExperimentalBulletproofBackendCache *circuit_cache)
Definition legacy.cpp:638
Result< PreparedNonceWithProof > prepare_message_nonce_with_proof(const SecretKey &secret, std::span< const unsigned char > message, purify_secp_context *secp_context)
Definition legacy.cpp:459
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, ExperimentalBulletproofBackendCache *circuit_cache)
Definition legacy.cpp:614
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 legacy.cpp:550
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, ExperimentalBulletproofBackendCache *circuit_cache)
Definition legacy.cpp:695
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, ExperimentalBulletproofBackendCache *circuit_cache)
Definition legacy.cpp:665
Result< PreparedNonce > prepare_message_nonce(const SecretKey &secret, std::span< const unsigned char > message, purify_secp_context *secp_context)
Definition legacy.cpp:450
Result< PublicKey > derive_public_key(const SecretKey &secret, purify_secp_context *secp_context)
Definition legacy.cpp:413
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)
Definition legacy.cpp:583
Result< MessageProofCache > build_message_proof_cache(std::span< const unsigned char > message)
Definition legacy.cpp:419
Result< bool > verify_signature(const PublicKey &public_key, std::span< const unsigned char > message, const Signature &signature, purify_secp_context *secp_context)
Definition legacy.cpp:599
Result< Signature > sign_with_prepared_topic(const SecretKey &secret, std::span< const unsigned char > message, PreparedNonce prepared, purify_secp_context *secp_context)
Definition legacy.cpp:557
std::array< unsigned char, 32 > Scalar32
Definition legacy.hpp:25
std::array< unsigned char, 32 > XOnly32
Definition legacy.hpp:26
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
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< bool > verify_experimental_circuit(const NativeBulletproofCircuit &circuit, const ExperimentalBulletproofProof &proof, const BulletproofGeneratorBytes &value_generator, purify_secp_context *secp_context, std::span< const unsigned char > statement_binding={}, ExperimentalBulletproofBackendCache *backend_cache=nullptr)
Verifies a proof produced by prove_experimental_circuit against the same native circuit and ordered o...
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
Result< ExperimentalBulletproofProof > prove_experimental_circuit_assume_valid(const NativeBulletproofCircuit::PackedWithSlack &circuit, const BulletproofAssignmentData &assignment, const BulletproofScalarBytes &nonce, const BulletproofGeneratorBytes &value_generator, purify_secp_context *secp_context, std::span< const unsigned char > statement_binding, std::span< const BulletproofScalarBytes > blinds, ExperimentalBulletproofBackendCache *backend_cache)
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.
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.
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
Canonical BIP340 keypair derived deterministically from a packed Purify secret.
Definition api.hpp:38
Cacheable message-bound nonce-proof template.
Definition legacy.hpp:247
NativeBulletproofCircuitTemplate circuit_template
Definition legacy.hpp:250
static Result< MessageProofCache > build(std::span< const unsigned char > message)
Builds a reusable verifier template for one exact message.
Definition legacy.cpp:794
ExperimentalBulletproofBackendCache backend_cache
Definition legacy.hpp:252
Public nonce together with its experimental Purify statement proof.
Definition legacy.hpp:221
Public BIP340 nonce point in x-only form.
Definition legacy.hpp:190
Standard signature bundled with the public nonce proof it relied on.
Definition legacy.hpp:231
Public key bundle pairing a Purify packed public key with its derived BIP340 x-only key.
Definition legacy.hpp:42
Result< bool > verify_message_signature_with_proof(std::span< const unsigned char > message, const ProvenSignature &signature, purify_secp_context *secp_context, ExperimentalBulletproofBackendCache *circuit_cache=nullptr) const
Verifies a message signature bundled with its nonce proof.
Definition legacy.cpp:764
Result< bool > verify_message_nonce_proof(std::span< const unsigned char > message, const NonceProof &nonce_proof, purify_secp_context *secp_context, ExperimentalBulletproofBackendCache *circuit_cache=nullptr) const
Verifies a message-bound nonce proof against this public key.
Definition legacy.cpp:738
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 legacy.cpp:733
static Result< PublicKey > from_secret(const SecretKey &secret, purify_secp_context *secp_context)
Derives both public identities from one packed Purify secret.
Definition legacy.cpp:729
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, ExperimentalBulletproofBackendCache *circuit_cache=nullptr) const
Verifies a topic-bound signature bundled with its nonce proof.
Definition legacy.cpp:778
Result< bool > verify_topic_nonce_proof(std::span< const unsigned char > topic, const NonceProof &nonce_proof, purify_secp_context *secp_context, ExperimentalBulletproofBackendCache *circuit_cache=nullptr) const
Verifies a topic-bound nonce proof against this public key.
Definition legacy.cpp:751
Standard 64-byte BIP340 signature.
Definition legacy.hpp:201
Cacheable topic-bound nonce-proof template.
Definition legacy.hpp:269
NativeBulletproofCircuitTemplate circuit_template
Definition legacy.hpp:272
ExperimentalBulletproofBackendCache backend_cache
Definition legacy.hpp:274
static Result< TopicProofCache > build(std::span< const unsigned char > topic)
Builds a reusable verifier template for one exact topic.
Definition legacy.cpp:798