@mikeargento/bitgraph-verify 1.12.0 → 1.13.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/dist/__tests__/carrier.test.d.ts +2 -0
- package/dist/__tests__/carrier.test.d.ts.map +1 -0
- package/dist/__tests__/carrier.test.js +162 -0
- package/dist/__tests__/carrier.test.js.map +1 -0
- package/dist/carrier-verify.d.ts +16 -0
- package/dist/carrier-verify.d.ts.map +1 -0
- package/dist/carrier-verify.js +82 -0
- package/dist/carrier-verify.js.map +1 -0
- package/dist/carrier.d.ts +140 -0
- package/dist/carrier.d.ts.map +1 -0
- package/dist/carrier.js +457 -0
- package/dist/carrier.js.map +1 -0
- package/dist/fuse-member.js +8 -8
- package/dist/fuse-member.js.map +1 -1
- package/dist/fuse-verify.js +4 -4
- package/dist/fuse-verify.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/verifier.js +4 -4
- package/dist/verifier.js.map +1 -1
- package/package.json +4 -3
- package/src/__tests__/carrier.test.ts +184 -0
- package/src/__tests__/fixtures/carrier/ceiling2.anchor.json +67 -0
- package/src/__tests__/fixtures/carrier/ceiling2.witness.json +5 -0
- package/src/__tests__/fixtures/carrier/demo1.proof.json +47 -0
- package/src/__tests__/fixtures/carrier/demo1.txt +12 -0
- package/src/__tests__/fixtures/carrier/demo2.proof.json +47 -0
- package/src/__tests__/fixtures/carrier/demo2.txt +14 -0
- package/src/__tests__/fixtures/carrier/floor1.anchor.json +67 -0
- package/src/__tests__/fixtures/carrier/floor1.witness.json +5 -0
- package/src/__tests__/fixtures/carrier/floor2.anchor.json +67 -0
- package/src/__tests__/fixtures/carrier/floor2.witness.json +5 -0
- package/src/carrier-verify.ts +101 -0
- package/src/carrier.ts +478 -0
- package/src/fuse-member.ts +8 -8
- package/src/fuse-verify.ts +4 -4
- package/src/index.ts +15 -0
- package/src/verifier.ts +4 -4
- package/dist/run.d.ts +0 -59
- package/dist/run.d.ts.map +0 -1
- package/dist/run.js +0 -147
- package/dist/run.js.map +0 -1
package/src/carrier.ts
ADDED
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
// Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* bitgraph-carrier/1 — the file that carries its own proof.
|
|
5
|
+
*
|
|
6
|
+
* A carrier is the committed bytes followed by one structural block holding
|
|
7
|
+
* the proof and the evidence needed to read its time window offline:
|
|
8
|
+
*
|
|
9
|
+
* [ committed bytes ............................ ] ← exactly what was hashed
|
|
10
|
+
* [ BGPROOF\x01 ][ u32 len ][ payload ][ u32 len ][ BGPROOF\x01 ]
|
|
11
|
+
*
|
|
12
|
+
* The committed digest covers the bytes BEFORE the block, never the block.
|
|
13
|
+
* The block is found from the END of the file only — last 8 bytes must be the
|
|
14
|
+
* magic, then the trailing length, then the leading magic and length must
|
|
15
|
+
* agree — never by scanning, so a carrier fused inside another carrier
|
|
16
|
+
* resolves deterministically to the outermost block, and no ordinary file can
|
|
17
|
+
* be misread as one by accident.
|
|
18
|
+
*
|
|
19
|
+
* THE ENVELOPE RULE. The carrier's own hash is committed nowhere and proves
|
|
20
|
+
* nothing. Anyone can repackage the same committed bytes with the same block
|
|
21
|
+
* and get a different outer hash; that is fine and expected. Never describe
|
|
22
|
+
* the carrier as recorded — the bytes inside it are.
|
|
23
|
+
*
|
|
24
|
+
* THE TIME WINDOW. The payload always carries the floor: the anchor the
|
|
25
|
+
* enclave signed into the slot (`commit.slotAnchor`, present since enclave
|
|
26
|
+
* v7) plus that block's header, so "placed no earlier than this block" checks
|
|
27
|
+
* offline by identity. The ceiling cannot exist when the carrier is written —
|
|
28
|
+
* the anchor that follows the commit has not landed yet — so it is either
|
|
29
|
+
* `present` (stamped in later from public data) or `unfetched`, stated in so
|
|
30
|
+
* many words. There is no third state, and absence of the field is a corrupt
|
|
31
|
+
* block, not a missing bound.
|
|
32
|
+
*
|
|
33
|
+
* ⚠️ TWO BYTE-IDENTICAL COPIES OF THIS FILE EXIST, on purpose:
|
|
34
|
+
* packages/verify/src/carrier.ts (canonical, published with the package)
|
|
35
|
+
* website/src/lib/carrier.ts (the site cannot import unpublished code)
|
|
36
|
+
* A parity test in the website suite asserts they match. Edit both or the
|
|
37
|
+
* test fails the build. The file is self-contained (only @noble imports) so
|
|
38
|
+
* the copies can be identical.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
import { sha256 } from "@noble/hashes/sha256";
|
|
42
|
+
import { keccak_256 } from "@noble/hashes/sha3";
|
|
43
|
+
|
|
44
|
+
/* ── The block ──────────────────────────────────────────────────────────── */
|
|
45
|
+
|
|
46
|
+
/** "BGPROOF" + 0x01: seven ASCII bytes and a block version. */
|
|
47
|
+
export const CARRIER_MAGIC = Uint8Array.from([0x42, 0x47, 0x50, 0x52, 0x4f, 0x4f, 0x46, 0x01]);
|
|
48
|
+
/** magic + u32 + u32 + magic around the payload. */
|
|
49
|
+
export const CARRIER_OVERHEAD = 24;
|
|
50
|
+
/** A payload larger than this is refused as corrupt: a manifest of 100k members fits in a tenth of it. */
|
|
51
|
+
export const MAX_CARRIER_PAYLOAD = 8 * 1024 * 1024;
|
|
52
|
+
|
|
53
|
+
export const CARRIER_VERSION = "bitgraph-carrier/1";
|
|
54
|
+
|
|
55
|
+
/* ── The payload ────────────────────────────────────────────────────────── */
|
|
56
|
+
|
|
57
|
+
/** The raw Ethereum block header, so the block's identity and time check offline. */
|
|
58
|
+
export interface CarrierWitness {
|
|
59
|
+
headerRlpHex: string;
|
|
60
|
+
blockNumber: number;
|
|
61
|
+
blockHash: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** A minimal view of a bitgraph/1 proof; the real shape is the verify package's. */
|
|
65
|
+
export type CarrierProof = Record<string, unknown>;
|
|
66
|
+
|
|
67
|
+
export interface CarrierFloor {
|
|
68
|
+
status: "present";
|
|
69
|
+
/** The anchor proof named by the carried proof's signed `commit.slotAnchor`. */
|
|
70
|
+
anchor: CarrierProof;
|
|
71
|
+
witness: CarrierWitness;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export type CarrierCeiling =
|
|
75
|
+
| {
|
|
76
|
+
status: "unfetched";
|
|
77
|
+
/** Set when a completion looked and nothing had landed yet, so "never looked" and "not yet" read apart. */
|
|
78
|
+
searched?: { asOfCounter: string; epochId: string };
|
|
79
|
+
}
|
|
80
|
+
| {
|
|
81
|
+
status: "present";
|
|
82
|
+
/** How the ceiling anchor is ordered after the proof. v1 stamps counter-order only. */
|
|
83
|
+
basis: "counter-order";
|
|
84
|
+
anchor: CarrierProof;
|
|
85
|
+
witness: CarrierWitness;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export interface CarrierPayload {
|
|
89
|
+
carrier: typeof CARRIER_VERSION;
|
|
90
|
+
/** The bitgraph/1 proof exactly as the commit returned it. */
|
|
91
|
+
proof: CarrierProof;
|
|
92
|
+
floor: CarrierFloor;
|
|
93
|
+
ceiling: CarrierCeiling;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export type CarrierParse =
|
|
97
|
+
/** No block: the bytes are not a carrier. Not a verdict about anything. */
|
|
98
|
+
| { kind: "none" }
|
|
99
|
+
/** A block is there but cannot be read. A corrupted block, not a forgery. */
|
|
100
|
+
| { kind: "corrupt"; reason: string }
|
|
101
|
+
| {
|
|
102
|
+
kind: "carrier";
|
|
103
|
+
/** The committed bytes: everything before the block. A view, not a copy. */
|
|
104
|
+
inner: Uint8Array;
|
|
105
|
+
payload: CarrierPayload;
|
|
106
|
+
/** Where the block starts, = inner.length. */
|
|
107
|
+
blockOffset: number;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/* ── Small helpers ──────────────────────────────────────────────────────── */
|
|
111
|
+
|
|
112
|
+
const te = new TextEncoder();
|
|
113
|
+
|
|
114
|
+
function u32be(n: number): Uint8Array {
|
|
115
|
+
return Uint8Array.from([(n >>> 24) & 0xff, (n >>> 16) & 0xff, (n >>> 8) & 0xff, n & 0xff]);
|
|
116
|
+
}
|
|
117
|
+
function readU32be(b: Uint8Array, at: number): number {
|
|
118
|
+
return ((b[at]! << 24) | (b[at + 1]! << 16) | (b[at + 2]! << 8) | b[at + 3]!) >>> 0;
|
|
119
|
+
}
|
|
120
|
+
function magicAt(b: Uint8Array, at: number): boolean {
|
|
121
|
+
if (at < 0 || at + 8 > b.length) return false;
|
|
122
|
+
for (let i = 0; i < 8; i++) if (b[at + i] !== CARRIER_MAGIC[i]!) return false;
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
function record(v: unknown): Record<string, unknown> | null {
|
|
126
|
+
return typeof v === "object" && v !== null && !Array.isArray(v) ? (v as Record<string, unknown>) : null;
|
|
127
|
+
}
|
|
128
|
+
/** base64 (either alphabet, padded or not) → bytes, or null. */
|
|
129
|
+
export function b64ToBytes(s: string): Uint8Array | null {
|
|
130
|
+
if (typeof s !== "string" || !/^[A-Za-z0-9+/_-]*={0,2}$/.test(s)) return null;
|
|
131
|
+
const std = s.replace(/-/g, "+").replace(/_/g, "/");
|
|
132
|
+
const pad = std.length % 4 === 0 ? std : std + "=".repeat(4 - (std.length % 4));
|
|
133
|
+
try {
|
|
134
|
+
if (typeof atob === "function") {
|
|
135
|
+
const bin = atob(pad);
|
|
136
|
+
const out = new Uint8Array(bin.length);
|
|
137
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
140
|
+
return new Uint8Array(Buffer.from(pad, "base64"));
|
|
141
|
+
} catch {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
export function bytesToB64(b: Uint8Array): string {
|
|
146
|
+
if (typeof btoa === "function") {
|
|
147
|
+
let s = "";
|
|
148
|
+
for (let i = 0; i < b.length; i++) s += String.fromCharCode(b[i]!);
|
|
149
|
+
return btoa(s);
|
|
150
|
+
}
|
|
151
|
+
return Buffer.from(b).toString("base64");
|
|
152
|
+
}
|
|
153
|
+
/** Digest equality across the two base64 alphabets. */
|
|
154
|
+
export function sameDigest(a: unknown, b: unknown): boolean {
|
|
155
|
+
if (typeof a !== "string" || typeof b !== "string") return false;
|
|
156
|
+
const na = b64ToBytes(a), nb = b64ToBytes(b);
|
|
157
|
+
if (na === null || nb === null || na.length !== nb.length) return false;
|
|
158
|
+
let diff = 0;
|
|
159
|
+
for (let i = 0; i < na.length; i++) diff |= na[i]! ^ nb[i]!;
|
|
160
|
+
return diff === 0;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/* ── Build, parse, complete ─────────────────────────────────────────────── */
|
|
164
|
+
|
|
165
|
+
export function encodeCarrierBlock(payload: CarrierPayload): Uint8Array {
|
|
166
|
+
const body = te.encode(JSON.stringify(payload));
|
|
167
|
+
if (body.length > MAX_CARRIER_PAYLOAD) throw new Error(`carrier payload too large: ${body.length} bytes`);
|
|
168
|
+
const out = new Uint8Array(body.length + CARRIER_OVERHEAD);
|
|
169
|
+
out.set(CARRIER_MAGIC, 0);
|
|
170
|
+
out.set(u32be(body.length), 8);
|
|
171
|
+
out.set(body, 12);
|
|
172
|
+
out.set(u32be(body.length), 12 + body.length);
|
|
173
|
+
out.set(CARRIER_MAGIC, 16 + body.length);
|
|
174
|
+
return out;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** The committed bytes followed by their block. The inner bytes are copied, never altered. */
|
|
178
|
+
export function buildCarrier(inner: Uint8Array, payload: CarrierPayload): Uint8Array {
|
|
179
|
+
const block = encodeCarrierBlock(payload);
|
|
180
|
+
const out = new Uint8Array(inner.length + block.length);
|
|
181
|
+
out.set(inner, 0);
|
|
182
|
+
out.set(block, inner.length);
|
|
183
|
+
return out;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Structural payload check: is this object a readable bitgraph-carrier/1 payload? */
|
|
187
|
+
function payloadShapeError(p: unknown): string | null {
|
|
188
|
+
const o = record(p);
|
|
189
|
+
if (o === null) return "payload is not an object";
|
|
190
|
+
if (o["carrier"] !== CARRIER_VERSION) return `unknown carrier version ${JSON.stringify(o["carrier"])}`;
|
|
191
|
+
if (record(o["proof"]) === null) return "payload carries no proof object";
|
|
192
|
+
const floor = record(o["floor"]);
|
|
193
|
+
if (floor === null || floor["status"] !== "present") return "floor must be present: a floorless carrier is not a not-before";
|
|
194
|
+
if (record(floor["anchor"]) === null) return "floor carries no anchor proof";
|
|
195
|
+
const fw = record(floor["witness"]);
|
|
196
|
+
if (fw === null || typeof fw["headerRlpHex"] !== "string" || typeof fw["blockNumber"] !== "number" || typeof fw["blockHash"] !== "string") {
|
|
197
|
+
return "floor carries no readable block-header witness";
|
|
198
|
+
}
|
|
199
|
+
const ceil = record(o["ceiling"]);
|
|
200
|
+
if (ceil === null) return "ceiling is missing: absence must be a stated fact, not a missing key";
|
|
201
|
+
if (ceil["status"] === "unfetched") return null;
|
|
202
|
+
if (ceil["status"] !== "present") return `ceiling status must be "unfetched" or "present", got ${JSON.stringify(ceil["status"])}`;
|
|
203
|
+
if (ceil["basis"] !== "counter-order") return `unknown ceiling basis ${JSON.stringify(ceil["basis"])}`;
|
|
204
|
+
if (record(ceil["anchor"]) === null) return "ceiling carries no anchor proof";
|
|
205
|
+
const cw = record(ceil["witness"]);
|
|
206
|
+
if (cw === null || typeof cw["headerRlpHex"] !== "string" || typeof cw["blockNumber"] !== "number" || typeof cw["blockHash"] !== "string") {
|
|
207
|
+
return "ceiling carries no readable block-header witness";
|
|
208
|
+
}
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function parseCarrier(bytes: Uint8Array): CarrierParse {
|
|
213
|
+
if (bytes.length < CARRIER_OVERHEAD + 2) return { kind: "none" };
|
|
214
|
+
if (!magicAt(bytes, bytes.length - 8)) return { kind: "none" };
|
|
215
|
+
const len = readU32be(bytes, bytes.length - 12);
|
|
216
|
+
if (len > MAX_CARRIER_PAYLOAD) return { kind: "corrupt", reason: `declared payload length ${len} exceeds the cap` };
|
|
217
|
+
const start = bytes.length - CARRIER_OVERHEAD - len;
|
|
218
|
+
if (start < 0) return { kind: "corrupt", reason: "declared payload length runs past the start of the file" };
|
|
219
|
+
if (!magicAt(bytes, start)) return { kind: "corrupt", reason: "leading magic missing where the trailing length points" };
|
|
220
|
+
if (readU32be(bytes, start + 8) !== len) return { kind: "corrupt", reason: "leading and trailing payload lengths disagree" };
|
|
221
|
+
let payload: unknown;
|
|
222
|
+
try {
|
|
223
|
+
payload = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes.subarray(start + 12, start + 12 + len)));
|
|
224
|
+
} catch {
|
|
225
|
+
return { kind: "corrupt", reason: "payload is not valid UTF-8 JSON" };
|
|
226
|
+
}
|
|
227
|
+
const shape = payloadShapeError(payload);
|
|
228
|
+
if (shape !== null) return { kind: "corrupt", reason: shape };
|
|
229
|
+
return { kind: "carrier", inner: bytes.subarray(0, start), payload: payload as CarrierPayload, blockOffset: start };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Stamp a verified ceiling into a carrier, or record that a completion looked
|
|
234
|
+
* and found nothing yet. Pure: callers verify the material BEFORE stamping.
|
|
235
|
+
* Never overwrites: a present ceiling that differs from the offered one is
|
|
236
|
+
* reported, since a conflicting embedded ceiling is evidence worth keeping.
|
|
237
|
+
*/
|
|
238
|
+
export function completeCarrier(
|
|
239
|
+
bytes: Uint8Array,
|
|
240
|
+
ceiling: { anchor: CarrierProof; witness: CarrierWitness } | null,
|
|
241
|
+
searched?: { asOfCounter: string; epochId: string },
|
|
242
|
+
): { changed: boolean; bytes: Uint8Array; error?: string } {
|
|
243
|
+
const p = parseCarrier(bytes);
|
|
244
|
+
if (p.kind !== "carrier") return { changed: false, bytes, error: p.kind === "none" ? "not a carrier" : `corrupt block: ${p.reason}` };
|
|
245
|
+
const cur = p.payload.ceiling;
|
|
246
|
+
if (cur.status === "present") {
|
|
247
|
+
if (ceiling === null) return { changed: false, bytes };
|
|
248
|
+
const a = record(record(cur.anchor)?.["commit"]);
|
|
249
|
+
const b = record(record(ceiling.anchor)?.["commit"]);
|
|
250
|
+
const same = a !== null && b !== null && a["counter"] === b["counter"] && cur.witness.blockHash.toLowerCase() === ceiling.witness.blockHash.toLowerCase();
|
|
251
|
+
return same ? { changed: false, bytes } : { changed: false, bytes, error: "carrier already holds a different ceiling; refusing to overwrite it" };
|
|
252
|
+
}
|
|
253
|
+
const payload: CarrierPayload = {
|
|
254
|
+
...p.payload,
|
|
255
|
+
ceiling: ceiling === null
|
|
256
|
+
? { status: "unfetched", ...(searched ? { searched } : {}) }
|
|
257
|
+
: { status: "present", basis: "counter-order", anchor: ceiling.anchor, witness: ceiling.witness },
|
|
258
|
+
};
|
|
259
|
+
if (ceiling === null && !searched) return { changed: false, bytes };
|
|
260
|
+
return { changed: true, bytes: buildCarrier(p.inner, payload) };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/* ── Consistency checks (no network, no signatures — bindings only) ─────── */
|
|
264
|
+
|
|
265
|
+
/** The identity an authenticated anchor proof claims: enclave-signed since v7. */
|
|
266
|
+
export function anchorIdentity(anchor: unknown): { counter: string; epochId: string; chainId: string; publicKeyB64: string; blockNumber: number; blockHash: string } | null {
|
|
267
|
+
const p = record(anchor);
|
|
268
|
+
const commit = record(p?.["commit"]);
|
|
269
|
+
const signer = record(p?.["signer"]);
|
|
270
|
+
const a = record(commit?.["anchor"]);
|
|
271
|
+
if (!p || !commit || !signer || !a) return null;
|
|
272
|
+
const counter = commit["counter"], epochId = commit["epochId"], chainId = commit["chainId"];
|
|
273
|
+
const publicKeyB64 = signer["publicKeyB64"], blockNumber = a["blockNumber"], blockHash = a["blockHash"];
|
|
274
|
+
if (typeof counter !== "string" || typeof epochId !== "string" || typeof chainId !== "string") return null;
|
|
275
|
+
if (typeof publicKeyB64 !== "string" || typeof blockNumber !== "number" || typeof blockHash !== "string") return null;
|
|
276
|
+
return { counter, epochId, chainId, publicKeyB64, blockNumber, blockHash };
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function partitionOf(proof: unknown): { epochId: string; chainId: string; publicKeyB64: string; counter: string; slotAnchor: { counter: string; blockNumber: number; blockHash: string } | null } | null {
|
|
280
|
+
const p = record(proof);
|
|
281
|
+
const commit = record(p?.["commit"]);
|
|
282
|
+
const signer = record(p?.["signer"]);
|
|
283
|
+
if (!p || !commit || !signer) return null;
|
|
284
|
+
const epochId = commit["epochId"], chainId = commit["chainId"], counter = commit["counter"], publicKeyB64 = signer["publicKeyB64"];
|
|
285
|
+
if (typeof epochId !== "string" || typeof chainId !== "string" || typeof counter !== "string" || typeof publicKeyB64 !== "string") return null;
|
|
286
|
+
const sa = record(commit["slotAnchor"]);
|
|
287
|
+
const slotAnchor = sa && typeof sa["counter"] === "string" && typeof sa["blockNumber"] === "number" && typeof sa["blockHash"] === "string"
|
|
288
|
+
? { counter: sa["counter"], blockNumber: sa["blockNumber"], blockHash: sa["blockHash"] }
|
|
289
|
+
: null;
|
|
290
|
+
return { epochId, chainId, publicKeyB64, counter, slotAnchor };
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* The floor must be the anchor the enclave signed into the slot — matched by
|
|
295
|
+
* identity against `commit.slotAnchor`, never merely "some anchor below the
|
|
296
|
+
* slot". An older anchor would read as an earlier not-before than was signed.
|
|
297
|
+
*/
|
|
298
|
+
export function checkFloorBinding(proof: unknown, floor: CarrierFloor): string[] {
|
|
299
|
+
const errs: string[] = [];
|
|
300
|
+
const part = partitionOf(proof);
|
|
301
|
+
if (part === null) return ["proof is missing commit or signer fields"];
|
|
302
|
+
if (part.slotAnchor === null) return ["proof carries no signed slotAnchor (pre-v7): it cannot travel as a carrier floor"];
|
|
303
|
+
const id = anchorIdentity(floor.anchor);
|
|
304
|
+
if (id === null) return ["floor anchor is not an authenticated anchor (no signed commit.anchor)"];
|
|
305
|
+
if (id.chainId !== part.chainId || id.epochId !== part.epochId || id.publicKeyB64 !== part.publicKeyB64) {
|
|
306
|
+
errs.push("floor anchor is not from the proof's own chain, epoch and key");
|
|
307
|
+
}
|
|
308
|
+
if (id.counter !== part.slotAnchor.counter) errs.push(`floor anchor counter ${id.counter} is not the signed slotAnchor counter ${part.slotAnchor.counter}`);
|
|
309
|
+
if (id.blockNumber !== part.slotAnchor.blockNumber) errs.push(`floor anchor block ${id.blockNumber} is not the signed slotAnchor block ${part.slotAnchor.blockNumber}`);
|
|
310
|
+
if (id.blockHash.toLowerCase() !== part.slotAnchor.blockHash.toLowerCase()) errs.push("floor anchor block hash differs from the signed slotAnchor block hash");
|
|
311
|
+
if (floor.witness.blockNumber !== id.blockNumber) errs.push("floor witness is for a different block number than the floor anchor");
|
|
312
|
+
if (floor.witness.blockHash.toLowerCase() !== id.blockHash.toLowerCase()) errs.push("floor witness is for a different block hash than the floor anchor");
|
|
313
|
+
return errs;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** A present ceiling: same partition, strictly after the commit, counter-order. */
|
|
317
|
+
export function checkCeilingBinding(proof: unknown, ceiling: Extract<CarrierCeiling, { status: "present" }>): string[] {
|
|
318
|
+
const errs: string[] = [];
|
|
319
|
+
const part = partitionOf(proof);
|
|
320
|
+
if (part === null) return ["proof is missing commit or signer fields"];
|
|
321
|
+
const id = anchorIdentity(ceiling.anchor);
|
|
322
|
+
if (id === null) return ["ceiling anchor is not an authenticated anchor (no signed commit.anchor)"];
|
|
323
|
+
if (id.chainId !== part.chainId || id.epochId !== part.epochId || id.publicKeyB64 !== part.publicKeyB64) {
|
|
324
|
+
errs.push("ceiling anchor is not from the proof's own chain, epoch and key: counter-order says nothing across epochs");
|
|
325
|
+
}
|
|
326
|
+
let after = false;
|
|
327
|
+
try {
|
|
328
|
+
after = BigInt(id.counter) > BigInt(part.counter);
|
|
329
|
+
} catch {
|
|
330
|
+
errs.push("ceiling or commit counter is not a whole number");
|
|
331
|
+
}
|
|
332
|
+
if (!after) errs.push(`ceiling anchor counter ${id.counter} does not follow the commit counter ${part.counter}`);
|
|
333
|
+
if (ceiling.witness.blockNumber !== id.blockNumber) errs.push("ceiling witness is for a different block number than the ceiling anchor");
|
|
334
|
+
if (ceiling.witness.blockHash.toLowerCase() !== id.blockHash.toLowerCase()) errs.push("ceiling witness is for a different block hash than the ceiling anchor");
|
|
335
|
+
return errs;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* An anchor commits SHA-256 of its signed message, the block hash string.
|
|
340
|
+
* This ties the anchor's artifact digest to the block it claims, offline.
|
|
341
|
+
* Also returns the exact bytes the anchor's own digest covers, for verify().
|
|
342
|
+
*/
|
|
343
|
+
export function anchorMessageBytes(anchor: unknown): { bytes: Uint8Array; error: string | null } {
|
|
344
|
+
const p = record(anchor);
|
|
345
|
+
const attr = record(p?.["attribution"]);
|
|
346
|
+
const id = anchorIdentity(anchor);
|
|
347
|
+
const msg = typeof attr?.["message"] === "string" ? (attr["message"] as string) : id ? id.blockHash.toLowerCase() : null;
|
|
348
|
+
if (msg === null) return { bytes: new Uint8Array(), error: "anchor carries no signed message and no block hash" };
|
|
349
|
+
const bytes = te.encode(msg);
|
|
350
|
+
const artifact = record(p?.["artifact"]);
|
|
351
|
+
const digest = artifact?.["digestB64"];
|
|
352
|
+
if (typeof digest !== "string" || !sameDigest(bytesToB64(sha256(bytes)), digest)) {
|
|
353
|
+
return { bytes, error: "anchor's artifact digest is not the SHA-256 of its signed block-hash message" };
|
|
354
|
+
}
|
|
355
|
+
if (id && msg.toLowerCase() !== id.blockHash.toLowerCase()) return { bytes, error: "anchor's signed message is not its claimed block hash" };
|
|
356
|
+
return { bytes, error: null };
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/* ── The block-header witness (keccak + minimal RLP) ────────────────────── */
|
|
360
|
+
/* The same procedure bitgraph-audit's witness.ts runs (docs/BUNDLE-FORMAT.md
|
|
361
|
+
§10.3), reduced to what a carrier needs: recompute the hash, cross-check
|
|
362
|
+
number, read the timestamp. Ethereum headers RLP-encode as one list whose
|
|
363
|
+
items 8 and 11 are the block number and timestamp. */
|
|
364
|
+
|
|
365
|
+
function hexToBytes(hex: string): Uint8Array | null {
|
|
366
|
+
const clean = hex.startsWith("0x") || hex.startsWith("0X") ? hex.slice(2) : hex;
|
|
367
|
+
if (clean.length % 2 !== 0 || !/^[0-9a-fA-F]*$/.test(clean)) return null;
|
|
368
|
+
const out = new Uint8Array(clean.length / 2);
|
|
369
|
+
for (let i = 0; i < out.length; i++) out[i] = parseInt(clean.slice(i * 2, i * 2 + 2), 16);
|
|
370
|
+
return out;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Top-level items of one RLP list, as raw byte payloads. Null when malformed. */
|
|
374
|
+
export function decodeRlpTop(bytes: Uint8Array): Uint8Array[] | null {
|
|
375
|
+
if (bytes.length === 0) return null;
|
|
376
|
+
const b0 = bytes[0]!;
|
|
377
|
+
let at: number, end: number;
|
|
378
|
+
if (b0 >= 0xf8) {
|
|
379
|
+
const ll = b0 - 0xf7;
|
|
380
|
+
if (1 + ll > bytes.length) return null;
|
|
381
|
+
let len = 0;
|
|
382
|
+
for (let i = 0; i < ll; i++) len = len * 256 + bytes[1 + i]!;
|
|
383
|
+
at = 1 + ll;
|
|
384
|
+
end = at + len;
|
|
385
|
+
} else if (b0 >= 0xc0) {
|
|
386
|
+
at = 1;
|
|
387
|
+
end = at + (b0 - 0xc0);
|
|
388
|
+
} else return null;
|
|
389
|
+
if (end !== bytes.length) return null;
|
|
390
|
+
const items: Uint8Array[] = [];
|
|
391
|
+
while (at < end) {
|
|
392
|
+
const h = bytes[at]!;
|
|
393
|
+
if (h < 0x80) {
|
|
394
|
+
items.push(bytes.subarray(at, at + 1));
|
|
395
|
+
at += 1;
|
|
396
|
+
} else if (h <= 0xb7) {
|
|
397
|
+
const len = h - 0x80;
|
|
398
|
+
if (at + 1 + len > end) return null;
|
|
399
|
+
items.push(bytes.subarray(at + 1, at + 1 + len));
|
|
400
|
+
at += 1 + len;
|
|
401
|
+
} else if (h <= 0xbf) {
|
|
402
|
+
const ll = h - 0xb7;
|
|
403
|
+
if (at + 1 + ll > end) return null;
|
|
404
|
+
let len = 0;
|
|
405
|
+
for (let i = 0; i < ll; i++) len = len * 256 + bytes[at + 1 + i]!;
|
|
406
|
+
if (at + 1 + ll + len > end) return null;
|
|
407
|
+
items.push(bytes.subarray(at + 1 + ll, at + 1 + ll + len));
|
|
408
|
+
at += 1 + ll + len;
|
|
409
|
+
} else {
|
|
410
|
+
// A nested list inside a header's top level (withdrawals etc. stay hashed): skip it whole.
|
|
411
|
+
let len: number, hl: number;
|
|
412
|
+
if (h <= 0xf7) {
|
|
413
|
+
len = h - 0xc0;
|
|
414
|
+
hl = 1;
|
|
415
|
+
} else {
|
|
416
|
+
const ll = h - 0xf7;
|
|
417
|
+
len = 0;
|
|
418
|
+
for (let i = 0; i < ll; i++) len = len * 256 + bytes[at + 1 + i]!;
|
|
419
|
+
hl = 1 + ll;
|
|
420
|
+
}
|
|
421
|
+
if (at + hl + len > end) return null;
|
|
422
|
+
items.push(bytes.subarray(at + hl, at + hl + len));
|
|
423
|
+
at += hl + len;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return items;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function rlpNumber(item: Uint8Array): number | null {
|
|
430
|
+
if (item.length > 6) return null;
|
|
431
|
+
let n = 0;
|
|
432
|
+
for (const b of item) n = n * 256 + b;
|
|
433
|
+
return n;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
export interface WitnessCheck {
|
|
437
|
+
ok: boolean;
|
|
438
|
+
error: string | null;
|
|
439
|
+
/** The block's own timestamp, seconds since the epoch, read from the verified header. */
|
|
440
|
+
timestamp: number | null;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** keccak256(header) must reproduce the block hash; the header's number must match. Offline. */
|
|
444
|
+
export function verifyWitnessHeader(w: CarrierWitness): WitnessCheck {
|
|
445
|
+
const raw = hexToBytes(w.headerRlpHex);
|
|
446
|
+
if (raw === null) return { ok: false, error: "witness header is not hex", timestamp: null };
|
|
447
|
+
const hash = "0x" + Array.from(keccak_256(raw), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
448
|
+
if (hash !== w.blockHash.toLowerCase()) return { ok: false, error: "keccak-256 of the witness header does not reproduce the block hash", timestamp: null };
|
|
449
|
+
const items = decodeRlpTop(raw);
|
|
450
|
+
if (items === null || items.length < 12) return { ok: false, error: "witness header is not a well-formed block-header list", timestamp: null };
|
|
451
|
+
const num = rlpNumber(items[8]!);
|
|
452
|
+
if (num !== w.blockNumber) return { ok: false, error: `witness header is block ${num ?? "?"}, not ${w.blockNumber}`, timestamp: null };
|
|
453
|
+
return { ok: true, error: null, timestamp: rlpNumber(items[11]!) };
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/* ── The window a reader states ─────────────────────────────────────────── */
|
|
457
|
+
|
|
458
|
+
export interface CarrierBounds {
|
|
459
|
+
notBefore: { blockNumber: number; blockHash: string; timestamp: number | null };
|
|
460
|
+
/** null means NOT FETCHED — say so in those words — never "none exists". */
|
|
461
|
+
notAfter: { blockNumber: number; blockHash: string; timestamp: number | null } | null;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
export function carrierBounds(payload: CarrierPayload): CarrierBounds {
|
|
465
|
+
const f = payload.floor.witness;
|
|
466
|
+
const floorTs = verifyWitnessHeader(f);
|
|
467
|
+
const notBefore = { blockNumber: f.blockNumber, blockHash: f.blockHash.toLowerCase(), timestamp: floorTs.ok ? floorTs.timestamp : null };
|
|
468
|
+
if (payload.ceiling.status !== "present") return { notBefore, notAfter: null };
|
|
469
|
+
const c = payload.ceiling.witness;
|
|
470
|
+
const ceilTs = verifyWitnessHeader(c);
|
|
471
|
+
return { notBefore, notAfter: { blockNumber: c.blockNumber, blockHash: c.blockHash.toLowerCase(), timestamp: ceilTs.ok ? ceilTs.timestamp : null } };
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/** SHA-256 of the committed bytes, compared to the carried proof's artifact digest. */
|
|
475
|
+
export function innerDigestMatches(inner: Uint8Array, payload: CarrierPayload): boolean {
|
|
476
|
+
const artifact = record(payload.proof["artifact"]);
|
|
477
|
+
return sameDigest(bytesToB64(sha256(inner)), artifact?.["digestB64"] as string);
|
|
478
|
+
}
|
package/src/fuse-member.ts
CHANGED
|
@@ -289,7 +289,7 @@ function admit(bound: BoundSet, fusedBytes: Uint8Array, row: SetMember): Admitte
|
|
|
289
289
|
return {
|
|
290
290
|
category: "INVALID_SLOT_COMMITMENT",
|
|
291
291
|
placement: placement.id,
|
|
292
|
-
reason: "listed in the committed set manifest, but the bytes carry a commitment to a different
|
|
292
|
+
reason: "listed in the committed set manifest, but the bytes carry a commitment to a different position; membership without the floor is not a member verdict",
|
|
293
293
|
};
|
|
294
294
|
}
|
|
295
295
|
const embedded = located.originDigest ?? (located.originalBytes !== undefined ? sha256(located.originalBytes) : undefined);
|
|
@@ -306,19 +306,19 @@ function admit(bound: BoundSet, fusedBytes: Uint8Array, row: SetMember): Admitte
|
|
|
306
306
|
|
|
307
307
|
/** The stray's two-part reason: made after the slot, yet not in the committed set. */
|
|
308
308
|
function strayReason(span: FuseSpan | null, memberCount: number): string {
|
|
309
|
-
const slot = span !== null ? `
|
|
309
|
+
const slot = span !== null ? `position ${span.slotCounter}` : "its reserved position";
|
|
310
310
|
const commit = span !== null ? `committed at position ${span.commitCounter}` : "at its commit";
|
|
311
|
-
return `these bytes carry the commitment of ${slot}, so they were made after that
|
|
311
|
+
return `these bytes carry the position commitment of ${slot}, so they were made after that position existed, but their digest is not among the ${memberCount} members listed in the set manifest ${commit}; the set proof does not cover them`;
|
|
312
312
|
}
|
|
313
313
|
|
|
314
314
|
/** set/2: the two-part reason when the floor holds but no evidence, or evidence for another member, is in hand. */
|
|
315
315
|
function unprovenReason(span: FuseSpan | null, memberCount: number, otherMember: boolean): string {
|
|
316
|
-
const slot = span !== null ? `
|
|
316
|
+
const slot = span !== null ? `position ${span.slotCounter}` : "its reserved position";
|
|
317
317
|
const commit = span !== null ? `committed at position ${span.commitCounter}` : "at its commit";
|
|
318
318
|
const shown = otherMember
|
|
319
319
|
? "the member evidence in hand describes a different member, so"
|
|
320
320
|
: "no member evidence (row, index, path) is in hand, so";
|
|
321
|
-
return `these bytes carry the commitment of ${slot}, so they were made after that
|
|
321
|
+
return `these bytes carry the position commitment of ${slot}, so they were made after that position existed, but ${shown} their place among the ${memberCount} members of the set ${commit} is not shown; a set/2 proof covers a member only with its path`;
|
|
322
322
|
}
|
|
323
323
|
|
|
324
324
|
export async function verifyFuseMember(opts: FuseMemberOptions): Promise<FuseMemberResult> {
|
|
@@ -536,7 +536,7 @@ export async function verifyFuseMember(opts: FuseMemberOptions): Promise<FuseMem
|
|
|
536
536
|
return base(
|
|
537
537
|
"RECONSTRUCTION_MISMATCH",
|
|
538
538
|
{ ...withSet, placement: candidates[0] ?? null, originDigestB64: fileDigestB64 },
|
|
539
|
-
`rebuilding ${candidates.join(" or ")} from this file and the proof's
|
|
539
|
+
`rebuilding ${candidates.join(" or ")} from this file and the proof's position record does not reproduce the listed member digest`,
|
|
540
540
|
);
|
|
541
541
|
}
|
|
542
542
|
|
|
@@ -554,7 +554,7 @@ export async function verifyFuseMember(opts: FuseMemberOptions): Promise<FuseMem
|
|
|
554
554
|
const other = bound.rows.length > 0;
|
|
555
555
|
return base(other ? "INVALID_SET_PATH" : "SET_MEMBERSHIP_UNPROVEN", { ...withSet, placement: p.id, statements: [] }, unprovenReason(span, bound.count, other));
|
|
556
556
|
}
|
|
557
|
-
return base("NO_MATCH", withSet, "these bytes carry a commitment to a different
|
|
557
|
+
return base("NO_MATCH", withSet, "these bytes carry a commitment to a different position; the proof proves nothing about them");
|
|
558
558
|
}
|
|
559
|
-
return base("NO_MATCH", withSet, "this file is neither a listed member nor a listed original and carries no commitment to this
|
|
559
|
+
return base("NO_MATCH", withSet, "this file is neither a listed member nor a listed original and carries no commitment to this position; the proof proves nothing about it");
|
|
560
560
|
}
|
package/src/fuse-verify.ts
CHANGED
|
@@ -126,7 +126,7 @@ export function spanOf(proof: BitGraphProof): FuseSpan | null {
|
|
|
126
126
|
|
|
127
127
|
/** The floor sentence every fused verdict ends with: bounded below by the slot, above by the commit. */
|
|
128
128
|
export function floorStatement(span: FuseSpan): string {
|
|
129
|
-
return `The exact fused bytes could not feasibly have been finalized before
|
|
129
|
+
return `The exact fused bytes could not feasibly have been finalized before position ${span.slotCounter} was reserved and signed for them, and were committed no later than position ${span.commitCounter}.`;
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
function statements(category: FuseCategory, span: FuseSpan | null, originMatched: boolean): string[] {
|
|
@@ -163,8 +163,8 @@ function statements(category: FuseCategory, span: FuseSpan | null, originMatched
|
|
|
163
163
|
function inlineStatements(span: FuseSpan | null, offset: number): string[] {
|
|
164
164
|
if (span === null) return [];
|
|
165
165
|
return [
|
|
166
|
-
`The enclave
|
|
167
|
-
`These exact bytes contain that
|
|
166
|
+
`The enclave reserved and signed position ${span.slotCounter} before this artifact's digest reached it.`,
|
|
167
|
+
`These exact bytes contain that position's commitment, first at byte ${offset}, so they were assembled after position ${span.slotCounter} existed and were committed at position ${span.commitCounter}.`,
|
|
168
168
|
];
|
|
169
169
|
}
|
|
170
170
|
|
|
@@ -341,7 +341,7 @@ export async function verifyFuse(opts: FuseVerifyOptions): Promise<FuseVerifyRes
|
|
|
341
341
|
"RECONSTRUCTION_MISMATCH",
|
|
342
342
|
{ ...common, slotCommitmentB64, placement: declared?.id ?? null },
|
|
343
343
|
declared !== undefined
|
|
344
|
-
? `rebuilding ${declared.id} from this file and the proof's
|
|
344
|
+
? `rebuilding ${declared.id} from this file and the proof's position record does not reproduce the committed artifact digest`
|
|
345
345
|
: "no registered byte-exact placement rebuilds the committed artifact digest from this file",
|
|
346
346
|
);
|
|
347
347
|
}
|
package/src/index.ts
CHANGED
|
@@ -87,3 +87,18 @@ export { merkleLeafHash, merkleNodeHash, merkleRoot, merklePath, merkleRootFromP
|
|
|
87
87
|
// MADE with the commitment inside it, so look for it). verifyFuse handles both;
|
|
88
88
|
// these were briefly two profiles, which was one idea wearing two names.
|
|
89
89
|
export { ENCODING_BASE64URL, isCarryEncoding, findCommitment, inlineAttribution } from "./fuse.js";
|
|
90
|
+
|
|
91
|
+
/* bitgraph-carrier/1: the file that carries its own proof (2026-09-24). */
|
|
92
|
+
export {
|
|
93
|
+
CARRIER_MAGIC, CARRIER_OVERHEAD, MAX_CARRIER_PAYLOAD, CARRIER_VERSION,
|
|
94
|
+
encodeCarrierBlock, buildCarrier, parseCarrier, completeCarrier,
|
|
95
|
+
anchorIdentity, checkFloorBinding, checkCeilingBinding, anchorMessageBytes,
|
|
96
|
+
decodeRlpTop, verifyWitnessHeader, carrierBounds, innerDigestMatches,
|
|
97
|
+
sameDigest, b64ToBytes, bytesToB64,
|
|
98
|
+
} from "./carrier.js";
|
|
99
|
+
export type {
|
|
100
|
+
CarrierWitness, CarrierProof, CarrierFloor, CarrierCeiling, CarrierPayload,
|
|
101
|
+
CarrierParse, WitnessCheck, CarrierBounds,
|
|
102
|
+
} from "./carrier.js";
|
|
103
|
+
export { verifyCarrier } from "./carrier-verify.js";
|
|
104
|
+
export type { CarrierVerifyResult } from "./carrier-verify.js";
|
package/src/verifier.ts
CHANGED
|
@@ -618,7 +618,7 @@ function checkPolicy(proof: BitGraphProof, policy: VerificationPolicy): string |
|
|
|
618
618
|
// Slot allocation required (BitGraph causal ordering)
|
|
619
619
|
if (policy.requireSlot === true) {
|
|
620
620
|
if (proof.slotAllocation === undefined) {
|
|
621
|
-
return "policy requires slotAllocation (
|
|
621
|
+
return "policy requires slotAllocation (the reserved position's record) but proof has none";
|
|
622
622
|
}
|
|
623
623
|
// Slot verification itself is handled in step 4c (before policy checks),
|
|
624
624
|
// so by this point the slot has already been validated if present.
|
|
@@ -971,7 +971,7 @@ async function verifySlotAllocation(proof: BitGraphProof): Promise<string | null
|
|
|
971
971
|
return "commit.slotHashB64 is not valid base64";
|
|
972
972
|
}
|
|
973
973
|
if (!constantTimeEqual(computedSlotHash, proofSlotHash)) {
|
|
974
|
-
return "commit.slotHashB64 does not match SHA-256 of canonical
|
|
974
|
+
return "commit.slotHashB64 does not match SHA-256 of the canonical position record: position binding broken";
|
|
975
975
|
}
|
|
976
976
|
|
|
977
977
|
// 5. Verify nonce binding: slot nonce === commit nonce
|
|
@@ -987,7 +987,7 @@ async function verifySlotAllocation(proof: BitGraphProof): Promise<string | null
|
|
|
987
987
|
return "commit.slotCounter does not match slotAllocation.counter";
|
|
988
988
|
}
|
|
989
989
|
if (proof.commit.counter === undefined) {
|
|
990
|
-
return "commit.counter must be present for
|
|
990
|
+
return "commit.counter must be present for position ordering verification";
|
|
991
991
|
}
|
|
992
992
|
try {
|
|
993
993
|
const slotCounter = BigInt(slot.counter);
|
|
@@ -996,7 +996,7 @@ async function verifySlotAllocation(proof: BitGraphProof): Promise<string | null
|
|
|
996
996
|
return `slotAllocation.counter (${slot.counter}) must be less than commit.counter (${proof.commit.counter})`;
|
|
997
997
|
}
|
|
998
998
|
} catch {
|
|
999
|
-
return "could not parse
|
|
999
|
+
return "could not parse the reserved or commit counter as an integer";
|
|
1000
1000
|
}
|
|
1001
1001
|
|
|
1002
1002
|
// 7. Verify same enclave: same public key
|