tiny-mod12-verified / README.md
phanerozoic's picture
Upload README.md with huggingface_hub
2a3fb50 verified
metadata
license: mit
tags:
  - formal-verification
  - coq
  - threshold-logic
  - neuromorphic
  - modular-arithmetic

tiny-mod12-verified

Formally verified MOD-12 circuit. Single-layer threshold network computing modulo-12 arithmetic with 100% accuracy.

Architecture

Component Value
Inputs 8
Outputs 1 (per residue class)
Neurons 12 (one per residue 0-11)
Parameters 108 (12 × 9)
Weights [1, 1, 1, 1, 1, 1, 1, 1]
Bias 0
Activation Heaviside step

Key Properties

  • 100% accuracy (256/256 inputs correct)
  • Coq-proven correctness
  • All-ones weight pattern (m > input width)
  • Computes Hamming weight mod 12
  • Compatible with neuromorphic hardware

Algebraic Pattern

MOD-12 uses all-ones weights because the reset position (position 12) is beyond the 8-bit input width:

  • All positions 1-8: weight = 1
  • Position 12 (beyond input): would be weight = 1-12 = -11

The circuit tracks cumulative sum mod 12 using the Hamming weight directly.

Usage

import torch
from safetensors.torch import load_file

weights = load_file('mod12.safetensors')

def mod12_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']
    return int(weighted_sum.item()) % 12

# Test
print(mod12_circuit([1,1,1,1,1,1,1,1]))  # 8 mod 12 = 8
print(mod12_circuit([1,1,1,1,0,0,0,0]))  # 4 mod 12 = 4

Verification

Coq Theorem:

Theorem mod12_correct_residue_0 : forall x0 x1 x2 x3 x4 x5 x6 x7,
  mod12_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 12) 0.

Proven axiom-free using algebraic weight patterns.

Full proof: coq-circuits/Modular/Mod12.v

Residue Distribution

For 8-bit inputs (256 total), limited to residues 0-8:

  • Residue 0: 1 inputs
  • Residue 1: 8 inputs
  • Residue 2: 28 inputs
  • Residue 3: 56 inputs
  • Residue 4: 70 inputs
  • Residue 5: 56 inputs
  • Residue 6: 28 inputs
  • Residue 7: 8 inputs
  • Residue 8: 1 inputs
  • Residues 9-11: 0 inputs (unreachable with 8-bit input)

Citation

@software{tiny_mod12_verified_2025,
  title={tiny-mod12-verified: Formally Verified MOD-12 Circuit},
  author={Norton, Charles},
  url={https://huggingface.co/phanerozoic/tiny-mod12-verified},
  year={2025}
}