2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
|
|
|
#if TD_HAVE_OPENSSL
|
|
|
|
|
|
|
|
#include "td/utils/Slice.h"
|
2018-08-09 20:03:21 +02:00
|
|
|
#include "td/utils/Status.h"
|
2018-08-10 01:36:05 +02:00
|
|
|
#include "td/utils/StringBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class BigNumContext {
|
|
|
|
public:
|
|
|
|
BigNumContext();
|
|
|
|
BigNumContext(const BigNumContext &other) = delete;
|
|
|
|
BigNumContext &operator=(const BigNumContext &other) = delete;
|
|
|
|
BigNumContext(BigNumContext &&other);
|
|
|
|
BigNumContext &operator=(BigNumContext &&other);
|
|
|
|
~BigNumContext();
|
|
|
|
|
|
|
|
private:
|
|
|
|
class Impl;
|
|
|
|
unique_ptr<Impl> impl_;
|
|
|
|
|
|
|
|
friend class BigNum;
|
|
|
|
};
|
|
|
|
|
|
|
|
class BigNum {
|
|
|
|
public:
|
|
|
|
BigNum();
|
|
|
|
BigNum(const BigNum &other);
|
|
|
|
BigNum &operator=(const BigNum &other);
|
|
|
|
BigNum(BigNum &&other);
|
|
|
|
BigNum &operator=(BigNum &&other);
|
|
|
|
~BigNum();
|
|
|
|
|
|
|
|
static BigNum from_binary(Slice str);
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
static BigNum from_le_binary(Slice str);
|
|
|
|
|
2018-08-09 20:03:21 +02:00
|
|
|
static Result<BigNum> from_decimal(CSlice str);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
static Result<BigNum> from_hex(CSlice str);
|
2018-08-13 19:15:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
static BigNum from_raw(void *openssl_big_num);
|
|
|
|
|
|
|
|
void set_value(uint32 new_value);
|
|
|
|
|
|
|
|
int get_num_bits() const;
|
|
|
|
|
|
|
|
int get_num_bytes() const;
|
|
|
|
|
|
|
|
void set_bit(int num);
|
|
|
|
|
|
|
|
void clear_bit(int num);
|
|
|
|
|
|
|
|
bool is_bit_set(int num) const;
|
|
|
|
|
|
|
|
bool is_prime(BigNumContext &context) const;
|
|
|
|
|
|
|
|
BigNum clone() const;
|
|
|
|
|
|
|
|
string to_binary(int exact_size = -1) const;
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
string to_le_binary(int exact_size = -1) const;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
string to_decimal() const;
|
|
|
|
|
|
|
|
void operator+=(uint32 value);
|
|
|
|
|
|
|
|
void operator-=(uint32 value);
|
|
|
|
|
|
|
|
void operator*=(uint32 value);
|
|
|
|
|
|
|
|
void operator/=(uint32 value);
|
|
|
|
|
|
|
|
uint32 operator%(uint32 value) const;
|
|
|
|
|
|
|
|
static void random(BigNum &r, int bits, int top, int bottom);
|
|
|
|
|
|
|
|
static void add(BigNum &r, const BigNum &a, const BigNum &b);
|
|
|
|
|
|
|
|
static void sub(BigNum &r, const BigNum &a, const BigNum &b);
|
|
|
|
|
|
|
|
static void mul(BigNum &r, BigNum &a, BigNum &b, BigNumContext &context);
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
static void mod_add(BigNum &r, BigNum &a, BigNum &b, const BigNum &m, BigNumContext &context);
|
|
|
|
|
|
|
|
static void mod_sub(BigNum &r, BigNum &a, BigNum &b, const BigNum &m, BigNumContext &context);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
static void mod_mul(BigNum &r, BigNum &a, BigNum &b, const BigNum &m, BigNumContext &context);
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
static void mod_inverse(BigNum &r, BigNum &a, const BigNum &m, BigNumContext &context);
|
2018-08-08 21:56:23 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
static void div(BigNum *quotient, BigNum *remainder, const BigNum ÷nd, const BigNum &divisor,
|
|
|
|
BigNumContext &context);
|
|
|
|
|
|
|
|
static void mod_exp(BigNum &r, const BigNum &a, const BigNum &p, const BigNum &m, BigNumContext &context);
|
|
|
|
|
|
|
|
static void gcd(BigNum &r, BigNum &a, BigNum &b, BigNumContext &context);
|
|
|
|
|
|
|
|
static int compare(const BigNum &a, const BigNum &b);
|
|
|
|
|
|
|
|
private:
|
|
|
|
class Impl;
|
|
|
|
unique_ptr<Impl> impl_;
|
|
|
|
|
|
|
|
explicit BigNum(unique_ptr<Impl> &&impl);
|
|
|
|
};
|
|
|
|
|
2018-08-10 01:36:05 +02:00
|
|
|
StringBuilder &operator<<(StringBuilder &sb, const BigNum &bn);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
#endif
|