purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
common.hpp
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
5#pragma once
6
7#include <cstddef>
8#include <cstdint>
9#include <optional>
10#include <span>
11#include <string_view>
12
14
15namespace purify::detail {
16
17inline Bytes copy_bytes(std::span<const unsigned char> input) {
18 return Bytes(input.begin(), input.end());
19}
20
21inline Bytes tagged_eval_input(std::string_view tag, std::span<const unsigned char> input) {
22 Bytes out;
23 best_effort_reserve_add(out, tag.size(), input.size());
24 out.insert(out.end(), tag.begin(), tag.end());
25 out.insert(out.end(), input.begin(), input.end());
26 return out;
27}
28
29inline void append_u32_le(Bytes& out, std::uint32_t value) {
30 for (int i = 0; i < 4; ++i) {
31 out.push_back(static_cast<unsigned char>((value >> (8 * i)) & 0xffU));
32 }
33}
34
35inline std::optional<std::uint32_t> read_u32_le(std::span<const unsigned char> bytes,
36 std::size_t offset) {
37 std::uint32_t value = 0;
38 if (offset > bytes.size() || bytes.size() - offset < 4) {
39 return std::nullopt;
40 }
41 for (int i = 0; i < 4; ++i) {
42 value |= static_cast<std::uint32_t>(bytes[offset + i]) << (8 * i);
43 }
44 return value;
45}
46
47inline std::size_t circuit_n_gates(const NativeBulletproofCircuit& circuit) {
48 return circuit.n_gates;
49}
50
52 return circuit.n_gates();
53}
54
55inline std::size_t circuit_n_commitments(const NativeBulletproofCircuit& circuit) {
56 return circuit.n_commitments;
57}
58
60 return circuit.n_commitments();
61}
62
63template <typename CircuitLike>
64Status validate_proof_cache_circuit(const CircuitLike& circuit, const char* context) {
65 if (!circuit.has_valid_shape()) {
67 }
70 }
71 if (circuit_n_commitments(circuit) != 1) {
73 }
74 return {};
75}
76
77template <typename CacheLike>
78inline Status validate_message_proof_cache(const CacheLike& cache,
79 std::string_view nonce_tag) {
80 if (cache.eval_input != tagged_eval_input(nonce_tag, cache.message)) {
81 return unexpected_error(ErrorCode::BindingMismatch, "validate_message_proof_cache:eval_input");
82 }
83 PURIFY_ASSIGN_OR_RETURN(const auto& template_digest, cache.circuit_template.integrity_digest(),
84 "validate_message_proof_cache:integrity_digest");
85 if (cache.template_digest != template_digest) {
86 return unexpected_error(ErrorCode::BindingMismatch, "validate_message_proof_cache:circuit_template");
87 }
88 return {};
89}
90
91template <typename CacheLike>
92inline Status validate_topic_proof_cache(const CacheLike& cache,
93 std::string_view nonce_tag) {
94 if (cache.topic.empty()) {
95 return unexpected_error(ErrorCode::EmptyInput, "validate_topic_proof_cache:empty_topic");
96 }
97 if (cache.eval_input != tagged_eval_input(nonce_tag, cache.topic)) {
98 return unexpected_error(ErrorCode::BindingMismatch, "validate_topic_proof_cache:eval_input");
99 }
100 PURIFY_ASSIGN_OR_RETURN(const auto& template_digest, cache.circuit_template.integrity_digest(),
101 "validate_topic_proof_cache:integrity_digest");
102 if (cache.template_digest != template_digest) {
103 return unexpected_error(ErrorCode::BindingMismatch, "validate_topic_proof_cache:circuit_template");
104 }
105 return {};
106}
107
108} // namespace purify::detail
Purify result carrier that either holds a value or an error.
Definition expected.hpp:64
Resettable packed circuit representation backed by one aligned owning slab.
#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
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 append_u32_le(Bytes &out, std::uint32_t value)
Definition common.hpp:29
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
std::size_t circuit_n_gates(const NativeBulletproofCircuit &circuit)
Definition common.hpp:47
std::size_t circuit_n_commitments(const NativeBulletproofCircuit &circuit)
Definition common.hpp:55
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
bool is_power_of_two_size(std::size_t value) noexcept
Definition common.hpp:94
std::vector< unsigned char > Bytes
Dynamically sized byte string used for messages, serialized witnesses, and proofs.
Definition common.hpp:99
void best_effort_reserve_add(std::vector< T > &out, std::size_t lhs, std::size_t rhs)
Reserve capacity when the size arithmetic fits, otherwise skip the hint.
Definition common.hpp:107
Native in-memory representation of a Bulletproof-style arithmetic circuit.