diff --git a/tachyon/math/elliptic_curves/short_weierstrass/affine_point_unittest.cc b/tachyon/math/elliptic_curves/short_weierstrass/affine_point_unittest.cc index 80427ab9ee..992fd3aae3 100644 --- a/tachyon/math/elliptic_curves/short_weierstrass/affine_point_unittest.cc +++ b/tachyon/math/elliptic_curves/short_weierstrass/affine_point_unittest.cc @@ -177,7 +177,7 @@ TEST_F(AffinePointTest, Copyable) { TEST_F(AffinePointTest, JsonValueConverter) { test::AffinePoint expected_point(GF7(1), GF7(2)); - std::string expected_json = R"({"x":"0x1","y":"0x2"})"; + std::string expected_json = R"({"x":1,"y":2})"; test::AffinePoint p; std::string error; diff --git a/tachyon/math/elliptic_curves/short_weierstrass/jacobian_point_unittest.cc b/tachyon/math/elliptic_curves/short_weierstrass/jacobian_point_unittest.cc index 776a543f35..b8800eeaca 100644 --- a/tachyon/math/elliptic_curves/short_weierstrass/jacobian_point_unittest.cc +++ b/tachyon/math/elliptic_curves/short_weierstrass/jacobian_point_unittest.cc @@ -253,7 +253,7 @@ TEST_F(JacobianPointTest, Copyable) { TEST_F(JacobianPointTest, JsonValueConverter) { test::JacobianPoint expected_point(GF7(1), GF7(2), GF7(3)); - std::string expected_json = R"({"x":"0x1","y":"0x2","z":"0x3"})"; + std::string expected_json = R"({"x":1,"y":2,"z":3})"; test::JacobianPoint p; std::string error; diff --git a/tachyon/math/elliptic_curves/short_weierstrass/point_xyzz_unittest.cc b/tachyon/math/elliptic_curves/short_weierstrass/point_xyzz_unittest.cc index 61e38f568e..b9896723aa 100644 --- a/tachyon/math/elliptic_curves/short_weierstrass/point_xyzz_unittest.cc +++ b/tachyon/math/elliptic_curves/short_weierstrass/point_xyzz_unittest.cc @@ -256,7 +256,7 @@ TEST_F(PointXYZZTest, Copyable) { TEST_F(PointXYZZTest, JsonValueConverter) { test::PointXYZZ expected_point(GF7(1), GF7(2), GF7(3), GF7(4)); - std::string expected_json = R"({"x":"0x1","y":"0x2","zz":"0x3","zzz":"0x4"})"; + std::string expected_json = R"({"x":1,"y":2,"zz":3,"zzz":4})"; test::PointXYZZ p; std::string error; diff --git a/tachyon/math/elliptic_curves/short_weierstrass/projective_point_unittest.cc b/tachyon/math/elliptic_curves/short_weierstrass/projective_point_unittest.cc index 4370b4226f..669ece2b85 100644 --- a/tachyon/math/elliptic_curves/short_weierstrass/projective_point_unittest.cc +++ b/tachyon/math/elliptic_curves/short_weierstrass/projective_point_unittest.cc @@ -256,7 +256,7 @@ TEST_F(ProjectivePointTest, Copyable) { TEST_F(ProjectivePointTest, JsonValueConverter) { test::ProjectivePoint expected_point(GF7(1), GF7(2), GF7(3)); - std::string expected_json = R"({"x":"0x1","y":"0x2","z":"0x3"})"; + std::string expected_json = R"({"x":1,"y":2,"z":3})"; test::ProjectivePoint p; std::string error; diff --git a/tachyon/math/finite_fields/cubic_extension_field_unittest.cc b/tachyon/math/finite_fields/cubic_extension_field_unittest.cc index 39d5c7d5f8..4b35afe4d1 100644 --- a/tachyon/math/finite_fields/cubic_extension_field_unittest.cc +++ b/tachyon/math/finite_fields/cubic_extension_field_unittest.cc @@ -181,7 +181,7 @@ TEST_F(CubicExtensionFieldTest, MultiplicativeGroupOperators) { TEST_F(CubicExtensionFieldTest, JsonValueConverter) { GF7_3 expected_point(GF7(1), GF7(2), GF7(3)); - std::string expected_json = R"({"c0":"0x1","c1":"0x2","c2":"0x3"})"; + std::string expected_json = R"({"c0":1,"c1":2,"c2":3})"; GF7_3 p; std::string error; diff --git a/tachyon/math/finite_fields/prime_field_base.h b/tachyon/math/finite_fields/prime_field_base.h index e56d0cff06..1de8ca4b2a 100644 --- a/tachyon/math/finite_fields/prime_field_base.h +++ b/tachyon/math/finite_fields/prime_field_base.h @@ -222,6 +222,7 @@ template class RapidJsonValueConverter< T, std::enable_if_t, T>>> { public: + using value_type = typename T::value_type; using BigInt = typename T::BigIntTy; static bool s_allow_value_greater_than_or_equal_to_modulus; @@ -231,21 +232,32 @@ class RapidJsonValueConverter< static rapidjson::Value From(const T& value, Allocator& allocator) { if constexpr (T::Config::kUseMontgomery) { if (s_is_in_montgomery) { - return RapidJsonValueConverter::From(value.value(), allocator); + return RapidJsonValueConverter::From(value.value(), + allocator); } } - return RapidJsonValueConverter::From(value.ToBigInt(), allocator); + if constexpr (T::Config::kModulusBits <= 32) { + if constexpr (T::Config::kUseMontgomery) { + return RapidJsonValueConverter::From( + T::Config::FromMontgomery(value.value()), allocator); + } else { + return RapidJsonValueConverter::From(value.value(), + allocator); + } + } else { + return RapidJsonValueConverter::From(value.ToBigInt(), allocator); + } } static bool To(const rapidjson::Value& json_value, std::string_view key, T* value, std::string* error) { - BigInt v; - if (!RapidJsonValueConverter::To(json_value, key, &v, error)) + value_type v; + if (!RapidJsonValueConverter::To(json_value, key, &v, error)) return false; if (s_allow_value_greater_than_or_equal_to_modulus) { - if (v >= BigInt(T::Config::kModulus)) { - v = v.Mod(BigInt(T::Config::kModulus)); + if (v >= T::Config::kModulus) { + v %= T::Config::kModulus; } } if constexpr (T::Config::kUseMontgomery) { @@ -254,7 +266,7 @@ class RapidJsonValueConverter< return true; } } - *value = T::FromBigInt(v); + *value = T(v); return true; } }; diff --git a/tachyon/math/finite_fields/quadratic_extension_field_unittest.cc b/tachyon/math/finite_fields/quadratic_extension_field_unittest.cc index 09418c40f7..ee7be6c67c 100644 --- a/tachyon/math/finite_fields/quadratic_extension_field_unittest.cc +++ b/tachyon/math/finite_fields/quadratic_extension_field_unittest.cc @@ -193,7 +193,7 @@ TEST(CyclotomicInverseTest, FastCyclotomicInverse) { TEST_F(QuadraticExtensionFieldTest, JsonValueConverter) { GF7_2 expected_point(GF7(1), GF7(2)); - std::string expected_json = R"({"c0":"0x1","c1":"0x2"})"; + std::string expected_json = R"({"c0":1,"c1":2})"; GF7_2 p; std::string error; diff --git a/tachyon/math/geometry/point2_unittest.cc b/tachyon/math/geometry/point2_unittest.cc index f518a42cf8..4584e4b85d 100644 --- a/tachyon/math/geometry/point2_unittest.cc +++ b/tachyon/math/geometry/point2_unittest.cc @@ -55,7 +55,7 @@ TEST(Point2Test, Copyable) { TEST(Point2Test, JsonValueConverter) { Point2GF7 expected_point(GF7(1), GF7(2)); - std::string expected_json = R"({"x":"0x1","y":"0x2"})"; + std::string expected_json = R"({"x":1,"y":2})"; Point2GF7 p; std::string error; diff --git a/tachyon/math/geometry/point3_unittest.cc b/tachyon/math/geometry/point3_unittest.cc index bc4d11804f..6f91731d2c 100644 --- a/tachyon/math/geometry/point3_unittest.cc +++ b/tachyon/math/geometry/point3_unittest.cc @@ -57,7 +57,7 @@ TEST(Point3Test, Copyable) { TEST(Point3Test, JsonValueConverter) { Point3GF7 expected_point(GF7(1), GF7(2), GF7(3)); - std::string expected_json = R"({"x":"0x1","y":"0x2","z":"0x3"})"; + std::string expected_json = R"({"x":1,"y":2,"z":3})"; Point3GF7 p; std::string error; diff --git a/tachyon/math/geometry/point4_unittest.cc b/tachyon/math/geometry/point4_unittest.cc index 43acd10dd6..a1b148abbb 100644 --- a/tachyon/math/geometry/point4_unittest.cc +++ b/tachyon/math/geometry/point4_unittest.cc @@ -61,7 +61,7 @@ TEST(Point4Test, Copyable) { TEST(Point4Test, JsonValueConverter) { Point4GF7 expected_point(GF7(1), GF7(2), GF7(3), GF7(4)); - std::string expected_json = R"({"x":"0x1","y":"0x2","z":"0x3","w":"0x4"})"; + std::string expected_json = R"({"x":1,"y":2,"z":3,"w":4})"; Point4GF7 p; std::string error; diff --git a/tachyon/math/polynomials/univariate/univariate_dense_polynomial_unittest.cc b/tachyon/math/polynomials/univariate/univariate_dense_polynomial_unittest.cc index f73296d262..178a6e77cd 100644 --- a/tachyon/math/polynomials/univariate/univariate_dense_polynomial_unittest.cc +++ b/tachyon/math/polynomials/univariate/univariate_dense_polynomial_unittest.cc @@ -399,7 +399,7 @@ TEST_F(UnivariateDensePolynomialTest, Hash) { TEST_F(UnivariateDensePolynomialTest, JsonValueConverter) { Poly expected_poly(Coeffs({GF7(1), GF7(2), GF7(3), GF7(4), GF7(5)})); std::string expected_json = - R"({"coefficients":{"coefficients":["0x1","0x2","0x3","0x4","0x5"]}})"; + R"({"coefficients":{"coefficients":[1,2,3,4,5]}})"; Poly poly; std::string error; diff --git a/tachyon/math/polynomials/univariate/univariate_evaluations_unittest.cc b/tachyon/math/polynomials/univariate/univariate_evaluations_unittest.cc index 3c6214478f..2e8a5d12dc 100644 --- a/tachyon/math/polynomials/univariate/univariate_evaluations_unittest.cc +++ b/tachyon/math/polynomials/univariate/univariate_evaluations_unittest.cc @@ -285,8 +285,7 @@ TEST_F(UnivariateEvaluationsTest, Hash) { TEST_F(UnivariateEvaluationsTest, JsonValueConverter) { Poly expected_poly({GF7(1), GF7(2), GF7(3), GF7(4), GF7(5)}); - std::string expected_json = - R"({"evaluations":["0x1","0x2","0x3","0x4","0x5"]})"; + std::string expected_json = R"({"evaluations":[1,2,3,4,5]})"; Poly poly; std::string error; diff --git a/tachyon/math/polynomials/univariate/univariate_sparse_polynomial_unittest.cc b/tachyon/math/polynomials/univariate/univariate_sparse_polynomial_unittest.cc index 9e9dbc2ef0..fe37f5c138 100644 --- a/tachyon/math/polynomials/univariate/univariate_sparse_polynomial_unittest.cc +++ b/tachyon/math/polynomials/univariate/univariate_sparse_polynomial_unittest.cc @@ -451,7 +451,7 @@ TEST_F(UnivariateSparsePolynomialTest, Hash) { TEST_F(UnivariateSparsePolynomialTest, JsonValueConverter) { Poly expected_poly(Coeffs({{0, GF7(1)}, {1, GF7(2)}, {4, GF7(3)}})); std::string expected_json = - R"({"coefficients":{"terms":[{"degree":0,"coefficient":"0x1"},{"degree":1,"coefficient":"0x2"},{"degree":4,"coefficient":"0x3"}]}})"; + R"({"coefficients":{"terms":[{"degree":0,"coefficient":1},{"degree":1,"coefficient":2},{"degree":4,"coefficient":3}]}})"; Poly poly; std::string error;