aboutsummaryrefslogtreecommitdiffstats
path: root/sample/bls_smpl.cpp
blob: a91616c72a99eb2a6da9b7bfa4a7a2926c4e4163 (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
#include <bls/bls.hpp>
#include <cybozu/option.hpp>
#include <cybozu/itoa.hpp>
#include <fstream>

const std::string pubFile = "sample/publickey";
const std::string secFile = "sample/secretkey";
const std::string signFile = "sample/sign";

std::string makeName(const std::string& name, const bls::Id& id)
{
    const std::string suf = ".txt";
    if (id.isZero()) return name + suf;
    std::ostringstream os;
    os << name << '.' << id << suf;
    return os.str();
}

template<class T>
void save(const std::string& file, const T& t, const bls::Id& id = 0)
{
    const std::string name = makeName(file, id);
    std::ofstream ofs(name.c_str(), std::ios::binary);
    if (!(ofs << t)) {
        throw cybozu::Exception("can't save") << name;
    }
}

template<class T>
void load(T& t, const std::string& file, const bls::Id& id = 0)
{
    const std::string name = makeName(file, id);
    std::ifstream ifs(name.c_str(), std::ios::binary);
    if (!(ifs >> t)) {
        throw cybozu::Exception("can't load") << name;
    }
}

int init()
{
    printf("make %s and %s files\n", secFile.c_str(), pubFile.c_str());
    bls::SecretKey sec;
    sec.init();
    save(secFile, sec);
    bls::PublicKey pub;
    sec.getPublicKey(pub);
    save(pubFile, pub);
    return 0;
}

int sign(const std::string& m, int id)
{
    printf("sign message `%s` by id=%d\n", m.c_str(), id);
    bls::SecretKey sec;
    load(sec, secFile, id);
    bls::Sign s;
    sec.sign(s, m);
    save(signFile, s, id);
    return 0;
}

int verify(const std::string& m, int id)
{
    printf("verify message `%s` by id=%d\n", m.c_str(), id);
    bls::PublicKey pub;
    load(pub, pubFile, id);
    bls::Sign s;
    load(s, signFile, id);
    if (s.verify(pub, m)) {
        puts("verify ok");
        return 0;
    } else {
        puts("verify err");
        return 1;
    }
}

int share(size_t n, size_t k)
{
    printf("%d-out-of-%d threshold sharing\n", (int)k, (int)n);
    bls::SecretKey sec;
    load(sec, secFile);
    bls::SecretKeyVec msk;
    sec.getMasterSecretKey(msk, k);
    bls::SecretKeyVec secVec(n);
    bls::IdVec ids(n);
    for (size_t i = 0; i < n; i++) {
        int id = i + 1;
        ids[i] = id;
        secVec[i].set(msk, id);
    }
    for (size_t i = 0; i < n; i++) {
        save(secFile, secVec[i], ids[i]);
        bls::PublicKey pub;
        secVec[i].getPublicKey(pub);
        save(pubFile, pub, ids[i]);
    }
    return 0;
}

int recover(const bls::IdVec& ids)
{
    printf("recover from");
    for (size_t i = 0; i < ids.size(); i++) {
        std::cout << ' ' << ids[i];
    }
    printf("\n");
    bls::SignVec signVec(ids.size());
    for (size_t i = 0; i < signVec.size(); i++) {
        load(signVec[i], signFile, ids[i]);
    }
    bls::Sign s;
    s.recover(signVec, ids);
    save(signFile, s);
    return 0;
}

int main(int argc, char *argv[])
    try
{
    bls::init();

    std::string mode;
    std::string m;
    size_t n;
    size_t k;
    int id;
    bls::IdVec ids;

    cybozu::Option opt;
    opt.appendParam(&mode, "init|sign|verify|share|recover");
    opt.appendOpt(&n, 10, "n", ": k-out-of-n threshold");
    opt.appendOpt(&k, 3, "k", ": k-out-of-n threshold");
    opt.appendOpt(&m, "", "m", ": message to be signed");
    opt.appendOpt(&id, 0, "id", ": id of secretKey");
    opt.appendVec(&ids, "ids", ": select k id in [0, n). this option should be last");
    opt.appendHelp("h");
    if (!opt.parse(argc, argv)) {
        goto ERR_EXIT;
    }

    if (mode == "init") {
        return init();
    } else if (mode == "sign") {
        if (m.empty()) goto ERR_EXIT;
        return sign(m, id);
    } else if (mode == "verify") {
        if (m.empty()) goto ERR_EXIT;
        return verify(m, id);
    } else if (mode == "share") {
        return share(n, k);
    } else if (mode == "recover") {
        if (ids.empty()) {
            fprintf(stderr, "use -ids option. ex. share -ids 1 3 5\n");
            goto ERR_EXIT;
        }
        return recover(ids);
    } else {
        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;
}