-
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.
test: added tests for substring-match template
- Loading branch information
1 parent
d85b22e
commit b9008c4
Showing
3 changed files
with
121 additions
and
3 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,102 @@ | ||
import { wasm as wasm_tester } from 'circom_tester'; | ||
import path from 'path'; | ||
|
||
describe('SubstringMatch', () => { | ||
let circuit: any; | ||
|
||
beforeAll(async () => { | ||
circuit = await wasm_tester( | ||
path.join(__dirname, './test-circuits/substring-match-test.circom'), | ||
{ | ||
recompile: true, | ||
include: path.join(__dirname, '../../../node_modules'), | ||
output: path.join(__dirname, './compiled-test-circuits'), | ||
} | ||
); | ||
}); | ||
|
||
const padArray = (arr: number[], length: number) => [ | ||
...arr, | ||
...Array(length - arr.length).fill(0) | ||
]; | ||
|
||
it('should correctly match a substring', async () => { | ||
const input = { | ||
in: padArray([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100], 32), // "hello world" | ||
startIndex: 6, | ||
revealedString: padArray([119, 111, 114, 108, 100], 16), // "world" | ||
r: 69, // A prime number for the random linear combination | ||
}; | ||
|
||
const witness = await circuit.calculateWitness(input); | ||
await circuit.checkConstraints(witness); | ||
|
||
await circuit.assertOut(witness, { | ||
isValid: 1, | ||
}); | ||
}); | ||
|
||
it('should fail when substring does not match', async () => { | ||
const input = { | ||
in: padArray([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100], 32), // "hello world" | ||
startIndex: 6, | ||
revealedString: padArray([119, 111, 114, 108, 107], 16), // "worlk" (last character different) | ||
r: 69, | ||
}; | ||
|
||
const witness = await circuit.calculateWitness(input); | ||
await circuit.checkConstraints(witness); | ||
|
||
await circuit.assertOut(witness, { | ||
isValid: 0, | ||
}); | ||
}); | ||
|
||
it('should handle matching at the beginning of the string', async () => { | ||
const input = { | ||
in: padArray([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100], 32), // "hello world" | ||
startIndex: 0, | ||
revealedString: padArray([104, 101, 108, 108, 111], 16), // "hello" | ||
r: 69, | ||
}; | ||
|
||
const witness = await circuit.calculateWitness(input); | ||
await circuit.checkConstraints(witness); | ||
|
||
await circuit.assertOut(witness, { | ||
isValid: 1, | ||
}); | ||
}); | ||
|
||
it('should handle matching at the end of the string', async () => { | ||
const input = { | ||
in: padArray([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100], 32), // "hello world" | ||
startIndex: 7, | ||
revealedString: padArray([111, 114, 108, 100], 16), // "orld" | ||
r: 69, | ||
}; | ||
|
||
const witness = await circuit.calculateWitness(input); | ||
await circuit.checkConstraints(witness); | ||
|
||
await circuit.assertOut(witness, { | ||
isValid: 1, | ||
}); | ||
}); | ||
|
||
it('should fail when startIndex is out of bounds', async () => { | ||
const input = { | ||
in: padArray([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100], 32), // "hello world" | ||
startIndex: 32, // Out of bounds (valid indices are 0-31) | ||
revealedString: padArray([100], 16), // "d" | ||
r: 69, | ||
}; | ||
|
||
const witness = await circuit.calculateWitness(input); | ||
await circuit.checkConstraints(witness); | ||
|
||
await circuit.assertOut(witness, { | ||
isValid: 0, | ||
}); | ||
}); | ||
}); |
5 changes: 5 additions & 0 deletions
5
packages/circuits/tests/test-circuits/substring-match-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/substring-match.circom"; | ||
|
||
component main = SubstringMatch(32, 16); |