Pattern matching algorithms in Blockchain for network fees reduction
Robert Susik, Robert Nowotniak
TL;DR
This work tackles the problem of exact pattern matching on Ethereum's EVM to reduce gas costs for smart contracts. It implements and adapts several classic algorithms—Naive, Knuth–Morris–Pratt, Boyer–Moore–Horspool, Rabin–Karp, Shift-Or, Backward Nondeterministic Dawg Matching—in Solidity with YUL assembly, considering the EVM's 32-byte word size and gas model. Extensive experiments on datasets from the Pizza & Chili corpus show that the proposed implementations can achieve substantial savings over a widely used Solidity library (StringUtils), with gas reductions up to $22\times$ and execution-time improvements up to $55\times$, depending on pattern length $m$ and text size $n$ (e.g., $m$ up to 512 and $n$ up to 128 KiB). The results demonstrate practical gas-efficient building blocks for Dapps requiring substring search, highlighting the trade-offs among algorithms and providing a foundation for further optimization on the EVM, including potential extensions to larger patterns and alternative datasets. $m \le 512$, $n \le 128\mathrm{KiB}$ are the focal regimes studied, illustrating how algorithmic choices translate into tangible cost savings on blockchain platforms.
Abstract
Blockchain received a vast amount of attention in recent years and is still growing. The second generation of blockchain, such as Ethereum, allows execution of almost any program in Ethereum Virtual Machine (EVM), making it a global protocol for distributed applications. The code deployment and each operation performed in EVM cost the network fee called gas, which price varies and can be significant. That is why code optimization and well-chosen algorithms are crucial in programming on the blockchain. This paper evaluates the gas usage of several exact pattern matching algorithms on the Ethereum Virtual Machine. We also propose an efficient implementation of the algorithms in the Solidity/YUL language. We evaluate the gas fees of all the algorithms for different parameters (such as pattern length, alphabet size, and text size). We show a significant gas fee and execution time reduction with up to 22-fold lower gas usage and 55-fold speed-up comparing to StringUtils (a popular Solidity string library).
