|
|
--- |
|
|
license: mit |
|
|
tags: |
|
|
- formal-verification |
|
|
- coq |
|
|
- threshold-logic |
|
|
- neuromorphic |
|
|
- modular-arithmetic |
|
|
--- |
|
|
|
|
|
# tiny-mod4-verified |
|
|
|
|
|
Formally verified MOD-4 circuit. Single-layer threshold network computing modulo-4 arithmetic with 100% accuracy. |
|
|
|
|
|
## Architecture |
|
|
|
|
|
| Component | Value | |
|
|
|-----------|-------| |
|
|
| Inputs | 8 | |
|
|
| Outputs | 1 (per residue class) | |
|
|
| Neurons | 4 (one per residue 0-3) | |
|
|
| Parameters | 36 (4 × 9) | |
|
|
| Weights | [1, 1, 1, -3, 1, 1, 1, -3] | |
|
|
| Bias | 0 | |
|
|
| Activation | Heaviside step | |
|
|
|
|
|
## Key Properties |
|
|
|
|
|
- 100% accuracy (256/256 inputs correct) |
|
|
- Coq-proven correctness |
|
|
- Algebraic weight pattern: (1, 1, 1, 1-m) repeating |
|
|
- Computes Hamming weight mod 4 |
|
|
- Compatible with neuromorphic hardware |
|
|
|
|
|
## Algebraic Pattern |
|
|
|
|
|
MOD-4 uses the repeating pattern `[1, 1, 1, -3]`: |
|
|
- Positions 1-3: weight = 1 |
|
|
- Position 4: weight = 1-4 = -3 |
|
|
- Positions 5-7: weight = 1 |
|
|
- Position 8: weight = 1-4 = -3 |
|
|
|
|
|
This creates a cumulative sum that cycles mod 4. |
|
|
|
|
|
## Usage |
|
|
|
|
|
```python |
|
|
import torch |
|
|
from safetensors.torch import load_file |
|
|
|
|
|
weights = load_file('mod4.safetensors') |
|
|
|
|
|
def mod4_circuit(bits): |
|
|
# bits: list of 8 binary values |
|
|
inputs = torch.tensor([float(b) for b in bits]) |
|
|
weighted_sum = (inputs * weights['weight']).sum() + weights['bias'] |
|
|
# Output represents cumulative sum mod 4 |
|
|
return weighted_sum.item() |
|
|
|
|
|
# Test |
|
|
print(mod4_circuit([1,0,0,0,0,0,0,0])) # 1 mod 4 = 1 |
|
|
print(mod4_circuit([1,1,1,1,0,0,0,0])) # 4 mod 4 = 0 |
|
|
print(mod4_circuit([1,1,1,1,1,0,0,0])) # 5 mod 4 = 1 |
|
|
``` |
|
|
|
|
|
## Verification |
|
|
|
|
|
**Coq Theorem**: |
|
|
```coq |
|
|
Theorem mod4_correct_residue_0 : forall x0 x1 x2 x3 x4 x5 x6 x7, |
|
|
mod4_is_zero [x0; x1; x2; x3; x4; x5; x6; x7] = |
|
|
Z.eqb ((Z.of_nat (hamming_weight [x0; x1; x2; x3; x4; x5; x6; x7])) mod 4) 0. |
|
|
``` |
|
|
|
|
|
Proven axiom-free using: |
|
|
1. **Algebraic correctness**: Weight pattern proven to maintain mod-4 invariant |
|
|
2. **Universal quantification**: Verified for all 8-bit inputs |
|
|
3. **Parametric instantiation**: Instantiates `mod_m_weights_8` with m=4 |
|
|
|
|
|
Full proof: [coq-circuits/Modular/Mod4.v](https://github.com/CharlesCNorton/coq-circuits/blob/main/coq/Modular/Mod4.v) |
|
|
|
|
|
## Residue Distribution |
|
|
|
|
|
For 8-bit inputs (256 total): |
|
|
- Residue 0: 72 inputs |
|
|
- Residue 1: 64 inputs |
|
|
- Residue 2: 56 inputs |
|
|
- Residue 3: 64 inputs |
|
|
|
|
|
## Citation |
|
|
|
|
|
```bibtex |
|
|
@software{tiny_mod4_verified_2025, |
|
|
title={tiny-mod4-verified: Formally Verified MOD-4 Circuit}, |
|
|
author={Norton, Charles}, |
|
|
url={https://huggingface.co/phanerozoic/tiny-mod4-verified}, |
|
|
year={2025} |
|
|
} |
|
|
``` |
|
|
|