aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/dexon-foundation/mcl/ffi/python/she.py
blob: ab8975274b941539c886464d78d5dd536be6e27d (plain) (blame)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import os
import platform
from ctypes import *

MCL_BN254 = 0
MCLBN_FR_UNIT_SIZE = 4
MCLBN_FP_UNIT_SIZE = 4

FR_SIZE = MCLBN_FR_UNIT_SIZE
G1_SIZE = MCLBN_FP_UNIT_SIZE * 3
G2_SIZE = MCLBN_FP_UNIT_SIZE * 6
GT_SIZE = MCLBN_FP_UNIT_SIZE * 12

SEC_SIZE = FR_SIZE * 2
PUB_SIZE = G1_SIZE + G2_SIZE
G1_CIPHER_SIZE = G1_SIZE * 2
G2_CIPHER_SIZE = G2_SIZE * 2
GT_CIPHER_SIZE = GT_SIZE * 4

MCLBN_COMPILED_TIME_VAR = (MCLBN_FR_UNIT_SIZE * 10) + MCLBN_FP_UNIT_SIZE

Buffer = c_ubyte * 1536
lib = None

def init(curveType=MCL_BN254):
    global lib
    name = platform.system()
    if name == 'Linux':
        libName = 'libmclshe256.so'
    elif name == 'Darwin':
        libName = 'libmclshe256.dylib'
    elif name == 'Windows':
        libName = 'mclshe256.dll'
    else:
        raise RuntimeError("not support yet", name)
    lib = cdll.LoadLibrary(libName)
    ret = lib.sheInit(MCL_BN254, MCLBN_COMPILED_TIME_VAR)
    if ret != 0:
        raise RuntimeError("sheInit", ret)
    # custom setup for a function which returns pointer
    lib.shePrecomputedPublicKeyCreate.restype = c_void_p

def setRangeForDLP(hashSize):
    ret = lib.sheSetRangeForDLP(hashSize)
    if ret != 0:
        raise RuntimeError("setRangeForDLP", ret)

def setTryNum(tryNum):
    ret = lib.sheSetTryNum(tryNum)
    if ret != 0:
        raise RuntimeError("setTryNum", ret)

def hexStr(v):
    s = ""
    for x in v:
        s += format(x, '02x')
    return s

class CipherTextG1(Structure):
    _fields_ = [("v", c_ulonglong * G1_CIPHER_SIZE)]
    def serialize(self):
        buf = Buffer()
        ret = lib.sheCipherTextG1Serialize(byref(buf), len(buf), byref(self.v))
        if ret == 0:
            raise RuntimeError("serialize")
        return buf[0:ret]
    def serializeToHexStr(self):
        return hexStr(self.serialize())

class CipherTextG2(Structure):
    _fields_ = [("v", c_ulonglong * G2_CIPHER_SIZE)]
    def serialize(self):
        buf = Buffer()
        ret = lib.sheCipherTextG2Serialize(byref(buf), len(buf), byref(self.v))
        if ret == 0:
            raise RuntimeError("serialize")
        return buf[0:ret]
    def serializeToHexStr(self):
        return hexStr(self.serialize())

class CipherTextGT(Structure):
    _fields_ = [("v", c_ulonglong * GT_CIPHER_SIZE)]
    def serialize(self):
        buf = Buffer()
        ret = lib.sheCipherTextGTSerialize(byref(buf), len(buf), byref(self.v))
        if ret == 0:
            raise RuntimeError("serialize")
        return buf[0:ret]
    def serializeToHexStr(self):
        return hexStr(self.serialize())

class PrecomputedPublicKey(Structure):
    def __init__(self):
        self.p = 0
    def create(self):
        if not self.p:
            self.p = c_void_p(lib.shePrecomputedPublicKeyCreate())
            if self.p == 0:
                raise RuntimeError("PrecomputedPublicKey::create")
    def destroy(self):
        lib.shePrecomputedPublicKeyDestroy(self.p)
    def encG1(self, m):
        c = CipherTextG1()
        ret = lib.shePrecomputedPublicKeyEncG1(byref(c.v), self.p, m)
        if ret != 0:
            raise RuntimeError("encG1", m)
        return c
    def encG2(self, m):
        c = CipherTextG2()
        ret = lib.shePrecomputedPublicKeyEncG2(byref(c.v), self.p, m)
        if ret != 0:
            raise RuntimeError("encG2", m)
        return c
    def encGT(self, m):
        c = CipherTextGT()
        ret = lib.shePrecomputedPublicKeyEncGT(byref(c.v), self.p, m)
        if ret != 0:
            raise RuntimeError("encGT", m)
        return c

class PublicKey(Structure):
    _fields_ = [("v", c_ulonglong * PUB_SIZE)]
    def serialize(self):
        buf = Buffer()
        ret = lib.shePublicKeySerialize(byref(buf), len(buf), byref(self.v))
        if ret == 0:
            raise RuntimeError("serialize")
        return buf[0:ret]
    def serializeToHexStr(self):
        return hexStr(self.serialize())
    def encG1(self, m):
        c = CipherTextG1()
        ret = lib.sheEncG1(byref(c.v), byref(self.v), m)
        if ret != 0:
            raise RuntimeError("encG1", m)
        return c
    def encG2(self, m):
        c = CipherTextG2()
        ret = lib.sheEncG2(byref(c.v), byref(self.v), m)
        if ret != 0:
            raise RuntimeError("encG2", m)
        return c
    def encGT(self, m):
        c = CipherTextGT()
        ret = lib.sheEncGT(byref(c.v), byref(self.v), m)
        if ret != 0:
            raise RuntimeError("encGT", m)
        return c
    def createPrecomputedPublicKey(self):
        ppub = PrecomputedPublicKey()
        ppub.create()
        ret = lib.shePrecomputedPublicKeyInit(ppub.p, byref(self.v))
        if ret != 0:
            raise RuntimeError("createPrecomputedPublicKey")
        return ppub

class SecretKey(Structure):
    _fields_ = [("v", c_ulonglong * SEC_SIZE)]
    def setByCSPRNG(self):
        ret = lib.sheSecretKeySetByCSPRNG(byref(self.v))
        if ret != 0:
            raise RuntimeError("setByCSPRNG", ret)
    def serialize(self):
        buf = Buffer()
        ret = lib.sheSecretKeySerialize(byref(buf), len(buf), byref(self.v))
        if ret == 0:
            raise RuntimeError("serialize")
        return buf[0:ret]
    def serializeToHexStr(self):
        return hexStr(self.serialize())
    def getPulicKey(self):
        pub = PublicKey()
        lib.sheGetPublicKey(byref(pub.v), byref(self.v))
        return pub
    def dec(self, c):
        m = c_longlong()
        if isinstance(c, CipherTextG1):
            ret = lib.sheDecG1(byref(m), byref(self.v), byref(c.v))
        elif isinstance(c, CipherTextG2):
            ret = lib.sheDecG2(byref(m), byref(self.v), byref(c.v))
        elif isinstance(c, CipherTextGT):
            ret = lib.sheDecGT(byref(m), byref(self.v), byref(c.v))
        if ret != 0:
            raise RuntimeError("dec")
        return m.value

def neg(c):
    ret = -1
    if isinstance(c, CipherTextG1):
        out = CipherTextG1()
        ret = lib.sheNegG1(byref(out.v), byref(c.v))
    elif isinstance(c, CipherTextG2):
        out = CipherTextG2()
        ret = lib.sheNegG2(byref(out.v), byref(c.v))
    elif isinstance(c, CipherTextGT):
        out = CipherTextGT()
        ret = lib.sheNegGT(byref(out.v), byref(c.v))
    if ret != 0:
        raise RuntimeError("neg")
    return out

def add(cx, cy):
    ret = -1
    if isinstance(cx, CipherTextG1) and isinstance(cy, CipherTextG1):
        out = CipherTextG1()
        ret = lib.sheAddG1(byref(out.v), byref(cx.v), byref(cy.v))
    elif isinstance(cx, CipherTextG2) and isinstance(cy, CipherTextG2):
        out = CipherTextG2()
        ret = lib.sheAddG2(byref(out.v), byref(cx.v), byref(cy.v))
    elif isinstance(cx, CipherTextGT) and isinstance(cy, CipherTextGT):
        out = CipherTextGT()
        ret = lib.sheAddGT(byref(out.v), byref(cx.v), byref(cy.v))
    if ret != 0:
        raise RuntimeError("add")
    return out

def sub(cx, cy):
    ret = -1
    if isinstance(cx, CipherTextG1) and isinstance(cy, CipherTextG1):
        out = CipherTextG1()
        ret = lib.sheSubG1(byref(out.v), byref(cx.v), byref(cy.v))
    elif isinstance(cx, CipherTextG2) and isinstance(cy, CipherTextG2):
        out = CipherTextG2()
        ret = lib.sheSubG2(byref(out.v), byref(cx.v), byref(cy.v))
    elif isinstance(cx, CipherTextGT) and isinstance(cy, CipherTextGT):
        out = CipherTextGT()
        ret = lib.sheSubGT(byref(out.v), byref(cx.v), byref(cy.v))
    if ret != 0:
        raise RuntimeError("sub")
    return out

def mul(cx, cy):
    ret = -1
    if isinstance(cx, CipherTextG1) and isinstance(cy, CipherTextG2):
        out = CipherTextGT()
        ret = lib.sheMul(byref(out.v), byref(cx.v), byref(cy.v))
    elif isinstance(cx, CipherTextG1) and isinstance(cy, int):
        out = CipherTextG1()
        ret = lib.sheMulG1(byref(out.v), byref(cx.v), cy)
    elif isinstance(cx, CipherTextG2) and isinstance(cy, int):
        out = CipherTextG2()
        ret = lib.sheMulG2(byref(out.v), byref(cx.v), cy)
    elif isinstance(cx, CipherTextGT) and isinstance(cy, int):
        out = CipherTextGT()
        ret = lib.sheMulGT(byref(out.v), byref(cx.v), cy)
    if ret != 0:
        raise RuntimeError("mul")
    return out

if __name__ == '__main__':
    init()
    sec = SecretKey()
    sec.setByCSPRNG()
    print("sec=", sec.serializeToHexStr())
    pub = sec.getPulicKey()
    print("pub=", pub.serializeToHexStr())

    m11 = 1
    m12 = 5
    m21 = 3
    m22 = -4
    c11 = pub.encG1(m11)
    c12 = pub.encG1(m12)
    # dec(enc) for G1
    if sec.dec(c11) != m11: print("err1")

    # add/sub for G1
    if sec.dec(add(c11, c12)) != m11 + m12: print("err2")
    if sec.dec(sub(c11, c12)) != m11 - m12: print("err3")

    # add/sub for G2
    c21 = pub.encG2(m21)
    c22 = pub.encG2(m22)
    if sec.dec(c21) != m21: print("err4")
    if sec.dec(add(c21, c22)) != m21 + m22: print("err5")
    if sec.dec(sub(c21, c22)) != m21 - m22: print("err6")

    mt = -56
    ct = pub.encGT(mt)
    if sec.dec(ct) != mt: print("err7")

    # mul G1 and G2
    if sec.dec(mul(c11, c21)) != m11 * m21: print("err8")

    # use precomputedPublicKey for performance
    ppub = pub.createPrecomputedPublicKey()
    c1 = ppub.encG1(m11)
    if sec.dec(c1) != m11: print("err9")

    import sys
    if sys.version_info.major >= 3:
        import timeit
        N = 100000
        print(str(timeit.timeit("pub.encG1(12)", number=N, globals=globals()) / float(N) * 1e3) + "msec")
        print(str(timeit.timeit("ppub.encG1(12)", number=N, globals=globals()) / float(N) * 1e3) + "msec")

    ppub.destroy() # necessary to avoid memory leak