Botan 3.11.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/assert.h>
13#include <botan/types.h>
14#include <array>
15
16namespace Botan {
17
18/**
19 * A generic sponge construction with a fixed state size defined in terms of
20 * "words" of an unsigned integral type.
21 *
22 * This is meant to be used as a base class for specific sponge constructions
23 * like Keccak or Ascon.
24 */
25template <size_t words, std::unsigned_integral word = uint64_t>
26class Sponge {
27 public:
28 using word_t = word;
29 using state_t = std::array<word, words>;
30 constexpr static size_t word_bytes = sizeof(word);
31 constexpr static size_t word_bits = word_bytes * 8;
32
33 struct Config final {
34 size_t bit_rate; /// The number of bits that using algorithms can modify between permutations
35 state_t initial_state; /// The state of the sponge state at initialization
36 };
37
38 public:
39 constexpr explicit Sponge(Config config) : m_S(config.initial_state), m_S_cursor(0), m_bit_rate(config.bit_rate) {
40 BOTAN_ARG_CHECK(m_bit_rate % word_bits == 0 && m_bit_rate < words * word_bits, "Invalid sponge bit rate");
41 }
42
43 constexpr static size_t state_bytes() { return sizeof(state_t); }
44
45 constexpr static size_t state_bits() { return state_bytes() * 8; }
46
47 constexpr size_t bit_rate() const { return m_bit_rate; }
48
49 constexpr size_t byte_rate() const { return m_bit_rate / 8; }
50
51 constexpr size_t bit_capacity() const { return state_bits() - bit_rate(); }
52
53 constexpr size_t byte_capacity() const { return state_bytes() - byte_rate(); }
54
55 constexpr auto& state() { return m_S; }
56
57 size_t cursor() const { return m_S_cursor; }
58
59 size_t& _cursor() { return m_S_cursor; }
60
61 protected:
62 void reset_cursor() { m_S_cursor = 0; }
63
64 private:
65 state_t m_S;
66 size_t m_S_cursor;
67 size_t m_bit_rate;
68};
69
70} // namespace Botan
71
72#endif
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
void reset_cursor()
Definition sponge.h:62
word word_t
Definition sponge.h:28
constexpr size_t bit_capacity() const
Definition sponge.h:51
static constexpr size_t state_bytes()
Definition sponge.h:43
static constexpr size_t word_bytes
Definition sponge.h:30
size_t & _cursor()
Definition sponge.h:59
constexpr auto & state()
Definition sponge.h:55
std::array< word, words > state_t
Definition sponge.h:29
constexpr size_t byte_rate() const
Definition sponge.h:49
constexpr size_t byte_capacity() const
Definition sponge.h:53
constexpr size_t bit_rate() const
Definition sponge.h:47
size_t cursor() const
Definition sponge.h:57
static constexpr size_t word_bits
Definition sponge.h:31
static constexpr size_t state_bits()
Definition sponge.h:45
constexpr Sponge(Config config)
Definition sponge.h:39
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:35