@coordination-network/revocation-registry 0.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.
- package/LICENSE +21 -0
- package/README.md +147 -0
- package/dist/index.cjs +463 -0
- package/dist/index.js +454 -0
- package/package.json +51 -0
- package/src/constants.ts +39 -0
- package/src/hashes.ts +22 -0
- package/src/index.ts +5 -0
- package/src/revocation-registry.ts +396 -0
- package/src/types.ts +22 -0
- package/src/utils.ts +55 -0
- package/types/index.d.ts +351 -0
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import { LeanIMTPlus } from "@zk-kit/lean-imt-plus"
|
|
2
|
+
import { PROOF_TYPE, SENTINEL_INDEX, SNAPSHOT_VERSION, TAG_LEAF } from "./constants"
|
|
3
|
+
import { revocationHashes } from "./hashes"
|
|
4
|
+
import type { BigNumberish, RevocationLeaf, RevocationProof } from "./types"
|
|
5
|
+
import { toCommitment, toFieldElement } from "./utils"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A revocation registry: a
|
|
9
|
+
* {@link https://pse.dev/blog/lean-imt-plus-efficient-merkle-tree-for-membership-and-non-membership-proofs | LeanIMT+}
|
|
10
|
+
* whose leaves are the revocation commitments of revoked credentials.
|
|
11
|
+
*
|
|
12
|
+
* The class is a self-contained implementation of the structure, not a client
|
|
13
|
+
* for any particular backend. It can hold a registry outright, which is what a
|
|
14
|
+
* service that publishes revocations from its own database does, or mirror one
|
|
15
|
+
* whose authoritative copy lives elsewhere, which is what a holder does before
|
|
16
|
+
* building a proof. Nothing here reads or writes anything external.
|
|
17
|
+
*
|
|
18
|
+
* Two operations serve the two roles:
|
|
19
|
+
*
|
|
20
|
+
* - **Issuer.** {@link revoke} inserts a commitment. Where the authoritative
|
|
21
|
+
* registry is a separate system that validates a predecessor index rather than
|
|
22
|
+
* searching for one, {@link lowLeafIndexOf} computes that index.
|
|
23
|
+
* - **Holder.** {@link generateProof} produces a proof about a commitment. A
|
|
24
|
+
* non-membership proof (`proofType: 1`) is what shows a credential is not
|
|
25
|
+
* revoked, and is the witness a zero-knowledge circuit consumes to show it
|
|
26
|
+
* without revealing the commitment.
|
|
27
|
+
*
|
|
28
|
+
* When this instance is a mirror rather than the authority, it is trusted for
|
|
29
|
+
* availability only, never for integrity: compare {@link root} against the root
|
|
30
|
+
* the authority reports before using any proof built from it. Building the tree
|
|
31
|
+
* locally is also what keeps a holder private, since they read the whole public
|
|
32
|
+
* registry and never disclose which commitment they care about.
|
|
33
|
+
*
|
|
34
|
+
* An empty registry has a root of `0` and cannot produce proofs: nothing has
|
|
35
|
+
* been revoked yet, so there is nothing to prove against. Verifiers must handle
|
|
36
|
+
* that case as "no revocations", not as a failure.
|
|
37
|
+
*/
|
|
38
|
+
export class RevocationRegistry {
|
|
39
|
+
/** The underlying LeanIMT+ instance. */
|
|
40
|
+
public leanIMTPlus: LeanIMTPlus<bigint>
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Creates a registry, optionally pre-loaded with revoked commitments.
|
|
44
|
+
* Passing the full list is more efficient than revoking them one by one,
|
|
45
|
+
* because each affected internal node is rehashed at most once.
|
|
46
|
+
*
|
|
47
|
+
* The tree is position-addressable, so a different insertion order produces
|
|
48
|
+
* a different root even for the same set of commitments. When rebuilding a
|
|
49
|
+
* registry that already exists elsewhere, the commitments must be supplied
|
|
50
|
+
* in the order that registry inserted them.
|
|
51
|
+
*
|
|
52
|
+
* This reconstructs a registry only while nothing has ever been un-revoked.
|
|
53
|
+
* An un-revocation leaves a tombstoned slot behind, and that slot still
|
|
54
|
+
* occupies a Merkle position, so replaying the surviving commitments would
|
|
55
|
+
* produce a different root. Use {@link RevocationRegistry.fromLeaves} or
|
|
56
|
+
* {@link RevocationRegistry.import} for a registry that may have tombstones.
|
|
57
|
+
*
|
|
58
|
+
* @param commitments Revoked commitments, in insertion order.
|
|
59
|
+
*/
|
|
60
|
+
constructor(commitments: BigNumberish[] = []) {
|
|
61
|
+
this.leanIMTPlus = new LeanIMTPlus(revocationHashes, commitments.map(toCommitment))
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The current revocation root. `0` while the registry is empty. */
|
|
65
|
+
public get root(): bigint {
|
|
66
|
+
return this.leanIMTPlus.root ?? 0n
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The current tree depth. */
|
|
70
|
+
public get depth(): number {
|
|
71
|
+
return this.leanIMTPlus.depth
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The number of revoked commitments, excluding the sentinel and tombstones. */
|
|
75
|
+
public get size(): number {
|
|
76
|
+
return this.leanIMTPlus.size
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The revoked commitments in insertion order. Reading `.value` off each leaf
|
|
81
|
+
* gives the commitments; `.nextValue` exposes the sorted linked list the
|
|
82
|
+
* non-membership proof relies on.
|
|
83
|
+
*/
|
|
84
|
+
public get leaves(): RevocationLeaf[] {
|
|
85
|
+
return this.leanIMTPlus.leaves
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The revoked commitments, in insertion order. Excludes the sentinel and any
|
|
90
|
+
* tombstones, so this is a lossy view of the tree: it round-trips through the
|
|
91
|
+
* constructor only while nothing has been un-revoked. See
|
|
92
|
+
* {@link leafSlots}.
|
|
93
|
+
*/
|
|
94
|
+
public get commitments(): bigint[] {
|
|
95
|
+
return this.leanIMTPlus.leaves.map((leaf) => leaf.value)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Every physical leaf slot in index order, sentinel and tombstones included.
|
|
100
|
+
*
|
|
101
|
+
* This is the lossless view of the registry and the counterpart of
|
|
102
|
+
* {@link RevocationRegistry.fromLeaves}: an authority publishes these slots,
|
|
103
|
+
* and anyone can rebuild the exact same tree from them. It mirrors what the
|
|
104
|
+
* on-chain registry exposes through `getLeaf`.
|
|
105
|
+
*
|
|
106
|
+
* Prefer {@link commitments} to enumerate what is revoked. This walks the
|
|
107
|
+
* whole tree, so treat it as a publishing operation rather than a hot path.
|
|
108
|
+
*/
|
|
109
|
+
public get leafSlots(): RevocationLeaf[] {
|
|
110
|
+
const { leaves } = JSON.parse(this.export()) as { leaves: { value: string; nextValue: string }[] }
|
|
111
|
+
|
|
112
|
+
return leaves.map(({ value, nextValue }) => ({ value: BigInt(value), nextValue: BigInt(nextValue) }))
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Returns true if the commitment has been revoked. */
|
|
116
|
+
public isRevoked(commitment: BigNumberish): boolean {
|
|
117
|
+
return this.leanIMTPlus.has(toCommitment(commitment))
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Returns the physical leaf index of a revoked commitment, or `-1` if it is
|
|
122
|
+
* not revoked.
|
|
123
|
+
*
|
|
124
|
+
* This is a slot in the tree, never a position in {@link commitments}. The
|
|
125
|
+
* two always differ by at least one, because slot 0 is the sentinel, and
|
|
126
|
+
* they drift further apart with every tombstone.
|
|
127
|
+
*/
|
|
128
|
+
public indexOf(commitment: BigNumberish): number {
|
|
129
|
+
return this.leanIMTPlus.indexOf(toCommitment(commitment))
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Revokes a credential by inserting its commitment.
|
|
134
|
+
*
|
|
135
|
+
* @param commitment The revocation commitment of the credential.
|
|
136
|
+
*/
|
|
137
|
+
public revoke(commitment: BigNumberish): void {
|
|
138
|
+
this.leanIMTPlus.insert(toCommitment(commitment))
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Revokes several credentials in one pass. Cheaper than calling
|
|
143
|
+
* {@link revoke} once per commitment, because each affected internal node is
|
|
144
|
+
* rehashed at most once.
|
|
145
|
+
*
|
|
146
|
+
* Not transactional: if a commitment mid-batch is rejected, the earlier ones
|
|
147
|
+
* have already been applied. Validate with {@link isRevoked} first when the
|
|
148
|
+
* input is untrusted.
|
|
149
|
+
*
|
|
150
|
+
* @param commitments The revocation commitments, in insertion order.
|
|
151
|
+
*/
|
|
152
|
+
public revokeMany(commitments: BigNumberish[]): void {
|
|
153
|
+
this.leanIMTPlus.insertMany(commitments.map(toCommitment))
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Un-revokes a credential, tombstoning its slot.
|
|
158
|
+
*
|
|
159
|
+
* Revocation is normally permanent, and a registry that never un-revokes is
|
|
160
|
+
* the simpler thing to reason about: an un-revoked credential becomes valid
|
|
161
|
+
* again for every verifier that has not yet seen the new root. The operation
|
|
162
|
+
* is exposed because the underlying tree supports it and some issuers need
|
|
163
|
+
* to undo a mistaken revocation, but reach for it deliberately.
|
|
164
|
+
*
|
|
165
|
+
* It also leaves a tombstoned slot behind, after which the registry can no
|
|
166
|
+
* longer be rebuilt from {@link commitments} alone. Publish the leaf slots
|
|
167
|
+
* (see {@link RevocationRegistry.fromLeaves}) or a snapshot instead.
|
|
168
|
+
*
|
|
169
|
+
* @param commitment The revocation commitment to un-revoke.
|
|
170
|
+
*/
|
|
171
|
+
public unrevoke(commitment: BigNumberish): void {
|
|
172
|
+
this.leanIMTPlus.remove(toCommitment(commitment))
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Replaces a revoked commitment with another one, in place, reusing the old
|
|
177
|
+
* physical slot. Cheaper than an {@link unrevoke} followed by a
|
|
178
|
+
* {@link revoke}, which would tombstone a slot and append a new one.
|
|
179
|
+
*
|
|
180
|
+
* @param oldCommitment The revoked commitment to replace.
|
|
181
|
+
* @param newCommitment The commitment to put in its place.
|
|
182
|
+
*/
|
|
183
|
+
public updateCommitment(oldCommitment: BigNumberish, newCommitment: BigNumberish): void {
|
|
184
|
+
this.leanIMTPlus.update(toCommitment(oldCommitment), toCommitment(newCommitment))
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Generates a proof for a commitment: a non-membership proof
|
|
189
|
+
* (`proofType: 1`) if the credential is not revoked, a membership proof
|
|
190
|
+
* (`proofType: 0`) if it is.
|
|
191
|
+
*
|
|
192
|
+
* The proof carries the root it was built against. It proves nothing until
|
|
193
|
+
* the verifier independently accepts that root as one the authoritative
|
|
194
|
+
* registry currently publishes.
|
|
195
|
+
*
|
|
196
|
+
* @param commitment The revocation commitment of the credential.
|
|
197
|
+
* @returns The revocation proof.
|
|
198
|
+
*/
|
|
199
|
+
public generateProof(commitment: BigNumberish): RevocationProof {
|
|
200
|
+
if (this.root === 0n) {
|
|
201
|
+
throw new Error("RevocationRegistry: cannot generate a proof against an empty registry")
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return this.leanIMTPlus.generateProof(toCommitment(commitment))
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Verifies a proof against this tree's current root. Use the static
|
|
209
|
+
* {@link RevocationRegistry.verifyProof} to check a proof without a local tree.
|
|
210
|
+
*/
|
|
211
|
+
public verifyProof(proof: RevocationProof): boolean {
|
|
212
|
+
return proof.root === this.root && this.leanIMTPlus.verifyProof(proof)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Verifies that a proof is internally consistent with the root it carries.
|
|
217
|
+
*
|
|
218
|
+
* This does not bind the proof to any registry: the caller supplies
|
|
219
|
+
* `proof.root`, so on its own this returns true for a proof against an
|
|
220
|
+
* attacker-chosen root. Always compare `proof.root` against a root you
|
|
221
|
+
* independently accept.
|
|
222
|
+
*/
|
|
223
|
+
public static verifyProof(proof: RevocationProof): boolean {
|
|
224
|
+
return LeanIMTPlus.verifyProof(proof, revocationHashes)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Returns the physical index of the low leaf of a commitment: the
|
|
229
|
+
* predecessor a constrained implementation needs supplied rather than
|
|
230
|
+
* searched for.
|
|
231
|
+
*
|
|
232
|
+
* {@link revoke} does not need this, since it finds the predecessor itself.
|
|
233
|
+
* It exists for registries whose authoritative implementation validates a
|
|
234
|
+
* caller-supplied index in O(1) instead of searching, which is how a version
|
|
235
|
+
* that has to bound its own cost per write is built. Such an index is only
|
|
236
|
+
* valid against the tree state the write applies to, so if another revocation
|
|
237
|
+
* lands first it must be recomputed against the new state.
|
|
238
|
+
*
|
|
239
|
+
* @param commitment The commitment about to be revoked.
|
|
240
|
+
* @returns The physical index of its predecessor, or `0` for the first
|
|
241
|
+
* revocation, where the argument is ignored and the sentinel is created.
|
|
242
|
+
*/
|
|
243
|
+
public lowLeafIndexOf(commitment: BigNumberish): number {
|
|
244
|
+
const value = toCommitment(commitment)
|
|
245
|
+
|
|
246
|
+
if (this.root === 0n) {
|
|
247
|
+
return SENTINEL_INDEX
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const proof = this.leanIMTPlus.generateProof(value)
|
|
251
|
+
|
|
252
|
+
if (proof.proofType === PROOF_TYPE.MEMBERSHIP) {
|
|
253
|
+
throw new Error("RevocationRegistry: the commitment has already been revoked")
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Not `proof.leafIndex`: that is the compacted Merkle path, which skips the
|
|
257
|
+
// levels where the leaf's ancestor had no sibling, so it does not equal the
|
|
258
|
+
// physical slot. Resolve the slot from the low leaf's value instead. A low
|
|
259
|
+
// leaf holding zero is the sentinel, which always sits at index 0.
|
|
260
|
+
return proof.leaf.value === 0n ? SENTINEL_INDEX : this.leanIMTPlus.indexOf(proof.leaf.value)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Returns the low leaf indices for a batch of commitments, as a batched
|
|
265
|
+
* write expects them: index `i` is computed against the tree state after
|
|
266
|
+
* commitments `0..i-1` have been inserted.
|
|
267
|
+
*
|
|
268
|
+
* Computed on a throwaway copy, so this tree is left untouched.
|
|
269
|
+
*
|
|
270
|
+
* @param commitments The commitments about to be revoked, in insertion order.
|
|
271
|
+
*/
|
|
272
|
+
public lowLeafIndicesOf(commitments: BigNumberish[]): number[] {
|
|
273
|
+
const scratch = RevocationRegistry.import(this.export(), false)
|
|
274
|
+
|
|
275
|
+
return commitments.map((commitment) => {
|
|
276
|
+
const index = scratch.lowLeafIndexOf(commitment)
|
|
277
|
+
scratch.revoke(commitment)
|
|
278
|
+
return index
|
|
279
|
+
})
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Returns the physical index of the leaf whose `nextValue` is this
|
|
284
|
+
* commitment. The counterpart of {@link lowLeafIndexOf} for un-revoking and
|
|
285
|
+
* for replacing a commitment.
|
|
286
|
+
*
|
|
287
|
+
* Returns `0` when the commitment is the smallest in the registry, because
|
|
288
|
+
* the sentinel at index 0 is then its predecessor.
|
|
289
|
+
*
|
|
290
|
+
* @param commitment A revoked commitment.
|
|
291
|
+
*/
|
|
292
|
+
public predecessorIndexOf(commitment: BigNumberish): number {
|
|
293
|
+
const value = toCommitment(commitment)
|
|
294
|
+
|
|
295
|
+
if (!this.leanIMTPlus.has(value)) {
|
|
296
|
+
throw new Error("RevocationRegistry: the commitment has not been revoked")
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const predecessor = this.leanIMTPlus.leaves.find((leaf) => leaf.nextValue === value)
|
|
300
|
+
|
|
301
|
+
return predecessor === undefined ? SENTINEL_INDEX : this.leanIMTPlus.indexOf(predecessor.value)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Serializes the whole tree, hashes included.
|
|
306
|
+
*
|
|
307
|
+
* This is the snapshot format: publishing a snapshot lets a holder rebuild a
|
|
308
|
+
* large registry without replaying every revocation since genesis. A snapshot
|
|
309
|
+
* is trusted for availability only, so an importing client must still compare
|
|
310
|
+
* {@link root} against the root the authoritative registry publishes.
|
|
311
|
+
*/
|
|
312
|
+
public export(): string {
|
|
313
|
+
return this.leanIMTPlus.export()
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Rebuilds a tree from {@link export} output.
|
|
318
|
+
*
|
|
319
|
+
* @param snapshot The serialized tree.
|
|
320
|
+
* @param validate Recompute every commitment and check it against the
|
|
321
|
+
* serialized hashes, rejecting a tampered snapshot. Defaults to `true`; pass
|
|
322
|
+
* `false` only for data you produced yourself.
|
|
323
|
+
*/
|
|
324
|
+
public static import(snapshot: string, validate = true): RevocationRegistry {
|
|
325
|
+
const tree = new RevocationRegistry()
|
|
326
|
+
|
|
327
|
+
tree.leanIMTPlus = LeanIMTPlus.import(revocationHashes, snapshot, { validate })
|
|
328
|
+
|
|
329
|
+
return tree
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Rebuilds a registry from its full physical leaf slots, sentinel and
|
|
334
|
+
* tombstones included.
|
|
335
|
+
*
|
|
336
|
+
* This is the reconstruction path that always works. The constructor takes
|
|
337
|
+
* only the active commitments, which is enough only while no commitment has
|
|
338
|
+
* ever been un-revoked: an un-revocation leaves a tombstoned slot behind, and
|
|
339
|
+
* a slot still occupies a Merkle position, so a registry that has one cannot
|
|
340
|
+
* be reproduced from its commitments alone.
|
|
341
|
+
*
|
|
342
|
+
* The leaf slots are exactly what an authoritative registry can publish
|
|
343
|
+
* verbatim: {@link leafSlots} here, or `getLeaf(registryId, i)` for `i` in
|
|
344
|
+
* `0..getRevocationTreeLeafCount(registryId) - 1` on chain.
|
|
345
|
+
*
|
|
346
|
+
* Hashes are recomputed here rather than taken on trust, so unlike a snapshot
|
|
347
|
+
* the input carries no node data to tamper with. The caller must still
|
|
348
|
+
* compare {@link root} against the root the authority publishes, which is
|
|
349
|
+
* what detects missing or reordered slots.
|
|
350
|
+
*
|
|
351
|
+
* @param leaves The physical leaf slots, in index order starting at the
|
|
352
|
+
* sentinel.
|
|
353
|
+
* @returns The reconstructed registry.
|
|
354
|
+
*/
|
|
355
|
+
public static fromLeaves(leaves: { value: BigNumberish; nextValue: BigNumberish }[]): RevocationRegistry {
|
|
356
|
+
if (leaves.length === 0) {
|
|
357
|
+
return new RevocationRegistry()
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const records = leaves.map(({ value, nextValue }) => ({
|
|
361
|
+
value: toFieldElement(value),
|
|
362
|
+
nextValue: toFieldElement(nextValue)
|
|
363
|
+
}))
|
|
364
|
+
|
|
365
|
+
if (records[0].value !== 0n) {
|
|
366
|
+
throw new Error("RevocationRegistry: the leaf at index 0 must be the sentinel, whose value is zero")
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// Rebuild every level of the LeanIMT, promoting an unpaired node unchanged.
|
|
370
|
+
const nodes: bigint[][] = [records.map((leaf) => revocationHashes.leaf(leaf.value, leaf.nextValue, TAG_LEAF))]
|
|
371
|
+
|
|
372
|
+
while (nodes[nodes.length - 1].length > 1) {
|
|
373
|
+
const level = nodes[nodes.length - 1]
|
|
374
|
+
const parents: bigint[] = []
|
|
375
|
+
|
|
376
|
+
for (let i = 0; i < level.length; i += 2) {
|
|
377
|
+
parents.push(i + 1 < level.length ? revocationHashes.internal(level[i], level[i + 1]) : level[i])
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
nodes.push(parents)
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const snapshot = JSON.stringify({
|
|
384
|
+
version: SNAPSHOT_VERSION,
|
|
385
|
+
nodes: nodes.map((level) => level.map(String)),
|
|
386
|
+
leaves: records.map(({ value, nextValue }) => ({
|
|
387
|
+
value: value.toString(),
|
|
388
|
+
nextValue: nextValue.toString()
|
|
389
|
+
}))
|
|
390
|
+
})
|
|
391
|
+
|
|
392
|
+
// The node hashes were just derived from the leaves, so re-validating them
|
|
393
|
+
// would be circular. Integrity comes from comparing the root to the authority.
|
|
394
|
+
return RevocationRegistry.import(snapshot, false)
|
|
395
|
+
}
|
|
396
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { LeanIMTPlusLeaf, LeanIMTPlusProof } from "@zk-kit/lean-imt-plus"
|
|
2
|
+
|
|
3
|
+
/** Anything that can be coerced to a field element with `BigInt()`. */
|
|
4
|
+
export type BigNumberish = bigint | number | string
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A revocation proof. Structurally a LeanIMT+ proof: a Merkle proof of a single
|
|
8
|
+
* indexed leaf plus a `proofType` that says which extra check the verifier runs.
|
|
9
|
+
*
|
|
10
|
+
* - `proofType: 0` (membership): `leaf.value === value`, so the commitment is in
|
|
11
|
+
* the registry and the credential is revoked.
|
|
12
|
+
* - `proofType: 1` (non-membership): `leaf` is the low leaf of `value`, so the
|
|
13
|
+
* commitment is absent and the credential is not revoked.
|
|
14
|
+
*
|
|
15
|
+
* `root` is the value the verifier must independently accept as one the
|
|
16
|
+
* authoritative registry currently publishes. The proof proves nothing on its
|
|
17
|
+
* own: its root is supplied by whoever built it.
|
|
18
|
+
*/
|
|
19
|
+
export type RevocationProof = LeanIMTPlusProof<bigint>
|
|
20
|
+
|
|
21
|
+
/** An indexed leaf of the revocation tree: a commitment and its successor. */
|
|
22
|
+
export type RevocationLeaf = LeanIMTPlusLeaf<bigint>
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { SNARK_SCALAR_FIELD } from "./constants"
|
|
2
|
+
import type { BigNumberish } from "./types"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Coerces a value to a BN254 field element, rejecting anything outside the
|
|
6
|
+
* field.
|
|
7
|
+
*
|
|
8
|
+
* Every leaf field and every hash the registry produces must be a field element:
|
|
9
|
+
* outside that range two distinct values reduce to the same element, which would
|
|
10
|
+
* let one revocation silently cover an unrelated credential and would break the
|
|
11
|
+
* sorted ordering a non-membership proof relies on.
|
|
12
|
+
*
|
|
13
|
+
* Zero is accepted, because the sentinel and tombstone leaf slots hold it. Use
|
|
14
|
+
* {@link toCommitment} for anything that must be a real commitment.
|
|
15
|
+
*
|
|
16
|
+
* @param value The value, as a bigint, number or decimal string.
|
|
17
|
+
* @returns The value as a bigint.
|
|
18
|
+
*/
|
|
19
|
+
export function toFieldElement(value: BigNumberish): bigint {
|
|
20
|
+
const element = BigInt(value)
|
|
21
|
+
|
|
22
|
+
if (element < 0n) {
|
|
23
|
+
throw new Error("RevocationRegistry: a field element must not be negative")
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (element >= SNARK_SCALAR_FIELD) {
|
|
27
|
+
throw new Error("RevocationRegistry: a field element must be less than the SNARK scalar field order")
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return element
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Coerces a value to a revocation commitment, rejecting anything the registry
|
|
35
|
+
* cannot hold as one.
|
|
36
|
+
*
|
|
37
|
+
* A commitment must be a field element, for the reason given on
|
|
38
|
+
* {@link toFieldElement}, and must additionally be non-zero, because zero is
|
|
39
|
+
* reserved for the sentinel leaf and for tombstones.
|
|
40
|
+
*
|
|
41
|
+
* These are the rules of the structure itself, so every implementation of a
|
|
42
|
+
* registry enforces them.
|
|
43
|
+
*
|
|
44
|
+
* @param value The commitment, as a bigint, number or decimal string.
|
|
45
|
+
* @returns The commitment as a bigint.
|
|
46
|
+
*/
|
|
47
|
+
export function toCommitment(value: BigNumberish): bigint {
|
|
48
|
+
const commitment = BigInt(value)
|
|
49
|
+
|
|
50
|
+
if (commitment <= 0n) {
|
|
51
|
+
throw new Error("RevocationRegistry: a commitment must be greater than zero")
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return toFieldElement(commitment)
|
|
55
|
+
}
|