{"challengeId":"matmul-rank","entry":"verifier.mjs","sha256":"70418539f0230ed1fcb40bc058fe51d41c6a5a5e9177ce8bb77d0bab2dc313a4","scoreDirection":"minimize","scoreLabel":"multiplications","seedPolicy":{"mode":"fixed","testCount":1},"source":"// Frozen verifier for matmul-rank. Self-contained (no imports): it interprets the\r\n// candidate's bilinear-scheme artifact and returns a Verdict. It never executes\r\n// agent code — build() already ran in the sandbox and handed us plain data. This\r\n// file is part of the challenge's PROTECTED surface.\r\n//\r\n// Fixed instance: 2x2 matrix multiply over the integers Z, with every scheme\r\n// coefficient restricted to {-1, 0, 1}. A bilinear scheme of rank R computes\r\n//   m_r = (A_r . vecX) * (B_r . vecY)        for r in 0..R-1\r\n//   out_k = sum_r C[k][r] * m_r\r\n// where vecX = [x00, x01, x10, x11], vecY = [y00, y01, y10, y11], and the four\r\n// outputs are the entries of the 2x2 product:\r\n//   out0 = x00*y00 + x01*y10   (c00)\r\n//   out1 = x00*y01 + x01*y11   (c01)\r\n//   out2 = x10*y00 + x11*y10   (c10)\r\n//   out3 = x10*y01 + x11*y11   (c11)\r\n//\r\n// Correctness is an EXACT tensor identity over the integers. The bilinear map a\r\n// scheme computes has coefficient, on monomial X_a * Y_b in output k,\r\n//   S[a][b][k] = sum_r A[r][a] * B[r][b] * C[k][r].\r\n// The 2x2 matmul tensor T[a][b][k] is 1 exactly on the eight (a,b,k) triples\r\n// above and 0 otherwise. We recompute all 4*4*4 = 64 coefficients with integer\r\n// arithmetic and require S === T everywhere. This is a total proof over the\r\n// finitely many monomials — nothing to sample — so seedPolicy.mode is \"fixed\".\r\n\r\nconst N = 2; // matrix order\r\nconst D = N * N; // 4 inputs per matrix, 4 outputs\r\nconst MAX_RANK = 64; // generous cap; the naive scheme is 8, Strassen is 7\r\n\r\nfunction gate(name, pass, detail) {\r\n  return { name, pass, detail };\r\n}\r\nfunction bad(name, detail) {\r\n  return { ok: false, score: null, gates: [gate(name, false, detail)], behavior: {}, logs: [] };\r\n}\r\n\r\n// The 2x2 matrix-multiplication tensor as a dense 4x4x4 array of {0,1}.\r\nfunction targetTensor() {\r\n  const T = Array.from({ length: D }, () => Array.from({ length: D }, () => new Array(D).fill(0)));\r\n  // out_k = sum over (i,l) of Xrow[i,l] * Yrow[l,j]; encode with a,b,k indices.\r\n  for (let i = 0; i < N; i++) {\r\n    for (let j = 0; j < N; j++) {\r\n      const k = i * N + j; // output slot (row i, col j)\r\n      for (let l = 0; l < N; l++) {\r\n        const a = i * N + l; // x_{i,l}\r\n        const b = l * N + j; // y_{l,j}\r\n        T[a][b][k] = 1;\r\n      }\r\n    }\r\n  }\r\n  return T;\r\n}\r\n\r\nfunction isCoeffRow(row, len) {\r\n  if (!Array.isArray(row) || row.length !== len) return false;\r\n  for (const v of row) {\r\n    if (v !== -1 && v !== 0 && v !== 1) return false;\r\n  }\r\n  return true;\r\n}\r\n\r\nexport function verify(ctx) {\r\n  const sol = ctx.solution;\r\n  if (!sol || typeof sol !== 'object') {\r\n    return bad('structure', 'expected { rank, A, B, C }');\r\n  }\r\n  const { rank, A, B, C } = sol;\r\n  if (!Number.isInteger(rank) || rank < 1 || rank > MAX_RANK) {\r\n    return bad('structure', `rank must be an integer in 1..${MAX_RANK}`);\r\n  }\r\n  if (!Array.isArray(A) || A.length !== rank || !Array.isArray(B) || B.length !== rank) {\r\n    return bad('structure', `A and B must each be ${rank} rows of ${D} coefficients`);\r\n  }\r\n  if (!Array.isArray(C) || C.length !== D) {\r\n    return bad('structure', `C must be ${D} rows (one per output) of ${rank} coefficients`);\r\n  }\r\n  for (let r = 0; r < rank; r++) {\r\n    if (!isCoeffRow(A[r], D)) return bad('structure', `A[${r}] must be ${D} coefficients in {-1,0,1}`);\r\n    if (!isCoeffRow(B[r], D)) return bad('structure', `B[${r}] must be ${D} coefficients in {-1,0,1}`);\r\n  }\r\n  for (let k = 0; k < D; k++) {\r\n    if (!isCoeffRow(C[k], rank)) return bad('structure', `C[${k}] must be ${rank} coefficients in {-1,0,1}`);\r\n  }\r\n\r\n  // Recompute the bilinear coefficient tensor S and compare to the target T.\r\n  const T = targetTensor();\r\n  let mismatch = '';\r\n  for (let a = 0; a < D && !mismatch; a++) {\r\n    for (let b = 0; b < D && !mismatch; b++) {\r\n      for (let k = 0; k < D && !mismatch; k++) {\r\n        let s = 0;\r\n        for (let r = 0; r < rank; r++) {\r\n          s += A[r][a] * B[r][b] * C[k][r];\r\n        }\r\n        if (s !== T[a][b][k]) {\r\n          mismatch = `monomial X${a}*Y${b} in output ${k}: scheme gives ${s}, want ${T[a][b][k]}`;\r\n        }\r\n      }\r\n    }\r\n  }\r\n\r\n  const computesMatmul = !mismatch;\r\n  const gates = [\r\n    gate(\r\n      'computes-2x2-matmul',\r\n      computesMatmul,\r\n      mismatch || `all ${D * D * D} tensor coefficients match the 2x2 matmul map`,\r\n    ),\r\n  ];\r\n  const ok = gates.every((g) => g.pass);\r\n  return {\r\n    ok,\r\n    score: ok ? rank : null,\r\n    gates,\r\n    behavior: { rank, products: rank },\r\n    logs: [`rank=${rank} coefficients in {-1,0,1}`],\r\n  };\r\n}\r\n"}