Skip to content

feat(web): presets, matrix paste and export, undo with saved workspace and share links, circuit from matrix - #15

Merged
4waan merged 19 commits into
mainfrom
feat/web-workspace-productivity
Nov 11, 2026
Merged

4waan merged 19 commits into
mainfrom
feat/web-workspace-productivity

Conversation

@4waan

@4waan 4waan commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Summary

The Workspace is now faster for a cipher designer working through many matrices:

  • A searchable presets library with designs, matrices, S-boxes and circuits, each with an honest scope note. Every MDS, invertibility, permutation and circuit-match fact shown on a preset is recomputed by a unit test with lib/preview.ts.
  • Paste and import matrices in the notations people copy from papers, C or Rust reference code, Python and Sage, with cell normalization and inline errors. A Copy as menu exports JSON, C, Rust, Sage and a plain grid.
  • Undo and redo over workspace edits, the workspace saved in localStorage, story steps that keep the edits they replace, and share links that carry the design and circuit in the URL hash.
  • Circuit from matrix: a term-by-term straight-line program for the target matrix, a shortcut into the quick experiment search, and a live computed-matrix diff that marks which coefficients of the circuit's matrix differ from the target.

Everything computed in the browser stays labelled as an unchecked preview. Lean still decides on Check.

Changes

Presets (src/lib/library.ts, src/components/PresetPicker.tsx)

  • 29 presets in four groups. GF(2^8) presets use 0x11B only:
    • Designs: AES-like with the shared-sum circuit; row-scaled AES with a generated circuit; identity (weak); Poseidon2 t = 3 over BN254; Poseidon2 with the deliberately weak internal [[2,1,1],[1,2,1],[1,1,2]].
    • Matrices: AES MixColumns and InvMixColumns, row-scaled AES, MDS circulants circ(01,01,04,09) and circ(01,02,03,04), non-MDS circulants circ(02,01,01,01), circ(01,02,04,06) and binary circ(00,01,01,01) (each labelled not MDS), identity; the Poseidon2 external and internal matrices and the weak internal one.
    • S-boxes: AES, identity (a permutation with no nonlinearity), a non-injective table; x⁵, x⁷, and x³ (not a permutation over BN254).
    • Circuits: AES shared-sum (textbook, 15 XOR + 4 xtime), AES term by term, a miswired shared-sum, identity, and the BN254 external and internal shared sums (the internal one uses no multiplications).
  • Scope notes say what each preset is and is not. Examples:
    • The AES-like template is not full AES.
    • Midori-style 0/1 patterns are not Midori, which works over GF(2^4).
    • The weak internal matrix passes the invertibility check. It is weak because v = (0,1,−1) satisfies Mv = v with v₀ = 0; a unit test checks this fixed point. That weakness is outside what the workbench checks today.
  • Ranked search: a whole label token scores higher than a prefix, and a phrase match (circ(02, 01) ranks first. Results are grouped by kind.
  • ARIA combobox and listbox: arrow keys, Page Up/Down, Enter and Escape. The highlighted preset expands its scope note. Fact chips are dashed, like the other unchecked previews. A legend at the top of the list says in plain words that dashed chips are quick checks computed in the browser, not Lean proofs, and that Check gives evidence; each chip also carries this as a tooltip and a screen-reader prefix.

Matrix notation (src/lib/notation.ts, src/components/MatrixEditor.tsx)

  • parseMatrixText reads:
    • whitespace, comma or semicolon separated grids;
    • a flat list of n·n values;
    • C and Rust literals, with or without declarations and suffixes (0x02u8);
    • Python, NumPy and Sage lists, including F(2), F.fetch_int(2), e(0x02) and matrix(F, 4, 4, [...]);
    • quoted JSON strings and LaTeX pmatrix.
  • Bare numbers are read as hex in a plain grid and as decimal inside code. The preview names the reading and lets the user switch to Hex or Decimal.
  • BN254 accepts decimal and 0x input. Negative values are reduced modulo p and values at or above p are rejected.
  • Errors name the row and column (for example "Row 2 has 3 entries; row 1 has 4.").
  • normalizeCell runs on blur: 1 becomes 01, 0x1b becomes 1B, and in BN254 -1 becomes p − 1.
  • Pasting a whole matrix into any cell fills the grid. Pasting a run of values fills from the focused cell; a run that would pass the last cell is refused with its own message ("3 values from row 4, column 3 run past the last cell"). Paste matrix… opens a box with a live preview first. Its live region holds only a one-line summary or the error, not the preview grid. Closing the box returns focus to the button, and Use this matrix moves focus to the first cell.
  • Copy as is a keyboard-navigable menu. While cells are invalid it is disabled and says why in visible text tied to the button. formatMatrix writes JSON, C uint8_t, Rust [[u8; n]; n], Sage over GF(2)[x]/(x⁸+x⁴+x³+x+1) (or GF(p) for BN254), or a plain grid. Every format reads back to the same matrix (tested).

Undo, persistence, story snapshots and share links (src/lib/{history,debounce,persist,share,route,shortcuts,snapshot}.ts, src/state/*)

  • All workspace edits go through one commit path with a bounded history of 100 steps.
    • Keystrokes in one cell coalesce into one step if they arrive within 600 ms of each other.
    • Pastes, presets, imports, share links and generated circuits are one step each.
  • Ctrl/⌘+Z undoes and Shift+Ctrl/⌘+Z or Ctrl+Y redoes. Undo and Redo buttons in the toolbar carry aria-keyshortcuts. Text areas and the preset search keep the browser's own undo.
  • The design, circuit, target and any kept story edits are saved to localStorage (cryptificate.workspace.v1) 300 ms after each change and flushed on pagehide. Storage access is wrapped in try/catch. Saved data is shape-checked on restore, and corrupt or foreign data is ignored. The toolbar shows the save status: Saved in this browser only after the first write or a restore, Changes are saved in this browser before that, and a warning when storage is blocked.
  • Story steps load with origin: 'story'. If they replace user edits, the edits are kept: the Workspace shows Restore my edits, and the story shows a note linking there.
  • Share link (#/workspace?d=<token>) encodes the canonical request and compresses it with deflate-raw and base64url where available, with a plain JSON fallback. The AES design with the shared-sum circuit is about 450 characters.
    • Opening a link validates it like an import and also requires the design and circuit to pass validation. Token and decoded sizes are capped.
    • A valid link loads as one undo step. On a clean workspace a notice offers Undo. If the user has edits, the link keeps them in the same restorable slot story steps use, saved with the workspace, so Restore my edits survives a reload; nothing is overwritten without a way back. A damaged link leaves the workspace unchanged and says why (not JSON, no design, invalid request, too large).
    • While the inputs have problems, Copy share link is disabled and the reason is shown as text next to it.
    • The token is removed from the address after loading, so a reload does not reapply it.

Circuit from matrix and computed-matrix diff (src/lib/circuit.ts, src/components/ComputedMatrix.tsx)

  • naiveCircuit(matrix, field) builds the circuit term by term:
    • each product k·x_j is computed once;
    • a coefficient of 1 uses the input directly and 02 uses xtime;
    • a zero row becomes a multiplication by 0;
    • additions are chained.
  • matrixDiff lists the coefficients that differ.
  • Below the circuit editor, a Computed matrix panel shows the target beside what the circuit computes, recomputed on every edit.
    • Differing cells are marked with a diamond, a frame and screen-reader text, so colour is never the only signal.
    • The panel lists the first differences and is labelled Unchecked preview.
    • Long BN254 values are shortened in the read-only grids and the summary: values just below p as p−1, p−2, …, other long values as the first 6 and last 4 digits. The full value stays in the tooltip and in screen-reader text. Grids may scroll inside their own box, so the page never scrolls sideways at 390 px.
    • Replace circuit with term-by-term writes the term-by-term circuit. It stays a secondary button, and its announcement says Undo brings the previous circuit back.
    • Search for cheaper circuits starts the quick experiment and opens Experiments. On the replay build the button reads Show the recorded search only when a recorded search matches the inputs, and No recorded search for these inputs (disabled, with the reason shown) otherwise. When the circuit does not compute the matrix, the panel says the search starts from the matrix alone and leaves the circuit out of the comparison.
  • The No circuit panel gets Generate from matrix as its primary action, with a visible reason while the matrix has invalid cells.
  • Workspace announcements (Undone, Loaded …, Replaced the circuit …) go through one polite live region that stays mounted and is cleared before each message.

Review fixes

Each reviewer finding was reproduced first and fixed in its own commit:

  • BN254 values such as p−1 widened the page at 390 px (scrollWidth 748): shortened display, wrapping cells, and a Playwright check that scrollWidth equals clientWidth after typing −1 into the Poseidon2 external matrix.
  • Opening a share link overwrote the saved workspace with no way back after a reload: edits are kept for Restore my edits.
  • Focus fell to the page when the paste box closed: it returns to the toggle or the first cell.
  • The toolbar claimed a save before any write: neutral wording until the first write or restore.
  • A pasted run that did not fit reported a full-matrix parse error: its own message.
  • The paste preview grid sat inside a live region, and the announcement region was remounted with its text: summary-only region, and one persistent announcer.
  • The generate button replaced a hand-written circuit silently: renamed, secondary, Undo in the announcement. The diff marker now matches the ◆ the summary names, and the evidence icon keeps its size on phones.
  • The preset chip note used developer language: plain legend at the top of the list.
  • Disabled controls explained themselves only in tooltips: visible reasons tied with aria-describedby.
  • Show the recorded search was enabled for inputs without a recording: label and state follow the recording index.
  • Share-link errors had a double period and blamed instructions for a missing design: fixed wording.
  • Missing tests for pasted runs, preset grouping and the decompression limit: added.

Screenshots

Each scenario was captured in light and dark themes, at 1440 px (desktop) and 390 px (phone), with Playwright against the replay-mode dev server:

Screenshots were taken locally (light and dark, 1440 px and 390 px) and are not attached:

  • 01-workspace-dark-desktop.png
  • 01-workspace-dark-phone.png
  • 01-workspace-light-desktop.png
  • 01-workspace-light-phone.png
  • 02-presets-search-dark-desktop.png
  • 02-presets-search-dark-phone.png
  • 02-presets-search-light-desktop.png
  • 02-presets-search-light-phone.png
  • 03-computed-diff-dark-desktop.png
  • 03-computed-diff-dark-phone.png
  • 03-computed-diff-light-desktop.png
  • 03-computed-diff-light-phone.png
  • 04-generated-dark-desktop.png
  • 04-generated-dark-phone.png
  • 04-generated-light-desktop.png
  • 04-generated-light-phone.png
  • 05-paste-preview-dark-desktop.png
  • 05-paste-preview-dark-phone.png
  • 05-paste-preview-light-desktop.png
  • 05-paste-preview-light-phone.png
  • 06-paste-error-dark-desktop.png
  • 06-paste-error-dark-phone.png
  • 06-paste-error-light-desktop.png
  • 06-paste-error-light-phone.png
  • 07-copy-menu-dark-desktop.png
  • 07-copy-menu-dark-phone.png
  • 07-copy-menu-light-desktop.png
  • 07-copy-menu-light-phone.png
  • 08-toolbar-shared-dark-desktop.png
  • 08-toolbar-shared-dark-phone.png
  • 08-toolbar-shared-light-desktop.png
  • 08-toolbar-shared-light-phone.png
  • 09-share-opened-dark-desktop.png
  • 09-share-opened-dark-phone.png
  • 09-share-opened-light-desktop.png
  • 09-share-opened-light-phone.png
  • 10-story-replaced-dark-desktop.png
  • 10-story-replaced-dark-phone.png
  • 10-story-replaced-light-desktop.png
  • 10-story-replaced-light-phone.png
  • 11-presets-poseidon-dark-desktop.png
  • 11-presets-poseidon-dark-phone.png
  • 11-presets-poseidon-light-desktop.png
  • 11-presets-poseidon-light-phone.png
  • 12-workspace-bn254-dark-desktop.png
  • 12-workspace-bn254-dark-phone.png
  • 12-workspace-bn254-light-desktop.png
  • 12-workspace-bn254-light-phone.png
  • 13-story-kept-edits-dark-desktop.png
  • 13-story-kept-edits-dark-phone.png
  • 13-story-kept-edits-light-desktop.png
  • 13-story-kept-edits-light-phone.png
  • 14-paste-run-overflow-dark-desktop.png
  • 14-paste-run-overflow-dark-phone.png
  • 14-paste-run-overflow-light-desktop.png
  • 14-paste-run-overflow-light-phone.png
  • 15-share-kept-edits-dark-desktop.png
  • 15-share-kept-edits-dark-phone.png
  • 15-share-kept-edits-light-desktop.png
  • 15-share-kept-edits-light-phone.png
  • 16-bn254-p-minus-1-dark-desktop.png
  • 16-bn254-p-minus-1-dark-phone.png
  • 16-bn254-p-minus-1-light-desktop.png
  • 16-bn254-p-minus-1-light-phone.png
  • 17-presets-legend-dark-desktop.png
  • 17-presets-legend-dark-phone.png
  • 17-presets-legend-light-desktop.png
  • 17-presets-legend-light-phone.png
  • 18-disabled-reasons-design-dark-desktop.png
  • 18-disabled-reasons-design-dark-phone.png
  • 18-disabled-reasons-design-light-desktop.png
  • 18-disabled-reasons-design-light-phone.png
  • 18-disabled-reasons-toolbar-dark-desktop.png
  • 18-disabled-reasons-toolbar-dark-phone.png
  • 18-disabled-reasons-toolbar-light-desktop.png
  • 18-disabled-reasons-toolbar-light-phone.png

Scenarios:

  • 01: the workspace with the toolbar.
  • 02: searching "circ".
  • 03: diff after loading circ(02,01,01,01) with the AES circuit (4 of 16 coefficients differ); the search button reads No recorded search for these inputs with its reason.
  • 04: after Replace circuit with term-by-term (all 16 equal).
  • 05 and 06: paste preview with decimal reading, and the inline error.
  • 07: the Copy as menu.
  • 08: link copied.
  • 09: a share link opened in a fresh browser.
  • 10 and 13: kept edits after a story step, in the workspace and in the story.
  • 11: searching "poseidon2 weak".
  • 12: full page with the weak-internal Poseidon2 design.
  • 14: a run of three values pasted at row 4, column 3 is refused with its own message.
  • 15: a share link opened over a workspace with edits, after a reload: Restore my edits is still offered.
  • 16: Poseidon2 t = 3 with −1 typed into external cell (1, 1): the computed matrix shows p−1 and the page does not scroll sideways at 390 px.
  • 17: the preset list with the chip legend at the top.
  • 18: disabled controls with visible reasons (toolbar share link, Copy as).

The script also checked:

  • Keyboard undo and redo: the diff panel moves from 4 differences to 0 and back.
  • Cell normalization: 0x1b becomes 1B, and Ctrl+Z inside a cell restores the previous value.
  • Copy as: the clipboard holds the Rust literal.
  • Persistence: the workspace survives a reload.
  • Share links: the copied link loads in a fresh context, and the token is dropped from the hash. A broken token shows an error and leaves the workspace unchanged.
  • Story edits: restoring the edits a story step replaced brings them back.
  • Focus: after Escape in the paste box, focus is on Paste matrix…; after Use this matrix, on cell (1, 1).
  • Share over edits: cell (1, 1) set to 07, a share link opened, the page reloaded, Restore my edits brings 07 back.
  • Overflow: with p−1 in the computed matrix, scrollWidth equals clientWidth at 390 px and 1440 px (the script fails otherwise).
  • Save status: a fresh visit with empty storage reads Changes are saved in this browser.
  • Console: no errors other than the dev server's refused /api/capabilities proxy calls (no local service running, which is how the replay fallback is exercised).

Tests run

  • npm run build: all workspaces build, including tsc --noEmit and vite build for apps/web.
  • npm test: schema 14/14 and server 42 passed with 5 skipped (unchanged). Web: 21 files, 373 tests passed. New test files:
    • lib/notation.test.ts: every notation, the hex and decimal readings, error messages and cell positions, BN254 reduction, and export round-trips.
    • lib/library.test.ts: every preset applies cleanly to both fields; MDS, invertibility, permutation and equivalence facts are recomputed with lib/preview.ts and branchWitness; checks that v = (0,1,−1) is fixed by the weak internal matrix; search ranking.
    • lib/circuit.test.ts (extended): naiveCircuit on 50 random 4×4 GF(2^8) and 50 random 3×3 BN254 matrices equals the matrix; operation counts; zero and single-term rows; matrixDiff.
    • lib/history.test.ts, lib/debounce.test.ts (fake timers), lib/persist.test.ts (corrupt data and throwing storage), lib/share.test.ts (round-trip with the inputs digest preserved, compression, rejection of damaged, oversized and invalid links), lib/route.test.ts, lib/shortcuts.test.ts.
    • state/actions.test.ts: per-cell coalescing, pastes as separate steps, story snapshot and restore, presets as one step, debounced saving and restore (and the save status), unavailable storage, share-link load and failure, edits kept when a link opens over them and restored after a fresh start, and a link that matches the open inputs.
    • lib/share.test.ts also covers a deflate bomb that would inflate past 128 KB (rejected as too large), a token just under the limit, a bare circuit and a JSON array (no design), and single-period error wording.
    • lib/bn254.test.ts: p−k and 6…4 shortening and displayElement.
    • components/MatrixEditor.test.tsx: pastedCells (spreadsheet rows, quoted values, BN254 negatives, values that are not elements), planCellPaste (fits, overflows, full-matrix text), MatrixView rendering of p−1 with full values in the tooltip and screen-reader text, and describeCells.
    • components/PresetPicker.test.ts: groupedPresets order with and without a query, field-first without a query, and every match listed once.
    • components/WorkspaceToolbar.test.tsx: save-status wording and the visible share-link reason tied by aria-describedby.
  • Each fix commit was also type-checked and tested on its own staged tree.

Scope and limits

  • Changes are confined to apps/web plus the README files; the schema, server, engine and Lean are untouched.
  • Preset facts are quick browser checks, re-run by unit tests. They are not Lean certificates, and the picker says so at the top of the list.
  • Matrices from ciphers over GF(2^4) or other GF(2^8) polynomials (LED, Midori, PRINCE, SKINNY, Twofish, Whirlpool) cannot be represented in aes-like/gf256/v1 and are not listed. The binary circ(00,01,01,01) is labelled as a Midori-style 0/1 pattern, not Midori.
  • The weak Poseidon2 internal matrix passes every check the workbench performs. The preset exists to show that gap; this PR does not add a partial-round subspace check.
  • The generated circuit is a correct baseline, not an optimized one. Searching for cheaper circuits uses the existing quick-experiment flow, which in replay mode finds only recorded runs.
  • Share links hold the full request in the hash, so a custom 256-entry S-box makes a longer link. Compressed links need CompressionStream (current Chromium, Firefox and Safari). Links made without it are plain base64url JSON and open everywhere.
  • Undo history is per tab and is not persisted. Only the inputs and any edits kept from a story step or a share link are saved; one set of kept edits is held at a time, so a share link opened over new edits replaces older kept edits.
  • Width 2 and 3 AES-like designs are still reachable only through JSON import. Presets are width 4.

Generated by Claude Code

Parse hex grids, C and Rust array literals, Python, NumPy and Sage
nested lists and LaTeX matrices into canonical cells, with errors that
name the row and column. Format matrices as JSON, C, Rust, Sage or a
plain grid; every export reads back to the same matrix.
naiveCircuit builds a straight-line program for y = M·x with each
product computed once, and matrixDiff lists the coefficients where a
circuit's matrix differs from its target.
Designs, matrices, S-boxes and circuits over GF(2^8) (0x11B) and BN254,
each with a scope note. Unit tests recompute every MDS, invertibility,
permutation and equivalence fact with lib/preview.ts, so a preset is
listed as MDS only when the computation agrees.
Pure undo/redo with coalesced bursts, a debounce helper, a validated
localStorage format for the workspace, compact share tokens
(deflate-raw and base64url in the URL hash), hash routes with a share
parameter and the undo key bindings.
Workspace edits go through one commit path with bounded undo history:
typing in one cell coalesces, pastes, presets and imports are single
steps. Ctrl/Cmd+Z and Shift+Ctrl/Cmd+Z work outside text areas. The
inputs are saved to localStorage 300 ms after each change and restored
on load. A story step that replaces edits keeps them for restoring, and
links of the form #/workspace?d=... load a shared design.
Pasting a matrix in any supported notation into a cell fills the grid,
a run of values fills from the focused cell, and a paste box previews
the parsed matrix with a hex/decimal choice before applying it. Cells
are normalized on blur. A Copy as menu exports JSON, C, Rust, Sage and
a plain grid. The ad-hoc matrix load buttons give way to the presets.
A searchable presets picker groups designs, matrices, S-boxes and
circuits with scope notes and browser-preview fact chips. The toolbar
adds undo, redo, a share-link button and the save status. Below the
circuit, the coefficient matrix it computes is shown beside the target
with differing coefficients marked, a generator for a term-by-term
circuit and a shortcut into the quick experiment search.
restoreWorkspace only needs getItem; the reader no longer asks for
setItem too.
A value such as p-1 printed all 77 digits in the read-only matrices and
in the diff summary, which pushed the workspace wider than a phone
screen. Read-only cells and the summary now show values just below p as
p-k and other long values as the first 6 and last 4 digits, with the
full value in the tooltip and for screen readers. The grid may also
scroll inside its own box and cells wrap rather than widen the page.
Opening a share link replaced the workspace as an ordinary edit, and the
debounced save then overwrote the saved workspace, so a reload lost the
user's own design with no way back. A share link now keeps unsaved edits
in the same restorable slot a story step uses, saved with the workspace,
and the banner offers Restore my edits or Keep the shared inputs. A link
that matches the open inputs only says so.
Closing the Paste matrix box with Escape or Cancel dropped focus to the
page; it now returns to the Paste matrix button, and Use this matrix
moves focus to the first cell. The paste box's live region holds only a
one-line summary or the error, with the preview matrix outside it, so a
screen reader no longer re-reads every cell on each keystroke. Workspace
announcements go through one live region that stays mounted and clears
before each new message, instead of a region remounted with its text.
The generate button replaced a hand-written circuit without saying so
and turned primary whenever the circuit differed. It now reads Replace
circuit with term-by-term, stays secondary, and its announcement says
Undo brings the previous circuit back. On the static build the search
button reads Show the recorded search only when a recorded search
matches the inputs, and No recorded search for these inputs otherwise;
when the circuit does not compute the matrix, the panel says the search
starts from the matrix and leaves the circuit out. Reasons for disabled
buttons are shown as text tied to the buttons. The diff marker is drawn
as a diamond that matches the summary, and the evidence icon no longer
shrinks on phones.
Copy share link and Copy as explained a disabled state only in a title
tooltip, which touch screens and keyboard focus never show. The reason
is now visible text next to the control and its accessible description.
The toolbar said Saved in this browser on a first visit before anything
was saved; it now reads Changes are saved in this browser until the
first write, or until a saved workspace has been restored.
The note under the preset list called the chips browser previews re-run
by the unit tests, which means little to a cipher designer, and sat
below the fold. A legend at the top now says the dashed chips are quick
checks computed in the browser, not Lean proofs, and that Check gives
evidence; each chip also carries that as a tooltip and a screen-reader
prefix. Tests cover how presets are grouped and ordered with and
without a query.
A link whose contents were not JSON ended in a double period, and a
link holding only a circuit was reported as missing instructions. The
decoder now says the link is damaged when its contents are not JSON,
says the link does not contain a design when there is none, and wraps
other import errors without doubling the period. Tests also guard the
128 KB limit on decompressed links against a small deflate bomb.
Pasting 01 02 03 into row 4, column 3 of a 4x4 matrix reported that
one row was found where four were expected, as if a whole matrix had
been pasted. A run of values that reads cleanly but does not fit from
the focused cell now gets its own message; the matrix parser's error is
kept for text that does not read as a run. Tests cover pasted runs
(spreadsheet rows, quoted values, BN254 negatives, values that do not
fit) and the shortened BN254 display.
Resolve the overlap with the branches merged before it, as in the
release/v0.2.0 integration.
@4waan
4waan merged commit 87dda30 into main Nov 11, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants