Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bb::stdlib::blake_util Namespace Reference

Enumerations

enum  blake_constant { BLAKE_STATE_SIZE = 16 }
 

Functions

template<typename Builder >
field_t< Builderadd_normalize (const field_t< Builder > &a, const field_t< Builder > &b)
 
template<typename Builder >
void g (field_t< Builder > state[BLAKE_STATE_SIZE], size_t a, size_t b, size_t c, size_t d, field_t< Builder > x, field_t< Builder > y, const bool last_update=false)
 
template<typename Builder >
void round_fn (field_t< Builder > state[BLAKE_STATE_SIZE], field_t< Builder > msg[BLAKE_STATE_SIZE], size_t round, const bool which_blake=false)
 

Variables

constexpr uint8_t MSG_SCHEDULE_BLAKE3 [7][16]
 
constexpr uint8_t MSG_SCHEDULE_BLAKE2 [10][16]
 

Enumeration Type Documentation

◆ blake_constant

Enumerator
BLAKE_STATE_SIZE 

Definition at line 16 of file blake_util.hpp.

Function Documentation

◆ add_normalize()

template<typename Builder >
field_t< Builder > bb::stdlib::blake_util::add_normalize ( const field_t< Builder > &  a,
const field_t< Builder > &  b 
)

Addition with normalisation (to ensure the addition is in the scalar field.) Given two field_t elements a and b, this function computes ((a + b) % 2^{32}). Additionally, it checks if the overflow of the addition is a maximum of 3 bits. This is to ascertain that the additions of two 32-bit scalars in blake2s and blake3s do not exceed 35 bits.

Definition at line 39 of file blake_util.hpp.

◆ g()

template<typename Builder >
void bb::stdlib::blake_util::g ( field_t< Builder state[BLAKE_STATE_SIZE],
size_t  a,
size_t  b,
size_t  c,
size_t  d,
field_t< Builder x,
field_t< Builder y,
const bool  last_update = false 
)

Function ‘G’ in the Blake2s and Blake3s algorithm which is the core mixing step with additions, xors and right-rotates. This function is used in Ultra version (with lookup tables).

Inputs: - A pointer to a 16-word state,

  • indices a, b, c, d,
  • addition messages x and y
  • boolean last_update to make sure addition is normalised only in last update of the state

Gate costs per call to function G in lookup case:

Read sequence from table = 6 gates per read => 6 * 4 = 24 Addition gates = 4 gates Range gates = 2 gates Addition gate for correct output of XOR rotate 12 = 1 gate Normalizing scaling factors = 2 gates

Subtotal = 33 gates Outside rounds, each of Blake2s and Blake3s needs 20 and 24 lookup reads respectively.

+--------—+-----------—+--------------------—+------------------------—+-----------—+

calls to G gate count for rounds gate count outside rounds total
Blake2s 80 80 * 33 20 * 6 2760
Blake3s 56 56 * 33 24 * 6 1992

+--------—+-----------—+--------------------—+------------------------—+-----------—+

P.S. This doesn't include some more addition gates required after the rounds. This cost would be negligible as compared to the above gate counts.

TODO: Idea for getting rid of extra addition and multiplication gates by tweaking gate structure. To be implemented later.

q_plookup = 1 | d0 | a0 | d'0 | – | q_plookup = 1 | d1 | a1 | d'1 | d2 | <— set q_arith = 1 and validate d2 - d'5 * scale_factor = 0 q_plookup = 1 | d2 | a2 | d'2 | d'5 | q_plookup = 1 | d3 | a3 | d'3 | – | q_plookup = 1 | d4 | a4 | d'4 | – | q_plookup = 1 | d5 | a5 | d'5 | c | <-— set q_arith = 1 and validate d'5 * scale_factor + c - c2 = 0. | | c2 | <-— this row is start of another lookup table (b ^ c)

Definition at line 113 of file blake_util.hpp.

◆ round_fn()

template<typename Builder >
void bb::stdlib::blake_util::round_fn ( field_t< Builder state[BLAKE_STATE_SIZE],
field_t< Builder msg[BLAKE_STATE_SIZE],
size_t  round,
const bool  which_blake = false 
)

Definition at line 176 of file blake_util.hpp.

Variable Documentation

◆ MSG_SCHEDULE_BLAKE2

constexpr uint8_t bb::stdlib::blake_util::MSG_SCHEDULE_BLAKE2[10][16]
constexpr
Initial value:
= {
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
{ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 }, { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 }, { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
{ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 }, { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
{ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 }, { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
}

Definition at line 25 of file blake_util.hpp.

◆ MSG_SCHEDULE_BLAKE3

constexpr uint8_t bb::stdlib::blake_util::MSG_SCHEDULE_BLAKE3[7][16]
constexpr
Initial value:
= {
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, { 2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8 },
{ 3, 4, 10, 12, 13, 2, 7, 14, 6, 5, 9, 0, 11, 15, 8, 1 }, { 10, 7, 12, 9, 14, 3, 13, 15, 4, 0, 11, 2, 5, 8, 1, 6 },
{ 12, 13, 9, 11, 15, 10, 14, 8, 7, 2, 5, 3, 0, 1, 6, 4 }, { 9, 14, 11, 5, 8, 12, 15, 1, 13, 3, 0, 10, 2, 6, 4, 7 },
{ 11, 15, 5, 0, 1, 9, 8, 6, 14, 10, 2, 12, 3, 4, 7, 13 },
}

Definition at line 18 of file blake_util.hpp.