@agent-blueprint-protocol/verifier 0.7.0
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/CHANGELOG.md +891 -0
- package/LICENSE +190 -0
- package/NOTICE +4 -0
- package/README.md +199 -0
- package/dist/corpus/cases/base64url-decode.json +1 -0
- package/dist/corpus/cases/blueprint-decode.json +1 -0
- package/dist/corpus/cases/bounds-new.json +1 -0
- package/dist/corpus/cases/bounds_algebra-intersect.json +1 -0
- package/dist/corpus/cases/canonicalization-encode.json +1 -0
- package/dist/corpus/cases/compatibility-verify.json +1 -0
- package/dist/corpus/cases/deployment-decode.json +1 -0
- package/dist/corpus/cases/digest-tagged.json +1 -0
- package/dist/corpus/cases/extension-resolve.json +1 -0
- package/dist/corpus/cases/federation-decode.json +1 -0
- package/dist/corpus/cases/federation-verify_commitment.json +1 -0
- package/dist/corpus/cases/json-decode.json +1 -0
- package/dist/corpus/cases/negotiation-negotiate.json +1 -0
- package/dist/corpus/cases/portability-scan.json +1 -0
- package/dist/corpus/cases/schema-validate_instance.json +1 -0
- package/dist/corpus/cases/signature-verify.json +1 -0
- package/dist/corpus/index.json +1 -0
- package/dist/corpus/schemas/estate-contract.schema.json +1 -0
- package/dist/corpus/schemas/object.schema.json +1 -0
- package/dist/corpus/vectors/blueprint-golden.json +1 -0
- package/dist/corpus/vectors/deployment-golden.json +1 -0
- package/dist/corpus/vectors/rfc8785-numbers.json +1 -0
- package/dist/kit/cli.mjs +24 -0
- package/dist/verifier/b64url.d.ts +16 -0
- package/dist/verifier/b64url.js +36 -0
- package/dist/verifier/blueprint.d.ts +56 -0
- package/dist/verifier/blueprint.js +795 -0
- package/dist/verifier/bounds.d.ts +19 -0
- package/dist/verifier/bounds.js +45 -0
- package/dist/verifier/bounds_algebra.d.ts +27 -0
- package/dist/verifier/bounds_algebra.js +282 -0
- package/dist/verifier/canonical.d.ts +17 -0
- package/dist/verifier/canonical.js +160 -0
- package/dist/verifier/cli.d.ts +1 -0
- package/dist/verifier/cli.js +149 -0
- package/dist/verifier/compatibility.d.ts +22 -0
- package/dist/verifier/compatibility.js +112 -0
- package/dist/verifier/corpus.d.ts +42 -0
- package/dist/verifier/corpus.js +672 -0
- package/dist/verifier/decode.d.ts +16 -0
- package/dist/verifier/decode.js +457 -0
- package/dist/verifier/deployment.d.ts +49 -0
- package/dist/verifier/deployment.js +830 -0
- package/dist/verifier/digest.d.ts +23 -0
- package/dist/verifier/digest.js +58 -0
- package/dist/verifier/extension.d.ts +11 -0
- package/dist/verifier/extension.js +75 -0
- package/dist/verifier/federation.d.ts +76 -0
- package/dist/verifier/federation.js +817 -0
- package/dist/verifier/negotiation.d.ts +29 -0
- package/dist/verifier/negotiation.js +324 -0
- package/dist/verifier/portability.d.ts +12 -0
- package/dist/verifier/portability.js +184 -0
- package/dist/verifier/predicate.d.ts +10 -0
- package/dist/verifier/predicate.js +172 -0
- package/dist/verifier/registry.d.ts +16 -0
- package/dist/verifier/registry.js +120 -0
- package/dist/verifier/registry_engine.d.ts +42 -0
- package/dist/verifier/registry_engine.js +220 -0
- package/dist/verifier/report.d.ts +13 -0
- package/dist/verifier/report.js +29 -0
- package/dist/verifier/run_corpus.d.ts +1 -0
- package/dist/verifier/run_corpus.js +81 -0
- package/dist/verifier/runner.d.ts +20 -0
- package/dist/verifier/runner.js +778 -0
- package/dist/verifier/schema.d.ts +20 -0
- package/dist/verifier/schema.js +623 -0
- package/dist/verifier/self_checks.d.ts +1 -0
- package/dist/verifier/self_checks.js +316 -0
- package/dist/verifier/signature.d.ts +23 -0
- package/dist/verifier/signature.js +226 -0
- package/dist/verifier/value.d.ts +44 -0
- package/dist/verifier/value.js +77 -0
- package/package.json +42 -0
|
@@ -0,0 +1,672 @@
|
|
|
1
|
+
// The conformance-corpus loader and integrity verifier — the mirror of
|
|
2
|
+
// AgentBlueprintProtocol.Conformance.Corpus. No I/O: load() takes a path→bytes
|
|
3
|
+
// map; the CLI owns the filesystem. Every check is two-directional and fails
|
|
4
|
+
// with the twin's typed, value-free codes (exit 2 at the CLI):
|
|
5
|
+
//
|
|
6
|
+
// index absent/undecodable/bad structure/non-canonical bytes/digest mismatch
|
|
7
|
+
// → corpus_index_invalid
|
|
8
|
+
// case file undecodable/wrong shape; case structure incl. malformed tamper,
|
|
9
|
+
// verbatim ≠ derived, lying raw_file reference → corpus_case_invalid
|
|
10
|
+
// per-file SHA-256 ≠ index → corpus_hash_mismatch
|
|
11
|
+
// file-set inequality (either direction) → corpus_file_set_mismatch
|
|
12
|
+
// case-id repeat or non-binary → corpus_case_id_duplicate
|
|
13
|
+
// per-file or total count disagreement → corpus_count_mismatch
|
|
14
|
+
// applicability shape / required-cell / observed-count failure
|
|
15
|
+
// → corpus_applicability_incomplete
|
|
16
|
+
// zero cases total → corpus_empty
|
|
17
|
+
//
|
|
18
|
+
// Integrity works over TAGGED values (pair-array members) so hostile member
|
|
19
|
+
// names never touch JS object semantics.
|
|
20
|
+
import * as b64 from "./b64url.js";
|
|
21
|
+
import { encode } from "./canonical.js";
|
|
22
|
+
import { decode } from "./decode.js";
|
|
23
|
+
import * as digest from "./digest.js";
|
|
24
|
+
import { registryDigest } from "./registry.js";
|
|
25
|
+
const INDEX_FORMAT = "agent-blueprint-protocol-conformance-corpus-index";
|
|
26
|
+
const CASE_FILE_FORMAT = "agent-blueprint-protocol-conformance-cases";
|
|
27
|
+
export const SURFACES = [
|
|
28
|
+
"json.decode",
|
|
29
|
+
"canonicalization.encode",
|
|
30
|
+
"base64url.decode",
|
|
31
|
+
"digest.tagged",
|
|
32
|
+
"signature.verify",
|
|
33
|
+
"schema.validate_instance",
|
|
34
|
+
"blueprint.decode",
|
|
35
|
+
"deployment.decode",
|
|
36
|
+
"negotiation.negotiate",
|
|
37
|
+
"extension.resolve",
|
|
38
|
+
"bounds.new",
|
|
39
|
+
"bounds_algebra.intersect",
|
|
40
|
+
"portability.scan",
|
|
41
|
+
"compatibility.verify",
|
|
42
|
+
"federation.decode",
|
|
43
|
+
"federation.verify_commitment",
|
|
44
|
+
];
|
|
45
|
+
export const CLASSES = [
|
|
46
|
+
"valid",
|
|
47
|
+
"boundary_near",
|
|
48
|
+
"exact_bound",
|
|
49
|
+
"maximum_plus_one",
|
|
50
|
+
"invalid_encoding",
|
|
51
|
+
"invalid_duplicate",
|
|
52
|
+
"invalid_type",
|
|
53
|
+
"invalid_constraint",
|
|
54
|
+
"invalid_cardinality",
|
|
55
|
+
"unknown_member",
|
|
56
|
+
"tamper_meaningful_byte",
|
|
57
|
+
"digest_mismatch",
|
|
58
|
+
"signature_invalid",
|
|
59
|
+
"revision_above_max",
|
|
60
|
+
"revision_below_min",
|
|
61
|
+
"required_field_unsupported",
|
|
62
|
+
"required_field_not_covered",
|
|
63
|
+
"extension_unknown_critical",
|
|
64
|
+
"extension_unknown_optional_roundtrip",
|
|
65
|
+
"extension_criticality_conflict",
|
|
66
|
+
"extension_schema_unavailable",
|
|
67
|
+
"extension_deprecated_retained",
|
|
68
|
+
"bound_widening_operational",
|
|
69
|
+
"bound_widening_protected",
|
|
70
|
+
"forbidden_portable_value",
|
|
71
|
+
"compatibility_range_rejected",
|
|
72
|
+
"binding_stale",
|
|
73
|
+
"federation_state_unmappable",
|
|
74
|
+
"federation_terminal_conflict",
|
|
75
|
+
"audience_mismatch",
|
|
76
|
+
"terminal_equivocation",
|
|
77
|
+
];
|
|
78
|
+
// The pinned 16×29 floor: per surface, required classes + the falsifiable
|
|
79
|
+
// n_a reason covering its complement.
|
|
80
|
+
const FLOOR = {
|
|
81
|
+
"json.decode": {
|
|
82
|
+
required: [
|
|
83
|
+
"valid",
|
|
84
|
+
"boundary_near",
|
|
85
|
+
"exact_bound",
|
|
86
|
+
"maximum_plus_one",
|
|
87
|
+
"invalid_encoding",
|
|
88
|
+
"invalid_duplicate",
|
|
89
|
+
"invalid_type",
|
|
90
|
+
],
|
|
91
|
+
n_a: "all artifact/negotiation/signature/bounds/federation classes - no such semantics reach the byte decoder",
|
|
92
|
+
},
|
|
93
|
+
"canonicalization.encode": {
|
|
94
|
+
required: [
|
|
95
|
+
"valid",
|
|
96
|
+
"boundary_near",
|
|
97
|
+
"exact_bound",
|
|
98
|
+
"maximum_plus_one",
|
|
99
|
+
"invalid_encoding",
|
|
100
|
+
"tamper_meaningful_byte",
|
|
101
|
+
],
|
|
102
|
+
n_a: "decode-side and artifact-layer classes - encoder consumes the tagged algebra only",
|
|
103
|
+
},
|
|
104
|
+
"base64url.decode": {
|
|
105
|
+
required: ["valid", "invalid_encoding", "exact_bound"],
|
|
106
|
+
n_a: "all others - pure codec",
|
|
107
|
+
},
|
|
108
|
+
"digest.tagged": {
|
|
109
|
+
required: ["valid", "invalid_encoding", "digest_mismatch", "tamper_meaningful_byte"],
|
|
110
|
+
n_a: "artifact/negotiation classes - digest layer sees bytes + tags only",
|
|
111
|
+
},
|
|
112
|
+
"signature.verify": {
|
|
113
|
+
required: [
|
|
114
|
+
"valid",
|
|
115
|
+
"signature_invalid",
|
|
116
|
+
"tamper_meaningful_byte",
|
|
117
|
+
"invalid_encoding",
|
|
118
|
+
"invalid_constraint",
|
|
119
|
+
],
|
|
120
|
+
n_a: "bounds/federation/artifact classes - verification sees JWS inputs only",
|
|
121
|
+
},
|
|
122
|
+
"schema.validate_instance": {
|
|
123
|
+
required: ["valid", "invalid_type", "invalid_constraint", "maximum_plus_one", "invalid_cardinality"],
|
|
124
|
+
n_a: "signature/digest/federation classes - instance validation is schema-local",
|
|
125
|
+
},
|
|
126
|
+
"blueprint.decode": {
|
|
127
|
+
required: [
|
|
128
|
+
"valid",
|
|
129
|
+
"unknown_member",
|
|
130
|
+
"invalid_type",
|
|
131
|
+
"invalid_constraint",
|
|
132
|
+
"invalid_cardinality",
|
|
133
|
+
"maximum_plus_one",
|
|
134
|
+
"forbidden_portable_value",
|
|
135
|
+
"tamper_meaningful_byte",
|
|
136
|
+
],
|
|
137
|
+
n_a: "federation/compatibility classes - deployment-side; revision classes owned by negotiation surface",
|
|
138
|
+
},
|
|
139
|
+
"deployment.decode": {
|
|
140
|
+
required: [
|
|
141
|
+
"valid",
|
|
142
|
+
"unknown_member",
|
|
143
|
+
"invalid_type",
|
|
144
|
+
"invalid_constraint",
|
|
145
|
+
"invalid_cardinality",
|
|
146
|
+
"maximum_plus_one",
|
|
147
|
+
"forbidden_portable_value",
|
|
148
|
+
"tamper_meaningful_byte",
|
|
149
|
+
"digest_mismatch",
|
|
150
|
+
"binding_stale",
|
|
151
|
+
"compatibility_range_rejected",
|
|
152
|
+
],
|
|
153
|
+
n_a: "federation state classes - no task semantics",
|
|
154
|
+
},
|
|
155
|
+
"negotiation.negotiate": {
|
|
156
|
+
required: [
|
|
157
|
+
"valid",
|
|
158
|
+
"revision_above_max",
|
|
159
|
+
"revision_below_min",
|
|
160
|
+
"required_field_unsupported",
|
|
161
|
+
"required_field_not_covered",
|
|
162
|
+
"extension_unknown_critical",
|
|
163
|
+
"extension_criticality_conflict",
|
|
164
|
+
"extension_schema_unavailable",
|
|
165
|
+
"invalid_constraint",
|
|
166
|
+
"invalid_cardinality",
|
|
167
|
+
],
|
|
168
|
+
n_a: "byte/signature classes - negotiation consumes decoded headers",
|
|
169
|
+
},
|
|
170
|
+
"extension.resolve": {
|
|
171
|
+
required: [
|
|
172
|
+
"valid",
|
|
173
|
+
"extension_unknown_critical",
|
|
174
|
+
"extension_unknown_optional_roundtrip",
|
|
175
|
+
"extension_criticality_conflict",
|
|
176
|
+
"extension_deprecated_retained",
|
|
177
|
+
"forbidden_portable_value",
|
|
178
|
+
"invalid_constraint",
|
|
179
|
+
],
|
|
180
|
+
n_a: "revision classes - owned by negotiation",
|
|
181
|
+
},
|
|
182
|
+
"bounds.new": {
|
|
183
|
+
required: ["valid", "exact_bound", "maximum_plus_one", "invalid_type"],
|
|
184
|
+
n_a: "artifact classes - parse-ceiling struct only",
|
|
185
|
+
},
|
|
186
|
+
"bounds_algebra.intersect": {
|
|
187
|
+
required: [
|
|
188
|
+
"valid",
|
|
189
|
+
"bound_widening_operational",
|
|
190
|
+
"bound_widening_protected",
|
|
191
|
+
"invalid_type",
|
|
192
|
+
"invalid_constraint",
|
|
193
|
+
],
|
|
194
|
+
n_a: "byte/signature classes - algebra consumes typed bound sets",
|
|
195
|
+
},
|
|
196
|
+
"portability.scan": {
|
|
197
|
+
required: ["valid", "forbidden_portable_value"],
|
|
198
|
+
n_a: "all others - single-purpose guard",
|
|
199
|
+
},
|
|
200
|
+
"compatibility.verify": {
|
|
201
|
+
required: ["valid", "compatibility_range_rejected", "invalid_constraint"],
|
|
202
|
+
n_a: "byte classes",
|
|
203
|
+
},
|
|
204
|
+
"federation.decode": {
|
|
205
|
+
required: [
|
|
206
|
+
"valid",
|
|
207
|
+
"federation_state_unmappable",
|
|
208
|
+
"unknown_member",
|
|
209
|
+
"invalid_type",
|
|
210
|
+
"forbidden_portable_value",
|
|
211
|
+
],
|
|
212
|
+
n_a: "bounds classes - envelope carries commitments, not bound sets",
|
|
213
|
+
},
|
|
214
|
+
"federation.verify_commitment": {
|
|
215
|
+
required: [
|
|
216
|
+
"valid",
|
|
217
|
+
"audience_mismatch",
|
|
218
|
+
"federation_terminal_conflict",
|
|
219
|
+
"terminal_equivocation",
|
|
220
|
+
"signature_invalid",
|
|
221
|
+
],
|
|
222
|
+
n_a: "decode classes - consumes decoded commitments",
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
const indexInvalid = () => ({ ok: false, e: "corpus_index_invalid" });
|
|
226
|
+
const caseInvalid = () => ({ ok: false, e: "corpus_case_invalid" });
|
|
227
|
+
export function load(map) {
|
|
228
|
+
const indexBytes = map.get("index.json");
|
|
229
|
+
if (indexBytes === undefined)
|
|
230
|
+
return indexInvalid();
|
|
231
|
+
const decodedIndex = decode(indexBytes);
|
|
232
|
+
if (!decodedIndex.ok || decodedIndex.v.t !== "obj")
|
|
233
|
+
return indexInvalid();
|
|
234
|
+
const index = decodedIndex.v;
|
|
235
|
+
const structure = verifyStructure(index);
|
|
236
|
+
if (!structure.ok)
|
|
237
|
+
return structure;
|
|
238
|
+
const canonicalBytes = verifyCanonicalBytes(index, indexBytes);
|
|
239
|
+
if (!canonicalBytes.ok)
|
|
240
|
+
return canonicalBytes;
|
|
241
|
+
const corpusDigestOk = verifyCorpusDigest(index);
|
|
242
|
+
if (!corpusDigestOk.ok)
|
|
243
|
+
return corpusDigestOk;
|
|
244
|
+
if (memberString(index, "registry_digest") !== registryDigest()) {
|
|
245
|
+
return indexInvalid();
|
|
246
|
+
}
|
|
247
|
+
const totalCases = memberNumber(index, "total_cases");
|
|
248
|
+
if (totalCases === null)
|
|
249
|
+
return indexInvalid();
|
|
250
|
+
if (totalCases === 0)
|
|
251
|
+
return { ok: false, e: "corpus_empty" };
|
|
252
|
+
const files = orderedFiles(index);
|
|
253
|
+
const loaded = loadFiles(files, map);
|
|
254
|
+
if (!loaded.ok)
|
|
255
|
+
return loaded;
|
|
256
|
+
const fileSet = verifyFileSet(files, map);
|
|
257
|
+
if (!fileSet.ok)
|
|
258
|
+
return fileSet;
|
|
259
|
+
const hashes = verifyHashes(files, map);
|
|
260
|
+
if (!hashes.ok)
|
|
261
|
+
return hashes;
|
|
262
|
+
const counts = verifyCounts(index, loaded.v.cases);
|
|
263
|
+
if (!counts.ok)
|
|
264
|
+
return counts;
|
|
265
|
+
const caseIds = verifyCaseIds(loaded.v.cases);
|
|
266
|
+
if (!caseIds.ok)
|
|
267
|
+
return caseIds;
|
|
268
|
+
const validity = verifyCaseValidity(loaded.v.cases);
|
|
269
|
+
if (!validity.ok)
|
|
270
|
+
return validity;
|
|
271
|
+
const applicability = verifyApplicability(index, loaded.v.cases);
|
|
272
|
+
if (!applicability.ok)
|
|
273
|
+
return applicability;
|
|
274
|
+
const rawBindings = verifyRawBindings(loaded.v.cases, loaded.v.raws);
|
|
275
|
+
if (!rawBindings.ok)
|
|
276
|
+
return rawBindings;
|
|
277
|
+
return {
|
|
278
|
+
ok: true,
|
|
279
|
+
v: {
|
|
280
|
+
index,
|
|
281
|
+
indexBytes,
|
|
282
|
+
cases: loaded.v.cases,
|
|
283
|
+
data: loaded.v.data,
|
|
284
|
+
raws: loaded.v.raws,
|
|
285
|
+
caseIds: new Set(loaded.v.cases.flatMap((file) => file.cases.map((c) => c.id))),
|
|
286
|
+
identity: digest.toTagged(corpusDigestOf(index)),
|
|
287
|
+
},
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
// ---- structure -------------------------------------------------------------------
|
|
291
|
+
function verifyStructure(index) {
|
|
292
|
+
if (index.t !== "obj")
|
|
293
|
+
return indexInvalid();
|
|
294
|
+
if (memberString(index, "format") !== INDEX_FORMAT)
|
|
295
|
+
return indexInvalid();
|
|
296
|
+
const revision = memberNumber(index, "protocol_revision");
|
|
297
|
+
if (revision === null || revision < 1 || !Number.isInteger(revision))
|
|
298
|
+
return indexInvalid();
|
|
299
|
+
if (memberNumber(index, "total_cases") === null)
|
|
300
|
+
return indexInvalid();
|
|
301
|
+
const fingerprints = member(index, "public_key_fingerprints");
|
|
302
|
+
if (fingerprints === null || fingerprints.t !== "arr")
|
|
303
|
+
return indexInvalid();
|
|
304
|
+
if (typeof memberString(index, "registry_digest") !== "string")
|
|
305
|
+
return indexInvalid();
|
|
306
|
+
if (typeof memberString(index, "corpus_digest") !== "string")
|
|
307
|
+
return indexInvalid();
|
|
308
|
+
const files = member(index, "files");
|
|
309
|
+
if (files === null || files.t !== "arr")
|
|
310
|
+
return indexInvalid();
|
|
311
|
+
const paths = [];
|
|
312
|
+
for (const entry of files.v) {
|
|
313
|
+
if (entry.t !== "obj")
|
|
314
|
+
return indexInvalid();
|
|
315
|
+
const path = memberString(entry, "path");
|
|
316
|
+
const hash = memberString(entry, "sha256_base64url");
|
|
317
|
+
const cases = memberNumber(entry, "cases");
|
|
318
|
+
if (typeof path !== "string" || typeof hash !== "string" || cases === null || !Number.isInteger(cases) || cases < 0) {
|
|
319
|
+
return indexInvalid();
|
|
320
|
+
}
|
|
321
|
+
paths.push(path);
|
|
322
|
+
}
|
|
323
|
+
if (new Set(paths).size !== paths.length)
|
|
324
|
+
return indexInvalid();
|
|
325
|
+
for (const path of paths) {
|
|
326
|
+
if (!pathAllowed(path))
|
|
327
|
+
return indexInvalid();
|
|
328
|
+
}
|
|
329
|
+
if (member(index, "applicability")?.t !== "obj")
|
|
330
|
+
return indexInvalid();
|
|
331
|
+
return { ok: true };
|
|
332
|
+
}
|
|
333
|
+
function pathAllowed(path) {
|
|
334
|
+
const allowed = (prefix, ext) => path.startsWith(prefix + "/") && path.endsWith(ext);
|
|
335
|
+
return (allowed("cases", ".json") ||
|
|
336
|
+
allowed("schemas", ".json") ||
|
|
337
|
+
allowed("vectors", ".json") ||
|
|
338
|
+
allowed("raw", ".raw"));
|
|
339
|
+
}
|
|
340
|
+
function verifyCanonicalBytes(index, indexBytes) {
|
|
341
|
+
const encoded = encode(index);
|
|
342
|
+
if (!encoded.ok)
|
|
343
|
+
return indexInvalid();
|
|
344
|
+
if (Buffer.from(encoded.v, "utf8").equals(indexBytes))
|
|
345
|
+
return { ok: true };
|
|
346
|
+
return indexInvalid();
|
|
347
|
+
}
|
|
348
|
+
function corpusDigestOf(index) {
|
|
349
|
+
const without = objWithout(index, "corpus_digest");
|
|
350
|
+
const encoded = encode(without);
|
|
351
|
+
if (!encoded.ok)
|
|
352
|
+
throw new Error("index must re-encode");
|
|
353
|
+
return digest.hash("corpus_index", Buffer.from(encoded.v, "utf8"));
|
|
354
|
+
}
|
|
355
|
+
function verifyCorpusDigest(index) {
|
|
356
|
+
if (digest.toTagged(corpusDigestOf(index)) === memberString(index, "corpus_digest")) {
|
|
357
|
+
return { ok: true };
|
|
358
|
+
}
|
|
359
|
+
return indexInvalid();
|
|
360
|
+
}
|
|
361
|
+
function orderedFiles(index) {
|
|
362
|
+
const files = member(index, "files");
|
|
363
|
+
// The index "files" member is an array by the structure gate upstream.
|
|
364
|
+
return files.v
|
|
365
|
+
.map((entry) => ({
|
|
366
|
+
path: memberString(entry, "path"),
|
|
367
|
+
cases: memberNumber(entry, "cases"),
|
|
368
|
+
sha: memberString(entry, "sha256_base64url"),
|
|
369
|
+
}))
|
|
370
|
+
.sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
|
|
371
|
+
}
|
|
372
|
+
function classifyFile(path) {
|
|
373
|
+
if (path.startsWith("cases/"))
|
|
374
|
+
return "cases";
|
|
375
|
+
if (path.startsWith("raw/"))
|
|
376
|
+
return "raw";
|
|
377
|
+
return "data";
|
|
378
|
+
}
|
|
379
|
+
function loadFiles(files, map) {
|
|
380
|
+
const cases = [];
|
|
381
|
+
const data = new Map();
|
|
382
|
+
const raws = new Map();
|
|
383
|
+
for (const entry of files) {
|
|
384
|
+
const bytes = map.get(entry.path);
|
|
385
|
+
if (bytes === undefined)
|
|
386
|
+
return { ok: false, e: "corpus_file_set_mismatch" };
|
|
387
|
+
const kind = classifyFile(entry.path);
|
|
388
|
+
if (kind === "raw") {
|
|
389
|
+
raws.set(entry.path, bytes);
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
const decoded = decode(bytes);
|
|
393
|
+
if (!decoded.ok)
|
|
394
|
+
return caseInvalid();
|
|
395
|
+
if (kind === "data") {
|
|
396
|
+
data.set(entry.path, decoded.v);
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
if (decoded.v.t !== "obj")
|
|
400
|
+
return caseInvalid();
|
|
401
|
+
if (memberString(decoded.v, "format") !== CASE_FILE_FORMAT)
|
|
402
|
+
return caseInvalid();
|
|
403
|
+
const caseArray = member(decoded.v, "cases");
|
|
404
|
+
if (caseArray === null || caseArray.t !== "arr")
|
|
405
|
+
return caseInvalid();
|
|
406
|
+
const parsed = [];
|
|
407
|
+
for (const raw of caseArray.v) {
|
|
408
|
+
if (raw.t !== "obj")
|
|
409
|
+
return caseInvalid();
|
|
410
|
+
const parsedCase = parseCase(raw);
|
|
411
|
+
if (parsedCase === null)
|
|
412
|
+
return caseInvalid();
|
|
413
|
+
parsed.push(parsedCase);
|
|
414
|
+
}
|
|
415
|
+
cases.push({ path: entry.path, cases: parsed });
|
|
416
|
+
}
|
|
417
|
+
return { ok: true, v: { cases, data, raws } };
|
|
418
|
+
}
|
|
419
|
+
function parseCase(raw) {
|
|
420
|
+
if (raw.t !== "obj")
|
|
421
|
+
return null;
|
|
422
|
+
const id = memberString(raw, "id");
|
|
423
|
+
const surface = memberString(raw, "surface");
|
|
424
|
+
const klass = memberString(raw, "class");
|
|
425
|
+
const input = member(raw, "input");
|
|
426
|
+
const expected = member(raw, "expected");
|
|
427
|
+
if (typeof id !== "string" ||
|
|
428
|
+
typeof surface !== "string" ||
|
|
429
|
+
typeof klass !== "string" ||
|
|
430
|
+
!input ||
|
|
431
|
+
input.t !== "obj" ||
|
|
432
|
+
!expected ||
|
|
433
|
+
expected.t !== "obj") {
|
|
434
|
+
return null;
|
|
435
|
+
}
|
|
436
|
+
const tamperRaw = member(raw, "tamper");
|
|
437
|
+
let tamper = null;
|
|
438
|
+
if (tamperRaw !== null && tamperRaw.t === "obj") {
|
|
439
|
+
const base = memberString(tamperRaw, "base_case");
|
|
440
|
+
const byteIndex = memberNumber(tamperRaw, "byte_index");
|
|
441
|
+
const xor = memberNumber(tamperRaw, "xor");
|
|
442
|
+
const target = memberString(tamperRaw, "target");
|
|
443
|
+
if (typeof base !== "string" || byteIndex === null || !Number.isInteger(byteIndex) || byteIndex < 0 || xor === null || !Number.isInteger(xor) || xor <= 0) {
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
446
|
+
tamper = { baseCase: base, byteIndex, target: typeof target === "string" ? target : null, xor };
|
|
447
|
+
}
|
|
448
|
+
return { id, surface, klass, input, expected, tamper, raw };
|
|
449
|
+
}
|
|
450
|
+
// ---- file-set + hashes ---------------------------------------------------------------
|
|
451
|
+
function verifyFileSet(files, map) {
|
|
452
|
+
const declared = new Set(files.map((f) => f.path));
|
|
453
|
+
const present = new Set([...map.keys()].filter((k) => k !== "index.json"));
|
|
454
|
+
if (declared.size === present.size && [...declared].every((p) => present.has(p))) {
|
|
455
|
+
return { ok: true };
|
|
456
|
+
}
|
|
457
|
+
return { ok: false, e: "corpus_file_set_mismatch" };
|
|
458
|
+
}
|
|
459
|
+
function sha256B64(bytes) {
|
|
460
|
+
return digest.of(bytes).bytes.toString("base64url");
|
|
461
|
+
}
|
|
462
|
+
function verifyHashes(files, map) {
|
|
463
|
+
for (const entry of files) {
|
|
464
|
+
const bytes = map.get(entry.path);
|
|
465
|
+
if (bytes === undefined || sha256B64(bytes) !== entry.sha) {
|
|
466
|
+
return { ok: false, e: "corpus_hash_mismatch" };
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return { ok: true };
|
|
470
|
+
}
|
|
471
|
+
// ---- counts ---------------------------------------------------------------------------
|
|
472
|
+
function verifyCounts(index, cases) {
|
|
473
|
+
const perFile = new Map(cases.map((file) => [file.path, file.cases.length]));
|
|
474
|
+
const files = member(index, "files");
|
|
475
|
+
let total = 0;
|
|
476
|
+
// The index "files" member is an array by the structure gate upstream.
|
|
477
|
+
for (const entry of files.v) {
|
|
478
|
+
const path = memberString(entry, "path");
|
|
479
|
+
const declaredCases = memberNumber(entry, "cases");
|
|
480
|
+
if (path.startsWith("cases/")) {
|
|
481
|
+
const observed = perFile.get(path);
|
|
482
|
+
if (observed === undefined || observed !== declaredCases) {
|
|
483
|
+
return { ok: false, e: "corpus_count_mismatch" };
|
|
484
|
+
}
|
|
485
|
+
total += declaredCases;
|
|
486
|
+
}
|
|
487
|
+
else if (declaredCases !== 0) {
|
|
488
|
+
return { ok: false, e: "corpus_count_mismatch" };
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
if (total !== memberNumber(index, "total_cases")) {
|
|
492
|
+
return { ok: false, e: "corpus_count_mismatch" };
|
|
493
|
+
}
|
|
494
|
+
return { ok: true };
|
|
495
|
+
}
|
|
496
|
+
// ---- case ids + validity -----------------------------------------------------------------
|
|
497
|
+
function verifyCaseIds(cases) {
|
|
498
|
+
const ids = cases.flatMap((file) => file.cases.map((c) => c.id));
|
|
499
|
+
return new Set(ids).size === ids.length ? { ok: true } : { ok: false, e: "corpus_case_id_duplicate" };
|
|
500
|
+
}
|
|
501
|
+
function verifyCaseValidity(cases) {
|
|
502
|
+
const all = cases.flatMap((file) => file.cases);
|
|
503
|
+
const byId = new Map(all.map((c) => [c.id, c]));
|
|
504
|
+
for (const caseObj of all) {
|
|
505
|
+
if (!SURFACES.includes(caseObj.surface))
|
|
506
|
+
return caseInvalid();
|
|
507
|
+
if (!CLASSES.includes(caseObj.klass))
|
|
508
|
+
return caseInvalid();
|
|
509
|
+
// A verdict-only valid expectation agrees with any ok-map — the vacuous
|
|
510
|
+
// green; a valid case must pin at least one projection field.
|
|
511
|
+
if (caseObj.expected.t === "obj") {
|
|
512
|
+
const verdict = memberString(caseObj.expected, "verdict");
|
|
513
|
+
if (verdict === "valid" && caseObj.expected.v.length === 1)
|
|
514
|
+
return caseInvalid();
|
|
515
|
+
}
|
|
516
|
+
if (!verifyOneTamper(caseObj, byId))
|
|
517
|
+
return caseInvalid();
|
|
518
|
+
}
|
|
519
|
+
return { ok: true };
|
|
520
|
+
}
|
|
521
|
+
// A tamper case's verbatim artifact must byte-equal the re-derived
|
|
522
|
+
// base-with-one-meaningful-byte-flip on the ADDRESSED target.
|
|
523
|
+
function verifyOneTamper(caseObj, byId) {
|
|
524
|
+
if (caseObj.tamper === null)
|
|
525
|
+
return true;
|
|
526
|
+
const { baseCase, byteIndex, target, xor } = caseObj.tamper;
|
|
527
|
+
const base = byId.get(baseCase);
|
|
528
|
+
if (base === undefined)
|
|
529
|
+
return false;
|
|
530
|
+
const baseBytes = tamperTargetBytes(base.input, target);
|
|
531
|
+
const verbatimBytes = tamperTargetBytes(caseObj.input, target);
|
|
532
|
+
if (baseBytes === null || verbatimBytes === null)
|
|
533
|
+
return false;
|
|
534
|
+
if (byteIndex >= baseBytes.length)
|
|
535
|
+
return false;
|
|
536
|
+
const derived = Buffer.from(baseBytes);
|
|
537
|
+
derived[byteIndex] = derived[byteIndex] ^ xor;
|
|
538
|
+
return derived.equals(verbatimBytes);
|
|
539
|
+
}
|
|
540
|
+
function tamperTargetBytes(input, target) {
|
|
541
|
+
if (input.t !== "obj")
|
|
542
|
+
return null;
|
|
543
|
+
if (target === null) {
|
|
544
|
+
const text = memberString(input, "text");
|
|
545
|
+
if (typeof text === "string")
|
|
546
|
+
return Buffer.from(text, "utf8");
|
|
547
|
+
const encoded = memberString(input, "base64url");
|
|
548
|
+
if (typeof encoded === "string") {
|
|
549
|
+
const decoded = b64.decodeLenient(encoded);
|
|
550
|
+
return decoded.ok ? decoded.v : null;
|
|
551
|
+
}
|
|
552
|
+
return null;
|
|
553
|
+
}
|
|
554
|
+
if (target === "input.text") {
|
|
555
|
+
const text = memberString(input, "text");
|
|
556
|
+
return typeof text === "string" ? Buffer.from(text, "utf8") : null;
|
|
557
|
+
}
|
|
558
|
+
if (target === "input.base64url") {
|
|
559
|
+
const encoded = memberString(input, "base64url");
|
|
560
|
+
if (typeof encoded !== "string")
|
|
561
|
+
return null;
|
|
562
|
+
const decoded = b64.decodeLenient(encoded);
|
|
563
|
+
return decoded.ok ? decoded.v : null;
|
|
564
|
+
}
|
|
565
|
+
if (target === "input.entry") {
|
|
566
|
+
const entry = memberString(input, "entry");
|
|
567
|
+
return typeof entry === "string" ? Buffer.from(entry, "utf8") : null;
|
|
568
|
+
}
|
|
569
|
+
return null;
|
|
570
|
+
}
|
|
571
|
+
// ---- applicability ------------------------------------------------------------------------
|
|
572
|
+
function verifyApplicability(index, cases) {
|
|
573
|
+
const applicability = member(index, "applicability");
|
|
574
|
+
if (applicability.t !== "obj")
|
|
575
|
+
return { ok: false, e: "corpus_applicability_incomplete" };
|
|
576
|
+
const surfaceKeys = new Set(applicability.v.map(([k]) => k));
|
|
577
|
+
const expectedSurfaces = new Set(SURFACES);
|
|
578
|
+
if (surfaceKeys.size !== expectedSurfaces.size || ![...expectedSurfaces].every((s) => surfaceKeys.has(s))) {
|
|
579
|
+
return { ok: false, e: "corpus_applicability_incomplete" };
|
|
580
|
+
}
|
|
581
|
+
const leavesBySurface = new Map();
|
|
582
|
+
for (const [surface, leaves] of applicability.v) {
|
|
583
|
+
if (leaves.t !== "obj")
|
|
584
|
+
return { ok: false, e: "corpus_applicability_incomplete" };
|
|
585
|
+
const classKeys = new Set(leaves.v.map(([k]) => k));
|
|
586
|
+
const expectedClasses = new Set(CLASSES);
|
|
587
|
+
if (classKeys.size !== expectedClasses.size || ![...expectedClasses].every((c) => classKeys.has(c))) {
|
|
588
|
+
return { ok: false, e: "corpus_applicability_incomplete" };
|
|
589
|
+
}
|
|
590
|
+
leavesBySurface.set(surface, new Map(leaves.v));
|
|
591
|
+
}
|
|
592
|
+
for (const [surface, floor] of Object.entries(FLOOR)) {
|
|
593
|
+
const leaves = leavesBySurface.get(surface);
|
|
594
|
+
for (const required of floor.required) {
|
|
595
|
+
const leaf = leaves.get(required);
|
|
596
|
+
if (leaf === undefined || leaf.t !== "int" || leaf.v < 1) {
|
|
597
|
+
return { ok: false, e: "corpus_applicability_incomplete" };
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
const observed = new Map();
|
|
602
|
+
for (const file of cases) {
|
|
603
|
+
for (const caseObj of file.cases) {
|
|
604
|
+
const key = caseObj.surface + "\u0000" + caseObj.klass;
|
|
605
|
+
observed.set(key, (observed.get(key) ?? 0) + 1);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
for (const surface of SURFACES) {
|
|
609
|
+
const leaves = leavesBySurface.get(surface);
|
|
610
|
+
for (const klass of CLASSES) {
|
|
611
|
+
const leaf = leaves.get(klass);
|
|
612
|
+
const observedCount = observed.get(surface + "\u0000" + klass) ?? 0;
|
|
613
|
+
if (leaf.t === "int") {
|
|
614
|
+
if (observedCount !== leaf.v)
|
|
615
|
+
return { ok: false, e: "corpus_applicability_incomplete" };
|
|
616
|
+
}
|
|
617
|
+
else if (leaf.t === "obj") {
|
|
618
|
+
const reason = memberString(leaf, "n_a");
|
|
619
|
+
if (typeof reason !== "string" || reason === "" || observedCount !== 0) {
|
|
620
|
+
return { ok: false, e: "corpus_applicability_incomplete" };
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
else {
|
|
624
|
+
return { ok: false, e: "corpus_applicability_incomplete" };
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
return { ok: true };
|
|
629
|
+
}
|
|
630
|
+
// ---- raw bindings -----------------------------------------------------------------------------
|
|
631
|
+
function verifyRawBindings(cases, raws) {
|
|
632
|
+
for (const file of cases) {
|
|
633
|
+
for (const caseObj of file.cases) {
|
|
634
|
+
if (caseObj.input.t !== "obj")
|
|
635
|
+
continue;
|
|
636
|
+
const rawPath = memberString(caseObj.input, "raw_file");
|
|
637
|
+
if (rawPath === null)
|
|
638
|
+
continue;
|
|
639
|
+
const refHash = memberString(caseObj.input, "sha256_base64url");
|
|
640
|
+
const bytes = raws.get(rawPath);
|
|
641
|
+
if (bytes === undefined || typeof refHash !== "string")
|
|
642
|
+
return caseInvalid();
|
|
643
|
+
if (sha256B64(bytes) !== refHash)
|
|
644
|
+
return caseInvalid();
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
return { ok: true };
|
|
648
|
+
}
|
|
649
|
+
// ---- tagged-object helpers ----------------------------------------------------------------------
|
|
650
|
+
export function member(container, key) {
|
|
651
|
+
if (container.t !== "obj")
|
|
652
|
+
return null;
|
|
653
|
+
const found = container.v.find(([k]) => k === key);
|
|
654
|
+
return found ? found[1] : null;
|
|
655
|
+
}
|
|
656
|
+
export function memberString(container, key) {
|
|
657
|
+
const found = member(container, key);
|
|
658
|
+
return found !== null && found.t === "str" ? found.v : null;
|
|
659
|
+
}
|
|
660
|
+
export function memberNumber(container, key) {
|
|
661
|
+
const found = member(container, key);
|
|
662
|
+
if (found === null)
|
|
663
|
+
return null;
|
|
664
|
+
if (found.t === "int" || found.t === "float")
|
|
665
|
+
return found.v;
|
|
666
|
+
return null;
|
|
667
|
+
}
|
|
668
|
+
function objWithout(container, drop) {
|
|
669
|
+
if (container.t !== "obj")
|
|
670
|
+
return container;
|
|
671
|
+
return { t: "obj", v: container.v.filter(([k]) => k !== drop) };
|
|
672
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Bounds } from "./bounds.ts";
|
|
2
|
+
import type { Value } from "./value.ts";
|
|
3
|
+
export declare class CodeFail extends Error {
|
|
4
|
+
readonly code: string;
|
|
5
|
+
constructor(code: string);
|
|
6
|
+
}
|
|
7
|
+
export declare class SyntaxFail extends Error {
|
|
8
|
+
}
|
|
9
|
+
export declare function decode(input: Buffer | string | number | symbol, bounds?: Bounds | Record<string, unknown>): {
|
|
10
|
+
ok: true;
|
|
11
|
+
v: Value;
|
|
12
|
+
} | {
|
|
13
|
+
ok: false;
|
|
14
|
+
e: string;
|
|
15
|
+
};
|
|
16
|
+
export declare function isUtf8(bytes: Buffer): boolean;
|