Skip to content

Commit

Permalink
Reformatted code
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Bognolo <git@bogny.eu>
  • Loading branch information
Luca Bognolo committed Jun 23, 2021
1 parent 9676f5a commit 7b10d22
Show file tree
Hide file tree
Showing 69 changed files with 5,356 additions and 5,555 deletions.
76 changes: 42 additions & 34 deletions AddrGen/main.cpp
Original file line number Diff line number Diff line change
@@ -1,81 +1,86 @@
#include <iostream>
#include <string>
#include <time.h>

#include "secp256k1.h"
#include "util.h"
#include "AddressUtil.h"
#include "CmdParse.h"

int main(int argc, char **argv)
{
std::vector<secp256k1::uint256> keys;
std::vector <secp256k1::uint256> keys;

clock_t start, end;

bool compressed = true;
bool compressed = true;
bool printPrivate = false;
bool printPublic = false;
bool printAddr = false;
bool printAll = true;
int count = 1;

secp256k1::uint256 k;
secp256k1::uint256 k;

k = secp256k1::generatePrivateKey();
k = secp256k1::generatePrivateKey();

CmdParse parser;
CmdParse parser;

parser.add("-c", "--compressed", false);
parser.add("-u", "--uncompressed", false);
parser.add("-c", "--compressed", false);
parser.add("-u", "--uncompressed", false);
parser.add("-p", "--pub", false);
parser.add("-k", "--priv", false);
parser.add("-a", "--addr", false);
parser.add("-n", true);

parser.parse(argc, argv);
parser.parse(argc, argv);

std::vector <OptArg> args = parser.getArgs();

std::vector<OptArg> args = parser.getArgs();
for (unsigned int i = 0; i < args.size(); i++) {
OptArg arg = args[i];

for(unsigned int i = 0; i < args.size(); i++) {
OptArg arg = args[i];

if(arg.equals("-c", "--compressed")) {
compressed = true;
} else if(arg.equals("-u", "--uncompressed")) {
compressed = false;
} else if(arg.equals("-k", "--priv")) {
if (arg.equals("-c", "--compressed")) {
compressed = true;
} else if (arg.equals("-u", "--uncompressed")) {
compressed = false;
} else if (arg.equals("-k", "--priv")) {
printAll = false;
printPrivate = true;
} else if(arg.equals("-p", "--pub")) {
} else if (arg.equals("-p", "--pub")) {
printAll = false;
printPublic = true;
} else if(arg.equals("-a", "--addr")) {
} else if (arg.equals("-a", "--addr")) {
printAll = false;
printAddr = true;
} else if(arg.equals("-n")) {
count = (int)util::parseUInt32(arg.arg);
} else if (arg.equals("-n")) {
count = (int) util::parseUInt32(arg.arg);
}
}
}

std::vector<std::string> operands = parser.getOperands();
std::vector <std::string> operands = parser.getOperands();

if(operands.size() > 0) {
for(int i = 0; i < operands.size(); i++) {
if (operands.size() > 0) {
for (int i = 0; i < operands.size(); i++) {
try {
keys.push_back(secp256k1::uint256(operands[i]));
} catch(std::string err) {
} catch (std::string err) {
printf("Error parsing private key: %s\n", err.c_str());
return 1;
}
}
} else {
for(int i = 0; i < count; i++) {
for (int i = 0; i < count; i++) {
keys.push_back(secp256k1::generatePrivateKey());
}
}

for(int i = 0; i < keys.size(); i++) {
start = clock();

for (int i = 0; i < keys.size(); i++) {
secp256k1::uint256 k = keys[i];

if(k.isZero() || k.cmp(secp256k1::N) >= 0)
{
if (k.isZero() || k.cmp(secp256k1::N) >= 0) {
printf("Error parsing private key: Private key is out of range\n");

return 1;
Expand All @@ -84,16 +89,19 @@ int main(int argc, char **argv)
secp256k1::ecpoint p = secp256k1::multiplyPoint(k, secp256k1::G());
std::string address = Address::fromPublicKey(p, compressed);

if(printAll || printPrivate) {
if (printAll || printPrivate) {
std::cout << k.toString() << std::endl;
}
if(printAll || printPublic) {
if (printAll || printPublic) {
std::cout << p.toString() << std::endl;
}
if(printAll || printAddr) {
if (printAll || printAddr) {
std::cout << address << std::endl;
}
}

return 0;
end = clock();
std::cout << " Total taken : " << double(end - start) / double(CLOCKS_PER_SEC) << " s" << std::endl;

return 0;
}
29 changes: 14 additions & 15 deletions AddressUtil/AddressUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,31 @@
#include "secp256k1.h"

namespace Address {
std::string fromPublicKey(const secp256k1::ecpoint &p, bool compressed = false);
bool verifyAddress(std::string address);
std::string fromPublicKey(const secp256k1::ecpoint &p, bool compressed = false);

bool verifyAddress(std::string address);
};

namespace Base58 {
std::string toBase58(const secp256k1::uint256 &x);
secp256k1::uint256 toBigInt(const std::string &s);
void getMinMaxFromPrefix(const std::string &prefix, secp256k1::uint256 &minValueOut, secp256k1::uint256 &maxValueOut);
std::string toBase58(const secp256k1::uint256 &x);

void toHash160(const std::string &s, unsigned int hash[5]);
secp256k1::uint256 toBigInt(const std::string &s);

bool isBase58(std::string s);
};
void getMinMaxFromPrefix(const std::string &prefix, secp256k1::uint256 &minValueOut, secp256k1::uint256 &maxValueOut);

void toHash160(const std::string &s, unsigned int hash[5]);

bool isBase58(std::string s);
};

namespace Hash {
void hashPublicKey(const secp256k1::ecpoint &p, unsigned int *digest);

void hashPublicKeyCompressed(const secp256k1::ecpoint &p, unsigned int *digest);

void hashPublicKey(const secp256k1::ecpoint &p, unsigned int *digest);
void hashPublicKeyCompressed(const secp256k1::ecpoint &p, unsigned int *digest);

void hashPublicKey(const unsigned int *x, const unsigned int *y, unsigned int *digest);
void hashPublicKeyCompressed(const unsigned int *x, const unsigned int *y, unsigned int *digest);
void hashPublicKey(const unsigned int *x, const unsigned int *y, unsigned int *digest);

void hashPublicKeyCompressed(const unsigned int *x, const unsigned int *y, unsigned int *digest);
};


#endif
#endif
125 changes: 61 additions & 64 deletions AddressUtil/Base58.cpp
Original file line number Diff line number Diff line change
@@ -1,117 +1,114 @@
#include <map>
#include "CryptoUtil.h"

#include "CryptoUtil.h"
#include "AddressUtil.h"


static const std::string BASE58_STRING = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

struct Base58Map {
static std::map<char, int> createBase58Map()
{
std::map<char, int> m;
for(int i = 0; i < 58; i++) {
m[BASE58_STRING[i]] = i;
}
static std::map<char, int> createBase58Map()
{
std::map<char, int> m;
for (int i = 0; i < 58; i++) {
m[BASE58_STRING[i]] = i;
}

return m;
}
return m;
}

static std::map<char, int> myMap;
static std::map<char, int> myMap;
};

std::map<char, int> Base58Map::myMap = Base58Map::createBase58Map();



/**
* Converts a base58 string to uint256
*/
secp256k1::uint256 Base58::toBigInt(const std::string &s)
{
secp256k1::uint256 value;
secp256k1::uint256 value;

for(unsigned int i = 0; i < s.length(); i++) {
value = value.mul(58);
for (unsigned int i = 0; i < s.length(); i++) {
value = value.mul(58);

int c = Base58Map::myMap[s[i]];
value = value.add(c);
}
int c = Base58Map::myMap[s[i]];
value = value.add(c);
}

return value;
return value;
}

void Base58::toHash160(const std::string &s, unsigned int hash[5])
{
secp256k1::uint256 value = toBigInt(s);
unsigned int words[6];
secp256k1::uint256 value = toBigInt(s);
unsigned int words[6];

value.exportWords(words, 6, secp256k1::uint256::BigEndian);
value.exportWords(words, 6, secp256k1::uint256::BigEndian);

// Extract words, ignore checksum
for(int i = 0; i < 5; i++) {
hash[i] = words[i];
}
// Extract words, ignore checksum
for (int i = 0; i < 5; i++) {
hash[i] = words[i];
}
}

bool Base58::isBase58(std::string s)
{
for(unsigned int i = 0; i < s.length(); i++) {
if(BASE58_STRING.find(s[i]) < 0) {
return false;
}
}
for (unsigned int i = 0; i < s.length(); i++) {
if (BASE58_STRING.find(s[i]) < 0) {
return false;
}
}

return true;
return true;
}

std::string Base58::toBase58(const secp256k1::uint256 &x)
{
std::string s;
std::string s;

secp256k1::uint256 value = x;
secp256k1::uint256 value = x;

while(!value.isZero()) {
secp256k1::uint256 digit = value.mod(58);
int digitInt = digit.toInt32();
while (!value.isZero()) {
secp256k1::uint256 digit = value.mod(58);
int digitInt = digit.toInt32();

s = BASE58_STRING[digitInt] + s;
s = BASE58_STRING[digitInt] + s;

value = value.div(58);
}
value = value.div(58);
}

return s;
return s;
}

void Base58::getMinMaxFromPrefix(const std::string &prefix, secp256k1::uint256 &minValueOut, secp256k1::uint256 &maxValueOut)
{
secp256k1::uint256 minValue = toBigInt(prefix);
secp256k1::uint256 maxValue = minValue;
int exponent = 1;
secp256k1::uint256 minValue = toBigInt(prefix);
secp256k1::uint256 maxValue = minValue;
int exponent = 1;

// 2^192
unsigned int expWords[] = { 0, 0, 0, 0, 0, 0, 1, 0 };
// 2^192
unsigned int expWords[] = {0, 0, 0, 0, 0, 0, 1, 0};

secp256k1::uint256 exp(expWords);
secp256k1::uint256 exp(expWords);

// Find the smallest 192-bit number that starts with the prefix. That is, the prefix multiplied
// by some power of 58
secp256k1::uint256 nextValue = minValue.mul(58);
// Find the smallest 192-bit number that starts with the prefix. That is, the prefix multiplied
// by some power of 58
secp256k1::uint256 nextValue = minValue.mul(58);

while(nextValue.cmp(exp) < 0) {
exponent++;
minValue = nextValue;
nextValue = nextValue.mul(58);
}
while (nextValue.cmp(exp) < 0) {
exponent++;
minValue = nextValue;
nextValue = nextValue.mul(58);
}

secp256k1::uint256 diff = secp256k1::uint256(58).pow(exponent - 1).sub(1);
secp256k1::uint256 diff = secp256k1::uint256(58).pow(exponent - 1).sub(1);

maxValue = minValue.add(diff);
maxValue = minValue.add(diff);

if(maxValue.cmp(exp) > 0) {
maxValue = exp.sub(1);
}
if (maxValue.cmp(exp) > 0) {
maxValue = exp.sub(1);
}

minValueOut = minValue;
maxValueOut = maxValue;
}
minValueOut = minValue;
maxValueOut = maxValue;
}
Loading

0 comments on commit 7b10d22

Please sign in to comment.