From b6efeaded3a00bab1f0f56a52438a8f4e185a628 Mon Sep 17 00:00:00 2001 From: SoraSuegami Date: Sun, 12 May 2024 19:36:28 +0900 Subject: [PATCH] add caret tests. --- packages/circom/tests/asterisk.test.js | 2 +- packages/circom/tests/caret.test.js | 549 ++++++++++++++++++ packages/circom/tests/circuits/caret4.json | 2 +- packages/circom/tests/circuits/caret5.json | 2 +- packages/circom/tests/circuits/caret6.json | 8 - .../tests/circuits/test_caret1_regex.circom | 3 + .../tests/circuits/test_caret2_regex.circom | 3 + .../tests/circuits/test_caret3_regex.circom | 3 + .../tests/circuits/test_caret4_regex.circom | 3 + .../tests/circuits/test_caret5_regex.circom | 3 + 10 files changed, 567 insertions(+), 11 deletions(-) create mode 100644 packages/circom/tests/caret.test.js delete mode 100644 packages/circom/tests/circuits/caret6.json create mode 100644 packages/circom/tests/circuits/test_caret1_regex.circom create mode 100644 packages/circom/tests/circuits/test_caret2_regex.circom create mode 100644 packages/circom/tests/circuits/test_caret3_regex.circom create mode 100644 packages/circom/tests/circuits/test_caret4_regex.circom create mode 100644 packages/circom/tests/circuits/test_caret5_regex.circom diff --git a/packages/circom/tests/asterisk.test.js b/packages/circom/tests/asterisk.test.js index 258f4a0..33fcbf9 100644 --- a/packages/circom/tests/asterisk.test.js +++ b/packages/circom/tests/asterisk.test.js @@ -33,7 +33,7 @@ describe("Asterisk Regex", () => { ); writeFileSync( - path.join(__dirname, "./circuits/asterisk1_regex.circom"), + path.join(__dirname, "./circuits/asterisk2_regex.circom"), compiler.genFromDecomposed( readFileSync( path.join(__dirname, "./circuits/asterisk2.json"), diff --git a/packages/circom/tests/caret.test.js b/packages/circom/tests/caret.test.js new file mode 100644 index 0000000..909732d --- /dev/null +++ b/packages/circom/tests/caret.test.js @@ -0,0 +1,549 @@ +import circom_tester from "circom_tester"; +import * as path from "path"; +import { readFileSync, writeFileSync } from "fs"; +import apis from "../../apis/pkg"; +import compiler from "../../compiler/pkg"; +const option = { + include: path.join(__dirname, "../../../node_modules"), +}; +const wasm_tester = circom_tester.wasm; + +jest.setTimeout(600000); +describe("Caret Regex", () => { + let circuit1; + let circuit2; + let circuit3; + let circuit4; + let circuit5; + beforeAll(async () => { + writeFileSync( + path.join(__dirname, "./circuits/caret1_regex.circom"), + compiler.genFromDecomposed( + readFileSync( + path.join(__dirname, "./circuits/caret1.json"), + "utf8" + ), + "Caret1Regex" + ) + ); + circuit1 = await wasm_tester( + path.join(__dirname, "./circuits/test_caret1_regex.circom"), + option + ); + + writeFileSync( + path.join(__dirname, "./circuits/caret2_regex.circom"), + compiler.genFromDecomposed( + readFileSync( + path.join(__dirname, "./circuits/caret2.json"), + "utf8" + ), + "Caret2Regex" + ) + ); + circuit2 = await wasm_tester( + path.join(__dirname, "./circuits/test_caret2_regex.circom"), + option + ); + + writeFileSync( + path.join(__dirname, "./circuits/caret3_regex.circom"), + compiler.genFromDecomposed( + readFileSync( + path.join(__dirname, "./circuits/caret3.json"), + "utf8" + ), + "Caret3Regex" + ) + ); + circuit3 = await wasm_tester( + path.join(__dirname, "./circuits/test_caret3_regex.circom"), + option + ); + + writeFileSync( + path.join(__dirname, "./circuits/caret4_regex.circom"), + compiler.genFromDecomposed( + readFileSync( + path.join(__dirname, "./circuits/caret4.json"), + "utf8" + ), + "Caret4Regex" + ) + ); + circuit4 = await wasm_tester( + path.join(__dirname, "./circuits/test_caret4_regex.circom"), + option + ); + + writeFileSync( + path.join(__dirname, "./circuits/caret5_regex.circom"), + compiler.genFromDecomposed( + readFileSync( + path.join(__dirname, "./circuits/caret5.json"), + "utf8" + ), + "Caret5Regex" + ) + ); + circuit5 = await wasm_tester( + path.join(__dirname, "./circuits/test_caret5_regex.circom"), + option + ); + }); + + it("caret1 valid case 1", async () => { + const inputStr = `a`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit1.calculateWitness(circuitInputs); + await circuit1.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + const prefixIdxes = apis.extractSubstrIdxes( + inputStr, + readFileSync( + path.join(__dirname, "../circuits/common/caret1.json"), + "utf8" + ) + )[0]; + for (let idx = 0; idx < 8; ++idx) { + if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) { + expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]); + } else { + expect(0n).toEqual(witness[2 + idx]); + } + } + }); + + it("caret1 valid case 2", async () => { + const inputStr = `abnjknda`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit1.calculateWitness(circuitInputs); + await circuit1.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + const prefixIdxes = apis.extractSubstrIdxes( + inputStr, + readFileSync( + path.join(__dirname, "../circuits/common/caret1.json"), + "utf8" + ) + )[0]; + for (let idx = 0; idx < 8; ++idx) { + if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) { + expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]); + } else { + expect(0n).toEqual(witness[2 + idx]); + } + } + }); + + it("caret1 invalid case 1", async () => { + const inputStr = `ba`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit1.calculateWitness(circuitInputs); + await circuit1.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + for (let idx = 0; idx < 8; ++idx) { + expect(0n).toEqual(witness[2 + idx]); + } + }); + + it("caret1 invalid case 2", async () => { + const inputStr = `bav`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit1.calculateWitness(circuitInputs); + await circuit1.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + for (let idx = 0; idx < 8; ++idx) { + expect(0n).toEqual(witness[2 + idx]); + } + }); + + + it("caret2 valid case 1", async () => { + const inputStr = `abc`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit2.calculateWitness(circuitInputs); + await circuit2.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + const prefixIdxes = apis.extractSubstrIdxes( + inputStr, + readFileSync( + path.join(__dirname, "../circuits/common/caret2.json"), + "utf8" + ) + )[0]; + for (let idx = 0; idx < 8; ++idx) { + if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) { + expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]); + } else { + expect(0n).toEqual(witness[2 + idx]); + } + } + }); + + it("caret2 valid case 2", async () => { + const inputStr = `bca`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit2.calculateWitness(circuitInputs); + await circuit2.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + const prefixIdxes = apis.extractSubstrIdxes( + inputStr, + readFileSync( + path.join(__dirname, "../circuits/common/caret2.json"), + "utf8" + ) + )[0]; + for (let idx = 0; idx < 8; ++idx) { + if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) { + expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]); + } else { + expect(0n).toEqual(witness[2 + idx]); + } + } + }); + + it("caret2 valid case 3", async () => { + const inputStr = `cab`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit2.calculateWitness(circuitInputs); + await circuit2.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + const prefixIdxes = apis.extractSubstrIdxes( + inputStr, + readFileSync( + path.join(__dirname, "../circuits/common/caret2.json"), + "utf8" + ) + )[0]; + for (let idx = 0; idx < 8; ++idx) { + if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) { + expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]); + } else { + expect(0n).toEqual(witness[2 + idx]); + } + } + }); + + it("caret2 invalid case 1", async () => { + const inputStr = `7abc9mna`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit2.calculateWitness(circuitInputs); + await circuit2.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + for (let idx = 0; idx < 8; ++idx) { + expect(0n).toEqual(witness[2 + idx]); + } + }); + + it("caret3 valid case 1", async () => { + const inputStr = `a7hv8a9b`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit3.calculateWitness(circuitInputs); + await circuit3.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + const prefixIdxes = apis.extractSubstrIdxes( + inputStr, + readFileSync( + path.join(__dirname, "../circuits/common/caret3.json"), + "utf8" + ) + )[0]; + for (let idx = 0; idx < 8; ++idx) { + if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) { + expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]); + } else { + expect(0n).toEqual(witness[2 + idx]); + } + } + }); + + it("caret3 invalid case 1", async () => { + const inputStr = ``; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit3.calculateWitness(circuitInputs); + await circuit3.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + const prefixIdxes = apis.extractSubstrIdxes( + inputStr, + readFileSync( + path.join(__dirname, "../circuits/common/caret3.json"), + "utf8" + ) + )[0]; + for (let idx = 0; idx < 8; ++idx) { + if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) { + expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]); + } else { + expect(0n).toEqual(witness[2 + idx]); + } + } + }); + + it("caret4 valid case 1", async () => { + const inputStr = `abaaabba`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit4.calculateWitness(circuitInputs); + await circuit4.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + const prefixIdxes = apis.extractSubstrIdxes( + inputStr, + readFileSync( + path.join(__dirname, "../circuits/common/caret4.json"), + "utf8" + ) + )[0]; + for (let idx = 0; idx < 8; ++idx) { + if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) { + expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]); + } else { + expect(0n).toEqual(witness[2 + idx]); + } + } + }); + + it("caret4 valid case 2", async () => { + const inputStr = `baab82ab`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit4.calculateWitness(circuitInputs); + await circuit4.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + const prefixIdxes = apis.extractSubstrIdxes( + inputStr, + readFileSync( + path.join(__dirname, "../circuits/common/caret4.json"), + "utf8" + ) + )[0]; + for (let idx = 0; idx < 8; ++idx) { + if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) { + expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]); + } else { + expect(0n).toEqual(witness[2 + idx]); + } + } + }); + + it("caret4 valid case 3", async () => { + const inputStr = `7w1\nabba`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit4.calculateWitness(circuitInputs); + await circuit4.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + const prefixIdxes = apis.extractSubstrIdxes( + inputStr, + readFileSync( + path.join(__dirname, "../circuits/common/caret4.json"), + "utf8" + ) + )[0]; + for (let idx = 0; idx < 8; ++idx) { + if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) { + expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]); + } else { + expect(0n).toEqual(witness[2 + idx]); + } + } + }); + + it("caret4 valid case 4", async () => { + const inputStr = `7w\nbbba9`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit4.calculateWitness(circuitInputs); + await circuit4.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + const prefixIdxes = apis.extractSubstrIdxes( + inputStr, + readFileSync( + path.join(__dirname, "../circuits/common/caret4.json"), + "utf8" + ) + )[0]; + for (let idx = 0; idx < 8; ++idx) { + if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) { + expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]); + } else { + expect(0n).toEqual(witness[2 + idx]); + } + } + }); + + + it("caret4 invalid case 1", async () => { + const inputStr = `7w1nabba`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit4.calculateWitness(circuitInputs); + await circuit4.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + for (let idx = 0; idx < 8; ++idx) { + expect(0n).toEqual(witness[2 + idx]); + } + }); + + it("caret4 invalid case 2", async () => { + const inputStr = `7wnbbba9`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit4.calculateWitness(circuitInputs); + await circuit4.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + for (let idx = 0; idx < 8; ++idx) { + expect(0n).toEqual(witness[2 + idx]); + } + }); + + it("caret5 valid case 1", async () => { + const inputStr = `defabc81`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit5.calculateWitness(circuitInputs); + await circuit5.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + const prefixIdxes = apis.extractSubstrIdxes( + inputStr, + readFileSync( + path.join(__dirname, "../circuits/common/caret5.json"), + "utf8" + ) + )[0]; + for (let idx = 0; idx < 8; ++idx) { + if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) { + expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]); + } else { + expect(0n).toEqual(witness[2 + idx]); + } + } + }); + + it("caret5 valid case 2", async () => { + const inputStr = `91\n9eabc`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit5.calculateWitness(circuitInputs); + await circuit5.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + const prefixIdxes = apis.extractSubstrIdxes( + inputStr, + readFileSync( + path.join(__dirname, "../circuits/common/caret5.json"), + "utf8" + ) + )[0]; + for (let idx = 0; idx < 8; ++idx) { + if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) { + expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]); + } else { + expect(0n).toEqual(witness[2 + idx]); + } + } + }); + + it("caret5 invalid case 1", async () => { + const inputStr = `abc`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit5.calculateWitness(circuitInputs); + await circuit5.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + for (let idx = 0; idx < 8; ++idx) { + expect(0n).toEqual(witness[2 + idx]); + } + }); + + + it("caret5 invalid case 2", async () => { + const inputStr = `a8abc8`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit5.calculateWitness(circuitInputs); + await circuit5.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + for (let idx = 0; idx < 8; ++idx) { + expect(0n).toEqual(witness[2 + idx]); + } + }); + + it("caret5 invalid case 2", async () => { + const inputStr = `71\na81ma`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit5.calculateWitness(circuitInputs); + await circuit5.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + for (let idx = 0; idx < 8; ++idx) { + expect(0n).toEqual(witness[2 + idx]); + } + }); + + + it("caret5 invalid case 3", async () => { + const inputStr = `91n9eabc`; + const paddedStr = apis.padString(inputStr, 8); + const circuitInputs = { + msg: paddedStr, + }; + const witness = await circuit5.calculateWitness(circuitInputs); + await circuit5.checkConstraints(witness); + expect(1n).toEqual(witness[1]); + for (let idx = 0; idx < 8; ++idx) { + expect(0n).toEqual(witness[2 + idx]); + } + }); +}); diff --git a/packages/circom/tests/circuits/caret4.json b/packages/circom/tests/circuits/caret4.json index ced8e74..5ee7493 100644 --- a/packages/circom/tests/circuits/caret4.json +++ b/packages/circom/tests/circuits/caret4.json @@ -2,7 +2,7 @@ "parts": [ { "is_public": true, - "regex_def": "(\n|^)a+" + "regex_def": "(\n|^)(a|b)+" } ] } \ No newline at end of file diff --git a/packages/circom/tests/circuits/caret5.json b/packages/circom/tests/circuits/caret5.json index ea431f3..ccb614e 100644 --- a/packages/circom/tests/circuits/caret5.json +++ b/packages/circom/tests/circuits/caret5.json @@ -2,7 +2,7 @@ "parts": [ { "is_public": true, - "regex_def": "(\n|^).+" + "regex_def": "(\n|^)[^abc]+" } ] } \ No newline at end of file diff --git a/packages/circom/tests/circuits/caret6.json b/packages/circom/tests/circuits/caret6.json deleted file mode 100644 index ccb614e..0000000 --- a/packages/circom/tests/circuits/caret6.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "parts": [ - { - "is_public": true, - "regex_def": "(\n|^)[^abc]+" - } - ] -} \ No newline at end of file diff --git a/packages/circom/tests/circuits/test_caret1_regex.circom b/packages/circom/tests/circuits/test_caret1_regex.circom new file mode 100644 index 0000000..75c867a --- /dev/null +++ b/packages/circom/tests/circuits/test_caret1_regex.circom @@ -0,0 +1,3 @@ +include "./caret1_regex.circom"; + +component main = Caret1Regex(8); \ No newline at end of file diff --git a/packages/circom/tests/circuits/test_caret2_regex.circom b/packages/circom/tests/circuits/test_caret2_regex.circom new file mode 100644 index 0000000..52d18be --- /dev/null +++ b/packages/circom/tests/circuits/test_caret2_regex.circom @@ -0,0 +1,3 @@ +include "./caret2_regex.circom"; + +component main = Caret2Regex(8); \ No newline at end of file diff --git a/packages/circom/tests/circuits/test_caret3_regex.circom b/packages/circom/tests/circuits/test_caret3_regex.circom new file mode 100644 index 0000000..8f4a6de --- /dev/null +++ b/packages/circom/tests/circuits/test_caret3_regex.circom @@ -0,0 +1,3 @@ +include "./caret3_regex.circom"; + +component main = Caret3Regex(8); \ No newline at end of file diff --git a/packages/circom/tests/circuits/test_caret4_regex.circom b/packages/circom/tests/circuits/test_caret4_regex.circom new file mode 100644 index 0000000..71118e7 --- /dev/null +++ b/packages/circom/tests/circuits/test_caret4_regex.circom @@ -0,0 +1,3 @@ +include "./caret4_regex.circom"; + +component main = Caret4Regex(8); \ No newline at end of file diff --git a/packages/circom/tests/circuits/test_caret5_regex.circom b/packages/circom/tests/circuits/test_caret5_regex.circom new file mode 100644 index 0000000..695afc4 --- /dev/null +++ b/packages/circom/tests/circuits/test_caret5_regex.circom @@ -0,0 +1,3 @@ +include "./caret5_regex.circom"; + +component main = Caret5Regex(8); \ No newline at end of file