aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/byzantine-lab/mcl/sample/vote.cpp
blob: 88137187cd68d99351087fb1a672e298f38dad8e (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
/*
    vote sample tool
    Copyright (c) 2014, National Institute of Advanced Industrial
    Science and Technology All rights reserved.
    This source file is subject to BSD 3-Clause license.

    modifyed for mcl by herumi
*/
#include <iostream>
#include <fstream>
#include <cybozu/random_generator.hpp>
#include <cybozu/option.hpp>
#include <cybozu/itoa.hpp>
#include <mcl/fp.hpp>
#include <mcl/ec.hpp>
#include <mcl/elgamal.hpp>
#include <mcl/ecparam.hpp>

typedef mcl::FpT<> Fp;
typedef mcl::FpT<mcl::ZnTag> Zn; // use ZnTag because Zn is different class with Fp
typedef mcl::EcT<Fp> Ec;
typedef mcl::ElgamalT<Ec, Zn> Elgamal;

cybozu::RandomGenerator rg;

const std::string pubFile = "vote_pub.txt";
const std::string prvFile = "vote_prv.txt";
const std::string resultFile = "vote_ret.txt";

std::string GetSheetName(size_t n)
{
    return std::string("vote_") + cybozu::itoa(n) + ".txt";
}

struct Param {
    std::string mode;
    std::string voteList;
    Param(int argc, const char *const argv[])
    {
        cybozu::Option opt;
        opt.appendOpt(&voteList, "11001100", "l", ": list of voters for vote mode(eg. 11001100)");
        opt.appendHelp("h", ": put this message");
        opt.appendParam(&mode, "mode", ": init/vote/count/open");
        if (!opt.parse(argc, argv)) {
            opt.usage();
            exit(1);
        }
        printf("mode=%s\n", mode.c_str());
        if (mode == "vote") {
            printf("voters=%s\n", voteList.c_str());
            size_t pos = voteList.find_first_not_of("01");
            if (pos != std::string::npos) {
                printf("bad char %c\n", voteList[pos]);
                exit(1);
            }
        }
    }
};

void SysInit()
{
    const mcl::EcParam& para = mcl::ecparam::secp192k1;
    Zn::init(para.n);
    Fp::init(para.p);
    Ec::init(para.a, para.b);
}

template<class T>
bool Load(T& t, const std::string& name, bool doThrow = true)
{
    std::ifstream ifs(name.c_str(), std::ios::binary);
    if (!ifs) {
        if (doThrow) throw cybozu::Exception("Load:can't read") << name;
        return false;
    }
    if (ifs >> t) return true;
    if (doThrow) throw cybozu::Exception("Load:bad data") << name;
    return false;
}

template<class T>
void Save(const std::string& name, const T& t)
{
    std::ofstream ofs(name.c_str(), std::ios::binary);
    ofs << t;
}

void Init()
{
    const mcl::EcParam& para = mcl::ecparam::secp192k1;
    const Fp x0(para.gx);
    const Fp y0(para.gy);
    const Ec P(x0, y0);
    const size_t bitSize = para.bitSize;

    Elgamal::PrivateKey prv;
    prv.init(P, bitSize, rg);
    const Elgamal::PublicKey& pub = prv.getPublicKey();
    printf("make privateKey=%s, publicKey=%s\n", prvFile.c_str(), pubFile.c_str());
    Save(prvFile, prv);
    Save(pubFile, pub);
}

struct CipherWithZkp {
    Elgamal::CipherText c;
    Elgamal::Zkp zkp;
    bool verify(const Elgamal::PublicKey& pub) const
    {
        return pub.verify(c, zkp);
    }
};

inline std::ostream& operator<<(std::ostream& os, const CipherWithZkp& self)
{
    return os << self.c << std::endl << self.zkp;
}
inline std::istream& operator>>(std::istream& is, CipherWithZkp& self)
{
    return is >> self.c >> self.zkp;
}

void Vote(const std::string& voteList)
{
    Elgamal::PublicKey pub;
    Load(pub, pubFile);
    puts("shuffle");
    std::vector<size_t> idxTbl(voteList.size());
    for (size_t i = 0; i < idxTbl.size(); i++) {
        idxTbl[i] = i;
    }
    cybozu::shuffle(idxTbl, rg);
    puts("each voter votes");
    for (size_t i = 0; i < voteList.size(); i++) {
        CipherWithZkp c;
        pub.encWithZkp(c.c, c.zkp, voteList[i] - '0', rg);
        const std::string sheetName = GetSheetName(idxTbl[i]);
        printf("make %s\n", sheetName.c_str());
        Save(sheetName, c);
    }
}

void Count()
{
    Elgamal::PublicKey pub;
    Load(pub, pubFile);
    Elgamal::CipherText result;
    puts("aggregate votes");
    for (size_t i = 0; ; i++) {
        const std::string sheetName = GetSheetName(i);
        CipherWithZkp c;
        if (!Load(c, sheetName, false)) break;
        if (!c.verify(pub)) throw cybozu::Exception("bad cipher text") << i;
        printf("add %s\n", sheetName.c_str());
        result.add(c.c);
    }
    printf("create result file : %s\n", resultFile.c_str());
    Save(resultFile, result);
}

void Open()
{
    Elgamal::PrivateKey prv;
    Load(prv, prvFile);
    Elgamal::CipherText c;
    Load(c, resultFile);
    Zn n;
    prv.dec(n, c);
    std::cout << "result of vote count " << n << std::endl;
#if 0
    puts("open real value");
    for (size_t i = 0; ; i++) {
        Elgamal::CipherText c;
        const std::string sheetName = GetSheetName(i);
        if (!Load(c, sheetName, false)) break;
        Zn n;
        prv.dec(n, c);
        std::cout << sheetName << " " << n << std::endl;
    }
#endif
}

int main(int argc, char *argv[])
    try
{
    const Param p(argc, argv);
    SysInit();
    if (p.mode == "init") {
        Init();
    } else
    if (p.mode == "vote") {
        Vote(p.voteList);
    } else
    if (p.mode == "count") {
        Count();
    } else
    if (p.mode == "open") {
        Open();
    } else
    {
        printf("bad mode=%s\n", p.mode.c_str());
        return 1;
    }
} catch (std::exception& e) {
    printf("ERR %s\n", e.what());
}