6.2 KiB
The Leo Programming Language
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!