-
Notifications
You must be signed in to change notification settings - Fork 9
/
accumulatorWitness.ts
260 lines (241 loc) · 9.24 KB
/
accumulatorWitness.ts
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
import {
updateMembershipWitnessesPostBatchUpdates,
updateMembershipWitnessPostAdd,
updateMembershipWitnessPostRemove,
updateMembershipWitnessUsingPublicInfoAfterBatchUpdate,
updateMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates,
updateNonMembershipWitnessesPostBatchUpdates,
updateNonMembershipWitnessPostAdd,
updateNonMembershipWitnessPostRemove,
updateNonMembershipWitnessUsingPublicInfoAfterBatchUpdate,
updateNonMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates
} from 'crypto-wasm-new';
import { getUint8ArraysFromObject } from '../util';
import { AccumulatorSecretKey } from './params-and-keys';
import { VBWitnessUpdateInfo, WitnessUpdateInfo } from './witness-update-info';
export abstract class AccumulatorWitness<V> {
value: Uint8Array | object;
constructor(value: Uint8Array | object) {
this.value = value;
}
abstract updatePostAdd(addition: Uint8Array, element: Uint8Array, accumulatorValueBeforeAddition: V): void;
abstract updatePostRemove(removal: Uint8Array, element: Uint8Array, accumulatorValueAfterRemoval: V): void;
abstract updateUsingPublicInfoPostBatchUpdate(
element: Uint8Array,
additions: Uint8Array[],
removals: Uint8Array[],
publicInfo: WitnessUpdateInfo
): void;
abstract updateUsingPublicInfoPostMultipleBatchUpdates(
element: Uint8Array,
additions: Uint8Array[][],
removals: Uint8Array[][],
publicInfo: WitnessUpdateInfo[]
): void;
}
export class VBMembershipWitness extends AccumulatorWitness<Uint8Array> {
// @ts-ignore
value: Uint8Array;
/**
* Update a membership witness after an element is added to the accumulator
* @param addition - new addition to the accumulator
* @param member - whose witness is being updated
* @param accumulatorValueBeforeAddition - accumulator value before the addition was done.
*/
updatePostAdd(addition: Uint8Array, member: Uint8Array, accumulatorValueBeforeAddition: Uint8Array) {
this.value = updateMembershipWitnessPostAdd(this.value, member, addition, accumulatorValueBeforeAddition);
}
/**
* Update a membership witness after an element is removed from the accumulator.
* @param removal - removal from the accumulator
* @param member - whose witness is being updated
* @param accumulatorValueAfterRemoval - accumulator value after the removal was done.
*/
updatePostRemove(removal: Uint8Array, member: Uint8Array, accumulatorValueAfterRemoval: Uint8Array) {
this.value = updateMembershipWitnessPostRemove(this.value, member, removal, accumulatorValueAfterRemoval);
}
/**
* Compute an update to the membership witness after adding and removing batches of elements from the accumulator.
* @param member - the member whose witness is to be updated
* @param additions - array of additions
* @param removals - array of removals
* @param publicInfo - witness update info published by the accumulator manager
*/
updateUsingPublicInfoPostBatchUpdate(
member: Uint8Array,
additions: Uint8Array[],
removals: Uint8Array[],
publicInfo: VBWitnessUpdateInfo
) {
this.value = updateMembershipWitnessUsingPublicInfoAfterBatchUpdate(
this.value,
member,
additions,
removals,
publicInfo.value
);
}
/**
* Compute an update to the membership witness after adding and removing several batches of elements from the accumulator.
* For each batch of updates, additions, removals and witness update info are provided.
* @param member - the member whose witness is to be updated
* @param additions - array of arrays of additions
* @param removals - array of arrays of removals
* @param publicInfo - array of witness update info
*/
updateUsingPublicInfoPostMultipleBatchUpdates(
member: Uint8Array,
additions: Uint8Array[][],
removals: Uint8Array[][],
publicInfo: VBWitnessUpdateInfo[]
) {
const info = publicInfo.map((i) => i.value);
this.value = updateMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates(
this.value,
member,
additions,
removals,
info
);
}
/**
* Compute an update to several membership witnesses after adding and removing batches of elements from the accumulator.
* @param witnesses - membership witnesses to update
* @param members - members corresponding to the witnesses
* @param additions
* @param removals
* @param accumulatorValueBeforeUpdates - accumulator value before the updates
* @param secretKey
*/
static updateMultiplePostBatchUpdates(
witnesses: VBMembershipWitness[],
members: Uint8Array[],
additions: Uint8Array[],
removals: Uint8Array[],
accumulatorValueBeforeUpdates: Uint8Array,
secretKey: AccumulatorSecretKey
): VBMembershipWitness[] {
const wits = witnesses.map((m) => m.value);
return updateMembershipWitnessesPostBatchUpdates(
wits,
members,
additions,
removals,
accumulatorValueBeforeUpdates,
secretKey.value
).map((m) => new VBMembershipWitness(m));
}
toJSON(): string {
return JSON.stringify({
value: Array.from(this.value)
});
}
static fromJSON(json: string): VBMembershipWitness {
const obj = JSON.parse(json);
const [value] = getUint8ArraysFromObject(obj, ['value']);
return new VBMembershipWitness(value);
}
}
export class VBNonMembershipWitness extends AccumulatorWitness<Uint8Array> {
// @ts-ignore
value: { d: Uint8Array; C: Uint8Array };
/**
* Update a non-membership witness after an element is added to the accumulator
* @param addition - new addition to the accumulator
* @param nonMember - whose witness is being updated
* @param accumulatorValueBeforeAddition - accumulator value before the addition was done.
*/
updatePostAdd(addition: Uint8Array, nonMember: Uint8Array, accumulatorValueBeforeAddition: Uint8Array) {
this.value = updateNonMembershipWitnessPostAdd(this.value, nonMember, addition, accumulatorValueBeforeAddition);
}
/**
* Update a non-membership witness after an element is removed from the accumulator.
* @param removal - removal from the accumulator
* @param nonMember - whose witness is being updated
* @param accumulatorValueAfterRemoval - accumulator value after the removal was done.
*/
updatePostRemove(removal: Uint8Array, nonMember: Uint8Array, accumulatorValueAfterRemoval: Uint8Array) {
this.value = updateNonMembershipWitnessPostRemove(this.value, nonMember, removal, accumulatorValueAfterRemoval);
}
/**
* Compute an update to the non-membership witness after adding and removing batches of elements from the accumulator.
* @param nonMember - the non-member whose witness is to be updated
* @param additions - array of additions
* @param removals - array of removals
* @param publicInfo - witness update info published by the accumulator manager
*/
updateUsingPublicInfoPostBatchUpdate(
nonMember: Uint8Array,
additions: Uint8Array[],
removals: Uint8Array[],
publicInfo: VBWitnessUpdateInfo
) {
this.value = updateNonMembershipWitnessUsingPublicInfoAfterBatchUpdate(
this.value,
nonMember,
additions,
removals,
publicInfo.value
);
}
/**
* Compute an update to the non-membership witness after adding and removing several batches of elements from the accumulator.
* For each batch of updates, additions, removals and witness update info are provided.
* @param nonMember - the non-member whose witness is to be updated
* @param additions - array of arrays of additions
* @param removals - array of arrays of removals
* @param publicInfo - array of witness update info
*/
updateUsingPublicInfoPostMultipleBatchUpdates(
nonMember: Uint8Array,
additions: Uint8Array[][],
removals: Uint8Array[][],
publicInfo: VBWitnessUpdateInfo[]
) {
const info = publicInfo.map((i) => i.value);
this.value = updateNonMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates(
this.value,
nonMember,
additions,
removals,
info
);
}
/**
* Compute an update to several non-membership witnesses after adding and removing batches of elements from the accumulator.
* @param witnesses - non-membership witnesses to update
* @param nonMembers - nonMembers corresponding to the witnesses
* @param additions
* @param removals
* @param accumulatorValueBeforeUpdates - accumulator value before the updates
* @param secretKey
*/
static updateMultiplePostBatchUpdates(
witnesses: VBNonMembershipWitness[],
nonMembers: Uint8Array[],
additions: Uint8Array[],
removals: Uint8Array[],
accumulatorValueBeforeUpdates: Uint8Array,
secretKey: AccumulatorSecretKey
): VBNonMembershipWitness[] {
const wits = witnesses.map((w) => w.value);
return updateNonMembershipWitnessesPostBatchUpdates(
wits,
nonMembers,
additions,
removals,
accumulatorValueBeforeUpdates,
secretKey.value
).map((m) => new VBNonMembershipWitness(m));
}
toJSON(): string {
return JSON.stringify({
value: { d: Array.from(this.value.d), C: Array.from(this.value.C) }
});
}
static fromJSON(json: string): VBNonMembershipWitness {
const obj = JSON.parse(json);
const [d, C] = getUint8ArraysFromObject(obj.value, ['d', 'C']);
return new VBNonMembershipWitness({ d, C });
}
}