Skip to content

Commit

Permalink
Snarkjs integration test (#113)
Browse files Browse the repository at this point in the history
* Snarkjs integration test

* Fix none setup in integration tests

* Fix memory for WitnessCalculatorCircom1. Added checks for loading code from wasm instance.
  • Loading branch information
OBrezhniev authored Sep 7, 2024
1 parent d0c52ba commit 494730a
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Continuous Integration
name: JS Witness calc tests
on:
push:
branches:
Expand Down
77 changes: 77 additions & 0 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Snarkjs Integration Test
on:
push:
branches:
- master
pull_request:

jobs:
test:
name: Test
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
node-version: ["18", "20", "22"]

steps:
- name: Checkout project
uses: actions/checkout@v4
with:
path: circom_runtime

- name: Checkout project
uses: actions/checkout@v4
with:
repository: iden3/snarkjs
path: snarkjs

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
check-latest: true
cache: 'npm'
cache-dependency-path: snarkjs/package-lock.json

- name: Install circom_runtime dependencies
run: npm ci
working-directory: circom_runtime

- name: Build circom_runtime
run: npm run build
working-directory: circom_runtime

- name: Install circom_runtime to snarkjs as a link
run: npm install ../circom_runtime
working-directory: snarkjs

- name: Install snarkjs dependencies
run: npm ci
working-directory: snarkjs

- name: Build snarkjs
run: npm run build
working-directory: snarkjs

- name: Run snarkjs tests
run: npm test
working-directory: snarkjs

- name: Install smart_contract_tests dependencies
working-directory: snarkjs/smart_contract_tests
run: npm ci

- name: Run smart_contract_tests
working-directory: snarkjs/smart_contract_tests
run: npm test

- name: Install browser dependencies
working-directory: snarkjs/browser_tests
run: npm ci

- name: Run browser tests
working-directory: snarkjs/browser_tests
run: npm test
17 changes: 12 additions & 5 deletions build/main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ function toArray32(s,size) {
async function builder(code, options) {
let instance;
let wc;
let memory;

options = options || {};

// Only circom 2 implements version lookup through exports in the WASM
// We default to `1` and update if we see the `getVersion` export (major version)
Expand All @@ -93,13 +96,13 @@ async function builder(code, options) {
// If we can't lookup the patch version, assume the lowest
let patchVersion = 0;

let codeIsWebAssemblyInstance = false;

if (code instanceof WebAssembly.Instance) {
instance = code;
codeIsWebAssemblyInstance = true;
} else {
options = options || {};

let memorySize = 32767;
let memory;
let memoryAllocated = false;
while (!memoryAllocated){
try{
Expand Down Expand Up @@ -249,9 +252,13 @@ async function builder(code, options) {
// We explicitly check for major version 2 in case there's a circom v3 in the future
if (majorVersion === 2) {
wc = new WitnessCalculatorCircom2(instance, sanityCheck);
} else {
// TODO: Maybe we want to check for the explicit version 1 before choosing this?
} else if (majorVersion === 1) {
if (codeIsWebAssemblyInstance) {
throw new Error('Loading code from WebAssembly instance is not supported for circom version 1');
}
wc = new WitnessCalculatorCircom1(memory, instance, sanityCheck);
} else {
throw new Error(`Unsupported circom version: ${majorVersion}`);
}
return wc;

Expand Down
17 changes: 12 additions & 5 deletions js/witness_calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import { Scalar, F1Field } from "ffjavascript";
export default async function builder(code, options) {
let instance;
let wc;
let memory;

options = options || {};

// Only circom 2 implements version lookup through exports in the WASM
// We default to `1` and update if we see the `getVersion` export (major version)
Expand All @@ -33,13 +36,13 @@ export default async function builder(code, options) {
// If we can't lookup the patch version, assume the lowest
let patchVersion = 0;

let codeIsWebAssemblyInstance = false;

if (code instanceof WebAssembly.Instance) {
instance = code;
codeIsWebAssemblyInstance = true;
} else {
options = options || {};

let memorySize = 32767;
let memory;
let memoryAllocated = false;
while (!memoryAllocated){
try{
Expand Down Expand Up @@ -189,9 +192,13 @@ export default async function builder(code, options) {
// We explicitly check for major version 2 in case there's a circom v3 in the future
if (majorVersion === 2) {
wc = new WitnessCalculatorCircom2(instance, sanityCheck);
} else {
// TODO: Maybe we want to check for the explicit version 1 before choosing this?
} else if (majorVersion === 1) {
if (codeIsWebAssemblyInstance) {
throw new Error('Loading code from WebAssembly instance is not supported for circom version 1');
}
wc = new WitnessCalculatorCircom1(memory, instance, sanityCheck);
} else {
throw new Error(`Unsupported circom version: ${majorVersion}`);
}
return wc;

Expand Down

0 comments on commit 494730a

Please sign in to comment.