Permalink
Browse files

lyra2: reduce a bit memory operations

  • Loading branch information...
1 parent c63508d commit df62b9d9709e6c62e1e5460deb4c4c2e7063d5d3 @tpruvot committed Aug 6, 2015
Showing with 7 additions and 16 deletions.
  1. +7 −16 lyra2/Lyra2.c
View
@@ -94,7 +94,6 @@ int LYRA2(void *K, int64_t kLen, const void *pwd, int32_t pwdlen, const void *sa
int64_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);
@@ -104,6 +103,8 @@ int LYRA2(void *K, int64_t kLen, const void *pwd, int32_t pwdlen, const void *sa
memcpy(ptrByte, salt, saltlen);
ptrByte += saltlen;
+ memset(ptrByte, 0, nBlocksInput * BLOCK_LEN_BLAKE2_SAFE_BYTES - (saltlen + pwdlen));
+
//Concatenates the basil: every integer passed as parameter, in the order they are provided by the interface
memcpy(ptrByte, &kLen, sizeof(int64_t));
ptrByte += sizeof(uint64_t);
@@ -132,10 +133,7 @@ int LYRA2(void *K, int64_t kLen, const void *pwd, int32_t pwdlen, const void *sa
//======================= 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(sizeof(uint64_t) * 16);
- if (state == NULL) {
- return -1;
- }
+ uint64_t state[16];
initState(state);
//==========================================================================/
@@ -182,8 +180,8 @@ int LYRA2(void *K, int64_t kLen, const void *pwd, int32_t pwdlen, const void *sa
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)
+ rowa = state[0] & (unsigned int)(nRows-1); //(USE THIS IF nRows IS A POWER OF 2)
+ //rowa = 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]
@@ -194,30 +192,23 @@ int LYRA2(void *K, int64_t kLen, const void *pwd, int32_t pwdlen, const void *sa
//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)
+ row = (row + step) & (unsigned int)(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, (unsigned int) kLen);
- //==========================================================================/
//========================= Freeing the memory =============================//
free(memMatrix);
free(wholeMatrix);
- //Wiping out the sponge's internal state before freeing it
- memset(state, 0, sizeof(uint64_t) * 16);
- free(state);
- //==========================================================================/
-
return 0;
}

0 comments on commit df62b9d

Please sign in to comment.