-
Notifications
You must be signed in to change notification settings - Fork 9
/
yearly-income.spec.ts
302 lines (255 loc) · 10.7 KB
/
yearly-income.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
import { generateFieldElementFromNumber } from 'crypto-wasm-new';
import {
CircomInputs,
CompositeProof,
createWitnessEqualityMetaStatement,
EncodeFunc,
Encoder,
encodeRevealedMsgs,
flattenObjectToKeyValuesList,
getIndicesForMsgNames,
getRevealedAndUnrevealed,
initializeWasm,
LegoProvingKeyUncompressed,
LegoVerifyingKeyUncompressed,
MetaStatements,
ParsedR1CSFile,
ProofSpec,
R1CSSnarkSetup,
SetupParam,
SignedMessages,
Statement,
Statements,
Witness,
WitnessEqualityMetaStatement,
Witnesses
} from '../../../../src';
import {
buildPublicKeySetupParam,
buildSignatureParamsSetupParam,
buildWitness, isKvac,
PublicKey,
Scheme,
Signature
} from '../../../scheme';
import { checkResult, getParamsAndKeys, getWasmBytes, parseR1CSFile, stringToBytes } from '../../../utils';
import { defaultEncoder } from '../data-and-encoder';
import { checkMapsEqual } from '../index';
import { adaptedSigParams, proverStmtFromSetupParamsRef, signAndVerify, verifierStmtFromSetupParamsRef } from '../util';
// Test for a scenario where a user wants to prove that his yearly income is less than 25000 where his income comprises
// of 12 payslip credentials, 1 for each month's.
describe(`${Scheme} Proving that yearly income calculated from monthly payslips is less than 25000`, () => {
let encoder: Encoder;
const label = stringToBytes('Sig params label');
let pk: PublicKey, sk, params;
let r1cs: ParsedR1CSFile;
let wasm: Uint8Array;
let provingKey: LegoProvingKeyUncompressed, verifyingKey: LegoVerifyingKeyUncompressed;
const payslipAttributesStruct = {
fname: null,
lname: null,
sensitive: {
email: null,
SSN: null
},
employer: null,
empId: null,
salary: {
paySlipId: null,
year: null,
month: null,
amount: null
}
};
const numPayslips = 12;
const payslipAttributes: object[] = [];
const signed: SignedMessages<Signature>[] = [];
const salaryLimit = 25000;
let salaryLimitEncoded: Uint8Array;
beforeAll(async () => {
await initializeWasm();
// Setup encoder
const encoders = new Map<string, EncodeFunc>();
encoders.set('salary.year', Encoder.positiveIntegerEncoder());
encoders.set('salary.month', Encoder.positiveIntegerEncoder());
encoders.set('salary.amount', Encoder.positiveDecimalNumberEncoder(2));
encoder = new Encoder(encoders, defaultEncoder);
// Important to encode the bound with the same encoder as attributes
salaryLimitEncoded = encoder.encodeMessage('salary.amount', salaryLimit);
// 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('sum_12_less_than_public.r1cs');
wasm = getWasmBytes('sum_12_less_than_public.wasm');
});
it('verifier generates SNARk proving and verifying key', async () => {
console.time('Snark setup');
const pk = R1CSSnarkSetup.fromParsedR1CSFile(r1cs, 12);
console.timeEnd('Snark setup');
console.time('Decompress keys');
provingKey = pk.decompress();
verifyingKey = pk.getVerifyingKeyUncompressed();
console.timeEnd('Decompress keys');
});
it('signers signs attributes', () => {
const numAttrs = flattenObjectToKeyValuesList(payslipAttributesStruct)[0].length;
// Issuing multiple credentials with the same number of attributes so create sig. params only once for faster execution
[params, sk, pk] = getParamsAndKeys(numAttrs, label);
for (let i = 0; i < numPayslips; i++) {
payslipAttributes.push({
fname: 'John',
lname: 'Smith',
sensitive: {
email: 'john.smith@example.com',
SSN: '123-456789-0'
},
employer: 'Acme Corp',
empId: 'e-123-987-1',
salary: {
paySlipId: 'e-123-987-1-22-' + (i + 1).toString(),
year: 2022,
month: i + 1,
amount: Math.floor(Math.random() * 2000) // salary will be under 2000
}
});
signed.push(signAndVerify(payslipAttributes[i], encoder, label, sk, pk));
}
});
it('proof verifies when yearly salary is less than 25000', () => {
// Check that yearly salary is indeed less than required
let salary = 0;
for (let i = 0; i < numPayslips; i++) {
// @ts-ignore
salary += payslipAttributes[i].salary.amount;
}
expect(salary).toBeLessThan(salaryLimit);
// Reveal first name ("fname" attribute), year ("salary.year" attribute) and month ("salary.month" attribute) from all 12 payslips
// Prove equality in zero knowledge of last name ("lname" attribute) and Social security number ("SSN" attribute) in all 12 payslips
console.time('Proof generate');
const revealedNames = new Set<string>();
revealedNames.add('fname');
revealedNames.add('salary.year');
revealedNames.add('salary.month');
const sigParams = adaptedSigParams(payslipAttributesStruct, label);
const revealedMsgs: Map<number, Uint8Array>[] = [];
const unrevealedMsgs: Map<number, Uint8Array>[] = [];
const revealedMsgsRaw: object[] = [];
for (let i = 0; i < numPayslips; i++) {
const [r, u, rRaw] = getRevealedAndUnrevealed(payslipAttributes[i], revealedNames, encoder);
revealedMsgs.push(r);
unrevealedMsgs.push(u);
revealedMsgsRaw.push(rRaw);
expect(rRaw).toEqual({ fname: 'John', salary: { year: 2022, month: i + 1 } });
}
const proverSetupParams: SetupParam[] = [];
proverSetupParams.push(buildSignatureParamsSetupParam(sigParams));
proverSetupParams.push(SetupParam.r1cs(r1cs));
proverSetupParams.push(SetupParam.bytes(wasm));
proverSetupParams.push(SetupParam.legosnarkProvingKeyUncompressed(provingKey));
if (!isKvac()) {
proverSetupParams.push(buildPublicKeySetupParam(pk));
}
const statementsProver = new Statements();
const sIdxs: number[] = [];
for (let i = 0; i < numPayslips; i++) {
sIdxs.push(statementsProver.add(proverStmtFromSetupParamsRef(0, revealedMsgs[i], 4, false)));
}
sIdxs.push(statementsProver.add(Statement.r1csCircomProverFromSetupParamRefs(1, 2, 3)));
const metaStmtsProver = new MetaStatements();
const witnessEq1 = createWitnessEqualityMetaStatement(
(() => {
const m = new Map<number, [msgNames: string[], msgStructure: object]>();
for (let i = 0; i < numPayslips; i++) {
m.set(sIdxs[i], [['lname'], payslipAttributesStruct]);
}
return m;
})()
);
metaStmtsProver.addWitnessEquality(witnessEq1);
const witnessEq2 = createWitnessEqualityMetaStatement(
(() => {
const m = new Map<number, [msgNames: string[], msgStructure: object]>();
for (let i = 0; i < numPayslips; i++) {
m.set(sIdxs[i], [['sensitive.SSN'], payslipAttributesStruct]);
}
return m;
})()
);
metaStmtsProver.addWitnessEquality(witnessEq2);
// The input to the circuit should match the signed "salary.amount" attribute.
for (let i = 0; i < numPayslips; i++) {
const witnessEq = new WitnessEqualityMetaStatement();
witnessEq.addWitnessRef(sIdxs[i], getIndicesForMsgNames(['salary.amount'], payslipAttributesStruct)[0]);
witnessEq.addWitnessRef(sIdxs[numPayslips], i);
metaStmtsProver.addWitnessEquality(witnessEq);
}
const proofSpecProver = new ProofSpec(statementsProver, metaStmtsProver, proverSetupParams);
expect(proofSpecProver.isValid()).toEqual(true);
const witnesses = new Witnesses();
for (let i = 0; i < numPayslips; i++) {
for (const wit of [].concat(buildWitness(signed[i].signature, unrevealedMsgs[i], false))) witnesses.add(wit);
}
const inputs = new CircomInputs();
// Add each encoded salary as the circuit input
inputs.setPrivateArrayInput(
'in',
signed.map((s) => s.encodedMessages['salary.amount'])
);
inputs.setPublicInput('max', salaryLimitEncoded);
witnesses.add(Witness.r1csCircomWitness(inputs));
const proof = CompositeProof.generate(proofSpecProver, witnesses);
console.timeEnd('Proof generate');
console.time('Proof verify');
// Verifier independently encodes revealed messages
const revealedMsgsFromVerifier: Map<number, Uint8Array>[] = [];
for (let i = 0; i < numPayslips; i++) {
revealedMsgsFromVerifier.push(encodeRevealedMsgs(revealedMsgsRaw[i], payslipAttributesStruct, encoder));
checkMapsEqual(revealedMsgs[i], revealedMsgsFromVerifier[i]);
}
const verifierSetupParams: SetupParam[] = [];
verifierSetupParams.push(buildSignatureParamsSetupParam(sigParams));
// To prove not less than, i.e. <= or >, replace `generateFieldElementFromNumber(1)` with `generateFieldElementFromNumber(0)`
// as 1 indicates success of the check, 0 indicates failure of the check
verifierSetupParams.push(SetupParam.fieldElementVec([generateFieldElementFromNumber(1), salaryLimitEncoded]));
verifierSetupParams.push(SetupParam.legosnarkVerifyingKeyUncompressed(verifyingKey));
if (!isKvac()) {
verifierSetupParams.push(buildPublicKeySetupParam(pk));
}
const statementsVerifier = new Statements();
const sIdxVs: number[] = [];
for (let i = 0; i < numPayslips; i++) {
sIdxVs.push(statementsVerifier.add(verifierStmtFromSetupParamsRef(0, revealedMsgsFromVerifier[i], 3, false)));
}
sIdxVs.push(statementsVerifier.add(Statement.r1csCircomVerifierFromSetupParamRefs(1, 2)));
const metaStmtsVerifier = new MetaStatements();
const witnessEq5 = createWitnessEqualityMetaStatement(
(() => {
const m = new Map<number, [msgNames: string[], msgStructure: object]>();
for (let i = 0; i < numPayslips; i++) {
m.set(sIdxVs[i], [['lname'], payslipAttributesStruct]);
}
return m;
})()
);
metaStmtsVerifier.addWitnessEquality(witnessEq5);
const witnessEq6 = createWitnessEqualityMetaStatement(
(() => {
const m = new Map<number, [msgNames: string[], msgStructure: object]>();
for (let i = 0; i < numPayslips; i++) {
m.set(sIdxVs[i], [['sensitive.SSN'], payslipAttributesStruct]);
}
return m;
})()
);
metaStmtsVerifier.addWitnessEquality(witnessEq6);
for (let i = 0; i < numPayslips; i++) {
const witnessEq = new WitnessEqualityMetaStatement();
witnessEq.addWitnessRef(sIdxVs[i], getIndicesForMsgNames(['salary.amount'], payslipAttributesStruct)[0]);
witnessEq.addWitnessRef(sIdxVs[numPayslips], i);
metaStmtsVerifier.addWitnessEquality(witnessEq);
}
const proofSpecVerifier = new ProofSpec(statementsVerifier, metaStmtsVerifier, verifierSetupParams);
expect(proofSpecVerifier.isValid()).toEqual(true);
checkResult(proof.verify(proofSpecVerifier));
console.timeEnd('Proof verify');
}, 40000);
});