Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
sha256_constraint.test.cpp
Go to the documentation of this file.
2#include "acir_format.hpp"
5
6#include <gtest/gtest.h>
7#include <vector>
8
10
11class Sha256Tests : public ::testing::Test {
12 protected:
14};
15
16TEST_F(Sha256Tests, TestSha256Compression)
17{
18 Sha256Compression sha256_compression;
19
20 for (size_t i = 0; i < 16; ++i) {
21 sha256_compression.inputs[i] = WitnessOrConstant<bb::fr>::from_index(static_cast<uint32_t>(i));
22 }
23 for (size_t i = 0; i < 8; ++i) {
24 sha256_compression.hash_values[i] = WitnessOrConstant<bb::fr>::from_index(static_cast<uint32_t>(i + 16));
25 }
26 for (size_t i = 0; i < 8; ++i) {
27 sha256_compression.result[i] = static_cast<uint32_t>(i + 24);
28 }
29
30 std::array<uint32_t, 16> input_block = { 0 };
31 std::array<uint32_t, 8> hash_values = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
32 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
33 std::array<uint32_t, 8> result = bb::crypto::sha256_block(hash_values, input_block);
34
35 WitnessVector witness(32, 0);
36 for (size_t idx = 16; idx < 24; idx++) {
37 witness[idx] = hash_values[idx - 16];
38 }
39 for (size_t idx = 0; idx < 8; idx++) {
40 witness[24 + idx] = result[idx];
41 }
42
43 AcirFormat constraint_system{
44 .max_witness_index = static_cast<uint32_t>(witness.size()) - 1,
45 .num_acir_opcodes = 1,
46 .public_inputs = {},
47 .sha256_compression = { sha256_compression },
48 .original_opcode_indices = create_empty_original_opcode_indices(),
49 };
50 mock_opcode_indices(constraint_system);
51
52 AcirProgram program{ constraint_system, witness };
53 auto builder = create_circuit<UltraCircuitBuilder>(program);
54 EXPECT_TRUE(CircuitChecker::check(builder));
55 EXPECT_FALSE(builder.failed());
56}
57} // namespace acir_format::tests
acir_format::AcirFormatOriginalOpcodeIndices create_empty_original_opcode_indices()
void mock_opcode_indices(acir_format::AcirFormat &constraint_system)
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
AluTraceBuilder builder
Definition alu.test.cpp:124
TEST_F(Sha256Tests, TestSha256Compression)
std::vector< bb::fr > WitnessVector
std::array< uint32_t, 8 > sha256_block(const std::array< uint32_t, 8 > &h_init, const std::array< uint32_t, 16 > &input)
SHA-256 compression function (FIPS 180-4 Section 6.2.2)
Definition sha256.cpp:73
std::filesystem::path bb_crs_path()
void init_file_crs_factory(const std::filesystem::path &path)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::array< WitnessOrConstant< bb::fr >, 8 > hash_values
std::array< uint32_t, 8 > result
std::array< WitnessOrConstant< bb::fr >, 16 > inputs
static WitnessOrConstant from_index(uint32_t index)