Botan 3.10.0
Crypto and TLS for C&
sponge.h
Go to the documentation of this file.
1/*
2* Base helper class for implementing sponge constructions like Keccak or Ascon
3* (C) 2025 Jack Lloyd
4* 2025 René Meusel
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#ifndef BOTAN_SPONGE_CONSTRUCTION_H_
10#define BOTAN_SPONGE_CONSTRUCTION_H_
11
12#include <botan/exceptn.h>
13#include <array>
14
15namespace Botan {
16
17/**
18 * A generic sponge construction with a fixed state size defined in terms of
19 * "words" of an unsigned integral type.
20 *
21 * This is meant to be used as a base class for specific sponge constructions
22 * like Keccak or Ascon.
23 */
24template <size_t words, std::unsigned_integral word = uint64_t>
25class Sponge {
26 public:
27 using word_t = word;
28 using state_t = std::array<word, words>;
29 constexpr static size_t word_bytes = sizeof(word);
30 constexpr static size_t word_bits = word_bytes * 8;
31
32 struct Config final {
33 size_t bit_rate; /// The number of bits that using algorithms can modify between permutations
34 state_t initial_state; /// The state of the sponge state at initialization
35 };
36
37 public:
38 constexpr explicit Sponge(Config config) : m_S(config.initial_state), m_S_cursor(0), m_bit_rate(config.bit_rate) {
39 if(m_bit_rate % (sizeof(word) * 8) != 0 || m_bit_rate > words * sizeof(word) * 8) {
40 throw Botan::Invalid_Argument("Invalid sponge bit rate");
41 }
42 }
43
44 constexpr static size_t state_bytes() { return sizeof(state_t); }
45
46 constexpr static size_t state_bits() { return state_bytes() * 8; }
47
48 constexpr size_t bit_rate() const { return m_bit_rate; }
49
50 constexpr size_t byte_rate() const { return m_bit_rate / 8; }
51
52 constexpr size_t bit_capacity() const { return state_bits() - bit_rate(); }
53
54 constexpr size_t byte_capacity() const { return state_bytes() - byte_rate(); }
55
56 constexpr auto& state() { return m_S; }
57
58 size_t cursor() const { return m_S_cursor; }
59
60 size_t& _cursor() { return m_S_cursor; }
61
62 protected:
63 void reset_cursor() { m_S_cursor = 0; }
64
65 private:
66 state_t m_S;
67 size_t m_S_cursor;
68 size_t m_bit_rate;
69};
70
71} // namespace Botan
72
73#endif
void reset_cursor()
Definition sponge.h:63
word word_t
Definition sponge.h:27
constexpr size_t bit_capacity() const
Definition sponge.h:52
static constexpr size_t state_bytes()
Definition sponge.h:44
static constexpr size_t word_bytes
Definition sponge.h:29
size_t & _cursor()
Definition sponge.h:60
constexpr auto & state()
Definition sponge.h:56
std::array< word, words > state_t
Definition sponge.h:28
constexpr size_t byte_rate() const
Definition sponge.h:50
constexpr size_t byte_capacity() const
Definition sponge.h:54
constexpr size_t bit_rate() const
Definition sponge.h:48
size_t cursor() const
Definition sponge.h:58
static constexpr size_t word_bits
Definition sponge.h:30
static constexpr size_t state_bits()
Definition sponge.h:46
constexpr Sponge(Config config)
Definition sponge.h:38
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
Definition types.h:119
state_t initial_state
The number of bits that using algorithms can modify between permutations.
Definition sponge.h:34