aboutsummaryrefslogtreecommitdiffstats
path: root/src/she_c_impl.hpp
blob: 9d29c67a6f9a8f457b58210fce3bd9eb6e90817f (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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#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 PrecomputedPublicKey *cast(shePrecomputedPublicKey *p) { return reinterpret_cast<PrecomputedPublicKey*>(p); }
static const PrecomputedPublicKey *cast(const shePrecomputedPublicKey *p) { return reinterpret_cast<const PrecomputedPublicKey*>(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;
}

template<class T>
mclSize serialize(void *buf, mclSize maxBufSize, const T *x)
    try
{
    return cast(x)->serialize(buf, maxBufSize);
} catch (std::exception& e) {
    fprintf(stderr, "err %s\n", e.what());
    return 0;
}

mclSize sheSecretKeySerialize(void *buf, mclSize maxBufSize, const sheSecretKey *sec)
{
    return serialize(buf, maxBufSize, sec);
}

mclSize shePublicKeySerialize(void *buf, mclSize maxBufSize, const shePublicKey *pub)
{
    return serialize(buf, maxBufSize, pub);
}

mclSize sheCipherTextG1Serialize(void *buf, mclSize maxBufSize, const sheCipherTextG1 *c)
{
    return serialize(buf, maxBufSize, c);
}

mclSize sheCipherTextG2Serialize(void *buf, mclSize maxBufSize, const sheCipherTextG2 *c)
{
    return serialize(buf, maxBufSize, c);
}

mclSize sheCipherTextGTSerialize(void *buf, mclSize maxBufSize, const sheCipherTextGT *c)
{
    return serialize(buf, maxBufSize, c);
}

template<class T>
mclSize deserialize(T *x, const void *buf, mclSize bufSize)
    try
{
    return cast(x)->deserialize(buf, bufSize);
} catch (std::exception& e) {
    fprintf(stderr, "err %s\n", e.what());
    return 0;
}

mclSize sheSecretKeyDeserialize(sheSecretKey* sec, const void *buf, mclSize bufSize)
{
    return deserialize(sec, buf, bufSize);
}

mclSize shePublicKeyDeserialize(shePublicKey* pub, const void *buf, mclSize bufSize)
{
    return deserialize(pub, buf, bufSize);
}

mclSize sheCipherTextG1Deserialize(sheCipherTextG1* c, const void *buf, mclSize bufSize)
{
    return deserialize(c, buf, bufSize);
}

mclSize sheCipherTextG2Deserialize(sheCipherTextG2* c, const void *buf, mclSize bufSize)
{
    return deserialize(c, buf, bufSize);
}

mclSize sheCipherTextGTDeserialize(sheCipherTextGT* c, const void *buf, mclSize bufSize)
{
    return deserialize(c, buf, bufSize);
}

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)(mclSize), mclSize hashSize)
    try
{
    f(hashSize);
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err %s\n", e.what());
    return -1;
}

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

void sheSetTryNum(mclSize tryNum)
{
    SHE::setTryNum(tryNum);
}
void sheUseDecG1ViaGT(int use)
{
    SHE::useDecG1ViaGT(use != 0);
}
void sheUseDecG2ViaGT(int use)
{
    SHE::useDecG2ViaGT(use != 0);
}

template<class HashTable>
mclSize loadTable(HashTable& table, const void *buf, mclSize bufSize)
    try
{
    return table.load(buf, bufSize);
} catch (std::exception& e) {
    fprintf(stderr, "err %s\n", e.what());
    return 0;
}

mclSize sheLoadTableForG1DLP(const void *buf, mclSize bufSize)
{
    return loadTable(SHE::PhashTbl_, buf, bufSize);
}
mclSize sheLoadTableForG2DLP(const void *buf, mclSize bufSize)
{
    return loadTable(SHE::QhashTbl_, buf, bufSize);
}
mclSize sheLoadTableForGTDLP(const void *buf, mclSize bufSize)
{
    return loadTable(SHE::ePQhashTbl_, buf, bufSize);
}

template<class HashTable>
mclSize saveTable(void *buf, mclSize maxBufSize, const HashTable& table)
    try
{
    return table.save(buf, maxBufSize);
} catch (std::exception& e) {
    fprintf(stderr, "err %s\n", e.what());
    return 0;
}
mclSize sheSaveTableForG1DLP(void *buf, mclSize maxBufSize)
{
    return saveTable(buf, maxBufSize, SHE::PhashTbl_);
}
mclSize sheSaveTableForG2DLP(void *buf, mclSize maxBufSize)
{
    return saveTable(buf, maxBufSize, SHE::QhashTbl_);
}
mclSize sheSaveTableForGTDLP(void *buf, mclSize maxBufSize)
{
    return saveTable(buf, maxBufSize, SHE::ePQhashTbl_);
}

template<class CT>
int encT(CT *c, const shePublicKey *pub, mclInt 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, mclInt m)
{
    return encT(c, pub, m);
}

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

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

template<class CT>
int decT(mclInt *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(mclInt *m, const sheSecretKey *sec, const sheCipherTextG1 *c)
{
    return decT(m, sec, c);
}

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

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

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

int sheDecG1ViaGT(mclInt *m, const sheSecretKey *sec, const sheCipherTextG1 *c)
{
    return decViaGTT(m, sec, c);
}

int sheDecG2ViaGT(mclInt *m, const sheSecretKey *sec, const sheCipherTextG2 *c)
{
    return decViaGTT(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, mclInt y)
{
    return mulT(*cast(z), *cast(x), y);
}

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

int sheMulGT(sheCipherTextGT *z, const sheCipherTextGT *x, mclInt 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));
}

int sheMulML(sheCipherTextGT *z, const sheCipherTextG1 *x, const sheCipherTextG2 *y)
    try
{
    CipherTextGT::mulML(*cast(z), *cast(x), *cast(y));
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err %s\n", e.what());
    return -1;
}

int sheFinalExpGT(sheCipherTextGT *y, const sheCipherTextGT *x)
    try
{
    CipherTextGT::finalExp(*cast(y), *cast(x));
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err %s\n", e.what());
    return -1;
}

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);
}

shePrecomputedPublicKey *shePrecomputedPublicKeyCreate()
    try
{
    return reinterpret_cast<shePrecomputedPublicKey*>(new PrecomputedPublicKey());
} catch (...) {
    return 0;
}

void shePrecomputedPublicKeyDestroy(shePrecomputedPublicKey *ppub)
{
    delete cast(ppub);
}

int shePrecomputedPublicKeyInit(shePrecomputedPublicKey *ppub, const shePublicKey *pub)
    try
{
    cast(ppub)->init(*cast(pub));
    return 0;
} catch (...) {
    return 1;
}

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

int shePrecomputedPublicKeyEncG1(sheCipherTextG1 *c, const shePrecomputedPublicKey *pub, mclInt m)
{
    return pEncT(c, pub, m);
}

int shePrecomputedPublicKeyEncG2(sheCipherTextG2 *c, const shePrecomputedPublicKey *pub, mclInt m)
{
    return pEncT(c, pub, m);
}

int shePrecomputedPublicKeyEncGT(sheCipherTextGT *c, const shePrecomputedPublicKey *pub, mclInt m)
{
    return pEncT(c, pub, m);
}