Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: noir protocol circuits - stop committing artifacts #3250

Closed
wants to merge 14 commits into from
1 change: 1 addition & 0 deletions yarn-project/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ COPY . .
RUN yarn workspace @aztec/foundation build && \
yarn workspace @aztec/noir-compiler build && \
yarn workspace @aztec/noir-contracts noir:build:all && \
yarn workspace @aztec/noir-protocol-circuits noir:build:all && \
yarn tsc -b

# Build aztec.js web artifact
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/foundation/src/noir/noir_package_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const noirLocalDependencySchema = z.object({
const noirPackageConfigSchema = z.object({
package: z.object({
name: z.string().default(''),
type: z.enum(['lib', 'contract', 'binary']).default('binary'),
type: z.enum(['lib', 'contract', 'bin']).default('bin'),
entry: z.string().optional(),
description: z.string().optional(),
authors: z.array(z.string()).optional(),
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/noir-compiler/src/cli/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ export function compileContract(program: Command, name = 'contract', log: LogFn
const currentDir = process.cwd();

const compile = compiler === 'wasm' ? compileUsingNoirWasm : compileUsingNargo;
log(`Compiling contracts...`);
log(`Compiling contracts... with ${compiler} backend`);
const result = await compile(projectPath, { log });
log(`result: ${result}`);

for (const contract of result) {
const artifactPath = resolve(projectPath, outdir, `${contract.name}.json`);
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/noir-compiler/src/compile/nargo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { emptyDirSync } from 'fs-extra';
import path from 'path';

import { NoirCommit, NoirTag } from '../index.js';
import { NoirCompilationArtifacts, NoirCompiledContract, NoirDebugMetadata } from '../noir_artifact.js';
import { NoirCompiledContract, NoirContractCompilationArtifacts, NoirDebugMetadata } from '../noir_artifact.js';

/** Compilation options */
export type CompileOpts = {
Expand All @@ -31,7 +31,7 @@ export class NargoContractCompiler {
* Compiles the contracts in projectPath and returns the Aztec.nr artifact.
* @returns Aztec.nr artifact of the compiled contracts.
*/
public compile(): Promise<NoirCompilationArtifacts[]> {
public compile(): Promise<NoirContractCompilationArtifacts[]> {
const stdio = this.opts.quiet ? 'ignore' : 'inherit';
const nargoBin = this.opts.nargoBin ?? 'nargo';
const version = execSync(`${nargoBin} --version`, { cwd: this.projectPath, stdio: 'pipe' }).toString();
Expand All @@ -51,7 +51,7 @@ export class NargoContractCompiler {
}
}

private collectArtifacts(): NoirCompilationArtifacts[] {
private collectArtifacts(): NoirContractCompilationArtifacts[] {
const contractArtifacts = new Map<string, NoirCompiledContract>();
const debugArtifacts = new Map<string, NoirDebugMetadata>();

Expand Down
30 changes: 19 additions & 11 deletions yarn-project/noir-compiler/src/compile/noir/noir-wasm-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { LogFn, createDebugLogger } from '@aztec/foundation/log';
import { CompileError, compile } from '@noir-lang/noir_wasm';
import { isAbsolute } from 'node:path';

import { NoirCompilationArtifacts } from '../../noir_artifact.js';
import {
NoirCompilationArtifacts,
NoirContractCompilationArtifacts,
NoirProgramCompilationArtifacts,
} from '../../noir_artifact.js';
import { NoirDependencyManager } from './dependencies/dependency-manager.js';
import { GithubDependencyResolver as GithubCodeArchiveDependencyResolver } from './dependencies/github-dependency-resolver.js';
import { LocalDependencyResolver } from './dependencies/local-dependency-resolver.js';
Expand All @@ -20,7 +24,7 @@ export type NoirWasmCompileOptions = {
};

/**
* Noir Package Compiler
* Noir Package Contract Compiler
*/
export class NoirWasmContractCompiler {
#log: LogFn;
Expand Down Expand Up @@ -54,9 +58,6 @@ export class NoirWasmContractCompiler {
}

const noirPackage = NoirPackage.open(projectPath, fileManager);
if (noirPackage.getType() !== 'contract') {
throw new Error('This is not a contract project');
}

const dependencyManager = new NoirDependencyManager(
[
Expand All @@ -82,26 +83,33 @@ export class NoirWasmContractCompiler {
/**
* Compiles the project.
*/
public async compile(): Promise<NoirCompilationArtifacts[]> {
public async compile(): Promise<NoirCompilationArtifacts> {
this.#debugLog(`Compiling contract at ${this.#package.getEntryPointPath()}`);
await this.#dependencyManager.resolveDependencies();
this.#debugLog(`Dependencies: ${this.#dependencyManager.getPackageNames().join(', ')}`);

initializeResolver(this.#resolveFile);

try {
const result = compile(this.#package.getEntryPointPath(), true, {
const isContract: boolean = this.#package.getType() === 'contract';
const result = compile(this.#package.getEntryPointPath(), isContract, {
/* eslint-disable camelcase */
root_dependencies: this.#dependencyManager.getEntrypointDependencies(),
library_dependencies: this.#dependencyManager.getLibraryDependencies(),
/* eslint-enable camelcase */
});

if (!('contract' in result)) {
throw new Error('No contract found in compilation result');
if (isContract) {
if (!('contract' in result)) {
throw new Error('No contract found in compilation result');
}
return result as NoirContractCompilationArtifacts;
} else {
if (!('program' in result)) {
throw new Error('No program found in compilation result');
}
return result as NoirProgramCompilationArtifacts;
}

return [result];
} catch (err) {
if (err instanceof Error && err.name === 'CompileError') {
this.#processCompileError(err as CompileError);
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/noir-compiler/src/compile/noir/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class NoirPackage {
entrypoint = 'lib.nr';
break;
case 'contract':
case 'binary':
case 'bin':
entrypoint = 'main.nr';
break;
default:
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/noir-compiler/src/contract-interface-gen/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ContractArtifact, DebugMetadata, FunctionArtifact, FunctionType } from
import { deflate } from 'pako';

import { mockVerificationKey } from '../mocked_keys.js';
import { NoirCompilationArtifacts, NoirFunctionEntry } from '../noir_artifact.js';
import { NoirContractCompilationArtifacts, NoirFunctionEntry } from '../noir_artifact.js';

/**
* Generates a function build artifact. Replaces verification key with a mock value.
Expand Down Expand Up @@ -38,7 +38,7 @@ function generateFunctionArtifact(fn: NoirFunctionEntry): FunctionArtifact {
* @returns Aztec contract build artifact.
*/
export function generateContractArtifact(
{ contract, debug }: NoirCompilationArtifacts,
{ contract, debug }: NoirContractCompilationArtifacts,
aztecNrVersion?: string,
): ContractArtifact {
const originalFunctions = contract.functions;
Expand Down
11 changes: 8 additions & 3 deletions yarn-project/noir-compiler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ export async function compileUsingNoirWasm(
const cacheRoot = process.env.XDG_CACHE_HOME ?? join(process.env.HOME ?? '', '.cache');
const fileManager = new FileManager(fs, join(cacheRoot, 'aztec-noir-compiler'));
const compiler = NoirWasmContractCompiler.new(fileManager, resolve(projectPath), opts);
const artifacts = await compiler.compile();
const artifact = await compiler.compile();
const resolvedAztecNrVersion = compiler.getResolvedAztecNrVersion();

if (resolvedAztecNrVersion && AztecNrVersion !== resolvedAztecNrVersion) {
opts.log(`WARNING: Aztec.nr version mismatch: expected "${AztecNrVersion}", got "${resolvedAztecNrVersion}"`);
}

return artifacts.map(artifact => generateContractArtifact(artifact, resolvedAztecNrVersion));
if ('program' in artifact) {
return [];
} else if ('contract' in artifact) {
return [generateContractArtifact(artifact, resolvedAztecNrVersion)];
} else {
throw Error('invalid nargo package type!');
}
}
25 changes: 24 additions & 1 deletion yarn-project/noir-compiler/src/noir_artifact.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ABIParameter, ABIType, DebugFileMap, DebugInfo, EventAbi } from '@aztec/foundation/abi';

import { CompiledProgram } from '@noir-lang/noir_wasm';

/** The Aztec.nr function types. */
type NoirFunctionType = 'Open' | 'Secret' | 'Unconstrained';

Expand Down Expand Up @@ -82,13 +84,34 @@ export interface NoirDebugMetadata {
/**
* The compilation artifacts of a given contract.
*/
export interface NoirCompilationArtifacts {
export interface NoirContractCompilationArtifacts {
/**
* The compiled contract.
*/
contract: NoirCompiledContract;

/**
* The artifact that contains the debug metadata about the contract.
*/
debug?: NoirDebugMetadata;
}

/**
* The compilation artifacts of a given program.
*/
export interface NoirProgramCompilationArtifacts {
/**
* The compiled contract.
*/
program: CompiledProgram;

/**
* The artifact that contains the debug metadata about the contract.
*/
debug?: NoirDebugMetadata;
}

/**
* union of two output types above
*/
export type NoirCompilationArtifacts = NoirProgramCompilationArtifacts | NoirContractCompilationArtifacts;
6 changes: 5 additions & 1 deletion yarn-project/noir-protocol-circuits/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
artifacts/
target/
proofs/
src/target/
src/types/*
Prover.toml
Verifier.toml
Verifier.toml
2 changes: 2 additions & 0 deletions yarn-project/noir-protocol-circuits/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"formatting": "run -T prettier --check ./src && run -T eslint ./src",
"formatting:fix": "run -T prettier -w ./src",
"noir:build": "cd src && nargo compile --no-backend && rm -rf ./target/debug_*",
"noir:build:all": "yarn noir:clean && ./src/scripts/compile_all.sh && yarn noir:types",
"noir:clean": "rm -rf ./src/artifacts ./src/types ./src/target/",
"noir:types": "yarn ts-node --esm src/scripts/generate_ts_from_abi.ts",
"noir:test": "cd src && nargo test",
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --passWithNoTests"
Expand Down
25 changes: 25 additions & 0 deletions yarn-project/noir-protocol-circuits/src/scripts/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

set -euo pipefail;

# Compiles Aztec.nr contracts in parallel, bubbling any compilation errors
# uses the aztec-cli instead of nargo so that we compile with noir_wasm

export self_dir=$(dirname "$(realpath $0)")
echo "self_dir: $self_dir"
export COMPILER="$self_dir/../../../noir-compiler/dest/cli.js"

build() {
CONTRACT_NAME=$1
CONTRACT_FOLDER="$self_dir/../crates/${CONTRACT_NAME}"
echo "Compiling $CONTRACT_NAME..."
rm -rf ${CONTRACT_FOLDER}/target

echo $(pwd)
node "$COMPILER" contract "$CONTRACT_FOLDER" --typescript src/types
}

export -f build

# run 4 builds at a time
echo "$@" | xargs -n 1 -P 4 bash -c 'build "$0"'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
# meant to be run from the root of the subpackage
echo "Compiling all contracts"
./src/scripts/compile.sh $(./src/scripts/get_all_contracts.sh)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
# Utility to get the names of all contracts
echo $(ls -d src/crates/*/Nargo.toml | sed -r "s/src\\/crates\\/(.+)\\/Nargo.toml/\\1/")

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading