Documentation

Complexitylib.Circuits.Encoding.Internal.Codec

Correctness of the machine-facing circuit codec #

This internal module proves that the proof-free encoding and iterative evaluator in Encoding.Defs faithfully enforce their advertised syntactic invariants. Semantic agreement with typed circuit evaluation is deliberately kept in a separate proof layer.

@[simp]

The unary encoding of n uses exactly n + 1 bits (n trues and a false).

@[simp]

A unary field can be decoded in front of an arbitrary suffix.

theorem Complexity.CircuitCode.NatCode.decodePrefix?_eq_some_iff (bits : List Bool) (n : ) (suffix : List Bool) :
decodePrefix? bits = some (n, suffix) bits = encode n ++ suffix

Successful unary prefix decoding reconstructs the consumed input exactly.

@[simp]
theorem Complexity.CircuitCode.RawGate.isWellFormedAt_eq_true (gate : RawGate) (available : ) :
gate.isWellFormedAt available = true gate.WellFormedAt available

The Boolean well-formedness check agrees with the WellFormedAt predicate.

@[simp]

Decoding a gate's operation bit recovers its operation.

@[simp]

A gate encoding uses five header/delimiter bits plus one bit per unary input reference.

@[simp]

A gate can be decoded in front of an arbitrary suffix.

theorem Complexity.CircuitCode.RawGate.decodePrefix?_eq_some_iff (bits : List Bool) (gate : RawGate) (suffix : List Bool) :
decodePrefix? bits = some (gate, suffix) bits = gate.encode ++ suffix

Successful gate-prefix decoding reconstructs the consumed input exactly.

@[simp]

The Boolean well-formedness check agrees with the WellFormed predicate.

@[simp]

Parsing an encoded gate list consumes exactly that list and leaves the caller-supplied suffix untouched.

@[simp]

A circuit prefix can be decoded in front of an arbitrary suffix.

@[simp]

Exact decoding is a left inverse of circuit serialization.

Exact decoding rejects any nonempty suffix after a canonical encoding.

@[simp]

A circuit encoding consists of the unary gate count followed by the concatenated gate encodings.

Topological well-formedness of a nonempty gate list splits at its head.

theorem Complexity.CircuitCode.RawCircuit.decodeGates?_eq_some_iff (count : ) (bits : List Bool) (circuit : RawCircuit) (suffix : List Bool) :
decodeGates? count bits = some (circuit, suffix) List.length circuit = count bits = List.flatMap RawGate.encode circuit ++ suffix

Successful fixed-count gate decoding reconstructs the consumed input.

theorem Complexity.CircuitCode.RawCircuit.decodePrefix?_eq_some_iff (bits : List Bool) (circuit : RawCircuit) (suffix : List Bool) :
decodePrefix? bits = some (circuit, suffix) bits = circuit.encode ++ suffix

Successful circuit-prefix decoding reconstructs its canonical encoding.

Exact decoding succeeds precisely on canonical encodings.

Running the iterative evaluator succeeds exactly for topological gate lists.

theorem Complexity.CircuitCode.RawCircuit.evalAux?_size {circuit : RawCircuit} {wires result : Array Bool} (h : circuit.evalAux? wires = some result) :
result.size = wires.size + List.length circuit

Successful iterative evaluation appends exactly one wire per gate.

Raw evaluation succeeds precisely for nonempty topologically ordered circuits.

theorem Complexity.CircuitCode.evalCode_isSome_iff (N : ) (code input : List Bool) :
(evalCode N code input).isSome = true input.length = N ∃ (circuit : RawCircuit), code = circuit.encode RawCircuit.WellFormed N circuit

Code evaluation succeeds exactly when the input length is the declared arity, the code is canonical, and the decoded raw circuit is well formed.

@[simp]

The first raw reference is the first typed input wire.

@[simp]

The second raw reference is the second typed input wire.

Erasing a typed gate's proofs never introduces an out-of-range reference.

@[simp]

Translating a typed single-output circuit produces one raw gate per internal gate, followed by its output gate.

Translation preserves the typed circuit's topological ordering.

Translation of a typed circuit is a valid raw single-output circuit.

theorem Complexity.CircuitCode.RawCircuit.encode_length_le (N G : ) (circuit : RawCircuit) (hlen : List.length circuit = G) (hwell : TopologicallyWellFormed N circuit) :
circuit.encode.length G + 1 + G * (2 * (N + G) + 5)

A generic topological raw circuit with G gates and input arity N has quadratic-size unary encoding.

A code produced from a typed circuit is evaluable exactly on inputs of the circuit's declared arity.

theorem Complexity.CircuitCode.encodeCircuit_length_le {N G : } [NeZero N] (c : Circuit Basis.andOr2 N 1 G) :
(encodeCircuit c).length G + 1 + 1 + (G + 1) * (2 * (N + (G + 1)) + 5)

The unary encoding of a typed G-internal-gate circuit has a concrete quadratic length bound.

In the library's size convention, which counts internal and output gates but not primary inputs or free negations, unary circuit codes have quadratic length in the input arity and circuit size.