-
Notifications
You must be signed in to change notification settings - Fork 9
/
sign-verify-js-objs.ts
364 lines (333 loc) · 12.6 KB
/
sign-verify-js-objs.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
// Utilities for signing and proving when working with messages as JS objects.
import { flatten, unflatten } from 'flat';
import { Encoder } from './encoder';
import { Statement, WitnessEqualityMetaStatement } from './composite-proof/statement';
import { BBSPlusBlindSignatureRequest, BBSPlusSignatureParamsG1 } from './bbs-plus';
import { BBSBlindSignatureRequest, BBSSignatureParams } from './bbs';
import { PSBlindSignatureRequest, PSSignatureParams } from './ps';
import { Witness } from './composite-proof/witness';
import { ISignatureParams, MessageStructure } from './types';
import { encodeMessageForSigningInConstantTime } from 'crypto-wasm-new';
import { BBDT16BlindMacRequest, BBDT16MacParams } from './bbdt16-mac';
export function flattenMessageStructure(msgStructure: MessageStructure): object {
return flatten(msgStructure);
}
export function getAdaptedSignatureParamsForMessages<Params extends ISignatureParams>(
params: Params,
msgStructure: MessageStructure
): Params {
const flattened = flattenMessageStructure(msgStructure);
return params.adapt(Object.keys(flattened).length);
}
/**
* Encodes revealed messages producing an object with names as keys and encoded messages as values and a map with
* indices as keys and encoded messages as values.
* Also returns the total amount of names produced by flattening a msg structure.
* @param revealedMessages - The messages known to the signer
* @param blindedMessageCount
* @param msgStructure
* @param encoder
*/
export function encodeRevealedMessageObject(
revealedMessages: object,
blindedMessageCount: number,
msgStructure: MessageStructure,
encoder: Encoder
): { encodedByName: { [key: string]: Uint8Array }; encodedByIndex: Map<number, Uint8Array>; total: number } {
const flattenedAllNames = Object.keys(flattenMessageStructure(msgStructure)).sort();
const encodedByName = encoder.encodeMessageObjectAsObjectConstantTime(revealedMessages);
const encodedByIndex = new Map<number, Uint8Array>();
flattenedAllNames.forEach((n, i) => {
const msg = encodedByName[n];
if (msg !== void 0) {
encodedByIndex.set(i, msg);
}
});
const encodedMessageCount = encodedByIndex.size;
if (encodedMessageCount !== Object.keys(encodedByName).length) {
throw new Error(
`Message structure incompatible with revealedMessages. Got ${encodedMessageCount} to encode but encoded only ${
Object.keys(encodedByName).length
}`
);
}
if (flattenedAllNames.length !== encodedMessageCount + blindedMessageCount) {
throw new Error(
`Message structure likely incompatible with revealedMessages and blindSigRequest. ${flattenedAllNames.length} != (${encodedMessageCount} + ${blindedMessageCount})`
);
}
return { encodedByName, encodedByIndex, total: flattenedAllNames.length };
}
/**
* Gives `SignatureParams` that can sign `msgCount` number of messages.
* @param SignatureParamsClass
* @param msgCount
* @param labelOrParams
*/
export function getSigParamsOfRequiredSize<S extends ISignatureParams>(
SignatureParamsClass: { new (...args): S; generate(msgCount: number, labelOrParams?: Uint8Array): S },
msgCount: number,
labelOrParams: Uint8Array | S
): S {
let sigParams: S;
if (labelOrParams instanceof SignatureParamsClass) {
if (labelOrParams.supportedMessageCount() !== msgCount) {
if (labelOrParams.label === undefined) {
throw new Error(`Signature params mismatch, needed ${msgCount}, got ${labelOrParams.supportedMessageCount()}`);
} else {
sigParams = labelOrParams.adapt(msgCount);
}
} else {
sigParams = labelOrParams;
}
} else {
sigParams = SignatureParamsClass.generate(msgCount, labelOrParams as Uint8Array);
}
return sigParams;
}
/**
* Given the messages as a JS object and the names (use "." for nested property names) of the messages to reveal, returns
* the encoded messages to reveal and hide as separate maps with the key being the index of the message when the object is
* flattened.
* @param messages
* @param revealedMsgNames
* @param encoder
* @param useConstantTimeEncoding
*/
export function getRevealedAndUnrevealed(
messages: object,
revealedMsgNames: Set<string>,
encoder: Encoder,
useConstantTimeEncoding = true
): [Map<number, Uint8Array>, Map<number, Uint8Array>, object] {
const [names, encodedValues] = useConstantTimeEncoding
? encoder.encodeMessageObjectConstantTime(messages)
: encoder.encodeMessageObject(messages);
const revealedMsgs = new Map<number, Uint8Array>();
const unrevealedMsgs = new Map<number, Uint8Array>();
let found = 0;
for (let i = 0; i < names.length; i++) {
if (revealedMsgNames.has(names[i])) {
revealedMsgs.set(i, encodedValues[i]);
found++;
} else {
unrevealedMsgs.set(i, encodedValues[i]);
}
}
if (revealedMsgNames.size !== found) {
throw new Error(
`Some of the revealed message names were not found in the given messages object, ${
revealedMsgNames.size - found
} extra names found`
);
}
// This will be given to the verifier to encode independently.
const revealedMsgsRaw: { [key: string]: unknown } = {};
const flattened = flatten(messages);
// @ts-ignore
revealedMsgNames.forEach((n: string) => (revealedMsgsRaw[n] = flattened[n]));
return [revealedMsgs, unrevealedMsgs, unflatten(revealedMsgsRaw)];
}
/**
* Used by the verifier to encode the revealed messages given by the prover.
* @param revealedMsgsRaw - Revealed messages given by the prover.
* @param msgStructure - Message structure, i.e. the structure of JS object with key names but values redacted.
* @param encoder
*/
export function encodeRevealedMsgs(
revealedMsgsRaw: object,
msgStructure: MessageStructure,
encoder: Encoder
): Map<number, Uint8Array> {
const revealed = new Map<number, Uint8Array>();
const names = Object.keys(flattenMessageStructure(msgStructure)).sort();
const flattenedRevealed = flatten(revealedMsgsRaw) as object;
Object.entries(flattenedRevealed).forEach(([n, v]) => {
const i = names.indexOf(n);
if (i === -1) {
throw new Error(`Message name ${n} was not found`);
}
revealed.set(i, encoder.encodeMessageConstantTime(n, v));
});
return revealed;
}
/**
* Check if the given structure is compatible with the given messages object.
* @param messages
* @param msgStructure
*/
export function isValidMsgStructure(messages: object, msgStructure: MessageStructure): boolean {
const namesInStruct = Object.keys(flattenMessageStructure(msgStructure)).sort();
const namesInMsgs = Object.keys(flatten(messages)).sort();
return (
namesInMsgs.length === namesInStruct.length &&
(() => {
for (let i = 0; i <= namesInMsgs.length; i++) {
if (namesInStruct[i] !== namesInMsgs[i]) {
return false;
}
}
return true;
})()
);
}
/**
* Flattens the object `msgStructure` and returns the indices of names given in `msgNames`
* @param msgNames
* @param msgStructure
* @returns Returns in same order as given names in `msgNames`
*/
export function getIndicesForMsgNames(msgNames: string[], msgStructure: MessageStructure): number[] {
const allNames = Object.keys(flattenMessageStructure(msgStructure)).sort();
return msgNames.map((n) => {
const i = allNames.indexOf(n);
if (i === -1) {
throw new Error(`Message name ${n} was not found`);
}
return i;
});
}
/**
* Takes an equality of messages across statements and returns the `MetaStatement` to be used in the proof.
* @param equality - Map with key as the statement index and value as the message names of that statement
* that are to be proved equal and the message structure.
*/
export function createWitnessEqualityMetaStatement(
equality: Map<number, [msgNames: string[], msgStructure: MessageStructure]>
): WitnessEqualityMetaStatement {
const ms = new WitnessEqualityMetaStatement();
for (const [sIdx, [names, struct]] of equality.entries()) {
const indices = getIndicesForMsgNames(names, struct);
indices.forEach((i) => ms.addWitnessRef(sIdx, i));
}
return ms;
}
/**
* Get the `BBS` statement to be used in composite proof for the blind signature request
* @param request
* @param sigParams
*/
export function getBBSStatementForBlindSigRequest(
request: BBSBlindSignatureRequest,
sigParams: BBSSignatureParams
): Uint8Array {
const commKey = sigParams.getParamsForIndices(request.blindedIndices);
return Statement.pedersenCommitmentG1(commKey, request.commitment);
}
/**
* Get the `BBS` witness to be used in composite proof for the blind signature request
* @param messages
*/
export function getBBSWitnessForBlindSigRequest(messages: Map<number, Uint8Array>): Uint8Array {
const sortedMessages = [...messages.entries()];
sortedMessages.sort(([a], [b]) => a - b);
return Witness.pedersenCommitment(sortedMessages.map(([_, m]) => m));
}
/**
* Get the `BBS+` statement to be used in composite proof for the blind signature request
* @param request
* @param sigParams
*/
export function getBBSPlusStatementForBlindSigRequest(
request: BBSPlusBlindSignatureRequest,
sigParams: BBSPlusSignatureParamsG1
): Uint8Array {
const commKey = sigParams.getParamsForIndices(request.blindedIndices);
return Statement.pedersenCommitmentG1(commKey, request.commitment);
}
/**
* Get the `BBS+` witness to be used in composite proof for the blind signature request
* @param messages
* @param blinding
*/
export function getBBSPlusWitnessForBlindSigRequest(
messages: Map<number, Uint8Array>,
blinding: Uint8Array
): Uint8Array {
const sortedMessages = [...messages.entries()];
sortedMessages.sort(([a], [b]) => a - b);
return Witness.pedersenCommitment([blinding, ...sortedMessages.map(([_, m]) => m)]);
}
/**
* Get the `PS` statements to be used in composite proof for the blind signature request
* @param request
* @param sigParams
* @param h
*/
export function getPSStatementsForBlindSigRequest(
request: PSBlindSignatureRequest,
sigParams: PSSignatureParams,
h: Uint8Array
): Uint8Array[] {
const sortedCommitments = [...request.commitments.entries()].sort(([a], [b]) => a - b);
const hArr = sigParams.getParamsForIndices(sortedCommitments.map(([key]) => key));
return [
Statement.pedersenCommitmentG1([sigParams.value.g, ...hArr], request.commitment),
...sortedCommitments.map(([_, commitment]) => Statement.pedersenCommitmentG1([sigParams.value.g, h], commitment))
];
}
/**
* Get the `PS witnesses to be used in composite proof for the blind signature request
* @param messages
* @param blinding
* @param blindings
*/
export function getPSWitnessesForBlindSigRequest(
messages: Map<number, Uint8Array>,
blinding: Uint8Array,
blindings: Map<number, Uint8Array>
): Uint8Array[] {
const sortedMessages = [...messages.entries()].sort(([a], [b]) => a - b);
return [
Witness.pedersenCommitment([blinding, ...sortedMessages.map(([_, msg]) => msg)]),
...sortedMessages.map(([idx, msg]) => {
const blinding = blindings.get(idx);
if (blinding === void 0) throw new Error(`Missing blinding for ${idx}`);
return Witness.pedersenCommitment([blinding, msg]);
})
];
}
/**
* Get the `BBDT16` statement to be used in composite proof for the blind signature request
* @param request
* @param macParams
*/
export function getBBDT16StatementForBlindMacRequest(
request: BBDT16BlindMacRequest,
macParams: BBDT16MacParams
): Uint8Array {
const commKey = macParams.getParamsForIndices(request.blindedIndices);
return Statement.pedersenCommitmentG1(commKey, request.commitment);
}
/**
* Get the `BBDT16` witness to be used in composite proof for the blind signature request
* @param messages
* @param blinding
*/
export function getBBDT16WitnessForBlindMacRequest(
messages: Map<number, Uint8Array>,
blinding: Uint8Array
): Uint8Array {
const sortedMessages = [...messages.entries()];
sortedMessages.sort(([a], [b]) => a - b);
return Witness.pedersenCommitment([blinding, ...sortedMessages.map(([_, m]) => m)]);
}
export function getBlindedIndicesAndRevealedMessages(
messagesToBlind: Map<number, Uint8Array>,
encodeMessages: boolean,
revealedMessages?: Map<number, Uint8Array>
): [number[], Map<number, Uint8Array> | undefined] {
const blindedIndices: number[] = [];
for (const k of messagesToBlind.keys()) {
blindedIndices.push(k);
}
let encodedRevealedMessages: Map<number, Uint8Array> | undefined;
if (revealedMessages) {
encodedRevealedMessages = new Map();
for (const [idx, msg] of revealedMessages) {
encodedRevealedMessages.set(idx, encodeMessages ? encodeMessageForSigningInConstantTime(msg) : msg);
}
}
blindedIndices.sort((a, b) => a - b);
return [blindedIndices, encodedRevealedMessages];
}