Skip to content

Commit

Permalink
feat(compiler): adds Aztec noir compiler (#438)
Browse files Browse the repository at this point in the history
* wip: added noir wasm to compile in typescript

* feat(compiler): first compiler version

* chore: added prettierignore

* docs: added tsdoc

* chore(compiler): fixed the executable

* chore(compiler): add to CI

* docs(contracts): added docs for script

* fix: fixed foundation abi import

* chore: fix path to module

* chore: update noir_wasm

* chore: updated packages with yarn prepare

* fix: update abi import

* docs: added jsdoc

* refactor: address some pr comments
  • Loading branch information
sirasistant authored May 5, 2023
1 parent 2ceba53 commit 7542d85
Show file tree
Hide file tree
Showing 62 changed files with 560 additions and 168 deletions.
12 changes: 12 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,17 @@ jobs:
name: "Build and test"
command: build noir-contracts

noir-compiler:
machine:
image: ubuntu-2004:202010-01
resource_class: large
steps:
- *checkout
- *setup_env
- run:
name: "Build and test"
command: build noir-compiler

world-state:
machine:
image: ubuntu-2004:202010-01
Expand Down Expand Up @@ -456,6 +467,7 @@ workflows:
- merkle-tree: *yarn_project
- p2p: *yarn_project
- noir-contracts: *yarn_project
- noir-compiler: *yarn_project
- sequencer-client: *yarn_project
- types: *yarn_project
- circuits-js: *yarn_project
Expand Down
11 changes: 11 additions & 0 deletions build_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,17 @@
"rebuildPatterns": ["^yarn-project/noir-contracts/"],
"dependencies": ["foundation"]
},
"noir-compiler": {
"buildDir": "yarn-project",
"projectDir": "yarn-project/noir-compiler",
"dockerfile": "noir-compiler/Dockerfile",
"rebuildPatterns": [
"^yarn-project/noir-compiler/"
],
"dependencies": [
"foundation"
]
},
"p2p": {
"buildDir": "yarn-project",
"projectDir": "yarn-project/p2p",
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/acir-simulator/src/abi_coder/decoder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fr } from '@aztec/foundation/fields';
import { ABIType, FunctionAbi } from '@aztec/noir-contracts';
import { ABIType, FunctionAbi } from '@aztec/foundation/abi';

/**
* Decodes return values from a function call.
Expand Down
3 changes: 1 addition & 2 deletions yarn-project/acir-simulator/src/abi_coder/encoder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ARGS_LENGTH } from '@aztec/circuits.js';
import { Fr } from '@aztec/foundation/fields';

import { ABIType, FunctionAbi } from '@aztec/noir-contracts';
import { ABIType, FunctionAbi } from '@aztec/foundation/abi';

/**
* Encodes arguments for a function call.
Expand Down
7 changes: 6 additions & 1 deletion yarn-project/acir-simulator/src/abi_coder/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { ABIType } from '@aztec/noir-contracts';
import { ABIType } from '@aztec/foundation/abi';

export * from './encoder.js';
export * from './decoder.js';

/**
* Get the size of an ABI type in field elements.
* @param type - The ABI type.
* @returns The size of the type in field elements.
*/
export function sizeOfType(type: ABIType): number {
switch (type.kind) {
case 'field':
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/acir-simulator/src/client/db_oracle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
import { FunctionAbi } from '@aztec/noir-contracts';
import { FunctionAbi } from '@aztec/foundation/abi';

/**
* The format that noir contracts use to get notes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '../acvm/index.js';
import { CallContext, PrivateCallStackItem, FunctionData } from '@aztec/circuits.js';
import { extractPublicInputs, frToAztecAddress, frToSelector } from '../acvm/deserialize.js';
import { FunctionAbi } from '@aztec/noir-contracts';
import { FunctionAbi } from '@aztec/foundation/abi';
import { createDebugLogger } from '@aztec/foundation/log';
import { decodeReturnValues } from '../abi_coder/decoder.js';
import { ClientTxExecutionContext } from './client_execution_context.js';
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/acir-simulator/src/client/simulator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CallContext, PrivateHistoricTreeRoots, TxRequest } from '@aztec/circuits.js';
import { FunctionAbi, FunctionType } from '@aztec/noir-contracts';
import { FunctionAbi, FunctionType } from '@aztec/foundation/abi';
import { DBOracle } from './db_oracle.js';
import { PrivateFunctionExecution, ExecutionResult } from './private_execution.js';
import { BarretenbergWasm } from '@aztec/barretenberg.js/wasm';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ACVMField, acvm, fromACVMField, toACVMField, toACVMWitness } from '../acvm/index.js';
import { CallContext, FunctionData } from '@aztec/circuits.js';
import { frToAztecAddress, frToNumber } from '../acvm/deserialize.js';
import { FunctionAbi } from '@aztec/noir-contracts';
import { FunctionAbi } from '@aztec/foundation/abi';
import { createDebugLogger } from '@aztec/foundation/log';
import { decodeReturnValues } from '../abi_coder/decoder.js';
import { ClientTxExecutionContext } from './client_execution_context.js';
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/acir-simulator/src/public/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CallContext, FunctionData } from '@aztec/circuits.js';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
import { FunctionAbi } from '@aztec/noir-contracts';
import { FunctionAbi } from '@aztec/foundation/abi';
import { ChildAbi, ParentAbi, PublicTokenContractAbi } from '@aztec/noir-contracts/examples';
import { MockProxy, mock } from 'jest-mock-extended';
import { default as memdown, type MemDown } from 'memdown';
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-rpc/src/abi_coder/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { keccak } from '@aztec/foundation/crypto';
import { ABIParameter } from '@aztec/noir-contracts';
import { ABIParameter } from '@aztec/foundation/abi';

/**
* Generate a function signature string for a given function name and parameters.
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-rpc/src/account_state/account_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr, Point } from '@aztec/foundation/fields';
import { createDebugLogger } from '@aztec/foundation/log';
import { ConstantKeyPair, KeyPair } from '@aztec/key-store';
import { FunctionType } from '@aztec/noir-contracts';
import { FunctionType } from '@aztec/foundation/abi';
import {
EncodedContractFunction,
INITIAL_L2_BLOCK_NUM,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AztecAddress, EthAddress, Fr, TxRequest, EcdsaSignature } from '@aztec/circuits.js';
import { Tx, TxHash } from '@aztec/types';
import { ContractAbi } from '@aztec/noir-contracts';
import { ContractAbi } from '@aztec/foundation/abi';
import { TxReceipt } from '../tx/index.js';
import { Point } from '@aztec/foundation/fields';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { Fr, Point } from '@aztec/foundation/fields';
import { createDebugLogger } from '@aztec/foundation/log';
import { KeyStore } from '@aztec/key-store';
import { ContractAbi, FunctionType } from '@aztec/noir-contracts';
import { ContractAbi, FunctionType } from '@aztec/foundation/abi';
import { Tx, TxHash } from '@aztec/types';
import { AztecRPCClient, DeployedContract } from '../aztec_rpc_client/index.js';
import { toContractDao } from '../contract_database/index.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ContractAbi, FunctionAbi } from '@aztec/noir-contracts';
import { generateFunctionSelector } from '../abi_coder/index.js';
import { ContractAbi, FunctionAbi } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-rpc/src/contract_tree/contract_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import {
hashVK,
} from '@aztec/circuits.js/abis';
import { CircuitsWasm } from '@aztec/circuits.js/wasm';
import { ContractAbi, FunctionType } from '@aztec/noir-contracts';
import { generateFunctionSelector } from '../abi_coder/index.js';
import { ContractDao, ContractFunctionDao } from '../contract_database/index.js';
import { computeFunctionTreeData } from './function_tree_data.js';
import { ContractAbi, FunctionType } from '@aztec/foundation/abi';
import { Fr } from '@aztec/foundation/fields';
import { keccak } from '@aztec/foundation/crypto';
import { EthAddress } from '@aztec/foundation/eth-address';
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-rpc/src/simulator_oracle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DBOracle } from '@aztec/acir-simulator';
import { AztecNode } from '@aztec/aztec-node';
import { AztecAddress, EthAddress, Fr } from '@aztec/circuits.js';
import { KeyPair } from '@aztec/key-store';
import { FunctionAbi } from '@aztec/noir-contracts';
import { FunctionAbi } from '@aztec/foundation/abi';
import { ContractDataOracle } from '../contract_data_oracle/index.js';
import { Database } from '../database/index.js';

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/contract/checker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ABIType, BasicType, ContractAbi, StructType } from '@aztec/noir-contracts';
import { ABIType, BasicType, ContractAbi, StructType } from '@aztec/foundation/abi';

/**
* Represents a type derived from input type T with the 'kind' property removed.
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/contract/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
TxReceipt,
TxRequest,
} from '@aztec/aztec-rpc';
import { ABIParameterVisibility, ContractAbi, FunctionType } from '@aztec/noir-contracts';
import { mock } from 'jest-mock-extended';

import { EcdsaSignature } from '@aztec/circuits.js';
import { Contract } from './contract.js';
import { ABIParameterVisibility, ContractAbi, FunctionType } from '@aztec/foundation/abi';
import { randomBytes } from '@aztec/foundation/crypto';

describe('Contract Class', () => {
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/contract/contract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AztecRPCClient, DeployedContract, generateFunctionSelector } from '@aztec/aztec-rpc';
import { ContractAbi, FunctionAbi } from '@aztec/noir-contracts';
import { ContractAbi, FunctionAbi } from '@aztec/foundation/abi';
import { ContractFunctionInteraction } from './contract_function_interaction.js';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AztecRPCClient, Tx, TxHash, TxRequest } from '@aztec/aztec-rpc';
import { AztecAddress, EcdsaSignature, Fr } from '@aztec/circuits.js';
import { FunctionType } from '@aztec/noir-contracts';
import { FunctionType } from '@aztec/foundation/abi';
import { SentTx } from './sent_tx.js';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AztecRPCClient, Tx, TxHash, TxReceipt, TxRequest } from '@aztec/aztec-rpc';
import { AztecAddress, EcdsaSignature, EthAddress, Fr } from '@aztec/circuits.js';
import { ContractAbi, FunctionType } from '@aztec/noir-contracts';
import { ContractAbi, FunctionType } from '@aztec/foundation/abi';
import { randomBytes } from 'crypto';
import { mock } from 'jest-mock-extended';
import { ContractDeployer } from './contract_deployer.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AztecRPCClient } from '@aztec/aztec-rpc';
import { ContractAbi } from '@aztec/noir-contracts';
import { ContractAbi } from '@aztec/foundation/abi';
import { DeployMethod } from './deploy_method.js';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AztecRPCClient } from '@aztec/aztec-rpc';
import { ContractAbi, FunctionType } from '@aztec/noir-contracts';
import { ContractAbi, FunctionType } from '@aztec/foundation/abi';
import { ContractFunctionInteraction, SendMethodOptions } from '../contract/index.js';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
Expand Down
3 changes: 1 addition & 2 deletions yarn-project/end-to-end/src/e2e_block_building.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { AztecRPCServer, ContractDeployer, Fr, TxStatus } from '@aztec/aztec.js'
import { mnemonicToAccount } from 'viem/accounts';
import { createAztecRpcServer } from './create_aztec_rpc_client.js';
import { deployL1Contracts } from './deploy_l1_contracts.js';
import { ContractAbi } from '@aztec/noir-contracts';
import { TestContractAbi } from '@aztec/noir-contracts/examples';
import times from 'lodash.times';
import { createDebugLogger } from '@aztec/foundation/log';
Expand All @@ -18,7 +17,7 @@ describe('e2e_block_building', () => {
let node: AztecNode;
let aztecRpcServer: AztecRPCServer;

const abi = TestContractAbi as ContractAbi;
const abi = TestContractAbi;

beforeEach(async () => {
const account = mnemonicToAccount(MNEMONIC);
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/end-to-end/src/e2e_nested_contract.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AztecNode, getConfigEnvVars } from '@aztec/aztec-node';
import { AztecAddress, AztecRPCServer, Contract, ContractDeployer, Fr, TxStatus } from '@aztec/aztec.js';
import { createDebugLogger } from '@aztec/foundation/log';
import { ContractAbi } from '@aztec/noir-contracts';
import { ContractAbi } from '@aztec/foundation/abi';
import { ChildAbi, ParentAbi } from '@aztec/noir-contracts/examples';

import { mnemonicToAccount } from 'viem/accounts';
Expand Down
1 change: 1 addition & 0 deletions yarn-project/foundation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"./eslint": "./.eslintrc.cjs",
"./eslint-legacy": "./.eslintrc.legacy.cjs",
"./prettier": "./.prettierrc.json",
"./abi": "./dest/abi/index.js",
"./async-map": "./dest/async-map/index.js",
"./aztec-address": "./dest/aztec-address/index.js",
"./collection": "./dest/collection/index.js",
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions yarn-project/noir-compiler/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@aztec/foundation/eslint');
4 changes: 4 additions & 0 deletions yarn-project/noir-compiler/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target/
proofs/
Prover.toml
Verifier.toml
1 change: 1 addition & 0 deletions yarn-project/noir-compiler/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/fixtures
15 changes: 15 additions & 0 deletions yarn-project/noir-compiler/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM 278380418400.dkr.ecr.eu-west-2.amazonaws.com/yarn-project-base AS builder

COPY . .

WORKDIR /usr/src/yarn-project/noir-compiler
RUN yarn build && yarn formatting && yarn test

# Prune dev dependencies. See comment in base image.
RUN yarn cache clean
RUN yarn workspaces focus --production > /dev/null

FROM node:18-alpine
COPY --from=builder /usr/src/yarn-project/noir-compiler /usr/src/yarn-project/noir-compiler
WORKDIR /usr/src/yarn-project/noir-compiler
ENTRYPOINT ["yarn"]
15 changes: 15 additions & 0 deletions yarn-project/noir-compiler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Aztec Noir compiler

The Aztec noir compiler compiles noir contracts using noir_wasm and outputs Aztec formatted contract ABIs.

## Installation

To install the package, just run `yarn add @aztec/noir-compiler`.

## Usage

To run the compiler as a CLI tool, run `yarn aztec_noir_compiler compile <path_to_noir_contract_crate>`

## Status

Currently, this noir compiler uses noir master branch. It's not compatible yet with the test contracts for Aztec that are in the `noir-contracts` package, that need to be built following its README.md instructions.
61 changes: 61 additions & 0 deletions yarn-project/noir-compiler/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "@aztec/noir-compiler",
"version": "0.0.0",
"type": "module",
"exports": "./dest/index.js",
"typedoc": {
"entryPoint": "./src/index.ts",
"displayName": "Aztec noir compiler",
"tsconfig": "./tsconfig.json"
},
"bin": {
"aztec_noir_compiler": "dest/cli.js"
},
"scripts": {
"prepare": "node ../yarn-project-base/scripts/update_build_manifest.mjs package.json",
"prepare:check": "node ../yarn-project-base/scripts/update_build_manifest.mjs package.json --check",
"build": "yarn clean && tsc -b",
"build:dev": "tsc -b --watch",
"clean": "rm -rf ./dest .tsbuildinfo",
"formatting": "run -T prettier --check ./src && run -T eslint ./src",
"formatting:fix": "run -T prettier -w ./src",
"test": "DEBUG=wasm NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --passWithNoTests"
},
"inherits": [
"../package.common.json"
],
"jest": {
"preset": "ts-jest/presets/default-esm",
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
},
"testRegex": "./src/.*\\.test\\.ts$",
"rootDir": "./src"
},
"dependencies": {
"@aztec/foundation": "workspace:^",
"@noir-lang/noir-source-resolver": "1.1.1",
"@noir-lang/noir_wasm": "0.5.1-9740f54",
"commander": "^9.0.0",
"fs-extra": "^11.1.1",
"toml": "^3.0.0",
"tslib": "^2.4.0"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
"@rushstack/eslint-patch": "^1.1.4",
"@types/fs-extra": "^11.0.1",
"@types/jest": "^29.5.0",
"@types/node": "^18.7.23",
"jest": "^29.5.0",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
},
"files": [
"dest",
"src",
"!*.test.*"
],
"types": "./dest/index.d.ts"
}
Loading

0 comments on commit 7542d85

Please sign in to comment.