aboutsummaryrefslogtreecommitdiffstats
path: root/src/bls.cpp
blob: 6bc5fc6a21a0f9be8b50c1c49ba86673fead36c9 (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
/**
    @file
    @author MITSUNARI Shigeo(@herumi)
    @license modified new BSD license
    http://opensource.org/licenses/BSD-3-Clause
*/
#include <bls.hpp>
#include <mcl/bn.hpp>
#include <cybozu/crypto.hpp>
#include <cybozu/random_generator.hpp>
#include <vector>
#include <string>

typedef mcl::FpT<mcl::FpTag, 256> Fp;
typedef mcl::bn::BNT<Fp> BN;
typedef BN::Fp2 Fp2;
typedef BN::Fp6 Fp6;
typedef BN::Fp12 Fp12;
typedef BN::G1 G1;
typedef BN::G2 G2;
typedef std::vector<int> IntVec;

struct FrTag;
typedef mcl::FpT<FrTag, 256> Fr;
typedef std::vector<Fr> FrVec;


#define PUT(x) std::cout << #x << "=" << x << std::endl;

static cybozu::RandomGenerator& getRG()
{
    static cybozu::RandomGenerator rg;
    return rg;
}

namespace bls {

void init()
{
    BN::init(mcl::bn::CurveFp254BNb);
    G1::setCompressedExpression();
//  G2::setCompressedExpression();
    Fr::init(BN::param.r);
}

static const G2& getQ()
{
    static const G2 Q(
        Fp2("12723517038133731887338407189719511622662176727675373276651903807414909099441", "4168783608814932154536427934509895782246573715297911553964171371032945126671"),
        Fp2("13891744915211034074451795021214165905772212241412891944830863846330766296736", "7937318970632701341203597196594272556916396164729705624521405069090520231616")
    );
    return Q;
}

static void mapToG1(G1& P, const Fp& t)
{
    static mcl::bn::MapTo<Fp> mapTo;
    mapTo.calcG1(P, t);
}
static void HashAndMapToG1(G1& P, const std::string& m)
{
    std::string digest = cybozu::crypto::Hash::digest(cybozu::crypto::Hash::N_SHA256, m);
    Fp t;
    t.setArrayMask(digest.c_str(), digest.size());
    mapToG1(P, t);
}

template<class T, class G>
void evalPoly(G& y, const T& x, const std::vector<G>& c)
{
    if (c.size() < 2) throw cybozu::Exception("bls:evalPoly:bad size") << c.size();
    y = c[c.size() - 1];
    for (int i = (int)c.size() - 2; i >= 0; i--) {
        G::mul(y, y, x);
        G::add(y, y, c[i]);
    }
}

struct Polynomial {
    FrVec c; // f[x] = sum_{i=0}^{k-1} c[i] x^i
    void init(const Fr& s, int k)
    {
        if (k < 2) throw cybozu::Exception("bls:Polynomial:init:bad k") << k;
        c.resize(k);
        c[0] = s;
        for (size_t i = 1; i < c.size(); i++) {
            c[i].setRand(getRG());
        }
    }
    // y = f(id)
    void eval(Fr& y, int id) const
    {
        if (id == 0) throw cybozu::Exception("bls:Polynomial:eval:id is zero");
        evalPoly(y, Fr(id), c);
    }
};

/*
    delta_{i,S}(0) = prod_{j != i} S[j] / (S[j] - S[i]) = a / b
    where a = prod S[j], b = S[i] * prod_{j != i} (S[j] - S[i])
*/
static void calcDelta(FrVec& delta, const IntVec& S)
{
    const size_t k = S.size();
    if (k < 2) throw cybozu::Exception("bls:calcDelta:bad size") << k;
    delta.resize(k);
    Fr a = S[0];
    for (size_t i = 1; i < k; i++) {
        a *= S[i];
    }
    for (size_t i = 0; i < k; i++) {
        Fr b = S[i];
        for (size_t j = 0; j < k; j++) {
            if (j != i) {
                int v = S[j] - S[i];
                if (v == 0) throw cybozu::Exception("bls:calcDelta:S has same id") << i << j;
                b *= v;
            }
        }
        delta[i] = a / b;
    }
}

template<class G, class T>
void LagrangeInterpolation(G& r, const T& vec)
{
    IntVec S(vec.size());
    for (size_t i = 0; i < vec.size(); i++) {
        S[i] = vec[i].getId();
    }
    FrVec delta;
    calcDelta(delta, S);

    r.clear();
    G t;
    for (size_t i = 0; i < delta.size(); i++) {
        G::mul(t, vec[i].self_->get(), delta[i]);
        r += t;
    }
}

namespace impl {

struct Sign {
    G1 sHm; // s Hash(m)
    const G1& get() const { return sHm; }
};

struct PublicKey {
    G2 sQ;
    void init(const Fr& s)
    {
        G2::mul(sQ, getQ(), s);
    }
    bool verify(const Sign& sign, const std::string& m) const
    {
        G1 Hm;
        HashAndMapToG1(Hm, m); // Hm = Hash(m)
        Fp12 e1, e2;
        BN::pairing(e1, getQ(), sign.sHm); // e(Q, s Hm)
        BN::pairing(e2, sQ, Hm); // e(sQ, Hm)
        return e1 == e2;
    }
    const G2& get() const { return sQ; }
};

struct MasterPublicKey {
    std::vector<G2> vecR;
};

struct PrivateKey {
    Fr s;
    const Fr& get() const { return s; }
    void init()
    {
        s.setRand(getRG());
    }
    void getPublicKey(PublicKey& pub) const
    {
        pub.init(s);
    }
    void sign(Sign& sign, const std::string& m) const
    {
        G1 Hm;
        HashAndMapToG1(Hm, m);
        G1::mul(sign.sHm, Hm, s);
    }
};

} // mcl::bls::impl

Sign::Sign()
    : self_(new impl::Sign())
    , id_(0)
{
}

Sign::~Sign()
{
    delete self_;
}

Sign::Sign(const Sign& rhs)
    : self_(new impl::Sign(*rhs.self_))
    , id_(rhs.id_)
{
}

Sign& Sign::operator=(const Sign& rhs)
{
    *self_ = *rhs.self_;
    id_ = rhs.id_;
    return *this;
}

bool Sign::operator==(const Sign& rhs) const
{
    return id_ == rhs.id_ && self_->sHm == rhs.self_->sHm;
}

std::ostream& operator<<(std::ostream& os, const Sign& s)
{
    return os << s.id_ << ' ' << s.self_->sHm;
}

std::istream& operator>>(std::istream& os, Sign& s)
{
    return os >> s.id_ >> s.self_->sHm;
}

void Sign::recover(const std::vector<Sign>& signVec)
{
    G1 sHm;
    LagrangeInterpolation(sHm, signVec);
    self_->sHm = sHm;
    id_ = 0;
}

void Sign::add(const Sign& rhs)
{
    if (id_ != 0 || rhs.id_ != 0) throw cybozu::Exception("bls:Sign:add:bad id") << id_ << rhs.id_;
    self_->sHm += rhs.self_->sHm;
}

MasterPublicKey::MasterPublicKey()
    : self_(new impl::MasterPublicKey())
{
}

MasterPublicKey::~MasterPublicKey()
{
    delete self_;
}

MasterPublicKey::MasterPublicKey(const MasterPublicKey& rhs)
    : self_(new impl::MasterPublicKey(*rhs.self_))
{
}

MasterPublicKey& MasterPublicKey::operator=(const MasterPublicKey& rhs)
{
    *self_ = *rhs.self_;
    return *this;
}

bool MasterPublicKey::operator==(const MasterPublicKey& rhs) const
{
    return self_->vecR == rhs.self_->vecR;
}

std::ostream& operator<<(std::ostream& os, const MasterPublicKey& mpk)
{
    const size_t n = mpk.self_->vecR.size();
    os << n;
    for (size_t i = 0; i < n; i++) {
        os << '\n' << mpk.self_->vecR[i];
    }
    return os;
}

std::istream& operator>>(std::istream& is, MasterPublicKey& mpk)
{
    size_t n;
    is >> n;
    mpk.self_->vecR.resize(n);
    for (size_t i = 0; i < n; i++) {
        is >> mpk.self_->vecR[i];
    }
    return is;
}

PublicKey::PublicKey()
    : self_(new impl::PublicKey())
    , id_(0)
{
}

PublicKey::~PublicKey()
{
    delete self_;
}

PublicKey::PublicKey(const PublicKey& rhs)
    : self_(new impl::PublicKey(*rhs.self_))
    , id_(rhs.id_)
{
}

PublicKey& PublicKey::operator=(const PublicKey& rhs)
{
    *self_ = *rhs.self_;
    id_ = rhs.id_;
    return *this;
}

bool PublicKey::operator==(const PublicKey& rhs) const
{
    return id_ == rhs.id_ && self_->sQ == rhs.self_->sQ;
}

std::ostream& operator<<(std::ostream& os, const PublicKey& pub)
{
    return os << pub.id_ << ' ' << pub.self_->sQ;
}

std::istream& operator>>(std::istream& is, PublicKey& pub)
{
    return is >> pub.id_ >> pub.self_->sQ;
}

bool PublicKey::verify(const Sign& sign, const std::string& m) const
{
    return self_->verify(*sign.self_, m);
}

void PublicKey::recover(const std::vector<PublicKey>& pubVec)
{
    G2 sQ;
    LagrangeInterpolation(sQ, pubVec);
    self_->sQ = sQ;
    id_ = 0;
}

bool PublicKey::isValid(const MasterPublicKey& mpk) const
{
    G2 v;
    evalPoly(v, Fr(id_), mpk.self_->vecR);
    return v == self_->sQ;
}

void PublicKey::add(const PublicKey& rhs)
{
    if (id_ != 0 || rhs.id_ != 0) throw cybozu::Exception("bls:PublicKey:add:bad id") << id_ << rhs.id_;
    self_->sQ += rhs.self_->sQ;
}

PrivateKey::PrivateKey()
    : self_(new impl::PrivateKey())
    , id_(0)
{
}

PrivateKey::~PrivateKey()
{
    delete self_;
}

PrivateKey::PrivateKey(const PrivateKey& rhs)
    : self_(new impl::PrivateKey(*rhs.self_))
    , id_(rhs.id_)
{
}

PrivateKey& PrivateKey::operator=(const PrivateKey& rhs)
{
    *self_ = *rhs.self_;
    id_ = rhs.id_;
    return *this;
}

bool PrivateKey::operator==(const PrivateKey& rhs) const
{
    return id_ == rhs.id_ && self_->s == rhs.self_->s;
}

void PrivateKey::init()
{
    self_->init();
}

void PrivateKey::getPublicKey(PublicKey& pub) const
{
    self_->getPublicKey(*pub.self_);
    pub.id_ = id_;
}

std::ostream& operator<<(std::ostream& os, const PrivateKey& prv)
{
    return os << prv.id_ << ' ' << prv.self_->s;
}

std::istream& operator>>(std::istream& is, PrivateKey& prv)
{
    return is >> prv.id_ >> prv.self_->s;
}

void PrivateKey::sign(Sign& sign, const std::string& m) const
{
    self_->sign(*sign.self_, m);
    sign.id_ = id_;
}

void PrivateKey::share(std::vector<PrivateKey>& prvVec, int n, int k, MasterPublicKey *mpk)
{
    if (id_ != 0) throw cybozu::Exception("bls:PrivateKey:share:already shared") << id_;
    if (n <= 0 || k <= 0 || k > n) throw cybozu::Exception("bls:PrivateKey:share:bad n, k") << n << k;
    Polynomial poly;
    poly.init(self_->s, k);
    prvVec.resize(n);
    for (int i = 0; i < n; i++) {
        int id = i + 1;
        poly.eval(prvVec[i].self_->s, id);
        prvVec[i].id_ = id;
    }
    if (mpk == 0) return;
    std::vector<G2>& vecR = mpk->self_->vecR;
    vecR.resize(k);
    for (size_t i = 0; i < vecR.size(); i++) {
        G2::mul(vecR[i], getQ(), poly.c[i]);
    }
}

void PrivateKey::recover(const std::vector<PrivateKey>& prvVec)
{
    Fr s;
    LagrangeInterpolation(s, prvVec);
    self_->s = s;
    id_ = 0;
}

void PrivateKey::add(const PrivateKey& rhs)
{
    if (id_ != 0 || rhs.id_ != 0) throw cybozu::Exception("bls:PrivateKey:add:bad id") << id_ << rhs.id_;
    self_->s += rhs.self_->s;
}

} // bls