@zanii/pq 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +159 -0
- package/README.md +49 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +202 -0
- package/dist/index.js.map +1 -0
- package/package.json +44 -0
- package/src/index.ts +258 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
95
|
+
Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work; and
|
|
103
|
+
|
|
104
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
105
|
+
distribution, then any Derivative Works that You distribute must
|
|
106
|
+
include a readable copy of the attribution notices contained
|
|
107
|
+
within such NOTICE file.
|
|
108
|
+
|
|
109
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
110
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
111
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
112
|
+
this License, without any additional terms or conditions.
|
|
113
|
+
|
|
114
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
115
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
116
|
+
except as required for reasonable and customary use in describing the
|
|
117
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
118
|
+
|
|
119
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed
|
|
120
|
+
to in writing, Licensor provides the Work (and each Contributor
|
|
121
|
+
provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES
|
|
122
|
+
OR CONDITIONS OF ANY KIND, either express or implied, including,
|
|
123
|
+
without limitation, any warranties or conditions of TITLE,
|
|
124
|
+
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE.
|
|
125
|
+
|
|
126
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
127
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
128
|
+
unless required by applicable law (such as deliberate and grossly
|
|
129
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
130
|
+
liable to You for damages, including any direct, indirect, special,
|
|
131
|
+
incidental, or consequential damages of any character arising as a
|
|
132
|
+
result of this License or out of the use or inability to use the
|
|
133
|
+
Work.
|
|
134
|
+
|
|
135
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
136
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
137
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
138
|
+
or other liability obligations and/or rights consistent with this
|
|
139
|
+
License. However, in accepting such obligations, You may act only
|
|
140
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
141
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
142
|
+
defend, and hold each Contributor harmless for any liability
|
|
143
|
+
incurred by, or claims asserted against, such Contributor.
|
|
144
|
+
|
|
145
|
+
END OF TERMS AND CONDITIONS
|
|
146
|
+
|
|
147
|
+
Copyright 2026 Zanii
|
|
148
|
+
|
|
149
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
150
|
+
you may not use this file except in compliance with the License.
|
|
151
|
+
You may obtain a copy of the License at
|
|
152
|
+
|
|
153
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
154
|
+
|
|
155
|
+
Unless required by applicable law or agreed to in writing, software
|
|
156
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
157
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
158
|
+
See the License for the specific language governing permissions and
|
|
159
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# @zanii/pq
|
|
2
|
+
|
|
3
|
+
Post-quantum migration rails: **dual-sign today, stay provable after Ed25519.**
|
|
4
|
+
Hybrid Ed25519 + **ML-DSA-65** (FIPS 204) — the boring package that becomes mandatory
|
|
5
|
+
the day a regulator says "post-quantum", and the government/health verticals ask early.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @zanii/pq @zanii/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { generatePqKeypair, bindPqKey, verifyPqBinding, dualSign, verifyDual, transitionPayload, verifyTransition } from '@zanii/pq';
|
|
13
|
+
|
|
14
|
+
// 1. Bind an ML-DSA-65 key to the agent's did:key — BOTH keys sign the same body,
|
|
15
|
+
// so possession of both is proven; one key alone cannot forge the binding.
|
|
16
|
+
const pq = generatePqKeypair();
|
|
17
|
+
const binding = bindPqKey({ did: agent.did, pqPublicKey: pq.publicKey, ts: now }, agent.privateKey, pq.secretKey);
|
|
18
|
+
verifyPqBinding(binding); // { ok, reasons }
|
|
19
|
+
|
|
20
|
+
// 2. Dual-sign anything — verification requires BOTH signatures.
|
|
21
|
+
const sigs = dualSign(doc, agent.privateKey, pq.secretKey);
|
|
22
|
+
verifyDual(doc, agent.did, binding, sigs); // missing ML-DSA = failure, never a fallback
|
|
23
|
+
|
|
24
|
+
// 3. Anchor the binding into the log NOW (unsalted — it's public by design):
|
|
25
|
+
await zanii.record({ target: 'pq.transition', payload: transitionPayload(binding), salt: false });
|
|
26
|
+
// later, prove the binding predates any Ed25519 break:
|
|
27
|
+
verifyTransition(binding, receipt, { sth, index, proof });
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Python (`pip install "zanii[pq]"`): `from zanii.pq import bind_pq_key, dual_sign, verify_dual, ...`
|
|
31
|
+
— **cross-language verified**: a Python-signed binding verifies in TypeScript
|
|
32
|
+
(`@noble/post-quantum`) and vice versa; both implement final FIPS 204.
|
|
33
|
+
|
|
34
|
+
## Why the transition record is the point
|
|
35
|
+
|
|
36
|
+
The Merkle log is SHA-256, which no known quantum algorithm breaks in any practical
|
|
37
|
+
sense. So anchoring the binding **now** buys the thing that matters later: when
|
|
38
|
+
Ed25519 falls, an Ed25519 signature that was provably included in an anchored tree
|
|
39
|
+
*before* the break is still evidence of when it was made — the same signature made
|
|
40
|
+
*after* the break proves nothing. "This PQ key was this agent's key all along"
|
|
41
|
+
survives the event it defends against.
|
|
42
|
+
|
|
43
|
+
## The limit, stated up front
|
|
44
|
+
|
|
45
|
+
Pre-migration receipts are **not re-signed** and never will be — their post-quantum
|
|
46
|
+
protection is the anchored timestamp, not the signature. And hybrid is only hybrid
|
|
47
|
+
when the verifier demands both signatures: `verifyDual` refuses a missing ML-DSA
|
|
48
|
+
signature rather than quietly falling back to Ed25519-only, because a hybrid that
|
|
49
|
+
degrades silently isn't one.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { type Receipt, type SignedTreeHead } from '@zanii/core';
|
|
2
|
+
export declare const PQ_ALG: "ML-DSA-65";
|
|
3
|
+
export interface PqKeypair {
|
|
4
|
+
publicKey: Uint8Array;
|
|
5
|
+
secretKey: Uint8Array;
|
|
6
|
+
}
|
|
7
|
+
/** Generate an ML-DSA-65 keypair. Pass a 32-byte seed for deterministic derivation. */
|
|
8
|
+
export declare function generatePqKeypair(seed?: Uint8Array): PqKeypair;
|
|
9
|
+
export interface PqBinding {
|
|
10
|
+
v: 1;
|
|
11
|
+
type: 'pq.binding';
|
|
12
|
+
/** The agent's classical identity — the Ed25519 did:key everything else uses. */
|
|
13
|
+
did: string;
|
|
14
|
+
alg: typeof PQ_ALG;
|
|
15
|
+
/** ML-DSA-65 public key, hex (1952 bytes). */
|
|
16
|
+
pq_pub: string;
|
|
17
|
+
ts: string;
|
|
18
|
+
/** `ed25519:<hex>` by the did's key — the classical key vouches for the PQ key. */
|
|
19
|
+
sig_ed?: string;
|
|
20
|
+
/** `mldsa65:<hex>` by the PQ key — proves possession; not just a claimed key. */
|
|
21
|
+
sig_pq?: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Bind a PQ public key to a did:key. BOTH keys sign the same unsigned body — an
|
|
25
|
+
* attacker holding only the Ed25519 key (or only the PQ key) cannot produce this.
|
|
26
|
+
*/
|
|
27
|
+
export declare function bindPqKey(fields: {
|
|
28
|
+
did: string;
|
|
29
|
+
pqPublicKey: Uint8Array;
|
|
30
|
+
ts: string;
|
|
31
|
+
}, edPrivateKey: Uint8Array, pqSecretKey: Uint8Array): PqBinding;
|
|
32
|
+
export interface PqCheck {
|
|
33
|
+
ok: boolean;
|
|
34
|
+
reasons: string[];
|
|
35
|
+
}
|
|
36
|
+
/** Verify a binding: structure + BOTH signatures over the same body. */
|
|
37
|
+
export declare function verifyPqBinding(binding: PqBinding | undefined): PqCheck;
|
|
38
|
+
/** Canonical hash of the full binding (both sigs included) — its stable id. */
|
|
39
|
+
export declare function bindingHash(binding: PqBinding): string;
|
|
40
|
+
export interface DualSignature {
|
|
41
|
+
/** `ed25519:<hex>`. */
|
|
42
|
+
sig_ed: string;
|
|
43
|
+
/** `mldsa65:<hex>`. */
|
|
44
|
+
sig_pq: string;
|
|
45
|
+
}
|
|
46
|
+
/** Sign any protocol object with both algorithms over its canonical (JCS) bytes. */
|
|
47
|
+
export declare function dualSign(obj: Record<string, unknown>, edPrivateKey: Uint8Array, pqSecretKey: Uint8Array): DualSignature;
|
|
48
|
+
/**
|
|
49
|
+
* Verify a dual signature. BOTH must pass, and the PQ public key comes from a
|
|
50
|
+
* **verified binding for the same did** — never from the message. A missing ML-DSA
|
|
51
|
+
* signature is a failure, not a fallback: hybrid that degrades silently isn't hybrid.
|
|
52
|
+
*/
|
|
53
|
+
export declare function verifyDual(obj: Record<string, unknown>, did: string, binding: PqBinding, sigs: Partial<DualSignature> | undefined): PqCheck;
|
|
54
|
+
/**
|
|
55
|
+
* The payload to `record()` UNSALTED (`salt: false` / `salt=False`) as the transition
|
|
56
|
+
* receipt — a PQ binding is public by design, and the receipt's `payload_hash` must be
|
|
57
|
+
* recomputable by any verifier. Suggested target: `pq.transition`.
|
|
58
|
+
*/
|
|
59
|
+
export declare function transitionPayload(binding: PqBinding): string;
|
|
60
|
+
export interface TransitionEvidence {
|
|
61
|
+
sth: SignedTreeHead;
|
|
62
|
+
index: number;
|
|
63
|
+
/** Audit path, hex-encoded. */
|
|
64
|
+
proof: string[];
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Verify the transition: the receipt commits to EXACTLY this binding
|
|
68
|
+
* (`payload_hash` matches, unsalted) and the receipt is Merkle-proven in the log the
|
|
69
|
+
* STH commits to. The STH's anchored timestamp then bounds when the binding existed —
|
|
70
|
+
* the piece that survives a future Ed25519 break.
|
|
71
|
+
*/
|
|
72
|
+
export declare function verifyTransition(binding: PqBinding, receipt: Receipt, evidence: TransitionEvidence | undefined): PqCheck;
|
|
73
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA+BA,OAAO,EAQL,KAAK,OAAO,EACZ,KAAK,cAAc,EACpB,MAAM,aAAa,CAA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,EAAE,OAAO,CAAC,EAAE,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,UAAU,GAAG,aAAa,CAMvH;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CACxB,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,SAAS,EAClB,IAAI,EAAE,OAAO,CAAC,aAAa,CAAC,GAAG,SAAS,GACvC,OAAO,CAiCT;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,SAAS,GAAG,MAAM,CAI5D;AAED,MAAM,WAAW,kBAAkB;IACjC,GAAG,EAAE,cAAc,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,+BAA+B;IAC/B,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,kBAAkB,GAAG,SAAS,GAAG,OAAO,CAwBxH"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zanii/pq — post-quantum migration rails: dual-sign today, stay provable after Ed25519.
|
|
3
|
+
*
|
|
4
|
+
* Everything in Zanii signs with Ed25519, which a large quantum computer breaks. The
|
|
5
|
+
* cheap, correct move is not to wait: it is a **hybrid** that a government or hospital
|
|
6
|
+
* buyer can adopt now. Three pieces:
|
|
7
|
+
*
|
|
8
|
+
* 1. **A PQ key binding** — an agent binds an ML-DSA-65 (FIPS 204) public key to its
|
|
9
|
+
* did:key, signed by **both** keys over the same body. Both signatures prove
|
|
10
|
+
* possession of both keys: an attacker holding only one cannot forge the binding.
|
|
11
|
+
* 2. **Dual-signing** (`dualSign`/`verifyDual`) — any protocol object signed by both
|
|
12
|
+
* algorithms, and verification requires **BOTH** to pass. Hybrid security: forging
|
|
13
|
+
* needs Ed25519 AND ML-DSA broken at once.
|
|
14
|
+
* 3. **The transition record** — anchor the binding into the Merkle log NOW. The log
|
|
15
|
+
* is SHA-256 (not broken by known quantum algorithms in any practical sense), so
|
|
16
|
+
* the anchored, timestamped inclusion of the binding proves it existed BEFORE any
|
|
17
|
+
* future Ed25519 break — "this PQ key was this agent's key all along" survives the
|
|
18
|
+
* thing it is defending against.
|
|
19
|
+
*
|
|
20
|
+
* ## The limit, stated up front
|
|
21
|
+
*
|
|
22
|
+
* Pre-migration receipts are **not re-signed** and never will be — their post-quantum
|
|
23
|
+
* protection is the anchored log timestamp (an Ed25519 signature that was included in
|
|
24
|
+
* an anchored tree BEFORE the break is evidence of when it was made; the same
|
|
25
|
+
* signature made AFTER the break proves nothing). Hybrid verification is only hybrid
|
|
26
|
+
* when the verifier demands both signatures — `verifyDual` refuses a missing
|
|
27
|
+
* ML-DSA signature rather than quietly falling back to Ed25519-only.
|
|
28
|
+
*/
|
|
29
|
+
import { ed25519 } from '@noble/curves/ed25519';
|
|
30
|
+
import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
|
|
31
|
+
import { ml_dsa65 } from '@noble/post-quantum/ml-dsa.js';
|
|
32
|
+
import { canonicalBytes, hashPayload, jcsHash, leafHash, publicKeyFromDid, verifyInclusion, verifySTH, } from '@zanii/core';
|
|
33
|
+
export const PQ_ALG = 'ML-DSA-65';
|
|
34
|
+
/** Generate an ML-DSA-65 keypair. Pass a 32-byte seed for deterministic derivation. */
|
|
35
|
+
export function generatePqKeypair(seed) {
|
|
36
|
+
const keys = seed ? ml_dsa65.keygen(seed) : ml_dsa65.keygen();
|
|
37
|
+
return { publicKey: keys.publicKey, secretKey: keys.secretKey };
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Bind a PQ public key to a did:key. BOTH keys sign the same unsigned body — an
|
|
41
|
+
* attacker holding only the Ed25519 key (or only the PQ key) cannot produce this.
|
|
42
|
+
*/
|
|
43
|
+
export function bindPqKey(fields, edPrivateKey, pqSecretKey) {
|
|
44
|
+
if (!fields.did)
|
|
45
|
+
throw new Error('did is required');
|
|
46
|
+
if (Number.isNaN(Date.parse(fields.ts)))
|
|
47
|
+
throw new Error('ts must be an ISO timestamp');
|
|
48
|
+
const unsigned = {
|
|
49
|
+
v: 1,
|
|
50
|
+
type: 'pq.binding',
|
|
51
|
+
did: fields.did,
|
|
52
|
+
alg: PQ_ALG,
|
|
53
|
+
pq_pub: bytesToHex(fields.pqPublicKey),
|
|
54
|
+
ts: fields.ts,
|
|
55
|
+
};
|
|
56
|
+
const bytes = canonicalBytes(unsigned);
|
|
57
|
+
return {
|
|
58
|
+
...unsigned,
|
|
59
|
+
sig_ed: `ed25519:${bytesToHex(ed25519.sign(bytes, edPrivateKey))}`,
|
|
60
|
+
sig_pq: `mldsa65:${bytesToHex(ml_dsa65.sign(bytes, pqSecretKey))}`,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** Verify a binding: structure + BOTH signatures over the same body. */
|
|
64
|
+
export function verifyPqBinding(binding) {
|
|
65
|
+
const reasons = [];
|
|
66
|
+
const b = binding ?? {};
|
|
67
|
+
if (b.v !== 1 || b.type !== 'pq.binding')
|
|
68
|
+
reasons.push('not a pq.binding (v1)');
|
|
69
|
+
if (b.alg !== PQ_ALG)
|
|
70
|
+
reasons.push(`unsupported alg '${b.alg}'`);
|
|
71
|
+
if (!b.did)
|
|
72
|
+
reasons.push('missing did');
|
|
73
|
+
if (typeof b.pq_pub !== 'string' || !/^[0-9a-f]+$/.test(b.pq_pub))
|
|
74
|
+
reasons.push('missing/invalid pq_pub');
|
|
75
|
+
if (typeof b.ts !== 'string' || Number.isNaN(Date.parse(b.ts)))
|
|
76
|
+
reasons.push('missing/invalid ts');
|
|
77
|
+
if (reasons.length > 0)
|
|
78
|
+
return { ok: false, reasons };
|
|
79
|
+
const { sig_ed, sig_pq, ...unsigned } = b;
|
|
80
|
+
const bytes = canonicalBytes(unsigned);
|
|
81
|
+
const pub = publicKeyFromDid(b.did);
|
|
82
|
+
const edHex = typeof sig_ed === 'string' && sig_ed.startsWith('ed25519:') ? sig_ed.slice(8) : '';
|
|
83
|
+
let edOk = false;
|
|
84
|
+
if (pub && /^[0-9a-f]{128}$/.test(edHex)) {
|
|
85
|
+
try {
|
|
86
|
+
edOk = ed25519.verify(hexToBytes(edHex), bytes, pub);
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
edOk = false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (!edOk)
|
|
93
|
+
reasons.push('Ed25519 signature does not verify against the did');
|
|
94
|
+
const pqHex = typeof sig_pq === 'string' && sig_pq.startsWith('mldsa65:') ? sig_pq.slice(8) : '';
|
|
95
|
+
let pqOk = false;
|
|
96
|
+
if (/^[0-9a-f]+$/.test(pqHex) && pqHex.length > 0) {
|
|
97
|
+
try {
|
|
98
|
+
pqOk = ml_dsa65.verify(hexToBytes(pqHex), bytes, hexToBytes(b.pq_pub));
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
pqOk = false;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (!pqOk)
|
|
105
|
+
reasons.push('ML-DSA signature does not verify against pq_pub (possession not proven)');
|
|
106
|
+
return { ok: reasons.length === 0, reasons };
|
|
107
|
+
}
|
|
108
|
+
/** Canonical hash of the full binding (both sigs included) — its stable id. */
|
|
109
|
+
export function bindingHash(binding) {
|
|
110
|
+
return jcsHash(binding);
|
|
111
|
+
}
|
|
112
|
+
/** Sign any protocol object with both algorithms over its canonical (JCS) bytes. */
|
|
113
|
+
export function dualSign(obj, edPrivateKey, pqSecretKey) {
|
|
114
|
+
const bytes = canonicalBytes(obj);
|
|
115
|
+
return {
|
|
116
|
+
sig_ed: `ed25519:${bytesToHex(ed25519.sign(bytes, edPrivateKey))}`,
|
|
117
|
+
sig_pq: `mldsa65:${bytesToHex(ml_dsa65.sign(bytes, pqSecretKey))}`,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Verify a dual signature. BOTH must pass, and the PQ public key comes from a
|
|
122
|
+
* **verified binding for the same did** — never from the message. A missing ML-DSA
|
|
123
|
+
* signature is a failure, not a fallback: hybrid that degrades silently isn't hybrid.
|
|
124
|
+
*/
|
|
125
|
+
export function verifyDual(obj, did, binding, sigs) {
|
|
126
|
+
const reasons = [];
|
|
127
|
+
const bind = verifyPqBinding(binding);
|
|
128
|
+
if (!bind.ok)
|
|
129
|
+
return { ok: false, reasons: bind.reasons.map((r) => `binding: ${r}`) };
|
|
130
|
+
if (binding.did !== did)
|
|
131
|
+
return { ok: false, reasons: [`binding is for ${binding.did}, not ${did}`] };
|
|
132
|
+
const bytes = canonicalBytes(obj);
|
|
133
|
+
const s = sigs ?? {};
|
|
134
|
+
const pub = publicKeyFromDid(did);
|
|
135
|
+
const edHex = typeof s.sig_ed === 'string' && s.sig_ed.startsWith('ed25519:') ? s.sig_ed.slice(8) : '';
|
|
136
|
+
let edOk = false;
|
|
137
|
+
if (pub && /^[0-9a-f]{128}$/.test(edHex)) {
|
|
138
|
+
try {
|
|
139
|
+
edOk = ed25519.verify(hexToBytes(edHex), bytes, pub);
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
edOk = false;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (!edOk)
|
|
146
|
+
reasons.push('Ed25519 signature missing or invalid');
|
|
147
|
+
const pqHex = typeof s.sig_pq === 'string' && s.sig_pq.startsWith('mldsa65:') ? s.sig_pq.slice(8) : '';
|
|
148
|
+
let pqOk = false;
|
|
149
|
+
if (/^[0-9a-f]+$/.test(pqHex) && pqHex.length > 0) {
|
|
150
|
+
try {
|
|
151
|
+
pqOk = ml_dsa65.verify(hexToBytes(pqHex), bytes, hexToBytes(binding.pq_pub));
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
pqOk = false;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (!pqOk)
|
|
158
|
+
reasons.push('ML-DSA signature missing or invalid — refusing Ed25519-only fallback');
|
|
159
|
+
return { ok: reasons.length === 0, reasons };
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* The payload to `record()` UNSALTED (`salt: false` / `salt=False`) as the transition
|
|
163
|
+
* receipt — a PQ binding is public by design, and the receipt's `payload_hash` must be
|
|
164
|
+
* recomputable by any verifier. Suggested target: `pq.transition`.
|
|
165
|
+
*/
|
|
166
|
+
export function transitionPayload(binding) {
|
|
167
|
+
const check = verifyPqBinding(binding);
|
|
168
|
+
if (!check.ok)
|
|
169
|
+
throw new Error(`refusing to anchor an invalid binding: ${check.reasons.join('; ')}`);
|
|
170
|
+
return JSON.stringify(binding);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Verify the transition: the receipt commits to EXACTLY this binding
|
|
174
|
+
* (`payload_hash` matches, unsalted) and the receipt is Merkle-proven in the log the
|
|
175
|
+
* STH commits to. The STH's anchored timestamp then bounds when the binding existed —
|
|
176
|
+
* the piece that survives a future Ed25519 break.
|
|
177
|
+
*/
|
|
178
|
+
export function verifyTransition(binding, receipt, evidence) {
|
|
179
|
+
const reasons = [];
|
|
180
|
+
const bind = verifyPqBinding(binding);
|
|
181
|
+
if (!bind.ok)
|
|
182
|
+
return { ok: false, reasons: bind.reasons.map((r) => `binding: ${r}`) };
|
|
183
|
+
if (receipt.agent_id !== binding.did)
|
|
184
|
+
reasons.push(`transition receipt by ${receipt.agent_id}, but the binding is for ${binding.did}`);
|
|
185
|
+
if (receipt.payload_hash !== hashPayload(transitionPayload(binding))) {
|
|
186
|
+
reasons.push('receipt payload_hash does not commit to this binding (was it recorded salted?)');
|
|
187
|
+
}
|
|
188
|
+
if (!evidence) {
|
|
189
|
+
reasons.push('no inclusion evidence — an unanchored binding proves nothing about when it existed');
|
|
190
|
+
}
|
|
191
|
+
else if (!verifySTH(evidence.sth)) {
|
|
192
|
+
reasons.push('inclusion evidence STH signature is invalid');
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
const root = hexToBytes(evidence.sth.root.replace('sha256:', ''));
|
|
196
|
+
const ok = verifyInclusion(leafHash(canonicalBytes(receipt)), evidence.index, evidence.sth.size, evidence.proof.map((h) => hexToBytes(h)), root);
|
|
197
|
+
if (!ok)
|
|
198
|
+
reasons.push('inclusion proof FAILED — the transition is not in the log the STH commits to');
|
|
199
|
+
}
|
|
200
|
+
return { ok: reasons.length === 0, reasons };
|
|
201
|
+
}
|
|
202
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EAAE,QAAQ,EAAE,MAAM,+BAA+B,CAAC;AACzD,OAAO,EACL,cAAc,EACd,WAAW,EACX,OAAO,EACP,QAAQ,EACR,gBAAgB,EAChB,eAAe,EACf,SAAS,GAGV,MAAM,aAAa,CAAC;AAErB,MAAM,CAAC,MAAM,MAAM,GAAG,WAAoB,CAAC;AAO3C,uFAAuF;AACvF,MAAM,UAAU,iBAAiB,CAAC,IAAiB;IACjD,MAAM,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC;IAC9D,OAAO,EAAE,SAAS,EAAE,IAAI,CAAC,SAAS,EAAE,SAAS,EAAE,IAAI,CAAC,SAAS,EAAE,CAAC;AAClE,CAAC;AAiBD;;;GAGG;AACH,MAAM,UAAU,SAAS,CACvB,MAA4D,EAC5D,YAAwB,EACxB,WAAuB;IAEvB,IAAI,CAAC,MAAM,CAAC,GAAG;QAAE,MAAM,IAAI,KAAK,CAAC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;AAC/C,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zanii/pq",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Post-quantum migration rails - hybrid dual-signing (Ed25519 + ML-DSA-65, both must verify), a PQ key binding signed by BOTH keys (proves possession of each), and a transition record anchored into the Merkle log so the binding provably predates any future break of Ed25519. Old receipts are not re-signed: their post-quantum protection is the anchored timestamp, and we say so.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"homepage": "https://ledger.zanii.agency",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"src",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=18"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^26.1.0",
|
|
30
|
+
"typescript": "^5.8.2",
|
|
31
|
+
"vitest": "^3.0.9"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@noble/post-quantum": "^0.7.0",
|
|
35
|
+
"@noble/curves": "^1.8.1",
|
|
36
|
+
"@noble/hashes": "^1.7.1",
|
|
37
|
+
"@zanii/core": "0.4.0"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -p tsconfig.build.json",
|
|
41
|
+
"test": "vitest run",
|
|
42
|
+
"typecheck": "tsc --noEmit"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zanii/pq — post-quantum migration rails: dual-sign today, stay provable after Ed25519.
|
|
3
|
+
*
|
|
4
|
+
* Everything in Zanii signs with Ed25519, which a large quantum computer breaks. The
|
|
5
|
+
* cheap, correct move is not to wait: it is a **hybrid** that a government or hospital
|
|
6
|
+
* buyer can adopt now. Three pieces:
|
|
7
|
+
*
|
|
8
|
+
* 1. **A PQ key binding** — an agent binds an ML-DSA-65 (FIPS 204) public key to its
|
|
9
|
+
* did:key, signed by **both** keys over the same body. Both signatures prove
|
|
10
|
+
* possession of both keys: an attacker holding only one cannot forge the binding.
|
|
11
|
+
* 2. **Dual-signing** (`dualSign`/`verifyDual`) — any protocol object signed by both
|
|
12
|
+
* algorithms, and verification requires **BOTH** to pass. Hybrid security: forging
|
|
13
|
+
* needs Ed25519 AND ML-DSA broken at once.
|
|
14
|
+
* 3. **The transition record** — anchor the binding into the Merkle log NOW. The log
|
|
15
|
+
* is SHA-256 (not broken by known quantum algorithms in any practical sense), so
|
|
16
|
+
* the anchored, timestamped inclusion of the binding proves it existed BEFORE any
|
|
17
|
+
* future Ed25519 break — "this PQ key was this agent's key all along" survives the
|
|
18
|
+
* thing it is defending against.
|
|
19
|
+
*
|
|
20
|
+
* ## The limit, stated up front
|
|
21
|
+
*
|
|
22
|
+
* Pre-migration receipts are **not re-signed** and never will be — their post-quantum
|
|
23
|
+
* protection is the anchored log timestamp (an Ed25519 signature that was included in
|
|
24
|
+
* an anchored tree BEFORE the break is evidence of when it was made; the same
|
|
25
|
+
* signature made AFTER the break proves nothing). Hybrid verification is only hybrid
|
|
26
|
+
* when the verifier demands both signatures — `verifyDual` refuses a missing
|
|
27
|
+
* ML-DSA signature rather than quietly falling back to Ed25519-only.
|
|
28
|
+
*/
|
|
29
|
+
import { ed25519 } from '@noble/curves/ed25519';
|
|
30
|
+
import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
|
|
31
|
+
import { ml_dsa65 } from '@noble/post-quantum/ml-dsa.js';
|
|
32
|
+
import {
|
|
33
|
+
canonicalBytes,
|
|
34
|
+
hashPayload,
|
|
35
|
+
jcsHash,
|
|
36
|
+
leafHash,
|
|
37
|
+
publicKeyFromDid,
|
|
38
|
+
verifyInclusion,
|
|
39
|
+
verifySTH,
|
|
40
|
+
type Receipt,
|
|
41
|
+
type SignedTreeHead,
|
|
42
|
+
} from '@zanii/core';
|
|
43
|
+
|
|
44
|
+
export const PQ_ALG = 'ML-DSA-65' as const;
|
|
45
|
+
|
|
46
|
+
export interface PqKeypair {
|
|
47
|
+
publicKey: Uint8Array;
|
|
48
|
+
secretKey: Uint8Array;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Generate an ML-DSA-65 keypair. Pass a 32-byte seed for deterministic derivation. */
|
|
52
|
+
export function generatePqKeypair(seed?: Uint8Array): PqKeypair {
|
|
53
|
+
const keys = seed ? ml_dsa65.keygen(seed) : ml_dsa65.keygen();
|
|
54
|
+
return { publicKey: keys.publicKey, secretKey: keys.secretKey };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface PqBinding {
|
|
58
|
+
v: 1;
|
|
59
|
+
type: 'pq.binding';
|
|
60
|
+
/** The agent's classical identity — the Ed25519 did:key everything else uses. */
|
|
61
|
+
did: string;
|
|
62
|
+
alg: typeof PQ_ALG;
|
|
63
|
+
/** ML-DSA-65 public key, hex (1952 bytes). */
|
|
64
|
+
pq_pub: string;
|
|
65
|
+
ts: string;
|
|
66
|
+
/** `ed25519:<hex>` by the did's key — the classical key vouches for the PQ key. */
|
|
67
|
+
sig_ed?: string;
|
|
68
|
+
/** `mldsa65:<hex>` by the PQ key — proves possession; not just a claimed key. */
|
|
69
|
+
sig_pq?: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Bind a PQ public key to a did:key. BOTH keys sign the same unsigned body — an
|
|
74
|
+
* attacker holding only the Ed25519 key (or only the PQ key) cannot produce this.
|
|
75
|
+
*/
|
|
76
|
+
export function bindPqKey(
|
|
77
|
+
fields: { did: string; pqPublicKey: Uint8Array; ts: string },
|
|
78
|
+
edPrivateKey: Uint8Array,
|
|
79
|
+
pqSecretKey: Uint8Array,
|
|
80
|
+
): PqBinding {
|
|
81
|
+
if (!fields.did) throw new Error('did is required');
|
|
82
|
+
if (Number.isNaN(Date.parse(fields.ts))) throw new Error('ts must be an ISO timestamp');
|
|
83
|
+
const unsigned = {
|
|
84
|
+
v: 1 as const,
|
|
85
|
+
type: 'pq.binding' as const,
|
|
86
|
+
did: fields.did,
|
|
87
|
+
alg: PQ_ALG,
|
|
88
|
+
pq_pub: bytesToHex(fields.pqPublicKey),
|
|
89
|
+
ts: fields.ts,
|
|
90
|
+
};
|
|
91
|
+
const bytes = canonicalBytes(unsigned);
|
|
92
|
+
return {
|
|
93
|
+
...unsigned,
|
|
94
|
+
sig_ed: `ed25519:${bytesToHex(ed25519.sign(bytes, edPrivateKey))}`,
|
|
95
|
+
sig_pq: `mldsa65:${bytesToHex(ml_dsa65.sign(bytes, pqSecretKey))}`,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface PqCheck {
|
|
100
|
+
ok: boolean;
|
|
101
|
+
reasons: string[];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Verify a binding: structure + BOTH signatures over the same body. */
|
|
105
|
+
export function verifyPqBinding(binding: PqBinding | undefined): PqCheck {
|
|
106
|
+
const reasons: string[] = [];
|
|
107
|
+
const b = binding ?? ({} as PqBinding);
|
|
108
|
+
if (b.v !== 1 || b.type !== 'pq.binding') reasons.push('not a pq.binding (v1)');
|
|
109
|
+
if (b.alg !== PQ_ALG) reasons.push(`unsupported alg '${b.alg}'`);
|
|
110
|
+
if (!b.did) reasons.push('missing did');
|
|
111
|
+
if (typeof b.pq_pub !== 'string' || !/^[0-9a-f]+$/.test(b.pq_pub)) reasons.push('missing/invalid pq_pub');
|
|
112
|
+
if (typeof b.ts !== 'string' || Number.isNaN(Date.parse(b.ts))) reasons.push('missing/invalid ts');
|
|
113
|
+
if (reasons.length > 0) return { ok: false, reasons };
|
|
114
|
+
|
|
115
|
+
const { sig_ed, sig_pq, ...unsigned } = b;
|
|
116
|
+
const bytes = canonicalBytes(unsigned as unknown as Record<string, unknown>);
|
|
117
|
+
|
|
118
|
+
const pub = publicKeyFromDid(b.did);
|
|
119
|
+
const edHex = typeof sig_ed === 'string' && sig_ed.startsWith('ed25519:') ? sig_ed.slice(8) : '';
|
|
120
|
+
let edOk = false;
|
|
121
|
+
if (pub && /^[0-9a-f]{128}$/.test(edHex)) {
|
|
122
|
+
try {
|
|
123
|
+
edOk = ed25519.verify(hexToBytes(edHex), bytes, pub);
|
|
124
|
+
} catch {
|
|
125
|
+
edOk = false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (!edOk) reasons.push('Ed25519 signature does not verify against the did');
|
|
129
|
+
|
|
130
|
+
const pqHex = typeof sig_pq === 'string' && sig_pq.startsWith('mldsa65:') ? sig_pq.slice(8) : '';
|
|
131
|
+
let pqOk = false;
|
|
132
|
+
if (/^[0-9a-f]+$/.test(pqHex) && pqHex.length > 0) {
|
|
133
|
+
try {
|
|
134
|
+
pqOk = ml_dsa65.verify(hexToBytes(pqHex), bytes, hexToBytes(b.pq_pub));
|
|
135
|
+
} catch {
|
|
136
|
+
pqOk = false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (!pqOk) reasons.push('ML-DSA signature does not verify against pq_pub (possession not proven)');
|
|
140
|
+
|
|
141
|
+
return { ok: reasons.length === 0, reasons };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Canonical hash of the full binding (both sigs included) — its stable id. */
|
|
145
|
+
export function bindingHash(binding: PqBinding): string {
|
|
146
|
+
return jcsHash(binding as unknown as Record<string, unknown>);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export interface DualSignature {
|
|
150
|
+
/** `ed25519:<hex>`. */
|
|
151
|
+
sig_ed: string;
|
|
152
|
+
/** `mldsa65:<hex>`. */
|
|
153
|
+
sig_pq: string;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Sign any protocol object with both algorithms over its canonical (JCS) bytes. */
|
|
157
|
+
export function dualSign(obj: Record<string, unknown>, edPrivateKey: Uint8Array, pqSecretKey: Uint8Array): DualSignature {
|
|
158
|
+
const bytes = canonicalBytes(obj);
|
|
159
|
+
return {
|
|
160
|
+
sig_ed: `ed25519:${bytesToHex(ed25519.sign(bytes, edPrivateKey))}`,
|
|
161
|
+
sig_pq: `mldsa65:${bytesToHex(ml_dsa65.sign(bytes, pqSecretKey))}`,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Verify a dual signature. BOTH must pass, and the PQ public key comes from a
|
|
167
|
+
* **verified binding for the same did** — never from the message. A missing ML-DSA
|
|
168
|
+
* signature is a failure, not a fallback: hybrid that degrades silently isn't hybrid.
|
|
169
|
+
*/
|
|
170
|
+
export function verifyDual(
|
|
171
|
+
obj: Record<string, unknown>,
|
|
172
|
+
did: string,
|
|
173
|
+
binding: PqBinding,
|
|
174
|
+
sigs: Partial<DualSignature> | undefined,
|
|
175
|
+
): PqCheck {
|
|
176
|
+
const reasons: string[] = [];
|
|
177
|
+
const bind = verifyPqBinding(binding);
|
|
178
|
+
if (!bind.ok) return { ok: false, reasons: bind.reasons.map((r) => `binding: ${r}`) };
|
|
179
|
+
if (binding.did !== did) return { ok: false, reasons: [`binding is for ${binding.did}, not ${did}`] };
|
|
180
|
+
|
|
181
|
+
const bytes = canonicalBytes(obj);
|
|
182
|
+
const s = sigs ?? {};
|
|
183
|
+
|
|
184
|
+
const pub = publicKeyFromDid(did);
|
|
185
|
+
const edHex = typeof s.sig_ed === 'string' && s.sig_ed.startsWith('ed25519:') ? s.sig_ed.slice(8) : '';
|
|
186
|
+
let edOk = false;
|
|
187
|
+
if (pub && /^[0-9a-f]{128}$/.test(edHex)) {
|
|
188
|
+
try {
|
|
189
|
+
edOk = ed25519.verify(hexToBytes(edHex), bytes, pub);
|
|
190
|
+
} catch {
|
|
191
|
+
edOk = false;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (!edOk) reasons.push('Ed25519 signature missing or invalid');
|
|
195
|
+
|
|
196
|
+
const pqHex = typeof s.sig_pq === 'string' && s.sig_pq.startsWith('mldsa65:') ? s.sig_pq.slice(8) : '';
|
|
197
|
+
let pqOk = false;
|
|
198
|
+
if (/^[0-9a-f]+$/.test(pqHex) && pqHex.length > 0) {
|
|
199
|
+
try {
|
|
200
|
+
pqOk = ml_dsa65.verify(hexToBytes(pqHex), bytes, hexToBytes(binding.pq_pub));
|
|
201
|
+
} catch {
|
|
202
|
+
pqOk = false;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (!pqOk) reasons.push('ML-DSA signature missing or invalid — refusing Ed25519-only fallback');
|
|
206
|
+
|
|
207
|
+
return { ok: reasons.length === 0, reasons };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* The payload to `record()` UNSALTED (`salt: false` / `salt=False`) as the transition
|
|
212
|
+
* receipt — a PQ binding is public by design, and the receipt's `payload_hash` must be
|
|
213
|
+
* recomputable by any verifier. Suggested target: `pq.transition`.
|
|
214
|
+
*/
|
|
215
|
+
export function transitionPayload(binding: PqBinding): string {
|
|
216
|
+
const check = verifyPqBinding(binding);
|
|
217
|
+
if (!check.ok) throw new Error(`refusing to anchor an invalid binding: ${check.reasons.join('; ')}`);
|
|
218
|
+
return JSON.stringify(binding);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export interface TransitionEvidence {
|
|
222
|
+
sth: SignedTreeHead;
|
|
223
|
+
index: number;
|
|
224
|
+
/** Audit path, hex-encoded. */
|
|
225
|
+
proof: string[];
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Verify the transition: the receipt commits to EXACTLY this binding
|
|
230
|
+
* (`payload_hash` matches, unsalted) and the receipt is Merkle-proven in the log the
|
|
231
|
+
* STH commits to. The STH's anchored timestamp then bounds when the binding existed —
|
|
232
|
+
* the piece that survives a future Ed25519 break.
|
|
233
|
+
*/
|
|
234
|
+
export function verifyTransition(binding: PqBinding, receipt: Receipt, evidence: TransitionEvidence | undefined): PqCheck {
|
|
235
|
+
const reasons: string[] = [];
|
|
236
|
+
const bind = verifyPqBinding(binding);
|
|
237
|
+
if (!bind.ok) return { ok: false, reasons: bind.reasons.map((r) => `binding: ${r}`) };
|
|
238
|
+
if (receipt.agent_id !== binding.did) reasons.push(`transition receipt by ${receipt.agent_id}, but the binding is for ${binding.did}`);
|
|
239
|
+
if (receipt.payload_hash !== hashPayload(transitionPayload(binding))) {
|
|
240
|
+
reasons.push('receipt payload_hash does not commit to this binding (was it recorded salted?)');
|
|
241
|
+
}
|
|
242
|
+
if (!evidence) {
|
|
243
|
+
reasons.push('no inclusion evidence — an unanchored binding proves nothing about when it existed');
|
|
244
|
+
} else if (!verifySTH(evidence.sth)) {
|
|
245
|
+
reasons.push('inclusion evidence STH signature is invalid');
|
|
246
|
+
} else {
|
|
247
|
+
const root = hexToBytes(evidence.sth.root.replace('sha256:', ''));
|
|
248
|
+
const ok = verifyInclusion(
|
|
249
|
+
leafHash(canonicalBytes(receipt as unknown as Record<string, unknown>)),
|
|
250
|
+
evidence.index,
|
|
251
|
+
evidence.sth.size,
|
|
252
|
+
evidence.proof.map((h) => hexToBytes(h)),
|
|
253
|
+
root,
|
|
254
|
+
);
|
|
255
|
+
if (!ok) reasons.push('inclusion proof FAILED — the transition is not in the log the STH commits to');
|
|
256
|
+
}
|
|
257
|
+
return { ok: reasons.length === 0, reasons };
|
|
258
|
+
}
|