示例#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_parameters):
        """ Initialize election object.

        Initialize election, where election_parameters is a dict
        with at least the following key/values:
            "election_id" is a string
            "ballot_style" is a list of (race_id, choices) pairs,
               in which
                   race_id is a string
                   choices is list consisting of
                       one string for each allowable candidate/choice name, or
                       a string "******************" of stars
                           of the maximum allowable length of a write-in
                           if write-ins are allowed.

                Example:
                  ballot_style = [("President", ("Smith", "Jones", "********"))]
                defines a ballot style with one race (for President), and
                for this race the voter may vote for Smith, for Jones, or
                may cast a write-in vote of length at most 8 characters.
            "n_voters" is the number of simulated voters
            "n_reps" is the parameter for the cut-and-choose step
                     (n_reps replicas are made)
                     (in our paper, n_reps is called "2m")
            "n_fail" is the number of servers that may fail
            "n_leak" is the number of servers that may leak
        """

        self.election_parameters = election_parameters

        # manadatory parameters
        election_id = election_parameters["election_id"]
        ballot_style = election_parameters["ballot_style"]
        n_voters = election_parameters["n_voters"]
        n_reps = election_parameters["n_reps"]
        n_fail = election_parameters["n_fail"]
        n_leak = election_parameters["n_leak"]
        # optional parameters (with defaults)
        ballot_id_len = election_parameters.get("ballot_id_len", 32)
        json_indent = election_parameters.get("json_indent", 0)
        self.json_indent = json_indent
        sv.set_json_indent(json_indent)

        # check and save parameters
        assert isinstance(election_id, str) and len(election_id) > 0
        self.election_id = election_id

        assert isinstance(ballot_style, list) and len(ballot_style) > 0
        self.ballot_style = ballot_style

        assert isinstance(n_voters, int) and n_voters > 0
        self.n_voters = n_voters
        # p-list is list ["p0", "p1", ..., "p(n-1)"]
        # these name the positions in a list of objects, one per voter.
        # they are not voter ids, they are really names of positions
        # used for clarity and greater compatibility with json
        # keep all the same length (use leading zeros) so that
        # json "sort by keys" options works.
        # the following list is in sorted order!
        self.p_list = sv.p_list(n_voters)

        assert isinstance(n_reps, int) and \
            0 < n_reps <= 26 and n_reps % 2 == 0
        self.n_reps = n_reps
        # Each copy will be identified by an upper-case ascii letter
        self.k_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[:n_reps]

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

        assert ballot_id_len > 0
        self.ballot_id_len = ballot_id_len

        about_text = \
        ["Secure Bulletin Board for Split-Value Voting Method Demo.",
         "by Michael O. Rabin and Ronald L. Rivest",
         "For paper: see http://people.csail.mit.edu/rivest/pubs.html#RR14a",
         "For code: see https://github.com/ron-rivest/split-value-voting",
        ]
        legend_text = \
        ["Indices between 0 and n_voters-1 indicated by p0, p1, ...",
         "Rows of server array indicated by a, b, c, d, ...",
         "Copies (n_reps = 2m passes) indicated by A, B, C, D, ...",
         "'********' in ballot style indicates a write-in option",
         "           (number of stars is max write-in length)",
         "Values represented are represented modulo race_modulus.",
         "'x' (or 'y') equals u+v (mod race_modulus),",
         "             and is a (Shamir-)share of the vote.",
         "'cu' and 'cv' are commitments to u and v, respectively.",
         "'ru' and 'rv' are randomization values for cu and cv.",
         "'icl' stands for 'input comparison list',",
         "'opl' for 'output production list';",
         "      these are the 'cut-and-choose' results",
         "      dividing up the lists into two sub-lists.",
         "'time' is time in ISO 8601 format."
        ]
        # start secure bulletin board
        self.sbb = sv_sbb.SBB(election_id)
        self.sbb.post("setup:start",
                      {"about": about_text,
                       "election_id": election_id,
                       "legend": legend_text
                      }
                     )

        self.races = []
        self.race_ids = [race_id for (race_id, choices) in ballot_style]
        self.setup_races(ballot_style)
        self.voters = []
        self.voter_ids = []
        self.setup_voters(self, n_voters)
        self.cast_votes = dict()
        self.server = sv_server.Server(self, n_fail, n_leak)
        self.output_commitments = dict()
        self.setup_keys()
        self.sbb.post("setup:finished")
    def __init__(self, election_parameters):
        """ Initialize election object.

        Initialize election, where election_parameters is a dict
        with at least the following key/values:
            "election_id" is a string
            "ballot_style" is a list of (race_id, choices) pairs,
               in which
                   race_id is a string
                   choices is list consisting of
                       one string for each allowable candidate/choice name, or
                       a string "******************" of stars
                           of the maximum allowable length of a write-in
                           if write-ins are allowed.

                Example:
                  ballot_style = [("President", ("Smith", "Jones", "********"))]
                defines a ballot style with one race (for President), and
                for this race the voter may vote for Smith, for Jones, or
                may cast a write-in vote of length at most 8 characters.
            "n_voters" is the number of simulated voters
            "n_reps" is the parameter for the cut-and-choose step
                     (n_reps replicas are made)
                     (in our paper, n_reps is called "2m")
            "n_fail" is the number of servers that may fail
            "n_leak" is the number of servers that may leak
        """

        self.election_parameters = election_parameters

        # mandatory parameters
        election_id = election_parameters["election_id"]
        ballot_style = election_parameters["ballot_style"]
        n_voters = election_parameters["n_voters"]
        n_reps = election_parameters["n_reps"]
        n_fail = election_parameters["n_fail"]
        n_leak = election_parameters["n_leak"]
        # optional parameters (with defaults)
        ballot_id_len = election_parameters.get("ballot_id_len", 32)
        json_indent = election_parameters.get("json_indent", 0)
        self.json_indent = json_indent
        sv.set_json_indent(json_indent)

        # check and save parameters
        assert isinstance(election_id, str) and len(election_id) > 0
        self.election_id = election_id

        assert isinstance(ballot_style, list) and len(ballot_style) > 0
        self.ballot_style = ballot_style

        assert isinstance(n_voters, int) and n_voters > 0
        self.n_voters = n_voters
        # p-list is list ["p0", "p1", ..., "p(n-1)"]
        # these name the positions in a list of objects, one per voter.
        # they are not voter ids, they are really names of positions
        # used for clarity and greater compatibility with json
        # keep all the same length (use leading zeros) so that
        # json "sort by keys" options works.
        # the following list is in sorted order!
        self.p_list = sv.p_list(n_voters)

        assert isinstance(n_reps, int) and \
            0 < n_reps <= 26 and n_reps % 2 == 0
        self.n_reps = n_reps
        # Each copy will be identified by an upper-case ascii letter
        self.k_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[:n_reps]

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

        assert ballot_id_len > 0
        self.ballot_id_len = ballot_id_len

        self.about_text = \
        ["Secure Bulletin Board for Split-Value Voting Method Demo.",
         "by Michael O. Rabin and Ronald L. Rivest",
         "For paper: see http://people.csail.mit.edu/rivest/pubs.html#RR14a",
         "For code: see https://github.com/ron-rivest/split-value-voting",
        ]
        self.legend_text = \
        ["Indices between 0 and n_voters-1 indicated by p0, p1, ...",
         "Rows of server array indicated by a, b, c, d, ...",
         "Copies (n_reps = 2m passes) indicated by A, B, C, D, ...",
         "'********' in ballot style indicates a write-in option",
         "           (number of stars is max write-in length)",
         "Values represented are represented modulo race_modulus.",
         "'x' (or 'y') equals u+v (mod race_modulus),",
         "             and is a (Shamir-)share of the vote.",
         "'cu' and 'cv' are commitments to u and v, respectively.",
         "'ru' and 'rv' are randomization values for cu and cv.",
         "'icl' stands for 'input comparison list',",
         "'opl' for 'output production list';",
         "      these are the 'cut-and-choose' results",
         "      dividing up the lists into two sub-lists.",
         "'time' is time in ISO 8601 format."
        ]
        self.legend_text = ["TODO:"]
        #TODO fix legend_text I was getting some out of bounds error, most likely due to the encoding
        

        self.races = []
        self.race_ids = [race_id for (race_id, choices) in ballot_style]
        self.setup_races(ballot_style)
        self.voters = []
        self.voter_ids = []
        self.setup_voters(self, self.n_voters)
        self.cast_votes = dict()
        self.initialize_cast_votes()
        self.output_commitments = dict()
        self.server = sv_server.Server(self, n_fail, n_leak)