From 494730aac72e91d8c7fb5b4355fdaaa829558ee4 Mon Sep 17 00:00:00 2001 From: Oleksandr Brezhniev Date: Sat, 7 Sep 2024 19:11:35 +0100 Subject: [PATCH] Snarkjs integration test (#113) * Snarkjs integration test * Fix none setup in integration tests * Fix memory for WitnessCalculatorCircom1. Added checks for loading code from wasm instance. --- .github/workflows/ci.yml | 2 +- .github/workflows/integration-test.yml | 77 ++++++++++++++++++++++++++ build/main.cjs | 17 ++++-- js/witness_calculator.js | 17 ++++-- 4 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/integration-test.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 68081c3..9aae475 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,4 +1,4 @@ -name: Continuous Integration +name: JS Witness calc tests on: push: branches: diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml new file mode 100644 index 0000000..9ea0f65 --- /dev/null +++ b/.github/workflows/integration-test.yml @@ -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 diff --git a/build/main.cjs b/build/main.cjs index 13e9404..1697598 100644 --- a/build/main.cjs +++ b/build/main.cjs @@ -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) @@ -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{ @@ -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; diff --git a/js/witness_calculator.js b/js/witness_calculator.js index 9bdd80f..ca96f27 100644 --- a/js/witness_calculator.js +++ b/js/witness_calculator.js @@ -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) @@ -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{ @@ -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;