Skip to content

Commit

Permalink
Handle lyra2v3 algo, for VTC fork
Browse files Browse the repository at this point in the history
mostly imported from opensourced vertcoin-miner with a few fixes
  • Loading branch information
tpruvot committed Jan 30, 2019
1 parent c59bc24 commit 02f1794
Show file tree
Hide file tree
Showing 15 changed files with 1,221 additions and 5 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ ccminer_SOURCES = elist.h miner.h compat.h \
lyra2/Lyra2.c lyra2/Sponge.c \
lyra2/lyra2RE.cu lyra2/cuda_lyra2.cu \
lyra2/lyra2REv2.cu lyra2/cuda_lyra2v2.cu \
lyra2/lyra2REv3.cu lyra2/cuda_lyra2v3.cu \
lyra2/Lyra2Z.c lyra2/lyra2Z.cu lyra2/cuda_lyra2Z.cu \
lyra2/allium.cu \
Algo256/cuda_bmw256.cu Algo256/cuda_cubehash256.cu \
Expand Down
7 changes: 4 additions & 3 deletions README.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

ccminer 2.3 "phi2 and cryptonight variants"
ccminer 2.3.1 "lyra2v3, exosis and sha256q"
---------------------------------------------------------------

***************************************************************
Expand Down Expand Up @@ -100,7 +100,8 @@ its command line interface and options.
lbry use to mine LBRY Credits
luffa use to mine Joincoin
lyra2 use to mine CryptoCoin
lyra2v2 use to mine Vertcoin
lyra2v2 use to mine Monacoin
lyra2v3 use to mine Vertcoin
lyra2z use to mine Zerocoin (XZC)
monero use to mine Monero (XMR)
myr-gr use to mine Myriad-Groest
Expand All @@ -117,7 +118,7 @@ its command line interface and options.
scrypt-jane use to mine Chacha coins like Cache and Ultracoin
s3 use to mine 1coin (ONE)
sha256t use to mine OneCoin (OC)
sha256q use to mine Pyrite
sha256q use to mine Pyrite
sia use to mine SIA
sib use to mine Sibcoin
skein use to mine Skeincoin
Expand Down
4 changes: 4 additions & 0 deletions algos.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ enum sha_algos {
ALGO_LUFFA,
ALGO_LYRA2,
ALGO_LYRA2v2,
ALGO_LYRA2v3,
ALGO_LYRA2Z,
ALGO_MJOLLNIR, /* Hefty hash */
ALGO_MYR_GR,
Expand Down Expand Up @@ -115,6 +116,7 @@ static const char *algo_names[] = {
"luffa",
"lyra2",
"lyra2v2",
"lyra2v3",
"lyra2z",
"mjollnir",
"myr-gr",
Expand Down Expand Up @@ -199,6 +201,8 @@ static inline int algo_to_int(char* arg)
i = ALGO_LYRA2;
else if (!strcasecmp("lyra2rev2", arg))
i = ALGO_LYRA2v2;
else if (!strcasecmp("lyra2rev3", arg))
i = ALGO_LYRA2v3;
else if (!strcasecmp("phi1612", arg))
i = ALGO_PHI;
else if (!strcasecmp("bitcoin", arg))
Expand Down
1 change: 1 addition & 0 deletions bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void algo_free_all(int thr_id)
free_luffa(thr_id);
free_lyra2(thr_id);
free_lyra2v2(thr_id);
free_lyra2v3(thr_id);
free_lyra2Z(thr_id);
free_myriad(thr_id);
free_neoscrypt(thr_id);
Expand Down
8 changes: 7 additions & 1 deletion ccminer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ Options:\n\
lbry LBRY Credits (Sha/Ripemd)\n\
luffa Joincoin\n\
lyra2 CryptoCoin\n\
lyra2v2 VertCoin\n\
lyra2v2 MonaCoin\n\
lyra2v3 Vertcoin\n\
lyra2z ZeroCoin (3rd impl)\n\
myr-gr Myriad-Groestl\n\
monero XMR cryptonight (v7)\n\
Expand Down Expand Up @@ -1742,6 +1743,7 @@ static bool stratum_gen_work(struct stratum_ctx *sctx, struct work *work)
case ALGO_KECCAKC:
case ALGO_LBRY:
case ALGO_LYRA2v2:
case ALGO_LYRA2v3:
case ALGO_LYRA2Z:
case ALGO_PHI2:
case ALGO_TIMETRAVEL:
Expand Down Expand Up @@ -2283,6 +2285,7 @@ static void *miner_thread(void *userdata)
case ALGO_JHA:
case ALGO_HSR:
case ALGO_LYRA2v2:
case ALGO_LYRA2v3:
case ALGO_PHI:
case ALGO_PHI2:
case ALGO_POLYTIMOS:
Expand Down Expand Up @@ -2474,6 +2477,9 @@ static void *miner_thread(void *userdata)
case ALGO_LYRA2v2:
rc = scanhash_lyra2v2(thr_id, &work, max_nonce, &hashes_done);
break;
case ALGO_LYRA2v3:
rc = scanhash_lyra2v3(thr_id, &work, max_nonce, &hashes_done);
break;
case ALGO_LYRA2Z:
rc = scanhash_lyra2Z(thr_id, &work, max_nonce, &hashes_done);
break;
Expand Down
3 changes: 3 additions & 0 deletions ccminer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,9 @@
<ClInclude Include="lyra2\cuda_lyra2_sm2.cuh" />
<ClInclude Include="lyra2\cuda_lyra2_sm5.cuh" />
<ClInclude Include="lyra2\cuda_lyra2v2_sm3.cuh" />
<CudaCompile Include="lyra2\lyra2REv3.cu" />
<CudaCompile Include="lyra2\cuda_lyra2v3.cu" />
<ClInclude Include="lyra2\cuda_lyra2v3_sm3.cuh" />
<CudaCompile Include="lyra2\lyra2Z.cu" />
<CudaCompile Include="lyra2\cuda_lyra2Z.cu" />
<ClInclude Include="lyra2\cuda_lyra2Z_sm5.cuh" />
Expand Down
9 changes: 9 additions & 0 deletions ccminer.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,15 @@
<CudaCompile Include="lyra2\lyra2REv2.cu">
<Filter>Source Files\CUDA\lyra2</Filter>
</CudaCompile>
<CudaCompile Include="lyra2\cuda_lyra2v3.cu">
<Filter>Source Files\CUDA\lyra2</Filter>
</CudaCompile>
<ClInclude Include="lyra2\cuda_lyra2v3_sm3.cuh">
<Filter>Source Files\CUDA\lyra2</Filter>
</CudaCompile>
<CudaCompile Include="lyra2\lyra2REv3.cu">
<Filter>Source Files\CUDA\lyra2</Filter>
</CudaCompile>
<CudaCompile Include="lyra2\cuda_lyra2Z.cu">
<Filter>Source Files\CUDA\lyra2</Filter>
</CudaCompile>
Expand Down
2 changes: 1 addition & 1 deletion compat/ccminer-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
#define PACKAGE_URL "http://github.com/tpruvot/ccminer"

/* Define to the version of this package. */
#define PACKAGE_VERSION "2.3"
#define PACKAGE_VERSION "2.3.1"

/* If using the C implementation of alloca, define if you know the
direction of stack growth for your system; otherwise it will be
Expand Down
173 changes: 173 additions & 0 deletions lyra2/Lyra2.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,176 @@ int LYRA2(void *K, int64_t kLen, const void *pwd, int32_t pwdlen, const void *sa

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
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ typedef unsigned char byte;
#endif

int LYRA2(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);
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_ */
Loading

0 comments on commit 02f1794

Please sign in to comment.