-
Notifications
You must be signed in to change notification settings - Fork 22
/
miximus.py
124 lines (99 loc) · 4.44 KB
/
miximus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
Copyright 2019 to the Miximus Authors
This file is part of Miximus.
Miximus is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Miximus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Miximus. If not, see <https://www.gnu.org/licenses/>.
"""
__all__ = ('Miximus',)
import os
import re
import json
import ctypes
from ethsnarks.verifier import Proof, VerifyingKey
class Miximus(object):
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.miximus_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.miximus_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.miximus_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.miximus_verify
lib_verify.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
lib_verify.restype = ctypes.c_bool
self._verify = lib_verify
lib_nullifier = lib.miximus_nullifier
lib_nullifier.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
lib_nullifier.restype = ctypes.c_char_p
self._nullifier = lib_nullifier
def nullifier(self, secret, leaf_index):
assert isinstance(secret, int)
assert isinstance(leaf_index, int)
secret = ctypes.c_char_p(str(secret).encode('ascii'))
leaf_index = ctypes.c_char_p(str(leaf_index).encode('ascii'))
return int(self._nullifier(secret, leaf_index))
def prove(self, root, spend_preimage, exthash, 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_preimage, int)
assert isinstance(exthash, int)
# TODO: require root, nullifier, spend_preimage and exthash are ints within curve order range
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_preimage),
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 )