aboutsummaryrefslogtreecommitdiffstats
path: root/sample/bls_tool.cpp
blob: a5c2abdceedc1fa4a776d6e795541329b05fdd85 (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
#include <bls.hpp>
#include <iostream>
#include <cybozu/option.hpp>

template<class T>
void write(const T& t)
{
    std::cout << std::hex << std::showbase << t << std::endl;
}

template<class T>
void read(T& t)
{
    if (!(std::cin >> t)) {
        throw std::runtime_error("can't read");
    }
}

void strip(std::string& str)
{
    if (str.empty()) return;
    if (str[str.size() - 1] == '\n') str.resize(str.size() - 1);
}

void readLine(std::string& str)
{
    str.clear();
    // retry once if blank line exists
    for (size_t i = 0; i < 2; i++) {
        std::getline(std::cin, str);
        strip(str);
        if (!str.empty()) return;
    }
    throw std::runtime_error("readLine:message is empty");
}

void init()
{
    bls::SecretKey sec;
    sec.init();
    write(sec);
}

void pubkey()
{
    bls::SecretKey sec;
    read(sec);
    bls::PublicKey pub;
    sec.getPublicKey(pub);
    write(pub);
}

void sign()
{
    bls::SecretKey sec;
    read(sec);
    std::string m;
    readLine(m);
    fprintf(stderr, "sign `%s`\n", m.c_str());
    bls::Sign s;
    sec.sign(s, m);
    write(s);
}

void verify()
{
    bls::Sign s;
    read(s);
    bls::PublicKey pub;
    read(pub);
    std::string m;
    readLine(m);
    fprintf(stderr, "verify `%s`\n", m.c_str());
    bool b = s.verify(pub, m);
    write(b ? "1" : "0");
}

void share_pub()
{
    size_t k;
    read(k);
    bls::PublicKeyVec mpk(k);
    for (size_t i = 0; i < k; i++) {
        read(mpk[i]);
    }
    bls::Id id;
    read(id);
    bls::PublicKey pub;
    pub.set(mpk, id);
    write(pub);
}

void recover_sig()
{
    size_t k;
    read(k);
    bls::SecretKeyVec msk(k);
    bls::IdVec idVec(k);
    for (size_t i = 0; i < k; i++) {
        read(idVec[i]);
        read(msk[i]);
    }
    bls::SecretKey sec;
    sec.recover(msk, idVec);
    write(sec);
}

void aggregate_pub()
{
    size_t n;
    read(n);
    if (n == 0) throw std::runtime_error("aggregate_pub:n is zero");
    bls::PublicKey pub;
    read(pub);
    for (size_t i = 1; i < n; i++) {
        bls::PublicKey rhs;
        read(rhs);
        pub.add(rhs);
    }
    write(pub);
}

void aggregate_sig()
{
    size_t n;
    read(n);
    if (n == 0) throw std::runtime_error("aggregate_sig:n is zero");
    bls::Sign s;
    read(s);
    for (size_t i = 1; i < n; i++) {
        bls::Sign rhs;
        read(rhs);
        s.add(rhs);
    }
    write(s);
}

int main(int argc, char *argv[])
    try
{
    const struct CmdTbl {
        const char *name;
        void (*exec)();
    } tbl[] = {
        { "init", init },
        { "pubkey", pubkey },
        { "sign", sign },
        { "verify", verify },
        { "share-pub", share_pub },
        { "recover-sig", recover_sig },
        { "aggregate-pub", aggregate_pub },
        { "aggregate-sig", aggregate_sig },
    };
    std::string cmdCat;
    for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
        if (i > 0) cmdCat += '|';
        cmdCat += tbl[i].name;
    }
    std::string mode;
    cybozu::Option opt;
    
    opt.appendParam(&mode, cmdCat.c_str());
    opt.appendHelp("h");
    if (!opt.parse(argc, argv)) {
        goto ERR_EXIT;
    }

    bls::init();
    for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
        if (mode == tbl[i].name) {
            tbl[i].exec();
            return 0;
        }
    }
    fprintf(stderr, "bad mode %s\n", mode.c_str());
ERR_EXIT:
    opt.usage();
    return 1;
} catch (std::exception& e) {
    fprintf(stderr, "ERR %s\n", e.what());
    return 1;
}