Skip to content

Commit

Permalink
feat: Add bitwise and, or, xor operations to Nada
Browse files Browse the repository at this point in the history
  • Loading branch information
navasvarela committed Sep 5, 2024
1 parent affbf5d commit 3fedfd8
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 1 deletion.
134 changes: 133 additions & 1 deletion nada_dsl/nada_types/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from . import NadaType
from dataclasses import dataclass
from nada_dsl.circuit_io import Literal
from nada_dsl.operations import Addition, Division, Equals, GreaterOrEqualThan, GreaterThan, IfElse, LeftShift, LessOrEqualThan, LessThan, Modulo, Multiplication, Not, NotEquals, Power, PublicOutputEquality, Random, Reveal, RightShift, Subtraction, TruncPr
from nada_dsl.operations import Addition, BooleanAnd, BooleanOr, BooleanXor, Division, Equals, GreaterOrEqualThan, GreaterThan, IfElse, LeftShift, LessOrEqualThan, LessThan, Modulo, Multiplication, Not, NotEquals, Power, PublicOutputEquality, Random, Reveal, RightShift, Subtraction, TruncPr
from nada_dsl.source_ref import SourceRef
from typing import Union

Expand Down Expand Up @@ -439,6 +439,48 @@ def __ne__(
else:
raise TypeError(f"Invalid operation: {self} != {other}")

def __and__(
self, other: Union["Boolean", "PublicBoolean", "SecretBoolean"]
) -> Union["Boolean", "PublicBoolean", "SecretBoolean"]:
if isinstance(other, Boolean):
return Boolean(value=bool(self.value & other.value))
elif isinstance(other, PublicBoolean):
operation = BooleanAnd(left=self, right=other, source_ref=SourceRef.back_frame())
return PublicBoolean(inner=operation)
elif isinstance(other, SecretBoolean):
operation = BooleanAnd(left=self, right=other, source_ref=SourceRef.back_frame())
return SecretBoolean(inner=operation)
else:
raise TypeError(f"Invalid operation: {self} & {other}")

def __or__(
self, other: Union["Boolean", "PublicBoolean", "SecretBoolean"]
) -> Union["Boolean", "PublicBoolean", "SecretBoolean"]:
if isinstance(other, Boolean):
return Boolean(value=bool(self.value | other.value))
elif isinstance(other, PublicBoolean):
operation = BooleanOr(left=self, right=other, source_ref=SourceRef.back_frame())
return PublicBoolean(inner=operation)
elif isinstance(other, SecretBoolean):
operation = BooleanOr(left=self, right=other, source_ref=SourceRef.back_frame())
return SecretBoolean(inner=operation)
else:
raise TypeError(f"Invalid operation: {self} | {other}")

def __xor__(
self, other: Union["Boolean", "PublicBoolean", "SecretBoolean"]
) -> Union["Boolean", "PublicBoolean", "SecretBoolean"]:
if isinstance(other, Boolean):
return Boolean(value=bool(self.value ^ other.value))
elif isinstance(other, PublicBoolean):
operation = BooleanXor(left=self, right=other, source_ref=SourceRef.back_frame())
return PublicBoolean(inner=operation)
elif isinstance(other, SecretBoolean):
operation = BooleanXor(left=self, right=other, source_ref=SourceRef.back_frame())
return SecretBoolean(inner=operation)
else:
raise TypeError(f"Invalid operation: {self} ^ {other}")

def __invert__(
self: "Boolean"
) -> "Boolean":
Expand Down Expand Up @@ -912,6 +954,51 @@ def __ne__(
else:
raise TypeError(f"Invalid operation: {self} != {other}")

def __and__(
self, other: Union["Boolean", "PublicBoolean", "SecretBoolean"]
) -> Union["PublicBoolean", "SecretBoolean"]:
if isinstance(other, Boolean):
operation = BooleanAnd(left=self, right=other, source_ref=SourceRef.back_frame())
return PublicBoolean(inner=operation)
elif isinstance(other, PublicBoolean):
operation = BooleanAnd(left=self, right=other, source_ref=SourceRef.back_frame())
return PublicBoolean(inner=operation)
elif isinstance(other, SecretBoolean):
operation = BooleanAnd(left=self, right=other, source_ref=SourceRef.back_frame())
return SecretBoolean(inner=operation)
else:
raise TypeError(f"Invalid operation: {self} & {other}")

def __or__(
self, other: Union["Boolean", "PublicBoolean", "SecretBoolean"]
) -> Union["PublicBoolean", "SecretBoolean"]:
if isinstance(other, Boolean):
operation = BooleanOr(left=self, right=other, source_ref=SourceRef.back_frame())
return PublicBoolean(inner=operation)
elif isinstance(other, PublicBoolean):
operation = BooleanOr(left=self, right=other, source_ref=SourceRef.back_frame())
return PublicBoolean(inner=operation)
elif isinstance(other, SecretBoolean):
operation = BooleanOr(left=self, right=other, source_ref=SourceRef.back_frame())
return SecretBoolean(inner=operation)
else:
raise TypeError(f"Invalid operation: {self} | {other}")

def __xor__(
self, other: Union["Boolean", "PublicBoolean", "SecretBoolean"]
) -> Union["PublicBoolean", "SecretBoolean"]:
if isinstance(other, Boolean):
operation = BooleanXor(left=self, right=other, source_ref=SourceRef.back_frame())
return PublicBoolean(inner=operation)
elif isinstance(other, PublicBoolean):
operation = BooleanXor(left=self, right=other, source_ref=SourceRef.back_frame())
return PublicBoolean(inner=operation)
elif isinstance(other, SecretBoolean):
operation = BooleanXor(left=self, right=other, source_ref=SourceRef.back_frame())
return SecretBoolean(inner=operation)
else:
raise TypeError(f"Invalid operation: {self} ^ {other}")

def __invert__(
self: "PublicBoolean"
) -> "PublicBoolean":
Expand Down Expand Up @@ -1467,6 +1554,51 @@ def __ne__(
else:
raise TypeError(f"Invalid operation: {self} != {other}")

def __and__(
self, other: Union["Boolean", "PublicBoolean", "SecretBoolean"]
) -> "SecretBoolean":
if isinstance(other, Boolean):
operation = BooleanAnd(left=self, right=other, source_ref=SourceRef.back_frame())
return SecretBoolean(inner=operation)
elif isinstance(other, PublicBoolean):
operation = BooleanAnd(left=self, right=other, source_ref=SourceRef.back_frame())
return SecretBoolean(inner=operation)
elif isinstance(other, SecretBoolean):
operation = BooleanAnd(left=self, right=other, source_ref=SourceRef.back_frame())
return SecretBoolean(inner=operation)
else:
raise TypeError(f"Invalid operation: {self} & {other}")

def __or__(
self, other: Union["Boolean", "PublicBoolean", "SecretBoolean"]
) -> "SecretBoolean":
if isinstance(other, Boolean):
operation = BooleanOr(left=self, right=other, source_ref=SourceRef.back_frame())
return SecretBoolean(inner=operation)
elif isinstance(other, PublicBoolean):
operation = BooleanOr(left=self, right=other, source_ref=SourceRef.back_frame())
return SecretBoolean(inner=operation)
elif isinstance(other, SecretBoolean):
operation = BooleanOr(left=self, right=other, source_ref=SourceRef.back_frame())
return SecretBoolean(inner=operation)
else:
raise TypeError(f"Invalid operation: {self} | {other}")

def __xor__(
self, other: Union["Boolean", "PublicBoolean", "SecretBoolean"]
) -> "SecretBoolean":
if isinstance(other, Boolean):
operation = BooleanXor(left=self, right=other, source_ref=SourceRef.back_frame())
return SecretBoolean(inner=operation)
elif isinstance(other, PublicBoolean):
operation = BooleanXor(left=self, right=other, source_ref=SourceRef.back_frame())
return SecretBoolean(inner=operation)
elif isinstance(other, SecretBoolean):
operation = BooleanXor(left=self, right=other, source_ref=SourceRef.back_frame())
return SecretBoolean(inner=operation)
else:
raise TypeError(f"Invalid operation: {self} ^ {other}")

def __invert__(
self: "SecretBoolean"
) -> "SecretBoolean":
Expand Down
12 changes: 12 additions & 0 deletions nada_dsl/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ class PublicOutputEquality(BinaryOperation):
pass


class BooleanAnd(BinaryOperation):
pass


class BooleanOr(BinaryOperation):
pass


class BooleanXor(BinaryOperation):
pass


class Random:
source_ref: SourceRef

Expand Down

0 comments on commit 3fedfd8

Please sign in to comment.