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";
46 "Purify/Accountable/CertificateNonceProof/v1");
48 "Purify/Accountable/CertificateNonceLeaf/v1");
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;
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;
65const UInt256& secp256k1_order() {
67 UInt256::from_hex(
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141");
71Status validate_puresign_field_alignment() {
78const unsigned char* nullable_data(std::span<const unsigned char> input) {
79 return input.empty() ? nullptr : input.data();
82XOnly32 binding_digest(
const TaggedHash& tag, std::span<const unsigned char> input) {
84 out = tag.digest(input);
88std::string_view proof_tag_for_scope(Scope scope) {
89 return scope == Scope::Message ? kMessageProofTag : kTopicProofTag;
92Bytes proof_statement_binding(Scope scope) {
96using SecretProofSeed = ::purify::detail::Secret<Scalar32>;
97using SecretPackedKeyBytes =
98 ::purify::detail::Secret<std::array<unsigned char, 64>>;
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()),
110 Scalar32 digest = proof_tagged_hash(scope).digest_many(digest_segments);
117 "puresign_plusplus:validate_public_key_bundle:purify_pubkey");
119 "puresign_plusplus:validate_public_key_bundle:secp_context");
122 "puresign_plusplus:validate_public_key_bundle:bip340_pubkey");
135struct DerivedNonceData {
136 Scope scope = Scope::Message;
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)) {
152 DerivedNonceData& operator=(DerivedNonceData&& other)
noexcept {
153 if (
this != &other) {
156 scalar = other.scalar;
158 signer_pubkey = other.signer_pubkey;
159 binding_digest = other.binding_digest;
160 eval_input = std::move(other.eval_input);
165 ~DerivedNonceData() { clear(); }
168 void clear() noexcept {
175 std::span<const unsigned char> input,
177 PURIFY_RETURN_IF_ERROR(validate_puresign_field_alignment(),
"derive_nonce_data:validate_puresign_field_alignment");
180 const std::string_view nonce_tag = scope == Scope::Message ? kMessageNonceTag : kTopicNonceTag;
181 const TaggedHash& binding_hash = binding_tagged_hash(scope);
183 DerivedNonceData out{};
185 out.signer_pubkey = signer.xonly_pubkey;
186 out.binding_digest = binding_digest(binding_hash, input);
189 auto nonce_value =
eval(secret, out.eval_input);
190 if (!nonce_value.has_value()) {
193 if (nonce_value->is_zero()) {
196 "derive_nonce_data:zero_nonce");
198 static_assert(std::is_trivially_destructible_v<FieldElement>);
205 "derive_nonce_data:secp_context");
214 std::span<const unsigned char> input,
216 if (scope == Scope::Topic && input.empty()) {
220 "prepare_nonce_data_impl:derive_nonce_data");
236 "nonce_proof_matches_nonce:secp_context");
239 "nonce_proof_matches_nonce:invalid_commitment_point");
242 return xonly == nonce_proof.nonce.xonly;
247 const Nonce& expected_nonce,
248 std::span<const unsigned char> eval_input,
249 const NativeBulletproofCircuitTemplate& circuit_template,
251 bppp::ExperimentalCircuitBackend* circuit_cache) {
253 if (!witness_result.has_value()) {
255 "build_nonce_proof_from_template:prove_assignment_data");
257 BulletproofWitnessData& witness = *witness_result;
259 "build_nonce_proof_from_template:partial_evaluate");
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");
269 "build_nonce_proof_from_template:instantiate");
271 "build_nonce_proof_from_template:validate_proof_cache_circuit");
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};
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");
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);
299 "build_nonce_proof_from_template:nonce_proof_matches_nonce");
308 std::span<const unsigned char> input,
309 const Nonce& expected_nonce,
311 bppp::ExperimentalCircuitBackend* circuit_cache) {
312 const std::string_view nonce_tag = scope == Scope::Message ? kMessageNonceTag : kTopicNonceTag;
314 "build_nonce_proof:verifier_circuit_template");
316 return build_nonce_proof_from_template(secret, scope, expected_nonce, eval_input, circuit_template, secp_context,
321 const UInt512& nonce_host_public_key,
322 const NativeBulletproofCircuit& circuit,
327 bppp::ExperimentalCircuitBackend* circuit_cache) {
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,
341 nonce_proof.public_commitment_dlog_nonce,
342 nonce_proof.public_commitment_dlog_response,
343 nonce_proof.norm_proof,
349 circuit, std::move(proof), public_commitments, secp_context,
354 circuit, publication, public_commitments, secp_context,
355 statement_binding, circuit_cache);
360 const NativeBulletproofCircuit& circuit,
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);
378 "MessageProofCache::build:verifier_circuit_template");
380 "MessageProofCache::build:integrity_digest");
395 "TopicProofCache::build:verifier_circuit_template");
397 "TopicProofCache::build:integrity_digest");
408 : scope_(scope), scalar_(scalar), nonce_(nonce),
409 signer_pubkey_(signer_pubkey), binding_digest_(binding_digest),
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_) {
425 if (
this != &other) {
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_;
442void PreparedNonce::clear() noexcept {
448 std::span<const unsigned char> message,
452 "PreparedNonce::sign_message:consumed");
459 if (signer_pubkey_ != signer.xonly_pubkey) {
462 if (binding_digest_ != binding_digest(binding_tagged_hash(
Scope::Message), message)) {
467 "PreparedNonce::sign_message:secp_context");
470 signer.seckey.data(), scalar_.data()) == 0) {
477 signer.xonly_pubkey.data()) == 0) {
484 std::span<const unsigned char> message,
488 "PreparedNonce::sign_topic_message:consumed");
495 if (signer_pubkey_ != signer.xonly_pubkey) {
500 "PreparedNonce::sign_topic_message:secp_context");
503 signer.seckey.data(), scalar_.data()) == 0) {
505 "PreparedNonce::sign_topic_message:sign_with_fixed_nonce");
511 signer.xonly_pubkey.data()) == 0) {
522 if (!prepared_.available()) {
524 "PreparedNonceWithProof::raw_scalar:consumed");
526 if (proof_.commitment_point[0] != 0x02 &&
527 proof_.commitment_point[0] != 0x03) {
529 "PreparedNonceWithProof::raw_scalar:point");
531 if (!std::equal(proof_.commitment_point.begin() + 1,
532 proof_.commitment_point.end(),
533 prepared_.public_nonce().xonly.begin())) {
536 "PreparedNonceWithProof::raw_scalar:nonce_mismatch");
539 "PreparedNonceWithProof::raw_scalar:scalar");
540 if (proof_.commitment_point[0] == 0x03) {
549 "PreparedNonceWithProof::raw_scalar:normalized_scalar");
559 std::span<const unsigned char> message,
564 "PreparedNonceWithProof::sign_message:sign_message_with_prepared");
565 if (signature.nonce().xonly != nonce_proof.
nonce.
xonly) {
572 std::span<const unsigned char> message,
577 "PreparedNonceWithProof::sign_topic_message:sign_with_prepared_topic");
578 if (signature.nonce().xonly != nonce_proof.
nonce.
xonly) {
589 return PublicKey{purify_key.public_key, bip340_key.xonly_pubkey};
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");
605 nonce_data.signer_pubkey, nonce_data.binding_digest);
609 std::span<const unsigned char> message,
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");
615 nonce_data.nonce, nonce_data.signer_pubkey,
616 nonce_data.binding_digest);
619 "prepare_message_nonce_with_proof:build_nonce_proof");
628 "prepare_message_nonce_with_proof:validate_message_proof_cache");
630 "prepare_message_nonce_with_proof:prepare_nonce_data_impl");
632 nonce_data.nonce, nonce_data.signer_pubkey,
633 nonce_data.binding_digest);
636 build_nonce_proof_from_template(secret, Scope::Message, prepared.
public_nonce(),
638 circuit_cache !=
nullptr ? circuit_cache : &cache.
backend_cache),
639 "prepare_message_nonce_with_proof:build_nonce_proof_from_template");
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");
648 nonce_data.signer_pubkey, nonce_data.binding_digest);
652 std::span<const unsigned char> topic,
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");
658 nonce_data.nonce, nonce_data.signer_pubkey,
659 nonce_data.binding_digest);
662 "prepare_topic_nonce_with_proof:build_nonce_proof");
671 "prepare_topic_nonce_with_proof:validate_topic_proof_cache");
673 "prepare_topic_nonce_with_proof:prepare_nonce_data_impl");
675 nonce_data.nonce, nonce_data.signer_pubkey,
676 nonce_data.binding_digest);
679 build_nonce_proof_from_template(secret, Scope::Topic, prepared.
public_nonce(),
681 circuit_cache !=
nullptr ? circuit_cache : &cache.
backend_cache),
682 "prepare_topic_nonce_with_proof:build_nonce_proof_from_template");
695 return std::move(prepared).sign_message(signer, message, secp_context);
699 std::span<const unsigned char> message,
702 return std::move(prepared).sign_message(secret, message, secp_context);
706 std::span<const unsigned char> topic,
715 return std::move(prepared).sign_topic_message(signer, message, secp_context);
719 std::span<const unsigned char> message,
722 return std::move(prepared).sign_topic_message(secret, message, secp_context);
729 "sign_message_with_proof:prepare_message_nonce_with_proof");
737 "sign_message_with_proof:prepare_message_nonce_with_proof");
742 std::span<const unsigned char> topic,
746 "sign_with_topic_proof:prepare_topic_nonce_with_proof");
755 "sign_with_topic_proof:prepare_topic_nonce_with_proof");
763 "verify_signature:secp_context");
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",
790 PURIFY_RETURN_IF_ERROR(validate_message_proof_cache(cache),
"verify_message_nonce_proof:validate_message_proof_cache");
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);
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",
817 PURIFY_RETURN_IF_ERROR(validate_topic_proof_cache(cache),
"verify_topic_nonce_proof:validate_topic_proof_cache");
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);
826 std::span<const unsigned char> message,
831 "verify_message_signature_with_proof:verify_signature");
846 "verify_message_signature_with_proof:verify_signature");
857 std::span<const unsigned char> message,
858 std::span<const unsigned char> topic,
863 "verify_topic_signature_with_proof:verify_signature");
874 std::span<const unsigned char> message,
879 "verify_topic_signature_with_proof:verify_signature");
941 std::span<const unsigned char> topic,
949 std::span<const unsigned char> message,
972 std::span<const unsigned char> message,
991 std::span<const unsigned char> topic,
1014 secret_, message, std::move(prepared), secp_context);
1020 return std::move(prepared).sign_message(secret_, message, secp_context);
1024 std::span<const unsigned char> topic,
1034 secret_, message, std::move(prepared), secp_context);
1040 return std::move(prepared).sign_topic_message(secret_, message, secp_context);
1047 "KeyPair::sign_message_with_proof:prepare_message_nonce_with_proof");
1055 "KeyPair::sign_message_with_proof:prepare_message_nonce_with_proof");
1060 std::span<const unsigned char> topic,
1064 "KeyPair::sign_with_topic_proof:prepare_topic_nonce_with_proof");
1073 "KeyPair::sign_with_topic_proof:prepare_topic_nonce_with_proof");
1078 std::span<const unsigned char> topic,
1082 if (topic.empty()) {
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);
1102 validate_topic_proof_cache(cache),
1103 "NonceHost::verify_topic:cache");
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);
1122 "Purify/Accountable/CertificateNonceLeafAccumulation/v1",
1124 "certificate_nonce_leaf_accumulation:basis");
1136 const auto matches, nonce_proof_matches_nonce(proof, secp_context),
1137 "certificate_nonce_leaf_parent:nonce");
1141 "certificate_nonce_leaf_parent:nonce");
1144 public_key, cache, circuit_precommitment,
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");
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{
1180 derive_circuit_norm_parent_constants_v4_with_public_commitments(
1181 circuit, precommitment, public_commitments, secp_context,
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),
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");
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{
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();
1242 prepare_circuit_zk_norm_arg_v4_with_public_commitments(
1243 circuit, publication, public_commitments, secp_context,
1245 circuit_cache !=
nullptr ? circuit_cache
1247 "certificate_nonce_leaf:outer");
1248 if (!prepared.has_value()) {
1252 const auto& transcript_input = prepared->transcript_input();
1254 prepared->circuit_binding_digest(),
1255 transcript_input.raw_generator_digest,
1256 transcript_input.coefficient_commitment,
1257 transcript_input.anchored_offset};
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);
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,
1277 cache.
topic, proof_digest, binding_digest,
1278 std::move(parent_constants), std::move(norm));
Result< NativeBulletproofCircuit > instantiate(const UInt512 &pubkey) const
Move-only packed Purify secret stored in dedicated heap memory.
Result< SecretKey > clone() const
Creates a second owned copy of this secret key.
Reusable BIP340-style tagged SHA-256 helper.
Common interface for reusable experimental BPPP backend state.
static Result< Basis > create(std::size_t n_vec_len, std::size_t l_vec_len, std::string_view semantic_domain, purify_secp_context *context)
static Result< CircuitPrecommitmentDigestV4 > parse(const ScalarBytes &encoded)
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
static constexpr std::size_t kNormVectorLength
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
static Result< CertificateNonceLeafAccumulator > create(purify_secp_context *secp_context)
Result< PreparedCertificateNonceLeaf > prepare(const PublicKey &public_key, const TopicProofCache &cache, const NonceProof &proof, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache=nullptr) const
const bppp::accumulation::Basis & basis() const noexcept
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.
Result< PreparedNonce > prepare_topic_nonce(std::span< const unsigned char > topic, purify_secp_context *secp_context) const
Deterministically prepares a topic-bound nonce.
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.
const PublicKey & public_key() const noexcept
Returns the public key bundle associated with this signer.
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.
static Result< KeyPair > from_secret(const SecretKey &secret, purify_secp_context *secp_context)
Derives a PureSign++ signing key pair from one packed Purify secret.
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.
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.
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.
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.
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.
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.
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.
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.
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
Result< Scalar32 > scalar() const
Explicitly exports the secret nonce scalar from the wrapped prepared nonce.
PreparedNonceWithProof(const PreparedNonceWithProof &)=delete
const NonceProof & proof() const noexcept
Returns the public nonce proof carried by this bundle.
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.
static PreparedNonceWithProof from_parts(PreparedNonce prepared, NonceProof proof)
Bundles a prepared nonce with its matching BPPP-backed public nonce proof.
Result< Scalar32 > raw_scalar() const
Explicitly exports the scalar for the proof's full raw point.
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.
Move-only prepared nonce bound to either a message or a topic.
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.
Result< Scalar32 > scalar() const
Explicitly exports the secret nonce scalar.
const Nonce & public_nonce() const noexcept
Returns the public nonce corresponding to this prepared secret nonce scalar.
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.
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.
#define PURIFY_RETURN_IF_ERROR(expr, context)
Evaluates an expected-like expression and returns the wrapped error on failure.
#define PURIFY_ASSIGN_OR_RETURN(lhs, expr, context)
Evaluates an expected-like expression, binds the value to lhs, and propagates errors.
Result< 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)
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)
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)
GeneratorBytes base_generator(purify_secp_context *secp_context)
Returns the serialized secp256k1 base generator used as the blind generator.
std::array< unsigned char, 33 > PointBytes
Compressed 33-byte curve-point encoding.
Result< PointBytes > pedersen_commit_char(const ScalarBytes &blind, const ScalarBytes &value, purify_secp_context *secp_context)
Computes a Pedersen commitment to an arbitrary 32-byte scalar value using Purify's default generators...
ScalarBytes scalar_bytes(const FieldElement &value)
Serializes a Purify field element into the scalar encoding expected by the BPPP bridge.
std::array< unsigned char, 32 > ScalarBytes
Big-endian 32-byte scalar encoding.
const UInt256 & secp256k1_order()
Bytes tagged_eval_input(std::string_view tag, std::span< const unsigned char > input)
Status validate_topic_proof_cache(const CacheLike &cache, std::string_view nonce_tag)
void secure_clear_bytes(void *data, std::size_t size) noexcept
Status validate_message_proof_cache(const CacheLike &cache, std::string_view nonce_tag)
Bytes copy_bytes(std::span< const unsigned char > input)
Status validate_proof_cache_circuit(const CircuitLike &circuit, const char *context)
Result< PublicKey > derive_public_key(const SecretKey &secret, purify_secp_context *secp_context)
Result< PreparedNonce > prepare_message_nonce(const SecretKey &secret, std::span< const unsigned char > message, purify_secp_context *secp_context)
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)
Result< PreparedNonce > prepare_topic_nonce(const SecretKey &secret, std::span< const unsigned char > topic, purify_secp_context *secp_context)
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)
Result< ProvenSignature > sign_message_with_proof(const SecretKey &secret, std::span< const unsigned char > message, purify_secp_context *secp_context, bppp::ExperimentalCircuitBackend *circuit_cache)
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)
Result< ProvenSignature > sign_message_with_prepared_proof(const SecretKey &secret, std::span< const unsigned char > message, PreparedNonceWithProof &&prepared, purify_secp_context *secp_context)
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)
Result< MessageProofCache > build_message_proof_cache(std::span< const unsigned char > message)
Result< Signature > sign_message_with_prepared(const SecretKey &secret, std::span< const unsigned char > message, PreparedNonce prepared, purify_secp_context *secp_context)
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)
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)
Result< Signature > sign_with_prepared_topic(const SecretKey &secret, std::span< const unsigned char > message, PreparedNonce prepared, purify_secp_context *secp_context)
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)
Result< ProvenSignature > sign_with_prepared_topic_proof(const SecretKey &secret, std::span< const unsigned char > message, PreparedNonceWithProof &&prepared, purify_secp_context *secp_context)
Result< bool > verify_signature(const PublicKey &public_key, std::span< const unsigned char > message, const Signature &signature, purify_secp_context *secp_context)
Result< Signature > sign_message(const SecretKey &secret, std::span< const unsigned char > message, purify_secp_context *secp_context)
Result< TopicProofCache > build_topic_proof_cache(std::span< const unsigned char > topic)
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)
std::array< unsigned char, 32 > XOnly32
std::array< unsigned char, 32 > Scalar32
Status require_secp_context(const purify_secp_context *context, const char *error_context)
constexpr Unexpected< Error > unexpected_error(ErrorCode code, const char *context=nullptr)
Constructs an unexpected Error value from a machine-readable code.
BigUInt< 8 > UInt512
512-bit unsigned integer used for private and packed public keys.
const UInt256 & prime_p()
Returns the Purify base-field modulus.
Result< NativeBulletproofCircuitTemplate > verifier_circuit_template(const Bytes &message)
Builds a reusable public-key-agnostic verifier-circuit template for a message.
Result< BulletproofWitnessData > prove_assignment_data(const Bytes &message, const SecretKey &secret)
Computes the native Purify witness for a message and secret.
Result< NativeBulletproofCircuit > verifier_circuit(const Bytes &message, const UInt512 &pubkey)
Builds the native verifier circuit for a message and public key.
Bytes bytes_from_ascii(std::string_view input)
Encodes an ASCII string as a byte vector.
BigUInt< 4 > UInt256
256-bit unsigned integer used for field elements and curve orders.
Status validate_public_key(const UInt512 &packed)
Validates the packed public-key encoding range.
Result< Bip340Key > derive_bip340_key(const SecretKey &secret, purify_secp_context *secp_context)
Derives a canonical BIP340 signing keypair from an owned Purify secret.
Result< GeneratedKey > derive_key(const SecretKey &secret)
Derives the packed public key corresponding to a packed secret.
Expected< T, Error > Result
Expected-returning convenience alias for Purify value-producing APIs.
Result< FieldElement > eval(const SecretKey &secret, const Bytes &message)
Evaluates the Purify PRF for an owned secret key and message.
Expected< void, Error > Status
Expected-returning convenience alias for Purify status-only APIs.
std::vector< unsigned char > Bytes
Dynamically sized byte string used for messages, serialized witnesses, and proofs.
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)
std::array< unsigned char, Words *8 > to_bytes_be() const
Serializes the value to a fixed-width big-endian byte array.
Canonical BIP340 keypair derived deterministically from a packed Purify secret.
static const purify_scalar & raw(const FieldElement &value) noexcept
Cacheable message-bound nonce-proof template for the BPPP-backed PureSign++ proof(R) flow.
bppp::ExperimentalCircuitCache backend_cache
NativeBulletproofCircuitTemplate circuit_template
static Result< MessageProofCache > build(std::span< const unsigned char > message)
Builds a reusable verifier template for one exact message.
bppp::PointBytes commitment_point
bppp::PointBytes raw_commitment
bppp::PointBytes mask_commitment
Result< Bytes > serialize(purify_secp_context *secp_context) const
bppp::ScalarBytes public_commitment_dlog_response
XOnly32 public_commitment_dlog_nonce
static constexpr std::size_t kNormProofSize
Public BIP340 nonce point in x-only form.
static Result< PublicKey > from_secret(const SecretKey &secret, purify_secp_context *secp_context)
Derives both public identities from one packed Purify secret.
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.
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.
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.
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.
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.
Standard 64-byte BIP340 signature.
Nonce nonce() const
Returns the x-only public nonce encoded in the first 32 signature bytes.
Cacheable topic-bound nonce-proof template for the BPPP-backed PureSign++ proof(R) flow.
bppp::ExperimentalCircuitCache backend_cache
NativeBulletproofCircuitTemplate circuit_template
static Result< TopicProofCache > build(std::span< const unsigned char > topic)
Builds a reusable verifier template for one exact topic.
Opaque scalar storage compatible with secp256k1-zkp internal scalar storage.