🦁 The Leo Programming Language. A Programming Language for Formally Verified, Zero-Knowledge Applications
Go to file
2020-08-17 20:21:45 -07:00
.cargo Remove revision peg for snarkOS dependency, change Leo to groth16, update to proper calling convention for snarks 2020-07-30 19:09:27 -07:00
.github Fix windows 2020-08-15 01:44:56 -07:00
.resources Add markdown changes 2020-06-20 08:42:33 +00:00
.travis Adds SSH key for snarkOS 2020-06-07 22:55:49 -07:00
ast merge master 2020-08-17 01:12:16 -07:00
compiler merge master 2020-08-17 01:12:16 -07:00
examples ignore examples outputs directories 2020-08-03 12:57:49 -07:00
gadgets Add and fix tests for all signed integer types 2020-08-14 16:10:48 -07:00
input impl new group notation for inputs ast + typed 2020-08-15 16:45:43 -07:00
leo write test output to filename specified by context 2020-08-15 23:25:34 -07:00
linter Add linter stub 2020-08-02 18:06:21 -07:00
package impl test context with test inputs 2020-08-15 22:09:22 -07:00
state pull master 2020-08-15 02:28:34 -07:00
typed merge master 2020-08-17 01:12:16 -07:00
.codecov.yml Adds configuration files for tooling 2020-06-07 22:53:45 -07:00
.gitignore allow outputs directory in leo package 2020-08-03 12:59:49 -07:00
.rustfmt.toml Adds configuration files for tooling 2020-06-07 22:53:45 -07:00
.rusty-hook.toml Adds configuration files for tooling 2020-06-07 22:53:45 -07:00
.travis.yml Update travis.yml 2020-06-08 00:21:15 -07:00
Cargo.lock merge master 2020-08-17 01:12:16 -07:00
Cargo.toml pull master 2020-08-15 02:28:34 -07:00
FAQs.md Update and rename FAQ.md to FAQs.md 2020-08-15 03:01:30 -07:00
README.md add flying tour, contributing 2020-08-17 20:21:45 -07:00

The Leo Programming Language

CI codecov

Leo is a functional, statically-typed programming language built for writing private applications.

Table of Contents

1. Overview

Welcome to the Leo programming language.

Leo exists to provide a simple high-level language that compiles to a rank one constraint system (R1CS) circuit. With Leo, you can write circuits to support zero-knowledge tokens, private stable coins, and decentralized marketplaces.

The syntax of Leo is influenced by JavaScript, Python, Scala, and Rust with a strong emphasis on readability and ease-of-use.

2. Build Guide

2.1 Install Rust

We recommend installing Rust using rustup. You can install rustup as follows:

  • macOS or Linux:

    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    
  • Windows (64-bit):

    Download the Windows 64-bit executable and follow the on-screen instructions.

  • Windows (32-bit):

    Download the Windows 32-bit executable and follow the on-screen instructions.

2.2a Build from Crates.io

We recommend installing Leo this way. In your terminal, run:

cargo install leo

Now to use Leo, in your terminal, run:

leo

2.2b Build from Source Code

Alternatively, you can install Leo by building from the source code as follows:

# Download the source code
git clone https://github.com/AleoHQ/leo
cd leo

# Build in release mode
$ cargo build --release

This will generate an executable under the ./target/release directory. To run snarkOS, run the following command:

./target/release/leo

3. Quick Start

Use the Leo CLI to create a new project

# create a new `hello_world` Leo project
leo new hello_world
cd hello_world

# build & setup & prove & verify
leo run

The leo new command creates a new Leo project with a given name.

The leo run command will compile the main program, generate keys for a trusted setup, fetch inputs, generate a proof and verify it.

Congratulations! You've just run your first Leo program.

4. Flying Tour

The best way to get to know Leo is by writing some code. We will fly through a high level overview of a Leo file. If you want to gain a deeper understanding of the Leo language, then check out the developer docs

Square Root Example: Let's prove that we know the square root of a number.

src/main.leo

function main(a: u32, b: u32) -> bool {
    return square_root(a, b)
}

function square_root(a: u32, b: u32) -> bool {
    return a * a == b
}

test function test_square_root() {
    let a: u32 = 5;
    let b: u32 = 25;
    
    let result = square_root(a, b);

    console.assert(result == true);
}

4.1 Functions

The main function is the entrypoint of a Leo program. leo run will provide private inputs directly to the function for proving and store the program result in an output file.

The square_root function is called by main with private inputs a and b which are both unsigned u32 integers.

4.2 Testing

A naive way to test square_root would be to execute leo run several times on different inputs and check the output of the program each time.

Luckily, we can write unit tests in Leo using the test function syntax. In test_square_root we can sanity check our code without having to load in private inputs from a file every time. Want to upgrade your test function into an integration test? In Leo you can add a test context annotation that loads different sets of private inputs to make your test suite even more robust.

The last line of test_square_root uses the console function console.assert. This function along with console.log, console.debug, and console.error provide developers with tools that are run without affecting the underlying constraint system.

4.3 Data Types

Leo supports boolean, unsigned integer, signed integer, field, group element, and address data types. Collections of data types can be created in the form of static arrays and tuples.

4.4 Circuits

Circuits Example

src/main.leo

circuit Point {
    x: u32,
    y: u32,

    static function new() -> Self {
        return Self { 
            x: 0, 
            y: 0, 
        }
    }

    function add() -> u32 {
        return self.x + self.y
    }
}

function main() {
    let mut p = Point::new();
    
    p.x = 4u32;
    p.y = 6u32;

    let sum = p.add();
    
    console.log("The sum is {}", sum);
}

Circuits in leo are similar to structures in other object-oriented languages. They provide a composite data type that can store primitive values and provide functions for instantiation and computation.

The static keyword modifies the new function so it can be called without instantiating the circuit.

Leo introduces Self and self keywords to access circuit member values.

4.5 Imports

Imports fetch other circuits and functions and bring them into the current file scope. Leo supports imports for dependencies that are declared locally or in an imported package.

Importing packages can be accomplished using the leo add command in the CLI.

5. Contributing

Please see our guidelines in the developer docs

Thank you for helping make Leo better!

6. License

License: GPL v3