- Added coloring option to graph saving.
[oweals/gnunet.git] / src / regex / regex.c
index d697aee89fd8f1e8974caf4c2261baaef8a56482..c8b8ad3faedb680268ff3256c91ea8b868a9a449 100644 (file)
 #include "gnunet_container_lib.h"
 #include "gnunet_crypto_lib.h"
 #include "gnunet_regex_lib.h"
-#include "regex.h"
+#include "regex_internal.h"
+
+
+/**
+ * Constant for how many bits the initial string regex should have.
+ */
+#define INITIAL_BITS 8
 
-#define initial_bits 10
 
 /**
  * Context that contains an id counter for states and transitions as well as a
@@ -46,11 +51,6 @@ struct GNUNET_REGEX_Context
    */
   unsigned int transition_id;
 
-  /**
-   * Unique SCC (Strongly Connected Component) id.
-   */
-  unsigned int scc_id;
-
   /**
    * DLL of GNUNET_REGEX_Automaton's used as a stack.
    */
@@ -62,206 +62,6 @@ struct GNUNET_REGEX_Context
   struct GNUNET_REGEX_Automaton *stack_tail;
 };
 
-/**
- * Type of an automaton.
- */
-enum GNUNET_REGEX_automaton_type
-{
-  NFA,
-  DFA
-};
-
-/**
- * Automaton representation.
- */
-struct GNUNET_REGEX_Automaton
-{
-  /**
-   * This is a linked list.
-   */
-  struct GNUNET_REGEX_Automaton *prev;
-
-  /**
-   * This is a linked list.
-   */
-  struct GNUNET_REGEX_Automaton *next;
-
-  /**
-   * First state of the automaton. This is mainly used for constructing an NFA,
-   * where each NFA itself consists of one or more NFAs linked together.
-   */
-  struct GNUNET_REGEX_State *start;
-
-  /**
-   * End state of the automaton.
-   */
-  struct GNUNET_REGEX_State *end;
-
-  /**
-   * Number of states in the automaton.
-   */
-  unsigned int state_count;
-
-  /**
-   * DLL of states.
-   */
-  struct GNUNET_REGEX_State *states_head;
-
-  /**
-   * DLL of states
-   */
-  struct GNUNET_REGEX_State *states_tail;
-
-  /**
-   * Type of the automaton.
-   */
-  enum GNUNET_REGEX_automaton_type type;
-
-  /**
-   * Regex
-   */
-  char *regex;
-
-  /**
-   * Computed regex (result of RX->NFA->DFA->RX)
-   */
-  char *computed_regex;
-};
-
-/**
- * A state. Can be used in DFA and NFA automatons.
- */
-struct GNUNET_REGEX_State
-{
-  /**
-   * This is a linked list.
-   */
-  struct GNUNET_REGEX_State *prev;
-
-  /**
-   * This is a linked list.
-   */
-  struct GNUNET_REGEX_State *next;
-
-  /**
-   * Unique state id.
-   */
-  unsigned int id;
-
-  /**
-   * If this is an accepting state or not.
-   */
-  int accepting;
-
-  /**
-   * Marking of the state. This is used for marking all visited states when
-   * traversing all states of an automaton and for cases where the state id
-   * cannot be used (dfa minimization).
-   */
-  int marked;
-
-  /**
-   * Marking the state as contained. This is used for checking, if the state is
-   * contained in a set in constant time
-   */
-  int contained;
-
-  /**
-   * Marking the state as part of an SCC (Strongly Connected Component).  All
-   * states with the same scc_id are part of the same SCC. scc_id is 0, if state
-   * is not a part of any SCC.
-   */
-  unsigned int scc_id;
-
-  /**
-   * Used for SCC detection.
-   */
-  int index;
-
-  /**
-   * Used for SCC detection.
-   */
-  int lowlink;
-
-  /**
-   * Human readable name of the automaton. Used for debugging and graph
-   * creation.
-   */
-  char *name;
-
-  /**
-   * Hash of the state.
-   */
-  GNUNET_HashCode hash;
-
-  /**
-   * Proof for this state.
-   */
-  char *proof;
-
-  /**
-   * Number of transitions from this state to other states.
-   */
-  unsigned int transition_count;
-
-  /**
-   * DLL of transitions.
-   */
-  struct Transition *transitions_head;
-
-  /**
-   * DLL of transitions.
-   */
-  struct Transition *transitions_tail;
-
-  /**
-   * Set of states on which this state is based on. Used when creating a DFA out
-   * of several NFA states.
-   */
-  struct GNUNET_REGEX_StateSet *nfa_set;
-};
-
-/**
- * Transition between two states. Each state can have 0-n transitions.  If label
- * is 0, this is considered to be an epsilon transition.
- */
-struct Transition
-{
-  /**
-   * This is a linked list.
-   */
-  struct Transition *prev;
-
-  /**
-   * This is a linked list.
-   */
-  struct Transition *next;
-
-  /**
-   * Unique id of this transition.
-   */
-  unsigned int id;
-
-  /**
-   * Label for this transition. This is basically the edge label for the graph.
-   */
-  char label;
-
-  /**
-   * State to which this transition leads.
-   */
-  struct GNUNET_REGEX_State *to_state;
-
-  /**
-   * State from which this transition origins.
-   */
-  struct GNUNET_REGEX_State *from_state;
-
-  /**
-   * Mark this transition. For example when reversing the automaton.
-   */
-  int mark;
-};
 
 /**
  * Set of states.
@@ -279,12 +79,25 @@ struct GNUNET_REGEX_StateSet
   unsigned int len;
 };
 
+
 /*
  * Debug helper functions
  */
+
+/**
+ * Print all the transitions of state 's'.
+ *
+ * @param s state for which to print it's transitions.
+ */
 void
-debug_print_transitions (struct GNUNET_REGEX_State *);
+debug_print_transitions (struct GNUNET_REGEX_State *s);
+
 
+/**
+ * Print information of the given state 's'.
+ *
+ * @param s state for which debug information should be printed.
+ */
 void
 debug_print_state (struct GNUNET_REGEX_State *s)
 {
@@ -304,6 +117,12 @@ debug_print_state (struct GNUNET_REGEX_State *s)
   debug_print_transitions (s);
 }
 
+
+/**
+ * Print debug information for all states contained in the automaton 'a'.
+ *
+ * @param a automaton for which debug information of it's states should be printed.
+ */
 void
 debug_print_states (struct GNUNET_REGEX_Automaton *a)
 {
@@ -313,8 +132,14 @@ debug_print_states (struct GNUNET_REGEX_Automaton *a)
     debug_print_state (s);
 }
 
+
+/**
+ * Print debug information for given transition 't'.
+ *
+ * @param t transition for which to print debug info.
+ */
 void
-debug_print_transition (struct Transition *t)
+debug_print_transition (struct GNUNET_REGEX_Transition *t)
 {
   char *to_state;
   char *from_state;
@@ -342,103 +167,16 @@ debug_print_transition (struct Transition *t)
               t->id, from_state, label, to_state);
 }
 
+
 void
 debug_print_transitions (struct GNUNET_REGEX_State *s)
 {
-  struct Transition *t;
+  struct GNUNET_REGEX_Transition *t;
 
   for (t = s->transitions_head; NULL != t; t = t->next)
     debug_print_transition (t);
 }
 
-/**
- * Recursive function doing DFS with 'v' as a start, detecting all SCCs inside
- * the subgraph reachable from 'v'. Used with scc_tarjan function to detect all
- * SCCs inside an automaton.
- *
- * @param ctx context
- * @param v start vertex
- * @param index current index
- * @param stack stack for saving all SCCs
- * @param stack_size current size of the stack
- */
-static void
-scc_tarjan_strongconnect (struct GNUNET_REGEX_Context *ctx,
-                          struct GNUNET_REGEX_State *v, int *index,
-                          struct GNUNET_REGEX_State **stack,
-                          unsigned int *stack_size)
-{
-  struct GNUNET_REGEX_State *w;
-  struct Transition *t;
-
-  v->index = *index;
-  v->lowlink = *index;
-  (*index)++;
-  stack[(*stack_size)++] = v;
-  v->contained = 1;
-
-  for (t = v->transitions_head; NULL != t; t = t->next)
-  {
-    w = t->to_state;
-    if (NULL != w && w->index < 0)
-    {
-      scc_tarjan_strongconnect (ctx, w, index, stack, stack_size);
-      v->lowlink = (v->lowlink > w->lowlink) ? w->lowlink : v->lowlink;
-    }
-    else if (0 != w->contained)
-      v->lowlink = (v->lowlink > w->index) ? w->index : v->lowlink;
-  }
-
-  if (v->lowlink == v->index)
-  {
-    w = stack[--(*stack_size)];
-    w->contained = 0;
-
-    if (v != w)
-    {
-      ctx->scc_id++;
-      while (v != w)
-      {
-        w->scc_id = ctx->scc_id;
-        w = stack[--(*stack_size)];
-        w->contained = 0;
-      }
-      w->scc_id = ctx->scc_id;
-    }
-  }
-}
-
-/**
- * Detect all SCCs (Strongly Connected Components) inside the given automaton.
- * SCCs will be marked using the scc_id on each state.
- *
- * @param ctx context
- * @param a automaton
- */
-static void
-scc_tarjan (struct GNUNET_REGEX_Context *ctx, struct GNUNET_REGEX_Automaton *a)
-{
-  int index;
-  struct GNUNET_REGEX_State *v;
-  struct GNUNET_REGEX_State *stack[a->state_count];
-  unsigned int stack_size;
-
-  for (v = a->states_head; NULL != v; v = v->next)
-  {
-    v->contained = 0;
-    v->index = -1;
-    v->lowlink = -1;
-  }
-
-  stack_size = 0;
-  index = 0;
-
-  for (v = a->states_head; NULL != v; v = v->next)
-  {
-    if (v->index < 0)
-      scc_tarjan_strongconnect (ctx, v, &index, stack, &stack_size);
-  }
-}
 
 /**
  * Adds a transition from one state to another on 'label'. Does not add
@@ -455,8 +193,8 @@ state_add_transition (struct GNUNET_REGEX_Context *ctx,
                       struct GNUNET_REGEX_State *to_state)
 {
   int is_dup;
-  struct Transition *t;
-  struct Transition *oth;
+  struct GNUNET_REGEX_Transition *t;
+  struct GNUNET_REGEX_Transition *oth;
 
   if (NULL == from_state)
   {
@@ -476,7 +214,7 @@ state_add_transition (struct GNUNET_REGEX_Context *ctx,
     }
   }
 
-  if (is_dup)
+  if (GNUNET_YES == is_dup)
     return;
 
   // sort transitions by label
@@ -486,7 +224,7 @@ state_add_transition (struct GNUNET_REGEX_Context *ctx,
       break;
   }
 
-  t = GNUNET_malloc (sizeof (struct Transition));
+  t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
   t->id = ctx->transition_id++;
   t->label = label;
   t->to_state = to_state;
@@ -498,6 +236,30 @@ state_add_transition (struct GNUNET_REGEX_Context *ctx,
                                       from_state->transitions_tail, oth, t);
 }
 
+
+/**
+ * Remove a 'transition' from 'state'.
+ *
+ * @param state state from which the to-be-removed transition originates.
+ * @param transition transition that should be removed from state 'state'.
+ */
+static void
+state_remove_transition (struct GNUNET_REGEX_State *state,
+                         struct GNUNET_REGEX_Transition *transition)
+{
+  if (NULL == state || NULL == transition)
+    return;
+
+  if (transition->from_state != state)
+    return;
+
+  state->transition_count--;
+  GNUNET_CONTAINER_DLL_remove (state->transitions_head, state->transitions_tail,
+                               transition);
+  GNUNET_free (transition);
+}
+
+
 /**
  * Compare two states. Used for sorting.
  *
@@ -520,18 +282,20 @@ state_compare (const void *a, const void *b)
   return (*s1)->id - (*s2)->id;
 }
 
+
 /**
  * Get all edges leaving state 's'.
  *
  * @param s state.
- * @param edges all edges leaving 's'.
+ * @param edges all edges leaving 's', expected to be allocated and have enough
+ *        space for s->transitions_count elements.
  *
  * @return number of edges.
  */
 static unsigned int
 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
 {
-  struct Transition *t;
+  struct GNUNET_REGEX_Transition *t;
   unsigned int count;
 
   if (NULL == s)
@@ -551,6 +315,7 @@ state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
   return count;
 }
 
+
 /**
  * Compare to state sets by comparing the id's of the states that are contained
  * in each set. Both sets are expected to be sorted by id!
@@ -567,7 +332,7 @@ state_set_compare (struct GNUNET_REGEX_StateSet *sset1,
                    struct GNUNET_REGEX_StateSet *sset2)
 {
   int result;
-  int i;
+  unsigned int i;
 
   if (NULL == sset1 || NULL == sset2)
     return 1;
@@ -584,6 +349,7 @@ state_set_compare (struct GNUNET_REGEX_StateSet *sset1,
   return result;
 }
 
+
 /**
  * Clears the given StateSet 'set'
  *
@@ -599,6 +365,7 @@ state_set_clear (struct GNUNET_REGEX_StateSet *set)
   }
 }
 
+
 /**
  * Clears an automaton fragment. Does not destroy the states inside the
  * automaton.
@@ -619,6 +386,7 @@ automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
   GNUNET_free (a);
 }
 
+
 /**
  * Frees the memory used by State 's'
  *
@@ -627,8 +395,8 @@ automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
 static void
 automaton_destroy_state (struct GNUNET_REGEX_State *s)
 {
-  struct Transition *t;
-  struct Transition *next_t;
+  struct GNUNET_REGEX_Transition *t;
+  struct GNUNET_REGEX_Transition *next_t;
 
   if (NULL == s)
     return;
@@ -648,6 +416,7 @@ automaton_destroy_state (struct GNUNET_REGEX_State *s)
   GNUNET_free (s);
 }
 
+
 /**
  * Remove a state from the given automaton 'a'. Always use this function when
  * altering the states of an automaton. Will also remove all transitions leading
@@ -662,7 +431,7 @@ automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
 {
   struct GNUNET_REGEX_State *ss;
   struct GNUNET_REGEX_State *s_check;
-  struct Transition *t_check;
+  struct GNUNET_REGEX_Transition *t_check;
 
   if (NULL == a || NULL == s)
     return;
@@ -690,6 +459,7 @@ automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
   automaton_destroy_state (ss);
 }
 
+
 /**
  * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
  * 's2'.
@@ -706,22 +476,38 @@ automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
                         struct GNUNET_REGEX_State *s2)
 {
   struct GNUNET_REGEX_State *s_check;
-  struct Transition *t_check;
+  struct GNUNET_REGEX_Transition *t_check;
+  struct GNUNET_REGEX_Transition *t;
+  struct GNUNET_REGEX_Transition *t_next;
   char *new_name;
+  int is_dup;
 
   GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
 
   if (s1 == s2)
     return;
 
-  // 1. Make all transitions pointing to s2 point to s1
+  // 1. Make all transitions pointing to s2 point to s1, unless this transition
+  // does not already exists, if it already exists remove transition.
   for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
   {
-    for (t_check = s_check->transitions_head; NULL != t_check;
-         t_check = t_check->next)
+    for (t_check = s_check->transitions_head; NULL != t_check; t_check = t_next)
     {
+      t_next = t_check->next;
+
       if (s2 == t_check->to_state)
-        t_check->to_state = s1;
+      {
+        is_dup = GNUNET_NO;
+        for (t = t_check->from_state->transitions_head; NULL != t; t = t->next)
+        {
+          if (t->to_state == s1 && t_check->label == t->label)
+            is_dup = GNUNET_YES;
+        }
+        if (GNUNET_NO == is_dup)
+          t_check->to_state = s1;
+        else
+          state_remove_transition (t_check->from_state, t_check);
+      }
     }
   }
 
@@ -733,8 +519,7 @@ automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
   }
 
   // 3. Rename s1 to {s1,s2}
-  new_name = GNUNET_strdup (s1->name);
-  GNUNET_free_non_null (s1->name);
+  new_name = s1->name;
   GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
   GNUNET_free (new_name);
 
@@ -744,6 +529,7 @@ automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
   automaton_destroy_state (s2);
 }
 
+
 /**
  * Add a state to the automaton 'a', always use this function to alter the
  * states DLL of the automaton.
@@ -759,64 +545,606 @@ automaton_add_state (struct GNUNET_REGEX_Automaton *a,
   a->state_count++;
 }
 
-/**
- * Function that is called with each state, when traversing an automaton.
- *
- * @param cls closure
- * @param s state
- */
-typedef void (*GNUNET_REGEX_traverse_action) (void *cls,
-                                              struct GNUNET_REGEX_State * s);
 
 /**
- * Traverses all states that are reachable from state 's'. Expects the states to
+ * Depth-first traversal of all states that are reachable from state 's'. Expects the states to
  * be unmarked (s->marked == GNUNET_NO). Performs 'action' on each visited
  * state.
  *
- * @param cls closure.
  * @param s start state.
+ * @param count current count of the state.
  * @param action action to be performed on each state.
+ * @param action_cls closure for action
+ */
+static void
+automaton_state_traverse (struct GNUNET_REGEX_State *s, unsigned int *count,
+                          GNUNET_REGEX_traverse_action action, void *action_cls)
+{
+  struct GNUNET_REGEX_Transition *t;
+
+  if (GNUNET_NO != s->marked)
+    return;
+  s->marked = GNUNET_YES;
+  if (NULL != action)
+    action (action_cls, *count, s);
+  (*count)++;
+  for (t = s->transitions_head; NULL != t; t = t->next)
+    automaton_state_traverse (t->to_state, count, action, action_cls);
+}
+
+
+/**
+ * Traverses the given automaton from it's start state, visiting all reachable
+ * states and calling 'action' on each one of them.
+ *
+ * @param a automaton.
+ * @param action action to be performed on each state.
+ * @param action_cls closure for action
+ */
+void
+GNUNET_REGEX_automaton_traverse (struct GNUNET_REGEX_Automaton *a,
+                                 GNUNET_REGEX_traverse_action action,
+                                 void *action_cls)
+{
+  unsigned int count;
+  struct GNUNET_REGEX_State *s;
+
+  for (s = a->states_head; NULL != s; s = s->next)
+    s->marked = GNUNET_NO;
+  count = 0;
+  automaton_state_traverse (a->start, &count, action, action_cls);
+}
+
+
+/**
+ * Check if the given string 'str' needs parentheses around it when
+ * using it to generate a regex.
+ *
+ * @param str string
+ *
+ * @return GNUNET_YES if parentheses are needed, GNUNET_NO otherwise
+ */
+static int
+needs_parentheses (const char *str)
+{
+  size_t slen;
+  const char *op;
+  const char *cl;
+  const char *pos;
+  unsigned int cnt;
+
+  if ((NULL == str) || ((slen = strlen (str)) < 2))
+    return GNUNET_NO;
+
+  if ('(' != str[0])
+    return GNUNET_YES;
+  cnt = 1;
+  pos = &str[1];
+  while (cnt > 0)
+  {
+    cl = strchr (pos, ')');
+    if (NULL == cl)
+    {
+      GNUNET_break (0);
+      return GNUNET_YES;
+    }
+    op = strchr (pos, '(');
+    if ((NULL != op) && (op < cl))
+    {
+      cnt++;
+      pos = op + 1;
+      continue;
+    }
+    /* got ')' first */
+    cnt--;
+    pos = cl + 1;
+  }
+  return (*pos == '\0') ? GNUNET_NO : GNUNET_YES;
+}
+
+
+/**
+ * Remove parentheses surrounding string 'str'.
+ * Example: "(a)" becomes "a", "(a|b)|(a|c)" stays the same.
+ * You need to GNUNET_free the returned string.
+ *
+ * @param str string, free'd or re-used by this function, can be NULL
+ *
+ * @return string without surrounding parentheses, string 'str' if no preceding
+ *         epsilon could be found, NULL if 'str' was NULL
+ */
+static char *
+remove_parentheses (char *str)
+{
+  size_t slen;
+  const char *pos;
+
+  if ((NULL == str) || ('(' != str[0]) ||
+      (str[(slen = strlen (str)) - 1] != ')'))
+    return str;
+
+  pos = strchr (&str[1], ')');
+  if (pos == &str[slen - 1])
+  {
+    memmove (str, &str[1], slen - 2);
+    str[slen - 2] = '\0';
+  }
+  return str;
+}
+
+
+/**
+ * Check if the string 'str' starts with an epsilon (empty string).
+ * Example: "(|a)" is starting with an epsilon.
+ *
+ * @param str string to test
+ *
+ * @return 0 if str has no epsilon, 1 if str starts with '(|' and ends with ')'
+ */
+static int
+has_epsilon (const char *str)
+{
+  return (NULL != str) && ('(' == str[0]) && ('|' == str[1]) &&
+      (')' == str[strlen (str) - 1]);
+}
+
+
+/**
+ * Remove an epsilon from the string str. Where epsilon is an empty string
+ * Example: str = "(|a|b|c)", result: "a|b|c"
+ * The returned string needs to be freed.
+ *
+ * @param str string
+ *
+ * @return string without preceding epsilon, string 'str' if no preceding epsilon
+ *         could be found, NULL if 'str' was NULL
+ */
+static char *
+remove_epsilon (const char *str)
+{
+  size_t len;
+
+  if (NULL == str)
+    return NULL;
+  if (('(' == str[0]) && ('|' == str[1]))
+  {
+    len = strlen (str);
+    if (')' == str[len - 1])
+      return GNUNET_strndup (&str[2], len - 3);
+  }
+  return GNUNET_strdup (str);
+}
+
+
+/**
+ * Compare 'str1', starting from position 'k',  with whole 'str2'
+ *
+ * @param str1 first string to compare, starting from position 'k'
+ * @param str2 second string for comparison
+ * @param k starting position in 'str1'
+ *
+ * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
+ */
+static int
+strkcmp (const char *str1, const char *str2, size_t k)
+{
+  if ((NULL == str1) || (NULL == str2) || (strlen (str1) < k))
+    return -1;
+  return strcmp (&str1[k], str2);
+}
+
+
+/**
+ * Compare two strings for equality. If either is NULL (or if both are
+ * NULL), they are not equal.
+ *
+ * @param str1 first string for comparison.
+ * @param str2 second string for comparison.
+ *
+ * @return  0 if the strings are the same, 1 or -1 if not
+ */
+static int
+nullstrcmp (const char *str1, const char *str2)
+{
+  if ((NULL == str1) || (NULL == str2))
+    return -1;
+  return strcmp (str1, str2);
+}
+
+
+/**
+ * Helper function used as 'action' in 'GNUNET_REGEX_automaton_traverse' function to create
+ * the depth-first numbering of the states.
+ *
+ * @param cls states array.
+ * @param count current state counter.
+ * @param s current state.
+ */
+static void
+number_states (void *cls, unsigned int count, struct GNUNET_REGEX_State *s)
+{
+  struct GNUNET_REGEX_State **states = cls;
+
+  s->proof_id = count;
+  if (NULL != states)
+    states[count] = s;
+}
+
+
+/**
+ * Construct the regular expression given the inductive step,
+ * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^*
+ * R^{(k-1)}_{kj}, and simplify the resulting expression saved in R_cur_ij.
+ *
+ * @param R_last_ij value of  $R^{(k-1)_{ij}.
+ * @param R_last_ik value of  $R^{(k-1)_{ik}.
+ * @param R_last_kk value of  $R^{(k-1)_{kk}.
+ * @param R_last_kj value of  $R^{(k-1)_{kj}.
+ * @param R_cur_ij result for this inductive step is saved in R_cur_ij, R_cur_ij
+ *                 is expected to be NULL when called!
  */
 static void
-automaton_state_traverse (void *cls, struct GNUNET_REGEX_State *s,
-                          GNUNET_REGEX_traverse_action action)
+automaton_create_proofs_simplify (char *R_last_ij, char *R_last_ik,
+                                  char *R_last_kk, char *R_last_kj,
+                                  char **R_cur_ij)
 {
-  struct Transition *t;
+  char *R_cur_l;
+  char *R_cur_r;
+  char *temp_a;
+  char *temp_b;
+  char *R_temp_ij;
+  char *R_temp_ik;
+  char *R_temp_kj;
+  char *R_temp_kk;
+
+  int eps_check;
+  int ij_ik_cmp;
+  int ij_kj_cmp;
+
+  int ik_kk_cmp;
+  int kk_kj_cmp;
+  int clean_ik_kk_cmp;
+  int clean_kk_kj_cmp;
+  unsigned int cnt;
+
+  size_t length;
+  size_t length_l;
+  size_t length_r;
+
+  GNUNET_assert (NULL == *R_cur_ij && NULL != R_cur_ij);
+
+  // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
+  // R_last == R^{(k-1)}, R_cur == R^{(k)}
+  // R_cur_ij = R_cur_l | R_cur_r
+  // R_cur_l == R^{(k-1)}_{ij}
+  // R_cur_r == R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
+
+  if ((NULL == R_last_ij) && ((NULL == R_last_ik) || (NULL == R_last_kk) ||     /* technically cannot happen, but looks saner */
+                              (NULL == R_last_kj)))
+  {
+    /* R^{(k)}_{ij} = N | N */
+    *R_cur_ij = NULL;
+    return;
+  }
+
+  if ((NULL == R_last_ik) || (NULL == R_last_kk) ||     /* technically cannot happen, but looks saner */
+      (NULL == R_last_kj))
+  {
+    /*  R^{(k)}_{ij} = R^{(k-1)}_{ij} | N */
+    *R_cur_ij = GNUNET_strdup (R_last_ij);
+    return;
+  }
+
+  // $R^{(k)}_{ij} = N | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} OR
+  // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
+
+  R_cur_r = NULL;
+  R_cur_l = NULL;
+
+  // cache results from strcmp, we might need these many times
+  ij_kj_cmp = nullstrcmp (R_last_ij, R_last_kj);
+  ij_ik_cmp = nullstrcmp (R_last_ij, R_last_ik);
+  ik_kk_cmp = nullstrcmp (R_last_ik, R_last_kk);
+  kk_kj_cmp = nullstrcmp (R_last_kk, R_last_kj);
+
+  // Assign R_temp_(ik|kk|kj) to R_last[][] and remove epsilon as well
+  // as parentheses, so we can better compare the contents
+  R_temp_ik = remove_parentheses (remove_epsilon (R_last_ik));
+  R_temp_kk = remove_parentheses (remove_epsilon (R_last_kk));
+  R_temp_kj = remove_parentheses (remove_epsilon (R_last_kj));
+
+  clean_ik_kk_cmp = nullstrcmp (R_last_ik, R_temp_kk);
+  clean_kk_kj_cmp = nullstrcmp (R_temp_kk, R_last_kj);
+
+  // construct R_cur_l (and, if necessary R_cur_r)
+  if (NULL != R_last_ij)
+  {
+    // Assign R_temp_ij to R_last_ij and remove epsilon as well
+    // as parentheses, so we can better compare the contents
+    R_temp_ij = remove_parentheses (remove_epsilon (R_last_ij));
+
+    if (0 == strcmp (R_temp_ij, R_temp_ik) && 0 == strcmp (R_temp_ik, R_temp_kk)
+        && 0 == strcmp (R_temp_kk, R_temp_kj))
+    {
+      if (0 == strlen (R_temp_ij))
+      {
+        R_cur_r = GNUNET_strdup ("");
+      }
+      else if ((0 == strncmp (R_last_ij, "(|", 2)) ||
+               (0 == strncmp (R_last_ik, "(|", 2) &&
+                0 == strncmp (R_last_kj, "(|", 2)))
+      {
+        // a|(e|a)a*(e|a) = a*
+        // a|(e|a)(e|a)*(e|a) = a*
+        // (e|a)|aa*a = a*
+        // (e|a)|aa*(e|a) = a*
+        // (e|a)|(e|a)a*a = a*
+        // (e|a)|(e|a)a*(e|a) = a*
+        // (e|a)|(e|a)(e|a)*(e|a) = a*
+        if (GNUNET_YES == needs_parentheses (R_temp_ij))
+          GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_ij);
+        else
+          GNUNET_asprintf (&R_cur_r, "%s*", R_temp_ij);
+      }
+      else
+      {
+        // a|aa*a = a+
+        // a|(e|a)a*a = a+
+        // a|aa*(e|a) = a+
+        // a|(e|a)(e|a)*a = a+
+        // a|a(e|a)*(e|a) = a+
+        if (GNUNET_YES == needs_parentheses (R_temp_ij))
+          GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_ij);
+        else
+          GNUNET_asprintf (&R_cur_r, "%s+", R_temp_ij);
+      }
+    }
+    else if (0 == ij_ik_cmp && 0 == clean_kk_kj_cmp && 0 != clean_ik_kk_cmp)
+    {
+      // a|ab*b = ab*
+      if (strlen (R_last_kk) < 1)
+        R_cur_r = GNUNET_strdup (R_last_ij);
+      else if (GNUNET_YES == needs_parentheses (R_temp_kk))
+        GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ij, R_temp_kk);
+      else
+        GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ij, R_last_kk);
+
+      R_cur_l = NULL;
+    }
+    else if (0 == ij_kj_cmp && 0 == clean_ik_kk_cmp && 0 != clean_kk_kj_cmp)
+    {
+      // a|bb*a = b*a
+      if (strlen (R_last_kk) < 1)
+        R_cur_r = GNUNET_strdup (R_last_kj);
+      else if (GNUNET_YES == needs_parentheses (R_temp_kk))
+        GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_kj);
+      else
+        GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_kj);
+
+      R_cur_l = NULL;
+    }
+    else if (0 == ij_ik_cmp && 0 == kk_kj_cmp && !has_epsilon (R_last_ij) &&
+             has_epsilon (R_last_kk))
+    {
+      // a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab*
+      if (needs_parentheses (R_temp_kk))
+        GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ij, R_temp_kk);
+      else
+        GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ij, R_temp_kk);
+
+      R_cur_l = NULL;
+    }
+    else if (0 == ij_kj_cmp && 0 == ik_kk_cmp && !has_epsilon (R_last_ij) &&
+             has_epsilon (R_last_kk))
+    {
+      // a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|...  = b*a
+      if (needs_parentheses (R_temp_kk))
+        GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_ij);
+      else
+        GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_ij);
+
+      R_cur_l = NULL;
+    }
+    else
+    {
+      temp_a = (NULL == R_last_ij) ? NULL : GNUNET_strdup (R_last_ij);
+      temp_a = remove_parentheses (temp_a);
+      R_cur_l = temp_a;
+    }
+
+    GNUNET_free_non_null (R_temp_ij);
+  }
+  else
+  {
+    // we have no left side
+    R_cur_l = NULL;
+  }
+
+  // construct R_cur_r, if not already constructed
+  if (NULL == R_cur_r)
+  {
+    length = strlen (R_temp_kk) - strlen (R_last_ik);
+
+    // a(ba)*bx = (ab)+x
+    if (length > 0 && NULL != R_last_kk && 0 < strlen (R_last_kk) &&
+        NULL != R_last_kj && 0 < strlen (R_last_kj) && NULL != R_last_ik &&
+        0 < strlen (R_last_ik) && 0 == strkcmp (R_temp_kk, R_last_ik, length) &&
+        0 == strncmp (R_temp_kk, R_last_kj, length))
+    {
+      temp_a = GNUNET_malloc (length + 1);
+      temp_b = GNUNET_malloc ((strlen (R_last_kj) - length) + 1);
+
+      length_l = 0;
+      length_r = 0;
+
+      for (cnt = 0; cnt < strlen (R_last_kj); cnt++)
+      {
+        if (cnt < length)
+        {
+          temp_a[length_l] = R_last_kj[cnt];
+          length_l++;
+        }
+        else
+        {
+          temp_b[length_r] = R_last_kj[cnt];
+          length_r++;
+        }
+      }
+      temp_a[length_l] = '\0';
+      temp_b[length_r] = '\0';
+
+      // e|(ab)+ = (ab)*
+      if (NULL != R_cur_l && 0 == strlen (R_cur_l) && 0 == strlen (temp_b))
+      {
+        GNUNET_asprintf (&R_cur_r, "(%s%s)*", R_last_ik, temp_a);
+        GNUNET_free (R_cur_l);
+        R_cur_l = NULL;
+      }
+      else
+      {
+        GNUNET_asprintf (&R_cur_r, "(%s%s)+%s", R_last_ik, temp_a, temp_b);
+      }
+      GNUNET_free (temp_a);
+      GNUNET_free (temp_b);
+    }
+    else if (0 == strcmp (R_temp_ik, R_temp_kk) &&
+             0 == strcmp (R_temp_kk, R_temp_kj))
+    {
+      // (e|a)a*(e|a) = a*
+      // (e|a)(e|a)*(e|a) = a*
+      if (has_epsilon (R_last_ik) && has_epsilon (R_last_kj))
+      {
+        if (needs_parentheses (R_temp_kk))
+          GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_kk);
+        else
+          GNUNET_asprintf (&R_cur_r, "%s*", R_temp_kk);
+      }
+      // aa*a = a+a
+      else if (0 == clean_ik_kk_cmp && 0 == clean_kk_kj_cmp &&
+               !has_epsilon (R_last_ik))
+      {
+        if (needs_parentheses (R_temp_kk))
+          GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
+        else
+          GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
+      }
+      // (e|a)a*a = a+
+      // aa*(e|a) = a+
+      // a(e|a)*(e|a) = a+
+      // (e|a)a*a = a+
+      else
+      {
+        eps_check =
+            (has_epsilon (R_last_ik) + has_epsilon (R_last_kk) +
+             has_epsilon (R_last_kj));
+
+        if (eps_check == 1)
+        {
+          if (needs_parentheses (R_temp_kk))
+            GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_kk);
+          else
+            GNUNET_asprintf (&R_cur_r, "%s+", R_temp_kk);
+        }
+      }
+    }
+    // aa*b = a+b
+    // (e|a)(e|a)*b = a*b
+    else if (0 == strcmp (R_temp_ik, R_temp_kk))
+    {
+      if (has_epsilon (R_last_ik))
+      {
+        if (needs_parentheses (R_temp_kk))
+          GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_kj);
+        else
+          GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_kj);
+      }
+      else
+      {
+        if (needs_parentheses (R_temp_kk))
+          GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_last_kj);
+        else
+          GNUNET_asprintf (&R_cur_r, "%s+%s", R_temp_kk, R_last_kj);
+      }
+    }
+    // ba*a = ba+
+    // b(e|a)*(e|a) = ba*
+    else if (0 == strcmp (R_temp_kk, R_temp_kj))
+    {
+      if (has_epsilon (R_last_kj))
+      {
+        if (needs_parentheses (R_temp_kk))
+          GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ik, R_temp_kk);
+        else
+          GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ik, R_temp_kk);
+      }
+      else
+      {
+        if (needs_parentheses (R_temp_kk))
+          GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_last_ik, R_temp_kk);
+        else
+          GNUNET_asprintf (&R_cur_r, "%s+%s", R_last_ik, R_temp_kk);
+      }
+    }
+    else
+    {
+      if (strlen (R_temp_kk) > 0)
+      {
+        if (needs_parentheses (R_temp_kk))
+        {
+          GNUNET_asprintf (&R_cur_r, "%s(%s)*%s", R_last_ik, R_temp_kk,
+                           R_last_kj);
+        }
+        else
+        {
+          GNUNET_asprintf (&R_cur_r, "%s%s*%s", R_last_ik, R_temp_kk,
+                           R_last_kj);
+        }
+      }
+      else
+      {
+        GNUNET_asprintf (&R_cur_r, "%s%s", R_last_ik, R_last_kj);
+      }
+    }
+  }
+
+  GNUNET_free_non_null (R_temp_ik);
+  GNUNET_free_non_null (R_temp_kk);
+  GNUNET_free_non_null (R_temp_kj);
 
-  if (GNUNET_NO == s->marked)
+  if (NULL == R_cur_l && NULL == R_cur_r)
   {
-    s->marked = GNUNET_YES;
+    *R_cur_ij = NULL;
+    return;
+  }
 
-    if (action > 0)
-      action (cls, s);
+  if (NULL != R_cur_l && NULL == R_cur_r)
+  {
+    *R_cur_ij = R_cur_l;
+    return;
+  }
 
-    for (t = s->transitions_head; NULL != t; t = t->next)
-      automaton_state_traverse (cls, t->to_state, action);
+  if (NULL == R_cur_l && NULL != R_cur_r)
+  {
+    *R_cur_ij = R_cur_r;
+    return;
   }
-}
 
-/**
- * Traverses the given automaton from it's start state, visiting all reachable
- * states and calling 'action' on each one of them.
- *
- * @param cls closure.
- * @param a automaton.
- * @param action action to be performed on each state.
- */
-static void
-automaton_traverse (void *cls, struct GNUNET_REGEX_Automaton *a,
-                    GNUNET_REGEX_traverse_action action)
-{
-  struct GNUNET_REGEX_State *s;
+  if (0 == nullstrcmp (R_cur_l, R_cur_r))
+  {
+    *R_cur_ij = R_cur_l;
+    GNUNET_free (R_cur_r);
+    return;
+  }
 
-  for (s = a->states_head; NULL != s; s = s->next)
-    s->marked = GNUNET_NO;
+  GNUNET_asprintf (R_cur_ij, "(%s|%s)", R_cur_l, R_cur_r);
 
-  automaton_state_traverse (cls, a->start, action);
+  GNUNET_free (R_cur_l);
+  GNUNET_free (R_cur_r);
 }
 
+
 /**
- * Create proofs for all states in the given automaton. Implementation of the
+ * create proofs for all states in the given automaton. Implementation of the
  * algorithm descriped in chapter 3.2.1 of "Automata Theory, Languages, and
  * Computation 3rd Edition" by Hopcroft, Motwani and Ullman.
  *
@@ -825,244 +1153,74 @@ automaton_traverse (void *cls, struct GNUNET_REGEX_Automaton *a,
 static void
 automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
 {
-  struct GNUNET_REGEX_State *s;
-  struct Transition *t;
-  int i;
-  int j;
-  int k;
-  int n;
-  struct GNUNET_REGEX_State *states[a->state_count];
-  char *R_last[a->state_count][a->state_count];
-  char *R_cur[a->state_count][a->state_count];
+  unsigned int n = a->state_count;
+  struct GNUNET_REGEX_State *states[n];
+  char *R_last[n][n];
+  char *R_cur[n][n];
   char *temp;
-  char *R_cur_l;
-  char *R_cur_r;
-  int length_l;
-  int length_r;
-  int s_cnt;
-  int l_cnt;
+  struct GNUNET_REGEX_Transition *t;
   char *complete_regex;
+  unsigned int i;
+  unsigned int j;
+  unsigned int k;
 
-  k = 0;
-  n = a->state_count;
 
-  // number the states
-  for (i = 0, s = a->states_head; NULL != s; s = s->next, i++)
-  {
-    s->marked = i;
-    states[i] = s;
-  }
+  /* create depth-first numbering of the states, initializes 'state' */
+  GNUNET_REGEX_automaton_traverse (a, &number_states, states);
 
-  // BASIS
+  /* Compute regular expressions of length "1" between each pair of states */
   for (i = 0; i < n; i++)
   {
     for (j = 0; j < n; j++)
     {
       R_cur[i][j] = NULL;
       R_last[i][j] = NULL;
-      for (t = states[i]->transitions_head; NULL != t; t = t->next)
-      {
-        if (t->to_state == states[j])
-        {
-          if (NULL == R_last[i][j])
-            GNUNET_asprintf (&R_last[i][j], "%c", t->label);
-          else
-          {
-            temp = R_last[i][j];
-            GNUNET_asprintf (&R_last[i][j], "%s|%c", R_last[i][j], t->label);
-            GNUNET_free (temp);
-          }
-        }
-      }
-
-      if (i == j)
+    }
+    for (t = states[i]->transitions_head; NULL != t; t = t->next)
+    {
+      j = t->to_state->proof_id;
+      if (NULL == R_last[i][j])
+        GNUNET_asprintf (&R_last[i][j], "%c", t->label);
+      else
       {
-        if (NULL == R_last[i][j])
-          GNUNET_asprintf (&R_last[i][j], "");
-        else if (NULL != R_last[i][j] && 1 < strlen (R_last[i][j]))
-        {
-          temp = R_last[i][j];
-          GNUNET_asprintf (&R_last[i][j], "(%s)", R_last[i][j]);
-          GNUNET_free (temp);
-        }
+        temp = R_last[i][j];
+        GNUNET_asprintf (&R_last[i][j], "%s|%c", R_last[i][j], t->label);
+        GNUNET_free (temp);
       }
-      else if (NULL != R_last[i][j] && 1 < strlen (R_last[i][j]))
+    }
+    if (NULL == R_last[i][i])
+      GNUNET_asprintf (&R_last[i][i], "");
+    else
+    {
+      temp = R_last[i][i];
+      GNUNET_asprintf (&R_last[i][i], "(|%s)", R_last[i][i]);
+      GNUNET_free (temp);
+    }
+  }
+  for (i = 0; i < n; i++)
+    for (j = 0; j < n; j++)
+      if (needs_parentheses (R_last[i][j]))
       {
         temp = R_last[i][j];
         GNUNET_asprintf (&R_last[i][j], "(%s)", R_last[i][j]);
         GNUNET_free (temp);
       }
-    }
-  }
 
-  // INDUCTION
+  /* Compute regular expressions of length "k" between each pair of states per induction */
   for (k = 0; k < n; k++)
   {
     for (i = 0; i < n; i++)
     {
       for (j = 0; j < n; j++)
       {
-        //construct R_cur
-
-        // 0*R = R*0 = 0
-        // 0+R = R+0 = R
-        if (NULL == R_last[i][k] || NULL == R_last[k][j])
-        {
-          if (NULL != R_last[i][j])
-            R_cur[i][j] = GNUNET_strdup (R_last[i][j]);
-        }
-        // a(ba)*b = (ab)+
-        /*else if ((NULL == R_last[i][j] || 0 == strlen (R_last[i][j])) && */
-        /*NULL != R_last[k][k] && 0 < strlen (R_last[k][k]) && */
-        /*NULL != R_last[k][j] && 0 < strlen (R_last[k][j]) && */
-        /*0 == strncmp (R_last[k][k], R_last[k][j], (strlen (R_last[k][k]) - 1)) && */
-        /*R_last[i][k][0] == R_last[k][k][strlen (R_last[k][k]) - 1]) */
-        /*{ */
-        /*GNUNET_asprintf (&R_cur[i][j], "(%s%s)+", R_last[i][k], R_last[k][j]); */
-        /*} */
-        else
-        {
-          // R(k)ij = R(k-1)ij + R(k-1)ik (R(k-1)kk)* R(k-1)kj
-          length_l = (NULL == R_last[i][j]) ? 1 : strlen (R_last[i][j]) + 1;
-          length_r =
-              snprintf (NULL, 0, "%s(%s)*%s", R_last[i][k], R_last[k][k],
-                        R_last[k][j]) + 1;
-          R_cur_l = GNUNET_malloc (length_l);
-          R_cur_r = GNUNET_malloc (length_r);
-
-          if (NULL != R_last[i][j])
-            strcat (R_cur_l, R_last[i][j]);
-
-          if (NULL != R_last[i][k] && 0 != strcmp (R_last[i][k], R_last[k][k]))
-            strcat (R_cur_r, R_last[i][k]);
-
-          if (NULL != R_last[k][k] && 0 != strcmp (R_last[k][k], ""))
-          {
-            if (R_last[k][k][0] == '(' &&
-                R_last[k][k][strlen (R_last[k][k]) - 1] == ')')
-            {
-              strcat (R_cur_r, R_last[k][k]);
-            }
-            else
-            {
-              strcat (R_cur_r, "(");
-              strcat (R_cur_r, R_last[k][k]);
-              strcat (R_cur_r, ")");
-            }
-
-            if (0 == strcmp (R_last[i][k], R_last[k][k]) ||
-                0 == strcmp (R_last[k][k], R_last[k][j]))
-              strcat (R_cur_r, "+");
-            else
-              strcat (R_cur_r, "*");
-          }
-
-          if (NULL != R_last[k][j] && 0 != strcmp (R_last[k][k], R_last[k][j]))
-            strcat (R_cur_r, R_last[k][j]);
-
-          // simplifications...
-
-          // | is idempotent: a | a = a for all a in A
-          if (0 == strcmp (R_cur_l, R_cur_r) || 0 == strcmp (R_cur_l, "") ||
-              0 == strcmp (R_cur_r, ""))
-          {
-            if (0 == strcmp (R_cur_l, ""))
-              GNUNET_asprintf (&R_cur[i][j], "%s", R_cur_r);
-            else
-              GNUNET_asprintf (&R_cur[i][j], "%s", R_cur_l);
-          }
-          // TODO: in theory only applicable if (e + a) | (e + a)(e + a)*(e+a)
-          // where e means epsilon... check if practical!
-          // a | a a* a = a*
-          else if (R_last[i][j] == R_last[i][k] && R_last[i][k] == R_last[k][k]
-                   && R_last[k][k] == R_last[k][j])
-          {
-            if (1 >= strlen (R_last[k][k]) ||
-                (R_last[k][k][0] == '(' &&
-                 R_last[k][k][strlen (R_last[k][k]) - 1] == ')'))
-              GNUNET_asprintf (&R_cur[i][j], "%s*", R_last[k][k]);
-            else
-              GNUNET_asprintf (&R_cur[i][j], "(%s)*", R_last[k][k]);
-          }
-          // a | a b* b => a | a b | a b b | ... => a b*
-          else if (R_last[i][j] == R_last[i][k] && R_last[k][k] == R_last[k][j])
-          {
-            // if a is xb then a b* becomes x b b* = x b+
-
-            s_cnt = strlen (R_last[k][k]);
-            l_cnt = strlen (R_last[i][k]);
-            R_cur[i][j] = GNUNET_malloc (s_cnt + l_cnt + 4);
-
-            if (l_cnt > 0 && s_cnt >= l_cnt)
-              for (; s_cnt > 0; s_cnt--, l_cnt--)
-                if (R_last[i][k][l_cnt] != R_last[k][k][s_cnt])
-                  break;
-
-            if (strlen (R_last[i][k]) > 0 && 0 == s_cnt && 0 <= l_cnt)
-              strncat (R_cur[i][j], R_last[i][k], l_cnt);
-            else
-              strcat (R_cur[i][j], R_last[i][k]);
-
-            if (1 >= strlen (R_last[k][k]) &&
-                !(R_last[k][k][0] == '(' &&
-                  R_last[k][k][strlen (R_last[k][k]) - 1] == ')'))
-            {
-              strcat (R_cur[i][j], "(");
-              strcat (R_cur[i][j], R_last[k][k]);
-              strcat (R_cur[i][j], ")");
-            }
-            else
-              strcat (R_cur[i][j], R_last[k][k]);
-
-            if (0 == s_cnt && 0 <= l_cnt)
-              strcat (R_cur[i][j], "+");
-            else
-              strcat (R_cur[i][j], "*");
-          }
-          // a | b b* a => a | b a | b b a | ... => b* a
-          else if (R_last[i][j] == R_last[k][j] && R_last[i][k] == R_last[k][k])
-          {
-            // if a is bx then b* a becomes b+ x
-
-            temp = NULL;
-            s_cnt = strlen (R_last[k][k]);
-            l_cnt = strlen (R_last[k][j]);
-            R_cur[i][j] = GNUNET_malloc (s_cnt + l_cnt + 4);
-
-            int bla;
-
-            if (l_cnt > 0 && s_cnt >= l_cnt)
-              for (bla = 0; bla < s_cnt; bla++)
-                if (R_last[k][k][bla] != R_last[k][j][bla])
-                  break;
-
-            if (1 >= strlen (R_last[k][k]) &&
-                !(R_last[k][k][0] == '(' &&
-                  R_last[k][k][strlen (R_last[k][k]) - 1] == ')'))
-            {
-              strcat (R_cur[i][j], "(");
-              strcat (R_cur[i][j], R_last[k][k]);
-              strcat (R_cur[i][j], ")");
-            }
-            else
-              strcat (R_cur[i][j], R_last[k][k]);
-
-            if (bla == s_cnt)
-              strcat (R_cur[i][j], "+");
-            else
-              strcat (R_cur[i][j], "*");
-
-            if (strlen (R_last[k][j]) > 0 && bla == s_cnt)
-              strcat (R_cur[i][j], &R_last[k][j][bla]);
-            else
-              strcat (R_cur[i][j], R_last[k][j]);
-          }
-          else
-            GNUNET_asprintf (&R_cur[i][j], "(%s|%s)", R_cur_l, R_cur_r);
-
-          GNUNET_free_non_null (R_cur_l);
-          GNUNET_free_non_null (R_cur_r);
-        }
+        // Basis for the recursion:
+        // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
+        // R_last == R^{(k-1)}, R_cur == R^{(k)}
+
+        // Create R_cur[i][j] and simplify the expression
+        automaton_create_proofs_simplify (R_last[i][j], R_last[i][k],
+                                          R_last[k][k], R_last[k][j],
+                                          &R_cur[i][j]);
       }
     }
 
@@ -1072,13 +1230,8 @@ automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
       for (j = 0; j < n; j++)
       {
         GNUNET_free_non_null (R_last[i][j]);
-
-        if (NULL != R_cur[i][j])
-        {
-          R_last[i][j] = GNUNET_strdup (R_cur[i][j]);
-          GNUNET_free (R_cur[i][j]);
-          R_cur[i][j] = NULL;
-        }
+        R_last[i][j] = R_cur[i][j];
+        R_cur[i][j] = NULL;
       }
     }
   }
@@ -1086,9 +1239,12 @@ automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
   // assign proofs and hashes
   for (i = 0; i < n; i++)
   {
-    states[i]->proof = GNUNET_strdup (R_last[a->start->marked][i]);
-    GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
-                        &states[i]->hash);
+    if (NULL != R_last[a->start->proof_id][i])
+    {
+      states[i]->proof = GNUNET_strdup (R_last[a->start->proof_id][i]);
+      GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
+                          &states[i]->hash);
+    }
   }
 
   // complete regex for whole DFA: union of all pairs (start state/accepting state(s)).
@@ -1097,19 +1253,21 @@ automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
   {
     if (states[i]->accepting)
     {
-      if (NULL == complete_regex)
-        GNUNET_asprintf (&complete_regex, "%s", R_last[a->start->marked][i]);
-      else if (NULL != R_last[a->start->marked][i] &&
-               0 != strcmp (R_last[a->start->marked][i], ""))
+      if (NULL == complete_regex && 0 < strlen (R_last[a->start->proof_id][i]))
+      {
+        GNUNET_asprintf (&complete_regex, "%s", R_last[a->start->proof_id][i]);
+      }
+      else if (NULL != R_last[a->start->proof_id][i] &&
+               0 < strlen (R_last[a->start->proof_id][i]))
       {
         temp = complete_regex;
         GNUNET_asprintf (&complete_regex, "%s|%s", complete_regex,
-                         R_last[a->start->marked][i]);
+                         R_last[a->start->proof_id][i]);
         GNUNET_free (temp);
       }
     }
   }
-  a->computed_regex = complete_regex;
+  a->canonical_regex = complete_regex;
 
   // cleanup
   for (i = 0; i < n; i++)
@@ -1119,6 +1277,7 @@ automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
   }
 }
 
+
 /**
  * Creates a new DFA state based on a set of NFA states. Needs to be freed using
  * automaton_destroy_state.
@@ -1136,10 +1295,8 @@ dfa_state_create (struct GNUNET_REGEX_Context *ctx,
   char *name;
   int len = 0;
   struct GNUNET_REGEX_State *cstate;
-  struct Transition *ctran;
-  int insert = 1;
-  struct Transition *t;
-  int i;
+  struct GNUNET_REGEX_Transition *ctran;
+  unsigned int i;
 
   s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
   s->id = ctx->state_id++;
@@ -1186,21 +1343,7 @@ dfa_state_create (struct GNUNET_REGEX_Context *ctx,
     for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
     {
       if (0 != ctran->label)
-      {
-        insert = 1;
-
-        for (t = s->transitions_head; NULL != t; t = t->next)
-        {
-          if (t->label == ctran->label)
-          {
-            insert = 0;
-            break;
-          }
-        }
-
-        if (insert)
-          state_add_transition (ctx, s, ctran->label, NULL);
-      }
+        state_add_transition (ctx, s, ctran->label, NULL);
     }
 
     // If the nfa_states contain an accepting state, the new dfa state is also
@@ -1214,6 +1357,7 @@ dfa_state_create (struct GNUNET_REGEX_Context *ctx,
   return s;
 }
 
+
 /**
  * Move from the given state 's' to the next state on transition 'label'
  *
@@ -1225,7 +1369,7 @@ dfa_state_create (struct GNUNET_REGEX_Context *ctx,
 static struct GNUNET_REGEX_State *
 dfa_move (struct GNUNET_REGEX_State *s, const char label)
 {
-  struct Transition *t;
+  struct GNUNET_REGEX_Transition *t;
   struct GNUNET_REGEX_State *new_s;
 
   if (NULL == s)
@@ -1245,6 +1389,7 @@ dfa_move (struct GNUNET_REGEX_State *s, const char label)
   return new_s;
 }
 
+
 /**
  * Remove all unreachable states from DFA 'a'. Unreachable states are those
  * states that are not reachable from the starting state.
@@ -1262,7 +1407,7 @@ dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
     s->marked = GNUNET_NO;
 
   // 2. traverse dfa from start state and mark all visited states
-  automaton_traverse (NULL, a, NULL);
+  GNUNET_REGEX_automaton_traverse (a, NULL, NULL);
 
   // 3. delete all states that were not visited
   for (s = a->states_head; NULL != s; s = s_next)
@@ -1273,9 +1418,10 @@ dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
   }
 }
 
+
 /**
  * Remove all dead states from the DFA 'a'. Dead states are those states that do
- * not transition to any other state but themselfes.
+ * not transition to any other state but themselves.
  *
  * @param a DFA automaton
  */
@@ -1283,7 +1429,7 @@ static void
 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
 {
   struct GNUNET_REGEX_State *s;
-  struct Transition *t;
+  struct GNUNET_REGEX_Transition *t;
   int dead;
 
   GNUNET_assert (DFA == a->type);
@@ -1311,6 +1457,7 @@ dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
   }
 }
 
+
 /**
  * Merge all non distinguishable states in the DFA 'a'
  *
@@ -1321,16 +1468,16 @@ static void
 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
                                      struct GNUNET_REGEX_Automaton *a)
 {
-  int i;
+  unsigned int i;
   int table[a->state_count][a->state_count];
   struct GNUNET_REGEX_State *s1;
   struct GNUNET_REGEX_State *s2;
-  struct Transition *t1;
-  struct Transition *t2;
+  struct GNUNET_REGEX_Transition *t1;
+  struct GNUNET_REGEX_Transition *t2;
   struct GNUNET_REGEX_State *s1_next;
   struct GNUNET_REGEX_State *s2_next;
   int change;
-  int num_equal_edges;
+  unsigned int num_equal_edges;
 
   for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
        i++, s1 = s1->next)
@@ -1405,6 +1552,7 @@ dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
   }
 }
 
+
 /**
  * Minimize the given DFA 'a' by removing all unreachable states, removing all
  * dead states and merging all non distinguishable states
@@ -1431,6 +1579,7 @@ dfa_minimize (struct GNUNET_REGEX_Context *ctx,
   dfa_merge_nondistinguishable_states (ctx, a);
 }
 
+
 /**
  * Creates a new NFA fragment. Needs to be cleared using
  * automaton_fragment_clear.
@@ -1452,7 +1601,7 @@ nfa_fragment_create (struct GNUNET_REGEX_State *start,
   n->start = NULL;
   n->end = NULL;
 
-  if (NULL == start && NULL == end)
+  if (NULL == start || NULL == end)
     return n;
 
   automaton_add_state (n, end);
@@ -1464,6 +1613,7 @@ nfa_fragment_create (struct GNUNET_REGEX_State *start,
   return n;
 }
 
+
 /**
  * Adds a list of states to the given automaton 'n'.
  *
@@ -1501,6 +1651,7 @@ nfa_add_states (struct GNUNET_REGEX_Automaton *n,
     n->state_count++;
 }
 
+
 /**
  * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
  *
@@ -1528,6 +1679,7 @@ nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
   return s;
 }
 
+
 /**
  * Calculates the NFA closure set for the given state.
  *
@@ -1546,7 +1698,7 @@ nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
   struct GNUNET_REGEX_StateSet *cls_check;
   struct GNUNET_REGEX_State *clsstate;
   struct GNUNET_REGEX_State *currentstate;
-  struct Transition *ctran;
+  struct GNUNET_REGEX_Transition *ctran;
 
   if (NULL == s)
     return NULL;
@@ -1594,6 +1746,7 @@ nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
   return cls;
 }
 
+
 /**
  * Calculates the closure set for the given set of states.
  *
@@ -1611,10 +1764,10 @@ nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
   struct GNUNET_REGEX_State *s;
   struct GNUNET_REGEX_StateSet *sset;
   struct GNUNET_REGEX_StateSet *cls;
-  int i;
-  int j;
-  int k;
-  int contains;
+  unsigned int i;
+  unsigned int j;
+  unsigned int k;
+  unsigned int contains;
 
   if (NULL == states)
     return NULL;
@@ -1650,6 +1803,7 @@ nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
   return cls;
 }
 
+
 /**
  * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
  *
@@ -1660,28 +1814,31 @@ nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
 {
   struct GNUNET_REGEX_Automaton *a;
   struct GNUNET_REGEX_Automaton *b;
-  struct GNUNET_REGEX_Automaton *new;
+  struct GNUNET_REGEX_Automaton *new_nfa;
 
   b = ctx->stack_tail;
+  GNUNET_assert (NULL != b);
   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
   a = ctx->stack_tail;
+  GNUNET_assert (NULL != a);
   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
 
   state_add_transition (ctx, a->end, 0, b->start);
   a->end->accepting = 0;
   b->end->accepting = 1;
 
-  new = nfa_fragment_create (NULL, NULL);
-  nfa_add_states (new, a->states_head, a->states_tail);
-  nfa_add_states (new, b->states_head, b->states_tail);
-  new->start = a->start;
-  new->end = b->end;
+  new_nfa = nfa_fragment_create (NULL, NULL);
+  nfa_add_states (new_nfa, a->states_head, a->states_tail);
+  nfa_add_states (new_nfa, b->states_head, b->states_tail);
+  new_nfa->start = a->start;
+  new_nfa->end = b->end;
   automaton_fragment_clear (a);
   automaton_fragment_clear (b);
 
-  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
+  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
 }
 
+
 /**
  * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
  *
@@ -1691,12 +1848,11 @@ static void
 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
 {
   struct GNUNET_REGEX_Automaton *a;
-  struct GNUNET_REGEX_Automaton *new;
+  struct GNUNET_REGEX_Automaton *new_nfa;
   struct GNUNET_REGEX_State *start;
   struct GNUNET_REGEX_State *end;
 
   a = ctx->stack_tail;
-  GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
 
   if (NULL == a)
   {
@@ -1705,6 +1861,8 @@ nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
     return;
   }
 
+  GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
+
   start = nfa_state_create (ctx, 0);
   end = nfa_state_create (ctx, 1);
 
@@ -1716,13 +1874,14 @@ nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
   a->end->accepting = 0;
   end->accepting = 1;
 
-  new = nfa_fragment_create (start, end);
-  nfa_add_states (new, a->states_head, a->states_tail);
+  new_nfa = nfa_fragment_create (start, end);
+  nfa_add_states (new_nfa, a->states_head, a->states_tail);
   automaton_fragment_clear (a);
 
-  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
+  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
 }
 
+
 /**
  * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
  *
@@ -1741,6 +1900,7 @@ nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
 }
 
+
 /**
  * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
  *
@@ -1750,12 +1910,11 @@ static void
 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
 {
   struct GNUNET_REGEX_Automaton *a;
-  struct GNUNET_REGEX_Automaton *new;
+  struct GNUNET_REGEX_Automaton *new_nfa;
   struct GNUNET_REGEX_State *start;
   struct GNUNET_REGEX_State *end;
 
   a = ctx->stack_tail;
-  GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
 
   if (NULL == a)
   {
@@ -1764,6 +1923,8 @@ nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
     return;
   }
 
+  GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
+
   start = nfa_state_create (ctx, 0);
   end = nfa_state_create (ctx, 1);
 
@@ -1773,13 +1934,14 @@ nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
 
   a->end->accepting = 0;
 
-  new = nfa_fragment_create (start, end);
-  nfa_add_states (new, a->states_head, a->states_tail);
+  new_nfa = nfa_fragment_create (start, end);
+  nfa_add_states (new_nfa, a->states_head, a->states_tail);
   automaton_fragment_clear (a);
 
-  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
+  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
 }
 
+
 /**
  * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
  * alternates between a and b (a|b)
@@ -1791,13 +1953,15 @@ nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
 {
   struct GNUNET_REGEX_Automaton *a;
   struct GNUNET_REGEX_Automaton *b;
-  struct GNUNET_REGEX_Automaton *new;
+  struct GNUNET_REGEX_Automaton *new_nfa;
   struct GNUNET_REGEX_State *start;
   struct GNUNET_REGEX_State *end;
 
   b = ctx->stack_tail;
+  GNUNET_assert (NULL != b);
   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
   a = ctx->stack_tail;
+  GNUNET_assert (NULL != a);
   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
 
   start = nfa_state_create (ctx, 0);
@@ -1812,15 +1976,16 @@ nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
   b->end->accepting = 0;
   end->accepting = 1;
 
-  new = nfa_fragment_create (start, end);
-  nfa_add_states (new, a->states_head, a->states_tail);
-  nfa_add_states (new, b->states_head, b->states_tail);
+  new_nfa = nfa_fragment_create (start, end);
+  nfa_add_states (new_nfa, a->states_head, a->states_tail);
+  nfa_add_states (new_nfa, b->states_head, b->states_tail);
   automaton_fragment_clear (a);
   automaton_fragment_clear (b);
 
-  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
+  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
 }
 
+
 /**
  * Adds a new nfa fragment to the stack
  *
@@ -1844,6 +2009,7 @@ nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
 }
 
+
 /**
  * Initialize a new context
  *
@@ -1859,11 +2025,11 @@ GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
   }
   ctx->state_id = 0;
   ctx->transition_id = 0;
-  ctx->scc_id = 0;
   ctx->stack_head = NULL;
   ctx->stack_tail = NULL;
 }
 
+
 /**
  * Construct an NFA by parsing the regex string of length 'len'.
  *
@@ -1974,6 +2140,7 @@ GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
     case 92:                   /* escape: \ */
       regexp++;
       count++;
+      /* fall through! */
     default:
       if (atomcount > 1)
       {
@@ -2008,24 +2175,28 @@ GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
 
   nfa->regex = GNUNET_strdup (regex);
 
+  /* create depth-first numbering of the states for pretty printing */
+  GNUNET_REGEX_automaton_traverse (nfa, &number_states, NULL);
+
   return nfa;
 
 error:
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: %s\n", regex);
   if (NULL != error_msg)
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
 
   GNUNET_free_non_null (p);
 
-  while (NULL != ctx.stack_tail)
+  while (NULL != (nfa = ctx.stack_head))
   {
-    GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
-    GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
-                                 ctx.stack_tail);
+    GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
+    GNUNET_REGEX_automaton_destroy (nfa);
   }
+
   return NULL;
 }
 
+
 /**
  * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
  *
@@ -2041,7 +2212,7 @@ construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
                       struct GNUNET_REGEX_Automaton *dfa,
                       struct GNUNET_REGEX_State *dfa_state)
 {
-  struct Transition *ctran;
+  struct GNUNET_REGEX_Transition *ctran;
   struct GNUNET_REGEX_State *state_iter;
   struct GNUNET_REGEX_State *new_dfa_state;
   struct GNUNET_REGEX_State *state_contains;
@@ -2079,6 +2250,7 @@ construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
   }
 }
 
+
 /**
  * Construct DFA for the given 'regex' of length 'len'
  *
@@ -2123,15 +2295,13 @@ GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
   // Minimize DFA
   dfa_minimize (&ctx, dfa);
 
-  // Calculate SCCs
-  scc_tarjan (&ctx, dfa);
-
   // Create proofs for all states
   automaton_create_proofs (dfa);
 
   return dfa;
 }
 
+
 /**
  * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
  * structure.
@@ -2147,8 +2317,8 @@ GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
   if (NULL == a)
     return;
 
-  GNUNET_free (a->regex);
-  GNUNET_free_non_null (a->computed_regex);
+  GNUNET_free_non_null (a->regex);
+  GNUNET_free_non_null (a->canonical_regex);
 
   for (s = a->states_head; NULL != s;)
   {
@@ -2160,125 +2330,6 @@ GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
   GNUNET_free (a);
 }
 
-/**
- * Save a state to an open file pointer. cls is expected to be a file pointer to
- * an open file. Used only in conjunction with
- * GNUNET_REGEX_automaton_save_graph.
- *
- * @param cls file pointer
- * @param s state
- */
-void
-GNUNET_REGEX_automaton_save_graph_step (void *cls, struct GNUNET_REGEX_State *s)
-{
-  FILE *p;
-  struct Transition *ctran;
-  char *s_acc = NULL;
-  char *s_tran = NULL;
-
-  p = cls;
-
-  if (s->accepting)
-  {
-    GNUNET_asprintf (&s_acc,
-                     "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
-                     s->name, s->scc_id);
-  }
-  else
-  {
-    GNUNET_asprintf (&s_acc, "\"%s\" [color=\"0.%i 0.8 0.95\"];\n", s->name,
-                     s->scc_id);
-  }
-
-  if (NULL == s_acc)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n", s->name);
-    return;
-  }
-  fwrite (s_acc, strlen (s_acc), 1, p);
-  GNUNET_free (s_acc);
-  s_acc = NULL;
-
-  for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
-  {
-    if (NULL == ctran->to_state)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  "Transition from State %i has has no state for transitioning\n",
-                  s->id);
-      continue;
-    }
-
-    if (ctran->label == 0)
-    {
-      GNUNET_asprintf (&s_tran,
-                       "\"%s\" -> \"%s\" [label = \"epsilon\", color=\"0.%i 0.8 0.95\"];\n",
-                       s->name, ctran->to_state->name, s->scc_id);
-    }
-    else
-    {
-      GNUNET_asprintf (&s_tran,
-                       "\"%s\" -> \"%s\" [label = \"%c\", color=\"0.%i 0.8 0.95\"];\n",
-                       s->name, ctran->to_state->name, ctran->label, s->scc_id);
-    }
-
-    if (NULL == s_tran)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
-                  s->name);
-      return;
-    }
-
-    fwrite (s_tran, strlen (s_tran), 1, p);
-    GNUNET_free (s_tran);
-    s_tran = NULL;
-  }
-}
-
-/**
- * Save the given automaton as a GraphViz dot file
- *
- * @param a the automaton to be saved
- * @param filename where to save the file
- */
-void
-GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
-                                   const char *filename)
-{
-  char *start;
-  char *end;
-  FILE *p;
-
-  if (NULL == a)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
-    return;
-  }
-
-  if (NULL == filename || strlen (filename) < 1)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
-    return;
-  }
-
-  p = fopen (filename, "w");
-
-  if (NULL == p)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
-                filename);
-    return;
-  }
-
-  start = "digraph G {\nrankdir=LR\n";
-  fwrite (start, strlen (start), 1, p);
-
-  automaton_traverse (p, a, GNUNET_REGEX_automaton_save_graph_step);
-
-  end = "\n}\n";
-  fwrite (end, strlen (end), 1, p);
-  fclose (p);
-}
 
 /**
  * Evaluates the given string using the given DFA automaton
@@ -2320,6 +2371,7 @@ evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
   return 1;
 }
 
+
 /**
  * Evaluates the given string using the given NFA automaton
  *
@@ -2335,7 +2387,7 @@ evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
   struct GNUNET_REGEX_State *s;
   struct GNUNET_REGEX_StateSet *sset;
   struct GNUNET_REGEX_StateSet *new_sset;
-  int i;
+  unsigned int i;
   int result;
 
   if (NFA != a->type)
@@ -2350,7 +2402,6 @@ evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
     return 0;
 
   result = 1;
-  strp = string;
   sset = nfa_closure_create (a, a->start, 0);
 
   for (strp = string; NULL != strp && *strp; strp++)
@@ -2375,6 +2426,7 @@ evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
   return result;
 }
 
+
 /**
  * Evaluates the given 'string' against the given compiled regex
  *
@@ -2406,25 +2458,31 @@ GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
   return result;
 }
 
+
 /**
- * Get the computed regex of the given automaton.
+ * Get the canonical regex of the given automaton.
  * When constructing the automaton a proof is computed for each state,
  * consisting of the regular expression leading to this state. A complete
  * regex for the automaton can be computed by combining these proofs.
- * As of now this computed regex is only useful for testing.
+ * As of now this function is only useful for testing.
+ *
+ * @param a automaton for which the canonical regex should be returned.
+ *
+ * @return
  */
 const char *
-GNUNET_REGEX_get_computed_regex (struct GNUNET_REGEX_Automaton *a)
+GNUNET_REGEX_get_canonical_regex (struct GNUNET_REGEX_Automaton *a)
 {
   if (NULL == a)
     return NULL;
 
-  return a->computed_regex;
+  return a->canonical_regex;
 }
 
+
 /**
  * Get the first key for the given 'input_string'. This hashes the first x bits
- * of the 'input_strings'.
+ * of the 'input_string'.
  *
  * @param input_string string.
  * @param string_len length of the 'input_string'.
@@ -2433,13 +2491,13 @@ GNUNET_REGEX_get_computed_regex (struct GNUNET_REGEX_Automaton *a)
  * @return number of bits of 'input_string' that have been consumed
  *         to construct the key
  */
-unsigned int
-GNUNET_REGEX_get_first_key (const char *input_string, unsigned int string_len,
-                            GNUNET_HashCode * key)
+size_t
+GNUNET_REGEX_get_first_key (const char *input_string, size_t string_len,
+                            struct GNUNET_HashCode * key)
 {
   unsigned int size;
 
-  size = string_len < initial_bits ? string_len : initial_bits;
+  size = string_len < INITIAL_BITS ? string_len : INITIAL_BITS;
 
   if (NULL == input_string)
   {
@@ -2452,20 +2510,142 @@ GNUNET_REGEX_get_first_key (const char *input_string, unsigned int string_len,
   return size;
 }
 
+
 /**
  * Check if the given 'proof' matches the given 'key'.
  *
- * @param proof partial regex
- * @param key hash
+ * @param proof partial regex of a state.
+ * @param key hash of a state.
  *
- * @return GNUNET_OK if the proof is valid for the given key
+ * @return GNUNET_OK if the proof is valid for the given key.
  */
 int
-GNUNET_REGEX_check_proof (const char *proof, const GNUNET_HashCode * key)
+GNUNET_REGEX_check_proof (const char *proof, const struct GNUNET_HashCode *key)
+{
+  struct GNUNET_HashCode key_check;
+
+  GNUNET_CRYPTO_hash (proof, strlen (proof), &key_check);
+  return (0 ==
+          GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO;
+}
+
+
+/**
+ * Recursive helper function for iterate_initial_edges. Will call iterator
+ * function for each initial state.
+ *
+ * @param min_len minimum length of the path in the graph.
+ * @param max_len maximum length of the path in the graph.
+ * @param cur_len current length of the path already traversed.
+ * @param consumed_string string consumed by traversing the graph till this state.
+ * @param state current state of the automaton.
+ * @param iterator iterator function called for each edge.
+ * @param iterator_cls closure for the iterator function.
+ */
+static void
+iterate_initial_edge (const unsigned int min_len, const unsigned int max_len,
+                      unsigned int cur_len, char *consumed_string,
+                      struct GNUNET_REGEX_State *state,
+                      GNUNET_REGEX_KeyIterator iterator, void *iterator_cls)
+{
+  unsigned int i;
+  char label[state->transition_count][2];
+  char *temp;
+  struct GNUNET_REGEX_Transition *t;
+  unsigned int num_edges = state->transition_count;
+  struct GNUNET_REGEX_Edge edges[num_edges];
+  struct GNUNET_HashCode hash;
+
+  if (cur_len > min_len && NULL != consumed_string && cur_len <= max_len)
+  {
+    for (i = 0, t = state->transitions_head; NULL != t; t = t->next, i++)
+    {
+      label[i][0] = t->label;
+      label[i][1] = '\0';
+      edges[i].label = label[i];
+      edges[i].destination = t->to_state->hash;
+    }
+
+    GNUNET_CRYPTO_hash (consumed_string, strlen (consumed_string), &hash);
+    iterator (iterator_cls, &hash, consumed_string, state->accepting, num_edges,
+              edges);
+  }
+
+  if (cur_len < max_len)
+  {
+    cur_len++;
+    for (t = state->transitions_head; NULL != t; t = t->next)
+    {
+      if (NULL != consumed_string)
+        GNUNET_asprintf (&temp, "%s%c", consumed_string, t->label);
+      else
+        GNUNET_asprintf (&temp, "%c", t->label);
+
+      iterate_initial_edge (min_len, max_len, cur_len, temp, t->to_state,
+                            iterator, iterator_cls);
+      GNUNET_free (temp);
+    }
+  }
+}
+
+
+/**
+ * Iterate over all initial edges that aren't actually part of the automaton.
+ * This is needed to find the initial states returned by
+ * GNUNET_REGEX_get_first_key. Iteration will start at the first branch state (a
+ * state that has more than one outgoing edge, can be the first state), because
+ * all previous states will have the same proof and be iterated over in
+ * iterate_all_edges.
+ *
+ * @param a the automaton for which the initial states should be computed.
+ * @param initial_len length of the initial state string.
+ * @param iterator iterator function called for each edge.
+ * @param iterator_cls closure for the iterator function.
+ */
+void
+iterate_initial_edges (struct GNUNET_REGEX_Automaton *a,
+                       const unsigned int initial_len,
+                       GNUNET_REGEX_KeyIterator iterator, void *iterator_cls)
 {
-  return GNUNET_OK;
+  char *consumed_string;
+  char *temp;
+  struct GNUNET_REGEX_State *s;
+  unsigned int cur_len;
+
+  if (1 > initial_len)
+    return;
+
+  consumed_string = NULL;
+  s = a->start;
+  cur_len = 0;
+
+  if (1 == s->transition_count)
+  {
+    do
+    {
+      if (NULL != consumed_string)
+      {
+        temp = consumed_string;
+        GNUNET_asprintf (&consumed_string, "%s%c", consumed_string,
+                         s->transitions_head->label);
+        GNUNET_free (temp);
+      }
+      else
+        GNUNET_asprintf (&consumed_string, "%c", s->transitions_head->label);
+
+      s = s->transitions_head->to_state;
+      cur_len++;
+    }
+    while (cur_len < initial_len && 1 == s->transition_count);
+  }
+
+  iterate_initial_edge (cur_len, initial_len, cur_len, consumed_string, s,
+                        iterator, iterator_cls);
+
+  GNUNET_free_non_null (consumed_string);
 }
 
+
 /**
  * Iterate over all edges helper function starting from state 's', calling
  * iterator on for each edge.
@@ -2478,7 +2658,7 @@ static void
 iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
               void *iterator_cls)
 {
-  struct Transition *t;
+  struct GNUNET_REGEX_Transition *t;
   struct GNUNET_REGEX_Edge edges[s->transition_count];
   unsigned int num_edges;
 
@@ -2488,13 +2668,16 @@ iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
 
     num_edges = state_get_edges (s, edges);
 
-    iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges, edges);
+    if (0 < strlen (s->proof) || s->accepting)
+      iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges,
+                edges);
 
     for (t = s->transitions_head; NULL != t; t = t->next)
       iterate_edge (t->to_state, iterator, iterator_cls);
   }
 }
 
+
 /**
  * Iterate over all edges starting from start state of automaton 'a'. Calling
  * iterator for each edge.
@@ -2513,5 +2696,6 @@ GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
   for (s = a->states_head; NULL != s; s = s->next)
     s->marked = GNUNET_NO;
 
+  iterate_initial_edges (a, INITIAL_BITS, iterator, iterator_cls);
   iterate_edge (a->start, iterator, iterator_cls);
 }