Skip to main content

Programming with Nada

This is a basic introduction to programming with the Nada embedded domain-specific language (DSL). Use this page to learn how to compile and run a basic Nada program. Once you are ready, proceed to the other tutorials to learn Nada features via simple example programs!

Example Programsโ€‹

Tiny Addition (with a Single Party)โ€‹

Below is a tiny Nada program tiny_addition.py that adds two secret integer inputs. Both of these integer inputs belong to Nilla ๐Ÿถ, who is using the Nillion Network to compute their total. Most importantly, the secret integers are never revealed to the Nillion Network nodes!

from nada_dsl import *

def nada_main():
nilla_the_dog = Party(name="Nilla ๐Ÿถ")

secret_int_1 = SecretInteger(Input(name="my_secret_1", party=nilla_the_dog))
secret_int_2 = SecretInteger(Input(name="my_secret_2", party=nilla_the_dog))

total = secret_int_1 + secret_int_2

return [Output(total, "my_output", nilla_the_dog)]

The tiny_addition.py program takes in two Inputs from the same Party.

Input nameInput typeParty name
my_secret_1SecretIntegerNilla ๐Ÿถ
my_secret_2SecretIntegerNilla ๐Ÿถ

The program tiny_addition.py returns an Output to a Party. Only that party sees the output because it it of type SecretInteger.

Output valueOutput NameOutput TypeParty name
totalmy_outputSecretIntegerNilla ๐Ÿถ

Ternary conditional operatorsโ€‹

Next, let's see an example of our ternary operation:

programs/nada_fn_max.py
loading...

Multiple parties in Nadaโ€‹

Lastly, below is an example with two parties:

programs/less_or_equal_than.py
loading...

For more examples visit our programs directory.

Compile and run Nada programsโ€‹

The nada tool can be used to manage Nada projects (create project, compile, run, and test programs, generate tests, etc.). Alternatively, you can use the pynadac and nada-run standalone tools.

Compile a Nada program with pynadacโ€‹

Before the programs can be stored and run on the Nillion Network, they must be compiled into bytecode. The Nada Compiler, referred to as pynadac, is a developer tool within the Nillion SDK used to compile programs written in Nada to bytecode.

Compiling the example above outputs the compiled circuit to bytecode in a new file:

  • Input program: tiny_addition.py
  • Output bytecode: tiny_addition.nada.bin

Run a program locally with the nada-runโ€‹

Use the nada-run developer tool within the Nillion SDK to quickly execute and iterate on Nada programs under an environment that is a close as it can get to running them in a real network. The local network tool:

  • takes in a compiled Nada program and secrets,
  • creates blinding factors locally to hide every secret input you provide to the program,
  • creates a stripped down version of a Nillion cluster and runs the same bytecode that would be run during a real execution of your program.
# secrets file
integers:
my_secret_1: 6
my_secret_2: 4

Local program execution goes through the same flows your code would on the Nillion Network. Here's how you could execute the program locally from the command line assuming you had the following files

  • secrets
  • tiny_addition.nada.bin
./nada-run --secrets-path ./secrets tiny_addition.nada.bin

The result of executing the tiny_addition.nada.bin program on the secret inputs from the secrets file with nada-run is 10.