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 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
- arithmetic
|
| 9 |
+
- adder
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# tiny-FullAdder-verified
|
| 13 |
+
|
| 14 |
+
Formally verified full adder circuit. Adds three 1-bit inputs (a, b, carry_in), producing sum and carry_out outputs with 100% accuracy.
|
| 15 |
+
|
| 16 |
+
## Architecture
|
| 17 |
+
|
| 18 |
+
| Component | Value |
|
| 19 |
+
|-----------|-------|
|
| 20 |
+
| Inputs | 3 (a, b, carry_in) |
|
| 21 |
+
| Outputs | 2 (sum, carry_out) |
|
| 22 |
+
| Neurons | 9 (2 HalfAdders + OR) |
|
| 23 |
+
| Parameters | 27 |
|
| 24 |
+
| Depth | 3 layers |
|
| 25 |
+
| Activation | Heaviside step |
|
| 26 |
+
|
| 27 |
+
## Key Properties
|
| 28 |
+
|
| 29 |
+
- 100% accuracy (8/8 input combinations correct)
|
| 30 |
+
- Coq-proven correctness
|
| 31 |
+
- Compositional design: 2 HalfAdders + OR gate
|
| 32 |
+
- Building block for ripple-carry adders
|
| 33 |
+
- Compatible with neuromorphic hardware
|
| 34 |
+
|
| 35 |
+
## Circuit Design
|
| 36 |
+
|
| 37 |
+
Composed from two half adders and an OR gate:
|
| 38 |
+
|
| 39 |
+
```
|
| 40 |
+
HalfAdder1: (a, b) -> (s1, c1)
|
| 41 |
+
HalfAdder2: (s1, carry_in) -> (sum, c2)
|
| 42 |
+
carry_out = OR(c1, c2)
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
**Components:**
|
| 46 |
+
- 2 HalfAdders: 4 neurons each (XOR + AND)
|
| 47 |
+
- 1 OR gate: 1 neuron
|
| 48 |
+
- Total: 9 neurons, 27 parameters, depth 3
|
| 49 |
+
|
| 50 |
+
## Usage
|
| 51 |
+
|
| 52 |
+
```python
|
| 53 |
+
import torch
|
| 54 |
+
from safetensors.torch import load_file
|
| 55 |
+
|
| 56 |
+
weights = load_file('fulladder.safetensors')
|
| 57 |
+
|
| 58 |
+
def heaviside(x):
|
| 59 |
+
return int(x >= 0)
|
| 60 |
+
|
| 61 |
+
def full_adder(a, b, carry_in):
|
| 62 |
+
# HalfAdder1: (a, b) -> (s1, c1)
|
| 63 |
+
inputs_ha1 = torch.tensor([float(a), float(b)])
|
| 64 |
+
|
| 65 |
+
or_out1 = heaviside((inputs_ha1 * weights['ha1.sum.layer1.or.weight']).sum() +
|
| 66 |
+
weights['ha1.sum.layer1.or.bias'])
|
| 67 |
+
nand_out1 = heaviside((inputs_ha1 * weights['ha1.sum.layer1.nand.weight']).sum() +
|
| 68 |
+
weights['ha1.sum.layer1.nand.bias'])
|
| 69 |
+
layer1_outs1 = torch.tensor([float(or_out1), float(nand_out1)])
|
| 70 |
+
s1 = heaviside((layer1_outs1 * weights['ha1.sum.layer2.weight']).sum() +
|
| 71 |
+
weights['ha1.sum.layer2.bias'])
|
| 72 |
+
c1 = heaviside((inputs_ha1 * weights['ha1.carry.weight']).sum() +
|
| 73 |
+
weights['ha1.carry.bias'])
|
| 74 |
+
|
| 75 |
+
# HalfAdder2: (s1, carry_in) -> (sum, c2)
|
| 76 |
+
inputs_ha2 = torch.tensor([float(s1), float(carry_in)])
|
| 77 |
+
|
| 78 |
+
or_out2 = heaviside((inputs_ha2 * weights['ha2.sum.layer1.or.weight']).sum() +
|
| 79 |
+
weights['ha2.sum.layer1.or.bias'])
|
| 80 |
+
nand_out2 = heaviside((inputs_ha2 * weights['ha2.sum.layer1.nand.weight']).sum() +
|
| 81 |
+
weights['ha2.sum.layer1.nand.bias'])
|
| 82 |
+
layer1_outs2 = torch.tensor([float(or_out2), float(nand_out2)])
|
| 83 |
+
sum_out = heaviside((layer1_outs2 * weights['ha2.sum.layer2.weight']).sum() +
|
| 84 |
+
weights['ha2.sum.layer2.bias'])
|
| 85 |
+
c2 = heaviside((inputs_ha2 * weights['ha2.carry.weight']).sum() +
|
| 86 |
+
weights['ha2.carry.bias'])
|
| 87 |
+
|
| 88 |
+
# Carry out: OR(c1, c2)
|
| 89 |
+
carry_inputs = torch.tensor([float(c1), float(c2)])
|
| 90 |
+
carry_out = heaviside((carry_inputs * weights['carry_or.weight']).sum() +
|
| 91 |
+
weights['carry_or.bias'])
|
| 92 |
+
|
| 93 |
+
return sum_out, carry_out
|
| 94 |
+
|
| 95 |
+
# Test
|
| 96 |
+
print(full_adder(0, 0, 0)) # (0, 0)
|
| 97 |
+
print(full_adder(1, 1, 0)) # (0, 1) - 1+1=2, sum=0, carry=1
|
| 98 |
+
print(full_adder(1, 1, 1)) # (1, 1) - 1+1+1=3, sum=1, carry=1
|
| 99 |
+
```
|
| 100 |
+
|
| 101 |
+
## Truth Table
|
| 102 |
+
|
| 103 |
+
| a | b | cin | sum | cout |
|
| 104 |
+
|---|---|-----|-----|------|
|
| 105 |
+
| 0 | 0 | 0 | 0 | 0 |
|
| 106 |
+
| 0 | 0 | 1 | 1 | 0 |
|
| 107 |
+
| 0 | 1 | 0 | 1 | 0 |
|
| 108 |
+
| 0 | 1 | 1 | 0 | 1 |
|
| 109 |
+
| 1 | 0 | 0 | 1 | 0 |
|
| 110 |
+
| 1 | 0 | 1 | 0 | 1 |
|
| 111 |
+
| 1 | 1 | 0 | 0 | 1 |
|
| 112 |
+
| 1 | 1 | 1 | 1 | 1 |
|
| 113 |
+
|
| 114 |
+
## Verification
|
| 115 |
+
|
| 116 |
+
**Coq Theorem**:
|
| 117 |
+
```coq
|
| 118 |
+
Theorem full_adder_correct : forall a b c,
|
| 119 |
+
full_adder a b c = full_adder_spec a b c.
|
| 120 |
+
```
|
| 121 |
+
|
| 122 |
+
Where `full_adder_spec` computes:
|
| 123 |
+
- `sum = a XOR b XOR c`
|
| 124 |
+
- `carry_out = (a AND b) OR (c AND (a XOR b))`
|
| 125 |
+
|
| 126 |
+
Proven axiom-free with verified properties:
|
| 127 |
+
- Commutativity in first two arguments
|
| 128 |
+
- Equivalence to half adder when carry_in = false
|
| 129 |
+
- Exhaustive correctness on all 8 input combinations
|
| 130 |
+
|
| 131 |
+
Full proof: [coq-circuits/Arithmetic/FullAdder.v](https://github.com/CharlesCNorton/coq-circuits/blob/main/coq/Arithmetic/FullAdder.v)
|
| 132 |
+
|
| 133 |
+
## Applications
|
| 134 |
+
|
| 135 |
+
- Building block for multi-bit ripple-carry adders
|
| 136 |
+
- Component of ALU designs
|
| 137 |
+
- Foundational for all integer arithmetic circuits
|
| 138 |
+
- Educational hardware design
|
| 139 |
+
|
| 140 |
+
## Citation
|
| 141 |
+
|
| 142 |
+
```bibtex
|
| 143 |
+
@software{tiny_fulladder_verified_2025,
|
| 144 |
+
title={tiny-FullAdder-verified: Formally Verified Full Adder},
|
| 145 |
+
author={Norton, Charles},
|
| 146 |
+
url={https://huggingface.co/phanerozoic/tiny-FullAdder-verified},
|
| 147 |
+
year={2025}
|
| 148 |
+
}
|
| 149 |
+
```
|