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: Complete, auditors: [Nishat], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
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 static constexpr size_t KECCAK_LANE_SIZE = 64;
41
42 // round constants. Used in IOTA round
43 static constexpr std::array<uint64_t, NUM_KECCAK_ROUNDS> RC = {
44 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 0x8000000080008000, 0x000000000000808b,
45 0x0000000080000001, 0x8000000080008081, 0x8000000000008009, 0x000000000000008a, 0x0000000000000088,
46 0x0000000080008009, 0x000000008000000a, 0x000000008000808b, 0x800000000000008b, 0x8000000000008089,
47 0x8000000000008003, 0x8000000000008002, 0x8000000000000080, 0x000000000000800a, 0x800000008000000a,
48 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008
49 };
50
51 // Rotation offsets, y vertically, x horizontally: r[y * 5 + x]
52 static constexpr std::array<size_t, NUM_KECCAK_LANES> ROTATIONS = {
53 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,
54 };
55
65 static constexpr uint256_t convert_to_sparse(uint256_t input)
66 {
67 std::array<uint64_t, 64> out_bits;
68 size_t count = 0;
69 while (input > 0) {
70 uint64_t bit = static_cast<uint64_t>(input & 1);
71 out_bits[count++] = bit;
72 input = input >> 1;
73 }
74 uint256_t output = 0;
75 for (size_t i = 0; i < count; ++i) {
76 output *= BASE;
77 output += out_bits[count - 1 - i];
78 }
79 return output;
80 };
81
93 static constexpr uint256_t normalize_sparse(uint256_t input)
94 {
95 std::array<uint64_t, 64> out_bits;
96 size_t count = 0;
97 while (input > 0) {
98 const auto [quotient, slice] = input.divmod(BASE);
99 uint64_t bit = static_cast<uint64_t>(slice) & 1;
100 out_bits[count++] = bit;
101 input = quotient;
102 }
103 uint256_t out;
104 for (size_t i = 0; i < count; ++i) {
105 out *= BASE;
106 out += out_bits[count - 1 - i];
107 }
108 return out;
109 }
110
117 {
119 for (size_t i = 0; i < NUM_KECCAK_ROUNDS; ++i) {
120 output[i] = convert_to_sparse(RC[i]);
121 }
122 return output;
123 }
125
136 static constexpr uint256_t get_chi_offset()
137 {
138 uint256_t result = 0;
139 for (size_t i = 0; i < 64; ++i) {
140 result *= 11;
141 result += 1;
142 }
143 return result;
144 }
145 static constexpr uint256_t CHI_OFFSET = get_chi_offset();
146
153
154 template <size_t lane_index> static field_t<Builder> normalize_and_rotate(const field_ct& limb, field_ct& msb);
155 static void compute_twisted_state(keccak_state& internal);
156 static void theta(keccak_state& state);
157 static void rho(keccak_state& state);
158 static void pi(keccak_state& state);
159 static void chi(keccak_state& state);
160 static void iota(keccak_state& state, size_t round);
161
162 static void keccakf1600(keccak_state& state);
163
164 static std::vector<uint8_t> hash_native(const std::vector<uint8_t>& data)
165 {
166 auto hash_result = ethash_keccak256(&data[0], data.size());
167
168 std::vector<uint8_t> output;
169 output.resize(32);
170
171 memcpy((void*)&output[0], (void*)&hash_result.word64s[0], 32);
172 return output;
173 }
174
175 // exposing keccak f1600 permutation
176
179 static std::array<field_ct, NUM_KECCAK_LANES> extended_2_normal(keccak_state& internal);
180};
181
182} // 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:136
static void rho(keccak_state &state)
RHO round.
Definition keccak.cpp:385
static constexpr uint256_t CHI_OFFSET
Definition keccak.hpp:145
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:93
static constexpr std::array< uint256_t, NUM_KECCAK_ROUNDS > get_sparse_round_constants()
Get the sparse round constants object.
Definition keccak.hpp:116
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:164
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:52
static constexpr std::array< uint64_t, NUM_KECCAK_ROUNDS > RC
Definition keccak.hpp:43
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:124
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:517
static void keccakf1600(keccak_state &state)
Definition keccak.cpp:482
static constexpr size_t NUM_KECCAK_LANES
Definition keccak.hpp:39
static constexpr size_t KECCAK_LANE_SIZE
Definition keccak.hpp:40
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:65
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:148
std::array< field_ct, NUM_KECCAK_LANES > twisted_state
Definition keccak.hpp:150
std::array< field_ct, NUM_KECCAK_LANES > state_msb
Definition keccak.hpp:149