@certcrypt/verifier-toolkit 0.0.0-stage → 1.0.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.

Potentially problematic release.


This version of @certcrypt/verifier-toolkit might be problematic. Click here for more details.

Files changed (221) hide show
  1. package/CHANGELOG.md +72 -0
  2. package/LICENSE +178 -0
  3. package/README.md +563 -2
  4. package/SECURITY.md +77 -0
  5. package/dist/cli/certcrypt-verify.d.ts +2 -0
  6. package/dist/cli/certcrypt-verify.js +174 -0
  7. package/dist/cli/certcrypt-verify.js.map +1 -0
  8. package/dist/core/config/certcrypt-foundation.d.ts +13 -0
  9. package/dist/core/config/certcrypt-foundation.js +123 -0
  10. package/dist/core/config/certcrypt-foundation.js.map +1 -0
  11. package/dist/core/config/default.d.ts +47 -0
  12. package/dist/core/config/default.js +93 -0
  13. package/dist/core/config/default.js.map +1 -0
  14. package/dist/core/config/test-keys.d.ts +40 -0
  15. package/dist/core/config/test-keys.js +41 -0
  16. package/dist/core/config/test-keys.js.map +1 -0
  17. package/dist/core/conformance/attestation.d.ts +4 -0
  18. package/dist/core/conformance/attestation.js +4 -0
  19. package/dist/core/conformance/attestation.js.map +1 -0
  20. package/dist/core/consistency/verify-consistency.d.ts +92 -0
  21. package/dist/core/consistency/verify-consistency.js +353 -0
  22. package/dist/core/consistency/verify-consistency.js.map +1 -0
  23. package/dist/core/crypto/canonical-json.d.ts +14 -0
  24. package/dist/core/crypto/canonical-json.js +13 -0
  25. package/dist/core/crypto/canonical-json.js.map +1 -0
  26. package/dist/core/crypto/canonical-subject.d.ts +76 -0
  27. package/dist/core/crypto/canonical-subject.js +248 -0
  28. package/dist/core/crypto/canonical-subject.js.map +1 -0
  29. package/dist/core/crypto/collection-merkle.d.ts +91 -0
  30. package/dist/core/crypto/collection-merkle.js +243 -0
  31. package/dist/core/crypto/collection-merkle.js.map +1 -0
  32. package/dist/core/crypto/composite.d.ts +73 -0
  33. package/dist/core/crypto/composite.js +206 -0
  34. package/dist/core/crypto/composite.js.map +1 -0
  35. package/dist/core/crypto/digest.d.ts +18 -0
  36. package/dist/core/crypto/digest.js +37 -0
  37. package/dist/core/crypto/digest.js.map +1 -0
  38. package/dist/core/crypto/ecdsa-p256.d.ts +12 -0
  39. package/dist/core/crypto/ecdsa-p256.js +70 -0
  40. package/dist/core/crypto/ecdsa-p256.js.map +1 -0
  41. package/dist/core/crypto/ed25519.d.ts +10 -0
  42. package/dist/core/crypto/ed25519.js +28 -0
  43. package/dist/core/crypto/ed25519.js.map +1 -0
  44. package/dist/core/crypto/holder-keyset.d.ts +23 -0
  45. package/dist/core/crypto/holder-keyset.js +64 -0
  46. package/dist/core/crypto/holder-keyset.js.map +1 -0
  47. package/dist/core/crypto/jcs.d.ts +9 -0
  48. package/dist/core/crypto/jcs.js +59 -0
  49. package/dist/core/crypto/jcs.js.map +1 -0
  50. package/dist/core/crypto/merkle.d.ts +34 -0
  51. package/dist/core/crypto/merkle.js +147 -0
  52. package/dist/core/crypto/merkle.js.map +1 -0
  53. package/dist/core/crypto/ml-dsa.d.ts +25 -0
  54. package/dist/core/crypto/ml-dsa.js +104 -0
  55. package/dist/core/crypto/ml-dsa.js.map +1 -0
  56. package/dist/core/crypto/provider/bytes.d.ts +31 -0
  57. package/dist/core/crypto/provider/bytes.js +106 -0
  58. package/dist/core/crypto/provider/bytes.js.map +1 -0
  59. package/dist/core/crypto/provider/index.d.ts +45 -0
  60. package/dist/core/crypto/provider/index.js +60 -0
  61. package/dist/core/crypto/provider/index.js.map +1 -0
  62. package/dist/core/crypto/provider/node.d.ts +2 -0
  63. package/dist/core/crypto/provider/node.js +96 -0
  64. package/dist/core/crypto/provider/node.js.map +1 -0
  65. package/dist/core/crypto/provider/register-node.d.ts +11 -0
  66. package/dist/core/crypto/provider/register-node.js +16 -0
  67. package/dist/core/crypto/provider/register-node.js.map +1 -0
  68. package/dist/core/crypto/provider/types.d.ts +77 -0
  69. package/dist/core/crypto/provider/types.js +16 -0
  70. package/dist/core/crypto/provider/types.js.map +1 -0
  71. package/dist/core/crypto/provider/web.d.ts +2 -0
  72. package/dist/core/crypto/provider/web.js +122 -0
  73. package/dist/core/crypto/provider/web.js.map +1 -0
  74. package/dist/core/crypto/pseudonym.d.ts +25 -0
  75. package/dist/core/crypto/pseudonym.js +37 -0
  76. package/dist/core/crypto/pseudonym.js.map +1 -0
  77. package/dist/core/crypto/subject-json-preconditions.d.ts +47 -0
  78. package/dist/core/crypto/subject-json-preconditions.js +373 -0
  79. package/dist/core/crypto/subject-json-preconditions.js.map +1 -0
  80. package/dist/core/domain/verify-domain.d.ts +13 -0
  81. package/dist/core/domain/verify-domain.js +39 -0
  82. package/dist/core/domain/verify-domain.js.map +1 -0
  83. package/dist/core/foundation/anti-equivocation.d.ts +87 -0
  84. package/dist/core/foundation/anti-equivocation.js +0 -0
  85. package/dist/core/foundation/anti-equivocation.js.map +1 -0
  86. package/dist/core/foundation/verify-foundation.d.ts +36 -0
  87. package/dist/core/foundation/verify-foundation.js +559 -0
  88. package/dist/core/foundation/verify-foundation.js.map +1 -0
  89. package/dist/core/independent/anti-replay.d.ts +15 -0
  90. package/dist/core/independent/anti-replay.js +27 -0
  91. package/dist/core/independent/anti-replay.js.map +1 -0
  92. package/dist/core/independent/anti-splicing.d.ts +16 -0
  93. package/dist/core/independent/anti-splicing.js +54 -0
  94. package/dist/core/independent/anti-splicing.js.map +1 -0
  95. package/dist/core/independent/capacity-binding.d.ts +26 -0
  96. package/dist/core/independent/capacity-binding.js +57 -0
  97. package/dist/core/independent/capacity-binding.js.map +1 -0
  98. package/dist/core/independent/capacity-evidence.d.ts +38 -0
  99. package/dist/core/independent/capacity-evidence.js +286 -0
  100. package/dist/core/independent/capacity-evidence.js.map +1 -0
  101. package/dist/core/independent/effective-units.d.ts +42 -0
  102. package/dist/core/independent/effective-units.js +147 -0
  103. package/dist/core/independent/effective-units.js.map +1 -0
  104. package/dist/core/independent/issuance-participation.d.ts +13 -0
  105. package/dist/core/independent/issuance-participation.js +24 -0
  106. package/dist/core/independent/issuance-participation.js.map +1 -0
  107. package/dist/core/independent/leaf-binding.d.ts +5 -0
  108. package/dist/core/independent/leaf-binding.js +135 -0
  109. package/dist/core/independent/leaf-binding.js.map +1 -0
  110. package/dist/core/independent/operator-recognition-registry.d.ts +33 -0
  111. package/dist/core/independent/operator-recognition-registry.js +116 -0
  112. package/dist/core/independent/operator-recognition-registry.js.map +1 -0
  113. package/dist/core/independent/proof-structure.d.ts +8 -0
  114. package/dist/core/independent/proof-structure.js +167 -0
  115. package/dist/core/independent/proof-structure.js.map +1 -0
  116. package/dist/core/independent/proof-views.d.ts +26 -0
  117. package/dist/core/independent/proof-views.js +58 -0
  118. package/dist/core/independent/proof-views.js.map +1 -0
  119. package/dist/core/independent/settlement.d.ts +18 -0
  120. package/dist/core/independent/settlement.js +548 -0
  121. package/dist/core/independent/settlement.js.map +1 -0
  122. package/dist/core/independent/verify-independent.d.ts +30 -0
  123. package/dist/core/independent/verify-independent.js +123 -0
  124. package/dist/core/independent/verify-independent.js.map +1 -0
  125. package/dist/core/independent/verify-operator-recognition.d.ts +26 -0
  126. package/dist/core/independent/verify-operator-recognition.js +60 -0
  127. package/dist/core/independent/verify-operator-recognition.js.map +1 -0
  128. package/dist/core/independent/verify-proof-signature.d.ts +20 -0
  129. package/dist/core/independent/verify-proof-signature.js +97 -0
  130. package/dist/core/independent/verify-proof-signature.js.map +1 -0
  131. package/dist/core/input/fs-bridge.d.ts +30 -0
  132. package/dist/core/input/fs-bridge.js +35 -0
  133. package/dist/core/input/fs-bridge.js.map +1 -0
  134. package/dist/core/input/load-artifacts.d.ts +2 -0
  135. package/dist/core/input/load-artifacts.js +80 -0
  136. package/dist/core/input/load-artifacts.js.map +1 -0
  137. package/dist/core/input/load-original-material.d.ts +11 -0
  138. package/dist/core/input/load-original-material.js +126 -0
  139. package/dist/core/input/load-original-material.js.map +1 -0
  140. package/dist/core/input/register-node-fs.d.ts +1 -0
  141. package/dist/core/input/register-node-fs.js +24 -0
  142. package/dist/core/input/register-node-fs.js.map +1 -0
  143. package/dist/core/input/resolve-target.d.ts +16 -0
  144. package/dist/core/input/resolve-target.js +43 -0
  145. package/dist/core/input/resolve-target.js.map +1 -0
  146. package/dist/core/input/strict-json.d.ts +25 -0
  147. package/dist/core/input/strict-json.js +167 -0
  148. package/dist/core/input/strict-json.js.map +1 -0
  149. package/dist/core/input/validate.d.ts +7 -0
  150. package/dist/core/input/validate.js +83 -0
  151. package/dist/core/input/validate.js.map +1 -0
  152. package/dist/core/issued/verify-collection-element.d.ts +66 -0
  153. package/dist/core/issued/verify-collection-element.js +137 -0
  154. package/dist/core/issued/verify-collection-element.js.map +1 -0
  155. package/dist/core/issued/verify-issued.d.ts +15 -0
  156. package/dist/core/issued/verify-issued.js +361 -0
  157. package/dist/core/issued/verify-issued.js.map +1 -0
  158. package/dist/core/issued/verify-original-material.d.ts +35 -0
  159. package/dist/core/issued/verify-original-material.js +418 -0
  160. package/dist/core/issued/verify-original-material.js.map +1 -0
  161. package/dist/core/package/verify-package.d.ts +34 -0
  162. package/dist/core/package/verify-package.js +109 -0
  163. package/dist/core/package/verify-package.js.map +1 -0
  164. package/dist/core/public-api.d.ts +178 -0
  165. package/dist/core/public-api.js +182 -0
  166. package/dist/core/public-api.js.map +1 -0
  167. package/dist/core/result/builder.d.ts +92 -0
  168. package/dist/core/result/builder.js +408 -0
  169. package/dist/core/result/builder.js.map +1 -0
  170. package/dist/core/rules/verify-corpus.d.ts +88 -0
  171. package/dist/core/rules/verify-corpus.js +165 -0
  172. package/dist/core/rules/verify-corpus.js.map +1 -0
  173. package/dist/core/rules/verify-rules.d.ts +14 -0
  174. package/dist/core/rules/verify-rules.js +62 -0
  175. package/dist/core/rules/verify-rules.js.map +1 -0
  176. package/dist/core/testing/validate-against-schema.d.ts +31 -0
  177. package/dist/core/testing/validate-against-schema.js +93 -0
  178. package/dist/core/testing/validate-against-schema.js.map +1 -0
  179. package/dist/core/types/artifacts.d.ts +224 -0
  180. package/dist/core/types/artifacts.js +2 -0
  181. package/dist/core/types/artifacts.js.map +1 -0
  182. package/dist/core/types/capacity-evidence.d.ts +85 -0
  183. package/dist/core/types/capacity-evidence.js +51 -0
  184. package/dist/core/types/capacity-evidence.js.map +1 -0
  185. package/dist/core/types/check.d.ts +61 -0
  186. package/dist/core/types/check.js +2 -0
  187. package/dist/core/types/check.js.map +1 -0
  188. package/dist/core/types/config.d.ts +97 -0
  189. package/dist/core/types/config.js +2 -0
  190. package/dist/core/types/config.js.map +1 -0
  191. package/dist/core/types/foundation.d.ts +121 -0
  192. package/dist/core/types/foundation.js +25 -0
  193. package/dist/core/types/foundation.js.map +1 -0
  194. package/dist/core/types/index.d.ts +9 -0
  195. package/dist/core/types/index.js +2 -0
  196. package/dist/core/types/index.js.map +1 -0
  197. package/dist/core/types/operator-recognition.d.ts +71 -0
  198. package/dist/core/types/operator-recognition.js +24 -0
  199. package/dist/core/types/operator-recognition.js.map +1 -0
  200. package/dist/core/types/proof.d.ts +94 -0
  201. package/dist/core/types/proof.js +2 -0
  202. package/dist/core/types/proof.js.map +1 -0
  203. package/dist/core/types/request.d.ts +59 -0
  204. package/dist/core/types/request.js +2 -0
  205. package/dist/core/types/request.js.map +1 -0
  206. package/dist/core/types/result.d.ts +164 -0
  207. package/dist/core/types/result.js +2 -0
  208. package/dist/core/types/result.js.map +1 -0
  209. package/dist/core/types/settlement-material.d.ts +130 -0
  210. package/dist/core/types/settlement-material.js +2 -0
  211. package/dist/core/types/settlement-material.js.map +1 -0
  212. package/dist/core/verify-node.d.ts +14 -0
  213. package/dist/core/verify-node.js +17 -0
  214. package/dist/core/verify-node.js.map +1 -0
  215. package/dist/core/verify-web.d.ts +1 -0
  216. package/dist/core/verify-web.js +25 -0
  217. package/dist/core/verify-web.js.map +1 -0
  218. package/dist/core/verify.d.ts +18 -0
  219. package/dist/core/verify.js +183 -0
  220. package/dist/core/verify.js.map +1 -0
  221. package/package.json +71 -4
@@ -0,0 +1,248 @@
1
+ /**
2
+ * `canonical_bytes(subject)` dispatch by subject profile (CCCP/1;
3
+ * verifier-toolkit/1). The verifier recomputes these bytes from the
4
+ * original material, hashes them to `subject_local_digest_hex`, and rebuilds the
5
+ * certificate commitment to confirm the material matches.
6
+ *
7
+ * Supported profiles: `subject.binary/1`, `subject.text/1`, `subject.email/1`,
8
+ * `subject.collection/1`, `subject.ai-decision/1`, and `subject.context-snapshot/1`.
9
+ * An unsupported profile
10
+ * MUST fail closed as UNSUPPORTED — the caller maps a thrown
11
+ * `UnsupportedProfileError` to that outcome rather than falling back to another
12
+ * profile.
13
+ *
14
+ * The subject canonicalization context is opaque bytes at the CCCP layer
15
+ * (CCCP/1); CCCP/1 UTF-8/BOM rules govern CCCP-defined contexts, not
16
+ * subject input. `subject.text/1` and `subject.email/1` therefore operate on the
17
+ * RAW bytes and never decode/re-encode through a platform string (which could
18
+ * alter bytes via U+FFFD substitution, normalization, or NUL truncation).
19
+ */
20
+ import { jcsCanonicalizeBytes } from "./jcs.js";
21
+ import { strictJsonParse } from "../input/strict-json.js";
22
+ import { assertContextSnapshotPrecondition, assertCollectionPrecondition, assertAiDecisionPrecondition, SubjectPreconditionError, } from "./subject-json-preconditions.js";
23
+ export const SUBJECT_BINARY = "subject.binary/1";
24
+ export const SUBJECT_TEXT = "subject.text/1";
25
+ export const SUBJECT_EMAIL = "subject.email/1";
26
+ export const SUBJECT_COLLECTION = "subject.collection/1";
27
+ export const SUBJECT_AI_DECISION = "subject.ai-decision/1";
28
+ export const SUBJECT_CONTEXT_SNAPSHOT = "subject.context-snapshot/1";
29
+ const AI_DECISION_MAX_BYTES = 1024 * 1024;
30
+ /** Profiles the toolkit supports for `canonical_bytes` recomputation. */
31
+ export const SUPPORTED_SUBJECT_PROFILES = [
32
+ SUBJECT_BINARY,
33
+ SUBJECT_TEXT,
34
+ SUBJECT_EMAIL,
35
+ SUBJECT_COLLECTION,
36
+ SUBJECT_AI_DECISION,
37
+ SUBJECT_CONTEXT_SNAPSHOT,
38
+ ];
39
+ /**
40
+ * Profiles whose canonical_bytes ARE the input bytes, unchanged.
41
+ *
42
+ * This is the precondition for hashing material INCREMENTALLY: a digest assembled from
43
+ * chunks equals a digest of the whole buffer only when nothing is rewritten in between.
44
+ * Every other profile fails it — `text/1` normalizes line endings and appends a terminator,
45
+ * `email/1` unfolds headers across the whole message, and the JSON-object profiles
46
+ * re-serialize the PARSED value under JCS, which is not even a byte transformation.
47
+ *
48
+ * It is a LIST, not a string comparison against `SUBJECT_BINARY`, because the property
49
+ * belongs to the profile family and its versions: a future `subject.binary/2` that still
50
+ * defines canonical_bytes as the exact input bytes belongs here, and one that does not must
51
+ * NOT be added — the whole point of naming this separately from `SUPPORTED_SUBJECT_PROFILES`
52
+ * is that supporting a profile and being streamable are different questions, decided here,
53
+ * next to the switch that implements them.
54
+ */
55
+ export const OPAQUE_BYTE_PROFILES = [SUBJECT_BINARY];
56
+ /**
57
+ * Whether `profile`'s canonical_bytes are its input bytes, so material under it may be
58
+ * hashed in chunks instead of held whole in memory.
59
+ */
60
+ export function isOpaqueByteProfile(profile) {
61
+ return OPAQUE_BYTE_PROFILES.includes(profile);
62
+ }
63
+ /** Thrown when a subject declares a profile the toolkit does not support. */
64
+ export class UnsupportedProfileError extends Error {
65
+ profile;
66
+ constructor(profile) {
67
+ super(`unsupported subject profile: ${profile}`);
68
+ this.name = "UnsupportedProfileError";
69
+ this.profile = profile;
70
+ }
71
+ }
72
+ const CR = 0x0d;
73
+ const LF = 0x0a;
74
+ const SP = 0x20;
75
+ const HTAB = 0x09;
76
+ function isWsp(b) {
77
+ return b === SP || b === HTAB;
78
+ }
79
+ /**
80
+ * subject.text/1 line-ending normalization: a single left-to-right pass
81
+ * over the ORIGINAL bytes, CRLF longest-match before lone CR / lone LF.
82
+ */
83
+ function normalizeLineEndings(input) {
84
+ const out = [];
85
+ let i = 0;
86
+ while (i < input.length) {
87
+ if (input[i] === CR && input[i + 1] === LF) {
88
+ out.push(CR, LF);
89
+ i += 2;
90
+ }
91
+ else if (input[i] === CR) {
92
+ out.push(CR, LF);
93
+ i += 1;
94
+ }
95
+ else if (input[i] === LF) {
96
+ out.push(CR, LF);
97
+ i += 1;
98
+ }
99
+ else {
100
+ out.push(input[i]);
101
+ i += 1;
102
+ }
103
+ }
104
+ return out;
105
+ }
106
+ /**
107
+ * subject.text/1: normalize line endings to CRLF, then collapse the
108
+ * trailing CRLF run to exactly one CRLF; all other bytes preserved.
109
+ */
110
+ function canonicalizeText(material) {
111
+ const norm = normalizeLineEndings(material);
112
+ // strip the maximal trailing run of CRLF, then append exactly one.
113
+ let end = norm.length;
114
+ while (end >= 2 && norm[end - 2] === CR && norm[end - 1] === LF)
115
+ end -= 2;
116
+ const out = norm.slice(0, end);
117
+ out.push(CR, LF);
118
+ return Uint8Array.from(out);
119
+ }
120
+ /**
121
+ * subject.email/1: global CRLF normalization, split at the first
122
+ * CRLF CRLF, unfold headers only as in RFC 5322 §2.2.3, body
123
+ * trailing-terminator normalization, assemble headers || CRLF CRLF ||
124
+ * body. Operates on raw bytes; no MIME decoding.
125
+ */
126
+ function canonicalizeEmail(material) {
127
+ const m = Uint8Array.from(normalizeLineEndings(material));
128
+ // Split at the FIRST CRLF CRLF (the four-byte delimiter may end at the
129
+ // final byte, so the last candidate index is m.length - 4).
130
+ let split = -1;
131
+ for (let i = 0; i + 4 <= m.length; i++) {
132
+ if (m[i] === CR && m[i + 1] === LF && m[i + 2] === CR && m[i + 3] === LF) {
133
+ split = i;
134
+ break;
135
+ }
136
+ }
137
+ let headers;
138
+ let body;
139
+ if (split === -1) {
140
+ headers = m;
141
+ body = new Uint8Array(0);
142
+ }
143
+ else {
144
+ headers = m.slice(0, split);
145
+ body = m.slice(split + 4);
146
+ }
147
+ // Unfold headers only (RFC 5322 §2.2.3): each CRLF immediately followed
148
+ // by a WSP byte is deleted (CRLF removed, the WSP preserved byte-for-byte,
149
+ // including SP-vs-HTAB identity and run length).
150
+ const hu = [];
151
+ let i = 0;
152
+ while (i < headers.length) {
153
+ if (headers[i] === CR && headers[i + 1] === LF && isWsp(headers[i + 2])) {
154
+ i += 2; // drop the CRLF; the WSP is emitted on the next iteration
155
+ }
156
+ else {
157
+ hu.push(headers[i]);
158
+ i += 1;
159
+ }
160
+ }
161
+ // Body trailing-terminator normalization: a non-empty body ends in
162
+ // exactly one CRLF (trailing CRLF run collapsed, or one appended if absent);
163
+ // an empty body stays empty.
164
+ let bodyOut;
165
+ if (body.length === 0) {
166
+ bodyOut = [];
167
+ }
168
+ else {
169
+ let end = body.length;
170
+ while (end >= 2 && body[end - 2] === CR && body[end - 1] === LF)
171
+ end -= 2;
172
+ bodyOut = [...body.slice(0, end), CR, LF];
173
+ }
174
+ // Assembly: headers_unfolded || CRLF CRLF || body (delimiter always present).
175
+ return Uint8Array.from([...hu, CR, LF, CR, LF, ...bodyOut]);
176
+ }
177
+ /**
178
+ * Compute `canonical_bytes(subject)` for a subject under its declared profile.
179
+ *
180
+ * - `subject.binary/1`: the exact input bytes, opaque, no transformation
181
+ * (subject.binary/1). `material` MUST already be the raw bytes.
182
+ * - `subject.text/1`: line-ending normalization + single trailing CRLF, byte
183
+ * preserved otherwise (subject.text/1). Operates on raw bytes.
184
+ * - `subject.email/1`: global CRLF normalization, header/body split, RFC 5322
185
+ * header unfolding (WSP preserved), body trailing-terminator normalization,
186
+ * deterministic assembly (subject.email/1). Raw bytes.
187
+ * - `subject.collection/1`: UTF-8 bytes of the RFC 8785 JCS serialization of the
188
+ * parsed JSON object (subject.collection/1). `material` is the JSON source
189
+ * bytes; it is parsed and re-canonicalized (canonicalize the value, not the
190
+ * source bytes).
191
+ * - `subject.ai-decision/1`: the same JCS rule over the parsed decision record,
192
+ * after its field-set and string-only preconditions (subject.ai-decision/1).
193
+ * - `subject.context-snapshot/1`: the same JCS rule over the parsed snapshot,
194
+ * which must carry no JSON number at any depth (subject.context-snapshot/1).
195
+ *
196
+ * Throws `UnsupportedProfileError` for any other declared profile.
197
+ */
198
+ export function canonicalBytesForSubject(profile, material) {
199
+ switch (profile) {
200
+ case SUBJECT_BINARY:
201
+ // Opaque byte array: canonical_bytes == exact input bytes.
202
+ return material;
203
+ case SUBJECT_TEXT:
204
+ return canonicalizeText(material);
205
+ case SUBJECT_EMAIL:
206
+ return canonicalizeEmail(material);
207
+ case SUBJECT_COLLECTION:
208
+ case SUBJECT_AI_DECISION:
209
+ case SUBJECT_CONTEXT_SNAPSHOT: {
210
+ if (profile === SUBJECT_AI_DECISION && material.byteLength > AI_DECISION_MAX_BYTES) {
211
+ throw new SubjectPreconditionError(profile, `source material exceeds ${AI_DECISION_MAX_BYTES} bytes`);
212
+ }
213
+ if (profile === SUBJECT_AI_DECISION &&
214
+ material.byteLength >= 3 &&
215
+ material[0] === 0xef && material[1] === 0xbb && material[2] === 0xbf) {
216
+ throw new SubjectPreconditionError(profile, "source material must not begin with a UTF-8 BOM");
217
+ }
218
+ // JSON-object subjects: parse the value, enforce the profile
219
+ // precondition (field set + string-only discipline; fail-closed for any
220
+ // other object), then JCS-canonicalize the VALUE (not the source bytes).
221
+ // subject.ai-decision/1 and subject.collection/1 both define
222
+ // canonical_bytes as RFC 8785 JCS of the parsed object; the string-only
223
+ // discipline the precondition enforces keeps JCS free of number
224
+ // serialization (subject.collection/1, subject.ai-decision/1).
225
+ const text = new TextDecoder("utf-8", { fatal: true }).decode(material);
226
+ // Strict parse: a member name repeated at any depth makes the material
227
+ // non-conformant (the profile is undefined for it), so it fails closed
228
+ // instead of being canonicalized from whichever duplicate a parser keeps.
229
+ const value = strictJsonParse(text);
230
+ if (profile === SUBJECT_COLLECTION)
231
+ assertCollectionPrecondition(value);
232
+ else if (profile === SUBJECT_AI_DECISION)
233
+ assertAiDecisionPrecondition(value);
234
+ // context-snapshot fixes no member set — the snapshot shape is CCVP/1's,
235
+ // not this profile's. All it fixes here is the absence of JSON numbers.
236
+ else
237
+ assertContextSnapshotPrecondition(value);
238
+ const canonical = jcsCanonicalizeBytes(value);
239
+ if (profile === SUBJECT_AI_DECISION && canonical.byteLength > AI_DECISION_MAX_BYTES) {
240
+ throw new SubjectPreconditionError(profile, `canonical material exceeds ${AI_DECISION_MAX_BYTES} bytes`);
241
+ }
242
+ return canonical;
243
+ }
244
+ default:
245
+ throw new UnsupportedProfileError(profile);
246
+ }
247
+ }
248
+ //# sourceMappingURL=canonical-subject.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"canonical-subject.js","sourceRoot":"","sources":["../../../core/crypto/canonical-subject.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EACL,iCAAiC,EACjC,4BAA4B,EAC5B,4BAA4B,EAC5B,wBAAwB,GACzB,MAAM,iCAAiC,CAAC;AAEzC,MAAM,CAAC,MAAM,cAAc,GAAG,kBAAkB,CAAC;AACjD,MAAM,CAAC,MAAM,YAAY,GAAG,gBAAgB,CAAC;AAC7C,MAAM,CAAC,MAAM,aAAa,GAAG,iBAAiB,CAAC;AAC/C,MAAM,CAAC,MAAM,kBAAkB,GAAG,sBAAsB,CAAC;AACzD,MAAM,CAAC,MAAM,mBAAmB,GAAG,uBAAuB,CAAC;AAC3D,MAAM,CAAC,MAAM,wBAAwB,GAAG,4BAA4B,CAAC;AACrE,MAAM,qBAAqB,GAAG,IAAI,GAAG,IAAI,CAAC;AAE1C,yEAAyE;AACzE,MAAM,CAAC,MAAM,0BAA0B,GAAG;IACxC,cAAc;IACd,YAAY;IACZ,aAAa;IACb,kBAAkB;IAClB,mBAAmB;IACnB,wBAAwB;CACzB,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAsB,CAAC,cAAc,CAAC,CAAC;AAExE;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,OAAe;IACjD,OAAO,oBAAoB,CAAC,QAAQ,CAAC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IAAI,wBAAwB,CAAC,OAAO,EAAE,iDAAiD,CAAC,CAAC;YACjG,CAAC;YACD,6DAA6D;YAC7D,wEAAwE;YACxE,yEAAyE;YACzE,6DAA6D;YAC7D,wEAAwE;YACxE,gEAAgE;YAChE,+DAA+D;YAC/D,MAAM,IAAI,GAAG,IAAI,WAAW,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YACxE,uEAAuE;YACvE,uEAAuE;YACvE,0EAA0E;YAC1E,MAAM,KAAK,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC;YACpC,IAAI,OAAO,KAAK,kBAAkB;gBAAE,4BAA4B,CAAC,KAAK,CAAC,CAAC;iBACnE,IAAI,OAAO,KAAK,mBAAmB;gBAAE,4BAA4B,CAAC,KAAK,CAAC,CAAC;YAC9E,yEAAyE;YACzE,wEAAwE;;gBACnE,iCAAiC,CAAC,KAAK,CAAC,CAAC;YAC9C,MAAM,SAAS,GAAG,oBAAoB,CAAC,KAAK,CAAC,CAAC;YAC9C,IAAI,OAAO,KAAK,mBAAmB,IAAI,SAAS,CAAC,UAAU,GAAG,qBAAqB,EAAE,CAAC;gBACpF,MAAM,IAAI,wBAAwB,CAChC,OAAO,EACP,8BAA8B,qBAAqB,QAAQ,CAC5D,CAAC;YACJ,CAAC;YACD,OAAO,SAAS,CAAC;QACnB,CAAC;QACD;YACE,MAAM,IAAI,uBAAuB,CAAC,OAAO,CAAC,CAAC;IAC/C,CAAC;AACH,CAAC"}
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Merkle commitment scheme `collection.merkle-sha3-256/1` — the scheme that
3
+ * hides the leaves of a `subject.collection/1` subject behind a single root.
4
+ *
5
+ * This is DISTINCT from the settlement Merkle in `merkle.ts`
6
+ * (`settlement.aggregated.xrpl/1`, which prefixes 0x00 / 0x01). Do NOT substitute
7
+ * one for the other — they have different leaf-preimage and node-preimage models
8
+ * and would produce different roots. (verifier-toolkit/1.)
9
+ *
10
+ * Spec: collection.merkle-sha3-256/1.
11
+ * leaf_preimage = utf8("CERTCRYPT:collection.merkle-sha3-256/1:leaf")
12
+ * || u64be(len(collection_salt_bytes)) || collection_salt_bytes
13
+ * || u64be(len(leaf_profile_bytes)) || leaf_profile_bytes
14
+ * || u64be(len(leaf_canonical_bytes)) || leaf_canonical_bytes
15
+ * leaf_hash = SHA3-256(leaf_preimage)
16
+ * node_preimage = utf8("CERTCRYPT:collection.merkle-sha3-256/1:node")
17
+ * || left_hash || right_hash (each 32 raw bytes)
18
+ * node_hash = SHA3-256(node_preimage)
19
+ * root: 1 leaf -> that leaf_hash; else pair left-to-right, promote a lone
20
+ * right-edge hash unchanged, until one remains. Empty tree is invalid.
21
+ */
22
+ export declare const COLLECTION_MERKLE_SCHEME = "collection.merkle-sha3-256/1";
23
+ /**
24
+ * leaf_hash for one leaf, given the collection salt, the declared `leaf_profile`
25
+ * and the leaf's `canonical_bytes` (already produced under that profile).
26
+ * Returns lowercase hex.
27
+ *
28
+ * The salt is bound into every leaf so that the same leaf set under a different
29
+ * salt yields a different root (§9). Under `subject.collection/1` §8 the salt is
30
+ * the certificate anchor's `nonce_hex` as ASCII bytes; this function takes it as
31
+ * opaque bytes and MUST NOT interpret it.
32
+ *
33
+ * An empty salt is rejected: `/1` requires one, and silently accepting none would
34
+ * reproduce the unsalted construction whose leaf hashes are stable global
35
+ * identifiers of content.
36
+ */
37
+ export declare function collectionLeafHash(collectionSalt: Uint8Array, leafProfile: string, leafCanonicalBytes: Uint8Array): string;
38
+ /** node_hash for two child hashes (lowercase hex in, lowercase hex out). */
39
+ export declare function collectionNodeHash(leftHex: string, rightHex: string): string;
40
+ /**
41
+ * Compute the collection root from an ordered list of leaf hashes (lowercase
42
+ * hex). Rule: single leaf -> its hash; else pair left-to-right and
43
+ * promote a lone right-edge hash unchanged until one remains. Throws on empty
44
+ * (empty-tree prohibition).
45
+ */
46
+ export declare function collectionMerkleRoot(leafHashes: string[]): string;
47
+ /**
48
+ * The logical inclusion-proof object of `collection.merkle-sha3-256/1` — the
49
+ * exact three members, no more. `leaf_index` and `proof_version` are
50
+ * strings because that is how the scheme serializes them; `sibling_hashes` are lowercase
51
+ * hex digests in bottom-up order. verifyCollectionInclusion enforces all of it,
52
+ * including the absence of any additional member, so parsed JSON can be passed
53
+ * as is.
54
+ */
55
+ export interface CollectionInclusionProof {
56
+ proof_version: string;
57
+ leaf_index: string;
58
+ sibling_hashes: string[];
59
+ }
60
+ /** Outcome of a `collection.merkle-sha3-256/1` element inclusion check. `valid`
61
+ * is true only when the leaf provably belongs to the declared root; otherwise
62
+ * `reason` names the failure condition that fired. Fail-closed: any malformed
63
+ * input yields `valid: false`, never a throw for adversary-controlled proof data. */
64
+ export interface CollectionInclusionResult {
65
+ valid: boolean;
66
+ reason: string | null;
67
+ }
68
+ /**
69
+ * Verify that ONE declared leaf belongs to a `subject.collection/1` root under
70
+ * `collection.merkle-sha3-256/1`, given only that leaf, its inclusion path, and
71
+ * the declared root — WITHOUT supplying the rest of the collection. This is the
72
+ * element-level inclusion capacity of the scheme, distinct from and NOT built on
73
+ * `verify-original-material.ts`, which needs the full ordered leaf set and rebuilds
74
+ * the whole root.
75
+ *
76
+ * Pure and fail-closed: adversary-controlled `proof`/`declaredRoot` never throw;
77
+ * any malformed or non-matching input returns `{valid:false, reason}`.
78
+ *
79
+ * The fold reproduces the specified procedure exactly, so a positive result reconstructs the SAME root
80
+ * `collectionMerkleRoot` would compute over the full ordered leaf set — verified by
81
+ * the shared conformance vectors (fixtures/primitives/collection-merkle).
82
+ *
83
+ * @param collectionSalt the collection salt (subject.collection/1 §8: the
84
+ * anchor `nonce_hex` as ASCII bytes)
85
+ * @param leafProfile declared `leaf_profile` of the collection subject
86
+ * @param leafCanonicalBytes canonical_bytes of the leaf being proven, under that profile
87
+ * @param proof the inclusion-proof object for this leaf
88
+ * @param declaredCardinality the collection's `declared_cardinality` (canonical uint string)
89
+ * @param declaredRoot the collection subject's declared `root` (64-char lowercase hex)
90
+ */
91
+ export declare function verifyCollectionInclusion(collectionSalt: Uint8Array, leafProfile: string, leafCanonicalBytes: Uint8Array, proof: CollectionInclusionProof, declaredCardinality: string, declaredRoot: string): CollectionInclusionResult;
@@ -0,0 +1,243 @@
1
+ /**
2
+ * Merkle commitment scheme `collection.merkle-sha3-256/1` — the scheme that
3
+ * hides the leaves of a `subject.collection/1` subject behind a single root.
4
+ *
5
+ * This is DISTINCT from the settlement Merkle in `merkle.ts`
6
+ * (`settlement.aggregated.xrpl/1`, which prefixes 0x00 / 0x01). Do NOT substitute
7
+ * one for the other — they have different leaf-preimage and node-preimage models
8
+ * and would produce different roots. (verifier-toolkit/1.)
9
+ *
10
+ * Spec: collection.merkle-sha3-256/1.
11
+ * leaf_preimage = utf8("CERTCRYPT:collection.merkle-sha3-256/1:leaf")
12
+ * || u64be(len(collection_salt_bytes)) || collection_salt_bytes
13
+ * || u64be(len(leaf_profile_bytes)) || leaf_profile_bytes
14
+ * || u64be(len(leaf_canonical_bytes)) || leaf_canonical_bytes
15
+ * leaf_hash = SHA3-256(leaf_preimage)
16
+ * node_preimage = utf8("CERTCRYPT:collection.merkle-sha3-256/1:node")
17
+ * || left_hash || right_hash (each 32 raw bytes)
18
+ * node_hash = SHA3-256(node_preimage)
19
+ * root: 1 leaf -> that leaf_hash; else pair left-to-right, promote a lone
20
+ * right-edge hash unchanged, until one remains. Empty tree is invalid.
21
+ */
22
+ import { digestHexBytes } from "./digest.js";
23
+ // The VALUE declared in a collection's `commitment_scheme` field is the full
24
+ // scheme identifier `collection.merkle-sha3-256/1` — identical to the scheme's
25
+ // published identifier and document path, consistent with every other field
26
+ // identifier in the stack (`subject.binary/1`, `settlement.aggregated.xrpl/1`,
27
+ // `independence.proof/1`, …), so the value is resolvable to its spec. The same
28
+ // string is the domain-separation tag namespace below.
29
+ export const COLLECTION_MERKLE_SCHEME = "collection.merkle-sha3-256/1";
30
+ const LEAF_TAG = new TextEncoder().encode("CERTCRYPT:collection.merkle-sha3-256/1:leaf");
31
+ const NODE_TAG = new TextEncoder().encode("CERTCRYPT:collection.merkle-sha3-256/1:node");
32
+ /** 8-byte big-endian length prefix (u64be). */
33
+ function u64be(n) {
34
+ if (!Number.isInteger(n) || n < 0) {
35
+ throw new Error(`u64be requires a non-negative integer, got ${n}`);
36
+ }
37
+ const out = new Uint8Array(8);
38
+ // JS numbers are safe integers up to 2^53; write the low 53 bits big-endian.
39
+ let v = n;
40
+ for (let i = 7; i >= 0; i--) {
41
+ out[i] = v & 0xff;
42
+ v = Math.floor(v / 256);
43
+ }
44
+ return out;
45
+ }
46
+ function concat(...parts) {
47
+ const total = parts.reduce((s, p) => s + p.length, 0);
48
+ const out = new Uint8Array(total);
49
+ let off = 0;
50
+ for (const p of parts) {
51
+ out.set(p, off);
52
+ off += p.length;
53
+ }
54
+ return out;
55
+ }
56
+ function hexToBytes(hex) {
57
+ if (hex.length % 2 !== 0)
58
+ throw new Error("hex length must be even");
59
+ if (!/^[0-9a-fA-F]*$/.test(hex)) {
60
+ throw new Error("invalid hex: non-hex characters present");
61
+ }
62
+ const bytes = new Uint8Array(hex.length / 2);
63
+ for (let i = 0; i < hex.length; i += 2) {
64
+ bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);
65
+ }
66
+ return bytes;
67
+ }
68
+ /**
69
+ * leaf_hash for one leaf, given the collection salt, the declared `leaf_profile`
70
+ * and the leaf's `canonical_bytes` (already produced under that profile).
71
+ * Returns lowercase hex.
72
+ *
73
+ * The salt is bound into every leaf so that the same leaf set under a different
74
+ * salt yields a different root (§9). Under `subject.collection/1` §8 the salt is
75
+ * the certificate anchor's `nonce_hex` as ASCII bytes; this function takes it as
76
+ * opaque bytes and MUST NOT interpret it.
77
+ *
78
+ * An empty salt is rejected: `/1` requires one, and silently accepting none would
79
+ * reproduce the unsalted construction whose leaf hashes are stable global
80
+ * identifiers of content.
81
+ */
82
+ export function collectionLeafHash(collectionSalt, leafProfile, leafCanonicalBytes) {
83
+ if (collectionSalt.length === 0) {
84
+ throw new Error("collection.merkle-sha3-256/1: a non-empty collection salt is required (§9)");
85
+ }
86
+ const leafProfileBytes = new TextEncoder().encode(leafProfile);
87
+ const preimage = concat(LEAF_TAG, u64be(collectionSalt.length), collectionSalt, u64be(leafProfileBytes.length), leafProfileBytes, u64be(leafCanonicalBytes.length), leafCanonicalBytes);
88
+ return digestHexBytes(preimage);
89
+ }
90
+ /** node_hash for two child hashes (lowercase hex in, lowercase hex out). */
91
+ export function collectionNodeHash(leftHex, rightHex) {
92
+ const preimage = concat(NODE_TAG, hexToBytes(leftHex), hexToBytes(rightHex));
93
+ return digestHexBytes(preimage);
94
+ }
95
+ /**
96
+ * Compute the collection root from an ordered list of leaf hashes (lowercase
97
+ * hex). Rule: single leaf -> its hash; else pair left-to-right and
98
+ * promote a lone right-edge hash unchanged until one remains. Throws on empty
99
+ * (empty-tree prohibition).
100
+ */
101
+ export function collectionMerkleRoot(leafHashes) {
102
+ if (leafHashes.length === 0) {
103
+ throw new Error("collection.merkle-sha3-256/1: empty tree is invalid");
104
+ }
105
+ let level = leafHashes;
106
+ while (level.length > 1) {
107
+ const next = [];
108
+ for (let i = 0; i < level.length; i += 2) {
109
+ if (i + 1 < level.length) {
110
+ next.push(collectionNodeHash(level[i], level[i + 1]));
111
+ }
112
+ else {
113
+ next.push(level[i]); // promote lone right-edge hash unchanged
114
+ }
115
+ }
116
+ level = next;
117
+ }
118
+ return level[0];
119
+ }
120
+ const HEX64_RE = /^[0-9a-f]{64}$/;
121
+ const CANONICAL_UINT_RE = /^(0|[1-9][0-9]*)$/;
122
+ const ok = { valid: true, reason: null };
123
+ function reject(reason) {
124
+ return { valid: false, reason };
125
+ }
126
+ /**
127
+ * Verify that ONE declared leaf belongs to a `subject.collection/1` root under
128
+ * `collection.merkle-sha3-256/1`, given only that leaf, its inclusion path, and
129
+ * the declared root — WITHOUT supplying the rest of the collection. This is the
130
+ * element-level inclusion capacity of the scheme, distinct from and NOT built on
131
+ * `verify-original-material.ts`, which needs the full ordered leaf set and rebuilds
132
+ * the whole root.
133
+ *
134
+ * Pure and fail-closed: adversary-controlled `proof`/`declaredRoot` never throw;
135
+ * any malformed or non-matching input returns `{valid:false, reason}`.
136
+ *
137
+ * The fold reproduces the specified procedure exactly, so a positive result reconstructs the SAME root
138
+ * `collectionMerkleRoot` would compute over the full ordered leaf set — verified by
139
+ * the shared conformance vectors (fixtures/primitives/collection-merkle).
140
+ *
141
+ * @param collectionSalt the collection salt (subject.collection/1 §8: the
142
+ * anchor `nonce_hex` as ASCII bytes)
143
+ * @param leafProfile declared `leaf_profile` of the collection subject
144
+ * @param leafCanonicalBytes canonical_bytes of the leaf being proven, under that profile
145
+ * @param proof the inclusion-proof object for this leaf
146
+ * @param declaredCardinality the collection's `declared_cardinality` (canonical uint string)
147
+ * @param declaredRoot the collection subject's declared `root` (64-char lowercase hex)
148
+ */
149
+ export function verifyCollectionInclusion(collectionSalt, leafProfile, leafCanonicalBytes, proof, declaredCardinality, declaredRoot) {
150
+ // A salt is required (§9, §16). Rejected rather than thrown: this entry point is
151
+ // fail-closed for adversary-controlled material.
152
+ if (!(collectionSalt instanceof Uint8Array) || collectionSalt.length === 0) {
153
+ return reject("collection salt is missing or empty");
154
+ }
155
+ // declared root must be a 64-char lowercase hex string.
156
+ if (typeof declaredRoot !== "string" || !HEX64_RE.test(declaredRoot)) {
157
+ return reject("declared root is not a 64-char lowercase hex string");
158
+ }
159
+ // declared_cardinality must parse as a positive integer.
160
+ if (typeof declaredCardinality !== "string" || !CANONICAL_UINT_RE.test(declaredCardinality)) {
161
+ return reject("declared_cardinality is not a canonical unsigned integer string");
162
+ }
163
+ const n = Number(declaredCardinality);
164
+ if (n < 1 || !Number.isSafeInteger(n)) {
165
+ return reject("declared_cardinality out of range (empty tree prohibited)");
166
+ }
167
+ // exactly the three members of the proof object; an additional one fails.
168
+ if (proof === null || typeof proof !== "object" || Array.isArray(proof)) {
169
+ return reject("inclusion proof is not an object");
170
+ }
171
+ const members = Object.keys(proof).sort();
172
+ if (members.length !== 3 ||
173
+ members[0] !== "leaf_index" ||
174
+ members[1] !== "proof_version" ||
175
+ members[2] !== "sibling_hashes") {
176
+ return reject("inclusion proof members are not exactly proof_version, leaf_index, sibling_hashes");
177
+ }
178
+ // proof_version must be exactly the scheme identifier.
179
+ if (proof.proof_version !== COLLECTION_MERKLE_SCHEME) {
180
+ return reject(`proof_version differs (expected ${COLLECTION_MERKLE_SCHEME})`);
181
+ }
182
+ // leaf_index must be a canonical unsigned base-10 integer string.
183
+ if (typeof proof.leaf_index !== "string" || !CANONICAL_UINT_RE.test(proof.leaf_index)) {
184
+ return reject("leaf_index missing or non-canonical");
185
+ }
186
+ const i = Number(proof.leaf_index);
187
+ if (!Number.isSafeInteger(i)) {
188
+ return reject("leaf_index out of representable range");
189
+ }
190
+ // reject if i >= N.
191
+ if (i >= n) {
192
+ return reject(`leaf_index ${i} out of bounds for declared_cardinality ${n}`);
193
+ }
194
+ // sibling_hashes must be an array of 64-char lowercase hex strings.
195
+ if (!Array.isArray(proof.sibling_hashes)) {
196
+ return reject("sibling_hashes is not an array");
197
+ }
198
+ for (const s of proof.sibling_hashes) {
199
+ if (typeof s !== "string" || !HEX64_RE.test(s)) {
200
+ return reject("sibling_hashes contains a malformed entry (not 64-char lowercase hex)");
201
+ }
202
+ }
203
+ // compute the leaf hash of the leaf being proven.
204
+ let currentHash;
205
+ try {
206
+ currentHash = collectionLeafHash(collectionSalt, leafProfile, leafCanonicalBytes);
207
+ }
208
+ catch (e) {
209
+ return reject(`leaf canonicalization error: ${e.message}`);
210
+ }
211
+ // fold upward. A lone rightmost node on an odd-width level is promoted
212
+ // unchanged and consumes NO sibling (promotion rule) — so the proof carries exactly
213
+ // one sibling per level at which the current node HAS a sibling, and no more.
214
+ let currentIndex = i;
215
+ let currentWidth = n;
216
+ let cursor = 0;
217
+ while (currentWidth > 1) {
218
+ const isLoneRightmost = currentWidth % 2 === 1 && currentIndex === currentWidth - 1;
219
+ if (!isLoneRightmost) {
220
+ if (cursor >= proof.sibling_hashes.length) {
221
+ return reject("too few sibling hashes for the tree shape");
222
+ }
223
+ const sibling = proof.sibling_hashes[cursor++];
224
+ currentHash =
225
+ currentIndex % 2 === 0
226
+ ? collectionNodeHash(currentHash, sibling) // even -> node(current, sibling)
227
+ : collectionNodeHash(sibling, currentHash); // odd -> node(sibling, current)
228
+ }
229
+ // else: promoted unchanged, no sibling consumed.
230
+ currentIndex = Math.floor(currentIndex / 2);
231
+ currentWidth = Math.ceil(currentWidth / 2);
232
+ }
233
+ // every supplied sibling MUST have been consumed — no extras.
234
+ if (cursor !== proof.sibling_hashes.length) {
235
+ return reject("too many sibling hashes for the tree shape");
236
+ }
237
+ // accept only if the recomputed root equals the declared root.
238
+ if (currentHash !== declaredRoot) {
239
+ return reject("recomputed root differs from declared root");
240
+ }
241
+ return ok;
242
+ }
243
+ //# sourceMappingURL=collection-merkle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collection-merkle.js","sourceRoot":"","sources":["../../../core/crypto/collection-merkle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C,6EAA6E;AAC7E,+EAA+E;AAC/E,4EAA4E;AAC5E,+EAA+E;AAC/E,+EAA+E;AAC/E,uDAAuD;AACvD,MAAM,CAAC,MAAM,wBAAwB,GAAG,8BAA8B,CAAC;AAEvE,MAAM,QAAQ,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CACvC,6CAA6C,CAC9C,CAAC;AACF,MAAM,QAAQ,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CACvC,6CAA6C,CAC9C,CAAC;AAEF,+CAA+C;AAC/C,SAAS,KAAK,CAAC,CAAS;IACtB,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;QAClC,MAAM,IAAI,KAAK,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