ChaCha20 in Brainfuck
ChaCha20 is a secure stream cipher that generates a pseudo-random keystream through 20 rounds of operations on a 4x4 matrix of 32-bit integers, which is then XORed with plaintext to produce ciphertext. Brainfuck, an esoteric programming language known for its minimalism, has been used to implement a ChaCha20 encryptor consisting of over 140,000 instructions. The implementation is presented as a web-based tool requiring JavaScript, allowing users to observe the encryption process in action.
- ▪ChaCha20 operates on a 4x4 matrix of 32-bit integers and applies 20 rounds of quarter-round functions to generate a keystream.
- ▪The quarter-round function involves a series of additions, XORs, and bit rotations on four variables.
- ▪The final keystream is produced by adding the initial matrix state to the final state after all rounds.
- ▪Brainfuck uses a tape-based memory model and eight simple instructions to manipulate data and control program flow.
- ▪The ChaCha20 algorithm in Brainfuck was implemented using over 140,000 instructions and is executable in a web browser with JavaScript enabled.
Opening excerpt (first ~120 words) tap to expand
ChaCha20 is an electronic cipher thought to be secure against advanced adversaries. It computes a pseudo-random bitstream, the keystream, by operating on a 4x4 matrix of 32-bit integers. The core consists of the quarter-round function, which takes in four variables and kneads them like so: a = a plus b, d = d xor a, d = d rol 16 c = c plus d, b = b xor c, b = b rol 12 a = a plus b, d = d xor a, d = d rol 8 c = c plus d, b = b xor c, b = b rol 7 This quarter-round function is applied to each column of the matrix, giving you one round. The second round applies the same quarter-round function to each diagonal of the matrix. Subsequent rounds alternate between columns and diagonals. Initially, this matrix holds the key, block counter, nonce (one-time number) and a cipher-wide constant.
…
Excerpt limited to ~120 words for fair-use compliance. The full article is at mid's site.