[feature] update proof-of-work to allow setting required rounds (#4186)

# Description

This updates our proof-of-work middleware, NoLLaMas, to work on a more easily configurable algorithm (thank you f0x for bringing this to my attention!). Instead of requiring that a solution with pre-determined number of '0' chars be found, it now pre-computes a result with a pre-determined nonce value that it expects the client to iterate up-to. (though with some level of jitter applied, to prevent it being too-easily gamed). This allows the user to configure roughly how many hash-encode rounds they want their clients to have to complete.

## Checklist

- [x] I/we have read the [GoToSocial contribution guidelines](https://codeberg.org/superseriousbusiness/gotosocial/src/branch/main/CONTRIBUTING.md).
- [x] I/we have discussed the proposed changes already, either in an issue on the repository, or in the Matrix chat.
- [x] I/we have not leveraged AI to create the proposed changes.
- [x] I/we have performed a self-review of added code.
- [x] I/we have written code that is legible and maintainable by others.
- [x] I/we have commented the added code, particularly in hard-to-understand areas.
- [x] I/we have made any necessary changes to documentation.
- [ ] I/we have added tests that cover new code.
- [x] I/we have run tests and they pass locally with the changes.
- [x] I/we have run `go fmt ./...` and `golangci-lint run`.

Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4186
Co-authored-by: kim <grufwub@gmail.com>
Co-committed-by: kim <grufwub@gmail.com>
This commit is contained in:
kim 2025-05-26 11:57:50 +02:00 committed by tobi
commit 326e04283a
23 changed files with 4350 additions and 160 deletions

View file

@ -43,18 +43,18 @@ document.addEventListener("DOMContentLoaded", function() {
// Read the challenge and difficulty from
// data attributes on the nollamas section.
const seed = nollamas.dataset.nollamasSeed;
const challenge = nollamas.dataset.nollamasChallenge;
const difficulty = nollamas.dataset.nollamasDifficulty;
console.log("challenge:", challenge); // eslint-disable-line no-console
console.log("difficulty:", difficulty); // eslint-disable-line no-console
console.log("seed:", seed); // eslint-disable-line no-console
console.log("challenge:", challenge); // eslint-disable-line no-console
// Prepare the worker with task function.
const worker = new Worker("/assets/dist/nollamasworker.js");
const startTime = performance.now();
worker.postMessage({
challenge: challenge,
difficulty: difficulty,
seed: seed,
});
// Set the main worker function.

View file

@ -19,32 +19,22 @@
import sha256 from "./sha256";
let compute = async function(challengeStr, diffStr) {
let compute = async function(seedStr, challengeStr) {
const textEncoder = new TextEncoder();
// Get difficulty1 as number and generate
// expected zero ASCII prefix to check for.
const diff1 = parseInt(diffStr, 10);
const zeros = "0".repeat(diff1);
// Calculate hex encoded prefix required to check solution, where we
// need diff1 no. chars in hex, and hex encoding doubles input length.
const prefixLen = diff1 / 2 + (diff1 % 2 != 0 ? 2 : 0);
let nonce = 0;
while (true) { // eslint-disable-line no-constant-condition
// Create possible solution string from challenge string + nonce.
const solution = textEncoder.encode(challengeStr + nonce.toString());
const solution = textEncoder.encode(seedStr + nonce.toString());
// Generate SHA256 hashsum of solution string, and hex encode the
// necessary prefix length we need to check for a valid solution.
const prefixArray = Array.from(sha256(solution).slice(0, prefixLen));
const prefixHex = prefixArray.map(b => b.toString(16).padStart(2, "0")).join("");
// Generate hex encoded SHA256 hashsum of solution.
const hashArray = Array.from(sha256(solution));
const hashAsHex = hashArray.map(b => b.toString(16).padStart(2, "0")).join("");
// Check if the hex encoded hash has
// difficulty defined zeroes prefix.
if (prefixHex.startsWith(zeros)) {
// Check whether hex encoded
// solution matches challenge.
if (hashAsHex == challengeStr) {
return nonce;
}
@ -56,11 +46,8 @@ let compute = async function(challengeStr, diffStr) {
onmessage = async function(e) {
console.log('worker started'); // eslint-disable-line no-console
const challenge = e.data.challenge;
const difficulty = e.data.difficulty;
// Compute the nonce that produces solution with args.
let nonce = await compute(challenge, difficulty);
// Compute nonce value that produces 'challenge' for seed.
let nonce = await compute(e.data.seed, e.data.challenge);
// Post the solution nonce back to caller.
postMessage({ nonce: nonce, done: true });

View file

@ -20,8 +20,8 @@
{{- with . }}
<main>
<section class="nollamas"
data-nollamas-seed="{{ .seed }}"
data-nollamas-challenge="{{ .challenge }}"
data-nollamas-difficulty="{{ .difficulty }}"
>
<h1>Checking you're not a creepy crawler...</h1>
<noscript>