Index Mathematics Conjectures nbv lettuce Widgets

chacha_block

stdlib/chacha/chacha_block.nbv

chacha_block(static rounds, key, iv)

ChaCha block function with rounds rounds (20 for ChaCha20, 8 for ChaCha8) on bit vectors, returning the 512-bit keystream block.

Words are 32 little-endian bits and word i of the state and of the output is at bits 32 i .. 32 i + 32, so this is the byte order of the key, nonce and keystream with the least significant bit of each byte first (see bytes_to_bits). key is 256 bits and iv the 128 bits of state words 12..16: a 32-bit block counter followed by a 96-bit nonce for RFC 8439, or a 64-bit counter followed by a 64-bit nonce for the original ChaCha.

Inputs must be bits. Outputs are bits.

Calls
bits/add32_bits, bits/bytes_to_bits_static, chacha/chacha_quarter_round, slice
Precompiles
assert_eq!, clone!, concat!, div_exact!, len!, loop!, modulo!
Used by
chacha/chacha20_block, chacha/chacha8_block, typed/typed_lex_chacha

Source

# ChaCha block function with `rounds` rounds (20 for ChaCha20, 8 for ChaCha8)
# on bit vectors, returning the 512-bit keystream block.
#
# Words are 32 little-endian bits and word i of the state and of the output
# is at bits 32 i .. 32 i + 32, so this is the byte order of the key, nonce
# and keystream with the least significant bit of each byte first (see
# bytes_to_bits). `key` is 256 bits and `iv` the 128 bits of state words
# 12..16: a 32-bit block counter followed by a 96-bit nonce for RFC 8439, or
# a 64-bit counter followed by a 64-bit nonce for the original ChaCha.
#
# Inputs must be bits. Outputs are bits.
(static rounds, key, iv)

assert_eq! 256, len! key
assert_eq! 128, len! iv
assert_eq! 0, (modulo! rounds, 2)

# "expand 32-byte k"
static SIGMA = bytes_to_bits_static([101 120 112 97 110 100 32 51 50 45 98 121 116 101 32 107])

let input = concat! SIGMA, key, iv
let mut x = clone! input
loop! (div_exact! rounds, 2) {
  # column round
  x = chacha_quarter_round(x)
  # diagonal round: rotate rows b, c and d left by 1, 2 and 3 words so the
  # diagonals line up as columns, then rotate them back
  let y = chacha_quarter_round(concat! slice((clone! x), 0, 128), slice((clone! x), 160, 96), slice((clone! x), 128, 32), slice((clone! x), 320, 64), slice((clone! x), 256, 64), slice((clone! x), 480, 32), slice((clone! x), 384, 96))
  x = concat! slice((clone! y), 0, 128), slice((clone! y), 224, 32), slice((clone! y), 128, 96), slice((clone! y), 320, 64), slice((clone! y), 256, 64), slice((clone! y), 416, 96), slice((clone! y), 384, 32)
}

add32_bits(x + input, 2)