// File: /workspace/github_c_makefile_star2_O3/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 $0x198, %rsp            # imm = 0x198
movq %r9, %r14
movq %r8, 0x20(%rsp)
movq %rcx, %rbp
movq %rdx, 0x18(%rsp)
movq %rsi, %r15
movq %rdi, %rbx
callq 0x1050
testq %r15, %r15
je 0x1d51
movq %r14, %rax
orq %rbp, %rax
je 0x1d58
cmpq $0x0, 0x1e0(%rsp)
sete %al
testq %rbx, %rbx
sete %cl
orb %al, %cl
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jne 0x1d61
cmpq $0x0, 0x1d8(%rsp)
je 0x1d76
movq %r14, 0x40(%rsp)
movq %rbx, 0x8(%rsp)
movq $0x0, (%r15)
vpxor %xmm0, %xmm0, %xmm0
vmovdqu %ymm0, 0xf0(%rsp)
vmovdqu %ymm0, 0xd0(%rsp)
testq %rbp, %rbp
movq %r15, 0x10(%rsp)
je 0x1a34
cmpq $0x0, 0x18(%rsp)
je 0x1d7d
leaq 0x1(%rbp), %rax
movq %rax, 0x28(%rsp)
leaq 0x112(%rsp), %r13
movl $0x1, %ecx
leaq 0xd2(%rsp), %rbx
movw $0x3f, %ax
kmovd %eax, %k1
kmovw %k1, 0x6(%rsp)
xorl %r12d, %r12d
movq %rbp, %r14
movl %ebp, %r15d
cmpq $0x1, %r15
adcq $0x0, %r15
movb $-0x60, 0x51(%rsp)
cmpq $0x2a, %rbp
movl $0x0, %eax
adcb $-0x60, %al
movq %rcx, 0x48(%rsp)
vpbroadcastq %rcx, %ymm0
vpsrlvq 0x1985(%rip), %ymm0, %ymm1 # 0x3140
vpsrlvq 0x199c(%rip), %ymm0, %ymm0 # 0x3160
vpmovqb %ymm0, %xmm0
vpmovqb %ymm1, %xmm1
movb %al, 0x50(%rsp)
vpunpckldq %xmm0, %xmm1, %xmm0 # xmm0 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
vmovdqu %xmm0, 0x52(%rsp)
leaq 0x68(%rsp), %rdx
vpxor %xmm0, %xmm0, %xmm0
kmovw 0x6(%rsp), %k1
vmovdqu8 %xmm0, -0x6(%rdx) {%k1}
movq 0x1d8(%rsp), %rcx
movl 0x10(%rcx), %eax
movl %eax, 0x10(%rdx)
vmovups (%rcx), %xmm0
vmovups %xmm0, (%rdx)
movq 0x1e0(%rsp), %rcx
movl 0x10(%rcx), %eax
movl %eax, 0x24(%rdx)
vmovdqu (%rcx), %xmm0
vmovdqu %xmm0, 0x14(%rdx)
leaq 0x50(%rsp), %rdi
leaq 0x90(%rsp), %rsi
vzeroupper
callq 0x1080
subq $0x2a, %r14
movq %r14, 0x30(%rsp)
jae 0x1895
movq 0x8(%rsp), %rax
addq %r12, %rax
movq 0x18(%rsp), %rcx
addq %r12, %rcx
xorl %edx, %edx
movb 0x92(%rsp,%rdx), %sil
xorb (%rcx,%rdx), %sil
movb %sil, 0x92(%rsp,%rdx)
movb %sil, (%rax,%rdx)
incq %rdx
cmpq %rdx, %r15
jne 0x1856
movq 0x28(%rsp), %rcx
movq %rcx, %rax
xorb %bpl, 0x91(%rsp,%rcx)
incq %rax
cmpl $0x2a, %ecx
movq %rax, %rcx
jne 0x187e
movb $-0x5d, %al
jmp 0x18ca
movq 0x8(%rsp), %rax
addq %r12, %rax
movq 0x18(%rsp), %rcx
addq %r12, %rcx
xorl %edx, %edx
movb 0x92(%rsp,%rdx), %sil
xorb (%rcx,%rdx), %sil
movb %sil, 0x92(%rsp,%rdx)
movb %sil, (%rax,%rdx)
incq %rdx
cmpq $0x2a, %rdx
jne 0x18a7
movb $-0x5e, %al
movq 0x10(%rsp), %r15
movb $-0x60, 0x91(%rsp)
movb %al, 0x90(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x1080
xorl %eax, %eax
vpmovsxbq 0x198e(%rip), %ymm8 # 0x328c
vpmovsxbq 0x1989(%rip), %ymm9 # 0x3290
vpmovsxbq 0x1984(%rip), %ymm10 # 0x3294
vpmovsxbq 0x197f(%rip), %ymm11 # 0x3298
vpmovsxbq 0x197a(%rip), %ymm12 # 0x329c
vpmovsxbq 0x1975(%rip), %ymm13 # 0x32a0
vpmovsxbq 0x1970(%rip), %ymm14 # 0x32a4
vpmovsxbq 0x194b(%rip), %ymm15 # 0x3288
vpbroadcastq 0x1931(%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 (%r13,%rax), %ymm0 {%k1} {z}
vmovdqu8 (%rbx,%rax), %ymm1 {%k1} {z}
vpxor %ymm0, %ymm1, %ymm0
vmovdqu8 %ymm0, (%rbx,%rax) {%k1}
addq $0x20, %rax
cmpq $0x40, %rax
jne 0x1947
movq 0x48(%rsp), %rcx
incq %rcx
cmpq $0x2a, %rbp
movl $0x2a, %eax
cmovbq %rbp, %rax
addq %rax, %r12
addq $-0x2a, 0x28(%rsp)
movq %r12, (%r15)
cmpq $0x2a, %rbp
movq 0x30(%rsp), %r14
movq %r14, %rbp
ja 0x178c
movq 0x40(%rsp), %rax
testq %rax, %rax
je 0x1cc8
cmpq $0x0, 0x20(%rsp)
je 0x1d84
vpmovsxbq 0x15ab(%rip), %xmm0 # 0x3002
leaq 0x66(%rsp), %rbx
vpinsrq $0x1, %rax, %xmm0, %xmm0
leaq 0x112(%rsp), %r14
leaq 0xd2(%rsp), %r12
xorl %r15d, %r15d
vpbroadcastq 0x1802(%rip), %ymm3 # 0x3280
vpbroadcastq 0x17e1(%rip), %ymm4 # 0x3268
vpbroadcastq 0x17e0(%rip), %ymm5 # 0x3270
leaq 0x110(%rsp), %rbp
movb $-0x60, 0x51(%rsp)
vpextrq $0x1, %xmm0, %r13
cmpq $0x16, %r13
setb %al
orb $-0x5c, %al
movb %al, 0x50(%rsp)
vmovdqa %xmm0, 0x30(%rsp)
vpbroadcastq %xmm0, %ymm0
movl $0x2, %eax
vpmovsxbq 0x17bf(%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, 0x50(%rsp,%rax) {%k1}
vpaddq %ymm5, %ymm1, %ymm1
addq $0x4, %rax
cmpq $0x16, %rax
jne 0x1ac9
movq 0x20(%rsp), %rax
addq %r15, %rax
cmpq $0x15, %r13
ja 0x1b41
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
leal 0x16(%rcx), %esi
movb %dl, 0x50(%rsp,%rsi)
incq %rcx
cmpl %r13d, %ecx
jb 0x1b13
leaq (%rbx,%r13), %rdi
movl $0x15, %edx
subl %r13d, %edx
incq %rdx
movl %r13d, %esi
vzeroupper
callq 0x1030
jmp 0x1b51
movq 0xe(%rax), %rcx
movq %rcx, 0xe(%rbx)
vmovdqu (%rax), %xmm0
vmovdqu %xmm0, (%rbx)
movq 0x1e0(%rsp), %rcx
movl 0x10(%rcx), %eax
movl %eax, 0x26(%rbx)
vmovups (%rcx), %xmm0
vmovups %xmm0, 0x16(%rbx)
leaq 0x50(%rsp), %rdi
movq %rbp, %rsi
vzeroupper
callq 0x1080
xorl %eax, %eax
vpmovsxbq 0x1709(%rip), %ymm8 # 0x328c
vpmovsxbq 0x1704(%rip), %ymm9 # 0x3290
vpmovsxbq 0x16ff(%rip), %ymm10 # 0x3294
vpmovsxbq 0x16fa(%rip), %ymm11 # 0x3298
vpmovsxbq 0x16f5(%rip), %ymm12 # 0x329c
vpmovsxbq 0x16f0(%rip), %ymm13 # 0x32a0
vpmovsxbq 0x16eb(%rip), %ymm14 # 0x32a4
vpmovsxbq 0x16c6(%rip), %ymm15 # 0x3288
vpbroadcastq 0x16ac(%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 (%r14,%rax), %ymm0 {%k1} {z}
vmovdqu8 (%r12,%rax), %ymm1 {%k1} {z}
vpxor %ymm0, %ymm1, %ymm0
vmovdqu8 %ymm0, (%r12,%rax) {%k1}
addq $0x20, %rax
cmpq $0x40, %rax
jne 0x1bcc
cmpq $0x16, %r13
movl $0x16, %eax
cmovbq %r13, %rax
addq %rax, %r15
vmovdqa 0x30(%rsp), %xmm0
vpaddq 0x161d(%rip), %xmm0, %xmm0 # 0x32c0
cmpq $0x16, %r13
vpbroadcastq 0x15d0(%rip), %ymm3 # 0x3280
vpbroadcastq 0x15af(%rip), %ymm4 # 0x3268
vpbroadcastq 0x15ae(%rip), %ymm5 # 0x3270
ja 0x1a98
movq 0x1e0(%rsp), %rbx
movl 0x10(%rbx), %eax
leaq 0xd0(%rsp), %rdi
movl %eax, 0x3c(%rdi)
vmovups (%rbx), %xmm0
vmovups %xmm0, 0x2c(%rdi)
movw $0xa0a6, (%rdi)         # imm = 0xA0A6
leaq 0x150(%rsp), %r15
movq %r15, %rsi
vzeroupper
callq 0x1080
movl $0xfffff, %eax          # imm = 0xFFFFF
kmovd %eax, %k1
vmovdqu8 (%rbx), %ymm0 {%k1} {z}
vmovdqu8 0x2c(%r15), %ymm1 {%k1} {z}
vpxor %ymm0, %ymm1, %ymm0
vmovdqu8 %ymm0, 0x2c(%r15) {%k1}
xorl %eax, %eax
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdi
movb 0x150(%rsp,%rax), %cl
movq (%rsi), %rdx
addq %rdi, %rdx
movb %cl, (%rax,%rdx)
incq %rax
cmpq $0x14, %rax
jne 0x1d32
addq $0x14, (%rsi)
jmp 0x1d5f
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1d61
movq $0x0, (%r15)
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq
movl $0xfffffff9, %eax       # imm = 0xFFFFFFF9
jmp 0x1d61
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
jmp 0x1d61
movl $0xfffffffc, %eax       # imm = 0xFFFFFFFC
jmp 0x1d61