diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-02-15 11:02:39 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-02-15 11:02:39 +0800 |
commit | 3ca15da1b6ca2bbd3951d698133bbae25cbd382e (patch) | |
tree | f9c0b00668d7033911561bc37cb444e6662880e3 | |
parent | be862f4fc85308608a3cd0da2e5d9e7e45999c4b (diff) | |
download | dexon-mcl-3ca15da1b6ca2bbd3951d698133bbae25cbd382e.tar.gz dexon-mcl-3ca15da1b6ca2bbd3951d698133bbae25cbd382e.tar.zst dexon-mcl-3ca15da1b6ca2bbd3951d698133bbae25cbd382e.zip |
add BLS signature sample
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | sample/bls_sig.cpp | 66 |
2 files changed, 67 insertions, 1 deletions
@@ -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; + } +} |