// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/primatesv1hanuman80/ref/encrypt.cpp
// Function: crypto_aead_decrypt
int crypto_aead_decrypt(
    unsigned char *m,unsigned long long *mlen,
    unsigned char *nsec,
    const unsigned char *c,unsigned long long clen,
    const unsigned char *ad,unsigned long long adlen,
    const unsigned char *npub,
    const unsigned char *k
)
{
	/*
    ... generating a plaintext m[0],m[1],...,m[*mlen-1]
    ... and secret message number nsec[0],nsec[1],...
    ... from a ciphertext c[0],c[1],...,c[clen-1]
    ... and associated data ad[0],ad[1],...,ad[adlen-1]
    ... and public message number npub[0],npub[1],...
    ... and secret key k[0],k[1],...
    ...
	*/
	unsigned char V[StateSize]; //the state represented in Elements
	//Temp variables represented as elements
	unsigned char key[KeySizeElements];
	unsigned char nonce[NonceSizeElements];
	unsigned char tag[TagSizeElements];
	unsigned char data[RateSizeElements];
	unsigned char cipher[RateSizeElements];
	
	*mlen=clen-TagSizeBytes;
	
	if(clen<TagSizeBytes) return -1; //invalid ciphertext
   
	//a temporary array for message
	unsigned char *mT = (unsigned char*) malloc(clen-TagSizeBytes);
    if (mT == NULL) return -2; // memory allocation failure
	
	Bytes2Element(key,k,0,KeySizeElements); //representing k as elements
	Bytes2Element(nonce,npub,0,NonceSizeElements); //representing npub as elements

	// V <-- p_1(O||K||N)
	InitializeState(V,key,nonce);
	p_1(V); 
	
	if(adlen!=0)
	{ 
		// for i=1 to u-1
		if(adlen>RateSizeBytes)
		{
			for(unsigned long long i=0; i<adlen-RateSizeBytes; i=i+RateSizeBytes)
			{
				Bytes2Element(data,ad,i,RateSizeElements); // representing a block of ad as elements
				// V <-- p_4 (A[i]^V_r || V_c)
				for(int j=0; j<RateSizeElements; j++)
					V[j]^=data[j];
				p_4(V);
			}
		}
		// V <-- A[u]^v_r || V_c
		int l = adlen%RateSizeBytes;
		if(l==0){ //do padding with spill over			
			Bytes2Element(data,ad,adlen-RateSizeBytes,RateSizeElements);
			for(int i=0; i<RateSizeElements; i++)
				V[i]^=data[i];
			V[RateSizeElements]^=16; //padding spills over to capacity
		}
		else{ //do standard padding
			unsigned char dataP[RateSizeBytes];
			for(int i=0; i< l; i++)
				dataP[i]=ad[adlen-l+i];
			dataP[l]=128; //padding over bytes
			for(int i=l+1; i<RateSizeBytes; i++)
				dataP[i]=0;
			Bytes2Element(data,dataP,0,RateSizeElements);
			for(int i=0; i<RateSizeElements; i++)
				V[i]^=data[i];
		}
		// V <-- p_1(V)
		p_1(V);	
	}

	//for i=1 to w-1 (except the last block which needs padding)
	if(clen-TagSizeBytes>RateSizeBytes)
	{
		for(unsigned long long i=0; i<clen-(TagSizeBytes+RateSizeBytes); i=i+RateSizeBytes)
		{ 
			Bytes2Element(cipher,c,i,RateSizeElements); //representing a block of c as elements
			// M[i] <-- C[i]^V_r
			for(int j=0; j<RateSizeElements; j++)
				data[j]=V[j]^cipher[j];
			Element2Bytes(mT,data,i,RateSizeBytes); //representing m as bytes
			//V <-- p_1 (C[i] || V_c)
			for(int j=0; j<RateSizeElements; j++)
				V[j]=cipher[j];
			p_1(V);
		}
	}
	//for i=w
	int l = clen%RateSizeBytes;
	// M[w] <-- C[w]^V_r
	// V <-- M[w]||10* ^ V
	if((l==0) && ((clen-TagSizeBytes)!=0)){ //do padding with spill over			
		Bytes2Element(cipher,c,clen-(TagSizeBytes+RateSizeBytes),RateSizeElements);
		for(int i=0; i<RateSizeElements; i++){
			data[i]=V[i]^cipher[i];
			V[i]=cipher[i];
		}
		Element2Bytes(mT,data,clen-(TagSizeBytes+RateSizeBytes),RateSizeBytes); //representing last block m as bytes
		V[RateSizeElements]^=16; //padding spills over to capacity
	}
	else{ //do standard padding
		unsigned char cipherP[RateSizeElements];
		unsigned char mtmp[RateSizeBytes];
		for(int i=0; i< RateSizeElements; i++)
			cipherP[i]=V[i];
		Element2Bytes(mtmp,cipherP,0,RateSizeBytes);
		for(int i=0; i< l; i++)
			mT[clen-TagSizeBytes-l+i]=mtmp[i]^c[clen-TagSizeBytes-l+i];		
		unsigned char dataP[RateSizeBytes];
		for(int i=0; i< l; i++)
			dataP[i]=mT[clen-TagSizeBytes-l+i];
		dataP[l]=128; //padding over bytes
		for(int i=l+1; i<RateSizeBytes; i++)
			dataP[i]=0;
		Bytes2Element(data,dataP,0,RateSizeElements);
		for(int i=0; i<RateSizeElements; i++)
			V[i]^=data[i];
	}
	//V <-- p_1 (V)
	p_1(V);

	// T <-- (V_c)_c/2 ^ K
	for(int i=0; i<TagSizeElements; i++)
		tag[i]=V[RateSizeElements+i]^key[i]; 
	unsigned char tagT[TagSizeBytes];
	Element2Bytes(tagT,tag,0,TagSizeBytes) ;

	unsigned char cor=0;
	for(int i=0; i<TagSizeBytes; i++) //check if tags match
		cor = cor || (tagT[i]^c[clen-TagSizeBytes+i]);

	if(cor==0) //if tags match release the message
		for(unsigned long long i=0; i<clen-TagSizeBytes; i++)
			m[i]=mT[i];

	free(mT);
    if(cor==0) return 0;
	else return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r8, %r14
subq $0xa, %r14
movq %r14, (%rsi)
jae 0x1635
pushq $-0x1
jmp 0x176d
movq %r9, %rbp
movq %r8, %r12
movq %rcx, %r15
movq %rdi, %rbx
movq %r14, %rdi
callq 0x10a0
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x176b
movq %r12, 0x20(%rsp)
movq %r15, 0x28(%rsp)
movq %r14, 0x88(%rsp)
movq %rbx, 0x80(%rsp)
movq 0x100(%rsp), %rsi
movq 0xf8(%rsp), %rbx
movq 0xf0(%rsp), %r13
leaq 0x90(%rsp), %r14
pushq $0x10
popq %r15
movq %r14, %rdi
xorl %edx, %edx
movl %r15d, %ecx
callq 0x1160
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movl %r15d, %ecx
callq 0x1160
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x10d0
movq %rbx, %rdi
callq 0x1070
testq %r13, %r13
je 0x17f7
cmpq $0x6, %r13
jb 0x1736
movq 0xf0(%rsp), %rax
leaq -0x5(%rax), %r13
xorl %ebx, %ebx
movq %rsp, %r14
pushq $0x8
popq %r15
leaq 0x40(%rsp), %r12
cmpq %r13, %rbx
jae 0x1736
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movl %r15d, %ecx
callq 0x1160
xorl %eax, %eax
cmpq $0x8, %rax
je 0x1728
movb (%rsp,%rax), %cl
xorb %cl, 0x40(%rsp,%rax)
incq %rax
jmp 0x1716
movq %r12, %rdi
callq 0x1050
addq $0x5, %rbx
jmp 0x16fe
movq 0xf0(%rsp), %r8
movq %r8, %rax
xorl %edx, %edx
pushq $0x5
popq %rcx
divq %rcx
testq %rdx, %rdx
je 0x17bf
movq %r8, %rax
subq %rdx, %rax
addq %rax, %rbp
xorl %eax, %eax
cmpq %rax, %rdx
je 0x1773
movb (%rbp,%rax), %cl
movb %cl, 0x30(%rsp,%rax)
incq %rax
jmp 0x1759
pushq $-0x2
popq %rax
jmp 0x19e8
movq %r8, %rax
xorl %edx, %edx
pushq $0x5
popq %rcx
divq %rcx
leaq (%rax,%rax,4), %rax
notq %rax
addq %r8, %rax
movb $-0x80, %cl
movb %cl, 0x31(%rsp,%rax)
incq %rax
xorl %ecx, %ecx
cmpq $0x4, %rax
jb 0x178a
xorl %ebx, %ebx
movq %rsp, %rdi
leaq 0x30(%rsp), %rsi
pushq $0x8
popq %rcx
xorl %edx, %edx
callq 0x1160
cmpq $0x8, %rbx
je 0x17ed
movb (%rsp,%rbx), %al
xorb %al, 0x40(%rsp,%rbx)
incq %rbx
jmp 0x17ad
addq $-0x5, %r8
movq %rsp, %rdi
pushq $0x8
popq %rcx
movq %rbp, %rsi
movq %r8, %rdx
callq 0x1160
xorl %eax, %eax
cmpq $0x8, %rax
je 0x17e8
movb (%rsp,%rax), %cl
xorb %cl, 0x40(%rsp,%rax)
incq %rax
jmp 0x17d6
xorb $0x10, 0x48(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x1070
cmpq $0x6, 0x88(%rsp)
jb 0x1888
movq 0x20(%rsp), %rax
leaq -0xf(%rax), %rbp
xorl %r13d, %r13d
pushq $0x8
popq %rbx
movq %rsp, %r14
pushq $0x5
popq %r15
leaq 0x40(%rsp), %r12
cmpq %rbp, %r13
jae 0x1888
leaq 0x13(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %r13, %rdx
movl %ebx, %ecx
callq 0x1160
xorl %eax, %eax
cmpq $0x8, %rax
je 0x1852
movb 0x13(%rsp,%rax), %cl
xorb 0x40(%rsp,%rax), %cl
movb %cl, (%rsp,%rax)
incq %rax
jmp 0x183c
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %r15d, %ecx
callq 0x1090
xorl %eax, %eax
cmpq $0x8, %rax
je 0x187a
movb 0x13(%rsp,%rax), %cl
movb %cl, 0x40(%rsp,%rax)
incq %rax
jmp 0x1867
movq %r12, %rdi
callq 0x1070
addq $0x5, %r13
jmp 0x1821
movq 0x20(%rsp), %rbp
movq %rbp, %rax
xorl %edx, %edx
pushq $0x5
popq %rcx
divq %rcx
testq %rdx, %rdx
setne %al
movq 0x88(%rsp), %r15
testq %r15, %r15
sete %cl
orb %al, %cl
je 0x18d5
movq %rdx, %r13
xorl %eax, %eax
movq 0x80(%rsp), %r14
movq 0x28(%rsp), %r12
cmpq $0x8, %rax
je 0x1917
movb 0x40(%rsp,%rax), %cl
movb %cl, 0x30(%rsp,%rax)
incq %rax
jmp 0x18c2
leaq -0xf(%rbp), %rbx
leaq 0x13(%rsp), %rdi
pushq $0x8
popq %rcx
movq 0x28(%rsp), %r12
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1160
xorl %eax, %eax
movq 0x80(%rsp), %r14
cmpq $0x8, %rax
je 0x1956
movb 0x13(%rsp,%rax), %cl
movb 0x40(%rsp,%rax), %dl
xorb %cl, %dl
movb %dl, (%rsp,%rax)
movb %cl, 0x40(%rsp,%rax)
incq %rax
jmp 0x18fb
xorl %ebx, %ebx
leaq 0x76(%rsp), %rdi
leaq 0x30(%rsp), %rsi
pushq $0x5
popq %rcx
xorl %edx, %edx
callq 0x1090
movq %r15, %rcx
subq %r13, %rcx
movq 0x8(%rsp), %rax
addq %rcx, %rax
addq %r12, %rcx
cmpq %rbx, %r13
je 0x19fa
movb (%rcx,%rbx), %dl
xorb 0x76(%rsp,%rbx), %dl
movb %dl, (%rax,%rbx)
incq %rbx
jmp 0x193e
movq %rsp, %rsi
pushq $0x5
popq %rcx
movq 0x8(%rsp), %rdi
movq %rbx, %rdx
callq 0x1090
xorb $0x10, 0x48(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x1070
xorl %eax, %eax
cmpq $0x10, %rax
je 0x1994
movb 0x90(%rsp,%rax), %cl
xorb 0x48(%rsp,%rax), %cl
movb %cl, 0x30(%rsp,%rax)
incq %rax
jmp 0x197a
xorl %ebx, %ebx
leaq 0x76(%rsp), %rdi
leaq 0x30(%rsp), %rsi
pushq $0xa
popq %rcx
xorl %edx, %edx
callq 0x1090
addq %rbp, %r12
pushq $-0xa
popq %rax
testq %rax, %rax
je 0x19d0
movb $0x1, %cl
testb %bl, %bl
jne 0x19c9
movb 0x80(%rsp,%rax), %cl
cmpb (%r12,%rax), %cl
setne %cl
incq %rax
movl %ecx, %ebx
jmp 0x19b0
testb %bl, %bl
je 0x1a62
movq 0x8(%rsp), %rdi
callq 0x10e0
xorl %eax, %eax
negb %bl
sbbl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ecx, %ecx
cmpq %rcx, %r13
je 0x1a0d
movb (%rax,%rcx), %dl
movb %dl, 0x1b(%rsp,%rcx)
incq %rcx
jmp 0x19fc
movq 0x20(%rsp), %rbp
movq %rbp, %rax
xorl %edx, %edx
pushq $0x5
popq %rcx
divq %rcx
leaq (%rax,%rax,4), %rax
notq %rax
addq %rbp, %rax
movb $-0x80, %cl
movb %cl, 0x1c(%rsp,%rax)
incq %rax
xorl %ecx, %ecx
cmpq $0x4, %rax
jb 0x1a29
xorl %ebx, %ebx
movq %rsp, %rdi
leaq 0x1b(%rsp), %rsi
pushq $0x8
popq %rcx
xorl %edx, %edx
callq 0x1160
cmpq $0x8, %rbx
je 0x196e
movb (%rsp,%rbx), %al
xorb %al, 0x40(%rsp,%rbx)
incq %rbx
jmp 0x1a4c
xorl %eax, %eax
cmpq %rax, %r15
je 0x19d8
movq 0x8(%rsp), %rcx
movb (%rcx,%rax), %cl
movb %cl, (%r14,%rax)
incq %rax
jmp 0x1a64
nop