// File: /workspace/github_c_makefile_star2_O0/rb-de0[P]hashverifier-for-monacoin/Lyra2RE/Lyra2.c
// Function: LYRA2
int LYRA2(void *K, uint64_t kLen, const void *pwd, uint64_t pwdlen, const void *salt, uint64_t saltlen, uint64_t timeCost, uint64_t nRows, uint64_t nCols) {

    //============================= Basic variables ============================//
    int64_t row = 2; //index of row to be processed
    int64_t prev = 1; //index of prev (last row ever computed/modified)
    int64_t rowa = 0; //index of row* (a previous row, deterministically picked during Setup and randomly picked while Wandering)
    int64_t tau; //Time Loop iterator
    int64_t step = 1; //Visitation step (used during Setup and Wandering phases)
    int64_t window = 2; //Visitation window (used to define which rows can be revisited during Setup)
    int64_t gap = 1; //Modifier to the step, assuming the values 1 or -1
    int64_t i; //auxiliary iteration counter
    //==========================================================================/

    //========== Initializing the Memory Matrix and pointers to it =============//
    //Tries to allocate enough space for the whole memory matrix


    const int64_t ROW_LEN_INT64 = BLOCK_LEN_INT64 * nCols;
    const int64_t ROW_LEN_BYTES = ROW_LEN_INT64 * 8;

    i = (int64_t) ((int64_t) nRows * (int64_t) ROW_LEN_BYTES);
    uint64_t *wholeMatrix = malloc(i);
    if (wholeMatrix == NULL) {
      return -1;
    }
	memset(wholeMatrix, 0, i);

    //Allocates pointers to each row of the matrix
    uint64_t **memMatrix = malloc(nRows * sizeof (uint64_t*));
    if (memMatrix == NULL) {
      return -1;
    }
    //Places the pointers in the correct positions
    uint64_t *ptrWord = wholeMatrix;
    for (i = 0; i < nRows; i++) {
      memMatrix[i] = ptrWord;
      ptrWord += ROW_LEN_INT64;
    }
    //==========================================================================/

    //============= Getting the password + salt + basil padded with 10*1 ===============//
    //OBS.:The memory matrix will temporarily hold the password: not for saving memory,
    //but this ensures that the password copied locally will be overwritten as soon as possible

    //First, we clean enough blocks for the password, salt, basil and padding
    uint64_t nBlocksInput = ((saltlen + pwdlen + 6 * sizeof (uint64_t)) / BLOCK_LEN_BLAKE2_SAFE_BYTES) + 1;
    byte *ptrByte = (byte*) wholeMatrix;
    memset(ptrByte, 0, nBlocksInput * BLOCK_LEN_BLAKE2_SAFE_BYTES);

    //Prepends the password
    memcpy(ptrByte, pwd, pwdlen);
    ptrByte += pwdlen;

    //Concatenates the salt
    memcpy(ptrByte, salt, saltlen);
    ptrByte += saltlen;

    //Concatenates the basil: every integer passed as parameter, in the order they are provided by the interface
    memcpy(ptrByte, &kLen, sizeof (uint64_t));
    ptrByte += sizeof (uint64_t);
    memcpy(ptrByte, &pwdlen, sizeof (uint64_t));
    ptrByte += sizeof (uint64_t);
    memcpy(ptrByte, &saltlen, sizeof (uint64_t));
    ptrByte += sizeof (uint64_t);
    memcpy(ptrByte, &timeCost, sizeof (uint64_t));
    ptrByte += sizeof (uint64_t);
    memcpy(ptrByte, &nRows, sizeof (uint64_t));
    ptrByte += sizeof (uint64_t);
    memcpy(ptrByte, &nCols, sizeof (uint64_t));
    ptrByte += sizeof (uint64_t);

    //Now comes the padding
    *ptrByte = 0x80; //first byte of padding: right after the password
    ptrByte = (byte*) wholeMatrix; //resets the pointer to the start of the memory matrix
    ptrByte += nBlocksInput * BLOCK_LEN_BLAKE2_SAFE_BYTES - 1; //sets the pointer to the correct position: end of incomplete block
    *ptrByte ^= 0x01; //last byte of padding: at the end of the last incomplete block
    //==========================================================================/

    //======================= Initializing the Sponge State ====================//
    //Sponge state: 16 uint64_t, BLOCK_LEN_INT64 words of them for the bitrate (b) and the remainder for the capacity (c)
    uint64_t *state = malloc(16 * sizeof (uint64_t));
    if (state == NULL) {
      return -1;
    }
    initState(state);
    //==========================================================================/

    //================================ Setup Phase =============================//
    //Absorbing salt, password and basil: this is the only place in which the block length is hard-coded to 512 bits
    ptrWord = wholeMatrix;
    for (i = 0; i < nBlocksInput; i++) {
      absorbBlockBlake2Safe(state, ptrWord); //absorbs each block of pad(pwd || salt || basil)
      ptrWord += BLOCK_LEN_BLAKE2_SAFE_INT64; //goes to next block of pad(pwd || salt || basil)
    }

    //Initializes M[0] and M[1]
    reducedSqueezeRow0(state, memMatrix[0], nCols); //The locally copied password is most likely overwritten here
    reducedDuplexRow1(state, memMatrix[0], memMatrix[1], nCols);

    do {
      //M[row] = rand; //M[row*] = M[row*] XOR rotW(rand)
      reducedDuplexRowSetup(state, memMatrix[prev], memMatrix[rowa], memMatrix[row], nCols);


      //updates the value of row* (deterministically picked during Setup))
      rowa = (rowa + step) & (window - 1);
      //update prev: it now points to the last row ever computed
      prev = row;
      //updates row: goes to the next row to be computed
      row++;

      //Checks if all rows in the window where visited.
      if (rowa == 0) {
      step = window + gap; //changes the step: approximately doubles its value
      window *= 2; //doubles the size of the re-visitation window
      gap = -gap; //inverts the modifier to the step
    }

    } while (row < nRows);
    //==========================================================================/

    //============================ Wandering Phase =============================//
    row = 0; //Resets the visitation to the first row of the memory matrix
    for (tau = 1; tau <= timeCost; tau++) {
    	//Step is approximately half the number of all rows of the memory matrix for an odd tau; otherwise, it is -1
    	step = (tau % 2 == 0) ? -1 : nRows / 2 - 1;
    	do {
  	    //Selects a pseudorandom index row*
  	    //------------------------------------------------------------------------------------------
  	    //rowa = ((unsigned int)state[0]) & (nRows-1);	//(USE THIS IF nRows IS A POWER OF 2)
  	    rowa = ((uint64_t) (state[0])) % nRows; //(USE THIS FOR THE "GENERIC" CASE)
  	    //------------------------------------------------------------------------------------------

  	    //Performs a reduced-round duplexing operation over M[row*] XOR M[prev], updating both M[row*] and M[row]
  	    reducedDuplexRow(state, memMatrix[prev], memMatrix[rowa], memMatrix[row], nCols);

  	    //update prev: it now points to the last row ever computed
  	    prev = row;

  	    //updates row: goes to the next row to be computed
  	    //------------------------------------------------------------------------------------------
  	    //row = (row + step) & (nRows-1);	//(USE THIS IF nRows IS A POWER OF 2)
  	    row = (row + step) % nRows; //(USE THIS FOR THE "GENERIC" CASE)
  	    //------------------------------------------------------------------------------------------

      } while (row != 0);
    }
    //==========================================================================/

    //============================ Wrap-up Phase ===============================//
    //Absorbs the last block of the memory matrix
    absorbBlock(state, memMatrix[rowa]);

    //Squeezes the key
    squeeze(state, K, kLen);
    //==========================================================================/

    //========================= Freeing the memory =============================//
    free(memMatrix);
    free(wholeMatrix);

    //Wiping out the sponge's internal state before freeing it
    memset(state, 0, 16 * sizeof (uint64_t));
    free(state);
    //==========================================================================/

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq $0x2, -0x40(%rbp)
movq $0x1, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x1, -0x60(%rbp)
movq $0x2, -0x68(%rbp)
movq $0x1, -0x70(%rbp)
imulq $0xc, 0x20(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
shlq $0x3, %rax
movq %rax, -0x88(%rbp)
movq 0x18(%rbp), %rax
imulq -0x88(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
callq 0x1090
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x90(%rbp)
jne 0x1b6d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2054
movq -0x90(%rbp), %rdi
movq -0x78(%rbp), %rdx
xorl %esi, %esi
callq 0x1060
movq 0x18(%rbp), %rdi
shlq $0x3, %rdi
callq 0x1090
movq %rax, -0x98(%rbp)
cmpq $0x0, -0x98(%rbp)
jne 0x1ba9
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2054
movq -0x90(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq 0x18(%rbp), %rax
jae 0x1c03
movq -0xa0(%rbp), %rdx
movq -0x98(%rbp), %rax
movq -0x78(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x80(%rbp), %rax
shlq $0x3, %rax
addq -0xa0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x1bbf
movq -0x38(%rbp), %rax
addq -0x28(%rbp), %rax
addq $0x30, %rax
shrq $0x6, %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
movq -0xa8(%rbp), %rdx
shlq $0x6, %rdx
xorl %esi, %esi
callq 0x1060
movq -0xb0(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x1080
movq -0x28(%rbp), %rax
addq -0xb0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x1080
movq -0x38(%rbp), %rax
addq -0xb0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq -0x18(%rbp), %rcx
movq %rcx, (%rax)
movq -0xb0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq -0x28(%rbp), %rcx
movq %rcx, (%rax)
movq -0xb0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq -0x38(%rbp), %rcx
movq %rcx, (%rax)
movq -0xb0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq 0x10(%rbp), %rcx
movq %rcx, (%rax)
movq -0xb0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq 0x18(%rbp), %rcx
movq %rcx, (%rax)
movq -0xb0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq 0x20(%rbp), %rcx
movq %rcx, (%rax)
movq -0xb0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movb $-0x80, (%rax)
movq -0x90(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rax
shlq $0x6, %rax
subq $0x1, %rax
addq -0xb0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movzbl (%rax), %ecx
xorl $0x1, %ecx
movb %cl, (%rax)
movl $0x80, %edi
callq 0x1090
movq %rax, -0xb8(%rbp)
cmpq $0x0, -0xb8(%rbp)
jne 0x1dbc
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2054
movq -0xb8(%rbp), %rdi
callq 0x2610
movq -0x90(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jae 0x1e1e
movq -0xb8(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0x8410
movq -0xa0(%rbp), %rax
addq $0x40, %rax
movq %rax, -0xa0(%rbp)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x1dde
movq -0xb8(%rbp), %rdi
movq -0x98(%rbp), %rax
movq (%rax), %rsi
movq 0x20(%rbp), %rdx
callq 0x84d0
movq -0xb8(%rbp), %rdi
movq -0x98(%rbp), %rax
movq (%rax), %rsi
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rdx
movq 0x20(%rbp), %rcx
callq 0x8dc0
movq -0xb8(%rbp), %rdi
movq -0x98(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x98(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x98(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq 0x20(%rbp), %r8
callq 0x9050
movq -0x50(%rbp), %rax
addq -0x60(%rbp), %rax
movq -0x68(%rbp), %rcx
subq $0x1, %rcx
andq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x1eed
movq -0x68(%rbp), %rax
addq -0x70(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x68(%rbp), %rax
shlq %rax
movq %rax, -0x68(%rbp)
xorl %eax, %eax
subq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x1eef
movq -0x40(%rbp), %rax
cmpq 0x18(%rbp), %rax
jb 0x1e5d
movq $0x0, -0x40(%rbp)
movq $0x1, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq 0x10(%rbp), %rax
ja 0x1fe5
movq -0x58(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
cmpq $0x0, %rdx
jne 0x1f3f
movq $-0x1, %rax
movq %rax, -0xc0(%rbp)
jmp 0x1f51
movq 0x18(%rbp), %rax
shrq %rax
subq $0x1, %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0xb8(%rbp), %rax
movq (%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x18(%rbp)
movq %rdx, -0x50(%rbp)
movq -0xb8(%rbp), %rdi
movq -0x98(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x98(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x98(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq 0x20(%rbp), %r8
callq 0x9450
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
addq -0x60(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x18(%rbp)
movq %rdx, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x1f5c
jmp 0x1fd4
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x1f0d
movq -0xb8(%rbp), %rdi
movq -0x98(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x8300
movq -0xb8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rax
movl %eax, %edx
callq 0x26b0
movq -0x98(%rbp), %rdi
callq 0x1030
movq -0x90(%rbp), %rdi
callq 0x1030
movq -0xb8(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x1060
movq -0xb8(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq