Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 37c6d31

Browse files
committedJul 21, 2024
Update README
1 parent 08b9a84 commit 37c6d31

File tree

4 files changed

+109
-28
lines changed

4 files changed

+109
-28
lines changed
 

‎README.md

+109-28
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,73 @@ The innovation was originally developed for [zk-email-verify](https://github.com
1010

1111
By leveraging ZK Regex, developers can securely verify the presence of specific text patterns without compromising data privacy, opening the door to a wide range of use cases such as secure messaging, confidential document validation, and more.
1212

13-
## How to use
13+
## How to install the CLI
1414

15-
1. Start by providing a raw or parsed (expanded) regex pattern, then execute the following command:
15+
```sh
16+
npm install -g o1js-regex-cli
17+
```
18+
19+
## How to confirm successful installation:
20+
21+
```sh
22+
zk-regex --version
23+
```
24+
25+
or
26+
27+
```sh
28+
zkr --version
29+
```
30+
31+
### How to update the ZK Regex CLI
32+
33+
```sh
34+
npm update -g o1js-regex-cli
35+
```
36+
37+
## How to display help
38+
39+
```sh
40+
zk-regex --help
41+
```
42+
43+
![alt text](./images/cli-help.png)
44+
45+
## How to use CLI to write or append o1js regex circuits directly into a TS File
46+
47+
Start by providing a raw regex pattern and specifying the `--filePath` option:
48+
49+
```sh
50+
zk-regex <regexPattern> [options] --filePath <path> --functionName <name>
51+
```
52+
53+
- **Note:** When using the `--filePath` option, you must also provide the `--functionName` option. This ensures that the compiled regex circuit is given a specific function name within the TypeScript file.
54+
55+
### File Handling:
56+
57+
- If the _file_ specified by `--filePath` does not exist:
58+
- The CLI will create the file.
59+
- An import statement for the required types (`Bool`, `Field`, `UInt8` from `o1js`) will be added at the top of the file.
60+
- The compiled regex circuit will be written to the file.
61+
- If the file specified by --filePath exists:
62+
- The compiled regex circuit will be appended to the existing file.
63+
64+
### Example Commands:
65+
66+
```sh
67+
zk-regex '(a|b)c' --functionName myRegexFunction --filePath ./src/regexCircuits.ts
68+
```
69+
70+
This command will append the compiled regex circuit for the pattern `(a|b)c `to `./src/regexCircuits.ts` with the function name `myRegexFunction`.
71+
72+
## How to use manually
73+
74+
1. Provide a raw or parsed (expanded) regex pattern along with the desired options as described in the CLI documentation, then execute the following command:
1675

1776
```sh
18-
npm run zk-regex '<regexPattern>'
77+
zk-regex <regexPattern> [options]
1978
```
2079

21-
**NOTE:** `countEnabled` and `substringEnabled` arguments are set to **false** by default.
22-
2380
2. In your TypeScript file, import the necessary modules at the top:
2481

2582
```typescript
@@ -42,34 +99,58 @@ Below is a screenshot depicting how a ZK Regex circuit in `o1js` appears upon su
4299

43100
## Compiler Options
44101

45-
### Arguments
102+
1. `--count`: A boolean option that is set to **false** by default.
103+
104+
- If `--count` is not used, the compiled circuit will have a **Bool** output, signifying whether the input matches the regex pattern.
105+
106+
- Example: For a regex `Hello [a-z]+!`, the input `Hello world!` will return `Bool(true)`, whereas the input `Hello there` will return `Bool(false)` because the exclamation mark `!` is missing at the end of the input.
107+
108+
- If `--count` is used, the compiled circuit will return a **Field** output, signifying the number of matching patterns according to the regex.
109+
- Example: For a regex `[0-9]+`, the input `I saw 279 ants and 1 elephant` will return `Field(4)` because it contains 4 numbers. Whereas an input like `Cheese` will return `Field(0)` because there are no digits in the input, which can also invalidate the input like `Bool(false)`.
110+
111+
2. For a defined regex, there are two alternative variadic options to reveal its substring patterns: `--revealTransitions` and `--revealSubpatterns`.
112+
113+
- **Warning** Only one of these options can be used at a time. Using both will result in an error, as they are mutually exclusive choices for revealing patterns.
114+
115+
- If neither reveal option is used, the compiled circuit will return either `Field` or `Bool` output based on the `--count` option. This means the regex circuit will only validate the input without revealing any sub-patterns.
116+
117+
- If one of the reveal options is used, the compiled circuit will reveal parts of the input based on the specified regex sub-pattern(s) or the provided min-DFA transitions.
118+
119+
- **Notes:**
120+
121+
- The reveal substring feature is particularly useful for occurrence matching with the `+` (one or more) operator. This feature allows for meaningful use cases in various circuits by fetching the matching values. For instance, it can be used to match and reveal a series of base64 characters or to extract an abstract number from a match like `[0-9]+`.
122+
- While matching specific patterns such as `white` or `Human` might seem less practical, these examples are provided to illustrate how to effectively use the reveal options.
123+
124+
- **Sub-patterns:** `--revealSubpatterns` or `-s`
125+
126+
- Sub-patterns refer to specific parts of the regex input that are parsed to extract and reveal their respective transitions.
127+
128+
- **Warning:** The compiler will throw an error if a sub-pattern is not included in the entire regex or if the specified sub-pattern does not match the correct type.
129+
130+
- **Example:** For the command `zk-regex '(Human|Robot)+' --revealSubpatterns Human`, `Human` is a valid sub-pattern within the regex `(Human|Robot)+`. However, the compiler will raise an error if the sub-pattern specified is not present in the main regex or if it does not match, such as using `Humanoid` or `human` instead of `Human`.
131+
132+
- Additionally, sub-patterns can be specified separately for more flexibility. For example, using `zk-regex '(Human|Robot)+' --revealSubpatterns Human Robot` allows you to reveal specific sub-patterns, as opposed to revealing everything attached with `zk-regex '(Human|Robot)' --revealSubpatterns '(Human|Robot)'`.
46133

47-
1. `countEnabled`: A boolean argument that is set to **false** by default.
134+
- When using the `--revealSubpatterns` option, the output of the circuit includes a key to respect the scattering of revealed sub-patterns.
48135

49-
- If `countEnabled` is **false**, the compiled circuit will have a **Bool** output, signifying whether the input matches the regex pattern.
136+
- In practice, if you reveal a single sub-pattern, you can access it with `reveal[0]` within the zkApp when using the regex circuit.
50137

51-
- Example: For a regex `Hello [a-z]+!`, the input `Hello world!` will return `Bool(true)`, whereas the input `Hello there` will return `Bool(false)` because the exclamation mark `!` is missing at the end of the input.
138+
- **Transitions:** `--revealTransitions` or `-t`
52139

53-
- If `countEnabled` is **true**, the compiled circuit will return a **Field** output, signifying the number of matching patterns according to the regex.
54-
- Example: For a regex `([0-9])+`, the input `I saw 279 ants and 1 elephant` will return `Field(4)` because it contains 4 numbers. Whereas an input like `Cheese` will return `Field(0)` because there are no digits in the input, which can also invalidate the input like `Bool(false)`.
140+
- Transitions represent the underlying raw inputs used to reveal substrings in a regex match.
55141

56-
2. `substringEnabled`: A boolean argument that is set to **false** by default.
142+
- The `--revealTransitions` option in the CLI allows you to specify a variadic list of transition pairs in a special format.
57143

58-
- If `substringEnabled` is **false**, the compiled circuit will only return `Field` or `Bool` output based on `countEnabled` arguments. In other words, the regex circuit will solely validate given an input.
144+
- **Example Command:** `zk-regex [a-z]+ --revealTransitions [0,1],[1,1]`
59145

60-
- If `substringEnabled` is **true**, the compiled circuit will reveal parts of the input following regex sub-pattern(s) or directly following the given min-DFA transitions.
146+
- **Warning:**
61147

62-
- **Sub-patterns**:
148+
- The compiler will issue a warning if you provide a transition array that contains pairs not included in the full regex transition.
149+
- The warning will occur if the format of the transitions does not match `[number, number],[number, number],...`.
63150

64-
- Sub-patterns are parts of the regex input that get parsed, extracting the respective transitions to be revealed.
65-
- **NOTE:** The compiler will complain if a part regex is not included in the entire regex.
66-
- **Example:** `npm run zk-regex '(black|white)+' '["black"]'` black is included in the entire regex `(black|white)+` but it will throw an error if a part regex is not included or of the wrong type such as `["yellow"]` or `black`.
67-
- Following the color example, it is also possible to separate part regex for better flexibility such as giving `["black", "white"]` as opposed to `["(black|white)"]` that reveals everything attached.
151+
- For greater flexibility, similar to subpatterns, you can specify multiple series of transition pairs, such as `[0,1],[1,1] [2,3],[3,4]`.
68152

69-
- **Transitions**:
70-
- Transitions are the raw inputs behind revealing substring for an input.
71-
- **Warning:** Opposed to sub-patterns, the compiler won't complain if you give it any transition array, it will only complain if the type of the transitions is different than `[number, number][][]`.
72-
- Transitions are more straightforward if you know what you are doing following the DFA visualization; otherwise, use sub-patterns.
153+
- Working with transitions can be more straightforward if you understand the DFA (Deterministic Finite Automaton) visualization. Otherwise, it is recommended to use sub-patterns for ease of use.
73154

74155
### Enabling Substring Walkthrough
75156

@@ -80,17 +161,17 @@ Below is a screenshot depicting how a ZK Regex circuit in `o1js` appears upon su
80161

81162
- In this example, we want to reveal the dynamic substring knowing that it's abstract given the repetition pattern.
82163

83-
- Entering the **sub-pattern** is quite straightforward, and giving the input `'["[A-Z][a-z]+"]'` solves the issue easily.
164+
- Entering the **sub-pattern** is quite straightforward, and giving the input `[A-Z][a-z]+` solves the issue easily.
84165

85-
- Command: `npm run zk-regex 'name: [A-Z][a-z]+' '["[A-Z][a-z]+"]'`
166+
- Command: `zk-regex 'name: [A-Z][a-z]+' -s [A-Z][a-z]+`
86167

87168
- As with transitions, you should first use this amazing [min-DFA visualizer](https://zkregex.com/min_dfa?regex=bmFtZTogKEF8QnxDfER8RXxGfEd8SHxJfEp8S3xMfE18TnxPfFB8UXxSfFN8VHxVfFZ8V3xYfFl8WikoYXxifGN8ZHxlfGZ8Z3xofGl8anxrfGx8bXxufG98cHxxfHJ8c3x0fHV8dnx3fHh8eXx6KSs=)
88169
![alt text](./images/min-dfa-example.png)
89170

90171
- You can see in the graph above that the transitions containing the inputs `[A-Z]` and `[a-z]+` are `8 to 1`, `1 to 2`, and `2 to 2`.
91-
- Hence the transitions input `[[[8,1],[1,2],[2,2]]]` also reveals the name starting with a capital letter.
92-
- Command: `npm run zk-regex 'name: [A-Z][a-z]+' '[[[8,1],[1,2],[2,2]]]'`
93-
- **NOTE:** Separation is also possible by reorganizing the transitions sub-array, such as `[[[8,1]],[[1,2],[2,2]]]`, which reveals the first capital letter and the rest lowercase letters separately.
172+
- Hence the transitions input `[8,1],[1,2],[2,2]` also reveals the name starting with a capital letter.
173+
- Command: `zk-regex 'name: [A-Z][a-z]+' -t [8,1],[1,2],[2,2]`
174+
- **NOTE:** Separation is also possible by reorganizing the transitions sub-array, such as `[8,1] [1,2],[2,2]`, which reveals the first capital letter and the rest lowercase letters separately.
94175

95176
### Options Graph
96177

‎images/alpha-low-zk-regex.png

13.5 KB
Loading

‎images/cli-help.png

59.9 KB
Loading

‎images/regex-command-graph.png

12.4 KB
Loading

0 commit comments

Comments
 (0)
Failed to load comments.