-
Notifications
You must be signed in to change notification settings - Fork 9
/
keyed-proofs.spec.ts
383 lines (341 loc) · 14 KB
/
keyed-proofs.spec.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import { VerifyResult } from 'crypto-wasm-new';
import {
AccumulatorPublicKey,
AccumulatorSecretKey,
BBDT16Credential,
BBDT16CredentialBuilder,
BBDT16MacSecretKey,
CredentialSchema,
KeyedProof,
ID_STR,
initializeWasm,
MEM_CHECK_KV_STR,
MEM_CHECK_STR,
NON_MEM_CHECK_KV_STR,
PositiveAccumulator,
Presentation,
PresentationBuilder,
REV_CHECK_STR,
RevocationStatusProtocol,
SignatureType,
TYPE_STR,
VBMembershipWitness,
BBDT16MacPublicKeyG1,
BBDT16MacParams,
BBDT16_MAC_PARAMS_LABEL_BYTES,
BBDT16KeyedProof,
VBAccumMembershipKeyedProof,
AccumulatorPublicKeyForKeyedVerification,
KBUniAccumMembershipKeyedProof,
KBUniAccumNonMembershipKeyedProof, dockAccumulatorParams
} from '../../src';
import {
KBUniversalMembershipWitness,
KBUniversalNonMembershipWitness
} from '../../src/accumulator/kb-acccumulator-witness';
import { KBUniversalAccumulator } from '../../src/accumulator/kb-universal-accumulator';
import { Credential, CredentialBuilder, isKvac, isPS, PublicKey, Scheme, SecretKey } from '../scheme';
import { checkResult } from '../utils';
import {
checkPresentationJson,
getExampleSchema,
getKeys,
setupKBUniAccumulator,
setupPrefilledAccum,
verifyCred
} from './utils';
describe(`Keyed proof verification with BBDT16 MAC and ${Scheme} signatures`, () => {
let sk: SecretKey, pk: PublicKey;
let skKvac: BBDT16MacSecretKey;
let pkKvac: BBDT16MacPublicKeyG1;
let paramsKvac: BBDT16MacParams;
let credential1: Credential;
let credential2: Credential;
let credential3: Credential;
let credential4: BBDT16Credential;
let credential5: BBDT16Credential;
let credential6: Credential;
let credential7: Credential;
// Accumulator where membership is publicly verifiable
let accumulator1: PositiveAccumulator;
let accumulator1Pk: AccumulatorPublicKey;
let accumulator1Witness: VBMembershipWitness;
// Accumulator where membership verification needs secret key
let accumulator2: PositiveAccumulator;
let accumulator2Sk: AccumulatorSecretKey;
let accumulator2Pk: AccumulatorPublicKeyForKeyedVerification;
let accumulator2Witness: VBMembershipWitness;
// Accumulator where membership and non-membership verification needs secret key
let accumulator3: KBUniversalAccumulator;
let accumulator3Sk: AccumulatorSecretKey;
let accumulator3Pk: AccumulatorPublicKeyForKeyedVerification;
let accumulator3MemWitness: KBUniversalMembershipWitness;
let accumulator3NonMemWitness: KBUniversalNonMembershipWitness;
beforeAll(async () => {
await initializeWasm();
[sk, pk] = getKeys('seed1');
paramsKvac = BBDT16MacParams.generate(1, BBDT16_MAC_PARAMS_LABEL_BYTES);
skKvac = BBDT16MacSecretKey.generate();
pkKvac = skKvac.generatePublicKeyG1(paramsKvac);
const schema = new CredentialSchema(getExampleSchema(5));
const subject = {
fname: 'John',
lname: 'Smith',
sensitive: {
very: {
secret: 'my-secret-that-wont-tell-anyone'
},
email: '[email protected]',
phone: '801009801',
SSN: '123-456789-0'
},
lessSensitive: {
location: {
country: 'USA',
city: 'New York'
},
department: {
name: 'Random',
location: {
name: 'Somewhere',
geo: {
lat: -23.658,
long: 2.556
}
}
}
},
rank: 6
};
const builder1 = new CredentialBuilder();
builder1.schema = schema;
builder1.subject = subject;
builder1.setCredentialStatus('dock:accumulator:accumId123', MEM_CHECK_STR, 'user:A-123');
credential1 = builder1.sign(sk);
verifyCred(credential1, pk, sk);
const builder2 = new CredentialBuilder();
builder2.schema = schema;
builder2.subject = subject;
builder2.setCredentialStatus('dock:accumulator:accumId124', MEM_CHECK_KV_STR, 'user:A-124');
credential2 = builder2.sign(sk);
verifyCred(credential2, pk, sk);
const builder3 = new CredentialBuilder();
builder3.schema = new CredentialSchema(getExampleSchema(9));
builder3.subject = {
fname: 'John',
lname: 'Smith',
email: '[email protected]',
SSN: '123-456789-0',
userId: 'user:123-xyz-#',
country: 'USA',
city: 'New York',
timeOfBirth: 1662010849619,
height: 181.5,
weight: 210.4,
BMI: 23.25,
score: -13.5,
secret: 'my-secret-that-wont-tell-anyone'
};
credential3 = builder3.sign(sk);
verifyCred(credential3, pk, sk);
const builder4 = new BBDT16CredentialBuilder();
builder4.schema = schema;
builder4.subject = subject;
builder4.setCredentialStatus('dock:accumulator:accumId123', MEM_CHECK_STR, 'user:A-123');
// @ts-ignore
credential4 = builder4.sign(skKvac);
verifyCred(credential4, undefined, skKvac);
const builder5 = new BBDT16CredentialBuilder();
builder5.schema = schema;
builder5.subject = subject;
builder5.setCredentialStatus('dock:accumulator:accumId124', MEM_CHECK_KV_STR, 'user:A-124');
// @ts-ignore
credential5 = builder5.sign(skKvac);
verifyCred(credential5, undefined, skKvac);
// @ts-ignore
[, accumulator1Pk, accumulator1, accumulator1Witness] = await setupPrefilledAccum(200, 122, 'user:A-', schema);
// @ts-ignore
[accumulator2Sk, , accumulator2, accumulator2Witness] = await setupPrefilledAccum(200, 123, 'user:A-', schema);
accumulator2Pk = AccumulatorPublicKeyForKeyedVerification.generate(accumulator2Sk, dockAccumulatorParams());
let others;
// @ts-ignore
[accumulator3Sk, , accumulator3, ...others] = await setupKBUniAccumulator(100, 'user:A-', schema);
accumulator3Pk = AccumulatorPublicKeyForKeyedVerification.generate(accumulator3Sk, dockAccumulatorParams());
const [domain, state] = others;
const builder6 = new CredentialBuilder();
builder6.schema = schema;
builder6.subject = subject;
builder6.setCredentialStatus('dock:accumulator:accumId125', MEM_CHECK_KV_STR, 'user:A-25', RevocationStatusProtocol.KbUni24);
credential6 = builder6.sign(sk);
await accumulator3.add(domain[24], accumulator3Sk, state);
accumulator3MemWitness = await accumulator3.membershipWitness(domain[24], accumulator3Sk, state);
verifyCred(credential6, pk, sk);
const builder7 = new CredentialBuilder();
builder7.schema = schema;
builder7.subject = subject;
builder7.setCredentialStatus('dock:accumulator:accumId125', NON_MEM_CHECK_KV_STR, 'user:A-26', RevocationStatusProtocol.KbUni24);
credential7 = builder7.sign(sk);
accumulator3NonMemWitness = await accumulator3.nonMembershipWitness(domain[25], accumulator3Sk, state);
verifyCred(credential6, pk, sk);
});
it('works', () => {
// Describes a test with 7 credentials. Credentials 1, 2, 3, 6, 7 are non-KVAC and 4 and 5 is KVAC.
// Status verification of credential 1 and credential 4 requires public key but for credential 2, 5, 6, 7 requires secret key
const builder = new PresentationBuilder();
expect(builder.addCredential(credential1, isPS() ? pk : undefined)).toEqual(0);
expect(builder.addCredential(credential2, isPS() ? pk : undefined)).toEqual(1);
expect(builder.addCredential(credential3, isPS() ? pk : undefined)).toEqual(2);
expect(builder.addCredential(credential4)).toEqual(3);
expect(builder.addCredential(credential5)).toEqual(4);
expect(builder.addCredential(credential6, isPS() ? pk : undefined)).toEqual(5);
expect(builder.addCredential(credential7, isPS() ? pk : undefined)).toEqual(6);
builder.addAccumInfoForCredStatus(0, accumulator1Witness, accumulator1.accumulated, accumulator1Pk, {
blockNo: 2010334
});
builder.addAccumInfoForCredStatus(1, accumulator2Witness, accumulator2.accumulated, undefined, {
blockNo: 2010335
});
builder.addAccumInfoForCredStatus(3, accumulator1Witness, accumulator1.accumulated, accumulator1Pk, {
blockNo: 2010336
});
builder.addAccumInfoForCredStatus(4, accumulator2Witness, accumulator2.accumulated, undefined, {
blockNo: 2010337
});
builder.addAccumInfoForCredStatus(5, accumulator3MemWitness, accumulator3.accumulated, undefined, {
blockNo: 2010338
});
builder.addAccumInfoForCredStatus(6, accumulator3NonMemWitness, accumulator3.accumulated, undefined, {
blockNo: 2010339
});
const pres = builder.finalize();
const pks = new Map();
pks.set(0, pk);
pks.set(1, pk);
pks.set(2, pk);
pks.set(5, pk);
pks.set(6, pk);
const accumPks = new Map();
accumPks.set(0, accumulator1Pk);
accumPks.set(3, accumulator1Pk);
checkResult(pres.verify(pks, accumPks));
checkPresentationJson(pres, pks, accumPks);
// Check full verification using secret key. This will be done by a verifier who has the secret key and is useful in
// situations when issuer and verifier are the same entity (or share the secret key)
pks.set(3, skKvac);
pks.set(4, skKvac);
accumPks.set(1, accumulator2Sk);
accumPks.set(4, accumulator2Sk);
accumPks.set(5, accumulator3Sk);
accumPks.set(6, accumulator3Sk);
checkResult(pres.verify(pks, accumPks));
let recreatedPres = checkPresentationJson(pres, pks, accumPks);
checkKeyedProofs(pres);
checkKeyedProofs(recreatedPres);
function checkKeyedProofs(presentation: Presentation) {
/**
* Check if the serialized versions of keyed proofs can be verified
* @param verifyFunc - the function that verifier
* @param keyedCredProof
*/
function checkSerialized(verifyFunc, keyedCredProof?: KeyedProof) {
let j = keyedCredProof?.toJSON();
let recreated = KeyedProof.fromJSON(j as object);
verifyFunc(recreated);
}
function credProofAvailable(keyedCredProof?: KeyedProof) {
expect(keyedCredProof?.credential).toMatchObject({
sigType: SignatureType.Bbdt16
});
checkResult(keyedCredProof?.credential?.proof.verify(sk) as VerifyResult);
}
function check2(keyedCredProof?: KeyedProof) {
if (!isKvac()) {
expect(keyedCredProof?.credential).not.toBeDefined();
}
expect(keyedCredProof?.status).toMatchObject({
[ID_STR]: 'dock:accumulator:accumId124',
[TYPE_STR]: RevocationStatusProtocol.Vb22,
[REV_CHECK_STR]: MEM_CHECK_KV_STR
});
checkResult(keyedCredProof?.status?.proof.verify(accumulator2Sk) as VerifyResult);
}
function check3(keyedCredProof?: KeyedProof) {
expect(keyedCredProof?.credential).toMatchObject({
sigType: SignatureType.Bbdt16
});
const proof = keyedCredProof?.credential?.proof as BBDT16KeyedProof;
checkResult(proof.verify(skKvac) as VerifyResult);
const pv = proof.proofOfValidity(skKvac, pkKvac, paramsKvac);
checkResult(pv.verify(proof, pkKvac, paramsKvac));
expect(keyedCredProof?.status).not.toBeDefined();
}
function check4(keyedCredProof?: KeyedProof) {
expect(keyedCredProof?.credential).toMatchObject({
sigType: SignatureType.Bbdt16
});
checkResult(keyedCredProof?.credential?.proof.verify(skKvac) as VerifyResult);
expect(keyedCredProof?.status).toMatchObject({
[ID_STR]: 'dock:accumulator:accumId124',
[TYPE_STR]: RevocationStatusProtocol.Vb22,
[REV_CHECK_STR]: MEM_CHECK_KV_STR
});
const proof = keyedCredProof?.status?.proof as VBAccumMembershipKeyedProof;
checkResult(proof.verify(accumulator2Sk) as VerifyResult);
const pv = proof.proofOfValidity(accumulator2Sk, accumulator2Pk, dockAccumulatorParams());
checkResult(pv.verify(proof, accumulator2Pk, dockAccumulatorParams()));
}
function check5(keyedCredProof?: KeyedProof) {
if (!isKvac()) {
expect(keyedCredProof?.credential).not.toBeDefined();
}
expect(keyedCredProof?.status).toMatchObject({
[ID_STR]: 'dock:accumulator:accumId125',
[TYPE_STR]: RevocationStatusProtocol.KbUni24,
[REV_CHECK_STR]: MEM_CHECK_KV_STR
});
const proof = keyedCredProof?.status?.proof as KBUniAccumMembershipKeyedProof;
checkResult(proof.verify(accumulator3Sk) as VerifyResult);
const pv = proof.proofOfValidity(accumulator3Sk, accumulator3Pk, dockAccumulatorParams());
checkResult(pv.verify(proof, accumulator3Pk, dockAccumulatorParams()));
}
function check6(keyedCredProof?: KeyedProof) {
if (!isKvac()) {
expect(keyedCredProof?.credential).not.toBeDefined();
}
expect(keyedCredProof?.status).toMatchObject({
[ID_STR]: 'dock:accumulator:accumId125',
[TYPE_STR]: RevocationStatusProtocol.KbUni24,
[REV_CHECK_STR]: NON_MEM_CHECK_KV_STR
});
const proof = keyedCredProof?.status?.proof as KBUniAccumNonMembershipKeyedProof;
checkResult(proof.verify(accumulator3Sk) as VerifyResult);
const pv = proof.proofOfValidity(accumulator3Sk, accumulator3Pk, dockAccumulatorParams());
checkResult(pv.verify(proof, accumulator3Pk, dockAccumulatorParams()));
}
const keyedProofs = presentation.getKeyedProofs();
expect(keyedProofs.size).toEqual(isKvac() ? 7 : 5);
if (isKvac()) {
for (let i = 0; i < 3; i++) {
const keyedCredProof = keyedProofs.get(i);
credProofAvailable(keyedCredProof);
checkSerialized(credProofAvailable, keyedCredProof);
}
}
const keyedCredProof2 = keyedProofs.get(1);
check2(keyedCredProof2);
checkSerialized(check2, keyedCredProof2);
const keyedCredProof4 = keyedProofs.get(3);
check3(keyedCredProof4);
checkSerialized(check3, keyedCredProof4);
const keyedCredProof5 = keyedProofs.get(4);
check4(keyedCredProof5);
checkSerialized(check4, keyedCredProof5);
const keyedCredProof6 = keyedProofs.get(5);
check5(keyedCredProof6);
checkSerialized(check5, keyedCredProof6);
const keyedCredProof7 = keyedProofs.get(6);
check6(keyedCredProof7);
checkSerialized(check6, keyedCredProof7);
}
})
})