Example #1
0
def read_rows_cols_n_reps_threshold_indent(sbb_dict, db):
    """ Read setup:server-array to extract rows/cols/n_reps into db. """
    sbbd = sbb_dict['setup:server-array']
    rows = sbbd['rows']
    cols = sbbd['cols']
    n_reps = sbbd['n_reps']
    threshold = sbbd['threshold']
    json_indent = sbbd['json_indent']
    assert isinstance(rows, int) and rows > 0
    assert isinstance(cols, int) and cols > 0
    assert isinstance(n_reps, int) and n_reps > 0
    assert isinstance(threshold, int) and threshold > 0
    db['rows'] = rows
    assert rows < 27
    db['row_list'] = sv.row_list(rows)
    db['cols'] = cols
    db['n_reps'] = n_reps
    db['threshold'] = threshold
    assert n_reps < 27
    db['k_list'] = sv.k_list(n_reps)
    assert json_indent is None or isinstance(json_indent, int)
    assert json_indent is None or json_indent >= 0
    db['json_indent'] = json_indent
    sv.set_json_indent(json_indent)
    print('read_rows_cols_n_reps_threshold: successful.')
def read_rows_cols_n_reps_threshold_indent(sbb_dict, db):
    """ Read setup:server-array to extract rows/cols/n_reps into db. """
    sbbd = sbb_dict['setup:server-array']
    rows = sbbd['rows']
    cols = sbbd['cols']
    n_reps = sbbd['n_reps']
    threshold = sbbd['threshold']
    json_indent = sbbd['json_indent']
    assert isinstance(rows, int) and rows > 0
    assert isinstance(cols, int) and cols > 0
    assert isinstance(n_reps, int) and n_reps > 0
    assert isinstance(threshold, int) and threshold > 0
    db['rows'] = rows
    assert rows < 27
    db['row_list'] = sv.row_list(rows)
    db['cols'] = cols
    db['n_reps'] = n_reps
    db['threshold'] = threshold
    assert n_reps < 27
    db['k_list'] = sv.k_list(n_reps)
    assert json_indent is None or isinstance(json_indent, int)
    assert json_indent is None or json_indent >= 0
    db['json_indent'] = json_indent
    sv.set_json_indent(json_indent)
    print('read_rows_cols_n_reps_threshold: successful.')
    def __init__(self, election, n_fail, n_leak):
        """ Initialize server.  This is one for the whole election.

        The server array has the given number of rows and columns
        as parts or sub-servers.  Here we simulate the actions of
        these sub-servers.
        n_fail = number of servers that could fail
        n_leak = number of servers that my leak information
        """

        assert isinstance(n_fail, int) and n_fail >= 0
        assert isinstance(n_leak, int) and n_leak >= 0

        self.election = election
        self.n_fail = n_fail
        self.n_leak = n_leak

        if self.n_fail > 0:
            self.cols = 1 + n_leak
            self.rows = 2 + n_fail + n_leak
            self.threshold = 2 + n_leak   # number of rows needed to reconstruct
        else:
            self.cols = 1 + n_leak
            self.rows = 1 + n_leak
            self.threshold = 1 + n_leak
        rows = self.rows
        cols = self.cols
        threshold = self.threshold

        # each row has an identifier in "abcd..."
        assert rows <= 26
        self.row_list = sv.row_list(rows)

        self.challenges = dict()

        # The state of the server in row i, col j is represented
        # here in the dictionary P[race_id][i][j] for the given race
        # where  i in row_list  and 0 <= j < cols.

        # create one top-level dict sdb as database for this main server
        self.sdb = dict()

        # within the top level dict sdb, create a three-dimensional array of
        # sub-dicts for data storage, each addressed as sdb[race_id][i][j]
        for race in election.races:
            race_id = race.race_id
            self.sdb[race_id] = dict()
            for i in self.row_list:
                self.sdb[race_id][i] = dict()
                for j in range(cols):
                    self.sdb[race_id][i][j] = dict()

        # each server has its own random-number source for each race
        # in practice, these could be derived from true random seeds input
        # independently to each server
        for race_id in election.race_ids:
            for i in self.row_list:
                for j in range(cols):
                    rand_name = "server:" + race_id + ":" + \
                                str(i) + ":" + str(j)
                    self.sdb[race_id][i][j]['rand_name'] = rand_name
                    sv.init_randomness_source(rand_name)

        # within each sub-dict sdb[race_id][i][j] create a variety of lists for
        # storage of cast votes and associated data, including 2m-way replicated
        # lists needed for the 2m mixes (passes) needed.
        for race_id in election.race_ids:
            for i in self.row_list:
                # first-column lists for storing cast votes and secrets
                sdbp = self.sdb[race_id][i][0]
                sdbp['ballot_id'] = dict()   # ballot_id (indices from p_list)
                sdbp['cu'] = dict()          # commitments to u
                sdbp['cv'] = dict()          # commitments to v
                sdbp['x'] = dict()           # choice x, where x = u+v mod M
                sdbp['u'] = dict()           # u
                sdbp['v'] = dict()           # v
                sdbp['ru'] = dict()          # randomness to open com(u)
                sdbp['rv'] = dict()          # randomness to open com(v)
                # for all columns, have 2m-way replicated data structures
                for j in range(cols):
                    sdbp = self.sdb[race_id][i][j]
                    for k in election.k_list:
                        sdbp[k] = dict()
                        sdbp[k]['x'] = dict()    # inputs on pass k
                        sdbp[k]['y'] = dict()    # outputs on pass k
                # last-column lists for storing published lists of commitments
                for k in election.k_list:
                    sdbp = self.sdb[race_id][i][self.cols-1][k]
                    sdbp['y'] = dict()
                    sdbp['u'] = dict()
                    sdbp['v'] = dict()
                    sdbp['ru'] = dict()
                    sdbp['rv'] = dict()
                    sdbp['cu'] = dict()
                    sdbp['cv'] = dict()

        # create one top-level dict sdb as database for this main server (a unshared copy of sbd)
        self.sdb = deepcopy(self.sdb) # TODO remove
    def __init__(self, election, n_fail, n_leak):
        """ Initialize server.  This is one for the whole election.

        The server array has the given number of rows and columns
        as parts or sub-servers.  Here we simulate the actions of
        these sub-servers.
        n_fail = number of servers that could fail
        n_leak = number of servers that my leak information
        """

        assert isinstance(n_fail, int) and n_fail >= 0
        assert isinstance(n_leak, int) and n_leak >= 0

        self.election = election
        self.n_fail = n_fail
        self.n_leak = n_leak

        if self.n_fail > 0:
            self.cols = 1 + n_leak
            self.rows = 2 + n_fail + n_leak
            self.threshold = 2 + n_leak   # number of rows needed to reconstruct
        else:
            self.cols = 1 + n_leak
            self.rows = 1 + n_leak
            self.threshold = 1 + n_leak
        rows = self.rows
        cols = self.cols
        threshold = self.threshold

        # each row has an identifier in "abcd..."
        assert rows <= 26
        self.row_list = sv.row_list(rows)

        # The state of the server in row i, col j is represented
        # here in the dictionary P[race_id][i][j] for the given race
        # where  i in row_list  and 0 <= j < cols.

        # create one top-level dict sdb as database for this main server
        self.sdb = dict()

        # within the top level dict sdb, create a three-dimensional array of
        # sub-dicts for data storage, each addressed as sdb[race_id][i][j]
        for race in election.races:
            race_id = race.race_id
            self.sdb[race_id] = dict()
            for i in self.row_list:
                self.sdb[race_id][i] = dict()
                for j in range(cols):
                    self.sdb[race_id][i][j] = dict()

        # each server has its own random-number source for each race
        # in practice, these could be derived from true random seeds input
        # independently to each server
        for race_id in election.race_ids:
            for i in self.row_list:
                for j in range(cols):
                    rand_name = "server:" + race_id + ":" + \
                                str(i) + ":" + str(j)
                    self.sdb[race_id][i][j]['rand_name'] = rand_name
                    sv.init_randomness_source(rand_name)

        # within each sub-dict sdb[race_id][i][j] create a variety of lists for
        # storage of cast votes and associated data, including 2m-way replicated
        # lists needed for the 2m mixes (passes) needed.
        for race_id in election.race_ids:
            for i in self.row_list:
                # first-column lists for storing cast votes and secrets
                sdbp = self.sdb[race_id][i][0]
                sdbp['ballot_id'] = dict()   # ballot_id (indices from p_list)
                sdbp['cu'] = dict()          # commitments to u
                sdbp['cv'] = dict()          # commitments to v
                sdbp['x'] = dict()           # choice x, where x = u+v mod M
                sdbp['u'] = dict()           # u
                sdbp['v'] = dict()           # v
                sdbp['ru'] = dict()          # randomness to open com(u)
                sdbp['rv'] = dict()          # randomness to open com(v)
                # for all columns, have 2m-way replicated data structures
                for j in range(cols):
                    sdbp = self.sdb[race_id][i][j]
                    for k in election.k_list:
                        sdbp[k] = dict()
                        sdbp[k]['x'] = dict()    # inputs on pass k
                        sdbp[k]['y'] = dict()    # outputs on pass k
                # last-column lists for storing published lists of commitments
                for k in election.k_list:
                    sdbp = self.sdb[race_id][i][self.cols-1][k]
                    sdbp['y'] = dict()
                    sdbp['u'] = dict()
                    sdbp['v'] = dict()
                    sdbp['ru'] = dict()
                    sdbp['rv'] = dict()
                    sdbp['cu'] = dict()
                    sdbp['cv'] = dict()
        # post on log that server array is set up
        election.sbb.post("setup:server-array",
                          {"rows": rows, "cols": cols,
                           "n_reps": election.n_reps,
                           "threshold": threshold,
                           'json_indent': election.json_indent},
                          time_stamp=False)