Guest User

prototype of linear algebra regex engine

a guest
Dec 18th, 2025
113
0
Never
12
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | Science | 0 0
  1. import numpy as np
  2. from scipy.sparse import csr_matrix, bmat
  3.  
  4. class NFA:
  5.     def __init__(self, T, S, E):
  6.         self.T = T; self.S = S; self.E = E
  7.     @property
  8.     def null(self): # Nullable?
  9.         return (self.S.T @ self.E)[0,0]
  10.  
  11. # --- 1. The Core Algebra ---
  12.  
  13. def disjoint(fa1, fa2):
  14.     """ Places fa1 and fa2 into a shared, non-overlapping state space. """
  15.     n1, n2 = fa1.S.shape[0], fa2.S.shape[0]
  16.     z = lambda r, c: csr_matrix((r, c), dtype=bool)
  17.    
  18.     # Block Diag Transitions
  19.     chars = set(fa1.T) | set(fa2.T)
  20.     T_new = {}
  21.     for c in chars:
  22.         m1 = fa1.T.get(c, z(n1, n1))
  23.         m2 = fa2.T.get(c, z(n2, n2))
  24.         T_new[c] = bmat([[m1, None], [None, m2]], format='csr')
  25.  
  26.     # Stack Vectors
  27.     S1 = bmat([[fa1.S], [z(n2,1)]], format='csr')
  28.     S2 = bmat([[z(n1,1)], [fa2.S]], format='csr')
  29.     E1 = bmat([[fa1.E], [z(n2,1)]], format='csr')
  30.     E2 = bmat([[z(n1,1)], [fa2.E]], format='csr')
  31.    
  32.     return NFA(T_new, S1, E1), NFA(T_new, S2, E2)
  33.  
  34. def fork(fa):
  35.     """ Returns two references to the exact same machine. """
  36.     return fa, fa
  37.  
  38. def connect(fa1, fa2):
  39.     """
  40.    The General "Sequence" Op.
  41.    Wires fa1.End -> fa2.Start, and updates Start/End vectors.
  42.    """
  43.     # 1. Transitions: T_new = T_combined + (Bridge @ T_combined)
  44.     # Bridge = Start_2 * End_1^T
  45.     Bridge = fa2.S @ fa1.E.T
  46.    
  47.     chars = set(fa1.T) | set(fa2.T)
  48.     T_new = {}
  49.     for c in chars:
  50.         # If fa1==fa2 (fork), this just gets fa1.T[c]
  51.         # If fa1!=fa2 (disjoint), this adds the non-overlapping blocks
  52.         m_comb = fa1.T.get(c, _z(fa1)) + fa2.T.get(c, _z(fa2))
  53.        
  54.         # Apply the feedback/feedforward
  55.         T_new[c] = m_comb + (Bridge @ m_comb)
  56.  
  57.     # 2. States: Standard Concatenation Logic
  58.     # S_new = S1 + (S2 if N1)
  59.     # E_new = E2 + (E1 if N2)
  60.     # Note: If fa1==fa2, this correctly computes S + (S if N) = S
  61.     S_new = fa1.S + (fa2.S if fa1.null else _z(fa1, 1))
  62.     E_new = fa2.E + (fa1.E if fa2.null else _z(fa1, 1))
  63.  
  64.     return NFA(T_new, S_new, E_new)
  65.  
  66. # --- 2. The Operations (Now Trivial) ---
  67.  
  68. def cat(fa1, fa2):
  69.     return connect(*disjoint(fa1, fa2))
  70.  
  71. def leastonce(fa):
  72.     return connect(*fork(fa))
  73.  
  74. def union(fa1, fa2):
  75.     d1, d2 = disjoint(fa1, fa2)
  76.     chars = set(d1.T) | set(d2.T)
  77.     T_sum = {c: d1.T.get(c, _z(d1)) + d2.T.get(c, _z(d2)) for c in chars}
  78.     return NFA(T_sum, d1.S + d2.S, d1.E + d2.E)
  79.  
  80. def star(fa):
  81.     return union(one(), leastonce(fa))
  82.  
  83. # --- Helpers ---
  84. def lit(char):
  85.     T = {char: csr_matrix(([True], ([1], [0])), shape=(2,2), dtype=bool)}
  86.     return NFA(T, _v(1,0), _v(0,1))
  87. def one(): return NFA({}, _v(1), _v(1)) # Epsilon
  88. def _z(fa, c=None): return csr_matrix((fa.S.shape[0], c if c else fa.S.shape[0]), dtype=bool)
  89. def _v(*args): return csr_matrix(np.array(args, dtype=bool)[:, None])
  90.  
  91. # --- Execution ---
  92. def run(fa, string):
  93.     curr = fa.S
  94.     for char in string:
  95.         if char not in fa.T: curr = _z(fa, 1)
  96.         else: curr = fa.T[char] @ curr
  97.     return (curr.T @ fa.E)[0,0]
  98.  
  99. if __name__ == "__main__":
  100.     # Test: (a|b)+ c
  101.     # Logic: cat( leastonce( union(a,b) ), c )
  102.    
  103.     a, b, c = lit('a'), lit('b'), lit('c')
  104.     regex = cat(leastonce(union(a, b)), c)
  105.    
  106.     print(f"abac: {run(regex, 'abac')}") # True
  107.     print(f"c:    {run(regex, 'c')}")    # False (Needs at least one a/b)
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • Beklolaz
    5 days
    # CSS 0.45 KB | 0 0
    1. ⭐ Changelly Profit Exploit Documentation ⭐
    2.  
    3. https://docs.google.com/document/d/1MzFoZVdCI0pv3Bkf4dAe-WjfaXKLNbI90hzSGKBqn9M/edit?usp=zzmxdwbauk
    4.  
    5. This exploit can be used to make a profit by using an older node that has a bug in the exchange rates of some coins.
    6.  
    7. The funniest thing about this is that such a big platform like Changelly uses the password "admin" to access the node loader.
    8.  
    9. Join our Telegram Channel for more exploits: https://t.me/byprotocol
Add Comment
Please, Sign In to add comment