-
Notifications
You must be signed in to change notification settings - Fork 268
/
generate_variants.js
executable file
·190 lines (155 loc) · 5.3 KB
/
generate_variants.js
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
const TOML = require("@iarna/toml");
const fs = require("fs");
const path = require("path");
const config = require("../private_kernel_reset_config.json");
const root = path.join(__dirname, "../");
const sourceFolder = "private-kernel-reset";
const sourceSimulatedFolder = "private-kernel-reset-simulated";
const autogeneratedCircuitsFolder = "crates/autogenerated";
const dimensionNames = Object.keys(config.dimensions);
const aliases = {
tiny: [4, 4, 4, 4, 4, 4, 4, 4, 4],
full: [64, 64, 64, 64, 64, 64, 64, 64, 32],
};
function getResetTag(dimensions) {
return dimensions.join("_");
}
function areValidDimensions(dimensions) {
return (
dimensions.length === dimensionNames.length &&
dimensions.every((v) => typeof v === "number")
);
}
function isFullDimensions(dimensions) {
return dimensions.every((v, i) => v === aliases.full[i]);
}
function getDimensionsList(allowedVariants) {
if (allowedVariants.length) {
// allowedVariant can be a key in aliases, or a tag.
return allowedVariants
.map((name) => aliases[name] || allowedVariants.split("_").map((v) => +v))
.filter(areValidDimensions);
}
// Variants.
const dimensionsList = dimensionNames.reduce(
(acc, name) =>
acc.flatMap((combination) =>
config.dimensions[name].variants.map((v) => [...combination, v])
),
[[]]
);
// Standalone.
dimensionNames.forEach((name, i) => {
config.dimensions[name].standalone.forEach((val) => {
const dimensions = Array(dimensionNames.length).fill(0);
dimensions[i] = val;
dimensionsList.push(dimensions);
});
});
// Special cases.
config.specialCases.forEach((dimensions) => dimensionsList.push(dimensions));
return dimensionsList;
}
function generateDimensions(dimensionsList) {
const dimensionValuesStrings = dimensionsList.map(
(dimensions) => ` [${dimensions.join(", ")}]`
);
const fileContent = ["[", dimensionValuesStrings.join(",\n"), "]"].join("\n");
fs.writeFileSync(
path.join(root, "private_kernel_reset_dimensions.json"),
fileContent
);
}
function generateCircuits(dimensionsList, nargoToml, isSimulated) {
const originalFolder = !isSimulated ? sourceFolder : sourceSimulatedFolder;
const originalCratePath = path.join(root, "crates", originalFolder);
const originalNargoToml = TOML.parse(
fs.readFileSync(path.join(originalCratePath, "Nargo.toml"), "utf8")
);
const originalName = originalNargoToml.package.name;
for (const dimensions of dimensionsList) {
// The default private-kernel-reset(-simulated) has full dimensions.
if (isFullDimensions(dimensions)) {
continue;
}
const variantNargoToml = structuredClone(originalNargoToml);
const tag = getResetTag(dimensions);
variantNargoToml.package.name = `${originalName}_${tag}`;
for ([depName, depDescriptor] of Object.entries(
variantNargoToml.dependencies
)) {
if (depDescriptor.path) {
depDescriptor.path = "../" + depDescriptor.path;
}
}
let mainDotNoirCode = fs.readFileSync(
path.join(originalCratePath, "src/main.nr"),
"utf8"
);
for (let i = 0; i < dimensions.length; i++) {
const value = dimensions[i];
const name = dimensionNames[i];
const regex = new RegExp(`^global\\s+${name}:\\su32\\s=\\s(.*);.*$`, "m");
if (!mainDotNoirCode.match(regex)) {
throw new Error(`Could not find dimension ${name} in main.nr`);
}
// Update value.
mainDotNoirCode = mainDotNoirCode.replace(
regex,
`global ${name}: u32 = ${value};`
);
// Remove constants.
mainDotNoirCode = mainDotNoirCode.replace(
/use dep::types::\{\s*constants::\{[^}]*?\},\s*([^}]*?)\};/,
(_, rest) => {
const restVars = rest
.split(",")
.map((item) => item.trim())
.filter((item) => item.length > 0);
return restVars.length > 1
? `use dep::types::{${restVars.join(", ")}};`
: restVars.length === 1
? `use dep::types::${restVars[0]};`
: "";
}
);
}
const variantFolder = path.join(
autogeneratedCircuitsFolder,
`${originalFolder}-${tag}`
);
fs.mkdirSync(path.join(variantFolder, "src"), {
recursive: true,
});
fs.writeFileSync(path.join(variantFolder, "src/main.nr"), mainDotNoirCode);
fs.writeFileSync(
path.join(variantFolder, "Nargo.toml"),
TOML.stringify(variantNargoToml)
);
nargoToml.workspace.members.push(variantFolder);
}
}
function main() {
const allowedVariants = process.argv.slice(2) || [];
const nargoToml = TOML.parse(
fs.readFileSync("./Nargo.template.toml", "utf8")
);
const dimensionsList = getDimensionsList(allowedVariants);
const autogeneratedCircuitsPath = path.join(
root,
autogeneratedCircuitsFolder
);
if (fs.existsSync(autogeneratedCircuitsPath)) {
fs.rmSync(autogeneratedCircuitsPath, { recursive: true });
}
fs.mkdirSync(autogeneratedCircuitsPath);
generateDimensions(dimensionsList);
generateCircuits(dimensionsList, nargoToml, false);
generateCircuits(dimensionsList, nargoToml, true);
const content = [
"# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.",
TOML.stringify(nargoToml),
].join("\n");
fs.writeFileSync("./Nargo.toml", content);
}
main();