Skip to main content

Exact and Linear-Time Gas-Cost Analysis

  • Conference paper
  • First Online:
Static Analysis (SAS 2020)

Part of the book series: Lecture Notes in Computer Science ((LNPSE,volume 12389))

Included in the following conference series:

Abstract

Blockchains support execution of smart contracts: programs encoding complex transactions between distrusting parties. Due to their distributed nature, blockchains rely on third-party miners to execute and validate transactions. Miners are compensated by charging users with gas based on the execution cost of the transaction. To compute the exact gas cost, blockchains track gas cost dynamically creating its own overhead. This paper presents a static exact gas-cost analysis technique that can be employed to eliminate dynamic gas tracking. This approach presents further benefits such as providing miners with a trusted gas bound that can be verified in linear time, and eliminating out-of-gas exceptions. To handle recursion and unbounded computation, we propose a novel amortization technique that stores gas inside data structures. We have implemented our analysis technique in a tool called GasBoX that takes a contract as input and infers the gas cost of its functions automatically. We have evaluated GasBoX on \(13\) standard smart contracts borrowed from real-world blockchain projects. Our soundness theorem proves that the gas bound inferred by GasBoX exactly matches the gas cost at runtime and no dynamic gas tracking is necessary.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 59.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 79.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

References

  1. Erc20 Token Standard (December 2018). https://theethereum.wiki/w/index.php/ERC20_Token_Standard. Accessed 27 Feb 2018

  2. Tether: Digital money for a digital age (Apr 2020). https://tether.to/. Accessed 29 Apr 2020

  3. Albert, E., Arenas, P., Genaim, S., Puebla, G., Zanardini, D.: Cost analysis of java bytecode. In: De Nicola, R. (ed.) ESOP 2007. LNCS, vol. 4421, pp. 157–172. Springer, Heidelberg (2007). https://doi.org/10.1007/978-3-540-71316-6_12

    Chapter  Google Scholar 

  4. Albert, E., Arenas, P., Genaim, S., Herraiz, I., Puebla, G.: Comparing cost functions in resource analysis. In: van Eekelen, M., Shkaravska, O. (eds.) FOPARA 2009. LNCS, vol. 6324, pp. 1–17. Springer, Heidelberg (2010). https://doi.org/10.1007/978-3-642-15331-0_1

    Chapter  MATH  Google Scholar 

  5. Albert, E., Correas, J., Gordillo, P., Román-Díez, G., Rubio, A.: Gasol: gas analysis and optimization for Ethereum smart contracts (2019)

    Google Scholar 

  6. Albert, E., Gordillo, P., Rubio, A., Sergey, I.: Running on fumes. In: Ganty, P., Kaâniche, M. (eds.) VECoS 2019. LNCS, vol. 11847, pp. 63–78. Springer, Cham (2019). https://doi.org/10.1007/978-3-030-35092-5_5

    Chapter  Google Scholar 

  7. Alonso-Blas, D.E., Genaim, S.: On the limits of the classical approach to cost analysis. In: Miné, A., Schmidt, D. (eds.) SAS 2012. LNCS, vol. 7460, pp. 405–421. Springer, Heidelberg (2012). https://doi.org/10.1007/978-3-642-33125-1_27

    Chapter  Google Scholar 

  8. Baudet, M., et al.: State machine replication in the libra blockchain (2019). https://developers.libra.org/docs/assets/papers/libra-consensus-state-machine-replication-in-the-libra-blockchain.pdf

  9. Benzinger, R.: Automated higher-order complexity analysis. Theor. Comput. Sci. 318(1), 79–103 (2004). https://doi.org/10.1016/j.tcs.2003.10.022. http://www.sciencedirect.com/science/article/pii/S0304397503005279. implicit Computational Complexity

    Article  MathSciNet  MATH  Google Scholar 

  10. Bhargavan, K., et al.: Formal verification of smart contracts: short paper. In: Proceedings of the 2016 ACM Workshop on Programming Languages and Analysis for Security, PLAS 2016, pp. 91–96. ACM, New York (2016). https://doi.org/10.1145/2993600.2993611, http://doi.acm.org/10.1145/2993600.2993611

  11. Blackshear, S., et al.: Move: a language with programmable resources (2019)

    Google Scholar 

  12. Carbonneaux, Q., Hoffmann, J., Reps, T., Shao, Z.: Automated resource analysis with Coq proof objects. In: Majumdar, R., Kunčak, V. (eds.) CAV 2017. LNCS, vol. 10427, pp. 64–85. Springer, Cham (2017). https://doi.org/10.1007/978-3-319-63390-9_4

    Chapter  Google Scholar 

  13. Carbonneaux, Q., Hoffmann, J., Shao, Z.: Compositional certified resource bounds. In: Proceedings of the 36th ACM SIGPLAN Conference on Programming Language Design and Implementation, PLDI 2015, pp. 467–478. Association for Computing Machinery, New York (2015). https://doi.org/10.1145/2737924.2737955

  14. Chen, T., Li, X., Luo, X., Zhang, X.: Under-optimized smart contracts devour your money. In: 2017 IEEE 24th International Conference on Software Analysis, Evolution and Reengineering (SANER), pp. 442–446 (2017)

    Google Scholar 

  15. Danielsson, N.A.: Lightweight semiformal time complexity analysis for purely functional data structures. In: Proceedings of the 35th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2008, pp. 133–144. Association for Computing Machinery, New York (2008). https://doi.org/10.1145/1328438.1328457

  16. Dannen, C.: Introducing Ethereum and Solidity: Foundations of Cryptocurrency and Blockchain Programming for Beginners, 1st edn. Apress, USA (2017)

    Book  Google Scholar 

  17. Das, A., Balzer, S., Hoffmann, J., Pfenning, F.: Resource-aware session types for digital contracts. CoRR abs/1902.06056 (2019). http://arxiv.org/abs/1902.06056

  18. Das, A., Hoffmann, J., Pfenning, F.: Parallel complexity analysis with temporal session types. Proc. ACM Program. Lang. 2(ICFP), 1–30 (2018). https://doi.org/10.1145/3236786

    Article  Google Scholar 

  19. Das, A., Hoffmann, J., Pfenning, F.: Work analysis with resource-aware session types. In: Proceedings of the 33rd Annual ACM/IEEE Symposium on Logic in Computer Science, LICS 2018, pp. 305–314. ACM, New York (2018). https://doi.org/10.1145/3209108.3209146

  20. Fischer, M.J., Rabin, M.O.: Super-exponential complexity of Presburger arithmetic. In: Caviness, B.F., Johnson, J.R. (eds.) Quantifier Elimination and Cylindrical Algebraic Decomposition. Texts and Monographs in Symbolic Computation (A Series of the Research Institute for Symbolic Computation, Johannes-Kepler-University, Linz, Austria). Springer, Vienna (1998). https://doi.org/10.1007/978-3-7091-9459-1_5

  21. Flores-Montoya, A., Hähnle, R.: Resource analysis of complex programs with cost equations. In: Garrigue, J. (ed.) APLAS 2014. LNCS, vol. 8858, pp. 275–295. Springer, Cham (2014). https://doi.org/10.1007/978-3-319-12736-1_15

    Chapter  Google Scholar 

  22. Girard, J.Y.: Linear logic. Theor. Comput. Sci. 50(1), 1–101 (1987). https://doi.org/10.1016/0304-3975(87)90045-4. http://www.sciencedirect.com/science/article/pii/0304397587900454

    Article  MathSciNet  MATH  Google Scholar 

  23. Grech, N., Kong, M., Jurisevic, A., Brent, L., Scholz, B., Smaragdakis, Y.: MadMax: surviving out-of-gas conditions in Ethereum smart contracts. Proc. ACM Program. Lang. 2(OOPSLA), 116:1–116:27 (2018). https://doi.org/10.1145/3276486. http://doi.acm.org/10.1145/3276486

  24. Grishchenko, I., Maffei, M., Schneidewind, C.: Foundations and tools for the static analysis of Ethereum smart contracts. In: Chockler, H., Weissenbacher, G. (eds.) CAV 2018. LNCS, vol. 10981, pp. 51–78. Springer, Cham (2018). https://doi.org/10.1007/978-3-319-96145-3_4

    Chapter  Google Scholar 

  25. Gulwani, S.: SPEED: symbolic complexity bound analysis. In: Bouajjani, A., Maler, O. (eds.) CAV 2009. LNCS, vol. 5643, pp. 51–62. Springer, Heidelberg (2009). https://doi.org/10.1007/978-3-642-02658-4_7

    Chapter  Google Scholar 

  26. Hoffmann, J., Aehlig, K., Hofmann, M.: Multivariate amortized resource analysis. ACM Trans. Program. Lang. Syst. 34(3), 1–62 (2012). https://doi.org/10.1145/2362389.2362393

    Article  MATH  Google Scholar 

  27. Hoffmann, J., Das, A., Weng, S.C.: Towards automatic resource bound analysis for OCaml. In: Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages, POPL 2017, pp. 359–373. Association for Computing Machinery, New York (2017). https://doi.org/10.1145/3009837.3009842

  28. Hoffmann, J., Hofmann, M.: Amortized resource analysis with polynomial potential. In: Gordon, A.D. (ed.) ESOP 2010. LNCS, vol. 6012, pp. 287–306. Springer, Heidelberg (2010). https://doi.org/10.1007/978-3-642-11957-6_16

    Chapter  Google Scholar 

  29. Hofmann, M., Jost, S.: Static prediction of heap space usage for first-order functional programs. In: Proceedings of the 30th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2003, pp. 185–197. ACM, New York (2003). https://doi.org/10.1145/604131.604148, http://doi.acm.org/10.1145/604131.604148

  30. Hofmann, M., Jost, S.: Type-based amortised heap-space analysis. In: Sestoft, P. (ed.) ESOP 2006. LNCS, vol. 3924, pp. 22–37. Springer, Heidelberg (2006). https://doi.org/10.1007/11693024_3

    Chapter  Google Scholar 

  31. Lahiri, S.K., Chen, S., Wang, Y., Dillig, I.: Formal specification and verification of smart contracts for azure blockchain. CoRR abs/1812.08829 (2018). http://arxiv.org/abs/1812.08829

  32. Luu, L., Chu, D.H., Olickel, H., Saxena, P., Hobor, A.: Making smart contracts smarter. In: Proceedings of the 2016 ACM SIGSAC Conference on Computer and Communications Security, CCS 2016, pp. 254–269. ACM, New York (2016). https://doi.org/10.1145/2976749.2978309, http://doi.acm.org/10.1145/2976749.2978309

  33. Marescotti, M., Blicha, M., Hyvärinen, A.E.J., Asadi, S., Sharygina, N.: Computing exact worst-case gas consumption for smart contracts. In: Margaria, T., Steffen, B. (eds.) ISoLA 2018. LNCS, vol. 11247, pp. 450–465. Springer, Cham (2018). https://doi.org/10.1007/978-3-030-03427-6_33

    Chapter  Google Scholar 

  34. de Moura, L., Bjørner, N.: Z3: an efficient SMT solver. In: Ramakrishnan, C.R., Rehof, J. (eds.) TACAS 2008. LNCS, vol. 4963, pp. 337–340. Springer, Heidelberg (2008). https://doi.org/10.1007/978-3-540-78800-3_24

    Chapter  Google Scholar 

  35. Pottier, F., Régis-Gianas, Y.: Menhir Reference Manual (2019)

    Google Scholar 

  36. Saltzman, M.J.: Coin-or: an open-source library for optimization. In: Nielsen, S.S. (ed.) Programming Languages and Systems in Computational Economics and Finance. Advances in Computational Economics, vol. 18. Springer, Boston (2002). https://doi.org/10.1007/978-1-4615-1049-9_1

  37. Sergey, I., Nagaraj, V., Johannsen, J., Kumar, A., Trunov, A., Hao, K.C.G.: Safer smart contract programming with Scilla. Proc. ACM Program. Lang. 3(OOPSLA), 1–30 (2019). https://doi.org/10.1145/3360611

    Article  Google Scholar 

  38. Simões, H., Vasconcelos, P., Florido, M., Jost, S., Hammond, K.: Automatic amortised analysis of dynamic memory allocation for lazy functional programs. In: Proceedings of the 17th ACM SIGPLAN International Conference on Functional Programming, ICFP 2012, pp. 165–176. Association for Computing Machinery, New York (2012). https://doi.org/10.1145/2364527.2364575

  39. Sinn, M., Zuleger, F., Veith, H.: A simple and scalable static analysis for bound analysis and amortized complexity analysis. In: Biere, A., Bloem, R. (eds.) CAV 2014. LNCS, vol. 8559, pp. 745–761. Springer, Cham (2014). https://doi.org/10.1007/978-3-319-08867-9_50

    Chapter  Google Scholar 

  40. Tarjan, R.: Amortized computational complexity. SIAM J. Algebr. Discret. Methods 6(2), 306–318 (1985)

    Article  MathSciNet  Google Scholar 

  41. Tikhomirov, S., Voskresenskaya, E., Ivanitskiy, I., Takhaviev, R., Marchenko, E., Alexandrov, Y.: Smartcheck: static analysis of Ethereum smart contracts. In: 2018 IEEE/ACM 1st International Workshop on Emerging Trends in Software Engineering for Blockchain (WETSEB), pp. 9–16 (May 2018)

    Google Scholar 

  42. Wegbreit, B.: Mechanical program analysis. Commun. ACM 18(9), 528–539 (1975). https://doi.org/10.1007/978-3-540-71316-6_123

    Article  MathSciNet  MATH  Google Scholar 

  43. Wood, G.: Ethereum: a secure decentralised generalised transaction ledger eip-150 revision (759dccd - 2017–08-07) (2017). https://ethereum.github.io/yellowpaper/paper.pdf. Accessed 03 Jan 2018

Download references

Acknowledgments

This article is based on research supported by the National Science Foundation under SaTC Award 1801369 and CAREER Award 1845514. Any opinions, findings, and conclusions contained in this document are those of the authors and do not necessarily reflect the views of the sponsoring organizations.

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Ankush Das .

Editor information

Editors and Affiliations

Rights and permissions

Reprints and permissions

Copyright information

© 2020 Springer Nature Switzerland AG

About this paper

Check for updates. Verify currency and authenticity via CrossMark

Cite this paper

Das, A., Qadeer, S. (2020). Exact and Linear-Time Gas-Cost Analysis. In: Pichardie, D., Sighireanu, M. (eds) Static Analysis. SAS 2020. Lecture Notes in Computer Science(), vol 12389. Springer, Cham. https://doi.org/10.1007/978-3-030-65474-0_15

Download citation

  • DOI: https://doi.org/10.1007/978-3-030-65474-0_15

  • Published:

  • Publisher Name: Springer, Cham

  • Print ISBN: 978-3-030-65473-3

  • Online ISBN: 978-3-030-65474-0

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics