Skip to content

OffchainLabs/zig-on-stylus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ethereum Compatible Smart Contracts In Zig

NOTE: This repo is a demo showing how smart contracts can be written in Zig. It is not an SDK

This repo implements a demo smart contract using the sieve of erathosthenes algorithm to compute prime numbers in the Zig programming language. This code can be deployed as a WASM smart contract to Arbitrum Stylus.

Arbitrum is an Ethereum scaling solution which allows developers to write EVM and WASM smart contracts. Stylus is a new technology developed for Arbitrum chains which gives smart contract developers superpowers. With Stylus, developers can write EVM-compatible smart contracts in many different programming languages, and reap massive performance gains. Stylus slashes fees, with performance gains ranging from 10-70x, and memory efficiency gains as high as 100-500x.

fn sieve_of_erathosthenes(comptime limit: usize, nth: u16) bool {
    var prime = [_]bool{true} ** limit;
    prime[0] = false;
    prime[1] = false;
    var i: usize = 2;
    while (i * i < limit) : (i += 1) {
        if (prime[i]) {
            var j = i * i;
            while (j < limit) : (j += i)
                prime[j] = false;
        }
    }
    return prime[nth];
}

Walkthrough

See our full guide on how we added support for Zig on Arbitrum Stylus in WALKTHROUGH.md

How and Why

Being able to deploy smart contracts to an Ethereum-based chain is thanks to WebAssembly technology, which all Stylus programs compile to. Stylus smart contracts live under the same Ethereum state trie in Arbitrum nodes, and can fully interoperate with Solidity or Vyper EVM smart contracts. With Stylus, developers can write smart contracts in Rust that talk to Solidity and vice versa without any limitations.

Today, the Stylus testnet also comes with 2 officially supported SDKs for developers to write contracts in the Rust or C programming languages.

However, anyone can add support for new languages in Stylus. As long as a programming language can compile to WebAssembly, fit under 24Kb brotli-compressed, and meets some of the gas metering requirements of Stylus, it can be deployed and used onchain.

Why Zig?

  1. Zig contains memory safety guardrails, requiring developers to think hard about manual memory allocation in a prudent manner
  2. Zig is a C equivalent language, and its tooling is also a C compiler. This means C projects can incrementally adopt Zig when refactoring
  3. Zig is lightning fast and produces small binaries, making it suitable for blockchain applications

Programs written in Zig and deployed to Stylus have a tiny footprint and will have gas costs comparable, if not equal to, C programs.

License

This project is fully open source, including an Apache-2.0 or MIT license at your choosing under your own copyright.

About

EVM-interoperable smart contracts written in Zig!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published