Browse Source

Support BS autoconfig KIKIMR-19031

alexvru 1 year ago
parent
commit
d6f67906ea

+ 2 - 0
library/cpp/openssl/CMakeLists.txt

@@ -6,6 +6,8 @@
 # original buildsystem will not be accepted.
 
 
+add_subdirectory(big_integer)
+add_subdirectory(crypto)
 add_subdirectory(holders)
 add_subdirectory(init)
 add_subdirectory(io)

+ 19 - 0
library/cpp/openssl/big_integer/CMakeLists.darwin-x86_64.txt

@@ -0,0 +1,19 @@
+
+# This file was generated by the build system used internally in the Yandex monorepo.
+# Only simple modifications are allowed (adding source-files to targets, adding simple properties
+# like target_include_directories). These modifications will be ported to original
+# ya.make files by maintainers. Any complex modifications which can't be ported back to the
+# original buildsystem will not be accepted.
+
+
+find_package(OpenSSL REQUIRED)
+
+add_library(cpp-openssl-big_integer)
+target_link_libraries(cpp-openssl-big_integer PUBLIC
+  contrib-libs-cxxsupp
+  yutil
+  OpenSSL::OpenSSL
+)
+target_sources(cpp-openssl-big_integer PRIVATE
+  ${CMAKE_SOURCE_DIR}/library/cpp/openssl/big_integer/big_integer.cpp
+)

+ 20 - 0
library/cpp/openssl/big_integer/CMakeLists.linux-aarch64.txt

@@ -0,0 +1,20 @@
+
+# This file was generated by the build system used internally in the Yandex monorepo.
+# Only simple modifications are allowed (adding source-files to targets, adding simple properties
+# like target_include_directories). These modifications will be ported to original
+# ya.make files by maintainers. Any complex modifications which can't be ported back to the
+# original buildsystem will not be accepted.
+
+
+find_package(OpenSSL REQUIRED)
+
+add_library(cpp-openssl-big_integer)
+target_link_libraries(cpp-openssl-big_integer PUBLIC
+  contrib-libs-linux-headers
+  contrib-libs-cxxsupp
+  yutil
+  OpenSSL::OpenSSL
+)
+target_sources(cpp-openssl-big_integer PRIVATE
+  ${CMAKE_SOURCE_DIR}/library/cpp/openssl/big_integer/big_integer.cpp
+)

+ 20 - 0
library/cpp/openssl/big_integer/CMakeLists.linux-x86_64.txt

@@ -0,0 +1,20 @@
+
+# This file was generated by the build system used internally in the Yandex monorepo.
+# Only simple modifications are allowed (adding source-files to targets, adding simple properties
+# like target_include_directories). These modifications will be ported to original
+# ya.make files by maintainers. Any complex modifications which can't be ported back to the
+# original buildsystem will not be accepted.
+
+
+find_package(OpenSSL REQUIRED)
+
+add_library(cpp-openssl-big_integer)
+target_link_libraries(cpp-openssl-big_integer PUBLIC
+  contrib-libs-linux-headers
+  contrib-libs-cxxsupp
+  yutil
+  OpenSSL::OpenSSL
+)
+target_sources(cpp-openssl-big_integer PRIVATE
+  ${CMAKE_SOURCE_DIR}/library/cpp/openssl/big_integer/big_integer.cpp
+)

+ 17 - 0
library/cpp/openssl/big_integer/CMakeLists.txt

@@ -0,0 +1,17 @@
+
+# This file was generated by the build system used internally in the Yandex monorepo.
+# Only simple modifications are allowed (adding source-files to targets, adding simple properties
+# like target_include_directories). These modifications will be ported to original
+# ya.make files by maintainers. Any complex modifications which can't be ported back to the
+# original buildsystem will not be accepted.
+
+
+if (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" AND NOT HAVE_CUDA)
+  include(CMakeLists.linux-aarch64.txt)
+elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
+  include(CMakeLists.darwin-x86_64.txt)
+elseif (WIN32 AND CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64" AND NOT HAVE_CUDA)
+  include(CMakeLists.windows-x86_64.txt)
+elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" AND NOT HAVE_CUDA)
+  include(CMakeLists.linux-x86_64.txt)
+endif()

+ 19 - 0
library/cpp/openssl/big_integer/CMakeLists.windows-x86_64.txt

@@ -0,0 +1,19 @@
+
+# This file was generated by the build system used internally in the Yandex monorepo.
+# Only simple modifications are allowed (adding source-files to targets, adding simple properties
+# like target_include_directories). These modifications will be ported to original
+# ya.make files by maintainers. Any complex modifications which can't be ported back to the
+# original buildsystem will not be accepted.
+
+
+find_package(OpenSSL REQUIRED)
+
+add_library(cpp-openssl-big_integer)
+target_link_libraries(cpp-openssl-big_integer PUBLIC
+  contrib-libs-cxxsupp
+  yutil
+  OpenSSL::OpenSSL
+)
+target_sources(cpp-openssl-big_integer PRIVATE
+  ${CMAKE_SOURCE_DIR}/library/cpp/openssl/big_integer/big_integer.cpp
+)

+ 61 - 0
library/cpp/openssl/big_integer/big_integer.cpp

@@ -0,0 +1,61 @@
+#include "big_integer.h"
+
+#include <util/generic/yexception.h>
+#include <util/generic/scope.h>
+#include <util/stream/output.h>
+
+#include <openssl/bn.h>
+
+using namespace NOpenSsl;
+
+TBigInteger::~TBigInteger() noexcept {
+    BN_free(Impl_);
+}
+
+TBigInteger TBigInteger::FromULong(ui64 value) {
+    TBigInteger result(BN_new());
+
+    Y_ENSURE(result.Impl(), "BN_new() failed");
+    Y_ENSURE(BN_set_word(result.Impl(), value) == 1, "BN_set_word() failed");
+
+    return result;
+}
+
+TBigInteger TBigInteger::FromRegion(const void* ptr, size_t len) {
+    auto result = BN_bin2bn((ui8*)(ptr), len, nullptr);
+
+    Y_ENSURE(result, "BN_bin2bn() failed");
+
+    return result;
+}
+
+int TBigInteger::Compare(const TBigInteger& a, const TBigInteger& b) noexcept {
+    return BN_cmp(a.Impl(), b.Impl());
+}
+
+size_t TBigInteger::NumBytes() const noexcept {
+    return BN_num_bytes(Impl_);
+}
+
+size_t TBigInteger::ToRegion(void* to) const noexcept {
+    const auto ret = BN_bn2bin(Impl_, (unsigned char*)to);
+
+    Y_VERIFY(ret >= 0, "it happens");
+
+    return ret;
+}
+
+TString TBigInteger::ToDecimalString() const {
+    auto res = BN_bn2dec(Impl_);
+
+    Y_DEFER {
+        OPENSSL_free(res);
+    };
+
+    return res;
+}
+
+template <>
+void Out<TBigInteger>(IOutputStream& out, const TBigInteger& bi) {
+    out << bi.ToDecimalString();
+}

+ 57 - 0
library/cpp/openssl/big_integer/big_integer.h

@@ -0,0 +1,57 @@
+#pragma once
+
+#include <util/generic/ptr.h>
+#include <util/generic/strbuf.h>
+#include <util/generic/utility.h>
+#include <util/generic/string.h>
+
+struct bignum_st;
+
+namespace NOpenSsl {
+    class TBigInteger {
+        inline TBigInteger(bignum_st* impl) noexcept
+            : Impl_(impl)
+        {
+        }
+
+        static int Compare(const TBigInteger& a, const TBigInteger& b) noexcept;
+
+    public:
+        inline TBigInteger(TBigInteger&& other) noexcept {
+            Swap(other);
+        }
+
+        ~TBigInteger() noexcept;
+
+        static TBigInteger FromULong(ui64 value);
+        static TBigInteger FromRegion(const void* ptr, size_t len);
+
+        inline const bignum_st* Impl() const noexcept {
+            return Impl_;
+        }
+
+        inline bignum_st* Impl() noexcept {
+            return Impl_;
+        }
+
+        inline void Swap(TBigInteger& other) noexcept {
+            DoSwap(Impl_, other.Impl_);
+        }
+
+        inline friend bool operator==(const TBigInteger& a, const TBigInteger& b) noexcept {
+            return Compare(a, b) == 0;
+        }
+
+        inline friend bool operator!=(const TBigInteger& a, const TBigInteger& b) noexcept {
+            return !(a == b);
+        }
+
+        size_t NumBytes() const noexcept;
+        size_t ToRegion(void* to) const noexcept;
+
+        TString ToDecimalString() const;
+
+    private:
+        bignum_st* Impl_ = nullptr;
+    };
+}

+ 43 - 0
library/cpp/openssl/big_integer/ut/big_integer_ut.cpp

@@ -0,0 +1,43 @@
+#include "big_integer.h"
+
+#include <library/cpp/testing/unittest/registar.h>
+
+#include <util/system/byteorder.h>
+#include <util/stream/str.h>
+
+Y_UNIT_TEST_SUITE(BigInteger) {
+    using NOpenSsl::TBigInteger;
+
+    Y_UNIT_TEST(Initialization) {
+        constexpr ui64 testVal = 12345678900;
+        const auto fromULong = TBigInteger::FromULong(testVal);
+
+        const ui64 testArea = HostToInet(testVal); // transform to big-endian
+        const auto fromRegion = TBigInteger::FromRegion(&testArea, sizeof(testArea));
+        UNIT_ASSERT(fromULong == fromRegion);
+        UNIT_ASSERT_VALUES_EQUAL(fromULong, fromRegion);
+
+        const auto fromULongOther = TBigInteger::FromULong(22345678900);
+        UNIT_ASSERT(fromULong != fromULongOther);
+    }
+
+    Y_UNIT_TEST(Decimal) {
+        UNIT_ASSERT_VALUES_EQUAL(TBigInteger::FromULong(123456789).ToDecimalString(), "123456789");
+    }
+
+    Y_UNIT_TEST(Region) {
+        const auto v1 = TBigInteger::FromULong(1234567890);
+        char buf[1024];
+        const auto v2 = TBigInteger::FromRegion(buf, v1.ToRegion(buf));
+
+        UNIT_ASSERT_VALUES_EQUAL(v1, v2);
+    }
+
+    Y_UNIT_TEST(Output) {
+        TStringStream ss;
+
+        ss << TBigInteger::FromULong(123456789);
+
+        UNIT_ASSERT_VALUES_EQUAL(ss.Str(), "123456789");
+    }
+}

+ 7 - 0
library/cpp/openssl/big_integer/ut/ya.make

@@ -0,0 +1,7 @@
+UNITTEST_FOR(library/cpp/openssl/big_integer)
+
+SRCS(
+    big_integer_ut.cpp
+)
+
+END()

Some files were not shown because too many files changed in this diff