{"challengeId":"circuit-min","entry":"verifier.mjs","sha256":"d7b8b501d2525b1cbffa67222839da3f26fc66e36479b90988b69cf7b1bbefb0","scoreDirection":"minimize","scoreLabel":"weighted circuit cost","seedPolicy":{"mode":"fixed","testCount":16},"source":"// Frozen verifier for circuit-min. Self-contained (no imports): it interprets\n// the candidate's netlist artifact and returns a Verdict. It never executes\n// agent code — the candidate's build() already ran in the sandbox and handed us\n// plain data. This file is part of the challenge's PROTECTED surface.\n\nconst N_IN = 4; // a0, a1, b0, b1\nconst N_OUT = 4; // p0, p1, p2, p3\nconst MAX_WIDTH = 64;\nconst MAX_GATES = 256;\nconst MAX_CONTROLS = 8;\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// Cost of a multi-controlled-X with k controls. k<=1 (NOT/CNOT) is cheap; a\n// Toffoli (k=2) costs 7 (its T-count), and each extra control adds two more\n// Toffolis' worth — multi-controlled gates are expensive, just like real life.\nfunction gateCost(k) {\n  if (k <= 1) return 1;\n  return (2 * (k - 1) - 1) * 7;\n}\n\nfunction isWireList(arr, width) {\n  if (!Array.isArray(arr)) return false;\n  const seen = new Set();\n  for (const w of arr) {\n    if (!Number.isInteger(w) || w < 0 || w >= width || seen.has(w)) return false;\n    seen.add(w);\n  }\n  return true;\n}\n\nexport function verify(ctx) {\n  const sol = ctx.solution;\n  if (!sol || typeof sol !== 'object') return bad('structure', 'solution is not an object');\n\n  const { width, inputs, outputs, gates } = sol;\n  if (!Number.isInteger(width) || width < N_IN + N_OUT || width > MAX_WIDTH) {\n    return bad('structure', `width must be an integer in [${N_IN + N_OUT}, ${MAX_WIDTH}]`);\n  }\n  if (!isWireList(inputs, width) || inputs.length !== N_IN) {\n    return bad('structure', `inputs must be ${N_IN} distinct wires in range`);\n  }\n  if (!isWireList(outputs, width) || outputs.length !== N_OUT) {\n    return bad('structure', `outputs must be ${N_OUT} distinct wires in range`);\n  }\n  if (inputs.some((w) => outputs.includes(w))) {\n    return bad('structure', 'inputs and outputs must be disjoint');\n  }\n  if (!Array.isArray(gates) || gates.length > MAX_GATES) {\n    return bad('structure', `gates must be an array of length <= ${MAX_GATES}`);\n  }\n  for (let i = 0; i < gates.length; i++) {\n    const g = gates[i];\n    if (!g || typeof g !== 'object') return bad('structure', `gate ${i} is not an object`);\n    if (!Number.isInteger(g.t) || g.t < 0 || g.t >= width) {\n      return bad('structure', `gate ${i} target out of range`);\n    }\n    const c = g.c ?? [];\n    if (!isWireList(c, width) || c.length > MAX_CONTROLS) {\n      return bad('structure', `gate ${i} controls must be distinct wires in range (<= ${MAX_CONTROLS})`);\n    }\n    if (c.includes(g.t)) return bad('structure', `gate ${i} target is also a control`);\n  }\n\n  const outSet = new Set(outputs);\n\n  // Functional + clean-ancilla over all 16 input rows.\n  for (let combo = 0; combo < 16; combo++) {\n    const a0 = combo & 1;\n    const a1 = (combo >> 1) & 1;\n    const b0 = (combo >> 2) & 1;\n    const b1 = (combo >> 3) & 1;\n\n    const reg = new Array(width).fill(0);\n    const init = new Array(width).fill(0);\n    reg[inputs[0]] = a0;\n    reg[inputs[1]] = a1;\n    reg[inputs[2]] = b0;\n    reg[inputs[3]] = b1;\n    init[inputs[0]] = a0;\n    init[inputs[1]] = a1;\n    init[inputs[2]] = b0;\n    init[inputs[3]] = b1;\n\n    for (const g of gates) {\n      const c = g.c ?? [];\n      let on = 1;\n      for (const w of c) on &= reg[w];\n      reg[g.t] ^= on;\n    }\n\n    const a = a0 + 2 * a1;\n    const b = b0 + 2 * b1;\n    const product = a * b;\n    for (let k = 0; k < N_OUT; k++) {\n      const want = (product >> k) & 1;\n      if (reg[outputs[k]] !== want) {\n        return {\n          ok: false,\n          score: null,\n          gates: [gate('functional', false, `a=${a} b=${b}: bit ${k} wrong (got ${reg[outputs[k]]}, want ${want})`)],\n          behavior: {},\n          logs: [],\n        };\n      }\n    }\n    for (let w = 0; w < width; w++) {\n      if (outSet.has(w)) continue;\n      if (reg[w] !== init[w]) {\n        return {\n          ok: false,\n          score: null,\n          gates: [gate('clean-ancilla', false, `a=${a} b=${b}: wire ${w} dirty (got ${reg[w]}, want ${init[w]})`)],\n          behavior: {},\n          logs: [],\n        };\n      }\n    }\n  }\n\n  let cost = 0;\n  for (const g of gates) cost += gateCost((g.c ?? []).length);\n  const score = cost * width;\n\n  return {\n    ok: true,\n    score,\n    gates: [\n      gate('structure', true, `${gates.length} gates, width ${width}`),\n      gate('functional', true, 'all 16 products correct'),\n      gate('clean-ancilla', true, 'all non-output wires return to 0'),\n    ],\n    behavior: { gates: gates.length, width },\n    logs: [`cost=${cost} width=${width} score=${score}`],\n  };\n}\n"}