aboutsummaryrefslogtreecommitdiffstats
path: root/sample
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2017-02-15 11:29:45 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2017-02-15 11:29:45 +0800
commit2c310e76ea90a9a8693057fd729814a463e91354 (patch)
tree4923a33e77ae13fcfa25abc98e8d76d01f272e25 /sample
parenta188d8090dce0f6927d1b651384773cfc3f24090 (diff)
downloadtangerine-mcl-2c310e76ea90a9a8693057fd729814a463e91354.tar.gz
tangerine-mcl-2c310e76ea90a9a8693057fd729814a463e91354.tar.zst
tangerine-mcl-2c310e76ea90a9a8693057fd729814a463e91354.zip
split functions for bls_sig.cpp
Diffstat (limited to 'sample')
-rw-r--r--sample/bls_sig.cpp44
1 files changed, 28 insertions, 16 deletions
diff --git a/sample/bls_sig.cpp b/sample/bls_sig.cpp
index c7cac2b..f054d9c 100644
--- a/sample/bls_sig.cpp
+++ b/sample/bls_sig.cpp
@@ -26,6 +26,29 @@ void Hash(G1& P, const std::string& m)
BN::mapToG1(P, t);
}
+void KeyGen(Fr& s, G2& pub, const G2& Q)
+{
+ s.setRand(g_rg);
+ G2::mul(pub, Q, s); // pub = sQ
+}
+
+void Sign(G1& sign, const Fr& s, const std::string& m)
+{
+ G1 Hm;
+ Hash(Hm, m);
+ G1::mul(sign, Hm, s); // sign = s H(m)
+}
+
+bool Verify(const G1& sign, const G2& Q, const G2& pub, const std::string& m)
+{
+ Fp12 e1, e2;
+ G1 Hm;
+ Hash(Hm, m);
+ BN::pairing(e1, sign, Q); // e1 = e(sign, Q)
+ BN::pairing(e2, Hm, pub); // e2 = e(Hm, sQ)
+ return e1 == e2;
+}
+
int main(int argc, char *argv[])
{
std::string m = argc == 1 ? "hello mcl" : argv[1];
@@ -37,29 +60,18 @@ int main(int argc, char *argv[])
// generate secret key and public key
Fr s;
- s.setRand(g_rg);
- std::cout << "secret key " << s << std::endl;
G2 pub;
- G2::mul(pub, Q, s); // pub = sQ
+ KeyGen(s, pub, Q);
+ std::cout << "secret key " << s << std::endl;
std::cout << "public key " << pub << std::endl;
// sign
G1 sign;
- {
- G1 Hm;
- Hash(Hm, m);
- G1::mul(sign, Hm, s); // sign = s H(m)
- }
+ Sign(sign, s, m);
std::cout << "msg " << m << std::endl;
std::cout << "sign " << sign << std::endl;
// verify
- {
- Fp12 e1, e2;
- G1 Hm;
- Hash(Hm, m);
- BN::pairing(e1, sign, Q); // e1 = e(sign, Q)
- BN::pairing(e2, Hm, pub); // e2 = e(Hm, sQ)
- std::cout << "verify " << (e1 == e2 ? "ok" : "ng") << std::endl;
- }
+ bool ok = Verify(sign, Q, pub, m);
+ std::cout << "verify " << (ok ? "ok" : "ng") << std::endl;
}