-
Notifications
You must be signed in to change notification settings - Fork 9
/
blind-sig.spec.ts
210 lines (187 loc) · 6.93 KB
/
blind-sig.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
import { generateRandomG1Element } from 'crypto-wasm-new';
import {
BBSSignature,
CompositeProof,
getAdaptedSignatureParamsForMessages,
initializeWasm,
MetaStatements,
ProofSpec,
Statements,
Witnesses
} from '../../../src';
import {
adaptKeyForParams,
BlindSignature,
getStatementForBlindSigRequest,
getWitnessForBlindSigRequest,
isBBS,
isKvac,
isPS,
Scheme
} from '../../scheme';
import { checkResult, getParamsAndKeys, stringToBytes } from '../../utils';
import {
attributes1,
attributes1Struct,
attributes2,
attributes2Struct,
attributes3,
attributes3Struct,
GlobalEncoder
} from './data-and-encoder';
describe(`${Scheme} Requesting blind signatures`, () => {
beforeAll(async () => {
// Load the WASM module
await initializeWasm();
});
it('blind signature', () => {
// This test check that a user can get signatures from the signer even after hiding some of its messages. The signature
// generated by the signer is a blind signature as signer could not see all the messages. The user will then unblind the
// signature to use in proofs
const label = stringToBytes('Sig params label - this is public');
// Message count shouldn't matter as `label` is known
const [params, sk, pk] = getParamsAndKeys(100, label);
const h = generateRandomG1Element();
// The user will hide the "user-id" and "secret" attributes from the signer for the 1st signature
const hiddenAttrNames1 = new Set<string>();
hiddenAttrNames1.add('user-id');
hiddenAttrNames1.add('secret');
// The user will hide the "user-id" and "secret" attributes from the signer for the 2nd signature
const hiddenAttrNames2 = new Set<string>();
hiddenAttrNames2.add('sensitive.user-id');
hiddenAttrNames2.add('sensitive.secret');
// The user will hide the "employee-id", "phone" and "secret" attributes from the signer for the 3rd signature
const hiddenAttrNames3 = new Set<string>();
hiddenAttrNames3.add('sensitive.employee-id');
hiddenAttrNames3.add('sensitive.phone');
hiddenAttrNames3.add('sensitive.very.secret');
// The attributes known to signer for the 1st signature
const knownAttributes1 = {
fname: 'John',
lname: 'Smith',
email: '[email protected]',
SSN: '123-456789-0',
country: 'USA',
city: 'New York',
timeOfBirth: 1662010849619,
height: 181.5,
weight: 210,
BMI: 23.25,
score: -13.5
};
// The attributes known to signer for the 2nd signature
const knownAttributes2 = {
fname: 'John',
lname: 'Smith',
sensitive: {
email: '[email protected]',
SSN: '123-456789-0'
},
location: {
country: 'USA',
city: 'New York'
},
timeOfBirth: 1662010849619,
physical: {
height: 181.5,
weight: 210,
BMI: 23.25
},
score: -13.5
};
// The attributes known to signer for the 3rd signature
const knownAttributes3 = {
fname: 'John',
lname: 'Smith',
sensitive: {
email: '[email protected]',
SSN: '123-456789-0'
},
lessSensitive: {
location: {
country: 'USA',
city: 'New York'
},
department: {
name: 'Random',
location: {
name: 'Somewhere',
geo: {
lat: -23.658,
long: 2.556
}
}
}
},
rank: 6
};
for (let [attributes, attributesStruct, hiddenAttrNames, knownAttributes] of [
[attributes1, attributes1Struct, hiddenAttrNames1, knownAttributes1],
[attributes2, attributes2Struct, hiddenAttrNames2, knownAttributes2],
[attributes3, attributes3Struct, hiddenAttrNames3, knownAttributes3]
]) {
hiddenAttrNames = hiddenAttrNames as Set<any>;
const sigParams = getAdaptedSignatureParamsForMessages(params, attributesStruct);
const sigPk = adaptKeyForParams(pk, sigParams);
const sigSk = adaptKeyForParams(sk, sigParams);
const [names, encodedValues] = GlobalEncoder.encodeMessageObject(attributes);
const hiddenMsgs = new Map<number, Uint8Array>();
let found = 0;
hiddenAttrNames.forEach((n) => {
const i = names.indexOf(n);
if (i !== -1) {
hiddenMsgs.set(i, encodedValues[i]);
found++;
}
});
if (hiddenAttrNames.size !== found) {
throw new Error(
`Some of the hidden message names were not found in the given messages object, ${
hiddenAttrNames.size - found
} missing names`
);
}
const blindings = new Map();
let blinding, request;
if (isPS()) {
[blinding, request] = BlindSignature.generateRequest(hiddenMsgs, sigParams, h, blindings);
} else if (isBBS()) {
request = BlindSignature.generateRequest(hiddenMsgs, sigParams, false);
} else {
[blinding, request] = BlindSignature.generateRequest(hiddenMsgs, sigParams, false);
}
const witnesses = new Witnesses(getWitnessForBlindSigRequest(hiddenMsgs, blinding, blindings));
// The user creates a proof of knowledge of the blinded attributes.
const proverStatements = new Statements(getStatementForBlindSigRequest(request, sigParams, h));
const proofSpecProver = new ProofSpec(proverStatements, new MetaStatements());
expect(proofSpecProver.isValid()).toEqual(true);
const proof = CompositeProof.generate(proofSpecProver, witnesses);
// The signer is the verifier of the user's proof here. Uses the blind signature request to create the statement
// and proof spec independently.
const verifierStatements = new Statements(getStatementForBlindSigRequest(request, sigParams, h));
const proofSpecVerifier = new ProofSpec(verifierStatements, new MetaStatements());
expect(proofSpecVerifier.isValid()).toEqual(true);
// Signer/verifier verifies the proof
checkResult(proof.verify(proofSpecVerifier));
// Signer generates the blind signature using the signature request and attributes known to him. It sends the blind
// signature to the user
const blindSignature = BlindSignature.blindSignMessageObject(
request,
knownAttributes,
sigSk,
attributesStruct,
isPS() ? h : sigParams,
GlobalEncoder
);
// User unblinds the blind signature
const revealedSig = isPS()
? blindSignature.signature.unblind(blindings, sigPk, h)
: isBBS()
? new BBSSignature(blindSignature.signature.value)
: blindSignature.signature.unblind(blinding);
// The revealed signature can now be used in the usual verification process
checkResult(isKvac() ? revealedSig.verifyMessageObject(attributes, sigSk, sigParams, GlobalEncoder) : revealedSig.verifyMessageObject(attributes, sigPk, sigParams, GlobalEncoder));
// Proof of knowledge of signature can be created and verified as usual.
}
});
});