aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/openssl/big_integer/big_integer.cpp
blob: 49a60fd975badc9246782797cf955ff1ed13b081 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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_ABORT_UNLESS(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();
}