aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2017-02-15 11:02:39 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2017-02-15 11:02:39 +0800
commit3ca15da1b6ca2bbd3951d698133bbae25cbd382e (patch)
treef9c0b00668d7033911561bc37cb444e6662880e3
parentbe862f4fc85308608a3cd0da2e5d9e7e45999c4b (diff)
downloaddexon-mcl-3ca15da1b6ca2bbd3951d698133bbae25cbd382e.tar.gz
dexon-mcl-3ca15da1b6ca2bbd3951d698133bbae25cbd382e.tar.zst
dexon-mcl-3ca15da1b6ca2bbd3951d698133bbae25cbd382e.zip
add BLS signature sample
-rw-r--r--Makefile2
-rw-r--r--sample/bls_sig.cpp66
2 files changed, 67 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index f15c34c..f9fe9bd 100644
--- a/Makefile
+++ b/Makefile
@@ -14,7 +14,7 @@ ifeq ($(CPU),x86-64)
TEST_SRC+=fp_generator_test.cpp
endif
endif
-SAMPLE_SRC=bench.cpp ecdh.cpp random.cpp rawbench.cpp vote.cpp pairing.cpp large.cpp tri-dh.cpp
+SAMPLE_SRC=bench.cpp ecdh.cpp random.cpp rawbench.cpp vote.cpp pairing.cpp large.cpp tri-dh.cpp bls_sig.cpp
ifneq ($(MCL_MAX_BIT_SIZE),)
CFLAGS+=-DMCL_MAX_BIT_SIZE=$(MCL_MAX_BIT_SIZE)
diff --git a/sample/bls_sig.cpp b/sample/bls_sig.cpp
new file mode 100644
index 0000000..9098185
--- /dev/null
+++ b/sample/bls_sig.cpp
@@ -0,0 +1,66 @@
+/**
+ @file
+ @brief a sample of BLS signature
+ see https://github.com/herumi/bls
+ @author MITSUNARI Shigeo(@herumi)
+ @license modified new BSD license
+ http://opensource.org/licenses/BSD-3-Clause
+
+*/
+#include <mcl/bn256.hpp>
+
+#if CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11
+#include <random>
+std::random_device g_rg;
+#else
+#include <cybozu/random_generator.hpp>
+cybozu::RandomGenerator g_rg;
+#endif
+
+using namespace mcl::bn256;
+
+void Hash(G1& P, const std::string& m)
+{
+ Fp t;
+ t.setMsg(m);
+ BN::mapToG1(P, t);
+}
+
+int main(int argc, char *argv[])
+{
+ std::string m = argc == 1 ? "hello mcl" : argv[1];
+
+ // setup parameter
+ bn256init();
+ G1 P(-1, 1);
+ G2 Q;
+ BN::mapToG2(Q, 1);
+
+ // 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
+ std::cout << "public key " << pub << std::endl;
+
+ // sign
+ G1 sign;
+ {
+ G1 Hm;
+ Hash(Hm, m);
+ G1::mul(sign, Hm, s); // sign = s H(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;
+ }
+}