// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/paeq160/ref/encrypt.cpp
// Function: crypto_aead_encrypt
int crypto_aead_encrypt(
       unsigned char *c,unsigned long long *clen,
       const unsigned char *m,unsigned long long mlen,
       const unsigned char *ad,unsigned long long adlen,
       const unsigned char *nsec,
       const unsigned char *npub,
       const unsigned char *k
     )

     {
		 Init();//Initializing GF(256) multiplication table for AES

		 if(clen==NULL)
			 return -1;
		 if((mlen==0) && (adlen==0))
		 {
			 *clen=0;
			 return 0;
		 }

		 //Assume that we do encryption and/or authentication so we need a key and a ciphertext pointer valid
		 if( (k==NULL) || (c==NULL) )
			 return -2;

		 if(npub==NULL)
			 return -7;

		 //Initializing constants
		unsigned char D0[2];
		D0[0] = CRYPTO_NPUBBYTES*8; //nonce length in bits, zero for 256-bit nonce
		D0[1] = CRYPTO_KEYBYTES*8; //key length in bits
		(*clen)=0;
		 
		 //Block variables
		 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 char BlockLastInput[64];  //Z1 - input to the last call of F
		 memset(BlockLastInput,0,64);
		 unsigned char Tag[64]; //Tag output
		 
		 unsigned long long encrypted_bytes=0;//Encrypted bytes counter
		 

		 //Encryption part
		 if(mlen!=0)
		 {
			 if(m==NULL)
			 {
				 //Clearing variables
				for(unsigned i=0; i<64; ++i)
					BlockInput[i] = BlockMiddle[i] = BlockOutput[i] = BlockLastInput[i] = 0;
				return -3;
			 }
				 
			 unsigned long long mblock_counter=1;   //Message block counter
			 
			 while((mlen>0))
			 {
				 /* I. First layer */

				 //1. Domain-separation constant
				 BlockInput[1] = D0[1]; 
				 if(mlen>= CRYPTO_MBLOCK)
					 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(mlen>=CRYPTO_MBLOCK)//Full block encryption
				 {
					 for(unsigned i=0; i<CRYPTO_MBLOCK; ++i)
					 {
						 BlockMiddle[i+2] ^= m[encrypted_bytes+i];
						 c[encrypted_bytes+i] = BlockMiddle[i+2];
						 
					 }
					 BlockMiddle[1] =  D0[1];
					 BlockMiddle[0] = D0[0]+2;  //New Di constant

				 }

				 else //Last incomplete block
				 {
					 for(unsigned i=0; i<(unsigned)mlen; ++i)//Incomplete block Encryption
					 {
						 BlockMiddle[i+2] ^= m[encrypted_bytes+i];
						 c[encrypted_bytes+i] = BlockMiddle[i+2];						 
					 }
					 for(unsigned i=(unsigned)mlen; i<CRYPTO_MBLOCK; ++i)
					 {
						 BlockMiddle[i+2] ^=  (unsigned char)mlen;  //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(mlen>=CRYPTO_MBLOCK)
				 {					 
					 encrypted_bytes += CRYPTO_MBLOCK;
					 mlen-=CRYPTO_MBLOCK;
				 }
				 else 
				 {
					 encrypted_bytes += mlen;
					 mlen=0;
				 }
				 (*clen) = encrypted_bytes;
			 }
		 }

		//Associated data part	  
		if(adlen!=0)
		{
			if(ad==NULL)
			{
				//Clearing variables
				for(unsigned i=0; i<64; ++i)
					BlockInput[i] = BlockMiddle[i] = BlockOutput[i] = BlockLastInput[i] = 0;
				return -4;
			}

			 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 
		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];

		//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
		for(unsigned i=0; i<CRYPTO_ABYTES; ++i)
			c[(*clen)+i] = Tag[i];
		*clen += CRYPTO_ABYTES;


		//Clearing variables
		for(unsigned i=0; i<64; ++i)
			BlockInput[i] = BlockMiddle[i] = BlockOutput[i] = BlockLastInput[i] = 0;
		

	   
		return 0;
     }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %r9, %r12
movq %r8, 0x18(%rsp)
movq %rcx, %rbp
movq %rdx, 0x8(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
callq 0x1050
testq %rbx, %rbx
je 0x1d9e
movq %r12, %rax
orq %rbp, %rax
je 0x1da5
cmpq $0x0, 0x1b0(%rsp)
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jne 0x1ddf
cmpq $0x0, 0x1a8(%rsp)
je 0x1db0
movq %r14, (%rsp)
movq %rbx, 0x10(%rsp)
movq $0x0, (%rbx)
vpxor %xmm0, %xmm0, %xmm0
vmovdqu %ymm0, 0xc0(%rsp)
vmovdqu %ymm0, 0xa0(%rsp)
testq %rbp, %rbp
je 0x1a93
cmpq $0x0, 0x8(%rsp)
je 0x1db7
leaq 0xe2(%rsp), %r13
movl $0x1, %r14d
leaq 0xa2(%rsp), %r15
vpbroadcastq 0x1a8f(%rip), %ymm5 # 0x3238
vpbroadcastq 0x1a76(%rip), %ymm6 # 0x3228
xorl %ebx, %ebx
movb $-0x60, 0x21(%rsp)
cmpq $0x2a, %rbp
setae %al
xorb $-0x5f, %al
movb %al, 0x20(%rsp)
vpbroadcastq %r14, %ymm0
movl $0x2, %eax
vpmovsxbq 0x1a76(%rip), %ymm1 # 0x3250
vpmovsxbq 0x1a71(%rip), %ymm2 # 0x3254
vpcmpltuq %ymm5, %ymm1, %k0
vpcmpltuq %ymm5, %ymm2, %k1
kshiftlb $0x4, %k1, %k1
korb %k1, %k0, %k0
kmovb %k0, %k1
vpcmpltuq %ymm6, %ymm1, %k0
vpcmpltuq %ymm6, %ymm2, %k2
kshiftlb $0x4, %k2, %k2
korb %k2, %k0, %k2
vpsllq $0x3, %ymm1, %ymm3
vpsllq $0x3, %ymm2, %ymm4
vpsrlvq %ymm4, %ymm0, %ymm4
vpsrlvq %ymm3, %ymm0, %ymm3
vpmovqb %ymm3, %xmm3
vpmovqb %ymm4, %xmm4
vpunpckldq %xmm4, %xmm3, %xmm3 # xmm3 = xmm3[0],xmm4[0],xmm3[1],xmm4[1]
vmovdqu8 %xmm3, %xmm3 {%k2} {z}
vmovdqu8 %xmm3, 0x20(%rsp,%rax) {%k1}
vpaddq %ymm6, %ymm1, %ymm1
vpaddq %ymm6, %ymm2, %ymm2
addq $0x8, %rax
cmpq $0x1a, %rax
jne 0x17e3
movq 0x1a8(%rsp), %rcx
movl 0x10(%rcx), %eax
leaq 0x38(%rsp), %rdx
movl %eax, 0x10(%rdx)
vmovups (%rcx), %xmm0
vmovups %xmm0, (%rdx)
movq 0x1b0(%rsp), %rcx
movl 0x10(%rcx), %eax
movl %eax, 0x24(%rdx)
vmovdqu (%rcx), %xmm0
vmovdqu %xmm0, 0x14(%rdx)
leaq 0x20(%rsp), %rdi
leaq 0x60(%rsp), %rsi
vzeroupper
callq 0x1080
cmpq $0x2a, %rbp
jb 0x18d5
movq (%rsp), %rax
addq %rbx, %rax
movq 0x8(%rsp), %rcx
addq %rbx, %rcx
xorl %edx, %edx
movb 0x62(%rsp,%rdx), %sil
xorb (%rcx,%rdx), %sil
movb %sil, 0x62(%rsp,%rdx)
movb %sil, (%rax,%rdx)
incq %rdx
cmpq $0x2a, %rdx
jne 0x18b6
movb $-0x5e, %al
jmp 0x1924
cmpl $0x2, %ebp
movl $0x1, %eax
cmovael %ebp, %eax
movq (%rsp), %rcx
addq %rbx, %rcx
movq 0x8(%rsp), %rdx
addq %rbx, %rdx
xorl %esi, %esi
movb 0x62(%rsp,%rsi), %dil
xorb (%rdx,%rsi), %dil
movb %dil, 0x62(%rsp,%rsi)
movb %dil, (%rcx,%rsi)
incq %rsi
cmpq %rsi, %rax
jne 0x18f1
leaq 0x1(%rbp), %rax
movq %rax, %rcx
xorb %bpl, 0x61(%rsp,%rax)
incq %rcx
cmpl $0x2a, %eax
movq %rcx, %rax
jne 0x1912
movb $-0x5d, %al
movb $-0x60, 0x61(%rsp)
movb %al, 0x60(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x1080
xorl %eax, %eax
vpmovsxbq 0x190e(%rip), %ymm8 # 0x3258
vpmovsxbq 0x1909(%rip), %ymm9 # 0x325c
vpmovsxbq 0x1904(%rip), %ymm10 # 0x3260
vpmovsxbq 0x18ff(%rip), %ymm11 # 0x3264
vpmovsxbq 0x18fa(%rip), %ymm12 # 0x3268
vpmovsxbq 0x18f5(%rip), %ymm13 # 0x326c
vpmovsxbq 0x18d4(%rip), %ymm14 # 0x3254
vpmovsxbq 0x18c7(%rip), %ymm15 # 0x3250
vpbroadcastq 0x18ad(%rip), %ymm16 # 0x3240
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 (%r13,%rax), %ymm0 {%k1} {z}
vmovdqu8 (%r15,%rax), %ymm1 {%k1} {z}
vpxor %ymm0, %ymm1, %ymm0
vmovdqu8 %ymm0, (%r15,%rax) {%k1}
addq $0x20, %rax
cmpq $0x40, %rax
jne 0x1993
incq %r14
leaq -0x2a(%rbp), %rax
cmpq $0x2a, %rbp
movl $0x0, %ecx
cmovbq %rcx, %rax
movl $0x2a, %ecx
cmovaeq %rcx, %rbp
addq %rbp, %rbx
movq 0x10(%rsp), %rcx
movq %rbx, (%rcx)
movq %rax, %rbp
testq %rax, %rax
vpbroadcastq 0x17b4(%rip), %ymm5 # 0x3238
vpbroadcastq 0x179b(%rip), %ymm6 # 0x3228
jne 0x17b4
testq %r12, %r12
je 0x1d12
cmpq $0x0, 0x18(%rsp)
je 0x1dbe
leaq 0x36(%rsp), %rbx
leaq 0xe2(%rsp), %r14
movl $0x1, %r15d
leaq 0xa2(%rsp), %rbp
vpbroadcastq 0x177c(%rip), %ymm3 # 0x3248
vpbroadcastq 0x1753(%rip), %ymm4 # 0x3228
vpbroadcastq 0x1752(%rip), %ymm5 # 0x3230
xorl %r13d, %r13d
movb $-0x60, 0x21(%rsp)
cmpq $0x16, %r12
setae %al
xorb $-0x5b, %al
movb %al, 0x20(%rsp)
vpbroadcastq %r15, %ymm0
movl $0x2, %eax
vpmovsxbq 0x1749(%rip), %ymm1 # 0x3250
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, 0x20(%rsp,%rax) {%k1}
vpaddq %ymm5, %ymm1, %ymm1
addq $0x4, %rax
cmpq $0x16, %rax
jne 0x1b07
movq 0x18(%rsp), %rax
addq %r13, %rax
cmpq $0x15, %r12
jbe 0x1b61
movq 0xe(%rax), %rcx
movq %rcx, 0xe(%rbx)
vmovdqu (%rax), %xmm0
vmovdqu %xmm0, (%rbx)
jmp 0x1b8f
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
leal 0x16(%rcx), %esi
movb %dl, 0x20(%rsp,%rsi)
incq %rcx
cmpl %r12d, %ecx
jb 0x1b63
leaq (%rbx,%r12), %rdi
movl $0x15, %edx
subl %r12d, %edx
incq %rdx
movl %r12d, %esi
vzeroupper
callq 0x1030
movq 0x1b0(%rsp), %rcx
movl 0x10(%rcx), %eax
movl %eax, 0x26(%rbx)
vmovups (%rcx), %xmm0
vmovups %xmm0, 0x16(%rbx)
leaq 0x20(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
vzeroupper
callq 0x1080
xorl %eax, %eax
vpmovsxbq 0x1692(%rip), %ymm8 # 0x3258
vpmovsxbq 0x168d(%rip), %ymm9 # 0x325c
vpmovsxbq 0x1688(%rip), %ymm10 # 0x3260
vpmovsxbq 0x1683(%rip), %ymm11 # 0x3264
vpmovsxbq 0x167e(%rip), %ymm12 # 0x3268
vpmovsxbq 0x1679(%rip), %ymm13 # 0x326c
vpmovsxbq 0x1658(%rip), %ymm14 # 0x3254
vpmovsxbq 0x164b(%rip), %ymm15 # 0x3250
vpbroadcastq 0x1631(%rip), %ymm16 # 0x3240
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 (%r14,%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 0x1c0f
incq %r15
leaq -0x16(%r12), %rax
cmpq $0x16, %r12
movl $0x0, %ecx
cmovbq %rcx, %rax
movl $0x16, %ecx
cmovaeq %rcx, %r12
addq %r12, %r13
movq %rax, %r12
testq %rax, %rax
vpbroadcastq 0x154e(%rip), %ymm3 # 0x3248
vpbroadcastq 0x1525(%rip), %ymm4 # 0x3228
vpbroadcastq 0x1524(%rip), %ymm5 # 0x3230
jne 0x1ae1
movq 0x1b0(%rsp), %rbx
movl 0x10(%rbx), %eax
leaq 0xa0(%rsp), %rdi
movl %eax, 0x3c(%rdi)
vmovups (%rbx), %xmm0
vmovups %xmm0, 0x2c(%rdi)
movw $0xa0a6, (%rdi)         # imm = 0xA0A6
leaq 0x120(%rsp), %r12
movq %r12, %rsi
vzeroupper
callq 0x1080
movl $0xfffff, %eax          # imm = 0xFFFFF
kmovd %eax, %k1
vmovdqu8 (%rbx), %ymm0 {%k1} {z}
vmovdqu8 0x2c(%r12), %ymm1 {%k1} {z}
vpxor %ymm0, %ymm1, %ymm0
vmovdqu8 %ymm0, 0x2c(%r12) {%k1}
xorl %eax, %eax
movq 0x10(%rsp), %rsi
movq (%rsp), %rdi
movb 0x120(%rsp,%rax), %cl
movq (%rsi), %rdx
addq %rdi, %rdx
movb %cl, (%rax,%rdx)
incq %rax
cmpq $0x14, %rax
jne 0x1d7d
addq $0x14, (%rsi)
xorl %eax, %eax
jmp 0x1dc3
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1ddf
movq $0x0, (%rbx)
xorl %eax, %eax
jmp 0x1ddf
movl $0xfffffff9, %eax       # imm = 0xFFFFFFF9
jmp 0x1ddf
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
jmp 0x1dc3
movl $0xfffffffc, %eax       # imm = 0xFFFFFFFC
vpxor %xmm0, %xmm0, %xmm0
vmovdqu %ymm0, 0xc0(%rsp)
vmovdqu %ymm0, 0xe0(%rsp)
vmovdqu %ymm0, 0x60(%rsp)
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq