-
Notifications
You must be signed in to change notification settings - Fork 19
/
utils.ts
319 lines (277 loc) · 8.95 KB
/
utils.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import { CircuitConfig } from './CircuitConfig'
import { Groth16Proof } from 'snarkjs'
import {
EpochKeyControl,
Field,
ReputationControl,
SignupControl,
UserStateTransitionControl,
} from './type'
/**
* Format snark proof for verifier smart contract
* @param proof The proof of `SnarkProof` type
* @returns An one dimensional array of stringified proof data
*/
export const formatProofForVerifierContract = (
proof: Groth16Proof
): string[] => {
return [
proof.pi_a[0],
proof.pi_a[1],
proof.pi_b[0][1],
proof.pi_b[0][0],
proof.pi_b[1][1],
proof.pi_b[1][0],
proof.pi_c[0],
proof.pi_c[1],
]
}
/**
* Format an one dimensional array for `snarkjs` verification
* @param proof The string array of the proof
* @returns The `SnarkProof` type proof data
*/
export const formatProofForSnarkjsVerification = (
proof: Field[]
): Groth16Proof => {
return {
pi_a: [BigInt(proof[0]), BigInt(proof[1]), BigInt('1')].map((x) =>
x.toString()
),
pi_b: [
[BigInt(proof[3]), BigInt(proof[2])].map((x) => x.toString()),
[BigInt(proof[5]), BigInt(proof[4])].map((x) => x.toString()),
[BigInt('1'), BigInt('0')].map((x) => x.toString()),
],
pi_c: [BigInt(proof[6]), BigInt(proof[7]), BigInt('1')].map((x) =>
x.toString()
),
protocol: 'groth16',
curve: 'bn128',
}
}
/**
* Shift `shiftBits` bits and compute the **AND** operation in `variableBits` bits `data`.
* For example, Get `data` from LSB `20` to `30` bits.
* Then use `shiftBits(data, 20, 10)` to get the `10` bits data.
* @param data The raw data in `bigint` type.
* @param shiftBits The shifted bits.
* @param variableBits The output data bits.
*/
export const shiftBits = (
data: bigint,
shiftBits: bigint,
variableBits: bigint
): bigint => {
return (data >> shiftBits) & ((BigInt(1) << variableBits) - BigInt(1))
}
/**
* Decode the raw control field to desired data.
* @param control The raw control field generated by `genProofAndPublicSignals`
* @param config The circuit config. Default: `CircuitConfig.default`
* @returns \{ `nonce`, `epoch`, `attesterId`, `revealNonce`, `chainId` \}
*/
export const decodeEpochKeyControl = (
control: bigint,
config: CircuitConfig = CircuitConfig.default
): EpochKeyControl => {
const {
NONCE_BITS,
EPOCH_BITS,
ATTESTER_ID_BITS,
REVEAL_NONCE_BITS,
CHAIN_ID_BITS,
} = config
let accBits = BigInt(0)
const nonce = shiftBits(control, accBits, NONCE_BITS)
accBits += NONCE_BITS
const epoch = shiftBits(control, accBits, EPOCH_BITS)
accBits += EPOCH_BITS
const attesterId = shiftBits(control, accBits, ATTESTER_ID_BITS)
accBits += ATTESTER_ID_BITS
const revealNonce = shiftBits(control, accBits, REVEAL_NONCE_BITS)
accBits += REVEAL_NONCE_BITS
const chainId = shiftBits(control, accBits, CHAIN_ID_BITS)
return {
nonce,
epoch,
attesterId,
revealNonce,
chainId,
}
}
/**
* Decode the raw control field to desired data.
* @param control The raw control field generated by `genProofAndPublicSignals`
* @param config The circuit config. Default: `CircuitConfig.default`
* @returns \{ `minRep`, `maxRep`, `proveMinRep`, `proveMaxRep`, `proveZeroRep`, `proveGraffiti` \}
*/
export const decodeReputationControl = (
control: bigint,
config: CircuitConfig = CircuitConfig.default
) => {
const { REP_BITS, ONE_BIT } = config
let accBits = BigInt(0)
const minRep = shiftBits(control, accBits, REP_BITS)
accBits += REP_BITS
const maxRep = shiftBits(control, accBits, REP_BITS)
accBits += REP_BITS
const proveMinRep = shiftBits(control, accBits, ONE_BIT)
accBits += ONE_BIT
const proveMaxRep = shiftBits(control, accBits, ONE_BIT)
accBits += ONE_BIT
const proveZeroRep = shiftBits(control, accBits, ONE_BIT)
accBits += ONE_BIT
const proveGraffiti = shiftBits(control, accBits, ONE_BIT)
return {
minRep,
maxRep,
proveMinRep,
proveMaxRep,
proveZeroRep,
proveGraffiti,
}
}
/**
* Decode the raw control field to desired data.
* @param control The raw control field generated by `genProofAndPublicSignals`
* @param config The circuit config. Default: `CircuitConfig.default`
* @returns \{ `attesterId`, `toEpoch` \}
*/
export const decodeUserStateTransitionControl = (
control: bigint,
config: CircuitConfig = CircuitConfig.default
): UserStateTransitionControl => {
const { ATTESTER_ID_BITS, EPOCH_BITS } = config
let accBits = BigInt(0)
const attesterId = shiftBits(control, accBits, ATTESTER_ID_BITS)
accBits += ATTESTER_ID_BITS
const toEpoch = shiftBits(control, accBits, EPOCH_BITS)
return {
attesterId,
toEpoch,
}
}
/**
* Decode the raw control field to desired data.
* @param control The raw control field generated by `genProofAndPublicSignals`
* @param config The circuit config. Default: `CircuitConfig.default`
* @returns \{ `attesterId`, `epoch`, `chainId` \}
*/
export const decodeSignupControl = (
control: bigint,
config: CircuitConfig = CircuitConfig.default
): SignupControl => {
const { ATTESTER_ID_BITS, EPOCH_BITS, CHAIN_ID_BITS } = config
let accBits = BigInt(0)
const attesterId = shiftBits(control, accBits, ATTESTER_ID_BITS)
accBits += ATTESTER_ID_BITS
const epoch = shiftBits(control, accBits, EPOCH_BITS)
accBits += EPOCH_BITS
const chainId = shiftBits(control, accBits, CHAIN_ID_BITS)
return {
attesterId,
epoch,
chainId,
}
}
/**
* Encode data to a 253 bits variable.
* @param params The data is going to be encoded. \{ `nonce`, `epoch`, `attesterId`, `revealNonce`, `chainId` \}
* @param config The circuit config. Default: `CircuitConfig.default`
* @returns a 253 bits control.
*/
export const buildEpochKeyControl = (
params: EpochKeyControl,
config: CircuitConfig = CircuitConfig.default
): bigint => {
const { chainId, revealNonce, attesterId, epoch, nonce } = params
const { NONCE_BITS, EPOCH_BITS, ATTESTER_ID_BITS, REVEAL_NONCE_BITS } =
config
let control = BigInt(0)
let accBits = BigInt(0)
control += BigInt(nonce) * BigInt(revealNonce)
accBits += NONCE_BITS
control += BigInt(epoch) << accBits
accBits += EPOCH_BITS
control += BigInt(attesterId) << accBits
accBits += ATTESTER_ID_BITS
control += BigInt(revealNonce) << accBits
accBits += REVEAL_NONCE_BITS
control += BigInt(chainId) << accBits
return control
}
/**
* Encode data to a 253 bits variable.
* @param params The data is going to be encoded. \{ `minRep`, `maxRep`, `proveMinRep`, `proveMaxRep`, `proveZeroRep`, `proveGraffiti` \}
* @param config The circuit config. Default: `CircuitConfig.default`
* @returns a 253 bits control.
*/
export const buildReputationControl = (
params: ReputationControl,
config: CircuitConfig = CircuitConfig.default
): bigint => {
const {
minRep,
maxRep,
proveMinRep,
proveMaxRep,
proveZeroRep,
proveGraffiti,
} = params
const { REP_BITS, ONE_BIT } = config
let control = BigInt(0)
let accBits = BigInt(0)
control += minRep
accBits += REP_BITS
control += maxRep << accBits
accBits += REP_BITS
control += proveMinRep << accBits
accBits += ONE_BIT
control += proveMaxRep << accBits
accBits += ONE_BIT
control += proveZeroRep << accBits
accBits += ONE_BIT
control += proveGraffiti << accBits
return control
}
/**
* Encode data to a 253 bits variable.
* @param params The data is going to be encoded. \{ `attesterId`, `toEpoch` \}
* @param config The circuit config. Default: `CircuitConfig.default`
* @returns a 253 bits control.
*/
export const buildUserStateTransitionControl = (
params: UserStateTransitionControl,
config: CircuitConfig = CircuitConfig.default
): bigint => {
const { attesterId, toEpoch } = params
const { ATTESTER_ID_BITS } = config
let control = BigInt(0)
let accBits = BigInt(0)
control += BigInt(attesterId)
accBits += ATTESTER_ID_BITS
control += BigInt(toEpoch) << accBits
return control
}
/**
* Encode data to a 253 bits variable.
* @param params The data is going to be encoded. \{ `attesterId`, `epoch`, `chainId` \}
* @param config The circuit config. Default: `CircuitConfig.default`
* @returns a 253 bits control.
*/
export const buildSignupControl = (
params: SignupControl,
config: CircuitConfig = CircuitConfig.default
): bigint => {
const { attesterId, epoch, chainId } = params
const { ATTESTER_ID_BITS, EPOCH_BITS } = config
let control = BigInt(0)
let accBits = BigInt(0)
control += BigInt(attesterId)
accBits += ATTESTER_ID_BITS
control += BigInt(epoch) << accBits
accBits += EPOCH_BITS
control += BigInt(chainId) << accBits
return control
}