Skip to content

Commit 4ab88ac

Browse files
committed
KB universal accumulator
Signed-off-by: lovesh <lovesh.bond@gmail.com>
1 parent 3057ca0 commit 4ab88ac

35 files changed

+1853
-314
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@docknetwork/crypto-wasm-ts",
3-
"version": "0.56.0",
3+
"version": "0.57.0",
44
"description": "Typescript abstractions over Dock's Rust crypto library's WASM wrapper",
55
"homepage": "https://github.com/docknetwork/crypto-wasm-ts",
66
"main": "lib/index.js",
@@ -31,7 +31,7 @@
3131
"@types/flat": "^5.0.2",
3232
"@types/lodash": "^4.14.195",
3333
"bs58": "5.0.0",
34-
"crypto-wasm-new": "npm:@docknetwork/crypto-wasm@0.25.0",
34+
"crypto-wasm-new": "npm:@docknetwork/crypto-wasm@0.26.0",
3535
"crypto-wasm-old": "npm:@docknetwork/crypto-wasm@0.23.0",
3636
"flat": "^5.0.2",
3737
"json-pointer": "^0.6.2",

src/accumulator/IAccumulatorState.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,17 @@ export interface IAccumulatorState {
2020
export interface IUniversalAccumulatorState extends IAccumulatorState {
2121
elements(): Promise<Iterable<Uint8Array>>;
2222
}
23+
24+
export interface IKBUniversalAccumulatorState extends IAccumulatorState {
25+
/**
26+
* Whether this element is in the domain (could be a member or not)
27+
* @param element
28+
*/
29+
inDomain(element: Uint8Array): Promise<boolean>;
30+
31+
/**
32+
* Takes an element not in the domain and adds it.
33+
* @param element
34+
*/
35+
addToDomain(element: Uint8Array): Promise<void>;
36+
}

src/accumulator/accumulator.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
universalAccumulatorVerifyMembership,
3131
universalAccumulatorVerifyNonMembership
3232
} from 'crypto-wasm-new';
33-
import { VBMembershipWitness, VBNonMembershipWitness } from './accumulatorWitness';
33+
import { AccumulatorWitness, VBMembershipWitness, VBNonMembershipWitness } from './accumulatorWitness';
3434
import { getUint8ArraysFromObject } from '../util';
3535
import { IAccumulatorState, IUniversalAccumulatorState } from './IAccumulatorState';
3636
import { IInitialElementsStore } from './IInitialElementsStore';
@@ -53,7 +53,7 @@ import {
5353
* the state object is to check if duplicate elements are not added or already absent elements are not removed or membership witness
5454
* for absent elements is not created. If checks in the `state` fail, they throw errors.
5555
*/
56-
export abstract class Accumulator {
56+
export abstract class Accumulator<T> {
5757
value: Uint8Array | object;
5858
secretKey: AccumulatorSecretKey | undefined;
5959
params: AccumulatorParams | undefined;
@@ -194,7 +194,7 @@ export abstract class Accumulator {
194194
/**
195195
* Get the accumulated value.
196196
*/
197-
abstract get accumulated(): Uint8Array;
197+
abstract get accumulated(): T;
198198

199199
// The following functions optionally take secret key as an argument as its better to not store secret key in memory for
200200
// long time.
@@ -261,7 +261,7 @@ export abstract class Accumulator {
261261
element: Uint8Array,
262262
secretKey?: AccumulatorSecretKey,
263263
state?: IAccumulatorState
264-
): Promise<VBMembershipWitness>;
264+
): Promise<AccumulatorWitness<T>>;
265265

266266
/**
267267
* Calculate the membership witnesses for the given batch of elements
@@ -273,7 +273,7 @@ export abstract class Accumulator {
273273
elements: Uint8Array[],
274274
secretKey?: AccumulatorSecretKey,
275275
state?: IAccumulatorState
276-
): Promise<VBMembershipWitness[]>;
276+
): Promise<AccumulatorWitness<T>[]>;
277277

278278
/**
279279
* Verify the membership witness.
@@ -284,7 +284,7 @@ export abstract class Accumulator {
284284
*/
285285
abstract verifyMembershipWitness(
286286
member: Uint8Array,
287-
witness: VBMembershipWitness,
287+
witness: AccumulatorWitness<T>,
288288
pk: AccumulatorPublicKey,
289289
params?: AccumulatorParams
290290
): boolean;
@@ -401,9 +401,9 @@ export abstract class Accumulator {
401401
}
402402

403403
/**
404-
* Accumulator that supports only membership proofs.
404+
* VB accumulator that supports only membership proofs.
405405
*/
406-
export class PositiveAccumulator extends Accumulator {
406+
export class PositiveAccumulator extends Accumulator<Uint8Array> {
407407
// @ts-ignore
408408
value: Uint8Array;
409409

@@ -441,7 +441,7 @@ export class PositiveAccumulator extends Accumulator {
441441
* Remove a single element from the accumulator
442442
* @param element
443443
* @param secretKey
444-
* @param state- Optional. If provided, checked before removing and element is removed
444+
* @param state Optional. If provided, checked before removing and element is removed
445445
*/
446446
async remove(element: Uint8Array, secretKey?: AccumulatorSecretKey, state?: IAccumulatorState) {
447447
await this.ensurePresence(element, state);
@@ -576,12 +576,12 @@ export class PositiveAccumulator extends Accumulator {
576576
}
577577

578578
/**
579-
* Accumulator that supports both membership proofs and non-membership proofs. For guarding against forgery of
579+
* VB accumulator that supports both membership proofs and non-membership proofs. For guarding against forgery of
580580
* non-membership proofs (details in the paper), during initialization, it should generate several accumulator members
581581
* and never remove them from accumulator, nor it should allow duplicates of them to be added. Thus, several methods
582582
* accept an optional persistent database `IInitialElementsStore` which stores those initial elements.
583583
*/
584-
export class UniversalAccumulator extends Accumulator {
584+
export class UniversalAccumulator extends Accumulator<Uint8Array> {
585585
/**
586586
* `f_V` is supposed to kept private by the accumulator manager. `V` is the accumulated value.
587587
*/

src/accumulator/accumulatorWitness.ts

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
publicInfoForWitnessUpdate,
32
updateMembershipWitnessesPostBatchUpdates,
43
updateMembershipWitnessPostAdd,
54
updateMembershipWitnessPostRemove,
@@ -11,34 +10,34 @@ import {
1110
updateNonMembershipWitnessUsingPublicInfoAfterBatchUpdate,
1211
updateNonMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates
1312
} from 'crypto-wasm-new';
14-
import { getUint8ArraysFromObject, jsonObjToUint8Array } from '../util';
13+
import { getUint8ArraysFromObject } from '../util';
1514
import { AccumulatorSecretKey } from './params-and-keys';
16-
import { BytearrayWrapper } from '../bytearray-wrapper';
15+
import { VBWitnessUpdateInfo, WitnessUpdateInfo } from './witness-update-info';
1716

18-
export abstract class AccumulatorWitness {
17+
export abstract class AccumulatorWitness<V> {
1918
value: Uint8Array | object;
2019

2120
constructor(value: Uint8Array | object) {
2221
this.value = value;
2322
}
2423

25-
abstract updatePostAdd(addition: Uint8Array, element: Uint8Array, accumulatorValueBeforeAddition: Uint8Array): void;
26-
abstract updatePostRemove(removal: Uint8Array, element: Uint8Array, accumulatorValueAfterRemoval: Uint8Array): void;
24+
abstract updatePostAdd(addition: Uint8Array, element: Uint8Array, accumulatorValueBeforeAddition: V): void;
25+
abstract updatePostRemove(removal: Uint8Array, element: Uint8Array, accumulatorValueAfterRemoval: V): void;
2726
abstract updateUsingPublicInfoPostBatchUpdate(
2827
element: Uint8Array,
2928
additions: Uint8Array[],
3029
removals: Uint8Array[],
31-
publicInfo: VBWitnessUpdatePublicInfo
30+
publicInfo: WitnessUpdateInfo
3231
): void;
3332
abstract updateUsingPublicInfoPostMultipleBatchUpdates(
3433
element: Uint8Array,
3534
additions: Uint8Array[][],
3635
removals: Uint8Array[][],
37-
publicInfo: VBWitnessUpdatePublicInfo[]
36+
publicInfo: WitnessUpdateInfo[]
3837
): void;
3938
}
4039

41-
export class VBMembershipWitness extends AccumulatorWitness {
40+
export class VBMembershipWitness extends AccumulatorWitness<Uint8Array> {
4241
// @ts-ignore
4342
value: Uint8Array;
4443

@@ -73,7 +72,7 @@ export class VBMembershipWitness extends AccumulatorWitness {
7372
member: Uint8Array,
7473
additions: Uint8Array[],
7574
removals: Uint8Array[],
76-
publicInfo: VBWitnessUpdatePublicInfo
75+
publicInfo: VBWitnessUpdateInfo
7776
) {
7877
this.value = updateMembershipWitnessUsingPublicInfoAfterBatchUpdate(
7978
this.value,
@@ -96,7 +95,7 @@ export class VBMembershipWitness extends AccumulatorWitness {
9695
member: Uint8Array,
9796
additions: Uint8Array[][],
9897
removals: Uint8Array[][],
99-
publicInfo: VBWitnessUpdatePublicInfo[]
98+
publicInfo: VBWitnessUpdateInfo[]
10099
) {
101100
const info = publicInfo.map((i) => i.value);
102101
this.value = updateMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates(
@@ -149,7 +148,7 @@ export class VBMembershipWitness extends AccumulatorWitness {
149148
}
150149
}
151150

152-
export class VBNonMembershipWitness extends AccumulatorWitness {
151+
export class VBNonMembershipWitness extends AccumulatorWitness<Uint8Array> {
153152
// @ts-ignore
154153
value: { d: Uint8Array; C: Uint8Array };
155154

@@ -184,7 +183,7 @@ export class VBNonMembershipWitness extends AccumulatorWitness {
184183
nonMember: Uint8Array,
185184
additions: Uint8Array[],
186185
removals: Uint8Array[],
187-
publicInfo: VBWitnessUpdatePublicInfo
186+
publicInfo: VBWitnessUpdateInfo
188187
) {
189188
this.value = updateNonMembershipWitnessUsingPublicInfoAfterBatchUpdate(
190189
this.value,
@@ -207,7 +206,7 @@ export class VBNonMembershipWitness extends AccumulatorWitness {
207206
nonMember: Uint8Array,
208207
additions: Uint8Array[][],
209208
removals: Uint8Array[][],
210-
publicInfo: VBWitnessUpdatePublicInfo[]
209+
publicInfo: VBWitnessUpdateInfo[]
211210
) {
212211
const info = publicInfo.map((i) => i.value);
213212
this.value = updateNonMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates(
@@ -259,35 +258,3 @@ export class VBNonMembershipWitness extends AccumulatorWitness {
259258
return new VBNonMembershipWitness({ d, C });
260259
}
261260
}
262-
263-
/**
264-
* Public info published by the accumulator manager used to update witnesses after several additions and removals.
265-
*/
266-
export class VBWitnessUpdatePublicInfo extends BytearrayWrapper {
267-
toJSON(): string {
268-
return JSON.stringify({
269-
value: this.value
270-
});
271-
}
272-
273-
fromJSON(json: string): VBWitnessUpdatePublicInfo {
274-
return new VBWitnessUpdatePublicInfo(jsonObjToUint8Array(json));
275-
}
276-
277-
/**
278-
* Accumulator manager creates the witness update info corresponding to the additions and removals.
279-
* @param accumulatorValueBeforeUpdates - accumulator value before the additions and removals
280-
* @param additions
281-
* @param removals
282-
* @param sk
283-
*/
284-
static new(
285-
accumulatorValueBeforeUpdates: Uint8Array,
286-
additions: Uint8Array[],
287-
removals: Uint8Array[],
288-
sk: AccumulatorSecretKey
289-
): VBWitnessUpdatePublicInfo {
290-
const value = publicInfoForWitnessUpdate(accumulatorValueBeforeUpdates, additions, removals, sk.value);
291-
return new VBWitnessUpdatePublicInfo(value);
292-
}
293-
}

src/accumulator/in-memory-persistence.ts

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IAccumulatorState, IUniversalAccumulatorState } from './IAccumulatorState';
1+
import { IAccumulatorState, IKBUniversalAccumulatorState, IUniversalAccumulatorState } from './IAccumulatorState';
22
import { IInitialElementsStore } from './IInitialElementsStore';
33

44
/**
@@ -11,6 +11,10 @@ export class InMemoryState implements IAccumulatorState {
1111
this.state = new Set<string>();
1212
}
1313

14+
get size(): number {
15+
return this.state.size;
16+
}
17+
1418
async add(element: Uint8Array): Promise<void> {
1519
const key = InMemoryState.key(element);
1620
if (this.state.has(key)) {
@@ -75,3 +79,69 @@ export class InMemoryInitialElementsStore implements IInitialElementsStore {
7579
return JSON.stringify(Array.from(element));
7680
}
7781
}
82+
83+
/**
84+
* In memory implementation of the state. For testing only
85+
*/
86+
export class InMemoryKBUniversalState implements IKBUniversalAccumulatorState {
87+
memState: Set<string>;
88+
nonMemState: Set<string>;
89+
90+
constructor() {
91+
this.memState = new Set<string>();
92+
this.nonMemState = new Set<string>();
93+
}
94+
95+
get size(): number {
96+
return this.memState.size;
97+
}
98+
99+
add(element: Uint8Array): Promise<void> {
100+
const key = InMemoryKBUniversalState.key(element);
101+
if (this.memState.has(key)) {
102+
throw new Error(`${element} already present in mem state`);
103+
}
104+
if (!this.nonMemState.has(key)) {
105+
throw new Error(`${element} not present in non mem state`);
106+
}
107+
this.memState.add(key);
108+
this.nonMemState.delete(key);
109+
return Promise.resolve();
110+
}
111+
112+
has(element: Uint8Array): Promise<boolean> {
113+
const key = InMemoryKBUniversalState.key(element);
114+
// Ideally, something present in `memState` should not be present in `nonMemState` and vice-versa
115+
const b = this.memState.has(key) && !this.nonMemState.has(key);
116+
return Promise.resolve(b);
117+
}
118+
119+
remove(element: Uint8Array): Promise<void> {
120+
const key = InMemoryKBUniversalState.key(element);
121+
if (!this.memState.has(key)) {
122+
throw new Error(`${element} not present in mem state`);
123+
}
124+
if (this.nonMemState.has(key)) {
125+
throw new Error(`${element} already present in non mem state`);
126+
}
127+
this.memState.delete(key);
128+
this.nonMemState.add(key);
129+
return Promise.resolve();
130+
}
131+
132+
static key(element: Uint8Array) {
133+
return JSON.stringify(Array.from(element));
134+
}
135+
136+
inDomain(element: Uint8Array): Promise<boolean> {
137+
const key = InMemoryKBUniversalState.key(element);
138+
const b = this.nonMemState.has(key) || this.memState.has(key);
139+
return Promise.resolve(b);
140+
}
141+
142+
async addToDomain(element: Uint8Array) {
143+
const key = InMemoryKBUniversalState.key(element);
144+
this.nonMemState.add(key);
145+
return Promise.resolve();
146+
}
147+
}

src/accumulator/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './accumulatorWitness';
44
export * from './proof';
55
export * from './IAccumulatorState';
66
export * from './IInitialElementsStore';
7+
export { VBWitnessUpdateInfo } from './witness-update-info';

0 commit comments

Comments
 (0)