Skip to content

Commit 22f4655

Browse files
authored
chore: constants codegen release (#24728)
Hooks the new package to release workflows (blocked until we decide where in npm that should live and make sure the corresponding org/scope is created) Closes F-821
1 parent 38c0eb2 commit 22f4655

9 files changed

Lines changed: 133 additions & 11 deletions

File tree

bootstrap.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ function release {
579579
noir
580580
l1-contracts
581581
noir-projects/aztec-nr
582+
protocol/constants-codegen
582583
yarn-project
583584
boxes
584585
aztec-up
@@ -603,10 +604,11 @@ function release_dryrun {
603604
function private_release {
604605
# Release flow for the private repo, run on a (nightly) ci-private-release PR. We publish only to our
605606
# internal GCP Artifact Registry: the docker image (release-image -> INTERNAL_DOCKER_REGISTRY that
606-
# GKE/staging pulls from) and the npm packages (barretenberg/ts, noir, wsdb, yarn-project -> the
607-
# INTERNAL_NPM_REGISTRY npm repo). We run the release step for real on exactly those components and do
608-
# not invoke the others — the remaining release sources publish public artifacts (github releases,
609-
# crates.io, the aztec-up/playground S3 installers) and are not interrelated with these.
607+
# GKE/staging pulls from) and the npm packages (barretenberg/ts, noir, ipc-runtime, wsdb,
608+
# protocol/constants-codegen, yarn-project -> the INTERNAL_NPM_REGISTRY npm repo). We run the release
609+
# step for real on exactly those components and do not invoke the others — the remaining release
610+
# sources publish public artifacts (github releases, crates.io, the aztec-up/playground S3 installers)
611+
# and are not interrelated with these.
610612
echo_header "private release"
611613

612614
# Default to the private staging Artifact Registry; override via the INTERNAL_*_REGISTRY env vars.
@@ -671,7 +673,7 @@ function private_release {
671673
# them. @aztec/world-state has a runtime dependency on @aztec/wsdb, and the ipc-codegen-generated
672674
# @aztec/wsdb in turn has a runtime dependency on @aztec/ipc-runtime, so ipc-runtime must precede wsdb.
673675
# npm packages are platform-independent, so only the docker image is published on arm64.
674-
local publish=(barretenberg/ts noir ipc-runtime wsdb yarn-project release-image)
676+
local publish=(barretenberg/ts noir ipc-runtime wsdb protocol/constants-codegen yarn-project release-image)
675677
if [ $(arch) == arm64 ]; then
676678
publish=(release-image)
677679
fi

protocol/constants-codegen/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This directory will contain the standalone cross-language generator for Aztec pr
55
## Version 1 interface
66

77
The command reads a primary Noir source file, optionally adds named constants from other Noir files, and writes any
8-
requested combination of the four outputs produced by the existing generator.
8+
requested combination of the supported outputs.
99

1010
```text
1111
constants-codegen \
@@ -14,7 +14,8 @@ constants-codegen \
1414
[--typescript <output.ts>] \
1515
[--cpp <output.hpp>] \
1616
[--pil <output.pil>] \
17-
[--solidity <output.sol>]
17+
[--solidity <output.sol>] \
18+
[--rust <output.rs>]
1819
```
1920

2021
- `--input` is required.
@@ -29,6 +30,9 @@ Version 1 preserves the existing renderer behavior, including each language's cu
2930
TypeScript emits all parsed constants and domain separators; C++, PIL, and Solidity retain their current selected
3031
subsets and formatting.
3132

33+
Rust emits all parsed constants and domain separators: values that fit `u128` become `pub const NAME: u128` items,
34+
and larger field-sized values become `pub const NAME: &str` hex-string items.
35+
3236
## Compatibility target
3337

3438
The implementation must preserve the symbols and values currently checked in at:

protocol/constants-codegen/bootstrap.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@ function build {
1515

1616
function test_cmds {
1717
echo "$hash cd protocol/constants-codegen && node --test src/*.test.ts"
18+
echo "$hash cd protocol/constants-codegen && ./scripts/test-package.sh"
1819
}
1920

2021
function test {
2122
echo_header "constants-codegen test"
2223
test_cmds | filter_test_cmds | parallelize
2324
}
2425

26+
function release {
27+
npm_install_deps
28+
retry "deploy_npm ${REF_NAME#v}"
29+
}
30+
2531
case "$cmd" in
2632
"")
2733
build

protocol/constants-codegen/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@aztec-foundation/constants-codegen",
2+
"name": "@aztec/constants-codegen",
33
"version": "0.0.0",
44
"description": "Generate Aztec protocol constants from Noir definitions",
55
"license": "Apache-2.0",
@@ -15,7 +15,7 @@
1515
"build": "tsc -p tsconfig.json",
1616
"clean": "rm -rf dest",
1717
"prepack": "yarn build",
18-
"test": "node --test src/*.test.ts"
18+
"test": "node --test src/*.test.ts && yarn build && ./scripts/test-package.sh"
1919
},
2020
"devDependencies": {
2121
"@types/node": "^22",
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
package_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
6+
work_dir=$(mktemp -d)
7+
trap 'rm -rf "$work_dir"' EXIT
8+
9+
(cd "$package_dir" && npm pack --ignore-scripts --pack-destination "$work_dir" --quiet >/dev/null)
10+
11+
shopt -s nullglob
12+
tarballs=("$work_dir"/*.tgz)
13+
if [ "${#tarballs[@]}" -ne 1 ]; then
14+
echo "expected npm pack to produce one tarball, found ${#tarballs[@]}" >&2
15+
exit 1
16+
fi
17+
18+
input="$work_dir/constants.nr"
19+
cat > "$input" <<'EOF'
20+
pub global MAX_FIELD_VALUE: Field =
21+
21888242871839275222246405745257275088548364400416034343698204186575808495616;
22+
pub global MAX_ETH_ADDRESS_VALUE: Field = 0xffffffffffffffffffffffffffffffffffffffff;
23+
pub global ARCHIVE_HEIGHT: u32 = 30;
24+
EOF
25+
26+
mkdir "$work_dir/consumer"
27+
(
28+
cd "$work_dir/consumer"
29+
npm init --yes >/dev/null
30+
npm install --ignore-scripts "${tarballs[0]}" >/dev/null
31+
./node_modules/.bin/constants-codegen \
32+
--input "$input" \
33+
--typescript "$work_dir/constants.ts" \
34+
--cpp "$work_dir/constants.hpp" \
35+
--pil "$work_dir/constants.pil" \
36+
--solidity "$work_dir/Constants.sol" \
37+
--rust "$work_dir/constants.rs"
38+
)
39+
40+
function check_output {
41+
if ! grep -Fq "$2" "$work_dir/$1"; then
42+
echo "installed constants-codegen produced unexpected $1:" >&2
43+
cat "$work_dir/$1" >&2
44+
exit 1
45+
fi
46+
}
47+
48+
# Each language receives a different allowlisted subset of the input constants,
49+
# so each check uses a constant known to be in that language's subset.
50+
check_output constants.ts 'export const ARCHIVE_HEIGHT = 30;'
51+
check_output constants.hpp '#define ARCHIVE_HEIGHT 30'
52+
check_output constants.pil 'pol MAX_ETH_ADDRESS_VALUE = 1461501637330902918203684832716283019655932542975;'
53+
check_output Constants.sol 'uint256 internal constant MAX_FIELD_VALUE = 21888242871839275222246405745257275088548364400416034343698204186575808495616;'
54+
check_output constants.rs 'pub const ARCHIVE_HEIGHT: u128 = 30;'

protocol/constants-codegen/src/cli.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
evaluateExpressions,
99
generateCppConstants,
1010
generatePilConstants,
11+
generateRustConstants,
1112
generateSolidityConstants,
1213
generateTypescriptConstants,
1314
parseNoirFile,
@@ -41,6 +42,7 @@ function run(args: string[]): void {
4142
cpp: { type: 'string' },
4243
pil: { type: 'string' },
4344
solidity: { type: 'string' },
45+
rust: { type: 'string' },
4446
},
4547
strict: true,
4648
});
@@ -54,6 +56,7 @@ function run(args: string[]): void {
5456
values.cpp ? { path: values.cpp, generate: generateCppConstants } : undefined,
5557
values.pil ? { path: values.pil, generate: generatePilConstants } : undefined,
5658
values.solidity ? { path: values.solidity, generate: generateSolidityConstants } : undefined,
59+
values.rust ? { path: values.rust, generate: generateRustConstants } : undefined,
5760
].filter((output): output is RequestedOutput => output !== undefined);
5861

5962
if (outputs.length === 0) {

protocol/constants-codegen/src/generator.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
evaluateExpressions,
1212
generateCppConstants,
1313
generatePilConstants,
14+
generateRustConstants,
1415
generateSolidityConstants,
1516
generateTypescriptConstants,
1617
parseNoirFile,
@@ -66,6 +67,21 @@ test('generates the existing PIL subset', () => {
6667
assert.doesNotMatch(output, /ARCHIVE_HEIGHT/);
6768
});
6869

70+
test('generates Rust constants', () => {
71+
const output = generateToString(generateRustConstants);
72+
73+
assert.match(output, /pub const ARCHIVE_HEIGHT: u128 = 30;/);
74+
assert.match(
75+
output,
76+
/pub const MAX_ETH_ADDRESS_VALUE: &str = "0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff";/,
77+
);
78+
assert.match(
79+
output,
80+
/pub const MAX_FIELD_VALUE: &str = "0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000000";/,
81+
);
82+
assert.match(output, /pub const DOM_SEP__MERKLE_HASH: u128 = 2982624097;/);
83+
});
84+
6985
test('generates the existing Solidity subset', () => {
7086
const output = generateToString(generateSolidityConstants);
7187

protocol/constants-codegen/src/generator.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,32 @@ export function processConstantsSolidity(constants: { [key: string]: string }, p
474474
return code.join('\n');
475475
}
476476

477+
/**
478+
* Processes a collection of constants and generates code to export them as Rust constants.
479+
*
480+
* @param constants - An object containing key-value pairs representing constants.
481+
* @param generatorIndices - An object containing key-value pairs representing domain separator indices.
482+
* @returns A string containing code that exports the constants as Rust constants.
483+
*/
484+
export function processConstantsRust(
485+
constants: { [key: string]: string },
486+
generatorIndices: { [key: string]: number },
487+
): string {
488+
const code: string[] = [];
489+
Object.entries(constants).forEach(([key, value]) => {
490+
if (BigInt(value) <= 2n ** 128n - 1n) {
491+
code.push(`pub const ${key}: u128 = ${value};`);
492+
} else {
493+
// Field-sized values exceed u128, so they are emitted as hex strings.
494+
code.push(`pub const ${key}: &str = "0x${BigInt(value).toString(16).padStart(64, '0')}";`);
495+
}
496+
});
497+
Object.entries(generatorIndices).forEach(([key, value]) => {
498+
code.push(`pub const DOM_SEP__${key}: u128 = ${value};`);
499+
});
500+
return code.join('\n');
501+
}
502+
477503
/**
478504
* Generate the constants file in Typescript.
479505
*/
@@ -537,6 +563,17 @@ ${processConstantsSolidity(constants)}
537563
fs.writeFileSync(targetPath, resultSolidity);
538564
}
539565

566+
/**
567+
* Generate the constants file in Rust.
568+
*/
569+
export function generateRustConstants({ constants, domainSeparatorEnum }: ParsedContent, targetPath: string) {
570+
const resultRust: string = `// GENERATED FILE - DO NOT EDIT, RUN yarn remake-constants
571+
${processConstantsRust(constants, domainSeparatorEnum)}
572+
`;
573+
574+
fs.writeFileSync(targetPath, resultRust);
575+
}
576+
540577
/**
541578
* Parse the content of the constants file in Noir.
542579
*/

protocol/constants-codegen/yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ __metadata:
55
version: 8
66
cacheKey: 10c0
77

8-
"@aztec-foundation/constants-codegen@workspace:.":
8+
"@aztec/constants-codegen@workspace:.":
99
version: 0.0.0-use.local
10-
resolution: "@aztec-foundation/constants-codegen@workspace:."
10+
resolution: "@aztec/constants-codegen@workspace:."
1111
dependencies:
1212
"@types/node": "npm:^22"
1313
typescript: "npm:^5.7.0"

0 commit comments

Comments
 (0)