forked from docknetwork/crypto-wasm-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrade.spec.ts
262 lines (208 loc) · 9.19 KB
/
grade.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
import { generateFieldElementFromNumber } from 'crypto-wasm-new';
import {
checkResult,
getParamsAndKeys,
getWasmBytes,
parseR1CSFile,
stringToBytes
} from '../../../utils';
import {
initializeWasm,
CircomInputs,
CompositeProof,
Encoder,
encodeRevealedMsgs,
getIndicesForMsgNames,
getRevealedAndUnrevealed,
LegoProvingKeyUncompressed,
LegoVerifyingKeyUncompressed,
MetaStatements,
ParsedR1CSFile,
ProofSpec,
R1CSSnarkSetup,
SignedMessages,
Statement,
Statements,
Witness,
WitnessEqualityMetaStatement,
Witnesses, areUint8ArraysEqual
} from '../../../../src';
import { checkMapsEqual } from '../index';
import { defaultEncoder } from '../data-and-encoder';
import {
SignatureParams,
KeyPair,
PublicKey,
Signature,
buildVerifierStatement,
buildWitness,
Scheme,
adaptKeyForParams
} from '../../../scheme';
import { adaptedSigParams, proverStmt, signAndVerify, verifierStmt } from '../util';
// Test for scenario where the user wants to prove that his grade belongs/does not belong to the given set.
// Similar test can be written for other "set-membership" relations like user is not resident of certain cities
describe(`${Scheme} Proving that grade is either A+, A, B+, B or C`, () => {
let encoder: Encoder;
const label = stringToBytes('Sig params label');
let sigPk: PublicKey, sk, params;
let signed1: SignedMessages<Signature>;
let signed2: SignedMessages<Signature>;
const allowedGrades = ['A+', 'A', 'B+', 'B', 'C'];
let encodedGrades: Uint8Array[];
let r1cs: ParsedR1CSFile;
let wasm: Uint8Array;
let provingKey: LegoProvingKeyUncompressed, verifyingKey: LegoVerifyingKeyUncompressed;
const attributesStruct = {
fname: null,
lname: null,
email: null,
SSN: null,
'user-id': null,
grade: null
};
// 1st attribute where grade is B+ and a satisfactory proof can be created
const attributes1 = {
fname: 'John',
lname: 'Smith',
email: 'john.smith@example.com',
SSN: '123-456789-0',
'user-id': 'user:123-xyz-#',
grade: 'B+'
};
// 2nd attribute where grade is E and its not an acceptable grade so proof will fail
const attributes2 = {
fname: 'Carol',
lname: 'Smith',
email: 'carol.smith@example.com',
SSN: '233-456788-1',
'user-id': 'user:764-xyz-#',
grade: 'E'
};
beforeAll(async () => {
await initializeWasm();
// Setup encoder
encoder = new Encoder(undefined, defaultEncoder);
encodedGrades = allowedGrades.map((g: string) => encoder.encodeDefault(g));
// This can be done by the verifier or the verifier can publish only the Circom program and
// prover can check that the same R1CS and WASM are generated.
r1cs = await parseR1CSFile('set_membership_5_public.r1cs');
wasm = getWasmBytes('set_membership_5_public.wasm');
});
it('verifier generates SNARk proving and verifying key', async () => {
const pk = R1CSSnarkSetup.fromParsedR1CSFile(r1cs, 1);
provingKey = pk.decompress();
verifyingKey = pk.getVerifyingKeyUncompressed();
});
it('signers signs attributes', () => {
// Message count shouldn't matter as `label` is known
[params, sk, sigPk] = getParamsAndKeys(20, label);
signed1 = signAndVerify(attributes1, encoder, label, sk, sigPk);
signed2 = signAndVerify(attributes2, encoder, label, sk, sigPk);
});
it('proof verifies when grade is either A+, A, B+, B or C', () => {
expect(encodedGrades.some((g) => areUint8ArraysEqual(g, signed1.encodedMessages['grade']))).toEqual(true);
const revealedNames = new Set<string>();
revealedNames.add('fname');
const sigParams = adaptedSigParams(attributesStruct, label);
const [revealedMsgs, unrevealedMsgs, revealedMsgsRaw] = getRevealedAndUnrevealed(
attributes1,
revealedNames,
encoder
);
expect(revealedMsgsRaw).toEqual({ fname: 'John' });
const statement1 = proverStmt(sigParams, revealedMsgs, sigPk);
const statement2 = Statement.r1csCircomProver(r1cs, wasm, provingKey);
const statementsProver = new Statements(statement1);
const sIdx2 = statementsProver.add(statement2);
const witnessEq1 = new WitnessEqualityMetaStatement();
witnessEq1.addWitnessRef(0, getIndicesForMsgNames(['grade'], attributesStruct)[0]);
witnessEq1.addWitnessRef(sIdx2, 0);
const metaStmtsProver = new MetaStatements();
metaStmtsProver.addWitnessEquality(witnessEq1);
// The prover should independently construct this `ProofSpec`
const proofSpecProver = new ProofSpec(statementsProver, metaStmtsProver);
expect(proofSpecProver.isValid()).toEqual(true);
const witness1 = buildWitness(signed1.signature, unrevealedMsgs, false);
const inputs = new CircomInputs();
inputs.setPrivateInput('x', signed1.encodedMessages['grade']);
inputs.setPublicArrayInput('set', encodedGrades);
const witness2 = Witness.r1csCircomWitness(inputs);
const witnesses = new Witnesses(witness1);
witnesses.add(witness2);
const proof = CompositeProof.generate(proofSpecProver, witnesses);
// Verifier independently encodes revealed messages
const revealedMsgsFromVerifier = encodeRevealedMsgs(revealedMsgsRaw, attributesStruct, encoder);
checkMapsEqual(revealedMsgs, revealedMsgsFromVerifier);
const statement3 = verifierStmt(sigParams, revealedMsgs, sigPk);
// generateFieldElementFromNumber(1) because membership is being check, use generateFieldElementFromNumber(0) for checking non-membership
const pub = [generateFieldElementFromNumber(1), ...encodedGrades];
const statement4 = Statement.r1csCircomVerifier(pub, verifyingKey);
const verifierStatements = new Statements();
verifierStatements.add(statement3);
verifierStatements.add(statement4);
const statementsVerifier = new Statements();
const sIdx3 = statementsVerifier.add(statement3);
const sIdx4 = statementsVerifier.add(statement4);
const witnessEq2 = new WitnessEqualityMetaStatement();
witnessEq2.addWitnessRef(sIdx3, getIndicesForMsgNames(['grade'], attributesStruct)[0]);
witnessEq2.addWitnessRef(sIdx4, 0);
const metaStmtsVerifier = new MetaStatements();
metaStmtsVerifier.addWitnessEquality(witnessEq2);
const proofSpecVerifier = new ProofSpec(verifierStatements, metaStmtsVerifier);
expect(proofSpecVerifier.isValid()).toEqual(true);
checkResult(proof.verify(proofSpecVerifier));
});
it('proof does not verify when grade is none of A+, A, B+, B or C but E', () => {
expect(encodedGrades.some((g) => areUint8ArraysEqual(g, signed2.encodedMessages['grade']))).toEqual(false);
const revealedNames = new Set<string>();
revealedNames.add('fname');
const sigParams = adaptedSigParams(attributesStruct, label);
const [revealedMsgs, unrevealedMsgs, revealedMsgsRaw] = getRevealedAndUnrevealed(
attributes2,
revealedNames,
encoder
);
expect(revealedMsgsRaw).toEqual({ fname: 'Carol' });
const statement1 = proverStmt(sigParams, revealedMsgs, sigPk);
const statement2 = Statement.r1csCircomProver(r1cs, wasm, provingKey);
const statementsProver = new Statements();
const sIdx1 = statementsProver.add(statement1);
const sIdx2 = statementsProver.add(statement2);
const metaStmtsProver = new MetaStatements();
const witnessEq1 = new WitnessEqualityMetaStatement();
witnessEq1.addWitnessRef(sIdx1, getIndicesForMsgNames(['grade'], attributesStruct)[0]);
witnessEq1.addWitnessRef(sIdx2, 0);
metaStmtsProver.addWitnessEquality(witnessEq1);
// The prover should independently construct this `ProofSpec`
const proofSpecProver = new ProofSpec(statementsProver, metaStmtsProver);
expect(proofSpecProver.isValid()).toEqual(true);
const witness1 = buildWitness(signed2.signature, unrevealedMsgs, false);
const inputs = new CircomInputs();
inputs.setPrivateInput('x', signed2.encodedMessages['grade']);
inputs.setPublicArrayInput('set', encodedGrades);
const witness2 = Witness.r1csCircomWitness(inputs);
const witnesses = new Witnesses(witness1);
witnesses.add(witness2);
const proof = CompositeProof.generate(proofSpecProver, witnesses);
// Verifier independently encodes revealed messages
const revealedMsgsFromVerifier = encodeRevealedMsgs(revealedMsgsRaw, attributesStruct, encoder);
checkMapsEqual(revealedMsgs, revealedMsgsFromVerifier);
const statement3 = verifierStmt(sigParams, revealedMsgs, sigPk);
const pub = [generateFieldElementFromNumber(0), ...encodedGrades];
const statement4 = Statement.r1csCircomVerifier(pub, verifyingKey);
const verifierStatements = new Statements(statement3);
verifierStatements.add(statement4);
const statementsVerifier = new Statements();
const sIdx3 = statementsVerifier.add(statement3);
const sIdx4 = statementsVerifier.add(statement4);
const metaStmtsVerifier = new MetaStatements();
const witnessEq2 = new WitnessEqualityMetaStatement();
witnessEq2.addWitnessRef(sIdx3, getIndicesForMsgNames(['grade'], attributesStruct)[0]);
witnessEq2.addWitnessRef(sIdx4, 0);
metaStmtsVerifier.addWitnessEquality(witnessEq2);
const proofSpecVerifier = new ProofSpec(verifierStatements, metaStmtsVerifier);
expect(proofSpecVerifier.isValid()).toEqual(true);
checkResult(proof.verify(proofSpecVerifier));
});
});