@aztec/aztec 0.0.1-commit.7ac86ea28 → 0.0.1-commit.7b86788

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.
Files changed (50) hide show
  1. package/dest/bin/index.js +2 -0
  2. package/dest/cli/admin_api_key_store.d.ts +3 -3
  3. package/dest/cli/admin_api_key_store.d.ts.map +1 -1
  4. package/dest/cli/admin_api_key_store.js +3 -3
  5. package/dest/cli/aztec_start_action.js +2 -2
  6. package/dest/cli/aztec_start_options.d.ts +1 -1
  7. package/dest/cli/aztec_start_options.d.ts.map +1 -1
  8. package/dest/cli/aztec_start_options.js +4 -3
  9. package/dest/cli/cmds/compile.d.ts +1 -1
  10. package/dest/cli/cmds/compile.d.ts.map +1 -1
  11. package/dest/cli/cmds/compile.js +100 -35
  12. package/dest/cli/cmds/profile.d.ts +4 -0
  13. package/dest/cli/cmds/profile.d.ts.map +1 -0
  14. package/dest/cli/cmds/profile.js +8 -0
  15. package/dest/cli/cmds/profile_flamegraph.d.ts +4 -0
  16. package/dest/cli/cmds/profile_flamegraph.d.ts.map +1 -0
  17. package/dest/cli/cmds/profile_flamegraph.js +51 -0
  18. package/dest/cli/cmds/profile_gates.d.ts +4 -0
  19. package/dest/cli/cmds/profile_gates.d.ts.map +1 -0
  20. package/dest/cli/cmds/profile_gates.js +57 -0
  21. package/dest/cli/cmds/profile_utils.d.ts +18 -0
  22. package/dest/cli/cmds/profile_utils.d.ts.map +1 -0
  23. package/dest/cli/cmds/profile_utils.js +50 -0
  24. package/dest/cli/cmds/utils/artifacts.d.ts +21 -0
  25. package/dest/cli/cmds/utils/artifacts.d.ts.map +1 -0
  26. package/dest/cli/cmds/utils/artifacts.js +24 -0
  27. package/dest/cli/cmds/utils/spawn.d.ts +3 -0
  28. package/dest/cli/cmds/utils/spawn.d.ts.map +1 -0
  29. package/dest/cli/cmds/utils/spawn.js +16 -0
  30. package/dest/cli/util.js +3 -3
  31. package/package.json +34 -34
  32. package/scripts/aztec.sh +6 -2
  33. package/scripts/init.sh +23 -13
  34. package/scripts/new.sh +17 -16
  35. package/scripts/setup_workspace.sh +124 -0
  36. package/src/bin/index.ts +2 -0
  37. package/src/cli/admin_api_key_store.ts +4 -4
  38. package/src/cli/aztec_start_action.ts +2 -2
  39. package/src/cli/aztec_start_options.ts +4 -3
  40. package/src/cli/cmds/compile.ts +113 -36
  41. package/src/cli/cmds/profile.ts +25 -0
  42. package/src/cli/cmds/profile_flamegraph.ts +63 -0
  43. package/src/cli/cmds/profile_gates.ts +67 -0
  44. package/src/cli/cmds/profile_utils.ts +58 -0
  45. package/src/cli/cmds/utils/artifacts.ts +44 -0
  46. package/src/cli/cmds/utils/spawn.ts +16 -0
  47. package/src/cli/util.ts +2 -2
  48. package/scripts/extract_function.js +0 -47
  49. package/scripts/flamegraph.sh +0 -59
  50. package/scripts/setup_project.sh +0 -31
@@ -0,0 +1,63 @@
1
+ import type { LogFn } from '@aztec/foundation/log';
2
+
3
+ import { readFile, rename, rm, writeFile } from 'fs/promises';
4
+ import { basename, dirname, join } from 'path';
5
+
6
+ import { makeFunctionArtifact } from './profile_utils.js';
7
+ import type { CompiledArtifact } from './utils/artifacts.js';
8
+ import { run } from './utils/spawn.js';
9
+
10
+ /** Generates a gate count flamegraph SVG for a single contract function. */
11
+ export async function profileFlamegraph(artifactPath: string, functionName: string, log: LogFn): Promise<void> {
12
+ const raw = await readFile(artifactPath, 'utf-8');
13
+ const artifact: CompiledArtifact = JSON.parse(raw);
14
+
15
+ if (!Array.isArray(artifact.functions)) {
16
+ throw new Error(`${artifactPath} does not appear to be a contract artifact (no functions array)`);
17
+ }
18
+
19
+ const func = artifact.functions.find(f => f.name === functionName);
20
+ if (!func) {
21
+ const available = artifact.functions.map(f => f.name).join(', ');
22
+ throw new Error(`Function "${functionName}" not found in artifact. Available: ${available}`);
23
+ }
24
+ if (func.is_unconstrained) {
25
+ throw new Error(`Function "${functionName}" is unconstrained and cannot be profiled`);
26
+ }
27
+
28
+ const outputDir = dirname(artifactPath);
29
+ const contractName = basename(artifactPath, '.json');
30
+ const functionArtifact = join(outputDir, `${contractName}-${functionName}.json`);
31
+
32
+ try {
33
+ await writeFile(functionArtifact, makeFunctionArtifact(artifact, func));
34
+
35
+ const profiler = process.env.PROFILER_PATH ?? 'noir-profiler';
36
+ const bb = process.env.BB ?? 'bb';
37
+
38
+ await run(profiler, [
39
+ 'gates',
40
+ '--artifact-path',
41
+ functionArtifact,
42
+ '--backend-path',
43
+ bb,
44
+ '--backend-gates-command',
45
+ 'gates',
46
+ '--output',
47
+ outputDir,
48
+ '--scheme',
49
+ 'chonk',
50
+ '--include_gates_per_opcode',
51
+ ]);
52
+
53
+ // noir-profiler names the SVG using the internal function name which
54
+ // retains the __aztec_nr_internals__ prefix in the bytecode metadata.
55
+ const srcSvg = join(outputDir, `__aztec_nr_internals__${functionName}_gates.svg`);
56
+ const destSvg = join(outputDir, `${contractName}-${functionName}-flamegraph.svg`);
57
+ await rename(srcSvg, destSvg);
58
+
59
+ log(`Flamegraph written to ${destSvg}`);
60
+ } finally {
61
+ await rm(functionArtifact, { force: true });
62
+ }
63
+ }
@@ -0,0 +1,67 @@
1
+ import { asyncPool } from '@aztec/foundation/async-pool';
2
+ import type { LogFn } from '@aztec/foundation/log';
3
+
4
+ import { execFile as execFileCb } from 'child_process';
5
+ import { rm } from 'fs/promises';
6
+ import { promisify } from 'util';
7
+
8
+ import { MAX_CONCURRENT, discoverArtifacts } from './profile_utils.js';
9
+
10
+ const execFile = promisify(execFileCb);
11
+
12
+ interface GateCountResult {
13
+ name: string;
14
+ gateCount: number;
15
+ }
16
+
17
+ /** Parses circuit_size from bb gates JSON output: { "functions": [{ "circuit_size": N }] } */
18
+ function parseGateCount(stdout: string): number {
19
+ const parsed = JSON.parse(stdout);
20
+ const size = parsed?.functions?.[0]?.circuit_size;
21
+ if (typeof size !== 'number') {
22
+ throw new Error('Failed to parse circuit_size from bb gates output');
23
+ }
24
+ return size;
25
+ }
26
+
27
+ /** Runs bb gates on a single artifact file and returns the gate count. */
28
+ async function getGateCount(bb: string, artifactPath: string): Promise<number> {
29
+ const { stdout } = await execFile(bb, ['gates', '--scheme', 'chonk', '-b', artifactPath]);
30
+ return parseGateCount(stdout);
31
+ }
32
+
33
+ /** Profiles all compiled artifacts in a target directory and prints gate counts. */
34
+ export async function profileGates(targetDir: string, log: LogFn): Promise<void> {
35
+ const bb = process.env.BB ?? 'bb';
36
+ const { artifacts, tmpDir } = await discoverArtifacts(targetDir);
37
+
38
+ if (artifacts.length === 0) {
39
+ log('No artifacts found in target directory.');
40
+ return;
41
+ }
42
+
43
+ try {
44
+ const results: GateCountResult[] = await asyncPool(MAX_CONCURRENT, artifacts, async artifact => ({
45
+ name: artifact.name,
46
+ gateCount: await getGateCount(bb, artifact.filePath),
47
+ }));
48
+ results.sort((a, b) => a.name.localeCompare(b.name));
49
+
50
+ if (results.length === 0) {
51
+ log('No constrained circuits found.');
52
+ return;
53
+ }
54
+
55
+ const maxNameLen = Math.max(...results.map(r => r.name.length));
56
+ log('');
57
+ log('Gate counts:');
58
+ log('-'.repeat(maxNameLen + 16));
59
+ for (const { name, gateCount } of results) {
60
+ log(`${name.padEnd(maxNameLen)} ${gateCount.toLocaleString().padStart(12)}`);
61
+ }
62
+ log('-'.repeat(maxNameLen + 16));
63
+ log(`Total: ${results.length} circuit(s)`);
64
+ } finally {
65
+ await rm(tmpDir, { recursive: true, force: true });
66
+ }
67
+ }
@@ -0,0 +1,58 @@
1
+ import { mkdtemp, writeFile } from 'fs/promises';
2
+ import { tmpdir } from 'os';
3
+ import { join } from 'path';
4
+
5
+ import type { CompiledArtifact, ContractFunction } from './utils/artifacts.js';
6
+ import { readArtifactFiles } from './utils/artifacts.js';
7
+
8
+ export const MAX_CONCURRENT = 4;
9
+
10
+ export interface DiscoveredArtifact {
11
+ name: string;
12
+ filePath: string;
13
+ type: 'contract-function' | 'program';
14
+ }
15
+
16
+ /**
17
+ * Reads a target directory and returns a list of discovered artifacts with temp files
18
+ * created for contract functions. Caller must clean up tmpDir when done.
19
+ */
20
+ export async function discoverArtifacts(
21
+ targetDir: string,
22
+ ): Promise<{ artifacts: DiscoveredArtifact[]; tmpDir: string }> {
23
+ const files = await readArtifactFiles(targetDir);
24
+ const tmpDir = await mkdtemp(join(tmpdir(), 'aztec-profile-'));
25
+ const artifacts: DiscoveredArtifact[] = [];
26
+
27
+ for (const file of files) {
28
+ if (Array.isArray(file.content.functions)) {
29
+ for (const func of file.content.functions) {
30
+ if (!func.bytecode || func.is_unconstrained) {
31
+ continue;
32
+ }
33
+ const name = `${file.name}::${func.name}`;
34
+ const tmpPath = join(tmpDir, `${file.name}-${func.name}.json`);
35
+ await writeFile(tmpPath, makeFunctionArtifact(file.content, func));
36
+ artifacts.push({ name, filePath: tmpPath, type: 'contract-function' });
37
+ }
38
+ } else if (file.content.bytecode) {
39
+ artifacts.push({ name: file.name, filePath: file.filePath, type: 'program' });
40
+ }
41
+ }
42
+
43
+ return { artifacts, tmpDir };
44
+ }
45
+
46
+ /** Extracts a contract function as a standalone program artifact JSON string. */
47
+ export function makeFunctionArtifact(artifact: CompiledArtifact, func: ContractFunction) {
48
+ /* eslint-disable camelcase */
49
+ return JSON.stringify({
50
+ noir_version: artifact.noir_version,
51
+ hash: 0,
52
+ abi: func.abi,
53
+ bytecode: func.bytecode,
54
+ debug_symbols: func.debug_symbols,
55
+ file_map: artifact.file_map,
56
+ });
57
+ /* eslint-enable camelcase */
58
+ }
@@ -0,0 +1,44 @@
1
+ import { readFile, readdir } from 'fs/promises';
2
+ import { join } from 'path';
3
+
4
+ export interface CompiledArtifact {
5
+ noir_version: string;
6
+ file_map: unknown;
7
+ functions: ContractFunction[];
8
+ bytecode?: string;
9
+ }
10
+
11
+ export interface ContractFunction {
12
+ name: string;
13
+ abi: unknown;
14
+ bytecode: string;
15
+ debug_symbols: unknown;
16
+ is_unconstrained?: boolean;
17
+ }
18
+
19
+ export interface ArtifactFile {
20
+ name: string;
21
+ filePath: string;
22
+ content: CompiledArtifact;
23
+ }
24
+
25
+ /** Reads all JSON artifact files from a target directory and returns their parsed contents. */
26
+ export async function readArtifactFiles(targetDir: string): Promise<ArtifactFile[]> {
27
+ let entries: string[];
28
+ try {
29
+ entries = (await readdir(targetDir)).filter(f => f.endsWith('.json'));
30
+ } catch (err: any) {
31
+ if (err?.code === 'ENOENT') {
32
+ throw new Error(`Target directory '${targetDir}' does not exist. Compile first with 'aztec compile'.`);
33
+ }
34
+ throw err;
35
+ }
36
+
37
+ const artifacts: ArtifactFile[] = [];
38
+ for (const file of entries) {
39
+ const filePath = join(targetDir, file);
40
+ const content = JSON.parse(await readFile(filePath, 'utf-8')) as CompiledArtifact;
41
+ artifacts.push({ name: file.replace('.json', ''), filePath, content });
42
+ }
43
+ return artifacts;
44
+ }
@@ -0,0 +1,16 @@
1
+ import { spawn } from 'child_process';
2
+
3
+ /** Spawns a command with inherited stdio and rejects on non-zero exit. */
4
+ export function run(cmd: string, args: string[]): Promise<void> {
5
+ return new Promise((resolve, reject) => {
6
+ const child = spawn(cmd, args, { stdio: 'inherit' });
7
+ child.on('error', reject);
8
+ child.on('close', code => {
9
+ if (code !== 0) {
10
+ reject(new Error(`${cmd} exited with code ${code}`));
11
+ } else {
12
+ resolve();
13
+ }
14
+ });
15
+ });
16
+ }
package/src/cli/util.ts CHANGED
@@ -271,7 +271,7 @@ export async function preloadCrsDataForVerifying(
271
271
  ): Promise<void> {
272
272
  if (realProofs) {
273
273
  const { Crs, GrumpkinCrs } = await import('@aztec/bb.js');
274
- await Promise.all([Crs.new(2 ** 1, undefined, log), GrumpkinCrs.new(2 ** 16 + 1, undefined, log)]);
274
+ await Promise.all([Crs.new(2 ** 1, undefined, log), GrumpkinCrs.new(2 ** 16, undefined, log)]);
275
275
  }
276
276
  }
277
277
 
@@ -286,7 +286,7 @@ export async function preloadCrsDataForServerSideProving(
286
286
  ): Promise<void> {
287
287
  if (realProofs) {
288
288
  const { Crs, GrumpkinCrs } = await import('@aztec/bb.js');
289
- await Promise.all([Crs.new(2 ** 25 + 1, undefined, log), GrumpkinCrs.new(2 ** 18 + 1, undefined, log)]);
289
+ await Promise.all([Crs.new(2 ** 25, undefined, log), GrumpkinCrs.new(2 ** 18, undefined, log)]);
290
290
  }
291
291
  }
292
292
 
@@ -1,47 +0,0 @@
1
- #!/usr/bin/env node
2
- import fs from 'fs/promises';
3
- import path from 'path';
4
-
5
- // Simple script to extract a contract function as a separate Noir artifact.
6
- // We need to use this since the transpiling that we do on public functions make the contract artifacts
7
- // unreadable by noir tooling, since they are no longer following the noir artifact format.
8
- async function main() {
9
- let [contractArtifactPath, functionName] = process.argv.slice(2);
10
- if (!contractArtifactPath || !functionName) {
11
- console.log('Usage: node extractFunctionAsNoirArtifact.js <contractArtifactPath> <functionName>');
12
- return;
13
- }
14
-
15
- const contractArtifact = JSON.parse(await fs.readFile(contractArtifactPath, 'utf8'));
16
- const func = contractArtifact.functions.find(f => f.name === functionName);
17
- if (!func) {
18
- console.error(`Function ${functionName} not found in ${contractArtifactPath}`);
19
- return;
20
- }
21
-
22
- const artifact = {
23
- noir_version: contractArtifact.noir_version,
24
- hash: 0,
25
- abi: func.abi,
26
- bytecode: func.bytecode,
27
- debug_symbols: func.debug_symbols,
28
- file_map: contractArtifact.file_map,
29
- expression_width: {
30
- Bounded: {
31
- width: 4,
32
- },
33
- },
34
- };
35
-
36
- const outputDir = path.dirname(contractArtifactPath);
37
- const outputName = path.basename(contractArtifactPath, '.json') + `-${functionName}.json`;
38
-
39
- const outPath = path.join(outputDir, outputName);
40
-
41
- await fs.writeFile(outPath, JSON.stringify(artifact, null, 2));
42
- }
43
-
44
- main().catch(err => {
45
- console.error(err);
46
- process.exit(1);
47
- });
@@ -1,59 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -eu
3
-
4
- # If first arg is -h or --help, print usage.
5
- if [ $# -lt 2 ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
6
- cat << 'EOF'
7
- Aztec Flamegraph - Generate a gate count flamegraph for an aztec contract function.
8
-
9
- Usage: aztec flamegraph <contract_artifact> <function>
10
-
11
- Options:
12
- -h, --help Print help
13
-
14
- Will output an svg at <artifact_path>/<contract>-<function>-flamegraph.svg.
15
- You can open it in your browser to view it.
16
-
17
- EOF
18
- exit 0
19
- fi
20
-
21
- cleanup() {
22
- set +e
23
- if [ -f "$function_artifact" ]; then
24
- rm -f "$function_artifact"
25
- fi
26
- }
27
-
28
- trap cleanup EXIT
29
-
30
- # Get the directory of the script
31
- script_dir=$(realpath $(dirname $0))
32
-
33
- PROFILER=${PROFILER_PATH:-noir-profiler}
34
- BB=${BB:-bb}
35
-
36
- # first console arg is contract name in camel case or path to contract artifact
37
- contract=$1
38
-
39
- # second console arg is the contract function
40
- function=$2
41
-
42
- if [ ! -f "$contract" ]; then
43
- echo "Error: Contract artifact not found at: $contract"
44
- exit 1
45
- fi
46
- artifact_path=$contract
47
- function_artifact="${artifact_path%%.json}-${function}.json"
48
- output_dir=$(dirname "$artifact_path")
49
-
50
- # Extract artifact for the specific function.
51
- node $script_dir/extract_function.js "$artifact_path" $function
52
-
53
- # Generate the flamegraph
54
- $PROFILER gates --artifact-path "$function_artifact" --backend-path "$BB" --backend-gates-command "gates" --output "$output_dir" --scheme chonk --include_gates_per_opcode
55
-
56
- # Save as $artifact_name-$function-flamegraph.svg
57
- output_file="${function_artifact%%.json}-flamegraph.svg"
58
- mv "$output_dir/__aztec_nr_internals__${function}_gates.svg" "$output_file"
59
- echo "Flamegraph generated at: $output_file"
@@ -1,31 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
-
4
- # Get the actual aztec version for the git tag.
5
- AZTEC_VERSION=$(jq -r '.version' $(dirname $0)/../package.json)
6
- NARGO_TOML_PATH="Nargo.toml"
7
- MAIN_NR_PATH="src/main.nr"
8
-
9
- if [ ! -f "$NARGO_TOML_PATH" ]; then
10
- >&2 echo "Warning: Could not find Nargo.toml at $NARGO_TOML_PATH to add aztec dependency"
11
- exit 1
12
- fi
13
-
14
- if [ ! -f "$MAIN_NR_PATH" ]; then
15
- >&2 echo "Warning: Could not find main.nr at $MAIN_NR_PATH"
16
- exit 1
17
- fi
18
-
19
- # Add aztec dependency to Nargo.toml
20
- echo "" >> "$NARGO_TOML_PATH"
21
- echo "aztec = { git=\"https://github.com/AztecProtocol/aztec-nr\", tag=\"v${AZTEC_VERSION}\", directory=\"aztec\" }" >> "$NARGO_TOML_PATH"
22
- echo "Added aztec dependency (v${AZTEC_VERSION}) to Nargo.toml"
23
-
24
- # Replace the contents of main.nr with the Aztec contract template
25
- cat > "$MAIN_NR_PATH" << 'EOF'
26
- use aztec::macros::aztec;
27
-
28
- #[aztec]
29
- contract Main {}
30
- EOF
31
- echo "Created main.nr with Aztec contract template"