aboutsummaryrefslogtreecommitdiffstats
path: root/src/she_c_impl.hpp
blob: d7b6e31e3c069c58158ef5b7939c960e6a6943d8 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <iosfwd>
#include <stdint.h>
#include <memory.h>
#include "../src/bn_c_impl.hpp"
#define MCLSHE_DLL_EXPORT

#include <mcl/she.h>
#include <mcl/she.hpp>

using namespace mcl::she;
using namespace mcl::bn_current;

#if defined(CYBOZU_CPP_VERSION) && CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11
#include <mutex>
    #define USE_STD_MUTEX
#else
#include <cybozu/mutex.hpp>
#endif

static SecretKey *cast(sheSecretKey *p) { return reinterpret_cast<SecretKey*>(p); }
static const SecretKey *cast(const sheSecretKey *p) { return reinterpret_cast<const SecretKey*>(p); }

static PublicKey *cast(shePublicKey *p) { return reinterpret_cast<PublicKey*>(p); }
static const PublicKey *cast(const shePublicKey *p) { return reinterpret_cast<const PublicKey*>(p); }

static CipherTextG1 *cast(sheCipherTextG1 *p) { return reinterpret_cast<CipherTextG1*>(p); }
static const CipherTextG1 *cast(const sheCipherTextG1 *p) { return reinterpret_cast<const CipherTextG1*>(p); }

static CipherTextG2 *cast(sheCipherTextG2 *p) { return reinterpret_cast<CipherTextG2*>(p); }
static const CipherTextG2 *cast(const sheCipherTextG2 *p) { return reinterpret_cast<const CipherTextG2*>(p); }

static CipherTextGT *cast(sheCipherTextGT *p) { return reinterpret_cast<CipherTextGT*>(p); }
static const CipherTextGT *cast(const sheCipherTextGT *p) { return reinterpret_cast<const CipherTextGT*>(p); }

int sheInit(int curve, int maxUnitSize)
    try
{
    if (maxUnitSize != MCLBN_FP_UNIT_SIZE) {
        fprintf(stderr, "err sheInit:maxUnitSize is mismatch %d %d\n", maxUnitSize, MCLBN_FP_UNIT_SIZE);
        return -1;
    }
#ifdef USE_STD_MUTEX
    static std::mutex m;
    std::lock_guard<std::mutex> lock(m);
#else
    static cybozu::Mutex m;
    cybozu::AutoLock lock(m);
#endif
    static int g_curve = -1;
    if (g_curve == curve) return 0;

    mcl::bn::CurveParam cp;
    switch (curve) {
    case mclBn_CurveFp254BNb:
        cp = mcl::bn::CurveFp254BNb;
        break;
    case mclBn_CurveFp382_1:
        cp = mcl::bn::CurveFp382_1;
        break;
    case mclBn_CurveFp382_2:
        cp = mcl::bn::CurveFp382_2;
        break;
    case mclBn_CurveFp462:
        cp = mcl::bn::CurveFp462;
        break;
    default:
        fprintf(stderr, "err bad curve %d\n", curve);
        return -1;
    }
    SHE::init(cp);
    g_curve = curve;
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err sheInit %s\n", e.what());
    return -1;
}

size_t sheSecretKeySerialize(void *buf, size_t maxBufSize, const sheSecretKey *sec)
{
    char *p = (char *)buf;
    size_t n = mclBnFr_serialize(p, maxBufSize, &sec->x);
    if (n == 0) return 0;
    return n += mclBnFr_serialize(p + n, maxBufSize - n, &sec->y);
}

size_t shePublicKeySerialize(void *buf, size_t maxBufSize, const shePublicKey *pub)
{
    char *p = (char *)buf;
    size_t n = mclBnG1_serialize(p, maxBufSize, &pub->xP);
    if (n == 0) return 0;
    return n += mclBnG2_serialize(p + n, maxBufSize - n, &pub->yQ);
}

size_t sheCipherTextG1Serialize(void *buf, size_t maxBufSize, const sheCipherTextG1 *c)
{
    char *p = (char *)buf;
    size_t n = mclBnG1_serialize(p, maxBufSize, &c->S);
    if (n == 0) return 0;
    return n += mclBnG1_serialize(p + n, maxBufSize - n, &c->T);
}

size_t sheCipherTextG2Serialize(void *buf, size_t maxBufSize, const sheCipherTextG2 *c)
{
    char *p = (char *)buf;
    size_t n = mclBnG2_serialize(p, maxBufSize, &c->S);
    if (n == 0) return 0;
    return n += mclBnG2_serialize(p + n, maxBufSize - n, &c->T);
}

size_t sheCipherTextGTSerialize(void *buf, size_t maxBufSize, const sheCipherTextGT *c)
{
    char *p = (char *)buf;
    size_t n = 0;
    for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(c->g); i++) {
        size_t r = mclBnGT_serialize(p + n, maxBufSize - n, &c->g[i]);
        if (r == 0) return 0;
        n += r;
    }
    return n;
}

int sheSecretKeyDeserialize(sheSecretKey* sec, const void *buf, size_t bufSize)
{
    const char *p = (const char *)buf;
    if (mclBnFr_deserialize(&sec->x, p, bufSize)) return -1;
    const size_t size = Fr::getByteSize();
    return mclBnFr_deserialize(&sec->y, p + size, bufSize - size);
}

int shePublicKeyDeserialize(shePublicKey* sec, const void *buf, size_t bufSize)
{
    const char *p = (const char *)buf;
    if (mclBnG1_deserialize(&sec->xP, p, bufSize)) return -1;
    const size_t size = Fr::getByteSize();
    return mclBnG2_deserialize(&sec->yQ, p + size, bufSize - size);
}

int sheCipherTextG1Deserialize(sheCipherTextG1* c, const void *buf, size_t bufSize)
{
    const char *p = (const char *)buf;
    if (mclBnG1_deserialize(&c->S, p, bufSize)) return -1;
    const size_t size = Fr::getByteSize();
    return mclBnG1_deserialize(&c->T, p + size, bufSize - size);
}

int sheCipherTextG2Deserialize(sheCipherTextG2* c, const void *buf, size_t bufSize)
{
    const char *p = (const char *)buf;
    if (mclBnG2_deserialize(&c->S, p, bufSize)) return -1;
    const size_t size = Fr::getByteSize() * 2;
    return mclBnG2_deserialize(&c->T, p + size, bufSize - size);
}

int sheCipherTextGTDeserialize(sheCipherTextGT* c, const void *buf, size_t bufSize)
{
    const char *p = (const char *)buf;
    const size_t size = Fr::getByteSize() * 12;
    for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(c->g); i++) {
        if (mclBnGT_deserialize(&c->g[i], p + size * i, bufSize - size * i)) return -1;
    }
    return 0;
}

int sheSecretKeySetByCSPRNG(sheSecretKey *sec)
    try
{
    cast(sec)->setByCSPRNG();
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err %s\n", e.what());
    return -1;
}

void sheGetPublicKey(shePublicKey *pub, const sheSecretKey *sec)
{
    cast(sec)->getPublicKey(*cast(pub));
}

static int setRangeForDLP(void (*f)(size_t, size_t), size_t hashSize, size_t tryNum)
    try
{
    f(hashSize, tryNum);
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err %s\n", e.what());
    return -1;
}

int sheSetRangeForDLP(size_t hashSize, size_t tryNum)
{
    return setRangeForDLP(SHE::setRangeForDLP, hashSize, tryNum);
}
int sheSetRangeForG1DLP(size_t hashSize, size_t tryNum)
{
    return setRangeForDLP(SHE::setRangeForG1DLP, hashSize, tryNum);
}
int sheSetRangeForG2DLP(size_t hashSize, size_t tryNum)
{
    return setRangeForDLP(SHE::setRangeForG2DLP, hashSize, tryNum);
}
int sheSetRangeForGTDLP(size_t hashSize, size_t tryNum)
{
    return setRangeForDLP(SHE::setRangeForGTDLP, hashSize, tryNum);
}

template<class CT>
int encT(CT *c, const shePublicKey *pub, int64_t m)
    try
{
    cast(pub)->enc(*cast(c), m);
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err %s\n", e.what());
    return -1;
}

int sheEncG1(sheCipherTextG1 *c, const shePublicKey *pub, int64_t m)
{
    return encT(c, pub, m);
}

int sheEncG2(sheCipherTextG2 *c, const shePublicKey *pub, int64_t m)
{
    return encT(c, pub, m);
}

int sheEncGT(sheCipherTextGT *c, const shePublicKey *pub, int64_t m)
{
    return encT(c, pub, m);
}

int sheEnc32G1(sheCipherTextG1 *c, const shePublicKey *pub, int m)
{
    return encT(c, pub, m);
}

int sheEnc32G2(sheCipherTextG2 *c, const shePublicKey *pub, int m)
{
    return encT(c, pub, m);
}

int sheEnc32GT(sheCipherTextGT *c, const shePublicKey *pub, int m)
{
    return encT(c, pub, m);
}

template<class CT>
int decT(int64_t *m, const sheSecretKey *sec, const CT *c)
    try
{
    *m = cast(sec)->dec(*cast(c));
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err %s\n", e.what());
    return -1;
}

int sheDecG1(int64_t *m, const sheSecretKey *sec, const sheCipherTextG1 *c)
{
    return decT(m, sec, c);
}

int sheDecG2(int64_t *m, const sheSecretKey *sec, const sheCipherTextG2 *c)
{
    return decT(m, sec, c);
}

int sheDecGT(int64_t *m, const sheSecretKey *sec, const sheCipherTextGT *c)
{
    return decT(m, sec, c);
}

template<class CT>
int isZeroT(const sheSecretKey *sec, const CT *c)
    try
{
    return cast(sec)->isZero(*cast(c));
} catch (std::exception& e) {
    fprintf(stderr, "err %s\n", e.what());
    return 0;
}

int sheIsZeroG1(const sheSecretKey *sec, const sheCipherTextG1 *c)
{
    return isZeroT(sec, c);
}
int sheIsZeroG2(const sheSecretKey *sec, const sheCipherTextG2 *c)
{
    return isZeroT(sec, c);
}
int sheIsZeroGT(const sheSecretKey *sec, const sheCipherTextGT *c)
{
    return isZeroT(sec, c);
}


template<class CT>
int addT(CT& z, const CT& x, const CT& y)
    try
{
    CT::add(z, x, y);
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err %s\n", e.what());
    return -1;
}

int sheAddG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, const sheCipherTextG1 *y)
{
    return addT(*cast(z), *cast(x), *cast(y));
}

int sheAddG2(sheCipherTextG2 *z, const sheCipherTextG2 *x, const sheCipherTextG2 *y)
{
    return addT(*cast(z), *cast(x), *cast(y));
}

int sheAddGT(sheCipherTextGT *z, const sheCipherTextGT *x, const sheCipherTextGT *y)
{
    return addT(*cast(z), *cast(x), *cast(y));
}

template<class CT>
int subT(CT& z, const CT& x, const CT& y)
    try
{
    CT::sub(z, x, y);
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err %s\n", e.what());
    return -1;
}

int sheSubG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, const sheCipherTextG1 *y)
{
    return subT(*cast(z), *cast(x), *cast(y));
}

int sheSubG2(sheCipherTextG2 *z, const sheCipherTextG2 *x, const sheCipherTextG2 *y)
{
    return subT(*cast(z), *cast(x), *cast(y));
}

int sheSubGT(sheCipherTextGT *z, const sheCipherTextGT *x, const sheCipherTextGT *y)
{
    return subT(*cast(z), *cast(x), *cast(y));
}

template<class CT1, class CT2, class CT3>
int mulT(CT1& z, const CT2& x, const CT3& y)
    try
{
    CT1::mul(z, x, y);
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err %s\n", e.what());
    return -1;
}

int sheMulG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, int64_t y)
{
    return mulT(*cast(z), *cast(x), y);
}

int sheMulG2(sheCipherTextG2 *z, const sheCipherTextG2 *x, int64_t y)
{
    return mulT(*cast(z), *cast(x), y);
}

int sheMulGT(sheCipherTextGT *z, const sheCipherTextGT *x, int64_t y)
{
    return mulT(*cast(z), *cast(x), y);
}

int sheMul32G1(sheCipherTextG1 *z, const sheCipherTextG1 *x, int y)
{
    return mulT(*cast(z), *cast(x), y);
}

int sheMul32G2(sheCipherTextG2 *z, const sheCipherTextG2 *x, int y)
{
    return mulT(*cast(z), *cast(x), y);
}

int sheMul32GT(sheCipherTextGT *z, const sheCipherTextGT *x, int y)
{
    return mulT(*cast(z), *cast(x), y);
}

int sheMul(sheCipherTextGT *z, const sheCipherTextG1 *x, const sheCipherTextG2 *y)
{
    return mulT(*cast(z), *cast(x), *cast(y));
}

template<class CT>
int reRandT(CT& c, const shePublicKey *pub)
    try
{
    cast(pub)->reRand(c);
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err %s\n", e.what());
    return -1;
}

int sheReRandG1(sheCipherTextG1 *c, const shePublicKey *pub)
{
    return reRandT(*cast(c), pub);
}

int sheReRandG2(sheCipherTextG2 *c, const shePublicKey *pub)
{
    return reRandT(*cast(c), pub);
}

int sheReRandGT(sheCipherTextGT *c, const shePublicKey *pub)
{
    return reRandT(*cast(c), pub);
}

template<class CT>
int convert(sheCipherTextGT *y, const shePublicKey *pub, const CT *x)
    try
{
    cast(pub)->convert(*cast(y), *cast(x));
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err %s\n", e.what());
    return -1;
}

int sheConvertG1(sheCipherTextGT *y, const shePublicKey *pub, const sheCipherTextG1 *x)
{
    return convert(y, pub, x);
}

int sheConvertG2(sheCipherTextGT *y, const shePublicKey *pub, const sheCipherTextG2 *x)
{
    return convert(y, pub, x);
}