purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
sha256_transcript.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
6
7#include <algorithm>
8#include <array>
9#include <exception>
10#include <string_view>
11#include <utility>
12
13#include "purify/secp_bridge.h"
14
16namespace {
17
18constexpr std::uint32_t kFieldItem = 0x4649'454c; // FIEL
19constexpr std::uint32_t kScalarItem = 0x5343'414c; // SCAL
20constexpr std::uint32_t kPointItem = 0x504f'494e; // POIN
21constexpr std::uint32_t kPointPairItem = 0x5041'4952; // PAIR
22constexpr std::uint32_t kBytesItem = 0x4259'5445; // BYTE
23constexpr std::uint32_t kU64Item = 0x5536'3420; // U64<space>
24constexpr std::uint32_t kChallengeItem = 0x4348'414c; // CHAL
25constexpr std::uint32_t kOpeningBaseItem = 0x4f42'4153; // OBAS
26
27constexpr std::string_view kSuiteId = "Purify/SHA256Transcript/v1";
28constexpr std::size_t kHeaderSize = 12;
29
30std::array<unsigned char, kHeaderSize> header(std::uint32_t item_type,
31 std::uint64_t auxiliary) {
32 std::array<unsigned char, kHeaderSize> out{};
33 for (std::size_t index = 0; index < 4; ++index) {
34 out[index] = static_cast<unsigned char>(item_type >> (24 - 8 * index));
35 }
36 for (std::size_t index = 0; index < 8; ++index) {
37 out[4 + index] =
38 static_cast<unsigned char>(auxiliary >> (56 - 8 * index));
39 }
40 return out;
41}
42
43TranscriptDigest sha256_many(
44 std::span<const std::span<const unsigned char>> segments) {
45 TranscriptDigest out{};
46 std::array<const unsigned char*, 5> items{};
47 std::array<std::size_t, 5> lengths{};
48 if (segments.size() > items.size()) std::terminate();
49 for (std::size_t index = 0; index < segments.size(); ++index) {
50 items[index] = segments[index].empty() ? nullptr : segments[index].data();
51 lengths[index] = segments[index].size();
52 }
53 if (purify_sha256_many(out.data(), items.data(), lengths.data(),
54 segments.size()) == 0) {
55 std::terminate();
56 }
57 return out;
58}
59
60Result<Sha256Challenge> make_challenge(const TranscriptDigest& digest) {
61 auto scalar_bytes = digest;
62 scalar_bytes[0] &= 0x7f;
63 PURIFY_ASSIGN_OR_RETURN(auto scalar,
65 "sha256_transcript:scalar");
66 if (scalar.is_zero()) {
68 "sha256_transcript:zero_challenge");
69 }
70 return Sha256Challenge{digest, std::move(scalar)};
71}
72
73} // namespace
74
76 purify_sha256(state_.data(),
77 reinterpret_cast<const unsigned char*>(kSuiteId.data()),
78 kSuiteId.size());
79}
80
81void Sha256Transcript::append_header(std::uint32_t item_type,
82 std::uint64_t auxiliary) {
83 const auto encoded = header(item_type, auxiliary);
84 append_payload(encoded);
85}
86
87void Sha256Transcript::append_payload(
88 std::span<const unsigned char> payload) {
89 best_effort_reserve_add(pending_, pending_.size(), payload.size());
90 pending_.insert(pending_.end(), payload.begin(), payload.end());
91}
92
94 append_header(kFieldItem, 0);
95 append_payload(value);
96}
97
99 append_header(kScalarItem, 0);
100 const auto encoded = value.to_bytes_be();
101 append_payload(encoded);
102}
103
105 const TranscriptBytes32& x) {
106 if (prefix != 0 && prefix != 2 && prefix != 3) {
108 "sha256_transcript:point_prefix");
109 }
110 if (prefix == 0 &&
111 std::any_of(x.begin(), x.end(), [](unsigned char byte) {
112 return byte != 0;
113 })) {
115 "sha256_transcript:point_identity");
116 }
117 append_header(kPointItem, prefix);
118 append_payload(x);
119 return {};
120}
121
123 unsigned char parity, const TranscriptBytes32& first_x,
124 const TranscriptBytes32& second_x) {
125 if (parity > 3) {
127 "sha256_transcript:pair_parity");
128 }
129 append_header(kPointPairItem, parity);
130 append_payload(first_x);
131 append_payload(second_x);
132 return {};
133}
134
135Status Sha256Transcript::append_bytes(std::span<const unsigned char> bytes) {
136 if (!size_fits_u64(bytes.size())) {
138 "sha256_transcript:byte_length");
139 }
140 append_header(kBytesItem, static_cast<std::uint64_t>(bytes.size()));
141 append_payload(bytes);
142 return {};
143}
144
145void Sha256Transcript::append_u64(std::uint64_t value) {
146 append_header(kU64Item, value);
147}
148
150 const auto challenge_header = header(kChallengeItem, round);
151 const std::array<std::span<const unsigned char>, 3> segments{
152 state_, pending_, challenge_header};
153 const auto digest = sha256_many(segments);
154 PURIFY_ASSIGN_OR_RETURN(auto challenge, make_challenge(digest),
155 "sha256_transcript:challenge");
156 state_ = digest;
157 pending_.clear();
158 return challenge;
159}
160
162 const TranscriptDigest& basis_digest, std::uint64_t role) const {
163 const auto bytes_header = header(kBytesItem, basis_digest.size());
164 const auto opening_header = header(kOpeningBaseItem, role);
165 const std::array<std::span<const unsigned char>, 5> segments{
166 state_, pending_, bytes_header, basis_digest, opening_header};
167 return make_challenge(sha256_many(segments));
168}
169
170} // namespace purify::bppp::candidate
Field element modulo the backend scalar field used by this implementation.
Definition numeric.hpp:834
static Result< FieldElement > try_from_bytes32(const std::array< unsigned char, 32 > &bytes)
Decodes a canonical 32-byte big-endian field element.
Definition numeric.cpp:53
std::array< unsigned char, 32 > to_bytes_be() const
Serializes the field element in big-endian form.
Definition numeric.cpp:84
void append_scalar(const FieldElement &value)
Result< Sha256Challenge > challenge(std::uint64_t round)
Status append_point(unsigned char prefix, const TranscriptBytes32 &x)
Status append_bytes(std::span< const unsigned char > bytes)
void append_field(const TranscriptBytes32 &value)
Status append_point_pair(unsigned char parity, const TranscriptBytes32 &first_x, const TranscriptBytes32 &second_x)
Result< Sha256Challenge > opening_base_scalar(const TranscriptDigest &basis_digest, std::uint64_t role) const
#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
std::array< unsigned char, 32 > TranscriptBytes32
TranscriptBytes32 TranscriptDigest
ScalarBytes scalar_bytes(const FieldElement &value)
Serializes a Purify field element into the scalar encoding expected by the BPPP bridge.
Definition bppp.hpp:78
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 size_fits_u64(std::size_t value) noexcept
Definition common.hpp:83
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
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
Narrow C ABI exposing secp256k1 scalar and HMAC helpers to the C++ headers.
int purify_sha256_many(unsigned char output32[32], const unsigned char *const *items, const size_t *item_lens, size_t items_count)
Computes SHA-256 over a set of byte strings.
void purify_sha256(unsigned char output32[32], const unsigned char *data, size_t data_len)
Computes SHA-256 over a byte string.