// File: /workspace/github_c_makefile_star2_O2/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, 0x30(%rsp)
movq %r8, %rbp
movq %rcx, 0x38(%rsp)
movq %rdx, %r12
movq %rsi, 0x78(%rsp)
movq %rdi, 0x18(%rsp)
movq 0xe8(%rsp), %rbx
movq 0xf0(%rsp), %rsi
leaq 0x80(%rsp), %r14
pushq $0x10
popq %r15
movq %r14, %rdi
xorl %edx, %edx
movl %r15d, %ecx
callq 0x1160
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movl %r15d, %ecx
callq 0x1160
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x10d0
movq %rbx, %rdi
movq 0x30(%rsp), %rbx
callq 0x1070
testq %rbx, %rbx
je 0x1402
cmpq $0x6, %rbx
jb 0x1346
movq 0x30(%rsp), %rax
leaq -0x5(%rax), %r14
xorl %ebx, %ebx
pushq $0x8
popq %r15
leaq 0x40(%rsp), %r13
cmpq %r14, %rbx
jae 0x1346
leaq 0x8(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movl %r15d, %ecx
callq 0x1160
xorl %eax, %eax
cmpq $0x8, %rax
je 0x1338
movb 0x8(%rsp,%rax), %cl
xorb %cl, 0x40(%rsp,%rax)
incq %rax
jmp 0x1325
movq %r13, %rdi
callq 0x1050
addq $0x5, %rbx
jmp 0x130b
movq 0x30(%rsp), %r8
movq %r8, %rax
xorl %edx, %edx
pushq $0x5
popq %rcx
divq %rcx
testq %rdx, %rdx
je 0x13c7
movq %r8, %rax
subq %rdx, %rax
addq %rax, %rbp
xorl %eax, %eax
cmpq %rax, %rdx
je 0x1378
movb (%rbp,%rax), %cl
movb %cl, 0x20(%rsp,%rax)
incq %rax
jmp 0x1366
movq %r8, %rax
xorl %edx, %edx
pushq $0x5
popq %rcx
divq %rcx
leaq (%rax,%rax,4), %rax
notq %rax
addq %r8, %rax
movb $-0x80, %cl
movb %cl, 0x21(%rsp,%rax)
incq %rax
xorl %ecx, %ecx
cmpq $0x4, %rax
jb 0x138f
xorl %ebx, %ebx
leaq 0x8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
pushq $0x8
popq %rcx
xorl %edx, %edx
callq 0x1160
cmpq $0x8, %rbx
je 0x13f8
movb 0x8(%rsp,%rbx), %al
xorb %al, 0x40(%rsp,%rbx)
incq %rbx
jmp 0x13b4
addq $-0x5, %r8
leaq 0x8(%rsp), %rdi
pushq $0x8
popq %rcx
movq %rbp, %rsi
movq %r8, %rdx
callq 0x1160
xorl %eax, %eax
cmpq $0x8, %rax
je 0x13f3
movb 0x8(%rsp,%rax), %cl
xorb %cl, 0x40(%rsp,%rax)
incq %rax
jmp 0x13e0
xorb $0x10, 0x48(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x1070
cmpq $0x6, 0x38(%rsp)
jb 0x148f
movq 0x38(%rsp), %rax
leaq -0x5(%rax), %r14
xorl %r13d, %r13d
leaq 0x10(%rsp), %rbx
pushq $0x5
popq %r15
leaq 0x40(%rsp), %rbp
cmpq %r14, %r13
jae 0x148f
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
pushq $0x8
popq %rcx
callq 0x1160
xorl %eax, %eax
cmpq $0x8, %rax
je 0x1459
movb 0x8(%rsp,%rax), %cl
xorb 0x40(%rsp,%rax), %cl
movb %cl, 0x10(%rsp,%rax)
incq %rax
jmp 0x1442
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq %r13, %rdx
movl %r15d, %ecx
callq 0x1090
xorl %eax, %eax
cmpq $0x8, %rax
je 0x1481
movb 0x10(%rsp,%rax), %cl
movb %cl, 0x40(%rsp,%rax)
incq %rax
jmp 0x146e
movq %rbp, %rdi
callq 0x1070
addq $0x5, %r13
jmp 0x1428
movq 0x38(%rsp), %r15
movq %r15, %rax
xorl %edx, %edx
pushq $0x5
popq %rcx
divq %rcx
testq %rdx, %rdx
setne %al
testq %r15, %r15
sete %cl
orb %al, %cl
jne 0x14e4
leaq -0x5(%r15), %rbx
leaq 0x8(%rsp), %rdi
pushq $0x8
popq %rcx
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1160
xorl %eax, %eax
movq 0x78(%rsp), %r12
cmpq $0x8, %rax
je 0x1508
movb 0x8(%rsp,%rax), %cl
xorb 0x40(%rsp,%rax), %cl
movb %cl, 0x10(%rsp,%rax)
incq %rax
jmp 0x14cd
movq %rdx, %r13
movq %r15, %rbx
subq %rdx, %rbx
addq %rbx, %r12
xorl %eax, %eax
cmpq %rax, %r13
je 0x158e
movb (%r12,%rax), %cl
movb %cl, 0x20(%rsp,%rax)
incq %rax
jmp 0x14f2
leaq 0x10(%rsp), %rsi
pushq $0x5
popq %rcx
movq 0x18(%rsp), %rdi
movq %rbx, %rdx
callq 0x1090
xorb $0x10, 0x48(%rsp)
xorl %eax, %eax
cmpq $0x8, %rax
je 0x1537
movb 0x10(%rsp,%rax), %cl
movb %cl, 0x40(%rsp,%rax)
incq %rax
jmp 0x1524
leaq 0x40(%rsp), %rdi
callq 0x1070
xorl %eax, %eax
cmpq $0x10, %rax
je 0x155d
movb 0x80(%rsp,%rax), %cl
xorb 0x48(%rsp,%rax), %cl
movb %cl, 0x20(%rsp,%rax)
incq %rax
jmp 0x1543
leaq 0x20(%rsp), %rsi
pushq $0xa
popq %rcx
movq 0x18(%rsp), %rdi
movq %r15, %rdx
callq 0x1090
addq $0xa, %r15
movq %r15, (%r12)
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rax
notq %rax
addq %r15, %rax
movb $-0x80, %cl
movb %cl, 0x21(%rsp,%rax)
incq %rax
xorl %ecx, %ecx
cmpq $0x4, %rax
jb 0x1599
xorl %r14d, %r14d
leaq 0x8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
pushq $0x8
popq %rcx
xorl %edx, %edx
callq 0x1160
movq 0x78(%rsp), %r12
cmpq $0x8, %r14
je 0x15de
movb 0x8(%rsp,%r14), %al
xorb 0x40(%rsp,%r14), %al
movb %al, 0x10(%rsp,%r14)
incq %r14
jmp 0x15c4
xorl %r14d, %r14d
leaq 0x73(%rsp), %rdi
leaq 0x10(%rsp), %rsi
pushq $0x5
popq %rcx
xorl %edx, %edx
callq 0x1090
addq 0x18(%rsp), %rbx
cmpq %r14, %r13
je 0x1522
movb 0x73(%rsp,%r14), %al
movb %al, (%rbx,%r14)
incq %r14
jmp 0x15fa