// File: BU-DiSC[P]BF-Shared-Hashing/hash/sha-256.c
// Function: calc_sha_256(unsigned char*, void const*, unsigned long)
void calc_sha_256(uint8_t hash[32], const void * input, size_t len)
{
	/*
	 * Note 1: All integers (expect indexes) are 32-bit unsigned integers and addition is calculated modulo 2^32.
	 * Note 2: For each round, there is one round constant k[i] and one entry in the message schedule array w[i], 0 = i = 63
	 * Note 3: The compression function uses 8 working variables, a through h
	 * Note 4: Big-endian convention is used when expressing the constants in this pseudocode,
	 *     and when parsing message block data from bytes to words, for example,
	 *     the first word of the input message "abc" after padding is 0x61626380
	 */

	/*
	 * Initialize hash values:
	 * (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
	 */
	uint32_t h[] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
	unsigned i, j;

	/* 512-bit chunks is what we will operate on. */
	uint8_t chunk[64];

	struct buffer_state state;

	init_buf_state(&state, input, len);

	while (calc_chunk(chunk, &state)) {
		uint32_t ah[8];

		const uint8_t *p = chunk;

		/* Initialize working variables to current hash value: */
		for (i = 0; i < 8; i++)
			ah[i] = h[i];

		/* Compression function main loop: */
		for (i = 0; i < 4; i++) {
			/*
			 * The w-array is really w[64], but since we only need
			 * 16 of them at a time, we save stack by calculating
			 * 16 at a time.
			 *
			 * This optimization was not there initially and the
			 * rest of the comments about w[64] are kept in their
			 * initial state.
			 */

			/*
			 * create a 64-entry message schedule array w[0..63] of 32-bit words
			 * (The initial values in w[0..63] don't matter, so many implementations zero them here)
			 * copy chunk into first 16 words w[0..15] of the message schedule array
			 */
			uint32_t w[16];

			for (j = 0; j < 16; j++) {
				if (i == 0) {
					w[j] = (uint32_t) p[0] << 24 | (uint32_t) p[1] << 16 |
						(uint32_t) p[2] << 8 | (uint32_t) p[3];
					p += 4;
				} else {
					/* Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array: */
					const uint32_t s0 = right_rot(w[(j + 1) & 0xf], 7) ^ right_rot(w[(j + 1) & 0xf], 18) ^ (w[(j + 1) & 0xf] >> 3);
					const uint32_t s1 = right_rot(w[(j + 14) & 0xf], 17) ^ right_rot(w[(j + 14) & 0xf], 19) ^ (w[(j + 14) & 0xf] >> 10);
					w[j] = w[j] + s0 + w[(j + 9) & 0xf] + s1;
				}
				const uint32_t s1 = right_rot(ah[4], 6) ^ right_rot(ah[4], 11) ^ right_rot(ah[4], 25);
				const uint32_t ch = (ah[4] & ah[5]) ^ (~ah[4] & ah[6]);
				const uint32_t temp1 = ah[7] + s1 + ch + k[i << 4 | j] + w[j];
				const uint32_t s0 = right_rot(ah[0], 2) ^ right_rot(ah[0], 13) ^ right_rot(ah[0], 22);
				const uint32_t maj = (ah[0] & ah[1]) ^ (ah[0] & ah[2]) ^ (ah[1] & ah[2]);
				const uint32_t temp2 = s0 + maj;

				ah[7] = ah[6];
				ah[6] = ah[5];
				ah[5] = ah[4];
				ah[4] = ah[3] + temp1;
				ah[3] = ah[2];
				ah[2] = ah[1];
				ah[1] = ah[0];
				ah[0] = temp1 + temp2;
			}
		}

		/* Add the compressed chunk to the current hash value: */
		for (i = 0; i < 8; i++)
			h[i] += ah[i];
	}

	/* Produce the final hash value (big-endian): */
	for (i = 0, j = 0; i < 8; i++)
	{
		hash[j++] = (uint8_t) (h[i] >> 24);
		hash[j++] = (uint8_t) (h[i] >> 16);
		hash[j++] = (uint8_t) (h[i] >> 8);
		hash[j++] = (uint8_t) h[i];
	}
}

// Assembly:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x150(%rsp)
movq %rsi, 0x148(%rsp)
movq %rdx, 0x140(%rsp)
movq 0x18dba(%rip), %rax     # 0x65450
movq %rax, 0x120(%rsp)
movq 0x18db3(%rip), %rax     # 0x65458
movq %rax, 0x128(%rsp)
movq 0x18dac(%rip), %rax     # 0x65460
movq %rax, 0x130(%rsp)
movq 0x18da5(%rip), %rax     # 0x65468
movq %rax, 0x138(%rsp)
movq 0x148(%rsp), %rsi
movq 0x140(%rsp), %rdx
leaq 0xb0(%rsp), %rdi
callq 0x4cc30
leaq 0xd0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x4cc90
cmpl $0x0, %eax
je 0x4cb1e
leaq 0xd0(%rsp), %rax
movq %rax, 0x88(%rsp)
movl $0x0, 0x11c(%rsp)
cmpl $0x8, 0x11c(%rsp)
jae 0x4c75a
movl 0x11c(%rsp), %eax
movl 0x120(%rsp,%rax,4), %ecx
movl 0x11c(%rsp), %eax
movl %ecx, 0x90(%rsp,%rax,4)
movl 0x11c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x11c(%rsp)
jmp 0x4c721
movl $0x0, 0x11c(%rsp)
cmpl $0x4, 0x11c(%rsp)
jae 0x4cace
movl $0x0, 0x118(%rsp)
cmpl $0x10, 0x118(%rsp)
jae 0x4cab6
cmpl $0x0, 0x11c(%rsp)
jne 0x4c7f8
movq 0x88(%rsp), %rax
movzbl (%rax), %ecx
shll $0x18, %ecx
movq 0x88(%rsp), %rax
movzbl 0x1(%rax), %eax
shll $0x10, %eax
orl %eax, %ecx
movq 0x88(%rsp), %rax
movzbl 0x2(%rax), %eax
shll $0x8, %eax
orl %eax, %ecx
movq 0x88(%rsp), %rax
movzbl 0x3(%rax), %eax
orl %eax, %ecx
movl 0x118(%rsp), %eax
movl %ecx, 0x40(%rsp,%rax,4)
movq 0x88(%rsp), %rax
addq $0x4, %rax
movq %rax, 0x88(%rsp)
jmp 0x4c8ed
movl 0x118(%rsp), %eax
addl $0x1, %eax
andl $0xf, %eax
movl %eax, %eax
movl 0x40(%rsp,%rax,4), %edi
movl $0x7, %esi
callq 0x4ce80
movl %eax, 0x18(%rsp)
movl 0x118(%rsp), %eax
addl $0x1, %eax
andl $0xf, %eax
movl %eax, %eax
movl 0x40(%rsp,%rax,4), %edi
movl $0x12, %esi
callq 0x4ce80
movl %eax, %ecx
movl 0x18(%rsp), %eax
xorl %ecx, %eax
movl 0x118(%rsp), %ecx
addl $0x1, %ecx
andl $0xf, %ecx
movl %ecx, %ecx
movl 0x40(%rsp,%rcx,4), %ecx
shrl $0x3, %ecx
xorl %ecx, %eax
movl %eax, 0x3c(%rsp)
movl 0x118(%rsp), %eax
addl $0xe, %eax
andl $0xf, %eax
movl %eax, %eax
movl 0x40(%rsp,%rax,4), %edi
movl $0x11, %esi
callq 0x4ce80
movl %eax, 0x1c(%rsp)
movl 0x118(%rsp), %eax
addl $0xe, %eax
andl $0xf, %eax
movl %eax, %eax
movl 0x40(%rsp,%rax,4), %edi
movl $0x13, %esi
callq 0x4ce80
movl %eax, %ecx
movl 0x1c(%rsp), %eax
xorl %ecx, %eax
movl 0x118(%rsp), %ecx
addl $0xe, %ecx
andl $0xf, %ecx
movl %ecx, %ecx
movl 0x40(%rsp,%rcx,4), %ecx
shrl $0xa, %ecx
xorl %ecx, %eax
movl %eax, 0x38(%rsp)
movl 0x118(%rsp), %eax
movl 0x40(%rsp,%rax,4), %ecx
addl 0x3c(%rsp), %ecx
movl 0x118(%rsp), %eax
addl $0x9, %eax
andl $0xf, %eax
movl %eax, %eax
addl 0x40(%rsp,%rax,4), %ecx
addl 0x38(%rsp), %ecx
movl 0x118(%rsp), %eax
movl %ecx, 0x40(%rsp,%rax,4)
movl 0xa0(%rsp), %edi
movl $0x6, %esi
callq 0x4ce80
movl %eax, 0x8(%rsp)
movl 0xa0(%rsp), %edi
movl $0xb, %esi
callq 0x4ce80
movl %eax, %ecx
movl 0x8(%rsp), %eax
xorl %ecx, %eax
movl %eax, 0xc(%rsp)
movl 0xa0(%rsp), %edi
movl $0x19, %esi
callq 0x4ce80
movl %eax, %ecx
movl 0xc(%rsp), %eax
xorl %ecx, %eax
movl %eax, 0x34(%rsp)
movl 0xa0(%rsp), %eax
andl 0xa4(%rsp), %eax
movl 0xa0(%rsp), %ecx
xorl $-0x1, %ecx
andl 0xa8(%rsp), %ecx
xorl %ecx, %eax
movl %eax, 0x30(%rsp)
movl 0xac(%rsp), %eax
addl 0x34(%rsp), %eax
addl 0x30(%rsp), %eax
movl 0x11c(%rsp), %ecx
shll $0x4, %ecx
orl 0x118(%rsp), %ecx
movl %ecx, %ecx
movl %ecx, %edx
leaq 0x18ae4(%rip), %rcx     # 0x65470
addl (%rcx,%rdx,4), %eax
movl 0x118(%rsp), %ecx
addl 0x40(%rsp,%rcx,4), %eax
movl %eax, 0x2c(%rsp)
movl 0x90(%rsp), %edi
movl $0x2, %esi
callq 0x4ce80
movl %eax, 0x10(%rsp)
movl 0x90(%rsp), %edi
movl $0xd, %esi
callq 0x4ce80
movl %eax, %ecx
movl 0x10(%rsp), %eax
xorl %ecx, %eax
movl %eax, 0x14(%rsp)
movl 0x90(%rsp), %edi
movl $0x16, %esi
callq 0x4ce80
movl %eax, %ecx
movl 0x14(%rsp), %eax
xorl %ecx, %eax
movl %eax, 0x28(%rsp)
movl 0x90(%rsp), %eax
andl 0x94(%rsp), %eax
movl 0x90(%rsp), %ecx
andl 0x98(%rsp), %ecx
xorl %ecx, %eax
movl 0x94(%rsp), %ecx
andl 0x98(%rsp), %ecx
xorl %ecx, %eax
movl %eax, 0x24(%rsp)
movl 0x28(%rsp), %eax
addl 0x24(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0xa8(%rsp), %eax
movl %eax, 0xac(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0xa8(%rsp)
movl 0xa0(%rsp), %eax
movl %eax, 0xa4(%rsp)
movl 0x9c(%rsp), %eax
addl 0x2c(%rsp), %eax
movl %eax, 0xa0(%rsp)
movl 0x98(%rsp), %eax
movl %eax, 0x9c(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x98(%rsp)
movl 0x90(%rsp), %eax
movl %eax, 0x94(%rsp)
movl 0x2c(%rsp), %eax
addl 0x20(%rsp), %eax
movl %eax, 0x90(%rsp)
movl 0x118(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x118(%rsp)
jmp 0x4c77e
jmp 0x4cab8
movl 0x11c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x11c(%rsp)
jmp 0x4c765
movl $0x0, 0x11c(%rsp)
cmpl $0x8, 0x11c(%rsp)
jae 0x4cb19
movl 0x11c(%rsp), %eax
movl 0x90(%rsp,%rax,4), %ecx
movl 0x11c(%rsp), %eax
addl 0x120(%rsp,%rax,4), %ecx
movl %ecx, 0x120(%rsp,%rax,4)
movl 0x11c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x11c(%rsp)
jmp 0x4cad9
jmp 0x4c6e8
movl $0x0, 0x11c(%rsp)
movl $0x0, 0x118(%rsp)
cmpl $0x8, 0x11c(%rsp)
jae 0x4cc21
movl 0x11c(%rsp), %eax
movl 0x120(%rsp,%rax,4), %eax
shrl $0x18, %eax
movb %al, %dl
movq 0x150(%rsp), %rax
movl 0x118(%rsp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x118(%rsp)
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movl 0x11c(%rsp), %eax
movl 0x120(%rsp,%rax,4), %eax
shrl $0x10, %eax
movb %al, %dl
movq 0x150(%rsp), %rax
movl 0x118(%rsp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x118(%rsp)
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movl 0x11c(%rsp), %eax
movl 0x120(%rsp,%rax,4), %eax
shrl $0x8, %eax
movb %al, %dl
movq 0x150(%rsp), %rax
movl 0x118(%rsp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x118(%rsp)
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movl 0x11c(%rsp), %eax
movl 0x120(%rsp,%rax,4), %eax
movb %al, %dl
movq 0x150(%rsp), %rax
movl 0x118(%rsp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x118(%rsp)
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movl 0x11c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x11c(%rsp)
jmp 0x4cb34
addq $0x158, %rsp            # imm = 0x158
retq
nopl (%rax)