Skip to content

Commit

Permalink
Bugfix/examples (#43)
Browse files Browse the repository at this point in the history
Update examples
  • Loading branch information
mathias-nillion authored Jun 21, 2024
1 parent cdc7778 commit e45cebf
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 111 deletions.
21 changes: 12 additions & 9 deletions examples/broadcasting/main.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
"""Broadcasting example script"""

import asyncio
import os
import sys

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))

import asyncio

import numpy as np
import py_nillion_client as nillion
from dotenv import load_dotenv
from nillion_python_helpers import (create_nillion_client, getNodeKeyFromFile,
getUserKeyFromFile)

import nada_numpy.client as na_client
# Import helper functions for creating nillion client and getting keys
from examples.broadcasting.config import DIM
from examples.common.nillion_client_helper import create_nillion_client
from examples.common.nillion_keypath_helper import (getNodeKeyFromFile,
getUserKeyFromFile)
from examples.common.utils import compute, store_program, store_secrets
from examples.common.utils import compute, store_program, store_secret_array

# Load environment variables from a .env file
load_dotenv()
Expand Down Expand Up @@ -43,7 +46,7 @@ async def main() -> None:
# Create and store secrets for two parties
A = np.ones([DIM])
C = np.ones([DIM])
A_store_id = await store_secrets(
A_store_id = await store_secret_array(
client,
cluster_id,
program_id,
Expand All @@ -53,7 +56,7 @@ async def main() -> None:
"A",
nillion.SecretInteger,
)
C_store_id = await store_secrets(
C_store_id = await store_secret_array(
client,
cluster_id,
program_id,
Expand All @@ -66,7 +69,7 @@ async def main() -> None:

B = np.ones([DIM])
D = np.ones([DIM])
B_store_id = await store_secrets(
B_store_id = await store_secret_array(
client,
cluster_id,
program_id,
Expand All @@ -76,7 +79,7 @@ async def main() -> None:
"B",
nillion.SecretInteger,
)
D_store_id = await store_secrets(
D_store_id = await store_secret_array(
client,
cluster_id,
program_id,
Expand Down
26 changes: 0 additions & 26 deletions examples/common/nillion_client_helper.py

This file was deleted.

29 changes: 0 additions & 29 deletions examples/common/nillion_keypath_helper.py

This file was deleted.

101 changes: 87 additions & 14 deletions examples/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import os
import time
from typing import Any, Callable, Dict, List, Type
from typing import Any, Callable, Dict, List

import numpy as np
import py_nillion_client as nillion

import nada_numpy as na
import nada_numpy.client as na_client


Expand Down Expand Up @@ -82,46 +83,118 @@ async def store_program(
return program_id


async def store_secrets(
async def store_secret_array(
client: nillion.NillionClient,
cluster_id: str,
program_id: str,
party_id: str,
party_name: str,
secret: Any,
secret_array: np.ndarray,
name: str,
nada_type: Any,
):
"""
Asynchronous function to store secrets on the nillion client.
Asynchronous function to store secret arrays on the nillion client.
Args:
client (nillion.NillionClient): Nillion client.
cluster_id (str): Cluster ID.
program_id (str): Program ID.
party_id (str): Party ID.
party_name (str): Party name.
secret (Any): Secret.
secret_array (np.ndarray): Secret array.
name (str): Secrets name.
nada_type (Any): Nada type.
Returns:
str: Store ID.
"""
if isinstance(secret, np.ndarray):
secret = na_client.array(secret, name, nada_type)
else:
secret = {name: nada_type(secret)}
stored_secret = nillion.Secrets(secret)
secret_bindings = nillion.ProgramBindings(program_id)
secret_bindings.add_input_party(party_name, party_id)
secret = na_client.array(secret_array, name, nada_type)
secrets = nillion.Secrets(secret)
store_id = await store_secrets(
client,
cluster_id,
program_id,
party_id,
party_name,
secrets,
)
return store_id


store_id = await client.store_secrets(
cluster_id, secret_bindings, stored_secret, None
async def store_secret_value(
client: nillion.NillionClient,
cluster_id: str,
program_id: str,
party_id: str,
party_name: str,
secret_value: Any,
name: str,
nada_type: Any,
):
"""
Asynchronous function to store secret values on the nillion client.
Args:
client (nillion.NillionClient): Nillion client.
cluster_id (str): Cluster ID.
program_id (str): Program ID.
party_id (str): Party ID.
party_name (str): Party name.
secret_value (Any): Secret single value.
name (str): Secrets name.
nada_type (Any): Nada type.
Returns:
str: Store ID.
"""
if nada_type == na.Rational:
secret_value = round(secret_value * 2 ** na.get_log_scale())
nada_type = nillion.PublicVariableInteger
elif nada_type == na.SecretRational:
secret_value = round(secret_value * 2 ** na.get_log_scale())
nada_type = nillion.SecretInteger

secrets = nillion.Secrets({name: nada_type(secret_value)})
store_id = await store_secrets(
client,
cluster_id,
program_id,
party_id,
party_name,
secrets,
)
return store_id


async def store_secrets(
client: nillion.NillionClient,
cluster_id: str,
program_id: str,
party_id: str,
party_name: str,
secrets: nillion.Secrets,
):
"""
Asynchronous function to store secret values on the nillion client.
Args:
client (nillion.NillionClient): Nillion client.
cluster_id (str): Cluster ID.
program_id (str): Program ID.
party_id (str): Party ID.
party_name (str): Party name.
secrets (nillion.Secrets): Secrets.
Returns:
str: Store ID.
"""
secret_bindings = nillion.ProgramBindings(program_id)
secret_bindings.add_input_party(party_name, party_id)
store_id = await client.store_secrets(cluster_id, secret_bindings, secrets, None)
return store_id


async def compute(
client: nillion.NillionClient,
cluster_id: str,
Expand Down
19 changes: 11 additions & 8 deletions examples/dot_product/main.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
"""Dot product Nada example"""

import asyncio
import os
import sys

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))

import asyncio
from typing import Dict

import numpy as np
import py_nillion_client as nillion
from dotenv import load_dotenv
# Import helper functions for creating nillion client and getting keys
from nillion_python_helpers import (create_nillion_client, getNodeKeyFromFile,
getUserKeyFromFile)

import nada_numpy.client as na_client
# Import helper functions for creating nillion client and getting keys
from examples.common.nillion_client_helper import create_nillion_client
from examples.common.nillion_keypath_helper import (getNodeKeyFromFile,
getUserKeyFromFile)
from examples.common.utils import compute, store_program, store_secrets
from examples.common.utils import compute, store_program, store_secret_array
from examples.dot_product.config import DIM

# Load environment variables from a .env file
Expand Down Expand Up @@ -43,7 +46,7 @@ async def main() -> Dict:

# Create and store secrets for two parties
A = np.ones([DIM])
A_store_id = await store_secrets(
A_store_id = await store_secret_array(
client,
cluster_id,
program_id,
Expand All @@ -55,7 +58,7 @@ async def main() -> Dict:
)

B = np.ones([DIM])
B_store_id = await store_secrets(
B_store_id = await store_secret_array(
client,
cluster_id,
program_id,
Expand Down
19 changes: 11 additions & 8 deletions examples/matrix_multiplication/main.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
"""Matrix multiplication example"""

import asyncio
import os
import sys

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))

import asyncio
from typing import Dict

import numpy as np
import py_nillion_client as nillion
from dotenv import load_dotenv
# Import helper functions for creating nillion client and getting keys
from nillion_python_helpers import (create_nillion_client, getNodeKeyFromFile,
getUserKeyFromFile)

import nada_numpy.client as na_client
# Import helper functions for creating nillion client and getting keys
from examples.common.nillion_client_helper import create_nillion_client
from examples.common.nillion_keypath_helper import (getNodeKeyFromFile,
getUserKeyFromFile)
from examples.common.utils import compute, store_program, store_secrets
from examples.common.utils import compute, store_program, store_secret_array
from examples.matrix_multiplication.config import DIM

# Load environment variables from a .env file
Expand Down Expand Up @@ -43,7 +46,7 @@ async def main() -> Dict:

# Create and store secrets for two parties
A = np.ones(DIM)
A_store_id = await store_secrets(
A_store_id = await store_secret_array(
client,
cluster_id,
program_id,
Expand All @@ -55,7 +58,7 @@ async def main() -> Dict:
)

B = np.ones(DIM)
B_store_id = await store_secrets(
B_store_id = await store_secret_array(
client,
cluster_id,
program_id,
Expand Down
Loading

0 comments on commit e45cebf

Please sign in to comment.