-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathps.spec.ts
341 lines (294 loc) · 11.1 KB
/
ps.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
import {
psAdaptPublicKeyForLessMessages,
psAdaptSecretKeyForLessMessages,
psAdaptSecretKeyForMoreMessages,
psChallengeMessagesPoKContributionFromProof,
psChallengeMessagesPoKContributionFromProtocol,
encodeMessageForSigning,
psGenMessagesPoK,
psInitializeMessagesPoK,
psPublicKeyMaxSupportedMsgs,
psSigningKeyMaxSupportedMsgs,
psVerifyMessagesPoK,
} from "../../lib";
import {
psGenerateSignatureParams,
psIsSignatureParamsValid,
psSignatureParamsMaxSupportedMsgs,
psGenerateSigningKey,
psGeneratePublicKey,
psIsPublicKeyValid,
psSign,
psVerify,
generateRandomFieldElement,
psMessageCommitment,
psBlindSign,
psUnblindSignature,
psInitializeSignaturePoK,
psGenSignaturePoK,
psVerifySignaturePoK,
psChallengeSignaturePoKContributionFromProtocol,
psChallengeSignaturePoKContributionFromProof,
generateChallengeFromBytes,
psAdaptSignatureParamsForMsgCount,
psSignatureParamsFromBytes,
psSignatureParamsToBytes,
PSSigParams,
initializeWasm,
PSCommitmentOrMessage,
} from "../../lib";
import { generateRandomG1Element } from "../../lib/dock_crypto_wasm";
import {stringToBytes} from "./util";
describe("For PS signatures", () => {
let sigParams: PSSigParams, sk: Uint8Array, pk: Uint8Array, messages, h;
const seed = new Uint8Array([0, 2, 3, 4, 5]);
const messageCount = 6;
beforeAll(async () => {
await initializeWasm();
h = generateRandomG1Element();
messages = [
stringToBytes("Message1"),
stringToBytes("Message2"),
stringToBytes("Message3"),
stringToBytes("Message4"),
stringToBytes("Message4"),
stringToBytes("Message6"),
].map(encodeMessageForSigning);
sigParams = psGenerateSignatureParams(
messageCount,
stringToBytes("test label")
);
sk = psGenerateSigningKey(messageCount);
pk = psGeneratePublicKey(sk, sigParams);
});
it("checks key generation", () => {
const sk_ = psGenerateSigningKey(6);
expect(psSigningKeyMaxSupportedMsgs(sk_)).toBe(6);
expect(sk_).toBeInstanceOf(Uint8Array);
const sk1 = psGenerateSigningKey(6, seed);
expect(sk1).toBeInstanceOf(Uint8Array);
const sk2 = psGenerateSigningKey(6, seed);
expect(sk2).toBeInstanceOf(Uint8Array);
expect(sk1).toEqual(sk2);
});
it("generate signature params", () => {
expect(() => psGenerateSignatureParams(-5)).toThrow();
expect(() => psGenerateSignatureParams(6.3)).toThrow();
const params0 = psGenerateSignatureParams(messageCount);
expect(params0).toBeInstanceOf(Object);
expect(params0.h.length).toEqual(messageCount);
expect(psIsSignatureParamsValid(params0)).toBe(true);
const label = stringToBytes("Sig params g1");
const params = psGenerateSignatureParams(messageCount, label);
expect(params).toBeInstanceOf(Object);
expect(params.h.length).toEqual(messageCount);
expect(psIsSignatureParamsValid(params)).toBe(true);
expect(psSignatureParamsMaxSupportedMsgs(params)).toBe(messageCount);
const bytes = psSignatureParamsToBytes(params);
const deserzParams = psSignatureParamsFromBytes(bytes);
expect(params).toEqual(deserzParams);
});
it("generate signature params", () => {
expect(() => psGenerateSignatureParams(-5)).toThrow();
expect(() => psGenerateSignatureParams(6.3)).toThrow();
const params0 = psGenerateSignatureParams(messageCount);
expect(params0).toBeInstanceOf(Object);
expect(params0.h.length).toEqual(messageCount);
expect(psIsSignatureParamsValid(params0)).toBe(true);
const params = psGenerateSignatureParams(messageCount);
expect(params).toBeInstanceOf(Object);
expect(params.h.length).toEqual(messageCount);
expect(psIsSignatureParamsValid(params)).toBe(true);
expect(psSignatureParamsMaxSupportedMsgs(params)).toBe(messageCount);
const bytes = psSignatureParamsToBytes(params);
const deserzParams = psSignatureParamsFromBytes(bytes);
expect(params).toEqual(deserzParams);
});
it("generate public key from secret key", () => {
expect(psPublicKeyMaxSupportedMsgs(pk)).toBe(messageCount);
expect(pk).toBeInstanceOf(Uint8Array);
expect(psIsPublicKeyValid(pk)).toBe(true);
});
it("generate and verify signature", () => {
const sig = psSign(messages, sk, sigParams);
const res = psVerify(messages, sig, pk, sigParams);
expect(res.verified).toBe(true);
});
it("generate and verify signature", () => {
const sig = psSign(messages, sk, sigParams);
const res = psVerify(messages, sig, pk, sigParams);
expect(res.verified).toBe(true);
});
it("extend signature params", () => {
const label = stringToBytes("Sig params g1");
const params0 = psGenerateSignatureParams(1);
expect(psSignatureParamsMaxSupportedMsgs(params0)).toBe(1);
const params1 = psAdaptSignatureParamsForMsgCount(params0, label, 5);
expect(psSignatureParamsMaxSupportedMsgs(params1)).toBe(5);
expect(psIsSignatureParamsValid(params1)).toBe(true);
expect(params0.h[0]).toEqual(params1.h[0]);
const params2 = psAdaptSignatureParamsForMsgCount(params1, label, 2);
expect(psSignatureParamsMaxSupportedMsgs(params2)).toBe(2);
expect(psIsSignatureParamsValid(params2)).toBe(true);
expect(params1.h[0]).toEqual(params2.h[0]);
expect(params1.h[1]).toEqual(params2.h[1]);
});
it("extend signature params", () => {
const label = stringToBytes("Sig params g2");
const params0 = psGenerateSignatureParams(1);
expect(psSignatureParamsMaxSupportedMsgs(params0)).toBe(1);
const params1 = psAdaptSignatureParamsForMsgCount(params0, label, 5);
expect(psSignatureParamsMaxSupportedMsgs(params1)).toBe(5);
expect(psIsSignatureParamsValid(params1)).toBe(true);
expect(params0.h[0]).toEqual(params1.h[0]);
const params2 = psAdaptSignatureParamsForMsgCount(params1, label, 2);
expect(psSignatureParamsMaxSupportedMsgs(params2)).toBe(2);
expect(psIsSignatureParamsValid(params2)).toBe(true);
expect(params1.h[0]).toEqual(params2.h[0]);
expect(params1.h[1]).toEqual(params2.h[1]);
});
it("adapt secret key", () => {
const seed = stringToBytes("123");
const sk5 = psGenerateSigningKey(5, seed);
expect(psSigningKeyMaxSupportedMsgs(sk5)).toBe(5);
const sk2 = psAdaptSecretKeyForLessMessages(sk5, 2)!;
expect(psAdaptSecretKeyForLessMessages(sk5, 6)).toBe(undefined);
expect(psSigningKeyMaxSupportedMsgs(sk2)).toBe(2);
const sk7 = psAdaptSecretKeyForMoreMessages(sk5, seed, 7)!;
expect(psSigningKeyMaxSupportedMsgs(sk7)).toBe(7);
expect(sk7).toEqual(psGenerateSigningKey(7, seed));
});
it("adapt public key", () => {
const sk = psGenerateSigningKey(5);
const pk = psGeneratePublicKey(sk, sigParams);
expect(psPublicKeyMaxSupportedMsgs(pk)).toBe(5);
const pk1 = psAdaptPublicKeyForLessMessages(pk, 1)!;
expect(psAdaptPublicKeyForLessMessages(pk, messageCount + 1)).toBe(
undefined
);
expect(psPublicKeyMaxSupportedMsgs(pk1)).toBe(1);
});
it("generate and verify a blind signature", () => {
// Commit to message indices 1 and 5
const commitIndices = new Set([1, 5]);
const usedBlindings = new Map();
const msgOrComs: PSCommitmentOrMessage[] = [];
for (let i = 0; i < messages.length; i++) {
let msgOrCom;
if (commitIndices.has(i)) {
const msg = messages[i];
const blinding = generateRandomFieldElement();
msgOrCom = {
BlindedMessage: psMessageCommitment(msg, blinding, h, sigParams.g),
};
usedBlindings.set(i, blinding);
} else {
msgOrCom = {
RevealedMessage: messages[i],
};
}
msgOrComs.push(msgOrCom);
}
const blindSig = psBlindSign(msgOrComs, sk, h);
const sig = psUnblindSignature(blindSig, usedBlindings, pk, h);
const res = psVerify(messages, sig, pk, sigParams);
expect(res.verified).toBe(true);
});
it("generate a proof of knowledge of signature", () => {
const sig = psSign(messages, sk, sigParams);
const res = psVerify(messages, sig, pk, sigParams);
expect(res.verified).toBe(true);
// Prover reveals message indices 0 and 2 and supplies blindings for message indices 1, 4 and 5
const blindingsMap = new Map();
const revealedMsgs = new Map();
blindingsMap.set(1, generateRandomFieldElement());
blindingsMap.set(4, generateRandomFieldElement());
blindingsMap.set(5, generateRandomFieldElement());
revealedMsgs.set(0, messages[0]);
revealedMsgs.set(2, messages[2]);
revealedMsgs.set(3, messages[3]);
const protocol = psInitializeSignaturePoK(
sig,
sigParams,
pk,
messages.map((message, idx) => {
const blinding = blindingsMap.get(idx);
return blinding != void 0
? { BlindMessageWithConcreteBlinding: { message, blinding } }
: "RevealMessage";
})
);
const pBytes = psChallengeSignaturePoKContributionFromProtocol(
protocol,
pk,
sigParams
);
expect(pBytes).toBeInstanceOf(Uint8Array);
const proverChallenge = generateChallengeFromBytes(pBytes);
const proof = psGenSignaturePoK(protocol, proverChallenge);
const vBytes = psChallengeSignaturePoKContributionFromProof(
proof,
pk,
sigParams
);
expect(vBytes).toBeInstanceOf(Uint8Array);
expect(pBytes).toEqual(vBytes);
const verifierChallenge = generateChallengeFromBytes(vBytes);
expect(proverChallenge).toEqual(verifierChallenge);
const result = psVerifySignaturePoK(
proof,
revealedMsgs,
verifierChallenge,
pk,
sigParams
);
expect(result.verified).toBe(true);
});
it("generate a proof of knowledge of messages", () => {
// Prover reveals message indices 0 and 2 and supplies blindings for message indices 1, 4 and 5
const blindingsMap = new Map();
const revealedMsgs = new Map();
const h = generateRandomG1Element();
blindingsMap.set(1, generateRandomFieldElement());
blindingsMap.set(4, generateRandomFieldElement());
blindingsMap.set(5, generateRandomFieldElement());
revealedMsgs.set(0, messages[0]);
revealedMsgs.set(2, messages[2]);
revealedMsgs.set(3, messages[3]);
const protocol = psInitializeMessagesPoK(
messages.map((message, idx) => {
const blinding = blindingsMap.get(idx);
return blinding != void 0
? { BlindMessageWithConcreteBlinding: { message, blinding } }
: "RevealMessage";
}),
sigParams,
h
);
const pBytes = psChallengeMessagesPoKContributionFromProtocol(
protocol,
sigParams,
h
);
expect(pBytes).toBeInstanceOf(Uint8Array);
const proverChallenge = generateChallengeFromBytes(pBytes);
const proof = psGenMessagesPoK(protocol, proverChallenge);
const vBytes = psChallengeMessagesPoKContributionFromProof(
proof,
sigParams,
h
);
expect(vBytes).toBeInstanceOf(Uint8Array);
expect(pBytes).toEqual(vBytes);
const verifierChallenge = generateChallengeFromBytes(vBytes);
expect(proverChallenge).toEqual(verifierChallenge);
const result = psVerifyMessagesPoK(
proof,
new Set(revealedMsgs.keys()),
verifierChallenge,
sigParams,
h
);
expect(result.verified).toBe(true);
});
});