// File: /workspace/github_c_makefile_star2_O2/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 $0x138, %rsp            # imm = 0x138
movq %rcx, %r13
movq %rdx, %rbp
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x110(%rsp), %r14
pushq $0x14
popq %rsi
movq %r14, %rdi
callq 0x1140
leaq 0xb1a(%rip), %rsi       # 0x2000
leaq 0xf0(%rsp), %r15
movq %r15, %rdi
movq %r12, 0x10(%rsp)
movq %r12, %rdx
movq %r14, %rcx
callq 0x10c0
leaq 0x90(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x1070
movq %r14, %rdi
movq %r14, %rsi
callq 0x1070
vxorps %xmm0, %xmm0, %xmm0
movq %rbx, 0x18(%rsp)
vmovups %ymm0, (%rbx)
testq %r13, %r13
je 0x1741
movq %r13, 0x8(%rsp)
leaq 0x3f(%r13), %rax
shrq $0x6, %rax
movq %rbp, %rdx
leaq 0x70(%rsp), %rbp
vmovups %ymm0, (%rbp)
movq %rax, 0x20(%rsp)
leaq -0x1(%rax), %rcx
movq %rcx, %rax
sarq $0x3f, %rax
movq %rcx, (%rsp)
andnq %rcx, %rax, %r14
movq %rdx, 0x28(%rsp)
leaq 0x20(%rdx), %rbx
xorl %r12d, %r12d
leaq 0xd0(%rsp), %r13
leaq 0xb0(%rsp), %r15
cmpq %r12, %r14
je 0x15f8
incq %r12
movq %rbp, %rdi
movq %rbp, %rsi
leaq 0x90(%rsp), %rdx
movl %r12d, %ecx
vzeroupper
callq 0x1050
vmovups -0x20(%rbx), %ymm0
vmovups %ymm0, 0x50(%rsp)
vmovups (%rbx), %ymm0
vmovups %ymm0, 0x30(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
leaq 0x50(%rsp), %rdx
vzeroupper
callq 0x1130
movq %r15, %rdi
movq %r13, %rsi
movq 0x10(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0x10c0
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
movq %r15, %rdx
callq 0x1130
addq $0x40, %rbx
jmp 0x1584
movq (%rsp), %r13
shlq $0x6, %r13
movq 0x28(%rsp), %rbx
leaq (%rbx,%r13), %r15
movq 0x8(%rsp), %r14
andl $0x3f, %r14d
je 0x16b6
xorl %r12d, %r12d
movl %r14d, %ebp
subl $0x20, %ebp
movq %r15, (%rsp)
pushq $0x20
popq %r15
cmovbl %r14d, %r15d
cmovbl %r12d, %ebp
pushq $0x20
popq %rax
subq %r15, %rax
cmpl $0x20, %r14d
cmovbq %rax, %r12
leaq 0x70(%rsp), %rdi
leaq 0xf0(%rsp), %rdx
movq %rdi, %rsi
vzeroupper
callq 0x1130
vxorps %xmm0, %xmm0, %xmm0
leaq 0x30(%rsp), %rax
vmovups %ymm0, (%rax)
leaq (%rsp,%r15), %rdi
addq $0x50, %rdi
addq %rbx, %r13
addq $0x20, %r13
xorl %esi, %esi
movq %r12, %rdx
vzeroupper
callq 0x1040
leaq 0x50(%rsp), %rdi
movq (%rsp), %rsi
movq %r15, %rdx
callq 0x1150
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x1150
cmpl $0x1f, %r14d
ja 0x16ed
movq 0x8(%rsp), %rax
andl $0x3f, %eax
movb $-0x80, 0x50(%rsp,%rax)
jmp 0x16f7
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rdx
movq %rdi, %rsi
movq 0x20(%rsp), %rcx
vzeroupper
callq 0x1050
vmovups (%r15), %ymm0
vmovups %ymm0, 0x50(%rsp)
vmovups 0x20(%rbx,%r13), %ymm0
vmovups %ymm0, 0x30(%rsp)
jmp 0x16f7
addl $-0x20, %r14d
movb $-0x80, 0x30(%rsp,%r14)
leaq 0xd0(%rsp), %r14
leaq 0x70(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movq %r14, %rdi
vzeroupper
callq 0x1130
leaq 0xb0(%rsp), %r15
leaq 0x30(%rsp), %rcx
movq %r15, %rdi
movq %r14, %rsi
movq 0x10(%rsp), %rdx
callq 0x10c0
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
movq %r15, %rdx
callq 0x1130
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq