make example compatible w/ stubs

This commit is contained in:
evan-schott 2023-12-11 13:19:42 -08:00
parent 1090c3e55e
commit de8b2a2cad
83 changed files with 1651 additions and 69 deletions

5
.circleci/lottery/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.env
*.avm
*.prover
*.verifier
outputs/

View File

@ -0,0 +1,19 @@
# lottery.aleo
## Run Guide
To run this program, run:
```bash
leo run play
or
./run.sh
```
## Execute Guide
To execute this program, run:
```bash
leo execute play
```

View File

@ -0,0 +1,26 @@
program lottery.aleo;
record Ticket:
owner as address.private;
mapping num_winners:
key as u8.public;
value as u8.public;
function play:
cast self.caller into r0 as Ticket.record;
async play into r1;
output r0 as Ticket.record;
output r1 as lottery.aleo/play.future;
finalize play:
lte block.height 1000u32 into r0;
assert.eq r0 true;
rand.chacha into r1 as boolean;
assert.eq r1 true;
get.or_use num_winners[0u8] 0u8 into r2;
lt r2 5u8 into r3;
assert.eq r3 true;
add r2 1u8 into r4;
set r4 into num_winners[0u8];

View File

@ -0,0 +1,6 @@
{
"program": "lottery.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}

View File

@ -0,0 +1,2 @@
// The program input for lottery/src/main.leo
[play]

View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1,6 @@
{
"program": "lottery.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}

10
.circleci/lottery/run.sh Normal file
View File

@ -0,0 +1,10 @@
#!/bin/bash
# First check that Leo is installed.
if ! command -v leo &> /dev/null
then
echo "leo is not installed."
exit
fi
# Run the lottery example
leo run play || exit

View File

@ -0,0 +1,30 @@
// The 'lottery' program.
program lottery.aleo {
mapping num_winners: u8 => u8;
record Ticket {
owner: address,
}
transition play() -> Ticket {
let ticket: Ticket = Ticket {
owner: self.caller,
};
return ticket then finalize();
}
finalize play() {
// Check that the lottery has not expired.
assert(block.height <= 1000u32);
// Randomly select whether or not the ticket is a winner.
assert(ChaCha::rand_bool());
// Check that the maximum number of winners have not been reached.
let winners: u8 = num_winners.get_or_use(0u8, 0u8);
assert(winners < 5u8);
num_winners.set(0u8, winners + 1u8);
}
}

5
.circleci/tictactoe/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.env
*.avm
*.prover
*.verifier
outputs/

View File

@ -0,0 +1,82 @@
<!-- # ⭕ Tic-Tac-Toe -->
[//]: # (<img alt="workshop/tictactoe" width="1412" src="../.resources/tictactoe.png">)
A standard game of Tic-Tac-Toe in Leo.
⭕ ❕ ⭕ ❕ ❌
⭕ ❕ ⁣❌ ❕ ⭕
❌ ❕ ❌ ❕ ⭕
## Representing State
Leo allows users to define composite data types with the `struct` keyword.
The game board is represented by a struct called `Board`, which contains three `Row`s.
An alternative representation would be to use an array, however, these are not yet supported in Leo.
## Language Features
- `struct` declarations
- conditional statements
- early termination. Leo allows users to return from a function early using the `return` keyword.
## Running the Program
Leo provides users with a command line interface for compiling and running Leo programs.
Users may either specify input values via the command line or provide an input file in `inputs/`.
### Providing inputs via the command line.
1. Run
```bash
leo run <function_name> <input_1> <input_2> ...
```
See `./run.sh` for an example.
### Using an input file.
1. Modify `inputs/tictactoe.in` with the desired inputs.
2. Run
```bash
leo run <function_name>
```
## Executing the Program
```bash
leo execute <function_name> <input_1> <input_2> ...
```
## Playing the Game
### 1. Create a new game board
```bash
leo run new
```
| | | |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 0 | 0 |
| 0 | 0 | 0 |
### 2. Player 1 makes a move
```bash
leo run make_move 1u8 1u8 1u8 "{ r1: { c1: 0u8, c2: 0u8, c3: 0u8 }, r2: { c1: 0u8, c2: 0u8, c3: 0u8 }, r3: { c1: 0u8, c2: 0u8, c3: 0u8 } }"
```
| | | |
|---|---|---|
| 1 | 0 | 0 |
| 0 | 0 | 0 |
| 0 | 0 | 0 |
### 3. Player 2 makes a move
```bash
leo run make_move 2u8 2u8 2u8 "{ r1: { c1: 1u8, c2: 0u8, c3: 0u8 }, r2: { c1: 0u8, c2: 0u8, c3: 0u8 }, r3: { c1: 0u8, c2: 0u8, c3: 0u8 } }"
```
| | | |
|---|---|---|
| 1 | 0 | 0 |
| 0 | 2 | 0 |
| 0 | 0 | 0 |

View File

@ -0,0 +1,237 @@
program tictactoe.aleo;
struct Row:
c1 as u8;
c2 as u8;
c3 as u8;
struct Board:
r1 as Row;
r2 as Row;
r3 as Row;
function new:
cast 0u8 0u8 0u8 into r0 as Row;
cast 0u8 0u8 0u8 into r1 as Row;
cast 0u8 0u8 0u8 into r2 as Row;
cast r0 r1 r2 into r3 as Board;
output r3 as Board.private;
closure check_for_win:
input r0 as Board;
input r1 as u8;
is.eq r0.r1.c1 r1 into r2;
is.eq r0.r1.c2 r1 into r3;
and r2 r3 into r4;
is.eq r0.r1.c3 r1 into r5;
and r4 r5 into r6;
is.eq r0.r2.c1 r1 into r7;
is.eq r0.r2.c2 r1 into r8;
and r7 r8 into r9;
is.eq r0.r2.c3 r1 into r10;
and r9 r10 into r11;
or r6 r11 into r12;
is.eq r0.r3.c1 r1 into r13;
is.eq r0.r3.c3 r1 into r14;
and r13 r14 into r15;
is.eq r0.r3.c3 r1 into r16;
and r15 r16 into r17;
or r12 r17 into r18;
is.eq r0.r1.c1 r1 into r19;
is.eq r0.r2.c1 r1 into r20;
and r19 r20 into r21;
is.eq r0.r3.c1 r1 into r22;
and r21 r22 into r23;
or r18 r23 into r24;
is.eq r0.r1.c2 r1 into r25;
is.eq r0.r2.c3 r1 into r26;
and r25 r26 into r27;
is.eq r0.r3.c2 r1 into r28;
and r27 r28 into r29;
or r24 r29 into r30;
is.eq r0.r1.c3 r1 into r31;
is.eq r0.r2.c3 r1 into r32;
and r31 r32 into r33;
is.eq r0.r3.c3 r1 into r34;
and r33 r34 into r35;
or r30 r35 into r36;
is.eq r0.r1.c1 r1 into r37;
is.eq r0.r2.c2 r1 into r38;
and r37 r38 into r39;
is.eq r0.r3.c3 r1 into r40;
and r39 r40 into r41;
or r36 r41 into r42;
is.eq r0.r1.c3 r1 into r43;
is.eq r0.r2.c2 r1 into r44;
and r43 r44 into r45;
is.eq r0.r3.c1 r1 into r46;
and r45 r46 into r47;
or r42 r47 into r48;
output r48 as boolean;
function make_move:
input r0 as u8.private;
input r1 as u8.private;
input r2 as u8.private;
input r3 as Board.private;
is.eq r0 1u8 into r4;
is.eq r0 2u8 into r5;
or r4 r5 into r6;
assert.eq r6 true;
lte 1u8 r1 into r7;
lte r1 3u8 into r8;
and r7 r8 into r9;
assert.eq r9 true;
lte 1u8 r2 into r10;
lte r2 3u8 into r11;
and r10 r11 into r12;
assert.eq r12 true;
is.eq r1 1u8 into r13;
is.eq r2 1u8 into r14;
and r13 r14 into r15;
is.eq r3.r1.c1 0u8 into r16;
and r15 r16 into r17;
is.eq r1 1u8 into r18;
is.eq r2 2u8 into r19;
and r18 r19 into r20;
is.eq r3.r1.c2 0u8 into r21;
and r20 r21 into r22;
is.eq r1 1u8 into r23;
is.eq r2 3u8 into r24;
and r23 r24 into r25;
is.eq r3.r1.c3 0u8 into r26;
and r25 r26 into r27;
is.eq r1 2u8 into r28;
is.eq r2 1u8 into r29;
and r28 r29 into r30;
is.eq r3.r2.c1 0u8 into r31;
and r30 r31 into r32;
is.eq r1 2u8 into r33;
is.eq r2 2u8 into r34;
and r33 r34 into r35;
is.eq r3.r2.c2 0u8 into r36;
and r35 r36 into r37;
is.eq r1 2u8 into r38;
is.eq r2 3u8 into r39;
and r38 r39 into r40;
is.eq r3.r2.c3 0u8 into r41;
and r40 r41 into r42;
is.eq r1 3u8 into r43;
is.eq r2 1u8 into r44;
and r43 r44 into r45;
is.eq r3.r3.c1 0u8 into r46;
and r45 r46 into r47;
is.eq r1 3u8 into r48;
is.eq r2 2u8 into r49;
and r48 r49 into r50;
is.eq r3.r3.c2 0u8 into r51;
and r50 r51 into r52;
is.eq r1 3u8 into r53;
is.eq r2 3u8 into r54;
and r53 r54 into r55;
is.eq r3.r3.c3 0u8 into r56;
and r55 r56 into r57;
ternary r57 r0 r3.r3.c3 into r58;
ternary r52 r0 r3.r3.c2 into r59;
ternary r52 r3.r3.c3 r58 into r60;
ternary r47 r0 r3.r3.c1 into r61;
ternary r47 r3.r3.c2 r59 into r62;
ternary r47 r3.r3.c3 r60 into r63;
ternary r42 r0 r3.r2.c3 into r64;
ternary r42 r3.r3.c1 r61 into r65;
ternary r42 r3.r3.c2 r62 into r66;
ternary r42 r3.r3.c3 r63 into r67;
ternary r37 r0 r3.r2.c2 into r68;
ternary r37 r3.r2.c3 r64 into r69;
ternary r37 r3.r3.c1 r65 into r70;
ternary r37 r3.r3.c2 r66 into r71;
ternary r37 r3.r3.c3 r67 into r72;
ternary r32 r0 r3.r2.c1 into r73;
ternary r32 r3.r2.c2 r68 into r74;
ternary r32 r3.r2.c3 r69 into r75;
ternary r32 r3.r3.c1 r70 into r76;
ternary r32 r3.r3.c2 r71 into r77;
ternary r32 r3.r3.c3 r72 into r78;
ternary r27 r0 r3.r1.c3 into r79;
ternary r27 r3.r2.c1 r73 into r80;
ternary r27 r3.r2.c2 r74 into r81;
ternary r27 r3.r2.c3 r75 into r82;
ternary r27 r3.r3.c1 r76 into r83;
ternary r27 r3.r3.c2 r77 into r84;
ternary r27 r3.r3.c3 r78 into r85;
ternary r22 r0 r3.r1.c2 into r86;
ternary r22 r3.r1.c3 r79 into r87;
ternary r22 r3.r2.c1 r80 into r88;
ternary r22 r3.r2.c2 r81 into r89;
ternary r22 r3.r2.c3 r82 into r90;
ternary r22 r3.r3.c1 r83 into r91;
ternary r22 r3.r3.c2 r84 into r92;
ternary r22 r3.r3.c3 r85 into r93;
ternary r17 r0 r3.r1.c1 into r94;
ternary r17 r3.r1.c2 r86 into r95;
ternary r17 r3.r1.c3 r87 into r96;
ternary r17 r3.r2.c1 r88 into r97;
ternary r17 r3.r2.c2 r89 into r98;
ternary r17 r3.r2.c3 r90 into r99;
ternary r17 r3.r3.c1 r91 into r100;
ternary r17 r3.r3.c2 r92 into r101;
ternary r17 r3.r3.c3 r93 into r102;
cast r94 r95 r96 into r103 as Row;
cast r97 r98 r99 into r104 as Row;
cast r100 r101 r102 into r105 as Row;
cast r103 r104 r105 into r106 as Board;
call check_for_win r106 1u8 into r107;
call check_for_win r106 2u8 into r108;
not r107 into r109;
and r109 r108 into r110;
ternary r110 r106.r1.c1 r106.r1.c1 into r111;
not r107 into r112;
and r112 r108 into r113;
ternary r113 r106.r1.c2 r106.r1.c2 into r114;
not r107 into r115;
and r115 r108 into r116;
ternary r116 r106.r1.c3 r106.r1.c3 into r117;
cast r111 r114 r117 into r118 as Row;
not r107 into r119;
and r119 r108 into r120;
ternary r120 r106.r2.c1 r106.r2.c1 into r121;
not r107 into r122;
and r122 r108 into r123;
ternary r123 r106.r2.c2 r106.r2.c2 into r124;
not r107 into r125;
and r125 r108 into r126;
ternary r126 r106.r2.c3 r106.r2.c3 into r127;
cast r121 r124 r127 into r128 as Row;
not r107 into r129;
and r129 r108 into r130;
ternary r130 r106.r3.c1 r106.r3.c1 into r131;
not r107 into r132;
and r132 r108 into r133;
ternary r133 r106.r3.c2 r106.r3.c2 into r134;
not r107 into r135;
and r135 r108 into r136;
ternary r136 r106.r3.c3 r106.r3.c3 into r137;
cast r131 r134 r137 into r138 as Row;
cast r118 r128 r138 into r139 as Board;
not r107 into r140;
and r140 r108 into r141;
ternary r141 2u8 0u8 into r142;
ternary r107 r106.r1.c1 r139.r1.c1 into r143;
ternary r107 r106.r1.c2 r139.r1.c2 into r144;
ternary r107 r106.r1.c3 r139.r1.c3 into r145;
cast r143 r144 r145 into r146 as Row;
ternary r107 r106.r2.c1 r139.r2.c1 into r147;
ternary r107 r106.r2.c2 r139.r2.c2 into r148;
ternary r107 r106.r2.c3 r139.r2.c3 into r149;
cast r147 r148 r149 into r150 as Row;
ternary r107 r106.r3.c1 r139.r3.c1 into r151;
ternary r107 r106.r3.c2 r139.r3.c2 into r152;
ternary r107 r106.r3.c3 r139.r3.c3 into r153;
cast r151 r152 r153 into r154 as Row;
cast r146 r150 r154 into r155 as Board;
ternary r107 1u8 r142 into r156;
output r155 as Board.private;
output r156 as u8.private;

View File

@ -0,0 +1,6 @@
{
"program": "tictactoe.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}

View File

@ -0,0 +1,18 @@
// The `new` function does not take any inputs.
[new]
// Inputs for the `make_move` function.
// - `player` : A u8 representing the player making the move. 1 for player 1, 2 for player 2.
// - `row` : A u8 representing the row to make the move in.
// - `column` : A u8 representing the column to make the move in.
// - `board` : A representation of the board state.
[make_move]
player: u8 = 1u8;
row: u8 = 1u8;
col: u8 = 1u8;
board: Board = Board {
r1: Row { c1: 0u8, c2: 0u8, c3: 0u8 },
r2: Row { c1: 0u8, c2: 0u8, c3: 0u8 },
r3: Row { c1: 0u8, c2: 0u8, c3: 0u8 },
};

View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1,6 @@
{
"program": "tictactoe.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}

157
.circleci/tictactoe/run.sh Normal file
View File

@ -0,0 +1,157 @@
#!/bin/bash
# First check that Leo is installed.
if ! command -v leo &> /dev/null
then
echo "leo is not installed."
exit
fi
# Create a new game.
echo "
###############################################################################
######## ########
######## STEP 0: Creating a new game of Tic-Tac-Toe ########
######## ########
######## | | | | ########
######## | | | | ########
######## | | | | ########
######## ########
###############################################################################
"
leo run new || exit
# Have the Player 1 make a move.
echo "
###############################################################################
######## ########
######## STEP 1: Player 1 makes the 1st move. ########
######## ########
######## | x | | | ########
######## | | | | ########
######## | | | | ########
######## ########
###############################################################################
"
leo run make_move 1u8 1u8 1u8 "{ r1: { c1: 0u8, c2: 0u8, c3: 0u8 }, r2: { c1: 0u8, c2: 0u8, c3: 0u8 }, r3: { c1: 0u8, c2: 0u8, c3: 0u8 } }" || exit
# Have the Player 2 make a move.
echo "
###############################################################################
######## ########
######## STEP 2: Player 2 makes the 2nd move. ########
######## ########
######## | x | | | ########
######## | | o | | ########
######## | | | | ########
######## ########
###############################################################################
"
leo run make_move 2u8 2u8 2u8 "{ r1: { c1: 1u8, c2: 0u8, c3: 0u8 }, r2: { c1: 0u8, c2: 0u8, c3: 0u8 }, r3: { c1: 0u8, c2: 0u8, c3: 0u8 } }" || exit
# Have the Player 1 make a move.
echo "
###############################################################################
######## ########
######## STEP 3: Player 1 makes the 3rd move. ########
######## ########
######## | x | | | ########
######## | | o | | ########
######## | x | | | ########
######## ########
###############################################################################
"
leo run make_move 1u8 3u8 1u8 "{ r1: { c1: 1u8, c2: 0u8, c3: 0u8 }, r2: { c1: 0u8, c2: 2u8, c3: 0u8 }, r3: { c1: 0u8, c2: 0u8, c3: 0u8 } }" || exit
# Have the Player 2 make a move.
echo "
###############################################################################
######## ########
######## STEP 4: Player 2 makes the 4th move. ########
######## ########
######## | x | | | ########
######## | o | o | | ########
######## | x | | | ########
######## ########
###############################################################################
"
leo run make_move 2u8 2u8 1u8 "{ r1: { c1: 1u8, c2: 0u8, c3: 0u8 }, r2: { c1: 0u8, c2: 2u8, c3: 0u8 }, r3: { c1: 1u8, c2: 0u8, c3: 0u8 } }" || exit
# Have the Player 1 make a move.
echo "
###############################################################################
######## ########
######## STEP 5: Player 1 makes the 5th move. ########
######## ########
######## | x | | | ########
######## | o | o | x | ########
######## | x | | | ########
######## ########
###############################################################################
"
leo run make_move 1u8 2u8 3u8 "{ r1: { c1: 1u8, c2: 0u8, c3: 0u8 }, r2: { c1: 2u8, c2: 2u8, c3: 0u8 }, r3: { c1: 1u8, c2: 0u8, c3: 0u8 } }" || exit
# Have the Player 2 make a move.
echo "
###############################################################################
######## ########
######## STEP 6: Player 2 makes the 6th move. ########
######## ########
######## | x | o | | ########
######## | o | o | x | ########
######## | x | | | ########
######## ########
###############################################################################
"
leo run make_move 2u8 1u8 2u8 "{ r1: { c1: 1u8, c2: 0u8, c3: 0u8 }, r2: { c1: 2u8, c2: 2u8, c3: 1u8 }, r3: { c1: 1u8, c2: 0u8, c3: 0u8 } }" || exit
# Have the Player 1 make a move.
echo "
###############################################################################
######## ########
######## STEP 7: Player 1 makes the 7th move. ########
######## ########
######## | x | o | | ########
######## | o | o | x | ########
######## | x | x | | ########
######## ########
###############################################################################
"
leo run make_move 1u8 3u8 2u8 "{ r1: { c1: 1u8, c2: 2u8, c3: 0u8 }, r2: { c1: 2u8, c2: 2u8, c3: 1u8 }, r3: { c1: 1u8, c2: 0u8, c3: 0u8 } }" || exit
# Have the Player 2 make a move.
echo "
###############################################################################
######## ########
######## STEP 8: Player 2 makes the 8th move. ########
######## ########
######## | x | o | | ########
######## | o | o | x | ########
######## | x | x | o | ########
######## ########
###############################################################################
"
leo run make_move 2u8 3u8 3u8 "{ r1: { c1: 1u8, c2: 2u8, c3: 0u8 }, r2: { c1: 2u8, c2: 2u8, c3: 1u8 }, r3: { c1: 1u8, c2: 1u8, c3: 0u8 } }" || exit
echo "
###############################################################################
######## ########
######## STEP 9: Player 1 makes the 9th move. ########
######## ########
######## | x | o | x | ########
######## | o | o | x | ########
######## | x | x | o | ########
######## ########
###############################################################################
"
leo run make_move 1u8 1u8 3u8 "{ r1: { c1: 1u8, c2: 2u8, c3: 0u8 }, r2: { c1: 2u8, c2: 2u8, c3: 1u8 }, r3: { c1: 1u8, c2: 1u8, c3: 2u8 } }" || exit
echo "
###############################################################################
######## ########
######## Game Complete! Players 1 & 2 Tied ########
######## ########
######## | x | o | x | ########
######## | o | o | x | ########
######## | x | x | o | ########
######## ########
###############################################################################
"

View File

@ -0,0 +1,111 @@
program tictactoe.aleo {
// A row in a tic tac toe board.
// - `c1` : The first entry in the row.
// - `c2` : The second entry in the row.
// - `c3` : The third entry in the row.
// A valid entry is either 0, 1, or 2, where 0 is empty, 1 corresponds to player 1, and 2 corresponds to player 2.
// Any other values are invalid.
struct Row {
c1: u8,
c2: u8,
c3: u8
}
// A tic tac toe board.
// - `r1` : The first row in the board.
// - `r2` : The second row in the board.
// - `r3` : The third row in the board.
struct Board {
r1: Row,
r2: Row,
r3: Row,
}
// Returns an empty board.
transition new() -> Board {
return Board {
r1: Row { c1: 0u8, c2: 0u8, c3: 0u8 },
r2: Row { c1: 0u8, c2: 0u8, c3: 0u8 },
r3: Row { c1: 0u8, c2: 0u8, c3: 0u8 },
};
}
// Returns `true` if there exists a row, column, or diagonal with all entries occupied by the same player.
// - `b` : A tic tac toe board.
// - `p` : A number corresponding to a player.
function check_for_win(b: Board, p: u8) -> bool {
return
(b.r1.c1 == p && b.r1.c2 == p && b.r1.c3 == p) || // row 1
(b.r2.c1 == p && b.r2.c2 == p && b.r2.c3 == p) || // row 2
(b.r3.c1 == p && b.r3.c3 == p && b.r3.c3 == p) || // row 3
(b.r1.c1 == p && b.r2.c1 == p && b.r3.c1 == p) || // column 1
(b.r1.c2 == p && b.r2.c3 == p && b.r3.c2 == p) || // column 2
(b.r1.c3 == p && b.r2.c3 == p && b.r3.c3 == p) || // column 3
(b.r1.c1 == p && b.r2.c2 == p && b.r3.c3 == p) || // diagonal
(b.r1.c3 == p && b.r2.c2 == p && b.r3.c1 == p); // other diagonal
}
// Returns an updated tic tac toe board with a move made by a player.
// Returns a `u8` corresponding to the player who won the game, or 0 if no one has won yet.
// - `player` : A number corresponding to a player.
// - `row` : The row of the move.
// - `col` : The column of the move.
// - `board` : A tic tac toe board.
// Assumes that `player` is either 1 or 2.
// Assumes that `row` and `col` are valid indices into the board.
// If an entry is already occupied, the move is invalid and the board is returned unchanged.
transition make_move(player: u8, row: u8, col: u8, board: Board) -> (Board, u8) {
// Check that inputs are valid.
assert(player == 1u8 || player == 2u8);
assert(1u8 <= row && row <= 3u8);
assert(1u8 <= col && col <= 3u8);
// Unpack the entries in the board into variables.
let r1c1: u8 = board.r1.c1;
let r1c2: u8 = board.r1.c2;
let r1c3: u8 = board.r1.c3;
let r2c1: u8 = board.r2.c1;
let r2c2: u8 = board.r2.c2;
let r2c3: u8 = board.r2.c3;
let r3c1: u8 = board.r3.c1;
let r3c2: u8 = board.r3.c2;
let r3c3: u8 = board.r3.c3;
// Update the appropriate entry with the given move.
if row == 1u8 && col == 1u8 && r1c1 == 0u8 {
r1c1 = player;
} else if row == 1u8 && col == 2u8 && r1c2 == 0u8 {
r1c2 = player;
} else if row == 1u8 && col == 3u8 && r1c3 == 0u8 {
r1c3 = player;
} else if row == 2u8 && col == 1u8 && r2c1 == 0u8 {
r2c1 = player;
} else if row == 2u8 && col == 2u8 && r2c2 == 0u8 {
r2c2 = player;
} else if row == 2u8 && col == 3u8 && r2c3 == 0u8 {
r2c3 = player;
} else if row == 3u8 && col == 1u8 && r3c1 == 0u8 {
r3c1 = player;
} else if row == 3u8 && col == 2u8 && r3c2 == 0u8 {
r3c2 = player;
} else if row == 3u8 && col == 3u8 && r3c3 == 0u8 {
r3c3 = player;
}
// Construct the updated game board.
let updated: Board = Board {
r1: Row { c1: r1c1, c2: r1c2, c3: r1c3 },
r2: Row { c1: r2c1, c2: r2c2, c3: r2c3 },
r3: Row { c1: r3c1, c2: r3c2, c3: r3c3 },
};
// Check if the game is over.
if check_for_win(updated, 1u8) {
return (updated, 1u8);
} else if check_for_win(updated, 2u8) {
return (updated, 2u8);
} else {
return (updated, 0u8);
}
}
}

5
.circleci/token/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.env
*.avm
*.prover
*.verifier
outputs/

12
.circleci/token/README.md Normal file
View File

@ -0,0 +1,12 @@
<!-- # 🪙 Token -->
[//]: # (<img alt="workshop/token" width="1412" src="../.resources/token.png">)
A transparent & shielded custom token in Leo.
## Run Guide
To run this program, run:
```bash
./run.sh
```

View File

@ -0,0 +1,93 @@
program token.aleo;
record token:
owner as address.private;
amount as u64.private;
mapping account:
key as address.public;
value as u64.public;
function mint_public:
input r0 as address.public;
input r1 as u64.public;
async mint_public r0 r1 into r2;
output r2 as token.aleo/mint_public.future;
finalize mint_public:
input r0 as address.public;
input r1 as u64.public;
get.or_use account[r0] 0u64 into r2;
add r2 r1 into r3;
set r3 into account[r0];
function mint_private:
input r0 as address.private;
input r1 as u64.private;
cast r0 r1 into r2 as token.record;
output r2 as token.record;
function transfer_public:
input r0 as address.public;
input r1 as u64.public;
async transfer_public self.caller r0 r1 into r2;
output r2 as token.aleo/transfer_public.future;
finalize transfer_public:
input r0 as address.public;
input r1 as address.public;
input r2 as u64.public;
get.or_use account[r0] 0u64 into r3;
sub r3 r2 into r4;
set r4 into account[r0];
get.or_use account[r1] 0u64 into r5;
add r5 r2 into r6;
set r6 into account[r1];
function transfer_private:
input r0 as token.record;
input r1 as address.private;
input r2 as u64.private;
sub r0.amount r2 into r3;
cast r0.owner r3 into r4 as token.record;
cast r1 r2 into r5 as token.record;
output r4 as token.record;
output r5 as token.record;
function transfer_private_to_public:
input r0 as token.record;
input r1 as address.public;
input r2 as u64.public;
sub r0.amount r2 into r3;
cast r0.owner r3 into r4 as token.record;
async transfer_private_to_public r1 r2 into r5;
output r4 as token.record;
output r5 as token.aleo/transfer_private_to_public.future;
finalize transfer_private_to_public:
input r0 as address.public;
input r1 as u64.public;
get.or_use account[r0] 0u64 into r2;
add r2 r1 into r3;
set r3 into account[r0];
function transfer_public_to_private:
input r0 as address.public;
input r1 as u64.public;
cast r0 r1 into r2 as token.record;
async transfer_public_to_private self.caller r1 into r3;
output r2 as token.record;
output r3 as token.aleo/transfer_public_to_private.future;
finalize transfer_public_to_private:
input r0 as address.public;
input r1 as u64.public;
get.or_use account[r0] 0u64 into r2;
sub r2 r1 into r3;
set r3 into account[r0];

View File

@ -0,0 +1,6 @@
{
"program": "token.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}

View File

@ -0,0 +1,34 @@
// The program input for token/src/main.leo
[mint_public]
receiver: address = aleo1ptqvxu4gjfge8tuhgq2pqap0u5pms4p97gwhu7dwngxshpfzcszsswzpzd;
amount: u64 = 100u64;
[mint_private]
receiver: address = aleo1ptqvxu4gjfge8tuhgq2pqap0u5pms4p97gwhu7dwngxshpfzcszsswzpzd;
amount: u64 = 100u64;
[transfer_public]
receiver: address = aleo1mgfq6g40l6zkhsm063n3uhr43qk5e0zsua5aszeq5080dsvlcvxsn0rrau;
amount: u64 = 50u64;
[transfer_private]
sender: token = token {
owner: aleo1ptqvxu4gjfge8tuhgq2pqap0u5pms4p97gwhu7dwngxshpfzcszsswzpzd,
amount: 100u64,
_nonce: 0group,
};
receiver: address = aleo1mgfq6g40l6zkhsm063n3uhr43qk5e0zsua5aszeq5080dsvlcvxsn0rrau;
amount: u64 = 50u64;
[transfer_private_to_public]
sender: token = token {
owner: aleo1ptqvxu4gjfge8tuhgq2pqap0u5pms4p97gwhu7dwngxshpfzcszsswzpzd,
amount: 100u64,
_nonce: 0group,
};
receiver: address = aleo1mgfq6g40l6zkhsm063n3uhr43qk5e0zsua5aszeq5080dsvlcvxsn0rrau;
amount: u64 = 50u64;
[transfer_public_to_private]
receiver: address = aleo1mgfq6g40l6zkhsm063n3uhr43qk5e0zsua5aszeq5080dsvlcvxsn0rrau;
amount: u64 = 50u64;

1
.circleci/token/leo.lock Normal file
View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1,6 @@
{
"program": "token.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}

239
.circleci/token/run.sh Normal file
View File

@ -0,0 +1,239 @@
#!/bin/bash
# First check that Leo is installed.
if ! command -v leo &> /dev/null
then
echo "leo is not installed."
exit
fi
# The private key and address of Alice.
# Swap these into program.json, when running transactions as the first bidder.
# "private_key": "APrivateKey1zkp1w8PTxrRgGfAtfKUSq43iQyVbdQHfhGbiNPEg2LVSEXR",
# "address": "aleo13ssze66adjjkt795z9u5wpq8h6kn0y2657726h4h3e3wfnez4vqsm3008q"
# The private key and address of Bob.
# Swap these into program.json, when running transactions as the second bidder.
# "private_key": "APrivateKey1zkpFo72g7N9iFt3JzzeG8CqsS5doAiXyFvNCgk2oHvjRCzF"
# "address": "aleo17vy26rpdhqx4598y5gp7nvaa9rk7tnvl6ufhvvf4calsrrqdaqyshdsf5z"
# Swap in the private key of Alice.
echo "
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkp1w8PTxrRgGfAtfKUSq43iQyVbdQHfhGbiNPEg2LVSEXR
" > .env
# Publicly mint 100 tokens for Alice.
echo "
###############################################################################
######## ########
######## STEP 1: Publicly mint 100 tokens for Alice ########
######## ########
######## ----------------------------------------- ########
######## | PUBLIC BALANCES | ########
######## ----------------------------------------- ########
######## ----------------------------------------- ########
######## | Alice | 100 | ########
######## ----------------------------------------- ########
######## | Bob | 0 | ########
######## ----------------------------------------- ########
######## ########
######## ----------------------------------------- ########
######## | PRIVATE BALANCES | ########
######## ----------------------------------------- ########
######## ----------------------------------------- ########
######## | Alice | 0 | ########
######## ----------------------------------------- ########
######## | Bob | 0 | ########
######## ----------------------------------------- ########
######## ########
###############################################################################
"
leo run mint_public aleo13ssze66adjjkt795z9u5wpq8h6kn0y2657726h4h3e3wfnez4vqsm3008q 100u64
# Swap in the private key of Bob.
echo "
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkpFo72g7N9iFt3JzzeG8CqsS5doAiXyFvNCgk2oHvjRCzF
" > .env
# Privately mint 100 tokens for Bob.
echo "
###############################################################################
######## ########
######## STEP 2: Privately mint 100 tokens for Bob ########
######## ########
######## ----------------------------------------- ########
######## | PUBLIC BALANCES | ########
######## ----------------------------------------- ########
######## ----------------------------------------- ########
######## | Alice | 100 | ########
######## ----------------------------------------- ########
######## | Bob | 0 | ########
######## ----------------------------------------- ########
######## ########
######## ----------------------------------------- ########
######## | PRIVATE BALANCES | ########
######## ----------------------------------------- ########
######## ----------------------------------------- ########
######## | Alice | 0 | ########
######## ----------------------------------------- ########
######## | Bob | 100 | ########
######## ----------------------------------------- ########
######## ########
###############################################################################
"
leo run mint_private aleo17vy26rpdhqx4598y5gp7nvaa9rk7tnvl6ufhvvf4calsrrqdaqyshdsf5z 100u64
# Swap in the private key of Alice.
echo "
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkp1w8PTxrRgGfAtfKUSq43iQyVbdQHfhGbiNPEg2LVSEXR
" > .env
# Publicly transfer 10 tokens from Alice to Bob.
echo "
###############################################################################
######## ########
######## STEP 3: Publicly transfer 10 tokens from Alice to Bob ########
######## ########
######## ----------------------------------------- ########
######## | PUBLIC BALANCES | ########
######## ----------------------------------------- ########
######## ----------------------------------------- ########
######## | Alice | 90 | ########
######## ----------------------------------------- ########
######## | Bob | 10 | ########
######## ----------------------------------------- ########
######## ########
######## ----------------------------------------- ########
######## | PRIVATE BALANCES | ########
######## ----------------------------------------- ########
######## ----------------------------------------- ########
######## | Alice | 0 | ########
######## ----------------------------------------- ########
######## | Bob | 100 | ########
######## ----------------------------------------- ########
######## ########
###############################################################################
"
leo run transfer_public aleo17vy26rpdhqx4598y5gp7nvaa9rk7tnvl6ufhvvf4calsrrqdaqyshdsf5z 10u64
# Swap in the private key of Bob.
echo "
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkpFo72g7N9iFt3JzzeG8CqsS5doAiXyFvNCgk2oHvjRCzF
" > .env
# Privately transfer 20 tokens from Bob to Alice.
echo "
###############################################################################
######## ########
######## STEP 4: Privately transfer 20 tokens from Bob to Alice ########
######## ########
######## ----------------------------------------- ########
######## | PUBLIC BALANCES | ########
######## ----------------------------------------- ########
######## ----------------------------------------- ########
######## | Alice | 90 | ########
######## ----------------------------------------- ########
######## | Bob | 10 | ########
######## ----------------------------------------- ########
######## ########
######## ----------------------------------------- ########
######## | PRIVATE BALANCES | ########
######## ----------------------------------------- ########
######## ----------------------------------------- ########
######## | Alice | 20 | ########
######## ----------------------------------------- ########
######## | Bob | 80 | ########
######## ----------------------------------------- ########
######## ########
###############################################################################
"
leo run transfer_private "{
owner: aleo17vy26rpdhqx4598y5gp7nvaa9rk7tnvl6ufhvvf4calsrrqdaqyshdsf5z.private,
amount: 100u64.private,
_nonce: 6586771265379155927089644749305420610382723873232320906747954786091923851913group.public
}" aleo13ssze66adjjkt795z9u5wpq8h6kn0y2657726h4h3e3wfnez4vqsm3008q 20u64
# Swap in the private key of Alice.
echo "
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkp1w8PTxrRgGfAtfKUSq43iQyVbdQHfhGbiNPEg2LVSEXR
" > .env
# Convert 30 public tokens from Alice into 30 private tokens for Bob.
echo "
###############################################################################
######## ########
######## STEP 5: Convert 30 public tokens from Alice into 30 ########
######## private tokens for Bob. ########
######## ########
######## ----------------------------------------- ########
######## | PUBLIC BALANCES | ########
######## ----------------------------------------- ########
######## ----------------------------------------- ########
######## | Alice | 60 | ########
######## ----------------------------------------- ########
######## | Bob | 10 | ########
######## ----------------------------------------- ########
######## ########
######## ----------------------------------------- ########
######## | PRIVATE BALANCES | ########
######## ----------------------------------------- ########
######## ----------------------------------------- ########
######## | Alice | 20 | ########
######## ----------------------------------------- ########
######## | Bob | 110 | ########
######## ----------------------------------------- ########
######## ########
###############################################################################
"
leo run transfer_public_to_private aleo17vy26rpdhqx4598y5gp7nvaa9rk7tnvl6ufhvvf4calsrrqdaqyshdsf5z 30u64
# Swap in the private key of Bob.
echo "
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkpFo72g7N9iFt3JzzeG8CqsS5doAiXyFvNCgk2oHvjRCzF
" > .env
# Convert 40 private tokens from Bob into 40 public tokens for Alice.
echo "
###############################################################################
######## ########
######## STEP 6: Convert 40 private tokens from Bob into 40 ########
######## public tokens for Alice. ########
######## ########
######## ----------------------------------------- ########
######## | PUBLIC BALANCES | ########
######## ----------------------------------------- ########
######## ----------------------------------------- ########
######## | Alice | 100 | ########
######## ----------------------------------------- ########
######## | Bob | 10 | ########
######## ----------------------------------------- ########
######## ########
######## ----------------------------------------- ########
######## | PRIVATE BALANCES | ########
######## ----------------------------------------- ########
######## ----------------------------------------- ########
######## | Alice | 20 | ########
######## ----------------------------------------- ########
######## | Bob | 70 | ########
######## ----------------------------------------- ########
######## ########
###############################################################################
"
leo run transfer_private_to_public "{
owner: aleo17vy26rpdhqx4598y5gp7nvaa9rk7tnvl6ufhvvf4calsrrqdaqyshdsf5z.private,
amount: 80u64.private,
_nonce: 1852830456042139988098466781381363679605019151318121788109768539956661608520group.public
}" aleo13ssze66adjjkt795z9u5wpq8h6kn0y2657726h4h3e3wfnez4vqsm3008q 40u64
# Swap in the private key of Alice.
# This is done to ensure that program.json is the same after every execution of ./run.sh.
echo "
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkp1w8PTxrRgGfAtfKUSq43iQyVbdQHfhGbiNPEg2LVSEXR
" > .env

View File

@ -0,0 +1,128 @@
program token.aleo {
// On-chain storage of an `account` map, with `address` as the key,
// and `u64` as the value.
mapping account: address => u64;
record token {
// The token owner.
owner: address,
// The token amount.
amount: u64,
}
/* Mint */
// The function `mint_public` issues the specified token amount for the token receiver publicly on the network.
transition mint_public(public receiver: address, public amount: u64) {
// Mint the tokens publicly by invoking the computation on-chain.
return then finalize(receiver, amount);
}
finalize mint_public(public receiver: address, public amount: u64) {
// Increments `account[receiver]` by `amount`.
// If `account[receiver]` does not exist, it will be created.
// If `account[receiver] + amount` overflows, `mint_public` is reverted.
let current_amount: u64 = Mapping::get_or_use(account, receiver, 0u64);
Mapping::set(account, receiver, current_amount + amount);
}
// The function `mint_private` initializes a new record with the specified amount of tokens for the receiver.
transition mint_private(receiver: address, amount: u64) -> token {
return token {
owner: receiver,
amount: amount,
};
}
/* Transfer */
transition transfer_public(public receiver: address, public amount: u64) {
// Transfer the tokens publicly, by invoking the computation on-chain.
return then finalize(self.caller, receiver, amount);
}
finalize transfer_public(public sender: address, public receiver: address, public amount: u64) {
// Decrements `account[sender]` by `amount`.
// If `account[sender]` does not exist, it will be created.
// If `account[sender] - amount` underflows, `transfer_public` is reverted.
let sender_amount: u64 = Mapping::get_or_use(account, sender, 0u64);
Mapping::set(account, sender, sender_amount - amount);
// Increments `account[receiver]` by `amount`.
// If `account[receiver]` does not exist, it will be created.
// If `account[receiver] + amount` overflows, `transfer_public` is reverted.
let receiver_amount: u64 = Mapping::get_or_use(account, receiver, 0u64);
Mapping::set(account, receiver, receiver_amount + amount);
}
// The function `transfer_private` sends the specified token amount to the token receiver from the specified token record.
transition transfer_private(sender: token, receiver: address, amount: u64) -> (token, token) {
// Checks the given token record has sufficient balance.
// This `sub` operation is safe, and the proof will fail if an overflow occurs.
// `difference` holds the change amount to be returned to sender.
let difference: u64 = sender.amount - amount;
// Produce a token record with the change amount for the sender.
let remaining: token = token {
owner: sender.owner,
amount: difference,
};
// Produce a token record for the specified receiver.
let transferred: token = token {
owner: receiver,
amount: amount,
};
// Output the sender's change record and the receiver's record.
return (remaining, transferred);
}
// The function `transfer_private_to_public` turns a specified token amount from a token record into public tokens for the specified receiver.
// This function preserves privacy for the sender's record, however it publicly reveals the token receiver and the token amount.
transition transfer_private_to_public(sender: token, public receiver: address, public amount: u64) -> token {
// Checks the given token record has a sufficient token amount.
// This `sub` operation is safe, and the proof will fail if an underflow occurs.
// `difference` holds the change amount for the caller.
let difference: u64 = sender.amount - amount;
// Produces a token record with the change amount for the caller.
let remaining: token = token {
owner: sender.owner,
amount: difference,
};
// Output the sender's change record.
// Increment the token amount publicly for the token receiver.
return remaining then finalize(receiver, amount);
}
finalize transfer_private_to_public(public receiver: address, public amount: u64) {
// Increments `account[receiver]` by `amount`.
// If `account[receiver]` does not exist, it will be created.
// If `account[receiver] + amount` overflows, `transfer_private_to_public` is reverted.
let current_amount: u64 = Mapping::get_or_use(account, receiver, 0u64);
Mapping::set(account, receiver, current_amount + amount);
}
// The function `transfer_public_to_private` turns a specified token amount from `account` into a token record for the specified receiver.
// This function preserves privacy for the receiver's record, however it publicly reveals the caller and the specified token amount.
transition transfer_public_to_private(public receiver: address, public amount: u64) -> token {
// Produces a token record for the token receiver.
let transferred: token = token {
owner: receiver,
amount: amount,
};
// Output the receiver's record.
// Decrement the token amount of the caller publicly.
return transferred then finalize(self.caller, amount);
}
finalize transfer_public_to_private(public sender: address, public amount: u64) {
// Decrements `account[sender]` by `amount`.
// If `account[sender]` does not exist, it will be created.
// If `account[sender] - amount` underflows, `transfer_public_to_private` is reverted.
let current_amount: u64 = Mapping::get_or_use(account, sender, 0u64);
Mapping::set(account, sender, current_amount - amount);
}
}

View File

@ -0,0 +1 @@
package = []

View File

@ -6,8 +6,8 @@ record Token:
mapping balances:
key left as field.public;
value right as u64.public;
key as field.public;
value as u64.public;
function issue:
input r0 as address.private;
@ -22,9 +22,10 @@ function deposit:
input r1 as u64.private;
sub r0.amount r1 into r2;
cast r0.owner r2 into r3 as Token.record;
hash.bhp256 r0.owner into r4 as field; output r3 as Token.record;
finalize r4 r1;
hash.bhp256 r0.owner into r4 as field;
async deposit r4 r1 into r5;
output r3 as Token.record;
output r5 as basic_bank.aleo/deposit.future;
finalize deposit:
input r0 as field.public;
@ -547,11 +548,12 @@ function withdraw:
input r2 as u64.private;
input r3 as u64.private;
assert.eq self.caller aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a;
hash.bhp256 r0 into r4 as field; call calculate_interest r1 r2 r3 into r5;
hash.bhp256 r0 into r4 as field;
call calculate_interest r1 r2 r3 into r5;
cast r0 r5 into r6 as Token.record;
async withdraw r4 r1 into r7;
output r6 as Token.record;
finalize r4 r1;
output r7 as basic_bank.aleo/withdraw.future;
finalize withdraw:
input r0 as field.public;

View File

@ -0,0 +1 @@
package = []

5
examples/battleship/board/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.env
*.avm
*.prover
*.verifier
outputs/

View File

@ -0,0 +1,13 @@
# board.aleo
## Build Guide
To compile this Aleo program, run:
```bash
snarkvm build
```
To execute this Aleo program, run:
```bash
snarkvm run hello
```

View File

@ -0,0 +1,46 @@
program board.aleo;
record board_state:
owner as address.private;
hits_and_misses as u64.private;
played_tiles as u64.private;
ships as u64.private;
player_1 as address.private;
player_2 as address.private;
game_started as boolean.private;
function new_board_state:
input r0 as u64.private;
input r1 as address.private;
cast self.caller 0u64 0u64 r0 self.caller r1 false into r2 as board_state.record;
output r2 as board_state.record;
function start_board:
input r0 as board_state.record;
not r0.game_started into r1;
assert.eq r1 true;
cast r0.owner r0.hits_and_misses r0.played_tiles r0.ships r0.player_1 r0.player_2 true into r2 as board_state.record;
output r2 as board_state.record;
function update_played_tiles:
input r0 as board_state.record;
input r1 as u64.private;
sub r1 1u64 into r2;
and r1 r2 into r3;
assert.eq r3 0u64;
and r1 r0.played_tiles into r4;
assert.eq r4 0u64;
or r0.played_tiles r1 into r5;
cast r0.owner r0.hits_and_misses r5 r0.ships r0.player_1 r0.player_2 r0.game_started into r6 as board_state.record;
output r6 as board_state.record;
function update_hits_and_misses:
input r0 as board_state.record;
input r1 as u64.private;
or r0.hits_and_misses r1 into r2;
cast r0.owner r2 r0.played_tiles r0.ships r0.player_1 r0.player_2 r0.game_started into r3 as board_state.record;
output r3 as board_state.record;

View File

@ -0,0 +1,6 @@
{
"program": "board.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}

View File

@ -0,0 +1,4 @@
// The program input for board/src/main.leo
[main]
public a: u32 = 1u32;
b: u32 = 2u32;

View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1,6 @@
{
"program": "board.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}

View File

@ -2,5 +2,22 @@
"program": "battleship.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
"license": "MIT",
"dependencies" : [
{
"name": "board.aleo",
"location": "local",
"path": "board"
},
{
"name": "move.aleo",
"location": "local",
"path": "move"
},
{
"name": "verify.aleo",
"location": "local",
"path": "verify"
}
]
}

View File

@ -0,0 +1,20 @@
[[package]]
name = "board"
location = "local"
path = "board"
checksum = "da94274230d0c0c3deb96d80e07ad9db8bbf53264286c14cc3231b7a8b7ef380"
dependencies = []
[[package]]
name = "move"
location = "local"
path = "move"
checksum = "7d9fef5fe083eb24376e63935855e4ec709c17fb5ee46a0bb4594b0f9ef8eb08"
dependencies = []
[[package]]
name = "verify"
location = "local"
path = "verify"
checksum = "2c2035ebd70500b7e5a9a6198bed1a1163cd1ddfd09128db8f4c16cf23ad2c62"
dependencies = []

5
examples/battleship/move/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.env
*.avm
*.prover
*.verifier
outputs/

View File

@ -0,0 +1,13 @@
# move.aleo
## Build Guide
To compile this Aleo program, run:
```bash
snarkvm build
```
To execute this Aleo program, run:
```bash
snarkvm run hello
```

View File

@ -0,0 +1,24 @@
program move.aleo;
record move:
owner as address.private;
incoming_fire_coordinate as u64.private;
player_1 as address.private;
player_2 as address.private;
prev_hit_or_miss as u64.private;
function create_move:
input r0 as move.record;
input r1 as u64.private;
input r2 as u64.private;
is.eq r0.player_1 r0.owner into r3;
ternary r3 r0.player_2 r0.player_1 into r4;
cast r4 r1 r0.player_2 r0.player_1 r2 into r5 as move.record;
output r5 as move.record;
function start_game:
input r0 as address.private;
cast r0 0u64 self.caller r0 0u64 into r1 as move.record;
output r1 as move.record;

View File

@ -0,0 +1,6 @@
{
"program": "move.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}

View File

@ -0,0 +1,4 @@
// The program input for move/src/main.leo
[main]
public a: u32 = 1u32;
b: u32 = 2u32;

View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1,6 @@
{
"program": "move.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}

View File

@ -2,5 +2,22 @@
"program": "battleship.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
"license": "MIT",
"dependencies" : [
{
"name": "board.aleo",
"location": "local",
"path": "board"
},
{
"name": "move.aleo",
"location": "local",
"path": "move"
},
{
"name": "verify.aleo",
"location": "local",
"path": "verify"
}
]
}

View File

@ -1,8 +1,8 @@
import board.leo;
import move.leo;
import verify.leo;
import board.aleo;
import move.aleo;
import verify.aleo;
// The 'battleship.leo' program.
// The 'battleship.aleo' program.
program battleship.aleo {
// Returns a new record representing a new game of battleship.
transition initialize_board(
@ -16,25 +16,25 @@ program battleship.aleo {
destroyer: u64,
// The address of the opponent.
player: address,
) -> board.leo/board_state.record {
) -> board.aleo/board_state.record {
// Verify that each individual ship placement bitstring is valid.
let valid_carrier: bool = verify.leo/validate_ship(carrier, 5u64, 31u64, 4311810305u64);
let valid_carrier: bool = verify.aleo/validate_ship(carrier, 5u64, 31u64, 4311810305u64);
assert(valid_carrier);
let valid_battleship: bool = verify.leo/validate_ship(battleship, 4u64, 15u64, 16843009u64);
let valid_battleship: bool = verify.aleo/validate_ship(battleship, 4u64, 15u64, 16843009u64);
assert(valid_battleship);
let valid_cruiser: bool = verify.leo/validate_ship(cruiser, 3u64, 7u64, 65793u64);
let valid_cruiser: bool = verify.aleo/validate_ship(cruiser, 3u64, 7u64, 65793u64);
assert(valid_cruiser);
let valid_destroyer: bool = verify.leo/validate_ship(destroyer, 2u64, 3u64, 257u64);
let valid_destroyer: bool = verify.aleo/validate_ship(destroyer, 2u64, 3u64, 257u64);
assert(valid_destroyer);
// Create the board with all the ship placements combined.
let board: u64 = verify.leo/create_board(carrier, battleship, cruiser, destroyer);
let board: u64 = verify.aleo/create_board(carrier, battleship, cruiser, destroyer);
// Initialize the board state record.
let state: board_state = board.leo/new_board_state(board, player);
let state: board_state = board.aleo/new_board_state(board, player);
return state;
}
@ -44,10 +44,10 @@ program battleship.aleo {
// This function commits a given board to a game with an opponent and creates the initial dummy move.
transition offer_battleship(
// The board record to start a game with.
board: board.leo/board_state.record,
) -> (board.leo/board_state.record, move.leo/move.record) {
let state: board_state = board.leo/start_board(board);
let dummy: move = move.leo/start_game(board.player_2);
board: board.aleo/board_state.record,
) -> (board.aleo/board_state.record, move.aleo/move.record) {
let state: board_state = board.aleo/start_board(board);
let dummy: move = move.aleo/start_game(board.player_2);
return (state, dummy);
}
@ -56,16 +56,16 @@ program battleship.aleo {
// Returns dummy move record owned by the opponent.
transition start_battleship(
// The board record to play the game with.
board: board.leo/board_state.record,
board: board.aleo/board_state.record,
// The move record to play to begin the game. This should be the dummy move record created from offer_battleship.
move_start: move.leo/move.record,
) -> (board.leo/board_state.record, move.leo/move.record) {
move_start: move.aleo/move.record,
) -> (board.aleo/board_state.record, move.aleo/move.record) {
// Validate that the move players and board players match each other.
assert_eq(board.player_1, move_start.player_2);
assert_eq(board.player_2, move_start.player_1);
let state: board_state = board.leo/start_board(board);
let dummy: move = move.leo/start_game(board.player_2);
let state: board_state = board.aleo/start_board(board);
let dummy: move = move.aleo/start_game(board.player_2);
return (state, dummy);
}
@ -74,12 +74,12 @@ program battleship.aleo {
// Returns new move record owned by opponent.
transition play(
// The board record to update.
board: board.leo/board_state.record,
board: board.aleo/board_state.record,
// The incoming move from the opponent.
move_incoming: move.leo/move.record,
move_incoming: move.aleo/move.record,
// The u64 equivalent of the bitwise representation of the next coordinate to play on the opponent's board.
shoot: u64,
) -> (board.leo/board_state.record, move.leo/move.record) {
) -> (board.aleo/board_state.record, move.aleo/move.record) {
// Verify the board has been started. This prevents players from starting a game and then creating
// a brand new board to play with.
assert(board.game_started);
@ -89,15 +89,15 @@ program battleship.aleo {
assert_eq(board.player_2, move_incoming.player_1);
// Play coordinate on own board. Will fail if not a valid move.
let hit_or_miss: board_state = board.leo/update_played_tiles(board, shoot);
let hit_or_miss: board_state = board.aleo/update_played_tiles(board, shoot);
// Update own board with result of last shot.
let next_board: board_state = board.leo/update_hits_and_misses(hit_or_miss, move_incoming.prev_hit_or_miss);
let next_board: board_state = board.aleo/update_hits_and_misses(hit_or_miss, move_incoming.prev_hit_or_miss);
// Assess whether incoming fire coordinate is a hit.
let is_hit: u64 = move_incoming.incoming_fire_coordinate & board.ships;
let next_move: move = move.leo/create_move(move_incoming, shoot, is_hit);
let next_move: move = move.aleo/create_move(move_incoming, shoot, is_hit);
return (next_board, next_move);
}

5
examples/battleship/verify/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.env
*.avm
*.prover
*.verifier
outputs/

View File

@ -0,0 +1,13 @@
# verify.aleo
## Build Guide
To compile this Aleo program, run:
```bash
snarkvm build
```
To execute this Aleo program, run:
```bash
snarkvm run hello
```

View File

@ -0,0 +1,73 @@
program verify.aleo;
closure bitcount:
input r0 as u64;
div r0 2u64 into r1;
div r0 4u64 into r2;
div r0 8u64 into r3;
and r1 8608480567731124087u64 into r4;
and r2 3689348814741910323u64 into r5;
and r3 1229782938247303441u64 into r6;
sub r0 r4 into r7;
sub r7 r5 into r8;
sub r8 r6 into r9;
div r9 16u64 into r10;
add r9 r10 into r11;
and r11 1085102592571150095u64 into r12;
rem r12 255u64 into r13;
output r13 as u64;
closure adjacency_check:
input r0 as u64;
input r1 as u64;
div r0 r1 into r2;
is.eq r2 0u64 into r3;
ternary r3 3u64 r2 into r4;
sub r4 1u64 into r5;
and r5 r4 into r6;
is.eq r6 0u64 into r7;
output r7 as boolean;
closure horizontal_check:
input r0 as u64;
input r1 as u64;
rem r0 255u64 into r2;
div r2 r1 into r3;
is.eq r3 0u64 into r4;
ternary r4 3u64 r3 into r5;
sub r5 1u64 into r6;
and r6 r5 into r7;
is.eq r7 0u64 into r8;
output r8 as boolean;
function validate_ship:
input r0 as u64.private;
input r1 as u64.private;
input r2 as u64.private;
input r3 as u64.private;
call bitcount r0 into r4;
assert.eq r4 r1;
call adjacency_check r0 r2 into r5;
call horizontal_check r0 r2 into r6;
and r5 r6 into r7;
call adjacency_check r0 r3 into r8;
or r7 r8 into r9;
output r9 as boolean.private;
function create_board:
input r0 as u64.private;
input r1 as u64.private;
input r2 as u64.private;
input r3 as u64.private;
or r0 r1 into r4;
or r4 r2 into r5;
or r5 r3 into r6;
call bitcount r6 into r7;
assert.eq r7 14u64;
output r6 as u64.private;

View File

@ -0,0 +1,6 @@
{
"program": "verify.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}

View File

@ -0,0 +1,4 @@
// The program input for verify/src/main.leo
[main]
public a: u32 = 1u32;
b: u32 = 2u32;

View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1,6 @@
{
"program": "verify.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}

View File

@ -0,0 +1 @@
package = []

View File

@ -4,4 +4,7 @@ program core.aleo;
function main:
input r0 as field.private;
hash.bhp256 r0 into r1 as field; hash.psd2 r1 into r2 as field; commit.bhp256 r2 1scalar into r3 as field; output r3 as field.private;
hash.bhp256 r0 into r1 as field;
hash.psd2 r1 into r2 as field;
commit.bhp256 r2 1scalar into r3 as field;
output r3 as field.private;

1
examples/core/leo.lock Normal file
View File

@ -0,0 +1 @@
package = []

1
examples/groups/leo.lock Normal file
View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1 @@
package = []

View File

@ -5,14 +5,14 @@ record Ticket:
mapping num_winners:
key left as u8.public;
value right as u8.public;
key as u8.public;
value as u8.public;
function play:
cast self.caller into r0 as Ticket.record;
async play into r1;
output r0 as Ticket.record;
finalize;
output r1 as lottery.aleo/play.future;
finalize play:
lte block.height 1000u32 into r0;

View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1 @@
package = []

View File

@ -181,7 +181,7 @@ function make_move:
ternary r17 r3.r3.c3 r93 into r102;
cast r94 r95 r96 into r103 as Row;
cast r97 r98 r99 into r104 as Row;
cast r100 r98 r99 into r105 as Row;
cast r100 r101 r102 into r105 as Row;
cast r103 r104 r105 into r106 as Board;
call check_for_win r106 1u8 into r107;
call check_for_win r106 2u8 into r108;

View File

@ -0,0 +1 @@
package = []

View File

@ -6,14 +6,14 @@ record token:
mapping account:
key left as address.public;
value right as u64.public;
key as address.public;
value as u64.public;
function mint_public:
input r0 as address.public;
input r1 as u64.public;
finalize r0 r1;
async mint_public r0 r1 into r2;
output r2 as token.aleo/mint_public.future;
finalize mint_public:
input r0 as address.public;
@ -33,8 +33,8 @@ function mint_private:
function transfer_public:
input r0 as address.public;
input r1 as u64.public;
finalize self.caller r0 r1;
async transfer_public self.caller r0 r1 into r2;
output r2 as token.aleo/transfer_public.future;
finalize transfer_public:
input r0 as address.public;
@ -65,9 +65,9 @@ function transfer_private_to_public:
input r2 as u64.public;
sub r0.amount r2 into r3;
cast r0.owner r3 into r4 as token.record;
async transfer_private_to_public r1 r2 into r5;
output r4 as token.record;
finalize r1 r2;
output r5 as token.aleo/transfer_private_to_public.future;
finalize transfer_private_to_public:
input r0 as address.public;
@ -81,9 +81,9 @@ function transfer_public_to_private:
input r0 as address.public;
input r1 as u64.public;
cast r0 r1 into r2 as token.record;
async transfer_public_to_private self.caller r1 into r3;
output r2 as token.record;
finalize self.caller r1;
output r3 as token.aleo/transfer_public_to_private.future;
finalize transfer_public_to_private:
input r0 as address.public;

1
examples/token/leo.lock Normal file
View File

@ -0,0 +1 @@
package = []

View File

@ -0,0 +1 @@
package = []

View File

@ -16,31 +16,32 @@ record Ticket:
mapping proposals:
key left as field.public;
value right as ProposalInfo.public;
key as field.public;
value as ProposalInfo.public;
mapping tickets:
key left as field.public;
value right as u64.public;
key as field.public;
value as u64.public;
mapping agree_votes:
key left as field.public;
value right as u64.public;
key as field.public;
value as u64.public;
mapping disagree_votes:
key left as field.public;
value right as u64.public;
key as field.public;
value as u64.public;
function propose:
input r0 as ProposalInfo.public;
assert.eq self.caller r0.proposer;
hash.bhp256 r0.title into r1 as field; cast self.caller r1 r0 into r2 as Proposal.record;
hash.bhp256 r0.title into r1 as field;
cast self.caller r1 r0 into r2 as Proposal.record;
async propose r1 into r3;
output r2 as Proposal.record;
finalize r1;
output r3 as vote.aleo/propose.future;
finalize propose:
input r0 as field.public;
@ -51,9 +52,9 @@ function new_ticket:
input r0 as field.public;
input r1 as address.public;
cast r1 r0 into r2 as Ticket.record;
async new_ticket r0 into r3;
output r2 as Ticket.record;
finalize r0;
output r3 as vote.aleo/new_ticket.future;
finalize new_ticket:
input r0 as field.public;
@ -64,8 +65,8 @@ finalize new_ticket:
function agree:
input r0 as Ticket.record;
finalize r0.pid;
async agree r0.pid into r1;
output r1 as vote.aleo/agree.future;
finalize agree:
input r0 as field.public;
@ -76,8 +77,8 @@ finalize agree:
function disagree:
input r0 as Ticket.record;
finalize r0.pid;
async disagree r0.pid into r1;
output r1 as vote.aleo/disagree.future;
finalize disagree:
input r0 as field.public;

1
examples/vote/leo.lock Normal file
View File

@ -0,0 +1 @@
package = []