aboutsummaryrefslogtreecommitdiffstats
path: root/include/bls.hpp
blob: 7d40ee364d525a43cb8a617d35256d98f91ff1b7 (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
#pragma once
/**
    @file
    @brief BLS threshold signature on BN curve
    @author MITSUNARI Shigeo(@herumi)
    @license modified new BSD license
    http://opensource.org/licenses/BSD-3-Clause
*/
#include <vector>
#include <string>
#include <iosfwd>

namespace bls {

namespace impl {

struct PublicKey;
struct PrivateKey;
struct Sign;
struct MasterPublicKey;

} // bls::impl

void init();

class Sign {
    impl::Sign *self_;
    int id_;
    friend class PublicKey;
    friend class PrivateKey;
    template<class G, class T>
    friend void LagrangeInterpolation(G& r, const T& vec);
public:
    Sign();
    ~Sign();
    Sign(const Sign& rhs);
    Sign& operator=(const Sign& rhs);
    bool operator==(const Sign& rhs) const;
    bool operator!=(const Sign& rhs) const { return !(*this == rhs); }
    int getId() const { return id_; }
    friend std::ostream& operator<<(std::ostream& os, const Sign& s);
    friend std::istream& operator>>(std::istream& is, Sign& s);

    /*
        recover sign from k signVec
    */
    void recover(const std::vector<Sign>& signVec);
};

/*
    Feldman's verifiable secret sharing
*/
class MasterPublicKey {
    impl::MasterPublicKey *self_;
    friend class PrivateKey;
    friend class PublicKey;
public:
    MasterPublicKey();
    ~MasterPublicKey();
    MasterPublicKey(const MasterPublicKey& rhs);
    MasterPublicKey& operator=(const MasterPublicKey& rhs);
    bool operator==(const MasterPublicKey& rhs) const;
    bool operator!=(const MasterPublicKey& rhs) const { return !(*this == rhs); }
    friend std::ostream& operator<<(std::ostream& os, const MasterPublicKey& mpk);
    friend std::istream& operator>>(std::istream& is, MasterPublicKey& mpk);
};

class PublicKey {
    impl::PublicKey *self_;
    int id_;
    friend class PrivateKey;
    template<class G, class T>
    friend void LagrangeInterpolation(G& r, const T& vec);
public:
    PublicKey();
    ~PublicKey();
    PublicKey(const PublicKey& rhs);
    PublicKey& operator=(const PublicKey& rhs);
    bool operator==(const PublicKey& rhs) const;
    bool operator!=(const PublicKey& rhs) const { return !(*this == rhs); }
    int getId() const { return id_; }
    friend std::ostream& operator<<(std::ostream& os, const PublicKey& pub);
    friend std::istream& operator>>(std::istream& is, PublicKey& pub);
    bool verify(const Sign& sign, const std::string& m) const;
    /*
        recover publicKey from k pubVec
    */
    void recover(const std::vector<PublicKey>& pubVec);
    /*
        validate self by MasterPublicKey
    */
    bool isValid(const MasterPublicKey& mpk) const;
};

class PrivateKey {
    impl::PrivateKey *self_;
    int id_; // master if id_ = 0, shared if id_ > 0
    template<class G, class T>
    friend void LagrangeInterpolation(G& r, const T& vec);
public:
    PrivateKey();
    ~PrivateKey();
    PrivateKey(const PrivateKey& rhs);
    PrivateKey& operator=(const PrivateKey& rhs);
    bool operator==(const PrivateKey& rhs) const;
    bool operator!=(const PrivateKey& rhs) const { return !(*this == rhs); }
    int getId() const { return id_; }
    friend std::ostream& operator<<(std::ostream& os, const PrivateKey& prv);
    friend std::istream& operator>>(std::istream& is, PrivateKey& prv);
    void init();
    void getPublicKey(PublicKey& pub) const;
    void sign(Sign& sign, const std::string& m) const;
    /*
        k-out-of-n secret sharing of privateKey
        set verifier if mpk is not 0
    */
    void share(std::vector<PrivateKey>& prvVec, int n, int k, MasterPublicKey *mpk = 0);
    /*
        recover privateKey from k prvVec
    */
    void recover(const std::vector<PrivateKey>& prvVec);
};

} //bls