aboutsummaryrefslogtreecommitdiffstats
path: root/src/bls_if.cpp
blob: 48f16b5815d08e203480ff3272ca6b968ff369cb (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
#include "bls/bls.hpp"
#define BLS_DLL_EXPORT
#include "bls/bls_if.h"
#include <iostream>
#include <sstream>
#include <memory.h>
#include <mcl/fp.hpp>

template<class Inner, class Outer>
int setStrT(Outer *p, const char *buf, size_t bufSize, int ioMode)
    try
{
    ((Inner*)p)->setStr(std::string(buf, bufSize), ioMode);
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err setStrT %s\n", e.what());
    return -1;
}

size_t checkAndCopy(char *buf, size_t maxBufSize, const std::string& s)
{
    if (s.size() > maxBufSize + 1) {
        return 0;
    }
    memcpy(buf, s.c_str(), s.size());
    buf[s.size()] = '\0';
    return s.size();
}
template<class Inner, class Outer>
size_t getStrT(const Outer *p, char *buf, size_t maxBufSize, int ioMode)
    try
{
    std::string s;
    ((const Inner*)p)->getStr(s, ioMode);
    size_t terminate = 0;
    if (ioMode == 0 || ioMode == bls::IoBin || ioMode == bls::IoDec || ioMode == bls::IoHex) {
        terminate = 1; // for '\0'
    }
    if (s.size() > maxBufSize + terminate) {
        return 0;
    }
    memcpy(buf, s.c_str(), s.size());
    if (terminate) {
        buf[s.size()] = '\0';
    }
    return s.size();
} catch (std::exception&) {
    return 0;
}

int blsInit(int curve, int maxUnitSize)
    try
{
    bls::init(curve, maxUnitSize);
    return 0;
} catch (std::exception&) {
    return -1;
}
size_t blsGetOpUnitSize()
{
    return bls::getOpUnitSize();
}

int blsGetCurveOrder(char *buf, size_t maxBufSize)
    try
{
    std::string s;
    bls::getCurveOrder(s);
    return (int)checkAndCopy(buf, maxBufSize, s);
} catch (std::exception&) {
    return 0;
}

int blsGetFieldOrder(char *buf, size_t maxBufSize)
    try
{
    std::string s;
    bls::getFieldOrder(s);
    return (int)checkAndCopy(buf, maxBufSize, s);
} catch (std::exception&) {
    return 0;
}

int blsIdIsSame(const blsId *lhs, const blsId *rhs)
{
    return *(const bls::Id*)lhs == *(const bls::Id*)rhs ? 1 : 0;
}
int blsIdSetLittleEndian(blsId *id, const void *buf, size_t bufSize)
{
    ((bls::Id*)id)->setLittleEndian(buf, bufSize);
    return 0;
}
int blsIdSetDecStr(blsId *id, const char *buf, size_t bufSize)
{
    return setStrT<bls::Id, blsId>(id, buf, bufSize, 10);
}
int blsIdSetHexStr(blsId *id, const char *buf, size_t bufSize)
{
    return setStrT<bls::Id, blsId>(id, buf, bufSize, 16);
}
size_t blsIdGetLittleEndian(void *buf, size_t maxBufSize, const blsId *id)
{
    return getStrT<bls::Id, blsId>(id, (char *)buf, maxBufSize, bls::IoFixedByteSeq);
}
size_t blsIdGetDecStr(char *buf, size_t maxBufSize, const blsId *id)
{
    return getStrT<bls::Id, blsId>(id, buf, maxBufSize, 10);
}
size_t blsIdGetHexStr(char *buf, size_t maxBufSize, const blsId *id)
{
    return getStrT<bls::Id, blsId>(id, buf, maxBufSize, 16);
}
int blsSecretKeyIsSame(const blsSecretKey *lhs, const blsSecretKey *rhs)
{
    return *(const bls::SecretKey*)lhs == *(const bls::SecretKey*)rhs ? 1 : 0;
}
int blsSecretKeySetLittleEndian(blsSecretKey *sec, const void *buf, size_t bufSize)
{
    ((bls::SecretKey*)sec)->setLittleEndian(buf, bufSize);
    return 0;
}
int blsSecretKeySetDecStr(blsSecretKey *sec, const char *buf, size_t bufSize)
{
    return setStrT<bls::SecretKey, blsSecretKey>(sec, buf, bufSize, 10);
}
int blsSecretKeySetHexStr(blsSecretKey *sec, const char *buf, size_t bufSize)
{
    return setStrT<bls::SecretKey, blsSecretKey>(sec, buf, bufSize, 16);
}
size_t blsSecretKeyGetLittleEndian(void *buf, size_t maxBufSize, const blsSecretKey *sec)
{
    return getStrT<bls::SecretKey, blsSecretKey>(sec, (char *)buf, maxBufSize, bls::IoFixedByteSeq);
}
size_t blsSecretKeyGetDecStr(char *buf, size_t maxBufSize, const blsSecretKey *sec)
{
    return getStrT<bls::SecretKey, blsSecretKey>(sec, buf, maxBufSize, 10);
}
size_t blsSecretKeyGetHexStr(char *buf, size_t maxBufSize, const blsSecretKey *sec)
{
    return getStrT<bls::SecretKey, blsSecretKey>(sec, buf, maxBufSize, 16);
}

int blsSecretKeySetByHash(blsSecretKey *sec, const void *buf, size_t bufSize)
    try
{
    std::string s = mcl::fp::hash(384, (const char *)buf, bufSize);
    return blsSecretKeySetLittleEndian(sec, s.c_str(), s.size());
} catch (std::exception& e) {
    fprintf(stderr, "err blsSecretKeySetByCSPRNG %s\n", e.what());
    return -1;
}

int blsSecretKeySetByCSPRNG(blsSecretKey *sec)
    try
{
    ((bls::SecretKey*)sec)->init();
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err blsSecretKeySetByCSPRNG %s\n", e.what());
    return -1;
}
void blsSecretKeyAdd(blsSecretKey *sec, const blsSecretKey *rhs)
{
    ((bls::SecretKey*)sec)->add(*(const bls::SecretKey*)rhs);
}

void blsGetPublicKey(blsPublicKey *pub, const blsSecretKey *sec)
{
    ((const bls::SecretKey*)sec)->getPublicKey(*(bls::PublicKey*)pub);
}
void blsSign(blsSignature *sig, const blsSecretKey *sec, const char *m, size_t size)
{
    ((const bls::SecretKey*)sec)->sign(*(bls::Signature*)sig, std::string(m, size));
}
int blsSecretKeyShare(blsSecretKey *sec, const blsSecretKey* msk, size_t k, const blsId *id)
    try
{
    ((bls::SecretKey*)sec)->set((const bls::SecretKey *)msk, k, *(const bls::Id*)id);
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err blsSecretKeyShare %s\n", e.what());
    return -1;
}

int blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const blsId *idVec, size_t n)
    try
{
    ((bls::SecretKey*)sec)->recover((const bls::SecretKey *)secVec, (const bls::Id *)idVec, n);
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err blsSecretKeyRecover %s\n", e.what());
    return -1;
}

void blsGetPop(blsSignature *sig, const blsSecretKey *sec)
{
    ((const bls::SecretKey*)sec)->getPop(*(bls::Signature*)sig);
}

int blsPublicKeyIsSame(const blsPublicKey *lhs, const blsPublicKey *rhs)
{
    return *(const bls::PublicKey*)lhs == *(const bls::PublicKey*)rhs ? 1 : 0;
}
int blsPublicKeyDeserialize(blsPublicKey *pub, const void *buf, size_t bufSize)
{
    return setStrT<bls::PublicKey, blsPublicKey>(pub, (const char*)buf, bufSize, bls::IoFixedByteSeq);
}
size_t blsPublicKeySerialize(void *buf, size_t maxBufSize, const blsPublicKey *pub)
{
    return getStrT<bls::PublicKey, blsPublicKey>(pub, (char *)buf, maxBufSize, bls::IoFixedByteSeq);
}
int blsPublicKeySetHexStr(blsPublicKey *pub, const char *buf, size_t bufSize)
    try
{
    std::string s = mcl::fp::hexStrToLittleEndian(buf, bufSize);
    return blsPublicKeyDeserialize(pub, s.c_str(), s.size());
} catch (std::exception& e) {
    fprintf(stderr, "err blsPublicKeySetHexStr %s\n", e.what());
    return -1;
}
size_t blsPublicKeyGetHexStr(char *buf, size_t maxBufSize, const blsPublicKey *pub)
{
    std::string s;
    s.resize(1024);
    size_t len = blsPublicKeySerialize(&s[0], s.size(), pub);
    if (len > 0) {
        s.resize(len);
        s = mcl::fp::littleEndianToHexStr(s.c_str(), s.size());
        if (s.size() < maxBufSize) {
            memcpy(buf, s.c_str(), s.size());
            buf[s.size()] = '\0';
            return s.size();
        }
    }
    return 0;
}
void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs)
{
    ((bls::PublicKey*)pub)->add(*(const bls::PublicKey*)rhs);
}
int blsPublicKeyShare(blsPublicKey *pub, const blsPublicKey *mpk, size_t k, const blsId *id)
    try
{
    ((bls::PublicKey*)pub)->set((const bls::PublicKey*)mpk, k, *(const bls::Id*)id);
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err blsPublicKeyShare %s\n", e.what());
    return -1;
}
int blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, size_t n)
    try
{
    ((bls::PublicKey*)pub)->recover((const bls::PublicKey*)pubVec, (const bls::Id*)idVec, n);
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err blsPublicKeyRecover %s\n", e.what());
    return -1;
}

int blsSignatureIsSame(const blsSignature *lhs, const blsSignature *rhs)
{
    return *(const bls::Signature*)lhs == *(const bls::Signature*)rhs ? 1 : 0;
}
int blsSignatureDeserialize(blsSignature *sig, const void *buf, size_t bufSize)
{
    return setStrT<bls::Signature, blsSignature>(sig, (const char *)buf, bufSize, bls::IoFixedByteSeq);
}
int blsSignatureSetHexStr(blsSignature *sig, const char *buf, size_t bufSize)
    try
{
    std::string s = mcl::fp::hexStrToLittleEndian(buf, bufSize);
    return blsSignatureDeserialize(sig, s.c_str(), s.size());
} catch (std::exception& e) {
    fprintf(stderr, "err blsSignatureSetHexStr %s\n", e.what());
    return -1;
}
size_t blsSignatureGetHexStr(char *buf, size_t maxBufSize, const blsSignature *sig)
{
    std::string s;
    s.resize(1024);
    size_t len = blsSignatureSerialize(&s[0], s.size(), sig);
    if (len > 0) {
        s.resize(len);
        s = mcl::fp::littleEndianToHexStr(s.c_str(), s.size());
        if (s.size() < maxBufSize) {
            memcpy(buf, s.c_str(), s.size());
            buf[s.size()] = '\0';
            return s.size();
        }
    }
    return 0;
}
size_t blsSignatureSerialize(void *buf, size_t maxBufSize, const blsSignature *sig)
{
    return getStrT<bls::Signature, blsSignature>(sig, (char *)buf, maxBufSize, bls::IoFixedByteSeq);
}
void blsSignatureAdd(blsSignature *sig, const blsSignature *rhs)
{
    ((bls::Signature*)sig)->add(*(const bls::Signature*)rhs);
}
int blsSignatureRecover(blsSignature *sig, const blsSignature *sigVec, const blsId *idVec, size_t n)
    try
{
    ((bls::Signature*)sig)->recover((const bls::Signature*)sigVec, (const bls::Id*)idVec, n);
    return 0;
} catch (std::exception& e) {
    fprintf(stderr, "err blsSignatureRecover %s\n", e.what());
    return -1;
}

int blsVerify(const blsSignature *sig, const blsPublicKey *pub, const char *m, size_t size)
{
    return ((const bls::Signature*)sig)->verify(*(const bls::PublicKey*)pub, std::string(m, size));
}

int blsVerifyPop(const blsSignature *sig, const blsPublicKey *pub)
{
    return ((const bls::Signature*)sig)->verify(*(const bls::PublicKey*)pub);
}