@zanii/train 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 +45 -0
- package/dist/index.d.ts +59 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +113 -0
- package/dist/index.js.map +1 -0
- package/package.json +42 -0
- package/src/index.ts +154 -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,45 @@
|
|
|
1
|
+
# @zanii/train
|
|
2
|
+
|
|
3
|
+
**Training-data provenance & subject-absence proofs.** The claim buyers actually want
|
|
4
|
+
is negative: *"my data was never used to train your model."* You can't prove that about
|
|
5
|
+
the whole world — but you can prove it about a **declared run**: commit the training set
|
|
6
|
+
to a sorted-Merkle manifest, then hand a data subject a **non-membership proof** their
|
|
7
|
+
`ref` is absent from it, checkable offline with no trust in the operator.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @zanii/train @zanii/core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { buildManifest, manifestReceipt, proveAbsence, verifyAbsence } from '@zanii/train';
|
|
15
|
+
|
|
16
|
+
// the model operator commits the training set (refs are hashes/HMACs, never content):
|
|
17
|
+
const manifest = buildManifest({ runId: 'gpt-run-7', refs: memberRefs, createdAt: now });
|
|
18
|
+
await zanii.record(manifestReceipt(manifest)); // anchor it into the ledger
|
|
19
|
+
|
|
20
|
+
// a data subject asks "was my document in it?" — the operator answers with a proof:
|
|
21
|
+
const proof = proveAbsence(manifest, memberRefs, userRef); // throws if actually present
|
|
22
|
+
verifyAbsence(proof, manifest); // TRUE, verified by the subject, offline
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Python: `from zanii.train import build_manifest, prove_absence, verify_absence, ...`
|
|
26
|
+
— **byte-identical** manifest roots (locked by a cross-language vector).
|
|
27
|
+
|
|
28
|
+
## How the absence proof works
|
|
29
|
+
|
|
30
|
+
Sorted-set non-membership — the pattern the Zanii L1's SMT uses. Sort the member refs
|
|
31
|
+
and Merkle-commit the adjacency leaves (each member, and the gap to the next). To prove
|
|
32
|
+
`x` is absent, reveal the one gap `(lo, hi)` with `lo < x < hi` plus its inclusion
|
|
33
|
+
proof in the manifest: since the set is sorted and every gap is committed, a covering
|
|
34
|
+
gap that verifies means nothing sits between `lo` and `hi` — so `x` is not a member.
|
|
35
|
+
Membership (`proveMembership`) is the same machinery on a member leaf. Proofs use
|
|
36
|
+
`@zanii/core`'s own `inclusionProof`/`verifyInclusion`, so they verify with the exact
|
|
37
|
+
primitive the ledger uses.
|
|
38
|
+
|
|
39
|
+
## The limit, stated up front
|
|
40
|
+
|
|
41
|
+
This proves what was **recorded in the manifest**, not what a GPU secretly saw. An
|
|
42
|
+
absence proof is only as honest as the manifest is complete — pair it with an attested
|
|
43
|
+
enclave (`@zanii/attest`: the published image hash is the only code that touched the
|
|
44
|
+
data) so "absent from the manifest" also means "absent from the run." The manifest
|
|
45
|
+
commits refs, never content.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/** One committed gap between two adjacent sorted refs. A member leaf has `lo === hi`. */
|
|
2
|
+
interface Pair {
|
|
3
|
+
lo: string;
|
|
4
|
+
hi: string;
|
|
5
|
+
}
|
|
6
|
+
export interface TrainingManifest {
|
|
7
|
+
v: 1;
|
|
8
|
+
type: 'training.manifest';
|
|
9
|
+
/** The model/run this set trained. */
|
|
10
|
+
run_id: string;
|
|
11
|
+
/** How many distinct member refs. */
|
|
12
|
+
size: number;
|
|
13
|
+
/** Merkle root over the sorted adjacency leaves — `sha256:<hex>`. */
|
|
14
|
+
root: string;
|
|
15
|
+
created_at: string;
|
|
16
|
+
}
|
|
17
|
+
/** Commit a training set (member refs) to a manifest. Refs are hashes/HMACs, never content. */
|
|
18
|
+
export declare function buildManifest(fields: {
|
|
19
|
+
runId: string;
|
|
20
|
+
refs: string[];
|
|
21
|
+
createdAt: string;
|
|
22
|
+
}): TrainingManifest;
|
|
23
|
+
export declare function manifestHash(m: TrainingManifest): string;
|
|
24
|
+
/** The payload to `record()` as `training.run` — anchors the manifest into the ledger. */
|
|
25
|
+
export declare function manifestReceipt(m: TrainingManifest): {
|
|
26
|
+
target: string;
|
|
27
|
+
payload: Record<string, unknown>;
|
|
28
|
+
};
|
|
29
|
+
export interface AbsenceProof {
|
|
30
|
+
v: 1;
|
|
31
|
+
type: 'training.absence';
|
|
32
|
+
run_id: string;
|
|
33
|
+
/** The ref proven absent. */
|
|
34
|
+
ref: string;
|
|
35
|
+
/** The covering gap `(lo, hi)` with `lo < ref < hi`. */
|
|
36
|
+
gap: Pair;
|
|
37
|
+
index: number;
|
|
38
|
+
size: number;
|
|
39
|
+
path: string[];
|
|
40
|
+
}
|
|
41
|
+
export interface MembershipProof {
|
|
42
|
+
v: 1;
|
|
43
|
+
type: 'training.membership';
|
|
44
|
+
run_id: string;
|
|
45
|
+
ref: string;
|
|
46
|
+
index: number;
|
|
47
|
+
size: number;
|
|
48
|
+
path: string[];
|
|
49
|
+
}
|
|
50
|
+
/** Build a proof that `ref` is ABSENT from the training set. Throws if it is present. */
|
|
51
|
+
export declare function proveAbsence(m: TrainingManifest, refs: string[], ref: string): AbsenceProof;
|
|
52
|
+
/** Build a proof that `ref` is PRESENT in the training set. Throws if absent. */
|
|
53
|
+
export declare function proveMembership(m: TrainingManifest, refs: string[], ref: string): MembershipProof;
|
|
54
|
+
/** Verify a subject-absence proof against a manifest — offline, no trust in the operator. */
|
|
55
|
+
export declare function verifyAbsence(proof: AbsenceProof | undefined, m: TrainingManifest): boolean;
|
|
56
|
+
/** Verify a membership proof against a manifest. */
|
|
57
|
+
export declare function verifyMembership(proof: MembershipProof | undefined, m: TrainingManifest): boolean;
|
|
58
|
+
export {};
|
|
59
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA4BA,yFAAyF;AACzF,UAAU,IAAI;IACZ,EAAE,EAAE,MAAM,CAAC;IACX,EAAE,EAAE,MAAM,CAAC;CACZ;AAED,MAAM,WAAW,gBAAgB;IAC/B,CAAC,EAAE,CAAC,CAAC;IACL,IAAI,EAAE,mBAAmB,CAAC;IAC1B,sCAAsC;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,qCAAqC;IACrC,IAAI,EAAE,MAAM,CAAC;IACb,qEAAqE;IACrE,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;CACpB;AAmBD,+FAA+F;AAC/F,wBAAgB,aAAa,CAAC,MAAM,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,EAAE,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG,gBAAgB,CAa5G;AAED,wBAAgB,YAAY,CAAC,CAAC,EAAE,gBAAgB,GAAG,MAAM,CAExD;AAED,0FAA0F;AAC1F,wBAAgB,eAAe,CAAC,CAAC,EAAE,gBAAgB,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAAE,CAEzG;AAQD,MAAM,WAAW,YAAY;IAC3B,CAAC,EAAE,CAAC,CAAC;IACL,IAAI,EAAE,kBAAkB,CAAC;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,6BAA6B;IAC7B,GAAG,EAAE,MAAM,CAAC;IACZ,wDAAwD;IACxD,GAAG,EAAE,IAAI,CAAC;IACV,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,EAAE,CAAC;CAChB;AAED,MAAM,WAAW,eAAe;IAC9B,CAAC,EAAE,CAAC,CAAC;IACL,IAAI,EAAE,qBAAqB,CAAC;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,EAAE,CAAC;CAChB;AAED,yFAAyF;AACzF,wBAAgB,YAAY,CAAC,CAAC,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,GAAG,YAAY,CAQ3F;AAED,iFAAiF;AACjF,wBAAgB,eAAe,CAAC,CAAC,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,GAAG,eAAe,CAMjG;AAED,6FAA6F;AAC7F,wBAAgB,aAAa,CAAC,KAAK,EAAE,YAAY,GAAG,SAAS,EAAE,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAO3F;AAED,oDAAoD;AACpD,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,eAAe,GAAG,SAAS,EAAE,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAKjG"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zanii/train — training-data provenance & subject-absence proofs.
|
|
3
|
+
*
|
|
4
|
+
* The claim buyers actually want is negative: *"my data was never used to train your
|
|
5
|
+
* model."* You cannot prove that about the whole world, but you CAN prove it about a
|
|
6
|
+
* declared training run: commit the training set to a **sorted-Merkle manifest**, then
|
|
7
|
+
* hand a data subject a **non-membership proof** that their `ref` is absent from it —
|
|
8
|
+
* checkable offline, with no trust in the model operator.
|
|
9
|
+
*
|
|
10
|
+
* How the absence proof works (sorted-set non-membership, the pattern the Zanii L1's
|
|
11
|
+
* SMT uses): sort the member refs, Merkle-commit the *adjacent pairs*. To prove `x`
|
|
12
|
+
* is absent, reveal the one pair `(lo, hi)` with `lo < x < hi` and its inclusion proof
|
|
13
|
+
* in the manifest — since the set is sorted and every gap is committed, a covering gap
|
|
14
|
+
* that verifies means nothing sits between `lo` and `hi`, so `x` is not a member.
|
|
15
|
+
* Membership is the same machinery: reveal the leaf whose `lo == x`.
|
|
16
|
+
*
|
|
17
|
+
* ## The limit, stated up front
|
|
18
|
+
*
|
|
19
|
+
* This proves what was **recorded** in the manifest, not what a GPU secretly saw. An
|
|
20
|
+
* absence proof is only as honest as the manifest is complete — pair it with an
|
|
21
|
+
* attested enclave (`@zanii/attest`: the published image hash is the only code that
|
|
22
|
+
* touched the data) so "absent from the manifest" also means "absent from the run".
|
|
23
|
+
* The manifest commits refs (`hmac:v1:…` / `sha256:…`), never content.
|
|
24
|
+
*/
|
|
25
|
+
import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
|
|
26
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
27
|
+
import { canonicalBytes, inclusionProof, jcsHash, leafHash, merkleRoot, verifyInclusion } from '@zanii/core';
|
|
28
|
+
/** Sorted, de-duplicated member refs → the adjacency leaves that get Merkle-committed. */
|
|
29
|
+
function pairsOf(refs) {
|
|
30
|
+
const sorted = [...new Set(refs)].sort();
|
|
31
|
+
if (sorted.length === 0)
|
|
32
|
+
return [];
|
|
33
|
+
const pairs = [];
|
|
34
|
+
// sentinel-bounded gaps so the smallest/largest members are covered too
|
|
35
|
+
pairs.push({ lo: '', hi: sorted[0] });
|
|
36
|
+
for (let i = 0; i < sorted.length; i++) {
|
|
37
|
+
pairs.push({ lo: sorted[i], hi: sorted[i] }); // the member itself (membership leaf)
|
|
38
|
+
const next = i + 1 < sorted.length ? sorted[i + 1] : '';
|
|
39
|
+
pairs.push({ lo: sorted[i], hi: next }); // the gap to the next member
|
|
40
|
+
}
|
|
41
|
+
return pairs;
|
|
42
|
+
}
|
|
43
|
+
const pairLeaf = (p) => leafHash(canonicalBytes(p));
|
|
44
|
+
/** Commit a training set (member refs) to a manifest. Refs are hashes/HMACs, never content. */
|
|
45
|
+
export function buildManifest(fields) {
|
|
46
|
+
if (!fields.runId)
|
|
47
|
+
throw new Error('runId is required');
|
|
48
|
+
if (Number.isNaN(Date.parse(fields.createdAt)))
|
|
49
|
+
throw new Error('createdAt must be an ISO timestamp');
|
|
50
|
+
const pairs = pairsOf(fields.refs);
|
|
51
|
+
const root = pairs.length ? bytesToHex(merkleRoot(pairs.map(pairLeaf))) : bytesToHex(sha256(new Uint8Array(0)));
|
|
52
|
+
return {
|
|
53
|
+
v: 1,
|
|
54
|
+
type: 'training.manifest',
|
|
55
|
+
run_id: fields.runId,
|
|
56
|
+
size: new Set(fields.refs).size,
|
|
57
|
+
root: `sha256:${root}`,
|
|
58
|
+
created_at: fields.createdAt,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export function manifestHash(m) {
|
|
62
|
+
return jcsHash(m);
|
|
63
|
+
}
|
|
64
|
+
/** The payload to `record()` as `training.run` — anchors the manifest into the ledger. */
|
|
65
|
+
export function manifestReceipt(m) {
|
|
66
|
+
return { target: 'training.run', payload: { manifest: m, manifest_hash: manifestHash(m) } };
|
|
67
|
+
}
|
|
68
|
+
// Proof paths use core's own inclusionProof/verifyInclusion — same RFC 6962
|
|
69
|
+
// construction as the ledger, so a proof here verifies with the same primitive.
|
|
70
|
+
const inclusionPath = (index, leaves) => inclusionProof(index, leaves).map(bytesToHex);
|
|
71
|
+
const verifyPath = (leaf, index, size, path, root) => verifyInclusion(leaf, index, size, path.map(hexToBytes), root);
|
|
72
|
+
/** Build a proof that `ref` is ABSENT from the training set. Throws if it is present. */
|
|
73
|
+
export function proveAbsence(m, refs, ref) {
|
|
74
|
+
const sorted = [...new Set(refs)].sort();
|
|
75
|
+
if (sorted.includes(ref))
|
|
76
|
+
throw new Error(`ref is PRESENT — cannot prove absence`);
|
|
77
|
+
const pairs = pairsOf(refs);
|
|
78
|
+
const leaves = pairs.map(pairLeaf);
|
|
79
|
+
const gi = pairs.findIndex((p) => p.lo < ref && ref < p.hi && !(p.lo === p.hi));
|
|
80
|
+
if (gi < 0)
|
|
81
|
+
throw new Error('no covering gap — manifest malformed');
|
|
82
|
+
return { v: 1, type: 'training.absence', run_id: m.run_id, ref, gap: pairs[gi], index: gi, size: leaves.length, path: inclusionPath(gi, leaves) };
|
|
83
|
+
}
|
|
84
|
+
/** Build a proof that `ref` is PRESENT in the training set. Throws if absent. */
|
|
85
|
+
export function proveMembership(m, refs, ref) {
|
|
86
|
+
const pairs = pairsOf(refs);
|
|
87
|
+
const leaves = pairs.map(pairLeaf);
|
|
88
|
+
const li = pairs.findIndex((p) => p.lo === ref && p.hi === ref);
|
|
89
|
+
if (li < 0)
|
|
90
|
+
throw new Error('ref is ABSENT — cannot prove membership');
|
|
91
|
+
return { v: 1, type: 'training.membership', run_id: m.run_id, ref, index: li, size: leaves.length, path: inclusionPath(li, leaves) };
|
|
92
|
+
}
|
|
93
|
+
/** Verify a subject-absence proof against a manifest — offline, no trust in the operator. */
|
|
94
|
+
export function verifyAbsence(proof, m) {
|
|
95
|
+
const p = proof ?? {};
|
|
96
|
+
if (p.v !== 1 || p.type !== 'training.absence' || p.run_id !== m.run_id)
|
|
97
|
+
return false;
|
|
98
|
+
if (!(p.gap.lo < p.ref && p.ref < p.gap.hi))
|
|
99
|
+
return false; // the gap must actually cover the ref
|
|
100
|
+
if (p.gap.lo === p.gap.hi)
|
|
101
|
+
return false; // a member leaf is not a gap
|
|
102
|
+
const root = hexToBytes(m.root.replace('sha256:', ''));
|
|
103
|
+
return verifyPath(pairLeaf(p.gap), p.index, p.size, p.path, root);
|
|
104
|
+
}
|
|
105
|
+
/** Verify a membership proof against a manifest. */
|
|
106
|
+
export function verifyMembership(proof, m) {
|
|
107
|
+
const p = proof ?? {};
|
|
108
|
+
if (p.v !== 1 || p.type !== 'training.membership' || p.run_id !== m.run_id)
|
|
109
|
+
return false;
|
|
110
|
+
const root = hexToBytes(m.root.replace('sha256:', ''));
|
|
111
|
+
return verifyPath(pairLeaf({ lo: p.ref, hi: p.ref }), p.index, p.size, p.path, root);
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAoB7G,0FAA0F;AAC1F,SAAS,OAAO,CAAC,IAAc;IAC7B,MAAM,MAAM,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IACzC,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IACnC,MAAM,KAAK,GAAW,EAAE,CAAC;IACzB,wEAAwE;IACxE,KAAK,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,CAAE,EAAE,CAAC,CAAC;IACvC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACvC,KAAK,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,CAAE,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,CAAE,EAAE,CAAC,CAAC,CAAC,sCAAsC;QACtF,MAAM,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAE,CAAC,CAAC,CAAC,GAAG,CAA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CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zanii/train",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Training-data provenance: hash a training set into a sorted-Merkle manifest, then PROVE a named subject's data was absent from a real training run - a non-membership proof anyone can check offline. 'It was never used to train a model' becomes a yes, not a promise. Proves what was recorded went in; cannot prove an off-manifest copy didn't (state the enclave boundary).",
|
|
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/hashes": "^1.7.1",
|
|
35
|
+
"@zanii/core": "0.4.0"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsc -p tsconfig.build.json",
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"typecheck": "tsc --noEmit"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zanii/train — training-data provenance & subject-absence proofs.
|
|
3
|
+
*
|
|
4
|
+
* The claim buyers actually want is negative: *"my data was never used to train your
|
|
5
|
+
* model."* You cannot prove that about the whole world, but you CAN prove it about a
|
|
6
|
+
* declared training run: commit the training set to a **sorted-Merkle manifest**, then
|
|
7
|
+
* hand a data subject a **non-membership proof** that their `ref` is absent from it —
|
|
8
|
+
* checkable offline, with no trust in the model operator.
|
|
9
|
+
*
|
|
10
|
+
* How the absence proof works (sorted-set non-membership, the pattern the Zanii L1's
|
|
11
|
+
* SMT uses): sort the member refs, Merkle-commit the *adjacent pairs*. To prove `x`
|
|
12
|
+
* is absent, reveal the one pair `(lo, hi)` with `lo < x < hi` and its inclusion proof
|
|
13
|
+
* in the manifest — since the set is sorted and every gap is committed, a covering gap
|
|
14
|
+
* that verifies means nothing sits between `lo` and `hi`, so `x` is not a member.
|
|
15
|
+
* Membership is the same machinery: reveal the leaf whose `lo == x`.
|
|
16
|
+
*
|
|
17
|
+
* ## The limit, stated up front
|
|
18
|
+
*
|
|
19
|
+
* This proves what was **recorded** in the manifest, not what a GPU secretly saw. An
|
|
20
|
+
* absence proof is only as honest as the manifest is complete — pair it with an
|
|
21
|
+
* attested enclave (`@zanii/attest`: the published image hash is the only code that
|
|
22
|
+
* touched the data) so "absent from the manifest" also means "absent from the run".
|
|
23
|
+
* The manifest commits refs (`hmac:v1:…` / `sha256:…`), never content.
|
|
24
|
+
*/
|
|
25
|
+
import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
|
|
26
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
27
|
+
import { canonicalBytes, inclusionProof, jcsHash, leafHash, merkleRoot, verifyInclusion } from '@zanii/core';
|
|
28
|
+
|
|
29
|
+
/** One committed gap between two adjacent sorted refs. A member leaf has `lo === hi`. */
|
|
30
|
+
interface Pair {
|
|
31
|
+
lo: string;
|
|
32
|
+
hi: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface TrainingManifest {
|
|
36
|
+
v: 1;
|
|
37
|
+
type: 'training.manifest';
|
|
38
|
+
/** The model/run this set trained. */
|
|
39
|
+
run_id: string;
|
|
40
|
+
/** How many distinct member refs. */
|
|
41
|
+
size: number;
|
|
42
|
+
/** Merkle root over the sorted adjacency leaves — `sha256:<hex>`. */
|
|
43
|
+
root: string;
|
|
44
|
+
created_at: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Sorted, de-duplicated member refs → the adjacency leaves that get Merkle-committed. */
|
|
48
|
+
function pairsOf(refs: string[]): Pair[] {
|
|
49
|
+
const sorted = [...new Set(refs)].sort();
|
|
50
|
+
if (sorted.length === 0) return [];
|
|
51
|
+
const pairs: Pair[] = [];
|
|
52
|
+
// sentinel-bounded gaps so the smallest/largest members are covered too
|
|
53
|
+
pairs.push({ lo: '', hi: sorted[0]! });
|
|
54
|
+
for (let i = 0; i < sorted.length; i++) {
|
|
55
|
+
pairs.push({ lo: sorted[i]!, hi: sorted[i]! }); // the member itself (membership leaf)
|
|
56
|
+
const next = i + 1 < sorted.length ? sorted[i + 1]! : '';
|
|
57
|
+
pairs.push({ lo: sorted[i]!, hi: next }); // the gap to the next member
|
|
58
|
+
}
|
|
59
|
+
return pairs;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const pairLeaf = (p: Pair): Uint8Array => leafHash(canonicalBytes(p as unknown as Record<string, unknown>));
|
|
63
|
+
|
|
64
|
+
/** Commit a training set (member refs) to a manifest. Refs are hashes/HMACs, never content. */
|
|
65
|
+
export function buildManifest(fields: { runId: string; refs: string[]; createdAt: string }): TrainingManifest {
|
|
66
|
+
if (!fields.runId) throw new Error('runId is required');
|
|
67
|
+
if (Number.isNaN(Date.parse(fields.createdAt))) throw new Error('createdAt must be an ISO timestamp');
|
|
68
|
+
const pairs = pairsOf(fields.refs);
|
|
69
|
+
const root = pairs.length ? bytesToHex(merkleRoot(pairs.map(pairLeaf))) : bytesToHex(sha256(new Uint8Array(0)));
|
|
70
|
+
return {
|
|
71
|
+
v: 1,
|
|
72
|
+
type: 'training.manifest',
|
|
73
|
+
run_id: fields.runId,
|
|
74
|
+
size: new Set(fields.refs).size,
|
|
75
|
+
root: `sha256:${root}`,
|
|
76
|
+
created_at: fields.createdAt,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function manifestHash(m: TrainingManifest): string {
|
|
81
|
+
return jcsHash(m as unknown as Record<string, unknown>);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The payload to `record()` as `training.run` — anchors the manifest into the ledger. */
|
|
85
|
+
export function manifestReceipt(m: TrainingManifest): { target: string; payload: Record<string, unknown> } {
|
|
86
|
+
return { target: 'training.run', payload: { manifest: m, manifest_hash: manifestHash(m) } };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Proof paths use core's own inclusionProof/verifyInclusion — same RFC 6962
|
|
90
|
+
// construction as the ledger, so a proof here verifies with the same primitive.
|
|
91
|
+
const inclusionPath = (index: number, leaves: Uint8Array[]): string[] => inclusionProof(index, leaves).map(bytesToHex);
|
|
92
|
+
const verifyPath = (leaf: Uint8Array, index: number, size: number, path: string[], root: Uint8Array): boolean =>
|
|
93
|
+
verifyInclusion(leaf, index, size, path.map(hexToBytes), root);
|
|
94
|
+
|
|
95
|
+
export interface AbsenceProof {
|
|
96
|
+
v: 1;
|
|
97
|
+
type: 'training.absence';
|
|
98
|
+
run_id: string;
|
|
99
|
+
/** The ref proven absent. */
|
|
100
|
+
ref: string;
|
|
101
|
+
/** The covering gap `(lo, hi)` with `lo < ref < hi`. */
|
|
102
|
+
gap: Pair;
|
|
103
|
+
index: number;
|
|
104
|
+
size: number;
|
|
105
|
+
path: string[];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface MembershipProof {
|
|
109
|
+
v: 1;
|
|
110
|
+
type: 'training.membership';
|
|
111
|
+
run_id: string;
|
|
112
|
+
ref: string;
|
|
113
|
+
index: number;
|
|
114
|
+
size: number;
|
|
115
|
+
path: string[];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Build a proof that `ref` is ABSENT from the training set. Throws if it is present. */
|
|
119
|
+
export function proveAbsence(m: TrainingManifest, refs: string[], ref: string): AbsenceProof {
|
|
120
|
+
const sorted = [...new Set(refs)].sort();
|
|
121
|
+
if (sorted.includes(ref)) throw new Error(`ref is PRESENT — cannot prove absence`);
|
|
122
|
+
const pairs = pairsOf(refs);
|
|
123
|
+
const leaves = pairs.map(pairLeaf);
|
|
124
|
+
const gi = pairs.findIndex((p) => p.lo < ref && ref < p.hi && !(p.lo === p.hi));
|
|
125
|
+
if (gi < 0) throw new Error('no covering gap — manifest malformed');
|
|
126
|
+
return { v: 1, type: 'training.absence', run_id: m.run_id, ref, gap: pairs[gi]!, index: gi, size: leaves.length, path: inclusionPath(gi, leaves) };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Build a proof that `ref` is PRESENT in the training set. Throws if absent. */
|
|
130
|
+
export function proveMembership(m: TrainingManifest, refs: string[], ref: string): MembershipProof {
|
|
131
|
+
const pairs = pairsOf(refs);
|
|
132
|
+
const leaves = pairs.map(pairLeaf);
|
|
133
|
+
const li = pairs.findIndex((p) => p.lo === ref && p.hi === ref);
|
|
134
|
+
if (li < 0) throw new Error('ref is ABSENT — cannot prove membership');
|
|
135
|
+
return { v: 1, type: 'training.membership', run_id: m.run_id, ref, index: li, size: leaves.length, path: inclusionPath(li, leaves) };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Verify a subject-absence proof against a manifest — offline, no trust in the operator. */
|
|
139
|
+
export function verifyAbsence(proof: AbsenceProof | undefined, m: TrainingManifest): boolean {
|
|
140
|
+
const p = proof ?? ({} as AbsenceProof);
|
|
141
|
+
if (p.v !== 1 || p.type !== 'training.absence' || p.run_id !== m.run_id) return false;
|
|
142
|
+
if (!(p.gap.lo < p.ref && p.ref < p.gap.hi)) return false; // the gap must actually cover the ref
|
|
143
|
+
if (p.gap.lo === p.gap.hi) return false; // a member leaf is not a gap
|
|
144
|
+
const root = hexToBytes(m.root.replace('sha256:', ''));
|
|
145
|
+
return verifyPath(pairLeaf(p.gap), p.index, p.size, p.path, root);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Verify a membership proof against a manifest. */
|
|
149
|
+
export function verifyMembership(proof: MembershipProof | undefined, m: TrainingManifest): boolean {
|
|
150
|
+
const p = proof ?? ({} as MembershipProof);
|
|
151
|
+
if (p.v !== 1 || p.type !== 'training.membership' || p.run_id !== m.run_id) return false;
|
|
152
|
+
const root = hexToBytes(m.root.replace('sha256:', ''));
|
|
153
|
+
return verifyPath(pairLeaf({ lo: p.ref, hi: p.ref }), p.index, p.size, p.path, root);
|
|
154
|
+
}
|