#include <stdint.h>
#include <stdio.h>
#define ROTL(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
#define QR(a, b, c, d) ( \
a += b, d ^= a, d = ROTL(d, 16), \
c += d, b ^= c, b = ROTL(b, 12), \
a += b, d ^= a, d = ROTL(d, 8), \
c += d, b ^= c, b = ROTL(b, 7))
#define ROUNDS 20
void chacha_block(uint32_t out[16], uint32_t const in[16])
{
int i;
uint32_t x[16];
for (i = 0; i < 16; ++i)
x[i] = in[i];
// 10 loops × 2 rounds/loop = 20 rounds
for (i = 0; i < ROUNDS; i += 2) {
// Odd round
QR(x[0], x[4], x[ 8], x[12]); // column 1
QR(x[1], x[5], x[ 9], x[13]); // column 2
QR(x[2], x[6], x[10], x[14]); // column 3
QR(x[3], x[7], x[11], x[15]); // column 4
// Even round
QR(x[0], x[5], x[10], x[15]); // diagonal 1 (main diagonal)
QR(x[1], x[6], x[11], x[12]); // diagonal 2
QR(x[2], x[7], x[ 8], x[13]); // diagonal 3
QR(x[3], x[4], x[ 9], x[14]); // diagonal 4
}
for (i = 0; i < 16; ++i)
out[i] = x[i] + in[i];
}
void generate_pseudorandom_truths(uint32_t seed, int count, int truths[])
{
uint32_t input[16] = {
0x61707865, 0x3320646e, 0x79622d32, 0x6b206574,
0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c,
0x13121110, 0x17161514, 0x1b1a1918, 0x1f1e1d1c,
0x00000001, seed, 0x4a000000, 0x00000000
};
uint32_t output[16];
int i, j;
for (i = 0; i < count; ++i) {
chacha_block(output, input);
for (j = 0; j < 16 && i < count; ++j, ++i) {
truths[i] = output[j] & 1; // Convert to boolean
}
}
}
int main(void)
{
int count = 16; // Number of truth values to generate
uint32_t seed = 0x31515433; // Example seed
int truths[count];
int i, j;
for (j = 0; j < 100; ++j) { // Loop n times
printf("Iteration %d:\n", j+1);
seed -= 133;
generate_pseudorandom_truths(seed, count, truths);
printf("Pseudorandom truth values:\n");
for (i = 0; i < count; ++i) {
printf("%d ", truths[i]);
if ((i & 15) == 15) printf("\n");
}
}
return 0;
}