From cf7a2e07156ffeb6cc032d3681089ac41ddb86c6 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Sun, 7 Jun 2015 05:53:02 +0900 Subject: [PATCH] BitVector conversion is removed --- include/mcl/ec.hpp | 54 ------------------------- include/mcl/fp.hpp | 15 ------- include/mcl/fp_util.hpp | 89 ----------------------------------------- test/ec_test.cpp | 54 ------------------------- test/fp_test.cpp | 35 ---------------- test/fp_util_test.cpp | 68 ------------------------------- test/mont_fp_test.cpp | 37 ----------------- 7 files changed, 352 deletions(-) diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp index 8011efe..0b3c369 100644 --- a/include/mcl/ec.hpp +++ b/include/mcl/ec.hpp @@ -8,7 +8,6 @@ */ #include #include -#include #include #include #include @@ -449,59 +448,6 @@ public: /* append to bv(not clear bv) */ - void appendToBitVec(cybozu::BitVector& bv) const - { -#if MCL_EC_COORD == MCL_EC_USE_AFFINE - #error "not implemented" -#else - normalize(); - const size_t bitLen = _Fp::getModBitLen(); - /* - elem |x|y|z| - size n n 1 if not compressed - size n 1 1 if compressed - */ - const size_t maxBitLen = compressedExpression_ ? (bitLen + 1 + 1) : (bitLen * 2 + 1); - if (isZero()) { - bv.resize(bv.size() + maxBitLen); - return; - } - x.appendToBitVec(bv); - if (compressedExpression_) { - bv.append(Fp::isOdd(y), 1); - } else { - y.appendToBitVec(bv); - } - bv.append(1, 1); // z = 1 -#endif - } - void fromBitVec(const cybozu::BitVector& bv) - { -#if MCL_EC_COORD == MCL_EC_USE_AFFINE - #error "not implemented" -#else - const size_t bitLen = _Fp::getModBitLen(); - const size_t maxBitLen = compressedExpression_ ? (bitLen + 1 + 1) : (bitLen * 2 + 1); - if (bv.size() != maxBitLen) { - throw cybozu::Exception("EcT:fromBitVec:bad size") << bv.size() << maxBitLen; - } - if (!bv.get(maxBitLen - 1)) { // if z = 0 - clear(); - return; - } - cybozu::BitVector t; - bv.extract(t, 0, bitLen); - x.fromBitVec(t); - if (compressedExpression_) { - bool odd = bv.get(bitLen); // y - getYfromX(y, x, odd); - } else { - bv.extract(t, bitLen, bitLen); - y.fromBitVec(t); - } - z = 1; -#endif - } static inline size_t getBitVecSize() { const size_t bitLen = _Fp::getModBitLen(); diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index 1be0c4b..b304179 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -22,7 +22,6 @@ #include #include #include -#include #include #include #include @@ -293,24 +292,10 @@ public: powerArray(z, x, Gmp::getBlock(y), Gmp::getBlockSize(x)); } bool isZero() const { return op_.isZero(v_); } - /* - append to bv(not clear bv) - */ - void appendToBitVec(cybozu::BitVector& bv) const - { - fp::Block b; - getBlock(b); - bv.append(b.p, op_.bitLen); - } bool isValid() const { return fp::compareArray(v_, op_.p, op_.N) < 0; } - void fromBitVec(const cybozu::BitVector& bv) - { - if (bv.size() != op_.bitLen) throw cybozu::Exception("FpT:fromBitVec:bad size") << bv.size() << op_.bitLen; - setRaw(bv.getBlock(), bv.getBlockSize()); - } static inline size_t getModBitLen() { return op_.bitLen; } static inline size_t getBitVecSize() { return op_.bitLen; } bool operator==(const FpT& rhs) const { return fp::isEqualArray(v_, rhs.v_, op_.N); } diff --git a/include/mcl/fp_util.hpp b/include/mcl/fp_util.hpp index 7419672..22748d0 100644 --- a/include/mcl/fp_util.hpp +++ b/include/mcl/fp_util.hpp @@ -2,7 +2,6 @@ #include #include #include -#include /** @file @brief utility of Fp @@ -201,94 +200,6 @@ inline void getRandVal(S *out, RG& rg, const S *in, size_t bitLen) } } -/* - z[] = (x[] << shift) | y - @param z [out] z[0..n) - @param x [in] x[0..n) - @param n [in] length of x, z - @param shift [in] 0 <= shift < (sizeof(S) * 8) - @param y [in] - @return (x[] << shift)[n] -*/ -template -S shiftLeftOr(S* z, const S* x, size_t n, size_t shift, S y = 0) -{ - if (n == 0) { - throw cybozu::Exception("fp:shiftLeftOr:bad n"); - } - if (shift == 0) { - for (size_t i = n - 1; i > 0; i--) { - z[i] = x[i]; - } - z[0] = x[0] | y; - return 0; - } - const size_t unitSize = sizeof(S) * 8; - if (shift >= unitSize) { - throw cybozu::Exception("fp:shiftLeftOr:large shift") << shift; - } - const size_t rev = unitSize - shift; - S ret = x[n - 1] >> rev; - for (size_t i = n - 1; i > 0; i--) { - z[i] = (x[i] << shift) | (x[i - 1] >> rev); - } - z[0] = (x[0] << shift) | y; - return ret; -} -template -void shiftRight(S* z, const S* x, size_t n, size_t shift) -{ - if (n == 0) return; - if (shift == 0) { - for (size_t i = 0; i < n; i++) { - z[i] = x[i]; - } - return; - } - const size_t unitSize = sizeof(S) * 8; - if (shift >= unitSize) { - throw cybozu::Exception("fp:shiftRight:large shift") << shift; - } - const size_t rev = unitSize - shift; - S prev = x[0]; - for (size_t i = 0; i < n - 1; i++) { - S t = x[i + 1]; - z[i] = (prev >> shift) | (t << rev); - prev = t; - } - z[n - 1] = prev >> shift; -} - -template -size_t splitBitVec(Vec& v, const cybozu::BitVectorT& bv, size_t width) -{ - if (width > sizeof(typename Vec::value_type) * 8) { - throw cybozu::Exception("fp:splitBitVec:bad width") << width; - } - const size_t q = bv.size() / width; - const size_t r = bv.size() % width; - for (size_t i = 0; i < q; i++) { - v.push_back(bv.extract(i * width, width)); - } - if (r > 0) { - v.push_back(bv.extract(q * width, r)); - } - return r ? r : width; -} - -template -void concatBitVec(cybozu::BitVectorT& bv, const Vec& v, size_t width, size_t lastWidth) -{ - if (width > sizeof(typename Vec::value_type) * 8) { - throw cybozu::Exception("fp:splitBitVec:bad width") << width; - } - bv.clear(); - for (size_t i = 0; i < v.size() - 1; i++) { - bv.append(v[i], width); - } - bv.append(v[v.size() - 1], lastWidth); -} - } // mcl::fp } // fp diff --git a/test/ec_test.cpp b/test/ec_test.cpp index dc72e41..d6e921c 100644 --- a/test/ec_test.cpp +++ b/test/ec_test.cpp @@ -157,59 +157,6 @@ struct Test { R += P; } } - void binaryExpression() const - { - puts("test binaryExpression"); - const Fp x(para.gx); - const Fp y(para.gy); - Ec P(x, y); - Ec Q; - // not compressed - Ec::setCompressedExpression(false); - { - cybozu::BitVector bv; - P.appendToBitVec(bv); - Q.fromBitVec(bv); - CYBOZU_TEST_EQUAL(P, Q); - } - { - P = -P; - cybozu::BitVector bv; - P.appendToBitVec(bv); - Q.fromBitVec(bv); - CYBOZU_TEST_EQUAL(P, Q); - } - P.clear(); - { - cybozu::BitVector bv; - P.appendToBitVec(bv); - Q.fromBitVec(bv); - CYBOZU_TEST_EQUAL(P, Q); - } - // compressed - Ec::setCompressedExpression(true); - P.set(x, y); - { - cybozu::BitVector bv; - P.appendToBitVec(bv); - Q.fromBitVec(bv); - CYBOZU_TEST_EQUAL(P, Q); - } - { - P = -P; - cybozu::BitVector bv; - P.appendToBitVec(bv); - Q.fromBitVec(bv); - CYBOZU_TEST_EQUAL(P, Q); - } - P.clear(); - { - cybozu::BitVector bv; - P.appendToBitVec(bv); - Q.fromBitVec(bv); - CYBOZU_TEST_EQUAL(P, Q); - } - } void str() const { puts("test str"); @@ -316,7 +263,6 @@ pow 499.00usec power(); neg_power(); power_fp(); - binaryExpression(); squareRoot(); str(); #ifdef NDEBUG diff --git a/test/fp_test.cpp b/test/fp_test.cpp index aac80a1..44da64e 100644 --- a/test/fp_test.cpp +++ b/test/fp_test.cpp @@ -374,41 +374,6 @@ CYBOZU_TEST_AUTO(toStr) } } -CYBOZU_TEST_AUTO(binaryRepl) -{ - const struct { - const char *s; - size_t n; - uint32_t v[6]; - } tbl[] = { - { "0", 0, { 0, 0, 0, 0, 0 } }, - { "1234", 1, { 1234, 0, 0, 0, 0 } }, - { "0xaabbccdd12345678", 2, { 0x12345678, 0xaabbccdd, 0, 0, 0 } }, - { "0x11112222333344445555666677778888", 4, { 0x77778888, 0x55556666, 0x33334444, 0x11112222, 0 } }, - { "0x9911112222333344445555666677778888", 5, { 0x77778888, 0x55556666, 0x33334444, 0x11112222, 0x99, 0 } }, - }; - Fp::setModulo("0xfffffffffffffffffffffffe26f2fc170f69466a74defd8d"); - for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) { - Fp x(tbl[i].s); - cybozu::BitVector bv; - x.appendToBitVec(bv); - CYBOZU_TEST_EQUAL(bv.size(), Fp::getModBitLen()); - CYBOZU_TEST_EQUAL(bv.size(), Fp::getBitVecSize()); - const Fp::BlockType *block = bv.getBlock(); - if (sizeof(Fp::BlockType) == 4) { - CYBOZU_TEST_EQUAL_ARRAY(block, tbl[i].v, tbl[i].n); - } else { - const size_t n = (tbl[i].n + 1) / 2; - for (size_t j = 0; j < n; j++) { - uint64_t v = (uint64_t(tbl[i].v[j * 2 + 1]) << 32) | tbl[i].v[j * 2]; - CYBOZU_TEST_EQUAL(block[j], v); - } - } - Fp y; - y.fromBitVec(bv); - CYBOZU_TEST_EQUAL(x, y); - } -} #endif #ifdef NDEBUG diff --git a/test/fp_util_test.cpp b/test/fp_util_test.cpp index 28d94ed..c9785ab 100644 --- a/test/fp_util_test.cpp +++ b/test/fp_util_test.cpp @@ -121,71 +121,3 @@ CYBOZU_TEST_AUTO(getRandVal) } } -CYBOZU_TEST_AUTO(shiftLeftOr) -{ - const struct { - uint32_t x[4]; - size_t n; - size_t shift; - uint32_t y; - uint32_t z[4]; - uint32_t ret; - } tbl[] = { - { { 0x12345678, 0, 0, 0 }, 1, 0, 0, { 0x12345678, 0, 0, 0 }, 0 }, - { { 0x12345678, 0, 0, 0 }, 1, 1, 0, { 0x2468acf0, 0, 0, 0 }, 0 }, - { { 0xf2345678, 0, 0, 0 }, 1, 1, 5, { 0xe468acf5, 0, 0, 0 }, 1 }, - { { 0x12345678, 0x9abcdef0, 0x11112222, 0xffccaaee }, 4, 19, 0x1234, { 0xb3c01234, 0xf78091a2, 0x1114d5e6, 0x57708889 }, 0x7fe65 }, - }; - for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) { - uint32_t z[4]; - uint32_t ret = mcl::fp::shiftLeftOr(z, tbl[i].x, tbl[i].n, tbl[i].shift, tbl[i].y); - CYBOZU_TEST_EQUAL_ARRAY(z, tbl[i].z, tbl[i].n); - CYBOZU_TEST_EQUAL(ret, tbl[i].ret); - } -} - -CYBOZU_TEST_AUTO(shiftRight) -{ - const struct { - uint32_t x[4]; - size_t n; - size_t shift; - uint32_t z[4]; - } tbl[] = { - { { 0x12345678, 0, 0, 0 }, 4, 0, { 0x12345678, 0, 0, 0 } }, - { { 0x12345678, 0xaaaabbbb, 0xffeebbcc, 0xfeba9874 }, 4, 1, { 0x891a2b3c, 0x55555ddd, 0x7ff75de6, 0x7f5d4c3a } }, - { { 0x12345678, 0xaaaabbbb, 0xffeebbcc, 0xfeba9874 }, 4, 18, { 0xaeeec48d, 0xaef32aaa, 0xa61d3ffb, 0x3fae } }, - }; - for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) { - uint32_t z[4]; - mcl::fp::shiftRight(z, tbl[i].x, tbl[i].n, tbl[i].shift); - CYBOZU_TEST_EQUAL_ARRAY(z, tbl[i].z, tbl[i].n); - } -} - -CYBOZU_TEST_AUTO(splitBitVec) -{ - uint32_t tbl[] = { 0x12345678, 0xaaaabbbb, 0xffeebbcc }; - typedef cybozu::BitVectorT BitVec; - typedef std::vector IntVec; - BitVec bv; - bv.append(tbl, sizeof(tbl) * 8); - for (size_t len = bv.size(); len > 0; len--) { - bv.resize(len); - for (size_t w = 1; w < 18; w++) { - IntVec iv; - size_t last = mcl::fp::splitBitVec(iv, bv, w); - size_t q = len / w; - size_t r = len % w; - if (r == 0) { - r = w; - } else { - q++; - } - CYBOZU_TEST_EQUAL(iv.size(), q); - BitVec bv2; - mcl::fp::concatBitVec(bv2, iv, w, last); - CYBOZU_TEST_ASSERT(bv == bv2); - } - } -} diff --git a/test/mont_fp_test.cpp b/test/mont_fp_test.cpp index 25a6ced..e0386ed 100644 --- a/test/mont_fp_test.cpp +++ b/test/mont_fp_test.cpp @@ -136,7 +136,6 @@ struct Test { setRaw(); set64bit(); getRaw(); - binaryExp(); bench(); } void cstr() @@ -487,42 +486,6 @@ struct Test { CYBOZU_TEST_EQUAL(x, Fp("0x3400000012")); #endif } - void binaryExp() - { - puts("binaryExp"); - for (int i = 2; i < 7; i++) { - mpz_class g = m / i; - Fp x, y; -// Fp::toMont(x, g); - x.fromGmp(g); - cybozu::BitVector bv; - x.appendToBitVec(bv); - uint64_t buf[N]; - mcl::Gmp::getRaw(buf, N, g); - CYBOZU_TEST_EQUAL(bv.getBlockSize(), N); - CYBOZU_TEST_EQUAL(bv.size(), Fp::getModBitLen()); - CYBOZU_TEST_EQUAL(bv.size(), Fp::getBitVecSize()); - const uint64_t *p = bv.getBlock(); - CYBOZU_TEST_EQUAL_ARRAY(p, buf, N); - } - const mpz_class yy("0x1255556666777788881111222233334444"); - if (yy > m) { - return; - } - Fp y; -// Fp::toMont(y, yy); - y.fromGmp(yy); - uint64_t b1[N] = { uint64_t(0x1111222233334444ull), uint64_t(0x5555666677778888ull), 0x12 }; - Fp x; - cybozu::BitVector bv; - bv.append(b1, Fp::getModBitLen()); - x.fromBitVec(bv); - CYBOZU_TEST_EQUAL(x, y); - bv.clear(); - x.appendToBitVec(bv); - const uint64_t *b2 = bv.getBlock(); - CYBOZU_TEST_EQUAL_ARRAY(b1, b2, N); - } void set64bit() {