aboutsummaryrefslogtreecommitdiffstats
path: root/test/bls_test.cpp
blob: ebd3d6c75f4a706f28236492d063e8408b762a79 (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
#include <bls/bls.hpp>
#include <cybozu/test.hpp>
#include <cybozu/inttype.hpp>
#include <iostream>
#include <sstream>
#include <cybozu/benchmark.hpp>

template<class T>
void streamTest(const T& t)
{
    std::ostringstream oss;
    oss << t;
    std::istringstream iss(oss.str());
    T t2;
    iss >> t2;
    CYBOZU_TEST_EQUAL(t, t2);
}

template<class T>
void testSet()
{
    /*
        mask value to be less than r if the value >= (1 << (192 + 62))
    */
    const uint64_t fff = uint64_t(-1);
    const uint64_t one = uint64_t(1);
    const struct {
        uint64_t in;
        uint64_t expected;
    } tbl[] = {
        { fff, (one << 61) - 1 }, // masked with (1 << 61) - 1
        { one << 62, 0 }, // masked
        { (one << 62) | (one << 61), (one << 61) }, // masked
        { (one << 61) - 1, (one << 61) - 1 }, // same
    };
    T t1, t2;
    for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
        uint64_t v1[] = { fff, fff, fff, tbl[i].in };
        uint64_t v2[] = { fff, fff, fff, tbl[i].expected };
        t1.set(v1);
        t2.set(v2);
        CYBOZU_TEST_EQUAL(t1, t2);
    }
}

void IdTestBN256()
{
    bls::Id id;
    CYBOZU_TEST_ASSERT(id.isZero());
    id = 5;
    CYBOZU_TEST_EQUAL(id, 5);
    {
        const uint64_t id1[] = { 1, 2, 3, 4 };
        id.set(id1);
        std::ostringstream os;
        os << id;
        CYBOZU_TEST_EQUAL(os.str(), "0x4000000000000000300000000000000020000000000000001");
    }
    testSet<bls::Id>();
}

void SecretKeyTestBN256()
{
    testSet<bls::SecretKey>();
}

CYBOZU_TEST_AUTO(bn256)
{
    bls::init(mclBn_CurveFp254BNb);
    IdTestBN256();
    SecretKeyTestBN256();
    CYBOZU_TEST_EQUAL(bls::getOpUnitSize(), 4);
}

void blsTest()
{
    bls::SecretKey sec;
    sec.init();
    streamTest(sec);
    bls::PublicKey pub;
    sec.getPublicKey(pub);
    streamTest(pub);
    for (int i = 0; i < 5; i++) {
        std::string m = "hello";
        m += char('0' + i);
        bls::Signature sig;
        sec.sign(sig, m);
        CYBOZU_TEST_ASSERT(sig.verify(pub, m));
        CYBOZU_TEST_ASSERT(!sig.verify(pub, m + "a"));
        streamTest(sig);
        CYBOZU_BENCH_C("sign", 100, sec.sign, sig, m);
        CYBOZU_BENCH_C("verify", 100, sig.verify, pub, m);
    }
}

void k_of_nTest()
{
    const std::string m = "abc";
    const int n = 5;
    const int k = 3;
    bls::SecretKey sec0;
    sec0.init();
    bls::Signature sig0;
    sec0.sign(sig0, m);
    bls::PublicKey pub0;
    sec0.getPublicKey(pub0);
    CYBOZU_TEST_ASSERT(sig0.verify(pub0, m));

    bls::SecretKeyVec msk;
    sec0.getMasterSecretKey(msk, k);

    bls::SecretKeyVec allPrvVec(n);
    bls::IdVec allIdVec(n);
    for (int i = 0; i < n; i++) {
        int id = i + 1;
        allPrvVec[i].set(msk, id);
        allIdVec[i] = id;

        bls::SecretKey p;
        p.set(msk.data(), k, id);
        CYBOZU_TEST_EQUAL(allPrvVec[i], p);
    }

    bls::SignatureVec allSigVec(n);
    for (int i = 0; i < n; i++) {
        CYBOZU_TEST_ASSERT(allPrvVec[i] != sec0);
        allPrvVec[i].sign(allSigVec[i], m);
        bls::PublicKey pub;
        allPrvVec[i].getPublicKey(pub);
        CYBOZU_TEST_ASSERT(pub != pub0);
        CYBOZU_TEST_ASSERT(allSigVec[i].verify(pub, m));
    }

    /*
        3-out-of-n
        can recover
    */
    bls::SecretKeyVec secVec(3);
    bls::IdVec idVec(3);
    for (int a = 0; a < n; a++) {
        secVec[0] = allPrvVec[a];
        idVec[0] = allIdVec[a];
        for (int b = a + 1; b < n; b++) {
            secVec[1] = allPrvVec[b];
            idVec[1] = allIdVec[b];
            for (int c = b + 1; c < n; c++) {
                secVec[2] = allPrvVec[c];
                idVec[2] = allIdVec[c];
                bls::SecretKey sec;
                sec.recover(secVec, idVec);
                CYBOZU_TEST_EQUAL(sec, sec0);
                bls::SecretKey sec2;
                sec2.recover(secVec.data(), idVec.data(), secVec.size());
                CYBOZU_TEST_EQUAL(sec, sec2);
            }
        }
    }
    {
        secVec[0] = allPrvVec[0];
        secVec[1] = allPrvVec[1];
        secVec[2] = allPrvVec[0]; // same of secVec[0]
        idVec[0] = allIdVec[0];
        idVec[1] = allIdVec[1];
        idVec[2] = allIdVec[0];
        bls::SecretKey sec;
        CYBOZU_TEST_EXCEPTION_MESSAGE(sec.recover(secVec, idVec), std::exception, "same id");
    }
    {
        /*
            n-out-of-n
            can recover
        */
        bls::SecretKey sec;
        sec.recover(allPrvVec, allIdVec);
        CYBOZU_TEST_EQUAL(sec, sec0);
    }
    /*
        2-out-of-n
        can't recover
    */
    secVec.resize(2);
    idVec.resize(2);
    for (int a = 0; a < n; a++) {
        secVec[0] = allPrvVec[a];
        idVec[0] = allIdVec[a];
        for (int b = a + 1; b < n; b++) {
            secVec[1] = allPrvVec[b];
            idVec[1] = allIdVec[b];
            bls::SecretKey sec;
            sec.recover(secVec, idVec);
            CYBOZU_TEST_ASSERT(sec != sec0);
        }
    }
    /*
        3-out-of-n
        can recover
    */
    bls::SignatureVec sigVec(3);
    idVec.resize(3);
    for (int a = 0; a < n; a++) {
        sigVec[0] = allSigVec[a];
        idVec[0] = allIdVec[a];
        for (int b = a + 1; b < n; b++) {
            sigVec[1] = allSigVec[b];
            idVec[1] = allIdVec[b];
            for (int c = b + 1; c < n; c++) {
                sigVec[2] = allSigVec[c];
                idVec[2] = allIdVec[c];
                bls::Signature sig;
                sig.recover(sigVec, idVec);
                CYBOZU_TEST_EQUAL(sig, sig0);
            }
        }
    }
    {
        sigVec[0] = allSigVec[1]; idVec[0] = allIdVec[1];
        sigVec[1] = allSigVec[4]; idVec[1] = allIdVec[4];
        sigVec[2] = allSigVec[3]; idVec[2] = allIdVec[3];
        bls::Signature sig;
        CYBOZU_BENCH_C("sig.recover", 100, sig.recover, sigVec, idVec);
    }
    {
        /*
            n-out-of-n
            can recover
        */
        bls::Signature sig;
        sig.recover(allSigVec, allIdVec);
        CYBOZU_TEST_EQUAL(sig, sig0);
    }
    /*
        2-out-of-n
        can't recover
    */
    sigVec.resize(2);
    idVec.resize(2);
    for (int a = 0; a < n; a++) {
        sigVec[0] = allSigVec[a];
        idVec[0] = allIdVec[a];
        for (int b = a + 1; b < n; b++) {
            sigVec[1] = allSigVec[b];
            idVec[1] = allIdVec[b];
            bls::Signature sig;
            sig.recover(sigVec, idVec);
            CYBOZU_TEST_ASSERT(sig != sig0);
        }
    }
    // share and recover publicKey
    {
        bls::PublicKeyVec pubVec(k);
        idVec.resize(k);
        // select [0, k) publicKey
        for (int i = 0; i < k; i++) {
            allPrvVec[i].getPublicKey(pubVec[i]);
            idVec[i] = allIdVec[i];
        }
        bls::PublicKey pub;
        pub.recover(pubVec, idVec);
        CYBOZU_TEST_EQUAL(pub, pub0);
        bls::PublicKey pub2;
        pub2.recover(pubVec.data(), idVec.data(), pubVec.size());
        CYBOZU_TEST_EQUAL(pub, pub2);
    }
}

void popTest()
{
    const size_t k = 3;
    const size_t n = 6;
    const std::string m = "pop test";
    bls::SecretKey sec0;
    sec0.init();
    bls::PublicKey pub0;
    sec0.getPublicKey(pub0);
    bls::Signature sig0;
    sec0.sign(sig0, m);
    CYBOZU_TEST_ASSERT(sig0.verify(pub0, m));

    bls::SecretKeyVec msk;
    sec0.getMasterSecretKey(msk, k);

    bls::PublicKeyVec mpk;
    bls::getMasterPublicKey(mpk, msk);
    bls::SignatureVec  popVec;
    bls::getPopVec(popVec, msk);

    for (size_t i = 0; i < popVec.size(); i++) {
        CYBOZU_TEST_ASSERT(popVec[i].verify(mpk[i]));
    }

    const int idTbl[n] = {
        3, 5, 193, 22, 15
    };
    bls::SecretKeyVec secVec(n);
    bls::PublicKeyVec pubVec(n);
    bls::SignatureVec sVec(n);
    for (size_t i = 0; i < n; i++) {
        int id = idTbl[i];
        secVec[i].set(msk, id);
        secVec[i].getPublicKey(pubVec[i]);
        bls::PublicKey pub;
        pub.set(mpk, id);
        CYBOZU_TEST_EQUAL(pubVec[i], pub);

        bls::Signature pop;
        secVec[i].getPop(pop);
        CYBOZU_TEST_ASSERT(pop.verify(pubVec[i]));

        secVec[i].sign(sVec[i], m);
        CYBOZU_TEST_ASSERT(sVec[i].verify(pubVec[i], m));
    }
    secVec.resize(k);
    sVec.resize(k);
    bls::IdVec idVec(k);
    for (size_t i = 0; i < k; i++) {
        idVec[i] = idTbl[i];
    }
    bls::SecretKey sec;
    sec.recover(secVec, idVec);
    CYBOZU_TEST_EQUAL(sec, sec0);
    bls::Signature sig;
    sig.recover(sVec, idVec);
    CYBOZU_TEST_EQUAL(sig, sig0);
    bls::Signature sig2;
    sig2.recover(sVec.data(), idVec.data(), sVec.size());
    CYBOZU_TEST_EQUAL(sig, sig2);
}

void addTest()
{
    bls::SecretKey sec1, sec2;
    sec1.init();
    sec2.init();
    CYBOZU_TEST_ASSERT(sec1 != sec2);

    bls::PublicKey pub1, pub2;
    sec1.getPublicKey(pub1);
    sec2.getPublicKey(pub2);

    const std::string m = "doremi";
    bls::Signature sig1, sig2;
    sec1.sign(sig1, m);
    sec2.sign(sig2, m);
    CYBOZU_TEST_ASSERT((sig1 + sig2).verify(pub1 + pub2, m));
}

void dataTest()
{
    const size_t size = bls::getOpUnitSize() * sizeof(uint64_t);
    bls::SecretKey sec;
    sec.init();
    std::string str;
    sec.getStr(str, bls::IoFixedByteSeq);
    {
        CYBOZU_TEST_EQUAL(str.size(), size);
        bls::SecretKey sec2;
        sec2.setStr(str, bls::IoFixedByteSeq);
        CYBOZU_TEST_EQUAL(sec, sec2);
    }
    bls::PublicKey pub;
    sec.getPublicKey(pub);
    pub.getStr(str, bls::IoFixedByteSeq);
    {
        CYBOZU_TEST_EQUAL(str.size(), size * 2);
        bls::PublicKey pub2;
        pub2.setStr(str, bls::IoFixedByteSeq);
        CYBOZU_TEST_EQUAL(pub, pub2);
    }
    std::string m = "abc";
    bls::Signature sign;
    sec.sign(sign, m);
    sign.getStr(str, bls::IoFixedByteSeq);
    {
        CYBOZU_TEST_EQUAL(str.size(), size);
        bls::Signature sign2;
        sign2.setStr(str, bls::IoFixedByteSeq);
        CYBOZU_TEST_EQUAL(sign, sign2);
    }
    bls::Id id;
    const uint64_t v[] = { 1, 2, 3, 4, 5, 6, };
    id.set(v);
    id.getStr(str, bls::IoFixedByteSeq);
    {
        CYBOZU_TEST_EQUAL(str.size(), size);
        bls::Id id2;
        id2.setStr(str, bls::IoFixedByteSeq);
        CYBOZU_TEST_EQUAL(id, id2);
    }
}

void testAll()
{
    blsTest();
    k_of_nTest();
    popTest();
    addTest();
    dataTest();
}
CYBOZU_TEST_AUTO(all)
{
    const struct {
        int type;
        const char *name;
    } tbl[] = {
        { mclBn_CurveFp254BNb, "Fp254" },
#if MCLBN_FP_UNIT_SIZE == 6
        { mclBn_CurveFp382_1, "Fp382_1" },
        { mclBn_CurveFp382_2, "Fp382_2" },
#endif
    };
    for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
        printf("curve=%s\n", tbl[i].name);
        bls::init(tbl[i].type);
        testAll();
    }
}