{"challengeId":"matmul-rank-6x6","entry":"verifier.mjs","sha256":"e31b6b4dadab998025f166108be3d4242e0a5862b17ca1d4e4b2d0432c445d8c","scoreDirection":"minimize","scoreLabel":"multiplications (rank R)","seedPolicy":{"mode":"fixed","testCount":1},"source":"// Frozen verifier for matmul-rank-6x6. Self-contained (no imports): it interprets\n// the candidate's bilinear-scheme artifact and returns a Verdict. It never executes\n// agent code — build() already ran in the sandbox and handed us plain data. This\n// file is part of the challenge's PROTECTED surface.\n//\n// Fixed instance: 6x6 matrix multiply over the integers Z, with every scheme\n// coefficient restricted to {-1, 0, 1}. A bilinear scheme of rank R computes\n//   m_r = (u_r . vecX) * (v_r . vecY)        for r in 0..R-1   (one multiplication)\n//   out_c = sum_r w_r[c] * m_r\n// where vecX and vecY are the 36 entries of the two 6x6 inputs flattened\n// row-major (slot a = i*6+k holds X[i][k]; slot b = k*6+j holds Y[k][j]) and the\n// 36 outputs out_c (c = i*6+j) are the entries of the 6x6 product:\n//   out_{i,j} = sum_k X[i][k] * Y[k][j].\n//\n// Correctness is an EXACT tensor identity over the integers. The bilinear map a\n// scheme computes has coefficient, on monomial X_a * Y_b in output c,\n//   S[a][b][c] = sum_r u_r[a] * v_r[b] * w_r[c].\n// The 6x6 matmul tensor T[a][b][c] is 1 exactly on the (a,b,c) triples where\n// a=(i,k), b=(k,j), c=(i,j) share i,j,k, and 0 otherwise. We recompute all\n// 36*36*36 = 46656 coefficients with integer arithmetic and require S === T\n// everywhere. This is a total proof over the finitely many monomials — nothing\n// to sample — so seedPolicy.mode is \"fixed\".\n\nconst N = 6; // matrix order\nconst D = N * N; // 36 inputs per matrix, 36 outputs\nconst MAX_RANK = 216; // naive scheme is 216 (6^3); sub-216 is the beat\nconst MAX_COEFF = 1; // coefficients restricted to {-1, 0, 1}\n\nfunction gate(name, pass, detail) {\n  return { name, pass, detail };\n}\nfunction bad(name, detail) {\n  return { ok: false, score: null, gates: [gate(name, false, detail)], behavior: {}, logs: [] };\n}\n\n// The 6x6 matrix-multiplication tensor as a dense 36x36x36 array of {0,1}, stored\n// sparsely as the set of \"on\" (a,b,c) triples for an O(rank) inner check.\nfunction targetOnTriples() {\n  const on = [];\n  for (let i = 0; i < N; i++) {\n    for (let j = 0; j < N; j++) {\n      const c = i * N + j; // output slot (row i, col j)\n      for (let k = 0; k < N; k++) {\n        const a = i * N + k; // x_{i,k}\n        const b = k * N + j; // y_{k,j}\n        on.push([a, b, c]);\n      }\n    }\n  }\n  return on;\n}\n\nfunction isCoeffVec(vec, len) {\n  if (!Array.isArray(vec) || vec.length !== len) return false;\n  for (const v of vec) {\n    if (!Number.isInteger(v) || v < -MAX_COEFF || v > MAX_COEFF) return false;\n  }\n  return true;\n}\n\nexport function verify(ctx) {\n  const sol = ctx.solution;\n  if (!sol || typeof sol !== 'object') {\n    return bad('structure', 'expected { rank, factors }');\n  }\n  const { rank, factors } = sol;\n  if (!Number.isInteger(rank) || rank < 1 || rank > MAX_RANK) {\n    return bad('structure', `rank must be an integer in 1..${MAX_RANK}`);\n  }\n  if (!Array.isArray(factors) || factors.length !== rank) {\n    return bad('structure', `factors must be an array of ${rank} terms`);\n  }\n  for (let r = 0; r < rank; r++) {\n    const f = factors[r];\n    if (!f || typeof f !== 'object') return bad('structure', `factors[${r}] must be { u, v, w }`);\n    if (!isCoeffVec(f.u, D)) return bad('structure', `factors[${r}].u must be ${D} integers in {-1,0,1}`);\n    if (!isCoeffVec(f.v, D)) return bad('structure', `factors[${r}].v must be ${D} integers in {-1,0,1}`);\n    if (!isCoeffVec(f.w, D)) return bad('structure', `factors[${r}].w must be ${D} integers in {-1,0,1}`);\n  }\n\n  // Recompute the bilinear coefficient tensor S and compare to the target T.\n  // We accumulate S densely as a Map keyed by the packed (a,b,c) index from the\n  // scheme's own support, then check every target \"on\" triple equals 1 and every\n  // other accumulated coefficient equals 0. This is exact integer arithmetic over\n  // all 46656 monomials with no sampling.\n  const S = new Map(); // key a*D*D + b*D + c -> integer coefficient\n  for (let r = 0; r < rank; r++) {\n    const { u, v, w } = factors[r];\n    // record the sparse support of u, v, w to avoid the full 36^3 dense triple-loop\n    const ua = [];\n    for (let a = 0; a < D; a++) if (u[a] !== 0) ua.push(a);\n    const vb = [];\n    for (let b = 0; b < D; b++) if (v[b] !== 0) vb.push(b);\n    const wc = [];\n    for (let c = 0; c < D; c++) if (w[c] !== 0) wc.push(c);\n    for (const a of ua) {\n      const ua_v = u[a];\n      const baseA = a * D * D;\n      for (const b of vb) {\n        const uv = ua_v * v[b];\n        const baseAB = baseA + b * D;\n        for (const c of wc) {\n          const key = baseAB + c;\n          const add = uv * w[c];\n          S.set(key, (S.get(key) || 0) + add);\n        }\n      }\n    }\n  }\n\n  // Check every target \"on\" triple has accumulated coefficient exactly 1.\n  const on = targetOnTriples();\n  let mismatch = '';\n  const onKeys = new Set();\n  for (const [a, b, c] of on) {\n    const key = a * D * D + b * D + c;\n    onKeys.add(key);\n    const got = S.get(key) || 0;\n    if (got !== 1) {\n      mismatch = `monomial X${a}*Y${b} in output ${c}: scheme gives ${got}, want 1`;\n      break;\n    }\n  }\n  // Check every accumulated coefficient OUTSIDE the target support is exactly 0.\n  if (!mismatch) {\n    for (const [key, val] of S) {\n      if (val !== 0 && !onKeys.has(key)) {\n        const a = (key / (D * D)) | 0;\n        const b = ((key % (D * D)) / D) | 0;\n        const c = key % D;\n        mismatch = `monomial X${a}*Y${b} in output ${c}: scheme gives ${val}, want 0`;\n        break;\n      }\n    }\n  }\n\n  const computesMatmul = !mismatch;\n  const gates = [\n    gate(\n      'computes-6x6-matmul',\n      computesMatmul,\n      mismatch || `all ${D * D * D} tensor coefficients match the 6x6 matmul map`,\n    ),\n  ];\n  const ok = gates.every((g) => g.pass);\n  return {\n    ok,\n    score: ok ? rank : null,\n    gates,\n    behavior: { rank },\n    logs: [`rank=${rank} coefficients in {-1,0,1}`],\n  };\n}\n"}