/** * Checkable dice (DESIGN §4-1, §4-2). This module is what the verifier page runs; keep it small and readable. * * seed 16 raw bytes (128 bits) from crypto.getRandomValues, shown and typed as 32 lowercase hex characters. * fingerprint "Huella de los dados": first 8 lowercase hex characters of SHA-256 over the seed's 32 lowercase hex * characters as ASCII text (no spaces, no newline), shown before roll #1. Anyone can check it with * `printf %s | shasum -a 256` (macOS) or `... | sha256sum` (Linux). * (Before fix round 1 it was SHA-256 of the 16 raw bytes: `legacyFingerprint`, kept only so a game saved * under that scheme still verifies; see save/schema.ts.) * daily seed first 16 bytes of SHA-256(utf8("parchis-daily-v1|YYYY-MM-DD")): the same for everyone that date. * roll #i SHA-256 in counter mode: h = SHA-256(utf8("parchis-dice-v1|") ‖ seed bytes ‖ i as 4 bytes big-endian). * Take the bytes of h in order, skip any byte 252..255 (rejection sampling: 252 = 6 × 42, so every face * has exactly 42 byte values behind it, no modulo bias) and the first kept byte b gives the face b % 6 + 1. * If all 32 bytes are rejected (chance 2^-192) the next block is SHA-256(h) and so on. * The roll input (domain prefix + raw bytes + counter) is a different message from the fingerprint's (the bare hex text), * so the fingerprint reveals nothing about a roll. The roll derivation stays on raw bytes: only the fingerprint is * something a skeptic recomputes by hand, and the bytes and the lowercase hex text are the same seed one-to-one. * * A roll depends on (seed, index) only: no move, level, score, leader or clock goes in. The game consumes the * indices 0, 1, 2, … in order, so the whole sequence is fixed before the first roll (tested in dice.test.ts). */ import { fromHex, sha256, sha256Hex, toHex, utf8 } from './sha256.ts'; export const SEED_BYTES = 16; const DICE_DOMAIN = utf8('parchis-dice-v1|'); /** A fresh 128-bit seed from the platform's crypto RNG (Web Crypto: browsers, WebViews, Node ≥ 19). Never time or a counter. */ export function newSeed(): string { const b = new Uint8Array(SEED_BYTES); globalThis.crypto.getRandomValues(b); return toHex(b); } /** Seed of the "Partida del día" (date = the device's local date as YYYY-MM-DD). */ export function dailySeed(ymd: string): string { if (!/^\d{4}-\d{2}-\d{2}$/.test(ymd)) throw new Error(`bad date: ${ymd}`); return sha256Hex(`parchis-daily-v1|${ymd}`).slice(0, SEED_BYTES * 2); } /** Seed (32 hex characters, any case, spaces ignored) -> the 16 raw bytes. Throws on anything else. */ export function seedBytes(seed: string): Uint8Array { const b = fromHex(seed.replace(/\s+/g, '')); if (b.length !== SEED_BYTES) throw new Error(`seed must be ${SEED_BYTES} bytes`); return b; } /** The seed as the 32 lowercase hex characters the player sees (any case and spaces accepted). Throws on anything else. */ export function seedText(seed: string): string { return toHex(seedBytes(seed)); } /** "Huella de los dados": the first 8 hex characters of SHA-256 over the seed's lowercase hex text: `printf %s | shasum -a 256`. */ export function fingerprint(seed: string): string { return sha256Hex(seedText(seed)).slice(0, 8); } /** The scheme before fix round 1 (SHA-256 of the 16 raw bytes). Only for games saved under it and the verifier's check of their chip. */ export function legacyFingerprint(seed: string): string { return sha256Hex(seedBytes(seed)).slice(0, 8); } /** The one-line shell command that reproduces `fingerprint(seed)` (shown on the verifier page). */ export const fingerprintCommand = (seed: string): string => `printf %s ${seedText(seed)} | shasum -a 256`; // One reusable message buffer: domain ‖ seed ‖ index. Remembers the last seed so a game doesn't re-parse it every roll. const msg = new Uint8Array(DICE_DOMAIN.length + SEED_BYTES + 4); msg.set(DICE_DOMAIN); const msgView = new DataView(msg.buffer); let lastSeed = ''; /** Face 1..6 of roll number `index` (0-based) of the game with this seed. */ export function dieRoll(seed: string, index: number): number { if (!(Number.isInteger(index) && index >= 0 && index <= 0xffffffff)) throw new Error(`bad roll index ${index}`); if (seed !== lastSeed) { msg.set(seedBytes(seed), DICE_DOMAIN.length); lastSeed = seed; } msgView.setUint32(DICE_DOMAIN.length + SEED_BYTES, index); let h = sha256(msg); for (;;) { for (const b of h) if (b < 252) return (b % 6) + 1; h = sha256(h); } } /** The first `n` rolls of a seed ("Ver todas las tiradas", the verifier page). */ export function rollList(seed: string, n: number): number[] { const out: number[] = []; for (let i = 0; i < n; i++) out.push(dieRoll(seed, i)); return out; }