Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
sha256.test.cpp File Reference

Go to the source code of this file.

Classes

class  Sha256Test< Builder >
 

Macros

#define STDLIB_TYPE_ALIASES
 

Typedefs

using BuilderTypes = ::testing::Types< bb::UltraCircuitBuilder, bb::MegaCircuitBuilder >
 

Functions

 TYPED_TEST_SUITE (Sha256Test, BuilderTypes)
 
 TYPED_TEST (Sha256Test, BlockNistVectorOne)
 Test sha256_block against NIST vector one ("abc")
 
 TYPED_TEST (Sha256Test, BlockNistVectorTwo)
 Test sha256_block against NIST vector two (56-byte message)
 
 TYPED_TEST (Sha256Test, BlockAllConstants)
 Test sha256_block with all-constant inputs produces correct output with zero gates.
 
 TYPED_TEST (Sha256Test, BlockConstantHinitWitnessInput)
 Test sha256_block with constant h_init and witness input block.
 
 TYPED_TEST (Sha256Test, BlockWitnessHinitConstantInput)
 Test sha256_block with witness h_init and constant input block.
 
 TYPED_TEST (Sha256Test, BlockMixedConstantsAndWitnesses)
 Test sha256_block with interleaved constant and witness values within both arrays.
 
 TYPED_TEST (Sha256Test, ExtendWitnessMixedInputs)
 Test extend_witness with mixed constant and witness message words.
 
 TYPED_TEST (Sha256Test, ExtendWitnessTamperingFailure)
 Test extend_witness constraints (boomerang attack regression)
 

Variables

constexpr std::array< uint32_t, 8 > SHA256_IV
 
constexpr std::array< uint32_t, 16 > ABC_PADDED_BLOCK
 
constexpr std::array< uint32_t, 8 > ABC_EXPECTED
 

Macro Definition Documentation

◆ STDLIB_TYPE_ALIASES

#define STDLIB_TYPE_ALIASES

Typedef Documentation

◆ BuilderTypes

Definition at line 24 of file sha256.test.cpp.

Function Documentation

◆ TYPED_TEST() [1/8]

TYPED_TEST ( Sha256Test  ,
BlockAllConstants   
)

Test sha256_block with all-constant inputs produces correct output with zero gates.

When both h_init and input are circuit constants (not witnesses), every plookup operation takes the constant path (no gate creation), and add_normalize_unsafe returns constants directly.

Definition at line 164 of file sha256.test.cpp.

◆ TYPED_TEST() [2/8]

TYPED_TEST ( Sha256Test  ,
BlockConstantHinitWitnessInput   
)

Test sha256_block with constant h_init and witness input block.

This is the natural use case for the first block of a SHA-256 hash: the IV is known at compile time, but the message is a witness. The constant h_init values take the constant plookup path for their initial sparse form conversions, saving gates compared to the all-witness case.

Definition at line 203 of file sha256.test.cpp.

◆ TYPED_TEST() [3/8]

TYPED_TEST ( Sha256Test  ,
BlockMixedConstantsAndWitnesses   
)

Test sha256_block with interleaved constant and witness values within both arrays.

Even-indexed h_init and input words are constants, odd-indexed are witnesses. This exercises the mixed-input paths through plookup (some lookups constant, some witness), extend_witness (lazy sparse conversion with mixed provenance), and add_normalize_unsafe (one constant operand, one witness operand).

Definition at line 273 of file sha256.test.cpp.

◆ TYPED_TEST() [4/8]

TYPED_TEST ( Sha256Test  ,
BlockNistVectorOne   
)

Test sha256_block against NIST vector one ("abc")

This tests the compression function directly by manually padding the message and comparing against the known NIST hash output.

For "abc" (3 bytes):

  • Padded block: "abc" + 0x80 + zeros + 64-bit length (24 bits)
  • Single block since message fits in 55 bytes

Definition at line 53 of file sha256.test.cpp.

◆ TYPED_TEST() [5/8]

TYPED_TEST ( Sha256Test  ,
BlockNistVectorTwo   
)

Test sha256_block against NIST vector two (56-byte message)

This tests chained compression by manually padding a two-block message and comparing against the known NIST hash output.

For "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" (56 bytes):

  • Block 1: message bytes + padding bit (0x80)
  • Block 2: zeros + 64-bit length (448 bits = 0x1c0)

Definition at line 99 of file sha256.test.cpp.

◆ TYPED_TEST() [6/8]

TYPED_TEST ( Sha256Test  ,
BlockWitnessHinitConstantInput   
)

Test sha256_block with witness h_init and constant input block.

This models the second block in a two-block hash where the intermediate hash state is a witness (output of first compression) but the padding block is all constants. The constant message words fold through extend_witness without creating lookup gates, yielding significantly fewer gates than the all-witness case.

Definition at line 238 of file sha256.test.cpp.

◆ TYPED_TEST() [7/8]

TYPED_TEST ( Sha256Test  ,
ExtendWitnessMixedInputs   
)

Test extend_witness with mixed constant and witness message words.

Only input[0] and input[15] are witnesses; the rest are constants. This exercises the lazy sparse conversion and context propagation in extend_witness when inputs have mixed constantness.

Definition at line 316 of file sha256.test.cpp.

◆ TYPED_TEST() [8/8]

TYPED_TEST ( Sha256Test  ,
ExtendWitnessTamperingFailure   
)

Test extend_witness constraints (boomerang attack regression)

This security test verifies that SHA256::extend_witness() properly constrains all 64 extended message schedule words. Modifying any word should cause circuit failure.

Definition at line 364 of file sha256.test.cpp.

◆ TYPED_TEST_SUITE()

TYPED_TEST_SUITE ( Sha256Test  ,
BuilderTypes   
)

Variable Documentation

◆ ABC_EXPECTED

constexpr std::array<uint32_t, 8> ABC_EXPECTED
constexpr
Initial value:
= { 0xba7816bf, 0x8f01cfea, 0x414140de, 0x5dae2223,
0xb00361a3, 0x96177a9c, 0xb410ff61, 0xf20015ad }

Definition at line 40 of file sha256.test.cpp.

◆ ABC_PADDED_BLOCK

constexpr std::array<uint32_t, 16> ABC_PADDED_BLOCK
constexpr
Initial value:
= { 0x61626380, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000018 }

Definition at line 34 of file sha256.test.cpp.

◆ SHA256_IV

constexpr std::array<uint32_t, 8> SHA256_IV
constexpr
Initial value:
= { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 }

Definition at line 30 of file sha256.test.cpp.