Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
sponge.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Sergei], commit: dd03c4a23ab067274b4964cacb36d1545f73fb14}
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
8
9#include <array>
10#include <cstddef>
11#include <cstdint>
12#include <span>
13
15
16namespace bb::crypto {
17
33template <typename FF, size_t rate, size_t capacity, size_t t, typename Permutation> class FieldSponge {
34 private:
35 // sponge state. t = rate + capacity. capacity = 1 field element (~256 bits)
37
38 // cached elements that have been absorbed.
40 size_t cache_size = 0;
41
42 FieldSponge(FF domain_iv) { state[rate] = domain_iv; }
43
45 {
46 // Add the cache into sponge state
47 for (size_t i = 0; i < rate; ++i) {
48 state[i] += cache[i];
49 }
50
51 // Apply permutation
52 Permutation::permutation_inplace(state);
53
54 // Reset the cache
55 cache = {};
56 }
57
58 void absorb(const FF& input)
59 {
60 if (cache_size == rate) {
61 // If the cache is full, apply the sponge permutation to compress the cache
63 cache[0] = input;
64 cache_size = 1;
65 } else {
66 // If the cache is not full, add the input into the cache
67 cache[cache_size] = input;
68 cache_size += 1;
69 }
70 }
71
73 {
75 return state[0];
76 }
77
78 public:
86 {
87 const size_t in_len = input.size();
88 const uint256_t iv = (static_cast<uint256_t>(in_len) << 64);
89 return hash_internal(input, iv);
90 }
91
100 {
101 FieldSponge sponge(iv);
102
103 const size_t in_len = input.size();
104 for (size_t i = 0; i < in_len; ++i) {
105 sponge.absorb(input[i]);
106 }
107
108 return sponge.squeeze();
109 }
110};
111} // namespace bb::crypto
Implements a cryptographic sponge over prime fields. Sponge construction follows the Duplex Sponge mo...
Definition sponge.hpp:33
static FF hash_internal(std::span< const FF > input)
Use the sponge to hash an input vector.
Definition sponge.hpp:85
void absorb(const FF &input)
Definition sponge.hpp:58
FieldSponge(FF domain_iv)
Definition sponge.hpp:42
std::array< FF, rate > cache
Definition sponge.hpp:39
std::array< FF, t > state
Definition sponge.hpp:36
static FF hash_internal(std::span< const FF > input, FF iv)
Use the sponge to hash an input vector with a custom IV.
Definition sponge.hpp:99
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13