html: Eliminate strings in Javascript (#5)

eliminate strings in hasher for 15kh/s speed improvement

---------

Co-authored-by: Vaxry <vaxry@vaxry.net>
This commit is contained in:
L95Kdg8bw9U74mUBK9XqBjWlUrS9ksUN
2025-04-14 01:20:02 +02:00
committed by GitHub
parent 067c993317
commit dd87fda183
2 changed files with 18 additions and 21 deletions

View File

@@ -240,16 +240,6 @@
const challengeSig = "{{ tl:text challengeSignature }}";
const challengeFingerprint = "{{ tl:text challengeFingerprint }}";
function valid(sha) {
const MIN_ZEROES = difficulty;
for (let i = 0; i < MIN_ZEROES; i += 1) {
if (sha[i] != '0')
return false;
}
return true;
}
function itShort(it) {
if (it > 1000000)
return parseInt(it / 100000) / 10 + "M";
@@ -297,18 +287,25 @@
});
}
const encoder = new TextEncoder();
while (true) {
const data = encoder.encode(challengeNonce + it);
const buffer = await window.crypto.subtle.digest("SHA-256", data);
const hashArray = Array.from(new Uint8Array(buffer));
const sha = hashArray
.map((item) => item.toString(16).padStart(2, "0"))
.join("");
const workBuffer = new Uint8Array(challengeNonce.length + 10);
for (let i = 0; i < challengeNonce.length; i++)
workBuffer[i] = challengeNonce.charCodeAt(i);
if (valid(sha)) {
while (true) {
const digits = it === 0 ? 1 : (~~Math.floor(Math.log10(it)) + 1);
for (let i = digits - 1; i >= 0; i--)
workBuffer[challengeNonce.length + (digits - 1 - i)] = 48 + ~~Math.floor((it / Math.pow(10, i)) % 10);
const hashedData = new Uint8Array(await window.crypto.subtle.digest('sha-256', workBuffer.slice(0, challengeNonce.length + digits)));
let zeroPos = 0;
for (; zeroPos < difficulty; zeroPos++) {
if ((zeroPos % 2 == 0 ? (hashedData[~~(zeroPos / 2)] >> 4) : (hashedData[~~(zeroPos / 2)] & 0x0F)) != 0)
break;
}
if (zeroPos >= difficulty) {
success(it);
console.log("Success: it " + it + ": " + sha);
console.log("Success: it " + it + ": " + Array.from(hashedData).map((item) => item.toString(16).padStart(2, "0")).join(""));
break;
}