<commit_before>#include <string.h>
#include <stdio.h>

#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif

typedef union {
	unsigned char 		bytes[8];
	long long unsigned int 	value;
} block_t;

typedef struct {
	unsigned char 	 	bytes[10];
} key_t;

#define test_bit(x,n)	x.bytes[(sizeof(x.bytes)-1)-(n/CHAR_BIT)] &  (1<<(n%CHAR_BIT))
#define set_bit(x,n)	x.bytes[(sizeof(x.bytes)-1)-(n/CHAR_BIT)] |= (1<<(n%CHAR_BIT))

const unsigned char sbox[16] = {
	0xC, 0x5, 0x6, 0xB, 0x9, 0x0, 0xA, 0xD, 0x3, 0xE, 0xF, 0x8, 0x4, 0x7, 0x1, 0x2
};
const unsigned char inverse_sbox[16] = {
	0x5, 0xE, 0xF, 0x8, 0xC, 0x1, 0x2, 0xD, 0xB, 0x4, 0x6, 0x3, 0x0, 0x7, 0x9, 0xA
};
const unsigned char *active_sbox = NULL;

unsigned int round_counter;
key_t 	key;
block_t roundkey;
block_t state;

void rotate_left_i(int i){
	key_t newkey;
	unsigned int bit;
	memset(newkey.bytes, 0x0, 10);
	for(bit = 0; bit < 80; bit++){
		if(test_bit(key,bit)){
			set_bit(newkey,(bit+i)%80);
		}
	}
	memcpy(key.bytes, newkey.bytes, 10);
}

void schedule_key(void){
	/* 3. [k38k37k36k35k34] ^= round_counter - also stolen from Zhu/Gong */
	key.bytes[5] ^= round_counter << 2;

	/* 1. [k79k78..k1k0] = [k18k17..k20k19] */
	rotate_left_i(61);
	
	/* 2. [k79k78k77k76] = S[k79k78k77k76] */
	/* sbox(high nibble) | low nibble */
	key.bytes[0] = (active_sbox[key.bytes[0] >> 4] << 4) | (key.bytes[0] & 0xf);
	/* Zhu/Gong eliminated the left shift by making their s-box produce upper nibbles */

	/* Ki = K63K62..K0 = k79k78..k16 */
	memcpy(roundkey.bytes, key.bytes, 8);
}

void addRoundKey(void){
	state.value ^= roundkey.value;
}

void sBoxLayer(void){
	unsigned int i;
	for(i = 0; i < 8; i++){
		/* sbox(high nibble) | sbox(low nibble) */
		state.bytes[i] = (active_sbox[state.bytes[i] >> 4] << 4) | active_sbox[(state.bytes[i] & 0xf)];
	}
}

void pLayer(void){
	block_t newstate;
	unsigned int bit;
	newstate.value = 0;

	for(bit = 0; bit < 64; bit++){
		if(test_bit(state,bit)){
			/* Kostas figured out the pattern */
			set_bit(newstate,((16 * (bit % 4)) + (bit / 4)));
		}
	}
	state.value = newstate.value;
}

void setup(void){
	active_sbox = sbox;
	memcpy(roundkey.bytes, key.bytes, 8);
}

void encrypt(void){
	setup();
	for(round_counter = 1; round_counter < 32; round_counter++){
		addRoundKey();
		sBoxLayer();
		pLayer();
		schedule_key();
	}
	addRoundKey();
}

void decrypt(void){
	setup();
schedule_last_key:
	for(round_counter = 1; round_counter < 32; round_counter++){
		schedule_key();
	}
	active_sbox = inverse_sbox;
	for(round_counter = 31; round_counter > 0; round_counter--){
		addRoundKey();
		pLayer();
		pLayer();
		sBoxLayer();

	inverse_schedule_key:
		key.bytes[0] = (active_sbox[key.bytes[0] >> 4] << 4) | (key.bytes[0] & 0xf);
		rotate_left_i(19);
		key.bytes[5] ^= round_counter << 2;
		memcpy(roundkey.bytes, key.bytes, 8);
	}
	addRoundKey();
}

void print_block(block_t block){
	printf("%02x%02x%02x%02x %02x%02x%02x%02x\n",
		block.bytes[0], block.bytes[1], block.bytes[2], block.bytes[3],
		block.bytes[4], block.bytes[5], block.bytes[6], block.bytes[7]
	);
}

void test(int keyval, int input){
	memset(key.bytes, keyval, 10);
	state.value = input;
	printf("Plaintext:  ");
	print_block(state);
	encrypt();
	printf("Ciphertext: ");
	print_block(state);
	memset(key.bytes, keyval, 10);
	decrypt();
	printf("Plaintext:  ");
	print_block(state);
	printf("\n");
}

int main(int argc, char* argv[]){
	test(0,0);
	test(-1,0);
	test(0,-1);
	test(-1,-1);

	return 0;
}

<commit_msg>Updated C version to comply more with asm<commit_after>#include <string.h>
#include <stdio.h>

#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif

typedef union {
	unsigned char 		bytes[8];
	long long unsigned int 	value;
} block_t;

typedef struct {
	unsigned char 	 	bytes[10];
} key_t;

#define test_bit(x,n)	x.bytes[(sizeof(x.bytes)-1)-(n/CHAR_BIT)] &  (1<<(n%CHAR_BIT))
#define set_bit(x,n)	x.bytes[(sizeof(x.bytes)-1)-(n/CHAR_BIT)] |= (1<<(n%CHAR_BIT))

const unsigned char sbox[16] = {
	0xC, 0x5, 0x6, 0xB, 0x9, 0x0, 0xA, 0xD, 0x3, 0xE, 0xF, 0x8, 0x4, 0x7, 0x1, 0x2
};
const unsigned char inverse_sbox[16] = {
	0x5, 0xE, 0xF, 0x8, 0xC, 0x1, 0x2, 0xD, 0xB, 0x4, 0x6, 0x3, 0x0, 0x7, 0x9, 0xA
};
const unsigned char *active_sbox = NULL;

unsigned int round_counter;
key_t 	key;
block_t roundkey;
block_t state;

void rotate_left_i(int i){
	key_t newkey;
	unsigned int bit;
	memset(newkey.bytes, 0x0, 10);
	for(bit = 0; bit < 80; bit++){
		if(test_bit(key,bit)){
			set_bit(newkey,(bit+i)%80);
		}
	}
	memcpy(key.bytes, newkey.bytes, 10);
}

void schedule_key(void){
	/* 3. [k38k37k36k35k34] ^= round_counter - also stolen from Zhu/Gong */
	key.bytes[5] ^= round_counter << 2;

	/* 1. [k79k78..k1k0] = [k18k17..k20k19] */
	rotate_left_i(61);
	
	/* 2. [k79k78k77k76] = S[k79k78k77k76] */
	/* sbox(high nibble) | low nibble */
	key.bytes[0] = (active_sbox[key.bytes[0] >> 4] << 4) | (key.bytes[0] & 0xf);
	/* Zhu/Gong eliminated the left shift by making their s-box produce upper nibbles */

	/* Ki = K63K62..K0 = k79k78..k16 */
	memcpy(roundkey.bytes, key.bytes, 8);
}

void addRoundKey(void){
	state.value ^= roundkey.value;
}

void sBoxLayer(void){
	unsigned int i;
	for(i = 0; i < 8; i++){
		/* sbox(high nibble) | sbox(low nibble) */
		state.bytes[i] = (active_sbox[state.bytes[i] >> 4] << 4) | active_sbox[(state.bytes[i] & 0xf)];
	}
}

void pLayer(void){
	block_t newstate;
	unsigned int bit;
	newstate.value = 0;

	for(bit = 0; bit < 64; bit++){
		if(test_bit(state,bit)){
			/* Kostas figured out the pattern */
			set_bit(newstate,((16 * (bit % 4)) + (bit / 4)));
		}
	}
	state.value = newstate.value;
}

void setup(void){
	active_sbox = sbox;
	memcpy(roundkey.bytes, key.bytes, 8);
}

void encrypt(void){
	setup();
	for(round_counter = 1; round_counter < 32; round_counter++){
		addRoundKey();
		sBoxLayer();
		pLayer();
		schedule_key();
	}
	addRoundKey();
}

void decrypt(void){
	setup();
schedule_last_key:
	for(round_counter = 1; round_counter < 32; round_counter++){
		schedule_key();
	}
	active_sbox = inverse_sbox;
	for(round_counter = 31; round_counter > 0; round_counter--){
		addRoundKey();
		pLayer();
		pLayer();
		sBoxLayer();

	inverse_schedule_key:
		key.bytes[0] = (active_sbox[key.bytes[0] >> 4] << 4) | (key.bytes[0] & 0xf);
		rotate_left_i(19);
		key.bytes[5] ^= round_counter << 2;
		memcpy(roundkey.bytes, key.bytes, 8);
	}
	addRoundKey();
}

void print_block(block_t block){
	printf("%02x%02x%02x%02x %02x%02x%02x%02x\n",
		block.bytes[0], block.bytes[1], block.bytes[2], block.bytes[3],
		block.bytes[4], block.bytes[5], block.bytes[6], block.bytes[7]
	);
}

void print_key(key_t key){
	printf("%02x%02x%02x%02x %02x%02x%02x%02x\n",
		key.bytes[0], key.bytes[1], key.bytes[2], key.bytes[3],
		key.bytes[4], key.bytes[5], key.bytes[6], key.bytes[7],
		key.bytes[8], key.bytes[9]
	);
}

void test(int keyval, int input){
	memset(key.bytes, keyval, 10);
	state.value = input;
	printf("Key:        ");
	print_key(key);
	printf("Plaintext:  ");
	print_block(state);
	encrypt();
	printf("Ciphertext: ");
	print_block(state);
	memset(key.bytes, keyval, 10);
	decrypt();
	printf("Plaintext:  ");
	print_block(state);
	printf("\n");
}

int main(int argc, char* argv[]){
	test(0,0);
	test(-1,0);
	test(0,-1);
	test(-1,-1);

	return 0;
}


{'fraction_non_alphanumeric': 0.1908788466869975, 'fraction_numerical': 0.06210146936512337, 'mean_word_length': 4.324723247232472, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3325753', 'n_tokens_mistral': 3511, 'n_tokens_neox': 3158, 'n_words': 644}