Tiny Verified Logic Circuits
Collection
Formally verified threshold logic circuits. Compatible with neuromorphic hardware.
•
33 items
•
Updated
Formally verified XOR gate. Two-layer threshold network computing exclusive or with 100% accuracy.
| Component | Value |
|---|---|
| Inputs | 2 |
| Outputs | 1 |
| Neurons | 3 (2 hidden, 1 output) |
| Layers | 2 |
| Parameters | 9 |
| Layer 1, Neuron 1 (OR) | |
| Weights | [1, 1] |
| Bias | -1 |
| Layer 1, Neuron 2 (NAND) | |
| Weights | [-1, -1] |
| Bias | 1 |
| Layer 2 (AND) | |
| Weights | [1, 1] |
| Bias | -2 |
| Activation | Heaviside step (all layers) |
XOR is the classic example of a function that is not linearly separable - a single threshold neuron cannot compute it. This network uses the minimal architecture:
Layer 1: Compute OR and NAND in parallel Layer 2: Compute AND of results
This implements: XOR(x,y) = AND(OR(x,y), NAND(x,y))
import torch
from safetensors.torch import load_file
weights = load_file('xor.safetensors')
def xor_gate(x, y):
inputs = torch.tensor([float(x), float(y)])
# Layer 1: OR and NAND
or_sum = (inputs * weights['layer1.neuron1.weight']).sum() + weights['layer1.neuron1.bias']
or_out = int(or_sum >= 0)
nand_sum = (inputs * weights['layer1.neuron2.weight']).sum() + weights['layer1.neuron2.bias']
nand_out = int(nand_sum >= 0)
# Layer 2: AND
layer1_outs = torch.tensor([float(or_out), float(nand_out)])
and_sum = (layer1_outs * weights['layer2.weight']).sum() + weights['layer2.bias']
return int(and_sum >= 0)
# Test
print(xor_gate(0, 0)) # 0
print(xor_gate(0, 1)) # 1
print(xor_gate(1, 0)) # 1
print(xor_gate(1, 1)) # 0
Coq Theorem:
Theorem xor_correct : forall x y, xor_circuit x y = xorb x y.
Proven axiom-free with properties:
Full proof: coq-circuits/Boolean/XOR.v
| Input (x,y) | OR | NAND | AND(OR,NAND) | XOR |
|---|---|---|---|---|
| (0,0) | 0 | 1 | 0 | 0 |
| (0,1) | 1 | 1 | 1 | 1 |
| (1,0) | 1 | 1 | 1 | 1 |
| (1,1) | 1 | 0 | 0 | 0 |
XOR outputs true when inputs differ.
@software{tiny_xor_prover_2025,
title={tiny-XOR-verified: Formally Verified XOR Gate},
author={Norton, Charles},
url={https://huggingface.co/phanerozoic/tiny-XOR-verified},
year={2025}
}