@mikeargento/bitgraph 1.1.0 → 1.2.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/README.md +49 -40
- package/dist/__tests__/audit-anchors.test.d.ts +2 -0
- package/dist/__tests__/audit-anchors.test.d.ts.map +1 -0
- package/dist/__tests__/audit-anchors.test.js +152 -0
- package/dist/__tests__/audit-anchors.test.js.map +1 -0
- package/dist/__tests__/audit-anomalies.test.d.ts +2 -0
- package/dist/__tests__/audit-anomalies.test.d.ts.map +1 -0
- package/dist/__tests__/audit-anomalies.test.js +663 -0
- package/dist/__tests__/audit-anomalies.test.js.map +1 -0
- package/dist/__tests__/audit-attestation.test.d.ts +2 -0
- package/dist/__tests__/audit-attestation.test.d.ts.map +1 -0
- package/dist/__tests__/audit-attestation.test.js +403 -0
- package/dist/__tests__/audit-attestation.test.js.map +1 -0
- package/dist/__tests__/audit-bench-smoke.test.d.ts +2 -0
- package/dist/__tests__/audit-bench-smoke.test.d.ts.map +1 -0
- package/dist/__tests__/audit-bench-smoke.test.js +87 -0
- package/dist/__tests__/audit-bench-smoke.test.js.map +1 -0
- package/dist/__tests__/audit-boundary.test.d.ts +20 -0
- package/dist/__tests__/audit-boundary.test.d.ts.map +1 -0
- package/dist/__tests__/audit-boundary.test.js +84 -0
- package/dist/__tests__/audit-boundary.test.js.map +1 -0
- package/dist/__tests__/audit-cli.test.d.ts +2 -0
- package/dist/__tests__/audit-cli.test.d.ts.map +1 -0
- package/dist/__tests__/audit-cli.test.js +239 -0
- package/dist/__tests__/audit-cli.test.js.map +1 -0
- package/dist/__tests__/audit-dependencies.test.d.ts +2 -0
- package/dist/__tests__/audit-dependencies.test.d.ts.map +1 -0
- package/dist/__tests__/audit-dependencies.test.js +116 -0
- package/dist/__tests__/audit-dependencies.test.js.map +1 -0
- package/dist/__tests__/audit-fixtures.d.ts +265 -0
- package/dist/__tests__/audit-fixtures.d.ts.map +1 -0
- package/dist/__tests__/audit-fixtures.js +511 -0
- package/dist/__tests__/audit-fixtures.js.map +1 -0
- package/dist/__tests__/audit-ingest.test.d.ts +2 -0
- package/dist/__tests__/audit-ingest.test.d.ts.map +1 -0
- package/dist/__tests__/audit-ingest.test.js +473 -0
- package/dist/__tests__/audit-ingest.test.js.map +1 -0
- package/dist/__tests__/audit-memory-ingest.test.d.ts +12 -0
- package/dist/__tests__/audit-memory-ingest.test.d.ts.map +1 -0
- package/dist/__tests__/audit-memory-ingest.test.js +120 -0
- package/dist/__tests__/audit-memory-ingest.test.js.map +1 -0
- package/dist/__tests__/audit-property.test.d.ts +2 -0
- package/dist/__tests__/audit-property.test.d.ts.map +1 -0
- package/dist/__tests__/audit-property.test.js +771 -0
- package/dist/__tests__/audit-property.test.js.map +1 -0
- package/dist/__tests__/audit-real-fixtures.test.d.ts +2 -0
- package/dist/__tests__/audit-real-fixtures.test.d.ts.map +1 -0
- package/dist/__tests__/audit-real-fixtures.test.js +132 -0
- package/dist/__tests__/audit-real-fixtures.test.js.map +1 -0
- package/dist/__tests__/audit-real-live.test.d.ts +2 -0
- package/dist/__tests__/audit-real-live.test.d.ts.map +1 -0
- package/dist/__tests__/audit-real-live.test.js +107 -0
- package/dist/__tests__/audit-real-live.test.js.map +1 -0
- package/dist/__tests__/audit-reconstruct.test.d.ts +2 -0
- package/dist/__tests__/audit-reconstruct.test.d.ts.map +1 -0
- package/dist/__tests__/audit-reconstruct.test.js +287 -0
- package/dist/__tests__/audit-reconstruct.test.js.map +1 -0
- package/dist/__tests__/audit-report.test.d.ts +2 -0
- package/dist/__tests__/audit-report.test.d.ts.map +1 -0
- package/dist/__tests__/audit-report.test.js +186 -0
- package/dist/__tests__/audit-report.test.js.map +1 -0
- package/dist/__tests__/audit-roundtrip.test.d.ts +2 -0
- package/dist/__tests__/audit-roundtrip.test.d.ts.map +1 -0
- package/dist/__tests__/audit-roundtrip.test.js +368 -0
- package/dist/__tests__/audit-roundtrip.test.js.map +1 -0
- package/dist/__tests__/audit-temporal.test.d.ts +2 -0
- package/dist/__tests__/audit-temporal.test.d.ts.map +1 -0
- package/dist/__tests__/audit-temporal.test.js +364 -0
- package/dist/__tests__/audit-temporal.test.js.map +1 -0
- package/dist/__tests__/audit-tiers.test.d.ts +2 -0
- package/dist/__tests__/audit-tiers.test.d.ts.map +1 -0
- package/dist/__tests__/audit-tiers.test.js +282 -0
- package/dist/__tests__/audit-tiers.test.js.map +1 -0
- package/dist/__tests__/audit-witness-real.test.d.ts +19 -0
- package/dist/__tests__/audit-witness-real.test.d.ts.map +1 -0
- package/dist/__tests__/audit-witness-real.test.js +82 -0
- package/dist/__tests__/audit-witness-real.test.js.map +1 -0
- package/dist/__tests__/audit-witness.test.d.ts +2 -0
- package/dist/__tests__/audit-witness.test.d.ts.map +1 -0
- package/dist/__tests__/audit-witness.test.js +308 -0
- package/dist/__tests__/audit-witness.test.js.map +1 -0
- package/dist/__tests__/chain-hash-anchor.test.d.ts +22 -0
- package/dist/__tests__/chain-hash-anchor.test.d.ts.map +1 -0
- package/dist/__tests__/chain-hash-anchor.test.js +55 -0
- package/dist/__tests__/chain-hash-anchor.test.js.map +1 -0
- package/dist/__tests__/declared-proof-fixture.d.ts +66 -0
- package/dist/__tests__/declared-proof-fixture.d.ts.map +1 -0
- package/dist/__tests__/declared-proof-fixture.js +66 -0
- package/dist/__tests__/declared-proof-fixture.js.map +1 -0
- package/dist/__tests__/fuse-roundtrip.test.d.ts +2 -0
- package/dist/__tests__/fuse-roundtrip.test.d.ts.map +1 -0
- package/dist/__tests__/fuse-roundtrip.test.js +172 -0
- package/dist/__tests__/fuse-roundtrip.test.js.map +1 -0
- package/dist/__tests__/fuse-sdk.test.d.ts +2 -0
- package/dist/__tests__/fuse-sdk.test.d.ts.map +1 -0
- package/dist/__tests__/fuse-sdk.test.js +117 -0
- package/dist/__tests__/fuse-sdk.test.js.map +1 -0
- package/dist/__tests__/fuse-vectors.test.d.ts +2 -0
- package/dist/__tests__/fuse-vectors.test.d.ts.map +1 -0
- package/dist/__tests__/fuse-vectors.test.js +175 -0
- package/dist/__tests__/fuse-vectors.test.js.map +1 -0
- package/dist/__tests__/fuse-verify.test.d.ts +2 -0
- package/dist/__tests__/fuse-verify.test.d.ts.map +1 -0
- package/dist/__tests__/fuse-verify.test.js +250 -0
- package/dist/__tests__/fuse-verify.test.js.map +1 -0
- package/dist/__tests__/mock-proof-fixture.d.ts +15 -0
- package/dist/__tests__/mock-proof-fixture.d.ts.map +1 -0
- package/dist/__tests__/mock-proof-fixture.js +34 -0
- package/dist/__tests__/mock-proof-fixture.js.map +1 -0
- package/dist/__tests__/proof-hash-regression.test.js +4 -43
- package/dist/__tests__/proof-hash-regression.test.js.map +1 -1
- package/dist/__tests__/proof-hash.test.js +3 -21
- package/dist/__tests__/proof-hash.test.js.map +1 -1
- package/dist/__tests__/proof-integrity.test.d.ts +2 -0
- package/dist/__tests__/proof-integrity.test.d.ts.map +1 -0
- package/dist/__tests__/proof-integrity.test.js +426 -0
- package/dist/__tests__/proof-integrity.test.js.map +1 -0
- package/dist/__tests__/realistic-proof-fixture.d.ts +56 -0
- package/dist/__tests__/realistic-proof-fixture.d.ts.map +1 -0
- package/dist/__tests__/realistic-proof-fixture.js +57 -0
- package/dist/__tests__/realistic-proof-fixture.js.map +1 -0
- package/dist/__tests__/verifier.test.js +74 -0
- package/dist/__tests__/verifier.test.js.map +1 -1
- package/dist/fuse-cli.d.ts +3 -0
- package/dist/fuse-cli.d.ts.map +1 -0
- package/dist/fuse-cli.js +210 -0
- package/dist/fuse-cli.js.map +1 -0
- package/dist/fuse.d.ts +75 -0
- package/dist/fuse.d.ts.map +1 -0
- package/dist/fuse.js +273 -0
- package/dist/fuse.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/package.json +19 -9
- package/src/__tests__/audit-anchors.test.ts +188 -0
- package/src/__tests__/audit-anomalies.test.ts +815 -0
- package/src/__tests__/audit-attestation.test.ts +529 -0
- package/src/__tests__/audit-bench-smoke.test.ts +108 -0
- package/src/__tests__/audit-boundary.test.ts +96 -0
- package/src/__tests__/audit-cli.test.ts +288 -0
- package/src/__tests__/audit-dependencies.test.ts +141 -0
- package/src/__tests__/audit-fixtures.ts +736 -0
- package/src/__tests__/audit-ingest.test.ts +575 -0
- package/src/__tests__/audit-memory-ingest.test.ts +140 -0
- package/src/__tests__/audit-property.test.ts +892 -0
- package/src/__tests__/audit-real-fixtures.test.ts +153 -0
- package/src/__tests__/audit-real-live.test.ts +128 -0
- package/src/__tests__/audit-reconstruct.test.ts +353 -0
- package/src/__tests__/audit-report.test.ts +222 -0
- package/src/__tests__/audit-roundtrip.test.ts +464 -0
- package/src/__tests__/audit-temporal.test.ts +446 -0
- package/src/__tests__/audit-tiers.test.ts +343 -0
- package/src/__tests__/audit-witness-real.test.ts +92 -0
- package/src/__tests__/audit-witness.test.ts +345 -0
- package/src/__tests__/chain-hash-anchor.test.ts +67 -0
- package/src/__tests__/declared-proof-fixture.ts +65 -0
- package/src/__tests__/fuse-roundtrip.test.ts +174 -0
- package/src/__tests__/fuse-sdk.test.ts +142 -0
- package/src/__tests__/fuse-vectors.test.ts +218 -0
- package/src/__tests__/fuse-verify.test.ts +278 -0
- package/src/__tests__/mock-proof-fixture.ts +36 -0
- package/src/__tests__/proof-hash-regression.test.ts +4 -44
- package/src/__tests__/proof-hash.test.ts +3 -22
- package/src/__tests__/proof-integrity.test.ts +541 -0
- package/src/__tests__/real-fixtures/anchor-after-112762.json +47 -0
- package/src/__tests__/real-fixtures/anchor-with-ethereum-112758.json +47 -0
- package/src/__tests__/real-fixtures/live-proof-0.json +42 -0
- package/src/__tests__/real-fixtures/live-proof-1.json +43 -0
- package/src/__tests__/real-fixtures/live-proof-2.json +43 -0
- package/src/__tests__/real-fixtures/successor-112760.json +38 -0
- package/src/__tests__/real-fixtures/witness-25525146.json +6 -0
- package/src/__tests__/real-fixtures/witness-25525147.json +6 -0
- package/src/__tests__/realistic-proof-fixture.ts +57 -0
- package/src/__tests__/verifier.test.ts +83 -0
- package/src/fuse-cli.ts +187 -0
- package/src/fuse.ts +356 -0
- package/src/index.ts +6 -0
- package/dist/canonical.d.ts +0 -54
- package/dist/canonical.d.ts.map +0 -1
- package/dist/canonical.js +0 -119
- package/dist/canonical.js.map +0 -1
- package/dist/proof-hash.d.ts +0 -9
- package/dist/proof-hash.d.ts.map +0 -1
- package/dist/proof-hash.js +0 -75
- package/dist/proof-hash.js.map +0 -1
- package/dist/types.d.ts +0 -705
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -3
- package/dist/types.js.map +0 -1
- package/dist/verifier.d.ts +0 -27
- package/dist/verifier.d.ts.map +0 -1
- package/dist/verifier.js +0 -844
- package/dist/verifier.js.map +0 -1
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// Copyright (c) Mike Argento. All rights reserved. See LICENSE.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* fuse() against a fake transport built from real fixture proofs (minted
|
|
5
|
+
* through the local enclave harness). Every refusal path runs.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, test } from "node:test";
|
|
9
|
+
import * as assert from "node:assert/strict";
|
|
10
|
+
import { readFileSync } from "node:fs";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
import { fuse, builderFor, FuseError } from "../fuse.js";
|
|
13
|
+
import type { BitGraphProof, SlotAllocation } from "../fuse.js";
|
|
14
|
+
|
|
15
|
+
const FIX = fileURLToPath(new URL("../../src/__tests__/fuse-fixtures/", import.meta.url));
|
|
16
|
+
const bytes = (name: string) => new Uint8Array(readFileSync(FIX + name));
|
|
17
|
+
const proofOf = (name: string) => JSON.parse(readFileSync(FIX + name, "utf8")) as BitGraphProof;
|
|
18
|
+
|
|
19
|
+
interface Call { path: string; body: unknown }
|
|
20
|
+
|
|
21
|
+
/** A transport that hands out `slot` and answers the commit with `answer`. */
|
|
22
|
+
function fakeTransport(slot: SlotAllocation, answer: (calls: Call[], body: unknown) => { status: number; json: unknown }, lookup?: (calls: Call[]) => { status: number; json: unknown }) {
|
|
23
|
+
const calls: Call[] = [];
|
|
24
|
+
const f: typeof fetch = async (input, init) => {
|
|
25
|
+
const url = String(input);
|
|
26
|
+
const path = url.replace(/^https?:\/\/[^/]+/, "");
|
|
27
|
+
const body = init?.body ? JSON.parse(String(init.body)) : undefined;
|
|
28
|
+
calls.push({ path, body });
|
|
29
|
+
const reply = (r: { status: number; json: unknown }) => new Response(JSON.stringify(r.json), { status: r.status, headers: { "Content-Type": "application/json" } });
|
|
30
|
+
if (path === "/api/fuse/allocate") return reply({ status: 200, json: { slotId: slot.nonceB64, slot, chainId: "bitgraph:main" } });
|
|
31
|
+
if (path === "/api/fuse/commit") return reply(answer(calls, body));
|
|
32
|
+
if (path.startsWith("/api/proofs/")) return reply(lookup ? lookup(calls) : { status: 200, json: { proofs: [] } });
|
|
33
|
+
return reply({ status: 404, json: { error: "no route" } });
|
|
34
|
+
};
|
|
35
|
+
return { calls, transport: { baseUrl: "https://example.test", fetch: f, recoveryAttempts: 2, recoveryDelayMs: 1 } };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe("fuse(): the four beats", () => {
|
|
39
|
+
test("trailer/1 over an existing original: allocate, fuse, hash, fill; Frame carries the unchanged proof", async () => {
|
|
40
|
+
const proof = proofOf("trailer.proof.json");
|
|
41
|
+
const { calls, transport } = fakeTransport(proof.slotAllocation!, () => ({ status: 200, json: { proof } }));
|
|
42
|
+
const original = bytes("original.txt");
|
|
43
|
+
const r = await fuse(builderFor("trailer/1", original), { placement: "trailer/1", original, fusedFile: "photo.jpg", transport });
|
|
44
|
+
assert.equal(r.artifactDigestB64, proof.artifact.digestB64);
|
|
45
|
+
assert.deepEqual(r.proof, proof);
|
|
46
|
+
assert.equal(r.frame.type, "bitgraph-fuse/1");
|
|
47
|
+
assert.equal(r.frame.manifest.placement, "trailer/1");
|
|
48
|
+
assert.equal(r.frame.manifest.fusedFile, "photo.jpg");
|
|
49
|
+
assert.deepEqual(r.frame.proof, proof);
|
|
50
|
+
assert.equal(r.recovered, false);
|
|
51
|
+
assert.equal(r.verification.category, "FUSED_DIRECT");
|
|
52
|
+
assert.equal(r.fusedBytes, undefined, "byte-exact placements evaporate by default");
|
|
53
|
+
// The commit carried the placement and origin in the attribution, and named the slot.
|
|
54
|
+
const commit = calls.find((c) => c.path === "/api/fuse/commit")!.body as Record<string, unknown>;
|
|
55
|
+
assert.equal(commit.slotId, proof.slotAllocation!.nonceB64);
|
|
56
|
+
assert.deepEqual(commit.attribution, { name: "bitgraph-fuse/1", title: "trailer/1", message: r.originDigestB64 });
|
|
57
|
+
assert.deepEqual(commit.digests, [{ digestB64: proof.artifact.digestB64, hashAlg: "sha256" }]);
|
|
58
|
+
assert.equal(commit.chainId, "bitgraph:main");
|
|
59
|
+
// The raw nonce appears in no error and nowhere but the slot field it must ride in.
|
|
60
|
+
assert.ok(!JSON.stringify(commit.attribution).includes(proof.slotAllocation!.nonceB64));
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("produced/1 keeps its bytes and its payload view in the Frame", async () => {
|
|
64
|
+
const proof = proofOf("produced-origin.proof.json");
|
|
65
|
+
const { transport } = fakeTransport(proof.slotAllocation!, () => ({ status: 200, json: [proof] }));
|
|
66
|
+
const originDigest = new Uint8Array(Buffer.from(proof.attribution!.message!, "base64"));
|
|
67
|
+
const r = await fuse(builderFor("produced/1"), { placement: "produced/1", originDigest, transport });
|
|
68
|
+
assert.ok(r.fusedBytes, "Form C keeps the fused bytes");
|
|
69
|
+
assert.deepEqual(r.fusedBytes, bytes("produced-origin.json"));
|
|
70
|
+
assert.ok(r.frame.fusePayload);
|
|
71
|
+
assert.equal(r.frame.fusePayload!.type, "bitgraph-fuse/1");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("a builder that drops the commitment is refused BEFORE any commit; the slot is left to expire", async () => {
|
|
75
|
+
const proof = proofOf("trailer.proof.json");
|
|
76
|
+
const { calls, transport } = fakeTransport(proof.slotAllocation!, () => ({ status: 200, json: { proof } }));
|
|
77
|
+
await assert.rejects(
|
|
78
|
+
fuse(() => bytes("original.txt"), { placement: "trailer/1", original: bytes("original.txt"), transport }),
|
|
79
|
+
(e: FuseError) => e.code === "commitment-missing",
|
|
80
|
+
);
|
|
81
|
+
assert.equal(calls.filter((c) => c.path === "/api/fuse/commit").length, 0, "no commit was attempted");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("a proof under a different slot is never labelled fused", async () => {
|
|
85
|
+
const proof = proofOf("trailer.proof.json");
|
|
86
|
+
const other = proofOf("trailer-undeclared.proof.json");
|
|
87
|
+
const { transport } = fakeTransport(proof.slotAllocation!, () => ({ status: 200, json: { proof: other } }));
|
|
88
|
+
await assert.rejects(
|
|
89
|
+
fuse(builderFor("trailer/1", bytes("original.txt")), { placement: "trailer/1", original: bytes("original.txt"), transport }),
|
|
90
|
+
(e: FuseError) => e.code === "slot-mismatch",
|
|
91
|
+
);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("409 slot-unavailable: the proof is read back by digest and matched on slotHashB64; nothing is re-allocated", async () => {
|
|
95
|
+
const proof = proofOf("trailer.proof.json");
|
|
96
|
+
const decoy = proofOf("trailer-undeclared.proof.json");
|
|
97
|
+
const { calls, transport } = fakeTransport(
|
|
98
|
+
proof.slotAllocation!,
|
|
99
|
+
() => ({ status: 409, json: { error: "gone", code: "slot-unavailable" } }),
|
|
100
|
+
() => ({ status: 200, json: { proofs: [{ proof: decoy }, { proof }] } }),
|
|
101
|
+
);
|
|
102
|
+
const r = await fuse(builderFor("trailer/1", bytes("original.txt")), { placement: "trailer/1", original: bytes("original.txt"), transport });
|
|
103
|
+
assert.equal(r.recovered, true);
|
|
104
|
+
assert.deepEqual(r.proof, proof);
|
|
105
|
+
assert.equal(calls.filter((c) => c.path === "/api/fuse/allocate").length, 1, "allocated exactly once");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("409 with no matching proof on the ledger is reported as slot-unavailable, not retried into a new slot", async () => {
|
|
109
|
+
const proof = proofOf("trailer.proof.json");
|
|
110
|
+
const { calls, transport } = fakeTransport(proof.slotAllocation!, () => ({ status: 409, json: { error: "gone", code: "slot-unavailable" } }));
|
|
111
|
+
await assert.rejects(
|
|
112
|
+
fuse(builderFor("trailer/1", bytes("original.txt")), { placement: "trailer/1", original: bytes("original.txt"), transport }),
|
|
113
|
+
(e: FuseError) => e.code === "slot-unavailable" && !e.message.includes(proof.slotAllocation!.nonceB64),
|
|
114
|
+
);
|
|
115
|
+
assert.equal(calls.filter((c) => c.path === "/api/fuse/allocate").length, 1);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("tee-restarting on commit: read back first, then report; a network failure after send does the same", async () => {
|
|
119
|
+
const proof = proofOf("trailer.proof.json");
|
|
120
|
+
const a = fakeTransport(proof.slotAllocation!, () => ({ status: 503, json: { error: "restarting", code: "tee-restarting" } }), () => ({ status: 200, json: { proofs: [{ proof }] } }));
|
|
121
|
+
const r = await fuse(builderFor("trailer/1", bytes("original.txt")), { placement: "trailer/1", original: bytes("original.txt"), transport: a.transport });
|
|
122
|
+
assert.equal(r.recovered, true);
|
|
123
|
+
const b = fakeTransport(proof.slotAllocation!, () => ({ status: 503, json: { error: "restarting", code: "tee-restarting" } }));
|
|
124
|
+
await assert.rejects(fuse(builderFor("trailer/1", bytes("original.txt")), { placement: "trailer/1", original: bytes("original.txt"), transport: b.transport }), (e: FuseError) => e.code === "tee-restarting");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test("bad inputs are refused before any request", async () => {
|
|
128
|
+
const proof = proofOf("trailer.proof.json");
|
|
129
|
+
const { calls, transport } = fakeTransport(proof.slotAllocation!, () => ({ status: 200, json: { proof } }));
|
|
130
|
+
await assert.rejects(fuse(builderFor("trailer/1", bytes("original.txt")), { placement: "xmp/9" as never, original: bytes("original.txt"), transport }), (e: FuseError) => e.code === "bad-placement");
|
|
131
|
+
await assert.rejects(fuse(builderFor("trailer/1"), { placement: "trailer/1", transport }), (e: FuseError) => e.code === "bad-input");
|
|
132
|
+
await assert.rejects(fuse(builderFor("produced/1"), { placement: "produced/1", original: bytes("original.txt"), transport }), (e: FuseError) => e.code === "bad-input");
|
|
133
|
+
assert.equal(calls.length, 0);
|
|
134
|
+
assert.throws(() => builderFor("nope/1" as never), (e: FuseError) => e.code === "bad-placement");
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("a slot off the anchored chain is refused", async () => {
|
|
138
|
+
const proof = proofOf("trailer.proof.json");
|
|
139
|
+
const { transport } = fakeTransport({ ...proof.slotAllocation!, chainId: "global" }, () => ({ status: 200, json: { proof } }));
|
|
140
|
+
await assert.rejects(fuse(builderFor("trailer/1", bytes("original.txt")), { placement: "trailer/1", original: bytes("original.txt"), transport }), (e: FuseError) => e.code === "allocate-failed" && /anchored chain/.test(e.message));
|
|
141
|
+
});
|
|
142
|
+
});
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// Copyright (c) Mike Argento. All rights reserved. See LICENSE.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* BitGraph Fuse: fixed vectors (spec 15.2).
|
|
5
|
+
*
|
|
6
|
+
* The slot record hash, the commitment preimage, the commitment, the Form C
|
|
7
|
+
* payload bytes, the trailer bytes and the container bytes are pinned, and
|
|
8
|
+
* the enclave's own commit.slotHashB64 on real fixture proofs must equal what
|
|
9
|
+
* this package computes from the embedded slot record.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { describe, test } from "node:test";
|
|
13
|
+
import * as assert from "node:assert/strict";
|
|
14
|
+
import { readFileSync } from "node:fs";
|
|
15
|
+
import { fileURLToPath } from "node:url";
|
|
16
|
+
import { sha256 } from "@noble/hashes/sha256";
|
|
17
|
+
import {
|
|
18
|
+
FUSE_DOMAIN,
|
|
19
|
+
TRAILER_MAGIC,
|
|
20
|
+
canonicalSlotBody,
|
|
21
|
+
canonicalize,
|
|
22
|
+
computeSlotRecordHash,
|
|
23
|
+
slotCommitmentPreimage,
|
|
24
|
+
computeSlotCommitment,
|
|
25
|
+
buildFusePayload,
|
|
26
|
+
parseFusePayload,
|
|
27
|
+
getPlacement,
|
|
28
|
+
bytesToHex,
|
|
29
|
+
hexToBytes,
|
|
30
|
+
bytesToBase64,
|
|
31
|
+
base64ToBytes,
|
|
32
|
+
} from "@mikeargento/bitgraph-verify";
|
|
33
|
+
import type { BitGraphProof, SlotAllocation } from "@mikeargento/bitgraph-verify";
|
|
34
|
+
|
|
35
|
+
const FIX = fileURLToPath(new URL("../../src/__tests__/fuse-fixtures/", import.meta.url));
|
|
36
|
+
const bytes = (name: string) => new Uint8Array(readFileSync(FIX + name));
|
|
37
|
+
const json = <T>(name: string): T => JSON.parse(readFileSync(FIX + name, "utf8")) as T;
|
|
38
|
+
|
|
39
|
+
interface Vec { slot: SlotAllocation; canonicalSlotBody: string; slotRecordHashHex: string; preimageHex: string; commitmentHex: string }
|
|
40
|
+
interface Vectors {
|
|
41
|
+
domainHex: string;
|
|
42
|
+
synthetic: Vec;
|
|
43
|
+
trailerFixture: Vec & { fusedDigestB64: string; originDigestB64: string };
|
|
44
|
+
containerFixture: Vec & { fusedDigestB64: string; originDigestB64: string };
|
|
45
|
+
producedOriginFixture: Vec & { payload: string; fusedDigestB64: string };
|
|
46
|
+
producedBareFixture: Vec & { payload: string; fusedDigestB64: string };
|
|
47
|
+
}
|
|
48
|
+
const V = json<Vectors>("vectors.json");
|
|
49
|
+
|
|
50
|
+
describe("domain separation", () => {
|
|
51
|
+
test("the domain is the 15 profile bytes followed by one zero byte, 16 bytes total", () => {
|
|
52
|
+
assert.equal(FUSE_DOMAIN.length, 16);
|
|
53
|
+
assert.equal(new TextDecoder().decode(FUSE_DOMAIN.subarray(0, 15)), "bitgraph-fuse/1");
|
|
54
|
+
assert.equal(FUSE_DOMAIN[15], 0);
|
|
55
|
+
assert.equal(bytesToHex(FUSE_DOMAIN), V.domainHex);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("changing the domain label changes the commitment", () => {
|
|
59
|
+
const slot = V.synthetic.slot;
|
|
60
|
+
const nonce = base64ToBytes(slot.nonceB64)!;
|
|
61
|
+
const other = new Uint8Array([...new TextEncoder().encode("bitgraph-fuse/2"), 0, ...computeSlotRecordHash(slot), ...nonce]);
|
|
62
|
+
assert.notEqual(bytesToHex(sha256(other)), V.synthetic.commitmentHex);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe("slot record hash and commitment, synthetic vector", () => {
|
|
67
|
+
const slot = V.synthetic.slot;
|
|
68
|
+
|
|
69
|
+
test("canonical slot body is the enclave's field subset, sorted, compact", () => {
|
|
70
|
+
const body = new TextDecoder().decode(canonicalize(canonicalSlotBody(slot)));
|
|
71
|
+
assert.equal(body, V.synthetic.canonicalSlotBody);
|
|
72
|
+
assert.equal(body, `{"chainId":"bitgraph:main","counter":"7","epochId":"${slot.epochId}","nonceB64":"${slot.nonceB64}","publicKeyB64":"${slot.publicKeyB64}","version":"bitgraph/slot/1"}`);
|
|
73
|
+
assert.ok(!body.includes("signatureB64"), "the signature is outside the hashed body");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("slotRecordHash, preimage and commitment are pinned and structurally related", () => {
|
|
77
|
+
const hash = computeSlotRecordHash(slot);
|
|
78
|
+
assert.equal(bytesToHex(hash), V.synthetic.slotRecordHashHex);
|
|
79
|
+
const pre = slotCommitmentPreimage(slot);
|
|
80
|
+
assert.equal(bytesToHex(pre), V.synthetic.preimageHex);
|
|
81
|
+
assert.equal(pre.length, 16 + 32 + 32);
|
|
82
|
+
assert.deepEqual(pre.subarray(0, 16), FUSE_DOMAIN);
|
|
83
|
+
assert.deepEqual(pre.subarray(16, 48), hash);
|
|
84
|
+
assert.deepEqual(pre.subarray(48, 80), base64ToBytes(slot.nonceB64));
|
|
85
|
+
assert.equal(bytesToHex(computeSlotCommitment(slot)), V.synthetic.commitmentHex);
|
|
86
|
+
assert.equal(bytesToHex(sha256(pre)), V.synthetic.commitmentHex);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("one-bit mutations of the nonce or any signed slot field change the commitment", () => {
|
|
90
|
+
const base = bytesToHex(computeSlotCommitment(slot));
|
|
91
|
+
const nonce = base64ToBytes(slot.nonceB64)!;
|
|
92
|
+
nonce[0] = nonce[0]! ^ 1;
|
|
93
|
+
assert.notEqual(bytesToHex(computeSlotCommitment({ ...slot, nonceB64: bytesToBase64(nonce) })), base);
|
|
94
|
+
assert.notEqual(bytesToHex(computeSlotCommitment({ ...slot, counter: "8" })), base);
|
|
95
|
+
assert.notEqual(bytesToHex(computeSlotCommitment({ ...slot, epochId: bytesToBase64(new Uint8Array(32).fill(9)) })), base);
|
|
96
|
+
assert.notEqual(bytesToHex(computeSlotCommitment({ ...slot, chainId: "global" })), base);
|
|
97
|
+
const { chainId: _c, ...noChain } = slot;
|
|
98
|
+
assert.notEqual(bytesToHex(computeSlotCommitment(noChain as SlotAllocation)), base, "a record without chainId is a different record");
|
|
99
|
+
// The signature is not part of the hashed body, so changing it changes nothing here.
|
|
100
|
+
assert.equal(bytesToHex(computeSlotCommitment({ ...slot, signatureB64: bytesToBase64(new Uint8Array(64).fill(7)) })), base);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("a nonce that is not 32 bytes is refused", () => {
|
|
104
|
+
assert.throws(() => computeSlotCommitment({ ...slot, nonceB64: bytesToBase64(new Uint8Array(16)) }), /32 bytes/);
|
|
105
|
+
assert.throws(() => computeSlotCommitment({ ...slot, nonceB64: "not base64!" }), /32 bytes/);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe("real fixture proofs: the enclave agrees with this package", () => {
|
|
110
|
+
for (const name of ["trailer", "container", "produced-origin", "produced-bare", "trailer-undeclared", "wrong-slot", "unregistered", "order-fused-during-hold"]) {
|
|
111
|
+
test(`${name}: SHA-256 of the canonical slot body equals commit.slotHashB64`, () => {
|
|
112
|
+
const proof = json<BitGraphProof>(`${name}.proof.json`);
|
|
113
|
+
assert.ok(proof.slotAllocation, "fixture carries its slot record");
|
|
114
|
+
assert.equal(bytesToBase64(computeSlotRecordHash(proof.slotAllocation!)), proof.commit.slotHashB64);
|
|
115
|
+
assert.equal(proof.slotAllocation!.nonceB64, proof.commit.nonceB64);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
test("the fixture vectors match the embedded slot records", () => {
|
|
120
|
+
for (const [name, v] of [["trailer", V.trailerFixture], ["container", V.containerFixture], ["produced-origin", V.producedOriginFixture], ["produced-bare", V.producedBareFixture]] as const) {
|
|
121
|
+
const proof = json<BitGraphProof>(`${name}.proof.json`);
|
|
122
|
+
assert.equal(bytesToHex(computeSlotCommitment(proof.slotAllocation!)), v.commitmentHex, name);
|
|
123
|
+
assert.equal(proof.artifact.digestB64, v.fusedDigestB64, name);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe("Form C payload bytes", () => {
|
|
129
|
+
test("pinned bytes: sorted keys, compact, lowercase hex, type last", () => {
|
|
130
|
+
const v = V.producedOriginFixture;
|
|
131
|
+
const commitment = hexToBytes(v.commitmentHex)!;
|
|
132
|
+
const origin = base64ToBytes(V.trailerFixture.originDigestB64)!;
|
|
133
|
+
const built = buildFusePayload(commitment, origin);
|
|
134
|
+
assert.equal(new TextDecoder().decode(built), v.payload);
|
|
135
|
+
assert.equal(v.payload, `{"origin":{"algorithm":"sha256","digest":"${bytesToHex(origin)}"},"slotCommitment":{"algorithm":"sha256","digest":"${v.commitmentHex}"},"type":"bitgraph-fuse/1"}`);
|
|
136
|
+
assert.equal(bytesToBase64(sha256(built)), v.fusedDigestB64);
|
|
137
|
+
const bare = buildFusePayload(hexToBytes(V.producedBareFixture.commitmentHex)!);
|
|
138
|
+
assert.equal(new TextDecoder().decode(bare), V.producedBareFixture.payload);
|
|
139
|
+
assert.ok(!V.producedBareFixture.payload.includes("origin"));
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("parse round-trips and rejects every non-canonical or malformed form", () => {
|
|
143
|
+
const good = bytes("produced-origin.json");
|
|
144
|
+
const parsed = parseFusePayload(good)!;
|
|
145
|
+
assert.equal(bytesToHex(parsed.commitment), V.producedOriginFixture.commitmentHex);
|
|
146
|
+
assert.equal(bytesToBase64(parsed.originDigest!), V.trailerFixture.originDigestB64);
|
|
147
|
+
const text = new TextDecoder().decode(good);
|
|
148
|
+
const enc = (s: string) => new TextEncoder().encode(s);
|
|
149
|
+
assert.equal(parseFusePayload(enc(JSON.stringify(JSON.parse(text), null, 2))), null, "whitespace");
|
|
150
|
+
assert.equal(parseFusePayload(enc(text.replace("bitgraph-fuse/1", "bitgraph-fuse/2"))), null, "type");
|
|
151
|
+
assert.equal(parseFusePayload(enc(text.replace(/"digest":"([0-9a-f]{8})/, (_m, h: string) => `"digest":"${h.toUpperCase()}`))), null, "uppercase hex");
|
|
152
|
+
assert.equal(parseFusePayload(enc(text.replace("{\"origin\"", "{\"extra\":1,\"origin\""))), null, "extra key");
|
|
153
|
+
assert.equal(parseFusePayload(enc(text.replace("\"type\":\"bitgraph-fuse/1\"", "\"type\":\"bitgraph-fuse/1\",\"type\":\"bitgraph-fuse/1\""))), null, "duplicate key");
|
|
154
|
+
assert.equal(parseFusePayload(enc(text.replace("sha256", "sha512"))), null, "algorithm");
|
|
155
|
+
assert.equal(parseFusePayload(enc("[]")), null);
|
|
156
|
+
assert.equal(parseFusePayload(new Uint8Array([0xff, 0xfe])), null, "not UTF-8");
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe("trailer/1 and container/1 bytes", () => {
|
|
161
|
+
test("trailer is original || 'BGFUSE01' || 8 zero bytes || commitment", () => {
|
|
162
|
+
const fused = bytes("fused-trailer.bin");
|
|
163
|
+
const original = bytes("original.txt");
|
|
164
|
+
assert.equal(fused.length, original.length + 48);
|
|
165
|
+
assert.deepEqual(fused.subarray(0, original.length), original);
|
|
166
|
+
assert.equal(new TextDecoder().decode(fused.subarray(original.length, original.length + 8)), TRAILER_MAGIC);
|
|
167
|
+
assert.ok(fused.subarray(original.length + 8, original.length + 16).every((b) => b === 0));
|
|
168
|
+
assert.equal(bytesToHex(fused.subarray(original.length + 16)), V.trailerFixture.commitmentHex);
|
|
169
|
+
assert.equal(bytesToBase64(sha256(fused)), V.trailerFixture.fusedDigestB64);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("container is a fixed-layout ustar: manifest, original, two zero blocks, all metadata zeroed", () => {
|
|
173
|
+
const fused = bytes("fused-container.tar");
|
|
174
|
+
const png = bytes("image.png");
|
|
175
|
+
assert.equal(fused.length % 512, 0);
|
|
176
|
+
const name = (off: number) => new TextDecoder().decode(fused.subarray(off, off + 100)).replace(/\0+$/, "");
|
|
177
|
+
assert.equal(name(0), "bitgraph-fuse/manifest.json");
|
|
178
|
+
const manifestLen = parseInt(new TextDecoder().decode(fused.subarray(124, 135)), 8);
|
|
179
|
+
const manifest = fused.subarray(512, 512 + manifestLen);
|
|
180
|
+
const parsed = parseFusePayload(manifest)!;
|
|
181
|
+
assert.equal(bytesToHex(parsed.commitment), V.containerFixture.commitmentHex);
|
|
182
|
+
assert.equal(bytesToBase64(parsed.originDigest!), V.containerFixture.originDigestB64);
|
|
183
|
+
const second = 512 + manifestLen + ((512 - (manifestLen % 512)) % 512);
|
|
184
|
+
assert.equal(name(second), "bitgraph-fuse/original");
|
|
185
|
+
assert.equal(new TextDecoder().decode(fused.subarray(second + 136, second + 147)), "00000000000", "mtime is zero");
|
|
186
|
+
assert.deepEqual(fused.subarray(second + 512, second + 512 + png.length), png);
|
|
187
|
+
assert.ok(fused.subarray(fused.length - 1024).every((b) => b === 0), "two zero blocks end the archive");
|
|
188
|
+
assert.equal(bytesToBase64(sha256(fused)), V.containerFixture.fusedDigestB64);
|
|
189
|
+
const rebuilt = getPlacement("container/1")!.build({ original: png, commitment: parsed.commitment });
|
|
190
|
+
assert.deepEqual(rebuilt, fused);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("one-bit mutation of the fused bytes changes the artifact digest", () => {
|
|
194
|
+
for (const name of ["fused-trailer.bin", "fused-container.tar", "produced-origin.json"]) {
|
|
195
|
+
const f = bytes(name);
|
|
196
|
+
const d0 = bytesToBase64(sha256(f));
|
|
197
|
+
f[0] = f[0]! ^ 1;
|
|
198
|
+
assert.notEqual(bytesToBase64(sha256(f)), d0, name);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
describe("encoding helpers", () => {
|
|
204
|
+
test("base64 is strict standard alphabet with canonical padding", () => {
|
|
205
|
+
assert.deepEqual(base64ToBytes("AQID"), new Uint8Array([1, 2, 3]));
|
|
206
|
+
assert.equal(base64ToBytes("AQI"), null, "length");
|
|
207
|
+
assert.equal(base64ToBytes("AQI-"), null, "url-safe alphabet");
|
|
208
|
+
assert.equal(base64ToBytes("AQ==\n"), null, "whitespace");
|
|
209
|
+
assert.equal(base64ToBytes("AR=="), null, "non-canonical trailing bits");
|
|
210
|
+
assert.equal(bytesToBase64(new Uint8Array(32).fill(255)), "//////////////////////////////////////////8=");
|
|
211
|
+
assert.deepEqual(base64ToBytes("//////////////////////////////////////////8="), new Uint8Array(32).fill(255));
|
|
212
|
+
});
|
|
213
|
+
test("hex is lowercase only", () => {
|
|
214
|
+
assert.deepEqual(hexToBytes("00ff"), new Uint8Array([0, 255]));
|
|
215
|
+
assert.equal(hexToBytes("00FF"), null);
|
|
216
|
+
assert.equal(hexToBytes("0"), null);
|
|
217
|
+
});
|
|
218
|
+
});
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
// Copyright (c) Mike Argento. All rights reserved. See LICENSE.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* BitGraph Fuse: verifier paths (spec 15.4, 15.5, 15.8) on real fixture
|
|
5
|
+
* proofs minted through the unmodified enclave. Every negative case runs.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, test } from "node:test";
|
|
9
|
+
import * as assert from "node:assert/strict";
|
|
10
|
+
import { readFileSync } from "node:fs";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
import { sha256 } from "@noble/hashes/sha256";
|
|
13
|
+
import { verifyFuse, assembledAfterCommit, verifyProofIntegrity, bytesToBase64, resetEpochLinkState } from "@mikeargento/bitgraph-verify";
|
|
14
|
+
import type { BitGraphProof, FuseVerifyResult } from "@mikeargento/bitgraph-verify";
|
|
15
|
+
|
|
16
|
+
const FIX = fileURLToPath(new URL("../../src/__tests__/fuse-fixtures/", import.meta.url));
|
|
17
|
+
const bytes = (name: string) => new Uint8Array(readFileSync(FIX + name));
|
|
18
|
+
const proofOf = (name: string) => JSON.parse(readFileSync(FIX + name, "utf8")) as BitGraphProof;
|
|
19
|
+
const text = (name: string) => readFileSync(FIX + name, "utf8");
|
|
20
|
+
|
|
21
|
+
const original = bytes("original.txt");
|
|
22
|
+
const png = bytes("image.png");
|
|
23
|
+
const unrelated = new TextEncoder().encode("a file nobody recorded\n");
|
|
24
|
+
|
|
25
|
+
async function run(proofName: string, file: Uint8Array, extra: Partial<Parameters<typeof verifyFuse>[0]> = {}): Promise<FuseVerifyResult> {
|
|
26
|
+
resetEpochLinkState();
|
|
27
|
+
return verifyFuse({ proof: proofOf(proofName), bytes: file, ...extra });
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe("path 3: the file is the committed bytes", () => {
|
|
31
|
+
test("an ordinary proof with its file is RECORDED, with the recorded statement", async () => {
|
|
32
|
+
const r = await run("recorded.proof.json", original);
|
|
33
|
+
assert.equal(r.category, "RECORDED");
|
|
34
|
+
assert.equal(r.proof.valid, true);
|
|
35
|
+
assert.equal(r.marker, null);
|
|
36
|
+
assert.match(r.statements[0]!, /existed no later than commit position/);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
for (const [proofName, fusedName, placement] of [
|
|
40
|
+
["trailer.proof.json", "fused-trailer.bin", "trailer/1"],
|
|
41
|
+
["container.proof.json", "fused-container.tar", "container/1"],
|
|
42
|
+
["produced-origin.proof.json", "produced-origin.json", "produced/1"],
|
|
43
|
+
["produced-bare.proof.json", "produced-bare.json", "produced/1"],
|
|
44
|
+
] as const) {
|
|
45
|
+
test(`${placement}: fused bytes verify FUSED_DIRECT`, async () => {
|
|
46
|
+
const r = await run(proofName, bytes(fusedName));
|
|
47
|
+
assert.equal(r.category, "FUSED_DIRECT", r.reason ?? "");
|
|
48
|
+
assert.equal(r.placement, placement);
|
|
49
|
+
assert.equal(r.marker?.source, "attribution");
|
|
50
|
+
assert.ok(r.span);
|
|
51
|
+
assert.ok(BigInt(r.span!.positions) >= 1n);
|
|
52
|
+
assert.equal(r.span!.chainId, "bitgraph:main");
|
|
53
|
+
assert.match(r.statements.at(-1)!, /could not feasibly have been finalized before their signed slot allocation at position/);
|
|
54
|
+
assert.equal(r.policy.spanExceeded, false);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
test("the origin statement names what was checked: consistency on the direct path, reconstruction on the origin path", async () => {
|
|
59
|
+
const direct = await run("trailer.proof.json", bytes("fused-trailer.bin"));
|
|
60
|
+
assert.equal(direct.category, "FUSED_DIRECT");
|
|
61
|
+
assert.equal(direct.statements.length, 2);
|
|
62
|
+
assert.match(direct.statements[0]!, /origin digest that matches the signed marker/);
|
|
63
|
+
assert.match(direct.statements[0]!, /was not supplied and was not checked/);
|
|
64
|
+
assert.doesNotMatch(direct.statements[0]!, /supplied original/, "no original was supplied on the direct path");
|
|
65
|
+
const fromOrigin = await run("trailer.proof.json", original);
|
|
66
|
+
assert.equal(fromOrigin.category, "FUSED_FROM_ORIGIN");
|
|
67
|
+
assert.equal(fromOrigin.statements.length, 2);
|
|
68
|
+
assert.match(fromOrigin.statements[0]!, /supplied original rebuilds the committed fused artifact byte for byte/);
|
|
69
|
+
const bare = await run("produced-bare.proof.json", bytes("produced-bare.json"));
|
|
70
|
+
assert.equal(bare.statements.length, 1);
|
|
71
|
+
assert.equal(bare.originDigestB64, null);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("a fused file committed under a different slot is INVALID_SLOT_COMMITMENT, never RECORDED", async () => {
|
|
75
|
+
const r = await run("wrong-slot.proof.json", bytes("fused-wrong-slot.bin"));
|
|
76
|
+
assert.equal(r.category, "INVALID_SLOT_COMMITMENT");
|
|
77
|
+
assert.equal(r.proof.valid, true, "the underlying proof is sound; only the commitment is wrong");
|
|
78
|
+
assert.match(r.reason!, /does not match the proof's slot record/);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("an unregistered placement id is UNDETERMINED_PLACEMENT", async () => {
|
|
82
|
+
const r = await run("unregistered.proof.json", bytes("fused-unregistered.bin"));
|
|
83
|
+
assert.equal(r.category, "UNDETERMINED_PLACEMENT");
|
|
84
|
+
assert.match(r.reason!, /xmp\/9/);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("a manifest origin hint that contradicts the origin inside the bytes is INVALID_ORIGIN_ATTRIBUTION", async () => {
|
|
88
|
+
// The signed attribution of the undeclared fixture names no origin, so the
|
|
89
|
+
// Frame's manifest supplies the hint. Trailer bytes carry the original as
|
|
90
|
+
// their prefix, and the hint (the PNG) contradicts what the bytes say.
|
|
91
|
+
const frameText = JSON.stringify({
|
|
92
|
+
type: "bitgraph-fuse/1",
|
|
93
|
+
manifest: { placement: "trailer/1", origin: { algorithm: "sha256", digest: Buffer.from(sha256(png)).toString("hex") }, artifact: { algorithm: "sha256", digest: Buffer.from(sha256(bytes("fused-trailer-undeclared.bin"))).toString("hex") }, fusedFile: null },
|
|
94
|
+
proof: proofOf("trailer-undeclared.proof.json"),
|
|
95
|
+
});
|
|
96
|
+
const r = await run("trailer-undeclared.proof.json", bytes("fused-trailer-undeclared.bin"), { frame: frameText });
|
|
97
|
+
assert.equal(r.category, "INVALID_ORIGIN_ATTRIBUTION");
|
|
98
|
+
assert.equal(r.marker?.source, "attribution", "the proof itself is signed fused");
|
|
99
|
+
assert.equal(r.marker?.originSource, "manifest", "the contradicted origin came from the manifest");
|
|
100
|
+
assert.match(r.reason!, /manifest/);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("a manifest marker that agrees with the bytes verifies FUSED_DIRECT", async () => {
|
|
104
|
+
const r = await run("trailer-undeclared.proof.json", bytes("fused-trailer-undeclared.bin"), {
|
|
105
|
+
frame: { type: "bitgraph-fuse/1", manifest: { placement: "trailer/1", origin: { algorithm: "sha256", digest: Buffer.from(sha256(original)).toString("hex") }, artifact: { algorithm: "sha256", digest: "00".repeat(32) }, fusedFile: null }, proof: proofOf("trailer-undeclared.proof.json") },
|
|
106
|
+
});
|
|
107
|
+
assert.equal(r.category, "FUSED_DIRECT");
|
|
108
|
+
assert.equal(r.statements.length, 2);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe("path 4: the file is the original", () => {
|
|
113
|
+
test("trailer/1: the original alone rebuilds and verifies FUSED_FROM_ORIGIN", async () => {
|
|
114
|
+
const r = await run("trailer.proof.json", original);
|
|
115
|
+
assert.equal(r.category, "FUSED_FROM_ORIGIN", r.reason ?? "");
|
|
116
|
+
assert.equal(r.placement, "trailer/1");
|
|
117
|
+
assert.equal(r.originDigestB64, bytesToBase64(sha256(original)));
|
|
118
|
+
assert.equal(r.statements.length, 2);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("container/1: the PNG alone rebuilds the archive", async () => {
|
|
122
|
+
const r = await run("container.proof.json", png);
|
|
123
|
+
assert.equal(r.category, "FUSED_FROM_ORIGIN", r.reason ?? "");
|
|
124
|
+
assert.equal(r.placement, "container/1");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test("produced/1 names a source but is not rebuildable from it: RECONSTRUCTION_MISMATCH", async () => {
|
|
128
|
+
const r = await run("produced-origin.proof.json", original);
|
|
129
|
+
assert.equal(r.category, "RECONSTRUCTION_MISMATCH");
|
|
130
|
+
assert.equal(r.placement, "produced/1");
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("a manifest origin hint on a signed fused proof lets the original rebuild", async () => {
|
|
134
|
+
const r = await run("trailer-undeclared.proof.json", original, {
|
|
135
|
+
frame: { type: "bitgraph-fuse/1", manifest: { placement: "trailer/1", origin: { algorithm: "sha256", digest: Buffer.from(sha256(original)).toString("hex") }, artifact: { algorithm: "sha256", digest: "00".repeat(32) }, fusedFile: null }, proof: proofOf("trailer-undeclared.proof.json") },
|
|
136
|
+
});
|
|
137
|
+
assert.equal(r.category, "FUSED_FROM_ORIGIN");
|
|
138
|
+
assert.equal(r.placement, "trailer/1");
|
|
139
|
+
assert.equal(r.marker?.originSource, "manifest");
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("an undeclared placement resolves through the registry in fixed order and reports the match", async () => {
|
|
143
|
+
// A proof whose signed attribution has no title cannot be minted through the
|
|
144
|
+
// parent (it requires one), so undeclared placement arrives only through a
|
|
145
|
+
// Frame: strip the title here to exercise the registry scan on both paths.
|
|
146
|
+
const p = structuredClone(proofOf("trailer-undeclared.proof.json"));
|
|
147
|
+
// The title is signed; removing it breaks the signature, so instead present a
|
|
148
|
+
// Frame with no placement and rely on the signed marker having one. To reach
|
|
149
|
+
// the scan we need BOTH undeclared, which only a hypothetical name-only
|
|
150
|
+
// attribution gives; construct that case by verifying against a Frame whose
|
|
151
|
+
// proof is name-only and whose signature we re-establish is impossible here,
|
|
152
|
+
// so exercise the scan through the registry helper directly instead.
|
|
153
|
+
const frame = { type: "bitgraph-fuse/1", manifest: { origin: { algorithm: "sha256", digest: Buffer.from(sha256(original)).toString("hex") }, artifact: { algorithm: "sha256", digest: "00".repeat(32) }, fusedFile: null }, proof: p };
|
|
154
|
+
const r = await run("trailer-undeclared.proof.json", original, { frame });
|
|
155
|
+
// The signed title (trailer/1) still declares the placement; the scan is not reached.
|
|
156
|
+
assert.equal(r.category, "FUSED_FROM_ORIGIN");
|
|
157
|
+
assert.equal(r.placement, "trailer/1");
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("an unsigned manifest with a FALSE origin is RECONSTRUCTION_MISMATCH: reconstruction makes the hint self-proving", async () => {
|
|
161
|
+
// The manifest claims the PNG is the origin of the undeclared trailer fixture. The
|
|
162
|
+
// PNG's hash matches the hint, so path 4 runs, and rebuilding from the PNG cannot
|
|
163
|
+
// reproduce the signed artifact digest.
|
|
164
|
+
const r = await run("trailer-undeclared.proof.json", png, {
|
|
165
|
+
frame: { type: "bitgraph-fuse/1", manifest: { placement: "trailer/1", origin: { algorithm: "sha256", digest: Buffer.from(sha256(png)).toString("hex") }, artifact: { algorithm: "sha256", digest: "00".repeat(32) }, fusedFile: null }, proof: proofOf("trailer-undeclared.proof.json") },
|
|
166
|
+
});
|
|
167
|
+
assert.equal(r.category, "RECONSTRUCTION_MISMATCH");
|
|
168
|
+
assert.equal(r.marker?.source, "attribution", "the proof is signed fused");
|
|
169
|
+
assert.equal(r.marker?.originSource, "manifest", "the false origin was the manifest's hint");
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("an unregistered declared placement cannot rebuild: UNDETERMINED_PLACEMENT", async () => {
|
|
173
|
+
const r = await run("unregistered.proof.json", original);
|
|
174
|
+
assert.equal(r.category, "UNDETERMINED_PLACEMENT");
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test("the wrong-slot fixture from its original: rebuilt bytes carry the proof's own commitment, so the hash differs", async () => {
|
|
178
|
+
const r = await run("wrong-slot.proof.json", original);
|
|
179
|
+
assert.equal(r.category, "RECONSTRUCTION_MISMATCH");
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
describe("path 5 and the underlying proof", () => {
|
|
184
|
+
test("a file matching neither digest is NO_MATCH", async () => {
|
|
185
|
+
for (const name of ["recorded.proof.json", "trailer.proof.json", "produced-bare.proof.json"]) {
|
|
186
|
+
const r = await run(name, unrelated);
|
|
187
|
+
assert.equal(r.category, "NO_MATCH", name);
|
|
188
|
+
}
|
|
189
|
+
// The fused bytes of one fixture against another fixture's proof: also NO_MATCH.
|
|
190
|
+
assert.equal((await run("container.proof.json", bytes("fused-trailer.bin"))).category, "NO_MATCH");
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("tampering with the nested proof is INVALID_UNDERLYING_PROOF and never downgrades", async () => {
|
|
194
|
+
const proof = proofOf("trailer.proof.json");
|
|
195
|
+
const cases: Array<[string, (p: BitGraphProof) => void]> = [
|
|
196
|
+
["counter", (p) => { p.commit.counter = String(Number(p.commit.counter) + 1); }],
|
|
197
|
+
["attribution.message (origin)", (p) => { p.attribution!.message = bytesToBase64(sha256(png)); }],
|
|
198
|
+
["attribution.title (placement)", (p) => { p.attribution!.title = "container/1"; }],
|
|
199
|
+
["artifact digest", (p) => { p.artifact.digestB64 = bytesToBase64(sha256(unrelated)); }],
|
|
200
|
+
["slot record nonce", (p) => { p.slotAllocation!.nonceB64 = bytesToBase64(new Uint8Array(32)); }],
|
|
201
|
+
["signature", (p) => { p.signer.signatureB64 = bytesToBase64(new Uint8Array(64)); }],
|
|
202
|
+
];
|
|
203
|
+
for (const [label, mutate] of cases) {
|
|
204
|
+
const p = structuredClone(proof);
|
|
205
|
+
mutate(p);
|
|
206
|
+
resetEpochLinkState();
|
|
207
|
+
const r = await verifyFuse({ proof: p, bytes: bytes("fused-trailer.bin") });
|
|
208
|
+
assert.equal(r.category, "INVALID_UNDERLYING_PROOF", label);
|
|
209
|
+
assert.equal(r.proof.valid, false, label);
|
|
210
|
+
assert.equal(r.statements.length, 0, label);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("tampering with the fused bytes is NO_MATCH (the digest no longer matches)", async () => {
|
|
215
|
+
const f = bytes("fused-trailer.bin");
|
|
216
|
+
f[3] = f[3]! ^ 1;
|
|
217
|
+
assert.equal((await run("trailer.proof.json", f)).category, "NO_MATCH");
|
|
218
|
+
const c = bytes("fused-trailer.bin");
|
|
219
|
+
c[c.length - 1] = c[c.length - 1]! ^ 1; // a commitment byte
|
|
220
|
+
assert.equal((await run("trailer.proof.json", c)).category, "NO_MATCH");
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test("reformatting only the outer Frame changes nothing", async () => {
|
|
224
|
+
const pretty = text("trailer.bitgraph-fuse.json");
|
|
225
|
+
const compact = JSON.stringify(JSON.parse(pretty));
|
|
226
|
+
const a = await run("trailer.proof.json", bytes("fused-trailer.bin"), { frame: pretty });
|
|
227
|
+
const b = await run("trailer.proof.json", bytes("fused-trailer.bin"), { frame: compact });
|
|
228
|
+
assert.deepEqual(a, b);
|
|
229
|
+
assert.equal(a.category, "FUSED_DIRECT");
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test("the ordinary verifier still accepts every fixture proof unchanged", async () => {
|
|
233
|
+
for (const name of ["recorded", "trailer", "container", "produced-origin", "produced-bare", "trailer-undeclared", "wrong-slot", "unregistered"]) {
|
|
234
|
+
resetEpochLinkState();
|
|
235
|
+
const v = await verifyProofIntegrity({ proof: proofOf(`${name}.proof.json`) });
|
|
236
|
+
assert.equal(v.valid, true, `${name}: ${v.reason ?? ""}`);
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
test("the span policy reports separately and never changes the category", async () => {
|
|
241
|
+
const r = await run("trailer.proof.json", bytes("fused-trailer.bin"), { maxPositions: 0 });
|
|
242
|
+
assert.equal(r.category, "FUSED_DIRECT");
|
|
243
|
+
assert.equal(r.policy.spanExceeded, true);
|
|
244
|
+
assert.equal(r.policy.maxPositions, "0");
|
|
245
|
+
const ok = await run("trailer.proof.json", bytes("fused-trailer.bin"), { maxPositions: 1_000_000 });
|
|
246
|
+
assert.equal(ok.policy.spanExceeded, false);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test("a trust policy still applies to the underlying proof", async () => {
|
|
250
|
+
const r = await run("trailer.proof.json", bytes("fused-trailer.bin"), { trustAnchors: { allowedMeasurements: ["00".repeat(48)] } });
|
|
251
|
+
assert.equal(r.category, "INVALID_UNDERLYING_PROOF");
|
|
252
|
+
assert.match(r.reason!, /measurement/i);
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
describe("strict ordering (spec 12.2)", () => {
|
|
257
|
+
test("recorded A, then fused B allocated after A committed: B assembled after A", () => {
|
|
258
|
+
const a = proofOf("recorded.proof.json");
|
|
259
|
+
const b = proofOf("trailer.proof.json");
|
|
260
|
+
assert.equal(assembledAfterCommit(a, b), true);
|
|
261
|
+
});
|
|
262
|
+
test("slot held across an ordinary commit: the strict rule does not claim B after A", () => {
|
|
263
|
+
const a = proofOf("order-held.proof.json");
|
|
264
|
+
const b = proofOf("order-fused-during-hold.proof.json");
|
|
265
|
+
assert.ok(BigInt(b.commit.slotCounter!) < BigInt(a.commit.counter!), "fixture: B's slot precedes A's commit");
|
|
266
|
+
assert.ok(BigInt(a.commit.counter!) < BigInt(b.commit.counter!), "fixture: A committed before B");
|
|
267
|
+
assert.equal(assembledAfterCommit(a, b), false);
|
|
268
|
+
});
|
|
269
|
+
test("incomparable proofs answer null", () => {
|
|
270
|
+
const a = proofOf("recorded.proof.json");
|
|
271
|
+
const b = structuredClone(proofOf("trailer.proof.json"));
|
|
272
|
+
b.commit.epochId = bytesToBase64(new Uint8Array(32).fill(5));
|
|
273
|
+
assert.equal(assembledAfterCommit(a, b), null);
|
|
274
|
+
const c = structuredClone(proofOf("trailer.proof.json"));
|
|
275
|
+
c.signer.publicKeyB64 = bytesToBase64(new Uint8Array(32).fill(6));
|
|
276
|
+
assert.equal(assembledAfterCommit(a, c), null);
|
|
277
|
+
});
|
|
278
|
+
});
|