File size: 2,760 Bytes
60f4258
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
---

license: mit
tags:
- formal-verification
- coq
- threshold-logic
- neuromorphic
- arithmetic
- adder
---


# tiny-RippleCarry2Bit-verified

Formally verified 2-bit ripple carry adder. Chains two full adders to add two 2-bit numbers with 100% accuracy.

## Architecture

| Component | Value |
|-----------|-------|
| Inputs | 4 (a1, a0, b1, b0) |
| Outputs | 3 (cout, s1, s0) |
| Neurons | 8 (2 full adders × 4 neurons each) |
| Parameters | 24 (2 full adders × 12 params each) |
| Layers | 2 (chained full adders) |
| Activation | Heaviside step |

## Key Properties

- 100% accuracy (16/16 input combinations correct)
- Coq-proven correctness
- Compositional construction from verified full adders
- Produces 3-bit output (sum can be 0-6, requiring 3 bits)
- Compatible with neuromorphic hardware

## Circuit Structure

```

a1 a0    b1 b0

 |  |     |  |

 |  +--+--+  |

 |     |     |

 |   FA0 (cin=0)

 |     |     |

 |    s0    c0

 |     |     |

 +--+--+-----+

    |

   FA1

    |

   s1  c1

```

First full adder adds least significant bits (a0 + b0 + 0), producing sum bit s0 and carry c0. Second full adder adds most significant bits with the carry (a1 + b1 + c0), producing s1 and final carry cout.

## Usage

```python

import torch

from safetensors.torch import load_file



weights = load_file('ripplecarry2bit.safetensors')



def full_adder_sim(a, b, cin):

    sum_out = a ^ b ^ cin

    carry_out = (a & b) | (cin & (a ^ b))

    return sum_out, carry_out



def ripple_carry_2bit(a1, a0, b1, b0):

    s0, c0 = full_adder_sim(a0, b0, 0)

    s1, cout = full_adder_sim(a1, b1, c0)

    return cout, s1, s0



# Test

print(ripple_carry_2bit(1, 1, 1, 0))  # 3 + 2 = 5 -> (1, 0, 1)

print(ripple_carry_2bit(1, 0, 1, 0))  # 2 + 2 = 4 -> (1, 0, 0)

print(ripple_carry_2bit(0, 1, 0, 1))  # 1 + 1 = 2 -> (0, 1, 0)

```

## Verification

**Coq Theorem**:
```coq

Theorem ripple_carry_2bit_correct : forall a1 a0 b1 b0,

  ripple_carry_2bit a1 a0 b1 b0 = ripple_carry_2bit_spec a1 a0 b1 b0.

```

Proven axiom-free via exhaustive case analysis on all 16 input combinations.

Full proof: [coq-circuits/Arithmetic/RippleCarry2Bit.v](https://github.com/CharlesCNorton/coq-circuits/blob/main/coq/Arithmetic/RippleCarry2Bit.v)

## Properties

- **Commutative**: Adding A + B equals B + A
- **Identity**: Adding 0 preserves the value
- **Compositional**: Built from two verified FullAdder circuits

## Citation

```bibtex

@software{tiny_ripplecarry2bit_verified_2025,

  title={tiny-RippleCarry2Bit-verified: Formally Verified 2-Bit Ripple Carry Adder},

  author={Norton, Charles},

  url={https://huggingface.co/phanerozoic/tiny-RippleCarry2Bit-verified},

  year={2025}

}

```