9#include <botan/internal/sp_wots.h>
11#include <botan/mem_ops.h>
12#include <botan/internal/loadstor.h>
13#include <botan/internal/sp_hash.h>
24void base_2_b(std::span<WotsHashIndex> output, std::span<const uint8_t> input,
const Sphincs_Parameters& params) {
27 size_t input_offset = 0;
28 uint8_t current_byte = 0;
29 uint32_t remaining_bits_in_current_byte = 0;
31 for(
auto& out : output) {
32 if(remaining_bits_in_current_byte == 0) {
33 current_byte = input[input_offset];
35 remaining_bits_in_current_byte = 8;
37 remaining_bits_in_current_byte -= params.log_w();
38 out =
WotsHashIndex((current_byte >> remaining_bits_in_current_byte) & (params.w() - 1));
46void wots_checksum(std::span<WotsHashIndex> output,
47 std::span<const WotsHashIndex> msg_base_w,
52 for(
auto wots_hash_index : msg_base_w) {
53 csum += params.w() - 1 - wots_hash_index.get();
57 csum = csum << ((8 - ((params.wots_len_2() * params.log_w()) % 8)) % 8);
59 std::array<uint8_t, 4> csum_bytes{};
62 const size_t csum_bytes_size = params.wots_checksum_bytes();
64 base_2_b(output, std::span(csum_bytes).last(csum_bytes_size), params);
72 const auto msg_base_w = std::span(result).first(params.
wots_len_1());
73 const auto checksum_base_w = std::span(result).last(params.
wots_len_2());
75 base_2_b(msg_base_w, msg.
get(), params);
76 wots_checksum(checksum_base_w, msg_base_w, params);
86 const std::vector<WotsHashIndex> lengths =
chain_lengths(hashed_message, params);
87 const size_t len = params.
wots_len();
88 const size_t n = params.
n();
92 std::copy(signature.
begin(), signature.
end(), pk_buffer.
begin());
93 const std::span<uint8_t> pk_bytes(pk_buffer.
get());
95 std::vector<Sphincs_Address> chain_addrs(len, address);
96 for(
size_t i = 0; i != len; ++i) {
97 chain_addrs[i].set_chain_address(
WotsChainIndex(
static_cast<uint32_t
>(i)));
100 std::vector<Sphincs_Address> addrs;
101 std::vector<std::span<uint8_t>> outs;
102 std::vector<std::span<const uint8_t>> ins;
114 for(
size_t i = 0; i != len; ++i) {
115 if(lengths[i] <= k) {
116 chain_addrs[i].set_hash_address(k);
117 addrs.push_back(chain_addrs[i]);
118 const auto node = pk_bytes.subspan(i * n, n);
119 outs.push_back(node);
124 hashes.
T_batch(outs, addrs, ins);
131 std::span<uint8_t> leaves_out,
135 std::optional<TreeNodeIndex> sign_leaf_idx,
136 const std::vector<WotsHashIndex>& wots_steps,
146 const size_t len = params.
wots_len();
147 const size_t n = params.
n();
148 const size_t lanes = leaf_count * len;
157 std::vector<Sphincs_Address> addrs;
158 addrs.reserve(lanes);
159 std::vector<std::span<uint8_t>> nodes(lanes);
160 std::vector<std::span<const uint8_t>> cnodes(lanes);
161 for(
size_t j = 0; j != leaf_count; ++j) {
163 for(
size_t i = 0; i != len; ++i) {
164 addrs.push_back(leaf_addr);
165 addrs.back().set_chain_address(
WotsChainIndex(
static_cast<uint32_t
>(i)));
166 const auto node = std::span(pk_buffers).subspan((j * len + i) * n, n);
167 nodes[j * len + i] = node;
168 cnodes[j * len + i] = node;
173 hashes.
PRF_batch(nodes, secret_seed, addrs);
175 for(
auto& addr : addrs) {
179 const bool signing_in_batch = sign_leaf_idx.has_value() && sign_leaf_idx.value() >= first_leaf_idx &&
180 sign_leaf_idx.value() < first_leaf_idx +
static_cast<uint32_t
>(leaf_count);
181 const size_t sign_j = signing_in_batch ?
static_cast<size_t>(sign_leaf_idx.value().get() - first_leaf_idx.
get()) : 0;
186 if(signing_in_batch) {
187 for(
size_t i = 0; i != len; ++i) {
188 if(wots_steps[i] == k) {
189 copy_mem(sig_out.
get().subspan(i * n, n), nodes[sign_j * len + i]);
195 if(k == params.
w() - 1) {
199 for(
auto& addr : addrs) {
200 addr.set_hash_address(k);
203 hashes.
T_batch(nodes, addrs, cnodes);
207 std::vector<Sphincs_Address> pk_addrs;
208 pk_addrs.reserve(leaf_count);
209 std::vector<std::span<uint8_t>> leaf_outs(leaf_count);
210 std::vector<std::span<const uint8_t>> pk_spans(leaf_count);
211 for(
size_t j = 0; j != leaf_count; ++j) {
213 pk_addrs.push_back(pk_addr);
214 leaf_outs[j] = leaves_out.subspan(j * n, n);
217 hashes.
T_batch(leaf_outs, pk_addrs, pk_spans);
#define BOTAN_ASSERT_NOMSG(expr)
Sphincs_Address_Type get_type() const
Sphincs_Address & set_hash_address(WotsHashIndex hash)
Sphincs_Address & set_keypair_address(TreeNodeIndex keypair)
Sphincs_Address & set_type(Sphincs_Address_Type type)
void T_batch(std::span< std::span< uint8_t > > outputs, std::span< const Sphincs_Address > addresses, std::span< std::span< const uint8_t > > inputs)
void PRF_batch(std::span< std::span< uint8_t > > outputs, const SphincsSecretSeed &sk_seed, std::span< const Sphincs_Address > addresses)
uint32_t wots_bytes() const
uint32_t wots_len_2() const
uint32_t wots_len_1() const
uint32_t wots_len() const
decltype(auto) begin() noexcept(noexcept(this->m_span.begin()))
underlying_span get() const
decltype(auto) end() noexcept(noexcept(this->m_span.end()))
decltype(auto) begin() noexcept(noexcept(this->get().begin()))
Strong< std::vector< uint8_t >, struct SphincsTreeNode_ > SphincsTreeNode
Either an XMSS or FORS tree node or leaf.
@ WotsPublicKeyCompression
Strong< uint8_t, struct WotsHashIndex_, EnableArithmeticWithPlainNumber > WotsHashIndex
Index of a hash application inside a single WOTS chain (integers in "base_w").
constexpr void copy_mem(T *out, const T *in, size_t n)
WotsPublicKey wots_public_key_from_signature(const SphincsTreeNode &hashed_message, StrongSpan< const WotsSignature > signature, const Sphincs_Address &address, const Sphincs_Parameters ¶ms, Sphincs_Hash_Functions &hashes)
FIPS 205, Algorithm 8: wots_pkFromSig.
Strong< std::vector< uint8_t >, struct WotsPublicKey_ > WotsPublicKey
Strong< uint32_t, struct WotsChainIndex_ > WotsChainIndex
Index of a WOTS chain within a single usage of WOTS.
Strong< secure_vector< uint8_t >, struct SphincsSecretSeed_ > SphincsSecretSeed
Strong< uint32_t, struct TreeNodeIndex_, EnableArithmeticWithPlainNumber > TreeNodeIndex
Index of an individual node inside an XMSS or FORS tree.
std::vector< T, secure_allocator< T > > secure_vector
void wots_sign_and_pkgen(StrongSpan< WotsSignature > sig_out, std::span< uint8_t > leaves_out, const SphincsSecretSeed &secret_seed, TreeNodeIndex first_leaf_idx, size_t leaf_count, std::optional< TreeNodeIndex > sign_leaf_idx, const std::vector< WotsHashIndex > &wots_steps, Sphincs_Address &leaf_addr, Sphincs_Address &pk_addr, const Sphincs_Parameters ¶ms, Sphincs_Hash_Functions &hashes)
FIPS 205, Algorithm 6 and 7: wots_pkGen and wots_sign.
std::vector< WotsHashIndex > chain_lengths(const SphincsTreeNode &msg, const Sphincs_Parameters ¶ms)
constexpr auto store_be(ParamTs &&... params)