@aztec/constants 0.77.0-testnet-ignition.17
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/dest/constants.d.ts +3 -0
- package/dest/constants.d.ts.map +1 -0
- package/dest/constants.gen.d.ts +410 -0
- package/dest/constants.gen.d.ts.map +1 -0
- package/dest/constants.gen.js +411 -0
- package/dest/constants.js +4 -0
- package/dest/scripts/constants.in.d.ts +2 -0
- package/dest/scripts/constants.in.d.ts.map +1 -0
- package/dest/scripts/constants.in.js +437 -0
- package/package.json +76 -0
- package/src/constants.gen.ts +416 -0
- package/src/constants.ts +5 -0
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import { dirname, join } from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
const NOIR_CONSTANTS_FILE = '../../../../noir-projects/noir-protocol-circuits/crates/types/src/constants.nr';
|
|
5
|
+
const TS_CONSTANTS_FILE = '../constants.gen.ts';
|
|
6
|
+
const CPP_AZTEC_CONSTANTS_FILE = '../../../../barretenberg/cpp/src/barretenberg/vm/aztec_constants.hpp';
|
|
7
|
+
const PIL_AZTEC_CONSTANTS_FILE = '../../../../barretenberg/cpp/pil/avm/constants_gen.pil';
|
|
8
|
+
// Temp while vm2 is WIP
|
|
9
|
+
const PIL_VM2_AZTEC_CONSTANTS_FILE = '../../../../barretenberg/cpp/pil/vm2/constants_gen.pil';
|
|
10
|
+
const SOLIDITY_CONSTANTS_FILE = '../../../../l1-contracts/src/core/libraries/ConstantsGen.sol';
|
|
11
|
+
// Whitelist of constants that will be copied to aztec_constants.hpp.
|
|
12
|
+
// We don't copy everything as just a handful are needed, and updating them breaks the cache and triggers expensive bb builds.
|
|
13
|
+
const CPP_CONSTANTS = [
|
|
14
|
+
'TOTAL_FEES_LENGTH',
|
|
15
|
+
'GAS_FEES_LENGTH',
|
|
16
|
+
'GAS_LENGTH',
|
|
17
|
+
'CONTENT_COMMITMENT_LENGTH',
|
|
18
|
+
'GLOBAL_VARIABLES_LENGTH',
|
|
19
|
+
'APPEND_ONLY_TREE_SNAPSHOT_LENGTH',
|
|
20
|
+
'PARTIAL_STATE_REFERENCE_LENGTH',
|
|
21
|
+
'STATE_REFERENCE_LENGTH',
|
|
22
|
+
'HEADER_LENGTH',
|
|
23
|
+
'CALL_CONTEXT_LENGTH',
|
|
24
|
+
'PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH',
|
|
25
|
+
'READ_REQUEST_LENGTH',
|
|
26
|
+
'MAX_ENQUEUED_CALLS_PER_TX',
|
|
27
|
+
'MAX_NOTE_HASHES_PER_TX',
|
|
28
|
+
'MAX_NULLIFIERS_PER_TX',
|
|
29
|
+
'MAX_L2_TO_L1_MSGS_PER_TX',
|
|
30
|
+
'MAX_PUBLIC_LOGS_PER_TX',
|
|
31
|
+
'PUBLIC_LOG_DATA_SIZE_IN_FIELDS',
|
|
32
|
+
'MAX_NOTE_HASH_READ_REQUESTS_PER_CALL',
|
|
33
|
+
'MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX',
|
|
34
|
+
'MAX_NULLIFIER_READ_REQUESTS_PER_CALL',
|
|
35
|
+
'MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL',
|
|
36
|
+
'MAX_L1_TO_L2_MSG_READ_REQUESTS_PER_CALL',
|
|
37
|
+
'CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH',
|
|
38
|
+
'MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL',
|
|
39
|
+
'CONTRACT_STORAGE_READ_LENGTH',
|
|
40
|
+
'PUBLIC_INNER_CALL_REQUEST_LENGTH',
|
|
41
|
+
'MAX_PUBLIC_DATA_READS_PER_CALL',
|
|
42
|
+
'MAX_ENQUEUED_CALLS_PER_CALL',
|
|
43
|
+
'NOTE_HASH_LENGTH',
|
|
44
|
+
'MAX_NOTE_HASHES_PER_CALL',
|
|
45
|
+
'NULLIFIER_LENGTH',
|
|
46
|
+
'MAX_NULLIFIERS_PER_CALL',
|
|
47
|
+
'L2_TO_L1_MESSAGE_LENGTH',
|
|
48
|
+
'MAX_L2_TO_L1_MSGS_PER_CALL',
|
|
49
|
+
'LOG_HASH_LENGTH',
|
|
50
|
+
'MAX_PUBLIC_LOGS_PER_CALL',
|
|
51
|
+
'HEADER_LENGTH',
|
|
52
|
+
'GLOBAL_VARIABLES_LENGTH',
|
|
53
|
+
'AZTEC_ADDRESS_LENGTH',
|
|
54
|
+
'START_NOTE_HASH_EXISTS_WRITE_OFFSET',
|
|
55
|
+
'START_NULLIFIER_EXISTS_OFFSET',
|
|
56
|
+
'START_NULLIFIER_NON_EXISTS_OFFSET',
|
|
57
|
+
'START_L1_TO_L2_MSG_EXISTS_WRITE_OFFSET',
|
|
58
|
+
'START_SSTORE_WRITE_OFFSET',
|
|
59
|
+
'START_SLOAD_WRITE_OFFSET',
|
|
60
|
+
'START_EMIT_NOTE_HASH_WRITE_OFFSET',
|
|
61
|
+
'START_EMIT_NULLIFIER_WRITE_OFFSET',
|
|
62
|
+
'START_EMIT_L2_TO_L1_MSG_WRITE_OFFSET',
|
|
63
|
+
'START_EMIT_UNENCRYPTED_LOG_WRITE_OFFSET',
|
|
64
|
+
'SENDER_KERNEL_INPUTS_COL_OFFSET',
|
|
65
|
+
'ADDRESS_KERNEL_INPUTS_COL_OFFSET',
|
|
66
|
+
'CHAIN_ID_KERNEL_INPUTS_COL_OFFSET',
|
|
67
|
+
'VERSION_KERNEL_INPUTS_COL_OFFSET',
|
|
68
|
+
'BLOCK_NUMBER_KERNEL_INPUTS_COL_OFFSET',
|
|
69
|
+
'TIMESTAMP_KERNEL_INPUTS_COL_OFFSET',
|
|
70
|
+
'FEE_PER_DA_GAS_KERNEL_INPUTS_COL_OFFSET',
|
|
71
|
+
'FEE_PER_L2_GAS_KERNEL_INPUTS_COL_OFFSET',
|
|
72
|
+
'IS_STATIC_CALL_KERNEL_INPUTS_COL_OFFSET',
|
|
73
|
+
'DA_START_GAS_KERNEL_INPUTS_COL_OFFSET',
|
|
74
|
+
'L2_START_GAS_KERNEL_INPUTS_COL_OFFSET',
|
|
75
|
+
'DA_END_GAS_KERNEL_INPUTS_COL_OFFSET',
|
|
76
|
+
'L2_END_GAS_KERNEL_INPUTS_COL_OFFSET',
|
|
77
|
+
'TRANSACTION_FEE_KERNEL_INPUTS_COL_OFFSET',
|
|
78
|
+
'MEM_TAG_U1',
|
|
79
|
+
'MEM_TAG_U8',
|
|
80
|
+
'MEM_TAG_U16',
|
|
81
|
+
'MEM_TAG_U32',
|
|
82
|
+
'MEM_TAG_U64',
|
|
83
|
+
'MEM_TAG_U128',
|
|
84
|
+
'MEM_TAG_FF',
|
|
85
|
+
'MAX_L2_GAS_PER_TX_PUBLIC_PORTION',
|
|
86
|
+
'MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS',
|
|
87
|
+
'CANONICAL_AUTH_REGISTRY_ADDRESS',
|
|
88
|
+
'DEPLOYER_CONTRACT_ADDRESS',
|
|
89
|
+
'REGISTERER_CONTRACT_ADDRESS',
|
|
90
|
+
'MULTI_CALL_ENTRYPOINT_ADDRESS',
|
|
91
|
+
'FEE_JUICE_ADDRESS',
|
|
92
|
+
'ROUTER_ADDRESS',
|
|
93
|
+
'FEE_JUICE_BALANCES_SLOT',
|
|
94
|
+
'MAX_PUBLIC_CALLS_TO_UNIQUE_CONTRACT_CLASS_IDS',
|
|
95
|
+
'UPDATED_CLASS_IDS_SLOT',
|
|
96
|
+
'UPDATES_SHARED_MUTABLE_VALUES_LEN'
|
|
97
|
+
];
|
|
98
|
+
const CPP_GENERATORS = [
|
|
99
|
+
'PARTIAL_ADDRESS',
|
|
100
|
+
'CONTRACT_ADDRESS_V1',
|
|
101
|
+
'CONTRACT_LEAF',
|
|
102
|
+
'PUBLIC_KEYS_HASH',
|
|
103
|
+
'NOTE_HASH_NONCE',
|
|
104
|
+
'UNIQUE_NOTE_HASH',
|
|
105
|
+
'SILOED_NOTE_HASH',
|
|
106
|
+
'OUTER_NULLIFIER',
|
|
107
|
+
'PUBLIC_LEAF_INDEX'
|
|
108
|
+
];
|
|
109
|
+
const PIL_CONSTANTS = [
|
|
110
|
+
'MAX_NOTE_HASH_READ_REQUESTS_PER_CALL',
|
|
111
|
+
'MAX_NULLIFIER_READ_REQUESTS_PER_CALL',
|
|
112
|
+
'MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL',
|
|
113
|
+
'MAX_L1_TO_L2_MSG_READ_REQUESTS_PER_CALL',
|
|
114
|
+
'MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL',
|
|
115
|
+
'MAX_PUBLIC_DATA_READS_PER_CALL',
|
|
116
|
+
'MAX_ENQUEUED_CALLS_PER_CALL',
|
|
117
|
+
'MAX_NOTE_HASHES_PER_CALL',
|
|
118
|
+
'MAX_NULLIFIERS_PER_CALL',
|
|
119
|
+
'MAX_L2_TO_L1_MSGS_PER_CALL',
|
|
120
|
+
'MAX_PUBLIC_LOGS_PER_CALL',
|
|
121
|
+
'START_NOTE_HASH_EXISTS_WRITE_OFFSET',
|
|
122
|
+
'START_NULLIFIER_EXISTS_OFFSET',
|
|
123
|
+
'START_NULLIFIER_NON_EXISTS_OFFSET',
|
|
124
|
+
'START_L1_TO_L2_MSG_EXISTS_WRITE_OFFSET',
|
|
125
|
+
'START_SSTORE_WRITE_OFFSET',
|
|
126
|
+
'START_SLOAD_WRITE_OFFSET',
|
|
127
|
+
'START_EMIT_NOTE_HASH_WRITE_OFFSET',
|
|
128
|
+
'START_EMIT_NULLIFIER_WRITE_OFFSET',
|
|
129
|
+
'START_EMIT_L2_TO_L1_MSG_WRITE_OFFSET',
|
|
130
|
+
'START_EMIT_UNENCRYPTED_LOG_WRITE_OFFSET',
|
|
131
|
+
'SENDER_KERNEL_INPUTS_COL_OFFSET',
|
|
132
|
+
'ADDRESS_KERNEL_INPUTS_COL_OFFSET',
|
|
133
|
+
'CHAIN_ID_KERNEL_INPUTS_COL_OFFSET',
|
|
134
|
+
'VERSION_KERNEL_INPUTS_COL_OFFSET',
|
|
135
|
+
'BLOCK_NUMBER_KERNEL_INPUTS_COL_OFFSET',
|
|
136
|
+
'TIMESTAMP_KERNEL_INPUTS_COL_OFFSET',
|
|
137
|
+
'FEE_PER_DA_GAS_KERNEL_INPUTS_COL_OFFSET',
|
|
138
|
+
'FEE_PER_L2_GAS_KERNEL_INPUTS_COL_OFFSET',
|
|
139
|
+
'IS_STATIC_CALL_KERNEL_INPUTS_COL_OFFSET',
|
|
140
|
+
'DA_START_GAS_KERNEL_INPUTS_COL_OFFSET',
|
|
141
|
+
'L2_START_GAS_KERNEL_INPUTS_COL_OFFSET',
|
|
142
|
+
'DA_END_GAS_KERNEL_INPUTS_COL_OFFSET',
|
|
143
|
+
'L2_END_GAS_KERNEL_INPUTS_COL_OFFSET',
|
|
144
|
+
'TRANSACTION_FEE_KERNEL_INPUTS_COL_OFFSET',
|
|
145
|
+
'MEM_TAG_U1',
|
|
146
|
+
'MEM_TAG_U8',
|
|
147
|
+
'MEM_TAG_U16',
|
|
148
|
+
'MEM_TAG_U32',
|
|
149
|
+
'MEM_TAG_U64',
|
|
150
|
+
'MEM_TAG_U128',
|
|
151
|
+
'MEM_TAG_FF',
|
|
152
|
+
'MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS'
|
|
153
|
+
];
|
|
154
|
+
const PIL_GENERATORS = [
|
|
155
|
+
'PARTIAL_ADDRESS',
|
|
156
|
+
'CONTRACT_ADDRESS_V1',
|
|
157
|
+
'CONTRACT_LEAF',
|
|
158
|
+
'PUBLIC_KEYS_HASH',
|
|
159
|
+
'NOTE_HASH_NONCE',
|
|
160
|
+
'UNIQUE_NOTE_HASH',
|
|
161
|
+
'SILOED_NOTE_HASH',
|
|
162
|
+
'OUTER_NULLIFIER',
|
|
163
|
+
'PUBLIC_LEAF_INDEX'
|
|
164
|
+
];
|
|
165
|
+
/**
|
|
166
|
+
* Processes a collection of constants and generates code to export them as TypeScript constants.
|
|
167
|
+
*
|
|
168
|
+
* @param constants - An object containing key-value pairs representing constants.
|
|
169
|
+
* @returns A string containing code that exports the constants as TypeScript constants.
|
|
170
|
+
*/ function processConstantsTS(constants) {
|
|
171
|
+
const code = [];
|
|
172
|
+
Object.entries(constants).forEach(([key, value])=>{
|
|
173
|
+
code.push(`export const ${key} = ${+value > Number.MAX_SAFE_INTEGER ? value + 'n' : value};`);
|
|
174
|
+
});
|
|
175
|
+
return code.join('\n');
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Processes a collection of constants and generates code to export them as cpp constants.
|
|
179
|
+
* Required to ensure consistency between the constants used in pil and used in the vm witness generator.
|
|
180
|
+
*
|
|
181
|
+
* @param constants - An object containing key-value pairs representing constants.
|
|
182
|
+
* @returns A string containing code that exports the constants as cpp constants.
|
|
183
|
+
*/ function processConstantsCpp(constants, generatorIndices) {
|
|
184
|
+
const code = [];
|
|
185
|
+
Object.entries(constants).forEach(([key, value])=>{
|
|
186
|
+
if (CPP_CONSTANTS.includes(key) || key.startsWith('AVM_') && key !== 'AVM_VK_INDEX') {
|
|
187
|
+
// stringify large numbers
|
|
188
|
+
code.push(`#define ${key} ${BigInt(value) > 2n ** 31n - 1n ? `"0x${BigInt(value).toString(16)}"` : value}`);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
Object.entries(generatorIndices).forEach(([key, value])=>{
|
|
192
|
+
if (CPP_GENERATORS.includes(key)) {
|
|
193
|
+
code.push(`#define GENERATOR_INDEX__${key} ${value}`);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
return code.join('\n');
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Processes a collection of constants and generates code to export them as PIL constants.
|
|
200
|
+
* Required to ensure consistency between the constants used in pil and used in the vm witness generator.
|
|
201
|
+
*
|
|
202
|
+
* @param constants - An object containing key-value pairs representing constants.
|
|
203
|
+
* @returns A string containing code that exports the constants as cpp constants.
|
|
204
|
+
*/ function processConstantsPil(constants, generatorIndices) {
|
|
205
|
+
const code = [];
|
|
206
|
+
Object.entries(constants).forEach(([key, value])=>{
|
|
207
|
+
if (PIL_CONSTANTS.includes(key)) {
|
|
208
|
+
code.push(` pol ${key} = ${value};`);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
Object.entries(generatorIndices).forEach(([key, value])=>{
|
|
212
|
+
if (PIL_GENERATORS.includes(key)) {
|
|
213
|
+
code.push(` pol GENERATOR_INDEX__${key} = ${value};`);
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
return code.join('\n');
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Processes an enum and generates code to export it as a TypeScript enum.
|
|
220
|
+
*
|
|
221
|
+
* @param enumName - The name of the enum.
|
|
222
|
+
* @param enumValues - An object containing key-value pairs representing enum values.
|
|
223
|
+
* @returns A string containing code that exports the enum as a TypeScript enum.
|
|
224
|
+
*/ function processEnumTS(enumName, enumValues) {
|
|
225
|
+
const code = [];
|
|
226
|
+
code.push(`export enum ${enumName} {`);
|
|
227
|
+
Object.entries(enumValues).forEach(([key, value])=>{
|
|
228
|
+
code.push(` ${key} = ${value},`);
|
|
229
|
+
});
|
|
230
|
+
code.push('}');
|
|
231
|
+
return code.join('\n');
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Processes a collection of constants and generates code to export them as Solidity constants.
|
|
235
|
+
*
|
|
236
|
+
* @param constants - An object containing key-value pairs representing constants.
|
|
237
|
+
* @param prefix - A prefix to add to the constant names.
|
|
238
|
+
* @returns A string containing code that exports the constants as Noir constants.
|
|
239
|
+
*/ function processConstantsSolidity(constants, prefix = '') {
|
|
240
|
+
const code = [];
|
|
241
|
+
Object.entries(constants).forEach(([key, value])=>{
|
|
242
|
+
if (!key.startsWith('AVM_')) {
|
|
243
|
+
code.push(` uint256 internal constant ${prefix}${key} = ${value};`);
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
return code.join('\n');
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Generate the constants file in Typescript.
|
|
250
|
+
*/ function generateTypescriptConstants({ constants, generatorIndexEnum }, targetPath) {
|
|
251
|
+
const result = [
|
|
252
|
+
'/* eslint-disable */\n// GENERATED FILE - DO NOT EDIT, RUN yarn remake-constants',
|
|
253
|
+
processConstantsTS(constants),
|
|
254
|
+
processEnumTS('GeneratorIndex', generatorIndexEnum)
|
|
255
|
+
].join('\n');
|
|
256
|
+
fs.writeFileSync(targetPath, result);
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Generate the constants file in C++.
|
|
260
|
+
*/ function generateCppConstants({ constants, generatorIndexEnum }, targetPath) {
|
|
261
|
+
const resultCpp = `// GENERATED FILE - DO NOT EDIT, RUN yarn remake-constants in yarn-project/constants
|
|
262
|
+
#pragma once
|
|
263
|
+
|
|
264
|
+
${processConstantsCpp(constants, generatorIndexEnum)}
|
|
265
|
+
`;
|
|
266
|
+
fs.writeFileSync(targetPath, resultCpp);
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Generate the constants file in PIL.
|
|
270
|
+
*/ function generatePilConstants({ constants, generatorIndexEnum }, targetPath) {
|
|
271
|
+
const resultPil = `// GENERATED FILE - DO NOT EDIT, RUN yarn remake-constants in yarn-project/constants
|
|
272
|
+
namespace constants;
|
|
273
|
+
${processConstantsPil(constants, generatorIndexEnum)}
|
|
274
|
+
\n`;
|
|
275
|
+
fs.writeFileSync(targetPath, resultPil);
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Generate the constants file in Solidity.
|
|
279
|
+
*/ function generateSolidityConstants({ constants }, targetPath) {
|
|
280
|
+
const resultSolidity = `// GENERATED FILE - DO NOT EDIT, RUN yarn remake-constants in yarn-project/constants
|
|
281
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
282
|
+
// Copyright 2023 Aztec Labs.
|
|
283
|
+
pragma solidity >=0.8.27;
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* @title Constants Library
|
|
287
|
+
* @author Aztec Labs
|
|
288
|
+
* @notice Library that contains constants used throughout the Aztec protocol
|
|
289
|
+
*/
|
|
290
|
+
library Constants {
|
|
291
|
+
// Prime field modulus
|
|
292
|
+
uint256 internal constant P =
|
|
293
|
+
21888242871839275222246405745257275088548364400416034343698204186575808495617;
|
|
294
|
+
|
|
295
|
+
${processConstantsSolidity(constants)}
|
|
296
|
+
}\n`;
|
|
297
|
+
fs.writeFileSync(targetPath, resultSolidity);
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Parse the content of the constants file in Noir.
|
|
301
|
+
*/ function parseNoirFile(fileContent) {
|
|
302
|
+
const constantsExpressions = [];
|
|
303
|
+
const generatorIndexEnum = {};
|
|
304
|
+
const emptyExpression = ()=>({
|
|
305
|
+
name: '',
|
|
306
|
+
content: []
|
|
307
|
+
});
|
|
308
|
+
let expression = emptyExpression();
|
|
309
|
+
fileContent.split('\n').forEach((l)=>{
|
|
310
|
+
const line = l.trim();
|
|
311
|
+
if (!line) {
|
|
312
|
+
// Empty line.
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
if (line.match(/^\/\/|^\s*\/?\*/)) {
|
|
316
|
+
// Comment.
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
{
|
|
320
|
+
const [, name, _type, value, end] = line.match(/global\s+(\w+)(\s*:\s*\w+)?\s*=\s*([^;]*)(;)?/) || [];
|
|
321
|
+
if (name && value) {
|
|
322
|
+
const [, indexName] = name.match(/GENERATOR_INDEX__(\w+)/) || [];
|
|
323
|
+
if (indexName) {
|
|
324
|
+
// Generator index.
|
|
325
|
+
generatorIndexEnum[indexName] = +value;
|
|
326
|
+
} else if (end) {
|
|
327
|
+
// A single line of expression.
|
|
328
|
+
constantsExpressions.push([
|
|
329
|
+
name,
|
|
330
|
+
value
|
|
331
|
+
]);
|
|
332
|
+
} else {
|
|
333
|
+
// The first line of an expression.
|
|
334
|
+
expression = {
|
|
335
|
+
name,
|
|
336
|
+
content: [
|
|
337
|
+
value
|
|
338
|
+
]
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
return;
|
|
342
|
+
} else if (name) {
|
|
343
|
+
// This case happens if we have only a name, with the value being on the next line
|
|
344
|
+
expression = {
|
|
345
|
+
name,
|
|
346
|
+
content: []
|
|
347
|
+
};
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (expression.name) {
|
|
352
|
+
// The expression continues...
|
|
353
|
+
const [, content, end] = line.match(/\s*([^;]+)(;)?/) || [];
|
|
354
|
+
expression.content.push(content);
|
|
355
|
+
if (end) {
|
|
356
|
+
// The last line of an expression.
|
|
357
|
+
constantsExpressions.push([
|
|
358
|
+
expression.name,
|
|
359
|
+
expression.content.join('')
|
|
360
|
+
]);
|
|
361
|
+
expression = emptyExpression();
|
|
362
|
+
}
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
if (!line.includes('use crate')) {
|
|
366
|
+
// eslint-disable-next-line no-console
|
|
367
|
+
console.warn(`Unknown content: ${line}`);
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
const constants = evaluateExpressions(constantsExpressions);
|
|
371
|
+
return {
|
|
372
|
+
constants,
|
|
373
|
+
generatorIndexEnum
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Converts constants defined as expressions to constants with actual values.
|
|
378
|
+
* @param expressions Ordered list of expressions of the type: "CONSTANT_NAME: expression".
|
|
379
|
+
* where the expression is a string that can be evaluated to a number.
|
|
380
|
+
* For example: "CONSTANT_NAME: 2 + 2" or "CONSTANT_NAME: CONSTANT_A * CONSTANT_B".
|
|
381
|
+
* @returns Parsed expressions of the form: "CONSTANT_NAME: number_as_string".
|
|
382
|
+
*/ function evaluateExpressions(expressions) {
|
|
383
|
+
const constants = {};
|
|
384
|
+
const knownBigInts = [
|
|
385
|
+
'AZTEC_EPOCH_DURATION',
|
|
386
|
+
'FEE_RECIPIENT_LENGTH'
|
|
387
|
+
];
|
|
388
|
+
// Create JS expressions. It is not as easy as just evaluating the expression!
|
|
389
|
+
// We basically need to convert everything to BigInts, otherwise things don't fit.
|
|
390
|
+
// However, (1) the bigints need to be initialized from strings; (2) everything needs to
|
|
391
|
+
// be a bigint, even the actual constant values!
|
|
392
|
+
const prelude = expressions.map(([name, rhs])=>{
|
|
393
|
+
const guardedRhs = rhs// Remove 'as u8' and 'as u32' castings
|
|
394
|
+
.replaceAll(' as u8', '').replaceAll(' as u32', '')// Remove the 'AztecAddress::from_field(...)' pattern
|
|
395
|
+
.replace(/AztecAddress::from_field\((0x[a-fA-F0-9]+|[0-9]+)\)/g, '$1')// We make some space around the parentheses, so that constant numbers are still split.
|
|
396
|
+
.replace(/\(/g, '( ').replace(/\)/g, ' )')// We also make some space around common operators
|
|
397
|
+
.replace(/\+/g, ' + ').replace(/(?<!\/)\*(?!\/)/, ' * ')// We split the expression into terms...
|
|
398
|
+
.split(/\s+/)// ...and then we convert each term to a BigInt if it is a number.
|
|
399
|
+
.map((term)=>isNaN(+term) ? term : `BigInt('${term}')`)// .. also, we convert the known bigints to BigInts.
|
|
400
|
+
.map((term)=>knownBigInts.includes(term) ? `BigInt(${term})` : term)// We join the terms back together.
|
|
401
|
+
.join(' ');
|
|
402
|
+
return `var ${name} = ${guardedRhs};`;
|
|
403
|
+
}).join('\n');
|
|
404
|
+
// Extract each value from the expressions. Observe that this will still be a string,
|
|
405
|
+
// so that we can then choose to express it as BigInt or Number depending on the size.
|
|
406
|
+
for (const [name, _] of expressions){
|
|
407
|
+
constants[name] = eval(prelude + `; BigInt(${name}).toString()`);
|
|
408
|
+
}
|
|
409
|
+
return constants;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Convert the Noir constants to TypeScript and Solidity.
|
|
413
|
+
*/ function main() {
|
|
414
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
415
|
+
const noirConstantsFile = join(__dirname, NOIR_CONSTANTS_FILE);
|
|
416
|
+
const noirConstants = fs.readFileSync(noirConstantsFile, 'utf-8');
|
|
417
|
+
const parsedContent = parseNoirFile(noirConstants);
|
|
418
|
+
// Typescript
|
|
419
|
+
const tsTargetPath = join(__dirname, TS_CONSTANTS_FILE);
|
|
420
|
+
generateTypescriptConstants(parsedContent, tsTargetPath);
|
|
421
|
+
// Cpp
|
|
422
|
+
const cppTargetPath = join(__dirname, CPP_AZTEC_CONSTANTS_FILE);
|
|
423
|
+
generateCppConstants(parsedContent, cppTargetPath);
|
|
424
|
+
// PIL - VM1
|
|
425
|
+
const pilTargetPath = join(__dirname, PIL_AZTEC_CONSTANTS_FILE);
|
|
426
|
+
generatePilConstants(parsedContent, pilTargetPath);
|
|
427
|
+
// PIL - VM2
|
|
428
|
+
const pilVm2TargetPath = join(__dirname, PIL_VM2_AZTEC_CONSTANTS_FILE);
|
|
429
|
+
generatePilConstants(parsedContent, pilVm2TargetPath);
|
|
430
|
+
// Solidity
|
|
431
|
+
const solidityTargetPath = join(__dirname, SOLIDITY_CONSTANTS_FILE);
|
|
432
|
+
fs.mkdirSync(dirname(solidityTargetPath), {
|
|
433
|
+
recursive: true
|
|
434
|
+
});
|
|
435
|
+
generateSolidityConstants(parsedContent, solidityTargetPath);
|
|
436
|
+
}
|
|
437
|
+
main();
|
package/package.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aztec/constants",
|
|
3
|
+
"version": "0.77.0-testnet-ignition.17",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"inherits": [
|
|
6
|
+
"../package.common.json"
|
|
7
|
+
],
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./dest/constants.js"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "yarn clean && tsc -b",
|
|
13
|
+
"build:dev": "tsc -b --watch",
|
|
14
|
+
"clean": "rm -rf ./dest .tsbuildinfo",
|
|
15
|
+
"formatting": "run -T prettier --check ./src && run -T eslint ./src",
|
|
16
|
+
"formatting:fix": "run -T eslint --fix ./src && run -T prettier -w ./src",
|
|
17
|
+
"remake-constants": "node --loader @swc-node/register/esm src/scripts/constants.in.ts && cd ../../l1-contracts && forge fmt",
|
|
18
|
+
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"tslib": "^2.4.0"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@jest/globals": "^29.5.0",
|
|
25
|
+
"@types/jest": "^29.5.0",
|
|
26
|
+
"@types/node": "^18.7.23",
|
|
27
|
+
"eslint": "^8.35.0",
|
|
28
|
+
"jest": "^29.5.0",
|
|
29
|
+
"prettier": "^2.8.4",
|
|
30
|
+
"ts-node": "^10.9.1",
|
|
31
|
+
"typescript": "^5.0.4"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dest",
|
|
35
|
+
"src",
|
|
36
|
+
"!*.test.*",
|
|
37
|
+
"!src/scripts/*"
|
|
38
|
+
],
|
|
39
|
+
"types": "./dest/index.d.ts",
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=18"
|
|
42
|
+
},
|
|
43
|
+
"jest": {
|
|
44
|
+
"extensionsToTreatAsEsm": [
|
|
45
|
+
".ts"
|
|
46
|
+
],
|
|
47
|
+
"transform": {
|
|
48
|
+
"^.+\\.tsx?$": [
|
|
49
|
+
"@swc/jest",
|
|
50
|
+
{
|
|
51
|
+
"jsc": {
|
|
52
|
+
"parser": {
|
|
53
|
+
"syntax": "typescript",
|
|
54
|
+
"decorators": true
|
|
55
|
+
},
|
|
56
|
+
"transform": {
|
|
57
|
+
"decoratorVersion": "2022-03"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
]
|
|
62
|
+
},
|
|
63
|
+
"moduleNameMapper": {
|
|
64
|
+
"^(\\.{1,2}/.*)\\.[cm]?js$": "$1"
|
|
65
|
+
},
|
|
66
|
+
"testRegex": "./src/.*\\.test\\.(js|mjs|ts)$",
|
|
67
|
+
"rootDir": "./src",
|
|
68
|
+
"reporters": [
|
|
69
|
+
"default"
|
|
70
|
+
],
|
|
71
|
+
"testTimeout": 120000,
|
|
72
|
+
"setupFiles": [
|
|
73
|
+
"../../foundation/src/jest/setup.mjs"
|
|
74
|
+
]
|
|
75
|
+
}
|
|
76
|
+
}
|