diff options
-rw-r--r-- | include/mcl/bn.hpp | 28 | ||||
-rw-r--r-- | include/mcl/fp_tower.hpp | 10 | ||||
-rw-r--r-- | include/mcl/vint.hpp | 36 | ||||
-rw-r--r-- | test/vint_test.cpp | 45 |
4 files changed, 87 insertions, 32 deletions
diff --git a/include/mcl/bn.hpp b/include/mcl/bn.hpp index cec183a..c69656d 100644 --- a/include/mcl/bn.hpp +++ b/include/mcl/bn.hpp @@ -186,7 +186,7 @@ struct GLV1 { Unit w[splitN][maxUnit]; // unit array of u[i] int maxBit = 0; // max bit of u[i] int maxN = 0; - int m = 0; + int remainBit = 0; x %= r; if (x == 0) { @@ -226,15 +226,15 @@ struct GLV1 { assert(maxBit > 0); maxBit--; /* - maxBit = maxN * UnitBitSize + m - 0 < m <= UnitBitSize + maxBit = maxN * UnitBitSize + remainBit + 0 < remainBit <= UnitBitSize */ maxN = maxBit / mcl::fp::UnitBitSize; - m = maxBit % mcl::fp::UnitBitSize; - m++; + remainBit = maxBit % mcl::fp::UnitBitSize; + remainBit++; Q.clear(); for (int i = maxN; i >= 0; i--) { - for (int j = m - 1; j >= 0; j--) { + for (int j = remainBit - 1; j >= 0; j--) { G1::dbl(Q, Q); uint32_t b0 = (w[0][i] >> j) & 1; uint32_t b1 = (w[1][i] >> j) & 1; @@ -245,7 +245,7 @@ struct GLV1 { Q += tbl[c]; } } - m = (int)mcl::fp::UnitBitSize; + remainBit = (int)mcl::fp::UnitBitSize; } #endif DummyLoop: @@ -366,7 +366,7 @@ struct GLV2 { Unit w[splitN][maxUnit]; // unit array of u[i] int maxBit = 0; // max bit of u[i] int maxN = 0; - int m = 0; + int remainBit = 0; x %= r; if (x == 0) { @@ -422,15 +422,15 @@ struct GLV2 { } maxBit--; /* - maxBit = maxN * UnitBitSize + m - 0 < m <= UnitBitSize + maxBit = maxN * UnitBitSize + remainBit + 0 < remainBit <= UnitBitSize */ maxN = maxBit / mcl::fp::UnitBitSize; - m = maxBit % mcl::fp::UnitBitSize; - m++; + remainBit = maxBit % mcl::fp::UnitBitSize; + remainBit++; Q.clear(); for (int i = maxN; i >= 0; i--) { - for (int j = m - 1; j >= 0; j--) { + for (int j = remainBit - 1; j >= 0; j--) { T::dbl(Q, Q); uint32_t b0 = (w[0][i] >> j) & 1; uint32_t b1 = (w[1][i] >> j) & 1; @@ -443,7 +443,7 @@ struct GLV2 { Q += tbl[c]; } } - m = (int)mcl::fp::UnitBitSize; + remainBit = (int)mcl::fp::UnitBitSize; } #endif DummyLoop: diff --git a/include/mcl/fp_tower.hpp b/include/mcl/fp_tower.hpp index 495c89b..37cc3d6 100644 --- a/include/mcl/fp_tower.hpp +++ b/include/mcl/fp_tower.hpp @@ -16,6 +16,16 @@ class FpDblT { Unit v_[Fp::maxSize * 2]; public: static size_t getUnitSize() { return Fp::op_.N * 2; } + FpDblT() : v_() + { + } + FpDblT(const FpDblT& rhs) + { + const size_t n = getUnitSize(); + for (size_t i = 0; i < n; i++) { + v_[i] = rhs.v_[i]; + } + } void dump() const { const size_t n = getUnitSize(); diff --git a/include/mcl/vint.hpp b/include/mcl/vint.hpp index a3e0753..2a3cd81 100644 --- a/include/mcl/vint.hpp +++ b/include/mcl/vint.hpp @@ -961,37 +961,37 @@ private: z.isNeg_ = yNeg; } } - static void _adds1(VintT& z, const VintT& x, bool xNeg, int y, bool yNeg) + static void _adds1(VintT& z, const VintT& x, int y, bool yNeg) { assert(y >= 0); - if ((xNeg ^ yNeg) == 0) { + if ((x.isNeg_ ^ yNeg) == 0) { // same sign uadd1(z, x.buf_, x.size(), y); - z.isNeg_ = xNeg; + z.isNeg_ = yNeg; return; } if (x.size() > 1 || x.buf_[0] >= (Unit)y) { usub1(z, x.buf_, x.size(), y); - z.isNeg_ = xNeg; + z.isNeg_ = x.isNeg_; } else { z = y - x.buf_[0]; z.isNeg_ = yNeg; } } - static void _addu1(VintT& z, const VintT& x, bool xNeg, Unit y) + static void _addu1(VintT& z, const VintT& x, Unit y, bool yNeg) { - if (!xNeg) { + if ((x.isNeg_ ^ yNeg) == 0) { // same sign uadd1(z, x.buf_, x.size(), y); - z.isNeg_ = xNeg; + z.isNeg_ = yNeg; return; } if (x.size() > 1 || x.buf_[0] >= y) { usub1(z, x.buf_, x.size(), y); - z.isNeg_ = xNeg; + z.isNeg_ = x.isNeg_; } else { z = y - x.buf_[0]; - z.isNeg_ = false; + z.isNeg_ = yNeg; } } /** @@ -1350,11 +1350,11 @@ public: } static void addu1(VintT& z, const VintT& x, Unit y) { - _addu1(z, x, x.isNeg_, y); + _addu1(z, x, y, false); } static void subu1(VintT& z, const VintT& x, Unit y) { - _addu1(z, x, x.isNeg_, y); + _addu1(z, x, y, true); } static void mulu1(VintT& z, const VintT& x, Unit y) { @@ -1378,12 +1378,12 @@ public: static void adds1(VintT& z, const VintT& x, int y) { if (y == invalidVar) throw cybozu::Exception("VintT:adds1:bad y"); - _adds1(z, x, x.isNeg_, std::abs(y), y < 0); + _adds1(z, x, std::abs(y), y < 0); } static void subs1(VintT& z, const VintT& x, int y) { if (y == invalidVar) throw cybozu::Exception("VintT:subs1:bad y"); - _adds1(z, x, x.isNeg_, std::abs(y), !(y < 0)); + _adds1(z, x, std::abs(y), !(y < 0)); } static void muls1(VintT& z, const VintT& x, int y) { @@ -1600,7 +1600,7 @@ public: static void powMod(VintT& z, const VintT& x, const VintT& y, const VintT& m) { if (y.isNeg_) throw cybozu::Exception("Vint::pow:negative y") << y; - VintT zz = 1; + VintT zz; MulMod mulMod; SqrMod sqrMod; mulMod.pm = &m; @@ -1774,10 +1774,10 @@ public: } return j; } - VintT& operator++() { add(*this, *this, 1); return *this; } - VintT& operator--() { sub(*this, *this, 1); return *this; } - VintT operator++(int) { VintT c = *this; add(*this, *this, 1); return c; } - VintT operator--(int) { VintT c = *this; sub(*this, *this, 1); return c; } + VintT& operator++() { adds1(*this, *this, 1); return *this; } + VintT& operator--() { subs1(*this, *this, 1); return *this; } + VintT operator++(int) { VintT c = *this; adds1(*this, *this, 1); return c; } + VintT operator--(int) { VintT c = *this; subs1(*this, *this, 1); return c; } friend bool operator<(const VintT& x, const VintT& y) { return compare(x, y) < 0; } friend bool operator>=(const VintT& x, const VintT& y) { return !operator<(x, y); } friend bool operator>(const VintT& x, const VintT& y) { return compare(x, y) > 0; } diff --git a/test/vint_test.cpp b/test/vint_test.cpp index d3e0238..5011941 100644 --- a/test/vint_test.cpp +++ b/test/vint_test.cpp @@ -967,6 +967,51 @@ CYBOZU_TEST_AUTO(withInt) CYBOZU_TEST_EQUAL(x, -3); x /= -1; CYBOZU_TEST_EQUAL(x, 3); + + x++; + CYBOZU_TEST_EQUAL(x, 4); + x--; + CYBOZU_TEST_EQUAL(x, 3); + x = -3; + x++; + CYBOZU_TEST_EQUAL(x, -2); + x--; + CYBOZU_TEST_EQUAL(x, -3); + + ++x; + CYBOZU_TEST_EQUAL(x, -2); + --x; + CYBOZU_TEST_EQUAL(x, -3); + x = 3; + ++x; + CYBOZU_TEST_EQUAL(x, 4); + --x; + CYBOZU_TEST_EQUAL(x, 3); +} + +CYBOZU_TEST_AUTO(addu1) +{ + Vint x = 4; + Vint::addu1(x, x, 2); + CYBOZU_TEST_EQUAL(x, 6); + Vint::subu1(x, x, 2); + CYBOZU_TEST_EQUAL(x, 4); + Vint::subu1(x, x, 10); + CYBOZU_TEST_EQUAL(x, -6); + x = -4; + Vint::addu1(x, x, 2); + CYBOZU_TEST_EQUAL(x, -2); + Vint::subu1(x, x, 2); + CYBOZU_TEST_EQUAL(x, -4); + Vint::addu1(x, x, 10); + CYBOZU_TEST_EQUAL(x, 6); + + x.setStr("0x10000000000000000000000002"); + Vint::subu1(x, x, 3); + CYBOZU_TEST_EQUAL(x, Vint("0xfffffffffffffffffffffffff")); + x.setStr("-0x10000000000000000000000000"); + Vint::addu1(x, x, 5); + CYBOZU_TEST_EQUAL(x, Vint("-0xffffffffffffffffffffffffb")); } CYBOZU_TEST_AUTO(pow) |