@devtion/actions 0.0.0-004e6ad

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +83 -0
  3. package/dist/index.mjs +2645 -0
  4. package/dist/index.node.js +2752 -0
  5. package/dist/types/hardhat.config.d.ts +6 -0
  6. package/dist/types/hardhat.config.d.ts.map +1 -0
  7. package/dist/types/src/helpers/authentication.d.ts +21 -0
  8. package/dist/types/src/helpers/authentication.d.ts.map +1 -0
  9. package/dist/types/src/helpers/constants.d.ts +202 -0
  10. package/dist/types/src/helpers/constants.d.ts.map +1 -0
  11. package/dist/types/src/helpers/contracts.d.ts +57 -0
  12. package/dist/types/src/helpers/contracts.d.ts.map +1 -0
  13. package/dist/types/src/helpers/crypto.d.ts +28 -0
  14. package/dist/types/src/helpers/crypto.d.ts.map +1 -0
  15. package/dist/types/src/helpers/database.d.ts +113 -0
  16. package/dist/types/src/helpers/database.d.ts.map +1 -0
  17. package/dist/types/src/helpers/functions.d.ts +145 -0
  18. package/dist/types/src/helpers/functions.d.ts.map +1 -0
  19. package/dist/types/src/helpers/security.d.ts +10 -0
  20. package/dist/types/src/helpers/security.d.ts.map +1 -0
  21. package/dist/types/src/helpers/services.d.ts +38 -0
  22. package/dist/types/src/helpers/services.d.ts.map +1 -0
  23. package/dist/types/src/helpers/storage.d.ts +124 -0
  24. package/dist/types/src/helpers/storage.d.ts.map +1 -0
  25. package/dist/types/src/helpers/tasks.d.ts +2 -0
  26. package/dist/types/src/helpers/tasks.d.ts.map +1 -0
  27. package/dist/types/src/helpers/utils.d.ts +153 -0
  28. package/dist/types/src/helpers/utils.d.ts.map +1 -0
  29. package/dist/types/src/helpers/verification.d.ts +96 -0
  30. package/dist/types/src/helpers/verification.d.ts.map +1 -0
  31. package/dist/types/src/helpers/vm.d.ts +112 -0
  32. package/dist/types/src/helpers/vm.d.ts.map +1 -0
  33. package/dist/types/src/index.d.ts +15 -0
  34. package/dist/types/src/index.d.ts.map +1 -0
  35. package/dist/types/src/types/enums.d.ts +133 -0
  36. package/dist/types/src/types/enums.d.ts.map +1 -0
  37. package/dist/types/src/types/index.d.ts +609 -0
  38. package/dist/types/src/types/index.d.ts.map +1 -0
  39. package/package.json +82 -0
  40. package/src/helpers/authentication.ts +37 -0
  41. package/src/helpers/constants.ts +324 -0
  42. package/src/helpers/contracts.ts +268 -0
  43. package/src/helpers/crypto.ts +55 -0
  44. package/src/helpers/database.ts +234 -0
  45. package/src/helpers/functions.ts +438 -0
  46. package/src/helpers/security.ts +67 -0
  47. package/src/helpers/services.ts +83 -0
  48. package/src/helpers/storage.ts +341 -0
  49. package/src/helpers/tasks.ts +56 -0
  50. package/src/helpers/utils.ts +782 -0
  51. package/src/helpers/verification.ts +354 -0
  52. package/src/helpers/vm.ts +399 -0
  53. package/src/index.ts +163 -0
  54. package/src/types/enums.ts +141 -0
  55. package/src/types/index.ts +674 -0
@@ -0,0 +1,782 @@
1
+ import { Firestore } from "firebase/firestore"
2
+ import fs, { ReadPosition, createWriteStream } from "fs"
3
+ import winston, { Logger } from "winston"
4
+ import fetch from "@adobe/node-fetch-retry"
5
+ import { pipeline } from "stream"
6
+ import { promisify } from "util"
7
+ import {
8
+ CircuitMetadata,
9
+ Contribution,
10
+ CircuitDocument,
11
+ CircuitInputData,
12
+ ContributionValidity,
13
+ FirebaseDocumentInfo,
14
+ SetupCeremonyData,
15
+ CeremonySetupTemplate,
16
+ CeremonySetupTemplateCircuitArtifacts,
17
+ StringifiedBigInts,
18
+ BigIntVariants
19
+ } from "../types/index"
20
+ import { finalContributionIndex, genesisZkeyIndex, potFilenameTemplate } from "./constants"
21
+ import {
22
+ getCircuitContributionsFromContributor,
23
+ getDocumentById,
24
+ getCircuitsCollectionPath,
25
+ getContributionsCollectionPath
26
+ } from "./database"
27
+ import { CeremonyTimeoutType } from "../types/enums"
28
+ import {
29
+ getPotStorageFilePath,
30
+ getR1csStorageFilePath,
31
+ getWasmStorageFilePath,
32
+ getZkeyStorageFilePath
33
+ } from "./storage"
34
+ import { blake512FromPath } from "./crypto"
35
+
36
+ /**
37
+ * Return a string with double digits if the provided input is one digit only.
38
+ * @param in <number> - the input number to be converted.
39
+ * @returns <string> - the two digits stringified number derived from the conversion.
40
+ */
41
+ export const convertToDoubleDigits = (amount: number): string => (amount < 10 ? `0${amount}` : amount.toString())
42
+
43
+ /**
44
+ * Extract a prefix consisting of alphanumeric and underscore characters from a string with arbitrary characters.
45
+ * @dev replaces all special symbols and whitespaces with an underscore char ('_'). Convert all uppercase chars to lowercase.
46
+ * @notice example: str = 'Multiplier-2!2.4.zkey'; output prefix = 'multiplier_2_2_4.zkey'.
47
+ * NB. Prefix extraction is a key process that conditions the name of the ceremony artifacts, download/upload from/to storage, collections paths.
48
+ * @param str <string> - the arbitrary string from which to extract the prefix.
49
+ * @returns <string> - the resulting prefix.
50
+ */
51
+ export const extractPrefix = (str: string): string =>
52
+ // eslint-disable-next-line no-useless-escape
53
+ str.replace(/[`\s~!@#$%^&*()|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "-").toLowerCase()
54
+
55
+ /**
56
+ * Extract data from a R1CS metadata file generated with a custom file-based logger.
57
+ * @notice useful for extracting metadata circuits contained in the generated file using a logger
58
+ * on the `r1cs.info()` method of snarkjs.
59
+ * @param fullFilePath <string> - the full path of the file.
60
+ * @param keyRgx <RegExp> - the regular expression linked to the key from which you want to extract the value.
61
+ * @returns <string> - the stringified extracted value.
62
+ */
63
+ export const extractR1CSInfoValueForGivenKey = (fullFilePath: string, keyRgx: RegExp): string => {
64
+ // Read the logger file.
65
+ const fileContents = fs.readFileSync(fullFilePath, "utf-8")
66
+
67
+ // Check for the matching value.
68
+ const matchingValue = fileContents.match(keyRgx)
69
+
70
+ if (!matchingValue)
71
+ throw new Error(
72
+ `Unable to retrieve circuit metadata. Possible causes may involve an error while using the logger. Please, check whether the corresponding \`.log\` file is present in your local \`output/setup/metadata\` folder. In any case, we kindly ask you to terminate the current session and repeat the process.`
73
+ )
74
+
75
+ // Elaborate spaces and special characters to extract the value.
76
+ // nb. this is a manual process which follows this custom arbitrary extraction rule
77
+ // accordingly to the output produced by the `r1cs.info()` method from snarkjs library.
78
+ return matchingValue?.at(0)?.split(":")[1].replace(" ", "").split("#")[0].replace("\n", "")!
79
+ }
80
+
81
+ /**
82
+ * Calculate the smallest amount of Powers of Tau needed for a circuit with a constraint size.
83
+ * @param constraints <number> - the number of circuit constraints (extracted from metadata).
84
+ * @param outputs <number> - the number of circuit outputs (extracted from metadata)
85
+ * @returns <number> - the smallest amount of Powers of Tau for the given constraint size.
86
+ */
87
+ export const computeSmallestPowersOfTauForCircuit = (constraints: number, outputs: number) => {
88
+ let power = 2
89
+ let tau = 2 ** power
90
+
91
+ while (constraints + outputs > tau) {
92
+ power += 1
93
+ tau = 2 ** power
94
+ }
95
+
96
+ return power
97
+ }
98
+
99
+ /**
100
+ * Transform a number in a zKey index format.
101
+ * @dev this method is aligned with the number of characters of the genesis zKey index (which is a constant).
102
+ * @param progress <number> - the progression in zKey index.
103
+ * @returns <string> - the progression in a zKey index format (`XYZAB`).
104
+ */
105
+ export const formatZkeyIndex = (progress: number): string => {
106
+ let index = progress.toString()
107
+
108
+ // Pad with zeros if the progression has less digits.
109
+ while (index.length < genesisZkeyIndex.length) {
110
+ index = `0${index}`
111
+ }
112
+
113
+ return index
114
+ }
115
+
116
+ /**
117
+ * Extract the amount of powers from Powers of Tau file name.
118
+ * @dev the PoT files must follow these convention (i_am_a_pot_file_09.ptau) where the numbers before '.ptau' are the powers.
119
+ * @param potCompleteFilename <string> - the complete filename of the Powers of Tau file.
120
+ * @returns <number> - the amount of powers.
121
+ */
122
+ export const extractPoTFromFilename = (potCompleteFilename: string): number =>
123
+ Number(potCompleteFilename.split("_").pop()?.split(".").at(0))
124
+
125
+ /**
126
+ * Automate the generation of an entropy for a contribution.
127
+ * @dev Took inspiration from here https://github.com/glamperd/setup-mpc-ui/blob/master/client/src/state/Compute.tsx#L112.
128
+ * @todo we need to improve the entropy generation (too naive).
129
+ * @returns <string> - the auto-generated entropy.
130
+ */
131
+ export const autoGenerateEntropy = () => new Uint8Array(256).map(() => Math.random() * 256).toString()
132
+
133
+ /**
134
+ * Check and return the circuit document based on its sequence position among a set of circuits (if any).
135
+ * @dev there should be only one circuit with a provided sequence position. This method checks and return an
136
+ * error if none is found.
137
+ * @param circuits <Array<FirebaseDocumentInfo>> - the set of ceremony circuits documents.
138
+ * @param sequencePosition <number> - the sequence position (index) of the circuit to be found and returned.
139
+ * @returns <FirebaseDocumentInfo> - the document of the circuit in the set of circuits that has the provided sequence position.
140
+ */
141
+ export const getCircuitBySequencePosition = (
142
+ circuits: Array<FirebaseDocumentInfo>,
143
+ sequencePosition: number
144
+ ): FirebaseDocumentInfo => {
145
+ // Filter by sequence position.
146
+ const matchedCircuits = circuits.filter(
147
+ (circuitDocument: FirebaseDocumentInfo) => circuitDocument.data.sequencePosition === sequencePosition
148
+ )
149
+
150
+ if (matchedCircuits.length !== 1)
151
+ throw new Error(
152
+ `Unable to find the circuit having position ${sequencePosition}. Run the command again and, if this error persists please contact the coordinator.`
153
+ )
154
+
155
+ return matchedCircuits.at(0)!
156
+ }
157
+
158
+ /**
159
+ * Convert bytes or chilobytes into gigabytes with customizable precision.
160
+ * @param bytesOrKb <number> - the amount of bytes or chilobytes to be converted.
161
+ * @param isBytes <boolean> - true when the amount to be converted is in bytes; otherwise false (= Chilobytes).
162
+ * @returns <number> - the converted amount in GBs.
163
+ */
164
+ export const convertBytesOrKbToGb = (bytesOrKb: number, isBytes: boolean): number =>
165
+ Number(bytesOrKb / 1024 ** (isBytes ? 3 : 2))
166
+
167
+ /**
168
+ * Get the validity of contributors' contributions for each circuit of the given ceremony (if any).
169
+ * @param firestoreDatabase <Firestore> - the Firestore service instance associated to the current Firebase application.
170
+ * @param circuits <Array<FirebaseDocumentInfo>> - the array of ceremony circuits documents.
171
+ * @param ceremonyId <string> - the unique identifier of the ceremony.
172
+ * @param participantId <string> - the unique identifier of the contributor.
173
+ * @param isFinalizing <boolean> - flag to discriminate between ceremony finalization (true) and contribution (false).
174
+ * @returns <Promise<Array<ContributionValidity>>> - a list of contributor contributions together with contribution validity (based on coordinator verification).
175
+ */
176
+ export const getContributionsValidityForContributor = async (
177
+ firestoreDatabase: Firestore,
178
+ circuits: Array<FirebaseDocumentInfo>,
179
+ ceremonyId: string,
180
+ participantId: string,
181
+ isFinalizing: boolean
182
+ ): Promise<Array<ContributionValidity>> => {
183
+ const contributionsValidity: Array<ContributionValidity> = []
184
+
185
+ for await (const circuit of circuits) {
186
+ // Get circuit contribution from contributor.
187
+ const circuitContributionsFromContributor = await getCircuitContributionsFromContributor(
188
+ firestoreDatabase,
189
+ ceremonyId,
190
+ circuit.id,
191
+ participantId
192
+ )
193
+
194
+ // Check for ceremony finalization (= there could be more than one contribution).
195
+ const contribution = isFinalizing
196
+ ? circuitContributionsFromContributor
197
+ .filter(
198
+ (contributionDocument: FirebaseDocumentInfo) =>
199
+ contributionDocument.data.zkeyIndex === finalContributionIndex
200
+ )
201
+ .at(0)
202
+ : circuitContributionsFromContributor.at(0)
203
+
204
+ if (!contribution)
205
+ throw new Error(
206
+ "Unable to retrieve contributions for the participant. There may have occurred a database-side error. Please, we kindly ask you to terminate the current session and repeat the process"
207
+ )
208
+
209
+ contributionsValidity.push({
210
+ contributionId: contribution?.id,
211
+ circuitId: circuit.id,
212
+ valid: contribution?.data.valid
213
+ })
214
+ }
215
+
216
+ return contributionsValidity
217
+ }
218
+
219
+ /**
220
+ * Return the public attestation preamble for given contributor.
221
+ * @param contributorIdentifier <string> - the identifier of the contributor (handle, name, uid).
222
+ * @param ceremonyName <string> - the name of the ceremony.
223
+ * @param isFinalizing <boolean> - true when the coordinator is finalizing the ceremony, otherwise false.
224
+ * @returns <string> - the public attestation preamble.
225
+ */
226
+ export const getPublicAttestationPreambleForContributor = (
227
+ contributorIdentifier: string,
228
+ ceremonyName: string,
229
+ isFinalizing: boolean
230
+ ) =>
231
+ `Hey, I'm ${contributorIdentifier} and I have ${isFinalizing ? "finalized" : "contributed to"} the ${ceremonyName}${
232
+ ceremonyName.toLowerCase().includes("trusted setup") || ceremonyName.toLowerCase().includes("ceremony")
233
+ ? "."
234
+ : " MPC Phase2 Trusted Setup ceremony."
235
+ }\nThe following are my contribution signatures:`
236
+
237
+ /**
238
+ * Check and prepare public attestation for the contributor made only of its valid contributions.
239
+ * @param firestoreDatabase <Firestore> - the Firestore service instance associated to the current Firebase application.
240
+ * @param circuits <Array<FirebaseDocumentInfo>> - the array of ceremony circuits documents.
241
+ * @param ceremonyId <string> - the unique identifier of the ceremony.
242
+ * @param participantId <string> - the unique identifier of the contributor.
243
+ * @param participantContributions <Array<Co> - the document data of the participant.
244
+ * @param contributorIdentifier <string> - the identifier of the contributor (handle, name, uid).
245
+ * @param ceremonyName <string> - the name of the ceremony.
246
+ * @param isFinalizing <boolean> - true when the coordinator is finalizing the ceremony, otherwise false.
247
+ * @returns <Promise<string>> - the public attestation for the contributor.
248
+ */
249
+ export const generateValidContributionsAttestation = async (
250
+ firestoreDatabase: Firestore,
251
+ circuits: Array<FirebaseDocumentInfo>,
252
+ ceremonyId: string,
253
+ participantId: string,
254
+ participantContributions: Array<Contribution>,
255
+ contributorIdentifier: string,
256
+ ceremonyName: string,
257
+ isFinalizing: boolean
258
+ ): Promise<string> => {
259
+ // Generate the attestation preamble for the contributor.
260
+ let publicAttestation = getPublicAttestationPreambleForContributor(
261
+ contributorIdentifier,
262
+ ceremonyName,
263
+ isFinalizing
264
+ )
265
+
266
+ // Get contributors' contributions validity.
267
+ const contributionsWithValidity = await getContributionsValidityForContributor(
268
+ firestoreDatabase,
269
+ circuits,
270
+ ceremonyId,
271
+ participantId,
272
+ isFinalizing
273
+ )
274
+
275
+ for await (const contributionWithValidity of contributionsWithValidity) {
276
+ // Filter for the related contribution document info.
277
+ const matchedContributions = participantContributions.filter(
278
+ (contribution: Contribution) => contribution.doc === contributionWithValidity.contributionId
279
+ )
280
+
281
+ if (matchedContributions.length === 0)
282
+ throw new Error(
283
+ `Unable to retrieve given circuit contribution information. This could happen due to some errors while writing the information on the database.`
284
+ )
285
+
286
+ if (matchedContributions.length > 1)
287
+ throw new Error(`Duplicated circuit contribution information. Please, contact the coordinator.`)
288
+
289
+ const participantContribution = matchedContributions.at(0)!
290
+
291
+ // Get circuit document (the one for which the contribution was calculated).
292
+ const circuitDocument = await getDocumentById(
293
+ firestoreDatabase,
294
+ getCircuitsCollectionPath(ceremonyId),
295
+ contributionWithValidity.circuitId
296
+ )
297
+ const contributionDocument = await getDocumentById(
298
+ firestoreDatabase,
299
+ getContributionsCollectionPath(ceremonyId, contributionWithValidity.circuitId),
300
+ participantContribution.doc
301
+ )
302
+
303
+ if (!contributionDocument.data() || !circuitDocument.data())
304
+ throw new Error(`Something went wrong when retrieving the data from the database`)
305
+
306
+ // Extract data.
307
+ const { sequencePosition, prefix } = circuitDocument.data()!
308
+ const { zkeyIndex } = contributionDocument.data()!
309
+
310
+ // Update public attestation.
311
+ publicAttestation = `${publicAttestation}\n\nCircuit # ${sequencePosition} (${prefix})\nContributor # ${
312
+ zkeyIndex > 0 ? Number(zkeyIndex) : zkeyIndex
313
+ }\n${participantContribution.hash}`
314
+ }
315
+
316
+ return publicAttestation
317
+ }
318
+
319
+ /**
320
+ * Create a custom logger to write logs on a local file.
321
+ * @param filename <string> - the name of the output file (where the logs are going to be written).
322
+ * @param level <winston.LoggerOptions["level"]> - the option for the logger level (e.g., info, error).
323
+ * @returns <Logger> - a customized winston logger for files.
324
+ */
325
+ export const createCustomLoggerForFile = (filename: string, level: winston.LoggerOptions["level"] = "info"): Logger =>
326
+ winston.createLogger({
327
+ level,
328
+ transports: new winston.transports.File({
329
+ filename,
330
+ format: winston.format.printf((log) => log.message),
331
+ level
332
+ })
333
+ })
334
+
335
+ /**
336
+ * Return an amount of bytes read from a file to a particular location in the form of a buffer.
337
+ * @param localFilePath <string> - the local path where the artifact will be downloaded.
338
+ * @param offset <number> - the index of the line to be read (0 from the start).
339
+ * @param length <number> - the length of the line to be read.
340
+ * @param position <ReadPosition> - the position inside the file.
341
+ * @returns <Buffer> - the buffer w/ the read bytes.
342
+ */
343
+ export const readBytesFromFile = (
344
+ localFilePath: string,
345
+ offset: number,
346
+ length: number,
347
+ position: ReadPosition
348
+ ): Buffer => {
349
+ // Open the file (read mode).
350
+ const fileDescriptor = fs.openSync(localFilePath, "r")
351
+
352
+ // Prepare buffer.
353
+ const buffer = Buffer.alloc(length)
354
+
355
+ // Read bytes.
356
+ fs.readSync(fileDescriptor, buffer, offset, length, position)
357
+
358
+ // Return the read bytes.
359
+ return buffer
360
+ }
361
+
362
+ /**
363
+ * Given a buffer in little endian format, convert it to bigint
364
+ * @param buffer
365
+ * @returns
366
+ */
367
+ export function leBufferToBigint(buffer: Buffer): bigint {
368
+ return BigInt(`0x${buffer.reverse().toString("hex")}`)
369
+ }
370
+
371
+ /**
372
+ * Given an input containing string values, convert them
373
+ * to bigint
374
+ * @param input - The input to convert
375
+ * @returns the input with string values converted to bigint
376
+ */
377
+ export const unstringifyBigInts = (input: StringifiedBigInts): BigIntVariants => {
378
+ if (typeof input === "string" && /^[0-9]+$/.test(input)) {
379
+ return BigInt(input)
380
+ }
381
+
382
+ if (typeof input === "string" && /^0x[0-9a-fA-F]+$/.test(input)) {
383
+ return BigInt(input)
384
+ }
385
+
386
+ if (Array.isArray(input)) {
387
+ return input.map(unstringifyBigInts)
388
+ }
389
+
390
+ if (input === null) {
391
+ return null
392
+ }
393
+
394
+ if (typeof input === "object") {
395
+ return Object.entries(input).reduce<Record<string, bigint>>((acc, [key, value]) => {
396
+ acc[key] = unstringifyBigInts(value) as bigint
397
+ return acc
398
+ }, {})
399
+ }
400
+
401
+ return input
402
+ }
403
+
404
+ /**
405
+ * Return the info about the R1CS file.ù
406
+ * @dev this method was built taking inspiration from
407
+ * https://github.com/weijiekoh/circom-helper/blob/master/ts/read_num_inputs.ts#L5.
408
+ * You can find the specs of R1CS file here
409
+ * https://github.com/iden3/r1csfile/blob/master/doc/r1cs_bin_format.md
410
+ * @param localR1CSFilePath <string> - the local path to the R1CS file.
411
+ * @returns <CircuitMetadata> - the info about the R1CS file.
412
+ */
413
+ export const getR1CSInfo = (localR1CSFilePath: string): CircuitMetadata => {
414
+ /**
415
+ * ┏━━━━┳━━━━━━━━━━━━━━━━━┓
416
+ * ┃ 4 │ 72 31 63 73 ┃ Magic "r1cs"
417
+ * ┗━━━━┻━━━━━━━━━━━━━━━━━┛
418
+ * ┏━━━━┳━━━━━━━━━━━━━━━━━┓
419
+ * ┃ 4 │ 01 00 00 00 ┃ Version 1
420
+ * ┗━━━━┻━━━━━━━━━━━━━━━━━┛
421
+ * ┏━━━━┳━━━━━━━━━━━━━━━━━┓
422
+ * ┃ 4 │ 03 00 00 00 ┃ Number of Sections
423
+ * ┗━━━━┻━━━━━━━━━━━━━━━━━┛
424
+ * ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓
425
+ * ┃ 4 │ sectionType ┃ 8 │ SectionSize ┃
426
+ * ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛
427
+ * ┏━━━━━━━━━━━━━━━━━━━━━┓
428
+ * ┃ ┃
429
+ * ┃ ┃
430
+ * ┃ ┃
431
+ * ┃ Section Content ┃
432
+ * ┃ ┃
433
+ * ┃ ┃
434
+ * ┃ ┃
435
+ * ┗━━━━━━━━━━━━━━━━━━━━━┛
436
+ *
437
+ * ┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓
438
+ * ┃ 4 │ sectionType ┃ 8 │ SectionSize ┃
439
+ * ┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛
440
+ * ┏━━━━━━━━━━━━━━━━━━━━━┓
441
+ * ┃ ┃
442
+ * ┃ ┃
443
+ * ┃ ┃
444
+ * ┃ Section Content ┃
445
+ * ┃ ┃
446
+ * ┃ ┃
447
+ * ┃ ┃
448
+ * ┗━━━━━━━━━━━━━━━━━━━━━┛
449
+ *
450
+ * ...
451
+ * ...
452
+ * ...
453
+ */
454
+
455
+ // Prepare state.
456
+ let pointer = 0 // selector to particular file data position in order to read data.
457
+ let wires = 0
458
+ let publicOutputs = 0
459
+ let publicInputs = 0
460
+ let privateInputs = 0
461
+ let labels = 0
462
+ let constraints = 0
463
+
464
+ try {
465
+ // Get 'number of section' (jump magic r1cs and version1 data).
466
+ const numberOfSections = leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, 8))
467
+
468
+ // Jump to first section.
469
+ pointer = 12
470
+
471
+ // For each section
472
+ for (let i = 0; i < numberOfSections; i++) {
473
+ // Read section type.
474
+ const sectionType = leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer))
475
+
476
+ // Jump to section size.
477
+ pointer += 4
478
+
479
+ // Read section size
480
+ const sectionSize = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 8, pointer)))
481
+
482
+ // If at header section (0x00000001 : Header Section).
483
+ if (sectionType === BigInt(1)) {
484
+ // Read info from header section.
485
+ /**
486
+ * ┏━━━━┳━━━━━━━━━━━━━━━━━┓
487
+ * ┃ 4 │ 20 00 00 00 ┃ Field Size in bytes (fs)
488
+ * ┗━━━━┻━━━━━━━━━━━━━━━━━┛
489
+ * ┏━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
490
+ * ┃ fs │ 010000f0 93f5e143 9170b979 48e83328 5d588181 b64550b8 29a031e1 724e6430 ┃ Prime size
491
+ * ┗━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
492
+ * ┏━━━━┳━━━━━━━━━━━━━━━━━┓
493
+ * ┃ 32 │ 01 00 00 00 ┃ nWires
494
+ * ┗━━━━┻━━━━━━━━━━━━━━━━━┛
495
+ * ┏━━━━┳━━━━━━━━━━━━━━━━━┓
496
+ * ┃ 32 │ 01 00 00 00 ┃ nPubOut
497
+ * ┗━━━━┻━━━━━━━━━━━━━━━━━┛
498
+ * ┏━━━━┳━━━━━━━━━━━━━━━━━┓
499
+ * ┃ 32 │ 01 00 00 00 ┃ nPubIn
500
+ * ┗━━━━┻━━━━━━━━━━━━━━━━━┛
501
+ * ┏━━━━┳━━━━━━━━━━━━━━━━━┓
502
+ * ┃ 32 │ 01 00 00 00 ┃ nPrvIn
503
+ * ┗━━━━┻━━━━━━━━━━━━━━━━━┛
504
+ * ┏━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
505
+ * ┃ 64 │ 01 00 00 00 00 00 00 00 ┃ nLabels
506
+ * ┗━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
507
+ * ┏━━━━┳━━━━━━━━━━━━━━━━━┓
508
+ * ┃ 32 │ 01 00 00 00 ┃ mConstraints
509
+ * ┗━━━━┻━━━━━━━━━━━━━━━━━┛
510
+ */
511
+
512
+ pointer += sectionSize - 20
513
+
514
+ // Read R1CS info.
515
+ wires = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
516
+ pointer += 4
517
+
518
+ publicOutputs = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
519
+ pointer += 4
520
+
521
+ publicInputs = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
522
+ pointer += 4
523
+
524
+ privateInputs = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
525
+ pointer += 4
526
+
527
+ labels = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 8, pointer)))
528
+ pointer += 8
529
+
530
+ constraints = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
531
+ }
532
+
533
+ pointer += 8 + Number(sectionSize)
534
+ }
535
+
536
+ return {
537
+ curve: "bn-128", /// @note currently default to bn-128 as we support only Groth16 proving system.
538
+ wires,
539
+ constraints,
540
+ privateInputs,
541
+ publicInputs,
542
+ labels,
543
+ outputs: publicOutputs,
544
+ pot: computeSmallestPowersOfTauForCircuit(constraints, publicOutputs)
545
+ }
546
+ } catch (err: any) {
547
+ throw new Error(
548
+ `The R1CS file you provided would not appear to be correct. Please, check that you have provided a valid R1CS file and repeat the process.`
549
+ )
550
+ }
551
+ }
552
+
553
+ /**
554
+ * Parse and validate that the ceremony configuration is correct
555
+ * @notice this does not upload any files to storage
556
+ * @param path <string> - the path to the configuration file
557
+ * @param cleanup <boolean> - whether to delete the r1cs file after parsing
558
+ * @returns any - the data to pass to the cloud function for setup and the circuit artifacts
559
+ */
560
+ export const parseCeremonyFile = async (path: string, cleanup: boolean = false): Promise<SetupCeremonyData> => {
561
+ // check that the path exists
562
+ if (!fs.existsSync(path))
563
+ throw new Error(
564
+ "The provided path to the configuration file does not exist. Please provide an absolute path and try again."
565
+ )
566
+
567
+ try {
568
+ // read the data
569
+ const data: CeremonySetupTemplate = JSON.parse(fs.readFileSync(path).toString())
570
+
571
+ // verify that the data is correct
572
+ if (
573
+ data.timeoutMechanismType !== CeremonyTimeoutType.DYNAMIC &&
574
+ data.timeoutMechanismType !== CeremonyTimeoutType.FIXED
575
+ )
576
+ throw new Error("Invalid timeout type. Please choose between DYNAMIC and FIXED.")
577
+
578
+ // validate that we have at least 1 circuit input data
579
+ if (!data.circuits || data.circuits.length === 0)
580
+ throw new Error("You need to provide the data for at least 1 circuit.")
581
+
582
+ // validate that the end date is in the future
583
+ let endDate: Date
584
+ let startDate: Date
585
+ try {
586
+ endDate = new Date(data.endDate)
587
+ startDate = new Date(data.startDate)
588
+ } catch (error: any) {
589
+ throw new Error("The dates should follow this format: 2023-07-04T00:00:00.")
590
+ }
591
+
592
+ if (endDate <= startDate) throw new Error("The end date should be greater than the start date.")
593
+
594
+ const currentDate = new Date()
595
+
596
+ if (endDate <= currentDate || startDate <= currentDate)
597
+ throw new Error("The start and end dates should be in the future.")
598
+
599
+ // validate penalty
600
+ if (data.penalty <= 0) throw new Error("The penalty should be greater than zero.")
601
+
602
+ const circuits: CircuitDocument[] = []
603
+ const urlPattern = /(https?:\/\/[^\s]+)/g
604
+ const commitHashPattern = /^[a-f0-9]{40}$/i
605
+
606
+ const circuitArtifacts: CeremonySetupTemplateCircuitArtifacts[] = []
607
+
608
+ for (let i = 0; i < data.circuits.length; i++) {
609
+ const circuitData = data.circuits[i]
610
+ const { artifacts } = circuitData
611
+ circuitArtifacts.push({
612
+ artifacts
613
+ })
614
+
615
+ // where we storing the r1cs downloaded
616
+ const localR1csPath = `./${circuitData.name}.r1cs`
617
+ // where we storing the wasm downloaded
618
+ const localWasmPath = `./${circuitData.name}.wasm`
619
+
620
+ // download the r1cs to extract the metadata
621
+ const streamPipeline = promisify(pipeline)
622
+
623
+ // Make the call.
624
+ const responseR1CS = await fetch(artifacts.r1csStoragePath)
625
+
626
+ // Handle errors.
627
+ if (!responseR1CS.ok && responseR1CS.status !== 200)
628
+ throw new Error(
629
+ `There was an error while trying to download the r1cs file for circuit ${circuitData.name}. Please check that the file has the correct permissions (public) set.`
630
+ )
631
+
632
+ await streamPipeline(responseR1CS.body!, createWriteStream(localR1csPath))
633
+ // Write the file locally
634
+
635
+ // extract the metadata from the r1cs
636
+ const metadata = getR1CSInfo(localR1csPath)
637
+
638
+ // download wasm too to ensure it's available
639
+ const responseWASM = await fetch(artifacts.wasmStoragePath)
640
+ if (!responseWASM.ok && responseWASM.status !== 200)
641
+ throw new Error(
642
+ `There was an error while trying to download the WASM file for circuit ${circuitData.name}. Please check that the file has the correct permissions (public) set.`
643
+ )
644
+ await streamPipeline(responseWASM.body!, createWriteStream(localWasmPath))
645
+
646
+ // validate that the circuit hash and template links are valid
647
+ const { template } = circuitData
648
+
649
+ const URLMatch = template.source.match(urlPattern)
650
+ if (!URLMatch || URLMatch.length === 0 || URLMatch.length > 1)
651
+ throw new Error("You should provide the URL to the circuits templates on GitHub.")
652
+
653
+ const hashMatch = template.commitHash.match(commitHashPattern)
654
+ if (!hashMatch || hashMatch.length === 0 || hashMatch.length > 1)
655
+ throw new Error("You should provide a valid commit hash of the circuit templates.")
656
+
657
+ // calculate the hash of the r1cs file
658
+ const r1csBlake2bHash = await blake512FromPath(localR1csPath)
659
+
660
+ const circuitPrefix = extractPrefix(circuitData.name)
661
+
662
+ // filenames
663
+ const doubleDigitsPowers = convertToDoubleDigits(metadata.pot!)
664
+ const r1csCompleteFilename = `${circuitData.name}.r1cs`
665
+ const wasmCompleteFilename = `${circuitData.name}.wasm`
666
+ const smallestPowersOfTauCompleteFilenameForCircuit = `${potFilenameTemplate}${doubleDigitsPowers}.ptau`
667
+ const firstZkeyCompleteFilename = `${circuitPrefix}_${genesisZkeyIndex}.zkey`
668
+
669
+ // storage paths
670
+ const r1csStorageFilePath = getR1csStorageFilePath(circuitPrefix, r1csCompleteFilename)
671
+ const wasmStorageFilePath = getWasmStorageFilePath(circuitPrefix, wasmCompleteFilename)
672
+ const potStorageFilePath = getPotStorageFilePath(smallestPowersOfTauCompleteFilenameForCircuit)
673
+ const zkeyStorageFilePath = getZkeyStorageFilePath(circuitPrefix, firstZkeyCompleteFilename)
674
+
675
+ const files: any = {
676
+ potFilename: smallestPowersOfTauCompleteFilenameForCircuit,
677
+ r1csFilename: r1csCompleteFilename,
678
+ wasmFilename: wasmCompleteFilename,
679
+ initialZkeyFilename: firstZkeyCompleteFilename,
680
+ potStoragePath: potStorageFilePath,
681
+ r1csStoragePath: r1csStorageFilePath,
682
+ wasmStoragePath: wasmStorageFilePath,
683
+ initialZkeyStoragePath: zkeyStorageFilePath,
684
+ r1csBlake2bHash
685
+ }
686
+
687
+ // validate that the compiler hash is a valid hash
688
+ const { compiler } = circuitData
689
+ const compilerHashMatch = compiler.commitHash.match(commitHashPattern)
690
+ if (!compilerHashMatch || compilerHashMatch.length === 0 || compilerHashMatch.length > 1)
691
+ throw new Error("You should provide a valid commit hash of the circuit compiler.")
692
+
693
+ // validate that the verification options are valid
694
+ const { verification } = circuitData
695
+ if (verification.cfOrVm !== "CF" && verification.cfOrVm !== "VM")
696
+ throw new Error("Please enter a valid verification mechanism: either CF or VM")
697
+
698
+ // @todo VM parameters verification
699
+ // if (verification['cfOrVM'] === "VM") {}
700
+
701
+ // check that the timeout is provided for the correct configuration
702
+ let dynamicThreshold: number | undefined
703
+ let fixedTimeWindow: number | undefined
704
+
705
+ let circuit: CircuitDocument | CircuitInputData = {} as CircuitDocument | CircuitInputData
706
+
707
+ if (data.timeoutMechanismType === CeremonyTimeoutType.DYNAMIC) {
708
+ if (circuitData.dynamicThreshold <= 0) throw new Error("The dynamic threshold should be > 0.")
709
+ dynamicThreshold = circuitData.dynamicThreshold
710
+
711
+ // the Circuit data for the ceremony setup
712
+ circuit = {
713
+ name: circuitData.name,
714
+ description: circuitData.description,
715
+ prefix: circuitPrefix,
716
+ sequencePosition: i + 1,
717
+ metadata,
718
+ files,
719
+ template,
720
+ compiler,
721
+ verification,
722
+ dynamicThreshold,
723
+ avgTimings: {
724
+ contributionComputation: 0,
725
+ fullContribution: 0,
726
+ verifyCloudFunction: 0
727
+ }
728
+ }
729
+ }
730
+
731
+ if (data.timeoutMechanismType === CeremonyTimeoutType.FIXED) {
732
+ if (circuitData.fixedTimeWindow <= 0) throw new Error("The fixed time window threshold should be > 0.")
733
+ fixedTimeWindow = circuitData.fixedTimeWindow
734
+
735
+ // the Circuit data for the ceremony setup
736
+ circuit = {
737
+ name: circuitData.name,
738
+ description: circuitData.description,
739
+ prefix: circuitPrefix,
740
+ sequencePosition: i + 1,
741
+ metadata,
742
+ files,
743
+ template,
744
+ compiler,
745
+ verification,
746
+ fixedTimeWindow,
747
+ avgTimings: {
748
+ contributionComputation: 0,
749
+ fullContribution: 0,
750
+ verifyCloudFunction: 0
751
+ }
752
+ }
753
+ }
754
+
755
+ circuits.push(circuit)
756
+
757
+ // remove the local r1cs and wasm downloads (if used for verifying the config only vs setup)
758
+ if (cleanup) {
759
+ fs.unlinkSync(localR1csPath)
760
+ fs.unlinkSync(localWasmPath)
761
+ }
762
+ }
763
+
764
+ const setupData: SetupCeremonyData = {
765
+ ceremonyInputData: {
766
+ title: data.title,
767
+ description: data.description,
768
+ startDate: startDate.valueOf(),
769
+ endDate: endDate.valueOf(),
770
+ timeoutMechanismType: data.timeoutMechanismType,
771
+ penalty: data.penalty
772
+ },
773
+ ceremonyPrefix: extractPrefix(data.title),
774
+ circuits,
775
+ circuitArtifacts
776
+ }
777
+
778
+ return setupData
779
+ } catch (error: any) {
780
+ throw new Error(`Error while parsing up the ceremony setup file. ${error.message}`)
781
+ }
782
+ }