purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
bppp.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Judica, Inc.
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or https://opensource.org/license/mit/.
4
9
11
12#include <algorithm>
13#include <cassert>
14
15#include "../detail/common.hpp"
17#include "purify/secp_bridge.h"
18
20
21namespace {
22
23Result<bool> nonce_proof_matches_nonce(const NonceProof& nonce_proof, purify_secp_context* secp_context) {
24 XOnly32 xonly{};
25 int parity = 0;
26 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "nonce_proof_matches_nonce:secp_context"),
27 "nonce_proof_matches_nonce:secp_context");
28 if (purify_bip340_xonly_from_point(secp_context, nonce_proof.commitment_point.data(), xonly.data(), &parity) == 0) {
30 "nonce_proof_matches_nonce:invalid_commitment_point");
31 }
32 (void)parity;
33 return xonly == nonce_proof.nonce.xonly;
34}
35
36Status validate_circuit_proof_encoding(
37 const NonceProof& nonce_proof,
38 purify_secp_context* secp_context) {
39 static constexpr std::size_t kRoundCount = 12;
40 static_assert(NonceProof::kNormProofSize == 65 * kRoundCount + 64);
41 const bppp::candidate::CircuitZkNormArgProofV4 proof{
42 nonce_proof.raw_commitment,
43 nonce_proof.mask_commitment,
44 nonce_proof.t2,
45 nonce_proof.public_commitment_dlog_nonce,
46 nonce_proof.public_commitment_dlog_response,
47 nonce_proof.norm_proof,
48 };
52 kRoundCount, secp_context);
53}
54
55} // namespace
56
58 std::span<const unsigned char> serialized) {
59 if (serialized.size() != kSerializedSize) {
60 return unexpected_error(
61 ErrorCode::InvalidFixedSize, "NonceHost::deserialize:size");
62 }
64 serialized.data(), serialized.size());
67 "NonceHost::deserialize:public_key");
68 return NonceHost(std::move(public_key));
69}
70
72 Bytes out;
73 out.reserve(kSerializedSize);
74 std::array<unsigned char, 64> packed = purify_pubkey.to_bytes_be();
75 out.insert(out.end(), packed.begin(), packed.end());
76 out.insert(out.end(), bip340_pubkey.begin(), bip340_pubkey.end());
77 return out;
78}
79
80Result<PublicKey> PublicKey::deserialize(std::span<const unsigned char> serialized,
81 purify_secp_context* secp_context) {
82 if (serialized.size() != kSerializedSize) {
83 return unexpected_error(ErrorCode::InvalidFixedSize, "PublicKey::deserialize:size");
84 }
85 PublicKey out{};
86 out.purify_pubkey = UInt512::from_bytes_be(serialized.data(), 64);
87 PURIFY_RETURN_IF_ERROR(validate_public_key(out.purify_pubkey), "PublicKey::deserialize:validate_public_key");
88 std::copy(serialized.begin() + 64, serialized.end(), out.bip340_pubkey.begin());
89 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "PublicKey::deserialize:secp_context"),
90 "PublicKey::deserialize:secp_context");
91 if (purify_bip340_validate_xonly_pubkey(secp_context, out.bip340_pubkey.data()) == 0) {
93 "PublicKey::deserialize:bip340_validate_xonly_pubkey");
94 }
95 return out;
96}
97
99 return Bytes(xonly.begin(), xonly.end());
100}
101
102Result<Nonce> Nonce::deserialize(std::span<const unsigned char> serialized,
103 purify_secp_context* secp_context) {
104 if (serialized.size() != kSerializedSize) {
105 return unexpected_error(ErrorCode::InvalidFixedSize, "Nonce::deserialize:size");
106 }
107 Nonce out{};
108 std::copy(serialized.begin(), serialized.end(), out.xonly.begin());
109 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "Nonce::deserialize:secp_context"),
110 "Nonce::deserialize:secp_context");
111 if (purify_bip340_validate_xonly_pubkey(secp_context, out.xonly.data()) == 0) {
112 return unexpected_error(ErrorCode::BackendRejectedInput, "Nonce::deserialize:bip340_validate_xonly_pubkey");
113 }
114 return out;
115}
116
118 Nonce out{};
119 std::copy(bytes.begin(), bytes.begin() + 32, out.xonly.begin());
120 return out;
121}
122
124 Scalar32 out{};
125 std::copy(bytes.begin() + 32, bytes.end(), out.begin());
126 return out;
127}
128
130 return Bytes(bytes.begin(), bytes.end());
131}
132
133Result<Signature> Signature::deserialize(std::span<const unsigned char> serialized,
134 purify_secp_context* secp_context) {
135 if (serialized.size() != kSerializedSize) {
136 return unexpected_error(ErrorCode::InvalidFixedSize, "Signature::deserialize:size");
137 }
138 Signature out{};
139 std::copy(serialized.begin(), serialized.end(), out.bytes.begin());
140 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "Signature::deserialize:secp_context"),
141 "Signature::deserialize:secp_context");
142 if (purify_bip340_validate_signature(secp_context, out.bytes.data()) == 0) {
143 return unexpected_error(ErrorCode::BackendRejectedInput, "Signature::deserialize:bip340_validate_signature");
144 }
145 return out;
146}
147
149 PURIFY_ASSIGN_OR_RETURN(auto match, nonce_proof_matches_nonce(*this, secp_context),
150 "NonceProof::serialize:nonce_proof_matches_nonce");
151 if (!match) {
152 return unexpected_error(ErrorCode::BindingMismatch, "NonceProof::serialize:nonce_mismatch");
153 }
155 validate_circuit_proof_encoding(*this, secp_context),
156 "NonceProof::serialize:circuit_proof");
157
158 Bytes out;
159 out.reserve(kSerializedSize);
160 out.push_back(kSerializationVersion);
161 out.insert(out.end(), nonce.xonly.begin(), nonce.xonly.end());
162 out.insert(out.end(), commitment_point.begin(), commitment_point.end());
163 out.insert(out.end(), raw_commitment.begin(), raw_commitment.end());
164 out.insert(out.end(), mask_commitment.begin(), mask_commitment.end());
165 out.insert(out.end(), t2.begin(), t2.end());
166 out.insert(out.end(), public_commitment_dlog_nonce.begin(),
168 out.insert(out.end(), public_commitment_dlog_response.begin(),
170 out.insert(out.end(), norm_proof.begin(), norm_proof.end());
171 assert(out.size() == kSerializedSize);
172 return out;
173}
174
175Result<NonceProof> NonceProof::deserialize(std::span<const unsigned char> serialized,
176 purify_secp_context* secp_context) {
177 constexpr std::size_t kHeaderSize =
178 1 + 32 + 33 + 33 + 33 + 32 + 32 + 32;
179 static_assert(kHeaderSize + kNormProofSize == kSerializedSize);
180 if (serialized.size() != kSerializedSize) {
181 return unexpected_error(ErrorCode::InvalidFixedSize, "NonceProof::deserialize:header");
182 }
183 if (serialized[0] != kSerializationVersion) {
184 return unexpected_error(ErrorCode::BackendRejectedInput, "NonceProof::deserialize:version");
185 }
186 NonceProof out{};
187 std::copy_n(serialized.begin() + 1, 32, out.nonce.xonly.begin());
188 PURIFY_RETURN_IF_ERROR(require_secp_context(secp_context, "NonceProof::deserialize:secp_context"),
189 "NonceProof::deserialize:secp_context");
190 if (purify_bip340_validate_xonly_pubkey(secp_context, out.nonce.xonly.data()) == 0) {
191 return unexpected_error(ErrorCode::BackendRejectedInput, "NonceProof::deserialize:nonce");
192 }
193 std::copy_n(serialized.begin() + 33, 33, out.commitment_point.begin());
194 std::copy_n(serialized.begin() + 66, 33, out.raw_commitment.begin());
195 std::copy_n(serialized.begin() + 99, 33, out.mask_commitment.begin());
196 std::copy_n(serialized.begin() + 132, 32, out.t2.begin());
197 std::copy_n(serialized.begin() + 164, 32,
198 out.public_commitment_dlog_nonce.begin());
199 std::copy_n(serialized.begin() + 196, 32,
201 out.norm_proof.assign(serialized.begin() + kHeaderSize,
202 serialized.end());
204 validate_circuit_proof_encoding(out, secp_context),
205 "NonceProof::deserialize:circuit_proof");
206
207 PURIFY_ASSIGN_OR_RETURN(auto match, nonce_proof_matches_nonce(out, secp_context),
208 "NonceProof::deserialize:nonce_proof_matches_nonce");
209 if (!match) {
210 return unexpected_error(ErrorCode::BindingMismatch, "NonceProof::deserialize:nonce_mismatch");
211 }
212 return out;
213}
214
216 PURIFY_ASSIGN_OR_RETURN(const auto& nonce_proof_bytes, nonce_proof.serialize(secp_context),
217 "ProvenSignature::serialize:nonce_proof");
218 Bytes out;
219 out.reserve(kSerializedSize);
220 out.push_back(kSerializationVersion);
221 out.insert(out.end(), nonce_proof_bytes.begin(), nonce_proof_bytes.end());
222 out.insert(out.end(), signature.bytes.begin(), signature.bytes.end());
223 assert(out.size() == kSerializedSize);
224 return out;
225}
226
227Result<ProvenSignature> ProvenSignature::deserialize(std::span<const unsigned char> serialized,
228 purify_secp_context* secp_context) {
229 if (serialized.size() != kSerializedSize) {
230 return unexpected_error(ErrorCode::InvalidFixedSize, "ProvenSignature::deserialize:header");
231 }
232 if (serialized[0] != kSerializationVersion) {
233 return unexpected_error(ErrorCode::BackendRejectedInput, "ProvenSignature::deserialize:version");
234 }
235 PURIFY_ASSIGN_OR_RETURN(auto nonce_proof_value,
237 serialized.subspan(1, NonceProof::kSerializedSize),
238 secp_context),
239 "ProvenSignature::deserialize:nonce_proof");
240 PURIFY_ASSIGN_OR_RETURN(auto signature_value,
242 serialized.subspan(1 + NonceProof::kSerializedSize,
244 secp_context),
245 "ProvenSignature::deserialize:signature");
246 return ProvenSignature{signature_value, nonce_proof_value};
247}
248
249} // namespace purify::puresign_plusplus
static constexpr std::size_t kSerializedSize
Definition bppp.hpp:44
const UInt512 & public_key() const noexcept
Definition bppp.hpp:49
static Result< NonceHost > deserialize(std::span< const unsigned char > serialized)
Definition bppp.cpp:57
#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
Status validate_circuit_zk_norm_arg_proof_v4_fixed_encoding(const CircuitZkNormArgProofV4 &proof, CircuitCommitmentModeV4 mode, std::size_t round_count, purify_secp_context *secp_context)
Definition bppp.cpp:3323
std::array< unsigned char, 32 > XOnly32
Definition bppp.hpp:24
std::array< unsigned char, 32 > Scalar32
Definition bppp.hpp:23
Status require_secp_context(const purify_secp_context *context, const char *error_context)
Definition common.hpp:56
constexpr Unexpected< Error > unexpected_error(ErrorCode code, const char *context=nullptr)
Constructs an unexpected Error value from a machine-readable code.
Definition error.hpp:293
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
Expected< void, Error > Status
Expected-returning convenience alias for Purify status-only APIs.
Definition error.hpp:102
std::vector< unsigned char > Bytes
Dynamically sized byte string used for messages, serialized witnesses, and proofs.
Definition common.hpp:99
Experimental BPPP-backed PureSign proof(R) helpers.
Narrow C ABI exposing secp256k1 scalar and HMAC helpers to the C++ headers.
int purify_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< NonceProof > deserialize(std::span< const unsigned char > serialized, purify_secp_context *secp_context)
Definition bppp.cpp:175
Result< Bytes > serialize(purify_secp_context *secp_context) const
Definition bppp.cpp:148
bppp::ScalarBytes public_commitment_dlog_response
Definition bppp.hpp:306
static constexpr std::size_t kNormProofSize
Definition bppp.hpp:297
static constexpr unsigned char kSerializationVersion
Definition bppp.hpp:296
static constexpr std::size_t kSerializedSize
Definition bppp.hpp:298
Public BIP340 nonce point in x-only form.
Definition bppp.hpp:220
static Result< Nonce > deserialize(std::span< const unsigned char > serialized, purify_secp_context *secp_context)
Parses a serialized x-only nonce.
Definition bppp.cpp:102
Bytes serialize() const
Serializes this x-only nonce into its fixed-size wire format.
Definition bppp.cpp:98
static constexpr std::size_t kSerializedSize
Definition bppp.hpp:221
Result< Bytes > serialize(purify_secp_context *secp_context) const
Definition bppp.cpp:215
static constexpr unsigned char kSerializationVersion
Definition bppp.hpp:315
static Result< ProvenSignature > deserialize(std::span< const unsigned char > serialized, purify_secp_context *secp_context)
Definition bppp.cpp:227
static constexpr std::size_t kSerializedSize
Definition bppp.hpp:316
static Result< PublicKey > deserialize(std::span< const unsigned char > serialized, purify_secp_context *secp_context)
Parses a serialized PureSign++ public-key bundle.
Definition bppp.cpp:80
Bytes serialize() const
Serializes this PureSign++ public-key bundle into its fixed-size wire format.
Definition bppp.cpp:71
static constexpr std::size_t kSerializedSize
Definition bppp.hpp:73
Standard 64-byte BIP340 signature.
Definition bppp.hpp:233
Bytes serialize() const
Serializes this signature into its fixed-size wire format.
Definition bppp.cpp:129
static constexpr std::size_t kSerializedSize
Definition bppp.hpp:234
Scalar32 s() const
Returns the 32-byte Schnorr s scalar encoded in the last 32 signature bytes.
Definition bppp.cpp:123
Nonce nonce() const
Returns the x-only public nonce encoded in the first 32 signature bytes.
Definition bppp.cpp:117
static Result< Signature > deserialize(std::span< const unsigned char > serialized, purify_secp_context *secp_context)
Parses a serialized BIP340 signature.
Definition bppp.cpp:133