aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/dexon-foundation/mcl/include/mcl/aggregate_sig.hpp
blob: f3140570564ce1a3e03dd2437e0b3809d4aa8fc6 (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
#pragma once
/**
    @file
    @brief aggregate signature
    @author MITSUNARI Shigeo(@herumi)
    see http://crypto.stanford.edu/~dabo/papers/aggreg.pdf
    @license modified new BSD license
    http://opensource.org/licenses/BSD-3-Clause
*/
#include <cmath>
#include <vector>
#include <iosfwd>
#include <set>
#ifndef MCLBN_FP_UNIT_SIZE
    #define MCLBN_FP_UNIT_SIZE 4
#endif
#if MCLBN_FP_UNIT_SIZE == 4
#include <mcl/bn256.hpp>
namespace mcl {
using namespace mcl::bn256;
}
#elif MCLBN_FP_UNIT_SIZE == 6
#include <mcl/bn384.hpp>
namespace mcl {
using namespace mcl::bn384;
}
#elif MCLBN_FP_UNIT_SIZE == 8
#include <mcl/bn512.hpp>
namespace mcl {
using namespace mcl::bn512;
}
#else
    #error "MCLBN_FP_UNIT_SIZE must be 4, 6, or 8"
#endif

namespace mcl { namespace aggs {

/*
    AGGregate Signature Template class
*/
template<size_t dummyImpl = 0>
struct AGGST {
    typedef typename G1::BaseFp Fp;

    class SecretKey;
    class PublicKey;
    class Signature;

    static G1 P_;
    static G2 Q_;
    static std::vector<Fp6> Qcoeff_;
public:
    static void init(const mcl::CurveParam& cp = mcl::BN254)
    {
        initPairing(cp);
        hashAndMapToG1(P_, "0");
        hashAndMapToG2(Q_, "0");
        precomputeG2(Qcoeff_, Q_);
    }
    class Signature : public fp::Serializable<Signature> {
        G1 S_;
        friend class SecretKey;
        friend class PublicKey;
    public:
        template<class InputStream>
        void load(InputStream& is, int ioMode = IoSerialize)
        {
            S_.load(is, ioMode);
        }
        template<class OutputStream>
        void save(OutputStream& os, int ioMode = IoSerialize) const
        {
            S_.save(os, ioMode);
        }
        friend std::istream& operator>>(std::istream& is, Signature& self)
        {
            self.load(is, fp::detectIoMode(G1::getIoMode(), is));
            return is;
        }
        friend std::ostream& operator<<(std::ostream& os, const Signature& self)
        {
            self.save(os, fp::detectIoMode(G1::getIoMode(), os));
            return os;
        }
        bool operator==(const Signature& rhs) const
        {
            return S_ == rhs.S_;
        }
        bool operator!=(const Signature& rhs) const { return !operator==(rhs); }
        /*
            aggregate sig[0..n) and set *this
        */
        void aggregate(const Signature *sig, size_t n)
        {
            G1 S;
            S.clear();
            for (size_t i =  0; i < n; i++) {
                S += sig[i].S_;
            }
            S_ = S;
        }
        void aggregate(const std::vector<Signature>& sig)
        {
            aggregate(sig.data(), sig.size());
        }
        /*
            aggregate verification
        */
        bool verify(const void *const *msgVec, const size_t *sizeVec, const PublicKey *pubVec, size_t n) const
        {
            if (n == 0) return false;
            typedef std::set<Fp> FpSet;
            FpSet msgSet;
            typedef std::vector<G1> G1Vec;
            G1Vec hv(n);
            for (size_t i = 0; i < n; i++) {
                Fp h;
                h.setHashOf(msgVec[i], sizeVec[i]);
                std::pair<typename FpSet::iterator, bool> ret = msgSet.insert(h);
                if (!ret.second) throw cybozu::Exception("aggs::verify:same msg");
                mapToG1(hv[i], h);
            }
            /*
                e(aggSig, xQ) = prod_i e(hv[i], pub[i].Q)
                <=> finalExp(e(-aggSig, xQ) * prod_i millerLoop(hv[i], pub[i].xQ)) == 1
            */
            GT e1, e2;
            precomputedMillerLoop(e1, -S_, Qcoeff_);
            millerLoop(e2, hv[0], pubVec[0].xQ_);
            for (size_t i = 1; i < n; i++) {
                GT e;
                millerLoop(e, hv[i], pubVec[i].xQ_);
                e2 *= e;
            }
            e1 *= e2;
            finalExp(e1, e1);
            return e1.isOne();
        }
        bool verify(const std::vector<std::string>& msgVec, const std::vector<PublicKey>& pubVec) const
        {
            const size_t n = msgVec.size();
            if (n != pubVec.size()) throw cybozu::Exception("aggs:Signature:verify:bad size") << msgVec.size() << pubVec.size();
            if (n == 0) return false;
            std::vector<const void*> mv(n);
            std::vector<size_t> sv(n);
            for (size_t i = 0; i < n; i++) {
                mv[i] = msgVec[i].c_str();
                sv[i] = msgVec[i].size();
            }
            return verify(&mv[0], &sv[0], &pubVec[0], n);
        }
    };
    class PublicKey : public fp::Serializable<PublicKey> {
        G2 xQ_;
        friend class SecretKey;
        friend class Signature;
    public:
        template<class InputStream>
        void load(InputStream& is, int ioMode = IoSerialize)
        {
            xQ_.load(is, ioMode);
        }
        template<class OutputStream>
        void save(OutputStream& os, int ioMode = IoSerialize) const
        {
            xQ_.save(os, ioMode);
        }
        friend std::istream& operator>>(std::istream& is, PublicKey& self)
        {
            self.load(is, fp::detectIoMode(G2::getIoMode(), is));
            return is;
        }
        friend std::ostream& operator<<(std::ostream& os, const PublicKey& self)
        {
            self.save(os, fp::detectIoMode(G2::getIoMode(), os));
            return os;
        }
        bool operator==(const PublicKey& rhs) const
        {
            return xQ_ == rhs.xQ_;
        }
        bool operator!=(const PublicKey& rhs) const { return !operator==(rhs); }
        bool verify(const Signature& sig, const void *m, size_t mSize) const
        {
            /*
                H = hash(m)
                e(S, Q) = e(H, xQ) where S = xH
                <=> e(S, Q)e(-H, xQ) = 1
                <=> finalExp(millerLoop(S, Q)e(-H, x)) = 1
            */
            G1 H;
            hashAndMapToG1(H, m,  mSize);
            G1::neg(H, H);
            GT e1, e2;
            precomputedMillerLoop(e1, sig.S_, Qcoeff_);
            millerLoop(e2, H, xQ_);
            e1 *= e2;
            finalExp(e1, e1);
            return e1.isOne();
        }
        bool verify(const Signature& sig, const std::string& m) const
        {
            return verify(sig, m.c_str(), m.size());
        }
    };
    class SecretKey : public fp::Serializable<SecretKey> {
        Fr x_;
        friend class PublicKey;
        friend class Signature;
    public:
        template<class InputStream>
        void load(InputStream& is, int ioMode = IoSerialize)
        {
            x_.load(is, ioMode);
        }
        template<class OutputStream>
        void save(OutputStream& os, int ioMode = IoSerialize) const
        {
            x_.save(os, ioMode);
        }
        friend std::istream& operator>>(std::istream& is, SecretKey& self)
        {
            self.load(is, fp::detectIoMode(Fr::getIoMode(), is));
            return is;
        }
        friend std::ostream& operator<<(std::ostream& os, const SecretKey& self)
        {
            self.save(os, fp::detectIoMode(Fr::getIoMode(), os));
            return os;
        }
        bool operator==(const SecretKey& rhs) const
        {
            return x_ == rhs.x_;
        }
        bool operator!=(const SecretKey& rhs) const { return !operator==(rhs); }
        void init()
        {
            x_.setByCSPRNG();
        }
        void getPublicKey(PublicKey& pub) const
        {
            G2::mul(pub.xQ_, Q_, x_);
        }
        void sign(Signature& sig, const void *m, size_t mSize) const
        {
            hashAndMapToG1(sig.S_, m, mSize);
            G1::mul(sig.S_, sig.S_, x_);
        }
        void sign(Signature& sig, const std::string& m) const
        {
            sign(sig, m.c_str(), m.size());
        }
    };
};

template<size_t dummyImpl> G1 AGGST<dummyImpl>::P_;
template<size_t dummyImpl> G2 AGGST<dummyImpl>::Q_;
template<size_t dummyImpl> std::vector<Fp6> AGGST<dummyImpl>::Qcoeff_;

typedef AGGST<> AGGS;
typedef AGGS::SecretKey SecretKey;
typedef AGGS::PublicKey PublicKey;
typedef AGGS::Signature Signature;

} } // mcl::aggs