aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/byzantine-lab/mcl/src/bn_c_impl.hpp
blob: bb0b4ba8e88a3d88ac3d66a98cb5f01dc24e9f10 (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
#define MCLBN_DLL_EXPORT
#include <mcl/bn.h>

#if MCLBN_FP_UNIT_SIZE == 4 && MCLBN_FR_UNIT_SIZE == 4
#include <mcl/bn256.hpp>
#elif MCLBN_FP_UNIT_SIZE == 6 && MCLBN_FR_UNIT_SIZE == 6
#include <mcl/bn384.hpp>
#elif MCLBN_FP_UNIT_SIZE == 6 && MCLBN_FR_UNIT_SIZE == 4
#include <mcl/bls12_381.hpp>
#elif MCLBN_FP_UNIT_SIZE == 8 && MCLBN_FR_UNIT_SIZE == 8
#include <mcl/bn512.hpp>
#else
    #error "not supported size"
#endif
#include <mcl/lagrange.hpp>
using namespace mcl::bn;

static Fr *cast(mclBnFr *p) { return reinterpret_cast<Fr*>(p); }
static const Fr *cast(const mclBnFr *p) { return reinterpret_cast<const Fr*>(p); }

static G1 *cast(mclBnG1 *p) { return reinterpret_cast<G1*>(p); }
static const G1 *cast(const mclBnG1 *p) { return reinterpret_cast<const G1*>(p); }

static G2 *cast(mclBnG2 *p) { return reinterpret_cast<G2*>(p); }
static const G2 *cast(const mclBnG2 *p) { return reinterpret_cast<const G2*>(p); }

static Fp12 *cast(mclBnGT *p) { return reinterpret_cast<Fp12*>(p); }
static const Fp12 *cast(const mclBnGT *p) { return reinterpret_cast<const Fp12*>(p); }

static Fp6 *cast(uint64_t *p) { return reinterpret_cast<Fp6*>(p); }
static const Fp6 *cast(const uint64_t *p) { return reinterpret_cast<const Fp6*>(p); }

template<class T>
int setStr(T *x, const char *buf, mclSize bufSize, int ioMode)
{
    size_t n = cast(x)->deserialize(buf, bufSize, ioMode);
    return n > 0 ? 0 : -1;
}

#ifdef __EMSCRIPTEN__
// use these functions forcibly
extern "C" MCLBN_DLL_API void *mclBnMalloc(size_t n)
{
    return malloc(n);
}
extern "C" MCLBN_DLL_API void mclBnFree(void *p)
{
    free(p);
}
#endif

int mclBn_init(int curve, int compiledTimeVar)
{
    if (compiledTimeVar != MCLBN_COMPILED_TIME_VAR) {
        return -(compiledTimeVar | (MCLBN_COMPILED_TIME_VAR * 100));
    }
    const mcl::CurveParam& cp = mcl::getCurveParam(curve);
    bool b;
    initPairing(&b, cp);
    return b ? 0 : -1;
}

int mclBn_getOpUnitSize()
{
    return (int)Fp::getUnitSize() * sizeof(mcl::fp::Unit) / sizeof(uint64_t);
}

int mclBn_getG1ByteSize()
{
    return (int)Fp::getByteSize();
}

int mclBn_getFrByteSize()
{
    return (int)Fr::getByteSize();
}

mclSize mclBn_getCurveOrder(char *buf, mclSize maxBufSize)
{
    return Fr::getModulo(buf, maxBufSize);
}

mclSize mclBn_getFieldOrder(char *buf, mclSize maxBufSize)
{
    return Fp::getModulo(buf, maxBufSize);
}
////////////////////////////////////////////////
// set zero
void mclBnFr_clear(mclBnFr *x)
{
    cast(x)->clear();
}

// set x to y
void mclBnFr_setInt(mclBnFr *y, mclInt x)
{
    *cast(y) = x;
}
void mclBnFr_setInt32(mclBnFr *y, int x)
{
    *cast(y) = x;
}

int mclBnFr_setStr(mclBnFr *x, const char *buf, mclSize bufSize, int ioMode)
{
    return setStr(x, buf, bufSize, ioMode);
}
int mclBnFr_setLittleEndian(mclBnFr *x, const void *buf, mclSize bufSize)
{
    cast(x)->setArrayMask((const char *)buf, bufSize);
    return 0;
}
mclSize mclBnFr_deserialize(mclBnFr *x, const void *buf, mclSize bufSize)
{
    return (mclSize)cast(x)->deserialize(buf, bufSize);
}
// return 1 if true
int mclBnFr_isValid(const mclBnFr *x)
{
    return cast(x)->isValid();
}
int mclBnFr_isEqual(const mclBnFr *x, const mclBnFr *y)
{
    return *cast(x) == *cast(y);
}
int mclBnFr_isZero(const mclBnFr *x)
{
    return cast(x)->isZero();
}
int mclBnFr_isOne(const mclBnFr *x)
{
    return cast(x)->isOne();
}

#ifndef MCL_DONT_USE_CSRPNG
int mclBnFr_setByCSPRNG(mclBnFr *x)
{
    cast(x)->setByCSPRNG();
    return 0;
}
#endif

// hash(buf) and set x
int mclBnFr_setHashOf(mclBnFr *x, const void *buf, mclSize bufSize)
{
    cast(x)->setHashOf(buf, bufSize);
    return 0;
}

mclSize mclBnFr_getStr(char *buf, mclSize maxBufSize, const mclBnFr *x, int ioMode)
{
    return cast(x)->getStr(buf, maxBufSize, ioMode);
}
mclSize mclBnFr_serialize(void *buf, mclSize maxBufSize, const mclBnFr *x)
{
    return (mclSize)cast(x)->serialize(buf, maxBufSize);
}

void mclBnFr_neg(mclBnFr *y, const mclBnFr *x)
{
    Fr::neg(*cast(y), *cast(x));
}
void mclBnFr_inv(mclBnFr *y, const mclBnFr *x)
{
    Fr::inv(*cast(y), *cast(x));
}
void mclBnFr_sqr(mclBnFr *y, const mclBnFr *x)
{
    Fr::sqr(*cast(y), *cast(x));
}
void mclBnFr_add(mclBnFr *z, const mclBnFr *x, const mclBnFr *y)
{
    Fr::add(*cast(z),*cast(x), *cast(y));
}
void mclBnFr_sub(mclBnFr *z, const mclBnFr *x, const mclBnFr *y)
{
    Fr::sub(*cast(z),*cast(x), *cast(y));
}
void mclBnFr_mul(mclBnFr *z, const mclBnFr *x, const mclBnFr *y)
{
    Fr::mul(*cast(z),*cast(x), *cast(y));
}
void mclBnFr_div(mclBnFr *z, const mclBnFr *x, const mclBnFr *y)
{
    Fr::div(*cast(z),*cast(x), *cast(y));
}

////////////////////////////////////////////////
// set zero
void mclBnG1_clear(mclBnG1 *x)
{
    cast(x)->clear();
}

int mclBnG1_setStr(mclBnG1 *x, const char *buf, mclSize bufSize, int ioMode)
{
    return setStr(x, buf, bufSize, ioMode);
}
mclSize mclBnG1_deserialize(mclBnG1 *x, const void *buf, mclSize bufSize)
{
    return (mclSize)cast(x)->deserialize(buf, bufSize);
}

// return 1 if true
int mclBnG1_isValid(const mclBnG1 *x)
{
    return cast(x)->isValid();
}
int mclBnG1_isEqual(const mclBnG1 *x, const mclBnG1 *y)
{
    return *cast(x) == *cast(y);
}
int mclBnG1_isZero(const mclBnG1 *x)
{
    return cast(x)->isZero();
}
int mclBnG1_isValidOrder(const mclBnG1 *x)
{
    return cast(x)->isValidOrder();
}

int mclBnG1_hashAndMapTo(mclBnG1 *x, const void *buf, mclSize bufSize)
{
    hashAndMapToG1(*cast(x), buf, bufSize);
    return 0;
}

mclSize mclBnG1_getStr(char *buf, mclSize maxBufSize, const mclBnG1 *x, int ioMode)
{
    return cast(x)->getStr(buf, maxBufSize, ioMode);
}

mclSize mclBnG1_serialize(void *buf, mclSize maxBufSize, const mclBnG1 *x)
{
    return (mclSize)cast(x)->serialize(buf, maxBufSize);
}

void mclBnG1_neg(mclBnG1 *y, const mclBnG1 *x)
{
    G1::neg(*cast(y), *cast(x));
}
void mclBnG1_dbl(mclBnG1 *y, const mclBnG1 *x)
{
    G1::dbl(*cast(y), *cast(x));
}
void mclBnG1_normalize(mclBnG1 *y, const mclBnG1 *x)
{
    G1::normalize(*cast(y), *cast(x));
}
void mclBnG1_add(mclBnG1 *z, const mclBnG1 *x, const mclBnG1 *y)
{
    G1::add(*cast(z),*cast(x), *cast(y));
}
void mclBnG1_sub(mclBnG1 *z, const mclBnG1 *x, const mclBnG1 *y)
{
    G1::sub(*cast(z),*cast(x), *cast(y));
}
void mclBnG1_mul(mclBnG1 *z, const mclBnG1 *x, const mclBnFr *y)
{
    G1::mul(*cast(z),*cast(x), *cast(y));
}
void mclBnG1_mulCT(mclBnG1 *z, const mclBnG1 *x, const mclBnFr *y)
{
    G1::mulCT(*cast(z),*cast(x), *cast(y));
}

////////////////////////////////////////////////
// set zero
void mclBnG2_clear(mclBnG2 *x)
{
    cast(x)->clear();
}

int mclBnG2_setStr(mclBnG2 *x, const char *buf, mclSize bufSize, int ioMode)
{
    return setStr(x, buf, bufSize, ioMode);
}
mclSize mclBnG2_deserialize(mclBnG2 *x, const void *buf, mclSize bufSize)
{
    return (mclSize)cast(x)->deserialize(buf, bufSize);
}

// return 1 if true
int mclBnG2_isValid(const mclBnG2 *x)
{
    return cast(x)->isValid();
}
int mclBnG2_isEqual(const mclBnG2 *x, const mclBnG2 *y)
{
    return *cast(x) == *cast(y);
}
int mclBnG2_isZero(const mclBnG2 *x)
{
    return cast(x)->isZero();
}
int mclBnG2_isValidOrder(const mclBnG2 *x)
{
    return cast(x)->isValidOrder();
}

int mclBnG2_hashAndMapTo(mclBnG2 *x, const void *buf, mclSize bufSize)
{
    hashAndMapToG2(*cast(x), buf, bufSize);
    return 0;
}

mclSize mclBnG2_getStr(char *buf, mclSize maxBufSize, const mclBnG2 *x, int ioMode)
{
    return cast(x)->getStr(buf, maxBufSize, ioMode);
}

mclSize mclBnG2_serialize(void *buf, mclSize maxBufSize, const mclBnG2 *x)
{
    return (mclSize)cast(x)->serialize(buf, maxBufSize);
}

void mclBnG2_neg(mclBnG2 *y, const mclBnG2 *x)
{
    G2::neg(*cast(y), *cast(x));
}
void mclBnG2_dbl(mclBnG2 *y, const mclBnG2 *x)
{
    G2::dbl(*cast(y), *cast(x));
}
void mclBnG2_normalize(mclBnG2 *y, const mclBnG2 *x)
{
    G2::normalize(*cast(y), *cast(x));
}
void mclBnG2_add(mclBnG2 *z, const mclBnG2 *x, const mclBnG2 *y)
{
    G2::add(*cast(z),*cast(x), *cast(y));
}
void mclBnG2_sub(mclBnG2 *z, const mclBnG2 *x, const mclBnG2 *y)
{
    G2::sub(*cast(z),*cast(x), *cast(y));
}
void mclBnG2_mul(mclBnG2 *z, const mclBnG2 *x, const mclBnFr *y)
{
    G2::mul(*cast(z),*cast(x), *cast(y));
}
void mclBnG2_mulCT(mclBnG2 *z, const mclBnG2 *x, const mclBnFr *y)
{
    G2::mulCT(*cast(z),*cast(x), *cast(y));
}

////////////////////////////////////////////////
// set zero
void mclBnGT_clear(mclBnGT *x)
{
    cast(x)->clear();
}
void mclBnGT_setInt(mclBnGT *y, mclInt x)
{
    cast(y)->clear();
    *(cast(y)->getFp0()) = x;
}
void mclBnGT_setInt32(mclBnGT *y, int x)
{
    cast(y)->clear();
    *(cast(y)->getFp0()) = x;
}

int mclBnGT_setStr(mclBnGT *x, const char *buf, mclSize bufSize, int ioMode)
{
    return setStr(x, buf, bufSize, ioMode);
}
mclSize mclBnGT_deserialize(mclBnGT *x, const void *buf, mclSize bufSize)
{
    return (mclSize)cast(x)->deserialize(buf, bufSize);
}

// return 1 if true
int mclBnGT_isEqual(const mclBnGT *x, const mclBnGT *y)
{
    return *cast(x) == *cast(y);
}
int mclBnGT_isZero(const mclBnGT *x)
{
    return cast(x)->isZero();
}
int mclBnGT_isOne(const mclBnGT *x)
{
    return cast(x)->isOne();
}

mclSize mclBnGT_getStr(char *buf, mclSize maxBufSize, const mclBnGT *x, int ioMode)
{
    return cast(x)->getStr(buf, maxBufSize, ioMode);
}

mclSize mclBnGT_serialize(void *buf, mclSize maxBufSize, const mclBnGT *x)
{
    return (mclSize)cast(x)->serialize(buf, maxBufSize);
}

void mclBnGT_neg(mclBnGT *y, const mclBnGT *x)
{
    Fp12::neg(*cast(y), *cast(x));
}
void mclBnGT_inv(mclBnGT *y, const mclBnGT *x)
{
    Fp12::inv(*cast(y), *cast(x));
}
void mclBnGT_sqr(mclBnGT *y, const mclBnGT *x)
{
    Fp12::sqr(*cast(y), *cast(x));
}
void mclBnGT_add(mclBnGT *z, const mclBnGT *x, const mclBnGT *y)
{
    Fp12::add(*cast(z),*cast(x), *cast(y));
}
void mclBnGT_sub(mclBnGT *z, const mclBnGT *x, const mclBnGT *y)
{
    Fp12::sub(*cast(z),*cast(x), *cast(y));
}
void mclBnGT_mul(mclBnGT *z, const mclBnGT *x, const mclBnGT *y)
{
    Fp12::mul(*cast(z),*cast(x), *cast(y));
}
void mclBnGT_div(mclBnGT *z, const mclBnGT *x, const mclBnGT *y)
{
    Fp12::div(*cast(z),*cast(x), *cast(y));
}

void mclBnGT_pow(mclBnGT *z, const mclBnGT *x, const mclBnFr *y)
{
    Fp12::pow(*cast(z), *cast(x), *cast(y));
}
void mclBnGT_powGeneric(mclBnGT *z, const mclBnGT *x, const mclBnFr *y)
{
    Fp12::powGeneric(*cast(z), *cast(x), *cast(y));
}

void mclBn_pairing(mclBnGT *z, const mclBnG1 *x, const mclBnG2 *y)
{
    pairing(*cast(z), *cast(x), *cast(y));
}
void mclBn_finalExp(mclBnGT *y, const mclBnGT *x)
{
    finalExp(*cast(y), *cast(x));
}
void mclBn_millerLoop(mclBnGT *z, const mclBnG1 *x, const mclBnG2 *y)
{
    millerLoop(*cast(z), *cast(x), *cast(y));
}
int mclBn_getUint64NumToPrecompute(void)
{
    return int(BN::param.precomputedQcoeffSize * sizeof(Fp6) / sizeof(uint64_t));
}

void mclBn_precomputeG2(uint64_t *Qbuf, const mclBnG2 *Q)
{
    precomputeG2(cast(Qbuf), *cast(Q));
}

void mclBn_precomputedMillerLoop(mclBnGT *f, const mclBnG1 *P, const uint64_t *Qbuf)
{
    precomputedMillerLoop(*cast(f), *cast(P), cast(Qbuf));
}

void mclBn_precomputedMillerLoop2(mclBnGT *f, const mclBnG1 *P1, const uint64_t  *Q1buf, const mclBnG1 *P2, const uint64_t *Q2buf)
{
    precomputedMillerLoop2(*cast(f), *cast(P1), cast(Q1buf), *cast(P2), cast(Q2buf));
}

void mclBn_precomputedMillerLoop2mixed(mclBnGT *f, const mclBnG1 *P1, const mclBnG2 *Q1, const mclBnG1 *P2, const uint64_t *Q2buf)
{
    precomputedMillerLoop2mixed(*cast(f), *cast(P1), *cast(Q1), *cast(P2), cast(Q2buf));
}

int mclBn_FrLagrangeInterpolation(mclBnFr *out, const mclBnFr *xVec, const mclBnFr *yVec, mclSize k)
{
    bool b;
    mcl::LagrangeInterpolation(&b, *cast(out), cast(xVec), cast(yVec), k);
    return b ? 0 : -1;
}
int mclBn_G1LagrangeInterpolation(mclBnG1 *out, const mclBnFr *xVec, const mclBnG1 *yVec, mclSize k)
{
    bool b;
    mcl::LagrangeInterpolation(&b, *cast(out), cast(xVec), cast(yVec), k);
    return b ? 0 : -1;
}
int mclBn_G2LagrangeInterpolation(mclBnG2 *out, const mclBnFr *xVec, const mclBnG2 *yVec, mclSize k)
{
    bool b;
    mcl::LagrangeInterpolation(&b, *cast(out), cast(xVec), cast(yVec), k);
    return b ? 0 : -1;
}
int mclBn_FrEvaluatePolynomial(mclBnFr *out, const mclBnFr *cVec, mclSize cSize, const mclBnFr *x)
{
    bool b;
    mcl::evaluatePolynomial(&b, *cast(out), cast(cVec), cSize, *cast(x));
    return b ? 0 : -1;
}
int mclBn_G1EvaluatePolynomial(mclBnG1 *out, const mclBnG1 *cVec, mclSize cSize, const mclBnFr *x)
{
    bool b;
    mcl::evaluatePolynomial(&b, *cast(out), cast(cVec), cSize, *cast(x));
    return b ? 0 : -1;
}
int mclBn_G2EvaluatePolynomial(mclBnG2 *out, const mclBnG2 *cVec, mclSize cSize, const mclBnFr *x)
{
    bool b;
    mcl::evaluatePolynomial(&b, *cast(out), cast(cVec), cSize, *cast(x));
    return b ? 0 : -1;
}

void mclBn_verifyOrderG1(int doVerify)
{
    verifyOrderG1(doVerify != 0);
}

void mclBn_verifyOrderG2(int doVerify)
{
    verifyOrderG2(doVerify != 0);
}