def test_verify_native(self): """Verify using fast native library""" vk = VerifyingKey.from_dict(VK_STATIC) proof = Proof.from_dict(PROOF_STATIC) wrapper = HashPreimage(native_lib_path('build/src/libhashpreimage'), vk) self.assertTrue(wrapper.verify(proof))
def test_proof_gen(self): leaves, nullifiers, sks = initMerkleTree(2) root, layers = genMerkelTree(tree_depth, leaves) signal_variables = sha256(str(1)) external_nullifier = sha256("nomimatedSpokesPerson" + root + str(time.time())) signal1 = sha256({ "NomimatedSpokesPersonFor": root, "candidate": "Candidate1" }) proof = None with open('zksnark_element/vk.json', 'r') as handle: vk = VerifyingKey.from_dict(json.load(handle)) for address, (nullifier, sk) in enumerate(zip(nullifiers, sks)): print("Generating witness") proof_data, proof_root = genWitness(leaves, nullifier, sk, signal1, signal_variables, external_nullifier, address, tree_depth, 0, PK_FILENAME) # Verify proof is correct proof = Proof.from_dict(proof_data) self.assertTrue(native_verify(vk.to_json(), proof.to_json())) # Verify modifying proof results in False state proof.input[n] -= 1 self.assertFalse(native_verify(vk.to_json(), proof.to_json())) break
def __init__(self, native_library_path, vk, pk_file=None): if pk_file: if not os.path.exists(pk_file): raise RuntimeError("Proving key file doesnt exist: " + pk_file) self._pk_file = pk_file if not isinstance(vk, VerifyingKey): if isinstance(vk, dict): vk = VerifyingKey.from_dict(vk) elif os.path.exists(vk): vk = VerifyingKey.from_file(vk) else: vk = VerifyingKey.from_json(vk) if not isinstance(vk, VerifyingKey): raise TypeError("Invalid vk type") self._vk = vk lib = cdll.LoadLibrary(native_library_path) lib_prove = lib.hashpreimage_prove lib_prove.argtypes = [ctypes.c_char_p, ctypes.c_char_p] lib_prove.restype = ctypes.c_char_p self._prove = lib_prove lib_verify = lib.hashpreimage_verify lib_verify.argtypes = [ctypes.c_char_p, ctypes.c_char_p] lib_verify.restype = ctypes.c_bool self._verify = lib_verify
def __init__(self, native_library_path, vk, pk_file=None): if pk_file: if not os.path.exists(pk_file): raise RuntimeError("Proving key file doesnt exist: " + pk_file) self._pk_file = pk_file if not isinstance(vk, VerifyingKey): if isinstance(vk, dict): vk = VerifyingKey.from_dict(vk) elif os.path.exists(vk): vk = VerifyingKey.from_file(vk) else: vk = VerifyingKey.from_json(vk) if not isinstance(vk, VerifyingKey): raise TypeError("Invalid vk type") self._vk = vk lib = ctypes.cdll.LoadLibrary(native_library_path) lib_tree_depth = lib.mixer_tree_depth lib_tree_depth.restype = ctypes.c_size_t self.tree_depth = lib_tree_depth() assert self.tree_depth > 0 assert self.tree_depth < 32 lib_prove = lib.mixer_prove lib_prove.argtypes = ([ctypes.c_char_p] * 6) + \ [(ctypes.c_char_p * self.tree_depth)] lib_prove.restype = ctypes.c_char_p self._prove = lib_prove lib_verify = lib.mixer_verify lib_verify.argtypes = [ctypes.c_char_p, ctypes.c_char_p] lib_verify.restype = ctypes.c_bool self._verify = lib_verify
def test_verify_python(self): # Verify using sloooow python implementation vk = VerifyingKey.from_dict(VK_STATIC) proof = Proof.from_dict(PROOF_STATIC) self.assertTrue(vk.verify(proof))
def test_vk_roundtrip(self): vk = VerifyingKey.from_dict(VK_STATIC) vk_json = vk.to_json() vk2 = VerifyingKey.from_dict(json.loads(vk_json)) self.assertEqual(vk, vk2)
def test_verify_native(self): """Verify using fast native library""" vk = VerifyingKey.from_dict(VK_STATIC) proof = Proof.from_dict(PROOF_STATIC) self.assertTrue(native_verify(vk.to_json(), proof.to_json()))
def __init__(self, native_library_path, vk, pk_file=None): if pk_file: if not os.path.exists(pk_file): raise RuntimeError("Proving key file doesnt exist: " + pk_file) self._pk_file = pk_file if not isinstance(vk, VerifyingKey): if isinstance(vk, dict): vk = VerifyingKey.from_dict(vk) elif os.path.exists(vk): vk = VerifyingKey.from_file(vk) else: vk = VerifyingKey.from_json(vk) if not isinstance(vk, VerifyingKey): raise TypeError("Invalid vk type") self._vk = vk lib = ctypes.cdll.LoadLibrary(native_library_path) lib_tree_depth = lib.ssles_tree_depth lib_tree_depth.restype = ctypes.c_size_t self.tree_depth = lib_tree_depth() assert self.tree_depth > 0 assert self.tree_depth <= 32 lib_prove = lib.ssles_prove lib_prove.argtypes = ([ctypes.c_char_p] * 5) + [ (ctypes.c_char_p * self.tree_depth) ] lib_prove.restype = ctypes.c_char_p self._prove = lib_prove lib_prove_json = lib.ssles_prove_json lib_prove_json.argtypes = [ ctypes.c_char_p, ctypes.c_char_p ] lib_prove_json.restype = ctypes.c_char_p self._prove_json = lib_prove_json lib_verify = lib.ssles_verify lib_verify.argtypes = [ ctypes.c_char_p, ctypes.c_char_p ] lib_verify.restype = ctypes.c_bool self._verify = lib_verify def prove(self, root, spend_pubkey, msg, exthash, prehash, address_bits, path, pk_file=None): assert isinstance(path, (list, tuple)) assert len(path) == self.tree_depth if isinstance(address_bits, (tuple, list)): address_bits = ''.join( [str(_) for _ in address_bits]) assert re.match(r'^[01]+$', address_bits) assert len(address_bits) == self.tree_depth assert isinstance(root, int) assert isinstance(spend_pubkey, int) assert isinstance(msg, int) assert isinstance(exthash, int) assert isinstance(prehash, int) if pk_file is None: pk_file = self._pk_file if pk_file is None: raise RuntimeError("No proving key file") args_dict = dict(root=hex(root), exthash=hex(exthash), secret=hex(spend_pubkey), msg=hex(msg), prehash=hex(prehash), address=sum([ (1 << i) * int(_) for i, _ in enumerate(address_bits) ]), path=[hex(_) for _ in path]) args_json = json.dumps(args_dict).encode('ascii') args_json_cstr = ctypes.c_char_p(args_json) pk_file_cstr = ctypes.c_char_p(pk_file.encode('ascii')) data = self._prove_json(pk_file_cstr, args_json_cstr) if data is None: raise RuntimeError("Could not prove!") return Proof.from_json(data) def verify(self, proof): if not isinstance(proof, Proof): raise TypeError("Invalid proof type") vk_cstr = ctypes.c_char_p( self._vk.to_json().encode('ascii')) proof_cstr = ctypes.c_char_p( proof.to_json().encode('ascii')) return self._verify(vk_cstr, proof_cstr)