-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcities.spec.ts
144 lines (118 loc) · 4.7 KB
/
cities.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
import {
areUint8ArraysEqual,
CompositeProof,
Encoder,
encodeRevealedMsgs,
getIndicesForMsgNames,
getRevealedAndUnrevealed,
initializeWasm,
MetaStatements,
ProofSpec,
SetupParam,
SignedMessages,
Statement,
Statements,
Witness,
WitnessEqualityMetaStatement,
Witnesses
} from '../../../src';
import { PederCommKey } from '../../../src/ped-com';
import { buildWitness, PublicKey, Scheme, SecretKey, Signature, SignatureParams } from '../../scheme';
import { checkResult, getParamsAndKeys, stringToBytes } from '../../utils';
import { defaultEncoder } from './data-and-encoder';
import { checkMapsEqual } from './index';
import { adaptedSigParams, proverStmt, signAndVerify, verifierStmt } from './util';
// Test for scenario where the user wants to prove that he is not resident of certain cities.
// Similar test can be written for other "set-membership" relations
describe(`${Scheme} Proving that not resident of certain cities`, () => {
let encoder: Encoder;
const label = stringToBytes('Sig params label');
let params: SignatureParams;
let sk: SecretKey;
let pk: PublicKey;
let commKey: PederCommKey;
let signed: SignedMessages<Signature>;
const cities = ['NYC', 'SF', 'LA', 'Chicago', 'Seattle'];
let encodedCities: Uint8Array[];
const attributesStruct = {
fname: null,
lname: null,
email: null,
SSN: null,
city: null
};
// City is Boston and a satisfactory proof can be created
const attributes = {
fname: 'John',
lname: 'Smith',
email: '[email protected]',
SSN: '123-456789-0',
city: 'Boston'
};
beforeAll(async () => {
await initializeWasm();
// Setup encoder
encoder = new Encoder(undefined, defaultEncoder);
encodedCities = cities.map((g: string) => encoder.encodeDefault(g));
commKey = new PederCommKey(stringToBytes('test'));
});
it('signers signs attributes', () => {
// Message count shouldn't matter as `label` is known
[params, sk, pk] = getParamsAndKeys(100, label);
signed = signAndVerify(attributes, encoder, label, sk, pk);
});
it('proof verifies when city is neither NYC, SF, LA, Chicago, Seattle', () => {
expect(encodedCities.some((g) => areUint8ArraysEqual(g, signed.encodedMessages['city']))).toEqual(false);
const revealedNames = new Set<string>();
revealedNames.add('fname');
const sigParams = adaptedSigParams(attributesStruct, label);
const [revealedMsgs, unrevealedMsgs, revealedMsgsRaw] = getRevealedAndUnrevealed(
attributes,
revealedNames,
encoder
);
expect(revealedMsgsRaw).toEqual({ fname: 'John' });
const setupParams = [SetupParam.pedCommKeyG1(commKey)];
const statementsProver = new Statements();
const metaStmtsProver = new MetaStatements();
const witnesses = new Witnesses();
const sIdx1 = statementsProver.add(proverStmt(
sigParams,
revealedMsgs,
pk
));
witnesses.add(buildWitness(signed.signature, unrevealedMsgs, false));
for (const c of encodedCities) {
const sIdx = statementsProver.add(Statement.publicInequalityG1FromSetupParamRefs(c, 0));
witnesses.add(Witness.publicInequality(signed.encodedMessages['city']));
const witnessEq = new WitnessEqualityMetaStatement();
witnessEq.addWitnessRef(sIdx1, getIndicesForMsgNames(['city'], attributesStruct)[0]);
witnessEq.addWitnessRef(sIdx, 0);
metaStmtsProver.addWitnessEquality(witnessEq);
}
// The prover should independently construct this `ProofSpec`
const proofSpecProver = new ProofSpec(statementsProver, metaStmtsProver, setupParams);
expect(proofSpecProver.isValid()).toEqual(true);
const proof = CompositeProof.generate(proofSpecProver, witnesses);
// Verifier independently encodes revealed messages
const revealedMsgsFromVerifier = encodeRevealedMsgs(revealedMsgsRaw, attributesStruct, encoder);
checkMapsEqual(revealedMsgs, revealedMsgsFromVerifier);
const statementsVerifier = new Statements();
const metaStmtsVerifier = new MetaStatements();
const sIdx2 = statementsVerifier.add(verifierStmt(
sigParams,
revealedMsgsFromVerifier,
pk
));
for (const c of encodedCities) {
const sIdx = statementsVerifier.add(Statement.publicInequalityG1FromSetupParamRefs(c, 0));
const witnessEq = new WitnessEqualityMetaStatement();
witnessEq.addWitnessRef(sIdx2, getIndicesForMsgNames(['city'], attributesStruct)[0]);
witnessEq.addWitnessRef(sIdx, 0);
metaStmtsVerifier.addWitnessEquality(witnessEq);
}
const proofSpecVerifier = new ProofSpec(statementsVerifier, metaStmtsVerifier, setupParams);
expect(proofSpecVerifier.isValid()).toEqual(true);
checkResult(proof.verify(proofSpecVerifier));
});
});