-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix SelectRegexReveal * Add a new test for the SelectRegexReveal template.
- Loading branch information
1 parent
4d34ccd
commit d718290
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { wasm } from "circom_tester"; | ||
import path from "path"; | ||
|
||
|
||
describe("Select Regex Reveal", () => { | ||
jest.setTimeout(10 * 60 * 1000); // 10 minutes | ||
|
||
let circuit: any; | ||
|
||
beforeAll(async () => { | ||
circuit = await wasm( | ||
path.join(__dirname, "./test-circuits/select-regex-reveal-test.circom"), | ||
{ | ||
recompile: true, | ||
include: path.join(__dirname, "../../../node_modules"), | ||
} | ||
); | ||
}); | ||
|
||
it("should reveal the substring with maximum revealed length", async function () { | ||
let input = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
const startIndex = Math.floor(Math.random() * 24); | ||
const revealed = Array.from("zk email").map(char => char.charCodeAt(0)); | ||
for (let i = 0; i < revealed.length; i++) { | ||
input[startIndex + i] = revealed[i]; | ||
} | ||
const witness = await circuit.calculateWitness({ | ||
in: input, | ||
startIndex: startIndex, | ||
}); | ||
await circuit.checkConstraints(witness); | ||
await circuit.assertOut(witness, { out: revealed }) | ||
}); | ||
|
||
it("should reveal the substring with non-maximum revealed length", async function () { | ||
let input = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
const startIndex = 30; | ||
const revealed = Array.from("zk").map(char => char.charCodeAt(0)); | ||
for (let i = 0; i < revealed.length; i++) { | ||
input[startIndex + i] = revealed[i]; | ||
} | ||
const witness = await circuit.calculateWitness({ | ||
in: input, | ||
startIndex: startIndex, | ||
}); | ||
await circuit.checkConstraints(witness); | ||
await circuit.assertOut(witness, { out: revealed.concat([0, 0, 0, 0, 0, 0]) }) | ||
}); | ||
|
||
it("should fail when all zero", async function () { | ||
let input = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
const startIndex = Math.floor(Math.random() * 32); | ||
try { | ||
const witness = await circuit.calculateWitness({ | ||
in: input, | ||
startIndex: startIndex, | ||
}); | ||
await circuit.checkConstraints(witness); | ||
} catch (error) { | ||
expect((error as Error).message).toMatch("Assert Failed"); | ||
} | ||
}); | ||
|
||
it("should fail when startIndex is 0", async function () { | ||
let input = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
const startIndex = 1 + Math.floor(Math.random() * 24); | ||
const revealed = Array.from("zk email").map(char => char.charCodeAt(0)); | ||
for (let i = 0; i < revealed.length; i++) { | ||
input[startIndex + i] = revealed[i]; | ||
} | ||
try { | ||
const witness = await circuit.calculateWitness({ | ||
in: input, | ||
startIndex: startIndex - 1, | ||
}); | ||
await circuit.checkConstraints(witness); | ||
} catch (error) { | ||
expect((error as Error).message).toMatch("Assert Failed"); | ||
} | ||
}); | ||
|
||
it("should fail when startIndex is not before 0", async function () { | ||
let input = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
const startIndex = Math.floor(Math.random() * 23); | ||
const revealed = Array.from("zk email").map(char => char.charCodeAt(0)); | ||
for (let i = 0; i < revealed.length; i++) { | ||
input[startIndex + i] = revealed[i]; | ||
} | ||
try { | ||
const witness = await circuit.calculateWitness({ | ||
in: input, | ||
startIndex: startIndex + 1, | ||
}); | ||
await circuit.checkConstraints(witness); | ||
} catch (error) { | ||
expect((error as Error).message).toMatch("Assert Failed"); | ||
} | ||
}); | ||
|
||
it("should fail when startIndex is larger than max length", async function () { | ||
let input = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
const startIndex = Math.floor(Math.random() * 24); | ||
const revealed = Array.from("zk email").map(char => char.charCodeAt(0)); | ||
for (let i = 0; i < revealed.length; i++) { | ||
input[startIndex + i] = revealed[i]; | ||
} | ||
try { | ||
const witness = await circuit.calculateWitness({ | ||
in: input, | ||
startIndex: 32 | ||
}); | ||
await circuit.checkConstraints(witness); | ||
} catch (error) { | ||
expect((error as Error).message).toMatch("Assert Failed"); | ||
} | ||
}); | ||
}); |
5 changes: 5 additions & 0 deletions
5
packages/circuits/tests/test-circuits/select-regex-reveal-test.circom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pragma circom 2.1.6; | ||
|
||
include "../../utils/regex.circom"; | ||
|
||
component main = SelectRegexReveal(32,8); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters