gdp-wired 0.49.0 → 0.50.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/dist/core/candidate-seeds.d.ts +25 -0
- package/dist/core/candidate-seeds.js +90 -0
- package/dist/core/chart.js +28 -4
- package/dist/core/cli.js +3 -0
- package/dist/core/compose.js +4 -3
- package/dist/core/count-support.d.ts +1 -0
- package/dist/core/count-support.js +23 -2
- package/dist/core/counted-candidate-seeds.d.ts +12 -0
- package/dist/core/counted-candidate-seeds.js +31 -0
- package/dist/core/counts.js +9 -2
- package/dist/core/dsl-binders.d.ts +15 -0
- package/dist/core/dsl-binders.js +129 -0
- package/dist/core/dsl.d.ts +28 -2
- package/dist/core/dsl.js +147 -52
- package/dist/core/exists.js +2 -1
- package/dist/core/field-laws.js +23 -5
- package/dist/core/infer.js +10 -5
- package/dist/core/laws.js +2 -1
- package/dist/core/lift-cli.d.ts +11 -0
- package/dist/core/lift-cli.js +113 -0
- package/dist/core/proof-output.d.ts +53 -0
- package/dist/core/proof-output.js +38 -0
- package/dist/core/quorum-laws.js +55 -16
- package/dist/kernel/function-instances.d.ts +45 -0
- package/dist/kernel/function-instances.js +460 -0
- package/dist/kernel/prove.d.ts +12 -3
- package/dist/kernel/prove.js +164 -90
- package/dist/lean-kernel/axiom-audit.d.ts +11 -0
- package/dist/lean-kernel/axiom-audit.js +50 -0
- package/dist/lean-kernel/emit.d.ts +25 -2
- package/dist/lean-kernel/emit.js +75 -10
- package/dist/lean-kernel/legacy-family-order.d.ts +1 -0
- package/dist/lean-kernel/legacy-family-order.js +26 -0
- package/dist/lean-kernel/library.js +22 -0
- package/dist/lean-kernel/lift/counted-source-data.d.ts +7 -0
- package/dist/lean-kernel/lift/counted-source-data.js +78 -0
- package/dist/lean-kernel/lift/index.d.ts +82 -0
- package/dist/lean-kernel/lift/index.js +116 -0
- package/dist/lean-kernel/lift/project.d.ts +4 -0
- package/dist/lean-kernel/lift/project.js +15 -0
- package/dist/lean-kernel/lift/proof-capture.d.ts +104 -0
- package/dist/lean-kernel/lift/proof-capture.js +216 -0
- package/dist/lean-kernel/lift/proof-codec.d.ts +28 -0
- package/dist/lean-kernel/lift/proof-codec.js +299 -0
- package/dist/lean-kernel/lift/proof-counted-source.d.ts +16 -0
- package/dist/lean-kernel/lift/proof-counted-source.js +68 -0
- package/dist/lean-kernel/lift/proof-frontier.d.ts +19 -0
- package/dist/lean-kernel/lift/proof-frontier.js +153 -0
- package/dist/lean-kernel/lift/proof-graph.d.ts +17 -0
- package/dist/lean-kernel/lift/proof-graph.js +130 -0
- package/dist/lean-kernel/lift/proof-parts.d.ts +29 -0
- package/dist/lean-kernel/lift/proof-parts.js +131 -0
- package/dist/lean-kernel/lift/proof-prelude.d.ts +2 -0
- package/dist/lean-kernel/lift/proof-prelude.js +38 -0
- package/dist/lean-kernel/lift/proof-witness.d.ts +30 -0
- package/dist/lean-kernel/lift/proof-witness.js +283 -0
- package/dist/lean-kernel/lift/shape.d.ts +5 -0
- package/dist/lean-kernel/lift/shape.js +57 -0
- package/dist/lean-kernel/lift/source-projection.d.ts +16 -0
- package/dist/lean-kernel/lift/source-projection.js +81 -0
- package/dist/lean-kernel/lift/source.d.ts +23 -0
- package/dist/lean-kernel/lift/source.js +111 -0
- package/dist/lean-kernel/lift/witness-alpha.d.ts +9 -0
- package/dist/lean-kernel/lift/witness-alpha.js +31 -0
- package/dist/lean-kernel/lift-legacy.d.ts +58 -0
- package/dist/lean-kernel/lift-legacy.js +84 -0
- package/dist/lean-kernel/lift-names.d.ts +22 -0
- package/dist/lean-kernel/lift-names.js +36 -0
- package/dist/lean-kernel/lift-simulation/index.d.ts +29 -0
- package/dist/lean-kernel/lift-simulation/index.js +352 -0
- package/dist/lean-kernel/lift-simulation/shape.d.ts +41 -0
- package/dist/lean-kernel/lift-simulation/shape.js +173 -0
- package/dist/lean-kernel/lift-special/index.d.ts +36 -0
- package/dist/lean-kernel/lift-special/index.js +241 -0
- package/dist/lean-kernel/lift-special/structural.d.ts +25 -0
- package/dist/lean-kernel/lift-special/structural.js +126 -0
- package/dist/lean-kernel/lift-typed/index.d.ts +35 -0
- package/dist/lean-kernel/lift-typed/index.js +256 -0
- package/dist/lean-kernel/lift-typed/roles.d.ts +17 -0
- package/dist/lean-kernel/lift-typed/roles.js +170 -0
- package/dist/lean-kernel/lift-typed/shape.d.ts +1 -0
- package/dist/lean-kernel/lift-typed/shape.js +12 -0
- package/dist/lean-kernel/lift.d.ts +30 -0
- package/dist/lean-kernel/lift.js +97 -0
- package/package.json +8 -2
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
// Lossless data only. The outer CAPTURE.json is the sole complete marker.
|
|
2
|
+
// The inherited streaming writer's O(graph-nodes) identity table is NOT a RAM cap.
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { openSync, closeSync, readSync, readFileSync, writeFileSync, fsyncSync, mkdirSync, renameSync, unlinkSync, lstatSync } from 'node:fs';
|
|
5
|
+
import { join, dirname } from 'node:path';
|
|
6
|
+
import { StringDecoder } from 'node:string_decoder';
|
|
7
|
+
import { writeGraphCapsule } from "./proof-graph.js";
|
|
8
|
+
import { inspectProvenance, retainedLawCoverage, projectionOf } from "./proof-capture.js";
|
|
9
|
+
const hash = (data) => createHash('sha256').update(data).digest('hex');
|
|
10
|
+
const SHARD = 8 * 1024 * 1024, LINE = 1024 * 1024, TEXT = 16 * 1024;
|
|
11
|
+
function syncDir(path) { const fd = openSync(path, 'r'); try {
|
|
12
|
+
fsyncSync(fd);
|
|
13
|
+
}
|
|
14
|
+
finally {
|
|
15
|
+
closeSync(fd);
|
|
16
|
+
} }
|
|
17
|
+
function writeNew(path, text) { const fd = openSync(path, 'wx'); try {
|
|
18
|
+
writeFileSync(fd, text);
|
|
19
|
+
fsyncSync(fd);
|
|
20
|
+
}
|
|
21
|
+
finally {
|
|
22
|
+
closeSync(fd);
|
|
23
|
+
} }
|
|
24
|
+
export function writeFamilyCapsule(directory, bundle, metadata) {
|
|
25
|
+
if (JSON.stringify(bundle.projection) !== JSON.stringify(projectionOf(bundle)))
|
|
26
|
+
throw Error('invalid projection qualification');
|
|
27
|
+
mkdirSync(directory, { recursive: false });
|
|
28
|
+
const graph = join(directory, 'graph'), pending = join(directory, 'CAPTURE.pending'), complete = join(directory, 'CAPTURE.json');
|
|
29
|
+
try {
|
|
30
|
+
writeGraphCapsule(graph, bundle, { metadata });
|
|
31
|
+
const manifest = { schema: 'bapa-native-certificate/v1', complete: true, route: bundle.route, identity: bundle.identity, projection: bundle.projection,
|
|
32
|
+
graphManifestSha256: hash(readFileSync(join(graph, 'manifest.json'))),
|
|
33
|
+
...('final' in bundle ? { publicLawIndices: bundle.publicLawIndices, familyLaws: bundle.final.laws.length, certificateKeys: Object.keys(bundle.final.proof.certs) } : {}), terms: bundle.terms.terms.length };
|
|
34
|
+
const text = JSON.stringify(manifest) + '\n';
|
|
35
|
+
writeNew(pending, text);
|
|
36
|
+
syncDir(graph);
|
|
37
|
+
syncDir(directory);
|
|
38
|
+
renameSync(pending, complete);
|
|
39
|
+
syncDir(directory);
|
|
40
|
+
syncDir(dirname(directory));
|
|
41
|
+
return { manifestSha256: hash(text) };
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
// Keep incomplete data for custody; never advertise it as a certificate.
|
|
45
|
+
try {
|
|
46
|
+
unlinkSync(complete);
|
|
47
|
+
}
|
|
48
|
+
catch { }
|
|
49
|
+
try {
|
|
50
|
+
syncDir(directory);
|
|
51
|
+
}
|
|
52
|
+
catch { }
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const nat = (v) => { if (typeof v !== 'number' || !Number.isSafeInteger(v) || v < 0)
|
|
57
|
+
throw Error('invalid graph integer'); return v; };
|
|
58
|
+
const bit = (v) => { if (typeof v !== 'boolean')
|
|
59
|
+
throw Error('invalid graph bit'); return v; };
|
|
60
|
+
function boundedFile(path, max) {
|
|
61
|
+
const s = lstatSync(path);
|
|
62
|
+
if (!s.isFile() || s.size > max)
|
|
63
|
+
throw Error('invalid or oversized capsule file');
|
|
64
|
+
const data = readFileSync(path);
|
|
65
|
+
if (data.length !== s.size || data.length > max)
|
|
66
|
+
throw Error('capsule file changed');
|
|
67
|
+
return data;
|
|
68
|
+
}
|
|
69
|
+
function* records(directory, manifest) {
|
|
70
|
+
for (let i = 0; i < manifest.shards.length; i++) {
|
|
71
|
+
const shard = manifest.shards[i], name = `shard-${String(i).padStart(6, '0')}.jsonl`, path = join(directory, name);
|
|
72
|
+
if (shard.name !== name || nat(shard.bytes) < 1 || shard.bytes > SHARD || !/^[0-9a-f]{64}$/.test(shard.sha256))
|
|
73
|
+
throw Error('invalid graph shard');
|
|
74
|
+
const stat = lstatSync(path);
|
|
75
|
+
if (!stat.isFile() || stat.size !== shard.bytes)
|
|
76
|
+
throw Error('graph shard size mismatch');
|
|
77
|
+
const fd = openSync(path, 'r'), buffer = Buffer.alloc(128 * 1024), decoder = new StringDecoder('utf8'), digest = createHash('sha256');
|
|
78
|
+
let text = '', bytes = 0;
|
|
79
|
+
try {
|
|
80
|
+
for (;;) {
|
|
81
|
+
const n = readSync(fd, buffer, 0, buffer.length, null);
|
|
82
|
+
if (!n)
|
|
83
|
+
break;
|
|
84
|
+
bytes += n;
|
|
85
|
+
if (bytes > shard.bytes)
|
|
86
|
+
throw Error('graph shard grew');
|
|
87
|
+
digest.update(buffer.subarray(0, n));
|
|
88
|
+
text += decoder.write(buffer.subarray(0, n));
|
|
89
|
+
let end;
|
|
90
|
+
while ((end = text.indexOf('\n')) >= 0) {
|
|
91
|
+
const line = text.slice(0, end);
|
|
92
|
+
if (Buffer.byteLength(line) + 1 > LINE)
|
|
93
|
+
throw Error('graph line exceeds bound');
|
|
94
|
+
const row = JSON.parse(line);
|
|
95
|
+
if (!Array.isArray(row))
|
|
96
|
+
throw Error('invalid graph record');
|
|
97
|
+
yield row;
|
|
98
|
+
text = text.slice(end + 1);
|
|
99
|
+
}
|
|
100
|
+
if (Buffer.byteLength(text) > LINE)
|
|
101
|
+
throw Error('graph line exceeds bound');
|
|
102
|
+
}
|
|
103
|
+
text += decoder.end();
|
|
104
|
+
if (text.length || bytes !== shard.bytes || digest.digest('hex') !== shard.sha256)
|
|
105
|
+
throw Error('graph shard integrity mismatch');
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
closeSync(fd);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// Fresh-process data loader. All sizes are explicit input limits, not forecasts.
|
|
113
|
+
// No term registration, native checking, function deserialization or evaluation.
|
|
114
|
+
export function readCertificateGraph(directory, expectedManifestSha256, limits, onMetadata) {
|
|
115
|
+
// Check required names: Object.values would silently skip an omitted bound.
|
|
116
|
+
for (const key of ['maxBytes', 'maxNodes', 'maxStrings', 'maxArrayLength', 'maxShards']) {
|
|
117
|
+
const value = limits?.[key];
|
|
118
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 1)
|
|
119
|
+
throw Error('invalid graph limit: ' + key);
|
|
120
|
+
}
|
|
121
|
+
if (!/^[0-9a-f]{64}$/.test(expectedManifestSha256))
|
|
122
|
+
throw Error('missing graph manifest binding');
|
|
123
|
+
const bytes = boundedFile(join(directory, 'manifest.json'), Math.min(limits.maxBytes, 8 * 1024 * 1024));
|
|
124
|
+
if (hash(bytes) !== expectedManifestSha256)
|
|
125
|
+
throw Error('graph manifest binding mismatch');
|
|
126
|
+
const m = JSON.parse(bytes.toString());
|
|
127
|
+
if (m.schema !== 'closing-graph/v1' || m.complete !== true || !Array.isArray(m.shards) || !m.shards.length || m.shards.length > limits.maxShards)
|
|
128
|
+
throw Error('invalid graph manifest');
|
|
129
|
+
if (nat(m.nodes) > limits.maxNodes || nat(m.strings) > limits.maxStrings)
|
|
130
|
+
throw Error('graph object limit');
|
|
131
|
+
let total = bytes.length;
|
|
132
|
+
for (const s of m.shards) {
|
|
133
|
+
total += nat(s.bytes);
|
|
134
|
+
if (!Number.isSafeInteger(total) || total > limits.maxBytes)
|
|
135
|
+
throw Error('graph byte limit');
|
|
136
|
+
}
|
|
137
|
+
const protos = { object: Object.prototype, null: null, array: Array.prototype, map: Map.prototype, set: Set.prototype,
|
|
138
|
+
error: Error.prototype, typeError: TypeError.prototype, rangeError: RangeError.prototype, referenceError: ReferenceError.prototype,
|
|
139
|
+
syntaxError: SyntaxError.prototype, uriError: URIError.prototype, evalError: EvalError.prototype, aggregateError: AggregateError.prototype };
|
|
140
|
+
const objects = [], tags = [], strings = [], seals = [], arrays = [];
|
|
141
|
+
let root, open = -1, stringId = -1, stringParts = [];
|
|
142
|
+
const metadataIds = new Set();
|
|
143
|
+
for (const row of records(directory, m)) {
|
|
144
|
+
const [kind] = row;
|
|
145
|
+
if (kind === 'root') {
|
|
146
|
+
if (root || row.length !== 2 || !Array.isArray(row[1]))
|
|
147
|
+
throw Error('invalid graph root');
|
|
148
|
+
root = row[1];
|
|
149
|
+
}
|
|
150
|
+
else if (kind === 'node') {
|
|
151
|
+
const id = nat(row[1]), tag = row[2];
|
|
152
|
+
if (row.length !== 4 || open !== -1 || id !== objects.length || id >= m.nodes || typeof tag !== 'string' || !Object.hasOwn(protos, tag))
|
|
153
|
+
throw Error('invalid graph node');
|
|
154
|
+
if (tag === 'array') {
|
|
155
|
+
const n = nat(row[3]);
|
|
156
|
+
if (n > limits.maxArrayLength || n > 0xffffffff)
|
|
157
|
+
throw Error('array limit');
|
|
158
|
+
objects.push(new Array(n));
|
|
159
|
+
arrays[id] = n;
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
if (row[3] !== null)
|
|
163
|
+
throw Error('invalid node length');
|
|
164
|
+
objects.push(tag === 'map' ? new Map() : tag === 'set' ? new Set() : Object.create(protos[tag]));
|
|
165
|
+
}
|
|
166
|
+
tags.push(tag);
|
|
167
|
+
open = id;
|
|
168
|
+
}
|
|
169
|
+
else if (kind === 'string') {
|
|
170
|
+
const id = nat(row[1]);
|
|
171
|
+
if (row.length !== 2 || stringId !== -1 || id !== strings.length || id >= m.strings)
|
|
172
|
+
throw Error('invalid graph string');
|
|
173
|
+
stringId = id;
|
|
174
|
+
strings.push('');
|
|
175
|
+
stringParts = [];
|
|
176
|
+
}
|
|
177
|
+
else if (kind === 'text') {
|
|
178
|
+
if (row.length !== 3 || nat(row[1]) !== stringId || typeof row[2] !== 'string' || row[2].length > TEXT)
|
|
179
|
+
throw Error('invalid graph text');
|
|
180
|
+
stringParts.push(row[2]);
|
|
181
|
+
}
|
|
182
|
+
else if (kind === 'string-end') {
|
|
183
|
+
if (row.length !== 2 || nat(row[1]) !== stringId)
|
|
184
|
+
throw Error('invalid graph string end');
|
|
185
|
+
strings[stringId] = stringParts.join('');
|
|
186
|
+
stringId = -1;
|
|
187
|
+
stringParts = [];
|
|
188
|
+
}
|
|
189
|
+
else if (['prop', 'map', 'set', 'meta', 'seal'].includes(String(kind))) {
|
|
190
|
+
const id = nat(row[1]), sizes = { prop: 7, map: 4, set: 3, meta: 3, seal: 4 };
|
|
191
|
+
if (id !== open || row.length !== sizes[String(kind)])
|
|
192
|
+
throw Error('graph edge outside its node');
|
|
193
|
+
if (kind === 'seal') {
|
|
194
|
+
seals[id] = [bit(row[2]), bit(row[3])];
|
|
195
|
+
open = -1;
|
|
196
|
+
}
|
|
197
|
+
if (kind === 'meta') {
|
|
198
|
+
if (metadataIds.has(id) || !onMetadata)
|
|
199
|
+
throw Error('unexpected graph metadata');
|
|
200
|
+
metadataIds.add(id);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
else
|
|
204
|
+
throw Error('unknown graph record');
|
|
205
|
+
}
|
|
206
|
+
if (!root || open !== -1 || stringId !== -1 || objects.length !== m.nodes || strings.length !== m.strings)
|
|
207
|
+
throw Error('incomplete graph records');
|
|
208
|
+
const deref = (wire) => {
|
|
209
|
+
if (!Array.isArray(wire))
|
|
210
|
+
throw Error('invalid graph reference');
|
|
211
|
+
const [kind, value] = wire;
|
|
212
|
+
if (kind === 'u' && wire.length === 1)
|
|
213
|
+
return undefined;
|
|
214
|
+
if (wire.length !== 2)
|
|
215
|
+
throw Error('invalid graph reference arity');
|
|
216
|
+
if (kind === 'v' && (value === null || typeof value === 'boolean' || typeof value === 'string' && value.length <= TEXT))
|
|
217
|
+
return value;
|
|
218
|
+
if (kind === 'n' && typeof value === 'string' && /^[0-9a-f]{16}$/.test(value)) {
|
|
219
|
+
const n = Buffer.from(value, 'hex').readDoubleBE(), check = Buffer.alloc(8);
|
|
220
|
+
check.writeDoubleBE(n);
|
|
221
|
+
if (check.toString('hex') !== value)
|
|
222
|
+
throw Error('numeric bit pattern not preserved by this runtime');
|
|
223
|
+
return n;
|
|
224
|
+
}
|
|
225
|
+
if (kind === 'b' && typeof value === 'string' && /^-?(0|[1-9][0-9]*)$/.test(value))
|
|
226
|
+
return BigInt(value);
|
|
227
|
+
const i = nat(value);
|
|
228
|
+
if (kind === 'r' && i < objects.length)
|
|
229
|
+
return objects[i];
|
|
230
|
+
if (kind === 's' && i < strings.length)
|
|
231
|
+
return strings[i];
|
|
232
|
+
throw Error('unknown graph reference');
|
|
233
|
+
};
|
|
234
|
+
const metadata = [];
|
|
235
|
+
for (const row of records(directory, m)) {
|
|
236
|
+
const kind = row[0];
|
|
237
|
+
if (['root', 'node', 'string', 'text', 'string-end', 'seal'].includes(String(kind)))
|
|
238
|
+
continue;
|
|
239
|
+
const id = nat(row[1]), object = objects[id];
|
|
240
|
+
if (kind === 'prop') {
|
|
241
|
+
const key = deref(row[2]);
|
|
242
|
+
if (typeof key !== 'string' || Object.hasOwn(object, key))
|
|
243
|
+
throw Error('invalid or duplicate graph property');
|
|
244
|
+
if (tags[id] === 'array' && /^(0|[1-9][0-9]*)$/.test(key) && Number(key) < 0xffffffff && Number(key) >= arrays[id])
|
|
245
|
+
throw Error('array property past length');
|
|
246
|
+
Object.defineProperty(object, key, { value: deref(row[3]), writable: bit(row[4]), enumerable: bit(row[5]), configurable: bit(row[6]) });
|
|
247
|
+
}
|
|
248
|
+
else if (kind === 'map') {
|
|
249
|
+
if (tags[id] !== 'map')
|
|
250
|
+
throw Error('invalid map node');
|
|
251
|
+
const map = object, key = deref(row[2]);
|
|
252
|
+
if (map.has(key))
|
|
253
|
+
throw Error('duplicate map entry');
|
|
254
|
+
map.set(key, deref(row[3]));
|
|
255
|
+
}
|
|
256
|
+
else if (kind === 'set') {
|
|
257
|
+
if (tags[id] !== 'set')
|
|
258
|
+
throw Error('invalid set node');
|
|
259
|
+
const set = object, value = deref(row[2]);
|
|
260
|
+
if (set.has(value))
|
|
261
|
+
throw Error('duplicate set entry');
|
|
262
|
+
set.add(value);
|
|
263
|
+
}
|
|
264
|
+
else if (kind === 'meta')
|
|
265
|
+
metadata.push([object, deref(row[2])]);
|
|
266
|
+
else
|
|
267
|
+
throw Error('unknown graph edge');
|
|
268
|
+
}
|
|
269
|
+
for (let i = 0; i < objects.length; i++) {
|
|
270
|
+
const [extensible, lengthWritable] = seals[i];
|
|
271
|
+
if (tags[i] === 'array')
|
|
272
|
+
Object.defineProperty(objects[i], 'length', { writable: lengthWritable });
|
|
273
|
+
if (!extensible)
|
|
274
|
+
Object.preventExtensions(objects[i]);
|
|
275
|
+
}
|
|
276
|
+
for (const [object, value] of metadata)
|
|
277
|
+
onMetadata(object, value);
|
|
278
|
+
return deref(root);
|
|
279
|
+
}
|
|
280
|
+
export function readFamilyCapsule(directory, expectedSha256, limits, onMetadata) {
|
|
281
|
+
const bytes = boundedFile(join(directory, 'CAPTURE.json'), 1024 * 1024);
|
|
282
|
+
if (!/^[0-9a-f]{64}$/.test(expectedSha256) || hash(bytes) !== expectedSha256)
|
|
283
|
+
throw Error('capture manifest binding mismatch');
|
|
284
|
+
const manifest = JSON.parse(bytes.toString());
|
|
285
|
+
if (manifest.schema !== 'bapa-native-certificate/v1' || manifest.complete !== true || !['counted-family', 'plain', 'inferred', 'witness'].includes(manifest.route))
|
|
286
|
+
throw Error('incomplete final-family capture');
|
|
287
|
+
const data = readCertificateGraph(join(directory, 'graph'), manifest.graphManifestSha256, limits, onMetadata);
|
|
288
|
+
if (!data || typeof data !== 'object' || data.route !== manifest.route || JSON.stringify(data.identity) !== JSON.stringify(manifest.identity) || data.terms?.terms.length !== manifest.terms || data.verdict?.proven !== true || data.verdict.checked !== true)
|
|
289
|
+
throw Error('capture envelope mismatch');
|
|
290
|
+
if (JSON.stringify(data.projection) !== JSON.stringify(projectionOf(data)) || JSON.stringify(data.projection) !== JSON.stringify(manifest.projection))
|
|
291
|
+
throw Error('projection qualification mismatch');
|
|
292
|
+
if (data.route === 'counted-family') {
|
|
293
|
+
if (data.schema !== 'bapa-family/v1' || data.final.checked !== true || data.final.laws.length !== manifest.familyLaws || JSON.stringify(Object.keys(data.final.proof.certs)) !== JSON.stringify(manifest.certificateKeys) || JSON.stringify(retainedLawCoverage(data.original.laws, data.final.laws)) !== JSON.stringify(manifest.publicLawIndices) || JSON.stringify(data.publicLawIndices) !== JSON.stringify(manifest.publicLawIndices))
|
|
294
|
+
throw Error('family manifest mismatch');
|
|
295
|
+
}
|
|
296
|
+
else if (data.schema !== 'bapa-proof/v1' || inspectProvenance(data.proof).route !== data.route)
|
|
297
|
+
throw Error('producer manifest mismatch');
|
|
298
|
+
return { manifest, data };
|
|
299
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { NativeBundle } from './proof-capture.js';
|
|
2
|
+
import { type PartOptions } from './proof-parts.js';
|
|
3
|
+
export declare function countedSourceProject(id: string, input: NativeBundle, options: PartOptions): {
|
|
4
|
+
project: Record<string, string>;
|
|
5
|
+
sourceTheorems: string[];
|
|
6
|
+
publicLawIndices: number[];
|
|
7
|
+
parts: {
|
|
8
|
+
familySize: number;
|
|
9
|
+
primaryObligations: number;
|
|
10
|
+
frontiers: {
|
|
11
|
+
law: string;
|
|
12
|
+
phase: "start" | "step";
|
|
13
|
+
frontier: import("./proof-frontier.ts").Frontier;
|
|
14
|
+
}[];
|
|
15
|
+
};
|
|
16
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { countedSourceDefinitions } from "./counted-source-data.js";
|
|
2
|
+
import { emitCountedParts } from "./proof-parts.js";
|
|
3
|
+
import { leanSource, liftProject as leanProject } from "./project.js";
|
|
4
|
+
export function countedSourceProject(id, input, options) {
|
|
5
|
+
if (input.route !== 'counted-family')
|
|
6
|
+
throw Error('counted source projection needs a complete counted family');
|
|
7
|
+
const bundle = input, data = countedSourceDefinitions(id, bundle), parts = emitCountedParts(id, bundle, options);
|
|
8
|
+
const sourceTheorems = data.publicLawIndices.map((_, j) => `${id}_source_law${j}_holds`);
|
|
9
|
+
// CDecl deliberately has no DecidableEq instance. Eliminate finite membership
|
|
10
|
+
// hypotheses by constructors rather than asking decide to compare declarations.
|
|
11
|
+
const membershipCases = (list, count, rest) => `by\n intro d hd${rest.startsWith('PUBLIC:') ? ' hn' : ''}\n simp only [${list}, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hd\n ${count ? `rcases hd with ${Array(count).fill('rfl').join(' | ')}\n all_goals ${rest.replace('PUBLIC:', '')}` : ''}`;
|
|
12
|
+
const sourceMembership = membershipCases(id + '_source_decls', Object.keys(bundle.original.machine.counts ?? {}).length, `simp [${id}_decls]`);
|
|
13
|
+
const publicMembership = membershipCases(id + '_decls', Object.keys(bundle.final.machine.counts ?? {}).length, `PUBLIC:simp_all [${id}_source_count_maps, ${id}_source_decls]`);
|
|
14
|
+
const publicProofs = data.publicLawIndices.map((k, j) => {
|
|
15
|
+
const entry = `${id}_entry${k}`;
|
|
16
|
+
return `theorem ${id}_source_law${j}_eq : ${id}_source_law${j} = ${entry}.e.law := by rfl
|
|
17
|
+
theorem ${id}_source_keys${j}_eq : ${id}_source_keys${j} = ${entry}.e.keys2 := by rfl
|
|
18
|
+
theorem ${id}_source_law${j}_holds (ρ M) (hr : CReach ${id}_source ${id}_source_decls ρ M) :
|
|
19
|
+
holdsOver ${id}_source_law${j} (withCnt ${id}_source_decls ρ M) ${id}_source_keys${j} ρ := by
|
|
20
|
+
have hq := (QAlpha.renameAll_equiv ${id}_source ${id}_source_renames ${id}_source_alpha_valid).symm.creach_imp ${id}_source_decls hr
|
|
21
|
+
have hr' : CReach ${id}_source_alpha ${id}_source_decls ρ M :=
|
|
22
|
+
(RowAlpha.renameAll_equiv ${id}_source_qalpha ${id}_source_row_renames ${id}_source_row_valid).symm.creach_imp ${id}_source_decls hq
|
|
23
|
+
have h := bapa_counted_source_law ${id}_source_alpha ${id}_source_decls ${id}_decls
|
|
24
|
+
${id}_source_count_maps ${id}_source_count_maps ${entry}.e
|
|
25
|
+
(by decide +kernel) (by decide +kernel) (by decide +kernel)
|
|
26
|
+
(by decide +kernel) (by decide +kernel) (by decide +kernel)
|
|
27
|
+
(${sourceMembership}) (${publicMembership}) (stepAvoids_of_check _ _ (by decide +kernel))
|
|
28
|
+
(by decide +kernel)
|
|
29
|
+
(by
|
|
30
|
+
intro X N hreach
|
|
31
|
+
rw [${id}_source_shape] at hreach
|
|
32
|
+
exact ${id}_laws X N hreach ${entry} (by simp [${id}_family])) ρ M hr'
|
|
33
|
+
simpa only [SEntry.Holds, ${id}_source_law${j}_eq, ${id}_source_keys${j}_eq] using h
|
|
34
|
+
#print axioms ${id}_source_law${j}_holds`;
|
|
35
|
+
}).join('\n');
|
|
36
|
+
const body = `import RowAlpha
|
|
37
|
+
import CountedSourceBridge
|
|
38
|
+
import ${parts.finalModule}
|
|
39
|
+
open Kern Cert
|
|
40
|
+
set_option maxHeartbeats ${options.heartbeats}
|
|
41
|
+
set_option maxRecDepth ${options.recursionDepth}
|
|
42
|
+
${data.sourceDefs}
|
|
43
|
+
def ${id}_source_qalpha : SMachine := QAlpha.renameAll ${id}_source ${id}_source_renames
|
|
44
|
+
def ${id}_source_alpha : SMachine := RowAlpha.renameAll ${id}_source_qalpha ${id}_source_row_renames
|
|
45
|
+
theorem ${id}_source_alpha_valid : QAlpha.validAll ${id}_source ${id}_source_renames = true := by decide +kernel
|
|
46
|
+
theorem ${id}_source_row_valid : RowAlpha.validAll ${id}_source_qalpha ${id}_source_row_renames = true := by decide +kernel
|
|
47
|
+
theorem ${id}_source_shape : {${id}_source_alpha with carried := ${id}_source_alpha.carried ++ ${id}_source_count_maps} = ${id}_machine := by rfl
|
|
48
|
+
${publicProofs}
|
|
49
|
+
#print axioms ${id}_laws
|
|
50
|
+
#print axioms ${id}_source_alpha_valid
|
|
51
|
+
#print axioms ${id}_source_row_valid
|
|
52
|
+
#print axioms ${id}_source_shape
|
|
53
|
+
`;
|
|
54
|
+
if (/\b(?:sorry|axiom|native_decide)\b/.test(body))
|
|
55
|
+
throw Error('unapproved counted source construct');
|
|
56
|
+
const defs = parts.files.find(f => f.role === 'definitions');
|
|
57
|
+
if (!defs)
|
|
58
|
+
throw Error('split definitions missing');
|
|
59
|
+
const project = leanProject(defs.text, { name: 'BapaProof' });
|
|
60
|
+
for (const f of parts.files)
|
|
61
|
+
project[f.module + '.lean'] = f.text;
|
|
62
|
+
for (const name of ['FamilyParts', 'CountedSourceProjection', 'CountedNoninterference', 'CountedSourceBridge'])
|
|
63
|
+
project[name + '.lean'] = leanSource(name);
|
|
64
|
+
project['BapaProof.lean'] = body;
|
|
65
|
+
const modules = Object.keys(project).filter(n => n.endsWith('.lean') && n !== 'lakefile.lean').map(n => n.slice(0, -5));
|
|
66
|
+
project['lakefile.lean'] = 'import Lake\nopen Lake DSL\npackage bapasource\n@[default_target]\nlean_lib BapaSource where\n roots := #[' + modules.map(n => '`' + n).join(', ') + ']\n';
|
|
67
|
+
return { project, sourceTheorems, publicLawIndices: data.publicLawIndices, parts: { familySize: parts.familySize, primaryObligations: parts.primaryObligations, frontiers: parts.frontiers } };
|
|
68
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Tree, CountCert } from '../../kernel/kernel.js';
|
|
2
|
+
export type Piece = {
|
|
3
|
+
address: string;
|
|
4
|
+
weight: string;
|
|
5
|
+
};
|
|
6
|
+
export type Frontier = {
|
|
7
|
+
schema: 'final29-frontier/v1';
|
|
8
|
+
proxy: 'structure-v1';
|
|
9
|
+
limit: string;
|
|
10
|
+
contextWeight: string;
|
|
11
|
+
pieces: Piece[];
|
|
12
|
+
};
|
|
13
|
+
export declare function structuralWeight(root: unknown): bigint;
|
|
14
|
+
export declare function makeFrontier(root: Tree, limit: bigint, contextWeight: bigint): Frontier;
|
|
15
|
+
export declare function verifyFrontier(root: Tree, frontier: Frontier): void;
|
|
16
|
+
export declare function certificateTrees(cert: CountCert): {
|
|
17
|
+
role: string;
|
|
18
|
+
tree: Tree;
|
|
19
|
+
}[];
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
const natural = (v) => { if (v < 0n)
|
|
2
|
+
throw Error('negative structural weight'); return v; };
|
|
3
|
+
function scalarSize(x) {
|
|
4
|
+
if (x === null || x === undefined)
|
|
5
|
+
return 1n;
|
|
6
|
+
if (typeof x === 'string')
|
|
7
|
+
return 1n + BigInt(x.length);
|
|
8
|
+
if (typeof x === 'number' || typeof x === 'boolean' || typeof x === 'bigint')
|
|
9
|
+
return 1n;
|
|
10
|
+
throw Error('non-data structural value');
|
|
11
|
+
}
|
|
12
|
+
export function structuralWeight(root) {
|
|
13
|
+
let weight = 0n;
|
|
14
|
+
const seen = new Set(), stack = [root];
|
|
15
|
+
while (stack.length) {
|
|
16
|
+
const x = stack.pop();
|
|
17
|
+
if (x === null || typeof x !== 'object') {
|
|
18
|
+
weight += scalarSize(x);
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
if (seen.has(x))
|
|
22
|
+
continue;
|
|
23
|
+
seen.add(x);
|
|
24
|
+
weight++;
|
|
25
|
+
if (x instanceof Map) {
|
|
26
|
+
for (const [k, v] of x)
|
|
27
|
+
stack.push(k, v);
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (x instanceof Set) {
|
|
31
|
+
for (const v of x)
|
|
32
|
+
stack.push(v);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
for (const key of Reflect.ownKeys(x)) {
|
|
36
|
+
if (typeof key !== 'string')
|
|
37
|
+
throw Error('symbol structural property');
|
|
38
|
+
const d = Object.getOwnPropertyDescriptor(x, key);
|
|
39
|
+
if (!('value' in d))
|
|
40
|
+
throw Error('accessor structural property');
|
|
41
|
+
weight += 1n + BigInt(key.length);
|
|
42
|
+
stack.push(d.value);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return weight;
|
|
46
|
+
}
|
|
47
|
+
function weights(root) {
|
|
48
|
+
const out = new Map(), active = new Set(), todo = [[root, false]];
|
|
49
|
+
while (todo.length) {
|
|
50
|
+
const [node, done] = todo.pop();
|
|
51
|
+
if (done) {
|
|
52
|
+
active.delete(node);
|
|
53
|
+
out.set(node, 'split' in node ? 1n + structuralWeight(node.split) + out.get(node.yes) + out.get(node.no) : structuralWeight(node));
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (active.has(node))
|
|
57
|
+
throw Error('cyclic certificate tree');
|
|
58
|
+
if (out.has(node))
|
|
59
|
+
continue;
|
|
60
|
+
if (!node || typeof node !== 'object' || (!('split' in node) && !('leaf' in node)))
|
|
61
|
+
throw Error('invalid certificate tree');
|
|
62
|
+
active.add(node);
|
|
63
|
+
todo.push([node, true]);
|
|
64
|
+
if ('split' in node)
|
|
65
|
+
todo.push([node.no, false], [node.yes, false]);
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
export function makeFrontier(root, limit, contextWeight) {
|
|
70
|
+
if (limit <= 0n)
|
|
71
|
+
throw Error('positive piece limit required');
|
|
72
|
+
natural(contextWeight);
|
|
73
|
+
const all = weights(root), pieces = [], todo = [[root, '']];
|
|
74
|
+
while (todo.length) {
|
|
75
|
+
const [node, address] = todo.pop(), weight = all.get(node) + contextWeight + 2n * BigInt(address.length);
|
|
76
|
+
if (weight <= limit)
|
|
77
|
+
pieces.push({ address, weight: String(weight) });
|
|
78
|
+
else if ('split' in node)
|
|
79
|
+
todo.push([node.no, address + '0'], [node.yes, address + '1']);
|
|
80
|
+
else
|
|
81
|
+
throw Error(`oversized leaf/context at ${address}; separate reviewed decomposition required`);
|
|
82
|
+
}
|
|
83
|
+
return { schema: 'final29-frontier/v1', proxy: 'structure-v1', limit: String(limit), contextWeight: String(contextWeight), pieces };
|
|
84
|
+
}
|
|
85
|
+
// Independent coverage walk. Does not trust generator node weights, leaf count,
|
|
86
|
+
// claimed paths or a fixed 2^depth table. Stops at each real leaf, never below it.
|
|
87
|
+
export function verifyFrontier(root, frontier) {
|
|
88
|
+
if (frontier.schema !== 'final29-frontier/v1' || frontier.proxy !== 'structure-v1' || !/^[1-9][0-9]*$/.test(frontier.limit) || !/^(0|[1-9][0-9]*)$/.test(frontier.contextWeight))
|
|
89
|
+
throw Error('invalid frontier header');
|
|
90
|
+
const limit = BigInt(frontier.limit), context = BigInt(frontier.contextWeight), byAddress = new Map();
|
|
91
|
+
for (const p of frontier.pieces) {
|
|
92
|
+
if (!/^[01]*$/.test(p.address) || !/^[1-9][0-9]*$/.test(p.weight) || byAddress.has(p.address))
|
|
93
|
+
throw Error('invalid or duplicate frontier address');
|
|
94
|
+
byAddress.set(p.address, p);
|
|
95
|
+
}
|
|
96
|
+
if (!byAddress.size)
|
|
97
|
+
throw Error('empty frontier');
|
|
98
|
+
for (const address of byAddress.keys())
|
|
99
|
+
for (let n = 0; n < address.length; n++)
|
|
100
|
+
if (byAddress.has(address.slice(0, n)))
|
|
101
|
+
throw Error('frontier is not prefix-free');
|
|
102
|
+
// Recompute subtree weights independently, preserving multiplicity of shared
|
|
103
|
+
// subtree occurrences (the proof checks an occurrence on each distinct path).
|
|
104
|
+
const score = (start) => {
|
|
105
|
+
let value = 0n;
|
|
106
|
+
const todo = [start], active = new Set(), walk = [];
|
|
107
|
+
walk.push([start, false]);
|
|
108
|
+
void todo;
|
|
109
|
+
while (walk.length) {
|
|
110
|
+
const [n, leave] = walk.pop();
|
|
111
|
+
if (leave) {
|
|
112
|
+
active.delete(n);
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (active.has(n))
|
|
116
|
+
throw Error('cyclic certificate tree');
|
|
117
|
+
active.add(n);
|
|
118
|
+
walk.push([n, true]);
|
|
119
|
+
if ('split' in n) {
|
|
120
|
+
value += 1n + structuralWeight(n.split);
|
|
121
|
+
walk.push([n.no, false], [n.yes, false]);
|
|
122
|
+
}
|
|
123
|
+
else if ('leaf' in n)
|
|
124
|
+
value += structuralWeight(n);
|
|
125
|
+
else
|
|
126
|
+
throw Error('invalid tree');
|
|
127
|
+
}
|
|
128
|
+
return value;
|
|
129
|
+
};
|
|
130
|
+
let consumed = 0;
|
|
131
|
+
const todo = [[root, '']], order = [];
|
|
132
|
+
while (todo.length) {
|
|
133
|
+
const [node, address] = todo.pop(), p = byAddress.get(address), weight = score(node) + context + 2n * BigInt(address.length);
|
|
134
|
+
if (p) {
|
|
135
|
+
if (weight > limit || p.weight !== String(weight))
|
|
136
|
+
throw Error('frontier weight mismatch');
|
|
137
|
+
consumed++;
|
|
138
|
+
order.push(address);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (weight <= limit)
|
|
142
|
+
throw Error('noncanonical over-split frontier');
|
|
143
|
+
if (!('split' in node))
|
|
144
|
+
throw Error('uncovered real leaf or oversized context');
|
|
145
|
+
todo.push([node.no, address + '0'], [node.yes, address + '1']);
|
|
146
|
+
}
|
|
147
|
+
if (consumed !== byAddress.size || order.some((a, i) => a !== frontier.pieces[i]?.address))
|
|
148
|
+
throw Error('extra, below-leaf, or noncanonical ordered frontier address');
|
|
149
|
+
}
|
|
150
|
+
export function certificateTrees(cert) {
|
|
151
|
+
return [{ role: 'main', tree: cert.tree }, ...(cert.lemmas ?? []).map((x, i) => ({ role: `lemma/${i}`, tree: x.tree })),
|
|
152
|
+
...(cert.monos ?? []).map((x, i) => ({ role: `mono/${i}`, tree: x.tree })), ...(cert.lins ?? []).map((x, i) => ({ role: `lin/${i}`, tree: x.tree }))];
|
|
153
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type Manifest = {
|
|
2
|
+
schema: 'closing-graph/v1';
|
|
3
|
+
complete: true;
|
|
4
|
+
nodes: number;
|
|
5
|
+
strings: number;
|
|
6
|
+
shards: {
|
|
7
|
+
name: string;
|
|
8
|
+
bytes: number;
|
|
9
|
+
sha256: string;
|
|
10
|
+
}[];
|
|
11
|
+
};
|
|
12
|
+
export type GraphOptions = {
|
|
13
|
+
prototypes?: Record<string, object>;
|
|
14
|
+
metadata?: (object: object) => unknown;
|
|
15
|
+
restoreMetadata?: (object: object, value: unknown) => void;
|
|
16
|
+
};
|
|
17
|
+
export declare function writeGraphCapsule(directory: string, root: unknown, options?: GraphOptions): Manifest;
|