diff options
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | include/bls/bls.hpp | 44 | ||||
-rw-r--r-- | test/bls_c_test.hpp | 45 | ||||
-rw-r--r-- | test/bls_test.hpp | 14 |
4 files changed, 88 insertions, 18 deletions
@@ -17,6 +17,9 @@ endif ifeq ($(DISABLE_THREAD_TEST),1) CFLAGS+=-DDISABLE_THREAD_TEST endif +ifeq ($(BLS_SWAP_G),1) + CFLAGS+=-DBLS_SWAP_G +endif SHARE_BASENAME_SUF?=_dy diff --git a/include/bls/bls.hpp b/include/bls/bls.hpp index b32b7e1..7413345 100644 --- a/include/bls/bls.hpp +++ b/include/bls/bls.hpp @@ -250,7 +250,7 @@ public: */ void recover(const SecretKeyVec& secVec, const IdVec& idVec) { - if (secVec.size() != idVec.size()) throw std::invalid_argument("SecretKey::recover"); + if (secVec.size() != idVec.size()) throw std::invalid_argument("SecretKey:recover"); recover(secVec.data(), idVec.data(), idVec.size()); } /* @@ -300,7 +300,12 @@ public: if (str != "0") { // 1 <x.a> <x.b> <y.a> <y.b> std::string t; - for (int i = 0; i < 4; i++) { +#ifdef BLS_SWAP_G + const int elemNum = 2; +#else + const int elemNum = 4; +#endif + for (int i = 0; i < elemNum; i++) { is >> t; str += ' '; str += t; @@ -312,14 +317,22 @@ public: void getStr(std::string& str, int ioMode = 0) const { str.resize(1024); +#ifdef BLS_SWAP_G + size_t n = mclBnG1_getStr(&str[0], str.size(), &self_.v, ioMode); +#else size_t n = mclBnG2_getStr(&str[0], str.size(), &self_.v, ioMode); - if (n == 0) throw std::runtime_error("mclBnG2_getStr"); +#endif + if (n == 0) throw std::runtime_error("PublicKey:getStr"); str.resize(n); } void setStr(const std::string& str, int ioMode = 0) { +#ifdef BLS_SWAP_G + int ret = mclBnG1_setStr(&self_.v, str.c_str(), str.size(), ioMode); +#else int ret = mclBnG2_setStr(&self_.v, str.c_str(), str.size(), ioMode); - if (ret != 0) throw std::runtime_error("mclBnG2_setStr"); +#endif + if (ret != 0) throw std::runtime_error("PublicKey:setStr"); } /* set public for id from mpk @@ -333,7 +346,7 @@ public: */ void recover(const PublicKeyVec& pubVec, const IdVec& idVec) { - if (pubVec.size() != idVec.size()) throw std::invalid_argument("PublicKey::recover"); + if (pubVec.size() != idVec.size()) throw std::invalid_argument("PublicKey:recover"); recover(pubVec.data(), idVec.data(), idVec.size()); } /* @@ -382,7 +395,12 @@ public: if (str != "0") { // 1 <x> <y> std::string t; - for (int i = 0; i < 2; i++) { +#ifdef BLS_SWAP_G + const int elemNum = 4; +#else + const int elemNum = 2; +#endif + for (int i = 0; i < elemNum; i++) { is >> t; str += ' '; str += t; @@ -394,14 +412,22 @@ public: void getStr(std::string& str, int ioMode = 0) const { str.resize(1024); +#ifdef BLS_SWAP_G + size_t n = mclBnG2_getStr(&str[0], str.size(), &self_.v, ioMode); +#else size_t n = mclBnG1_getStr(&str[0], str.size(), &self_.v, ioMode); - if (n == 0) throw std::runtime_error("mclBnG1_getStr"); +#endif + if (n == 0) throw std::runtime_error("Signature:tgetStr"); str.resize(n); } void setStr(const std::string& str, int ioMode = 0) { +#ifdef BLS_SWAP_G + int ret = mclBnG2_setStr(&self_.v, str.c_str(), str.size(), ioMode); +#else int ret = mclBnG1_setStr(&self_.v, str.c_str(), str.size(), ioMode); - if (ret != 0) throw std::runtime_error("mclBnG1_setStr"); +#endif + if (ret != 0) throw std::runtime_error("Signature:setStr"); } bool verify(const PublicKey& pub, const void *m, size_t size) const { @@ -437,7 +463,7 @@ public: */ void recover(const SignatureVec& sigVec, const IdVec& idVec) { - if (sigVec.size() != idVec.size()) throw std::invalid_argument("Signature::recover"); + if (sigVec.size() != idVec.size()) throw std::invalid_argument("Signature:recover"); recover(sigVec.data(), idVec.data(), idVec.size()); } /* diff --git a/test/bls_c_test.hpp b/test/bls_c_test.hpp index 7864097..809b487 100644 --- a/test/bls_c_test.hpp +++ b/test/bls_c_test.hpp @@ -4,6 +4,23 @@ #include <string.h> #include <cybozu/benchmark.hpp> +size_t pubSize(size_t FrSize) +{ +#ifdef BLS_SWAP_G + return FrSize; +#else + return FrSize * 2; +#endif +} +size_t sigSize(size_t FrSize) +{ +#ifdef BLS_SWAP_G + return FrSize * 2; +#else + return FrSize; +#endif +} + void bls_use_stackTest() { blsSecretKey sec; @@ -48,14 +65,14 @@ void blsDataTest() blsPublicKey pub1, pub2; blsGetPublicKey(&pub1, &sec1); n = blsPublicKeySerialize(buf, sizeof(buf), &pub1); - CYBOZU_TEST_EQUAL(n, FpSize * 2); + CYBOZU_TEST_EQUAL(n, pubSize(FpSize)); ret = blsPublicKeyDeserialize(&pub2, buf, n); CYBOZU_TEST_EQUAL(ret, n); CYBOZU_TEST_ASSERT(blsPublicKeyIsEqual(&pub1, &pub2)); blsSignature sig1, sig2; blsSign(&sig1, &sec1, msg, msgSize); n = blsSignatureSerialize(buf, sizeof(buf), &sig1); - CYBOZU_TEST_EQUAL(n, FpSize); + CYBOZU_TEST_EQUAL(n, sigSize(FpSize)); ret = blsSignatureDeserialize(&sig2, buf, n); CYBOZU_TEST_EQUAL(ret, n); CYBOZU_TEST_ASSERT(blsSignatureIsEqual(&sig1, &sig2)); @@ -176,7 +193,7 @@ void blsSerializeTest() CYBOZU_TEST_EQUAL(n, expectSize); // PublicKey - expectSize = FpSize * 2; + expectSize = pubSize(FpSize); blsGetPublicKey(&pub1, &sec1); n = blsPublicKeySerialize(buf, sizeof(buf), &pub1); CYBOZU_TEST_EQUAL(n, expectSize); @@ -199,7 +216,11 @@ void blsSerializeTest() CYBOZU_TEST_EQUAL(n, expectSize); // Signature +#ifdef BLS_SWAP_G + expectSize = FpSize * 2; +#else expectSize = FpSize; +#endif blsSign(&sig1, &sec1, "abc", 3); n = blsSignatureSerialize(buf, sizeof(buf), &sig1); CYBOZU_TEST_EQUAL(n, expectSize); @@ -225,10 +246,20 @@ void blsSerializeTest() void blsVerifyOrderTest() { puts("blsVerifyOrderTest"); - const uint8_t Ps[] = { +#ifdef BLS_SWAP_G + const uint8_t Qs[] = +#else + const uint8_t Ps[] = +#endif + { 0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, }; - const uint8_t Qs[] = { +#ifdef BLS_SWAP_G + const uint8_t Ps[] = +#else + const uint8_t Qs[] = +#endif + { 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, }; size_t n; @@ -242,10 +273,10 @@ void blsVerifyOrderTest() blsPublicKeyVerifyOrder(1); blsSignature sig; - n = blsSignatureDeserialize(&sig, Qs, sizeof(Ps)); + n = blsSignatureDeserialize(&sig, Qs, sizeof(Qs)); CYBOZU_TEST_EQUAL(n, 0); blsSignatureVerifyOrder(0); - n = blsSignatureDeserialize(&sig, Qs, sizeof(Ps)); + n = blsSignatureDeserialize(&sig, Qs, sizeof(Qs)); CYBOZU_TEST_ASSERT(n > 0); CYBOZU_TEST_ASSERT(!blsSignatureIsValidOrder(&sig)); blsSignatureVerifyOrder(1); diff --git a/test/bls_test.hpp b/test/bls_test.hpp index c3f6406..7c819b2 100644 --- a/test/bls_test.hpp +++ b/test/bls_test.hpp @@ -78,13 +78,15 @@ void hashTest(int type) CYBOZU_TEST_ASSERT(sig.verifyHash(pub, h)); CYBOZU_TEST_ASSERT(!sig.verifyHash(pub, "\x01\x02\04")); if (type == MCL_BN254) { - const uint64_t c1[] = { 0x0c00000000000004ull, 0xcf0f000000000006ull, 0x26cd890000000003ull, 0x2523648240000001ull }; - const uint64_t mc1[] = { 0x9b0000000000000full, 0x921200000000000dull, 0x9366c48000000004ull }; CYBOZU_TEST_EXCEPTION(sec.signHash(sig, "", 0), std::exception); CYBOZU_TEST_EXCEPTION(sec.signHash(sig, "\x00", 1), std::exception); CYBOZU_TEST_EXCEPTION(sec.signHash(sig, "\x00\x00", 2), std::exception); +#ifndef BLS_SWAP_G + const uint64_t c1[] = { 0x0c00000000000004ull, 0xcf0f000000000006ull, 0x26cd890000000003ull, 0x2523648240000001ull }; + const uint64_t mc1[] = { 0x9b0000000000000full, 0x921200000000000dull, 0x9366c48000000004ull }; CYBOZU_TEST_EXCEPTION(sec.signHash(sig, c1, 32), std::exception); CYBOZU_TEST_EXCEPTION(sec.signHash(sig, mc1, 24), std::exception); +#endif } } @@ -399,7 +401,11 @@ void dataTest() sec.getPublicKey(pub); pub.getStr(str, bls::IoFixedByteSeq); { +#ifdef BLS_SWAP_G + CYBOZU_TEST_EQUAL(str.size(), FpSize); +#else CYBOZU_TEST_EQUAL(str.size(), FpSize * 2); +#endif bls::PublicKey pub2; pub2.setStr(str, bls::IoFixedByteSeq); CYBOZU_TEST_EQUAL(pub, pub2); @@ -409,7 +415,11 @@ void dataTest() sec.sign(sign, m); sign.getStr(str, bls::IoFixedByteSeq); { +#ifdef BLS_SWAP_G + CYBOZU_TEST_EQUAL(str.size(), FpSize * 2); +#else CYBOZU_TEST_EQUAL(str.size(), FpSize); +#endif bls::Signature sign2; sign2.setStr(str, bls::IoFixedByteSeq); CYBOZU_TEST_EQUAL(sign, sign2); |