Skip to content

Commit

Permalink
fixed some bugs for legacy version
Browse files Browse the repository at this point in the history
  • Loading branch information
albertobsd committed May 17, 2023
1 parent f25e13b commit d33bbe8
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 208 deletions.
18 changes: 10 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ legacy:
gcc -march=native -mtune=native -Wno-unused-result -Ofast -ftree-vectorize -c base58/base58.c -o base58.o
gcc -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c xxhash/xxhash.c -o xxhash.o
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c util.c -o util.o
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c hashing.c -o hashing.o -lcrypto
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c gmp256k1/Int.cpp -o Int.o -lgmp
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c gmp256k1/Point.cpp -o Point.o -lgmp
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c gmp256k1/GMP256K1.cpp -o GMP256K1.o -lgmp
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c gmp256k1/IntMod.cpp -o IntMod.o -lgmp
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -flto -c gmp256k1/Random.cpp -o Random.o -lgmp
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -flto -c gmp256k1/IntGroup.cpp -o IntGroup.o -lgmp
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -o keyhunt keyhunt_legacy.cpp base58.o bloom.o oldbloom.o xxhash.o util.o Int.o Point.o GMP256K1.o IntMod.o IntGroup.o Random.o hashing.o -lm -lpthread -lcrypto -lgmp
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c sha3/sha3.c -o sha3.o
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c sha3/keccak.c -o keccak.o
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c hashing.c -o hashing.o
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c gmp256k1/Int.cpp -o Int.o
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c gmp256k1/Point.cpp -o Point.o
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c gmp256k1/GMP256K1.cpp -o GMP256K1.o
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -c gmp256k1/IntMod.cpp -o IntMod.o
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -flto -c gmp256k1/Random.cpp -o Random.o
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -flto -c gmp256k1/IntGroup.cpp -o IntGroup.o
g++ -march=native -mtune=native -Wall -Wextra -Ofast -ftree-vectorize -o keyhunt keyhunt_legacy.cpp base58.o bloom.o oldbloom.o xxhash.o util.o Int.o Point.o GMP256K1.o IntMod.o IntGroup.o Random.o hashing.o sha3.o keccak.o -lm -lpthread -lcrypto -lgmp
rm -r *.o
75 changes: 61 additions & 14 deletions gmp256k1/GMP256K1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Secp256K1::~Secp256K1() {

Point Secp256K1::Negation(Point &p) {
Point Q;
Q.Clear();
//Q.Clear();
Q.x.Set(&p.x);
Q.y.Set(&this->P);
Q.y.Sub(&p.y);
Expand Down Expand Up @@ -562,33 +562,80 @@ void Secp256K1::GetHash160(int type, bool compressed, Point &pubKey, unsigned ch
void Secp256K1::GetHash160(int type,bool compressed,
Point &k0,Point &k1,Point &k2,Point &k3,
uint8_t *h0,uint8_t *h1,uint8_t *h2,uint8_t *h3) {
GetHash160(type,compressed,k0,h0);
GetHash160(type,compressed,k1,h1);
GetHash160(type,compressed,k2,h2);
GetHash160(type,compressed,k3,h3);

switch (type) {
case P2PKH:
case BECH32:
unsigned char digests[4][65];

if (!compressed) {
// Full public key
digests[0][0] = 0x4;
digests[1][0] = 0x4;
digests[2][0] = 0x4;
digests[3][0] = 0x4;
k0.x.Get32Bytes(digests[0] + 1);
k0.y.Get32Bytes(digests[0] + 33);
k1.x.Get32Bytes(digests[1] + 1);
k1.y.Get32Bytes(digests[1] + 33);
k2.x.Get32Bytes(digests[2] + 1);
k2.y.Get32Bytes(digests[2] + 33);
k3.x.Get32Bytes(digests[3] + 1);
k3.y.Get32Bytes(digests[3] + 33);

sha256_4(65, digests[0], digests[1],digests[2],digests[3],digests[0], digests[1],digests[2],digests[3]);
} else {
// Compressed public key
digests[0][0] = (unsigned char) k0.y.IsEven() ? 0x2 : 0x3;
digests[1][0] = (unsigned char) k1.y.IsEven() ? 0x2 : 0x3;
digests[2][0] = (unsigned char) k2.y.IsEven() ? 0x2 : 0x3;
digests[3][0] = (unsigned char) k3.y.IsEven() ? 0x2 : 0x3;
k0.x.Get32Bytes(digests[0] + 1);
k1.x.Get32Bytes(digests[1] + 1);
k2.x.Get32Bytes(digests[2] + 1);
k3.x.Get32Bytes(digests[3] + 1);
sha256_4(33, digests[0], digests[1],digests[2],digests[3],digests[0], digests[1],digests[2],digests[3]);
}
rmd160_4(32, digests[0], digests[1],digests[2],digests[3],h0,h1,h2,h3);

break;
case P2SH:
printf("Unsoported P2SH\n");
exit(0);
/*
// Redeem Script (1 to 1 P2SH)
unsigned char script[64];
script[0] = 0x00; // OP_0
script[1] = 0x14; // PUSH 20 bytes
GetHash160(P2PKH, compressed, pubKey, script + 2);
sha256(script, 22, shapk);
rmd160(shapk,32,hash);
*/
break;
}
}


void Secp256K1::GetHash160_fromX(int type,unsigned char prefix,
Int *k0,Int *k1,Int *k2,Int *k3,
uint8_t *h0,uint8_t *h1,uint8_t *h2,uint8_t *h3) {
unsigned char digests[4][33];
int i;
//int i;
switch (type) {
case P2PKH:

k0->Get32Bytes((unsigned char*)(digests[0] + 1));
k1->Get32Bytes((unsigned char*)(digests[1] + 1));
k2->Get32Bytes((unsigned char*)(digests[2] + 1));
k3->Get32Bytes((unsigned char*)(digests[3] + 1));
for(i = 0; i < 4; i++) {
digests[i][0] = prefix;
sha256(digests[i],33,digests[i]);
}
rmd160(digests[0],32,h0);
rmd160(digests[1],32,h1);
rmd160(digests[2],32,h2);
rmd160(digests[3],32,h3);
digests[0][0] = prefix;
digests[1][0] = prefix;
digests[2][0] = prefix;
digests[3][0] = prefix;

sha256_4(33, digests[0], digests[1],digests[2],digests[3],digests[0], digests[1],digests[2],digests[3]);
rmd160_4(32, digests[0], digests[1],digests[2],digests[3],h0,h1,h2,h3);

break;

case P2SH:
Expand Down
11 changes: 4 additions & 7 deletions gmp256k1/Int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ bool Int::IsOdd() {
}

int Int::GetSize() {
/*
gmp_printf("GetSize of %Zi\n",num);
fflush(stdout);
*/
int r = mpz_sizeinbase(num,2);
if(r % 8 == 0)
return (int)(r/8);
Expand Down Expand Up @@ -281,13 +285,6 @@ char* Int::GetBase16() {
return mpz_get_str(NULL,16,num);
}

/*
char* Int::GetBaseN(int n,const char *charset);
char* Int::GetBlockStr();
char* Int::GetC64Str(int nbDigit);
*/


void Int::SetInt64(uint64_t value) {
char my_str_value[U64STRINGSIZE]; // 30 digits + null terminator
snprintf(my_str_value, U64STRINGSIZE, "%lu", value);
Expand Down
34 changes: 5 additions & 29 deletions gmp256k1/Point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include <stdio.h>

Point::Point() {
mpz_set_ui(x.num,0);
mpz_set_ui(y.num,0);
mpz_set_ui(z.num,0);

}

Expand Down Expand Up @@ -87,22 +90,7 @@ void Point::Reduce() {
i.ModInv();
x.ModMul(&x,&i);
y.ModMul(&y,&i);
z.SetInt32(1);
/*
Yes, exactly. The Reduce function you mentioned converts the point from projective coordinates back to affine coordinates.
In elliptic curve computations, it's often more efficient to work with projective coordinates because they allow addition and doubling operations to be performed without needing to do division operations, which are computationally expensive.
However, at the end of your computation, or at certain intermediate stages, you might need to convert the point back to affine coordinates. That's what this Reduce function is doing.
Here's what each line in Reduce is doing:
Int i(&z); creates an integer i from the z coordinate of the point.
i.ModInv(); computes the modular inverse of i, effectively performing a division operation. Note that this operation is only valid if i is not zero.
x.ModMul(&x,&i); and y.ModMul(&y,&i); multiply the x and y coordinates by the modular inverse of z, effectively dividing them by z. This converts the x and y coordinates from projective back to affine coordinates.
z.SetInt32(1); sets the z coordinate to 1, completing the conversion to affine coordinates.
In the end, Reduce leaves the point in the form (X/Z, Y/Z, 1), which is equivalent to (X, Y) in affine coordinates.
*/
z.SetInt32(1);
}

bool Point::equals(Point &p) {
Expand All @@ -120,19 +108,7 @@ Point& Point::operator=(const Point& other) {
mpz_set(x.num,other.x.num);
mpz_set(y.num,other.y.num);
mpz_set(z.num,other.z.num);
/*
ptrs[0] = x.GetBase16();
ptrs[1] = y.GetBase16();
ptrs[2] = z.GetBase16();
printf("Point\n");
printf("X: %s\n",ptrs[0]);
printf("Y: %s\n",ptrs[1]);
printf("Z: %s\n",ptrs[2]);
printf("End Point\n");
for(int i = 0; i<3; i++) {
free(ptrs[i]);
}
*/

// Return the current object
return *this;
}
Expand Down
Loading

0 comments on commit d33bbe8

Please sign in to comment.