Botan 3.11.0
Crypto and TLS for C&
sp_address.h
Go to the documentation of this file.
1/*
2 * SLH-DSA Address
3 * (C) 2023 Jack Lloyd
4 * 2023 Fabian Albert, René Meusel, Amos Treiber - Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9#ifndef BOTAN_SPHINCS_PLUS_ADDRESS_H_
10#define BOTAN_SPHINCS_PLUS_ADDRESS_H_
11
12#include <botan/internal/loadstor.h>
13#include <botan/internal/sp_types.h>
14#include <array>
15
16namespace Botan {
17
27
28/**
29 * Representation of a SLH-DSA hash function address as specified in
30 * FIPS 205, Section 4.2
31 */
33 private:
34 // Offsets of the address fields in the address array. Counted in 32-bit words.
35 static constexpr size_t layer_offset = 0;
36 static constexpr size_t tree_offset = 1; // tree address is 3 words wide
37 static constexpr size_t type_offset = 4;
38 static constexpr size_t keypair_offset = 5;
39 static constexpr size_t chain_offset = 6;
40 static constexpr size_t hash_offset = 7;
41 static constexpr size_t tree_height_offset = chain_offset;
42 static constexpr size_t tree_index_offset = hash_offset;
43
44 public:
45 using enum Sphincs_Address_Type;
46
47 explicit Sphincs_Address(Sphincs_Address_Type type) : m_address{} { set_type(type); }
48
49 explicit Sphincs_Address(std::array<uint32_t, 8> address) : m_address{} {
50 std::copy(address.begin(), address.end(), m_address.begin());
51 }
52
53 /* Setter member functions as specified in FIPS 205, Section 4.3 */
54
56 m_address[layer_offset] = layer.get();
57 return *this;
58 }
59
61 m_address[tree_offset + 0] = 0; // not required by all current instances
62 m_address[tree_offset + 1] = static_cast<uint32_t>(tree.get() >> 32);
63 m_address[tree_offset + 2] = static_cast<uint32_t>(tree.get());
64 return *this;
65 }
66
67 /*
68 * Sets the type without clearing the other fields (contrary to the specs setTypeAndClear).
69 * This adaption is used for optimization purposes.
70 */
72 m_address[type_offset] = static_cast<uint32_t>(type);
73 return *this;
74 }
75
77 m_address[keypair_offset] = keypair.get();
78 return *this;
79 }
80
82 m_address[chain_offset] = chain.get();
83 return *this;
84 }
85
87 m_address[tree_height_offset] = tree_height.get();
88 return *this;
89 }
90
92 m_address[hash_offset] = hash.get();
93 return *this;
94 }
95
97 m_address[tree_index_offset] = tree_index.get();
98 return *this;
99 }
100
101 /* Custom helper member functions */
102
104 m_address[layer_offset] = other.m_address[layer_offset];
105 m_address[tree_offset + 0] = other.m_address[tree_offset + 0];
106 m_address[tree_offset + 1] = other.m_address[tree_offset + 1];
107 m_address[tree_offset + 2] = other.m_address[tree_offset + 2];
108
109 return *this;
110 }
111
113 auto result = Sphincs_Address({0, 0, 0, 0, 0, 0, 0, 0});
114 result.copy_subtree_from(other);
115 return result;
116 }
117
119 m_address[layer_offset] = other.m_address[layer_offset];
120 m_address[tree_offset + 0] = other.m_address[tree_offset + 0];
121 m_address[tree_offset + 1] = other.m_address[tree_offset + 1];
122 m_address[tree_offset + 2] = other.m_address[tree_offset + 2];
123 m_address[keypair_offset] = other.m_address[keypair_offset];
124
125 return *this;
126 }
127
129 Sphincs_Address result({0, 0, 0, 0, 0, 0, 0, 0});
130 result.copy_keypair_from(other);
131 return result;
132 }
133
134 Sphincs_Address_Type get_type() const { return Sphincs_Address_Type(m_address[type_offset]); }
135
136 std::array<uint8_t, 32> to_bytes() const {
137 std::array<uint8_t, sizeof(m_address)> result{};
138 for(unsigned int i = 0; i < m_address.size(); ++i) {
139 store_be(m_address[i], result.data() + (i * 4));
140 }
141 return result;
142 }
143
144 std::array<uint8_t, 22> to_bytes_compressed() const {
145 std::array<uint8_t, 22> result{};
146
147 result[0] = static_cast<uint8_t>(m_address[layer_offset]);
148 store_be(m_address[tree_offset + 1], &result[1]);
149 store_be(m_address[tree_offset + 2], &result[5]);
150 result[9] = static_cast<uint8_t>(m_address[type_offset]);
151 store_be(m_address[keypair_offset], &result[10]);
152 store_be(m_address[chain_offset], &result[14]);
153 store_be(m_address[hash_offset], &result[18]);
154
155 return result;
156 }
157
158 private:
159 std::array<uint32_t, 8> m_address;
160};
161
162} // namespace Botan
163
164#endif
#define BOTAN_TEST_API
Definition api.h:41
Sphincs_Address_Type get_type() const
Definition sp_address.h:134
Sphincs_Address & set_layer_address(HypertreeLayerIndex layer)
Definition sp_address.h:55
Sphincs_Address & set_tree_address(XmssTreeIndexInLayer tree)
Definition sp_address.h:60
Sphincs_Address & set_chain_address(WotsChainIndex chain)
Definition sp_address.h:81
Sphincs_Address & copy_subtree_from(const Sphincs_Address &other)
Definition sp_address.h:103
std::array< uint8_t, 22 > to_bytes_compressed() const
Definition sp_address.h:144
Sphincs_Address & set_hash_address(WotsHashIndex hash)
Definition sp_address.h:91
Sphincs_Address & set_tree_height(TreeLayerIndex tree_height)
Definition sp_address.h:86
Sphincs_Address & set_tree_index(TreeNodeIndex tree_index)
Definition sp_address.h:96
static Sphincs_Address as_subtree_from(const Sphincs_Address &other)
Definition sp_address.h:112
Sphincs_Address & set_keypair_address(TreeNodeIndex keypair)
Definition sp_address.h:76
Sphincs_Address(Sphincs_Address_Type type)
Definition sp_address.h:47
Sphincs_Address & copy_keypair_from(const Sphincs_Address other)
Definition sp_address.h:118
Sphincs_Address(std::array< uint32_t, 8 > address)
Definition sp_address.h:49
Sphincs_Address & set_type(Sphincs_Address_Type type)
Definition sp_address.h:71
static Sphincs_Address as_keypair_from(const Sphincs_Address &other)
Definition sp_address.h:128
std::array< uint8_t, 32 > to_bytes() const
Definition sp_address.h:136
constexpr T & get() &
Definition strong_type.h:85
Sphincs_Address_Type
Definition sp_address.h:18
Strong< uint64_t, struct XmssTreeIndexInLayer_, EnableArithmeticWithPlainNumber > XmssTreeIndexInLayer
Index of an XMSS tree (unique for just the local hyper-tree layer).
Definition sp_types.h:89
Strong< uint8_t, struct WotsHashIndex_, EnableArithmeticWithPlainNumber > WotsHashIndex
Index of a hash application inside a single WOTS chain (integers in "base_w").
Definition sp_types.h:98
Strong< uint32_t, struct WotsChainIndex_ > WotsChainIndex
Index of a WOTS chain within a single usage of WOTS.
Definition sp_types.h:95
Strong< uint32_t, struct TreeNodeIndex_, EnableArithmeticWithPlainNumber > TreeNodeIndex
Index of an individual node inside an XMSS or FORS tree.
Definition sp_types.h:92
Strong< uint32_t, struct TreeLayerIndex_, EnableArithmeticWithPlainNumber > TreeLayerIndex
Index of the layer within a FORS/XMSS tree.
Definition sp_types.h:83
Strong< uint32_t, struct HypertreeLayerIndex_ > HypertreeLayerIndex
Index of a layer in the XMSS hyper-tree.
Definition sp_types.h:86
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745