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(ci): Add initial testing for running simulator in the browser #3

Merged
merged 9 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Setup

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: yarn --immutable
shell: bash
if: steps.cache.outputs.cache-hit != 'true'
34 changes: 34 additions & 0 deletions .github/workflows/browser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Browser

on: [push, pull_request]

# 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:
test:
name: Test Wasm in browser
runs-on: ubuntu-latest

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

- name: Setup rust toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.66.0

- name: Install wasm-pack
run: cargo install wasm-pack

- name: Build wasm crate
run: ./build-wasm

- name: Set up test environment
uses: ./.github/actions/setup

- name: Run browser tests
run: yarn test:browser
12 changes: 9 additions & 3 deletions .github/workflows/wasm.yml → .github/workflows/node.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Wasm
name: Node

on: [push, pull_request]

Expand All @@ -8,8 +8,8 @@ concurrency:
cancel-in-progress: true

jobs:
build:
name: Build Wasm
test:
name: Test Wasm in Node.js
runs-on: ubuntu-latest

steps:
Expand All @@ -26,3 +26,9 @@ jobs:

- name: Build wasm crate
run: ./build-wasm

- name: Set up test environment
uses: ./.github/actions/setup

- name: Run node tests
run: yarn test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ examples/9
.vscode
node_modules
pkg/
lib/

# Yarn
.pnp.*
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@
"private": true,
"packageManager": "yarn@3.5.1",
"scripts": {
"test": "jest ./test",
"test": "jest ./test/node",
"test:browser": "web-test-runner test/browser/**/*.test.ts --node-resolve",
"lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
"@types/jest": "^29.5.1",
"@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",
"eslint": "^8.40.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.5.0",
"jest-browser-globals": "^25.1.0-beta",
"prettier": "^2.8.8",
"ts-jest": "^29.1.0",
"typescript": "^5.0.4"
Expand Down
33 changes: 33 additions & 0 deletions test/browser/abi_encode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import initACVMSimulator, { abi_encode, abi_decode } from "../../pkg/";

test("recovers original inputs when abi encoding and decoding", async () => {
await initACVMSimulator();

// TODO use ts-rs to get ABI type bindings.
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: [],
};
const inputs = {
foo: "1",
bar: ["1", "2"],
};
const initial_witness: Map<string, string> = abi_encode(abi, inputs, null);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const decoded_inputs: { inputs: Record<string, any>; return_value: any } =
abi_decode(abi, initial_witness);

expect(BigInt(decoded_inputs.inputs.foo)).toBe(BigInt(inputs.foo));
expect(BigInt(decoded_inputs.inputs.bar[0])).toBe(BigInt(inputs.bar[0]));
expect(BigInt(decoded_inputs.inputs.bar[1])).toBe(BigInt(inputs.bar[1]));
expect(decoded_inputs.return_value).toBe(null);
});
160 changes: 160 additions & 0 deletions test/browser/execute_circuit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import initACVMSimulator, {
abi_encode,
abi_decode,
execute_circuit,
} from "../../pkg/";

test("successfully executes circuit and extracts return value", async () => {
await initACVMSimulator();

// Noir program which enforces that x != y and returns x + y.
const abi = {
parameters: [
{ name: "x", type: { kind: "field" }, visibility: "private" },
{ name: "y", type: { kind: "field" }, visibility: "public" },
],
param_witnesses: {
x: [1],
y: [2],
},
return_type: { kind: "field" },
return_witnesses: [6],
};
const bytecode = Uint8Array.from([
0, 0, 0, 0, 7, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 6, 0, 0, 0, 6,
0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 48,
100, 78, 114, 225, 49, 160, 41, 184, 80, 69, 182, 129, 129, 88, 93, 40, 51,
232, 72, 121, 185, 112, 145, 67, 225, 245, 147, 240, 0, 0, 0, 2, 0, 0, 0,
48, 100, 78, 114, 225, 49, 160, 41, 184, 80, 69, 182, 129, 129, 88, 93, 40,
51, 232, 72, 121, 185, 112, 145, 67, 225, 245, 147, 240, 0, 0, 0, 3, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 0, 1, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 0, 0, 4, 0, 0, 0, 48, 100, 78, 114, 225,
49, 160, 41, 184, 80, 69, 182, 129, 129, 88, 93, 40, 51, 232, 72, 121, 185,
112, 145, 67, 225, 245, 147, 240, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 0, 0, 5, 0, 0, 0, 48,
100, 78, 114, 225, 49, 160, 41, 184, 80, 69, 182, 129, 129, 88, 93, 40, 51,
232, 72, 121, 185, 112, 145, 67, 225, 245, 147, 240, 0, 0, 0, 3, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 48, 100, 78, 114, 225, 49, 160,
41, 184, 80, 69, 182, 129, 129, 88, 93, 40, 51, 232, 72, 121, 185, 112, 145,
67, 225, 245, 147, 240, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2,
0, 0, 0, 48, 100, 78, 114, 225, 49, 160, 41, 184, 80, 69, 182, 129, 129, 88,
93, 40, 51, 232, 72, 121, 185, 112, 145, 67, 225, 245, 147, 240, 0, 0, 0, 6,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]);

const inputs = {
x: "1",
y: "2",
};
const return_witness: number = abi.return_witnesses[0];

const initial_witness: Map<number, string> = abi_encode(abi, inputs, null);
const solved_witness: Map<number, string> = await execute_circuit(
bytecode,
initial_witness,
() => {
throw Error("unexpected oracle");
}
);

// Solved witness should be consistent with initial witness
initial_witness.forEach((value, key) => {
expect(solved_witness.get(key) as string).toBe(value);
});
// Solved witness should contain expected return value
expect(BigInt(solved_witness.get(return_witness) as string)).toBe(3n);

const decoded_inputs = abi_decode(abi, solved_witness);

expect(BigInt(decoded_inputs.return_value)).toBe(3n);
});

test("successfully processes oracle opcodes", async () => {
await initACVMSimulator();

// We use a handwritten circuit which uses an oracle to calculate the sum of witnesses 1 and 2
// and stores the result in witness 3. This is then enforced by an arithmetic opcode to check the result is correct.

// let oracle = OracleData {
// name: "example_oracle".to_owned(),
// inputs: vec![Witness(1).into(), Witness(2).into()],
// input_values: Vec::new(),
// outputs: vec![Witness(3)],
// output_values: Vec::new(),
// };
// let check: Expression = Expression {
// mul_terms: Vec::new(),
// linear_combinations: vec![
// (FieldElement::one(), Witness(1)),
// (FieldElement::one(), Witness(2)),
// (-FieldElement::one(), Witness(3)),
// ],
// q_c: FieldElement::zero(),
// };

// let circuit = Circuit {
// current_witness_index: 4,
// opcodes: vec![Opcode::Oracle(oracle), Opcode::Arithmetic(check)],
// public_parameters: PublicInputs::default(),
// return_values: PublicInputs::default(),
// };
const oracle_bytecode = new Uint8Array([
0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 14, 0, 0, 0,
101, 120, 97, 109, 112, 108, 101, 95, 111, 114, 97, 99, 108, 101, 2, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0,
48, 100, 78, 114, 225, 49, 160, 41, 184, 80, 69, 182, 129, 129, 88, 93, 40,
51, 232, 72, 121, 185, 112, 145, 67, 225, 245, 147, 240, 0, 0, 0, 3, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
]);

const initial_witness: Map<number, string> = new Map();
initial_witness.set(
1,
"0x0000000000000000000000000000000000000000000000000000000000000001"
);
initial_witness.set(
2,
"0x0000000000000000000000000000000000000000000000000000000000000001"
);

const solved_witness: Map<number, string> = await execute_circuit(
oracle_bytecode,
initial_witness,
async (_name: string, _inputs: string[]) => {
// We cannot use jest matchers here (or write to a variable in the outside scope) so cannot test that
// the values for `name` and `inputs` are correct, we can `console.log` them however.
// console.log(name)
// console.log(inputs)

// Witness(1) + Witness(2) = 1 + 1 = 2
return ["0x02"];
}
);

// If incorrect value is written into circuit then execution should halt due to unsatisfied constraint in
// arithmetic opcode. Nevertheless, check that returned value was inserted correctly.
expect(solved_witness.get(3) as string).toBe(
"0x0000000000000000000000000000000000000000000000000000000000000002"
);
});
2 changes: 1 addition & 1 deletion test/abi_encode.test.ts → test/node/abi_encode.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "@jest/globals";
import { abi_encode, abi_decode } from "../pkg/";
import { abi_encode, abi_decode } from "../../pkg/";

test("recovers original inputs when abi encoding and decoding", () => {
// TODO use ts-rs to get ABI type bindings.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "@jest/globals";
import { abi_encode, abi_decode, execute_circuit } from "../pkg/";
import { abi_encode, abi_decode, execute_circuit } from "../../pkg/";

test("successfully executes circuit and extracts return value", async () => {
// Noir program which enforces that x != y and returns x + y.
Expand Down
19 changes: 19 additions & 0 deletions web-test-runner.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { esbuildPlugin } from "@web/dev-server-esbuild";

export default {
nodeResolve: true,
files: ["src/**/*.test.ts", "src/**/*.spec.ts"],
plugins: [
esbuildPlugin({
ts: true,
}),
],
testRunnerHtml: (testFramework) => `
<html>
<head>
<script type="module" src="${testFramework}"></script>
<script type="module">import 'jest-browser-globals';</script>
</head>
</html>
`,
};
Loading