Create a Nada project and write your first Nada program
The nada tool is used to create a new Nada project, compile all programs in the project, run Nada programs, and generate tests for Nada programs.
Create a new Nada project
Create a quickstart directory. Inside of quickstart, use the nada tool to create a new nada project named "nada_quickstart_programs" by running
mkdir quickstart
cd quickstart
nada init nada_quickstart_programs
Set up a Python virtual environment and install nada-dsl
The Nillion Network leverages Nada, our MPC language, for defining MPC programs. Our initial implementation of Nada comes in the form of Nada, a Python DSL (Domain Specific Language).
-
Change directories into your new nada project directory
cd nada_quickstart_programs
-
Create a python virtual environment
python3 -m venv .venv -
Activate the python virtual environment
source .venv/bin/activate -
Install nada-dsl from pypi
pip install --upgrade nada-dsl
Write your first Nada program
Open the nada_quickstart_programs folder in your code editor of choice.
Your Nada project has the following structure:
nada-project.toml # Configuration file for your Nada programs
src/ # Directory for writing Nada programs
src/main.py # An example Nada program that adds 2 secret integers
target/ # Directory where `nada build` puts resulting compiled programs
tests/ # Directory where `nada generate-test` puts resulting test files
1. Create a Nada program file
The src/ directory is where you'll write your Nada programs. Create a new Nada program file in the src directory. The secret_addition.py Nada program will add 2 SecretInteger Nada values together.
touch src/secret_addition.py
2. Write the secret addition Nada program
Copy this Nada program into the secret_addition.py file. Don't worry, we'll go through a line by line explanation of the program next.
from nada_dsl import *
def nada_main():
party1 = Party(name="Party1")
my_int1 = SecretInteger(Input(name="my_int1", party=party1))
my_int2 = SecretInteger(Input(name="my_int2", party=party1))
new_int = my_int1 + my_int2
return [Output(new_int, "my_output", party1)]
3. Understand each line of the secret_addition.py Nada program
Let's go through the program line by line to understand what is each part is doing.
-
First we import all from nada_dsl, the Nada language
from nada_dsl import * -
Next we create
function nada_main()- this is the main function that contains our Nada program's code.from nada_dsl import *
def nada_main(): -
Create a party with a name:
Party1from nada_dsl import *
def nada_main():
party1 = Party(name="Party1")This is a single party program, but you could add more than one party; for example we could define
party2 = Party(name="Party2"). -
Create program inputs
from nada_dsl import *
def nada_main():
party1 = Party(name="Party1")
my_int1 = SecretInteger(Input(name="my_int1", party=party1))
my_int2 = SecretInteger(Input(name="my_int2", party=party1))This program has two inputs, both secret integers. Each input must have a
nameand apartyassociated to it. Currently in nada you can only compute on secret or public integers (and rationals by using thenada-algebralibrary). -
Compute on the inputs
from nada_dsl import *
def nada_main():
party1 = Party(name="Party1")
my_int1 = SecretInteger(Input(name="my_int1", party=party1))
my_int2 = SecretInteger(Input(name="my_int2", party=party1))
new_int = my_int1 + my_int2This performs addition to sum the inputs. Check out all the built in operations available in nada here.
-
Return the output of the program
from nada_dsl import *
def nada_main():
party1 = Party(name="Party1")
my_int1 = SecretInteger(Input(name="my_int1", party=party1))
my_int2 = SecretInteger(Input(name="my_int2", party=party1))
new_int = my_int1 + my_int2
return [Output(new_int, "my_output", party1)]To output the result of a program, we must provide a name - in this case
my_output- and a party to whom the output is provided - in this caseparty1.
Compile, run, and test your Nada program
Use the nada tool to compile, run and test the program we have just written. More information about the nada tool can be found here.
-
Add your program to
nada-project.tomlconfig fileFor the nada tool to know about our program, we need to add the following to the to the
nada-project.tomlconfig file.[[programs]]
path = "src/secret_addition.py"
name = "secret_addition"
prime_size = 128 -
Run the
nada buildcommand to compile programs.nada buildThis will compile all programs listed in the nada-project.toml file. You will see the binary files (.nada.bin) outputted to the
target/directory. -
Generate test
nada generate-test --test-name secret_addition_test secret_additionThis uses the nada tool to generate a set of test values, that will be stored in
tests/. Here secret_addition_test is the name of the test, and secret_addition is the name of the program we want to test. You will notice that the test file (tests/secret_addition_test.yaml) is automatically populated with3s everywhere by default. Later, for the test to pass, we will have to change the output from3to the correct output. -
Run the program with test values
nada run secret_addition_testNow we run the program. This uses the inputs defined in the test file (tests/secret_addition_test.yaml) and runs the program and prints the result. Make note of the result, we will need it next.
-
Test the program
nada test secret_addition_testFinally, we test the program. If you run the above command without altering the default values (
Modify test values in3s) in the test file (tests/secret_addition_test.yaml), the test will fail because the expected test output doesn't match the resulting output. Modify the value ofmy_outputin the test file and re-test the program.secret_addition_test.yaml
---
program: secret_addition
inputs:
my_int1:
SecretInteger: "3"
my_int2:
SecretInteger: "3"
expected_outputs:
my_output:
SecretInteger: "6"
nada test secret_addition_test
Next steps
🧮 Fantastic work! You just created your first Nada project and wrote and tested a Nada program. Now you can move onto the final step: hooking up the secret_addition Nada program to a blind app, which will be able to store the program, then compute with the program on secret, user provided inputs.