Skip to content

Commit

Permalink
lyra2rev3 algo
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausT committed Feb 3, 2019
1 parent a19ece6 commit 02e5217
Show file tree
Hide file tree
Showing 9 changed files with 1,262 additions and 4 deletions.
11 changes: 10 additions & 1 deletion ccminer.cpp
Expand Up @@ -113,6 +113,7 @@ const char *algo_names[] =
"jackpot",
"luffa",
"lyra2v2",
"lyra2v3",
"myr-gr",
"nist5",
"penta",
Expand Down Expand Up @@ -250,7 +251,8 @@ Options:\n\
jackpot Jackpot (JHA)\n\
keccak Keccak-256 (Maxcoin)\n\
luffa Doomcoin\n\
lyra2v2 VertCoin\n\
lyra2v2 Monacoin\n\
lyra2v3 Vertcoin\n\
myr-gr Myriad-Groestl\n\
neoscrypt neoscrypt (FeatherCoin)\n\
nist5 NIST5 (TalkCoin)\n\
Expand Down Expand Up @@ -1369,6 +1371,7 @@ static bool stratum_gen_work(struct stratum_ctx *sctx, struct work *work)
case ALGO_GROESTL:
case ALGO_KECCAK:
case ALGO_LYRA2v2:
case ALGO_LYRA2v3:
diff_to_target(work->target, sctx->job.diff / (256.0 * opt_difficulty));
break;
default:
Expand Down Expand Up @@ -1625,6 +1628,7 @@ static void *miner_thread(void *userdata)
minmax = 1000000 * max64time;
break;
case ALGO_LYRA2v2:
case ALGO_LYRA2v3:
minmax = 1900000 * max64time;
break;
case ALGO_NEO:
Expand Down Expand Up @@ -1763,6 +1767,11 @@ static void *miner_thread(void *userdata)
max_nonce, &hashes_done);
break;

case ALGO_LYRA2v3:
rc = scanhash_lyra2v3(thr_id, work.data, work.target,
max_nonce, &hashes_done);
break;

case ALGO_NIST5:
rc = scanhash_nist5(thr_id, work.data, work.target,
max_nonce, &hashes_done);
Expand Down
11 changes: 8 additions & 3 deletions ccminer.vcxproj
Expand Up @@ -55,7 +55,7 @@
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
<Import Project="$(VCTargetsPath)\BuildCustomizations\CUDA 9.2.props" />
<Import Project="$(VCTargetsPath)\BuildCustomizations\CUDA 10.0.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
Expand Down Expand Up @@ -246,7 +246,7 @@
<MaxRegCount>80</MaxRegCount>
<PtxAsOptionV>false</PtxAsOptionV>
<Keep>true</Keep>
<CodeGeneration>compute_60,sm_60;compute_61,sm_61;compute_52,sm_52;compute_50,sm_50;compute_35,sm_35;compute_30,sm_30;compute_37,sm_37;compute_70,sm_70</CodeGeneration>
<CodeGeneration>compute_60,sm_60;compute_61,sm_61;compute_52,sm_52;compute_50,sm_50;compute_35,sm_35;compute_30,sm_30;compute_37,sm_37;compute_70,sm_70;compute_75,sm_75</CodeGeneration>
<Include>
</Include>
<TargetMachinePlatform>64</TargetMachinePlatform>
Expand Down Expand Up @@ -281,6 +281,8 @@
<CudaCompile Include="cuda_bitcoin.cu">
<FileType>CppCode</FileType>
</CudaCompile>
<CudaCompile Include="lyra2\cuda_lyra2v3.cu" />
<CudaCompile Include="lyra2\lyra2REv3.cpp" />
<CudaCompile Include="neoscrypt\cuda_neoscrypt_tpruvot.cu">
<CodeGeneration Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">compute_52,sm_52;compute_50,sm_50;compute_35,sm_35;compute_37,sm_37</CodeGeneration>
<CodeGeneration Condition="'$(Configuration)|$(Platform)'=='Release|x64'">compute_70,sm_70;compute_52,sm_52;compute_50,sm_50;compute_35,sm_35;compute_37,sm_37</CodeGeneration>
Expand Down Expand Up @@ -354,6 +356,9 @@
<ClInclude Include="cuda_vector.h" />
<ClInclude Include="elist.h" />
<ClInclude Include="lyra2\cuda_lyra2v2_sm3.cuh" />
<ClInclude Include="lyra2\cuda_lyra2v3_sm3.cuh">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
</ClInclude>
<ClInclude Include="lyra2\cuda_lyra2_vectors.h" />
<ClInclude Include="miner.h" />
<ClInclude Include="neoscrypt\cuda_vector_tpruvot.cuh" />
Expand Down Expand Up @@ -529,7 +534,7 @@
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(VCTargetsPath)\BuildCustomizations\CUDA 9.2.targets" />
<Import Project="$(VCTargetsPath)\BuildCustomizations\CUDA 10.0.targets" />
</ImportGroup>
<!-- Copy the required dlls -->
<Target Name="AfterBuild">
Expand Down
9 changes: 9 additions & 0 deletions ccminer.vcxproj.filters
Expand Up @@ -371,6 +371,9 @@
<ClInclude Include="SHA3api_ref.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="lyra2\cuda_lyra2v3_sm3.cuh">
<Filter>Header Files\CUDA\lyra2</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CudaCompile Include="cuda.cpp">
Expand Down Expand Up @@ -577,5 +580,11 @@
<CudaCompile Include="x15\whirlpoolx.cpp">
<Filter>Source Files\CUDA</Filter>
</CudaCompile>
<CudaCompile Include="lyra2\cuda_lyra2v3.cu">
<Filter>Source Files\CUDA\Lyra2</Filter>
</CudaCompile>
<CudaCompile Include="lyra2\lyra2REv3.cpp">
<Filter>Source Files\CUDA\Lyra2</Filter>
</CudaCompile>
</ItemGroup>
</Project>
181 changes: 181 additions & 0 deletions lyra2/Lyra2.c
Expand Up @@ -382,3 +382,184 @@ int LYRA2_old(void *K, uint64_t kLen, const void *pwd, uint64_t pwdlen, const vo

return 0;
}

int LYRA2_3(void *K, int64_t kLen, const void *pwd, int32_t pwdlen, const void *salt, int32_t saltlen, int64_t timeCost, const int16_t nRows, const int16_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
int64_t v64; // 64bit var for memcpy
uint64_t instance = 0;
//==========================================================================/

//========== 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;
// for Lyra2REv2, nCols = 4, v1 was using 8
const int64_t BLOCK_LEN = (nCols == 4) ? BLOCK_LEN_BLAKE2_SAFE_INT64 : BLOCK_LEN_BLAKE2_SAFE_BYTES;

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

//Allocates pointers to each row of the matrix
uint64_t **memMatrix = malloc(sizeof(uint64_t*) * nRows);
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
int64_t nBlocksInput = ((saltlen + pwdlen + 6 * sizeof(uint64_t)) / BLOCK_LEN_BLAKE2_SAFE_BYTES) + 1;

byte *ptrByte = (byte*)wholeMatrix;

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

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

memset(ptrByte, 0, (size_t)(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);
v64 = pwdlen;
memcpy(ptrByte, &v64, sizeof(int64_t));
ptrByte += sizeof(uint64_t);
v64 = saltlen;
memcpy(ptrByte, &v64, sizeof(int64_t));
ptrByte += sizeof(uint64_t);
v64 = timeCost;
memcpy(ptrByte, &v64, sizeof(int64_t));
ptrByte += sizeof(uint64_t);
v64 = nRows;
memcpy(ptrByte, &v64, sizeof(int64_t));
ptrByte += sizeof(uint64_t);
v64 = nCols;
memcpy(ptrByte, &v64, sizeof(int64_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[16];
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; //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 & 1) == 0) ? -1 : (nRows >> 1) - 1;
do
{
//Selects a pseudorandom index row* (the only change in REv3)
//------------------------------------------------------------------------------------------
instance = state[instance & 0xF];
rowa = state[instance & 0xF] & (unsigned int)(nRows - 1);

//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]
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) & (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);

return 0;
}
1 change: 1 addition & 0 deletions lyra2/Lyra2.h
Expand Up @@ -43,5 +43,6 @@ typedef unsigned char byte;

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);
int LYRA2_old(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);
int LYRA2_3(void *K, int64_t kLen, const void *pwd, int32_t pwdlen, const void *salt, int32_t saltlen, int64_t timeCost, const int16_t nRows, const int16_t nCols);

#endif /* LYRA2_H_ */

0 comments on commit 02e5217

Please sign in to comment.