Aleo development quick start. Part 2: leo program coding

Loggy
4 min readAug 26, 2022

Previously we did a brief overview of Aleo toolset, set up the development environment, and run our first zk Application.

Now it’s time to write some code and have a look at the program lifecycle.

We’ll build a very basic auction program that stores the last highest bid and its bidder.

Aleo account

Let’s create a new account and see what it consists of:

aleo account new

Private Key APrivateKey1zkpDF9TGht8j1MnruiFec6HnZhCMKkGLTifJSMVFrx5SnJF
View Key AViewKey1fJ9zhDmvBLQvQGFiPVMv7GuU1osoFnUpLAgn5R5pnZgt
Address aleo1t08nvwtn6xsfr2vzdxlryme5sjp2fnfylqw8htyj3yrhrnjnvszqg5k64f

Usually we expect to receive a keypair consisting of public and private kay. But aleo generated for us three keys: Private, View, and Address. Let’s see how we can use them:

Private key — the most sensitive part here. This key you should never share with anybody. The account private key is used to authorize transactions that are able to update the state of account records.

View key —enables you to read your account record state.

Address — enables you to interact with one another, sending and receiving records that encode values and application data.

Account records

What are the records I’ve mentioned above?

Record is a fundamental thing in Aleo. Each account is able to produce records by consuming an old record it owns to produce a new one with the transaction. This is how assets and data related to the account are stored and updated and this is how you can share data between accounts. Only the sender and receiver can decrypt record data using their view key. Each transaction contains zk proof that verifies that produced record is valid. This is how your data keeps private and trusted.

program.json

After we’ve generated our account keys we can use it as the program owner.
Let’s create a new leo project:

leo new auction
cd auction

Now open this folder with the code editor you chose in previous article and find program.json file. This file contains our program description and info about the owner (something similar to `package.json` in Node.js projects).
Let’s change privet_kay and address to values that we generated above:

{  "program": "auction.aleo",  "version": "0.0.0",  "description": "",  "development": {    "private_key": "APrivateKey1zkpDF9TGht8j1MnruiFec6HnZhCMKkGLTifJSMVFrx5SnJF",    "address": "aleo1t08nvwtn6xsfr2vzdxlryme5sjp2fnfylqw8htyj3yrhrnjnvszqg5k64f"  },"license": "MIT"}

Leo program

Now let’s open src/main.leo and do some changes. src/main.leo` is an entrypoint to our program:

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

I hope you can easily read what it does. (takes two unsigned integers and returns sum =). From this short script, we can figure out that Leo is the statically typed language and it has a pretty common syntax for defining functions and typed variables.

Let’s modify our program to accept and store bids:

@programfunction main(bidder: address, bid: u64) -> (address, u64) {  let winning_bidder: address = bidder;  let winning_bid: u64 = bid;  return (winning_bidder, winning_bid);}

Now we have a program that stores the last bid and address that made this bid. But this is not enough for a real auction. Our program should store bid info only if it’s higher than the already stored bid. Let’s add ternary expressions to check this condition:

@program
function main(current_0: address, current_1: u64, bidder: address, bid: u64) -> (address, u64) {
let winning_bidder: address = bid > current_1 ? bidder : current_0;
let winning_bid: u64 = bid > current_1 ? bid : current_1;
return (winning_bidder, winning_bid);
}

And, actually, this is it. Now we have a functional leo program with no hint of cryptography. Our function takes the current highest bid address and amount compares the current bid with the new one and stores the new bid if it’s higher. As you can notice, Leo has a special type for addresses, and returned type is a tuple.

Our leo script is ready but don’t rush to build and run it. We need to discuss another important concept in aleo programs: input parameters.
To run the program we should define inputs in the inputs/main.leofile. This input usually provided by the user is fully private and not revealed to the public network unless the user marks them as public. Let’s open it and see that it stores parameters for the initial version of the program that sums two numbers:

[main]
public a: u32 = 1u32;
b: u32 = 2u32;

Our current program accepts 4 arguments, so let’s adjust the input file:

[main]
current_0: address = aleo144d0sdmvlgfuwuhuuzxl32ad5thrnz7clvm94d5ch5r8d302quyqx43a63;
current_1: u64 = 10u64;
bidder: address = aleo1ndg8jry0vkkyxrwggxhvka6e24a5n284wt0heteq68uq5juptsgqn8zesa;
bid: u64 = 100u64;

You can always run aleo account new to create as many addresses as you need.

And now our program is ready. Let’s run it:

# remove build of initial program
leo clean
# build and run our auction program
leo run
Build ✅ Compiled 'main.leo' into Aleo instructions (in "/home/ubuntu/auction/build/main.aleo")
⏳ Compiling 'auction.aleo'...
• Loaded universal setup (in 2901 ms)
• Built 'main' (in 49171 ms)
Build ✅ Built 'auction.aleo' (in "/home/ubuntu/auction/build")
• Loaded universal setup (in 1111 ms)
🚀 Executing 'auction.aleo/main'...
• Executing 'auction.aleo/main'...
• Executed 'main' (in 7575 ms)
➡️ Outputs• aleo1ndg8jry0vkkyxrwggxhvka6e24a5n284wt0heteq68uq5juptsgqn8zesa
• 100u64
Executing ✅ Executed 'auction.aleo/main' (in "/home/ubuntu/auction/build")

Look at the Output: it contains bidder's address and its bid as we expected.

Now if you want to run local node with your program just run this command from the project root directory:

leo node start

Summary

In this short series, we’ve learned what is Aleo toolset and written the very first simple zk Application from scratch. And I hope this was a good starting point in your endless journey to ZK world. I didn’t touch many important things like circuits, tests, etc., and I will cover them in the next articles but now you have everything to start experiencing. Don’t forget to check the official documentation: https://developer.aleo.org/ and project Discord.

Good luck to you and stay tuned!

--

--