-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6200617
commit 2219b57
Showing
5 changed files
with
186 additions
and
104 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
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,179 @@ | ||
import { wasm as wasm_tester } from "circom_tester"; | ||
import path from "path"; | ||
|
||
describe("RevealSubstring Circuit", () => { | ||
let circuit: any; | ||
|
||
beforeAll(async () => { | ||
circuit = await wasm_tester( | ||
path.join( | ||
__dirname, | ||
"./test-circuits/reveal-substring-test.circom" | ||
), | ||
{ | ||
recompile: true, | ||
include: path.join(__dirname, "../../../node_modules"), | ||
output: path.join(__dirname, "./compiled-test-circuits"), | ||
} | ||
); | ||
}); | ||
|
||
it("should correctly reveal a substring in the middle of the input", async () => { | ||
const input = { | ||
in: [ | ||
...Array(100) | ||
.fill(0) | ||
.map((_, i) => (i % 255) + 1), | ||
...Array(156).fill(0), | ||
], | ||
substringStartIndex: 50, | ||
substringLength: 5, | ||
}; | ||
const witness = await circuit.calculateWitness(input); | ||
await circuit.checkConstraints(witness); | ||
expect(witness.slice(1, 17)).toEqual([ | ||
51n, | ||
52n, | ||
53n, | ||
54n, | ||
55n, | ||
...Array(11).fill(0n), | ||
]); | ||
}); | ||
|
||
it("should correctly reveal a substring at the start of the input", async () => { | ||
const input = { | ||
in: [ | ||
...Array(100) | ||
.fill(0) | ||
.map((_, i) => (i % 255) + 1), | ||
...Array(156).fill(0), | ||
], | ||
substringStartIndex: 0, | ||
substringLength: 5, | ||
}; | ||
const witness = await circuit.calculateWitness(input); | ||
await circuit.checkConstraints(witness); | ||
expect(witness.slice(1, 17)).toEqual([ | ||
1n, | ||
2n, | ||
3n, | ||
4n, | ||
5n, | ||
...Array(11).fill(0n), | ||
]); | ||
}); | ||
|
||
it("should correctly reveal a substring at the end of the non-zero input", async () => { | ||
const input = { | ||
in: [ | ||
...Array(100) | ||
.fill(0) | ||
.map((_, i) => (i % 255) + 1), | ||
...Array(156).fill(0), | ||
], | ||
substringStartIndex: 95, | ||
substringLength: 5, | ||
}; | ||
const witness = await circuit.calculateWitness(input); | ||
await circuit.checkConstraints(witness); | ||
expect(witness.slice(1, 17)).toEqual([ | ||
96n, | ||
97n, | ||
98n, | ||
99n, | ||
100n, | ||
...Array(11).fill(0n), | ||
]); | ||
}); | ||
|
||
it("should fail when substringStartIndex is out of bounds", async () => { | ||
const input = { | ||
in: Array(256).fill(1), | ||
substringStartIndex: 256, | ||
substringLength: 5, | ||
}; | ||
await expect(circuit.calculateWitness(input)).rejects.toThrow( | ||
"Assert Failed" | ||
); | ||
}); | ||
|
||
it("should fail when substringLength is greater than maxSubstringLength", async () => { | ||
const input = { | ||
in: Array(256).fill(1), | ||
substringStartIndex: 0, | ||
substringLength: 17, | ||
}; | ||
await expect(circuit.calculateWitness(input)).rejects.toThrow( | ||
"Assert Failed" | ||
); | ||
}); | ||
|
||
it("should fail when substringStartIndex + substringLength exceeds maxLength", async () => { | ||
const input = { | ||
in: Array(256).fill(1), | ||
substringStartIndex: 250, | ||
substringLength: 7, | ||
}; | ||
await expect(circuit.calculateWitness(input)).rejects.toThrow( | ||
"Assert Failed" | ||
); | ||
}); | ||
|
||
it("should correctly reveal a substring of length 1", async () => { | ||
const input = { | ||
in: [ | ||
...Array(100) | ||
.fill(0) | ||
.map((_, i) => (i % 255) + 1), | ||
...Array(156).fill(0), | ||
], | ||
substringStartIndex: 50, | ||
substringLength: 1, | ||
}; | ||
const witness = await circuit.calculateWitness(input); | ||
await circuit.checkConstraints(witness); | ||
expect(witness.slice(1, 17)).toEqual([51n, ...Array(15).fill(0n)]); | ||
}); | ||
|
||
it("should correctly reveal the maximum length substring", async () => { | ||
const input = { | ||
in: [ | ||
...Array(100) | ||
.fill(0) | ||
.map((_, i) => (i % 255) + 1), | ||
...Array(156).fill(0), | ||
], | ||
substringStartIndex: 0, | ||
substringLength: 16, | ||
}; | ||
const witness = await circuit.calculateWitness(input); | ||
await circuit.checkConstraints(witness); | ||
expect(witness.slice(1, 17)).toEqual( | ||
Array(16) | ||
.fill(0) | ||
.map((_, i) => BigInt(i + 1)) | ||
); | ||
}); | ||
|
||
it("should pad with zeros when substringLength is less than maxSubstringLength", async () => { | ||
const input = { | ||
in: [ | ||
...Array(100) | ||
.fill(0) | ||
.map((_, i) => (i % 255) + 1), | ||
...Array(156).fill(0), | ||
], | ||
substringStartIndex: 0, | ||
substringLength: 3, | ||
}; | ||
const witness = await circuit.calculateWitness(input); | ||
await circuit.checkConstraints(witness); | ||
expect(witness.slice(1, 17)).toEqual([ | ||
1n, | ||
2n, | ||
3n, | ||
...Array(13).fill(0n), | ||
]); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
packages/circuits/tests/test-circuits/reveal-substring-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 "../../helpers/reveal-substring.circom"; | ||
|
||
component main = RevealSubstring(256, 16); |
5 changes: 0 additions & 5 deletions
5
packages/circuits/tests/test-circuits/substring-match-with-start-index-test.circom
This file was deleted.
Oops, something went wrong.