LEO Part 2

Neiro
5 min readAug 26, 2023

--

Hello, friends.
We continue our study of the Leo Language.
You can read the first part by clicking here.

In this article we will learn how to install Leo and how to write the phrase “Hello Leo”

Installation

MacOSInstall Leo for MacOS M1

All other platforms — Browse all Leo releases here

Install From Source

To use the latest Leo features, install the Leo source code from GitHub.

1. Install the Prerequisites

1.1 Install Git:

bit.ly/start-git

1.2 Install Rust:

bit.ly/start-rust

1.3 Check the Prerequisites

git --version
cargo --version

2. Build Leo from Source Code

You can build and install Leo from the source code as follows:

# Download the source code
git clone https://github.com/AleoHQ/leo
cd leo
# Build and install
cargo install --path .

That will generate the executable ~/.cargo/bin/leo.

Now to use Leo, in your terminal, run:

leo

3. IDE Syntax Highlighting

Aleo maintains syntax highlighting implementations across different platforms.
If you do not see your favorite editor on this list, please reach out on GitHub.

  1. Visual Studio Code
  2. Sublime Text
  3. Intellij

Hello Leo

Use the Leo Command Line Interface (CLI) to create a new project. In your terminal, run:

leo new hello
cd hello

This creates a directory with the following structure:

hello/
├── .env # Your program environment
├── program.json # Your program manifest
├── README.md # Your program description
├── build/
├── inputs/
│ ├── hello.in # Your program inputs
└── src/
└── main.leo # Your program file

Let’s run the project.

Zero Knowledge in one line

The leo run command will compile and run the program. In your terminal, run:

leo run main

console output:

Leo Compiled 'main.leo' into Aleo instructions
⛓  Constraints
• 'hello.aleo/main' - 35 constraints (called 1 time)
➡️ Output
• 3u32

Leo ✅ Finished 'hello.aleo/main' (in "/hello/build")

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

Let’s go through the file syntax of the program we just executed.

program.json is the Leo manifest file that configures our package.

program.json

{
"program": "hello.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}

The program ID in program is the official name that other developers will be able to look up after you have published your program.

"program": "hello.aleo",

All files in the current package will be compiled with the specified Leo version.

"version": "0.0.0",

Syntax to circuits

Open up src/main.leo. The main.leo file is the entry point of a Leo project. It often contains a function named main. Let's break down the structure of a Leo file.

src/main.leo

// The 'hello' program.
program hello.aleo {
transition main(public a: u32, b: u32) -> u32 {
let c: u32 = a + b;
return c;
}
}

program hello.aleo { defines the name of the program inside the Leo file. The program ID must match the program.json manifest file. The keyword transition indicates a transition function definition in Leo. Our hello main function takes an input a with type u32 and public visibility, and an input b with type u32 and private visibility (by default). The program returns one result with type u32. The transition function body is enclosed in curly braces { }. It is a common convention in Leo to place the opening curly brace on the same line as the function definition, adding one space in between.

transition main(public a: u32, b: u32) -> u32 {

Inside the main function we declare a variable c with type u32 and set it equal to the addition of variables a and b. Leo's compiler will check that the types of a and b are equal and that the result of the addition is type u32.

let c: u32 = a + b;

INFO

We designed Leo with the belief that developers are human and can make mistakes. Try changing the type of any variable and seeing what Leo recommends with helpful error messages.

Last, we return the variable c. Leo will check that c's type matches the function return type u32.

return c;

Wiring program inputs

Leo’s compiler will build a circuit out of the main.leo program. Open up inputs/hello.in. Files ending in .in provide inputs to the program. You can also specify program arguments via the command line.

inputs/hello.in

// The program input for hello/src/main.leo
[main]
public a: u32 = 1u32;
b: u32 = 2u32;

An input file begins with a section enclosed in square brackets [ ]. The main inside the square brackets indicates that we are defining the inputs to the transition function main. You can only define inputs to transition functions.

[main]

An input assignment shares syntax with an explicit variable assignment in normal .leo files. Here we assign the value 1 of type u32 to the public input named a. We also assign the value 2 of type u32 to the (private, by default) input named b. Leo's compiler will fetch these values and provide them as inputs to the circuit at proving time.

public a: u32 = 1u32;
b: u32 = 2u32;

Now let us use the Leo CLI and see what other commands we can run on our program.

Previously we ran the program with leo run. This command builds and runs the program natively.

leo run

Step by step

1. Clean

First, remove all build files with:

leo clean

console output:

Leo cleaned the build directory (in "/build/")

2. Update

The leo update command updates the Leo compiler to the latest version.

leo update

console output:

Leo ✅ Updated to version 1.9.0

2. Execute

The leo execute command executes the Leo program and outputs a transaction object

leo execute main

console output:

Leo ✅ Compiled 'main.leo' into Aleo instructions
⛓  Constraints
• 'hello.aleo/main' - 35 constraints (called 1 time)
➡️ Output
• 3u32

{"type":"execute","id":"at1 ... (transaction object truncated for brevity)

Leo ✅ Executed 'hello.aleo/main' (in "/hello/build")

The leo execute command will attempt to verify a proof only if all previous steps completed successfully. Under the hood, the Leo CLI will check for existing .prover, .verifier, and .avm files in the build directory before running each command. This ensures that we don't run unnecessary commands.

Thank you for reading this article to the end, and sign up for notifications.

See below for all the official links to Aleo:

Website — https://www.aleo.org/
Discord — https://discord.gg/aleohq
Twitter — https://twitter.com/AleoHQ
GitHub — https://github.com/AleoHQ

--

--