Skip to content

Commit

Permalink
Merge branch 'abi-wasm' of github.com:noir-lang/noir into abi-wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
kobyhallx committed Jul 19, 2023
2 parents d7579c6 + 6063d64 commit 4432308
Show file tree
Hide file tree
Showing 22 changed files with 6,395 additions and 13 deletions.
26 changes: 26 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Setup

inputs:
working-directory:
default: ./
required: false

runs:
using: composite
steps:
- name: Install node
uses: actions/setup-node@v3
with:
node-version: 18.15
- name: Cache
uses: actions/cache@v3
id: cache
with:
path: "**/node_modules"
key: yarn-v1-${{ hashFiles('**/yarn.lock') }}
- name: Install
run: |
cd ${{ inputs.working-directory }}
yarn --immutable
shell: bash
if: steps.cache.outputs.cache-hit != 'true'
95 changes: 95 additions & 0 deletions .github/workflows/abi_wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: ABI Wasm test

on:
pull_request:
merge_group:

# This will cancel previous runs when a branch or PR is updated
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref || github.run_id }}
cancel-in-progress: true

jobs:
build-noirc-abi-wasm:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3

- uses: cachix/install-nix-action@v20
with:
nix_path: nixpkgs=channel:nixos-22.11
github_access_token: ${{ secrets.GITHUB_TOKEN }}

- uses: cachix/cachix-action@v12
with:
name: barretenberg
authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"

- name: Build noirc_abi_wasm
run: |
nix build .#noirc_abi_wasm
- name: Dereference symlink
run: echo "UPLOAD_PATH=$(readlink -f result)" >> $GITHUB_ENV

- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: noirc_abi_wasm
path: ${{ env.UPLOAD_PATH }}
retention-days: 3

test-node:
needs: [build-noirc-abi-wasm]
name: Node.js Tests
runs-on: ubuntu-latest

steps:
- name: Checkout sources
uses: actions/checkout@v3

- name: Download artifact
uses: actions/download-artifact@v3
with:
name: noirc_abi_wasm
path: ./result

- name: Set up test environment
uses: ./.github/actions/setup
with:
working-directory: ./crates/noirc_abi_wasm

- name: Run node tests
working-directory: ./crates/noirc_abi_wasm
run: yarn test

test-browser:
needs: [build-noirc-abi-wasm]
name: Browser Tests
runs-on: ubuntu-latest

steps:
- name: Checkout sources
uses: actions/checkout@v3

- name: Download artifact
uses: actions/download-artifact@v3
with:
name: noirc_abi_wasm
path: ./result

- name: Set up test environment
uses: ./.github/actions/setup
with:
working-directory: ./crates/noirc_abi_wasm

- name: Install playwright deps
working-directory: ./crates/noirc_abi_wasm
run: |
npx playwright install
npx playwright install-deps
- name: Run browser tests
working-directory: ./crates/noirc_abi_wasm
run: yarn test:browser
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/noirc_abi_wasm/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
pkg
19 changes: 19 additions & 0 deletions crates/noirc_abi_wasm/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
root: true,
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint", "prettier"],
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
rules: {
"comma-spacing": ["error", { before: false, after: true }],
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"warn", // or "error"
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
caughtErrorsIgnorePattern: "^_",
},
],
"prettier/prettier": "error",
},
};
8 changes: 8 additions & 0 deletions crates/noirc_abi_wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Yarn
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
5 changes: 5 additions & 0 deletions crates/noirc_abi_wasm/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extension": ["ts"],
"spec": "test/node/**/*.test.ts",
"require": "ts-node/register"
}
873 changes: 873 additions & 0 deletions crates/noirc_abi_wasm/.yarn/releases/yarn-3.5.1.cjs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions crates/noirc_abi_wasm/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
yarnPath: .yarn/releases/yarn-3.5.1.cjs

nodeLinker: node-modules
2 changes: 0 additions & 2 deletions crates/noirc_abi_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ wasm-bindgen.workspace = true
serde.workspace = true

js-sys = "0.3.62"
log = "0.4.17"
wasm-logger = "0.2.0"
console_error_panic_hook = "0.1.7"
gloo-utils = { version = "0.1", features = ["serde"] }

Expand Down
23 changes: 22 additions & 1 deletion crates/noirc_abi_wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,26 @@
"repository": {
"type": "git",
"url": "https://github.com/noir-lang/noir.git"
},
"packageManager": "[email protected]",
"scripts": {
"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha",
"test:browser": "web-test-runner",
"lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0"
},
"devDependencies": {
"@esm-bundle/chai": "^4.3.4-fix.0",
"@typescript-eslint/eslint-plugin": "^5.59.5",
"@typescript-eslint/parser": "^5.59.5",
"@web/dev-server-esbuild": "^0.3.6",
"@web/test-runner": "^0.15.3",
"@web/test-runner-playwright": "^0.10.0",
"chai": "^4.3.7",
"eslint": "^8.40.0",
"eslint-plugin-prettier": "^4.2.1",
"mocha": "^10.2.0",
"prettier": "^2.8.8",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
}
}
}
10 changes: 6 additions & 4 deletions crates/noirc_abi_wasm/postBuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

# TODO: Handle the wasm target being built in release mode
WASM_BINARY=./target/wasm32-unknown-unknown/release/${pname}.wasm
NODE_WASM=./pkg/nodejs/${pname}_bg.wasm
BROWSER_WASM=./pkg/web/${pname}_bg.wasm
NODE_DIR=./pkg/nodejs
BROWSER_DIR=./pkg/web
NODE_WASM=${NODE_DIR}/${pname}_bg.wasm
BROWSER_WASM=${BROWSER_DIR}/${pname}_bg.wasm

wasm-bindgen $WASM_BINARY --out-dir ./pkg/nodejs --typescript --target nodejs
wasm-bindgen $WASM_BINARY --out-dir ./pkg/web --typescript --target web
wasm-bindgen $WASM_BINARY --out-dir ${NODE_DIR} --typescript --target nodejs
wasm-bindgen $WASM_BINARY --out-dir ${BROWSER_DIR} --typescript --target web
wasm-opt $NODE_WASM -o $NODE_WASM -O
wasm-opt $BROWSER_WASM -o $BROWSER_WASM -O
2 changes: 2 additions & 0 deletions crates/noirc_abi_wasm/src/js_witness_map.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! This can most likely be imported from acvm_js to avoid redefining it here.

use acvm::{
acir::native_types::{Witness, WitnessMap},
FieldElement,
Expand Down
3 changes: 3 additions & 0 deletions crates/noirc_abi_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#![warn(unreachable_pub)]
#![warn(clippy::semicolon_if_nothing_returned)]

// See Cargo.toml for explanation.
use getrandom as _;

use acvm::acir::native_types::WitnessMap;
use iter_extended::try_btree_map;
use noirc_abi::{errors::InputParserError, input_parser::InputValue, Abi, MAIN_RETURN_NAME};
Expand Down
24 changes: 24 additions & 0 deletions crates/noirc_abi_wasm/test/browser/abi_encode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect } from "@esm-bundle/chai";
import initACVM, { abiEncode, abiDecode, WitnessMap } from "../../../../result/";
import { DecodedInputs } from "../types";

beforeEach(async () => {
await initACVM();
});

it("recovers original inputs when abi encoding and decoding", async () => {
const { abi, inputs } = await import("../shared/abi_encode");

const initial_witness: WitnessMap = abiEncode(abi, inputs, null);
const decoded_inputs: DecodedInputs = abiDecode(abi, initial_witness);


expect(BigInt(decoded_inputs.inputs.foo)).to.be.equal(BigInt(inputs.foo));
expect(BigInt(decoded_inputs.inputs.bar[0])).to.be.equal(
BigInt(inputs.bar[0])
);
expect(BigInt(decoded_inputs.inputs.bar[1])).to.be.equal(
BigInt(inputs.bar[1])
);
expect(decoded_inputs.return_value).to.be.null;
});
19 changes: 19 additions & 0 deletions crates/noirc_abi_wasm/test/node/abi_encode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect } from "chai";
import { abiEncode, abiDecode, WitnessMap } from "../../../../result/";
import { DecodedInputs } from "../types";

it("recovers original inputs when abi encoding and decoding", async () => {
const { abi, inputs } = await import("../shared/abi_encode");

const initial_witness: WitnessMap = abiEncode(abi, inputs, null);
const decoded_inputs: DecodedInputs = abiDecode(abi, initial_witness);

expect(BigInt(decoded_inputs.inputs.foo)).to.be.equal(BigInt(inputs.foo));
expect(BigInt(decoded_inputs.inputs.bar[0])).to.be.equal(
BigInt(inputs.bar[0])
);
expect(BigInt(decoded_inputs.inputs.bar[1])).to.be.equal(
BigInt(inputs.bar[1])
);
expect(decoded_inputs.return_value).to.be.null;
});
20 changes: 20 additions & 0 deletions crates/noirc_abi_wasm/test/shared/abi_encode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// TODO: Add type definitions for these

export const abi = {
parameters: [
{ name: "foo", type: { kind: "field" }, visibility: "private" },
{
name: "bar",
type: { kind: "array", length: 2, type: { kind: "field" } },
visibility: "private",
},
],
param_witnesses: { foo: [1], bar: [2, 3] },
return_type: null,
return_witnesses: [],
};

export const inputs = {
foo: "1",
bar: ["1", "2"],
};
2 changes: 2 additions & 0 deletions crates/noirc_abi_wasm/test/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type DecodedInputs = { inputs: Record<string, any>; return_value: any };
17 changes: 17 additions & 0 deletions crates/noirc_abi_wasm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"moduleResolution": "node",
"outDir": "lib",
"target": "ESNext",
"module": "ESNext",
"strict": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"noImplicitAny": true,
"removeComments": false,
"preserveConstEnums": true,
"sourceMap": true,
"resolveJsonModule": true,
"importHelpers": true
}
}
25 changes: 25 additions & 0 deletions crates/noirc_abi_wasm/web-test-runner.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { esbuildPlugin } from "@web/dev-server-esbuild";
import { playwrightLauncher } from "@web/test-runner-playwright";

export default {
browsers: [
playwrightLauncher({ product: "chromium" }),
playwrightLauncher({ product: "webkit" }),
// Firefox requires 40s to perform a Pedersen hash so we recommend using either
// a Chromium- or Webkit-based browser
// playwrightLauncher({ product: "firefox" }),
],
plugins: [
esbuildPlugin({
ts: true,
}),
],
files: ["test/browser/**/*.test.ts"],
nodeResolve: true,
testFramework: {
config: {
ui: "bdd",
timeout: 40000,
},
},
};
Loading

0 comments on commit 4432308

Please sign in to comment.