The XSalsa20 state is generated by applying a series of operations to the key and nonce. These operations include additions, subtractions, rotations, and XORs.
Here is a pseudocode implementation of the XSalsa20 state generation algorithm:
def salsa20_core(input, output):
"""Salsa20 core function."""
x = [0] * 16
for i in range(16):
x[i] = input[i]
for i in range(8):
# Column round
x[ 4] ^= ROTL32(x[ 0]+x[12], 7)
x[ 8] ^= ROTL32(x[ 4]+x[ 0], 9)
x[12] ^= ROTL32(x[ 8]+x[ 4],13)
x[ 0] ^= ROTL32(x[12]+x[ 8],18)
x[ 9] ^= ROTL32(x[ 5]+x[ 1], 7)
x[13] ^= ROTL32(x[ 9]+x[ 5], 9)
x[ 1] ^= ROTL32(x[13]+x[ 9],13)
x[ 5] ^= ROTL32(x[ 1]+x[13],18)
x[14] ^= ROTL32(x[10]+x[ 6], 7)
x[ 2] ^= ROTL32(x[14]+x[10], 9)
x[ 6] ^= ROTL32(x[ 2]+x[14],13)
x[10] ^= ROTL32(x[ 6]+x[ 2],18)
x[ 3] ^= ROTL32(x[15]+x[11], 7)
x[ 7] ^= ROTL32(x[ 3]+x[15], 9)
x[11] ^= ROTL32(x[ 7]+x[ 3],13)
x[15] ^= ROTL32(x[11]+x[ 7],18)
# Diagonal round
x[ 1] ^= ROTL32(x[ 0]+x[ 3], 7)
x[ 2] ^= ROTL32(x[ 1]+x[ 0], 9)
x[ 3] ^= ROTL32(x[ 2]+x[ 1],13)
x[ 0] ^= ROTL32(x[ 3]+x[ 2],18)
x[ 6] ^= ROTL32(x[ 5]+x[ 4], 7)
x[ 7] ^= ROTL32(x[ 6]+x[ 5], 9)
x[ 4] ^= ROTL32(x[ 7]+x[ 6],13)
x[ 5] ^= ROTL32(x[ 4]+x[ 7],18)
x[11] ^= ROTL32(x[10]+x[ 9], 7)
x[ 8] ^= ROTL32(x[11]+x[10], 9)
x[ 9] ^= ROTL32(x[ 8]+x[11],13)
x[10] ^= ROTL32(x[ 9]+x[ 8],18)
x[12] ^= ROTL32(x[15]+x[14], 7)
x[13] ^= ROTL32(x[12]+x[15], 9)
x[14] ^= ROTL32(x[13]+x[12],13)
x[15] ^= ROTL32(x[14]+x[13],18)
for i in range(16):
output[i] = x[i] + input[i]
The ROTL32 function rotates a 32-bit value to the left by the specified number of bits.
The XSalsa20 state is generated by calling the salsa20_core function twice, with different constants. The first call uses the constants [0x61707865, 0x3320646e, 0x79622d32, 0x6b206574], and the second call uses the constants [0x766f6d70, 0x2c207361, 0x6c736120, 0x23313234].
The output of the second call to salsa20_core is the final XSalsa20 state.