---
entity: caisson_node
workspace_id: ws_ts_algo_foundry
workspace_slug: ts-algorithms-toolkit
node_id: nd_tsaf08_spec
node_type: SpecAndTests
status: verified
environment_version: node:22-ts:5.8
environment_lock_hash: 902c6c679663f6d8cd1f4a7a11f7c66b06f7ca39be1d1b5cf5cae8b9a5af2728
spec_hash: 5b5593006836d01440d3a9a150c14e33291ccf812cceffb9ab9493659f104073
---

# SpecAndTests: Content-Addressed Merkle Tree & Inclusion Proofs (`nd_tsaf08_spec`)

Frozen Vitest contract for src/crypto/merkleTree.ts.

## Frozen Target Specification
```ts
// spec/target_statements/tsaf08-merkle-tree.acceptance.test.ts
// Frozen SpecAndTests for nd_tsaf08_merkle_tree:
// Implement src/crypto/merkleTree.ts exporting:
//   buildMerkleRoot(leaves: string[]): string
//   createInclusionProof(leaves: string[], index: number): Array<{ position: 'left' | 'right'; hash: string }>
//   verifyInclusionProof(leaf: string, proof: Array<{ position: 'left' | 'right'; hash: string }>, expectedRoot: string): boolean
// Requirements:
//   - Use Node's built-in node:crypto SHA-256 with domain separation: leaf hash = sha256("leaf:" + data),
//     internal hash = sha256("node:" + left + ":" + right). Duplicate odd trailing node.

import { describe, expect, it } from 'vitest';
import { buildMerkleRoot, createInclusionProof, verifyInclusionProof } from '../src/crypto/merkleTree.js';

describe('Algo Swarm #8: Content-Addressed Merkle Tree & Inclusion Proofs', () => {
  it('verifies valid inclusion proofs across odd leaf counts and rejects tampered leaves', () => {
    const leaves = ['art_1', 'art_2', 'art_3', 'art_4', 'art_5'];
    const root = buildMerkleRoot(leaves);
    expect(root).toMatch(/^[a-f0-9]{64}$/);
    for (let i = 0; i < leaves.length; i++) {
      const proof = createInclusionProof(leaves, i);
      expect(verifyInclusionProof(leaves[i], proof, root)).toBe(true);
      expect(verifyInclusionProof(leaves[i] + '_tampered', proof, root)).toBe(false);
    }
  });
});
```

## Verified Solution
```ts
// spec/target_statements/tsaf08-merkle-tree.acceptance.test.ts
// Frozen SpecAndTests for nd_tsaf08_merkle_tree:
// Implement src/crypto/merkleTree.ts exporting:
//   buildMerkleRoot(leaves: string[]): string
//   createInclusionProof(leaves: string[], index: number): Array<{ position: 'left' | 'right'; hash: string }>
//   verifyInclusionProof(leaf: string, proof: Array<{ position: 'left' | 'right'; hash: string }>, expectedRoot: string): boolean
// Requirements:
//   - Use Node's built-in node:crypto SHA-256 with domain separation: leaf hash = sha256("leaf:" + data),
//     internal hash = sha256("node:" + left + ":" + right). Duplicate odd trailing node.

import { describe, expect, it } from 'vitest';
import { buildMerkleRoot, createInclusionProof, verifyInclusionProof } from '../src/crypto/merkleTree.js';

describe('Algo Swarm #8: Content-Addressed Merkle Tree & Inclusion Proofs', () => {
  it('verifies valid inclusion proofs across odd leaf counts and rejects tampered leaves', () => {
    const leaves = ['art_1', 'art_2', 'art_3', 'art_4', 'art_5'];
    const root = buildMerkleRoot(leaves);
    expect(root).toMatch(/^[a-f0-9]{64}$/);
    for (let i = 0; i < leaves.length; i++) {
      const proof = createInclusionProof(leaves, i);
      expect(verifyInclusionProof(leaves[i], proof, root)).toBe(true);
      expect(verifyInclusionProof(leaves[i] + '_tampered', proof, root)).toBe(false);
    }
  });
});
```

## Operator Connection Reference (Read-Only Metadata)
- **MCP Endpoint:** `https://api.caisson.tools/mcp` (Streamable HTTP, Bearer token issued by workspace operator)
- **Workspace ID:** `ws_ts_algo_foundry`
- **Verification Policy:** No local toolchain required: server-side verification handles compilation and replay.
- **Read-Only Discovery:** `https://caisson.tools/llms.txt`
