Detailed Explanation of the BigTime Engine WASM: Driving a New Paradigm for Smart Contracts and Cross-Chain Interactions

CN
2 years ago
Wasm's portability and high performance make it one of the important technologies in the field of blockchain.

Author: Mike@Foresight Ventures

TL;DR

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

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

There are some 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. Wasm's high performance and portability enable smart contracts to execute more efficiently and be used across platforms.

  2. Cross-chain Interaction: Wasm can be used to implement cross-chain interaction functionality. By compiling the logic of different blockchains into Wasm code, the same logic can be executed on different blockchains, achieving cross-chain data transmission and interaction.

  3. Off-chain Computing: Wasm can be used for computing outside the blockchain and submitting the computation results to the blockchain. This can improve the efficiency and flexibility of computation while maintaining data security and trustworthiness.

  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 the computation.

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

I. What is Web Assembly

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

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:

  • Efficiency: WASM has a complete set of language features and is actually a small and fast-loading binary format, aiming to fully leverage the hardware capabilities to achieve the execution efficiency of native languages.

  • Safety: 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, WASM has a limited interface, and most WASM applications cannot access the internet (because they do not support sockets) and can only support local databases. Many security issues arise from illegal memory access during execution, which WASM can avoid during compilation.

  • Compatibility: WASM is designed to be versionless, feature-testable, and backward-compatible in the web. WASM can be called by JavaScript, enter the JavaScript context, and can also call browser functions 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 and can be run immediately, making it plug-and-play.

  • In addition: 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, simplify updates, and ensure that all users can access your application. WASM supports 64-bit and 32-bit integer operations, corresponding 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 that 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 is supported in the backends of browsers developed by these companies.

    The beauty of WASM is that it's like a magic engine that can run anywhere, without the need for downloading or installing, because it's in binary format. With just one click, web applications can run immediately when needed. It's more secure than downloading and running binary files directly, as the browser has built-in security mechanisms to prevent the code running in it from harming your system. And sharing web applications is just as simple - the link is a clickable string that can be placed anywhere.

    II. Why Do We Need Web Assembly

    2.1 Web2

    Due to the built-in features of browsers and the interactivity provided by the web, the web has evolved from a static hypertext content and small script language to a very powerful and popular platform, full of amazing applications and features. However, up to now, web applications are still primarily driven by the same scripting language (JavaScript), which was not originally designed to achieve these.

    JavaScript was originally a simple scripting language designed to bring some interactivity to web applications filled with lightweight hypertext documents. Its design was easy to learn and write, and it did not aim for high execution speed. Over the years, significant performance improvements in JavaScript parsing by browsers have brought about significant performance improvements.

    With the increasing speed of JavaScript execution, the things that can be executed in browsers have greatly expanded. New APIs have brought interactive graphics, video streaming, offline browsing, and more features. Meanwhile, more and more applications (previously limited to local applications) are starting to enter the web. Now you can easily edit documents and send emails in the browser, but in some areas, the performance of JavaScript is still a challenge. Think about the software you use besides the browser: games, video editing, 3D rendering, or music production. These applications require a lot of computation and high performance. JavaScript is difficult to meet these high-performance requirements.

    However, replacing JavaScript is not very practical, and it may even take decades to do so, because the entire internet depends on it. And there are a lot of people constantly improving JavaScript. Indeed, compared to other languages, JavaScript is not quite good in some aspects such as null and ==, but these issues are not enough to replace the entire technology.

    So, WebAssembly will not replace JavaScript, but that doesn't mean no one will use WASM in the future. In fact, the use of WASM will become more and more widespread. Because WASM can bring powerful computing capabilities to web pages, such as image processing or games. With WASM, you can create a web-based Photoshop that runs well, or a 3D game that runs 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 ability to efficiently run games in the browser, opening up the possibility of bringing many other applications to the browser.

    The above images show a comparison of the workflow between JavaScript and WASM, and it can be seen that WASM is much more concise compared to JavaScript.

    2.2 Web3

    WASM VM

    In 2018, discussions began within the Ethereum ecosystem about using WASM VM as the smart contract virtual machine, as it was considered to have better performance than EVM. Gavin Wood, the inventor of EVM, has mentioned the feasibility of replacing EVM with WASM, and Vitalik has also stated that Ethereum 2.0 will upgrade to WASM contracts (eWASM) to meet more development needs. Now, the development of WASM contracts has taken shape.

    2.3 How was EVM designed? Why is it inefficient?

    Large Architecture Size

    Traditional computers' instruction sets only accept 32-bit or 64-bit inputs. EVM, on the other hand, is a 256-bit computer, intentionally designed this way to make it easier to handle Ethereum's hash algorithm, which explicitly produces 256-bit outputs.

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

    Additionally, if you want to implement a complex algorithm similar to SHA256 using its basic OPCODES on Ethereum, good luck! To address the high gas costs of running complex programs through the instruction set, Ethereum introduced the concept of precompiles, compiling programs into EVM and consuming fixed gas. A notable precompile is the Ethereum hash algorithm, as implementing this algorithm in the virtual machine would result in extremely expensive costs when called by a contract.

    Bulky Precompiles

    The problem with precompiles is that they continuously increase the bloat and complexity of the virtual machine, without addressing the core issue: the inefficient and crude design of the current instruction set and specification.

    What if we could define a new specification and instruction set that doesn't require precompiling these complex programs, but can efficiently implement them using basic instructions? This is where WASM comes into play.

    2.4 Comparison of EVM and WASM VM

    • Speed: WASM aims to provide faster execution speed than EVM. EVM may have efficiency issues in compiling and executing smart contracts, while WASM improves loading speed and processing capabilities by directly converting to compiled code.

    • Precompiles: EVM relies on precompiled contracts to efficiently execute cryptographic computations, but this may pose risks of hard forks. WASM eliminates the reliance on precompiled contracts, allowing developers to create efficient and fast smart contracts.

    • Transaction Costs: With a faster Wasm virtual machine, transaction throughput is greatly increased, leading to significantly lower contract deployment and transaction costs. It can be said that Wasm contracts effectively address the high transaction fees and congestion issues on Ethereum.

    • Flexibility and Interoperability: WASM extends the range of languages available to smart contract developers, supporting the use of any high-level language for Wasm (such as Rust, C++, JavaScript, etc.) to develop complex business logic. This means you can write smart contracts in any language you are familiar with, including the mature Rust-based ink! or the AssemblyScript-based Ask!, among others.

    The EWASM team is integrating WebAssembly on Ethereum to ensure a more efficient and simple execution layer for Ethereum, making it suitable as a fully decentralized computing platform. WASM has 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 aims to improve the performance of smart contract execution on the Ethereum Layer 2 network Arbitrum by introducing the WebAssembly (WASM) virtual machine. Contracts can be executed faster than Solidity, while reducing gas costs. This makes it easier to build high-performance smart contracts on the Arbitrum network, with support for C, C++, and Rust compilation.

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

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

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

    Based on the popularity of the Arbitrum ecosystem, the integration of Stylus may be the most meaningful WASM integration, and it also benefits the competitiveness of Arbitrum in zkrollup.

    2.6 Gear (Polkadot)

    The Gear protocol is creating a technology that can be deployed as a Polkadot parachain and will serve as a tool for hosting smart contracts. Like Polkadot, Gear also uses the Substrate framework, simplifying the process of creating different blockchains for specific applications. Substrate provides a wide range of out-of-the-box features, 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 rather than building and operating an entire blockchain from scratch.

    The primary 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, C++, etc. For developers from outside the crypto world, the barrier to entry is low, as 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

    • Minimal, intuitive, and sufficient API surface for blockchain context

    • Actor communication proxy model between on-chain components provides higher composability, 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 and powerful Wasm-based smart contract platform that can be easily integrated into the Cosmos-SDK. One of the main advantages of CosmWasm is its tight integration with IBC (Inter-Blockchain Communication) natively, allowing developers and users to enter the multi-chain future, currently only supporting Rust.

    Advantages of CosmWasm

    1. Security: Enhanced smart contract security using the Rust language.

    2. Cross-chain compatibility: Support for IBC (Inter-Blockchain Communication) protocol in the Cosmos ecosystem.

    3. Performance: CosmWasm has demonstrated higher efficiency and lower transaction costs 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: Rust, compared to more commonly used smart contract languages like Solidity, may have a steeper learning curve for beginners. CosmWasm needs to support more language compilations to have the potential for widespread adoption.

    2. Ecosystem and tool support: While 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 visibility: In the smart contract platform space, CosmWasm may not have high visibility compared to Ethereum, Binance Smart Chain, etc., affecting its ability to attract developers and users.

    4. Maintenance and upgrade challenges: While CosmWasm provides contract upgrade functionality, managing the maintenance and upgrade of smart contracts is still a complex task that needs to be handled carefully 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 a recent emerging technology called ZKWASM, and the inventor Delphinus Labs has open-sourced the code of ZK-WASM on GitHub. ZKWASM allows developers to verify the correctness of executed computations without re-execution. By leveraging ZKWASM, developers can flexibly build ZKP applications using various programming languages, which can seamlessly execute in web browsers.

    The concept of ZKWASM is derived from ZKSNARK, which is a hybrid of SNARG and zero-knowledge proofs.

    Let's explain, 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, to some extent, poses a barrier to existing programs, making it difficult for them to harness the power of ZKSNARK. However, there is another way, not to use ZKSNARK at the source code level, but to use it at the bytecode level of the virtual machine, and then implement a virtual machine that supports ZKSNARK. Delphinus Labs has adopted the latter approach, integrating the entire WASM virtual machine into ZKSNARK circuits. 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 honest and do not leak any private information. ZKWASM provides various use cases, such as enabling ZK proofs of some operations in the browser to be put on the chain, achieving verifiable blockchain operations in web pages. It can also be used for oracles, off-chain computation, automation, bridging Web2 and Web3, generating proofs for machine learning and data processing, and even for games and social applications. As adoption increases, zkWASM will expand the possibilities of Web3 and bring 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 trustworthy and decentralized digital landscape. ### Conclusion The future looks bright for the performance of the web and the execution layer of smart contract platforms. Not only will dApps have higher performance, but integrating WASM will also make it easier for those familiar with robust mainstream languages like Rust and Go to develop smart contracts, without needing to learn the various details of solidity or other blockchain development languages to build useful applications on Ethereum. According to Evans Data Corporation, there are nearly 27 million developers worldwide. This number is steadily growing, with an increase of about 3% last year, and is expected to exceed 28.7 million by 2024. However, the number of developers on the blockchain is less than 30,000, accounting for about one-thousandth of the total number of developers, although this number has been steadily increasing, learning new smart contract languages may still be a barrier for developers to enter the blockchain. But more and more blockchains are starting to support Web Assembly as the bytecode for compiled smart contracts. WASM brings not only efficiency, interoperability, and a wide range of applications to the blockchain, but also the key to liberating developers, lowering the barrier for developers to enter the blockchain. Imagine in the near future, when Web2 developers want to try blockchain development, they can use their familiar Python, C++, and JavaScript to develop large-scale applications on the blockchain, maximizing the value of the decentralized network of the blockchain, first lowering the threshold for creators (developers), and then lowering the threshold for users, moving towards Mass Adoption.

    免责声明:本文章仅代表作者个人观点,不代表本平台的立场和观点。本文章仅供信息分享,不构成对任何人的任何投资建议。用户与作者之间的任何争议,与本平台无关。如网页中刊载的文章或图片涉及侵权,请提供相关的权利证明和身份证明发送邮件到support@aicoin.com,本平台相关工作人员将会进行核查。

    Share To
    APP

    X

    Telegram

    Facebook

    Reddit

    CopyLink