18#include "../detail/common.hpp"
25Result<bool> nonce_proof_matches_nonce(
const NonceProof& nonce_proof,
purify_secp_context* secp_context) {
29 "nonce_proof_matches_nonce:secp_context");
32 "nonce_proof_matches_nonce:invalid_commitment_point");
35 return xonly == nonce_proof.nonce.xonly;
44 out.insert(out.end(), packed.begin(), packed.end());
57 std::copy(serialized.begin() + 64, serialized.end(), out.bip340_pubkey.begin());
59 "PublicKey::deserialize:secp_context");
62 "PublicKey::deserialize:bip340_validate_xonly_pubkey");
77 std::copy(serialized.begin(), serialized.end(), out.xonly.begin());
79 "Nonce::deserialize:secp_context");
88 std::copy(
bytes.begin(),
bytes.begin() + 32, out.xonly.begin());
94 std::copy(
bytes.begin() + 32,
bytes.end(), out.begin());
108 std::copy(serialized.begin(), serialized.end(), out.bytes.begin());
110 "Signature::deserialize:secp_context");
119 "NonceProof::serialize:nonce_proof_matches_nonce");
123 if (
proof.
proof.size() >
static_cast<std::size_t
>(std::numeric_limits<std::uint32_t>::max())) {
126 std::size_t serialized_size = 0;
132 out.reserve(serialized_size);
146 constexpr std::size_t kHeaderSize = 1 + 4 + 32 + 33 + 33 + 33 + 32;
147 if (serialized.size() < kHeaderSize) {
154 assert(proof_size.has_value() &&
"header length check should guarantee a u32 proof size");
155 if (*proof_size != serialized.size() - kHeaderSize) {
160 std::copy_n(serialized.begin() + 5, 32, out.nonce.xonly.begin());
162 "NonceProof::deserialize:secp_context");
166 std::copy_n(serialized.begin() + 37, 33, out.commitment_point.begin());
167 std::copy_n(serialized.begin() + 70, 33, out.proof.a_commitment.begin());
168 std::copy_n(serialized.begin() + 103, 33, out.proof.s_commitment.begin());
169 std::copy_n(serialized.begin() + 136, 32, out.proof.t2.begin());
170 out.proof.proof.assign(serialized.begin() + 168, serialized.end());
173 "NonceProof::deserialize:nonce_proof_matches_nonce");
182 "ProvenSignature::serialize:nonce_proof");
183 if (nonce_proof_bytes.size() >
static_cast<std::size_t
>(std::numeric_limits<std::uint32_t>::max())) {
186 std::size_t serialized_size = 0;
192 out.reserve(serialized_size);
195 out.insert(out.end(), nonce_proof_bytes.begin(), nonce_proof_bytes.end());
202 if (serialized.size() < 69) {
209 assert(nonce_proof_size.has_value() &&
"header length check should guarantee a u32 nonce proof size");
210 const std::size_t payload_size = serialized.size() - 5;
211 if (*nonce_proof_size > payload_size || payload_size - *nonce_proof_size != 64) {
217 "ProvenSignature::deserialize:nonce_proof");
220 "ProvenSignature::deserialize:signature");
Purify result carrier that either holds a value or an error.
#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.
std::optional< std::uint32_t > read_u32_le(std::span< const unsigned char > bytes, std::size_t offset)
void append_u32_le(Bytes &out, std::uint32_t value)
std::array< unsigned char, 32 > Scalar32
std::array< unsigned char, 32 > XOnly32
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.
Status validate_public_key(const UInt512 &packed)
Validates the packed public-key encoding range.
std::vector< unsigned char > Bytes
Dynamically sized byte string used for messages, serialized witnesses, and proofs.
bool checked_add_size(std::size_t lhs, std::size_t rhs, std::size_t &out) noexcept
Experimental BPPP-backed PureSign proof(R) helpers.
Narrow C ABI exposing secp256k1 scalar and HMAC helpers to the C++ headers.
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_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.
static BigUInt from_bytes_be(const unsigned char *data, std::size_t size)
Parses a big-endian byte string into the fixed-width integer.
std::array< unsigned char, Words *8 > to_bytes_be() const
Serializes the value to a fixed-width big-endian byte array.
PointBytes a_commitment
Witness-only outer A commitment; verifiers re-anchor it to the public statement.
bppp::PointBytes commitment_point
static Result< NonceProof > deserialize(std::span< const unsigned char > serialized, purify_secp_context *secp_context)
Result< Bytes > serialize(purify_secp_context *secp_context) const
bppp::ExperimentalCircuitZkNormArgProof proof
static constexpr unsigned char kSerializationVersion
Public BIP340 nonce point in x-only form.
static Result< Nonce > deserialize(std::span< const unsigned char > serialized, purify_secp_context *secp_context)
Parses a serialized x-only nonce.
Bytes serialize() const
Serializes this x-only nonce into its fixed-size wire format.
static constexpr std::size_t kSerializedSize
Result< Bytes > serialize(purify_secp_context *secp_context) const
static constexpr unsigned char kSerializationVersion
static Result< ProvenSignature > deserialize(std::span< const unsigned char > serialized, purify_secp_context *secp_context)
static Result< PublicKey > deserialize(std::span< const unsigned char > serialized, purify_secp_context *secp_context)
Parses a serialized PureSign++ public-key bundle.
Bytes serialize() const
Serializes this PureSign++ public-key bundle into its fixed-size wire format.
static constexpr std::size_t kSerializedSize
Standard 64-byte BIP340 signature.
Bytes serialize() const
Serializes this signature into its fixed-size wire format.
static constexpr std::size_t kSerializedSize
Scalar32 s() const
Returns the 32-byte Schnorr s scalar encoded in the last 32 signature bytes.
Nonce nonce() const
Returns the x-only public nonce encoded in the first 32 signature bytes.
static Result< Signature > deserialize(std::span< const unsigned char > serialized, purify_secp_context *secp_context)
Parses a serialized BIP340 signature.