// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/omdsha256k256n104tau160v2/ref/omdsha256.c
// Function: hash
void
hash (hashblock taga, const hashblock key, const unsigned char *ad,
      unsigned long long int adlen)
{
   hashblock lstar, lzero;
   hashblock delta;
   hashblock left, right;
   hashblock xor_res, key_func_res;
   hashblock taures;
   unsigned int  modlen;
   long long int i, l;
   size_t b;

   /* CHANGE FROM V1 TO V2 HERE*/
   /* L* = F_K(0^n, <tau>_m) */
   l2b (taures, OMD_tau);
   key_func (lstar, block0s, key, taures);
   /* L[0] = 4.L* */
   double_block (lzero, lstar);
   double_block (lzero, lzero);

   /*b = n+m*/
   b = OMD_n + OMD_m;

   /* A_1 || A_2 ··· A_{l−1} || A_l = A, where |A_i| = b for 1 ≤ i ≤ l−1
      and |A_l| ≤ b */
   /* l=ceil(adlen/b) */
   l = (adlen + b - 1)/b;

   /* Tag_a = 0^n  */
   memset (taga, 0x00, OMD_n);
   /* If AD="" => Tag_a = 0^n */
   if (adlen == 0)
      {
	 return;
      }

   /* \bar{\delta}_{0,0} = 0^n */
   memset (delta, 0x00, OMD_n);

   /** for i = 1 to l-1 **/
   /**********************/
   for (i = 0; i < l-1; i++)
      {

	 /*  \bar{\delta}_{i, 0} =  \bar{\delta}{i-1, 0} ^ L[ntz(i)]  */
	 increment_masking_associated_data (delta, delta, lzero, i+1);

	 /* Left = A_l[b-1...m]; */
	 memcpy (left, &ad[i*b],  OMD_n);
	 /* Right = A_l[m-1...0]  */
	 memcpy (right,&ad[i*b+OMD_n],OMD_m);

	 /* Tag_a = Tag_a ^ F_K(Left ^ \delta_{i, 0}, right) */
	 xor_block (xor_res, delta, left);
	 key_func (key_func_res, xor_res, key, right);
	 xor_block (taga, taga, key_func_res);
      }
   /**************************/
   /******* endfor ***********/

   /* here adlen!=0, we process last ad block*/

   modlen = adlen%b;

   /* |A_l| = b then */
   if (modlen == 0)
      {
	 /* last mask = \bar{\delta}{l,0}  */
	 increment_masking_associated_data(delta, delta, lzero,l);

	 memcpy (left, &ad[(l-1)*b],      OMD_n);
	 memcpy (right,&ad[(l-1)*b+OMD_n],OMD_m);
      }
   /* |A_l| < b then */
   else
      {
	 /* last mask = \bar{\delta}{l-1,1}  */
	 final_masking_associated_data (delta, delta, lstar);

	 memset (left , 0x00, OMD_n );
	 memset (right, 0x00, OMD_m );

	 memcpy (left, &ad[(l-1)*b], (modlen < OMD_n ? modlen : OMD_n));
	 memcpy (right, &ad[(l-1)*b+OMD_n], (modlen < OMD_n
					     ? 0
					     : modlen - OMD_n));

	 if (modlen < OMD_n)
	    /* pad last block */
	    left[modlen]= 0x80;
	 else
	    right[modlen-OMD_n]=0x80;
   }

   /* Tag_a = Tag_a ^ F_K(Left ^ \delta_{l, 0 or 1}, right) */
   xor_block(xor_res, delta, left);
   key_func(key_func_res, xor_res, key, right);
   xor_block(taga, taga, key_func_res);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rcx, %r13
movq %rdx, %rbp
movq %rsi, %r12
movq %rdi, %rbx
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x80(%rsp)
movb $-0x60, %cl
movl $0x20, %eax
movb %cl, 0x7f(%rsp,%rax)
movl $0x0, %ecx
decq %rax
jne 0x14d7
vmovups (%r12), %ymm0
leaq 0x100(%rsp), %rdx
vmovups %ymm0, (%rdx)
vmovups 0x80(%rsp), %ymm0
vmovups %ymm0, 0x20(%rdx)
leaq 0x1af4(%rip), %rsi      # 0x3000
leaq 0x140(%rsp), %r14
movq %r14, %rdi
vzeroupper
callq 0x1080
leaq 0xe0(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x1070
movq %r15, %rdi
movq %r15, %rsi
callq 0x1070
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, (%rbx)
testq %r13, %r13
je 0x1782
movq %rbp, 0x10(%rsp)
movq %r13, 0x18(%rsp)
leaq 0x3f(%r13), %rax
movq %rax, %r13
shrq $0x6, %r13
vmovups %ymm0, 0x20(%rsp)
cmpq $0x80, %rax
jb 0x1627
movq 0x10(%rsp), %rax
leaq 0x20(%rax), %r15
movl $0x1, %ebp
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %r14, %rsi
leaq 0xe0(%rsp), %rdx
movl %ebp, %ecx
vzeroupper
callq 0x1050
vmovups -0x20(%r15), %ymm0
vmovups %ymm0, 0x60(%rsp)
vmovups (%r15), %ymm1
vmovups %ymm1, 0x40(%rsp)
vxorps 0x20(%rsp), %ymm0, %ymm0
vmovups %ymm0, 0xc0(%rsp)
vmovups (%r12), %ymm0
vmovups %ymm0, 0x100(%rsp)
vmovups (%r15), %ymm0
leaq 0x120(%rsp), %rax
vmovups %ymm0, (%rax)
leaq 0xa0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
leaq 0x100(%rsp), %rdx
vzeroupper
callq 0x1080
vmovups 0xa0(%rsp), %ymm0
vxorps (%rbx), %ymm0, %ymm0
vmovups %ymm0, (%rbx)
incq %rbp
addq $0x40, %r15
cmpq %rbp, %r13
jne 0x1588
movq %r12, 0x8(%rsp)
leaq -0x1(%r13), %rbp
movq 0x18(%rsp), %r15
andl $0x3f, %r15d
je 0x16e3
xorl %edx, %edx
movl %r15d, %r12d
subl $0x20, %r12d
movl $0x20, %r14d
cmovbl %r15d, %r14d
cmovbl %edx, %r12d
movl $0x20, %eax
subq %r14, %rax
vmovups 0x140(%rsp), %ymm0
vxorps 0x20(%rsp), %ymm0, %ymm0
vmovups %ymm0, 0x20(%rsp)
vxorps %xmm0, %xmm0, %xmm0
leaq 0x40(%rsp), %rcx
vmovups %ymm0, (%rcx)
cmpl $0x20, %r15d
cmovbq %rax, %rdx
shlq $0x6, %rbp
movq 0x10(%rsp), %rax
leaq (%rax,%rbp), %r13
leaq (%rsp,%r14), %rdi
addq $0x60, %rdi
addq %rax, %rbp
addq $0x20, %rbp
xorl %esi, %esi
vzeroupper
callq 0x1040
leaq 0x60(%rsp), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x1100
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x1100
cmpl $0x20, %r15d
jae 0x1720
movq 0x18(%rsp), %rax
andl $0x3f, %eax
movb $-0x80, 0x60(%rsp,%rax)
jmp 0x172a
leaq 0x20(%rsp), %rdi
leaq 0xe0(%rsp), %rdx
movq %rdi, %rsi
movl %r13d, %ecx
vzeroupper
callq 0x1050
shlq $0x6, %rbp
movq 0x10(%rsp), %rax
vmovups (%rax,%rbp), %ymm0
vmovups %ymm0, 0x60(%rsp)
vmovups 0x20(%rax,%rbp), %ymm0
vmovups %ymm0, 0x40(%rsp)
jmp 0x172a
addl $-0x20, %r15d
movb $-0x80, 0x40(%rsp,%r15)
movq 0x8(%rsp), %rax
vmovups 0x60(%rsp), %ymm0
vxorps 0x20(%rsp), %ymm0, %ymm0
leaq 0xc0(%rsp), %rsi
vmovups %ymm0, (%rsi)
vmovups (%rax), %ymm0
leaq 0x100(%rsp), %rdx
vmovups %ymm0, (%rdx)
vmovups 0x40(%rsp), %ymm0
vmovups %ymm0, 0x20(%rdx)
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
vzeroupper
callq 0x1080
vmovups (%r14), %ymm0
vxorps (%rbx), %ymm0, %ymm0
vmovups %ymm0, (%rbx)
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq