-
Notifications
You must be signed in to change notification settings - Fork 9
/
util.ts
493 lines (468 loc) · 18 KB
/
util.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import { flatten } from 'flat';
import { BBSPublicKey, BBSSignature, BBSSignatureParams } from '../bbs';
import { BBSPlusPublicKeyG2, BBSPlusSignatureG1, BBSPlusSignatureParamsG1 } from '../bbs-plus';
import { BBDT16Mac, BBDT16MacParams, BBDT16MacSecretKey } from '../bbdt16-mac';
import { SetupParam, Statement, Witness, WitnessEqualityMetaStatement } from '../composite-proof';
import { PSPublicKey, PSSignature, PSSignatureParams } from '../ps';
import {
SaverChunkedCommitmentKey,
SaverChunkedCommitmentKeyUncompressed,
SaverEncryptionKey,
SaverEncryptionKeyUncompressed,
SaverProvingKey,
SaverProvingKeyUncompressed,
SaverVerifyingKey,
SaverVerifyingKeyUncompressed
} from '../saver';
import { SetupParamsTracker } from './setup-params-tracker';
import {
AttributeEquality,
AttributeRef,
BBS_PLUS_SIGNATURE_PARAMS_LABEL_BYTES,
BBS_SIGNATURE_PARAMS_LABEL_BYTES,
BBDT16_MAC_PARAMS_LABEL_BYTES,
CredentialVerificationParam,
FlattenedSchema,
PredicateParamType,
PS_SIGNATURE_PARAMS_LABEL_BYTES,
PublicKey,
Signature,
SignatureParams,
SignatureParamsClass
} from './types-and-consts';
export function isValueDate(value: string): boolean {
// YYYY-MM-DD
const datePattern = /^\d{4}-([0]\d|1[0-2])-([0-2]\d|3[01])$/;
return datePattern.test(value);
}
export function isValueDateTime(value: string): boolean {
// ISO 8601
const dateTimePattern = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|(\+|-)\d{2}:\d{2})?$/;
return dateTimePattern.test(value);
}
export function flattenTill2ndLastKey(obj: object): [string[], object[]] {
const flattened = {};
const temp = flatten(obj) as object;
for (const k of Object.keys(temp)) {
// Get 2nd last key, taken from https://stackoverflow.com/a/5555607
const pos = k.lastIndexOf('.');
const secondLast = k.substring(0, pos);
const last = k.substring(pos + 1);
if (flattened[secondLast] === undefined) {
flattened[secondLast] = {};
}
flattened[secondLast][last] = temp[k];
}
const keys = Object.keys(flattened).sort();
// @ts-ignore
const values = keys.map((k) => flattened[k]);
return [keys, values];
}
/**
* flatten till 2nd last key where the last key is an array of un-nested object
* @param obj
*/
export function flattenPredicatesInSpec(obj: object): [string[], object[][]] {
// matches text of form `<string>.<number>.<string>`
const re = /(.+)\.(\d+)\.(.+)/i;
const flattened = {};
const temp = flatten(obj) as object;
const tempMap: Map<string, [Map<number, Map<string, any>>, number]> = new Map();
for (const k of Object.keys(temp)) {
let matched = k.match(re);
if (!Array.isArray(matched)) {
if (temp[k] !== null && temp[k] !== undefined) {
// If temp[k] is null or undefined then encountered an array item which had no predicate
throw new Error(`Regex couldn't match key ${k}`);
}
} else {
const key = matched[1];
const arrayIdx = parseInt(matched[2]);
const innerObjKey = matched[3];
const tempMapValue = tempMap.get(key);
if (tempMapValue === undefined) {
const m = new Map();
const m1 = new Map();
m1.set(innerObjKey, temp[k]);
m.set(arrayIdx, m1);
tempMap.set(key, [m, arrayIdx]);
} else {
if (arrayIdx > tempMapValue[1]) {
tempMapValue[1] = arrayIdx;
}
const m1 = tempMapValue[0].get(arrayIdx);
if (m1 === undefined) {
const m1 = new Map();
m1.set(innerObjKey, temp[k]);
tempMapValue[0].set(arrayIdx, m1);
} else {
m1.set(innerObjKey, temp[k]);
}
}
}
}
for (const [key, val] of tempMap.entries()) {
const arr: object[] = new Array(val[1]);
for (const [idx, inner] of val[0].entries()) {
const obj = {};
for (const [ik, iv] of inner.entries()) {
obj[ik] = iv;
}
arr[idx] = obj;
}
flattened[key] = arr;
}
const keys = Object.keys(flattened).sort();
// @ts-ignore
const values = keys.map((k) => flattened[k]);
return [keys, values];
}
export function createWitEq(
eql: AttributeEquality,
flattenedSchemas: FlattenedSchema[]
): [WitnessEqualityMetaStatement, number[]] {
const witnessEq = new WitnessEqualityMetaStatement();
const attrIndices: number[] = [];
for (const [cIdx, name] of eql) {
const i = flattenedSchemas[cIdx][0].indexOf(name);
if (i === -1) {
throw new Error(`Attribute name ${name} was not found`);
}
attrIndices.push(i);
witnessEq.addWitnessRef(cIdx, i);
}
return [witnessEq, attrIndices];
}
export function createWitEqForBlindedCred(
statementIdx: number,
attrIdx: number,
attrRefs: AttributeRef[],
flattenedSchemas: FlattenedSchema[]
): WitnessEqualityMetaStatement {
const witnessEq = new WitnessEqualityMetaStatement();
witnessEq.addWitnessRef(statementIdx, attrIdx);
for (const [cIdx, name] of attrRefs) {
const i = flattenedSchemas[cIdx][0].indexOf(name);
if (i === -1) {
throw new Error(`Attribute name ${name} was not found`);
}
witnessEq.addWitnessRef(cIdx, i);
}
return witnessEq;
}
export function paramsClassBySignature(signature: Signature): SignatureParamsClass | null {
if (signature instanceof BBSSignature) {
return BBSSignatureParams;
} else if (signature instanceof BBSPlusSignatureG1) {
return BBSPlusSignatureParamsG1;
} else if (signature instanceof PSSignature) {
return PSSignatureParams;
} else if (signature instanceof BBDT16Mac) {
return BBDT16MacParams;
} else {
return null;
}
}
export function paramsClassByPublicKey(pk: PublicKey): SignatureParamsClass | null {
if (pk instanceof BBSPublicKey) {
return BBSSignatureParams;
} else if (pk instanceof BBSPlusPublicKeyG2) {
return BBSPlusSignatureParamsG1;
} else if (pk instanceof PSPublicKey) {
return PSSignatureParams;
} else {
return null;
}
}
export function buildSignatureVerifierStatementFromParamsRef(
setupParamsTrk: SetupParamsTracker,
sigParams: SignatureParams,
messageCount: number,
revealedMessages: Map<number, Uint8Array>,
credVerParam?: CredentialVerificationParam,
useConstantTimeEncoding = true
): Uint8Array {
let setupSigP: SetupParam,
setupPK: SetupParam | undefined,
buildStatement:
| ((
sigParamsRef: number,
publicKeyRef: number,
revealedMessages: Map<number, Uint8Array>,
encodeMessages: boolean
) => Uint8Array)
| ((sigParamsRef: number, revealedMessages: Map<number, Uint8Array>, encodeMessages: boolean) => Uint8Array);
function getPk(): PublicKey {
if (credVerParam === undefined) {
throw new Error('Public key needs to be provided for BBS signatures');
}
const pk = credVerParam as PublicKey;
if (paramsClassByPublicKey(pk) !== sigParams.constructor) {
throw new Error(`Public key and params have different schemes: ${credVerParam}, ${sigParams}`);
}
return pk;
}
switch (sigParams.constructor) {
case BBSSignatureParams:
setupSigP = SetupParam.bbsSignatureParams(sigParams.adapt(messageCount) as BBSSignatureParams);
setupPK = SetupParam.bbsPlusSignaturePublicKeyG2(getPk());
buildStatement = useConstantTimeEncoding
? Statement.bbsSignatureVerifierFromSetupParamRefsConstantTime
: Statement.bbsSignatureVerifierFromSetupParamRefs;
return buildStatement(setupParamsTrk.add(setupSigP), setupParamsTrk.add(setupPK), revealedMessages, false);
case BBSPlusSignatureParamsG1:
setupPK = SetupParam.bbsPlusSignaturePublicKeyG2(getPk());
setupSigP = SetupParam.bbsPlusSignatureParamsG1(sigParams.adapt(messageCount) as BBSPlusSignatureParamsG1);
buildStatement = useConstantTimeEncoding
? Statement.bbsPlusSignatureVerifierFromSetupParamRefsConstantTime
: Statement.bbsPlusSignatureVerifierFromSetupParamRefs;
return buildStatement(setupParamsTrk.add(setupSigP), setupParamsTrk.add(setupPK), revealedMessages, false);
case PSSignatureParams:
let psPK = getPk() as PSPublicKey;
const supported = psPK.supportedMessageCount();
if (messageCount !== supported) {
if (messageCount < supported) {
psPK = psPK.adaptForLess(messageCount);
} else {
throw new Error(`Unsupported message count - supported up to ${supported}, received = ${messageCount}`);
}
}
setupPK = SetupParam.psSignaturePublicKey(psPK);
setupSigP = SetupParam.psSignatureParams(sigParams.adapt(messageCount) as PSSignatureParams);
buildStatement = useConstantTimeEncoding
? Statement.psSignatureFromSetupParamRefsConstantTime
: Statement.psSignatureFromSetupParamRefs;
return buildStatement(setupParamsTrk.add(setupSigP), setupParamsTrk.add(setupPK), revealedMessages, false);
case BBDT16MacParams:
setupSigP = SetupParam.bbdt16MacParams(sigParams.adapt(messageCount) as BBDT16MacParams);
if (credVerParam instanceof BBDT16MacSecretKey) {
return useConstantTimeEncoding
? Statement.bbdt16MacFullVerifierFromSetupParamRefsConstantTime(
setupParamsTrk.add(setupSigP),
credVerParam,
revealedMessages,
false
)
: Statement.bbdt16MacFullVerifierFromSetupParamRefs(
setupParamsTrk.add(setupSigP),
credVerParam,
revealedMessages,
false
);
} else {
return useConstantTimeEncoding
? Statement.bbdt16MacFromSetupParamRefsConstantTime(setupParamsTrk.add(setupSigP), revealedMessages, false)
: Statement.bbdt16MacFromSetupParamRefs(setupParamsTrk.add(setupSigP), revealedMessages, false);
}
default:
throw new Error(`Signature params are invalid ${sigParams}`);
}
}
export function buildSignatureProverStatementFromParamsRef(
setupParamsTrk: SetupParamsTracker,
sigParams: SignatureParams,
messageCount: number,
revealedMessages: Map<number, Uint8Array>,
pk?: PublicKey,
useConstantTimeEncoding = true
): Uint8Array {
if (pk !== undefined && paramsClassByPublicKey(pk) !== sigParams.constructor) {
throw new Error(`Public key and params have different schemes: ${pk}, ${sigParams}`);
}
let setupParams: SetupParam,
setupPK: SetupParam | undefined,
buildStatement:
| ((
sigParamsRef: number,
publicKeyRef: number,
revealedMessages: Map<number, Uint8Array>,
encodeMessages: boolean
) => Uint8Array)
| ((sigParamsRef: number, revealedMessages: Map<number, Uint8Array>, encodeMessages: boolean) => Uint8Array);
switch (sigParams.constructor) {
case BBSSignatureParams:
setupParams = SetupParam.bbsSignatureParams(sigParams.adapt(messageCount) as BBSSignatureParams);
buildStatement = useConstantTimeEncoding
? Statement.bbsSignatureProverFromSetupParamRefsConstantTime
: Statement.bbsSignatureProverFromSetupParamRefs;
break;
case BBSPlusSignatureParamsG1:
setupParams = SetupParam.bbsPlusSignatureParamsG1(sigParams.adapt(messageCount) as BBSPlusSignatureParamsG1);
buildStatement = useConstantTimeEncoding
? Statement.bbsPlusSignatureProverFromSetupParamRefsConstantTime
: Statement.bbsPlusSignatureProverFromSetupParamRefs;
break;
case PSSignatureParams:
if (pk === undefined) {
throw new Error('Public key should be provided for PS signature');
}
let psPK = pk as PSPublicKey;
const supported = psPK.supportedMessageCount();
if (messageCount !== supported) {
if (messageCount < supported) {
psPK = psPK.adaptForLess(messageCount);
} else {
throw new Error(`Unsupported message count - supported up to ${supported}, received = ${messageCount}`);
}
}
setupPK = SetupParam.psSignaturePublicKey(psPK);
setupParams = SetupParam.psSignatureParams(sigParams.adapt(messageCount) as PSSignatureParams);
buildStatement = useConstantTimeEncoding
? Statement.psSignatureFromSetupParamRefsConstantTime
: Statement.psSignatureFromSetupParamRefs;
break;
case BBDT16MacParams:
setupParams = SetupParam.bbdt16MacParams(sigParams.adapt(messageCount) as BBDT16MacParams);
buildStatement = useConstantTimeEncoding
? Statement.bbdt16MacFromSetupParamRefsConstantTime
: Statement.bbdt16MacFromSetupParamRefs;
break;
default:
throw new Error(`${sigParams.constructor.name} signature params are invalid`);
}
return setupPK !== undefined
? // @ts-ignore
buildStatement(setupParamsTrk.add(setupParams), setupParamsTrk.add(setupPK), revealedMessages, false)
: // @ts-ignore
buildStatement(setupParamsTrk.add(setupParams), revealedMessages, false);
}
/**
* Returns default label bytes for the signature params class (if any).
* @param signatureParamsClass
* @returns
*/
export function getDefaultLabelBytesForSignatureParams(signatureParamsClass: SignatureParamsClass): Uint8Array | null {
switch (signatureParamsClass) {
case BBSSignatureParams:
return BBS_SIGNATURE_PARAMS_LABEL_BYTES;
case BBSPlusSignatureParamsG1:
return BBS_PLUS_SIGNATURE_PARAMS_LABEL_BYTES;
case PSSignatureParams:
return PS_SIGNATURE_PARAMS_LABEL_BYTES;
case BBDT16MacParams:
return BBDT16_MAC_PARAMS_LABEL_BYTES;
default:
return null;
}
}
export function buildWitness(signature: Signature, unrevealedMessages: Map<number, Uint8Array>): Uint8Array {
if (signature instanceof BBSSignature) {
return Witness.bbsSignatureConstantTime(signature, unrevealedMessages, false);
} else if (signature instanceof BBSPlusSignatureG1) {
return Witness.bbsPlusSignatureConstantTime(signature, unrevealedMessages, false);
} else if (signature instanceof PSSignature) {
return Witness.psSignatureConstantTime(signature, unrevealedMessages);
} else if (signature instanceof BBDT16Mac) {
return Witness.bbdt16MacConstantTime(signature, unrevealedMessages, false);
} else {
// @ts-ignore
throw new Error(`${signature.constructor.name} signature is invalid`);
}
}
/**
* Returns signature params adapted for the provided message count reusing them from the provided map. Acts as a cache lookup
* and can update the cache.
* @param sigParamsByScheme - The cache of signature params
* @param paramsClass
* @param msgCount
*/
export const getSignatureParamsForMsgCount = (
sigParamsByScheme: Map<SignatureParamsClass, { params: SignatureParams; msgCount: number }>,
paramsClass: SignatureParamsClass,
msgCount: number
): SignatureParams => {
let sigParamsEntry = sigParamsByScheme.get(paramsClass);
if (sigParamsEntry === void 0) {
const labelBytes = getDefaultLabelBytesForSignatureParams(paramsClass);
if (labelBytes === null) {
throw new Error(`Failed to get default label bytes for signature params: ${paramsClass}`);
}
sigParamsEntry = {
params: paramsClass.generate(msgCount, labelBytes),
msgCount
};
sigParamsByScheme.set(paramsClass, sigParamsEntry);
return sigParamsEntry.params;
}
const currentMsgCount = sigParamsEntry.msgCount;
if (msgCount !== currentMsgCount) {
sigParamsEntry = { params: sigParamsEntry.params.adapt(msgCount), msgCount };
if (msgCount > currentMsgCount) {
sigParamsByScheme.set(paramsClass, sigParamsEntry);
}
}
return sigParamsEntry.params;
};
export function saverStatement(
forProver: boolean,
chunkBitSize: number,
commKeyId: string,
encKeyId: string,
snarkKeyId: string,
commKeys: PredicateParamType,
encKey: PredicateParamType,
snarkKey: PredicateParamType,
setupParamsTrk: SetupParamsTracker
): Uint8Array {
if (
commKeys instanceof SaverChunkedCommitmentKeyUncompressed &&
encKey instanceof SaverEncryptionKeyUncompressed &&
((forProver && snarkKey instanceof SaverProvingKeyUncompressed) ||
(!forProver && snarkKey instanceof SaverVerifyingKeyUncompressed))
) {
if (!setupParamsTrk.hasEncryptionGensUncompressed()) {
setupParamsTrk.addEncryptionGensUncompressed();
}
if (!setupParamsTrk.isTrackingParam(commKeyId)) {
setupParamsTrk.addForParamId(commKeyId, SetupParam.saverCommitmentKeyUncompressed(commKeys));
}
if (!setupParamsTrk.isTrackingParam(encKeyId)) {
setupParamsTrk.addForParamId(encKeyId, SetupParam.saverEncryptionKeyUncompressed(encKey));
}
if (!setupParamsTrk.isTrackingParam(snarkKeyId)) {
setupParamsTrk.addForParamId(
snarkKeyId,
forProver
? SetupParam.saverProvingKeyUncompressed(snarkKey)
: SetupParam.saverVerifyingKeyUncompressed(snarkKey)
);
}
} else if (
commKeys instanceof SaverChunkedCommitmentKey &&
encKey instanceof SaverEncryptionKey &&
((forProver && snarkKey instanceof SaverProvingKey) || (!forProver && snarkKey instanceof SaverVerifyingKey))
) {
if (!setupParamsTrk.hasEncryptionGensCompressed()) {
setupParamsTrk.addEncryptionGensCompressed();
}
if (!setupParamsTrk.isTrackingParam(commKeyId)) {
setupParamsTrk.addForParamId(commKeyId, SetupParam.saverCommitmentKey(commKeys));
}
if (!setupParamsTrk.isTrackingParam(encKeyId)) {
setupParamsTrk.addForParamId(encKeyId, SetupParam.saverEncryptionKey(encKey));
}
if (!setupParamsTrk.isTrackingParam(snarkKeyId)) {
setupParamsTrk.addForParamId(
snarkKeyId,
forProver ? SetupParam.saverProvingKey(snarkKey as SaverProvingKey) : SetupParam.saverVerifyingKey(snarkKey)
);
}
} else {
throw new Error('All SAVER parameters should either be compressed in uncompressed');
}
return forProver
? Statement.saverProverFromSetupParamRefs(
setupParamsTrk.encGensIdx,
setupParamsTrk.indexForParam(commKeyId),
setupParamsTrk.indexForParam(encKeyId),
setupParamsTrk.indexForParam(snarkKeyId),
chunkBitSize
)
: Statement.saverVerifierFromSetupParamRefs(
setupParamsTrk.encGensIdx,
setupParamsTrk.indexForParam(commKeyId),
setupParamsTrk.indexForParam(encKeyId),
setupParamsTrk.indexForParam(snarkKeyId),
chunkBitSize
);
}