nx_crc32_gate.nx source
↩ module page · 93 lines · 4760 B
1// nx_crc32_gate.nx -- the gate for CRC-32, and the ecosystem's cleanest ORACLE-CLASS evidence.
2// WHY IT RANKS ABOVE ITS FAN-IN (14 importers): the check value below is EXTERNAL AUTHORITY.
3// 0xCBF43926 for "123456789" is the published CRC-32/ISO-HDLC check constant (IEEE 802.3
4// reflected poly 0xEDB88320, init/xorout 0xFFFFFFFF) reproduced by zlib crc32(), python
5// binascii.crc32() and the CRC RevEng catalogue -- authored by people who never saw this
6// implementation. ★THAT IS THE DEFINITION OF THIRD-PARTY: an oracle editable by neither the
7// provider nor the user. This gate is therefore a candidate EV_CLASS_ORACLE row, not merely a
8// consistency test. ("KAT" names the FORM, not the PROVENANCE -- a KAT whose answers we wrote
9// ourselves is self-consistency; these answers predate us.)
10// ⚠THE SAME DEFECT nx_str HAD: the module's vectors live inside its own main(), which
11// expand_imports STRIPS from every consumer -- so they ran only if someone built this module as
12// an entry point, and NO ruler could read the result. Re-derived here in readable form, WIDENED
13// with a vector the original did not carry (T4), and given a negative control.
14// license_tier: ORIGINAL expect_exit: 0 No hw writes (Rule 26).
15import "nx_syscalls.nx"
16import "nx_gate_verdict.nx"
17import "nx_crc32.nx"
18
19// PUBLISHED constants -- external authority, quoted not derived.
20const CG_CHECK_123456789: i64 = 0xCBF43926 // CRC-32/ISO-HDLC "check" value
21const CG_CHECK_A: i64 = 0xE8B7BE43 // CRC-32("a")
22const CG_CHECK_FOX: i64 = 0x414FA339 // CRC-32("The quick brown fox jumps over the lazy dog")
23
24func cg_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
25
26func main() -> i64 {
27 let ctr: *i64 = gv_ctr()
28 gv_head("NX-CRC32-GATE -- published check values; THIRD-PARTY oracle, not self-consistency" as *u8)
29
30 let nine: *u8 = "123456789" as *u8
31 let one: *u8 = "a" as *u8
32 let fox: *u8 = "The quick brown fox jumps over the lazy dog" as *u8
33
34 var t1: i64 = 0
35 if nx_crc32(nine, 9) == CG_CHECK_123456789 { t1 = 1 }
36 gv_check("T1 ORACLE: CRC-32(\"123456789\") == 0xCBF43926 (the published check value)" as *u8, t1, ctr)
37
38 var t2: i64 = 0
39 if nx_crc32(one, 1) == CG_CHECK_A { t2 = 1 }
40 gv_check("T2 ORACLE: CRC-32(\"a\") == 0xE8B7BE43" as *u8, t2, ctr)
41
42 // WIDENED: a vector the module's own self-test did not carry.
43 var t3: i64 = 0
44 let foxn: i64 = cg_len(fox)
45 if foxn == 43 { if nx_crc32(fox, foxn) == CG_CHECK_FOX { t3 = 1 } }
46 gv_check("T3 ORACLE (NEW): CRC-32(pangram, 43 bytes) == 0x414FA339" as *u8, t3, ctr)
47
48 var t4: i64 = 0
49 let e: *u8 = sys_mmap(8)
50 if nx_crc32(e, 0) == 0 { t4 = 1 }
51 gv_check("T4 empty input is 0 (init ^ xorout cancel exactly)" as *u8, t4, ctr)
52
53 // STREAMING EQUIVALENCE -- the property every chunked consumer depends on.
54 var c: i64 = nx_crc32_update(0, nine, 4)
55 let p5: *u8 = ((nine as i64) + 4) as *u8
56 c = nx_crc32_update(c, p5, 1)
57 let p6: *u8 = ((nine as i64) + 5) as *u8
58 c = nx_crc32_update(c, p6, 4)
59 var t5: i64 = 0
60 if c == CG_CHECK_123456789 { t5 = 1 }
61 gv_check("T5 STREAMING 4+1+4 == one-shot: chunk boundaries do not change the result" as *u8, t5, ctr)
62
63 // a DIFFERENT split of the same input must also agree -- one split could pass by luck
64 var d: i64 = nx_crc32_update(0, nine, 1)
65 let q: *u8 = ((nine as i64) + 1) as *u8
66 d = nx_crc32_update(d, q, 8)
67 var t6: i64 = 0
68 if d == CG_CHECK_123456789 { t6 = 1 }
69 gv_check("T6 a SECOND split (1+8) agrees -- one lucky boundary is not evidence" as *u8, t6, ctr)
70
71 // table sanity: entry 0 of the reflected table is 0 by construction
72 nx_crc32_init()
73 var t7: i64 = 0
74 if nx_crc32_table_at(0) == 0 { if nx_crc32_table_at(1) != 0 { t7 = 1 } }
75 gv_check("T7 lazy table built: entry0 == 0, entry1 != 0 (table is populated, not zeroed)" as *u8, t7, ctr)
76
77 // 32-BIT CONTAINMENT: a CRC that leaks above bit 31 corrupts every consumer that stores it
78 var t8: i64 = 1
79 if nx_crc32(nine, 9) > 0xFFFFFFFF { t8 = 0 }
80 if nx_crc32(fox, foxn) > 0xFFFFFFFF { t8 = 0 }
81 if nx_crc32(one, 1) < 0 { t8 = 0 }
82 gv_check("T8 result stays inside 32 bits and never goes negative" as *u8, t8, ctr)
83
84 // NEG-CONTROL: a one-bit input change must change the CRC (and the comparator must reject).
85 let alt: *u8 = "123456788" as *u8
86 var t9: i64 = 0
87 if nx_crc32(alt, 9) != CG_CHECK_123456789 { if nx_crc32(one, 1) != CG_CHECK_123456789 { t9 = 1 } }
88 gv_check("T9 NEG-CONTROL: a changed last byte REJECTS the published value" as *u8, t9, ctr)
89
90 let rc: i64 = gv_verdict("CRC32-GATE" as *u8, ctr, "agrees with published CRC-32/ISO-HDLC vectors" as *u8)
91 sys_exit(rc)
92 return rc
93}