From 2d96a2e7c65eb89fa217417c4a7a4076be1657fa Mon Sep 17 00:00:00 2001 From: mhchia Date: Thu, 25 May 2023 23:24:07 +0800 Subject: [PATCH 1/2] test: remove rln-same test and rename rln-diff to rln --- test/configs.ts | 2 - test/rln-same.test.ts | 91 -------------------------- test/{rln-diff.test.ts => rln.test.ts} | 4 +- 3 files changed, 2 insertions(+), 95 deletions(-) delete mode 100644 test/rln-same.test.ts rename test/{rln-diff.test.ts => rln.test.ts} (96%) diff --git a/test/configs.ts b/test/configs.ts index 13aaf6e..f6c7aac 100644 --- a/test/configs.ts +++ b/test/configs.ts @@ -1,5 +1,3 @@ -import * as path from "path"; - // MERKLE TREE export const MERKLE_TREE_DEPTH = 20; export const MERKLE_TREE_ZERO_VALUE = BigInt(0); diff --git a/test/rln-same.test.ts b/test/rln-same.test.ts deleted file mode 100644 index 4228954..0000000 --- a/test/rln-same.test.ts +++ /dev/null @@ -1,91 +0,0 @@ -import * as path from "path"; -import assert from "assert"; -const tester = require("circom_tester").wasm; -import poseidon from "poseidon-lite"; -import { calculateOutput, genFieldElement, genMerkleProof, getSignal } from "./utils"; - -const circuitPath = path.join(__dirname, "..", "circuits", "rln-same.circom"); - -// ffjavascript has no types so leave circuit with untyped -type CircuitT = any; - - -describe("Test rln-same.circom", function () { - let circuit: CircuitT; - - this.timeout(30000); - - before(async function () { - circuit = await tester(circuitPath); - }); - - it("Should generate witness with correct outputs", async () => { - // Public inputs - const x = genFieldElement(); - const externalNullifier = genFieldElement(); - // Private inputs - const identitySecret = genFieldElement(); - const identitySecretCommitment = poseidon([identitySecret]); - const merkleProof = genMerkleProof([identitySecretCommitment], 0) - const merkleRoot = merkleProof.root - const messageLimit = BigInt(10) - const messageId = BigInt(1) - - const inputs = { - // Private inputs - identitySecret, - messageId, - pathElements: merkleProof.siblings, - identityPathIndex: merkleProof.pathIndices, - // Public inputs - x, - externalNullifier, - messageLimit, - } - - // Test: should generate proof if inputs are correct - const witness: bigint[] = await circuit.calculateWitness(inputs, true); - await circuit.checkConstraints(witness); - - const {y, nullifier} = calculateOutput(identitySecret, x, externalNullifier, messageId) - - const outputRoot = await getSignal(circuit, witness, "root") - const outputY = await getSignal(circuit, witness, "y") - const outputNullifier = await getSignal(circuit, witness, "nullifier") - - assert.equal(outputY, y) - assert.equal(outputRoot, merkleRoot) - assert.equal(outputNullifier, nullifier) - }); - - it("should fail to generate witness if messageId is not in range [1, messageLimit]", async function () { - // Public inputs - const x = genFieldElement(); - const externalNullifier = genFieldElement(); - // Private inputs - const identitySecret = genFieldElement(); - const identitySecretCommitment = poseidon([identitySecret]); - const merkleProof = genMerkleProof([identitySecretCommitment], 0) - const messageLimit = BigInt(10) - // valid message id is in the range [0, messageLimit-1] - const invalidMessageIds = [messageLimit, messageLimit + BigInt(1)] - - for (const invalidMessageId of invalidMessageIds) { - const inputs = { - // Private inputs - identitySecret, - messageId: invalidMessageId, - pathElements: merkleProof.siblings, - identityPathIndex: merkleProof.pathIndices, - // Public inputs - x, - externalNullifier, - messageLimit, - } - await assert.rejects(async () => { - await circuit.calculateWitness(inputs, true); - }, /Error: Assert Failed/); - } - }); - -}); diff --git a/test/rln-diff.test.ts b/test/rln.test.ts similarity index 96% rename from test/rln-diff.test.ts rename to test/rln.test.ts index ac95b64..850f028 100644 --- a/test/rln-diff.test.ts +++ b/test/rln.test.ts @@ -5,7 +5,7 @@ import poseidon from "poseidon-lite"; import { calculateOutput, genFieldElement, genMerkleProof, getSignal } from "./utils" -const circuitPath = path.join(__dirname, "..", "circuits", "rln-diff.circom"); +const circuitPath = path.join(__dirname, "..", "circuits", "rln.circom"); // ffjavascript has no types so leave circuit with untyped type CircuitT = any; @@ -18,7 +18,7 @@ function calculateLeaf(identitySecret: bigint, userMessageLimit: bigint) { } -describe("Test rln-diff.circom", function () { +describe("Test rln.circom", function () { let circuit: CircuitT; this.timeout(30000); From ab5415996e3e2d8c47266ca1dfdd831e05fd6b25 Mon Sep 17 00:00:00 2001 From: mhchia Date: Thu, 25 May 2023 23:24:34 +0800 Subject: [PATCH 2/2] build: remove rln-same and change rln-diff to rln Also, refactor a little bit --- package.json | 2 +- scripts/build-circuits.sh | 41 ++++++++++++++++++--------------------- 2 files changed, 20 insertions(+), 23 deletions(-) mode change 100644 => 100755 scripts/build-circuits.sh diff --git a/package.json b/package.json index e3f5172..10e5c9b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "scripts": { - "build": "bash scripts/build-circuits.sh same && bash scripts/build-circuits.sh diff && bash scripts/build-circuits.sh withdraw", + "build": "./scripts/build-circuits.sh rln && ./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 old mode 100644 new mode 100755 index fc5aea2..9503254 --- a/scripts/build-circuits.sh +++ b/scripts/build-circuits.sh @@ -22,21 +22,18 @@ circuit_path="" circuit_type="" zkeydir="../zkeyFiles" -if [ "$1" = "diff" ]; then - echo -e "\033[32mUsing Diff circuit\033[0m" - circuit_name="rln-diff" -elif [ "$1" = "same" ]; then - echo -e "\033[32mUsing Same circuit\033[0m" - circuit_name="rln-same" +if [ "$1" = "rln" ]; then + echo -e "\033[32mUsing RLN circuit\033[0m" + circuit_name="rln" elif [ "$1" = "withdraw" ]; then echo -e "\033[32mUsing Withdraw circuit\033[0m" circuit_name="withdraw" else - echo -e "\033[33mUnrecognized argument, using 'same' as default.\033[0m" - circuit_name="rln-same" + echo -e "\033[33mUnrecognized argument" + exit 1 fi circuit_path="$circuit_dir/$circuit_name.circom" -zkeypath="$zkeydir/v2/$circuit_name" +zkeypath="$zkeydir/$circuit_name" if ! [ -x "$(command -v circom)" ]; then echo -e '\033[31mError: circom is not installed.\033[0m' >&2 @@ -54,24 +51,24 @@ echo -e "\033[36mBuild Path: $PWD\033[0m" circom --version circom $circuit_path --r1cs --wasm --sym -snarkjs r1cs export json $circuit_name.r1cs $circuit_name.r1cs.json +npx snarkjs r1cs export json $circuit_name.r1cs $circuit_name.r1cs.json echo -e "\033[36mRunning groth16 trusted setup\033[0m" -snarkjs groth16 setup $circuit_name.r1cs powersOfTau28_hez_final_14.ptau setup/rln_0000.zkey +npx snarkjs groth16 setup $circuit_name.r1cs powersOfTau28_hez_final_14.ptau setup/circuit_00000.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" -snarkjs zkey beacon setup/rln_0002.zkey setup/rln_final.zkey 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon phase2" +npx snarkjs zkey contribute setup/circuit_00000.zkey setup/circuit_00001.zkey --name="First contribution" -v -e="Random entropy" +npx snarkjs zkey contribute setup/circuit_00001.zkey setup/circuit_00002.zkey --name="Second contribution" -v -e="Another random entropy" +npx snarkjs zkey beacon setup/circuit_00002.zkey setup/final.zkey 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon phase2" echo -e "Exporting artifacts to zkeyFiles and contracts directory" 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 +npx snarkjs zkey export verificationkey setup/final.zkey $zkeypath/verification_key.json +npx snarkjs zkey export solidityverifier setup/final.zkey contracts/verifier.sol -cp $circuit_name\_js/$circuit_name.wasm $zkeypath/rln.wasm -cp setup/rln_final.zkey $zkeypath/rln_final.zkey +cp $circuit_name\_js/$circuit_name.wasm $zkeypath/circuit.wasm +cp setup/final.zkey $zkeypath/final.zkey shasumcmd="shasum -a 256" @@ -90,11 +87,11 @@ echo -e "Compilation_Time = $(date +%s)" >> $config_path echo -e "" >> $config_path echo -e "[Files]" >> $config_path -echo -e "Wasm = \"rln.wasm\"" >> $config_path -wasm_sha256=$($shasumcmd $zkeypath/rln.wasm | awk '{print $1}') +echo -e "Wasm = \"circuit.wasm\"" >> $config_path +wasm_sha256=$($shasumcmd $zkeypath/circuit.wasm | awk '{print $1}') echo -e "Wasm_SHA256SUM = \"$wasm_sha256\"" >> $config_path -echo -e "Zkey = \"rln_final.zkey\"" >> $config_path -zkey_sha256=$($shasumcmd $zkeypath/rln_final.zkey | awk '{print $1}') +echo -e "Zkey = \"final.zkey\"" >> $config_path +zkey_sha256=$($shasumcmd $zkeypath/final.zkey | awk '{print $1}') echo -e "Zkey_SHA256SUM = \"$zkey_sha256\"" >> $config_path echo -e "Verification_Key = \"verification_key.json\"" >> $config_path vkey_sha256=$($shasumcmd $zkeypath/verification_key.json | awk '{print $1}')