Skip to content

Instantly share code, notes, and snippets.

@danielevns
Last active December 14, 2022 04:28
Show Gist options
  • Save danielevns/adee44d3973865af9f22179564d22e7d to your computer and use it in GitHub Desktop.
Save danielevns/adee44d3973865af9f22179564d22e7d to your computer and use it in GitHub Desktop.
bustabit.com v2 history generation
const { createHash, createHmac } = require("crypto");
const salt = "0000000000000000004d6ec16dafe9d8370958664c1dc422f452892264c59526"; // chosen in seeding event: https://bitcointalk.org/index.php?topic=2807542.msg28778130#msg28778130
let [id, hash] = process.argv.slice(2);
id = Number(id);
if (Number.isNaN(id) || !Number.isSafeInteger(id) || id < 1) {
printUsageMessage();
process.exit(1);
}
const isSha256Hash = /^[0-9a-f]{64}$/i.test(hash);
if (!isSha256Hash) {
printUsageMessage();
process.exit(1);
}
hash = Buffer.from(hash, "hex");
console.log("id,hash,result");
for (; id > 0; id--) {
const result = gameResult(hash, salt);
console.log(`${id},${hash.toString("hex")},${result}`);
hash = sha256(hash.toString("hex"));
}
console.error(`The terminating hash is ${hash.toString("hex")}.`);
function gameResult(seed, salt) {
const nBits = 52; // number of most significant bits to use
// 1. HMAC_SHA256(key=salt, message=seed)
const hmac = createHmac("sha256", salt);
hmac.update(seed);
seed = hmac.digest("hex");
// 2. r = 52 most significant bits
seed = seed.slice(0, nBits / 4);
const r = parseInt(seed, 16);
// 3. X = r / 2^52
let X = r / Math.pow(2, nBits); // uniformly distributed in [0; 1)
// 4. X = 99 / (1-X)
X = 99 / (1 - X);
// 5. return max(trunc(X), 100)
const result = Math.floor(X);
return Math.max(1, result / 100);
}
function printUsageMessage() {
console.error(`Usage: node main.js <game_id> <hash>
Example: node main.js 1152006 01ad6c6988689c103b4cf6ed84e8a14b1c38ef48c26abc44fb97874f36be4e02`);
}
function sha256(value) {
return createHash("sha256")
.update(value)
.digest();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment