forked from docknetwork/crypto-wasm-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variable-number-of-messages.spec.ts
80 lines (69 loc) · 3.64 KB
/
variable-number-of-messages.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
import { CompositeProof, initializeWasm, MetaStatements, ProofSpec, Statements, Witnesses } from '../../src';
import { buildWitness, encodeMessageForSigningIfPS, Scheme } from '../scheme';
import { checkResult, getParamsAndKeys, proverStmt, signAndVerify, stringToBytes, verifierStmt } from '../utils';
describe(`Proving knowledge of 1 ${Scheme} signature where some of the attributes are null, i.e. not applicable`, () => {
it('works', async () => {
// Load the WASM module
await initializeWasm();
// Messages to sign; the messages are attributes of a user like SSN (Social Security Number), name, email, etc. The attributes
// N/A don't apply to this user
const messages: Uint8Array[] = [];
// Comma separated indices of N/A messages. An efficient way, especially in large number of messages, could be to use a bitvector
// where an unset bit would indicate N/A
messages.push(encodeMessageForSigningIfPS(stringToBytes('5,6,7,9')));
// SSN
messages.push(encodeMessageForSigningIfPS(stringToBytes('123-456789-0')));
// Name
messages.push(encodeMessageForSigningIfPS(stringToBytes('John Smith')));
// High school name
messages.push(encodeMessageForSigningIfPS(stringToBytes('Some High School')));
// High school year
messages.push(encodeMessageForSigningIfPS(stringToBytes('2010')));
// College name
messages.push(encodeMessageForSigningIfPS(stringToBytes('N/A')));
// Major
messages.push(encodeMessageForSigningIfPS(stringToBytes('N/A')));
// College year
messages.push(encodeMessageForSigningIfPS(stringToBytes('N/A')));
// City
messages.push(encodeMessageForSigningIfPS(stringToBytes('New York')));
// Last employer
messages.push(encodeMessageForSigningIfPS(stringToBytes('N/A')));
const messageCount = messages.length;
const label = stringToBytes('My sig params in g1');
// Signers keys
const [params, sk, pk] = getParamsAndKeys(messageCount, label);
// Signer knows all the messages and signs
const [sig, result] = signAndVerify(messages, params, sk, pk, true);
checkResult(result);
// User reveals his name, high school year and city to verifier, i.e. indices 2, 4 and 8. He also needs to reveal first
// attribute (index 0) which indicates which attributes don't apply to him.
const revealedMsgIndices: Set<number> = new Set();
revealedMsgIndices.add(0);
revealedMsgIndices.add(2);
revealedMsgIndices.add(4);
revealedMsgIndices.add(8);
const revealedMsgs: Map<number, Uint8Array> = new Map();
const unrevealedMsgs: Map<number, Uint8Array> = new Map();
for (let i = 0; i < messageCount; i++) {
if (revealedMsgIndices.has(i)) {
revealedMsgs.set(i, messages[i]);
} else {
unrevealedMsgs.set(i, messages[i]);
}
}
const statement1 = proverStmt(params, revealedMsgs, pk, true);
const statements = new Statements(statement1);
// Both the prover (user) and verifier should independently construct this `ProofSpec` but only for testing, i am reusing it.
const proverProofSpec = new ProofSpec(statements, new MetaStatements());
expect(proverProofSpec.isValid()).toEqual(true);
const witness1 = buildWitness(sig, unrevealedMsgs, true);
const witnesses = new Witnesses(witness1);
const proof = CompositeProof.generate(proverProofSpec, witnesses);
const statement2 = verifierStmt(params, revealedMsgs, pk, true);
const verifierStatements = new Statements(statement2);
const verifierProofSpec = new ProofSpec(verifierStatements, new MetaStatements(), []);
expect(verifierProofSpec.isValid()).toEqual(true);
checkResult(proof.verify(verifierProofSpec));
});
});