File size: 5,627 Bytes
518343a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * doctrine_entropy.test.ts
 *
 * Vitest tests for the Lutar.Shannon.DoctrineEntropy gate.
 *
 * Tests:
 *   1. 1000-input random channel-rate bound test
 *   2. Encoder/decoder round-trip for all labels
 *   3. Kraft inequality at equality
 *   4. Receipt emission with mock signer
 *
 * Lean commit: c4d13795689601324fce0236351bfe0ade990a43
 */

import { describe, it, expect } from "vitest";
import {
  shannonCode,
  shannonDecode,
  verifyRoundtrip,
  verifyCodeIn2Bits,
  verifyKraftEquality,
  channelRateBound,
  emitDoctrineEntropyReceipt,
  doctrineEntropyGate,
  ALL_LABELS,
  DOCTRINE_ALPHABET_SIZE,
  DOCTRINE_CODEWORD_LENGTH,
  type DoctrineLabel,
  type Signer,
} from "../../src/gates/doctrine_entropy";

// ---------------------------------------------------------------------------
// Seeded LCG
// ---------------------------------------------------------------------------

function seedRandom(seed: number) {
  let s = seed >>> 0;
  return () => {
    s = (Math.imul(1664525, s) + 1013904223) >>> 0;
    return s / 0x100000000;
  };
}

const mockSigner: Signer = (p: string) =>
  `mock-sig::${Buffer.from(p).slice(0, 16).toString("hex")}`;

// ---------------------------------------------------------------------------
// 1. 1000-input random channel-rate bound test
// ---------------------------------------------------------------------------

describe("doctrine_entropy: 1000-input channel rate bound test", () => {
  it("channelRateBound(B, rate) = true iff rate * 2 ≤ B for 1000 random pairs", () => {
    const rand = seedRandom(0x55667788);
    let failures = 0;

    for (let i = 0; i < 1000; i++) {
      const B = Math.floor(rand() * 10000);
      const rate = Math.floor(rand() * 10000);
      const expected = rate * DOCTRINE_CODEWORD_LENGTH <= B;
      if (channelRateBound(B, rate) !== expected) failures++;
    }

    expect(failures).toBe(0);
  });

  it("channelRateBound: rate = 0 is always admissible", () => {
    const rand = seedRandom(0x11111111);
    for (let i = 0; i < 1000; i++) {
      const B = Math.floor(rand() * 10000);
      expect(channelRateBound(B, 0)).toBe(true);
    }
  });
});

// ---------------------------------------------------------------------------
// 2. Encoder/decoder round-trip
// ---------------------------------------------------------------------------

describe("doctrine_entropy: encoder/decoder round-trip", () => {
  it("verifyRoundtrip() is true", () => {
    expect(verifyRoundtrip()).toBe(true);
  });

  it("shannonDecode(shannonCode(l)) = l for all labels", () => {
    for (const label of ALL_LABELS) {
      expect(shannonDecode(shannonCode(label))).toBe(label);
    }
  });

  it("shannonCode produces distinct values for distinct labels", () => {
    const codes = new Set(ALL_LABELS.map(shannonCode));
    expect(codes.size).toBe(DOCTRINE_ALPHABET_SIZE);
  });

  it("shannonDecode returns null for codewords ≥ 4", () => {
    expect(shannonDecode(4)).toBeNull();
    expect(shannonDecode(99)).toBeNull();
    expect(shannonDecode(-1)).toBeNull();
  });

  it("shannonCode(Bot)=0, L1=1, L2=2, Top=3", () => {
    expect(shannonCode("Bot")).toBe(0);
    expect(shannonCode("L1")).toBe(1);
    expect(shannonCode("L2")).toBe(2);
    expect(shannonCode("Top")).toBe(3);
  });
});

// ---------------------------------------------------------------------------
// 3. Kraft inequality at equality
// ---------------------------------------------------------------------------

describe("doctrine_entropy: Kraft inequality (kraft_inequality_doctrine)", () => {
  it("verifyKraftEquality() is true", () => {
    expect(verifyKraftEquality()).toBe(true);
  });

  it("alphabet has exactly 4 elements (doctrine_alphabet_size_4)", () => {
    expect(ALL_LABELS.length).toBe(DOCTRINE_ALPHABET_SIZE);
  });

  it("all codewords fit in 2 bits (shannon_code_in_2_bits)", () => {
    expect(verifyCodeIn2Bits()).toBe(true);
    for (const label of ALL_LABELS) {
      expect(shannonCode(label)).toBeGreaterThanOrEqual(0);
      expect(shannonCode(label)).toBeLessThan(4);
    }
  });

  it("codeword length is uniformly 2 (doctrine_uniform_code_length_2_bits)", () => {
    // All labels map to codewords fitting in 2 bits
    for (const label of ALL_LABELS) {
      expect(shannonCode(label) < 4).toBe(true);
    }
  });
});

// ---------------------------------------------------------------------------
// 4. Receipt emission
// ---------------------------------------------------------------------------

describe("doctrine_entropy: DSSE receipt emission", () => {
  it("emits receipt with correct theorem and commit SHA", () => {
    const receipt = emitDoctrineEntropyReceipt(mockSigner);
    expect(receipt.theorem).toBe(
      "Lutar.Shannon.doctrine_uniform_code_length_2_bits"
    );
    expect(receipt.lean_commit_sha).toBe("c4d13795689601324fce0236351bfe0ade990a43");
    expect(receipt.output).toBe(true);
    expect(receipt.inputs_hash).toMatch(/^[0-9a-f]{64}$/);
    expect(receipt.sig).toContain("mock-sig::");
  });

  it("receipt is deterministic (same inputs_hash across calls)", () => {
    const r1 = emitDoctrineEntropyReceipt(mockSigner);
    const r2 = emitDoctrineEntropyReceipt(mockSigner);
    expect(r1.inputs_hash).toBe(r2.inputs_hash);
    expect(r1.output).toBe(r2.output);
  });

  it("gate returns alphabetSize=4, codewordLength=2, kraftEqualityHolds=true", () => {
    const result = doctrineEntropyGate(mockSigner);
    expect(result.alphabetSize).toBe(4);
    expect(result.codewordLength).toBe(2);
    expect(result.kraftEqualityHolds).toBe(true);
    expect(result.receipt.output).toBe(true);
  });
});