nx_commons_witness.nx source
↩ module page · 129 lines · 6114 B
1// nx_commons_witness.nx -- the PRODUCER of witnessed_permil, the term nx_commons_lib leans on hardest
2// and which, until now, NOTHING in the estate emitted.
3//
4// ★ A GUARD WHOSE INPUT THE PREVIOUS STEP DOES NOT EMIT IS A GUARD NOBODY CAN USE. nx_commons_lib
5// scores standing from third-party-attested giving, and the sybil ring collapses precisely because it
6// cannot manufacture witnesses. But no organ computed that number, so the whole rule was unusable in
7// practice -- a ruler with no way to take a reading.
8//
9// KNOWN GOOD MATCHED: nx_trust_corroborate (the spore's sovereign trust bootstrap, 2026-06-05). Its
10// rule is exactly right and is adopted here verbatim in spirit:
11// - a claim is admitted only if >= K INDEPENDENT sources present the EXACT SAME bytes
12// - WE hash every source's view with our own sha256; a source's self-claimed hash is never trusted
13// - K=1 is HEARSAY and is forbidden by construction; K>=2 is corroboration
14// "A single tampered source cannot reach K alone" is the same anti-sybil property as the trust-flow
15// cap in nx_commons_lib, one layer further down.
16//
17// TWO IMPROVEMENTS, each measured by nx_commons_witness_gate rather than asserted:
18//
19// 1. PLURALITY TARGET, NOT SOURCE-0 TARGET. The known good computes
20// let agree = tc_agreement_count(fps, fps, n_sources)
21// where the target fingerprint IS source 0's. So admission counts agreement WITH SOURCE 0, not with
22// the majority. If source 0 is poisoned and K-1 accomplices present the identical poison, the claim
23// is admitted while ANY NUMBER of honest sources presenting the true bytes are simply not consulted.
24// ★★★★★★ AN AGREEMENT COUNT ANCHORED ON ONE PARTICULAR SOURCE IS NOT A MAJORITY, IT IS THAT
25// SOURCE'S OPINION WEARING A QUORUM'S CLOTHES.
26// Here the target is the largest agreement CLUSTER, so poison must out-number truth, not merely
27// occupy index 0.
28//
29// 2. PARTY EXCLUSION -- the thing a commons needs and a trust store does not. "The person I billed says
30// I helped them" is K=1 hearsay dressed as attestation, and it is exactly what the rent-extractor
31// has in abundance: every counterparty will confirm the fee-justifying service was rendered. Neither
32// the giver nor the receiver counts toward corroboration of their own exchange. A witness with a
33// stake is a participant.
34//
35// Integer only, no floats. Thresholds named (law 11). license_tier: ORIGINAL
36import "nx_sha256.nx"
37import "nx_syscalls.nx"
38
39const CWIT_HASHLEN: i64 = 32
40const CWIT_K_MIN: i64 = 2 // hearsay floor -- one voice is never corroboration
41const CWIT_FULL: i64 = 5 // independent attestors for a full 1000-permil reading
42const CWIT_SCALE: i64 = 1000
43
44func cwit_hash_eq(a: *u8, b: *u8) -> i64 {
45 var i: i64 = 0
46 while i < CWIT_HASHLEN { if a[i] != b[i] { return 0 } i = i + 1 }
47 return 1
48}
49
50// A witness with a stake is a participant. Neither party to an exchange corroborates it.
51func cwit_is_independent(attestor: i64, giver: i64, receiver: i64) -> i64 {
52 if attestor == giver { return 0 }
53 if attestor == receiver { return 0 }
54 return 1
55}
56
57// Largest agreement cluster among INDEPENDENT attestors. Every view is hashed by US.
58// O(n^2) on purpose: n is a neighbourhood, not a population, and clarity beats cleverness in a
59// function whose wrongness would silently mint standing.
60func cwit_plurality_agreement(views: *u8, stride: i64, len: i64,
61 ids: *i64, n: i64,
62 giver: i64, receiver: i64) -> i64 {
63 if n <= 0 { return 0 }
64 let fps: *u8 = sys_mmap(n * CWIT_HASHLEN)
65 var s: i64 = 0
66 while s < n {
67 sha256_digest(views + s * stride, len, fps + s * CWIT_HASHLEN)
68 s = s + 1
69 }
70 var best: i64 = 0
71 var a: i64 = 0
72 while a < n {
73 if cwit_is_independent(ids[a], giver, receiver) == 1 {
74 var c: i64 = 0
75 var b: i64 = 0
76 while b < n {
77 if cwit_is_independent(ids[b], giver, receiver) == 1 {
78 if cwit_hash_eq(fps + a * CWIT_HASHLEN, fps + b * CWIT_HASHLEN) == 1 { c = c + 1 }
79 }
80 b = b + 1
81 }
82 if c > best { best = c }
83 }
84 a = a + 1
85 }
86 return best
87}
88
89// agreement -> witnessed_permil, the input nx_commons_lib takes.
90// Below K this is HEARSAY and contributes NOTHING -- not a reduced score, zero. A claim nobody
91// independent can corroborate is not weak evidence, it is absent evidence.
92func cwit_permil(agreement: i64, k: i64) -> i64 {
93 var kk: i64 = k
94 if kk < CWIT_K_MIN { kk = CWIT_K_MIN } // the hearsay floor cannot be argued below 2
95 if agreement < kk { return 0 }
96 var p: i64 = (agreement * CWIT_SCALE) / CWIT_FULL
97 if p > CWIT_SCALE { p = CWIT_SCALE }
98 return p
99}
100
101// full reading for one claimed contribution
102func cwit_witnessed_permil(views: *u8, stride: i64, len: i64,
103 ids: *i64, n: i64,
104 giver: i64, receiver: i64, k: i64) -> i64 {
105 let agree: i64 = cwit_plurality_agreement(views, stride, len, ids, n, giver, receiver)
106 return cwit_permil(agree, k)
107}
108
109// ---- THE KNOWN GOOD'S RULE, kept as a NEGATIVE CONTROL ----------------------------------------
110// Source-0-anchored agreement with NO party exclusion, i.e. what nx_trust_corroborate computes.
111// Kept here so the gate can prove which of the two improvements is doing the work, exactly as
112// nx_connect_match_lib keeps the arithmetic mean beside the harmonic mean. Deleting it would leave
113// the next author unable to reproduce the comparison that justified this organ.
114func cwit_source0_agreement(views: *u8, stride: i64, len: i64, n: i64) -> i64 {
115 if n <= 0 { return 0 }
116 let fps: *u8 = sys_mmap(n * CWIT_HASHLEN)
117 var s: i64 = 0
118 while s < n {
119 sha256_digest(views + s * stride, len, fps + s * CWIT_HASHLEN)
120 s = s + 1
121 }
122 var c: i64 = 0
123 var b: i64 = 0
124 while b < n {
125 if cwit_hash_eq(fps, fps + b * CWIT_HASHLEN) == 1 { c = c + 1 }
126 b = b + 1
127 }
128 return c
129}