Home

Awesome

<br /> <p align="center"> <a href="https://arbitrum.io/"> <img src="https://arbitrum.io/assets/stylus/stylus_with_paint_bg.png" alt="Logo" width="100%"> </a> <h3 align="center">The Stylus SDK</h3> <p align="center"> <a href="https://docs.arbitrum.io/stylus/stylus-gentle-introduction"><strong>C/C++ contracts on Arbitrum »</strong></a> <br /> </p> </p>

General

The C/C++ SDK allows you to take full control of the underlying web-assembly executed in your smart contract.

Required Tools

The Stylus VM executes WebAssembly, so you'll need a C/C++ compiler with support for wasm32 targets. Support for this varies, so some users may have to build clang or gcc from source. Your package manager may also include a compatible version.

We suggest using these tools:

C/C++ SDK library

This SDK is neither audited, nor stable. Future versions may ship with backward incompatible changes.

HeaderInfo
stylus_types.hTypes used by the wasm entrypoint to define return values from stylus
stylus_entry.hIncludes used to generate stylus entrypoints
hostio.hFunctions supplied by the stylus environment to change and access the VM state (see Host I/O)
stylus_debug.hHost I/Os only available in debug mode. The best way to get a debug-enabled node is to run one locally
bebi.hTools for handling Big-Endian Big Integers in wasm-32
storage.hContract storage utilities
stylus_utils.hHigher-level utils that might help smart contract developers
string.hMinimal (and incomplete) implementation of the standard string.h
stdlib.hMinimal (and incomplete) implementation of the standard stdlib.h

Examples

The library includes two examples, each with a makefile that builds a wasm from source using the command make. Both are annotated, and users are encouraged to read through the code.

siphash

Demonstrates a custom precompile, compute-only smart contract that processes input bytes and returns their hash. This minimal example uses very little of the SDK library.

erc20

Provides an erc20-like smart contract implementation. This example uses the library as well as the c-code generation capabilities of cargo-stylus.

Host I/Os

include/hostios.h. There you can call VM hooks directly, which allows you to do everything from looking up the current block number to calling other contracts.

For example, the VM provides an efficient implementation of keccak256 via

void native_keccak256(const uint8_t * bytes, size_t len, uint8_t * output)

Unlike with the Rust SDK, however, you will have to work with raw pointers and deserialize arguments manually. This makes stylus.h an ideal environment for bytes-in bytes-out programming, but not general smart contract development.

For a comprehensive list of hostios, please see The Host I/O Reference.

Notes about using C to build wasm32

Clang flags

The table below includes clang flags commonly used to build Stylus contracts. The siphash example uses most of the following, and is a great starting point for programs that opt out of the standard library.

FlagInfoOptional
--target=wasm32compile to wasm
--no-standard-librariesopt out of the stdandard library
-mbulk-memoryenable bulk-memory operations (accelerates memset and memcpy)
-O2 / -O3 / -Ozoptimize for speed or size

Wasm-ld flags

Flags that should be used when linking a wasm file with wasm-ld.

FlagInfoOptional
--no-entrylet Stylus decide the entrypoint
--stack-firstputs the shadow-stack at the beginning of the memory
-z stack-size=...sets size for the shadow-stack

Performance

C binaries are both small and very efficient. The siphash example is only 609 bytes onchain and costs 22 gas to execute a 32-byte input. By contrast, 22 gas only buys 7 ADD instructions in Solidity.

How did we achieve this efficiency? All we had to do was Google for an example siphash program and add a simple entrypoint. In the Stylus model, you can deploy highly-optimized and thouroughly-audited, industry-standard reference implementations as-is. With the Stylus SDK, cryptography, algorithms, and other high-compute applications are both straightforward and economically viable.

Roadmap

Stylus is currently testnet-only and not recommended for production use. This will change as we complete an audit and add additional features.

Arbitrum Orbit L3s may opt into Stylus at any time. Arbitrum One and Arbitrum Nova will upgrade to Stylus should the DAO vote for it.

If you'd like to be a part of this journey, join us in the #stylus channel on Discord!

Don't know C?

The Stylus VM supports more than just C. In fact, any programming language that compiles down to WebAssembly could in principle be deployed to Stylus-enabled chains. The table below includes the official ports of the SDK, with more coming soon.

RepoUse casesLicense
Rust SDKEverything!Apache 2.0 or MIT
C/C++ SDKCryptography and algorithmsApache 2.0 or MIT
Bf SDKEducationalApache 2.0 or MIT

Want to write your own? Join us in the #stylus channel on discord!

License

© 2022-2023 Offchain Labs, Inc.

This project is licensed under either of

at your option.

The SPDX license identifier for this project is MIT OR Apache-2.0.