Skip to main content

If / Else (Ternary)

If / Else with a public condition

src/if_else_public.py
from nada_dsl import *

def nada_main():
party_alice = Party(name="Alice")
party_bob = Party(name="Bob")
party_charlie = Party(name="Charlie")

conditional_num_1 = PublicInteger(Input(name="conditional_num_1", party=party_alice))
conditional_num_2 = PublicInteger(Input(name="conditional_num_2", party=party_bob))

secret_1 = SecretInteger(Input(name="secret_1", party=party_alice))
secret_2 = SecretInteger(Input(name="secret_2", party=party_bob))

# Public condition
condition = conditional_num_1 > conditional_num_2
conditional_product = condition.if_else(secret_1, secret_2) * Integer(2)

return [Output(conditional_product, "conditional_product", party_charlie)]

Run and test the if_else_public program

1. Open "Nada by Example"

Open in Gitpod

2. Run the program with inputs from the test file

nada run if_else_public_test

3. Test the program with inputs from the test file against the expected_outputs from the test file

nada test if_else_public_test

If / Else with a private condition

src/if_else_private.py
from nada_dsl import *

def nada_main():
party_alice = Party(name="Alice")
party_bob = Party(name="Bob")
party_charlie = Party(name="Charlie")

conditional_num_1 = SecretInteger(Input(name="conditional_num_1", party=party_alice))
conditional_num_2 = SecretInteger(Input(name="conditional_num_2", party=party_bob))

secret_1 = SecretInteger(Input(name="secret_1", party=party_alice))
secret_2 = SecretInteger(Input(name="secret_2", party=party_bob))

# Private condition
condition = conditional_num_1 > conditional_num_2
conditional_product = condition.if_else(secret_1, secret_2) * Integer(2)

return [Output(conditional_product, "conditional_product", party_charlie)]

Run and test the if_else_private program

1. Open "Nada by Example"

Open in Gitpod

2. Run the program with inputs from the test file

nada run if_else_private_test

3. Test the program with inputs from the test file against the expected_outputs from the test file

nada test if_else_private_test
Feedback