chip_as_code 0.1.0

Semantic Chips β€” deterministic boolean circuits as text DNA. Parse, evaluate, evolve, and prove policy gates with No-Guess constitutional checkpoints.
Documentation
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<div align="center">

# πŸ”² chip_as_code

**Semantic Chips β€” Computation as Text DNA**

[![Crates.io](https://img.shields.io/crates/v/chip_as_code.svg)](https://crates.io/crates/chip_as_code)
[![Documentation](https://docs.rs/chip_as_code/badge.svg)](https://docs.rs/chip_as_code)
[![License](https://img.shields.io/crates/l/chip_as_code.svg)](LICENSE-MIT)

*Deterministic boolean circuits you can read, evolve, and prove.*

[Overview](#overview) β€’
[Installation](#installation) β€’
[Quick Start](#quick-start) β€’
[DNA Format](#dna-format) β€’
[GateBox](#gatebox) β€’
[Evolution](#evolution) β€’
[API](#api)

</div>

---

## Overview

**chip_as_code** implements the [Chip as Code](https://logline.foundation) paradigm: policy logic as compilable, auditable, and evolvable text files.

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    SEMANTIC CHIP                            β”‚
β”‚                                                             β”‚
β”‚   CHIP v0                     β”Œβ”€β”€β”€β”                        β”‚
β”‚   FEATURES n=3          f0───▢│AND│──┐                     β”‚
β”‚   GATES m=1             f1───▢│   β”‚  β”‚    β”Œβ”€β”€β”€β”           β”‚
β”‚   g0 = AND(f0,f1,f2)    f2β”€β”€β”€β–Άβ””β”€β”€β”€β”˜  └───▢│OUT│──▢ 0|1    β”‚
β”‚   OUTPUT = g0                        g0──▢│   β”‚           β”‚
β”‚                                           β””β”€β”€β”€β”˜           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### Key Features

| Feature | Description |
|---------|-------------|
| **πŸ“ Text DNA** | Human-readable `.chip` files define boolean circuits |
| **πŸ” Deterministic** | Same input β†’ same output, always |
| **🧬 Evolvable** | Discrete evolution learns circuits from data |
| **βš–οΈ No-Guess Law** | GateBox enforces constitutional checkpoints |
| **πŸ”— Provable** | BLAKE3 hashes + JSON✯Atomic CIDs for every artifact |

### Part of the LogLine Ecosystem

```toml
[dependencies]
chip_as_code = "0.1"   # This crate
logline = "0.1"        # TDLN + JSON✯Atomic bundle
ubl-ledger = "0.1"     # Append-only canonical ledger
```

> All crates by [danvoulez]https://crates.io/users/danvoulez

---

## Installation

### As a library

```toml
[dependencies]
chip_as_code = "0.1"
```

### As a CLI

```bash
cargo install chip_as_code
```

---

## Quick Start

### 30 seconds to your first chip

```bash
# Run the demo (commit, ghost, reject scenarios)
chip demo

# Evolve a chip that learns XOR
chip evolve --task xor --seed 1337 --generations 100

# Verify the result
chip replay --task xor --seed 1337 --chip out/best_chip.txt

# Check the golden reference
chip replay --task xor --seed 1337 --chip examples/xor_golden.chip
```

### Use as a library

```rust
use chip_as_code::{Chip, ChipHash};

fn main() -> anyhow::Result<()> {
    // Parse a chip from text
    let source = r#"
        CHIP v0
        FEATURES n=2
        GATES m=3
        g0 = NOT(f1)
        g1 = AND(f0,g0)
        g2 = NOT(f0)
        OUTPUT = g1
    "#;
    
    let chip = Chip::parse(source)?;
    
    // Evaluate against features
    let features = vec![true, false];  // f0=1, f1=0
    let result = chip.eval(&features)?;
    
    println!("Result: {}", result);  // true
    
    // Get the canonical hash
    let hash = chip.hash();
    println!("Hash: blake3:{}", hash);
    
    Ok(())
}
```

---

## DNA Format

Chips are defined in `.chip` files with a simple, deterministic grammar:

```
CHIP v0
FEATURES n=<N>
GATES m=<M>
g0 = OP(inputs...)
g1 = OP(inputs...)
...
OUTPUT = gK
```

### Operators

| Operator | Syntax | Description |
|----------|--------|-------------|
| `AND` | `AND(a,b,c,...)` | All inputs must be true |
| `OR` | `OR(a,b,c,...)` | At least one input true |
| `NOT` | `NOT(a)` | Negate single input |
| `THRESH` | `THRESH(k,a,b,c,...)` | At least k inputs true |

### References

- `f0`, `f1`, ... `fN-1` β€” Input features
- `g0`, `g1`, ... `gM-1` β€” Gate outputs (must reference earlier gates only)

### Example: XOR Gate

```
CHIP v0
FEATURES n=2
GATES m=5
g0 = NOT(f1)
g1 = AND(f0,g0)
g2 = NOT(f0)
g3 = AND(g2,f1)
g4 = OR(g1,g3)
OUTPUT = g4
```

This implements: `XOR(f0, f1) = OR(AND(f0, NOT(f1)), AND(NOT(f0), f1))`

### Canonical Identity

```
chip_hash = blake3(json_atomic_canonical_bytes(chip_ast))
```

Whitespace doesn't affect the hash. Two chips with the same logic have the same hash.

---

## GateBox

The **GateBox** is a constitutional checkpoint that enforces the **No-Guess Law**:

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        GATEBOX                              β”‚
β”‚                                                             β”‚
β”‚   Intent ──────┐                                           β”‚
β”‚                β”‚    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚   Evidence ────┼───▢│  1. Missing evidence? β†’ GHOST    β”‚   β”‚
β”‚                β”‚    β”‚  2. Unanchored?       β†’ REJECT   β”‚   β”‚
β”‚   Policy β”€β”€β”€β”€β”€β”€β”˜    β”‚  3. Policy fails?     β†’ REJECT   β”‚   β”‚
β”‚   (chip)            β”‚  4. Otherwise         β†’ COMMIT   β”‚   β”‚
β”‚                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                                    β”‚                        β”‚
β”‚                                    β–Ό                        β”‚
β”‚                           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                 β”‚
β”‚                           β”‚    Ledger    β”‚                 β”‚
β”‚                           β”‚  (NDJSON)    β”‚                 β”‚
β”‚                           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### Verdicts

| Verdict | When | Ledger Append |
|---------|------|---------------|
| **Commit** | All evidence present, anchored, policy passes | βœ… Yes |
| **Ghost** | Missing supporting evidence | βœ… Yes (with question) |
| **Reject** | Unanchored evidence OR policy fails | ❌ No |

### Usage

```rust
use chip_as_code::gatebox::{run_gate, GateVerdict};

let verdict = run_gate(
    "intent.json",
    "evidence.json", 
    "policy.chip",
    "ledger.ndjson"
)?;

match verdict {
    GateVerdict::Commit { cid_hex, chip_hash, proof } => {
        println!("βœ… Committed: {}", cid_hex);
    }
    GateVerdict::Ghost { question } => {
        println!("❓ Need more info: {}", question);
    }
    GateVerdict::Reject { reason } => {
        println!("❌ Rejected: {}", reason);
    }
}
```

---

## Evolution

**EvoChip** learns boolean circuits from labeled data using discrete evolution β€” no gradients, no floats in the core.

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      EVOLUTION                              β”‚
β”‚                                                             β”‚
β”‚   Dataset ──▢ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚               β”‚Population│───▢│ Evaluate │───▢│ Select β”‚  β”‚
β”‚               β”‚  (chips) β”‚    β”‚ (fitness)β”‚    β”‚ (elite)β”‚  β”‚
β”‚               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”˜  β”‚
β”‚                     β–²                              β”‚       β”‚
β”‚                     β”‚         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”‚       β”‚
β”‚                     └─────────│  Mutate  β”‚β—€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β”‚
β”‚                               β”‚ Crossoverβ”‚                 β”‚
β”‚                               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                 β”‚
β”‚                                                             β”‚
β”‚   Output: best_chip.txt + training_curve.ndjson            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### CLI

```bash
chip evolve \
  --task xor \
  --seed 1337 \
  --generations 200 \
  --population 2000 \
  --elite 50 \
  --max-gates 32
```

### Outputs

| File | Description |
|------|-------------|
| `out/best_chip.txt` | Best evolved chip (canonical DNA) |
| `out/training_curve.ndjson` | Per-generation metrics |
| `out/lineage.ndjson` | Offspring lineage (who came from whom) |
| `out/replay_report.json` | Deterministic replay proof |
| `out/dataset.ndjson` | Training/test data (transparency) |

### Determinism Guarantee

```
Same seed + Same command = Identical artifacts
```

- RNG: `ChaCha20Rng` with explicit seed
- Fitness: Integer per10k accuracy (no floats in core)
- Hashes: BLAKE3 over JSON✯Atomic canonical bytes

---

## API

### Core Types

```rust
/// A semantic chip (boolean circuit)
pub struct Chip {
    pub features: usize,
    pub gates: Vec<Gate>,
    pub output: Ref,
}

/// A gate operation
pub enum GateOp {
    And(Vec<Ref>),
    Or(Vec<Ref>),
    Not(Ref),
    Threshold { k: usize, inputs: Vec<Ref> },
}

/// Reference to a feature or gate
pub enum Ref {
    Feature(usize),
    Gate(usize),
}
```

### Key Methods

```rust
impl Chip {
    /// Parse from text DNA
    pub fn parse(text: &str) -> Result<Self, ChipParseError>;
    
    /// Evaluate against feature vector
    pub fn eval(&self, features: &[bool]) -> Result<bool, EvalError>;
    
    /// Get canonical BLAKE3 hash
    pub fn hash(&self) -> ChipHash;
    
    /// Serialize to canonical text DNA
    pub fn to_dna(&self) -> String;
    
    /// Generate random chip
    pub fn random<R: Rng>(rng: &mut R, features: usize, max_gates: usize) -> Self;
    
    /// Mutate chip (for evolution)
    pub fn mutate<R: Rng>(&self, rng: &mut R, rate_per10k: u32) -> Self;
}
```

---

## CLI Reference

```
chip_as_code β€” Semantic Chips CLI

USAGE:
    chip <COMMAND>

COMMANDS:
    demo      Run 4 GateBox scenarios (commit, ghost, rejectΓ—2)
    gate      Single GateBox decision
    verify    Verify ledger CIDs
    evolve    Evolve a chip from data
    replay    Replay chip against dataset
    stats     Show dataset statistics
    help      Print help

OPTIONS:
    -h, --help       Print help
    -V, --version    Print version
```

### Examples

```bash
# Evolve XOR with custom params
chip evolve --task xor --seed 42 --generations 500 --population 5000

# Verify a ledger file
chip verify --ledger out/ledger.ndjson

# Single gate decision
chip gate \
  --intent examples/intent_good.json \
  --evidence examples/evidence_good.json \
  --policy examples/policy.txt \
  --ledger out/ledger.ndjson
```

---

## Architecture

```
chip_as_code/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ lib.rs        # Public API exports
β”‚   β”œβ”€β”€ chip_ir.rs    # Chip parsing, evaluation, hashing
β”‚   β”œβ”€β”€ evolve.rs     # Discrete evolution engine
β”‚   β”œβ”€β”€ gatebox.rs    # No-Guess Law checkpoint
β”‚   β”œβ”€β”€ gpu_eval.rs   # Optional GPU acceleration
β”‚   └── main.rs       # CLI
β”œβ”€β”€ examples/
β”‚   β”œβ”€β”€ xor_golden.chip
β”‚   β”œβ”€β”€ policy.txt
β”‚   β”œβ”€β”€ intent_*.json
β”‚   └── evidence_*.json
└── out/              # Generated artifacts
```

---

## Contributing

```bash
# Run tests
cargo test

# Run with debug output
cargo run -- evolve --task xor --seed 1337 --debug

# Build with GPU support
cargo build --release --features gpu
```

---

## License

Licensed under either of:

- Apache License, Version 2.0 ([LICENSE-APACHE]LICENSE-APACHE)
- MIT License ([LICENSE-MIT]LICENSE-MIT)

at your option.

---

<div align="center">

**Part of the [LogLine Foundation](https://logline.foundation) ecosystem**

`chip_as_code` β€’ `logline` β€’ `json_atomic` β€’ `tdln-*` β€’ `ubl-ledger`

[crates.io/users/danvoulez](https://crates.io/users/danvoulez)

</div>