Skip to content

Commit

Permalink
Added partial private key search
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanLucPons committed Apr 17, 2019
1 parent d9ec510 commit fe6b1ed
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 81 deletions.
20 changes: 10 additions & 10 deletions GPU/GPUEngine.cu
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ bool GPUEngine::CheckHash(uint8_t *h, vector<ITEM>& found,int tid,int incr,int e

}

bool GPUEngine::Check(Secp256K1 &secp) {
bool GPUEngine::Check(Secp256K1 *secp) {

uint8_t h[20];
int i = 0;
Expand Down Expand Up @@ -662,10 +662,10 @@ bool GPUEngine::Check(Secp256K1 &secp) {
memset(nbFoundCPU, 0, sizeof(nbFoundCPU));
for (int i = 0; i < nbThread; i++) {
k.Rand(256);
p[i] = secp.ComputePublicKey(&k);
p[i] = secp->ComputePublicKey(&k);
// Group starts at the middle
k.Add((uint64_t)GRP_SIZE/2);
p2[i] = secp.ComputePublicKey(&k);
p2[i] = secp->ComputePublicKey(&k);
}

std::vector<prefix_t> prefs;
Expand Down Expand Up @@ -699,22 +699,22 @@ bool GPUEngine::Check(Secp256K1 &secp) {
p2 = p[j];
p1.x.ModMulK1(&beta);
p2.x.ModMulK1(&beta2);
p[j] = secp.NextKey(p[j]);
p[j] = secp->NextKey(p[j]);

// Point and endo
secp.GetHash160(P2PKH, searchComp, pt, h);
secp->GetHash160(P2PKH, searchComp, pt, h);
prefix_t pr = *(prefix_t *)h;
if (pr == 0xFEFE || pr == 0x1234) {
nbFoundCPU[0]++;
ok &= CheckHash(h,found, j, i, 0, nbOK + 0);
}
secp.GetHash160(P2PKH, searchComp, p1, h);
secp->GetHash160(P2PKH, searchComp, p1, h);
pr = *(prefix_t *)h;
if (pr == 0xFEFE || pr == 0x1234) {
nbFoundCPU[1]++;
ok &= CheckHash(h, found, j, i, 1, nbOK + 1);
}
secp.GetHash160(P2PKH, searchComp, p2, h);
secp->GetHash160(P2PKH, searchComp, p2, h);
pr = *(prefix_t *)h;
if (pr == 0xFEFE || pr == 0x1234) {
nbFoundCPU[2]++;
Expand All @@ -726,19 +726,19 @@ bool GPUEngine::Check(Secp256K1 &secp) {
p1.y.ModNeg();
p2.y.ModNeg();

secp.GetHash160(P2PKH, searchComp, pt, h);
secp->GetHash160(P2PKH, searchComp, pt, h);
pr = *(prefix_t *)h;
if (pr == 0xFEFE || pr == 0x1234) {
nbFoundCPU[3]++;
ok &= CheckHash(h, found, j, -i, 0, nbOK + 3);
}
secp.GetHash160(P2PKH, searchComp, p1, h);
secp->GetHash160(P2PKH, searchComp, p1, h);
pr = *(prefix_t *)h;
if (pr == 0xFEFE || pr == 0x1234) {
nbFoundCPU[4]++;
ok &= CheckHash(h, found, j, -i, 1, nbOK + 4);
}
secp.GetHash160(P2PKH, searchComp, p2, h);
secp->GetHash160(P2PKH, searchComp, p2, h);
pr = *(prefix_t *)h;
if (pr == 0xFEFE || pr == 0x1234) {
nbFoundCPU[5]++;
Expand Down
4 changes: 2 additions & 2 deletions GPU/GPUEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ class GPUEngine {
int GetNbThread();
int GetGroupSize();

bool Check(Secp256K1 &secp);
bool Check(Secp256K1 *secp);
std::string deviceName;

static void PrintCudaInfo();
static void GenerateCode(Secp256K1 &secp, int size);
static void GenerateCode(Secp256K1 *secp, int size);

private:

Expand Down
10 changes: 5 additions & 5 deletions GPU/GPUGenerate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@
using namespace std;


void GPUEngine::GenerateCode(Secp256K1 &secp, int size) {
void GPUEngine::GenerateCode(Secp256K1 *secp, int size) {

// Compute generator table
Point *Gn = new Point[size];
Point g = secp.G;
Point g = secp->G;
Gn[0] = g;
g = secp.DoubleDirect(g);
g = secp->DoubleDirect(g);
Gn[1] = g;
for (int i = 2; i < size; i++) {
g = secp.AddDirect(g, secp.G);
g = secp->AddDirect(g, secp->G);
Gn[i] = g;
}
// _2Gn = CPU_GRP_SIZE*G
Point _2Gn = secp.DoubleDirect(Gn[size / 2 - 1]);
Point _2Gn = secp->DoubleDirect(Gn[size / 2 - 1]);

// Write file
FILE *f = fopen("GPU/GPUGroup.h", "w");
Expand Down
5 changes: 3 additions & 2 deletions Int.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,13 @@ class Int {
void ModSqrt(); // this <- +/-sqrt(this) (mod n)
bool HasSqrt(); // true if this admit a square root

// Specific SecpK1 modular mult
// Specific SecpK1
static void InitK1(Int *order);
void ModMulK1(Int *a, Int *b);
void ModMulK1(Int *a);
static void InitK1(Int *order);
void ModMulK1order(Int *a);
void ModSquareK1(Int *a);
void ModAddK1order(Int *a,Int *b);

// Size
int GetSize();
Expand Down
6 changes: 6 additions & 0 deletions IntMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,12 @@ void Int::InitK1(Int *order) {
_R2o.SetBase16("9D671CD581C69BC5E697F5E45BCD07C6741496C20E7CF878896CF21467D7D140");
}

void Int::ModAddK1order(Int *a, Int *b) {
Add(a,b);
Sub(_O);
if (IsNegative())
Add(_O);
}

void Int::ModMulK1order(Int *a) {

Expand Down
118 changes: 118 additions & 0 deletions SECP256K1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ Int Secp256K1::DecodePrivateKey(char *key,bool *compressed) {
}

printf("Invalid private key, not starting with 5,K or L !\n");
ret.SetInt32(-1);
return ret;

}
Expand Down Expand Up @@ -441,6 +442,123 @@ void Secp256K1::GetHash160(int type,bool compressed,

}

uint8_t Secp256K1::GetByte(std::string &str, int idx) {

char tmp[3];
int val;

tmp[0] = str.data()[2 * idx];
tmp[1] = str.data()[2 * idx + 1];
tmp[2] = 0;

if (sscanf(tmp, "%X", &val) != 1) {
printf("ParsePublicKeyHex: Error invalid public key specified (unexpected hexadecimal digit)\n");
exit(-1);
}

return (uint8_t)val;

}

Point Secp256K1::ParsePublicKeyHex(std::string str,bool &isCompressed) {

Point ret;
ret.Clear();

if (str.length() < 2) {
printf("ParsePublicKeyHex: Error invalid public key specified (66 or 130 character length)\n");
exit(-1);
}

uint8_t type = GetByte(str, 0);

switch (type) {

case 0x02:
if (str.length() != 66) {
printf("ParsePublicKeyHex: Error invalid public key specified (66 character length)\n");
exit(-1);
}
for (int i = 0; i < 32; i++)
ret.x.SetByte(31 - i, GetByte(str, i + 1));
ret.y = GetY(ret.x, true);
isCompressed = true;
break;

case 0x03:
if (str.length() != 66) {
printf("ParsePublicKeyHex: Error invalid public key specified (66 character length)\n");
exit(-1);
}
for (int i = 0; i < 32; i++)
ret.x.SetByte(31 - i, GetByte(str, i + 1));
ret.y = GetY(ret.x, false);
isCompressed = true;
break;

case 0x04:
if (str.length() != 130) {
printf("ParsePublicKeyHex: Error invalid public key specified (130 character length)\n");
exit(-1);
}
for (int i = 0; i < 32; i++)
ret.x.SetByte(31 - i, GetByte(str, i + 1));
for (int i = 0; i < 32; i++)
ret.y.SetByte(31 - i, GetByte(str, i + 33));
isCompressed = false;
break;

default:
printf("ParsePublicKeyHex: Error invalid public key specified (Unexpected prefix (only 02,03 or 04 allowed)\n");
exit(-1);
}

ret.z.SetInt32(1);

if (!EC(ret)) {
printf("ParsePublicKeyHex: Error invalid public key specified (Not lie on elliptic curve)\n");
exit(-1);
}

return ret;

}

std::string Secp256K1::GetPublicKeyHex(bool compressed, Point &pubKey) {

unsigned char publicKeyBytes[128];
char tmp[3];
std::string ret;

if (!compressed) {

// Full public key
publicKeyBytes[0] = 0x4;
pubKey.x.Get32Bytes(publicKeyBytes + 1);
pubKey.y.Get32Bytes(publicKeyBytes + 33);

for (int i = 0; i < 65; i++) {
sprintf(tmp, "%02X", (int)publicKeyBytes[i]);
ret.append(tmp);
}

} else {

// Compressed public key
publicKeyBytes[0] = pubKey.y.IsEven() ? 0x2 : 0x3;
pubKey.x.Get32Bytes(publicKeyBytes + 1);

for (int i = 0; i < 33; i++) {
sprintf(tmp, "%02X", (int)publicKeyBytes[i]);
ret.append(tmp);
}

}

return ret;

}

void Secp256K1::GetHash160(int type, bool compressed, Point &pubKey, unsigned char *hash) {

unsigned char shapk[64];
Expand Down
4 changes: 4 additions & 0 deletions SECP256k1.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Secp256K1 {
std::string GetAddress(int type, bool compressed, Point &pubKey);
std::string GetAddress(int type, bool compressed, unsigned char *hash160);
std::string GetPrivAddress(bool compressed, Int &privKey );
std::string GetPublicKeyHex(bool compressed, Point &p);
Point ParsePublicKeyHex(std::string str, bool &isCompressed);

bool CheckPudAddress(std::string address);

Expand All @@ -63,6 +65,8 @@ class Secp256K1 {

private:

uint8_t GetByte(std::string &str,int idx);

Int GetY(Int x, bool isEven);
Point GTable[256*32]; // Generator table

Expand Down
Loading

0 comments on commit fe6b1ed

Please sign in to comment.