def genWitness(leaves, nullifier, sk, signal, signal_variables, external_nullifier, address, tree_depth, fee, pk_dir, isInt): path = [] address_bits = [] root, merkle_tree = utils.genMerkelTree(tree_depth, leaves) path1, address_bits1 = utils.getMerkelProof(leaves, address, tree_depth) try: path = [hexToBinary(x) for x in path1] except: path = [bytesToBinary(x) for x in path1] address_bits = address_bits1[::-1] print(path1, address_bits, address_bits, root, leaves, sk, nullifier) path = path[::-1] path.append(hexToBinary(nullifier)) path.append(hexToBinary(sk)) path.append(hexToBinary(root)) print("address bits ", address_bits) path = ((c.c_bool * 256) * (tree_depth + 3))(*path) address = int("".join([str(int(x)) for x in address_bits]), 2) address_bits = (c.c_bool * tree_depth)(*address_bits) signal = (c.c_bool * 256)(*hexToBinary(signal)) signal_variables = (c.c_bool * 256)(*hexToBinary(signal_variables)) external_nullifier = (c.c_bool * 256)(*hexToBinary(external_nullifier)) fee = c.c_int(fee) proof = prove(path, signal, signal_variables, external_nullifier, address, address_bits, tree_depth, fee, c.c_char_p(pk_dir.encode()), c.c_bool(isInt)) proof = json.loads(proof.decode("utf-8")) return (proof, root)
if __name__ == "__main__": pk_output = "../zksnark_element/pk.raw" vk_output = "../zksnark_element/vk.json" # perform the trusted setup making hte proving key , verification key genKeys(c.c_int(tree_depth), c.c_char_p(pk_output.encode()), c.c_char_p(vk_output.encode())) #part 1 merkel tree setup which act as the census of the vote #make merkel Tree with 10 members #This is a trusted part but it can be varified by the users before teh leaves, nullifiers, sks = initMerkleTree(1) root, layers = genMerkelTree(29, leaves) #part 2 definition of the vote properties. # You "sign" signal 1 to vote for canditate 1 signal1 = sha256({ "NomimatedSpokesPersonFor": root, "candidate": "Candidate1" }) # You "sign" signal 2 to vote for candidate 2 signal2 = sha256({ "NomimatedSpokesPersonFor": root, "candidate": "Candidate2" }) # You use external_nullifier to enforce one person (merkle tree memeber) one signal external_nullifier = sha256("nomimatedSpokesPerson" + root + str(time.time()))
def genWitness(leaves, public_key_x, public_key_y, address, tree_depth, _rhs_leaf, _new_leaf, r_x, r_y, s): path = [] fee = 0 address_bits = [] pub_key_x = [] pub_key_y = [] roots = [] paths = [] old_leaf = [] new_leaf = [] r_x_bin_array = [] r_y_bin_array = [] s_bin_array = [] for i in range(noTx): root, merkle_tree = utils.genMerkelTree(tree_depth, leaves[i]) path, address_bit = utils.getMerkelProof(leaves[i], address[i], tree_depth) path = [binary2ctypes(hexToBinary(x)) for x in path] address_bit = address_bit[::-1] path = path[::-1] paths.append(((c.c_bool * 256) * (tree_depth))(*path)) pub_key_x.append(binary2ctypes(hexToBinary(public_key_x[i]))) pub_key_y.append(binary2ctypes(hexToBinary(public_key_y[i]))) roots.append(binary2ctypes(hexToBinary(root))) address_bits.append((c.c_bool * tree_depth)(*address_bit)) old_leaf.append(binary2ctypes(hexToBinary(_rhs_leaf[i]))) new_leaf.append(binary2ctypes(hexToBinary(_new_leaf[i]))) r_x_bin_array.append(binary2ctypes(hexToBinary(r_x[i]))) r_y_bin_array.append(binary2ctypes(hexToBinary(r_y[i]))) s_bin_array.append(binary2ctypes(hexToBinary(hex(s[i])))) pub_key_x_array = ((c.c_bool * 256) * (noTx))(*pub_key_x) pub_key_y_array = ((c.c_bool * 256) * (noTx))(*pub_key_y) merkle_roots = ((c.c_bool * 256) * (noTx))(*roots) old_leaf = ((c.c_bool * 256) * (noTx))(*old_leaf) new_leaf = ((c.c_bool * 256) * (noTx))(*new_leaf) r_x_bin = ((c.c_bool * 256) * (noTx))(*r_x_bin_array) r_y_bin = ((c.c_bool * 256) * (noTx))(*r_y_bin_array) s_bin = ((c.c_bool * 256) * (noTx))(*s_bin_array) paths = ((c.c_bool * 256) * (tree_depth) * noTx)(*paths) address_bits = ((c.c_bool) * (tree_depth) * noTx)(*address_bits) proof = prove(paths, pub_key_x_array, pub_key_y_array, merkle_roots, address_bits, old_leaf, new_leaf, r_x_bin, r_y_bin, s_bin, tree_depth, noTx) proof = json.loads(proof.decode("utf-8")) root, merkle_tree = utils.genMerkelTree(tree_depth, leaves[0]) return (proof, root)