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
17
18namespace bb::stdlib {
19
25template <typename Builder> class FieldSponge {
26 private:
28 static constexpr size_t t = crypto::Poseidon2Bn254ScalarFieldParams::t; // = 4
29 static constexpr size_t capacity = 1;
30 static constexpr size_t rate = t - capacity; // = 3
31
33
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;
41
42 FieldSponge(Builder* builder_, size_t in_len)
43 : builder(builder_)
44 {
45 // Add the domain separation to the initial state.
46 field_t iv(static_cast<uint256_t>(in_len) << 64);
48 state[rate] = iv;
49 }
50
52 {
53 // Add the cache into sponge state
54 for (size_t i = 0; i < rate; ++i) {
55 state[i] += cache[i];
56 }
57
58 // Apply Poseidon2 permutation
60
61 // Reset the cache
62 cache = {};
63 }
64
65 void absorb(const field_t& input)
66 {
67 if (cache_size == rate) {
68 // If we're absorbing, and the cache is full, apply the sponge permutation to compress the cache
70 cache[0] = input;
71 cache_size = 1;
72 } else {
73 // If we're absorbing, and the cache is not full, add the input into the cache
74 cache[cache_size] = input;
75 cache_size += 1;
76 }
77 }
78
80 {
81
83
84 return state[0];
85 }
86
87 public:
95 {
96 // Ensure that all inputs belong to the same circuit and extract a pointer to the circuit object.
97 Builder* builder = validate_context<Builder>(input);
98
99 // Ensure that the pointer is not a `nullptr`
101
102 // Initialize the sponge state. Input length is used for domain separation.
103 const size_t in_len = input.size();
104 FieldSponge sponge(builder, in_len);
105
106 // Absorb inputs in blocks of size r = 3. Make sure that all inputs are witneesses.
107 for (size_t i = 0; i < in_len; ++i) {
108 BB_ASSERT_EQ(input[i].is_constant(), false, "Sponge inputs should not be stdlib constants.");
109 sponge.absorb(input[i]);
110 }
111
112 // Perform final duplex call. At this point, cache contains `m = in_len % 3` input elements and 3 - m constant
113 // zeroes served as padding.
114 field_t output = sponge.squeeze();
115
116 // The final state consists of 4 elements, we only use the first element, which means that the remaining
117 // 3 witnesses are only used in a single gate.
118 for (const auto& elem : sponge.state) {
120 }
121 return output;
122 }
123};
124} // namespace bb::stdlib
#define BB_ASSERT(expression,...)
Definition assert.hpp:67
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:77
Implements the circuit form of a cryptographic sponge over prime fields.
Definition sponge.hpp:25
static constexpr size_t rate
Definition sponge.hpp:30
FieldSponge(Builder *builder_, size_t in_len)
Definition sponge.hpp:42
std::array< field_t, t > state
Definition sponge.hpp:35
void absorb(const field_t &input)
Definition sponge.hpp:65
static constexpr size_t t
Definition sponge.hpp:28
std::array< field_t, rate > cache
Definition sponge.hpp:38
static field_t hash_internal(std::span< const field_t > input)
Use the sponge to hash an input vector.
Definition sponge.hpp:94
static constexpr size_t capacity
Definition sponge.hpp:29
Circuit form of Poseidon2 permutation from https://eprint.iacr.org/2023/323.
static State permutation(Builder *builder, const State &input)
Circuit form of Poseidon2 permutation from https://eprint.iacr.org/2023/323.
void convert_constant_to_fixed_witness(Builder *ctx)
Definition field.hpp:444
void mark_witness_as_used(const field_t< Builder > &field)
Mark a field_t witness as used (for UltraBuilder only).
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13