Skip to content

Commit

Permalink
Merge pull request #63 from kroma-network/fix/evaluate-for-sparse-uni…
Browse files Browse the repository at this point in the history
…variate-polynomials

fix(math): fix bit iteration in case of little endian
  • Loading branch information
chokobole authored Sep 25, 2023
2 parents 3c283f6 + 33b94f5 commit 8c1deaf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
8 changes: 4 additions & 4 deletions tachyon/math/base/bit_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class BitIteratorBE {
while (!(*ret)) {
++ret;
#if ARCH_CPU_BIG_ENDIAN
if (ret.index_ == bits) return ret;
if (ret.index_ == bits) break;
#else // ARCH_CPU_LITTLE_ENDIAN
if (ret.index_ == std::numeric_limits<size_t>::max()) return ret;
if (ret.index_ == std::numeric_limits<size_t>::max()) break;
#endif
}
}
Expand Down Expand Up @@ -141,9 +141,9 @@ class BitIteratorLE {
while (!(*ret)) {
--ret;
#if ARCH_CPU_LITTLE_ENDIAN
if (ret.index_ == 0) return ret;
if (ret.index_ == std::numeric_limits<size_t>::max()) break;
#else // ARCH_CPU_BIG_ENDIAN
if (ret.index_ == bits - 1) return ret;
if (ret.index_ == bits) break;
#endif
}
return ++ret;
Expand Down
4 changes: 4 additions & 0 deletions tachyon/math/base/bit_iterator_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ TEST(GmpUtilTest, BitIteratorLE) {
std::vector<bool> answers;
} tests[] = {
{BigInt<1>(0), std::vector<bool>{}},
{BigInt<1>(1), std::vector<bool>{1}},
{BigInt<1>(2), std::vector<bool>{0, 1}},
{BigInt<1>(3), std::vector<bool>{1, 1}},
{BigInt<1>(4), std::vector<bool>{0, 0, 1}},
{BigInt<1>(5), std::vector<bool>{1, 0, 1}},
};
Expand Down Expand Up @@ -54,7 +56,9 @@ TEST(GmpUtilTest, BitIteratorBE) {
std::vector<bool> answers;
} tests[] = {
{BigInt<1>(0), std::vector<bool>{}},
{BigInt<1>(1), std::vector<bool>{1}},
{BigInt<1>(2), std::vector<bool>{1, 0}},
{BigInt<1>(3), std::vector<bool>{1, 1}},
{BigInt<1>(4), std::vector<bool>{1, 0, 0}},
{BigInt<1>(5), std::vector<bool>{1, 0, 1}},
};
Expand Down
31 changes: 26 additions & 5 deletions tachyon/math/polynomials/univariate/sparse_polynomial_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class SparseUnivariatePolynomialTest : public testing::Test {
polys_.push_back(Poly(Coeffs({{0, GF7(3)}})));
polys_.push_back(Poly(Coeffs({{3, GF7(5)}})));
polys_.push_back(Poly(Coeffs({{4, GF7(5)}})));
polys_.push_back(Poly(Coeffs({{0, GF7(3)}, {1, GF7(4)}})));
polys_.push_back(Poly(Coeffs({{0, GF7(3)}, {1, GF7(4)}, {2, GF7(1)}})));
polys_.push_back(Poly::Zero());
}
SparseUnivariatePolynomialTest(const SparseUnivariatePolynomialTest&) =
Expand Down Expand Up @@ -75,7 +77,9 @@ TEST_F(SparseUnivariatePolynomialTest, IndexingOperator) {
{polys_[1], {3}},
{polys_[2], {std::nullopt, std::nullopt, std::nullopt, 5}},
{polys_[3], {std::nullopt, std::nullopt, std::nullopt, std::nullopt, 5}},
{polys_[4], {}},
{polys_[4], {3, 4}},
{polys_[5], {3, 4, 1}},
{polys_[6], {}},
};

for (const auto& test : tests) {
Expand All @@ -98,8 +102,8 @@ TEST_F(SparseUnivariatePolynomialTest, Degree) {
const Poly& poly;
size_t degree;
} tests[] = {
{polys_[0], 4}, {polys_[1], 0}, {polys_[2], 3},
{polys_[3], 4}, {polys_[4], 0},
{polys_[0], 4}, {polys_[1], 0}, {polys_[2], 3}, {polys_[3], 4},
{polys_[4], 1}, {polys_[5], 2}, {polys_[6], 0},
};

for (const auto& test : tests) {
Expand All @@ -113,7 +117,8 @@ TEST_F(SparseUnivariatePolynomialTest, Evaluate) {
GF7 expected;
} tests[] = {
{polys_[0], GF7(6)}, {polys_[1], GF7(3)}, {polys_[2], GF7(2)},
{polys_[3], GF7(6)}, {polys_[4], GF7(0)},
{polys_[3], GF7(6)}, {polys_[4], GF7(1)}, {polys_[5], GF7(3)},
{polys_[6], GF7(0)},
};

for (const auto& test : tests) {
Expand All @@ -130,7 +135,9 @@ TEST_F(SparseUnivariatePolynomialTest, ToString) {
{polys_[1], "3"},
{polys_[2], "5 * x^3"},
{polys_[3], "5 * x^4"},
{polys_[4], ""},
{polys_[4], "4 * x + 3"},
{polys_[5], "1 * x^2 + 4 * x + 3"},
{polys_[6], ""},
};

for (const auto& test : tests) {
Expand Down Expand Up @@ -170,6 +177,20 @@ TEST_F(SparseUnivariatePolynomialTest, AdditiveOperators) {
{
polys_[0],
polys_[4],
Poly(Coeffs({{0, GF7(6)}, {1, GF7(4)}, {2, GF7(1)}, {4, GF7(2)}})),
Poly(Coeffs({{1, GF7(3)}, {2, GF7(1)}, {4, GF7(2)}})),
Poly(Coeffs({{1, GF7(4)}, {2, GF7(6)}, {4, GF7(5)}})),
},
{
polys_[0],
polys_[5],
Poly(Coeffs({{0, GF7(6)}, {1, GF7(4)}, {2, GF7(2)}, {4, GF7(2)}})),
Poly(Coeffs({{1, GF7(3)}, {4, GF7(2)}})),
Poly(Coeffs({{1, GF7(4)}, {4, GF7(5)}})),
},
{
polys_[0],
polys_[6],
Poly(Coeffs({{0, GF7(3)}, {2, GF7(1)}, {4, GF7(2)}})),
Poly(Coeffs({{0, GF7(3)}, {2, GF7(1)}, {4, GF7(2)}})),
Poly(Coeffs({{0, GF7(4)}, {2, GF7(6)}, {4, GF7(5)}})),
Expand Down

0 comments on commit 8c1deaf

Please sign in to comment.