Skip to main content

TVM vs EVM

Introduction

Ethereum Virtual Machine (EVM) and TON Virtual Machine (TVM) are stack-based virtual machines developed for running smart contract code. Although they have standard features, there are notable distinctions between them.

Differences of TVM and EVM

Data presentation

EVM

  1. Fundamental Data Units The EVM operates primarily on 256-bit integers, reflecting its design around Ethereum's cryptographic functions, such as Keccak-256 hashing and elliptic curve operations.
  • Data types are limited mainly to integers, bytes, and occasionally arrays of these types, but all must conform to 256-bit processing rules.
  1. State Storage
  • The entire state of the Ethereum blockchain is a mapping of 256-bit addresses to 256-bit values. A data structure known as the Merkle Patricia Trie (MPT) maintains this mapping.
  • The MPT enables Ethereum to efficiently prove the consistency and integrity of the blockchain state through cryptographic verification, which is vital for a decentralized system like Ethereum.
  1. Data Structure Limitations
  • The simplification to 256-bit word constraints means that the EVM is not inherently designed to handle complex or custom data structures directly. Developers often need to implement additional logic within smart contracts to simulate more complex data structures, which can increase gas costs and complexity.

TVM

  1. Cell-Based Architecture
  • TVM uses a unique bag of cells model to represent data. Each cell can contain up to 128 data bytes and have up to 4 references to other cells.
  • This structure allows the TVM to natively support arbitrary algebraic data types and more complex constructions such as trees or directed acyclic graphs (DAGs) directly within its storage model.
  1. Flexibility and Efficiency
  • The cell model provides significant flexibility, enabling the TVM to handle various data structures more naturally and efficiently than the EVM.
  • For example, creating linked structures through cell references allows for dynamic and potentially infinite data structures, which is crucial for specific applications like decentralized social networks or complex decentralized finance (DeFi) protocols.
  1. Complex Data Handling
  • The ability to manage complex data types inherently within the VM architecture reduces the need for workaround implementations in smart contracts, potentially lowering the execution cost and increasing execution speed. TVM's design is particularly advantageous for applications requiring complex state management or interlinked data structures. It provides a robust foundation for developers to build sophisticated and scalable decentralized applications.

Stack machine

EVM

  • The EVM operates as a traditional stack-based machine, using a last-in, first-out (LIFO) stack to manage computation.
  • It processes operations by pushing and popping 256-bit integers, the standard size for all elements in the stack.

TVM

  • TVM also functions as a stack-based machine but with a key distinction: it supports both 257-bit integers and references to cells.
  • This allows TVM to push and pop these two distinct types of data onto/from the stack, providing enhanced flexibility in direct data manipulation.

Example of stack operations

Suppose we want to add two numbers, 2 and 2, in EVM. The process would involve pushing the numbers onto the stack and calling the ADD instruction. The result, 4, would be left on top of the stack.

We can do this operation in the same way in TVM. But let’s look at another example with more complex data structures, such as hashmaps and cell references. Suppose we have a hashmap that stores key-value pairs, where keys are integers and values are either integers or cell references. Let’s say our hashmap contains the following entries:

{
1: 10
2: cell_a (which contains 10)
}

We want to add the values associated with keys 1 and 2 and store the result with key 3. Let’s look at stack operations:

  1. Push key 1 onto the stack: stack = (1)
  2. Call DICTGET for key 1 (retrieves the value associated with the key at the top of the stack): Retrieves value 10. stack = (10)
  3. Push key 2 onto the stack: stack = (10, 2)
  4. Call DICTGET for key 2: Retrieves reference to Cell_A. stack = (10, Cell_A)
  5. Load value from Cell_A: TVM executes an instruction to load the value from the cell reference. stack = (10, 10)
  6. Call the ADD instruction: When the ADD instruction is executed, the TVM will pop the top two elements from the stack, add them together, and push the result back onto the stack. In this case, the top two elements are 10 and 10. After the addition, the stack will contain the result: stack = (20)
  7. Push key 3 onto the stack: stack = (20, 3)
  8. Call DICTSET: Stores 20 with key 3. Updated hashmap:
{
1: 10,
2: cell_a,
3: 20
}

To do the same in EVM, we need to define a mapping that stores key-value pairs and the function where we work directly with 256-bit integers stored in the mapping. It’s essential to note that the EVM supports complex data structures by leveraging Solidity, but these structures are built on top of the EVM’s simpler data model, which is fundamentally different from the TVM's more expressive data model.

Arithmetic operations

EVM

  • The Ethereum Virtual Machine (EVM) uses 256-bit integers to handle arithmetic operations such as addition, subtraction, multiplication, and division, tailoring them to this data size.

TVM

  • The TON Virtual Machine (TVM) supports more diverse arithmetic operations, including 64-bit, 128-bit, and 256-bit integers, both unsigned and signed and modulo operations. TVM further enhances its arithmetic capabilities with multiplication-then-shift and shift-then-divide, which are particularly useful for implementing fixed-point arithmetic. This variety allows developers to select the most efficient arithmetic operations based on the specific requirements of their smart contracts, offering potential optimizations based on data size and type.

Overflow checks

EVM

  • In the EVM, the virtual machine does not perform overflow checks inherently. With the introduction of Solidity 0.8.0, automatic overflow and underflow checks were integrated into the language to enhance security. These checks help prevent common vulnerabilities related to arithmetic operations but require newer versions of Solidity, as earlier versions necessitate manual implementation of these safeguards.

TVM

  • In contrast, TVM automatically performs overflow checks on all arithmetic operations, a feature built directly into the virtual machine. This design choice simplifies the development of smart contracts by inherently reducing the risk of errors and enhancing the overall reliability and security of the code.

Cryptography and hash functions

EVM

EVM supports Ethereum-specific cryptography schemes, such as the secp256k1 elliptic curve and the keccak256 hash function. However, it does not have built-in support for Merkle proofs, which are cryptographic proofs used to verify the membership of an element in a set.

TVM

  • TVM supports 256-bit Elliptic Curve Cryptography (ECC) for predefined curves, like Curve25519. It also supports Weil pairings on some elliptic curves, which are helpful for the fast implementation of zk-SNARKs (zero-knowledge proofs). Popular hash functions like sha256 are also supported, providing more cryptographic operation options. In addition, TVM can work with Merkle proofs, providing additional cryptographic features that can be beneficial for specific use cases, such as verifying the inclusion of a transaction in a block.

High-level languages

EVM

EVM primarily uses Solidity as its high-level language, an object-oriented, statically typed language similar to JavaScript. Other languages, such as Vyper and Yul, are also used for writing Ethereum smart contracts.

TVM

  • TVM uses FunC as a high-level language for writing TON smart contracts. It is a procedural language with static types and support for algebraic data types. FunC compiles to Fift, which in turn compiles to TVM bytecode.

Conclusion

In summary, while EVM and TVM are stack-based machines designed to execute smart contracts, TVM offers more flexibility, support for a broader range of data types and structures, built-in overflow checks, and advanced cryptographic features.

TVM’s support for sharding-aware smart contracts and its unique data representation approach make it better suited for specific use cases and scalable blockchain networks.

See also