Reduce
- Nada program
- Test file
src/reduce.py
from nada_dsl import *
import nada_numpy as na
def add(left: SecretInteger, right: SecretInteger) -> SecretInteger:
return left + right
def reduce(array: List[SecretInteger], fn: nada_fn, initialValue: Integer) -> SecretInteger:
total = Integer(initialValue)
for element in array:
total = fn(total, element)
return total
def nada_main():
num_parties = 10
parties = na.parties(num_parties)
secrets_list = []
for i in range(num_parties):
secrets_list.append(
SecretInteger(Input(name="num_" + str(i), party=parties[i]))
)
total = reduce(secrets_list, add, 0)
return [Output(total, "total", party=parties[0])]
tests/reduce_test.yaml
---
program: reduce
inputs:
num_2: 1
num_0: 2
num_9: 3
num_1: 4
num_3: 5
num_6: 6
num_7: 7
num_5: 8
num_8: 9
num_4: 10
expected_outputs:
total: 55
Run and test the reduce program
1. Open "Nada by Example"
2. Run the program with inputs
from the test file
nada run reduce_test
3. Test the program with inputs
from the test file against the expected_outputs
from the test file
nada test reduce_test