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 <cassert>
14#include <cstdint>
15#include <limits>
16#include <optional>
17
18#include "../detail/common.hpp"
19#include "purify/secp_bridge.h"
20
21namespace purify::puresign {
22
23namespace {
24
25Result<bool> nonce_proof_matches_nonce(const NonceProof& nonce_proof, purify_secp_context* secp_context) {
26 XOnly32 xonly{};
27 int parity = 0;
28 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "nonce_proof_matches_nonce:secp_context"),
29 "nonce_proof_matches_nonce:secp_context");
30 if (nonce_proof.proof.commitments.size() != 1) {
31 return unexpected_error(ErrorCode::SizeMismatch, "nonce_proof_matches_nonce:commitments_size");
32 }
33 if (purify_bip340_xonly_from_point(secp_context, nonce_proof.proof.commitments.front().data(), xonly.data(), &parity) == 0) {
34 return unexpected_error(ErrorCode::BackendRejectedInput, "nonce_proof_matches_nonce:invalid_commitment");
35 }
36 (void)parity;
37 return xonly == nonce_proof.nonce.xonly;
38}
39
40} // namespace
41
43 Bytes out;
44 out.reserve(kSerializedSize);
45 std::array<unsigned char, 64> packed = purify_pubkey.to_bytes_be();
46 out.insert(out.end(), packed.begin(), packed.end());
47 out.insert(out.end(), bip340_pubkey.begin(), bip340_pubkey.end());
48 return out;
49}
50
51Result<PublicKey> PublicKey::deserialize(std::span<const unsigned char> serialized,
52 purify_secp_context* secp_context) {
53 if (serialized.size() != kSerializedSize) {
54 return unexpected_error(ErrorCode::InvalidFixedSize, "PublicKey::deserialize:size");
55 }
56 UInt512 purify_pubkey = UInt512::from_bytes_be(serialized.data(), 64);
57 PURIFY_RETURN_IF_ERROR(validate_public_key(purify_pubkey), "PublicKey::deserialize:validate_public_key");
58
59 PublicKey out{};
61 std::copy(serialized.begin() + 64, serialized.end(), out.bip340_pubkey.begin());
62 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "PublicKey::deserialize:secp_context"),
63 "PublicKey::deserialize:secp_context");
64 if (purify_bip340_validate_xonly_pubkey(secp_context, out.bip340_pubkey.data()) == 0) {
65 return unexpected_error(ErrorCode::BackendRejectedInput, "PublicKey::deserialize:bip340_validate_xonly_pubkey");
66 }
67 return out;
68}
69
71 return Bytes(xonly.begin(), xonly.end());
72}
73
74Result<Nonce> Nonce::deserialize(std::span<const unsigned char> serialized,
75 purify_secp_context* secp_context) {
76 if (serialized.size() != kSerializedSize) {
77 return unexpected_error(ErrorCode::InvalidFixedSize, "Nonce::deserialize:size");
78 }
79 Nonce out{};
80 std::copy(serialized.begin(), serialized.end(), out.xonly.begin());
81 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "Nonce::deserialize:secp_context"),
82 "Nonce::deserialize:secp_context");
83 if (purify_bip340_validate_xonly_pubkey(secp_context, out.xonly.data()) == 0) {
84 return unexpected_error(ErrorCode::BackendRejectedInput, "Nonce::deserialize:bip340_validate_xonly_pubkey");
85 }
86 return out;
87}
88
90 Nonce out{};
91 std::copy(bytes.begin(), bytes.begin() + 32, out.xonly.begin());
92 return out;
93}
94
96 Scalar32 out{};
97 std::copy(bytes.begin() + 32, bytes.end(), out.begin());
98 return out;
99}
100
102 return Bytes(bytes.begin(), bytes.end());
103}
104
105Result<Signature> Signature::deserialize(std::span<const unsigned char> serialized,
106 purify_secp_context* secp_context) {
107 if (serialized.size() != kSerializedSize) {
108 return unexpected_error(ErrorCode::InvalidFixedSize, "Signature::deserialize:size");
109 }
110 Signature out{};
111 std::copy(serialized.begin(), serialized.end(), out.bytes.begin());
112 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "Signature::deserialize:secp_context"),
113 "Signature::deserialize:secp_context");
114 if (purify_bip340_validate_signature(secp_context, out.bytes.data()) == 0) {
115 return unexpected_error(ErrorCode::BackendRejectedInput, "Signature::deserialize:bip340_validate_signature");
116 }
117 return out;
118}
119
121 PURIFY_ASSIGN_OR_RETURN(auto match, nonce_proof_matches_nonce(*this, secp_context),
122 "NonceProof::serialize:nonce_proof_matches_nonce");
123 if (!match) {
124 return unexpected_error(ErrorCode::BindingMismatch, "NonceProof::serialize:nonce_mismatch");
125 }
126 PURIFY_ASSIGN_OR_RETURN(const auto& proof_bytes, proof.serialize(), "NonceProof::serialize:proof");
127 if (proof_bytes.size() > static_cast<std::size_t>(std::numeric_limits<std::uint32_t>::max())) {
128 return unexpected_error(ErrorCode::UnexpectedSize, "NonceProof::serialize:proof_size");
129 }
130 std::size_t serialized_size = 0;
131 if (!checked_add_size(37, proof_bytes.size(), serialized_size)) {
132 return unexpected_error(ErrorCode::Overflow, "NonceProof::serialize:reserve");
133 }
134
135 Bytes out;
136 out.reserve(serialized_size);
137 out.push_back(static_cast<unsigned char>(2));
138 detail::append_u32_le(out, static_cast<std::uint32_t>(proof_bytes.size()));
139 out.insert(out.end(), nonce.xonly.begin(), nonce.xonly.end());
140 out.insert(out.end(), proof_bytes.begin(), proof_bytes.end());
141 return out;
142}
143
144Result<NonceProof> NonceProof::deserialize(std::span<const unsigned char> serialized,
145 purify_secp_context* secp_context) {
146 if (serialized.size() < 37) {
147 return unexpected_error(ErrorCode::InvalidFixedSize, "NonceProof::deserialize:header");
148 }
149 if (serialized[0] != 2) {
150 return unexpected_error(ErrorCode::BackendRejectedInput, "NonceProof::deserialize:version");
151 }
152 std::optional<std::uint32_t> proof_size = detail::read_u32_le(serialized, 1);
153 assert(proof_size.has_value() && "header length check should guarantee a u32 proof size");
154 if (*proof_size != serialized.size() - 37) {
155 return unexpected_error(ErrorCode::InvalidFixedSize, "NonceProof::deserialize:proof_size");
156 }
157
158 NonceProof out{};
159 std::copy_n(serialized.begin() + 5, 32, out.nonce.xonly.begin());
160 PURIFY_ASSIGN_OR_RETURN(auto proof_value,
161 ExperimentalBulletproofProof::deserialize(serialized.subspan(37, *proof_size)),
162 "NonceProof::deserialize:proof");
163 out.proof = std::move(proof_value);
164 PURIFY_ASSIGN_OR_RETURN(auto match, nonce_proof_matches_nonce(out, secp_context),
165 "NonceProof::deserialize:nonce_proof_matches_nonce");
166 if (!match) {
167 return unexpected_error(ErrorCode::BindingMismatch, "NonceProof::deserialize:nonce_mismatch");
168 }
169 return out;
170}
171
173 PURIFY_ASSIGN_OR_RETURN(const auto& nonce_proof_bytes, nonce_proof.serialize(secp_context),
174 "ProvenSignature::serialize:nonce_proof");
175 if (nonce_proof_bytes.size() > static_cast<std::size_t>(std::numeric_limits<std::uint32_t>::max())) {
176 return unexpected_error(ErrorCode::UnexpectedSize, "ProvenSignature::serialize:nonce_proof_size");
177 }
178 std::size_t serialized_size = 0;
179 if (!checked_add_size(69, nonce_proof_bytes.size(), serialized_size)) {
180 return unexpected_error(ErrorCode::Overflow, "ProvenSignature::serialize:reserve");
181 }
182
183 Bytes out;
184 out.reserve(serialized_size);
185 out.push_back(static_cast<unsigned char>(1));
186 detail::append_u32_le(out, static_cast<std::uint32_t>(nonce_proof_bytes.size()));
187 out.insert(out.end(), nonce_proof_bytes.begin(), nonce_proof_bytes.end());
188 out.insert(out.end(), signature.bytes.begin(), signature.bytes.end());
189 return out;
190}
191
192Result<ProvenSignature> ProvenSignature::deserialize(std::span<const unsigned char> serialized,
193 purify_secp_context* secp_context) {
194 if (serialized.size() < 69) {
195 return unexpected_error(ErrorCode::InvalidFixedSize, "ProvenSignature::deserialize:header");
196 }
197 if (serialized[0] != 1) {
198 return unexpected_error(ErrorCode::BackendRejectedInput, "ProvenSignature::deserialize:version");
199 }
200 std::optional<std::uint32_t> nonce_proof_size = detail::read_u32_le(serialized, 1);
201 assert(nonce_proof_size.has_value() && "header length check should guarantee a u32 nonce proof size");
202 const std::size_t payload_size = serialized.size() - 5;
203 if (*nonce_proof_size > payload_size || payload_size - *nonce_proof_size != 64) {
204 return unexpected_error(ErrorCode::InvalidFixedSize, "ProvenSignature::deserialize:size");
205 }
206
207 PURIFY_ASSIGN_OR_RETURN(auto nonce_proof_value,
208 NonceProof::deserialize(serialized.subspan(5, *nonce_proof_size), secp_context),
209 "ProvenSignature::deserialize:nonce_proof");
210 PURIFY_ASSIGN_OR_RETURN(auto signature_value,
211 Signature::deserialize(serialized.subspan(5 + *nonce_proof_size, 64), secp_context),
212 "ProvenSignature::deserialize:signature");
213 return ProvenSignature{signature_value, nonce_proof_value};
214}
215
216} // namespace purify::puresign
#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.
std::optional< std::uint32_t > read_u32_le(std::span< const unsigned char > bytes, std::size_t offset)
Definition common.hpp:35
void append_u32_le(Bytes &out, std::uint32_t value)
Definition common.hpp:29
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
BigUInt< 8 > UInt512
512-bit unsigned integer used for private and packed public keys.
Definition numeric.hpp:821
Status validate_public_key(const UInt512 &packed)
Validates the packed public-key encoding range.
Definition curve.cpp:319
Expected< T, Error > Result
Expected-returning convenience alias for Purify value-producing APIs.
Definition error.hpp:99
std::vector< unsigned char > Bytes
Dynamically sized byte string used for messages, serialized witnesses, and proofs.
Definition common.hpp:99
bool checked_add_size(std::size_t lhs, std::size_t rhs, std::size_t &out) noexcept
Definition common.hpp:63
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.
struct purify_secp_context purify_secp_context
Caller-owned reusable secp256k1 context handle shared across Purify C and C++ APIs.
static BigUInt from_bytes_be(const unsigned char *data, std::size_t size)
Definition numeric.hpp:238
static Result< ExperimentalBulletproofProof > deserialize(std::span< const unsigned char > bytes)
Public nonce together with its experimental Purify statement proof.
Definition legacy.hpp:221
ExperimentalBulletproofProof proof
Definition legacy.hpp:223
static Result< NonceProof > deserialize(std::span< const unsigned char > serialized, purify_secp_context *secp_context)
Definition legacy.cpp:144
Result< Bytes > serialize(purify_secp_context *secp_context) const
Definition legacy.cpp:120
Public BIP340 nonce point in x-only form.
Definition legacy.hpp:190
static Result< Nonce > deserialize(std::span< const unsigned char > serialized, purify_secp_context *secp_context)
Definition legacy.cpp:74
static constexpr std::size_t kSerializedSize
Definition legacy.hpp:191
Bytes serialize() const
Definition legacy.cpp:70
Standard signature bundled with the public nonce proof it relied on.
Definition legacy.hpp:231
static Result< ProvenSignature > deserialize(std::span< const unsigned char > serialized, purify_secp_context *secp_context)
Definition legacy.cpp:192
Result< Bytes > serialize(purify_secp_context *secp_context) const
Definition legacy.cpp:172
Public key bundle pairing a Purify packed public key with its derived BIP340 x-only key.
Definition legacy.hpp:42
static constexpr std::size_t kSerializedSize
Definition legacy.hpp:43
Bytes serialize() const
Serializes this public-key bundle into its fixed-size wire format.
Definition legacy.cpp:42
static Result< PublicKey > deserialize(std::span< const unsigned char > serialized, purify_secp_context *secp_context)
Parses a serialized public-key bundle.
Definition legacy.cpp:51
Standard 64-byte BIP340 signature.
Definition legacy.hpp:201
static constexpr std::size_t kSerializedSize
Definition legacy.hpp:202
static Result< Signature > deserialize(std::span< const unsigned char > serialized, purify_secp_context *secp_context)
Definition legacy.cpp:105
Scalar32 s() const
Definition legacy.cpp:95