-
Notifications
You must be signed in to change notification settings - Fork 9
/
witness.ts
255 lines (228 loc) · 8.23 KB
/
witness.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
import {
generatePedersenCommitmentWitness,
generatePoKBBSPlusSignatureWitness,
generatePoKPSSignatureWitness,
generatePoKBBSSignatureWitness,
generateAccumulatorMembershipWitness,
generateAccumulatorNonMembershipWitness,
generateSaverWitness,
generateBoundCheckWitness,
generateR1CSCircomWitness,
generateBoundCheckBppWitness,
generateBoundCheckSmcWitness,
generateBoundCheckSmcWithKVWitness,
generatePublicInequalityWitness,
generatePoKBDDT16MacWitness,
generateKBUniversalAccumulatorNonMembershipWitness,
generateKBUniversalAccumulatorMembershipWitness,
generatePoKPSSignatureWitnessConstantTime,
generatePoKBBSSignatureWitnessConstantTime,
generatePoKBBSPlusSignatureWitnessConstantTime,
generatePoKBDDT16MacWitnessConstantTime
} from 'crypto-wasm-new';
import { KBUniversalMembershipWitness, KBUniversalNonMembershipWitness } from '../accumulator/kb-acccumulator-witness';
import { BBSPlusSignatureG1 } from '../bbs-plus';
import { VBMembershipWitness, VBNonMembershipWitness } from '../accumulator';
import { CircomInputs } from '../r1cs';
import { PSSignature } from '../ps';
import { BBSSignature } from '../bbs';
import { BBDT16Mac } from '../bbdt16-mac';
/**
* Private data known only to the prover whose knowledge is being proved in a proof.
*/
export class Witness {
/**
* Opening of the Pedersen commitment
* @param elements
*/
static pedersenCommitment(elements: Uint8Array[]): Uint8Array {
return generatePedersenCommitmentWitness(elements);
}
/**
* Signature and messages of PS signature
* @param signature
* @param unrevealedMessages
*/
static psSignature(signature: PSSignature, unrevealedMessages: Map<number, Uint8Array>): Uint8Array {
return generatePoKPSSignatureWitness(signature.value, unrevealedMessages);
}
/**
* Signature and messages of PS signature
* @param signature
* @param unrevealedMessages
*/
static psSignatureConstantTime(signature: PSSignature, unrevealedMessages: Map<number, Uint8Array>): Uint8Array {
return generatePoKPSSignatureWitnessConstantTime(signature.value, unrevealedMessages);
}
/**
* Signature and messages of BBS signature
* @param signature
* @param unrevealedMessages
* @param encodeMessages
*/
static bbsSignature(
signature: BBSSignature,
unrevealedMessages: Map<number, Uint8Array>,
encodeMessages: boolean
): Uint8Array {
return generatePoKBBSSignatureWitness(signature.value, unrevealedMessages, encodeMessages);
}
/**
* Signature and messages of BBS signature
* @param signature
* @param unrevealedMessages
* @param encodeMessages
*/
static bbsSignatureConstantTime(
signature: BBSSignature,
unrevealedMessages: Map<number, Uint8Array>,
encodeMessages: boolean
): Uint8Array {
return generatePoKBBSSignatureWitnessConstantTime(signature.value, unrevealedMessages, encodeMessages);
}
/**
* Signature and messages of BBS+ signature
* @param signature
* @param unrevealedMessages
* @param encodeMessages
*/
static bbsPlusSignature(
signature: BBSPlusSignatureG1,
unrevealedMessages: Map<number, Uint8Array>,
encodeMessages: boolean
): Uint8Array {
return generatePoKBBSPlusSignatureWitness(signature.value, unrevealedMessages, encodeMessages);
}
/**
* Signature and messages of BBS+ signature
* @param signature
* @param unrevealedMessages
* @param encodeMessages
*/
static bbsPlusSignatureConstantTime(
signature: BBSPlusSignatureG1,
unrevealedMessages: Map<number, Uint8Array>,
encodeMessages: boolean
): Uint8Array {
return generatePoKBBSPlusSignatureWitnessConstantTime(signature.value, unrevealedMessages, encodeMessages);
}
static bbdt16Mac(mac: BBDT16Mac, unrevealedMessages: Map<number, Uint8Array>, encodeMessages: boolean): Uint8Array {
return generatePoKBDDT16MacWitness(mac.value, unrevealedMessages, encodeMessages);
}
static bbdt16MacConstantTime(mac: BBDT16Mac, unrevealedMessages: Map<number, Uint8Array>, encodeMessages: boolean): Uint8Array {
return generatePoKBDDT16MacWitnessConstantTime(mac.value, unrevealedMessages, encodeMessages);
}
/**
* VB Accumulator member and its witness
* @param member
* @param accumulatorWitness
*/
static vbAccumulatorMembership(member: Uint8Array, accumulatorWitness: VBMembershipWitness): Uint8Array {
return generateAccumulatorMembershipWitness(member, accumulatorWitness.value);
}
/**
* VB Accumulator non-member and its witness
* @param nonMember
* @param accumulatorWitness
*/
static vbAccumulatorNonMembership(nonMember: Uint8Array, accumulatorWitness: VBNonMembershipWitness): Uint8Array {
return generateAccumulatorNonMembershipWitness(nonMember, accumulatorWitness.value);
}
/**
* KB universal Accumulator member and its witness
* @param member
* @param accumulatorWitness
*/
static kbUniAccumulatorMembership(member: Uint8Array, accumulatorWitness: KBUniversalMembershipWitness): Uint8Array {
return generateKBUniversalAccumulatorMembershipWitness(member, accumulatorWitness.value);
}
/**
* KB universal Accumulator non-member and its witness
* @param nonMember
* @param accumulatorWitness
*/
static kbUniAccumulatorNonMembership(
nonMember: Uint8Array,
accumulatorWitness: KBUniversalNonMembershipWitness
): Uint8Array {
return generateKBUniversalAccumulatorNonMembershipWitness(nonMember, accumulatorWitness.value);
}
/**
* Witness for verifiable encryption using SAVER
* @param message - Message being encrypted
*/
static saver(message: Uint8Array): Uint8Array {
return generateSaverWitness(message);
}
/**
* Witness for bound check using LegoGroth16
* @param message - Message whose bounds are being proven using LegoGroth16
*/
static boundCheckLegoGroth16(message: Uint8Array): Uint8Array {
return generateBoundCheckWitness(message);
}
static pseudonym(secretKey: Uint8Array): Uint8Array {
return Witness.pedersenCommitment([secretKey]);
}
static attributeBoundPseudonym(attributes: Uint8Array[], secretKey?: Uint8Array): Uint8Array {
const a = [...attributes];
if (secretKey !== undefined) {
a.push(secretKey);
}
return Witness.pedersenCommitment(a);
}
static r1csCircomWitness(inputs: CircomInputs): Uint8Array {
return generateR1CSCircomWitness(inputs.wires, inputs.privates, inputs.publics);
}
/**
* Witness for bound check using Bulletproofs++
* @param message - Message whose bounds are being proven using Bulletproofs++
*/
static boundCheckBpp(message: Uint8Array): Uint8Array {
return generateBoundCheckBppWitness(message);
}
/**
* Witness for bound check using set-membership
* @param message - Message whose bounds are being proven using set-membership check
*/
static boundCheckSmc(message: Uint8Array): Uint8Array {
return generateBoundCheckSmcWitness(message);
}
/**
* Witness for bound check using set-membership and keyed verification
* @param message - Message whose bounds are being proven using set-membership check
*/
static boundCheckSmcWithKV(message: Uint8Array): Uint8Array {
return generateBoundCheckSmcWithKVWitness(message);
}
static publicInequality(message: Uint8Array): Uint8Array {
return generatePublicInequalityWitness(message);
}
}
/**
* A collection of witnesses.
*/
export class Witnesses {
values: Uint8Array[];
constructor(values: Uint8Array | Uint8Array[] = []) {
this.values = Array.isArray(values) ? values : [values];
}
/**
* Add a new witness to the end of the list. Returns the index (id) of the added witness. This index is part of the witness reference.
* @param witness
*/
add(witness: Uint8Array): number {
this.values.push(witness);
return this.values.length - 1;
}
/**
* Add new witnesses to the end of the list. Returns the indices (ids) of the added witnesses. These indices are part of the witness reference.
* @param witnesses
*/
append(witnesses: Witnesses | Uint8Array[]): number[] {
const rawWitnesses = witnesses instanceof Witnesses ? witnesses.values : witnesses;
const indices = Array.from({ length: rawWitnesses.length }, (_, i) => this.values.length + i);
this.values = this.values.concat(rawWitnesses);
return indices;
}
}