// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/primatesv1ape80/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 <-- O||K
	InitializeState(V,key);
	// Treating the nonce as the first part of the associated data
	for(int i=0; i<(NonceSizeElements/RateSizeElements); i++)
	{
		// V <-- p_1 (N[i]^V_r || V_c)
		for(int j=0; j<RateSizeElements; j++)
			V[j]^=nonce[RateSizeElements*i+j];
		p_1(V);
	}
	if(adlen!=0)
	{ 
		// for i=1 to u-1 (except the last block which needs padding)
		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_1 (A[i]^V_r || V_c)
				for(int j=0; j<RateSizeElements; j++)
					V[j]^=data[j];
				p_1(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);	
	}	
	// V <-- V ^ (0..01)
	V[StateSize-1]^=1;
	//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
			//V <-- p_1 (M[i]^V_r || V_c)
			for(int j=0; j<RateSizeElements; j++)
				V[j]^=data[j];
			p_1(V);
			// C[i] <-- V_r
			for(int j=0; j<RateSizeElements; j++)
				cipher[j]=V[j];
			Element2Bytes(c,cipher,i,RateSizeBytes); //representing a block of c as bytes
		}
	}
	
	//for i=w
	int l = mlen%RateSizeBytes;
	// //V <-- p_1 (M[i]^V_r || V_c)
	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++)
			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]=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++)
			V[i]^=data[i];
	}	
	p_1(V);
	// C[i] <-- V_r
	for(int j=0; j<RateSizeElements; j++)
		cipher[j]=V[j];
	if(mlen<RateSizeBytes)
		Element2Bytes(c,cipher,0,RateSizeBytes); //representing last block of c as bytes
	else
		Element2Bytes(c,cipher,mlen-RateSizeBytes,RateSizeBytes); //representing last block of c as bytes

	// T <-- (V_c)_c/2 ^ K
	for(int i=0; i<TagSizeElements; i++)
		tag[i]=V[RateSizeElements+i]^key[i]; 
	if ((mlen>0)&&(mlen<RateSizeBytes)){
		Element2Bytes(c,tag,RateSizeBytes,TagSizeBytes) ;
		*clen=TagSizeBytes+RateSizeBytes;
	}
	else{
		Element2Bytes(c,tag,mlen,TagSizeBytes) ;
		*clen=mlen+TagSizeBytes;
	}
	
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r9, 0x18(%rsp)
movq %r8, %rbp
movq %rcx, %r13
movq %rdx, 0x20(%rsp)
movq %rsi, 0x88(%rsp)
movq %rdi, 0x10(%rsp)
movq 0x108(%rsp), %r14
movq 0x110(%rsp), %rsi
xorl %r12d, %r12d
leaq 0xa0(%rsp), %r15
pushq $0x20
popq %rcx
movq %r15, %rdi
xorl %edx, %edx
callq 0x2190
leaq 0x90(%rsp), %rbx
pushq $0x10
popq %rcx
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2190
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x20a0
cmpq $0x2, %r12
je 0x2345
xorl %eax, %eax
cmpq $0x8, %rax
je 0x2334
movb (%rbx,%rax), %cl
xorb %cl, 0x30(%rsp,%rax)
incq %rax
jmp 0x2322
movq %r14, %rdi
callq 0x2060
incq %r12
addq $0x8, %rbx
jmp 0x231a
movq 0x18(%rsp), %rax
testq %rax, %rax
movq %r13, 0x8(%rsp)
je 0x246a
cmpq $0x6, %rax
jb 0x23ad
movq 0x18(%rsp), %rax
leaq -0x5(%rax), %r13
xorl %ebx, %ebx
movq %rsp, %r14
pushq $0x8
popq %r15
leaq 0x30(%rsp), %r12
cmpq %r13, %rbx
jae 0x23ad
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movl %r15d, %ecx
callq 0x2190
xorl %eax, %eax
cmpq $0x8, %rax
je 0x239f
movb (%rsp,%rax), %cl
xorb %cl, 0x30(%rsp,%rax)
incq %rax
jmp 0x238d
movq %r12, %rdi
callq 0x2060
addq $0x5, %rbx
jmp 0x2375
movq 0x18(%rsp), %r8
movq %r8, %rax
xorl %edx, %edx
pushq $0x5
popq %rsi
divq %rsi
testq %rdx, %rdx
je 0x242d
movq %r8, %rax
subq %rdx, %rax
addq %rax, %rbp
xorl %eax, %eax
cmpq %rax, %rdx
je 0x23df
movb (%rbp,%rax), %cl
movb %cl, 0x60(%rsp,%rax)
incq %rax
jmp 0x23cd
movq %r8, %rax
xorl %edx, %edx
divq %rsi
leaq (%rax,%rax,4), %rax
notq %rax
addq %r8, %rax
movb $-0x80, %cl
movb %cl, 0x61(%rsp,%rax)
incq %rax
xorl %ecx, %ecx
cmpq $0x4, %rax
jb 0x23f3
xorl %ebx, %ebx
movq %rsp, %rdi
leaq 0x60(%rsp), %rsi
pushq $0x8
popq %rcx
xorl %edx, %edx
callq 0x2190
movq 0x8(%rsp), %r13
cmpq $0x8, %rbx
je 0x2460
movb (%rsp,%rbx), %al
xorb %al, 0x30(%rsp,%rbx)
incq %rbx
jmp 0x241b
addq $-0x5, %r8
movq %rsp, %rdi
pushq $0x8
popq %rcx
movq %rbp, %rsi
movq %r8, %rdx
callq 0x2190
xorl %eax, %eax
movq 0x8(%rsp), %r13
cmpq $0x8, %rax
je 0x245b
movb (%rsp,%rax), %cl
xorb %cl, 0x30(%rsp,%rax)
incq %rax
jmp 0x2449
xorb $0x10, 0x38(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x2060
xorb $0x1, 0x57(%rsp)
cmpq $0x6, %r13
jb 0x24f3
movq 0x8(%rsp), %rax
leaq -0x5(%rax), %rbp
xorl %r13d, %r13d
pushq $0x8
popq %rbx
leaq 0x30(%rsp), %r14
leaq 0x28(%rsp), %r15
pushq $0x5
popq %r12
cmpq %rbp, %r13
jae 0x24f3
movq %rsp, %rdi
movq 0x20(%rsp), %rsi
movq %r13, %rdx
movl %ebx, %ecx
callq 0x2190
xorl %eax, %eax
cmpq $0x8, %rax
je 0x24bd
movb (%rsp,%rax), %cl
xorb %cl, 0x30(%rsp,%rax)
incq %rax
jmp 0x24ab
movq %r14, %rdi
callq 0x2060
xorl %eax, %eax
cmpq $0x8, %rax
je 0x24da
movb 0x30(%rsp,%rax), %cl
movb %cl, 0x28(%rsp,%rax)
incq %rax
jmp 0x24c7
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r13, %rdx
movl %r12d, %ecx
callq 0x2080
addq $0x5, %r13
jmp 0x2492
movq 0x8(%rsp), %r14
movq %r14, %rax
xorl %edx, %edx
pushq $0x5
popq %rdi
divq %rdi
testq %rdx, %rdx
setne %al
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0x253b
leaq -0x5(%r14), %rdx
movq %rsp, %rdi
pushq $0x8
popq %rcx
movq 0x20(%rsp), %rsi
callq 0x2190
xorl %eax, %eax
cmpq $0x8, %rax
je 0x2560
movb (%rsp,%rax), %cl
xorb %cl, 0x30(%rsp,%rax)
incq %rax
jmp 0x2529
movq %r14, %rax
subq %rdx, %rax
movq 0x20(%rsp), %rsi
addq %rax, %rsi
xorl %eax, %eax
cmpq %rax, %rdx
je 0x25e2
movb (%rsi,%rax), %cl
movb %cl, 0x60(%rsp,%rax)
incq %rax
jmp 0x254b
xorb $0x10, 0x38(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x2060
xorl %eax, %eax
cmpq $0x8, %rax
je 0x2584
movb 0x30(%rsp,%rax), %cl
movb %cl, 0x28(%rsp,%rax)
incq %rax
jmp 0x2571
xorl %ebx, %ebx
movq %r14, %rdx
subq $0x5, %rdx
cmovbq %rbx, %rdx
leaq 0x28(%rsp), %rsi
pushq $0x5
popq %rcx
movq 0x10(%rsp), %rdi
callq 0x2080
cmpq $0x20, %rbx
je 0x25bd
movb 0xa0(%rsp,%rbx), %al
xorb 0x38(%rsp,%rbx), %al
movb %al, 0x60(%rsp,%rbx)
incq %rbx
jmp 0x25a3
leaq -0x1(%r14), %rax
leaq 0x60(%rsp), %rsi
cmpq $0x4, %rax
jae 0x262f
pushq $0x5
popq %rdx
pushq $0x14
popq %rcx
movq 0x10(%rsp), %rdi
callq 0x2080
pushq $0x19
popq %r14
jmp 0x2643
movq %r14, %rax
xorl %edx, %edx
divq %rdi
leaq (%rax,%rax,4), %rax
notq %rax
addq %r14, %rax
movb $-0x80, %cl
movb %cl, 0x61(%rsp,%rax)
incq %rax
xorl %ecx, %ecx
cmpq $0x4, %rax
jb 0x25f6
xorl %ebx, %ebx
movq %rsp, %rdi
leaq 0x60(%rsp), %rsi
pushq $0x8
popq %rcx
xorl %edx, %edx
callq 0x2190
cmpq $0x8, %rbx
je 0x2565
movb (%rsp,%rbx), %al
xorb %al, 0x30(%rsp,%rbx)
incq %rbx
jmp 0x2619
pushq $0x14
popq %rcx
movq 0x10(%rsp), %rdi
movq %r14, %rdx
callq 0x2080
addq $0x14, %r14
movq 0x88(%rsp), %rax
movq %r14, (%rax)
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq