Skip to content

Commit

Permalink
More allocation optimizations + type-conversion removals
Browse files Browse the repository at this point in the history
Once again giving the GC a better life, and also removing some unnecessary and slow type-conversions (string --> bytes --> string) by keeping the entire pubkey generation flow as byte buffers only, much faster than string operations!
  • Loading branch information
JSKitty committed Feb 4, 2022
1 parent 8b6a25b commit 23a33d1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 29 deletions.
3 changes: 0 additions & 3 deletions index.html
Expand Up @@ -393,9 +393,6 @@ <h3 class="pivx-bold-title-smaller"><span>Go to</span>My Wallet</h3>
</p>
</div>


<input style="display:none;" type="text" id='prefix' placeholder="Address Prefix">

<!-- IMPORT WALLET -->
<input class="hide-element" type="text" id="clipboard">
<div id='importWallet' style='display: none;'>
Expand Down
10 changes: 3 additions & 7 deletions scripts/bitTrx.js
Expand Up @@ -63,13 +63,9 @@
pubkeyBytes.unshift(0x04);

if (bitjs.compressed == true) {
const publicKeyBytesCompressed = EllipticCurve.integerToBytes(x,32)
if (y.isEven()) {
publicKeyBytesCompressed.unshift(0x02)
} else {
publicKeyBytesCompressed.unshift(0x03)
}
return Crypto.util.bytesToHex(publicKeyBytesCompressed);
const pubCompressed = EllipticCurve.integerToBytes(x,32)
pubCompressed.unshift(y.isEven() ? 2 : 3);
return Crypto.util.bytesToHex(pubCompressed);
} else {
return Crypto.util.bytesToHex(pubkeyBytes);
}
Expand Down
13 changes: 7 additions & 6 deletions scripts/vanitygen_worker.js
Expand Up @@ -82,6 +82,8 @@ function writeToUint8(arr, bytes, pos = 0) {
}

// Generation Constants + Pre-allocations
const pubCompressedLen = 33; // (int) Length of compressed pubkey
const pubCompressed = new Uint8Array(pubCompressedLen); // (Uint8) Pre-allocated array for a compressed pubkey
const pubHashNetLen = 21; // (int) Length of net-encoded pubkey hash
const pubHashNet = new Uint8Array(pubHashNetLen); // (Uint8) Pre-allocated array for a pubkey hash
const pubPreBaseLen = 25; // (int) Length of pre-base58 pubkey
Expand All @@ -94,14 +96,13 @@ while (true) {
getSafeRand(pkBytes);

// Public Key Derivation
let nPubkey = Crypto.util.bytesToHex(nSecp256k1.getPublicKey(pkBytes)).substr(2);
const pubY = new BN(nPubkey.substr(64), 16);
nPubkey = nPubkey.substr(0, 64);
const publicKeyBytesCompressed = Crypto.util.hexToBytes(nPubkey);
publicKeyBytesCompressed.unshift(pubY.isEven() ? 0x02 : 0x03);
let nPubkey = nSecp256k1.getPublicKey(pkBytes).slice(1, 65);
const pubY = new BN(nPubkey.slice(32), 16);
pubCompressed[0] = pubY.isEven() ? 2 : 3;
writeToUint8(pubCompressed, nPubkey.slice(0, 32), 1);
// First pubkey SHA-256 hash
const pubKeyHashing = new jsSHA(0, 0, { "numRounds": 1 });
pubKeyHashing.update(publicKeyBytesCompressed);
pubKeyHashing.update(pubCompressed);
// RIPEMD160 hash
const pubKeyHashRipemd160 = ripemd160(pubKeyHashing.getHash(0));
// Network Encoding
Expand Down
26 changes: 13 additions & 13 deletions scripts/wallet.js
Expand Up @@ -95,14 +95,13 @@ importWallet = function (newWif = false, raw = false) {
console.log(Crypto.util.bytesToHex(privkeyBytes));
}
// Public Key Derivation
let nPubkey = Crypto.util.bytesToHex(nSecp256k1.getPublicKey(privkeyBytes)).substr(2);
const pubY = new BN(nPubkey.slice(64), 16);
nPubkey = nPubkey.substr(0, 64);
const publicKeyBytesCompressed = Crypto.util.hexToBytes(nPubkey);
publicKeyBytesCompressed.unshift(pubY.isEven() ? 0x02 : 0x03);
let nPubkey = nSecp256k1.getPublicKey(privkeyBytes).slice(1, 65);
const pubY = new BN(nPubkey.slice(32), 16);
pubCompressed[0] = pubY.isEven() ? 2 : 3;
writeToUint8(pubCompressed, nPubkey.slice(0, 32), 1);
// First pubkey SHA-256 hash
const pubKeyHashing = new jsSHA(0, 0, { "numRounds": 1 });
pubKeyHashing.update(publicKeyBytesCompressed);
pubKeyHashing.update(pubCompressed);
// RIPEMD160 hash
const pubKeyHashRipemd160 = ripemd160(pubKeyHashing.getHash(0));
// Network Encoding
Expand Down Expand Up @@ -196,6 +195,8 @@ const pkNetBytesLen = 34; // (int) Length of net
const pkNetBytes = new Uint8Array(pkNetBytesLen); // (Uint8) Pre-allocated array for pk bytes
const pkNetChecksumLen = 38; // (int) Length of net-encoded, checksummed PK bytes
const keyWithChecksum = new Uint8Array(pkNetChecksumLen); // (Uint8) Pre-allocated array for checksummed pk bytes
const pubCompressedLen = 33; // (int) Length of compressed pubkey
const pubCompressed = new Uint8Array(pubCompressedLen); // (Uint8) Pre-allocated array for a compressed pubkey
const pubHashNetLen = 21; // (int) Length of net-encoded pubkey hash
const pubHashNet = new Uint8Array(pubHashNetLen); // (Uint8) Pre-allocated array for a pubkey hash
const pubPreBaseLen = 25; // (int) Length of pre-base58 pubkey
Expand Down Expand Up @@ -230,14 +231,13 @@ generateWallet = async function (noUI = false) {
privateKeyForTransactions = to_b58(keyWithChecksum);

// Public Key Derivation
let nPubkey = Crypto.util.bytesToHex(nSecp256k1.getPublicKey(pkBytes)).substr(2);
const pubY = new BN(nPubkey.slice(64), 16);
nPubkey = nPubkey.substr(0, 64);
const publicKeyBytesCompressed = Crypto.util.hexToBytes(nPubkey);
publicKeyBytesCompressed.unshift(pubY.isEven() ? 0x02 : 0x03);
let nPubkey = nSecp256k1.getPublicKey(pkBytes).slice(1, 65);
const pubY = new BN(nPubkey.slice(32), 16);
pubCompressed[0] = pubY.isEven() ? 2 : 3;
writeToUint8(pubCompressed, nPubkey.slice(0, 32), 1);
// First pubkey SHA-256 hash
const pubKeyHashing = new jsSHA(0, 0, { "numRounds": 1 });
pubKeyHashing.update(publicKeyBytesCompressed);
pubKeyHashing.update(pubCompressed);
// RIPEMD160 hash
const pubKeyHashRipemd160 = ripemd160(pubKeyHashing.getHash(0));
// Network Encoding
Expand Down Expand Up @@ -269,7 +269,7 @@ generateWallet = async function (noUI = false) {
console.log('Private Key')
console.log(privateKeyForTransactions)
console.log('Public Key')
console.log(publicKeyBytesCompressed)
console.log(pubCompressed)
console.log('Public Key Extended')
console.log(Crypto.util.bytesToHex(pubkeyExt))
console.log('SHA256 Public Key')
Expand Down

0 comments on commit 23a33d1

Please sign in to comment.