- 다빈치 코드: 조커를 놓는 단계(placeJoker)를 상대·관전자 화면에는 바로 전 단계로 보여 줌(서버 view) - 다빈치 코드: 공개된 타일은 반 칸 위로 올리고 아래에 노란 줄, 숨은 타일은 제자리 - 바퀴벌레 포커: 내가 한 빠른 말도 내 자리 위 말풍선으로 표시 - 난수: 새 게임은 256비트 OS 시드 + ChaCha20(RFC 8439, 암호학적 생성기). 옛 게임(128비트)은 sfc32 그대로라 다시 보기 동일 - 테스트: RFC 테스트 벡터, 균등성(카이제곱), 상태 이어받기, 조커 단계 비공개 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
101 lines
4.2 KiB
TypeScript
101 lines
4.2 KiB
TypeScript
import { describe, expect, test } from 'bun:test';
|
|
import { SeededRng, chachaBlock, createSeed, seedFromHex, seedToHex } from './rng';
|
|
|
|
describe('SeededRng', () => {
|
|
test('is deterministic for the same seed', () => {
|
|
const a = SeededRng.fromSeed([1, 2, 3, 4]);
|
|
const b = SeededRng.fromSeed([1, 2, 3, 4]);
|
|
for (let i = 0; i < 100; i++) expect(a.nextUint32()).toBe(b.nextUint32());
|
|
});
|
|
|
|
test('state can be saved and resumed', () => {
|
|
const a = SeededRng.fromSeed([9, 8, 7, 6]);
|
|
a.int(10);
|
|
const resumed = new SeededRng(a.state());
|
|
expect(resumed.int(1000)).toBe(a.int(1000));
|
|
});
|
|
|
|
test('int stays in range and is roughly uniform', () => {
|
|
const r = SeededRng.fromSeed([5, 5, 5, 5]);
|
|
const counts = new Array(6).fill(0);
|
|
for (let i = 0; i < 60000; i++) counts[r.int(6)]++;
|
|
for (const c of counts) expect(Math.abs(c - 10000)).toBeLessThan(500);
|
|
});
|
|
|
|
test('shuffle is a permutation', () => {
|
|
const r = SeededRng.fromSeed(createSeed());
|
|
const arr = Array.from({ length: 52 }, (_, i) => i);
|
|
const s = r.shuffle(arr);
|
|
expect(s.slice().sort((x, y) => x - y)).toEqual(arr);
|
|
});
|
|
|
|
test('hex round trip', () => {
|
|
const seed = createSeed();
|
|
expect(seedFromHex(seedToHex(seed))).toEqual(seed);
|
|
});
|
|
});
|
|
|
|
describe('ChaCha20 (new games)', () => {
|
|
test('block function matches the RFC 8439 §2.3.2 test vector', () => {
|
|
const key = [0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c, 0x13121110, 0x17161514, 0x1b1a1918, 0x1f1e1d1c];
|
|
const out = chachaBlock(key, 1, [0x09000000, 0x4a000000, 0x00000000]);
|
|
expect([...out].map((n) => n.toString(16).padStart(8, '0'))).toEqual([
|
|
'e4e7f110', '15593bd1', '1fdd0f50', 'c47120a3', 'c7f4d1c7', '0368c033', '9aaa2204', '4e6cd4c3',
|
|
'466482d2', '09aa9f07', '05d7c214', 'a2028bd9', 'd19c12b5', 'b94e16de', 'e883d0cb', '4e3c50a2',
|
|
]);
|
|
});
|
|
|
|
test('new seeds are 256 bits from the OS and pick ChaCha20; old 128-bit seeds keep sfc32', () => {
|
|
const seed = createSeed();
|
|
expect(seed).toHaveLength(8);
|
|
expect(seedToHex(seed)).toMatch(/^[0-9a-f]{64}$/);
|
|
expect(seedFromHex(seedToHex(seed))).toEqual(seed);
|
|
expect(SeededRng.fromSeed(seed).state()).toHaveLength(11);
|
|
expect(SeededRng.fromSeed(seedFromHex('00000001000000020000000300000004')).state()).toHaveLength(4);
|
|
// Old games replay exactly as before.
|
|
const old = SeededRng.fromSeed([1, 2, 3, 4]);
|
|
expect([old.nextUint32(), old.nextUint32(), old.nextUint32()]).toEqual([417285410, 1196253302, 123583739]); // sfc32 output before 2026-10-11
|
|
});
|
|
|
|
test('deterministic, and the saved state resumes exactly (also at a block boundary)', () => {
|
|
const seed = createSeed();
|
|
const a = SeededRng.fromSeed(seed);
|
|
const b = SeededRng.fromSeed(seed);
|
|
for (let i = 0; i < 100; i++) expect(a.nextUint32()).toBe(b.nextUint32());
|
|
for (const k of [0, 1, 15, 16, 17, 31, 32, 33]) {
|
|
const r = SeededRng.fromSeed(seed);
|
|
for (let i = 0; i < k; i++) r.nextUint32();
|
|
const saved = JSON.parse(JSON.stringify(r.state()));
|
|
const resumed = new SeededRng(saved);
|
|
for (let i = 0; i < 40; i++) expect(resumed.nextUint32()).toBe(r.nextUint32());
|
|
}
|
|
});
|
|
|
|
test('uniform: dice, cards and bits', () => {
|
|
const r = SeededRng.fromSeed(createSeed());
|
|
const counts = new Array(6).fill(0);
|
|
for (let i = 0; i < 60000; i++) counts[r.int(6)]++;
|
|
// chi-square with 5 degrees of freedom: 20.5 is the 0.1% tail.
|
|
const chi = counts.reduce((acc, c) => acc + (c - 10000) ** 2 / 10000, 0);
|
|
expect(chi).toBeLessThan(20.5);
|
|
const first = new Array(52).fill(0);
|
|
for (let i = 0; i < 52000; i++) first[r.shuffle(Array.from({ length: 52 }, (_, j) => j))[0]!]++;
|
|
const chi52 = first.reduce((acc, c) => acc + (c - 1000) ** 2 / 1000, 0);
|
|
expect(chi52).toBeLessThan(97); // 51 dof, 0.01% tail
|
|
let ones = 0;
|
|
for (let i = 0; i < 10000; i++) {
|
|
let x = r.nextUint32();
|
|
while (x) {
|
|
ones += x & 1;
|
|
x >>>= 1;
|
|
}
|
|
}
|
|
expect(Math.abs(ones / 320000 - 0.5)).toBeLessThan(0.005);
|
|
});
|
|
|
|
test('two games never share a seed', () => {
|
|
const seen = new Set(Array.from({ length: 1000 }, () => seedToHex(createSeed())));
|
|
expect(seen.size).toBe(1000);
|
|
});
|
|
});
|