Index Mathematics Conjectures nbv lettuce Widgets

blake3_compress

stdlib/blake3/blake3_compress.nbv

blake3_compress(cv, block, params, static out_words, static fresh)

The BLAKE3 compression function on a batch of B inputs. Words are 32 little-endian bits.

cv is B chaining values of 256 bits, block B message blocks of 512 bits and params B times the 128 bits of state words 12..16: the 64-bit counter, the block length and the flags. Returns B outputs of out_words words (8 for a chaining value, 16 for an extended output block), one after the other. With fresh 1 the outputs are fresh witness bits, otherwise linear combinations of the state, which is cheaper when they only feed another compression.

Calls
blake3/blake3_g, blake3/blake3_rotate_rows, bits/bytes_to_bits_static, slice, bits/xor_bits_vec, bits/xor_bits_vec_wtns
Precompiles
assert_eq!, clone!, concat!, div_exact!, if_eq!, len!, loop!, loop_flat_map!
Used by
blake3/blake3_core

Source

# The BLAKE3 compression function on a batch of B inputs. Words are 32
# little-endian bits.
#
# `cv` is B chaining values of 256 bits, `block` B message blocks of 512 bits
# and `params` B times the 128 bits of state words 12..16: the 64-bit
# counter, the block length and the flags. Returns B outputs of
# `out_words` words (8 for a chaining value, 16 for an extended output
# block), one after the other. With `fresh` 1 the outputs are fresh witness
# bits, otherwise linear combinations of the state, which is cheaper when
# they only feed another compression.
(cv, block, params, static out_words, static fresh)

static B = div_exact! (len! cv), 256
assert_eq! 512 * B, len! block
assert_eq! 128 * B, len! params

static IV4 = bytes_to_bits_static([103 230 9 106 133 174 103 187 114 243 110 60 58 245 79 165])
# message word index used at position i of round r is SCHEDULE[16 * r + i],
# the message permutation applied r times
static SCHEDULE = [
  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
]

# rows of the batch, see blake3_g
let cv_lo = loop_flat_map! B, j {
  slice((clone! cv), 256 * j, 128)
}
let cv_hi = loop_flat_map! B, j {
  slice((clone! cv), 256 * j + 128, 128)
}
let iv_row = loop_flat_map! B {
  clone! IV4
}
let mut v = concat! (clone! cv_lo), (clone! cv_hi), iv_row, params

loop! 7, r {
  # column step with message words s[0..8], diagonal step with s[8..16]
  let mx = loop_flat_map! B, j {
    loop_flat_map! 4, i {
      slice((clone! block), 512 * j + 32 * SCHEDULE[16 * r + 2 * i], 32)
    }
  }
  let my = loop_flat_map! B, j {
    loop_flat_map! 4, i {
      slice((clone! block), 512 * j + 32 * SCHEDULE[16 * r + 2 * i + 1], 32)
    }
  }
  let dx = loop_flat_map! B, j {
    loop_flat_map! 4, i {
      slice((clone! block), 512 * j + 32 * SCHEDULE[16 * r + 2 * i + 8], 32)
    }
  }
  let dy = loop_flat_map! B, j {
    loop_flat_map! 4, i {
      slice((clone! block), 512 * j + 32 * SCHEDULE[16 * r + 2 * i + 9], 32)
    }
  }
  v = blake3_g(v, mx, my)
  v = blake3_rotate_rows(blake3_g(blake3_rotate_rows(v, 1, 2, 3), dx, dy), 3, 2, 1)
}

static w = 128 * B
# words 0..8 are v[i] ^ v[i + 8], words 8..16 are v[i + 8] ^ cv[i]
let mut lo[w]
let mut hi[w]
let mut lo_ext[w]
let mut hi_ext[w]
if_eq! fresh, 1 {
  lo = xor_bits_vec_wtns(slice((clone! v), 0, w), slice((clone! v), 2 * w, w))
  hi = xor_bits_vec_wtns(slice((clone! v), w, w), slice((clone! v), 3 * w, w))
  if_eq! out_words, 16 {
    lo_ext = xor_bits_vec_wtns(slice((clone! v), 2 * w, w), cv_lo)
    hi_ext = xor_bits_vec_wtns(slice((clone! v), 3 * w, w), cv_hi)
  }
}
if_eq! fresh, 0 {
  lo = xor_bits_vec(slice((clone! v), 0, w), slice((clone! v), 2 * w, w))
  hi = xor_bits_vec(slice((clone! v), w, w), slice((clone! v), 3 * w, w))
  if_eq! out_words, 16 {
    lo_ext = xor_bits_vec(slice((clone! v), 2 * w, w), cv_lo)
    hi_ext = xor_bits_vec(slice((clone! v), 3 * w, w), cv_hi)
  }
}

loop_flat_map! B, j {
  let out = concat! slice((clone! lo), 128 * j, 128), slice((clone! hi), 128 * j, 128), slice((clone! lo_ext), 128 * j, 128), slice((clone! hi_ext), 128 * j, 128)
  slice(out, 0, 32 * out_words)
}