Skip to content

Commit b63c7d3

Browse files
committed
chore: add documents
1 parent 5ef509d commit b63c7d3

File tree

6 files changed

+53
-7
lines changed

6 files changed

+53
-7
lines changed

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ edition = "2021"
77

88
[dependencies]
99
halo2_proofs = { git = "https://github.com/scroll-tech/halo2", branch="develop" }
10-
regex-automata = { path = "../../alannotnerd/regex/regex-automata" }
10+
regex-automata = { git = "https://github.com/alannotnerd/regex" }

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# zkregex(WIP)
2+
3+
## Overview
4+
This repository showcases a Proof of Concept (POC) demonstrating the use of halo2 for validating regular expression (regex) matching processes. By transforming regex into Deterministic Finite Automata (DFA) using the regex_automata crate, this project demonstrates an innovative approach to regex matching verification.
5+
6+
## Limitation
7+
This crate is WIP, and has some limitations:
8+
9+
- Support only ASCII code
10+
- `Unanchored` mode only
11+
- Only support full text matching
12+
13+
## Getting Start
14+
### Prerequisites
15+
- rustc (only tested in latest nightly version)
16+
17+
### Installation
18+
1. Clone the repository
19+
```bash
20+
git clone https://github.com/alannotnerd/zkregex
21+
```
22+
23+
2. Navigate to the project directory
24+
```bash
25+
cd zkregex
26+
```
27+
28+
3. Build the project
29+
```bash
30+
cargo run --release
31+
```
32+
Note: This runs with `MockProver` which doen't create a real proof.
33+
34+

src/constants.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
pub(crate) const MAX_STATE: u64 = 1000u64;
1+
//TODO(alan): It should be corresponding to the DFA budler, which still needs effort.
2+
pub(crate) const MAX_STATE: u64 = 65536u64;

src/dfa.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use regex_automata::{
1010

1111
use crate::regex::DFADef;
1212

13+
/// Convert the regular expressions into a DFA. It only supports the `Unanchored` mode, which means
14+
/// the position matchers, like '^', '&', will lead to an error.
1315
pub fn gen_regex_dfa_def(regex: &str) -> DFADef {
1416
let dfa = Builder::new()
1517
.configure(
@@ -48,6 +50,7 @@ pub fn gen_regex_dfa_def(regex: &str) -> DFADef {
4850
}
4951
}
5052

53+
/// A helper function that generates the matching process traces step by step.
5154
pub fn gen_traces(regex: &str, input: &[u8]) -> Vec<(u16, u8, u64)> {
5255
let dfa = Builder::new()
5356
.configure(
@@ -72,7 +75,7 @@ pub fn gen_traces(regex: &str, input: &[u8]) -> Vec<(u16, u8, u64)> {
7275
traces.push((0, 0, state.as_u64()));
7376
state = dfa.next_eoi_state(state);
7477
traces.push((0, 0, state.as_u64()));
75-
78+
7679
traces
7780
}
7881

src/regex.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::{
2-
collections::{BTreeSet, HashMap, HashSet},
3-
marker::PhantomData,
4-
};
1+
use std::{collections::BTreeSet, marker::PhantomData};
52

63
use halo2_proofs::{
74
circuit::{Layouter, Value},
@@ -37,6 +34,8 @@ pub struct DFADef {
3734
pub accepted_state_val: u64,
3835
}
3936

37+
/// Table that contains all valid transitions. It doesn't contains travil transitions, eg, from
38+
/// `DEAD` state to `DEAD` state.
4039
#[derive(Clone)]
4140
pub struct DFATable<F> {
4241
pub class_id: TableColumn,
@@ -108,15 +107,22 @@ impl<F: PrimeField> DFATable<F> {
108107

109108
/// Table layout
110109
/// | character | class_id | state | q_state_check | q_enable |
110+
///
111+
/// NOTE: We should also add constraints for ``(character, class_id)`, which is trivial.`
111112
112113
#[derive(Clone)]
113114
pub struct RegexCircuitConfig<F> {
115+
/// Only enabled at the begin and the end row. In this stage, we need to check the state equals
116+
/// to init state or accepted state
114117
q_state_check: Selector,
115118
state: Column<Advice>,
116119
character: Column<Advice>,
117120
class_id: Column<Advice>,
121+
/// Points out the expected matching state.
118122
expected_state: Column<Advice>,
123+
/// Deactive in padding rows, otherwise active.
119124
q_enable: Selector,
125+
/// Transition table
120126
dfa_table: DFATable<F>,
121127
}
122128

0 commit comments

Comments
 (0)