purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
secret.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
9
10#pragma once
11
12#include <cstddef>
13#include <memory>
14#include <string_view>
15#include <type_traits>
16#include <utility>
17
18#include "purify/curve.hpp"
19
20namespace purify::detail {
21
22inline void secure_clear_bytes(void* data, std::size_t size) noexcept {
23 volatile unsigned char* out = reinterpret_cast<volatile unsigned char*>(data);
24 while (size != 0) {
25 *out = 0;
26 ++out;
27 --size;
28 }
29}
30
39template <typename T>
41public:
42 using value_type = T;
43 using is_always_equal = std::true_type;
45
46 SecureAllocator() noexcept = default;
47
48 template <typename U>
49 SecureAllocator(const SecureAllocator<U>&) noexcept {}
50
51 [[nodiscard]] T* allocate(std::size_t count) {
52 return std::allocator<T>{}.allocate(count);
53 }
54
55 void deallocate(T* data, std::size_t count) noexcept {
56 if (data == nullptr) {
57 return;
58 }
59 secure_clear_bytes(data, count * sizeof(T));
60 std::allocator<T>{}.deallocate(data, count);
61 }
62
63 template <typename U>
64 struct rebind {
66 };
67};
68
69template <typename T, typename U>
70bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) noexcept {
71 return true;
72}
73
75 void operator()(UInt512* value) const noexcept {
76 if (value == nullptr) {
77 return;
78 }
79 static_assert(std::is_trivially_destructible_v<UInt512>);
80 secure_clear_bytes(value, sizeof(UInt512));
81 delete value;
82 }
83};
84
85} // namespace purify::detail
86
87namespace purify {
88
95class SecretKey {
96public:
97 SecretKey() = delete;
98 SecretKey(const SecretKey&) = delete;
99 SecretKey& operator=(const SecretKey&) = delete;
100 SecretKey(SecretKey&&) noexcept = default;
101 SecretKey& operator=(SecretKey&&) noexcept = default;
102 ~SecretKey() = default;
103
109 [[nodiscard]] static Result<SecretKey> from_packed(const UInt512& packed) {
110 PURIFY_RETURN_IF_ERROR(validate_secret_key(packed), "SecretKey::from_packed:validate_secret_key");
111 return SecretKey(std::unique_ptr<UInt512, detail::SecureUInt512Deleter>(new UInt512(packed)));
112 }
113
119 [[nodiscard]] static Result<SecretKey> from_hex(std::string_view hex) {
120 PURIFY_ASSIGN_OR_RETURN(const auto& packed, UInt512::try_from_hex(hex), "SecretKey::from_hex:try_from_hex");
121 return from_packed(packed);
122 }
123
128 [[nodiscard]] Result<SecretKey> clone() const {
129 return from_packed(packed());
130 }
131
136 [[nodiscard]] const UInt512& packed() const noexcept {
137 return *packed_;
138 }
139
145 [[nodiscard]] bool operator==(const SecretKey& other) const noexcept {
146 return packed() == other.packed();
147 }
148
149private:
150 explicit SecretKey(std::unique_ptr<UInt512, detail::SecureUInt512Deleter>&& packed) noexcept
151 : packed_(std::move(packed)) {}
152
153 std::unique_ptr<UInt512, detail::SecureUInt512Deleter> packed_;
154};
155
156} // namespace purify
SecretKey(const SecretKey &)=delete
static Result< SecretKey > from_hex(std::string_view hex)
Parses and validates a packed Purify secret from hexadecimal text.
Definition secret.hpp:119
SecretKey & operator=(const SecretKey &)=delete
const UInt512 & packed() const noexcept
Exposes the packed secret for lower-level cryptographic operations.
Definition secret.hpp:136
SecretKey(SecretKey &&) noexcept=default
static Result< SecretKey > from_packed(const UInt512 &packed)
Constructs a validated secret key from packed Purify secret bytes.
Definition secret.hpp:109
Result< SecretKey > clone() const
Creates a second owned copy of this secret key.
Definition secret.hpp:128
bool operator==(const SecretKey &other) const noexcept
Compares two owned secrets by their packed values.
Definition secret.hpp:145
Allocator that clears an allocation immediately before releasing it.
Definition secret.hpp:40
T * allocate(std::size_t count)
Definition secret.hpp:51
std::true_type is_always_equal
Definition secret.hpp:43
void deallocate(T *data, std::size_t count) noexcept
Definition secret.hpp:55
std::true_type propagate_on_container_move_assignment
Definition secret.hpp:44
SecureAllocator() noexcept=default
Elliptic-curve helpers, fixed parameters, and hash-to-curve utilities for Purify.
#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
void secure_clear_bytes(void *data, std::size_t size) noexcept
Definition secret.hpp:22
bool operator==(const SecureAllocator< T > &, const SecureAllocator< U > &) noexcept
Definition secret.hpp:70
Status validate_secret_key(const UInt512 &z)
Validates the packed secret-key encoding range.
Definition curve.cpp:312
BigUInt< 8 > UInt512
512-bit unsigned integer used for private and packed public keys.
Definition numeric.hpp:821
Expected< T, Error > Result
Expected-returning convenience alias for Purify value-producing APIs.
Definition error.hpp:99
static Result< BigUInt > try_from_hex(std::string_view hex)
Definition numeric.hpp:252
void operator()(UInt512 *value) const noexcept
Definition secret.hpp:75