Skip to content

Commit

Permalink
feat: add support for EcdsaSignature, EcdsaDigestMessage and EcdsaPri…
Browse files Browse the repository at this point in the history
…vateKey types and ecdsa_sign function
  • Loading branch information
manel1874 committed Nov 7, 2024
1 parent 511e4fb commit 5501153
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
18 changes: 17 additions & 1 deletion nada_dsl/compile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json
import pytest
from nada_dsl.ast_util import AST_OPERATIONS
from nada_dsl.compile import compile_script, compile_string
from nada_dsl.compile import compile_script, compile_string, print_output
from nada_dsl.compiler_frontend import FUNCTIONS, INPUTS, PARTIES
from nada_dsl.errors import NotAllowedException

Expand Down Expand Up @@ -161,3 +161,19 @@ def test_compile_map_simple():
raise Exception(f"Unexpected operation: {name}")
assert map_inner > 0 and array_input_id > 0 and map_inner == array_input_id
assert function_op_id > 0 and output_id == function_op_id

def test_compile_ecdsa_program():
program_str = """
from nada_dsl import *
def nada_main():
party1 = Party(name="Party1")
private_key = EcdsaPrivateKey(Input(name="private_key", party=party1))
digest = EcdsaDigestMessage(Input(name="digest", party=party1))
new_int = private_key.ecdsa_sign(digest)
return [Output(new_int, "my_output", party1)]
"""
encoded_program_str = base64.b64encode(bytes(program_str, "utf-8")).decode("utf_8")
output = compile_string(encoded_program_str)
print_output(output)
6 changes: 6 additions & 0 deletions nada_dsl/nada_types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def __init__(self, name):
"Tuple",
"NTuple",
"Object",
"EcdsaPrivateKey",
"EcdsaDigestMessage",
"EcdsaSignature",
]
AllTypesType = Union[
Type["Integer"],
Expand All @@ -55,6 +58,9 @@ def __init__(self, name):
Type["Tuple"],
Type["NTuple"],
Type["Object"],
Type["EcdsaPrivateKey"],
Type["EcdsaDigestMessage"],
Type["EcdsaSignature"],
]
OperationType = Union[
"Addition",
Expand Down
32 changes: 32 additions & 0 deletions nada_dsl/nada_types/scalar_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,3 +557,35 @@ def to_public(self: "SecretBoolean") -> "PublicBoolean":
def random(cls) -> "SecretBoolean":
"""Generate a random secret boolean."""
return SecretBoolean(inner=Random(source_ref=SourceRef.back_frame()))


@dataclass
class EcdsaSignature(NadaType):
"""The EcdsaSignature Nada MIR type."""

def __init__(self, inner: OperationType):
super().__init__(inner=inner)


@dataclass
class EcdsaDigestMessage(NadaType):
"""The EcdsaDigestMessage Nada MIR type."""

def __init__(self, inner: OperationType):
super().__init__(inner=inner)


@dataclass
class EcdsaPrivateKey(NadaType):
"""The EcdsaPrivateKey Nada MIR type."""

def __init__(self, inner: OperationType):
super().__init__(inner=inner)

def ecdsa_sign(cls, digest: "EcdsaDigestMessage") -> "EcdsaSignature":
"""Random operation for Secret integers."""
return EcdsaSignature(
inner=EcdsaSign(
left=cls, right=digest, source_ref=SourceRef.back_frame()
)
)
3 changes: 3 additions & 0 deletions nada_dsl/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,6 @@ class Not(UnaryOperation):

def __init__(self, this: AllTypes, source_ref: SourceRef):
super().__init__(inner=this, source_ref=source_ref)

class EcdsaSign(BinaryOperation):
"""Ecdsa signing operation."""

0 comments on commit 5501153

Please sign in to comment.