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
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
32 void operator()(UInt512* value) const noexcept {
33 if (value == nullptr) {
34 return;
35 }
36 static_assert(std::is_trivially_destructible_v<UInt512>);
37 secure_clear_bytes(value, sizeof(UInt512));
38 delete value;
39 }
40};
41
42} // namespace purify::detail
43
44namespace purify {
45
52class SecretKey {
53public:
54 SecretKey() = delete;
55 SecretKey(const SecretKey&) = delete;
56 SecretKey& operator=(const SecretKey&) = delete;
57 SecretKey(SecretKey&&) noexcept = default;
58 SecretKey& operator=(SecretKey&&) noexcept = default;
59 ~SecretKey() = default;
60
66 [[nodiscard]] static Result<SecretKey> from_packed(const UInt512& packed) {
67 PURIFY_RETURN_IF_ERROR(validate_secret_key(packed), "SecretKey::from_packed:validate_secret_key");
68 return SecretKey(std::unique_ptr<UInt512, detail::SecureUInt512Deleter>(new UInt512(packed)));
69 }
70
76 [[nodiscard]] static Result<SecretKey> from_hex(std::string_view hex) {
77 PURIFY_ASSIGN_OR_RETURN(const auto& packed, UInt512::try_from_hex(hex), "SecretKey::from_hex:try_from_hex");
78 return from_packed(packed);
79 }
80
85 [[nodiscard]] Result<SecretKey> clone() const {
86 return from_packed(packed());
87 }
88
93 [[nodiscard]] const UInt512& packed() const noexcept {
94 return *packed_;
95 }
96
102 [[nodiscard]] bool operator==(const SecretKey& other) const noexcept {
103 return packed() == other.packed();
104 }
105
106private:
107 explicit SecretKey(std::unique_ptr<UInt512, detail::SecureUInt512Deleter>&& packed) noexcept
108 : packed_(std::move(packed)) {}
109
110 std::unique_ptr<UInt512, detail::SecureUInt512Deleter> packed_;
111};
112
113} // namespace purify
Purify result carrier that either holds a value or an error.
Definition expected.hpp:64
Move-only packed Purify secret stored in dedicated heap memory.
Definition secret.hpp:52
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:76
SecretKey & operator=(const SecretKey &)=delete
const UInt512 & packed() const noexcept
Exposes the packed secret for lower-level cryptographic operations.
Definition secret.hpp:93
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:66
Result< SecretKey > clone() const
Creates a second owned copy of this secret key.
Definition secret.hpp:85
bool operator==(const SecretKey &other) const noexcept
Compares two owned secrets by their packed values.
Definition secret.hpp:102
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
Definition api.hpp:21
Status validate_secret_key(const UInt512 &z)
Validates the packed secret-key encoding range.
Definition curve.cpp:307
BigUInt< 8 > UInt512
512-bit unsigned integer used for private and packed public keys.
Definition numeric.hpp:802
static Result< BigUInt > try_from_hex(std::string_view hex)
Parses a hexadecimal string, ignoring optional 0x and whitespace.
Definition numeric.hpp:249
void operator()(UInt512 *value) const noexcept
Definition secret.hpp:32