Foresight Ventures: WASM, the engine of a new era

Foresight Ventures
2023-11-09 14:17:56
Collection
Wasm and blockchain can be combined to provide more efficient, secure, and flexible blockchain applications and services.

Author: Mike, Foresight Ventures


TL;DR

WebAssembly (Wasm) is a portable, high-performance binary instruction format that can run in web browsers. It is designed as a universal compilation target that can be used for various programming languages and can run on different platforms.

Blockchain is a decentralized distributed ledger technology that ensures data security and credibility through the use of cryptography and consensus algorithms. Blockchain can be used for applications such as recording transactions, storing data, and executing smart contracts.

There are several relationships and application scenarios between Wasm and blockchain:

  1. Smart Contracts: Wasm can serve as the execution environment for smart contracts, allowing contracts to run on different blockchain platforms. The high performance and portability of Wasm enable more efficient execution of smart contracts and cross-platform usage.
  2. Cross-Chain Interaction: Wasm can be used to implement cross-chain interaction functionalities. By compiling the logic of different blockchains into Wasm code, the same logic can be executed on different blockchains, facilitating cross-chain data transfer and interaction.
  3. Off-Chain Computation: Wasm can be used for computations outside the blockchain, with the results submitted to the blockchain. This can improve computational efficiency and flexibility while maintaining data security and credibility.
  4. Data Privacy: Wasm can be used to implement data privacy protection on the blockchain. By compiling the processing logic of sensitive data into Wasm code and executing it on the blockchain, data privacy can be protected while ensuring the verifiability of computations.

In summary, Wasm and blockchain can be combined to provide more efficient, secure, and flexible blockchain applications and services. The portability and high performance of Wasm make it one of the important technologies in the blockchain field.

1. What is Web Assembly

WebAssembly is an efficient and lightweight instruction set standard developed by W3C (World Wide Web Consortium), hailed as a disruptor for the web and high performance, supporting cross-browser execution. This means we can compile different programming languages, including C/C++, Go, and Rust, into a unified standard binary format and run it in the browser with near-native code efficiency as a replacement for JavaScript.

WebAssembly, or Wasm for short, is memory-safe, platform-independent, and can be efficiently mapped to all types of CPU architectures, with the following main advantages:

  • Efficient: Wasm has a complete set of language features, and it is a small and fast-loading binary format aimed at fully utilizing hardware capabilities to achieve native language execution efficiency.
  • Secure: Wasm runs in a memory-safe, sandboxed execution environment and can even be implemented in existing JavaScript virtual machines. In the web environment, Wasm strictly adheres to the same-origin policy and browser security policies. During compilation, the interface is relatively minimal, and most Wasm applications cannot connect to the internet (due to lack of socket support), currently only supporting local databases. Many security issues arise from memory access during execution, and Wasm can avoid illegal memory access during compilation.
  • Compatibility: Wasm is designed to be versionless, feature-testable, and backward-compatible in the web. Wasm can be called by JavaScript, entering the JavaScript context, and can also invoke browser functionalities like a web API. Wasm can run not only in browsers but also in non-web environments (such as Node.js, Deno, IoT devices, etc.). Traditional methods may require multiple compilations, while Wasm only needs to be compiled once for plug-and-play operation.

Additionally, the web is the only truly universal platform that allows access to your applications on any device. This also allows you to maintain a single codebase, simplifying updates and ensuring all users can access your application. Wasm supports 64-bit and 32-bit integer operations, corresponding directly to CPU instructions. By removing floating-point operations, determinism can be easily achieved, which is necessary for consensus algorithms. Supported by the LLVM compiler infrastructure project, this means Wasm can benefit from over a decade of compiler optimizations from LLVM. Wasm is continuously developed by major companies such as Google, Apple, Microsoft, Mozilla, and Facebook, and the backends of the browsers developed by these companies support Wasm compilation.

The beauty of Wasm lies in its ability to run anywhere without the need for downloads or installations, as it is in binary format. With just one click, web applications can run immediately when needed. It is safer than directly downloading and running binary files because browsers have built-in security mechanisms that prevent the code running within them from harming your system. Sharing web applications is equally simple - links can be placed anywhere as clickable strings.

2. Why Do We Need Web Assembly

2.1 Web2

Due to the built-in functionalities of browsers and the interactivity provided by the web, the web has evolved from static hypertext content and small scripting languages into a powerful and popular platform filled with amazing applications and features. However, so far, web applications are still primarily driven by the same scripting language (JavaScript), yet the primary goal of JavaScript's design was never to achieve this.

JavaScript was originally a simple scripting language designed to bring some interactivity to web applications filled with lightweight hypertext documents. Its design is easy to learn and write, but it does not prioritize execution speed. Over the years, significant performance improvements in JavaScript parsing by browsers have led to substantial performance gains.

As JavaScript execution speeds have increased, the capabilities of what can be executed in browsers have greatly expanded. New APIs have brought functionalities such as interactive graphics, video streaming, offline browsing, and more. At the same time, more applications (previously limited to local applications) have begun to enter the web. Now you can easily edit documents and send emails in the browser, but in some areas, JavaScript's performance remains a challenge. Think about the software you use outside of the browser: games, video editing, 3D rendering, or music production. These applications require extensive computation and high performance. JavaScript struggles to meet these high-performance demands.

However, replacing JavaScript is not realistic and could take decades, as the entire internet relies on it. Moreover, there is a large group of people continuously improving JavaScript. Indeed, compared to other languages, JavaScript has some shortcomings in certain areas, such as null and ==, but these issues are not severe enough to warrant a complete overhaul of the technology.

Thus, WebAssembly will not replace JavaScript, but this does not mean that no one will use Wasm in the future. In fact, the use of Wasm will become increasingly widespread. This is because Wasm can bring powerful computational capabilities to web pages, such as image processing or gaming. With Wasm, it is possible to create a web-based Photoshop that runs well or a 3D game that can run at 60 frames per second or even higher in the browser. Games are particularly challenging because they require simultaneous execution of audio and video processing, as well as coordination of physical effects and AI. Wasm has the capability to run games efficiently in the browser, opening the door to bringing many other applications to the browser.

The above image shows a comparison of the workflows of JavaScript and Wasm, illustrating that Wasm is much simpler compared to JavaScript.

2.2 Web3

WASM VM

In 2018, discussions began within the Ethereum ecosystem about using Wasm VM as the virtual machine for smart contracts, as it was believed to perform better than EVM. Gavin Wood, the inventor of EVM, has expressed the feasibility of Wasm replacing EVM, and Vitalik has also stated that Ethereum 2.0 will upgrade to Wasm contracts (eWASM) to meet more development needs. Today, the development of Wasm contracts has already taken shape.

2.3 How is EVM Designed? Why is it Inefficient?

Architecture Size Too Large

Traditional computers have instruction sets that only accept 32-bit or 64-bit inputs. EVM is different and unique; it is a 256-bit computer, intentionally designed this way to facilitate the processing of Ethereum's hash algorithms, which explicitly produce 256-bit outputs.

However, the computers that actually run EVM programs need to split the 256-bit words into their local architectures to execute smart contracts, making the entire system very inefficient and impractical.

Additionally, if you want to implement a complex algorithm similar to SHA256 on Ethereum using its basic OPCODES, good luck! To address the high gas costs associated with running complex programs through the instruction set, Ethereum introduced the concept of precompilation, compiling programs into EVM and consuming fixed gas. One noteworthy precompilation is Ethereum's hash algorithm, as implementing this algorithm in the virtual machine would incur extremely high costs when the contract is called.

Bulky Precompilation

The problem with precompilation is that it continually increases the bulkiness and complexity of the virtual machine without addressing the core issue: the inefficient and crude design of the current instruction set and specifications.

What if we could define a new specification and instruction set that does not require precompiling these complex programs but can be efficiently implemented through basic instructions? This is where Wasm comes into play.

2.4 Comparison of EVM and WASM VM

  • Speed: Wasm is designed to provide faster execution speed than EVM. EVM may have efficiency issues when compiling and executing smart contracts, while Wasm improves loading speed and processing capability by directly converting to compiled code.
  • Precompilation: EVM relies on precompiled contracts to efficiently execute cryptographic computations, but this may lead to the risk of hard forks. Wasm eliminates the dependency on precompiled contracts, allowing developers to create efficient and fast smart contracts.
  • Transaction Fees: The faster Wasm virtual machine significantly increases transaction throughput, which can greatly reduce contract deployment and transaction costs. Wasm contracts effectively address the high transaction fees and congestion issues currently faced on Ethereum.
  • Flexibility and Interoperability: Wasm expands the range of languages available to smart contract developers, supporting the development of complex business logic using any high-level language compatible with Wasm (such as Rust, C++, JavaScript, etc.), meaning you can write smart contracts in any language you are familiar with, including the most mature Rust-based ink! or AssemblyScript-based Ask!.

The EWASM team is integrating WebAssembly into Ethereum to ensure that Ethereum's execution layer is more efficient and straightforward, making it suitable as a fully decentralized computing platform. Wasm has already been adopted as a standard by many other projects, including Dfinity and EOS, to strengthen their execution layers.

2.5 Stylus (Arbitrum)

The Stylus project is enhancing the execution performance of smart contracts on the Ethereum Layer 2 network Arbitrum by introducing a WebAssembly (WASM) virtual machine. Contracts can be executed faster than with Solidity while reducing gas costs. This makes it easier to build high-performance smart contracts on the Arbitrum network, currently supporting compilation from C, C++, and Rust.

Custom Precompilation Support: Stylus also supports custom precompilation, allowing developers to deploy their own Rust or C++ precompilations to the Arbitrum network. This can help introduce new cryptographic algorithms or other specific functionalities on-chain without waiting for on-chain upgrades. For example, tensor computation can be precompiled to reduce inference costs, which may benefit on-chain machine learning.

Interoperability with EVM: Stylus achieves integration with the existing Ethereum ecosystem through interoperability with the Ethereum Virtual Machine (EVM). This means Stylus contracts can interoperate with existing EVM contracts and share the same global state.

Reentrancy: Unlike Cosmos Wasm, the Stylus Rust SDK introduces reentrancy functionality and allows developers to enable it manually. This allows contracts to interoperate more flexibly, but developers need to manage state carefully to ensure security.

With the popularity of the Arbitrum ecosystem, Stylus may be the most significant Wasm integration, also enhancing Arbitrum's competitiveness in zkrollup.

2.6 Gear (Polkadot)

The Gear protocol is creating a technology that can be deployed as a Polkadot parachain, which will serve as a tool for hosting smart contracts. Like Polkadot, Gear also uses the Substrate framework. This simplifies the process of creating different blockchains for specific applications. Substrate provides a wide range of out-of-the-box functionalities, allowing developers to focus on creating custom engines on top of the protocol.

Previously, the cost of launching a blockchain was high, but Gear allows dApp developers to focus on their projects without having to build and operate an entire blockchain from scratch.

The main engine of the Gear protocol is the smart contract module. In the case of Gear, any smart contract is a WebAssembly program compiled from different languages (such as Rust, C, C++, etc.). For developers from outside the crypto world, the entry barrier is low because they can build smart contracts in a familiar environment. Developers find it easier to experiment with smart contract programming languages.

The smart contract architecture of Gear uses an actor model under the hood and provides the following features:

  • Persistent memory for immutable programs
  • Asynchronous message processing
  • A minimal, intuitive, and sufficient API surface for blockchain context
  • An actor communication proxy model between on-chain components provides higher composability and better compatibility with parallel code execution and sharding.

Each program has a fixed amount of memory, and Gear allows control over it. Programs can only read and write in their own memory and cannot access the memory space of other programs. Each program has its own independent memory space and can process information in parallel on Gear nodes.

2.7 CosmWasm (Cosmos)

CosmWasm is a modern, powerful Wasm-based smart contract platform that can be easily integrated into Cosmos-SDK. This showcases one of CosmWasm's main advantages: contracts written in CosmWasm are tightly integrated with IBC (Inter-Blockchain Communication), allowing developers and users to enter a multi-chain future, currently only supporting Rust.

Advantages of CosmWasm

  1. Security: Using Rust language enhances the security of smart contracts.
  2. Cross-Chain Compatibility: Support for the IBC protocol within the Cosmos ecosystem.
  3. Performance: CosmWasm has demonstrated higher efficiency and lower transaction fees in some cases compared to traditional EVM (Ethereum Virtual Machine).
  4. Developer-Friendly: The type safety and memory safety features of the Rust language can reduce certain types of errors in smart contracts.

Challenges and Limitations

  1. Learning Curve: Compared to more commonly used smart contract languages like Solidity, Rust may present a steeper learning curve for beginners. CosmWasm needs to support more language compilations to gain the potential for widespread adoption.
  2. Ecosystem and Tool Support: Although growing, the development tools and ecosystem of CosmWasm may still be relatively limited compared to mature smart contract platforms like Ethereum.
  3. Market Share and Recognition: In the smart contract platform space, CosmWasm may not be as well-known as Ethereum or Binance Smart Chain, affecting its ability to attract developers and users.
  4. Maintenance and Upgrade Challenges: Although CosmWasm provides contract upgrade functionality, maintaining and managing upgrades for smart contracts remains a complex task that requires careful handling to avoid security vulnerabilities.
  5. Compatibility Issues: For projects accustomed to EVM or other smart contract environments, migrating to CosmWasm may face compatibility challenges.

2.8 ZK-WASM

In addition to the Wasm virtual machine, there is a recently emerging technology called ZK-WASM, whose creators at Delphinus Labs have open-sourced the ZK-WASM code on GitHub. ZK-WASM allows developers to verify the correctness of executed computations without re-execution. By leveraging ZK-WASM, developers can flexibly use various programming languages to build ZKP applications. These applications can seamlessly execute in web browsers.

The concept of ZK-WASM is derived from ZKSNARK, which is a blend of SNARG and zero-knowledge proofs. To explain, typically, to use ZKSNARK, you need to write a program using an arithmetic circuit language or circuit-friendly language, such as Pinocchio, TinyRAM, Buffet/Pequin, Geppetto, xJsnark framework, ZoKrates, etc. This poses a barrier to existing programs, making it difficult for them to harness the power of ZKSNARK. However, there is another way, which is to use ZKSNARK at the bytecode level of the virtual machine instead of at the source code level, thereby implementing a virtual machine that supports ZKSNARK. Delphinus Labs adopted the latter approach, embedding the entire WASM virtual machine into the ZKSNARK circuit, allowing existing WASM applications to run directly on ZK-WASM without any modifications. Thus, cloud service providers can prove to any user that the computation results were computed honestly without disclosing any private information.

ZK-WASM offers various use cases, such as enabling ZK proofs of some operations in the browser to be on-chain, making web operations verifiable on the blockchain. It can also be used for oracles, off-chain computations, automation, connecting Web2 and Web3, and generating proofs for machine learning and data processing, as well as for gaming and social applications. As adoption increases, zkWASM will expand the possibilities of Web3 and bring Web2 developers into this transformative landscape.

Through the implementation of ZKWASM by Delphinus Lab, developers can harness the power of zero-knowledge proofs to enhance the security and privacy of their applications, paving the way for a more trustworthy and decentralized digital landscape.

3. Conclusion

The future of web performance and the execution layer of smart contract platforms is bright. Not only will dApps have higher performance, but integrating WASM will also make it easier for those familiar with mainstream languages like Rust and Go to develop smart contracts, rather than having to learn the various intricacies of Solidity or other blockchain development languages to create useful applications on Ethereum. According to Evans Data Corporation, there are nearly 27 million developers worldwide. This number is steadily increasing—growing by about 3% last year and expected to exceed 28.7 million by 2024. However, the number of developers on blockchain does not exceed 30,000, accounting for about one-thousandth of the total number of developers. Although this number has been steadily increasing, learning a new smart contract language may still be a barrier for developers entering the blockchain space.

However, more and more blockchains are beginning to support Web Assembly as the bytecode for compiled smart contracts. WASM brings not only efficiency, interoperability, and a wide range of application scenarios to blockchain but also serves as a key to liberating developers, lowering the entry barrier for developers to enter the blockchain. Imagine a future where Web2 developers can use their familiar Python, C++, and JavaScript to develop large applications on the blockchain when they want to try blockchain development, maximizing the value of decentralized networks. First, lowering the barriers for creators (developers), then lowering the barriers for users, moving towards mass adoption.

4. Index

https://blog.scottlogic.com/2022/06/20/state-of-wasm-2022.html

https://www.notion.so/18f67cee15c147dfae68b06269a455c0?pvs=21

https://wiki.polkadot.network/docs/learn-wasm

https://docs.arbitrum.io/stylus/stylus-gentle-introduction

https://medium.com/@gear_techs/introducing-gear-easy-to-use-polkadot-parachain-9ccd05437a9c

https://medium.com/cosmwasm/cosmwasm-for-ctos-f1ffa19cccb8

https://www.cncf.io/wp-content/uploads/2023/09/The-State-of-WebAssembly-2023.pdf

https://github.com/DelphinusLab/zkWasm

ChainCatcher reminds readers to view blockchain rationally, enhance risk awareness, and be cautious of various virtual token issuances and speculations. All content on this site is solely market information or related party opinions, and does not constitute any form of investment advice. If you find sensitive information in the content, please click "Report", and we will handle it promptly.
banner
ChainCatcher Building the Web3 world with innovators