From 5dc4ff52959915c0dd21a693d03f83ca5d01aab8 Mon Sep 17 00:00:00 2001 From: mhchia Date: Sat, 1 Apr 2023 21:46:01 -0600 Subject: [PATCH] ci: build all circuits and run test --- .github/workflows/test.yml | 35 +++++++++++++++++++++++++++++++++++ package.json | 2 +- scripts/build-circuits.sh | 29 +++++++++++++++-------------- scripts/install-circom.sh | 11 +++++++++++ 4 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100755 scripts/install-circom.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..b85c7b3 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,35 @@ +# This workflow test if circuits can be built and the tests pass. + +name: Test + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: 18 + cache: 'npm' + - name: Cache circom + id: cache-circom + uses: actions/cache@v3 + with: + path: ~/.cargo/bin/circom + # Since the version of circom is specified in `scripts/install-circom.sh`, + # as long as the file doesn't change we can reuse the circom binary. + key: ${{ runner.os }}-circom-${{ hashFiles('./scripts/install-circom.sh') }} + - name: Install circom if not cached + run: ./scripts/install-circom.sh + - run: npm ci + - name: Build all circuits + run: npm run build + - name: Run the tests + run: npm test diff --git a/package.json b/package.json index 97b2f68..e3f5172 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "scripts": { - "build": "bash scripts/build-circuits.sh", + "build": "bash scripts/build-circuits.sh same && bash scripts/build-circuits.sh diff && bash scripts/build-circuits.sh withdraw", "test": "ts-mocha --exit test/**/*.test.ts" }, "dependencies": { diff --git a/scripts/build-circuits.sh b/scripts/build-circuits.sh index 2c86f13..fc5aea2 100644 --- a/scripts/build-circuits.sh +++ b/scripts/build-circuits.sh @@ -17,25 +17,26 @@ else wget https://hermez.s3-eu-west-1.amazonaws.com/powersOfTau28_hez_final_14.ptau fi +circuit_dir="../circuits" circuit_path="" circuit_type="" -zkeypath="" +zkeydir="../zkeyFiles" + if [ "$1" = "diff" ]; then echo -e "\033[32mUsing Diff circuit\033[0m" - circuit_type="diff" - circuit_path="../circuits/rln-diff.circom" - zkeypath="../zkeyFiles/rln-v2-diff" + circuit_name="rln-diff" elif [ "$1" = "same" ]; then echo -e "\033[32mUsing Same circuit\033[0m" - circuit_type="same" - circuit_path="../circuits/rln-same.circom" - zkeypath="../zkeyFiles/rln-v2-same" + circuit_name="rln-same" +elif [ "$1" = "withdraw" ]; then + echo -e "\033[32mUsing Withdraw circuit\033[0m" + circuit_name="withdraw" else - circuit_type="same" - circuit_path="../circuits/rln-same.circom" - zkeypath="../zkeyFiles/rln-v2-same" echo -e "\033[33mUnrecognized argument, using 'same' as default.\033[0m" + circuit_name="rln-same" fi +circuit_path="$circuit_dir/$circuit_name.circom" +zkeypath="$zkeydir/v2/$circuit_name" if ! [ -x "$(command -v circom)" ]; then echo -e '\033[31mError: circom is not installed.\033[0m' >&2 @@ -53,11 +54,11 @@ echo -e "\033[36mBuild Path: $PWD\033[0m" circom --version circom $circuit_path --r1cs --wasm --sym -snarkjs r1cs export json rln-same.r1cs rln-same.r1cs.json +snarkjs r1cs export json $circuit_name.r1cs $circuit_name.r1cs.json echo -e "\033[36mRunning groth16 trusted setup\033[0m" -snarkjs groth16 setup rln-same.r1cs powersOfTau28_hez_final_14.ptau setup/rln_0000.zkey +snarkjs groth16 setup $circuit_name.r1cs powersOfTau28_hez_final_14.ptau setup/rln_0000.zkey snarkjs zkey contribute setup/rln_0000.zkey setup/rln_0001.zkey --name="First contribution" -v -e="Random entropy" snarkjs zkey contribute setup/rln_0001.zkey setup/rln_0002.zkey --name="Second contribution" -v -e="Another random entropy" @@ -69,7 +70,7 @@ mkdir -p $zkeypath snarkjs zkey export verificationkey setup/rln_final.zkey $zkeypath/verification_key.json snarkjs zkey export solidityverifier setup/rln_final.zkey contracts/verifier.sol -cp rln-$circuit_type\_js/rln-$circuit_type.wasm $zkeypath/rln.wasm +cp $circuit_name\_js/$circuit_name.wasm $zkeypath/rln.wasm cp setup/rln_final.zkey $zkeypath/rln_final.zkey shasumcmd="shasum -a 256" @@ -77,7 +78,7 @@ shasumcmd="shasum -a 256" config_path="$zkeypath/circuit.config.toml" echo -e "[Circuit_Version]" > $config_path echo -e "RLN_Version = 2" >> $config_path -echo -e "RLN_Type = \"$circuit_type\"" >> $config_path +echo -e "RLN_Type = \"$circuit_name\"" >> $config_path echo -e "" >> $config_path diff --git a/scripts/install-circom.sh b/scripts/install-circom.sh new file mode 100755 index 0000000..24e4b21 --- /dev/null +++ b/scripts/install-circom.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +circom_version=v2.1.5 + +if ! [ -x "$(command -v circom)" ]; then + git clone https://github.com/iden3/circom.git + cd circom + git checkout $circom_version + cargo build --release + cargo install --path circom +fi