// File: /workspace/github_c_makefile_star2_O3/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 $0x138, %rsp            # imm = 0x138
movq %r9, 0x18(%rsp)
movq %r8, %r13
movq %rcx, %r15
movq %rsi, %rbx
movq %rdi, %r14
callq 0x1050
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testq %rbx, %rbx
je 0x2b5b
movq 0x170(%rsp), %rdx
movq %rdx, %rax
orq %r13, %rax
je 0x2538
cmpq $0x0, 0x180(%rsp)
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jne 0x2b5b
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x14, %r13
jb 0x2b5b
movq $0x0, (%rbx)
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0xc0(%rsp)
vmovups %ymm0, 0xb0(%rsp)
movq 0x180(%rsp), %rax
vmovdqu (%rax), %xmm0
vmovdqu %xmm0, 0xdc(%rsp)
movl 0x10(%rax), %eax
movl %eax, 0xec(%rsp)
movw $0xa0a6, 0xb0(%rsp)     # imm = 0xA0A6
jne 0x2546
xorl %r12d, %r12d
jmp 0x280b
movq $0x0, (%rbx)
xorl %eax, %eax
jmp 0x2b5b
testq %r15, %r15
je 0x2b4d
movq %r15, 0x8(%rsp)
leaq 0x48(%rsp), %rbp
vpmovsxbq 0xaa4(%rip), %xmm0 # 0x3006
vpinsrq $0x1, %r13, %xmm0, %xmm0
leaq 0xb2(%rsp), %r13
xorl %r12d, %r12d
movw $0x3f, %ax
kmovd %eax, %k1
kmovw %k1, 0x10(%rsp)
movb $-0x60, 0x31(%rsp)
vpextrq $0x1, %xmm0, %r15
cmpq $0x3e, %r15
setae %al
xorb $-0x5f, %al
vmovdqa %xmm0, 0x20(%rsp)
vpbroadcastq %xmm0, %ymm0
vpsrlvq 0xb97(%rip), %ymm0, %ymm1 # 0x3140
vpsrlvq 0xbae(%rip), %ymm0, %ymm0 # 0x3160
vpmovqb %ymm0, %xmm0
vpmovqb %ymm1, %xmm1
movb %al, 0x30(%rsp)
vpunpckldq %xmm0, %xmm1, %xmm0 # xmm0 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
vmovdqu %xmm0, 0x32(%rsp)
vpxor %xmm0, %xmm0, %xmm0
kmovw 0x10(%rsp), %k1
vmovdqu8 %xmm0, -0x6(%rbp) {%k1}
movq 0x178(%rsp), %rcx
movl 0x10(%rcx), %eax
movl %eax, 0x10(%rbp)
vmovups (%rcx), %xmm0
vmovups %xmm0, (%rbp)
movq 0x180(%rsp), %rcx
movl 0x10(%rcx), %eax
movl %eax, 0x24(%rbp)
vmovdqu (%rcx), %xmm0
vmovdqu %xmm0, 0x14(%rbp)
leaq 0x30(%rsp), %rdi
leaq 0x70(%rsp), %rsi
vzeroupper
callq 0x1080
cmpq $0x3e, %r15
jb 0x265a
movq 0x8(%rsp), %rax
addq %r12, %rax
leaq (%r14,%r12), %rdx
xorl %ecx, %ecx
movb (%rax,%rcx), %sil
xorb 0x72(%rsp,%rcx), %sil
movb %sil, (%rdx,%rcx)
movb (%rax,%rcx), %sil
movb %sil, 0x72(%rsp,%rcx)
incq %rcx
cmpq $0x2a, %rcx
jne 0x2634
addq %rcx, %r12
movb $-0x5e, %dl
jmp 0x26b5
movl %r15d, %ecx
addl $-0x14, %ecx
movl %ecx, %eax
je 0x269a
movq 0x8(%rsp), %rdx
addq %r12, %rdx
leaq (%r14,%r12), %rdi
xorl %esi, %esi
movb (%rdx,%rsi), %r8b
xorb 0x72(%rsp,%rsi), %r8b
movb %r8b, (%rdi,%rsi)
movb (%rdx,%rsi), %r8b
movb %r8b, 0x72(%rsp,%rsi)
incq %rsi
cmpq %rsi, %rax
jne 0x2672
addq %rsi, %r12
movb $-0x5d, %dl
cmpl $0x29, %ecx
ja 0x26b5
leal -0x14(%r15), %ecx
incq %rax
movq %rax, %rdx
xorb %cl, 0x71(%rsp,%rax)
incq %rdx
cmpl $0x2a, %eax
movq %rdx, %rax
jne 0x26a4
movb $-0x5d, %dl
movb $-0x60, 0x71(%rsp)
movb %dl, 0x70(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x1080
xorl %eax, %eax
vpmovsxbq 0xbb1(%rip), %ymm8 # 0x328c
vpmovsxbq 0xbac(%rip), %ymm9 # 0x3290
vpmovsxbq 0xba7(%rip), %ymm10 # 0x3294
vpmovsxbq 0xba2(%rip), %ymm11 # 0x3298
vpmovsxbq 0xb9d(%rip), %ymm12 # 0x329c
vpmovsxbq 0xb98(%rip), %ymm13 # 0x32a0
vpmovsxbq 0xb93(%rip), %ymm14 # 0x32a4
vpmovsxbq 0xb6e(%rip), %ymm15 # 0x3288
vpbroadcastq 0xb54(%rip), %ymm16 # 0x3278
vpbroadcastq %rax, %ymm0
vpor %ymm0, %ymm8, %ymm1
vpor %ymm0, %ymm9, %ymm2
vpor %ymm0, %ymm10, %ymm3
vpor %ymm0, %ymm11, %ymm4
vpor %ymm0, %ymm12, %ymm5
vpor %ymm0, %ymm13, %ymm6
vpor %ymm0, %ymm14, %ymm7
vpor %ymm0, %ymm15, %ymm0
vpcmpltuq %ymm16, %ymm0, %k0
vpcmpltuq %ymm16, %ymm7, %k1
kshiftlb $0x4, %k1, %k1
korb %k1, %k0, %k0
vpcmpltuq %ymm16, %ymm6, %k1
vpcmpltuq %ymm16, %ymm5, %k2
kshiftlb $0x4, %k2, %k2
korb %k2, %k1, %k1
kunpckbw %k0, %k1, %k0
vpcmpltuq %ymm16, %ymm4, %k1
vpcmpltuq %ymm16, %ymm3, %k2
kshiftlb $0x4, %k2, %k2
korb %k2, %k1, %k1
vpcmpltuq %ymm16, %ymm2, %k2
vpcmpltuq %ymm16, %ymm1, %k3
kshiftlb $0x4, %k3, %k3
korb %k3, %k2, %k2
kunpckbw %k1, %k2, %k1
kunpckwd %k0, %k1, %k1
vmovdqu8 0xf2(%rsp,%rax), %ymm0 {%k1} {z}
vmovdqu8 (%r13,%rax), %ymm1 {%k1} {z}
vpxor %ymm0, %ymm1, %ymm0
vmovdqu8 %ymm0, (%r13,%rax) {%k1}
addq $0x20, %rax
cmpq $0x40, %rax
jne 0x2724
vmovdqa 0x20(%rsp), %xmm0
vpaddq 0xaef(%rip), %xmm0, %xmm0 # 0x32e0
movq %r12, (%rbx)
cmpq $0x3e, %r15
ja 0x2581
movq 0x8(%rsp), %r15
movq 0x170(%rsp), %rdx
testq %rdx, %rdx
je 0x2aa9
cmpq $0x0, 0x18(%rsp)
je 0x2b46
movq %r15, 0x8(%rsp)
movq %r14, 0x10(%rsp)
leaq 0x46(%rsp), %r15
vpmovsxbq 0x7ce(%rip), %xmm0 # 0x3006
vpinsrq $0x1, %rdx, %xmm0, %xmm0
leaq 0xb2(%rsp), %rbp
xorl %r14d, %r14d
vpbroadcastq 0xa2e(%rip), %ymm3 # 0x3280
vpbroadcastq 0xa0d(%rip), %ymm4 # 0x3268
vpbroadcastq 0xa0c(%rip), %ymm5 # 0x3270
movb $-0x60, 0x31(%rsp)
vpextrq $0x1, %xmm0, %r13
cmpq $0x16, %r13
setb %al
orb $-0x5c, %al
movb %al, 0x30(%rsp)
vmovdqa %xmm0, 0x20(%rsp)
vpbroadcastq %xmm0, %ymm0
movl $0x2, %eax
vpmovsxbq 0x9f3(%rip), %ymm1 # 0x3288
vpcmpltuq %ymm3, %ymm1, %k1
vpcmpltuq %ymm4, %ymm1, %k2
vpsllq $0x3, %ymm1, %ymm2
vpsrlvq %ymm2, %ymm0, %ymm2
vpmovqb %ymm2, %xmm2
vmovdqu8 %xmm2, %xmm2 {%k2} {z}
vmovdqu8 %xmm2, 0x30(%rsp,%rax) {%k1}
vpaddq %ymm5, %ymm1, %ymm1
addq $0x4, %rax
cmpq $0x16, %rax
jne 0x2895
movq 0x18(%rsp), %rax
addq %r14, %rax
cmpq $0x15, %r13
ja 0x290d
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
leal 0x16(%rcx), %esi
movb %dl, 0x30(%rsp,%rsi)
incq %rcx
cmpl %r13d, %ecx
jb 0x28df
leaq (%r15,%r13), %rdi
movl $0x15, %edx
subl %r13d, %edx
incq %rdx
movl %r13d, %esi
vzeroupper
callq 0x1030
jmp 0x291e
movq 0xe(%rax), %rcx
movq %rcx, 0xe(%r15)
vmovdqu (%rax), %xmm0
vmovdqu %xmm0, (%r15)
movq 0x180(%rsp), %rcx
movl 0x10(%rcx), %eax
movl %eax, 0x26(%r15)
vmovups (%rcx), %xmm0
vmovups %xmm0, 0x16(%r15)
leaq 0x30(%rsp), %rdi
leaq 0x70(%rsp), %rsi
vzeroupper
callq 0x1080
xorl %eax, %eax
vpmovsxbq 0x938(%rip), %ymm8 # 0x328c
vpmovsxbq 0x933(%rip), %ymm9 # 0x3290
vpmovsxbq 0x92e(%rip), %ymm10 # 0x3294
vpmovsxbq 0x929(%rip), %ymm11 # 0x3298
vpmovsxbq 0x924(%rip), %ymm12 # 0x329c
vpmovsxbq 0x91f(%rip), %ymm13 # 0x32a0
vpmovsxbq 0x91a(%rip), %ymm14 # 0x32a4
vpmovsxbq 0x8f5(%rip), %ymm15 # 0x3288
vpbroadcastq 0x8db(%rip), %ymm16 # 0x3278
vpbroadcastq %rax, %ymm0
vpor %ymm0, %ymm8, %ymm1
vpor %ymm0, %ymm9, %ymm2
vpor %ymm0, %ymm10, %ymm3
vpor %ymm0, %ymm11, %ymm4
vpor %ymm0, %ymm12, %ymm5
vpor %ymm0, %ymm13, %ymm6
vpor %ymm0, %ymm14, %ymm7
vpor %ymm0, %ymm15, %ymm0
vpcmpltuq %ymm16, %ymm0, %k0
vpcmpltuq %ymm16, %ymm7, %k1
kshiftlb $0x4, %k1, %k1
korb %k1, %k0, %k0
vpcmpltuq %ymm16, %ymm6, %k1
vpcmpltuq %ymm16, %ymm5, %k2
kshiftlb $0x4, %k2, %k2
korb %k2, %k1, %k1
kunpckbw %k0, %k1, %k0
vpcmpltuq %ymm16, %ymm4, %k1
vpcmpltuq %ymm16, %ymm3, %k2
kshiftlb $0x4, %k2, %k2
korb %k2, %k1, %k1
vpcmpltuq %ymm16, %ymm2, %k2
vpcmpltuq %ymm16, %ymm1, %k3
kshiftlb $0x4, %k3, %k3
korb %k3, %k2, %k2
kunpckbw %k1, %k2, %k1
kunpckwd %k0, %k1, %k1
vmovdqu8 0x72(%rsp,%rax), %ymm0 {%k1} {z}
vmovdqu8 (%rbp,%rax), %ymm1 {%k1} {z}
vpxor %ymm0, %ymm1, %ymm0
vmovdqu8 %ymm0, (%rbp,%rax) {%k1}
addq $0x20, %rax
cmpq $0x40, %rax
jne 0x299d
cmpq $0x16, %r13
movl $0x16, %eax
cmovbq %r13, %rax
addq %rax, %r14
vmovdqa 0x20(%rsp), %xmm0
vpaddq 0x846(%rip), %xmm0, %xmm0 # 0x32c0
cmpq $0x16, %r13
vpbroadcastq 0x7f9(%rip), %ymm3 # 0x3280
vpbroadcastq 0x7d8(%rip), %ymm4 # 0x3268
vpbroadcastq 0x7d7(%rip), %ymm5 # 0x3270
ja 0x2864
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %r15
leaq 0xb0(%rsp), %rdi
leaq 0x30(%rsp), %r13
movq %r13, %rsi
vzeroupper
callq 0x1080
movl $0xfffff, %eax          # imm = 0xFFFFF
kmovd %eax, %k1
movq 0x180(%rsp), %rax
vmovdqu8 (%rax), %ymm0 {%k1} {z}
vmovdqu8 0x2c(%r13), %ymm1 {%k1} {z}
vpxor %ymm0, %ymm1, %ymm0
vmovdqu8 %ymm0, 0x2c(%r13) {%k1}
movb (%r15,%r12), %cl
movb $0x1, %al
cmpb (%r13), %cl
jne 0x2b2a
leaq (%r12,%r15), %rax
incq %rax
movq $-0x1, %rcx
cmpq $0x12, %rcx
je 0x2b54
movb 0x1(%rax,%rcx), %sil
leaq 0x1(%rcx), %rdx
cmpb 0x32(%rsp,%rcx), %sil
movq %rdx, %rcx
je 0x2b0a
cmpq $0x13, %rdx
setb %al
testq %r12, %r12
je 0x2b56
movl $0x1, %ecx
xorl %edx, %edx
movb $0x0, (%r14,%rdx)
movl %ecx, %edx
incl %ecx
cmpq %rdx, (%rbx)
ja 0x2b36
jmp 0x2b56
movl $0xfffffffc, %eax       # imm = 0xFFFFFFFC
jmp 0x2b5b
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
jmp 0x2b5b
xorl %eax, %eax
movzbl %al, %eax
negl %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq