Skip to content

Commit

Permalink
Feature/cleartext typing (#39)
Browse files Browse the repository at this point in the history
Add cleartext typing
  • Loading branch information
mathias-nillion authored Jun 20, 2024
1 parent 3819b81 commit b1dcd37
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 98 deletions.
7 changes: 2 additions & 5 deletions examples/broadcasting/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
from dotenv import load_dotenv

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.nillion_keypath_helper import (getNodeKeyFromFile,
getUserKeyFromFile)
from examples.common.utils import compute, store_program, store_secrets

# Load environment variables from a .env file
Expand Down
7 changes: 2 additions & 5 deletions examples/dot_product/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
from dotenv import load_dotenv

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.nillion_keypath_helper import (getNodeKeyFromFile,
getUserKeyFromFile)
from examples.common.utils import compute, store_program, store_secrets
from examples.dot_product.config import DIM

Expand Down
7 changes: 2 additions & 5 deletions examples/matrix_multiplication/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
from dotenv import load_dotenv

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.nillion_keypath_helper import (getNodeKeyFromFile,
getUserKeyFromFile)
from examples.common.utils import compute, store_program, store_secrets
from examples.matrix_multiplication.config import DIM

Expand Down
7 changes: 2 additions & 5 deletions examples/rational_numbers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
from dotenv import load_dotenv

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.nillion_keypath_helper import (getNodeKeyFromFile,
getUserKeyFromFile)
from examples.common.utils import compute, store_program, store_secrets

# Load environment variables from a .env file
Expand Down
16 changes: 4 additions & 12 deletions nada_numpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@

from nada_numpy.array import NadaArray
from nada_numpy.funcs import * # pylint:disable=redefined-builtin
from nada_numpy.types import (
PublicBoolean,
Rational,
SecretBoolean,
SecretRational,
get_log_scale,
public_rational,
rational,
reset_log_scale,
secret_rational,
set_log_scale,
)
from nada_numpy.types import (PublicBoolean, Rational, SecretBoolean,
SecretRational, get_log_scale, public_rational,
rational, reset_log_scale, secret_rational,
set_log_scale)
51 changes: 26 additions & 25 deletions nada_numpy/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,16 @@
from typing import Any, Callable, Optional, Sequence, Union, get_args, overload

import numpy as np
from nada_dsl import (
Input,
Integer,
Output,
Party,
PublicInteger,
PublicUnsignedInteger,
SecretInteger,
SecretUnsignedInteger,
UnsignedInteger,
)
from nada_dsl import (Boolean, Input, Integer, Output, Party, PublicInteger,
PublicUnsignedInteger, SecretInteger,
SecretUnsignedInteger, UnsignedInteger)

from nada_numpy.context import UnsafeArithmeticSession
from nada_numpy.nada_typing import (
NadaBoolean,
NadaInteger,
NadaRational,
NadaUnsignedInteger,
)
from nada_numpy.types import (
Rational,
SecretRational,
get_log_scale,
public_rational,
rational,
secret_rational,
)
from nada_numpy.nada_typing import (NadaBoolean, NadaCleartextType,
NadaInteger, NadaRational,
NadaUnsignedInteger)
from nada_numpy.types import (Rational, SecretRational, get_log_scale,
public_rational, rational, secret_rational)
from nada_numpy.utils import copy_metadata


Expand Down Expand Up @@ -754,6 +737,24 @@ def is_boolean(self) -> bool:
"""
return self.dtype == NadaBoolean

@property
def cleartext_nada_type(self) -> NadaCleartextType:
"""
Returns a clear-text Nada type compatible with the Nada array.
Returns:
NadaCleartextType: Compatible cleartext type.
"""
if self.is_rational:
return Rational
if self.is_integer:
return Integer
if self.is_unsigned_integer:
return UnsignedInteger
if self.is_boolean:
return Boolean
raise TypeError(f"Array {self} is of unknown type {self.dtype}.")

def __str__(self) -> str:
"""
String representation of the NadaArray.
Expand Down
10 changes: 3 additions & 7 deletions nada_numpy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@
from typing import Dict, List, Optional, Union

import numpy as np

# pylint:disable=no-name-in-module
from py_nillion_client import (
PublicVariableInteger,
PublicVariableUnsignedInteger,
SecretInteger,
SecretUnsignedInteger,
)
from py_nillion_client import (PublicVariableInteger,
PublicVariableUnsignedInteger, SecretInteger,
SecretUnsignedInteger)

from nada_numpy.types import Rational, SecretRational, get_log_scale

Expand Down
80 changes: 58 additions & 22 deletions nada_numpy/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,58 @@
from typing import Any, Callable, List, Sequence, Tuple, Union

import numpy as np
from nada_dsl import (
Boolean,
Integer,
Party,
PublicInteger,
PublicUnsignedInteger,
SecretInteger,
SecretUnsignedInteger,
UnsignedInteger,
)
from nada_dsl import (Boolean, Integer, Party, PublicInteger,
PublicUnsignedInteger, SecretInteger,
SecretUnsignedInteger, UnsignedInteger)

from nada_numpy.array import NadaArray
from nada_numpy.nada_typing import NadaCleartextNumber
from nada_numpy.types import Rational, SecretRational, rational
from nada_numpy.utils import copy_metadata

_NadaCleartextType = Union[Integer, UnsignedInteger, Rational]
__all__ = [
"parties",
"from_list",
"ones",
"ones_like",
"zeros",
"zeros_like",
"alphas",
"alphas_like",
"array",
"random",
"output",
"vstack",
"hstack",
"ndim",
"shape",
"size",
"pad",
"frompyfunc",
"vectorize",
"eye",
"arange",
"linspace",
"split",
"compress",
"copy",
"cumprod",
"cumsum",
"diagonal",
"mean",
"prod",
"put",
"ravel",
"repeat",
"reshape",
"resize",
"squeeze",
"sum",
"swapaxes",
"take",
"trace",
"transpose",
]


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


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


def from_list(
lst: Union[List, np.ndarray], nada_type: _NadaCleartextType = Integer
lst: Union[List, np.ndarray], nada_type: NadaCleartextNumber = Integer
) -> NadaArray:
"""
Create a cleartext NadaArray from a list of integers.
Expand All @@ -75,7 +111,7 @@ def from_list(
return NadaArray(np.array(__from_numpy(lst_np, nada_type)))


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


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


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


def zeros_like(
a: np.ndarray | NadaArray, nada_type: _NadaCleartextType = Integer
a: np.ndarray | NadaArray, nada_type: NadaCleartextNumber = Integer
) -> NadaArray:
"""
Create a cleartext NadaArray filled with zeros with the same shape and type as a given array.
Expand Down Expand Up @@ -306,14 +342,14 @@ def size(arr: NadaArray) -> int:
return arr.size


def to_nada(arr: np.ndarray, nada_type: _NadaCleartextType) -> NadaArray:
def to_nada(arr: np.ndarray, nada_type: NadaCleartextNumber) -> NadaArray:
"""
Converts a plain-text NumPy array to the equivalent NadaArray with
a specified compatible NadaType.
Args:
arr (np.ndarray): Input Numpy array.
nada_type (_NadaCleartextType): Desired clear-text NadaType.
nada_type (NadaCleartextNumber): Desired clear-text NadaType.
Returns:
NadaArray: Output NadaArray.
Expand Down Expand Up @@ -409,19 +445,19 @@ def vectorize(*args, **kwargs) -> NadaCallable:

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


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


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


Expand Down
3 changes: 2 additions & 1 deletion nada_numpy/nada_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import nada_dsl as dsl

from nada_numpy.types import PublicBoolean, Rational, SecretBoolean, SecretRational
from nada_numpy.types import (PublicBoolean, Rational, SecretBoolean,
SecretRational)

NadaRational = Union[
Rational,
Expand Down
13 changes: 3 additions & 10 deletions nada_numpy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,9 @@

import nada_dsl as dsl
import numpy as np
from nada_dsl import (
Input,
Integer,
Party,
PublicInteger,
PublicUnsignedInteger,
SecretInteger,
SecretUnsignedInteger,
UnsignedInteger,
)
from nada_dsl import (Input, Integer, Party, PublicInteger,
PublicUnsignedInteger, SecretInteger,
SecretUnsignedInteger, UnsignedInteger)

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

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nada-numpy"
version = "0.1.0"
version = "0.1.1"
description = "Nada-Numpy is a Python library designed for algebraic operations on NumPy-like array objects on top of Nada DSL and Nillion Network."
authors = ["José Cabrero-Holgueras <jose.cabrero@nillion.com>"]
readme = "README.md"
Expand Down

0 comments on commit b1dcd37

Please sign in to comment.