mirror of
https://github.com/TecharoHQ/anubis.git
synced 2025-08-03 17:59:24 -04:00

This completely rewrites how the proof of work challenge works based on feedback from browser engine developers and starts the process of making the proof of work function easier to change out. - Import @aws-crypto/sha256-js to use in Firefox as its implementation of WebCrypto doesn't jump directly from highly optimized browser internals to JIT-ed JavaScript like Chrome's seems to. - Move the worker code to `web/js/worker/*` with each worker named after the hashing method and hash method implementation it uses. - Update bench.mjs to import algorithms the new way. - Delete video.mjs, it was part of a legacy experiment that I never had time to finish. - Update LibreJS comment to add info about the use of @aws-crypto/sha256-js. - Also update my email to my @techaro.lol address. Signed-off-by: Xe Iaso <me@xeiaso.net>
61 lines
1.4 KiB
JavaScript
61 lines
1.4 KiB
JavaScript
import { Sha256 } from '@aws-crypto/sha256-js';
|
|
|
|
const calculateSHA256 = (text) => {
|
|
const hash = new Sha256();
|
|
hash.update(text);
|
|
return hash.digest();
|
|
};
|
|
|
|
function uint8ArrayToHexString(arr) {
|
|
return Array.from(arr)
|
|
.map((c) => c.toString(16).padStart(2, "0"))
|
|
.join("");
|
|
}
|
|
|
|
addEventListener('message', async ({ data: eventData }) => {
|
|
const { data, difficulty, threads } = eventData;
|
|
let nonce = eventData.nonce;
|
|
const isMainThread = nonce === 0;
|
|
let iterations = 0;
|
|
|
|
const requiredZeroBytes = Math.floor(difficulty / 2);
|
|
const isDifficultyOdd = difficulty % 2 !== 0;
|
|
|
|
for (; ;) {
|
|
const hashBuffer = await calculateSHA256(data + nonce);
|
|
const hashArray = new Uint8Array(hashBuffer);
|
|
|
|
let isValid = true;
|
|
for (let i = 0; i < requiredZeroBytes; i++) {
|
|
if (hashArray[i] !== 0) {
|
|
isValid = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (isValid && isDifficultyOdd) {
|
|
if ((hashArray[requiredZeroBytes] >> 4) !== 0) {
|
|
isValid = false;
|
|
}
|
|
}
|
|
|
|
if (isValid) {
|
|
const finalHash = toHexString(hashArray);
|
|
postMessage({
|
|
hash: finalHash,
|
|
data,
|
|
difficulty,
|
|
nonce,
|
|
});
|
|
return; // Exit worker
|
|
}
|
|
|
|
nonce += threads;
|
|
iterations++;
|
|
|
|
// Send a progress update from the main thread every 1024 iterations.
|
|
if (isMainThread && (iterations & 1023) === 0) {
|
|
postMessage(nonce);
|
|
}
|
|
}
|
|
}); |