-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathkb-universal-accumulator.ts
More file actions
367 lines (336 loc) · 12.9 KB
/
kb-universal-accumulator.ts
File metadata and controls
367 lines (336 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import {
IKBUniversalAccumulator,
kbUniversalAccumulatorAdd,
kbUniversalAccumulatorAddBatch,
kbUniversalAccumulatorBatchUpdates,
kbUniversalAccumulatorComputeExtended,
kbUniversalAccumulatorInitialise,
kbUniversalAccumulatorMembershipWitness,
kbUniversalAccumulatorMembershipWitnessesForBatch,
kbUniversalAccumulatorNonMembershipWitness,
kbUniversalAccumulatorNonMembershipWitnessesForBatch,
kbUniversalAccumulatorRemove,
kbUniversalAccumulatorRemoveBatch,
kbUniversalAccumulatorVerifyMembership,
kbUniversalAccumulatorVerifyNonMembership,
kbUpdateBothWitnessesPostBatchUpdates,
publicInfoForBothKBUniversalWitnessUpdate,
publicInfoForKBUniversalMemWitnessUpdate,
publicInfoForKBUniversalNonMemWitnessUpdate,
publicInfoForKBUniversalNonMemWitnessUpdateOnDomainExtension
} from 'crypto-wasm-new';
import { Accumulator } from './accumulator';
import { IKBUniversalAccumulatorState } from './IAccumulatorState';
import { KBUniversalMembershipWitness, KBUniversalNonMembershipWitness } from './kb-acccumulator-witness';
import { AccumulatorParams, AccumulatorPublicKey, AccumulatorSecretKey } from './params-and-keys';
import {
KBUniversalMembershipWitnessUpdateInfo,
KBUniversalNonMembershipWitnessUpdateInfo
} from './witness-update-info';
/**
* KB universal accumulator. Its composed of 2 accumulators, one for accumulating elements that are "members" and one
* for "non-members". But this detail is largely abstracted away. All possible "members" and "non-members" of this
* accumulator are called its domain and during initialization, the domain needs to be known/passed. The domain can be
* extended at any point and any number of times.
*/
export class KBUniversalAccumulator extends Accumulator<KBUniversalAccumulatorValue> {
// @ts-ignore
value: KBUniversalAccumulatorValue;
/**
*
* @param domain - All possible members or non-members of this accumulator
* @param params
* @param secretKey
* @param state
*/
static async initialize(
domain: Uint8Array[],
params: AccumulatorParams,
secretKey: AccumulatorSecretKey,
state?: IKBUniversalAccumulatorState
): Promise<KBUniversalAccumulator> {
const v = kbUniversalAccumulatorInitialise(domain, secretKey.value, params.value);
const acc = new KBUniversalAccumulator({
value: new KBUniversalAccumulatorValue(v.mem, v.non_mem),
sk: secretKey,
params
});
if (state) {
for (const d of domain) {
await state.addToDomain(d);
}
}
return acc;
}
/**
* Extend the domain
* @param newElements - Add these elements to the domain. These should not be part of the domain
* @param secretKey
* @param state
*/
async extend(newElements: Uint8Array[], secretKey: AccumulatorSecretKey, state?: IKBUniversalAccumulatorState) {
if (state !== undefined) {
for (const e of newElements) {
const r = await state.inDomain(e);
if (r) {
throw new Error(`Element ${e} already part of domain`);
}
}
}
this.value = KBUniversalAccumulatorValue.fromInternalType(
kbUniversalAccumulatorComputeExtended(this.value.asInternalType, newElements, secretKey.value)
);
if (state) {
for (const d of newElements) {
await state.addToDomain(d);
}
}
}
get accumulated(): KBUniversalAccumulatorValue {
return this.value;
}
static fromAccumulated(accumulated: KBUniversalAccumulatorValue): KBUniversalAccumulator {
return new KBUniversalAccumulator({ value: accumulated });
}
async add(element: Uint8Array, secretKey?: AccumulatorSecretKey, state?: IKBUniversalAccumulatorState) {
await this.checkBeforeAdd(element, state);
const sk = this.getSecretKey(secretKey);
this.value = KBUniversalAccumulatorValue.fromInternalType(
kbUniversalAccumulatorAdd(this.value.asInternalType, element, sk.value)
);
await this.addToState(element, state);
}
async addBatch(elements: Uint8Array[], secretKey?: AccumulatorSecretKey, state?: IKBUniversalAccumulatorState) {
await this.checkBeforeAddBatch(elements, state);
const sk = this.getSecretKey(secretKey);
this.value = KBUniversalAccumulatorValue.fromInternalType(
kbUniversalAccumulatorAddBatch(this.value.asInternalType, elements, sk.value)
);
await this.addBatchToState(elements, state);
}
async addRemoveBatches(
additions: Uint8Array[],
removals: Uint8Array[],
secretKey?: AccumulatorSecretKey,
state?: IKBUniversalAccumulatorState
) {
await this.checkBeforeAddBatch(additions, state);
await this.ensurePresenceOfBatch(removals, state);
const sk = this.getSecretKey(secretKey);
this.value = KBUniversalAccumulatorValue.fromInternalType(
kbUniversalAccumulatorBatchUpdates(this.value.asInternalType, additions, removals, sk.value)
);
await this.addBatchToState(additions, state);
await this.removeBatchFromState(removals, state);
}
async membershipWitness(
member: Uint8Array,
secretKey?: AccumulatorSecretKey,
state?: IKBUniversalAccumulatorState
): Promise<KBUniversalMembershipWitness> {
await this.ensurePresence(member, state);
const sk = this.getSecretKey(secretKey);
const wit = kbUniversalAccumulatorMembershipWitness(this.value.asInternalType, member, sk.value);
return new KBUniversalMembershipWitness(wit);
}
async nonMembershipWitness(
nonMember: Uint8Array,
secretKey?: AccumulatorSecretKey,
state?: IKBUniversalAccumulatorState
): Promise<KBUniversalNonMembershipWitness> {
await this.ensureAbsence(nonMember, state);
const sk = this.getSecretKey(secretKey);
const wit = kbUniversalAccumulatorNonMembershipWitness(this.value.asInternalType, nonMember, sk.value);
return new KBUniversalNonMembershipWitness(wit);
}
async membershipWitnessesForBatch(
members: Uint8Array[],
secretKey?: AccumulatorSecretKey,
state?: IKBUniversalAccumulatorState
): Promise<KBUniversalMembershipWitness[]> {
await this.ensurePresenceOfBatch(members, state);
const sk = this.getSecretKey(secretKey);
return kbUniversalAccumulatorMembershipWitnessesForBatch(this.value.asInternalType, members, sk.value).map(
(m) => new KBUniversalMembershipWitness(m)
);
}
async nonMembershipWitnessesForBatch(
nonMembers: Uint8Array[],
secretKey?: AccumulatorSecretKey,
state?: IKBUniversalAccumulatorState
): Promise<KBUniversalNonMembershipWitness[]> {
await this.ensureAbsenceOfBatch(nonMembers, state);
const sk = this.getSecretKey(secretKey);
return kbUniversalAccumulatorNonMembershipWitnessesForBatch(this.value.asInternalType, nonMembers, sk.value).map(
(m) => new KBUniversalNonMembershipWitness(m)
);
}
async remove(element: Uint8Array, secretKey?: AccumulatorSecretKey, state?: IKBUniversalAccumulatorState) {
await this.ensurePresence(element, state);
const sk = this.getSecretKey(secretKey);
this.value = KBUniversalAccumulatorValue.fromInternalType(
kbUniversalAccumulatorRemove(this.value.asInternalType, element, sk.value)
);
await this.removeFromState(element, state);
}
async removeBatch(elements: Uint8Array[], secretKey?: AccumulatorSecretKey, state?: IKBUniversalAccumulatorState) {
await this.ensurePresenceOfBatch(elements, state);
const sk = this.getSecretKey(secretKey);
this.value = KBUniversalAccumulatorValue.fromInternalType(
kbUniversalAccumulatorRemoveBatch(this.value.asInternalType, elements, sk.value)
);
await this.removeBatchFromState(elements, state);
}
verifyMembershipWitness(
member: Uint8Array,
witness: KBUniversalMembershipWitness,
pk: AccumulatorPublicKey,
params?: AccumulatorParams
): boolean {
const params_ = this.getParams(params);
return kbUniversalAccumulatorVerifyMembership(
this.value.asInternalType,
member,
witness.value,
pk.value,
params_.value
);
}
verifyNonMembershipWitness(
nonMember: Uint8Array,
witness: KBUniversalNonMembershipWitness,
pk: AccumulatorPublicKey,
params?: AccumulatorParams
): boolean {
const params_ = this.getParams(params);
return kbUniversalAccumulatorVerifyNonMembership(
this.value.asInternalType,
nonMember,
witness.value,
pk.value,
params_.value
);
}
updateMultipleMemberAndNonMemberWitnessesPostBatchUpdates(
memWitnesses: KBUniversalMembershipWitness[],
members: Uint8Array[],
nonMemWitnesses: KBUniversalMembershipWitness[],
nonMembers: Uint8Array[],
additions: Uint8Array[],
removals: Uint8Array[],
secretKey?: AccumulatorSecretKey
): [KBUniversalMembershipWitness[], KBUniversalNonMembershipWitness[]] {
const m = memWitnesses.map((m) => m.value);
const nm = nonMemWitnesses.map((m) => m.value);
const sk = this.getSecretKey(secretKey);
const [mw, nmw] = kbUpdateBothWitnessesPostBatchUpdates(
m,
members,
nm,
nonMembers,
additions,
removals,
this.value.asInternalType,
sk.value
);
return [mw.map((v) => new KBUniversalMembershipWitness(v)), nmw.map((v) => new KBUniversalNonMembershipWitness(v))];
}
witnessUpdateInfoForMembershipWitness(
additions: Uint8Array[],
removals: Uint8Array[],
secretKey?: AccumulatorSecretKey
): KBUniversalMembershipWitnessUpdateInfo {
const sk = this.getSecretKey(secretKey);
return new KBUniversalMembershipWitnessUpdateInfo(
publicInfoForKBUniversalMemWitnessUpdate(this.value.asInternalType, additions, removals, sk.value)
);
}
witnessUpdateInfoForNonMembershipWitness(
additions: Uint8Array[],
removals: Uint8Array[],
secretKey?: AccumulatorSecretKey
): KBUniversalNonMembershipWitnessUpdateInfo {
const sk = this.getSecretKey(secretKey);
return new KBUniversalNonMembershipWitnessUpdateInfo(
publicInfoForKBUniversalNonMemWitnessUpdate(this.value.asInternalType, additions, removals, sk.value)
);
}
witnessUpdateInfoForNonMembershipWitnessAfterDomainExtension(
newElements: Uint8Array[],
secretKey?: AccumulatorSecretKey
): KBUniversalNonMembershipWitnessUpdateInfo {
const sk = this.getSecretKey(secretKey);
return new KBUniversalNonMembershipWitnessUpdateInfo(
publicInfoForKBUniversalNonMemWitnessUpdateOnDomainExtension(this.value.asInternalType, newElements, sk.value)
);
}
witnessUpdateInfoForBothWitnessTypes(
additions: Uint8Array[],
removals: Uint8Array[],
secretKey?: AccumulatorSecretKey
): [KBUniversalMembershipWitnessUpdateInfo, KBUniversalNonMembershipWitnessUpdateInfo] {
const sk = this.getSecretKey(secretKey);
const [m, nm] = publicInfoForBothKBUniversalWitnessUpdate(this.value.asInternalType, additions, removals, sk.value);
return [new KBUniversalMembershipWitnessUpdateInfo(m), new KBUniversalNonMembershipWitnessUpdateInfo(nm)];
}
protected async checkBeforeAdd(element: Uint8Array, state?: IKBUniversalAccumulatorState) {
await this.checkElementAcceptable(element, state);
await this.ensureAbsence(element, state);
}
protected async checkBeforeAddBatch(elements: Uint8Array[], state?: IKBUniversalAccumulatorState) {
await this.checkElementsAcceptable(elements, state);
await this.ensureAbsenceOfBatch(elements, state);
}
async checkElementAcceptable(element: Uint8Array, state?: IKBUniversalAccumulatorState): Promise<void> {
if (state !== undefined) {
const valid = await state.inDomain(element);
if (!valid) {
throw new Error(`${element} isn't acceptable`);
}
}
}
async checkElementsAcceptable(elements: Uint8Array[], state?: IKBUniversalAccumulatorState): Promise<void> {
if (state) {
for (const element of elements) {
await this.checkElementAcceptable(element, state);
}
}
}
}
/**
* Value of KB universal accumulator
*/
export class KBUniversalAccumulatorValue {
// Value of the accumulator accumulating members
mem: Uint8Array;
// Value of the accumulator accumulating non-members
nonMem: Uint8Array;
constructor(mem: Uint8Array, nonMem: Uint8Array) {
this.mem = mem;
this.nonMem = nonMem;
}
/**
* Object expected by wasm. Used when calling wasm functions
*/
get asInternalType(): IKBUniversalAccumulator {
return {
mem: this.mem,
non_mem: this.nonMem
};
}
static fromInternalType(o: IKBUniversalAccumulator): KBUniversalAccumulatorValue {
return new KBUniversalAccumulatorValue(o.mem, o.non_mem);
}
toBytes(): Uint8Array {
const merged = new Uint8Array(this.mem.length + this.nonMem.length);
merged.set(this.mem);
merged.set(this.nonMem, this.mem.length);
return merged;
}
static fromBytes(bytes: Uint8Array): KBUniversalAccumulatorValue {
// Create 2 Uint8Array from this hex. The 2 are guaranteed to be of the same length
const mem = bytes.subarray(0, bytes.length / 2);
const nonMem = bytes.subarray(bytes.length / 2);
return new KBUniversalAccumulatorValue(mem, nonMem);
}
}