create reverse dfa b4 reassigning name

This commit is contained in:
JernKunpittaya
2023-06-09 00:37:43 -04:00
parent edb77e93db
commit 2ee50e9e65
4 changed files with 115 additions and 14 deletions

View File

@@ -1,4 +1,5 @@
[WIP]
# full_zk_regex
Slide: https://docs.google.com/presentation/d/1nSZdmwDKXjEM6bP6WBYyAWbCgK4cjpm-SXqDAA-MOjE/edit?usp=sharing
@@ -24,27 +25,28 @@ Data flow and related functions
2. Frontend process this regex, by simp_regex = gen_dfa.simplifyRegex
(to deal with escape and change [] into or statements), then let users high light start and end of each group (inclusive) of this simp_regex. Save the value as submatches e.g.
const submatches = [
[5, 30],
[5, 29],
[7, 13],
[15, 25],
[15, 24],
];
3. Match all the strings that satisfies regex as a whole. ฺ(like previously version)
Run
const simp_graph = gen_dfa.simplifyGraph(regex);
const matched_dfa = gen_dfa.findSubstrings(simp_graph, text);
for (const subs of matched_dfa[1]) {
var matched = text.slice(subs[0], subs[1] + 1);} Very important of +1!!
4. Now for each matched, we start extract substring state (we can have multiple matched values!)
const tagged_simp_graph = gen_tagged_dfa.tagged_simplifyGraph(regex, submatches);
var final_graph = gen_tagged_dfa.findMatchStateTagged(tagged_simp_graph);
var allTags = final_graph["tags"];
var transitions = final_graph["transitions"];
5. For Circom, we run

View File

@@ -251,7 +251,7 @@ export function regexToM1(text, submatches) {
}
// New: simplifyRegex and simplify Plus
console.log("herer in Gen: ", simplifyRegex(text));
// console.log("simplify regex in Gen: ", simplifyRegex(text));
let after_plus = simplifyPlus(simplifyRegex(text), submatches);
// console.log("afterrr; ", after_plus["submatches"]);

76
src/gen_rev_dfa.js Normal file
View File

@@ -0,0 +1,76 @@
// given simp_graph (plain dfa), return its reversed version.
export function reverseDFA(simp_graph) {
// simp_q = [
// '0', '1', '2', '3',
// '4', '5', '6', '7',
// '8', '9', '10', '11',
// '12'
// ]
var simp_q = simp_graph["states"];
var simp_transition = simp_graph["transitions"];
var rev_q = [];
// set rev_q to [{f}]
rev_q.push(simp_graph["accepted_states"]);
var rev_q_all = new Set();
var rev_transition = {};
var visited = new Set();
var simp_start = new Set();
simp_start.add(simp_graph["start_state"]);
var rev_accepted = new Set();
var rev_start = simp_graph["accepted_states"];
// inside loop
while (rev_q.length > 0) {
var state_set = rev_q.pop();
var states_id = [];
for (const state of state_set) {
states_id.push(parseInt(state));
}
states_id.sort((a, b) => a - b);
states_id = states_id.toString();
if (visited.has(states_id)) {
continue;
}
var checkStart = states_id.split(",");
for (const state of checkStart) {
if (simp_start.has(state)) {
rev_accepted.add(states_id);
break;
}
}
rev_q_all.add(states_id);
visited.add(states_id);
var alp_dict = {};
for (let from in simp_transition) {
for (let alphabet in simp_transition[from]) {
if (state_set.has(simp_transition[from][alphabet])) {
if (!alp_dict.hasOwnProperty(alphabet)) {
alp_dict[alphabet] = new Set();
}
alp_dict[alphabet].add(from);
}
}
}
for (let alp in alp_dict) {
if (alp_dict[alp].size > 0) {
rev_q.push(alp_dict[alp]);
var alp_string = [];
for (const state of alp_dict[alp]) {
alp_string.push(parseInt(state));
}
alp_string.sort((a, b) => a - b);
alp_string = alp_string.toString();
if (!rev_transition.hasOwnProperty(states_id)) {
rev_transition[states_id] = {};
}
rev_transition[states_id][alp] = alp_string;
}
}
}
return {
states: Array.from(rev_q_all),
start_state: rev_start,
accept_states: rev_accepted,
transitions: rev_transition,
};
}

View File

@@ -1,14 +1,37 @@
import { simplifyGraph, findSubstrings } from "./gen_dfa";
import { simplifyGraph, findSubstrings, simplifyRegex } from "./gen_dfa";
import {
tagged_simplifyGraph,
findMatchStateTagged,
formatForCircom,
} from "./gen_tagged_dfa";
import { reverseDFA } from "./gen_rev_dfa";
function test() {
const text =
"adsfasd DKI: v=12/; d=22; a=//121; d=1; bh=xUqTs2T2FPGCOB52 sdflj";
const regex = "DKI: (([vad]=([12/]+); )+)bh";
const simp_regex = simplifyRegex(regex);
console.log("simp_regex: ", simp_regex);
const submatches = [
[5, 29],
[7, 13],
[15, 24],
];
const simp_graph = simplifyGraph(regex);
console.log("simp graph: ", simp_graph);
const rev_graph = reverseDFA(simp_graph);
console.log("rev graph: ", rev_graph);
const matched_dfa = findSubstrings(simp_graph, text);
for (const subs of matched_dfa[1]) {
var matched = text.slice(subs[0], subs[1] + 1);
console.log("matched: ", matched);
}
// for (const subs of matched_dfa[1]) {
// var matched = text.slice(subs[0], subs[1] + 1);
// console.log("matched: ", matched);
// }
const tagged_simp_graph = tagged_simplifyGraph(regex, submatches);
var final_graph = findMatchStateTagged(tagged_simp_graph);
var allTags = final_graph["tags"];
var transitions = final_graph["transitions"];
console.log("final graph: ", final_graph);
var circom_graph = formatForCircom(final_graph);
var circom_rev_graph = formatForCircom(rev_graph);
}
describe("test backend", function () {
it("should print correctly", function () {