@certcrypt/sdk 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/sdk might be problematic. Click here for more details.

Files changed (71) hide show
  1. package/CHANGELOG.md +74 -0
  2. package/LICENSE +178 -0
  3. package/README.md +557 -2
  4. package/SECURITY.md +55 -0
  5. package/dist/src/artifacts/base-certificate.d.ts +336 -0
  6. package/dist/src/artifacts/base-certificate.js +373 -0
  7. package/dist/src/artifacts/base-certificate.js.map +1 -0
  8. package/dist/src/artifacts/capacity-evidence.d.ts +195 -0
  9. package/dist/src/artifacts/capacity-evidence.js +202 -0
  10. package/dist/src/artifacts/capacity-evidence.js.map +1 -0
  11. package/dist/src/artifacts/commitment-inclusion.d.ts +23 -0
  12. package/dist/src/artifacts/commitment-inclusion.js +91 -0
  13. package/dist/src/artifacts/commitment-inclusion.js.map +1 -0
  14. package/dist/src/artifacts/holder-keyset.d.ts +67 -0
  15. package/dist/src/artifacts/holder-keyset.js +149 -0
  16. package/dist/src/artifacts/holder-keyset.js.map +1 -0
  17. package/dist/src/artifacts/rfc3339.d.ts +9 -0
  18. package/dist/src/artifacts/rfc3339.js +39 -0
  19. package/dist/src/artifacts/rfc3339.js.map +1 -0
  20. package/dist/src/client.d.ts +552 -0
  21. package/dist/src/client.js +1347 -0
  22. package/dist/src/client.js.map +1 -0
  23. package/dist/src/crypto/ai-decision.d.ts +73 -0
  24. package/dist/src/crypto/ai-decision.js +94 -0
  25. package/dist/src/crypto/ai-decision.js.map +1 -0
  26. package/dist/src/crypto/canonical-subject.d.ts +33 -0
  27. package/dist/src/crypto/canonical-subject.js +240 -0
  28. package/dist/src/crypto/canonical-subject.js.map +1 -0
  29. package/dist/src/crypto/collection-merkle.d.ts +58 -0
  30. package/dist/src/crypto/collection-merkle.js +164 -0
  31. package/dist/src/crypto/collection-merkle.js.map +1 -0
  32. package/dist/src/crypto/composite.d.ts +53 -0
  33. package/dist/src/crypto/composite.js +69 -0
  34. package/dist/src/crypto/composite.js.map +1 -0
  35. package/dist/src/crypto/digest.d.ts +3 -0
  36. package/dist/src/crypto/digest.js +31 -0
  37. package/dist/src/crypto/digest.js.map +1 -0
  38. package/dist/src/crypto/ecdsa-p256.d.ts +79 -0
  39. package/dist/src/crypto/ecdsa-p256.js +277 -0
  40. package/dist/src/crypto/ecdsa-p256.js.map +1 -0
  41. package/dist/src/crypto/jcs.d.ts +13 -0
  42. package/dist/src/crypto/jcs.js +65 -0
  43. package/dist/src/crypto/jcs.js.map +1 -0
  44. package/dist/src/crypto/merkle.d.ts +18 -0
  45. package/dist/src/crypto/merkle.js +150 -0
  46. package/dist/src/crypto/merkle.js.map +1 -0
  47. package/dist/src/crypto/ml-dsa.d.ts +28 -0
  48. package/dist/src/crypto/ml-dsa.js +60 -0
  49. package/dist/src/crypto/ml-dsa.js.map +1 -0
  50. package/dist/src/crypto/pseudonym.d.ts +8 -0
  51. package/dist/src/crypto/pseudonym.js +50 -0
  52. package/dist/src/crypto/pseudonym.js.map +1 -0
  53. package/dist/src/crypto/strict-json.d.ts +25 -0
  54. package/dist/src/crypto/strict-json.js +180 -0
  55. package/dist/src/crypto/strict-json.js.map +1 -0
  56. package/dist/src/crypto/subject-json-preconditions.d.ts +46 -0
  57. package/dist/src/crypto/subject-json-preconditions.js +381 -0
  58. package/dist/src/crypto/subject-json-preconditions.js.map +1 -0
  59. package/dist/src/index.d.ts +18 -0
  60. package/dist/src/index.js +80 -0
  61. package/dist/src/index.js.map +1 -0
  62. package/dist/src/issuer.d.ts +668 -0
  63. package/dist/src/issuer.js +1066 -0
  64. package/dist/src/issuer.js.map +1 -0
  65. package/dist/src/retry.d.ts +67 -0
  66. package/dist/src/retry.js +78 -0
  67. package/dist/src/retry.js.map +1 -0
  68. package/dist/src/types.d.ts +297 -0
  69. package/dist/src/types.js +15 -0
  70. package/dist/src/types.js.map +1 -0
  71. package/package.json +62 -4
@@ -0,0 +1,277 @@
1
+ // Copyright © 2025-2026 CERTCRYPT, Inc., its affiliates or licensors. All rights reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ /**
15
+ * ECDSA P-256 (secp256r1) signing — the classical component of the CERTCRYPT
16
+ * Composite Signature Profile (`certcrypt-composite-p256-mldsa65-v1`), issuer
17
+ * side. Signer counterpart to
18
+ * `certcrypt-verifier-toolkit-js/core/crypto/ecdsa-p256.ts`.
19
+ *
20
+ * Wire contract (must interoperate with every other implementation):
21
+ * - curve P-256 / secp256r1, hash **SHA3-256** over the message (NOT SHA-2);
22
+ * - signature encoding `raw-rs-64`: r || s, 64 bytes (32+32), big-endian, NOT DER;
23
+ * - low-s NORMATIVE: the producer MUST emit s ≤ n/2, so s is normalized here
24
+ * (the verifier rejects high-s outright).
25
+ * - public key on the wire: SEC1 uncompressed (65B, 0x04 prefix), base64.
26
+ *
27
+ * Signing is RFC 6979 deterministic, with H = SHA3-256 for both the prehash and
28
+ * the nonce's HMAC (conformance family `ecdsa-p256-deterministic`): one key over
29
+ * one message yields exactly one signature. node's `crypto.sign` draws a random
30
+ * nonce, so the signature is computed with `@noble/curves` instead; node:crypto
31
+ * still carries keys and the local self-check verify.
32
+ */
33
+ import { ecdsa } from "@noble/curves/abstract/weierstrass.js";
34
+ import { p256 } from "@noble/curves/nist.js";
35
+ import { sha3_256 } from "@noble/hashes/sha3.js";
36
+ import { createECDH, createHmac, createPrivateKey, createPublicKey, generateKeyPairSync, verify as cryptoVerify, } from "node:crypto";
37
+ // secp256r1 group order n, and n/2 (the low-s ceiling, inclusive).
38
+ const P256_N = 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n;
39
+ const P256_HALF_N = P256_N >> 1n;
40
+ /** Generate a fresh ECDSA P-256 keypair (dev/test; production keys via KMS). */
41
+ export function generateEcdsaP256KeyPair() {
42
+ const { privateKey, publicKey } = generateKeyPairSync("ec", {
43
+ namedCurve: "prime256v1",
44
+ });
45
+ return {
46
+ privateKey,
47
+ publicKey,
48
+ publicKeyB64: publicKeySec1UncompressedB64(publicKey),
49
+ };
50
+ }
51
+ /** Export a P-256 public KeyObject as SEC1 uncompressed (65B) base64. */
52
+ export function publicKeySec1UncompressedB64(publicKey) {
53
+ // JWK gives raw x/y coordinates; assemble 0x04 || X(32) || Y(32).
54
+ const jwk = publicKey.export({ format: "jwk" });
55
+ if (!jwk.x || !jwk.y) {
56
+ throw new Error("ECDSA-P256 public key JWK missing x/y coordinates");
57
+ }
58
+ const x = Buffer.from(jwk.x, "base64url");
59
+ const y = Buffer.from(jwk.y, "base64url");
60
+ if (x.length !== 32 || y.length !== 32) {
61
+ throw new Error(`ECDSA-P256 coordinates must be 32 bytes each (got x=${x.length}, y=${y.length})`);
62
+ }
63
+ return Buffer.concat([Buffer.from([0x04]), x, y]).toString("base64");
64
+ }
65
+ /** Load a P-256 private key from PKCS#8 DER/PEM (for KMS-provisioned keys). */
66
+ export function privateKeyFromPkcs8(pkcs8) {
67
+ return createPrivateKey(pkcs8);
68
+ }
69
+ /**
70
+ * Build a P-256 private KeyObject DETERMINISTICALLY from a 32-byte scalar `d`
71
+ * (1 ≤ d < n). Used to derive a stable issuer key from a master seed — the same
72
+ * seed always yields the same signing identity, with no stored key material.
73
+ *
74
+ * The public point (x, y) is derived from `d` via ECDH (`crypto.createECDH`,
75
+ * which does the scalar × G multiplication), then a JWK EC private key
76
+ * {crv, d, x, y} is imported: a pure-`node:crypto` way to go from a raw scalar to
77
+ * a signing KeyObject.
78
+ *
79
+ * Callers MUST pass a scalar already reduced into [1, n): see
80
+ * `scalarFromSeedMaterial`, which folds arbitrary seed bytes into range.
81
+ */
82
+ export function privateKeyFromScalar(scalar) {
83
+ if (scalar.length !== 32) {
84
+ throw new Error(`P-256 scalar must be 32 bytes (got ${scalar.length})`);
85
+ }
86
+ const d = beBytesToBigInt(scalar);
87
+ if (d <= 0n || d >= P256_N) {
88
+ throw new Error("P-256 scalar out of range [1, n) — reduce it first");
89
+ }
90
+ // Derive the public point from d: createECDH lets us set the private key and
91
+ // read back the SEC1-uncompressed public key (0x04 || X || Y).
92
+ const ecdh = createECDH("prime256v1");
93
+ ecdh.setPrivateKey(Buffer.from(scalar));
94
+ const pub = ecdh.getPublicKey(); // 65B SEC1 uncompressed
95
+ const x = pub.subarray(1, 33);
96
+ const y = pub.subarray(33, 65);
97
+ const toB64Url = (b) => Buffer.from(b).toString("base64url");
98
+ return createPrivateKey({
99
+ key: {
100
+ kty: "EC",
101
+ crv: "P-256",
102
+ d: toB64Url(scalar),
103
+ x: toB64Url(x),
104
+ y: toB64Url(y),
105
+ },
106
+ format: "jwk",
107
+ });
108
+ }
109
+ /**
110
+ * Domain-separation info string for deriving the P-256 half of a composite key
111
+ * from a 32-byte master seed.
112
+ *
113
+ * Load-bearing, and pinned by the conformance contract
114
+ * (`fixtures/primitives/key-derivation`): whatever derives the published key and
115
+ * every SDK MUST derive byte-identically, or the public key published in the manifest
116
+ * is not the one this seed produces.
117
+ */
118
+ export const P256_DERIVE_INFO = "certcrypt/composite/p256/v1";
119
+ /**
120
+ * HKDF (RFC 5869) with SHA3-256, no salt.
121
+ *
122
+ * Written out rather than taken from node's `hkdf`, which does not offer SHA3 in
123
+ * every supported runtime. This derivation is part of the byte contract, so a few
124
+ * lines a reader can check against RFC 5869 beat a dependency whose hash choice
125
+ * is an argument someone can change.
126
+ */
127
+ function hkdfSha3_256(ikm, info, length) {
128
+ const hashLen = 32;
129
+ const prk = createHmac("sha3-256", Buffer.alloc(hashLen)).update(ikm).digest();
130
+ const infoBytes = Buffer.from(info, "utf-8");
131
+ const out = [];
132
+ let block = Buffer.alloc(0);
133
+ let counter = 1;
134
+ let produced = 0;
135
+ while (produced < length) {
136
+ block = createHmac("sha3-256", prk)
137
+ .update(Buffer.concat([block, infoBytes, Buffer.from([counter])]))
138
+ .digest();
139
+ out.push(block);
140
+ produced += block.length;
141
+ counter += 1;
142
+ }
143
+ return new Uint8Array(Buffer.concat(out).subarray(0, length));
144
+ }
145
+ /**
146
+ * Deterministic P-256 signing key from a 32-byte master seed.
147
+ *
148
+ * HKDF-SHA3-256 with explicit domain separation, expanded to 48 bytes and reduced
149
+ * mod n. The oversampling past the 32-byte field removes the modular bias of
150
+ * reducing exactly 256 bits; a result of 0 maps to 1, so the scalar is always in
151
+ * [1, n-1].
152
+ *
153
+ * The SAME seed also derives the ML-DSA-65 half (FIPS 204 KeyGen, seed used
154
+ * verbatim). The distinct HKDF `info` keeps the two derivations separated, so one
155
+ * custodied seed materializes the whole composite identity without either
156
+ * component leaking into the other.
157
+ *
158
+ * Note the difference from {@link scalarFromSeedMaterial}, which folds arbitrary
159
+ * bytes with `% (n-1) + 1`. That is a different reduction and yields a different
160
+ * key: it is for callers holding their own material, not for the composite
161
+ * identity. Only this function reproduces the contract's vectors.
162
+ */
163
+ export function signingKeyFromSeed(seed) {
164
+ if (seed.length !== 32) {
165
+ throw new Error(`master seed must be 32 bytes (got ${seed.length})`);
166
+ }
167
+ const okm = hkdfSha3_256(seed, P256_DERIVE_INFO, 48);
168
+ let scalar = beBytesToBigInt(okm) % P256_N;
169
+ if (scalar === 0n)
170
+ scalar = 1n;
171
+ return privateKeyFromScalar(bigIntTo32Be(scalar));
172
+ }
173
+ /**
174
+ * Fold arbitrary seed-derived bytes into a valid P-256 scalar in [1, n). Reduces
175
+ * mod (n-1) and adds 1 so the result is never 0 and never ≥ n. Deterministic.
176
+ */
177
+ export function scalarFromSeedMaterial(material) {
178
+ const v = beBytesToBigInt(material) % (P256_N - 1n);
179
+ return bigIntTo32Be(v + 1n);
180
+ }
181
+ /** Derive the SEC1-uncompressed wire public key from a P-256 private key. */
182
+ export function publicKeyB64FromPrivate(privateKey) {
183
+ return publicKeySec1UncompressedB64(createPublicKey(privateKey));
184
+ }
185
+ function beBytesToBigInt(bytes) {
186
+ let acc = 0n;
187
+ for (const b of bytes)
188
+ acc = (acc << 8n) | BigInt(b);
189
+ return acc;
190
+ }
191
+ function bigIntTo32Be(v) {
192
+ const out = new Uint8Array(32);
193
+ for (let i = 31; i >= 0; i--) {
194
+ out[i] = Number(v & 0xffn);
195
+ v >>= 8n;
196
+ }
197
+ return out;
198
+ }
199
+ /**
200
+ * P-256 ECDSA with SHA3-256 as the curve hash: it prehashes the message and keys
201
+ * the RFC 6979 HMAC-DRBG, so the nonce is HMAC-SHA3-256 as RFC 6979 §3.2
202
+ * prescribes for a SHA3-256 message hash. No extra entropy: deterministic.
203
+ */
204
+ const P256_SHA3 = ecdsa(p256.Point, sha3_256);
205
+ /** The private scalar of a P-256 KeyObject, as 32 big-endian bytes. */
206
+ function privateScalar(privateKey) {
207
+ const jwk = privateKey.export({ format: "jwk" });
208
+ if (jwk.kty !== "EC" || jwk.crv !== "P-256" || !jwk.d) {
209
+ throw new Error("ECDSA-P256 signing needs a P-256 private key");
210
+ }
211
+ const d = Buffer.from(jwk.d, "base64url");
212
+ if (d.length !== 32) {
213
+ throw new Error(`P-256 private scalar must be 32 bytes (got ${d.length})`);
214
+ }
215
+ return new Uint8Array(d);
216
+ }
217
+ /**
218
+ * Sign `message` with a P-256 private key, returning a `raw-rs-64` signature
219
+ * (r||s, 64 bytes) normalized to low-s (s ≤ n/2).
220
+ *
221
+ * The message is hashed once with SHA3-256 and the nonce is RFC 6979
222
+ * deterministic with HMAC-SHA3-256, so the same key and message always give the
223
+ * same 64 bytes (conformance family `ecdsa-p256-deterministic`).
224
+ */
225
+ export function signEcdsaP256Raw(privateKey, message) {
226
+ const sig = P256_SHA3.sign(message, privateScalar(privateKey), {
227
+ prehash: true,
228
+ lowS: true,
229
+ extraEntropy: false,
230
+ format: "compact",
231
+ });
232
+ if (sig.length !== 64) {
233
+ throw new Error(`expected raw-rs-64 signature (64 bytes), got ${sig.length}`);
234
+ }
235
+ const r = beBytesToBigInt(sig.subarray(0, 32));
236
+ let s = beBytesToBigInt(sig.subarray(32, 64));
237
+ // Low-s normalization: if s > n/2, replace with n - s (the equivalent low-s
238
+ // signature). The verifier rejects high-s, so this is mandatory on produce.
239
+ // `lowS: true` already does it; kept explicit because the rule is normative.
240
+ if (s > P256_HALF_N) {
241
+ s = P256_N - s;
242
+ }
243
+ const out = new Uint8Array(64);
244
+ out.set(bigIntTo32Be(r), 0);
245
+ out.set(bigIntTo32Be(s), 32);
246
+ return out;
247
+ }
248
+ /**
249
+ * Local self-check verify (tests / minter self-verification). The production
250
+ * trust verdict is the composite check in the verifier toolkit; this only mirrors
251
+ * its ECDSA primitive (SHA3-256, raw-rs-64, low-s enforced).
252
+ */
253
+ export function verifyEcdsaP256Raw(publicKeyB64, message, sig) {
254
+ if (sig.length !== 64)
255
+ return false;
256
+ const s = beBytesToBigInt(sig.subarray(32, 64));
257
+ if (s > P256_HALF_N || s === 0n)
258
+ return false;
259
+ const r = beBytesToBigInt(sig.subarray(0, 32));
260
+ if (r === 0n || r >= P256_N)
261
+ return false;
262
+ const pub = Buffer.from(publicKeyB64, "base64");
263
+ // SEC1 uncompressed → SPKI via node import.
264
+ const keyObject = createPublicKey({
265
+ key: Buffer.concat([sec1SpkiPrefix(pub.length), pub]),
266
+ format: "der",
267
+ type: "spki",
268
+ });
269
+ return cryptoVerify("sha3-256", Buffer.from(message), { key: keyObject, dsaEncoding: "ieee-p1363" }, Buffer.from(sig));
270
+ }
271
+ const P256_ALG_ID = Buffer.from("301306072a8648ce3d020106082a8648ce3d030107", "hex");
272
+ function sec1SpkiPrefix(pointLen) {
273
+ const bitString = Buffer.from([0x03, pointLen + 1, 0x00]);
274
+ const body = Buffer.concat([P256_ALG_ID, bitString]);
275
+ return Buffer.concat([Buffer.from([0x30, body.length + pointLen]), body]);
276
+ }
277
+ //# sourceMappingURL=ecdsa-p256.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ecdsa-p256.js","sourceRoot":"","sources":["../../../src/crypto/ecdsa-p256.ts"],"names":[],"mappings":"AAAA,2FAA2F;AAC3F,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,iDAAiD;AACjD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,EAAE,KAAK,EAAE,MAAM,uCAAuC,CAAC;AAC9D,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EACL,UAAU,EACV,UAAU,EACV,gBAAgB,EAChB,eAAe,EACf,mBAAmB,EACnB,MAAM,IAAI,YAAY,GAEvB,MAAM,aAAa,CAAC;AAErB,mEAAmE;AACnE,MAAM,MAAM,GACV,mEAAmE,CAAC;AACtE,MAAM,WAAW,GAAG,MAAM,IAAI,EAAE,CAAC;AAUjC,gFAAgF;AAChF,MAAM,UAAU,wBAAwB;IACtC,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,GAAG,mBAAmB,CAAC,IAAI,EAAE;QAC1D,UAAU,EAAE,YAAY;KACzB,CAAC,CAAC;IACH,OAAO;QACL,UAAU;QACV,SAAS;QACT,YAAY,EAAE,4BAA4B,CAAC,SAAS,CAAC;KACtD,CAAC;AACJ,CAAC;AAED,yEAAyE;AACzE,MAAM,UAAU,4BAA4B,CAAC,SAAoB;IAC/D,kEAAkE;IAClE,MAAM,GAAG,GAAG,SAAS,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;IAChD,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CA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,MAAM,CAAC,MAAM,CAAC,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC,CAAC;IACrD,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,GAAG,QAAQ,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;AAC5E,CAAC"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * RFC 8785 JSON Canonicalization Scheme (JCS).
3
+ *
4
+ * Per CCCP/1: the canonical form for payload signing uses RFC 8785.
5
+ * This means lexicographic key ordering, no extra whitespace,
6
+ * and deterministic number serialization.
7
+ *
8
+ * Copied verbatim from certcrypt-verifier-toolkit-js/core/crypto/jcs.ts so the
9
+ * issuer-side canonical bytes match the verifier's byte-for-byte. The toolkit
10
+ * does not export this module, so it is vendored here rather than imported.
11
+ */
12
+ export declare function jcsCanonicalizeBytes(obj: unknown): Uint8Array;
13
+ export declare function jcsCanonicalizeString(obj: unknown): string;
@@ -0,0 +1,65 @@
1
+ // Copyright © 2025-2026 CERTCRYPT, Inc., its affiliates or licensors. All rights reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ /**
15
+ * RFC 8785 JSON Canonicalization Scheme (JCS).
16
+ *
17
+ * Per CCCP/1: the canonical form for payload signing uses RFC 8785.
18
+ * This means lexicographic key ordering, no extra whitespace,
19
+ * and deterministic number serialization.
20
+ *
21
+ * Copied verbatim from certcrypt-verifier-toolkit-js/core/crypto/jcs.ts so the
22
+ * issuer-side canonical bytes match the verifier's byte-for-byte. The toolkit
23
+ * does not export this module, so it is vendored here rather than imported.
24
+ */
25
+ export function jcsCanonicalizeBytes(obj) {
26
+ const canonical = jcsStringify(obj);
27
+ return new TextEncoder().encode(canonical);
28
+ }
29
+ export function jcsCanonicalizeString(obj) {
30
+ return jcsStringify(obj);
31
+ }
32
+ function jcsStringify(value) {
33
+ if (value === null)
34
+ return "null";
35
+ if (value === undefined)
36
+ return "null";
37
+ const t = typeof value;
38
+ if (t === "boolean")
39
+ return value ? "true" : "false";
40
+ if (t === "number") {
41
+ // RFC 8785: use ECMAScript Number.toString() serialization
42
+ if (!isFinite(value)) {
43
+ throw new Error("JCS does not support Infinity or NaN");
44
+ }
45
+ return String(value);
46
+ }
47
+ if (t === "string") {
48
+ return JSON.stringify(value);
49
+ }
50
+ if (Array.isArray(value)) {
51
+ const items = value.map((v) => jcsStringify(v));
52
+ return "[" + items.join(",") + "]";
53
+ }
54
+ if (t === "object" && value !== null) {
55
+ const obj = value;
56
+ // RFC 8785: sort keys lexicographically by Unicode code point
57
+ const keys = Object.keys(obj).sort();
58
+ const members = keys
59
+ .filter((k) => obj[k] !== undefined)
60
+ .map((k) => JSON.stringify(k) + ":" + jcsStringify(obj[k]));
61
+ return "{" + members.join(",") + "}";
62
+ }
63
+ throw new Error(`JCS: unsupported type ${t}`);
64
+ }
65
+ //# sourceMappingURL=jcs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jcs.js","sourceRoot":"","sources":["../../../src/crypto/jcs.ts"],"names":[],"mappings":"AAAA,2FAA2F;AAC3F,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,iDAAiD;AACjD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC;;;;;;;;;;GAUG;AACH,MAAM,UAAU,oBAAoB,CAAC,GAAY;IAC/C,MAAM,SAAS,GAAG,YAAY,CAAC,GAAG,CAAC,CA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,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;QACrC,MAAM,OAAO,GAAG,IAAI;aACjB,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC;aACnC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAC9D,OAAO,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;IACvC,CAAC;IAED,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,EAAE,CAAC,CAAC;AAChD,CAAC"}
@@ -0,0 +1,18 @@
1
+ /** Leaf hash: SHA3-256(0x00 || preimage_bytes), lowercase hex. */
2
+ export declare function computeLeafHash(preimageBytes: Uint8Array): string;
3
+ /**
4
+ * Merkle root over leaf hashes. Odd-rightmost child promoted unchanged.
5
+ * Single leaf: the root is that leaf hash.
6
+ */
7
+ export declare function computeMerkleRoot(leafHashes: string[]): string;
8
+ /** Sibling hashes (inclusion proof) for a leaf, bottom-up. */
9
+ export declare function generateInclusionProof(leafHashes: string[], leafIndex: number): string[];
10
+ /**
11
+ * Verify an inclusion proof against a root. Mirrors the verifier so the SDK can
12
+ * self-check what it produced before shipping it.
13
+ *
14
+ * Fail-closed, never throws: `treeSize` must be a safe integer >= 1 and
15
+ * `leafIndex` a safe integer in [0, treeSize); the leaf hash, the root and every
16
+ * sibling must be 64-char lowercase hex; every sibling must be consumed.
17
+ */
18
+ export declare function verifyInclusionProof(leafHash: string, leafIndex: number, siblingHashes: string[], expectedRoot: string, treeSize: number): boolean;
@@ -0,0 +1,150 @@
1
+ // Copyright © 2025-2026 CERTCRYPT, Inc., its affiliates or licensors. All rights reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ /**
15
+ * SHA3-256 Merkle tree — issuer-side mirror of
16
+ * `certcrypt-verifier-toolkit-js/core/crypto/merkle.ts`, ported byte-for-byte so
17
+ * the SDK builds the exact same `holder_key_set_root` and inclusion proofs the
18
+ * verifier checks.
19
+ *
20
+ * Construction (`holder-keyset.merkle-sha3-256/1`):
21
+ * - leaf hash = SHA3-256(0x00 || leaf_preimage_bytes)
22
+ * - node hash = SHA3-256(0x01 || left_bytes || right_bytes)
23
+ * - odd-rightmost child promoted unchanged
24
+ *
25
+ * Field/byte order is load-bearing: a divergent byte breaks verification.
26
+ */
27
+ import { digestHexBytes } from "./digest.js";
28
+ const LEAF_PREFIX = new Uint8Array([0x00]);
29
+ const NODE_PREFIX = new Uint8Array([0x01]);
30
+ function hexToBytes(hex) {
31
+ const bytes = new Uint8Array(hex.length / 2);
32
+ for (let i = 0; i < hex.length; i += 2) {
33
+ bytes[i / 2] = parseInt(hex.slice(i, i + 2), 16);
34
+ }
35
+ return bytes;
36
+ }
37
+ /** Leaf hash: SHA3-256(0x00 || preimage_bytes), lowercase hex. */
38
+ export function computeLeafHash(preimageBytes) {
39
+ const prefixed = new Uint8Array(1 + preimageBytes.length);
40
+ prefixed.set(LEAF_PREFIX, 0);
41
+ prefixed.set(preimageBytes, 1);
42
+ return digestHexBytes(prefixed);
43
+ }
44
+ /** Internal node hash: SHA3-256(0x01 || left_bytes || right_bytes). */
45
+ function computeNodeHash(leftHex, rightHex) {
46
+ const left = hexToBytes(leftHex);
47
+ const right = hexToBytes(rightHex);
48
+ const prefixed = new Uint8Array(1 + left.length + right.length);
49
+ prefixed.set(NODE_PREFIX, 0);
50
+ prefixed.set(left, 1);
51
+ prefixed.set(right, 1 + left.length);
52
+ return digestHexBytes(prefixed);
53
+ }
54
+ /**
55
+ * Merkle root over leaf hashes. Odd-rightmost child promoted unchanged.
56
+ * Single leaf: the root is that leaf hash.
57
+ */
58
+ export function computeMerkleRoot(leafHashes) {
59
+ if (leafHashes.length === 0) {
60
+ throw new Error("cannot compute Merkle root of empty leaf set");
61
+ }
62
+ if (leafHashes.length === 1) {
63
+ return leafHashes[0];
64
+ }
65
+ let level = [...leafHashes];
66
+ while (level.length > 1) {
67
+ const next = [];
68
+ for (let i = 0; i < level.length; i += 2) {
69
+ if (i + 1 < level.length) {
70
+ next.push(computeNodeHash(level[i], level[i + 1]));
71
+ }
72
+ else {
73
+ next.push(level[i]);
74
+ }
75
+ }
76
+ level = next;
77
+ }
78
+ return level[0];
79
+ }
80
+ /** Sibling hashes (inclusion proof) for a leaf, bottom-up. */
81
+ export function generateInclusionProof(leafHashes, leafIndex) {
82
+ if (leafHashes.length <= 1)
83
+ return [];
84
+ const siblings = [];
85
+ let level = [...leafHashes];
86
+ let idx = leafIndex;
87
+ while (level.length > 1) {
88
+ const hasSibling = idx % 2 === 1 || idx + 1 < level.length;
89
+ if (hasSibling) {
90
+ const siblingIdx = idx % 2 === 0 ? idx + 1 : idx - 1;
91
+ siblings.push(level[siblingIdx]);
92
+ }
93
+ const next = [];
94
+ for (let i = 0; i < level.length; i += 2) {
95
+ if (i + 1 < level.length) {
96
+ next.push(computeNodeHash(level[i], level[i + 1]));
97
+ }
98
+ else {
99
+ next.push(level[i]);
100
+ }
101
+ }
102
+ level = next;
103
+ idx = Math.floor(idx / 2);
104
+ }
105
+ return siblings;
106
+ }
107
+ const HEX64_RE = /^[0-9a-f]{64}$/;
108
+ /**
109
+ * Verify an inclusion proof against a root. Mirrors the verifier so the SDK can
110
+ * self-check what it produced before shipping it.
111
+ *
112
+ * Fail-closed, never throws: `treeSize` must be a safe integer >= 1 and
113
+ * `leafIndex` a safe integer in [0, treeSize); the leaf hash, the root and every
114
+ * sibling must be 64-char lowercase hex; every sibling must be consumed.
115
+ */
116
+ export function verifyInclusionProof(leafHash, leafIndex, siblingHashes, expectedRoot, treeSize) {
117
+ if (!Number.isSafeInteger(treeSize) || treeSize < 1)
118
+ return false;
119
+ if (!Number.isSafeInteger(leafIndex) || leafIndex < 0 || leafIndex >= treeSize)
120
+ return false;
121
+ if (typeof leafHash !== "string" || !HEX64_RE.test(leafHash))
122
+ return false;
123
+ if (typeof expectedRoot !== "string" || !HEX64_RE.test(expectedRoot))
124
+ return false;
125
+ if (!Array.isArray(siblingHashes))
126
+ return false;
127
+ for (const s of siblingHashes) {
128
+ if (typeof s !== "string" || !HEX64_RE.test(s))
129
+ return false;
130
+ }
131
+ let currentHash = leafHash;
132
+ let currentIndex = leafIndex;
133
+ let siblingIndex = 0;
134
+ let levelWidth = treeSize;
135
+ while (levelWidth > 1) {
136
+ const isRight = currentIndex % 2 === 1;
137
+ if (isRight || currentIndex + 1 < levelWidth) {
138
+ if (siblingIndex >= siblingHashes.length)
139
+ return false;
140
+ const sibling = siblingHashes[siblingIndex++];
141
+ currentHash = isRight
142
+ ? computeNodeHash(sibling, currentHash)
143
+ : computeNodeHash(currentHash, sibling);
144
+ }
145
+ currentIndex = Math.floor(currentIndex / 2);
146
+ levelWidth = Math.ceil(levelWidth / 2);
147
+ }
148
+ return siblingIndex === siblingHashes.length && currentHash === expectedRoot;
149
+ }
150
+ //# sourceMappingURL=merkle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"merkle.js","sourceRoot":"","sources":["../../../src/crypto/merkle.ts"],"names":[],"mappings":"AAAA,2FAA2F;AAC3F,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,iDAAiD;AACjD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC;;;;;;;;;;;;GAYG;AAE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OAAO,GAAG,aAAa,CAAC,YAAY,EAAE,CAAC,CAAC;YAC9C,WAAW,GAAG,OAAO;gBACnB,CAAC,CAAC,eAAe,CAAC,OAAO,EAAE,WAAW,CAAC;gBACvC,CAAC,CAAC,eAAe,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;QAC5C,CAAC;QACD,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,GAAG,CAAC,CAAC,CAAC;QAC5C,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC,CAAC;IACzC,CAAC;IACD,OAAO,YAAY,KAAK,aAAa,CAAC,MAAM,IAAI,WAAW,KAAK,YAAY,CAAC;AAC/E,CAAC"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Sign a message with an ML-DSA-65 holder secret key.
3
+ *
4
+ * @param secretKey raw ML-DSA-65 secret-key bytes
5
+ * @param msg the message bytes to sign (JCS canonical signing bytes)
6
+ * @returns raw ML-DSA-65 signature bytes
7
+ */
8
+ export declare function signMlDsa65(secretKey: Uint8Array, msg: Uint8Array): Uint8Array;
9
+ /**
10
+ * Verify an ML-DSA-65 signature. Provided for local self-checks and tests; the
11
+ * production verification path lives in the verifier toolkit / the server.
12
+ *
13
+ * @param publicKey raw ML-DSA-65 public-key bytes
14
+ * @param msg the signed message bytes
15
+ * @param sig raw ML-DSA-65 signature bytes
16
+ */
17
+ export declare function verifyMlDsa65(publicKey: Uint8Array, msg: Uint8Array, sig: Uint8Array): boolean;
18
+ /**
19
+ * Generate an ML-DSA-65 keypair. For development and tests: in normal use the
20
+ * operation-key tree is derived from the issuer's own master seed via
21
+ * `HolderKeySet.fromMasterSeed`, so keys are never generated ad hoc.
22
+ *
23
+ * @param seed optional 32-byte seed for deterministic keygen
24
+ */
25
+ export declare function keygenMlDsa65(seed?: Uint8Array): {
26
+ publicKey: Uint8Array;
27
+ secretKey: Uint8Array;
28
+ };
@@ -0,0 +1,60 @@
1
+ // Copyright © 2025-2026 CERTCRYPT, Inc., its affiliates or licensors. All rights reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ /**
15
+ * ML-DSA-65 (FIPS 204) signing for issuer-side Level-3 commitment signing.
16
+ *
17
+ * Backed by `@noble/post-quantum`. The issuer signs the canonical bytes of the
18
+ * projected commitment with a per-operation leaf key of its operation-key tree.
19
+ *
20
+ * Byte contract — every implementation of the protocol MUST agree on this:
21
+ * - EMPTY context (no `context` option passed to sign);
22
+ * - pure ML-DSA-65 (FIPS 204), no pre-hash; arg order `sign(msg, secretKey)`.
23
+ *
24
+ * The signature travels as `commitment_signature_b64` in `capacity.evidence/1`,
25
+ * with no algorithm identifier: that profile fixes pure ML-DSA-65.
26
+ */
27
+ import { ml_dsa65 } from "@noble/post-quantum/ml-dsa.js";
28
+ /**
29
+ * Sign a message with an ML-DSA-65 holder secret key.
30
+ *
31
+ * @param secretKey raw ML-DSA-65 secret-key bytes
32
+ * @param msg the message bytes to sign (JCS canonical signing bytes)
33
+ * @returns raw ML-DSA-65 signature bytes
34
+ */
35
+ export function signMlDsa65(secretKey, msg) {
36
+ // arg order is (msg, secretKey); no context option => empty context.
37
+ return ml_dsa65.sign(msg, secretKey);
38
+ }
39
+ /**
40
+ * Verify an ML-DSA-65 signature. Provided for local self-checks and tests; the
41
+ * production verification path lives in the verifier toolkit / the server.
42
+ *
43
+ * @param publicKey raw ML-DSA-65 public-key bytes
44
+ * @param msg the signed message bytes
45
+ * @param sig raw ML-DSA-65 signature bytes
46
+ */
47
+ export function verifyMlDsa65(publicKey, msg, sig) {
48
+ return ml_dsa65.verify(sig, msg, publicKey);
49
+ }
50
+ /**
51
+ * Generate an ML-DSA-65 keypair. For development and tests: in normal use the
52
+ * operation-key tree is derived from the issuer's own master seed via
53
+ * `HolderKeySet.fromMasterSeed`, so keys are never generated ad hoc.
54
+ *
55
+ * @param seed optional 32-byte seed for deterministic keygen
56
+ */
57
+ export function keygenMlDsa65(seed) {
58
+ return ml_dsa65.keygen(seed);
59
+ }
60
+ //# sourceMappingURL=ml-dsa.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ml-dsa.js","sourceRoot":"","sources":["../../../src/crypto/ml-dsa.ts"],"names":[],"mappings":"AAAA,2FAA2F;AAC3F,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,iDAAiD;AACjD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC;;;;;;;;;;;;GAYG;AACH,OAAO,EAAE,QAAQ,EAAE,MAAM,+BAA+B,CAAC;AAEzD;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,SAAqB,EAAE,GAAe;IAChE,qEAAqE;IACrE,OAAO,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;AACvC,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAC3B,SAAqB,EACrB,GAAe,EACf,GAAe;IAEf,OAAO,QAAQ,CAAC,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,SAAS,CAAC,CAAC;AAC9C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,IAAiB;IAI7C,OAAO,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;AAC/B,CAAC"}
@@ -0,0 +1,8 @@
1
+ export type PseudonymDomain = "holder" | "pool" | "authorization";
2
+ /**
3
+ * Derive an operation-scoped pseudonym. `operationSalt` is the public
4
+ * per-operation salt carried in the evidence; `domain` separates namespaces;
5
+ * `stableId` is the identifier being projected; the capacity evidence carries
6
+ * only its pseudonym.
7
+ */
8
+ export declare function derivePseudonym(operationSalt: Uint8Array, domain: PseudonymDomain, stableId: string): string;