Life: A secure, blazing-fast, cross-platform WebAssembly VM in Go.

https://github.com/perlin-network/life

WebAssembly is a high-level instruction set enabling developers to easily design computationally heavy programs that may be securely run on web browsers.

For many developers, it is a no-brainer that such a secure, high-level, yet performant instruction set has use cases in so many more places outside of the browser.

Imagine writing blazing-fast WebAssembly programs running on your smart TVs/fridges, mobile phones, or even your gaming laptops.

You could have smart devices all around the world securely training machine learning models, hosting up databases, or even hosting up blogs/online retail stores 24/7.

With that comes the reason we at Perlin are thrilled to unveil to the open-source community our new project Life.

P.S. check out our previous open-source project Noise if you haven’t already.


What is Life?

Life is a secure, blazing-fast, cross-platform, and modular WebAssembly VM written in Go, built for running computationally heavy code on practically any device you can imagine.

At Perlin, we developed Life to be:

Fast — Life uses a wide range of optimization techniques and is far more performant than quite a number of different WebAssembly implementations we’ve tested (such as go-interpreter/wagon and paritytech/wasmi).

Correct — Life was built from the WebAssembly reference manual, and thus passes most of the official test suite (66/72 passed, none of the failures are related to the execution semantics).

Secure — User code executed on Life is fully sandboxed. A WebAssembly module’s access to resources (instruction cycles, memory usage) may easily be controlled to the very finest detail.

Pure — Life does not rely on any native dependencies, and may easily be cross-compiled for running WebAssembly modules on practically any platform (Windows/Linux/Mac/Android/iOS/etc).

Practical — Life makes full use of the minimal nature of WebAssembly to write code once and run anywhere. Every single part of Life may be modularized and used for whatever interests developers may have in mind.

With that being said, we seek for Life to be a tool that any project may easily integrate into their applications for the sake of running WebAssembly code anywhere with ease.


How do I write WebAssembly modules?

If you’re entirely new to WebAssembly, I highly recommend reading any material you can find on WebAssembly online such as: https://developer.mozilla.org/en-US/docs/WebAssembly.

I’d recommend also trying to compile some WebAssembly (*.wasm) modules yourself through any online compiler such as: https://mbebenita.github.io/WasmExplorer/.

With the particular compiler above, let’s try compile the factorial (`fact`) example WebAssembly program in the compiler and hit Compile.

Testing out the factorial WebAssembly module demo.

The Wat section will output the program in WAT (WebAssembly human-readable text) format. You should a line resembling:

(export "_Z4facti" (func $_Z4facti))

Keep note of the text _Z4facti, or whatever else it may be. _Z4facti is the name of the function we compiled; we’ll need the function name so that we may execute the function from our module in Go via. Life.

Make sure to also download the module in *.wasm format by clicking Download.

How do I use Life?

First, let's read the raw byte contents of our *.wasm module (we’ll call it factorial.wasm for this article).

// Read WebAssembly *.wasm file.
bytes, err := ioutil.ReadFile("factorial.wasm")
if err != nil {
panic(err)
}

Now lets initialize our WebAssembly virtual machine and feed in the bytes of our WebAssembly module.

vm, err := exec.NewVirtualMachine(bytes, exec.VMConfig{}   , new(exec.NopResolver))
if err != nil {
panic(err)
}

We’ll now get the ID of our function _Z4facti.

id, ok := vm.GetFunctionExport("_Z4facti")
if !ok {
panic("function could not found")
}

Finally, lets pass in a number (say 35) as a parameter to our function, execute it by passing the ID our VM, and print out the result of the function decoded to a float64.

result, err := vm.Run(id, 35)
if err != nil {
vm.PrintStackTrace()
panic(err)
}
fmt.Printf("35! = %f\n", math.Float64frombits(uint64(result)))

And that’s all!

You’ve successfully executed a function in your WebAssembly module with a full-blown embeddable WebAssembly VM which you can incorporate in any of your projects.

Feel free to link it up to your Android app, desktop app, Raspberry Pi micro-controller; there are an endless amount of possibilities 😄.

How fast is Life?

We’ve stress-tested Life alongside other WebAssembly implementations on three different tasks and noticed Life to significantly outperform the others.

Closing Remarks

Life was made for the purpose of being the execution environment for all smart contracts and computational tasks in Perlin.

We’ve already tested Life out with complicated programs, and even created a new backend to the open-source deep learning model compiler TVM which allows users to compile PyTorch, Tensorflow, Keras, MXNet, and Caffe models down to WebAssembly*.

A neural network written in PyTorch compiled down to WebAssembly under Perlin.

What does this all mean?

Well, that means Perlin bootstrapped w/ Life now supports any computational task you can imagine. In terms of development progress, one can already run their tasks via. Perlin on the hundreds of millions of idle smart devices out there 😉.

I just want to remind though, that Life isn’t something we’re tailoring to for solely the purposes of just Perlin.

We want to empower other decentralized projects in the space by giving access to the utilities which we’ve built that have empowered ours.

In that process, we realized that a standardized, optimized WebAssembly runtime implementation is something that would significantly benefit both projects from the decentralized space and developer space.

On behalf of the tech team at Perlin, we’re always more than happy to share our work for the sake of bridging the cryptocurrency space and technology space closer together.

To those who really believe in our mission and would love to get to work with us, we are hiring!

To grab our attention, look in the contribution guidelines on our Github, or even feel free to reach out and chat with us on Discord 😄.