forked from docknetwork/crypto-wasm-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblood-group.spec.ts
266 lines (213 loc) · 9.24 KB
/
blood-group.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
import { generateFieldElementFromNumber } from 'crypto-wasm-new';
import {
areUint8ArraysEqual,
CircomInputs,
CompositeProof,
Encoder,
encodeRevealedMsgs,
getIndicesForMsgNames,
getRevealedAndUnrevealed,
initializeWasm,
LegoProvingKeyUncompressed,
LegoVerifyingKeyUncompressed,
MetaStatements,
ParsedR1CSFile,
ProofSpec,
R1CSSnarkSetup,
SignedMessages,
Statement,
Statements,
Witness,
WitnessEqualityMetaStatement,
Witnesses
} from '../../../../src';
import { buildWitness, 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, proverStmt, signAndVerify, verifierStmt } from '../util';
// Test for a scenario where a user wants to prove that his blood group is AB- without revealing the blood group.
// Similar test can be written for other "not-equals" relations like user is not resident of certain city
describe(`${Scheme} Proving that blood group is not AB-`, () => {
let encoder: Encoder;
let encodedABNeg: Uint8Array;
const label = stringToBytes('Sig params label');
let pk: PublicKey, sk, params;
// CredentialBuilder for the user with blood group AB+
let signed1: SignedMessages<Signature>;
// CredentialBuilder for the user with blood group AB-
let signed2: SignedMessages<Signature>;
let r1cs: ParsedR1CSFile;
let wasm: Uint8Array;
let provingKey: LegoProvingKeyUncompressed, verifyingKey: LegoVerifyingKeyUncompressed;
// Structure of credential that has the blood group attribute
const attributesStruct = {
fname: null,
lname: null,
verySensitive: {
email: null,
SSN: null
},
physical: {
gender: null,
bloodGroup: null
},
'user-id': null
};
// 1st credential where blood group is AB+ and a satisfactory proof can be created
const attributes1 = {
fname: 'John',
lname: 'Smith',
verySensitive: {
email: 'john.smith@example.com',
SSN: '123-456789-0'
},
physical: {
gender: 'male',
bloodGroup: 'AB+'
},
'user-id': 'user:123-xyz-#'
};
// 2nd credential where blood group is AB- and its not acceptable so proof will fail
const attributes2 = {
fname: 'Carol',
lname: 'Smith',
verySensitive: {
email: 'carol.smith@example.com',
SSN: '233-456788-1'
},
physical: {
gender: 'female',
bloodGroup: 'AB-'
},
'user-id': 'user:764-xyz-#'
};
beforeAll(async () => {
await initializeWasm();
// Setup encoder
encoder = new Encoder(undefined, defaultEncoder);
encodedABNeg = encoder.encodeDefault('AB-');
// 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('not_equal_public.r1cs');
wasm = getWasmBytes('not_equal_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, pk] = getParamsAndKeys(100, label);
signed1 = signAndVerify(attributes1, encoder, label, sk, pk);
signed2 = signAndVerify(attributes2, encoder, label, sk, pk);
});
it('proof verifies when blood groups is not AB-', () => {
expect(areUint8ArraysEqual(encodedABNeg, signed1.encodedMessages['physical.bloodGroup'])).toEqual(false);
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, pk);
const statement2 = Statement.r1csCircomProver(r1cs, wasm, provingKey);
const statementsProver = new Statements();
const sIdx1 = statementsProver.add(statement1);
const sIdx2 = statementsProver.add(statement2);
// Enforce the equality between credential attribute and the Circom program input
const metaStmtsProver = new MetaStatements();
const witnessEq1 = new WitnessEqualityMetaStatement();
witnessEq1.addWitnessRef(sIdx1, getIndicesForMsgNames(['physical.bloodGroup'], 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(signed1.signature, unrevealedMsgs, false);
const inputs = new CircomInputs();
inputs.setPrivateInput('in', signed1.encodedMessages['physical.bloodGroup']);
inputs.setPublicInput('pub', encodedABNeg);
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, revealedMsgsFromVerifier, pk);
const pub = [generateFieldElementFromNumber(1), encodedABNeg];
const statement4 = Statement.r1csCircomVerifier(pub, verifyingKey);
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(['physical.bloodGroup'], attributesStruct)[0]);
witnessEq2.addWitnessRef(sIdx4, 0);
metaStmtsVerifier.addWitnessEquality(witnessEq2);
const proofSpecVerifier = new ProofSpec(statementsVerifier, metaStmtsVerifier);
expect(proofSpecVerifier.isValid()).toEqual(true);
checkResult(proof.verify(proofSpecVerifier));
});
it('proof does not verify when blood groups is AB-', () => {
expect(areUint8ArraysEqual(encodedABNeg, signed2.encodedMessages['physical.bloodGroup'])).toEqual(true);
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, pk);
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(['physical.bloodGroup'], 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('in', signed2.encodedMessages['physical.bloodGroup']);
inputs.setPublicInput('pub', encodedABNeg);
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, revealedMsgsFromVerifier, pk);
const pub = [generateFieldElementFromNumber(1), encodedABNeg];
const statement4 = Statement.r1csCircomVerifier(pub, verifyingKey);
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(['physical.bloodGroup'], attributesStruct)[0]);
witnessEq2.addWitnessRef(sIdx4, 0);
metaStmtsVerifier.addWitnessEquality(witnessEq2);
const proofSpecVerifier = new ProofSpec(statementsVerifier, metaStmtsVerifier);
expect(proofSpecVerifier.isValid()).toEqual(true);
expect(proof.verify(proofSpecVerifier).verified).toEqual(false);
});
});