Skip to content

Commit

Permalink
Merge branch 'master' into zpedro/docs_release_please
Browse files Browse the repository at this point in the history
* master:
  feat: codegen typed interfaces for functions in `noir_codegen` (#3533)
  chore: add dependency on noir_js from docs package (#3559)
  • Loading branch information
TomAFrench committed Nov 27, 2023
2 parents 3508191 + 290c463 commit eddb86a
Show file tree
Hide file tree
Showing 14 changed files with 352 additions and 37 deletions.
25 changes: 5 additions & 20 deletions .github/workflows/docs-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
if: needs.add_label.outputs.has_label == 'true'
steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v2
Expand All @@ -71,28 +71,13 @@ jobs:
run: |
npm i wasm-opt -g
- name: Install dependencies
run: yarn

- name: Build acvm_js
run: yarn workspace @noir-lang/acvm_js build

- name: Build noirc_abi
run: yarn workspace @noir-lang/noirc_abi build

- name: Build noir_js_types
run: yarn workspace @noir-lang/types build

- name: Build barretenberg wrapper
run: yarn workspace @noir-lang/backend_barretenberg build

- name: Run noir_js
run: |
yarn workspace @noir-lang/noir_js build
- name: Install Yarn dependencies
uses: ./.github/actions/setup

- name: Build docs
working-directory: docs
run:
yarn workspace docs build
yarn workspaces foreach -Rt run build

- name: Deploy to Netlify
uses: nwtgck/[email protected]
Expand Down
97 changes: 97 additions & 0 deletions .github/workflows/test-noir_codegen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
name: noir_codegen

on:
pull_request:
merge_group:
push:
branches:
- master

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref || github.run_id }}
cancel-in-progress: true

jobs:
build-nargo:
runs-on: ubuntu-22.04
strategy:
matrix:
target: [x86_64-unknown-linux-gnu]

steps:
- name: Checkout Noir repo
uses: actions/checkout@v4

- name: Setup toolchain
uses: dtolnay/[email protected]

- uses: Swatinem/rust-cache@v2
with:
key: ${{ matrix.target }}
cache-on-failure: true
save-if: ${{ github.event_name != 'merge_group' }}

- name: Build Nargo
run: cargo build --package nargo_cli --release

- name: Package artifacts
run: |
mkdir dist
cp ./target/release/nargo ./dist/nargo
7z a -ttar -so -an ./dist/* | 7z a -si ./nargo-x86_64-unknown-linux-gnu.tar.gz
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: nargo
path: ./dist/*
retention-days: 3

test:
needs: [build-nargo]
name: Test noir_codegen
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install Yarn dependencies
uses: ./.github/actions/setup

- name: Setup toolchain
uses: dtolnay/[email protected]
with:
targets: wasm32-unknown-unknown

- uses: Swatinem/rust-cache@v2
with:
key: wasm32-unknown-unknown-noir-js
cache-on-failure: true
save-if: ${{ github.event_name != 'merge_group' }}

- name: Install jq
run: sudo apt-get install jq

- name: Install wasm-bindgen-cli
uses: taiki-e/install-action@v2
with:
tool: [email protected]

- name: Install wasm-opt
run: |
npm i wasm-opt -g
- name: Build acvm_js
run: yarn workspace @noir-lang/acvm_js build

- name: Build noirc_abi
run: yarn workspace @noir-lang/noirc_abi build

- name: Build noir_js_types
run: yarn workspace @noir-lang/types build

- name: Build noir_js
run: yarn workspace @noir-lang/noir_js build

- name: Run noir_codegen tests
run: yarn workspace @noir-lang/noir_codegen test
1 change: 1 addition & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@docusaurus/preset-classic": "^2.4.0",
"@easyops-cn/docusaurus-search-local": "^0.35.0",
"@mdx-js/react": "^1.6.22",
"@noir-lang/noir_js": "workspace:*",
"axios": "^1.4.0",
"clsx": "^1.2.1",
"docusaurus-plugin-typedoc": "1.0.0-next.18",
Expand Down
1 change: 1 addition & 0 deletions tooling/noir_codegen/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ crs
lib

!test/*/target
test/codegen
4 changes: 3 additions & 1 deletion tooling/noir_codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
"scripts": {
"dev": "tsc-multi --watch",
"build": "tsc",
"test": "ts-node --esm src/main.ts ./test/assert_lt/target/** --out-dir ./test/codegen && yarn test:node && rm -rf ./test/codegen",
"test": "yarn test:codegen && yarn test:node && yarn test:clean",
"test:codegen": "ts-node --esm src/main.ts ./test/assert_lt/target/** --out-dir ./test/codegen",
"test:node": "mocha --timeout 25000 --exit --config ./.mocharc.json",
"test:clean": "rm -rf ./test/codegen",
"prettier": "prettier 'src/**/*.ts'",
"prettier:fix": "prettier --write 'src/**/*.ts' 'test/**/*.ts'",
"lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0",
Expand Down
43 changes: 36 additions & 7 deletions tooling/noir_codegen/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,53 @@
import { CompiledCircuit } from '@noir-lang/types';
import { PrimitiveTypesUsed, generateTsInterface } from './noir_types.js';

const codegenImports = `import { InputMap, InputValue } from "@noir-lang/noirc_abi"
import { Noir } from "@noir-lang/noir_js"`;
// TODO: reenable this. See `abiTypeToTs` for reasoning.
// export type FixedLengthArray<T, L extends number> = L extends 0 ? never[]: T[] & { length: L };

const codegenPrelude = `/* Autogenerated file, do not edit! */
/* eslint-disable */
import { Noir, InputMap } from "@noir-lang/noir_js"
`;

const codegenFunction = (
name: string,
compiled_program: CompiledCircuit,
) => `export async function ${name}(args: InputMap): Promise<InputValue> {
function_signature: { inputs: [string, string][]; returnValue: string | null },
) => {
const args = function_signature.inputs.map(([name]) => `${name}`).join(', ');
const args_with_types = function_signature.inputs.map(([name, type]) => `${name}: ${type}`).join(', ');

return `export async function ${name}(${args_with_types}): Promise<${function_signature.returnValue}> {
const program = new Noir(${JSON.stringify(compiled_program)});
const args: InputMap = { ${args} };
const { returnValue } = await program.execute(args);
return returnValue;
return returnValue as ${function_signature.returnValue};
}`;
};

export const codegen = (programs: [string, CompiledCircuit][]): string => {
const results = [codegenImports];
let results = [codegenPrelude];
const primitiveTypeMap = new Map<string, PrimitiveTypesUsed>();

const functions: string[] = [];
for (const [name, program] of programs) {
results.push(codegenFunction(name, stripUnwantedFields(program)));
const [types_string, function_sig] = generateTsInterface(program.abi, primitiveTypeMap);
functions.push(types_string);
functions.push('\n');
functions.push(codegenFunction(name, stripUnwantedFields(program), function_sig));
}

// Add the primitive Noir types that do not have a 1-1 mapping to TypeScript.
const primitiveTypeAliases: string[] = [];
for (const value of primitiveTypeMap.values()) {
primitiveTypeAliases.push(`export type ${value.aliasName} = ${value.tsType};`);
}

return results.join('\n\n');
results = results.concat(...primitiveTypeAliases, ...functions);

return results.filter((val) => val !== '').join('\n');
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
Loading

0 comments on commit eddb86a

Please sign in to comment.