@agledger/verify-core 1.5.0 → 2.0.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.
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Throw `TypeError` on an options object that is not an object, or that
3
+ * carries a key `fn` does not read. A misspelt or renamed option that is
4
+ * dropped without a word switches off the check it named, and the result
5
+ * passes as if it had run.
6
+ */
7
+ export declare function assertKnownOptions(fn: string, options: unknown, known: readonly string[]): void;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Options renamed in a major release, by their old name. A caller upgrading
3
+ * with the old name would otherwise drop the check it asked for and still
4
+ * pass.
5
+ */
6
+ const RENAMED = {
7
+ requireOutOfBandKeys: 'requireSuppliedKeys',
8
+ };
9
+ /**
10
+ * Throw `TypeError` on an options object that is not an object, or that
11
+ * carries a key `fn` does not read. A misspelt or renamed option that is
12
+ * dropped without a word switches off the check it named, and the result
13
+ * passes as if it had run.
14
+ */
15
+ export function assertKnownOptions(fn, options, known) {
16
+ if (options === undefined)
17
+ return;
18
+ if (options === null || typeof options !== 'object' || Array.isArray(options)) {
19
+ throw new TypeError(`${fn}: options must be an object.`);
20
+ }
21
+ for (const key of Object.keys(options)) {
22
+ if (known.includes(key))
23
+ continue;
24
+ const renamed = RENAMED[key];
25
+ throw new TypeError(renamed !== undefined && known.includes(renamed)
26
+ ? `${fn}: the ${key} option is now ${renamed}; pass ${renamed} instead. It was renamed in 2.0.0, and an unknown option is refused rather than ignored.`
27
+ : `${fn}: unknown option ${key}. It reads ${known.join(', ')}.`);
28
+ }
29
+ }
@@ -303,9 +303,10 @@ export declare function decodePredicate(payloadBstr: Uint8Array): Record<string,
303
303
  * build-vault-claim.ts). This is the verifier-side mirror.
304
304
  *
305
305
  * Returns null when the row's payload is structurally insufficient (e.g.
306
- * SCHEMA_REGISTERED missing manifestDigest); the caller surfaces null as a
307
- * binding mismatch rather than throwing, so an attacker who zeros out payload
308
- * can't crash the verifier.
306
+ * SCHEMA_REGISTERED missing manifestDigest) or is not a JSON object at all (a
307
+ * nulled or retyped column); the caller surfaces null as a binding mismatch
308
+ * rather than throwing, so an attacker who zeros out payload can't crash the
309
+ * verifier.
309
310
  */
310
311
  export declare function buildPredicateForRow(recordId: string | null, entryType: string, payload: Record<string, unknown>): Record<string, unknown> | null;
311
312
  /**
@@ -350,7 +351,9 @@ export declare function rfc9162LeafHash(leafData: Uint8Array): Uint8Array;
350
351
  export declare function rfc9162NodeHash(left: Uint8Array, right: Uint8Array): Uint8Array;
351
352
  /**
352
353
  * Verify a RFC 9162 §2.1.3.2 inclusion proof. Mirrors the engine-side
353
- * implementation; reproduced here so the verifier stays engine-free.
354
+ * implementation; reproduced here so the verifier stays engine-free. False
355
+ * when `leafIndex` or `treeSize` is not a safe integer: the walk halves them,
356
+ * and a fraction, NaN or an unsafe integer would walk some other tree.
354
357
  */
355
358
  export declare function verifyRfc9162Inclusion(leafHashBytes: Uint8Array, leafIndex: number, treeSize: number, path: readonly Uint8Array[], expectedRoot: Uint8Array): boolean;
356
359
  export interface ReceiptInclusionProof {
@@ -380,5 +383,24 @@ export declare function extractReceiptInclusionProof(receiptBytes: Uint8Array):
380
383
  */
381
384
  export type ReceiptVerifyOutcome = 'ok' | 'decode-fail' | 'signature-invalid' | 'root-mismatch' | 'unsupported-algorithm';
382
385
  export declare function verifyReceipt(receiptBytes: Uint8Array, signedStatementBytes: Uint8Array, tsPublicKeyBase64: string): ReceiptVerifyOutcome;
383
- export declare function merkleRoot(leaves: readonly string[]): string;
384
- export declare function verifyInclusion(leafHash: string, leafIndex: number, treeSize: number, path: readonly string[], expectedRoot: string): boolean;
386
+ /**
387
+ * A read-log leaf's `leaf_hash`: hex(SHA-256(0x00 || cose_sign1)), the RFC
388
+ * 9162 leaf hash of the leaf's COSE_Sign1 bytes. It is also the next leaf
389
+ * claim's signed `previous_hash`.
390
+ */
391
+ export declare function orgReadLeafHash(coseSign1: Uint8Array): string;
392
+ /**
393
+ * RFC 9162 §2.1.1 Merkle Tree Hash over leaf hashes already computed with
394
+ * {@link orgReadLeafHash}, as hex. The empty tree is SHA-256(""). A leaf hash
395
+ * that is not 64 lowercase hex characters cannot be a stored `leaf_hash`, so
396
+ * the result is null rather than a root over bytes nobody wrote.
397
+ */
398
+ export declare function orgReadMerkleRoot(leafHashesHex: readonly string[]): string | null;
399
+ /**
400
+ * Verify an org_admin_reads inclusion proof (`GET
401
+ * /v1/audit/org-reads/checkpoints/{id}/proof`): the RFC 9162 §2.1.3.2 audit
402
+ * path walk over hex values. A one-leaf tree has an empty path. False on any
403
+ * value that is not 64 lowercase hex characters, and on a `leafIndex` or
404
+ * `treeSize` that is not a safe integer (a JSON number can be 0.5 or 1e300).
405
+ */
406
+ export declare function verifyOrgReadInclusion(leafHashHex: string, leafIndex: number, treeSize: number, pathHex: readonly string[], expectedRootHex: string): boolean;
@@ -3,7 +3,8 @@
3
3
  *
4
4
  * Mirrors the AGLedger engine-side implementations:
5
5
  * - COSE_Sign1 verify (RFC 9052 §4.4): audit-vault/encoders/cose-sign1.ts
6
- * - Merkle: audit-vault/merkle.ts
6
+ * - RFC 9162 §2.1 Merkle tree, for SCITT Receipts and the org_admin_reads
7
+ * log alike: audit-vault/merkle-rfc9162.ts
7
8
  *
8
9
  * Re-implemented here (not imported from any engine/SDK code) so the verifier
9
10
  * has zero engine dependency, the load-bearing property of an offline
@@ -734,11 +735,14 @@ const RECORD_STATE_RESERVED_KEYS = new Set([
734
735
  * build-vault-claim.ts). This is the verifier-side mirror.
735
736
  *
736
737
  * Returns null when the row's payload is structurally insufficient (e.g.
737
- * SCHEMA_REGISTERED missing manifestDigest); the caller surfaces null as a
738
- * binding mismatch rather than throwing, so an attacker who zeros out payload
739
- * can't crash the verifier.
738
+ * SCHEMA_REGISTERED missing manifestDigest) or is not a JSON object at all (a
739
+ * nulled or retyped column); the caller surfaces null as a binding mismatch
740
+ * rather than throwing, so an attacker who zeros out payload can't crash the
741
+ * verifier.
740
742
  */
741
743
  export function buildPredicateForRow(recordId, entryType, payload) {
744
+ if (payload === null || typeof payload !== 'object' || Array.isArray(payload))
745
+ return null;
742
746
  if (SCHEMA_EVENT_TYPES.has(entryType)) {
743
747
  return buildSchemaEventPredicateForRow(entryType, payload);
744
748
  }
@@ -934,13 +938,19 @@ export function rfc9162NodeHash(left, right) {
934
938
  }
935
939
  /**
936
940
  * Verify a RFC 9162 §2.1.3.2 inclusion proof. Mirrors the engine-side
937
- * implementation; reproduced here so the verifier stays engine-free.
941
+ * implementation; reproduced here so the verifier stays engine-free. False
942
+ * when `leafIndex` or `treeSize` is not a safe integer: the walk halves them,
943
+ * and a fraction, NaN or an unsafe integer would walk some other tree.
938
944
  */
939
945
  export function verifyRfc9162Inclusion(leafHashBytes, leafIndex, treeSize, path, expectedRoot) {
946
+ if (!Number.isSafeInteger(leafIndex) || !Number.isSafeInteger(treeSize))
947
+ return false;
940
948
  if (leafIndex < 0 || leafIndex >= treeSize)
941
949
  return false;
942
950
  if (treeSize === 1)
943
951
  return path.length === 0 && bytesEqual(leafHashBytes, expectedRoot);
952
+ // Arithmetic halving rather than `>>>`, which would wrap a size past 2^32.
953
+ const half = (n) => Math.floor(n / 2);
944
954
  let fn = leafIndex;
945
955
  let sn = treeSize - 1;
946
956
  let r = leafHashBytes;
@@ -949,18 +959,18 @@ export function verifyRfc9162Inclusion(leafHashBytes, leafIndex, treeSize, path,
949
959
  if (sn === 0)
950
960
  return false;
951
961
  const p = path[i];
952
- if ((fn & 1) === 1 || fn === sn) {
962
+ if (fn % 2 === 1 || fn === sn) {
953
963
  r = rfc9162NodeHash(p, r);
954
- while ((fn & 1) === 0 && fn !== 0) {
955
- fn >>>= 1;
956
- sn >>>= 1;
964
+ while (fn % 2 === 0 && fn !== 0) {
965
+ fn = half(fn);
966
+ sn = half(sn);
957
967
  }
958
968
  }
959
969
  else {
960
970
  r = rfc9162NodeHash(r, p);
961
971
  }
962
- fn >>>= 1;
963
- sn >>>= 1;
972
+ fn = half(fn);
973
+ sn = half(sn);
964
974
  if (sn === 0) {
965
975
  i += 1;
966
976
  break;
@@ -1041,43 +1051,77 @@ export function verifyReceipt(receiptBytes, signedStatementBytes, tsPublicKeyBas
1041
1051
  const ok = verifyRfc9162Inclusion(leaf, vdp.leafIndex, vdp.treeSize, vdp.auditPath, signedRoot);
1042
1052
  return ok ? 'ok' : 'root-mismatch';
1043
1053
  }
1044
- // --- Merkle tree (org_admin_reads) ---
1054
+ // --- Merkle tree (org_admin_reads), RFC 9162 §2.1 over hex leaf hashes ---
1045
1055
  /**
1046
- * Tree shape mirrors the engine's merkle.ts: balance-by-duplicating-last-leaf,
1047
- * sha256(hex(left) || hex(right)) over hex strings, no RFC 6962 leaf prefix.
1056
+ * The org_admin_reads log is the same RFC 9162 §2.1 SHA-256 tree the Receipts
1057
+ * above use. Its rows store and serve each hash as lowercase hex, so these are
1058
+ * hex forms of the byte functions: every hex string is decoded to the 32 bytes
1059
+ * it denotes before hashing, never hashed as text.
1048
1060
  */
1049
- function hashPair(left, right) {
1050
- return hash('sha256', left + right, 'hex');
1061
+ const HEX_32_BYTES = /^[0-9a-f]{64}$/;
1062
+ function hexToBytes(hex) {
1063
+ return HEX_32_BYTES.test(hex) ? new Uint8Array(Buffer.from(hex, 'hex')) : null;
1051
1064
  }
1052
- export function merkleRoot(leaves) {
1053
- if (leaves.length === 0)
1054
- return hash('sha256', '', 'hex');
1055
- let level = [...leaves];
1056
- while (level.length > 1) {
1057
- const next = [];
1058
- for (let i = 0; i < level.length; i += 2) {
1059
- const left = level[i] ?? '';
1060
- const right = level[i + 1] ?? left;
1061
- next.push(hashPair(left, right));
1062
- }
1063
- level = next;
1065
+ function bytesToHex(bytes) {
1066
+ return Buffer.from(bytes).toString('hex');
1067
+ }
1068
+ /**
1069
+ * A read-log leaf's `leaf_hash`: hex(SHA-256(0x00 || cose_sign1)), the RFC
1070
+ * 9162 leaf hash of the leaf's COSE_Sign1 bytes. It is also the next leaf
1071
+ * claim's signed `previous_hash`.
1072
+ */
1073
+ export function orgReadLeafHash(coseSign1) {
1074
+ return bytesToHex(rfc9162LeafHash(coseSign1));
1075
+ }
1076
+ /** Largest power of two strictly below n (n >= 2), the RFC 9162 §2.1.1 split point. */
1077
+ function splitPoint(n) {
1078
+ let k = 1;
1079
+ while (k * 2 < n)
1080
+ k *= 2;
1081
+ return k;
1082
+ }
1083
+ function rootOfLeafHashes(leafHashes, lo, hi) {
1084
+ if (hi - lo === 1)
1085
+ return leafHashes[lo];
1086
+ const k = splitPoint(hi - lo);
1087
+ return rfc9162NodeHash(rootOfLeafHashes(leafHashes, lo, lo + k), rootOfLeafHashes(leafHashes, lo + k, hi));
1088
+ }
1089
+ /**
1090
+ * RFC 9162 §2.1.1 Merkle Tree Hash over leaf hashes already computed with
1091
+ * {@link orgReadLeafHash}, as hex. The empty tree is SHA-256(""). A leaf hash
1092
+ * that is not 64 lowercase hex characters cannot be a stored `leaf_hash`, so
1093
+ * the result is null rather than a root over bytes nobody wrote.
1094
+ */
1095
+ export function orgReadMerkleRoot(leafHashesHex) {
1096
+ if (leafHashesHex.length === 0)
1097
+ return sha256Hex(new Uint8Array(0));
1098
+ const leaves = [];
1099
+ for (const h of leafHashesHex) {
1100
+ const b = hexToBytes(h);
1101
+ if (b === null)
1102
+ return null;
1103
+ leaves.push(b);
1064
1104
  }
1065
- return level[0] ?? '';
1105
+ return bytesToHex(rootOfLeafHashes(leaves, 0, leaves.length));
1066
1106
  }
1067
- export function verifyInclusion(leafHash, leafIndex, treeSize, path, expectedRoot) {
1068
- if (leafIndex < 0 || leafIndex >= treeSize)
1107
+ /**
1108
+ * Verify an org_admin_reads inclusion proof (`GET
1109
+ * /v1/audit/org-reads/checkpoints/{id}/proof`): the RFC 9162 §2.1.3.2 audit
1110
+ * path walk over hex values. A one-leaf tree has an empty path. False on any
1111
+ * value that is not 64 lowercase hex characters, and on a `leafIndex` or
1112
+ * `treeSize` that is not a safe integer (a JSON number can be 0.5 or 1e300).
1113
+ */
1114
+ export function verifyOrgReadInclusion(leafHashHex, leafIndex, treeSize, pathHex, expectedRootHex) {
1115
+ const leaf = hexToBytes(leafHashHex);
1116
+ const root = hexToBytes(expectedRootHex);
1117
+ if (leaf === null || root === null)
1069
1118
  return false;
1070
- let current = leafHash;
1071
- let index = leafIndex;
1072
- let levelSize = treeSize;
1073
- let pathPos = 0;
1074
- while (levelSize > 1) {
1075
- const sibling = path[pathPos++];
1076
- if (sibling === undefined)
1119
+ const path = [];
1120
+ for (const p of pathHex) {
1121
+ const b = hexToBytes(p);
1122
+ if (b === null)
1077
1123
  return false;
1078
- current = index % 2 === 0 ? hashPair(current, sibling) : hashPair(sibling, current);
1079
- index = Math.floor(index / 2);
1080
- levelSize = Math.ceil(levelSize / 2);
1124
+ path.push(b);
1081
1125
  }
1082
- return current === expectedRoot && pathPos === path.length;
1126
+ return verifyRfc9162Inclusion(leaf, leafIndex, treeSize, path, root);
1083
1127
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@agledger/verify-core",
3
- "version": "1.5.0",
4
- "description": "Shared offline verification core for AGLedger audit chains: COSE_Sign1 (RFC 9052) decode/verify, Ed25519 + ES256, hash-chain walk, Merkle proofs, and the canonical failure taxonomy. One dependency (cborg); no network. The single source of truth the SDK, CLI, MCP server, and @agledger/verify all build on.",
3
+ "version": "2.0.0",
4
+ "description": "Shared offline verification core for AGLedger audit chains: COSE_Sign1 (RFC 9052) decode/verify, Ed25519 + ES256, hash-chain walk, vault key-statement trust walk, RFC 9162 Merkle proofs, and the canonical failure taxonomy. One dependency (cborg); no network. The single source of truth the SDK, CLI, MCP server, and @agledger/verify all build on.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "type": "module",
7
7
  "engines": {