From 3727236edc95e7e20eef9dec384b4f94c305a7e1 Mon Sep 17 00:00:00 2001 From: PONS Jean-Luc Date: Wed, 17 Jun 2020 11:37:59 +0200 Subject: [PATCH] Code refurbishment --- Check.cpp | 4 +- Constants.h | 2 +- HashTable.cpp | 8 ++-- HashTable.h | 2 +- Kangaroo.cpp | 29 ++++++-------- Kangaroo.h | 13 ++++++- Network.cpp | 102 ++++++++++++++++++++++++++++++++++++++++---------- PartMerge.cpp | 16 ++++++-- Timer.cpp | 10 +++++ Timer.h | 1 + 10 files changed, 137 insertions(+), 50 deletions(-) diff --git a/Check.cpp b/Check.cpp index 2b677a9..f80a8fe 100644 --- a/Check.cpp +++ b/Check.cpp @@ -47,7 +47,7 @@ uint32_t Kangaroo::CheckHash(uint32_t h,uint32_t nbItem,HashTable* hT,FILE* f) { e = hT->E[h].items[i]; Int dist; uint32_t kType; - HashTable::CalcCollision(e->d,&dist,&kType); + HashTable::CalcDistAndType(e->d,&dist,&kType); dists.push_back(dist); types.push_back(kType); } @@ -61,7 +61,7 @@ uint32_t Kangaroo::CheckHash(uint32_t h,uint32_t nbItem,HashTable* hT,FILE* f) { e = items + i; Int dist; uint32_t kType; - HashTable::CalcCollision(e->d,&dist,&kType); + HashTable::CalcDistAndType(e->d,&dist,&kType); dists.push_back(dist); types.push_back(kType); } diff --git a/Constants.h b/Constants.h index 2192de0..0a754ee 100644 --- a/Constants.h +++ b/Constants.h @@ -19,7 +19,7 @@ #define CONSTANTSH // Release number -#define RELEASE "1.11gamma" +#define RELEASE "1.11theta" // Use symmetry //#define USE_SYMMETRY diff --git a/HashTable.cpp b/HashTable.cpp index 4692a56..70b5498 100644 --- a/HashTable.cpp +++ b/HashTable.cpp @@ -161,8 +161,8 @@ int HashTable::MergeH(uint32_t h,FILE* f1,FILE* f2,FILE* fd,uint32_t* nbDP,uint3 *duplicate = *duplicate + 1; } else { // Collision - CalcCollision(e1.d,d1,k1); - CalcCollision(e2.d,d2,k2); + CalcDistAndType(e1.d,d1,k1); + CalcDistAndType(e2.d,d2,k2); collisionFound = true; } memcpy(output + nbd,&e1,32); @@ -246,7 +246,7 @@ int HashTable::Add(uint64_t h,int128_t *x,int128_t *d) { } -void HashTable::CalcCollision(int128_t d,Int* kDist,uint32_t* kType) { +void HashTable::CalcDistAndType(int128_t d,Int* kDist,uint32_t* kType) { *kType = (d.i64[1] & 0x4000000000000000ULL) != 0; int sign = (d.i64[1] & 0x8000000000000000ULL) != 0; @@ -293,7 +293,7 @@ int HashTable::Add(uint64_t h,ENTRY* e) { } // Collision - CalcCollision(GET(h,mi)->d , &kDist, &kType); + CalcDistAndType(GET(h,mi)->d , &kDist, &kType); return ADD_COLLISION; } else { diff --git a/HashTable.h b/HashTable.h index 6f10821..e59d898 100644 --- a/HashTable.h +++ b/HashTable.h @@ -91,7 +91,7 @@ class HashTable { static void Convert(Int *x,Int *d,uint32_t type,uint64_t *h,int128_t *X,int128_t *D); static int MergeH(uint32_t h,FILE* f1,FILE* f2,FILE* fd,uint32_t *nbDP,uint32_t* duplicate, Int* d1,uint32_t* k1,Int* d2,uint32_t* k2); - static void CalcCollision(int128_t d,Int* kDist,uint32_t* kType); + static void CalcDistAndType(int128_t d,Int* kDist,uint32_t* kType); private: diff --git a/Kangaroo.cpp b/Kangaroo.cpp index 69dc989..40aa816 100644 --- a/Kangaroo.cpp +++ b/Kangaroo.cpp @@ -62,6 +62,7 @@ Kangaroo::Kangaroo(Secp256K1 *secp,int32_t initDPSize,bool useGpu,string &workFi this->collisionInSameHerd = 0; this->keyIdx = 0; this->splitWorkfile = splitWorkfile; + this->pid = Timer::getPID(); CPU_GRP_SIZE = 1024; @@ -318,7 +319,7 @@ bool Kangaroo::AddToTable(uint64_t h,int128_t *x,int128_t *d) { Int dist; uint32_t kType; - HashTable::CalcCollision(*d,&dist,&kType); + HashTable::CalcDistAndType(*d,&dist,&kType); return CollisionCheck(&hashTable.kDist,hashTable.kType,&dist,kType); } @@ -446,7 +447,7 @@ void Kangaroo::SolveKeyCPU(TH_PARAM *ph) { double now = Timer::get_tick(); if( now-lastSent > SEND_PERIOD ) { LOCK(ghMutex); - SendToServer(dps); + SendToServer(dps,ph->threadId,0xFFFF); UNLOCK(ghMutex); lastSent = now; } @@ -575,7 +576,7 @@ void Kangaroo::SolveKeyGPU(TH_PARAM *ph) { double now = Timer::get_tick(); if(now - lastSent > SEND_PERIOD) { LOCK(ghMutex); - SendToServer(dps); + SendToServer(dps,ph->threadId,ph->gpuId); UNLOCK(ghMutex); lastSent = now; } @@ -845,20 +846,14 @@ void Kangaroo::ComputeExpected(double dp,double *op,double *ram,double *overHead // theta double theta = pow(2.0,dp); - // Average for DP = 0 - double avgDP0 = (2.0 * (2.0 - sqrt(2.0)) * gainS) * sqrt(M_PI) * sqrt(N); - - // DP Overhead (for small number of kangaroo) - double overSmallK = k * theta; + // Z0 + double Z0 = (2.0 * (2.0 - sqrt(2.0)) * gainS) * sqrt(M_PI); - // Average estimation (when k >> theta) - double overLargeK = pow(16.0 * N * theta * k,1.0 / 3.0); + // Average for DP = 0 + double avgDP0 = Z0 * sqrt(N); - double avg = avgDP0 + overSmallK; - if(overLargeK>avg) - *op = overLargeK; - else - *op = avg; + // DP Overhead + *op = Z0 * pow(N * (k * theta + sqrt(N)),1.0 / 3.0); *ram = (double)sizeof(HASH_ENTRY) * (double)HASH_SIZE + // Table (double)sizeof(ENTRY *) * (double)(HASH_SIZE * 4) + // Allocation overhead @@ -1001,7 +996,7 @@ void Kangaroo::Run(int nbThread,std::vector gpuId,std::vector gridSize // Fetch kangaroos (if any) FectchKangaroos(params); -//#define STATS +#define STATS #ifdef STATS CPU_GRP_SIZE = 1024; @@ -1057,7 +1052,7 @@ void Kangaroo::Run(int nbThread,std::vector gpuId,std::vector gridSize double SN = pow(2.0,rangePower / 2.0); double avg = (double)totalCount / (double)(keyIdx + 1); ::printf("\n[%3d] 2^%.3f Dead:%d Avg:2^%.3f DeadAvg:%.1f (%.3f %.3f sqrt(N))\n", - keyIdx, log2((double)count), collisionInSameHerd, + keyIdx, log2((double)count), (int)collisionInSameHerd, log2(avg), (double)totalDead / (double)(keyIdx + 1), avg/SN,expectedNbOp/SN); } diff --git a/Kangaroo.h b/Kangaroo.h index 6aeeae7..b1a736a 100644 --- a/Kangaroo.h +++ b/Kangaroo.h @@ -100,6 +100,16 @@ typedef struct { } DP; +typedef struct { + + uint32_t header; + uint32_t nbDP; + uint32_t threadId; + uint32_t processId; + uint32_t gpuId; + +} DPHEADER; + // DP cache typedef struct { uint32_t nbDP; @@ -156,7 +166,7 @@ class Kangaroo { void CreateJumpTable(); bool AddToTable(uint64_t h,int128_t *x,int128_t *d); bool AddToTable(Int *pos,Int *dist,uint32_t kType); - bool SendToServer(std::vector &dp); + bool SendToServer(std::vector &dp,uint32_t threadId,uint32_t gpuId); bool CheckKey(Int d1,Int d2,uint8_t type); bool CollisionCheck(Int* d1,uint32_t type1,Int* d2,uint32_t type2); void ComputeExpected(double dp,double *op,double *ram,double* overHead = NULL); @@ -279,6 +289,7 @@ class Kangaroo { std::vector localCache; std::string serverStatus; int connectedClient; + uint32_t pid; }; diff --git a/Network.cpp b/Network.cpp index 2a57c76..2d6cee8 100644 --- a/Network.cpp +++ b/Network.cpp @@ -42,7 +42,9 @@ static SOCKET serverSock = 0; #define WAIT_FOR_READ 1 #define WAIT_FOR_WRITE 2 -#define SERVER_VERSION 2 +#define SERVER_VERSION 3 + +#define SERVER_HEADER 0x67DEDDC1 // Commands #define SERVER_GETCONFIG 0 @@ -307,13 +309,22 @@ bool Kangaroo::HandleRequest(TH_PARAM *p) { case SERVER_SENDDP: { - uint32_t nbDP=0; + DPHEADER head; + + GET("DPHeader",p->clientSock,&head,sizeof(DPHEADER),ntimeout); + + if(head.header != SERVER_HEADER) { + + ::printf("\nUnexpected DP header from %s\n",p->clientInfo); + ::printf("\nClosing connection with %s\n",p->clientInfo); + close_socket(p->clientSock); + return false; - GET("nbDP",p->clientSock,&nbDP,sizeof(uint32_t),ntimeout); + } - if(nbDP == 0) { + if(head.nbDP == 0) { - ::printf("\nUnexpected number of DP [%d] from %s\n",nbDP,p->clientInfo); + ::printf("\nUnexpected number of DP [%d] from %s\n",head.nbDP,p->clientInfo); ::printf("\nClosing connection with %s\n",p->clientInfo); close_socket(p->clientSock); return false; @@ -322,14 +333,15 @@ bool Kangaroo::HandleRequest(TH_PARAM *p) { //::printf("%d DP from %s\n",nbDP,p->clientInfo.c_str()); - DP *dp = (DP *)malloc(sizeof(DP)*nbDP); - GETFREE("DP",p->clientSock,dp,sizeof(DP)*nbDP,ntimeout,dp); + DP *dp = (DP *)malloc(sizeof(DP)* head.nbDP); + GETFREE("DP",p->clientSock,dp,sizeof(DP)* head.nbDP,ntimeout,dp); state = GetServerStatus(); PUTFREE("Status",p->clientSock,&state,sizeof(int32_t),ntimeout,dp); - if(nbRead != sizeof(DP)*nbDP) { + if(nbRead != sizeof(DP)* head.nbDP) { - ::printf("\nUnexpected DP size from %s [nbDP=%d,Got %d,Expected %d]\n",p->clientInfo,nbDP,nbRead,(int)(sizeof(DP)*nbDP)); + ::printf("\nUnexpected DP size from %s [nbDP=%d,Got %d,Expected %d]\n", + p->clientInfo,head.nbDP,nbRead,(int)(sizeof(DP)* head.nbDP)); ::printf("\nClosing connection with %s\n",p->clientInfo); free(dp); close_socket(p->clientSock); @@ -337,23 +349,54 @@ bool Kangaroo::HandleRequest(TH_PARAM *p) { } else { +#define VALIDITY_POINT_CHECK #ifdef VALIDITY_POINT_CHECK // Check validity - for(uint32_t i=0;i= HASH_SIZE) { - ::printf("\nInvalid data from: %s [at dp=%d]\n",p->clientInfo,i); + ::printf("\nInvalid data from: %s [dp=%d PID=%u thId=%u gpuId=%u]\n",p->clientInfo,i, + head.processId,head.threadId,head.gpuId); ::printf("\nClosing connection with %s\n",p->clientInfo); free(dp); close_socket(p->clientSock); return false; } + + Int dist; + uint32_t kType; + HashTable::CalcDistAndType(dp[i].d,&dist,&kType); + Point P = secp->ComputePublicKey(&dist); + + if(kType == WILD) + P = secp->AddDirect(keyToSearch,P); + + uint32_t hC = P.x.bits64[2] & HASH_MASK; + bool ok = (hC == h) && (P.x.bits64[0] == dp[i].x.i64[0]) && (P.x.bits64[1] == dp[i].x.i64[1]); + if(!ok) { + if(kType==TAME) { + ::printf("\nWrong TAME point from: %s [dp=%d PID=%u thId=%u gpuId=%u]\n",p->clientInfo,i, + head.processId,head.threadId,head.gpuId); + } else { + ::printf("\nWrong WILD point from: %s [dp=%d PID=%u thId=%u gpuId=%u]\n",p->clientInfo,i, + head.processId,head.threadId,head.gpuId); + } + //::printf("X=%s\n",P.x.GetBase16().c_str()); + //::printf("X=%08X%08X%08X%08X\n",dp[i].x.i32[3],dp[i].x.i32[2],dp[i].x.i32[1],dp[i].x.i32[0]); + //::printf("D=%08X%08X%08X%08X\n",dp[i].d.i32[3],dp[i].d.i32[2],dp[i].d.i32[1],dp[i].d.i32[0]); + ::printf("\nClosing connection with %s\n",p->clientInfo); + free(dp); + close_socket(p->clientSock); + return false; + } + } #endif LOCK(ghMutex); DP_CACHE dc; - dc.nbDP = nbDP; + dc.nbDP = head.nbDP; dc.dp = dp; recvDP.push_back(dc); UNLOCK(ghMutex); @@ -463,11 +506,18 @@ void Kangaroo::RunServer() { } SetDP(initDPSize); - if(sizeof(DP)!=40) { + if(sizeof(DP) != 40) { ::printf("Error: Invalid DP size struct\n"); exit(-1); } + if(sizeof(DPHEADER) != 20) { + ::printf("Error: Invalid DPHEADER size struct\n"); + exit(-1); + } + + + if(saveKangaroo) { ::printf("Waring: Server does not support -ws, ignoring\n"); saveKangaroo = false; @@ -718,7 +768,7 @@ void Kangaroo::WaitForServer() { } // Send DP to Server -bool Kangaroo::SendToServer(std::vector &dps) { +bool Kangaroo::SendToServer(std::vector &dps,uint32_t threadId,uint32_t gpuId) { int nbRead; int nbWrite; @@ -752,8 +802,15 @@ bool Kangaroo::SendToServer(std::vector &dps) { char cmd = SERVER_SENDDP; + DPHEADER head; + head.header = SERVER_HEADER; + head.nbDP = nbDP; + head.processId = pid; + head.threadId = threadId; + head.gpuId = gpuId; + PUTFREE("CMD",serverConn,&cmd,1,ntimeout,dp); - PUTFREE("nbDP",serverConn,&nbDP,sizeof(uint32_t),ntimeout,dp); + PUTFREE("DPHeader",serverConn,&head,sizeof(DPHEADER),ntimeout,dp); PUTFREE("DP",serverConn,dp,sizeof(DP)*nbDP,ntimeout,dp); GETFREE("Status",serverConn,&status,sizeof(uint32_t),ntimeout,dp) @@ -810,13 +867,18 @@ bool Kangaroo::GetConfigFromServer() { GET("KeyY",serverConn,key.y.bits64,32,ntimeout); GET("DP",serverConn,&initDPSize,sizeof(int32_t),ntimeout); - if(version>=2) { - // Set kangaroo number - char cmd = SERVER_SETKNB; - PUT("CMD",serverConn,&cmd,1,ntimeout); - PUT("nbKangaroo",serverConn,&totalRW,sizeof(uint64_t),ntimeout); + if(version<3) { + isConnected = false; + closesocket(serverConn); + ::printf("Cannot connect to server: %s\nServer version must be >= 3\n",serverIp.c_str()); + return false; } + // Set kangaroo number + cmd = SERVER_SETKNB; + PUT("CMD",serverConn,&cmd,1,ntimeout); + PUT("nbKangaroo",serverConn,&totalRW,sizeof(uint64_t),ntimeout); + ::printf("Succesfully connected to server: %s (Version %d)\n",serverIp.c_str(),version); keysToSearch.clear(); diff --git a/PartMerge.cpp b/PartMerge.cpp index 3491fac..412e406 100644 --- a/PartMerge.cpp +++ b/PartMerge.cpp @@ -172,8 +172,12 @@ bool Kangaroo::MergePartition(TH_PARAM* p) { // Rename string oldName = GetPartName(p1Name,part,true); string newName = GetPartName(p1Name,part,false); - remove(newName.c_str()); - rename(oldName.c_str(),newName.c_str()); + if(!endOfSearch) { + remove(newName.c_str()); + rename(oldName.c_str(),newName.c_str()); + } else { + remove(oldName.c_str()); + } return true; @@ -700,8 +704,12 @@ bool Kangaroo::MergeWorkPart(std::string& partName,std::string& file2,bool print // Rename string oldName = GetPartName(partName,part,true); string newName = GetPartName(partName,part,false); - remove(newName.c_str()); - rename(oldName.c_str(),newName.c_str()); + if(!endOfSearch) { + remove(newName.c_str()); + rename(oldName.c_str(),newName.c_str()); + } else { + remove(oldName.c_str()); + } } diff --git a/Timer.cpp b/Timer.cpp index 0102c09..4cf65e2 100644 --- a/Timer.cpp +++ b/Timer.cpp @@ -66,6 +66,16 @@ uint32_t Timer::getSeed32() { return ::strtoul(getSeed(4).c_str(),NULL,16); } +uint32_t Timer::getPID() { + +#ifdef WIN64 + return GetCurrentProcessId(); +#else + return (uint32_t)getpid(); +#endif + +} + std::string Timer::getSeed(int size) { std::string ret; diff --git a/Timer.h b/Timer.h index e7f8fea..36ccd27 100644 --- a/Timer.h +++ b/Timer.h @@ -35,6 +35,7 @@ class Timer { static std::string getSeed(int size); static void SleepMillis(uint32_t millis); static uint32_t getSeed32(); + static uint32_t getPID(); static std::string getTS(); #ifdef WIN64