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: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
8
9#include <array>
10#include <cstddef>
11#include <cstdint>
12#include <span>
13
15
16namespace bb::crypto {
17
32template <typename FF, size_t rate, size_t capacity, size_t t, typename Permutation> class FieldSponge {
33 private:
34 // sponge state. t = rate + capacity. capacity = 1 field element (~256 bits)
36
37 // cached elements that have been absorbed.
39 size_t cache_size = 0;
40
41 FieldSponge(FF domain_iv) { state[rate] = domain_iv; }
42
44 {
45 // Add the cache into sponge state
46 for (size_t i = 0; i < rate; ++i) {
47 state[i] += cache[i];
48 }
49
50 // Apply permutation
52
53 // Reset the cache
54 cache = {};
55 }
56
57 void absorb(const FF& input)
58 {
59 if (cache_size == rate) {
60 // If the cache is full, apply the sponge permutation to compress the cache
62 cache[0] = input;
63 cache_size = 1;
64 } else {
65 // If the cache is not full, add the input into the cache
66 cache[cache_size] = input;
67 cache_size += 1;
68 }
69 }
70
72 {
74 return state[0];
75 }
76
77 public:
85 {
86 const size_t in_len = input.size();
87 const uint256_t iv = (static_cast<uint256_t>(in_len) << 64);
88 return hash_internal(input, iv);
89 }
90
99 {
100 FieldSponge sponge(iv);
101
102 const size_t in_len = input.size();
103 for (size_t i = 0; i < in_len; ++i) {
104 sponge.absorb(input[i]);
105 }
106
107 return sponge.squeeze();
108 }
109};
110} // namespace bb::crypto
Implements a cryptographic sponge over prime fields. Implements the sponge specification from the Com...
Definition sponge.hpp:32
static FF hash_internal(std::span< const FF > input)
Use the sponge to hash an input vector.
Definition sponge.hpp:84
void absorb(const FF &input)
Definition sponge.hpp:57
FieldSponge(FF domain_iv)
Definition sponge.hpp:41
std::array< FF, rate > cache
Definition sponge.hpp:38
std::array< FF, t > state
Definition sponge.hpp:35
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:98
static State permutation(Builder *builder, const State &input)
Circuit form of Poseidon2 permutation from https://eprint.iacr.org/2023/323.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13