Skip to main content

Example Programs

A closer look into some Nada programs.

Here's a tiny Nada program that adds two secret integer inputs.

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)]

tiny_addition.py takes in two Inputs from the same Party

Input nameInput typeParty name
my_secret_1SecretIntegerNilla 🐶
my_secret_2SecretIntegerNilla 🐶

tiny_addition.py returns an Output to a Party

Output valueOutput nameOutput TypeParty nameOutput typeParty nameOutput name
totalmy_outputSecretIntegerNilla 🐶SecretIntegerNilla 🐶my_output

Let's do something more interesting!

The following program adds up every element of the array:

programs/reduce_simple.py
loading...

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

programs/nada_fn_max.py
loading...

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.