-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex.ts
113 lines (94 loc) · 3.11 KB
/
regex.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
import { genFromDecomposed } from '@zk-email/zk-regex-compiler';
// import { v4 as uuidv4 } from 'uuid';
// import { existsSync, mkdirSync } from 'fs';
import fs from 'fs/promises';
import init from '@zk-email/zk-regex-compiler';
import path from 'path';
import { exec as execCallback } from 'child_process';
import { promisify } from 'util';
import { DecomposedRegex } from 'zk-email-sdk-js';
const exec = promisify(execCallback);
// Using exec (for simpler commands)
async function execCmd(command: string): Promise<string> {
try {
const { stdout, stderr } = await exec(command);
console.log('Command Output:', stdout);
if (stderr) {
throw new Error(`Command failed: ${stderr}`);
}
return stdout;
} catch (error) {
throw new Error(`Failed to execute command: ${error}`);
}
}
// TODO: use env var
const COMPILED_CIRCUIT_OUT_DIR = './tmp/compiled_circuit';
init()
.then(() => {
console.log('Initialized wasm');
})
.catch((err) => {
console.error('Failed to initialize wasm: ', err);
});
/* 1. Create and save a new circuit.
* 2. Deploy a verifier on chain.
* 3. Save the circuit and infomation about the verifier contract to the registry.
*/
// type DecomposedRegexPart = {
// is_public: boolean;
// regex_def: string;
// };
// type DecomposedRegex = {
// parts: DecomposedRegexPart[];
// };
// interface Submission {
// DecomposedRegex: DecomposedRegex;
// circomTemplateName: string;
// }
export async function generateDecomposedRegexesCircuitTemplates(
decomposedRegexes: DecomposedRegex[],
id: string,
) {
for (const decomposedRegex of decomposedRegexes) {
const str = JSON.stringify(decomposedRegex);
const templateStr = genFromDecomposed(str, decomposedRegex.name);
const name = decomposedRegex.name.replaceAll(' ', '');
await fs.writeFile(`./tmp/${id}/regex/${name}.circom`, templateStr);
}
}
// save compiled circuit
// circom multiplier2.circom --r1cs --wasm --sym -o output_dir -l ./node_modules/
export async function compileCircuit(
circuitPath: string,
id: string,
): Promise<void> {
const nodeModulesDir = './node_modules';
const outputDir = `${COMPILED_CIRCUIT_OUT_DIR}/${id}`;
console.log('outputDIr: ', outputDIr);
await fs.mkdir(outputDir);
const circomResult = await execCmd(
`circom ${circuitPath} --r1cs --wasm --sym -o ${outputDir} -l ${nodeModulesDir}`,
);
console.log('circomResult: ', circomResult);
}
// function handleSubmission(submission: Submission): string {
// const tempFolderPath = './tmp';
// if (!existsSync(tempFolderPath)) {
// mkdirSync(tempFolderPath);
// }
// let circuit = createCircomCircuit(
// submission.DecomposedRegex,
// submission.circomTemplateName,
// );
// let id = uuidv4();
// // Create a new folder for the submission
// let submissionFolderPath = `${tempFolderPath}/${id}`;
// mkdirSync(submissionFolderPath);
// // Write the circom file
// let circomFilePath = `${submissionFolderPath}/${id}.circom`;
// // Compile the circom file
// let compiled = compileCircomCircuit(circomFilePath);
// if (!compiled) {
// return 'Error during compilation';
// }
// }