@kumiai/mls 0.12.0 → 0.12.2

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/README.md CHANGED
@@ -89,7 +89,8 @@ Member roots are supported only in lifecycle groups. Admission refuses removed o
89
89
  The reset form is `{ reset: true, log }`. Proofs carry `ControllerLog` and exact `EffectClaim` references.
90
90
  References identify a chain index, epoch event or reset snapshot.
91
91
  Removing an incarnation cascades through its rooted leaves. Valid holder or survivor grants can preserve them until their substitute expiry.
92
- `removalEvidence` captures removal evidence before settlement. Hosts retain it beside their controller store.
92
+ A settlement that keeps the removed incarnation as a tombstone (`snapshotOf`'s default) proves the removal itself, for receivers on 0.12.1 or later.
93
+ `removalEvidence` captures the removal's epoch proof before settlement. A host needs it when the settlement drops that tombstone, or while any group member runs 0.12.0.
93
94
 
94
95
  Tree time combines a preserved floor, root-issued token times and effective-member attestations over distinct pinned keys.
95
96
  The quorum uses the second-highest attestation, or the highest for one effective key.
@@ -6,5 +6,7 @@ export declare function foldChain(did: string, chain: ReadonlyArray<SignedEvent>
6
6
  export declare function foldControllerLog(did: string, log: ControllerLog): FoldResult;
7
7
  export declare function revokeIndices(did: string, chain: ReadonlyArray<SignedEvent>, subject: string): Array<number>;
8
8
  export declare function memberDropIndices(did: string, chain: ReadonlyArray<SignedEvent>, add: string): Array<number>;
9
+ /** Same-generation `rot` indices whose snapshot newly carries `add` as `member`'s removed incarnation. */
10
+ export declare function memberTombstoneIndices(did: string, chain: ReadonlyArray<SignedEvent>, add: string, member: string): Array<number>;
9
11
  export declare function closureOf(did: string, log: ControllerLog, targets: ReadonlyArray<string>): ControllerLog | undefined;
10
12
  export declare function isDetachedFoldReason(reason: string): boolean;
@@ -1,4 +1,5 @@
1
1
  import { deviceRevokeIndices, digestOf, foldLog, pruneLog } from '@kokuin/controller';
2
+ import { normalizeDID } from '@kokuin/token';
2
3
  export const CHAIN_REVOKE_REFUSED = 'chain revoke refused';
3
4
  export function asControllerLog(chain) {
4
5
  return {
@@ -48,6 +49,17 @@ export function memberDropIndices(did, chain, add) {
48
49
  }
49
50
  return indices;
50
51
  }
52
+ /** Same-generation `rot` indices whose snapshot newly carries `add` as `member`'s removed incarnation. */ export function memberTombstoneIndices(did, chain, add, member) {
53
+ const folded = foldChain(did, chain);
54
+ if (!folded.ok) return [];
55
+ const indices = [];
56
+ for(let index = 1; index < chain.length; index++){
57
+ const state = folded.states[index];
58
+ const prior = folded.states[index - 1];
59
+ if (chain[index]?.event.t === 'rot' && state != null && prior != null && state.gen === prior.gen && prior.tombstones?.has(add) !== true && normalizeDID(state.tombstones?.get(add)?.did ?? '') === normalizeDID(member)) indices.push(index);
60
+ }
61
+ return indices;
62
+ }
51
63
  export function closureOf(did, log, targets) {
52
64
  const folded = foldControllerLog(did, log);
53
65
  if (!folded.ok || folded.head.saturated) return undefined;
@@ -70,6 +70,7 @@ export type HeldLedgerEntry = {
70
70
  export type LedgerLogEntry = HeldLedgerEntry & {
71
71
  entryID: string;
72
72
  };
73
+ export declare const VERIFIED_LEDGER_TOKEN_CACHE_SIZE = 4096;
73
74
  export type GroupHandleParams = {
74
75
  state: ClientState;
75
76
  credential: MemberCredential;
@@ -1,4 +1,5 @@
1
1
  import { assertTimeClaimsValid, decodeSignedToken, normalizeDID } from '@kokuin/token';
2
+ import { maybeYield } from '@sozai/async';
2
3
  import { toB64 } from '@sozai/codec';
3
4
  import { EventEmitter } from '@sozai/event';
4
5
  import { contentTypes, createApplicationMessage, createProposal, decode, defaultCredentialTypes, defaultKeyRetentionConfig, defaultProposalTypes, encode, isDefaultCredential, isDefaultProposal, leafNodeSources, mlsExporter, mlsMessageDecoder, mlsMessageEncoder, processMessage as mlsProcessMessage, nodeTypes, senderTypes, UsageError, wireformats } from 'ts-mls';
@@ -235,6 +236,35 @@ export function mutexFor(handle) {
235
236
  }
236
237
  const VERIFIED_LEDGER_ENTRIES = new WeakMap();
237
238
  const VERIFIED_LEDGER_TOKENS = new WeakMap();
239
+ export const VERIFIED_LEDGER_TOKEN_CACHE_SIZE = 4096;
240
+ const VERIFIED_LEDGER_TOKEN_CACHE = new Map();
241
+ function cachedLedgerEntry(token) {
242
+ const verified = VERIFIED_LEDGER_TOKEN_CACHE.get(token);
243
+ if (verified == null) return undefined;
244
+ VERIFIED_LEDGER_TOKEN_CACHE.delete(token);
245
+ VERIFIED_LEDGER_TOKEN_CACHE.set(token, verified);
246
+ try {
247
+ assertTimeClaimsValid(decodeSignedToken(token).payload);
248
+ return structuredClone(verified);
249
+ } catch {
250
+ return null;
251
+ }
252
+ }
253
+ function cacheLedgerEntry(token, verified) {
254
+ VERIFIED_LEDGER_TOKEN_CACHE.delete(token);
255
+ VERIFIED_LEDGER_TOKEN_CACHE.set(token, structuredClone(verified));
256
+ if (VERIFIED_LEDGER_TOKEN_CACHE.size > VERIFIED_LEDGER_TOKEN_CACHE_SIZE) {
257
+ const oldest = VERIFIED_LEDGER_TOKEN_CACHE.keys().next().value;
258
+ if (oldest != null) VERIFIED_LEDGER_TOKEN_CACHE.delete(oldest);
259
+ }
260
+ }
261
+ async function verifyCachedLedgerEntry(token) {
262
+ const cached = cachedLedgerEntry(token);
263
+ if (cached !== undefined) return cached;
264
+ const verified = await verifyLedgerEntry(token);
265
+ if (verified != null) cacheLedgerEntry(token, verified);
266
+ return verified;
267
+ }
238
268
  // Record provenance before exposing claims; re-enactments keep the original snapshot.
239
269
  function ledgerLogEntry(entryID, token, verified) {
240
270
  const held = {
@@ -552,6 +582,7 @@ function ledgerLogEntry(entryID, token, verified) {
552
582
  const memo = VERIFIED_LEDGER_TOKENS.get(tokens);
553
583
  const nextMemo = [];
554
584
  for (const [index, token] of tokens.entries()){
585
+ await maybeYield();
555
586
  const held = memo?.[index];
556
587
  let verified;
557
588
  if (held?.token === token) {
@@ -562,7 +593,7 @@ function ledgerLogEntry(entryID, token, verified) {
562
593
  }
563
594
  verified = structuredClone(held.verified);
564
595
  } else {
565
- verified = await verifyLedgerEntry(token);
596
+ verified = await verifyCachedLedgerEntry(token);
566
597
  }
567
598
  if (verified == null || verified.entry.groupID !== this.groupID) continue;
568
599
  const entryID = ledgerEntryDigest(token);
@@ -660,8 +691,9 @@ function ledgerLogEntry(entryID, token, verified) {
660
691
  assertHeadMatches(authenticated.head, computeHead(this.groupID, entryIDs));
661
692
  const log = [];
662
693
  for(let index = 0; index < tokens.length; index++){
694
+ await maybeYield();
663
695
  const token = tokens[index];
664
- const verified = await verifyLedgerEntry(token);
696
+ const verified = await verifyCachedLedgerEntry(token);
665
697
  if (verified == null || verified.entry.groupID !== this.groupID) {
666
698
  // Unreachable past the gate — the chain digests the token bytes, so an
667
699
  // unverifiable or cross-group token cannot sit at a position the head
@@ -1261,10 +1293,11 @@ function ledgerLogEntry(entryID, token, verified) {
1261
1293
  }
1262
1294
  const tokens = await this.#resolveLedgerEntries(missing);
1263
1295
  for (const token of tokens){
1296
+ await maybeYield();
1264
1297
  const id = ledgerEntryDigest(token);
1265
1298
  // Content-addressing binds the untrusted body to the requested id.
1266
1299
  if (resolved.has(id) || !missing.includes(id)) continue;
1267
- const verified = await verifyLedgerEntry(token);
1300
+ const verified = await verifyCachedLedgerEntry(token);
1268
1301
  if (verified == null) continue;
1269
1302
  resolved.set(id, ledgerLogEntry(id, token, verified));
1270
1303
  }
package/lib/ledger.d.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  import { type SigningIdentity } from '@kokuin/token';
2
+ export declare const LEDGER_ENTRY_DIGEST_CACHE_SIZE = 4096;
3
+ /** @internal */
4
+ export declare function ledgerEntryDigestCacheSize(): number;
2
5
  /**
3
6
  * A control-ledger claim. The payload of a signed ledger token: `type` selects
4
7
  * the reducer, `groupID` binds the claim to a single group, `subject` is the
package/lib/ledger.js CHANGED
@@ -1,5 +1,10 @@
1
1
  import { encodeMultibase, isVerifiedToken, multihashSHA256, normalizeDID, stringifyToken, verifyToken } from '@kokuin/token';
2
2
  const textEncoder = new TextEncoder();
3
+ export const LEDGER_ENTRY_DIGEST_CACHE_SIZE = 4096;
4
+ const LEDGER_ENTRY_DIGEST_CACHE = new Map();
5
+ /** @internal */ export function ledgerEntryDigestCacheSize() {
6
+ return LEDGER_ENTRY_DIGEST_CACHE.size;
7
+ }
3
8
  /**
4
9
  * Sign a ledger entry. The signer fills `iss` with its DID, so the signature
5
10
  * covers every claim field — including `groupID` and `ord` — and binds the
@@ -63,5 +68,17 @@ const textEncoder = new TextEncoder();
63
68
  * Content-addressed digest of a signed ledger token, used as the append-only
64
69
  * store's dedup key. Multibase-encoded SHA-256 multihash over the token bytes.
65
70
  */ export function ledgerEntryDigest(signedToken) {
66
- return encodeMultibase(multihashSHA256(textEncoder.encode(signedToken)));
71
+ const cached = LEDGER_ENTRY_DIGEST_CACHE.get(signedToken);
72
+ if (cached != null) {
73
+ LEDGER_ENTRY_DIGEST_CACHE.delete(signedToken);
74
+ LEDGER_ENTRY_DIGEST_CACHE.set(signedToken, cached);
75
+ return cached;
76
+ }
77
+ const digest = encodeMultibase(multihashSHA256(textEncoder.encode(signedToken)));
78
+ LEDGER_ENTRY_DIGEST_CACHE.set(signedToken, digest);
79
+ if (LEDGER_ENTRY_DIGEST_CACHE.size > LEDGER_ENTRY_DIGEST_CACHE_SIZE) {
80
+ const oldest = LEDGER_ENTRY_DIGEST_CACHE.keys().next().value;
81
+ if (oldest != null) LEDGER_ENTRY_DIGEST_CACHE.delete(oldest);
82
+ }
83
+ return digest;
67
84
  }
@@ -78,6 +78,8 @@ function effectProofs(did, log) {
78
78
  ]
79
79
  ]);
80
80
  const drops = new Map();
81
+ // A settle that carries a removal in snapshot `u` proves it even when no chain state held it live.
82
+ const tombstones = new Map();
81
83
  const revokes = new Map();
82
84
  const snapshots = new Map();
83
85
  const denials = folded.states.map((state)=>new Set([
@@ -101,6 +103,9 @@ function effectProofs(did, log) {
101
103
  for (const [add, member] of prior.members){
102
104
  if (!member.removed && !state.members.has(add)) append(drops, add, index);
103
105
  }
106
+ for (const add of state.tombstones?.keys() ?? []){
107
+ if (!prior.tombstones?.has(add)) append(tombstones, add, index);
108
+ }
104
109
  }
105
110
  const viewAt = (epoch)=>{
106
111
  if (epoch == null) return views.get(folded.head.epoch);
@@ -123,6 +128,7 @@ function effectProofs(did, log) {
123
128
  folded,
124
129
  events,
125
130
  drops,
131
+ tombstones,
126
132
  revokes,
127
133
  snapshots,
128
134
  viewAt
@@ -160,7 +166,11 @@ function checkEffect(log, claim, genFloor, proofs) {
160
166
  } else {
161
167
  const member = prior.members.get(claim.member.add);
162
168
  if (member != null && normalizeDID(member.did) !== normalizeDID(claim.member.did)) throw new RevokeProofError('effects-mismatch');
163
- if (!proofs.drops.get(claim.member.add)?.includes(index)) throw new RevokeProofError('no-rev');
169
+ if (!proofs.drops.get(claim.member.add)?.includes(index)) {
170
+ const tombstone = state.tombstones?.get(claim.member.add);
171
+ if (tombstone == null || !proofs.tombstones.get(claim.member.add)?.includes(index)) throw new RevokeProofError('no-rev');
172
+ if (normalizeDID(tombstone.did) !== normalizeDID(claim.member.did)) throw new RevokeProofError('effects-mismatch');
173
+ }
164
174
  }
165
175
  if (state.gen < genFloor) throw new RevokeProofError('generation-floor');
166
176
  }
@@ -265,8 +275,9 @@ function claimsWithProofs(did, recorded, merged, genFloor, proofs) {
265
275
  event.claim
266
276
  ];
267
277
  }
268
- const indices = claim.kind === 'deny' ? proofs.revokes.get(claim.did) : proofs.drops.get(claim.member.add);
269
- const index = indices?.find((index)=>(after.states[index]?.gen ?? -1) >= floor);
278
+ const atFloor = (index)=>(after.states[index]?.gen ?? -1) >= floor;
279
+ // Drops before tombstones: a 0.12.0 receiver accepts only the drop.
280
+ const index = claim.kind === 'deny' ? proofs.revokes.get(claim.did)?.find(atFloor) : proofs.drops.get(claim.member.add)?.find(atFloor) ?? proofs.tombstones.get(claim.member.add)?.find(atFloor);
270
281
  let ref = index == null ? undefined : {
271
282
  chainIndex: index
272
283
  };
package/lib/registry.js CHANGED
@@ -2,7 +2,7 @@ import { digestOf, isControllerLog, resolveBranches } from '@kokuin/controller';
2
2
  import { normalizeDID } from '@kokuin/token';
3
3
  import { readCapability } from './capability.js';
4
4
  import { ingestControllerLog } from './controller-ingest.js';
5
- import { foldChain, foldControllerLog, memberDropIndices, revokeIndices } from './controller-log.js';
5
+ import { foldChain, foldControllerLog, memberDropIndices, memberTombstoneIndices, revokeIndices } from './controller-log.js';
6
6
  import { RecoveryKnowledgeError, RevokeProofError } from './errors.js';
7
7
  import { authenticateLifecycleProof } from './lifecycle-proof.js';
8
8
  import { claimsFor } from './member-root.js';
@@ -85,9 +85,10 @@ export function isLifecycleDeviceValue(value) {
85
85
  if (value.op === 'clock') {
86
86
  if (value.attest === true) {
87
87
  const folded = previous.recordedLog == null ? undefined : foldControllerLog(controller, previous.recordedLog);
88
+ // Only an effective incarnation attests; the head also holds removed and denied ones.
88
89
  const member = folded?.ok ? [
89
90
  ...folded.head.members.values()
90
- ].find((member)=>normalizeDID(member.did) === normalizeDID(verified.issuer)) : undefined;
91
+ ].find((member)=>member.effective && normalizeDID(member.did) === normalizeDID(verified.issuer)) : undefined;
91
92
  if (member == null) throw new RevokeProofError('effects-mismatch');
92
93
  const attestations = new Map(previous.attestations);
93
94
  attestations.set(member.key, Math.max(attestations.get(member.key) ?? 0, value.time));
@@ -155,7 +156,8 @@ export function isLifecycleDeviceValue(value) {
155
156
  }
156
157
  for (const [add, member] of removedMembers){
157
158
  if (!('event' in member.proofRef)) continue;
158
- const index = memberDropIndices(controller, effect.recordedLog.chain, add).at(-1);
159
+ const chain = effect.recordedLog.chain;
160
+ const index = memberDropIndices(controller, chain, add).at(-1) ?? memberTombstoneIndices(controller, chain, add, member.did).at(-1);
159
161
  if (index != null) removedMembers.set(add, {
160
162
  ...member,
161
163
  proofRef: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kumiai/mls",
3
- "version": "0.12.0",
3
+ "version": "0.12.2",
4
4
  "description": "E2EE group identity and membership for Enkaku using MLS",
5
5
  "keywords": [
6
6
  "mls",
@@ -26,11 +26,12 @@
26
26
  ],
27
27
  "dependencies": {
28
28
  "@kokuin/capability": "^0.5.2",
29
- "@kokuin/controller": "^0.2.4",
30
- "@kokuin/token": "^0.6.2",
29
+ "@kokuin/controller": "^0.2.5",
30
+ "@kokuin/token": "^0.6.3",
31
31
  "@noble/ciphers": "^2.4.0",
32
32
  "@noble/curves": "^2.4.0",
33
33
  "@noble/hashes": "^2.4.0",
34
+ "@sozai/async": "^0.4.3",
34
35
  "@sozai/codec": "^0.4.3",
35
36
  "@sozai/event": "^0.1.6",
36
37
  "@sozai/runtime": "^0.1.0",