Skip to content

Commit e45cebf

Browse files
Bugfix/examples (#43)
Update examples
1 parent cdc7778 commit e45cebf

File tree

10 files changed

+141
-111
lines changed

10 files changed

+141
-111
lines changed

examples/broadcasting/main.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
"""Broadcasting example script"""
22

3-
import asyncio
43
import os
4+
import sys
5+
6+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
7+
8+
import asyncio
59

610
import numpy as np
711
import py_nillion_client as nillion
812
from dotenv import load_dotenv
13+
from nillion_python_helpers import (create_nillion_client, getNodeKeyFromFile,
14+
getUserKeyFromFile)
915

1016
import nada_numpy.client as na_client
1117
# Import helper functions for creating nillion client and getting keys
1218
from examples.broadcasting.config import DIM
13-
from examples.common.nillion_client_helper import create_nillion_client
14-
from examples.common.nillion_keypath_helper import (getNodeKeyFromFile,
15-
getUserKeyFromFile)
16-
from examples.common.utils import compute, store_program, store_secrets
19+
from examples.common.utils import compute, store_program, store_secret_array
1720

1821
# Load environment variables from a .env file
1922
load_dotenv()
@@ -43,7 +46,7 @@ async def main() -> None:
4346
# Create and store secrets for two parties
4447
A = np.ones([DIM])
4548
C = np.ones([DIM])
46-
A_store_id = await store_secrets(
49+
A_store_id = await store_secret_array(
4750
client,
4851
cluster_id,
4952
program_id,
@@ -53,7 +56,7 @@ async def main() -> None:
5356
"A",
5457
nillion.SecretInteger,
5558
)
56-
C_store_id = await store_secrets(
59+
C_store_id = await store_secret_array(
5760
client,
5861
cluster_id,
5962
program_id,
@@ -66,7 +69,7 @@ async def main() -> None:
6669

6770
B = np.ones([DIM])
6871
D = np.ones([DIM])
69-
B_store_id = await store_secrets(
72+
B_store_id = await store_secret_array(
7073
client,
7174
cluster_id,
7275
program_id,
@@ -76,7 +79,7 @@ async def main() -> None:
7679
"B",
7780
nillion.SecretInteger,
7881
)
79-
D_store_id = await store_secrets(
82+
D_store_id = await store_secret_array(
8083
client,
8184
cluster_id,
8285
program_id,

examples/common/nillion_client_helper.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

examples/common/nillion_keypath_helper.py

Lines changed: 0 additions & 29 deletions
This file was deleted.

examples/common/utils.py

Lines changed: 87 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import os
44
import time
5-
from typing import Any, Callable, Dict, List, Type
5+
from typing import Any, Callable, Dict, List
66

77
import numpy as np
88
import py_nillion_client as nillion
99

10+
import nada_numpy as na
1011
import nada_numpy.client as na_client
1112

1213

@@ -82,46 +83,118 @@ async def store_program(
8283
return program_id
8384

8485

85-
async def store_secrets(
86+
async def store_secret_array(
8687
client: nillion.NillionClient,
8788
cluster_id: str,
8889
program_id: str,
8990
party_id: str,
9091
party_name: str,
91-
secret: Any,
92+
secret_array: np.ndarray,
9293
name: str,
9394
nada_type: Any,
9495
):
9596
"""
96-
Asynchronous function to store secrets on the nillion client.
97+
Asynchronous function to store secret arrays on the nillion client.
9798
9899
Args:
99100
client (nillion.NillionClient): Nillion client.
100101
cluster_id (str): Cluster ID.
101102
program_id (str): Program ID.
102103
party_id (str): Party ID.
103104
party_name (str): Party name.
104-
secret (Any): Secret.
105+
secret_array (np.ndarray): Secret array.
105106
name (str): Secrets name.
106107
nada_type (Any): Nada type.
107108
108109
Returns:
109110
str: Store ID.
110111
"""
111-
if isinstance(secret, np.ndarray):
112-
secret = na_client.array(secret, name, nada_type)
113-
else:
114-
secret = {name: nada_type(secret)}
115-
stored_secret = nillion.Secrets(secret)
116-
secret_bindings = nillion.ProgramBindings(program_id)
117-
secret_bindings.add_input_party(party_name, party_id)
112+
secret = na_client.array(secret_array, name, nada_type)
113+
secrets = nillion.Secrets(secret)
114+
store_id = await store_secrets(
115+
client,
116+
cluster_id,
117+
program_id,
118+
party_id,
119+
party_name,
120+
secrets,
121+
)
122+
return store_id
123+
118124

119-
store_id = await client.store_secrets(
120-
cluster_id, secret_bindings, stored_secret, None
125+
async def store_secret_value(
126+
client: nillion.NillionClient,
127+
cluster_id: str,
128+
program_id: str,
129+
party_id: str,
130+
party_name: str,
131+
secret_value: Any,
132+
name: str,
133+
nada_type: Any,
134+
):
135+
"""
136+
Asynchronous function to store secret values on the nillion client.
137+
138+
Args:
139+
client (nillion.NillionClient): Nillion client.
140+
cluster_id (str): Cluster ID.
141+
program_id (str): Program ID.
142+
party_id (str): Party ID.
143+
party_name (str): Party name.
144+
secret_value (Any): Secret single value.
145+
name (str): Secrets name.
146+
nada_type (Any): Nada type.
147+
148+
Returns:
149+
str: Store ID.
150+
"""
151+
if nada_type == na.Rational:
152+
secret_value = round(secret_value * 2 ** na.get_log_scale())
153+
nada_type = nillion.PublicVariableInteger
154+
elif nada_type == na.SecretRational:
155+
secret_value = round(secret_value * 2 ** na.get_log_scale())
156+
nada_type = nillion.SecretInteger
157+
158+
secrets = nillion.Secrets({name: nada_type(secret_value)})
159+
store_id = await store_secrets(
160+
client,
161+
cluster_id,
162+
program_id,
163+
party_id,
164+
party_name,
165+
secrets,
121166
)
122167
return store_id
123168

124169

170+
async def store_secrets(
171+
client: nillion.NillionClient,
172+
cluster_id: str,
173+
program_id: str,
174+
party_id: str,
175+
party_name: str,
176+
secrets: nillion.Secrets,
177+
):
178+
"""
179+
Asynchronous function to store secret values on the nillion client.
180+
181+
Args:
182+
client (nillion.NillionClient): Nillion client.
183+
cluster_id (str): Cluster ID.
184+
program_id (str): Program ID.
185+
party_id (str): Party ID.
186+
party_name (str): Party name.
187+
secrets (nillion.Secrets): Secrets.
188+
189+
Returns:
190+
str: Store ID.
191+
"""
192+
secret_bindings = nillion.ProgramBindings(program_id)
193+
secret_bindings.add_input_party(party_name, party_id)
194+
store_id = await client.store_secrets(cluster_id, secret_bindings, secrets, None)
195+
return store_id
196+
197+
125198
async def compute(
126199
client: nillion.NillionClient,
127200
cluster_id: str,

examples/dot_product/main.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
"""Dot product Nada example"""
22

3-
import asyncio
43
import os
4+
import sys
5+
6+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
7+
8+
import asyncio
59
from typing import Dict
610

711
import numpy as np
812
import py_nillion_client as nillion
913
from dotenv import load_dotenv
14+
# Import helper functions for creating nillion client and getting keys
15+
from nillion_python_helpers import (create_nillion_client, getNodeKeyFromFile,
16+
getUserKeyFromFile)
1017

1118
import nada_numpy.client as na_client
12-
# Import helper functions for creating nillion client and getting keys
13-
from examples.common.nillion_client_helper import create_nillion_client
14-
from examples.common.nillion_keypath_helper import (getNodeKeyFromFile,
15-
getUserKeyFromFile)
16-
from examples.common.utils import compute, store_program, store_secrets
19+
from examples.common.utils import compute, store_program, store_secret_array
1720
from examples.dot_product.config import DIM
1821

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

4447
# Create and store secrets for two parties
4548
A = np.ones([DIM])
46-
A_store_id = await store_secrets(
49+
A_store_id = await store_secret_array(
4750
client,
4851
cluster_id,
4952
program_id,
@@ -55,7 +58,7 @@ async def main() -> Dict:
5558
)
5659

5760
B = np.ones([DIM])
58-
B_store_id = await store_secrets(
61+
B_store_id = await store_secret_array(
5962
client,
6063
cluster_id,
6164
program_id,

examples/matrix_multiplication/main.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
"""Matrix multiplication example"""
22

3-
import asyncio
43
import os
4+
import sys
5+
6+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
7+
8+
import asyncio
59
from typing import Dict
610

711
import numpy as np
812
import py_nillion_client as nillion
913
from dotenv import load_dotenv
14+
# Import helper functions for creating nillion client and getting keys
15+
from nillion_python_helpers import (create_nillion_client, getNodeKeyFromFile,
16+
getUserKeyFromFile)
1017

1118
import nada_numpy.client as na_client
12-
# Import helper functions for creating nillion client and getting keys
13-
from examples.common.nillion_client_helper import create_nillion_client
14-
from examples.common.nillion_keypath_helper import (getNodeKeyFromFile,
15-
getUserKeyFromFile)
16-
from examples.common.utils import compute, store_program, store_secrets
19+
from examples.common.utils import compute, store_program, store_secret_array
1720
from examples.matrix_multiplication.config import DIM
1821

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

4447
# Create and store secrets for two parties
4548
A = np.ones(DIM)
46-
A_store_id = await store_secrets(
49+
A_store_id = await store_secret_array(
4750
client,
4851
cluster_id,
4952
program_id,
@@ -55,7 +58,7 @@ async def main() -> Dict:
5558
)
5659

5760
B = np.ones(DIM)
58-
B_store_id = await store_secrets(
61+
B_store_id = await store_secret_array(
5962
client,
6063
cluster_id,
6164
program_id,

0 commit comments

Comments
 (0)