Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
keccak.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
9#include <array>
10
11namespace bb::stdlib {
12
25template <typename Builder> class keccak {
26 public:
31
32 // base of extended representation we use for efficient logic operations
33 static constexpr uint256_t BASE = 11;
34
35 static constexpr size_t NUM_KECCAK_ROUNDS = 24;
36
37 // 1 "lane" = 64 bits. Instead of interpreting the keccak sponge as 1,600 bits, it's easier to work over 64-bit
38 // "lanes". 1,600 / 64 = 25.
39 static constexpr size_t NUM_KECCAK_LANES = 25;
40
41 // round constants. Used in IOTA round
42 static constexpr std::array<uint64_t, NUM_KECCAK_ROUNDS> RC = {
43 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 0x8000000080008000, 0x000000000000808b,
44 0x0000000080000001, 0x8000000080008081, 0x8000000000008009, 0x000000000000008a, 0x0000000000000088,
45 0x0000000080008009, 0x000000008000000a, 0x000000008000808b, 0x800000000000008b, 0x8000000000008089,
46 0x8000000000008003, 0x8000000000008002, 0x8000000000000080, 0x000000000000800a, 0x800000008000000a,
47 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008
48 };
49
50 // Rotation offsets, y vertically, x horizontally: r[y * 5 + x]
51 static constexpr std::array<size_t, NUM_KECCAK_LANES> ROTATIONS = {
52 0, 1, 62, 28, 27, 36, 44, 6, 55, 20, 3, 10, 43, 25, 39, 41, 45, 15, 21, 8, 18, 2, 61, 56, 14,
53 };
54
64 static constexpr uint256_t convert_to_sparse(uint256_t input)
65 {
67 size_t count = 0;
68 while (input > 0) {
69 uint64_t bit = static_cast<uint64_t>(input & 1);
70 out_bits[count++] = bit;
71 input = input >> 1;
72 }
73 uint256_t output = 0;
74 for (size_t i = 0; i < count; ++i) {
75 output *= BASE;
76 output += out_bits[count - 1 - i];
77 }
78 return output;
79 };
80
92 static constexpr uint256_t normalize_sparse(uint256_t input)
93 {
95 size_t count = 0;
96 while (input > 0) {
97 const auto [quotient, slice] = input.divmod(BASE);
98 uint64_t bit = static_cast<uint64_t>(slice) & 1;
99 out_bits[count++] = bit;
100 input = quotient;
101 }
102 uint256_t out;
103 for (size_t i = 0; i < count; ++i) {
104 out *= BASE;
105 out += out_bits[count - 1 - i];
106 }
107 return out;
108 }
109
116 {
118 for (size_t i = 0; i < 24; ++i) {
119 output[i] = convert_to_sparse(RC[i]);
120 }
121 return output;
122 }
124
135 static constexpr uint256_t get_chi_offset()
136 {
137 uint256_t result = 0;
138 for (size_t i = 0; i < 64; ++i) {
139 result *= 11;
140 result += 1;
141 }
142 return result;
143 }
144 static constexpr uint256_t CHI_OFFSET = get_chi_offset();
145
152
153 template <size_t lane_index> static field_t<Builder> normalize_and_rotate(const field_ct& limb, field_ct& msb);
154 static void compute_twisted_state(keccak_state& internal);
155 static void theta(keccak_state& state);
156 static void rho(keccak_state& state);
157 static void pi(keccak_state& state);
158 static void chi(keccak_state& state);
159 static void iota(keccak_state& state, size_t round);
160
161 static void keccakf1600(keccak_state& state);
162
163 static std::vector<uint8_t> hash_native(const std::vector<uint8_t>& data)
164 {
165 auto hash_result = ethash_keccak256(&data[0], data.size());
166
167 std::vector<uint8_t> output;
168 output.resize(32);
169
170 memcpy((void*)&output[0], (void*)&hash_result.word64s[0], 32);
171 return output;
172 }
173
174 // exposing keccak f1600 permutation
175
178 static std::array<field_ct, NUM_KECCAK_LANES> extended_2_normal(keccak_state& internal);
179};
180
181} // namespace bb::stdlib
constexpr std::pair< uint256_t, uint256_t > divmod(const uint256_t &b) const
Implements boolean logic in-circuit.
Definition bool.hpp:59
Represents a dynamic array of bytes in-circuit.
KECCAAAAAAAAAAK.
Definition keccak.hpp:25
static constexpr uint256_t get_chi_offset()
Compute the constant offset added in the Chi round.
Definition keccak.hpp:135
static void rho(keccak_state &state)
RHO round.
Definition keccak.cpp:385
static constexpr uint256_t CHI_OFFSET
Definition keccak.hpp:144
static constexpr uint256_t BASE
Definition keccak.hpp:33
static constexpr uint256_t normalize_sparse(uint256_t input)
Normalize a base-11 integer where each base value can be > 1.
Definition keccak.hpp:92
static constexpr std::array< uint256_t, NUM_KECCAK_ROUNDS > get_sparse_round_constants()
Get the sparse round constants object.
Definition keccak.hpp:115
static void pi(keccak_state &state)
PI.
Definition keccak.cpp:400
static void theta(keccak_state &state)
THETA round.
Definition keccak.cpp:251
static void compute_twisted_state(keccak_state &internal)
Compute twisted representation of hash lane.
Definition keccak.cpp:197
static void chi(keccak_state &state)
CHI.
Definition keccak.cpp:436
static std::vector< uint8_t > hash_native(const std::vector< uint8_t > &data)
Definition keccak.hpp:163
static field_t< Builder > normalize_and_rotate(const field_ct &limb, field_ct &msb)
Normalize a base-11 limb and left-rotate by keccak::ROTATIONS[lane_index] bits. This method also extr...
Definition keccak.cpp:37
static constexpr std::array< size_t, NUM_KECCAK_LANES > ROTATIONS
Definition keccak.hpp:51
static constexpr std::array< uint64_t, NUM_KECCAK_ROUNDS > RC
Definition keccak.hpp:42
static constexpr size_t NUM_KECCAK_ROUNDS
Definition keccak.hpp:35
static constexpr std::array< uint256_t, NUM_KECCAK_ROUNDS > SPARSE_RC
Definition keccak.hpp:123
static std::array< field_ct, NUM_KECCAK_LANES > permutation_opcode(std::array< field_ct, NUM_KECCAK_LANES > state, Builder *context)
Definition keccak.cpp:498
static std::array< field_ct, NUM_KECCAK_LANES > extended_2_normal(keccak_state &internal)
Definition keccak.cpp:519
static void keccakf1600(keccak_state &state)
Definition keccak.cpp:482
static constexpr size_t NUM_KECCAK_LANES
Definition keccak.hpp:39
static void iota(keccak_state &state, size_t round)
IOTA.
Definition keccak.cpp:469
static constexpr uint256_t convert_to_sparse(uint256_t input)
Convert a binary integer into a base11 integer.
Definition keccak.hpp:64
const std::vector< MemoryValue > data
StrictMock< MockContext > context
struct keccak256 ethash_keccak256(const uint8_t *data, size_t size) NOEXCEPT
Definition keccak.cpp:107
C slice(C const &container, size_t start)
Definition container.hpp:9
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::array< field_ct, NUM_KECCAK_LANES > state
Definition keccak.hpp:147
std::array< field_ct, NUM_KECCAK_LANES > twisted_state
Definition keccak.hpp:149
std::array< field_ct, NUM_KECCAK_LANES > state_msb
Definition keccak.hpp:148