Skip to content

Commit

Permalink
add pynada environment
Browse files Browse the repository at this point in the history
  • Loading branch information
oceans404 committed Apr 2, 2024
1 parent a13f19e commit 9915b14
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 57 deletions.
104 changes: 104 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@ Before you begin, you need to install the following tools:
nillion -V
```
- [Node (>= v18.17)](https://nodejs.org/en/download/)
- Check version with
```
node -v
```
- [python3](https://www.python.org/downloads/) version 3.11 or higher with a working [pip](https://pip.pypa.io/en/stable/getting-started/) installed
- Confirm that you have python3 (version >=3.11) and pip installed:
```
python3 --version
python3 -m pip --version
```
- Yarn ([v1](https://classic.yarnpkg.com/en/docs/install/) or [v2+](https://yarnpkg.com/getting-started/install))
- Check version with
```
Expand Down Expand Up @@ -78,6 +88,100 @@ This command deploys a test smart contract to the local network. The contract is
yarn nillion-devnet
```
5a. Optional: If you want to write your own Nada program, open another terminal to create and activate a virtual environment
```
cd packages/nillion && sh create-venv.sh && source .venv/bin/activate
```
The [nada tool](https://docs.nillion.com/nada) was used to initiate a project inside of packages/nillion/next-project-programs. Create a new Nada program file in next-project-programs/src
```
cd next-project-programs
touch src/{your-nada-program-name}.py
```
For example, if your program is `tiny_secret_addition.py`, run
```
cd next-project-programs
touch src/tiny_secret_addition.py
```
Write your Nada program in the file you just created. Then add the program path, name, and a prime size to your nada-project.toml file
```toml
[[programs]]
path = "src/{your-nada-program-name}.py"
name = "{your-nada-program-name}"
prime_size = 128
```

For example, if your program was `tiny_secret_addition.py`, add to nada-project.toml:

```toml
[[programs]]
path = "src/tiny_secret_addition.py"
name = "tiny_secret_addition"
prime_size = 128
```

Run the build command to build all programs added to the nada-project.toml file, creating nada.bin files for each Nada program.

```
nada build
```

[Generate a test file](https://docs.nillion.com/nada#generate-a-test-file) for your program passing in the test name and program name.

```
nada generate-test --test-name {your-test-name} {your-nada-program-name}
```

For example, if your program was `tiny_secret_addition.py`, run

```
nada generate-test --test-name tiny_secret_addition tiny_secret_addition
```

Update values in tests/{your-test-name}.yaml and run the test

```
nada run {your-test-name}
```

For example, if your test name was `tiny_secret_addition`, run

```
nada run tiny_secret_addition
```

Copy program binary file ({your-nada-program-name}.nada.bin) into nextjs public programs folder to use them in the nextjs app.

```
cp target/{your-nada-program-name}.nada.bin ../../nextjs/public/programs
```

For example, if your program was `tiny_secret_addition.py`, run

```
cp target/tiny_secret_addition.nada.bin ../../nextjs/public/programs
```

Copy the program file ({your-nada-program-name}.py) into nextjs public programs folder

```
cp src/{your-nada-program-name}.py ../../nextjs/public/programs
```

For example, if your program was `tiny_secret_addition.py`, run

```
cp src/tiny_secret_addition.py ../../nextjs/public/programs
```

Now the NextJs app has the Nada program and binaries in the `nextjs/public/programs` folder, where the program can be stored using the JavaScript Client.

6. On a fourth terminal, start your NextJS app:

```
Expand Down
Binary file modified packages/nextjs/public/programs/addition_simple.nada.bin
Binary file not shown.
54 changes: 0 additions & 54 deletions packages/nextjs/public/programs/addition_simple.nada.json

This file was deleted.

3 changes: 1 addition & 2 deletions packages/nextjs/public/programs/addition_simple.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
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)]
return [Output(new_int, "my_output", party1)]
3 changes: 2 additions & 1 deletion packages/nillion/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.env
.env
__pycache__
11 changes: 11 additions & 0 deletions packages/nillion/bootstrap-local-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ update_env "NEXT_PUBLIC_NILLION_BOOTNODE_MULTIADDRESS" "$BOOT_MULTIADDR" $ENV_TO

echo "Running at process pid: $(pgrep -f $NILLION_DEVNET)"

echo "-------------------------------------------------------"
echo " 7MM 7MM "
echo " MM MM "
echo " db MM MM db "
echo " MM MM "
echo ".7MMpMMMb. 7MM MM MM 7MM ,pW-Wq. 7MMpMMMb. "
echo " MM MM MM MM MM MM 6W' Wb MM MM "
echo " MM MM MM MM MM MM 8M M8 MM MM "
echo " MM MM MM MM MM MM YA. ,A9 MM MM "
echo ".JMML JMML..JMML..JMML..JMML..JMML. Ybmd9 .JMML JMML."
echo "-------------------------------------------------------"
echo "-------------------------------------------------------"
echo "-----------🦆 CONNECTED TO NILLION-DEVNET 🦆-----------"
echo "-------------------------------------------------------"
Expand Down
22 changes: 22 additions & 0 deletions packages/nillion/create-venv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

function create_venv () {
if [[ ! -z "${VIRTUAL_ENV:-}" ]]; then
echo "Virtualenv is already active! Run 'deactivate' to deactivate the virtualenv."
return 0
fi

echo "Creating virtualenv"
python3.11 -m pip install install --user virtualenv==20.24.6

NILLION_VENV=".venv"
mkdir -p "$NILLION_VENV"
python3.11 -m virtualenv -p python3.11 "$NILLION_VENV"
source "$NILLION_VENV/bin/activate"
python3.11 -m pip install -r requirements.txt

echo "Virtualenv: $NILLION_VENV"
echo "Check the $NILLION_VENV/lib/python3.11/site-packages folder to make sure you have py_nillion_client and nada_dsl packages"
}

create_venv
13 changes: 13 additions & 0 deletions packages/nillion/next-project-programs/nada-project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name = "next-project-programs"
version = "0.1.0"
authors = [""]

[[programs]]
path = "src/main.py"
name = "main"
prime_size = 128

[[programs]]
path = "src/addition_simple.py"
name = "addition_simple"
prime_size = 128
10 changes: 10 additions & 0 deletions packages/nillion/next-project-programs/src/addition_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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)]
12 changes: 12 additions & 0 deletions packages/nillion/next-project-programs/src/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from nada_dsl import *

def nada_main():
party1 = Party(name="Party1")
party2 = Party(name="Party2")
party3 = Party(name="Party3")
a = SecretInteger(Input(name="A", party=party1))
b = SecretInteger(Input(name="B", party=party2))

result = a + b

return [Output(result, "my_output", party3)]
Binary file not shown.
Binary file not shown.
12 changes: 12 additions & 0 deletions packages/nillion/next-project-programs/tests/addition_simple.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
program: addition_simple
inputs:
secrets:
my_int1:
SecretInteger: '30'
my_int2:
SecretInteger: '3'
public_variables: {}
expected_outputs:
my_output:
SecretInteger: '33'
1 change: 1 addition & 0 deletions packages/nillion/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nada-dsl

0 comments on commit 9915b14

Please sign in to comment.