From 8bfe51d7a1cc64ee4d8640746f7200c9bb55f710 Mon Sep 17 00:00:00 2001 From: Insun Date: Sat, 22 Jun 2024 02:18:49 +0900 Subject: [PATCH] feat(math): implement `GreateCommonDivisor()` logic Implement GCD logic for BigInt type. See https://github.com/openfheorg/openfhe-development/blob/94fd76a1d965cfde13f2a540d78ce64146fc2700/src/core/include/math/nbtheory-impl.h#L241-L252 --- tachyon/math/base/BUILD.bazel | 7 +++ tachyon/math/base/number_theory.h | 55 +++++++++++++++++++++ tachyon/math/base/number_theory_unittest.cc | 36 ++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 tachyon/math/base/number_theory.h create mode 100644 tachyon/math/base/number_theory_unittest.cc diff --git a/tachyon/math/base/BUILD.bazel b/tachyon/math/base/BUILD.bazel index 2180ae036..6494da73c 100644 --- a/tachyon/math/base/BUILD.bazel +++ b/tachyon/math/base/BUILD.bazel @@ -78,6 +78,11 @@ tachyon_cc_library( ], ) +tachyon_cc_library( + name = "number_theory", + hdrs = ["number_theory.h"], +) + tachyon_cc_library( name = "rational_field", hdrs = ["rational_field.h"], @@ -158,6 +163,7 @@ tachyon_cc_unittest( "egcd_unittest.cc", "field_unittest.cc", "groups_unittest.cc", + "number_theory_unittest.cc", "rational_field_unittest.cc", "semigroups_unittest.cc", "sign_unittest.cc", @@ -168,6 +174,7 @@ tachyon_cc_unittest( ":bit_iterator", ":egcd", ":groups", + ":number_theory", ":rational_field", ":sign", ":simd_int", diff --git a/tachyon/math/base/number_theory.h b/tachyon/math/base/number_theory.h new file mode 100644 index 000000000..83c096c80 --- /dev/null +++ b/tachyon/math/base/number_theory.h @@ -0,0 +1,55 @@ +//================================================================================== +// BSD 2-Clause License +// +// Copyright (c) 2014-2023, NJIT, Duality Technologies Inc. and other +// contributors +// +// All rights reserved. +// +// Author TPOC: contact@openfhe.org +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this +// list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +//================================================================================== + +#ifndef TACHYON_MATH_BASE_NUMBER_THEORY_H_ +#define TACHYON_MATH_BASE_NUMBER_THEORY_H_ + +namespace tachyon::math { + +// TODO(Insun35): support GCD logic for native int type. +template +BigIntTy GreatestCommonDivisor(const BigIntTy& a, const BigIntTy& b) { + BigIntTy m_a = a; + BigIntTy m_b = b; + while (m_b != BigIntTy::Zero()) { + BigIntTy tmp = m_b; + m_b = m_a % m_b; + m_a = tmp; + } + return m_a; +} + +} // namespace tachyon::math + +#endif // TACHYON_MATH_BASE_NUMBER_THEORY_H_ diff --git a/tachyon/math/base/number_theory_unittest.cc b/tachyon/math/base/number_theory_unittest.cc new file mode 100644 index 000000000..88fd15505 --- /dev/null +++ b/tachyon/math/base/number_theory_unittest.cc @@ -0,0 +1,36 @@ +#include "tachyon/math/base/number_theory.h" + +#include "gtest/gtest.h" + +#include "tachyon/math/base/big_int.h" + +namespace tachyon::math { + +TEST(NumberTheoryTest, GreatestCommonDivisorSmallNumber) { + BigInt<1> a = BigInt<1>(10403); + BigInt<1> b = BigInt<1>(103); + BigInt<1> c = GreatestCommonDivisor(a, b); + + BigInt<1> expected_result = BigInt<1>(103); + + EXPECT_EQ(expected_result, c); + + a = BigInt<1>(1048576); + b = BigInt<1>(4096); + + c = GreatestCommonDivisor(a, b); + + EXPECT_EQ(b, c); +} + +TEST(NumberTheoryTest, GreatestCommonDivisorBigNumber) { + BigInt<2> a = *BigInt<2>::FromDecString("883035439563027"); + BigInt<2> b = *BigInt<2>::FromDecString("3042269397984931"); + BigInt<2> c = GreatestCommonDivisor(a, b); + + BigInt<2> expected_result = BigInt<2>(1); + + EXPECT_EQ(expected_result, c); +} + +} // namespace tachyon::math