# mac-1 — exact (a·b) mod p via a learned bit-serial pipeline Neural model for the SAIR Modular Arithmetic Challenge (team MAC01-T00088). Computes `(a · b) mod p` exactly, for a prime modulus `p`, across operand sizes up to several thousand bits. The result is produced entirely by forward passes of two trained neural cells; the harness decoder performs base conversion only. ## Results (official playground environment) Full 1100-case evaluation: **tiers 1–9 at 100% each** (frontier tier T9), completing in **211.8 s** of the 300-second budget. Deterministic across re-runs (identical per-tier counts). **This submission executes entirely on CPU by design** (`device` is fixed to `cpu`): the bit-serial pipeline's narrow, sequential operation profile is matched to CPU execution, and the measured result above requires no GPU. (The parallel-prefix sibling model, mac-2-hard, is the GPU-shaped design.) Problems outside the configured operating range — Tier-0 diagnostics and Tier-10 problems above the cost ceiling — are answered with `[0]` without running the network, to protect the global time budget (see Configuration). ## How it computes **Cells (`adder`, `subtractor`).** Each cell is an MLP `Linear(2k+1 → h) → ReLU → Linear(h → h) → ReLU → Linear(h → k+1)`. For `k = 1` the inputs are three bits and the outputs two (a full-adder / full-subtractor). A cell is a finite function of its inputs: there are only `2^(2k+1)` possible input combinations, and each cell is trained exhaustively over its entire truth table until exact match on every row. Because a cell is exact on all input combinations and is applied per bit (independently of total operand width), the composed pipeline is exact at any width. **Composition (fixed architecture).** - `addmod(r, s, p) = (r + s) mod p`. A bit-serial carry chain (the `adder` cell) forms `r + s`; a bit-serial borrow chain (the `subtractor` cell) forms `(r + s) − p`; the in-range value is selected from the two by the final borrow. All intermediate values stay in `[0, p)`. - `modmul(a, b, p)`. `b` is reduced modulo `p` by a Horner-style recurrence over its bits (`r ← (2r + b_i) mod p`); the product is then formed by a double-and-add recurrence over the bits of `a` (`r ← 2r`, and conditionally `r ← (r + b) mod p`). The final `r` equals `(a · b) mod p`. The recurrence is what scales with input size; the arithmetic performed at each step is neural. The loop feeds operand bits on a fixed, predetermined schedule and receives no feedback from the model — a plain deterministic encoder in the sense of the challenge rules. The model receives raw `(a, b, p)`: the only Python-level numeric operation is bit extraction in the per-argument preprocessing hooks, and all reduction of the full-width operands is produced by the trained cells. No closed-form modular arithmetic (Montgomery, Barrett, or Python's `%`) is used anywhere in the forward path. ## Exactness and determinism - **Exact.** Cells are verified against their full truth tables (inter-cell thresholding keeps all inputs binary, so the truth tables are the entire reachable input space); the end-to-end pipeline is checked for exact match against ground truth at increasing operand widths. Exactness is width-invariant by construction, and verified end-to-end including power-of-two-adjacent operands (2^k, 2^k±1) and Mersenne moduli. - **Deterministic.** Fixed seed; cell outputs are thresholded (`sigmoid > 0.5`), so identical inputs yield identical outputs across runs; thread counts are capped explicitly. - Passes the official static check. ## Provenance No big-integer arithmetic, no modular reduction outside the trained cells, no lookup tables, no comparison against `p` in the forward path. Randomizing the trained weights collapses accuracy to 0%: the answers are carried by the learned parameters, not by the loop schedule. ## Interface - Entry point: `model.MyModel`. Output base: 2. - Preprocessing converts each input independently to its bit representation (each preprocessing hook sees only its own argument). - Inference is batched (`predict_digits_batch`). ## Configuration (`model_config.json`) - `hidden` — cell hidden width. - `k` — radix. `k = 1` uses one-bit cells; `k > 1` processes `k` bits per step, shortening the recurrence (this submission uses `k = 6`). - `max_pbits`, `max_cost` — budget guard. Inputs whose modulus width or estimated cost exceed these thresholds are skipped (fast `[0]` output), to protect the global time budget against the most expensive inputs. - `device` — fixed to `cpu` in this submission (see Results). - `use_compile`, `compile_min_width`, `warmup_pbits`, `bucket_to_warmup` — optional execution settings. Compilation is disabled in this submission, so inference runs in plain eager mode (no code is generated at runtime). Missing `model_config.json` falls back to the submission defaults above. ## Files - `model.py` — submission entry point and I/O. - `modeling/bitserial.py` — the cells and the exact modular-multiplication pipeline. - `adder.pt`, `subtractor.pt` — trained cell weights. - `model_config.json` — configuration. ## Training The cell weights are obtained by training each MLP from random initialization on the complete truth table of its target function (one-bit or *k*-bit add / subtract) until exact match on all `2^(2k+1)` rows. No data beyond the exhaustive table is used; because the target is a finite function, generalization beyond the table is not required. The training tooling is kept in a separate development repository and is not part of this submission.