Foresight Ventures: WASM - The Engine of a New Era

BeginnerJan 01, 2024
This article examines some of the relationships and use cases between Wasm and blockchain: smart contracts, cross-chain interactions, off-chain computation, and data privacy.
Foresight Ventures: WASM - The Engine of a New Era

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 with multiple programming languages and run on different platforms.

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

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 usability.
  2. Cross-Chain Interoperability: Wasm can be used to implement cross-chain interoperability. By compiling the logic of different blockchains into Wasm code, the same logic can be executed on different blockchains, enabling data transfer and interaction across chains.
  3. Off-Chain Computation: Wasm can be used for computation outside of the blockchain and submit the computation results to the blockchain. This allows for improved efficiency and flexibility in computation while maintaining data security and trustworthiness.
  4. Data Privacy: Wasm can be used to achieve 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 preserved while ensuring the verifiability of computations.

In summary, the combination of Wasm and blockchain provides more efficient, secure, and flexible blockchain applications and services. The portability and high performance of Wasm make it an important technology in the field of blockchain.

1. What is Web Assembly?

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

WebAssembly, also known as WASM, is a memory-safe and platform-independent technology that can efficiently map to different CPU architectures. It offers several key advantages:

  • Efficiency: WASM has a complete set of language features and is a compact and fast-loading binary format. Its goal is to fully leverage the hardware capabilities to achieve native language performance.
  • Security: WASM runs in a memory-safe and sandboxed execution environment, and it can even be implemented within existing JavaScript virtual machines. In a web environment, WASM strictly adheres to the same-origin policy and browser security policies. During compilation, WASM limits the interface to reduce security risks. Most WASM applications cannot access the internet (e.g., no socket support) and are limited to local database access. Many security issues arise from unauthorized memory access, which can be mitigated during compilation with WASM.
  • Compatibility: WASM is designed to be versionless, feature-testable, and backward-compatible in the web. It can be invoked by JavaScript, enter the JavaScript context, and access browser functionalities like a Web API. WASM can run not only in web browsers but also in non-web environments such as Node.js, Deno, and IoT devices. Unlike traditional approaches that require multiple compilations, WASM only needs to be compiled once for instant plug-and-play execution.

In addition, the Web is the only true universal platform that allows access to your applications on any device. This also enables you to maintain a single codebase, simplify updates, and ensure that all users can access your applications. WASM supports 64-bit and 32-bit integer operations, which directly correspond to CPU instructions. By removing floating-point operations, deterministic behavior can be easily achieved, which is necessary for consensus algorithms. Supported by the LLVM compiler infrastructure project, WASM can benefit from over a decade of compiler optimizations in LLVM. WASM is continuously developed by major companies such as Google, Apple, Microsoft, Mozilla, and Facebook, and it is supported by the backends of browsers developed by these companies.

The beauty of WASM lies in its ability to run anywhere without the need for downloading or installing, as it is in binary format. With just one click, web applications can be executed immediately when needed. It is even more secure than directly downloading and running binary files, as browsers have built-in security mechanisms that prevent the executed code from harming your system. Additionally, sharing web applications is simple - links can be placed anywhere as clickable strings.

2. Why Do We Need Web Assembly?

2.1 Web2

Web has evolved from a platform for static content and small script languages to a powerful and popular platform filled with amazing applications and features, thanks to the built-in functionality of browsers and the interactivity provided by the Web. However, Web applications are still predominantly driven by the same scripting language (JavaScript), which was not initially designed to accomplish these tasks.

JavaScript was initially a simple scripting language aimed at bringing interactivity to lightweight hypertext documents in Web applications. Its design was easy to learn and write, and it did not prioritize runtime speed. Over the years, significant performance improvements in JavaScript parsing by browsers have led to notable performance boosts.

With the accelerated speed of JavaScript execution, the range of things that can be done within browsers has expanded significantly. New APIs have introduced features such as interactive graphics, video streaming, offline browsing, and more. Moreover, an increasing number of applications that were previously limited to desktop environments have entered the Web. Now you can easily edit documents and send emails in your browser. However, in certain domains, the performance of JavaScript remains a challenge. Think about software applications you use besides browsers: games, video editing, 3D rendering, or music production. These applications require extensive computations and high performance. JavaScript struggles to meet these high-performance requirements.

However, it is not practical to replace JavaScript, and it might even take decades to achieve that goal, as the entire Internet relies on it. Moreover, there is a large community constantly improving JavaScript. Indeed, compared to other languages, JavaScript has some shortcomings in aspects such as null and ==. However, these issues are not severe enough to warrant replacing the entire technology.

So, WebAssembly will not replace JavaScript, but that doesn’t mean that WASM won’t be used in the future. In fact, the use of WASM will become more and more widespread. This is because WASM can bring powerful computational capabilities to the web, such as image processing or games. With WASM, you can create a web-based version of Photoshop that runs well, or a 3D game that runs at 60 frames per second or even higher frame rates in the browser. Games, in particular, present a challenge because they require simultaneous execution of audio and video processing, as well as coordination of physics effects and AI. WASM has the ability to efficiently run games in the browser, opening the door to bringing many other applications to the browser.

The figure above shows a comparison of the workflow between JavaScript and WebAssembly (wasm). It can be observed that wasm is much more concise compared to JavaScript.

2.2 Web3

WASM VM

In 2018, the Ethereum ecosystem began discussing the use of a WASM VM as a smart contract virtual machine, as it was considered to have better performance than the EVM. Gavin Wood, the inventor of the EVM, has expressed the feasibility of replacing it with WASM, and Vitalik has also stated that Ethereum 2.0 will be upgraded to support WASM contracts (eWASM) to meet more development needs. Today, the development of WASM contracts has already taken shape.

2.3 How is the EVM designed? Why is it inefficient?

Architecture Size Issue

Traditional computers have instruction sets that accept input of 32 or 64 bits. EVM, however, is different and unique as it is a 256-bit computer designed to handle Ethereum’s hash algorithm more easily, which produces explicit 256-bit outputs.

However, the actual computer running EVM programs needs to split the 256-bit words into their native architectures to execute smart contracts, making the entire system inefficient and impractical.

Additionally, if you want to implement a complex algorithm like SHA256 using EVM’s basic OPCODES on Ethereum, you’re in for a tough time! To address the high gas cost issue of running complex programs through the instruction set, Ethereum introduced the concept of precompiles, which compiles the program into EVM and consumes a fixed amount of gas. A notable precompile is the Ethereum hash algorithm, as implementing it within the virtual machine would result in extremely expensive fees when contracts are called.

Bloated Precompilation

The problem with precompilation is that it continuously increases the bloatedness and complexity of the virtual machine, without addressing the core issue: the inefficient and poor design of the current instruction set and specifications.

What if we could define a new specification and instruction set that doesn’t require precompilation of these complex programs, but instead efficiently achieves the desired results through basic instructions? This is where WASM comes into play.

2.4 Comparison between EVM and WASM VM

  • Speed: WASM aims to provide faster execution speed compared to EVM. EVM may have efficiency issues in compiling and executing smart contracts, while WASM improves loading speed and processing capability by directly converting to compiled code.
  • Precompiled Contracts: EVM relies on precompiled contracts for efficient execution of cryptographic calculations, but this can introduce risks of hard forks. WASM eliminates the need for precompiled contracts, allowing developers to create efficient and fast smart contracts.
  • Transaction Costs: With a faster WASM virtual machine, transaction throughput increases significantly, leading to reduced contract deployment and transaction costs. WASM contracts address the issues of high transaction fees and congestion on the Ethereum network.
  • Flexibility and Interoperability: WASM extends the range of languages available for smart contract development, supporting the use of any high-level language that compiles to WASM, such as Rust, C++, and JavaScript. This means developers can write smart contracts in their preferred language, including mature frameworks like ink! for Rust or Ask! for AssemblyScript.

The EWASM team is currently integrating WebAssembly on Ethereum to ensure a more efficient and simplified execution layer, making Ethereum suitable as a fully decentralized computing platform. WASM has been adopted as a standard by many other projects, including Dfinity and EOS, both using it to enhance their execution layers.

2.5 Stylus(Arbitrum)

The Stylus project is an initiative on the Arbitrum Ethereum Layer 2 network that aims to improve the performance of smart contract execution by introducing the 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, and currently supports compilation in C, C++, and Rust.

Custom Precompile Support: Stylus also supports custom precompiles, which allows developers to deploy their own Rust or C++ precompiles 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 can be beneficial for 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 that Stylus contracts can interact with existing EVM contracts and share the same global state as the EVM.

Reentrancy: Unlike Cosmos Wasm, Stylus Rust SDK introduces a reentrancy feature and allows developers to enable it manually. This enables contracts to have more flexibility in interoperability, but developers need to carefully manage the state to ensure security.

Based on the thriving ecosystem of Arbitrum, the integration of Stylus is perhaps the most meaningful WASM integration. It also enhances the competitiveness of Arbitrum among zkRollup fields.

2.6 Gear(Polkadot)

The Gear protocol is creating a technology that can be deployed as a Polkadot parachain, serving as a tool for hosting smart contracts. Similar to Polkadot, Gear also utilizes the Substrate framework, which simplifies the process of creating different blockchains for specific applications. Substrate provides out-of-the-box functionality, allowing people 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 instead of building and operating 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 in different languages such as Rust, C, and C++. For developers from outside the crypto world, the barrier to entry is low because they can build smart contracts in a familiar environment. Developers find it easier to experiment with smart contract programming languages.

Gear’s smart contract architecture utilizes the Actor Model under the hood and provides the following functionalities:

  • Persistent memory for immutable programs
  • Asynchronous message handling
  • Minimal, intuitive, and sufficient API surface for blockchain context
  • Actor communication proxy model between on-chain components for higher composability and better compatibility with parallel code execution and sharding.

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

2.7 CosmWasm(Cosmos)

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

Advantages of CosmWasm

  1. Security: Enhances the security of smart contracts using the Rust programming language
  2. Cross-chain compatibility: Supports the IBC (Inter-Blockchain Communication) protocol within the Cosmos ecosystem.
  3. Performance: Demonstrates higher efficiency and lower transaction costs in certain cases compared to the traditional Ethereum Virtual Machine (EVM).
  4. Developer-friendly: The type and memory safety features of the Rust language help reduce certain types of errors in smart contracts.

Challenges and Limitations

  1. Learning Curve: Rust may have a steeper learning curve compared to more commonly used smart contract languages like Solidity. To increase the potential for widespread adoption, CosmWasm needs to support compilation in more languages.
  2. Ecosystem and Tooling: While growing, the development tools and ecosystem for CosmWasm may still be relatively limited compared to mature smart contract platforms like Ethereum.
  3. Market Share and Awareness: In the smart contract platform space, CosmWasm may have lower awareness compared to platforms like Ethereum and Binance Smart Chain, which affects its ability to attract developers and users.
  4. Maintenance and Upgrade Challenges: While CosmWasm provides contract upgrade functionality, maintaining and managing upgrades for smart contracts is still a complex task that needs to be carefully handled to avoid security vulnerabilities.
  5. Compatibility Issues: Projects accustomed to EVM or other smart contract environments may face compatibility challenges when migrating to CosmWasm.

2.8 ZK-WASM

In addition to the wasm virtual machine, there is also a recent emerging technology called ZKWASM. The inventor, Delphinus Labs, has open-sourced the code for ZK-WASM on GitHub. ZKWASM allows developers to verify the correctness of executed computations without re-executing them. By leveraging ZKWASM, developers can flexibly build zero-knowledge proof (ZKP) applications using various programming languages. These applications can seamlessly execute in web browsers.

ZKWASM is derived from ZKSNARK, which is a combination of SNARG and zero-knowledge proofs. Let’s explain it further. Typically, to use ZKSNARK, you need to write a program in an arithmetic circuit language or a 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 leverage the power of ZKSNARK. However, there is another way, which is not to use ZKSNARK at the source code level but at the bytecode level of a virtual machine, and then implement a virtual machine that supports ZKSNARK. Delphinus Labs has adopted the latter approach by incorporating the entire WASM virtual machine into a ZKSNARK circuit. As a result, existing WASM applications can run directly on ZKWASM without any modifications. Therefore, cloud service providers can prove to any user that the computation results are computed honestly and will not disclose any private information.

ZKWASM provides various use cases, such as enabling the on-chain verification of operations performed in the browser. It allows for web-based interactions that are verifiable on the blockchain. Other use cases include oracle services, off-chain computation, automation, bridging the gap between Web2 and Web3, generating proofs for machine learning and data processing, and even gaming and social applications. With increasing adoption, zkWASM expands the possibilities of Web3 and integrates Web2 developers into this transformative landscape.

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

3. Conclusion

Web’s performance and the future of smart contract platform execution layer are promising. 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, without having to learn the intricacies of solidity or other blockchain development languages. According to Evans Data Corporation, there are nearly 27 million developers worldwide, and this number is steadily growing, with an expected increase to over 28.7 million by 2024. However, the number of developers in the blockchain field is still less than 30,000, accounting for only about one-thousandth of the total number of developers. Although this number is steadily increasing, learning new smart contract languages may still be a barrier for developers to enter the blockchain industry.

But more and more blockchains are starting to support Web Assembly as the bytecode for compiled smart contracts. WASM brings efficiency, interoperability, and a wide range of use cases to blockchains. It also serves as the key to unlock the door for developers, lowering the barrier to entry into blockchain development. Just imagine, in the near future, when Web 2.0 developers want to try their hand at blockchain development, they can use familiar languages like Python, C++, and JavaScript to build large-scale applications on the blockchain, maximizing the value of decentralized networks. First, lower the barrier for creators (developers), then lower the barrier for users, and move 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

Thank you to Maggie and Xinyou Ji (CMU) for their advice and guidance on this article.

Disclaimer: All articles from Foresight Ventures are not intended as investment advice. Investments involve risks, so please assess your own risk tolerance and make investment decisions carefully.

Disclaimer:

  1. This article is reprinted from [Foresight Research]. All copyrights belong to the original author [Mike@ Foresight Ventures]. If there are objections to this reprint, please contact the Gate Learn team, and they will handle it promptly.
  2. Liability Disclaimer: The views and opinions expressed in this article are solely those of the author and do not constitute any investment advice.
  3. Translations of the article into other languages are done by the Gate Learn team. Unless mentioned, copying, distributing, or plagiarizing the translated articles is prohibited.
Start Now
Sign up and get a
$100
Voucher!
Create Account