// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/primatesv1hanuman80/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
 )
 {
    /*...
    ... generating a ciphertext c[0],c[1],...,c[*clen-1]
    ... from a plaintext m[0],m[1],...,m[mlen-1]
    ... and associated data ad[0],ad[1],...,ad[adlen-1]
    ... and secret message number nsec[0],nsec[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];
   
	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); // representing last block of ad as elements
			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); // representing last padded block of ad as elements
			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(mlen>RateSizeBytes)
	{
		for(unsigned long long i=0; i<mlen-RateSizeBytes; i=i+RateSizeBytes)
		{ 
			Bytes2Element(data,m,i,RateSizeElements); //representing a block of m as elements
			// C[i] <-- M[i]^V_r
			for(int j=0; j<RateSizeElements; j++)
				cipher[j]=V[j]^data[j];
			Element2Bytes(c,cipher,i,RateSizeBytes); //representing a block of c 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 = mlen%RateSizeBytes;
	// C[i] <-- M[i]^V_r
	if((l==0) && (mlen!=0)){ //do padding with spill over			
		Bytes2Element(data,m,mlen-RateSizeBytes,RateSizeElements); // representing last block of m as elements
		for(int i=0; i<RateSizeElements; i++)
			cipher[i]=V[i]^data[i];
		Element2Bytes(c,cipher,mlen-RateSizeBytes,RateSizeBytes); //representing last block of c as bytes
		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]=m[mlen-l+i];
		dataP[l]=128; //padding over bytes
		for(int i=l+1; i<RateSizeBytes; i++)
			dataP[i]=0;
		Bytes2Element(data,dataP,0,RateSizeElements);  // representing last block of m as elements
		for(int i=0; i<RateSizeElements; i++)
			cipher[i]=V[i]^data[i];
		unsigned char ct[RateSizeBytes];
		Element2Bytes(ct,cipher,0,RateSizeBytes); //representing last block of c as bytes
		for(int i=0; i< l; i++)
			c[mlen-l+i]=ct[i]; //storing only l bytes of c
	}	
	//V <-- p_1 (C[i] || V_c)
	for(int j=0; j<RateSizeElements; j++)
		V[j]=cipher[j];
	p_1(V);

	// T <-- (V_c)_c/2 ^ K
	for(int i=0; i<TagSizeElements; i++)
		tag[i]=V[RateSizeElements+i]^key[i]; 
	Element2Bytes(c,tag,mlen,TagSizeBytes) ;
	*clen=mlen+TagSizeBytes;
	
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, %r12
movq %r8, %rbp
movq %rcx, %r15
movq %rdx, 0x70(%rsp)
movq %rsi, 0x78(%rsp)
movq %rdi, 0x28(%rsp)
movq 0xe8(%rsp), %rbx
movq 0xf0(%rsp), %rsi
leaq 0x80(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
movl $0x10, %ecx
callq 0x1140
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movl $0x10, %ecx
callq 0x1140
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r12, %r14
callq 0x10e0
movq %rbx, %rdi
callq 0x1080
testq %r12, %r12
movq %r15, 0x30(%rsp)
je 0x13d7
movq %r14, %r12
cmpq $0x6, %r14
jb 0x1317
leaq -0x5(%r12), %r15
xorl %ebx, %ebx
movq %rsp, %r14
leaq 0x40(%rsp), %r13
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movl $0x8, %ecx
callq 0x1140
movq (%rsp), %rax
xorq %rax, 0x40(%rsp)
movq %r13, %rdi
callq 0x1060
addq $0x5, %rbx
cmpq %r15, %rbx
jb 0x12ea
movq %r12, %rdx
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
mulxq %rax, %r14, %r14
shrq $0x2, %r14
leaq (%r14,%r14,4), %rax
movq %r12, %rbx
subq %rax, %rbx
je 0x13a1
movq %r12, %rax
subq %rbx, %rax
addq %rax, %rbp
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x1150
movb $-0x80, 0x10(%rsp,%rbx)
cmpq $0x3, %rbx
ja 0x1380
leal (%r14,%r14,4), %edx
movl %r12d, %eax
subl %edx, %eax
incl %eax
leaq (%rsp,%rax), %rdi
addq $0x10, %rdi
subl %r12d, %edx
addl $0x3, %edx
incq %rdx
xorl %esi, %esi
callq 0x1040
movq %rsp, %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
movl $0x8, %ecx
callq 0x1140
movq (%rbx), %rax
xorq %rax, 0x40(%rsp)
jmp 0x13c8
addq $-0x5, %r12
movq %rsp, %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movl $0x8, %ecx
callq 0x1140
movq (%rbx), %rax
xorq %rax, 0x40(%rsp)
xorb $0x10, 0x48(%rsp)
movq 0x30(%rsp), %r15
leaq 0x40(%rsp), %rdi
callq 0x1080
cmpq $0x6, %r15
movq 0x28(%rsp), %r12
movq 0x70(%rsp), %rbp
jb 0x144c
movq 0x30(%rsp), %rax
leaq -0x5(%rax), %r15
xorl %r13d, %r13d
leaq 0x8(%rsp), %r14
leaq 0x40(%rsp), %rbx
movq %rsp, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movl $0x8, %ecx
callq 0x1140
movq (%rsp), %rax
xorq 0x40(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl $0x5, %ecx
callq 0x10a0
movq 0x8(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %rbx, %rdi
callq 0x1080
addq $0x5, %r13
cmpq %r15, %r13
jb 0x13fd
movq 0x30(%rsp), %r15
movq %r15, %rdx
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
mulxq %rax, %rbx, %rbx
shrq $0x2, %rbx
leaq (%rbx,%rbx,4), %rax
movq %r15, %r13
subq %rax, %r13
setne %al
testq %r15, %r15
sete %cl
orb %al, %cl
jne 0x14c7
leaq -0x5(%r15), %rbx
movq %rsp, %r14
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movl $0x8, %ecx
callq 0x1140
movq (%r14), %rax
xorq 0x40(%rsp), %rax
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
movq 0x28(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rdx
movl $0x5, %ecx
callq 0x10a0
xorb $0x10, 0x48(%rsp)
jmp 0x1578
testq %r13, %r13
je 0x14f8
movq %r15, %rax
subq %r13, %rax
addq %rax, %rbp
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x1150
movb $-0x80, 0x10(%rsp,%r13)
cmpq $0x3, %r13
movq 0x28(%rsp), %r14
jbe 0x1503
jmp 0x1525
movb $-0x80, 0x10(%rsp,%r13)
movq 0x28(%rsp), %r14
leal (%rbx,%rbx,4), %edx
movl %r15d, %eax
subl %edx, %eax
incl %eax
leaq (%rsp,%rax), %rdi
addq $0x10, %rdi
subl %r15d, %edx
addl $0x3, %edx
incq %rdx
xorl %esi, %esi
callq 0x1040
movq %rsp, %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
movl $0x8, %ecx
callq 0x1140
movq (%rbx), %rax
xorq 0x40(%rsp), %rax
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x3b(%rsp), %rdi
xorl %edx, %edx
movl $0x5, %ecx
callq 0x10a0
testq %r13, %r13
je 0x1578
movq %r15, %rdi
subq %r13, %rdi
addq %r14, %rdi
leaq 0x3b(%rsp), %rsi
movq %r13, %rdx
callq 0x1150
movq 0x8(%rsp), %rax
leaq 0x40(%rsp), %rbx
movq %rax, (%rbx)
movq %rbx, %rdi
callq 0x1080
vmovaps 0x80(%rsp), %xmm0
vxorps 0x8(%rbx), %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
vmovaps %xmm0, (%rsi)
movq %r14, %rdi
movq %r15, %rdx
movl $0xa, %ecx
callq 0x10a0
addq $0xa, %r15
movq 0x78(%rsp), %rax
movq %r15, (%rax)
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq