A gentle introduction: Stylus
This document is currently in public preview and may change significantly as feedback is captured from readers like you. Click the Request an update button at the top of this document or join the Arbitrum Discord to share your feedback.
This introduction is for developers who want to build on Arbitrum using popular programming languages like Rust. This capability is made possible by Stylus, a new way to write EVM-compatible smart contracts using your favorite programming languages.
In a nutshell:
- Stylus lets you write smart contracts in programming languages that compile to WASM, such as Rust, C, C++, and many others.
- Rich language support already exists for Rust: you can use the Stylus SDK and CLI tool to start building today.
- Stylus smart contracts benefit from Arbitrum's EVM equivalence thanks to a second, coequal WASM virtual machine.
- Solidity contracts and Stylus contracts are fully interoperable. In Solidity, you can call a Rust program and vice versa.
- Stylus contracts are significantly faster and have lower gas fees due to the superior efficiency of WASM programs.
- Stylus makes it viable to consume RAM on the blockchain because it can greatly optimize memory use, enabling new use cases.
What's Stylus?
Stylus is an upgrade to Arbitrum Nitro, the tech stack powering Arbitrum One, Arbitrum Nova, and Arbitrum Orbit chains. This upgrade adds a second, coequal virtual machine to the EVM, where EVM contracts continue to behave exactly as they would in Ethereum. We call this paradigm MultiVM since everything is entirely additive.
This second virtual machine executes WebAssembly (WASM) rather than EVM bytecode. WASM is a modern binary format popularized by its use in major web standards, browsers, and companies to speed up computation. WASM is built to be fast, portable, and human-readable. It has sandboxed execution environments for security and simplicity. Working with WASM is nothing new for Arbitrum chains. Ever since the Nitro upgrade, WASM has been a fundamental component of Arbitrum's fully functioning fraud proofs.
With a WASM VM, any programming language that can compile to WASM is within Stylus's scope. While many popular programming languages can be compiled into WASM, some compilers are more suitable for smart contract development than others, like Rust, C, and C++. Other languages like Go, Sway, Move, and Cairo can also be supported. Languages that include their own runtimes, like Python and Javascript, are more complex for Stylus to support, although not impossible. Third-party contribution in the form of libraries for new and existing languages is welcomed!
Compared to using Solidity, WASM programs are much more efficient. There are many reasons for this, including the decades of compiler development for Rust and C. WASM also has a faster runtime than the EVM, resulting in faster execution.
How is this possible?
Stylus is only possible because of Arbitrum Nitro's unique fraud-proving technology. When there's a dispute on an Arbitrum network, Nitro replays the execution of the chain in WASM. Honest Arbitrum validators will then bisect what is being disputed until a single invalid step is identified and checked on-chain via a “one-step proof.”
Nitro's fraud-proving enables it to prove arbitrary WASM in a deterministic way.
Because any code logic can be proven with WASM, the correctness of any program compiling to WASM can also be proven. The combination of WASM's and Nitro's properties enables this technological leap we call Stylus.
For a detailed overview of Nitro's technical architecture, see the documentation or the Nitro whitepaper.
Why does this matter?
Stylus innovates on many levels, with the key ones described here:
One chain, many languages
There are estimated to be roughly 20k Solidity developers, compared to 3 million Rust developers or 12 million C developers [1]. Developers are now free to use their preferred programming language, all interoperable on any Arbitrum chain with Stylus. By onboarding the next million developers, scaling to the next billion users becomes possible.
A better EVM
Stylus' MultiVM brings the best of both worlds. Developers still get all of the benefits of the EVM, including the ecosystem and liquidity, while getting efficiency improvements and access to existing libraries in Rust, C, and C++, all without changing anything about how the EVM works. EVM equivalence is no longer the ceiling; it's the floor.
Cheaper execution
Stylus is a more efficient execution environment than the EVM, leading directly to gas savings for complex smart contracts. Computation and memory can be significantly cheaper. Cryptography libraries can be permissionlessly deployed as custom application layer precompiles. Use cases that are impractical in the EVM are now possible in Stylus.
Opt-in reentrancy
Stylus doesn't just improve on cost and speed. WASM programs are also safer. Reentrancy is a common vulnerability that developers can only attempt to mitigate in Solidity. Stylus provides cheap reentrancy detection, and using the Rust SDK, reentrancy is disabled by default unless intentionally overridden.
Fully interoperable
Solidity programs and WASM programs are completely composable. If working in Solidity, a developer can call a Rust program or rely on another dependency in a different language. If working in Rust, all Solidity functionalities are accessible out of the box.
How does it work?
There are four main steps for bringing a Stylus program to life: coding, activation, execution, and proving.
Coding
Developers can now write smart contracts in any programming language that compiles to WASM. Note that some high-level languages generate far more performant WASMs than others.
Initially, there will be support for Rust, C, and C++. However, the levels of support will differ at first. Rust has rich language support from day one, with an open-source SDK that makes writing smart contracts in Rust as easy as possible. C and C++ are supported off the bat, too, enabling the deployment of existing contracts in those languages on-chain with minimal modifications.
The Stylus SDK for Rust contains the smart contract development framework and language features most developers will need to use in Stylus. The SDK also makes it possible to perform all of the EVM-specific functionalities that smart contract developers are used to. Check out the Rust SDK Guide and the Crate Docs.
Activation
Stylus programs are compiled to WASM and then lowered to assembly. Starting from a high-level language (such as Rust, C, or C++), the first compilation stage happens either using the CLI provided in the Stylus SDK for Rust or any other compiler, such as Clang for C and C++. Once compiled, the WASM is posted onchain. Then, in a process called activation, WASM gets lowered to a node's native machine code (such as ARM or x86).
Activating a Stylus program requires a new precompile, ArbWasm. This precompile produces efficient binary code tailored to a node's native assembly. During this step, a series of middlewares ensure user programs can be safely executed and deterministically fraud-proven. Instrumentation includes gas metering, depth-checking, memory charging, and more to guarantee all WASM programs are safe for the chain to execute. Stylus contracts can be called only after they've been activated.
Gas metering is essential for certifying that computational resources are paid for. In Stylus, the unit for measuring cost is called “ink,” similar to Ethereum's gas but thousands of times smaller. There are two reasons why a new measurement is used: First, WASM execution is so much faster than the EVM that thousands of WASM opcodes could be executed in the same time it takes the EVM to execute one. Second, the conversion rate of ink to gas can change based on future hardware or VM improvements. For a conceptual introduction to Stylus gas and ink, see gas and ink (Stylus).
Execution
Stylus programs are executed in a fork of Wasmer, the leading WebAssembly runtime, with minimal changes to optimize their codebase for blockchain-specific use cases. Wasmer executes native code much faster than Geth executes EVM bytecode, contributing to the significant gas savings that Stylus provides.
EVM contracts continue to execute the same way they did before Stylus. When a contract is called, the difference between an EVM contract and a WASM program can be seen via an EOF-inspired contract header. From there, the contract is executed using its corresponding runtime. Contracts written in Solidity and WASM languages can make cross-contract calls to each other, meaning a developer never has to consider what language the contract was written in. Everything is interoperable.
Proving
Nitro has a happy case and a sad case. Most of the time, it's in a happy case, compiling execution history to native code. In the sad case of a dispute between validators, Nitro compiles execution history to WASM to conduct interactive fraud proofs on Ethereum. Stylus is a natural extension to Nitro's fraud-proving technology, utilizing it to not only bisect execution history but also any WASM programs deployed by developers.
New use cases
It's impossible to list all of the use cases Stylus enables, think about the properties of all WASM compatible languages!
That said, here are some new features that are particularly exciting:
- Generative art libraries that consume a bunch of RAM
- Bringing existing games written in C++ on-chain
- Compute-heavy AI models
- Projects using alternative signature schemes, such as
secp256r1
, via custom precompiles - Optimization of Solidity-based projects for speed and cost
We leave it up to you to ideate blockchain projects that were not technically feasible until now.
While many developers will be drawn to new use cases, rebuilding existing applications in Stylus will also open the door to innovation and optimization. dApps have never been faster, cheaper, or safer.
Stylus is open to all. Much thought has been given to creating the best programming experience possible. However, the work continues. Feedback gained from developers will help drive Stylus to the next level, improving tooling, documentation, and language features. Becoming an early adopter of Stylus is the best way to familiarize oneself with its opportunities.
If you're a developer interested in Stylus, visit the quickstart, join the Discord channel, and start building!
Wen mainnet?
Stylus is now live on mainnet.
To keep up with the latest announcements and updates about Stylus:
- Subscribe to the Arbitrum Node Upgrade Announcement channel on Telegram
- Join both the
#dev-announcements
and#node-runners
Discord channels in the Arbitrum Discord server - Follow the official Arbitrum (
@Arbitrum
) and Arbitrum Developers (@ArbitrumDevs
) X accounts, formerly Twitter.