8#include "barretenberg/numeric/uint256/uint256.hpp"
26template <
typename FF,
size_t rate,
size_t capacity,
size_t t,
typename Permutation>
class FieldSponge {
41 std::array<FF, t> state;
44 std::array<FF, rate> cache;
45 size_t cache_size = 0;
46 Mode mode = Mode::ABSORB;
50 for (
size_t i = 0; i < rate; ++i) {
53 state[rate] = domain_iv;
56 std::array<FF, rate> perform_duplex()
59 for (
size_t i = cache_size; i < rate; ++i) {
63 for (
size_t i = 0; i < rate; ++i) {
66 state = Permutation::permutation(state);
68 std::array<FF, rate> output;
69 for (
size_t i = 0; i < rate; ++i) {
75 void absorb(
const FF& input)
77 if (mode == Mode::ABSORB && cache_size == rate) {
82 }
else if (mode == Mode::ABSORB && cache_size < rate) {
84 cache[cache_size] = input;
86 }
else if (mode == Mode::SQUEEZE) {
97 if (mode == Mode::SQUEEZE && cache_size == 0) {
103 if (mode == Mode::ABSORB) {
107 auto new_output_elements = perform_duplex();
108 mode = Mode::SQUEEZE;
109 for (
size_t i = 0; i < rate; ++i) {
110 cache[i] = new_output_elements[i];
115 FF result = cache[0];
116 for (
size_t i = 1; i < cache_size; ++i) {
117 cache[i - 1] = cache[i];
120 cache[cache_size] = 0;
132 template <
size_t out_len,
bool is_variable_length>
static std::array<FF, out_len>
hash_internal(std::span<FF> input)
134 size_t in_len = input.size();
138 for (
size_t i = 0; i < in_len; ++i) {
139 sponge.absorb(input[i]);
145 if constexpr (is_variable_length) {
149 std::array<FF, out_len> output;
150 for (
size_t i = 0; i < out_len; ++i) {
151 output[i] = sponge.squeeze();
156 template <
size_t out_len>
static std::array<FF, out_len> hash_fixed_length(std::span<FF> input)
158 return hash_internal<out_len, false>(input);
160 static FF hash_fixed_length(std::span<FF> input) {
return hash_fixed_length<1>(input)[0]; }
162 template <
size_t out_len>
static std::array<FF, out_len> hash_variable_length(std::span<FF> input)
164 return hash_internal<out_len, true>(input);
166 static FF hash_variable_length(std::span<FF> input) {
return hash_variable_length<1>(input)[0]; }
Implements a cryptographic sponge over prime fields. Implements the sponge specification from the Com...
Definition: sponge.hpp:26
static std::array< FF, out_len > hash_internal(std::span< FF > input)
Use the sponge to hash an input string.
Definition: sponge.hpp:132
Mode
Defines what phase of the sponge algorithm we are in.
Definition: sponge.hpp:35
Definition: uint256.hpp:25