// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/paeq160/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
     )
     {
       Init();//Initializing GF(256) multiplication table for AES

		 if(mlen==NULL)
			 return -1;
		 if((clen==0) && (adlen==0))
		 {
			 *mlen=0;
			 return 0;
		 }

		 //Here we do decryption and/or authentication so we need a key and a plaintext pointer valid
		 if( (k==NULL) || (m==NULL) )
			 return -2;

		 //Minimum tag length verification
		 if (clen < CRYPTO_ABYTES)
			 return -1;

		 //Initializing constants
		unsigned char D0[2];
		D0[0] = CRYPTO_NPUBBYTES*8; //nonce length in bits
		D0[1] = CRYPTO_KEYBYTES*8; //key length in bits
		(*mlen)=0;
		 
		 //Initializing the last input to F
		 unsigned char BlockLastInput[64];  //Z1 - input to the last call of F
		 memset(BlockLastInput,0,64);
		 unsigned char Tag[64]; //Tag output
		 for(unsigned i=0; i<CRYPTO_KEYBYTES; ++i) //Key to the Z input
			BlockLastInput[64-CRYPTO_KEYBYTES+i] = k[i];
		 BlockLastInput[0] = D0[0]+6;
		 BlockLastInput[1] = D0[1];

		 unsigned long long decrypted_bytes=0;//Encrypted bytes counter
		 

		 //Encryption part
		 if(clen!=CRYPTO_ABYTES) //Ciphertext is more than just a tag
		 {
			 if(c==NULL)
				 return -3;
			 unsigned char BlockInput[64];  //V1 - input to the first layer call of F
			 unsigned char BlockMiddle[64];  //W1 - output of the first layer call of F
			 unsigned char BlockOutput[64];  //Y1 - output of the second layer call of F
			 unsigned long long mblock_counter=1;   //Message block counter
			 
			 while((clen>CRYPTO_ABYTES))
			 {
				 /* I. First layer */

				 //1. Domain-separation constant
				 BlockInput[1] = D0[1]; 
				 if(clen>= (CRYPTO_MBLOCK+CRYPTO_ABYTES))
					 BlockInput[0] = D0[0];
				 else //Last incomplete block
					 BlockInput[0] = D0[0]+1;

				 //2. Counter
				 for(unsigned i=0; i<CRYPTO_COUNTERBYTES; ++i)
				 {
					 BlockInput[i+2] = (i<sizeof(mblock_counter))?(mblock_counter>>(8*i))&0xff :0;//copying counter bytewise
				 }
				 
				 //3. Nonce
				 for(unsigned i=0; i<CRYPTO_NPUBBYTES; ++i) 
				 {
					 BlockInput[i+2+CRYPTO_COUNTERBYTES] = npub[i];
				 }

				 //4. Key
				 for(unsigned i=0; i<CRYPTO_KEYBYTES; ++i) 
				 {
					 BlockInput[i+2+CRYPTO_COUNTERBYTES+CRYPTO_NPUBBYTES] = k[i];
				 }	 

				 //5. Permutation call
				 FPerm(BlockInput, BlockMiddle); //First layer call to F

				 /* II. Encryption*/

				 if(clen>=CRYPTO_MBLOCK+CRYPTO_ABYTES)//Full block encryption
				 {
					 for(unsigned i=0; i<CRYPTO_MBLOCK; ++i)
					 {
						 m[decrypted_bytes] = BlockMiddle[i+2]^c[decrypted_bytes];
						 BlockMiddle[i+2] = c[decrypted_bytes];
						 decrypted_bytes++;
						 
					 }
					 BlockMiddle[1] =  D0[1];
					 BlockMiddle[0] = D0[0]+2;  //New Di constant

				 }

				 else //Last incomplete block
				 {
					 for(unsigned i=0; i<(unsigned)clen-CRYPTO_ABYTES; ++i)//Incomplete block Encryption
					 {
						  m[decrypted_bytes] = BlockMiddle[i+2]^c[decrypted_bytes];
						 BlockMiddle[i+2] = c[decrypted_bytes];
						 decrypted_bytes++;
					 }
					 for(unsigned i=(unsigned)clen-CRYPTO_ABYTES; i<CRYPTO_MBLOCK; ++i)
					 {
						 BlockMiddle[i+2] ^=  (unsigned char)(clen-CRYPTO_ABYTES);  //Extra Padding: extra bytes filled with the last block length in bytes
					 }

					 BlockMiddle[1] =  D0[1];
					 BlockMiddle[0] = D0[0]+3;  //New Di constant
				 }

				 //III. Second permutation call
				 //1. Call
				 FPerm(BlockMiddle, BlockOutput); //Second layer call to F
				 
				 //2. Buffer update
				 for(unsigned i=0; i<64-2-CRYPTO_KEYBYTES; ++i)//Adding the output to tag preparation buffer
				 {
					 BlockLastInput[i+2] ^= BlockOutput[i+2];
				 }


				 //Counters increment
				 mblock_counter++;
				 if(clen>=CRYPTO_MBLOCK+CRYPTO_ABYTES)
					 clen-=CRYPTO_MBLOCK;
				 
				 else 
				 	 clen=CRYPTO_ABYTES;
				 (*mlen) = decrypted_bytes;
			 }
		 }

		//Associated data part	  
		if(adlen!=0)
		{
			if(ad==NULL)
				return -4;

			unsigned char BlockInput[64];  //V1 - input to the first layer call of F
			 unsigned char BlockOutput[64];  //Y1 - output of the second layer call of F
			 unsigned long long adblock_counter=1;   //AD block counter
			 unsigned long long auth_bytes=0;

			while(adlen>0)
			{
				//1. Constant
				BlockInput[1] = D0[1];
				if(adlen>= CRYPTO_ADBLOCK)
					 BlockInput[0] = D0[0]+4;
				 else //Last incomplete block
					 BlockInput[0] = D0[0]+5;


				//2. Counter
				for(unsigned i=0; i<CRYPTO_KEYBYTES; ++i)
					BlockInput[i+2] = (i<sizeof(adblock_counter))? (adblock_counter>>(8*i))&0xff:0;//copying counter bytewise

				//3. AD block
				if(adlen >= CRYPTO_ADBLOCK) //Filling AD block
				{
					for(unsigned i=0; i<CRYPTO_ADBLOCK; ++i)
						BlockInput[i+2+CRYPTO_KEYBYTES] = ad[auth_bytes+i];
					
				}
				else //Last incomplete block
				{
					for(unsigned i=0; i<adlen; ++i)
						BlockInput[i+2+CRYPTO_KEYBYTES] = ad[auth_bytes+i];
					for(unsigned i=(unsigned)adlen; i<CRYPTO_ADBLOCK; ++i)
						BlockInput[i+2+CRYPTO_KEYBYTES] = (unsigned char)adlen;
				}

				//4. Key
				for(unsigned i=0; i<CRYPTO_KEYBYTES; ++i) 
				{
					 BlockInput[i+CRYPTO_ADBLOCK+CRYPTO_KEYBYTES+2] = k[i];
				}
				

				//5.Call to the F permutation

				FPerm(BlockInput,BlockOutput);//Call to the F permutation

				 for(unsigned i=0; i<64-2-CRYPTO_KEYBYTES; ++i)//Adding the output to Z
				 {
					 BlockLastInput[i+2] ^= BlockOutput[i+2];
				 }

				//Counters increment
				 adblock_counter++;
				 if(adlen>=CRYPTO_ADBLOCK)
				 {
					 auth_bytes += CRYPTO_ADBLOCK;
					 adlen-=CRYPTO_ADBLOCK;
				 }
				 else 
				 {
					 auth_bytes += adlen;
					 adlen=0;
				 }
			}
		}

		// Tag production 

		//1. Permutation call
		FPerm(BlockLastInput,Tag);
		//2. Key injection
		for(unsigned i=0; i<CRYPTO_KEYBYTES; ++i) 
		{
				Tag[64-CRYPTO_KEYBYTES+i] ^= k[i];
		}

		//3. Truncation
		if(clen!=CRYPTO_ABYTES)//Incorrect tag length
			return -1;

		for(unsigned i=0; i<CRYPTO_ABYTES; ++i)
		{
			if(c[(*mlen)+i] != Tag[i])
			{
				for(unsigned j=0; j< (*mlen); ++j)//Erasing decryption result
					m[j]=0;
				return -1; //Invalid
			}
		}
       return 0;
     }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %r9, 0x18(%rsp)
movq %r8, %rbx
movq %rcx, %r15
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
callq 0x1050
pushq $-0x1
popq %rax
testq %r14, %r14
je 0x230d
movq 0x160(%rsp), %r12
movq %r12, %rcx
orq %rbx, %rcx
je 0x1f9f
movq 0x170(%rsp), %rbp
testq %rbp, %rbp
sete %cl
cmpq $0x0, 0x8(%rsp)
sete %dl
orb %cl, %dl
je 0x1faa
pushq $-0x2
popq %rax
jmp 0x230d
andq $0x0, (%r14)
xorl %eax, %eax
jmp 0x230d
movq %rbx, %r8
cmpq $0x14, %rbx
jb 0x230d
movq 0x168(%rsp), %rsi
andq $0x0, (%r14)
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0xc0(%rsp)
vmovups %ymm0, 0xa0(%rsp)
xorl %eax, %eax
cmpq $0x14, %rax
je 0x1ff1
movb (%rbp,%rax), %cl
movb %cl, 0xcc(%rsp,%rax)
incq %rax
jmp 0x1fdb
movw $0xa0a6, 0xa0(%rsp)     # imm = 0xA0A6
cmpq $0x14, %r8
jne 0x2008
xorl %ebx, %ebx
jmp 0x2186
testq %r15, %r15
je 0x2329
movq %r14, (%rsp)
pushq $0x1
popq %r13
xorl %r14d, %r14d
xorl %ebx, %ebx
cmpq $0x15, %r8
jb 0x2182
movb $-0x60, 0x21(%rsp)
cmpq $0x3e, %r8
setae %al
xorb $-0x5f, %al
movb %al, 0x20(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0x16, %rcx
je 0x205e
cmpq $0x8, %rcx
shrxq %rax, %r13, %rdx
cmovael %r14d, %edx
movb %dl, 0x22(%rsp,%rcx)
incq %rcx
addq $0x8, %rax
jmp 0x203e
xorl %eax, %eax
cmpq $0x14, %rax
je 0x2072
movb (%rsi,%rax), %cl
movb %cl, 0x38(%rsp,%rax)
incq %rax
jmp 0x2060
xorl %eax, %eax
cmpq $0x14, %rax
je 0x2087
movb (%rbp,%rax), %cl
movb %cl, 0x4c(%rsp,%rax)
incq %rax
jmp 0x2074
leaq 0x20(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq %r8, 0x10(%rsp)
vzeroupper
callq 0x1080
movq 0x10(%rsp), %r8
cmpq $0x3e, %r8
jb 0x20d3
leaq 0x62(%rsp), %rax
pushq $0x2a
popq %rcx
movq 0x8(%rsp), %rsi
subq $0x1, %rcx
jb 0x2104
movb (%r15,%rbx), %dl
xorb (%rax), %dl
movb %dl, (%rsi,%rbx)
movb (%r15,%rbx), %dl
movb %dl, (%rax)
incq %rbx
incq %rax
jmp 0x20b6
leal -0x14(%r8), %eax
leaq 0x62(%rsp), %rcx
movq %rax, %rdx
movq 0x8(%rsp), %rdi
subq $0x1, %rdx
jb 0x2108
movb (%r15,%rbx), %sil
xorb (%rcx), %sil
movb %sil, (%rdi,%rbx)
movb (%r15,%rbx), %sil
movb %sil, (%rcx)
incq %rbx
incq %rcx
jmp 0x20e4
movb $-0x5e, %al
jmp 0x211d
leal -0x14(%r8), %ecx
cmpq $0x2a, %rax
je 0x211b
xorb %cl, 0x62(%rsp,%rax)
incq %rax
jmp 0x210c
movb $-0x5d, %al
movb $-0x60, 0x61(%rsp)
movb %al, 0x60(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x1080
pushq $0x2
popq %rax
movq 0x10(%rsp), %rdx
movq 0x168(%rsp), %rsi
cmpq $0x2c, %rax
je 0x2161
movb 0xe0(%rsp,%rax), %cl
xorb %cl, 0xa0(%rsp,%rax)
incq %rax
jmp 0x2148
incq %r13
cmpq $0x3e, %rdx
leaq -0x2a(%rdx), %rax
movq (%rsp), %rcx
movq %rbx, (%rcx)
pushq $0x14
popq %rcx
cmovbq %rcx, %rax
movq %rax, %r8
jmp 0x201e
movq (%rsp), %r14
testq %r12, %r12
je 0x22a0
cmpq $0x0, 0x18(%rsp)
je 0x2322
movq %r14, (%rsp)
pushq $0x1
popq %r14
xorl %r13d, %r13d
xorl %edi, %edi
testq %r12, %r12
je 0x229c
movb $-0x60, 0x21(%rsp)
cmpq $0x16, %r12
setae %al
xorb $-0x5b, %al
movb %al, 0x20(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0x14, %rcx
je 0x21e7
cmpq $0x8, %rcx
shrxq %rax, %r14, %rdx
cmovael %r13d, %edx
movb %dl, 0x22(%rsp,%rcx)
incq %rcx
addq $0x8, %rax
jmp 0x21c7
movq 0x18(%rsp), %rax
addq %rdi, %rax
xorl %ecx, %ecx
cmpq $0x16, %r12
jb 0x2209
cmpq $0x16, %rcx
je 0x2230
movb (%rax,%rcx), %dl
movb %dl, 0x36(%rsp,%rcx)
incq %rcx
jmp 0x21f7
cmpl %r12d, %ecx
jae 0x221d
movb (%rax,%rcx), %dl
leal 0x16(%rcx), %esi
movb %dl, 0x20(%rsp,%rsi)
incq %rcx
jmp 0x2209
movq %r12, %rax
cmpq $0x16, %rax
je 0x2230
movb %r12b, 0x36(%rsp,%rax)
incq %rax
jmp 0x2220
xorl %eax, %eax
cmpq $0x14, %rax
je 0x2245
movb (%rbp,%rax), %cl
movb %cl, 0x4c(%rsp,%rax)
incq %rax
jmp 0x2232
movq %rdi, 0x10(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x60(%rsp), %rsi
vzeroupper
callq 0x1080
movq 0x10(%rsp), %rdi
pushq $0x2
popq %rax
cmpq $0x2c, %rax
je 0x227a
movb 0x60(%rsp,%rax), %cl
xorb %cl, 0xa0(%rsp,%rax)
incq %rax
jmp 0x2264
incq %r14
leaq -0x16(%r12), %rax
cmpq $0x16, %r12
pushq $0x16
popq %rcx
cmovaeq %rcx, %r12
cmovbq %r13, %rax
addq %r12, %rdi
movq %rax, %r12
jmp 0x21a8
movq (%rsp), %r14
leaq 0xa0(%rsp), %rdi
leaq 0x20(%rsp), %rsi
vzeroupper
callq 0x1080
movq 0x8(%rsp), %rsi
xorl %eax, %eax
cmpq $0x14, %rax
je 0x22cf
movb (%rbp,%rax), %cl
xorb %cl, 0x4c(%rsp,%rax)
incq %rax
jmp 0x22bc
addq %rbx, %r15
pushq $-0x1
popq %rax
cmpq $0x13, %rax
je 0x1fa3
movb 0x1(%r15,%rax), %dl
leaq 0x1(%rax), %rcx
cmpb 0x21(%rsp,%rax), %dl
movq %rcx, %rax
je 0x22d5
xorl %eax, %eax
movl %eax, %eax
cmpq %rax, %rbx
jbe 0x2305
movb $0x0, (%rsi,%rax)
incl %eax
movq (%r14), %rbx
jmp 0x22f3
xorl %eax, %eax
cmpq $0x14, %rcx
sbbl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq
pushq $-0x4
jmp 0x1f99
pushq $-0x3
jmp 0x1f99