@mikeargento/bitgraph-verify 1.10.0 → 1.12.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/src/verifier.ts CHANGED
@@ -1,4 +1,4 @@
1
- // Copyright (c) 2024-2026 Mike Argento. Licensed under the MIT License. See LICENSE.
1
+ // Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
2
2
 
3
3
  /**
4
4
  * bitgraph-core Verifier
@@ -133,9 +133,11 @@ export async function verify(opts: {
133
133
  proof: BitGraphProof;
134
134
  bytes: Uint8Array;
135
135
  trustAnchors?: VerificationPolicy;
136
+ /** Single-successor history for this run. Omitted: the module-level one. */
137
+ context?: VerificationContext;
136
138
  }): Promise<VerifyResult> {
137
- const { proof, bytes, trustAnchors } = opts;
138
- const reason = await runChecks(proof, bytes, trustAnchors);
139
+ const { proof, bytes, trustAnchors, context } = opts;
140
+ const reason = await runChecks(proof, bytes, trustAnchors, context ?? defaultContext);
139
141
  return reason === null ? { valid: true } : fail(reason);
140
142
  }
141
143
 
@@ -179,9 +181,11 @@ export async function verify(opts: {
179
181
  export async function verifyProofIntegrity(opts: {
180
182
  proof: BitGraphProof;
181
183
  trustAnchors?: VerificationPolicy;
184
+ /** Single-successor history for this run. Omitted: the module-level one. */
185
+ context?: VerificationContext;
182
186
  }): Promise<ProofIntegrityResult> {
183
- const { proof, trustAnchors } = opts;
184
- const reason = await runChecks(proof, undefined, trustAnchors);
187
+ const { proof, trustAnchors, context } = opts;
188
+ const reason = await runChecks(proof, undefined, trustAnchors, context ?? defaultContext);
185
189
  return reason === null
186
190
  ? { valid: true, artifactBinding: "not-checked" }
187
191
  : { valid: false, artifactBinding: "not-checked", reason };
@@ -205,7 +209,8 @@ export async function verifyProofIntegrity(opts: {
205
209
  async function runChecks(
206
210
  proof: BitGraphProof,
207
211
  bytes: Uint8Array | undefined,
208
- trustAnchors: VerificationPolicy | undefined
212
+ trustAnchors: VerificationPolicy | undefined,
213
+ context: VerificationContext
209
214
  ): Promise<string | null> {
210
215
  // ------------------------------------------------------------------
211
216
  // 1. Structural validation
@@ -330,11 +335,13 @@ async function runChecks(
330
335
  // ------------------------------------------------------------------
331
336
  // 4d. Epoch link verification (cross-epoch lineage)
332
337
  // ------------------------------------------------------------------
338
+ let pendingConsumption: { key: string; toEpochId: string } | null = null;
333
339
  if (proof.commit.epochLink !== undefined) {
334
- const epochLinkError = verifyEpochLink(proof);
335
- if (epochLinkError !== null) {
336
- return epochLinkError;
340
+ const { error, pending } = checkEpochLink(proof, context);
341
+ if (error !== null) {
342
+ return error;
337
343
  }
344
+ pendingConsumption = pending;
338
345
  }
339
346
 
340
347
  // ------------------------------------------------------------------
@@ -347,6 +354,14 @@ async function runChecks(
347
354
  }
348
355
  }
349
356
 
357
+ // ------------------------------------------------------------------
358
+ // 6. Accepted. Only now does this proof consume its predecessor: a
359
+ // rejected proof must leave the accepted history untouched.
360
+ // ------------------------------------------------------------------
361
+ if (pendingConsumption !== null) {
362
+ context.consumedPredecessors.set(pendingConsumption.key, pendingConsumption.toEpochId);
363
+ }
364
+
350
365
  return null;
351
366
  }
352
367
 
@@ -1010,12 +1025,27 @@ async function verifySlotAllocation(proof: BitGraphProof): Promise<string | null
1010
1025
  * If a DIFFERENT successor epoch attempts to consume the same predecessor,
1011
1026
  * the verifier detects a fork and rejects.
1012
1027
  *
1013
- * This is an in-memory registry scoped to the verifier's lifecycle.
1014
- * For persistent fork detection across processes, callers should maintain
1015
- * an external store and pass consumed predecessors via the
1016
- * `consumedPredecessors` option.
1028
+ * This is an in-memory registry. Pass a context of your own to keep one
1029
+ * verification run's history out of another's; callers that pass none share
1030
+ * the module-level default, which resetEpochLinkState() clears.
1031
+ *
1032
+ * ⚠️ Only a proof that passed EVERY check, policy included, records its
1033
+ * consumption. A rejected proof that could still write here would make a
1034
+ * later honest proof fail as a fork, which is verification depending on what
1035
+ * the process happened to check before. Found by an outside audit of the
1036
+ * repository, 2026-09-20.
1017
1037
  */
1018
- const consumedPredecessors = new Map<string, string>(); // successorKey → toEpochId
1038
+ export interface VerificationContext {
1039
+ /** successorKey → the epochId that consumed it. */
1040
+ readonly consumedPredecessors: Map<string, string>;
1041
+ }
1042
+
1043
+ /** A fresh single-successor history, isolated from every other context. */
1044
+ export function createVerificationContext(): VerificationContext {
1045
+ return { consumedPredecessors: new Map<string, string>() };
1046
+ }
1047
+
1048
+ const defaultContext: VerificationContext = createVerificationContext();
1019
1049
 
1020
1050
  /**
1021
1051
  * Compute the unique key for a consumed predecessor.
@@ -1036,12 +1066,16 @@ function computeSuccessorKey(link: NonNullable<BitGraphProof["commit"]["epochLin
1036
1066
  * 3. Successor binding: toPublicKeyB64 === proof's signer key
1037
1067
  * 4. Single-successor: no other epoch has consumed this predecessor
1038
1068
  *
1069
+ * Records nothing. On success it returns the consumption the caller should
1070
+ * record once every later check has passed too (`pending`), or null when this
1071
+ * predecessor was already consumed by the same successor.
1072
+ *
1039
1073
  * Note: Validating the predecessor proof's signature requires the predecessor
1040
1074
  * proof itself, which is not embedded in the current proof. The enclave
1041
1075
  * performs this validation at init time. The verifier checks structural
1042
1076
  * consistency and fork detection.
1043
1077
  */
1044
- function verifyEpochLink(proof: BitGraphProof): string | null {
1078
+ function validateEpochLinkShape(proof: BitGraphProof): string | null {
1045
1079
  const link = proof.commit.epochLink!;
1046
1080
 
1047
1081
  // 1. Structural validation
@@ -1092,26 +1126,43 @@ function verifyEpochLink(proof: BitGraphProof): string | null {
1092
1126
  return "epochLink.prevPublicKeyB64 equals toPublicKeyB64 — epochs must have different keys";
1093
1127
  }
1094
1128
 
1129
+ return null;
1130
+ }
1131
+
1132
+ /**
1133
+ * The single-successor half: reads the context, never writes it. The caller
1134
+ * records `pending` only once every later check has passed as well.
1135
+ */
1136
+ function checkEpochLink(
1137
+ proof: BitGraphProof,
1138
+ context: VerificationContext,
1139
+ ): { error: string | null; pending: { key: string; toEpochId: string } | null } {
1140
+ const shapeError = validateEpochLinkShape(proof);
1141
+ if (shapeError !== null) {
1142
+ return { error: shapeError, pending: null };
1143
+ }
1144
+ const link = proof.commit.epochLink!;
1145
+
1095
1146
  // 6. Single-successor invariant: detect forks
1096
1147
  const successorKey = computeSuccessorKey(link);
1097
- const existingSuccessor = consumedPredecessors.get(successorKey);
1148
+ const existingSuccessor = context.consumedPredecessors.get(successorKey);
1098
1149
 
1099
1150
  if (existingSuccessor !== undefined) {
1100
1151
  if (existingSuccessor !== link.toEpochId) {
1101
- return (
1102
- `FORK DETECTED: predecessor (epoch=${link.prevEpochId.slice(0, 12)}..., counter=${link.prevCounter}) ` +
1103
- `already consumed by epoch ${existingSuccessor.slice(0, 12)}..., ` +
1104
- `but this proof claims consumption by epoch ${link.toEpochId.slice(0, 12)}... — ` +
1105
- `single-successor invariant violated`
1106
- );
1152
+ return {
1153
+ error:
1154
+ `FORK DETECTED: predecessor (epoch=${link.prevEpochId.slice(0, 12)}..., counter=${link.prevCounter}) ` +
1155
+ `already consumed by epoch ${existingSuccessor.slice(0, 12)}..., ` +
1156
+ `but this proof claims consumption by epoch ${link.toEpochId.slice(0, 12)}... — ` +
1157
+ `single-successor invariant violated`,
1158
+ pending: null,
1159
+ };
1107
1160
  }
1108
1161
  // Same successor — idempotent (re-verifying same proof)
1109
- } else {
1110
- // Record this consumption
1111
- consumedPredecessors.set(successorKey, link.toEpochId);
1162
+ return { error: null, pending: null };
1112
1163
  }
1113
1164
 
1114
- return null;
1165
+ return { error: null, pending: { key: successorKey, toEpochId: link.toEpochId } };
1115
1166
  }
1116
1167
 
1117
1168
  /**
@@ -1119,7 +1170,7 @@ function verifyEpochLink(proof: BitGraphProof): string | null {
1119
1170
  * Use this when starting a fresh verification context.
1120
1171
  */
1121
1172
  export function resetEpochLinkState(): void {
1122
- consumedPredecessors.clear();
1173
+ defaultContext.consumedPredecessors.clear();
1123
1174
  }
1124
1175
 
1125
1176
  // ---------------------------------------------------------------------------