Skip to content

Commit b1dcd37

Browse files
Feature/cleartext typing (#39)
Add cleartext typing
1 parent 3819b81 commit b1dcd37

File tree

11 files changed

+105
-98
lines changed

11 files changed

+105
-98
lines changed

examples/broadcasting/main.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@
88
from dotenv import load_dotenv
99

1010
import nada_numpy.client as na_client
11-
1211
# Import helper functions for creating nillion client and getting keys
1312
from examples.broadcasting.config import DIM
1413
from examples.common.nillion_client_helper import create_nillion_client
15-
from examples.common.nillion_keypath_helper import (
16-
getNodeKeyFromFile,
17-
getUserKeyFromFile,
18-
)
14+
from examples.common.nillion_keypath_helper import (getNodeKeyFromFile,
15+
getUserKeyFromFile)
1916
from examples.common.utils import compute, store_program, store_secrets
2017

2118
# Load environment variables from a .env file

examples/dot_product/main.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99
from dotenv import load_dotenv
1010

1111
import nada_numpy.client as na_client
12-
1312
# Import helper functions for creating nillion client and getting keys
1413
from examples.common.nillion_client_helper import create_nillion_client
15-
from examples.common.nillion_keypath_helper import (
16-
getNodeKeyFromFile,
17-
getUserKeyFromFile,
18-
)
14+
from examples.common.nillion_keypath_helper import (getNodeKeyFromFile,
15+
getUserKeyFromFile)
1916
from examples.common.utils import compute, store_program, store_secrets
2017
from examples.dot_product.config import DIM
2118

examples/matrix_multiplication/main.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99
from dotenv import load_dotenv
1010

1111
import nada_numpy.client as na_client
12-
1312
# Import helper functions for creating nillion client and getting keys
1413
from examples.common.nillion_client_helper import create_nillion_client
15-
from examples.common.nillion_keypath_helper import (
16-
getNodeKeyFromFile,
17-
getUserKeyFromFile,
18-
)
14+
from examples.common.nillion_keypath_helper import (getNodeKeyFromFile,
15+
getUserKeyFromFile)
1916
from examples.common.utils import compute, store_program, store_secrets
2017
from examples.matrix_multiplication.config import DIM
2118

examples/rational_numbers/main.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@
77
from dotenv import load_dotenv
88

99
import nada_numpy.client as na_client
10-
1110
# Import helper functions for creating nillion client and getting keys
1211
from examples.common.nillion_client_helper import create_nillion_client
13-
from examples.common.nillion_keypath_helper import (
14-
getNodeKeyFromFile,
15-
getUserKeyFromFile,
16-
)
12+
from examples.common.nillion_keypath_helper import (getNodeKeyFromFile,
13+
getUserKeyFromFile)
1714
from examples.common.utils import compute, store_program, store_secrets
1815

1916
# Load environment variables from a .env file

nada_numpy/__init__.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,7 @@
22

33
from nada_numpy.array import NadaArray
44
from nada_numpy.funcs import * # pylint:disable=redefined-builtin
5-
from nada_numpy.types import (
6-
PublicBoolean,
7-
Rational,
8-
SecretBoolean,
9-
SecretRational,
10-
get_log_scale,
11-
public_rational,
12-
rational,
13-
reset_log_scale,
14-
secret_rational,
15-
set_log_scale,
16-
)
5+
from nada_numpy.types import (PublicBoolean, Rational, SecretBoolean,
6+
SecretRational, get_log_scale, public_rational,
7+
rational, reset_log_scale, secret_rational,
8+
set_log_scale)

nada_numpy/array.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,16 @@
88
from typing import Any, Callable, Optional, Sequence, Union, get_args, overload
99

1010
import numpy as np
11-
from nada_dsl import (
12-
Input,
13-
Integer,
14-
Output,
15-
Party,
16-
PublicInteger,
17-
PublicUnsignedInteger,
18-
SecretInteger,
19-
SecretUnsignedInteger,
20-
UnsignedInteger,
21-
)
11+
from nada_dsl import (Boolean, Input, Integer, Output, Party, PublicInteger,
12+
PublicUnsignedInteger, SecretInteger,
13+
SecretUnsignedInteger, UnsignedInteger)
2214

2315
from nada_numpy.context import UnsafeArithmeticSession
24-
from nada_numpy.nada_typing import (
25-
NadaBoolean,
26-
NadaInteger,
27-
NadaRational,
28-
NadaUnsignedInteger,
29-
)
30-
from nada_numpy.types import (
31-
Rational,
32-
SecretRational,
33-
get_log_scale,
34-
public_rational,
35-
rational,
36-
secret_rational,
37-
)
16+
from nada_numpy.nada_typing import (NadaBoolean, NadaCleartextType,
17+
NadaInteger, NadaRational,
18+
NadaUnsignedInteger)
19+
from nada_numpy.types import (Rational, SecretRational, get_log_scale,
20+
public_rational, rational, secret_rational)
3821
from nada_numpy.utils import copy_metadata
3922

4023

@@ -754,6 +737,24 @@ def is_boolean(self) -> bool:
754737
"""
755738
return self.dtype == NadaBoolean
756739

740+
@property
741+
def cleartext_nada_type(self) -> NadaCleartextType:
742+
"""
743+
Returns a clear-text Nada type compatible with the Nada array.
744+
745+
Returns:
746+
NadaCleartextType: Compatible cleartext type.
747+
"""
748+
if self.is_rational:
749+
return Rational
750+
if self.is_integer:
751+
return Integer
752+
if self.is_unsigned_integer:
753+
return UnsignedInteger
754+
if self.is_boolean:
755+
return Boolean
756+
raise TypeError(f"Array {self} is of unknown type {self.dtype}.")
757+
757758
def __str__(self) -> str:
758759
"""
759760
String representation of the NadaArray.

nada_numpy/client.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@
66
from typing import Dict, List, Optional, Union
77

88
import numpy as np
9-
109
# pylint:disable=no-name-in-module
11-
from py_nillion_client import (
12-
PublicVariableInteger,
13-
PublicVariableUnsignedInteger,
14-
SecretInteger,
15-
SecretUnsignedInteger,
16-
)
10+
from py_nillion_client import (PublicVariableInteger,
11+
PublicVariableUnsignedInteger, SecretInteger,
12+
SecretUnsignedInteger)
1713

1814
from nada_numpy.types import Rational, SecretRational, get_log_scale
1915

nada_numpy/funcs.py

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,58 @@
66
from typing import Any, Callable, List, Sequence, Tuple, Union
77

88
import numpy as np
9-
from nada_dsl import (
10-
Boolean,
11-
Integer,
12-
Party,
13-
PublicInteger,
14-
PublicUnsignedInteger,
15-
SecretInteger,
16-
SecretUnsignedInteger,
17-
UnsignedInteger,
18-
)
9+
from nada_dsl import (Boolean, Integer, Party, PublicInteger,
10+
PublicUnsignedInteger, SecretInteger,
11+
SecretUnsignedInteger, UnsignedInteger)
1912

2013
from nada_numpy.array import NadaArray
14+
from nada_numpy.nada_typing import NadaCleartextNumber
2115
from nada_numpy.types import Rational, SecretRational, rational
2216
from nada_numpy.utils import copy_metadata
2317

24-
_NadaCleartextType = Union[Integer, UnsignedInteger, Rational]
18+
__all__ = [
19+
"parties",
20+
"from_list",
21+
"ones",
22+
"ones_like",
23+
"zeros",
24+
"zeros_like",
25+
"alphas",
26+
"alphas_like",
27+
"array",
28+
"random",
29+
"output",
30+
"vstack",
31+
"hstack",
32+
"ndim",
33+
"shape",
34+
"size",
35+
"pad",
36+
"frompyfunc",
37+
"vectorize",
38+
"eye",
39+
"arange",
40+
"linspace",
41+
"split",
42+
"compress",
43+
"copy",
44+
"cumprod",
45+
"cumsum",
46+
"diagonal",
47+
"mean",
48+
"prod",
49+
"put",
50+
"ravel",
51+
"repeat",
52+
"reshape",
53+
"resize",
54+
"squeeze",
55+
"sum",
56+
"swapaxes",
57+
"take",
58+
"trace",
59+
"transpose",
60+
]
2561

2662

2763
def parties(num: int, prefix: str = "Party") -> list:
@@ -38,7 +74,7 @@ def parties(num: int, prefix: str = "Party") -> list:
3874
return [Party(name=f"{prefix}{i}") for i in range(num)]
3975

4076

41-
def __from_numpy(arr: np.ndarray, nada_type: _NadaCleartextType) -> list:
77+
def __from_numpy(arr: np.ndarray, nada_type: NadaCleartextNumber) -> list:
4278
"""
4379
Recursively convert a n-dimensional NumPy array to a nested list of NadaInteger objects.
4480
@@ -57,7 +93,7 @@ def __from_numpy(arr: np.ndarray, nada_type: _NadaCleartextType) -> list:
5793

5894

5995
def from_list(
60-
lst: Union[List, np.ndarray], nada_type: _NadaCleartextType = Integer
96+
lst: Union[List, np.ndarray], nada_type: NadaCleartextNumber = Integer
6197
) -> NadaArray:
6298
"""
6399
Create a cleartext NadaArray from a list of integers.
@@ -75,7 +111,7 @@ def from_list(
75111
return NadaArray(np.array(__from_numpy(lst_np, nada_type)))
76112

77113

78-
def ones(dims: Sequence[int], nada_type: _NadaCleartextType = Integer) -> NadaArray:
114+
def ones(dims: Sequence[int], nada_type: NadaCleartextNumber = Integer) -> NadaArray:
79115
"""
80116
Create a cleartext NadaArray filled with ones.
81117
@@ -92,7 +128,7 @@ def ones(dims: Sequence[int], nada_type: _NadaCleartextType = Integer) -> NadaAr
92128

93129

94130
def ones_like(
95-
a: np.ndarray | NadaArray, nada_type: _NadaCleartextType = Integer
131+
a: np.ndarray | NadaArray, nada_type: NadaCleartextNumber = Integer
96132
) -> NadaArray:
97133
"""
98134
Create a cleartext NadaArray filled with one with the same shape and type as a given array.
@@ -111,7 +147,7 @@ def ones_like(
111147
return from_list(np.ones_like(a), nada_type)
112148

113149

114-
def zeros(dims: Sequence[int], nada_type: _NadaCleartextType = Integer) -> NadaArray:
150+
def zeros(dims: Sequence[int], nada_type: NadaCleartextNumber = Integer) -> NadaArray:
115151
"""
116152
Create a cleartext NadaArray filled with zeros.
117153
@@ -128,7 +164,7 @@ def zeros(dims: Sequence[int], nada_type: _NadaCleartextType = Integer) -> NadaA
128164

129165

130166
def zeros_like(
131-
a: np.ndarray | NadaArray, nada_type: _NadaCleartextType = Integer
167+
a: np.ndarray | NadaArray, nada_type: NadaCleartextNumber = Integer
132168
) -> NadaArray:
133169
"""
134170
Create a cleartext NadaArray filled with zeros with the same shape and type as a given array.
@@ -306,14 +342,14 @@ def size(arr: NadaArray) -> int:
306342
return arr.size
307343

308344

309-
def to_nada(arr: np.ndarray, nada_type: _NadaCleartextType) -> NadaArray:
345+
def to_nada(arr: np.ndarray, nada_type: NadaCleartextNumber) -> NadaArray:
310346
"""
311347
Converts a plain-text NumPy array to the equivalent NadaArray with
312348
a specified compatible NadaType.
313349
314350
Args:
315351
arr (np.ndarray): Input Numpy array.
316-
nada_type (_NadaCleartextType): Desired clear-text NadaType.
352+
nada_type (NadaCleartextNumber): Desired clear-text NadaType.
317353
318354
Returns:
319355
NadaArray: Output NadaArray.
@@ -409,19 +445,19 @@ def vectorize(*args, **kwargs) -> NadaCallable:
409445

410446
# pylint:disable=missing-function-docstring
411447
@copy_metadata(np.eye)
412-
def eye(*args, nada_type: _NadaCleartextType, **kwargs) -> NadaArray:
448+
def eye(*args, nada_type: NadaCleartextNumber, **kwargs) -> NadaArray:
413449
return to_nada(np.eye(*args, **kwargs), nada_type)
414450

415451

416452
# pylint:disable=missing-function-docstring
417453
@copy_metadata(np.arange)
418-
def arange(*args, nada_type: _NadaCleartextType, **kwargs) -> NadaArray:
454+
def arange(*args, nada_type: NadaCleartextNumber, **kwargs) -> NadaArray:
419455
return to_nada(np.arange(*args, **kwargs), nada_type)
420456

421457

422458
# pylint:disable=missing-function-docstring
423459
@copy_metadata(np.linspace)
424-
def linspace(*args, nada_type: _NadaCleartextType, **kwargs) -> NadaArray:
460+
def linspace(*args, nada_type: NadaCleartextNumber, **kwargs) -> NadaArray:
425461
return to_nada(np.linspace(*args, **kwargs), nada_type)
426462

427463

nada_numpy/nada_typing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import nada_dsl as dsl
66

7-
from nada_numpy.types import PublicBoolean, Rational, SecretBoolean, SecretRational
7+
from nada_numpy.types import (PublicBoolean, Rational, SecretBoolean,
8+
SecretRational)
89

910
NadaRational = Union[
1011
Rational,

nada_numpy/types.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,9 @@
77

88
import nada_dsl as dsl
99
import numpy as np
10-
from nada_dsl import (
11-
Input,
12-
Integer,
13-
Party,
14-
PublicInteger,
15-
PublicUnsignedInteger,
16-
SecretInteger,
17-
SecretUnsignedInteger,
18-
UnsignedInteger,
19-
)
10+
from nada_dsl import (Input, Integer, Party, PublicInteger,
11+
PublicUnsignedInteger, SecretInteger,
12+
SecretUnsignedInteger, UnsignedInteger)
2013

2114
_NadaRational = Union["Rational", "SecretRational"]
2215

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "nada-numpy"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
description = "Nada-Numpy is a Python library designed for algebraic operations on NumPy-like array objects on top of Nada DSL and Nillion Network."
55
authors = ["José Cabrero-Holgueras <jose.cabrero@nillion.com>"]
66
readme = "README.md"

0 commit comments

Comments
 (0)