@devtion/actions 0.0.0-bfc9ee4 → 0.0.0-c1f4cbe
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.
- package/README.md +1 -1
- package/dist/index.mjs +331 -298
- package/dist/index.node.js +331 -297
- package/dist/types/src/helpers/constants.d.ts +8 -0
- package/dist/types/src/helpers/constants.d.ts.map +1 -1
- package/dist/types/src/helpers/contracts.d.ts.map +1 -1
- package/dist/types/src/helpers/crypto.d.ts +1 -0
- package/dist/types/src/helpers/crypto.d.ts.map +1 -1
- package/dist/types/src/helpers/database.d.ts +8 -0
- package/dist/types/src/helpers/database.d.ts.map +1 -1
- package/dist/types/src/helpers/security.d.ts +2 -2
- package/dist/types/src/helpers/security.d.ts.map +1 -1
- package/dist/types/src/helpers/storage.d.ts +5 -2
- package/dist/types/src/helpers/storage.d.ts.map +1 -1
- package/dist/types/src/helpers/utils.d.ts +34 -20
- package/dist/types/src/helpers/utils.d.ts.map +1 -1
- package/dist/types/src/helpers/verification.d.ts +3 -2
- package/dist/types/src/helpers/verification.d.ts.map +1 -1
- package/dist/types/src/helpers/vm.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/types/index.d.ts +9 -3
- package/dist/types/src/types/index.d.ts.map +1 -1
- package/package.json +3 -8
- package/src/helpers/constants.ts +8 -0
- package/src/helpers/contracts.ts +3 -3
- package/src/helpers/database.ts +13 -0
- package/src/helpers/functions.ts +1 -1
- package/src/helpers/security.ts +33 -52
- package/src/helpers/services.ts +3 -3
- package/src/helpers/storage.ts +15 -3
- package/src/helpers/utils.ts +316 -277
- package/src/helpers/verification.ts +6 -6
- package/src/helpers/vm.ts +14 -7
- package/src/index.ts +3 -2
- package/src/types/index.ts +32 -8
package/src/helpers/utils.ts
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
import { Firestore } from "firebase/firestore"
|
|
2
|
-
import fs, { ReadPosition } from "fs"
|
|
3
|
-
import { utils as ffUtils } from "ffjavascript"
|
|
2
|
+
import fs, { ReadPosition, createWriteStream } from "fs"
|
|
4
3
|
import winston, { Logger } from "winston"
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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,
|
|
10
11
|
CircuitInputData,
|
|
11
|
-
ContributionValidity,
|
|
12
|
-
FirebaseDocumentInfo,
|
|
13
|
-
SetupCeremonyData,
|
|
12
|
+
ContributionValidity,
|
|
13
|
+
FirebaseDocumentInfo,
|
|
14
|
+
SetupCeremonyData,
|
|
14
15
|
CeremonySetupTemplate,
|
|
15
16
|
CeremonySetupTemplateCircuitArtifacts,
|
|
17
|
+
StringifiedBigInts,
|
|
18
|
+
BigIntVariants
|
|
16
19
|
} from "../types/index"
|
|
17
20
|
import { finalContributionIndex, genesisZkeyIndex, potFilenameTemplate } from "./constants"
|
|
18
21
|
import {
|
|
@@ -22,252 +25,32 @@ import {
|
|
|
22
25
|
getContributionsCollectionPath
|
|
23
26
|
} from "./database"
|
|
24
27
|
import { CeremonyTimeoutType } from "../types/enums"
|
|
25
|
-
import {
|
|
26
|
-
getPotStorageFilePath,
|
|
27
|
-
getR1csStorageFilePath,
|
|
28
|
-
getWasmStorageFilePath,
|
|
28
|
+
import {
|
|
29
|
+
getPotStorageFilePath,
|
|
30
|
+
getR1csStorageFilePath,
|
|
31
|
+
getWasmStorageFilePath,
|
|
29
32
|
getZkeyStorageFilePath
|
|
30
33
|
} from "./storage"
|
|
31
34
|
import { blake512FromPath } from "./crypto"
|
|
32
|
-
import { Readable, pipeline } from "stream"
|
|
33
|
-
import { promisify } from "util"
|
|
34
35
|
|
|
35
36
|
/**
|
|
36
|
-
*
|
|
37
|
-
* @
|
|
38
|
-
* @
|
|
39
|
-
* @param cleanup <boolean> - whether to delete the r1cs file after parsing
|
|
40
|
-
* @returns any - the data to pass to the cloud function for setup and the circuit artifacts
|
|
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.
|
|
41
40
|
*/
|
|
42
|
-
export const
|
|
43
|
-
// check that the path exists
|
|
44
|
-
if (!fs.existsSync(path)) throw new Error("The provided path to the configuration file does not exist. Please provide an absolute path and try again.")
|
|
45
|
-
|
|
46
|
-
try {
|
|
47
|
-
// read the data
|
|
48
|
-
const data: CeremonySetupTemplate = JSON.parse(fs.readFileSync(path).toString())
|
|
49
|
-
|
|
50
|
-
// verify that the data is correct
|
|
51
|
-
if (data['timeoutMechanismType'] !== CeremonyTimeoutType.DYNAMIC && data['timeoutMechanismType'] !== CeremonyTimeoutType.FIXED)
|
|
52
|
-
throw new Error("Invalid timeout type. Please choose between DYNAMIC and FIXED.")
|
|
53
|
-
|
|
54
|
-
// validate that we have at least 1 circuit input data
|
|
55
|
-
if (!data.circuits || data.circuits.length === 0)
|
|
56
|
-
throw new Error("You need to provide the data for at least 1 circuit.")
|
|
57
|
-
|
|
58
|
-
// validate that the end date is in the future
|
|
59
|
-
let endDate: Date
|
|
60
|
-
let startDate: Date
|
|
61
|
-
try {
|
|
62
|
-
endDate = new Date(data.endDate)
|
|
63
|
-
startDate = new Date(data.startDate)
|
|
64
|
-
} catch (error: any) {
|
|
65
|
-
throw new Error("The dates should follow this format: 2023-07-04T00:00:00.")
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (endDate <= startDate) throw new Error("The end date should be greater than the start date.")
|
|
69
|
-
|
|
70
|
-
const currentDate = new Date()
|
|
71
|
-
|
|
72
|
-
if (endDate <= currentDate || startDate <= currentDate)
|
|
73
|
-
throw new Error("The start and end dates should be in the future.")
|
|
74
|
-
|
|
75
|
-
// validate penalty
|
|
76
|
-
if (data.penalty <= 0) throw new Error("The penalty should be greater than zero.")
|
|
77
|
-
|
|
78
|
-
const circuits: CircuitDocument[] = []
|
|
79
|
-
const urlPattern = /(https?:\/\/[^\s]+)/g
|
|
80
|
-
const commitHashPattern = /^[a-f0-9]{40}$/i
|
|
81
|
-
|
|
82
|
-
const circuitArtifacts: CeremonySetupTemplateCircuitArtifacts[] = []
|
|
83
|
-
|
|
84
|
-
for (let i = 0; i < data.circuits.length; i++) {
|
|
85
|
-
const circuitData = data.circuits[i]
|
|
86
|
-
const artifacts = circuitData.artifacts
|
|
87
|
-
circuitArtifacts.push({
|
|
88
|
-
artifacts: artifacts
|
|
89
|
-
})
|
|
90
|
-
const r1csPath = artifacts.r1csStoragePath
|
|
91
|
-
const wasmPath = artifacts.wasmStoragePath
|
|
92
|
-
|
|
93
|
-
// where we storing the r1cs downloaded
|
|
94
|
-
const localR1csPath = `./${circuitData.name}.r1cs`
|
|
95
|
-
|
|
96
|
-
// check that the artifacts exist in S3
|
|
97
|
-
// we don't need any privileges to download this
|
|
98
|
-
// just the correct region
|
|
99
|
-
const s3 = new S3Client({region: artifacts.region})
|
|
100
|
-
|
|
101
|
-
try {
|
|
102
|
-
await s3.send(new HeadObjectCommand({
|
|
103
|
-
Bucket: artifacts.bucket,
|
|
104
|
-
Key: r1csPath
|
|
105
|
-
}))
|
|
106
|
-
} catch (error: any) {
|
|
107
|
-
throw new Error(`The r1cs file (${r1csPath}) seems to not exist. Please ensure this is correct and that the object is publicly available.`)
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
try {
|
|
111
|
-
await s3.send(new HeadObjectCommand({
|
|
112
|
-
Bucket: artifacts.bucket,
|
|
113
|
-
Key: wasmPath
|
|
114
|
-
}))
|
|
115
|
-
} catch (error: any) {
|
|
116
|
-
throw new Error(`The wasm file (${wasmPath}) seems to not exist. Please ensure this is correct and that the object is publicly available.`)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// download the r1cs to extract the metadata
|
|
120
|
-
const command = new GetObjectCommand({ Bucket: artifacts.bucket, Key: artifacts.r1csStoragePath })
|
|
121
|
-
const response = await s3.send(command)
|
|
122
|
-
const streamPipeline = promisify(pipeline)
|
|
123
|
-
|
|
124
|
-
if (response.$metadata.httpStatusCode !== 200)
|
|
125
|
-
throw new Error("There was an error while trying to download the r1cs file. Please check that the file has the correct permissions (public) set.")
|
|
126
|
-
|
|
127
|
-
if (response.Body instanceof Readable)
|
|
128
|
-
await streamPipeline(response.Body, fs.createWriteStream(localR1csPath))
|
|
129
|
-
|
|
130
|
-
// extract the metadata from the r1cs
|
|
131
|
-
const metadata = getR1CSInfo(localR1csPath)
|
|
132
|
-
|
|
133
|
-
// validate that the circuit hash and template links are valid
|
|
134
|
-
const template = circuitData.template
|
|
135
|
-
|
|
136
|
-
const URLMatch = template.source.match(urlPattern)
|
|
137
|
-
if (!URLMatch || URLMatch.length === 0 || URLMatch.length > 1) throw new Error("You should provide the URL to the circuits templates on GitHub.")
|
|
138
|
-
|
|
139
|
-
const hashMatch = template.commitHash.match(commitHashPattern)
|
|
140
|
-
if (!hashMatch || hashMatch.length === 0 || hashMatch.length > 1) throw new Error("You should provide a valid commit hash of the circuit templates.")
|
|
141
|
-
|
|
142
|
-
// calculate the hash of the r1cs file
|
|
143
|
-
const r1csBlake2bHash = await blake512FromPath(localR1csPath)
|
|
144
|
-
|
|
145
|
-
const circuitPrefix = extractPrefix(circuitData.name)
|
|
146
|
-
|
|
147
|
-
// filenames
|
|
148
|
-
const doubleDigitsPowers = convertToDoubleDigits(metadata.pot!)
|
|
149
|
-
const r1csCompleteFilename = `${circuitData.name}.r1cs`
|
|
150
|
-
const wasmCompleteFilename = `${circuitData.name}.wasm`
|
|
151
|
-
const smallestPowersOfTauCompleteFilenameForCircuit = `${potFilenameTemplate}${doubleDigitsPowers}.ptau`
|
|
152
|
-
const firstZkeyCompleteFilename = `${circuitPrefix}_${genesisZkeyIndex}.zkey`
|
|
153
|
-
|
|
154
|
-
// storage paths
|
|
155
|
-
const r1csStorageFilePath = getR1csStorageFilePath(circuitPrefix, r1csCompleteFilename)
|
|
156
|
-
const wasmStorageFilePath = getWasmStorageFilePath(circuitPrefix, wasmCompleteFilename)
|
|
157
|
-
const potStorageFilePath = getPotStorageFilePath(smallestPowersOfTauCompleteFilenameForCircuit)
|
|
158
|
-
const zkeyStorageFilePath = getZkeyStorageFilePath(circuitPrefix, firstZkeyCompleteFilename)
|
|
159
|
-
|
|
160
|
-
const files: any = {
|
|
161
|
-
potFilename: smallestPowersOfTauCompleteFilenameForCircuit,
|
|
162
|
-
r1csFilename: r1csCompleteFilename,
|
|
163
|
-
wasmFilename: wasmCompleteFilename,
|
|
164
|
-
initialZkeyFilename: firstZkeyCompleteFilename,
|
|
165
|
-
potStoragePath: potStorageFilePath,
|
|
166
|
-
r1csStoragePath: r1csStorageFilePath,
|
|
167
|
-
wasmStoragePath: wasmStorageFilePath,
|
|
168
|
-
initialZkeyStoragePath: zkeyStorageFilePath,
|
|
169
|
-
r1csBlake2bHash: r1csBlake2bHash
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// validate that the compiler hash is a valid hash
|
|
173
|
-
const compiler = circuitData.compiler
|
|
174
|
-
const compilerHashMatch = compiler.commitHash.match(commitHashPattern)
|
|
175
|
-
if (!compilerHashMatch || compilerHashMatch.length === 0 || compilerHashMatch.length > 1) throw new Error("You should provide a valid commit hash of the circuit compiler.")
|
|
176
|
-
|
|
177
|
-
// validate that the verification options are valid
|
|
178
|
-
const verification = circuitData.verification
|
|
179
|
-
if (verification.cfOrVm !== "CF" && verification.cfOrVm !== "VM")
|
|
180
|
-
throw new Error("Please enter a valid verification mechanism: either CF or VM")
|
|
181
|
-
|
|
182
|
-
// @todo VM parameters verification
|
|
183
|
-
// if (verification['cfOrVM'] === "VM") {}
|
|
184
|
-
|
|
185
|
-
// check that the timeout is provided for the correct configuration
|
|
186
|
-
let dynamicThreshold: number | undefined
|
|
187
|
-
let fixedTimeWindow: number | undefined
|
|
188
|
-
|
|
189
|
-
let circuit: CircuitDocument | CircuitInputData = {} as CircuitDocument | CircuitInputData
|
|
190
|
-
|
|
191
|
-
if (data.timeoutMechanismType === CeremonyTimeoutType.DYNAMIC) {
|
|
192
|
-
if (circuitData.dynamicThreshold <= 0)
|
|
193
|
-
throw new Error("The dynamic threshold should be > 0.")
|
|
194
|
-
dynamicThreshold = circuitData.dynamicThreshold
|
|
195
|
-
|
|
196
|
-
// the Circuit data for the ceremony setup
|
|
197
|
-
circuit = {
|
|
198
|
-
name: circuitData.name,
|
|
199
|
-
description: circuitData.description,
|
|
200
|
-
prefix: circuitPrefix,
|
|
201
|
-
sequencePosition: i+1,
|
|
202
|
-
metadata: metadata,
|
|
203
|
-
files: files,
|
|
204
|
-
template: template,
|
|
205
|
-
compiler: compiler,
|
|
206
|
-
verification: verification,
|
|
207
|
-
dynamicThreshold: dynamicThreshold,
|
|
208
|
-
avgTimings: {
|
|
209
|
-
contributionComputation: 0,
|
|
210
|
-
fullContribution: 0,
|
|
211
|
-
verifyCloudFunction: 0
|
|
212
|
-
},
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (data.timeoutMechanismType === CeremonyTimeoutType.FIXED) {
|
|
218
|
-
if (circuitData.fixedTimeWindow <= 0)
|
|
219
|
-
throw new Error("The fixed time window threshold should be > 0.")
|
|
220
|
-
fixedTimeWindow = circuitData.fixedTimeWindow
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
// the Circuit data for the ceremony setup
|
|
224
|
-
circuit = {
|
|
225
|
-
name: circuitData.name,
|
|
226
|
-
description: circuitData.description,
|
|
227
|
-
prefix: circuitPrefix,
|
|
228
|
-
sequencePosition: i+1,
|
|
229
|
-
metadata: metadata,
|
|
230
|
-
files: files,
|
|
231
|
-
template: template,
|
|
232
|
-
compiler: compiler,
|
|
233
|
-
verification: verification,
|
|
234
|
-
fixedTimeWindow: fixedTimeWindow,
|
|
235
|
-
avgTimings: {
|
|
236
|
-
contributionComputation: 0,
|
|
237
|
-
fullContribution: 0,
|
|
238
|
-
verifyCloudFunction: 0
|
|
239
|
-
},
|
|
240
|
-
|
|
241
|
-
}
|
|
242
|
-
}
|
|
41
|
+
export const convertToDoubleDigits = (amount: number): string => (amount < 10 ? `0${amount}` : amount.toString())
|
|
243
42
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
startDate: startDate.valueOf(),
|
|
256
|
-
endDate: endDate.valueOf(),
|
|
257
|
-
timeoutMechanismType: data.timeoutMechanismType,
|
|
258
|
-
penalty: data.penalty
|
|
259
|
-
},
|
|
260
|
-
ceremonyPrefix: extractPrefix(data.title),
|
|
261
|
-
circuits: circuits,
|
|
262
|
-
circuitArtifacts: circuitArtifacts
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
return setupData
|
|
266
|
-
|
|
267
|
-
} catch (error: any) {
|
|
268
|
-
throw new Error(`Error while parsing up the ceremony setup file. ${error.message}`)
|
|
269
|
-
}
|
|
270
|
-
}
|
|
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()
|
|
271
54
|
|
|
272
55
|
/**
|
|
273
56
|
* Extract data from a R1CS metadata file generated with a custom file-based logger.
|
|
@@ -339,18 +122,6 @@ export const formatZkeyIndex = (progress: number): string => {
|
|
|
339
122
|
export const extractPoTFromFilename = (potCompleteFilename: string): number =>
|
|
340
123
|
Number(potCompleteFilename.split("_").pop()?.split(".").at(0))
|
|
341
124
|
|
|
342
|
-
/**
|
|
343
|
-
* Extract a prefix consisting of alphanumeric and underscore characters from a string with arbitrary characters.
|
|
344
|
-
* @dev replaces all special symbols and whitespaces with an underscore char ('_'). Convert all uppercase chars to lowercase.
|
|
345
|
-
* @notice example: str = 'Multiplier-2!2.4.zkey'; output prefix = 'multiplier_2_2_4.zkey'.
|
|
346
|
-
* NB. Prefix extraction is a key process that conditions the name of the ceremony artifacts, download/upload from/to storage, collections paths.
|
|
347
|
-
* @param str <string> - the arbitrary string from which to extract the prefix.
|
|
348
|
-
* @returns <string> - the resulting prefix.
|
|
349
|
-
*/
|
|
350
|
-
export const extractPrefix = (str: string): string =>
|
|
351
|
-
// eslint-disable-next-line no-useless-escape
|
|
352
|
-
str.replace(/[`\s~!@#$%^&*()|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "-").toLowerCase()
|
|
353
|
-
|
|
354
125
|
/**
|
|
355
126
|
* Automate the generation of an entropy for a contribution.
|
|
356
127
|
* @dev Took inspiration from here https://github.com/glamperd/setup-mpc-ui/blob/master/client/src/state/Compute.tsx#L112.
|
|
@@ -457,9 +228,11 @@ export const getPublicAttestationPreambleForContributor = (
|
|
|
457
228
|
ceremonyName: string,
|
|
458
229
|
isFinalizing: boolean
|
|
459
230
|
) =>
|
|
460
|
-
`Hey, I'm ${contributorIdentifier} and I have ${
|
|
461
|
-
|
|
462
|
-
|
|
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:`
|
|
463
236
|
|
|
464
237
|
/**
|
|
465
238
|
* Check and prepare public attestation for the contributor made only of its valid contributions.
|
|
@@ -586,6 +359,48 @@ export const readBytesFromFile = (
|
|
|
586
359
|
return buffer
|
|
587
360
|
}
|
|
588
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
|
+
|
|
589
404
|
/**
|
|
590
405
|
* Return the info about the R1CS file.ù
|
|
591
406
|
* @dev this method was built taking inspiration from
|
|
@@ -648,7 +463,7 @@ export const getR1CSInfo = (localR1CSFilePath: string): CircuitMetadata => {
|
|
|
648
463
|
|
|
649
464
|
try {
|
|
650
465
|
// Get 'number of section' (jump magic r1cs and version1 data).
|
|
651
|
-
const numberOfSections =
|
|
466
|
+
const numberOfSections = leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, 8))
|
|
652
467
|
|
|
653
468
|
// Jump to first section.
|
|
654
469
|
pointer = 12
|
|
@@ -656,13 +471,13 @@ export const getR1CSInfo = (localR1CSFilePath: string): CircuitMetadata => {
|
|
|
656
471
|
// For each section
|
|
657
472
|
for (let i = 0; i < numberOfSections; i++) {
|
|
658
473
|
// Read section type.
|
|
659
|
-
const sectionType =
|
|
474
|
+
const sectionType = leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer))
|
|
660
475
|
|
|
661
476
|
// Jump to section size.
|
|
662
477
|
pointer += 4
|
|
663
478
|
|
|
664
479
|
// Read section size
|
|
665
|
-
const sectionSize = Number(
|
|
480
|
+
const sectionSize = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 8, pointer)))
|
|
666
481
|
|
|
667
482
|
// If at header section (0x00000001 : Header Section).
|
|
668
483
|
if (sectionType === BigInt(1)) {
|
|
@@ -697,22 +512,22 @@ export const getR1CSInfo = (localR1CSFilePath: string): CircuitMetadata => {
|
|
|
697
512
|
pointer += sectionSize - 20
|
|
698
513
|
|
|
699
514
|
// Read R1CS info.
|
|
700
|
-
wires = Number(
|
|
515
|
+
wires = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
|
|
701
516
|
pointer += 4
|
|
702
517
|
|
|
703
|
-
publicOutputs = Number(
|
|
518
|
+
publicOutputs = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
|
|
704
519
|
pointer += 4
|
|
705
520
|
|
|
706
|
-
publicInputs = Number(
|
|
521
|
+
publicInputs = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
|
|
707
522
|
pointer += 4
|
|
708
523
|
|
|
709
|
-
privateInputs = Number(
|
|
524
|
+
privateInputs = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
|
|
710
525
|
pointer += 4
|
|
711
526
|
|
|
712
|
-
labels = Number(
|
|
527
|
+
labels = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 8, pointer)))
|
|
713
528
|
pointer += 8
|
|
714
529
|
|
|
715
|
-
constraints = Number(
|
|
530
|
+
constraints = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
|
|
716
531
|
}
|
|
717
532
|
|
|
718
533
|
pointer += 8 + Number(sectionSize)
|
|
@@ -736,8 +551,232 @@ export const getR1CSInfo = (localR1CSFilePath: string): CircuitMetadata => {
|
|
|
736
551
|
}
|
|
737
552
|
|
|
738
553
|
/**
|
|
739
|
-
*
|
|
740
|
-
* @
|
|
741
|
-
* @
|
|
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
|
|
742
559
|
*/
|
|
743
|
-
export const
|
|
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
|
+
}
|