nx_zstd_seqtab_gate.nx source
↩ module page · 190 lines · 7829 B
1// nx_zstd_seqtab_gate.nx -- proves the predefined sequence distributions.
2//
3// These are transcribed spec data, so the gate's job is to catch a TYPO.
4//
5// T1 checks the totals directly: 64, 64, 32. A single mistyped digit almost
6// always breaks the sum, which makes this a real transcription check rather
7// than a restatement of the source.
8//
9// T2 checks that all three actually BUILD. nx_fse_build_dtable refuses unless
10// the counts tile the table exactly and every cell receives a symbol, so a
11// successful build is independent evidence on top of the sum.
12//
13// T3 pins that a -1 entry is a REAL low-probability symbol, not a terminator:
14// it must appear in the built table and it must sit at the TOP, which is where
15// the spread algorithm places low-probability symbols. Dropping those entries
16// is the obvious misreading -- they look like end markers.
17//
18// T4 spot-checks distinctive values that a shifted transcription would move:
19// the literal-length 3 at index 25 sitting alone in a run of 2s, and the
20// offset table's 2,2,2 at indices 6-8 surrounded by 1s.
21//
22// license_tier: ORIGINAL
23import "nx_syscalls.nx"
24import "nx_zstd_fse.nx"
25import "nx_zstd_seqtab.nx"
26
27func g_puts(s: *u8) -> i64 {
28 var i: i64 = 0
29 while s[i] != (0 as u8) { i = i + 1 }
30 sys_write(1, s, i)
31 return i
32}
33
34func g_putn(v: i64) -> i64 {
35 let buf: *u8 = sys_mmap(32)
36 var x: i64 = v
37 if x < 0 { g_puts("-" as *u8); x = 0 - x }
38 if x == 0 { buf[0] = 0x30 as u8; sys_write(1, buf, 1); return 1 }
39 let tmp: *u8 = sys_mmap(32)
40 var d: i64 = 0
41 while x > 0 { tmp[d] = ((x % 10) + 0x30) as u8; x = x / 10; d = d + 1 }
42 var i: i64 = 0
43 while i < d { buf[i] = tmp[d - 1 - i]; i = i + 1 }
44 sys_write(1, buf, d)
45 return d
46}
47
48func main() -> i64 {
49 var fails: i64 = 0
50 var mark: i64 = 0
51 var i: i64 = 0
52
53 let n: *i64 = sys_mmap(NX_FSE_MAX_SYMBOL * 8 + 64) as *i64
54
55 // ---- T1: the TOTALS -- the transcription check ----
56 i = 0
57 while i < NX_FSE_MAX_SYMBOL { n[i] = 0; i = i + 1 }
58 if nx_zstd_seqtab_ll(n) != 36 { fails = fails + 1 }
59 if nx_zstd_seqtab_total(n, 36) != 64 { fails = fails + 1 }
60
61 i = 0
62 while i < NX_FSE_MAX_SYMBOL { n[i] = 0; i = i + 1 }
63 if nx_zstd_seqtab_ml(n) != 53 { fails = fails + 1 }
64 if nx_zstd_seqtab_total(n, 53) != 64 { fails = fails + 1 }
65
66 i = 0
67 while i < NX_FSE_MAX_SYMBOL { n[i] = 0; i = i + 1 }
68 if nx_zstd_seqtab_of(n) != 29 { fails = fails + 1 }
69 if nx_zstd_seqtab_total(n, 29) != 32 { fails = fails + 1 }
70 if fails > 0 { if mark == 0 { mark = 1 } }
71
72 // ---- T2: all three BUILD -- independent evidence on top of the sum ----
73 let tll: *NxFseTable = nx_zstd_seqtab_ll_table()
74 if tll == (0 as *NxFseTable) { fails = fails + 1 } else {
75 if tll.table_size != 64 { fails = fails + 1 }
76 if tll.table_log != 6 { fails = fails + 1 }
77 }
78 let tml: *NxFseTable = nx_zstd_seqtab_ml_table()
79 if tml == (0 as *NxFseTable) { fails = fails + 1 } else {
80 if tml.table_size != 64 { fails = fails + 1 }
81 }
82 let tof: *NxFseTable = nx_zstd_seqtab_of_table()
83 if tof == (0 as *NxFseTable) { fails = fails + 1 } else {
84 if tof.table_size != 32 { fails = fails + 1 }
85 if tof.table_log != 5 { fails = fails + 1 }
86 }
87 if fails > 0 { if mark == 0 { mark = 2 } }
88
89 // ---- T3: -1 is a REAL symbol, placed at the TOP of the table ----
90 // the literal-length table's low-probability symbols are 32..35, and the
91 // spread puts them in the highest four slots, descending
92 if nx_fse_cell_symbol(tll, 63) != 32 { fails = fails + 1 }
93 if nx_fse_cell_symbol(tll, 62) != 33 { fails = fails + 1 }
94 if nx_fse_cell_symbol(tll, 61) != 34 { fails = fails + 1 }
95 if nx_fse_cell_symbol(tll, 60) != 35 { fails = fails + 1 }
96 // the offset table's low-probability symbols are 24..28 -> top five slots
97 if nx_fse_cell_symbol(tof, 31) != 24 { fails = fails + 1 }
98 if nx_fse_cell_symbol(tof, 27) != 28 { fails = fails + 1 }
99 // every cell of every table must name a symbol inside its alphabet
100 var bad: i64 = 0
101 i = 0
102 while i < 64 {
103 let s: i64 = nx_fse_cell_symbol(tll, i)
104 if s < 0 { bad = bad + 1 }
105 if s > NX_SEQTAB_LL_MAX { bad = bad + 1 }
106 let s2: i64 = nx_fse_cell_symbol(tml, i)
107 if s2 < 0 { bad = bad + 1 }
108 if s2 > NX_SEQTAB_ML_MAX { bad = bad + 1 }
109 i = i + 1
110 }
111 i = 0
112 while i < 32 {
113 let s3: i64 = nx_fse_cell_symbol(tof, i)
114 if s3 < 0 { bad = bad + 1 }
115 if s3 > NX_SEQTAB_OF_MAX { bad = bad + 1 }
116 i = i + 1
117 }
118 if bad != 0 { fails = fails + 1 }
119 if fails > 0 { if mark == 0 { mark = 3 } }
120
121 // ---- T4: distinctive values a shifted transcription would move ----
122 i = 0
123 while i < NX_FSE_MAX_SYMBOL { n[i] = 0; i = i + 1 }
124 nx_zstd_seqtab_ll(n)
125 if n[0] != 4 { fails = fails + 1 }
126 if n[1] != 3 { fails = fails + 1 }
127 // the lone 3 at index 25, sitting inside a run of 2s
128 if n[24] != 2 { fails = fails + 1 }
129 if n[25] != 3 { fails = fails + 1 }
130 if n[26] != 2 { fails = fails + 1 }
131 // the 1s at 13,14,15 then back to 2 at 16
132 if n[13] != 1 { fails = fails + 1 }
133 if n[15] != 1 { fails = fails + 1 }
134 if n[16] != 2 { fails = fails + 1 }
135 if n[35] != (0 - 1) { fails = fails + 1 }
136
137 i = 0
138 while i < NX_FSE_MAX_SYMBOL { n[i] = 0; i = i + 1 }
139 nx_zstd_seqtab_ml(n)
140 // match lengths open 1,4,3 -- the 4 is at index ONE, not zero
141 if n[0] != 1 { fails = fails + 1 }
142 if n[1] != 4 { fails = fails + 1 }
143 if n[2] != 3 { fails = fails + 1 }
144 if n[8] != 2 { fails = fails + 1 }
145 if n[9] != 1 { fails = fails + 1 }
146 if n[45] != 1 { fails = fails + 1 }
147 if n[46] != (0 - 1) { fails = fails + 1 }
148 if n[52] != (0 - 1) { fails = fails + 1 }
149
150 i = 0
151 while i < NX_FSE_MAX_SYMBOL { n[i] = 0; i = i + 1 }
152 nx_zstd_seqtab_of(n)
153 // the 2,2,2 island at 6-8, surrounded by 1s
154 if n[5] != 1 { fails = fails + 1 }
155 if n[6] != 2 { fails = fails + 1 }
156 if n[7] != 2 { fails = fails + 1 }
157 if n[8] != 2 { fails = fails + 1 }
158 if n[9] != 1 { fails = fails + 1 }
159 if n[23] != 1 { fails = fails + 1 }
160 if n[24] != (0 - 1) { fails = fails + 1 }
161 if n[28] != (0 - 1) { fails = fails + 1 }
162 if fails > 0 { if mark == 0 { mark = 4 } }
163
164 // ---- T5 NEG: the total helper rejects an impossible count ----
165 i = 0
166 while i < NX_FSE_MAX_SYMBOL { n[i] = 0; i = i + 1 }
167 n[0] = 0 - 5
168 if nx_zstd_seqtab_total(n, 1) != (0 - 1) { fails = fails + 1 }
169 // and dropping the -1 entries breaks the sum, which the build then refuses
170 i = 0
171 while i < NX_FSE_MAX_SYMBOL { n[i] = 0; i = i + 1 }
172 nx_zstd_seqtab_ll(n)
173 n[32] = 0; n[33] = 0; n[34] = 0; n[35] = 0
174 if nx_zstd_seqtab_total(n, 36) != 60 { fails = fails + 1 }
175 if nx_fse_build_dtable(n, NX_SEQTAB_LL_MAX, NX_SEQTAB_LL_LOG) != (0 as *NxFseTable) { fails = fails + 1 }
176 if fails > 0 { if mark == 0 { mark = 5 } }
177
178 if fails == 0 {
179 g_puts("GATE nx_zstd_seqtab verdict=GREEN pass=5/5 (RFC 8878 predefined distributions: LL 36 entries totalling 64, ML 53 totalling 64, OF 29 totalling 32 -- the sum IS the transcription check; all three BUILD as FSE tables, independent evidence on top of the sums; -1 proven a REAL low-probability symbol placed at the TOP of the table, not a terminator; distinctive values pinned -- the lone 3 at LL index 25, the 4 at ML index ONE not zero, the 2,2,2 island at OF 6-8; NEG a negative count below -1 refused, and DROPPING the -1 entries drops the total to 60 and the build refuses)\n" as *u8)
180 sys_exit(0)
181 return 0
182 }
183 g_puts("GATE nx_zstd_seqtab verdict=RED fails=" as *u8)
184 g_putn(fails)
185 g_puts(" first_stage=" as *u8)
186 g_putn(mark)
187 g_puts("\n" as *u8)
188 sys_exit(1)
189 return 1
190}