Skip to content

Commit

Permalink
feat: substring matching (#17)
Browse files Browse the repository at this point in the history
* add optimised
substring search

* resolve random input bug using poseidon hash

* import circomlib and remove circuits

* add array and hash circuits

* add search

* add js poseidon

* update tests

* add search tests

* refactor tests

* change docstyle

* add wrong key test

* add comment and change input file name

* separate utils circuits and tests
  • Loading branch information
lonerapier authored Aug 23, 2024
1 parent 7bb4afc commit 71b4470
Show file tree
Hide file tree
Showing 22 changed files with 2,144 additions and 676 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPARK
> Succinct Parser Attestation for Reconciliation of Knowledge
> Succinct Parser Attestation for Reconciliation of Knowledge
## Repo Structure
The repository is currently new and being organized as follows:
Expand All @@ -26,7 +26,7 @@ npm install -g snarkjs
```

### Circomkit
You will need `yarn` on your system (brew, or apt-get or something).
You will need `yarn` on your system (brew, or apt-get or something).
Then run: `npm install` to get everything else.

#### Commands
Expand All @@ -41,7 +41,7 @@ For example, to compile the extractor, you can:
```
npx circomkit compile extract
```
Then you can do
Then you can do
```
npx circomkit witness extract witness
```
Expand Down Expand Up @@ -69,6 +69,8 @@ To run specific tests, use the `-g` flag for `mocha`, e.g., to run any proof des
npx mocha -g State
```

> [!NOTE]
> Currently [search](./circuits/search.circom) circuit isn't working with circomkit, so you might have to compile using circom: `circom circuits/main/search.circom --r1cs --wasm -l node_modules/ -o build/search/`
## (MOSTLY DEPRECATED DUE TO CIRCOMKIT) Running an example
```
Expand Down
5 changes: 4 additions & 1 deletion circomkit.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"version": "2.1.9",
"proofSystem": "groth16",
"curve": "bn128"
"curve": "bn128",
"includes": [
"node_modules"
]
}
8 changes: 8 additions & 0 deletions circuits.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,13 @@
21,
3
]
},
"search": {
"file": "search",
"template": "SubstringMatch",
"params": [
787,
10
]
}
}
6 changes: 3 additions & 3 deletions circuits/extract.circom
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pragma circom 2.1.9;

include "utils.circom";
include "./utils/bytes.circom";
include "parser.circom";

template Extract(DATA_BYTES, MAX_STACK_HEIGHT) {
Expand All @@ -10,7 +10,7 @@ template Extract(DATA_BYTES, MAX_STACK_HEIGHT) {

//--------------------------------------------------------------------------------------------//
//-CONSTRAINTS--------------------------------------------------------------------------------//
//--------------------------------------------------------------------------------------------//
//--------------------------------------------------------------------------------------------//
component dataASCII = ASCII(DATA_BYTES);
dataASCII.in <== data;
//--------------------------------------------------------------------------------------------//
Expand Down Expand Up @@ -50,4 +50,4 @@ template Extract(DATA_BYTES, MAX_STACK_HEIGHT) {
log("State[", DATA_BYTES, "].parsing_string", "= ", State[DATA_BYTES-1].next_parsing_string);
log("State[", DATA_BYTES, "].parsing_number", "= ", State[DATA_BYTES-1].next_parsing_number);
log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
}
}
34 changes: 18 additions & 16 deletions circuits/parser.circom
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ Tests for this module are located in the files: `circuits/test/parser/*.test.ts

pragma circom 2.1.9;

include "utils.circom";
include "./utils/array.circom";
include "./utils/bytes.circom";
include "./utils/operators.circom";
include "language.circom";

/*
Expand All @@ -46,16 +48,16 @@ This template is for updating the state of the parser from a current state to a
template StateUpdate(MAX_STACK_HEIGHT) {
signal input byte; // TODO: Does this need to be constrained within here?

signal input stack[MAX_STACK_HEIGHT][2];
signal input stack[MAX_STACK_HEIGHT][2];
signal input parsing_string;
signal input parsing_number;

signal output next_stack[MAX_STACK_HEIGHT][2];
signal output next_parsing_string;
signal output next_parsing_number;

component Syntax = Syntax();
component Command = Command();
component Command = Command();

//--------------------------------------------------------------------------------------------//
// Break down what was read
Expand Down Expand Up @@ -117,9 +119,9 @@ template StateUpdate(MAX_STACK_HEIGHT) {
readQuoteInstruction.array <== Command.QUOTE;

component Instruction = GenericArrayAdd(3,8);
Instruction.arrays <== [readStartBraceInstruction.out, readEndBraceInstruction.out,
Instruction.arrays <== [readStartBraceInstruction.out, readEndBraceInstruction.out,
readStartBracketInstruction.out, readEndBracketInstruction.out,
readColonInstruction.out, readCommaInstruction.out,
readColonInstruction.out, readCommaInstruction.out,
readNumberInstruction.out, readQuoteInstruction.out];
//--------------------------------------------------------------------------------------------//
// Apply state changing data
Expand All @@ -143,7 +145,7 @@ template StateUpdate(MAX_STACK_HEIGHT) {
newStack.readEndBracket <== readEndBracket.out;
newStack.readColon <== readColon.out;
newStack.readComma <== readComma.out;
// * set all the next state of the parser *
// * set all the next state of the parser *
next_stack <== newStack.next_stack;
next_parsing_string <== parsing_string + mulMaskAndOut.out[1];
next_parsing_number <== parsing_number + mulMaskAndOut.out[2];
Expand Down Expand Up @@ -175,9 +177,9 @@ template StateToMask(n) {
signal input parsing_string;
signal input parsing_number;
signal output out[3];


// `read_write_value`can change: IF NOT `parsing_string`

// `read_write_value`can change: IF NOT `parsing_string`
out[0] <== (1 - parsing_string);

// `parsing_string` can change:
Expand All @@ -192,12 +194,12 @@ template StateToMask(n) {
Above is the binary value for each if is individually enabled
This is a total of 2^4 states
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
[0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0];
and the above is what we want to set `next_parsing_number` to given those
[0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0];
and the above is what we want to set `next_parsing_number` to given those
possible.
Below is an optimized version that could instead be done with a `Switch`
*/
signal parsingNumberReadDelimeter <== parsing_number * (readDelimeter);
signal parsingNumberReadDelimeter <== parsing_number * (readDelimeter);
signal readNumberNotParsingNumber <== (1 - parsing_number) * readNumber;
signal notParsingStringAndParsingNumberReadDelimeterOrReadNumberNotParsingNumber <== (1 - parsing_string) * (parsingNumberReadDelimeter + readNumberNotParsingNumber);
// 10 above ^^^^^^^^^^^^^^^^^ 4 above ^^^^^^^^^^^^^^^^^^
Expand All @@ -219,7 +221,7 @@ This template is for getting the values at the top of the stack as well as the p
# Outputs:
- `value[2]`: the value at the top of the stack
- `pointer` : the pointer for the top of stack index
- `pointer` : the pointer for the top of stack index
*/
template GetTopOfStack(n) {
signal input stack[n][2];
Expand Down Expand Up @@ -259,7 +261,7 @@ This template is for updating the stack given the current stack and the byte we
- `readComma` : a bool flag that indicates whether the byte value read was a comma `,`.
# Outputs:
- `next_stack[n][2]`: the next stack of the parser.
- `next_stack[n][2]`: the next stack of the parser.
*/
template RewriteStack(n) {
assert(n < 2**8);
Expand All @@ -273,7 +275,7 @@ template RewriteStack(n) {
signal input readComma;

signal output next_stack[n][2];

//--------------------------------------------------------------------------------------------//
// * scan value on top of stack *
component topOfStack = GetTopOfStack(n);
Expand Down Expand Up @@ -302,7 +304,7 @@ template RewriteStack(n) {
isPush.in <== [readStartBrace + readStartBracket, 1];
component isPop = IsEqual();
isPop.in <== [readEndBrace + readEndBracket, 1];
// * set an indicator array for where we are pushing to or popping from*
// * set an indicator array for where we are pushing to or popping from*
component indicator[n];
for(var i = 0; i < n; i++) {
// Points
Expand Down
Loading

0 comments on commit 71b4470

Please sign in to comment.