Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
keccak.fuzzer.cpp
Go to the documentation of this file.
1#include "keccak.hpp"
5#include <array>
6#include <cassert>
7#include <cstdint>
8#include <cstring>
9#include <vector>
10
11using namespace bb;
12using namespace bb::stdlib;
13
14#ifdef FUZZING_SHOW_INFORMATION
15#define PRINT_STATE(header, bs) \
16 { \
17 std::cout << header; \
18 for (const uint64_t& x : bs) { \
19 std::cout << "0x" << std::hex << std::uppercase << std::setw(16) << std::setfill('0') << x << " " \
20 << std::dec; \
21 } \
22 std::cout << std::endl; \
23 }
24#else
25#define PRINT_STATE(header, bs)
26#endif
27
42extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size)
43{
44 // Keccak-f1600 state is 25 lanes of 64 bits = 200 bytes
45 constexpr size_t KECCAK_STATE_SIZE = 200;
46 if (Size < KECCAK_STATE_SIZE) {
47 return 0;
48 }
49
50 // Convert input bytes to native state (25 x uint64_t)
51 std::array<uint64_t, 25> native_state;
52 std::memcpy(native_state.data(), Data, KECCAK_STATE_SIZE);
53
54 PRINT_STATE("Input: ", native_state);
55
56 // Run native permutation
57 std::array<uint64_t, 25> expected_state = native_state;
58 ethash_keccakf1600(expected_state.data());
59
60 // Build circuit with permutation
62
63 // Convert state to circuit field elements
65 for (size_t i = 0; i < 25; i++) {
66 circuit_state[i] = witness_t<UltraCircuitBuilder>(&builder, native_state[i]);
67 }
68
69 // Run circuit permutation
70 auto circuit_output = keccak<UltraCircuitBuilder>::permutation_opcode(circuit_state, &builder);
71
72 std::array<uint64_t, 25> circuit_output_u;
73 for (size_t i = 0; i < 25; i++) {
74 circuit_output_u[i] = static_cast<uint64_t>(circuit_output[i].get_value());
75 }
76
77 PRINT_STATE("Circuit output: ", circuit_output_u);
78 PRINT_STATE("Expected: ", expected_state);
79
80 // Compare outputs
81 assert(circuit_output_u == expected_state);
82
83 // Verify circuit correctness
85 return 0;
86}
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
KECCAAAAAAAAAAK.
Definition keccak.hpp:25
AluTraceBuilder builder
Definition alu.test.cpp:124
void ethash_keccakf1600(uint64_t state[25]) NOEXCEPT
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
Fuzzer for Keccak-f1600 permutation (permutation_opcode)
#define PRINT_STATE(header, bs)
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13