# The Square Vault: how it is built

Rooms of Square Lock (every tile a perfect square scrambled by one hidden XOR mask; flip the bits until every tile is a square) lead down through vault doors, where a square nested inside a square is peeled one root at a time to its ground seed. Everything comes from the `folds` engine.

## What it imports

```html
<script src="../../../sitoolz/js/blake2b.js"></script><script src="../../../sitoolz/js/qrand.js"></script>
<script src="../../../sitoolz/js/rangeblends.js"></script><script src="../../../sitoolz/js/rulerland.js"></script>
<script src="../../../sitoolz/js/indexrng.js"></script>   <!-- SIIndexrng.Index: the same name deals the same vault everywhere -->
<script src="../../../sitoolz/js/folds.js"></script>      <!-- SIFolds: squareLock, checkLock, lockHint, vault, isSquare, isqrt -->
```

## The twenty lines that make it

```js
const rng = name => new SIIndexrng.Index(name);
const lock = SIFolds.squareLock(rng("vault-2-0:lock"), 7, 6);        // {tiles, mask, roots, bits}: exactly one mask below 2^6 opens it
let guess = 0;
function flip(bit){ guess ^= (1 << bit); draw(); if (SIFolds.checkLock(lock.tiles, guess)) openVault(); }
function draw(){ for (const t of lock.tiles){ const v = t ^ guess; tile(v, SIFolds.isSquare(v) ? `${SIFolds.isqrt(v)}²` : `was ${t}`); } }
function hint(){ const b = SIFolds.lockHint(lock.tiles, guess, lock.bits); say(b === null ? "The mask is right." : `Bit ${b} is wrong.`); }
const vault = SIFolds.vault(rng("vault-2-0:vault"), 5);              // {seed, depth, top, ladder}: top = climb(seed, 5), ladder = peel(top)
let peeled = 0;
function peel(){ peeled++; rung(vault.ladder[peeled]); if (peeled === vault.ladder.length - 1) say(`Ground seed ${vault.seed}.`); }
```

The lock's uniqueness is the engine's: `squareLock` deals masks and roots from the indexed generator and keeps the first deal for which `solveLock` finds exactly one mask. The vault's ladder is `peel(climb(seed, depth))`, and it ends at the seed because a ground seed is one whose fold is not a square.

## How to change the law

- More tiles or more bits: `squareLock(rng, n, bits)`; the floors here go 5 tiles of 4 bits to 9 tiles of 7 bits. Past about 8 bits the brute-force uniqueness check (`solveLock`) starts to cost.
- A different fold: `vault(rng, depth, m)` nests under mask `m`; ground seeds change with it (`isGround(seed, m)`).
- Deeper vaults: `climb(seed, 8)` is already a 40-digit number; the engine keeps BigInt on the JavaScript side.
- The same laws as ground: `SIFolds.height(g, z, mask, unit)` is The Fold Marches, where these squares are vault doors sunk into a range whose relief is the fold ruler.

## How to verify it

`sitoolz/fixtures/folds.json` holds twenty locks with their masks, roots and hints and eight vaults with their ladders, written by `tests/write_folds_fixture.py` and checked by `tests/twins-folds.test.cjs` (9,106 values). `smoke.cjs` here opens every floor's lock by flipping exactly the bits that differ from the mask, peels every vault to a ground seed of 0 or 2 with one root per rung, and deals today's vault.

## What was missing

Nothing: the locks, the hints, the nested squares, the square test and the indexed deal are all the engine's. One note: `lockHint` gives away the lowest wrong bit, which is a strong hint on a four-bit lock; a gentler one would say only which tile proves it.

![](shot.png)
