File: bignum.h

package info (click to toggle)
golang-github-google-certificate-transparency 0.0~git20160709.0.0f6e3d1~ds1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 5,676 kB
  • sloc: cpp: 35,278; python: 11,838; java: 1,911; sh: 1,885; makefile: 950; xml: 520; ansic: 225
file content (173 lines) | stat: -rw-r--r-- 3,125 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#ifndef CERT_TRANS_UTIL_BIGNUM_H_
#define CERT_TRANS_UTIL_BIGNUM_H_

#include <openssl/bn.h>
#include <cassert>

namespace cert_trans {


class BigNum {
 public:
  BigNum();
  explicit BigNum(int64_t w);
  BigNum(const BigNum& other);
  ~BigNum();

  const BIGNUM* bn() const;
  BIGNUM* bn();

  int num_bits() const;

  BigNum& operator-=(const BigNum& n);
  BigNum& operator-=(int64_t n);
  BigNum& operator+=(const BigNum& n);
  BigNum& operator+=(int64_t n);
  BigNum& operator<<=(int n);
  BigNum& operator>>=(int n);

 private:
  BIGNUM bn_;

  friend bool operator==(const BigNum& a, const BigNum& b);
  friend bool operator==(const BigNum& a, int64_t b);
  friend bool operator<(const BigNum& a, const BigNum& b);
  friend bool operator<(const BigNum& a, int64_t b);
  friend bool operator>(const BigNum& a, const BigNum& b);
  friend bool operator>(const BigNum& a, int64_t b);
};


inline const BIGNUM* BigNum::bn() const {
  return &bn_;
}


inline BIGNUM* BigNum::bn() {
  return &bn_;
}


inline int BigNum::num_bits() const {
  return BN_num_bits(&bn_);
}


inline BigNum& BigNum::operator-=(const BigNum& n) {
  assert(BN_sub(&bn_, &bn_, &n.bn_) == 1);
  return *this;
}


inline BigNum& BigNum::operator-=(int64_t n) {
  assert(BN_sub_word(&bn_, n) == 1);
  return *this;
}


inline BigNum& BigNum::operator+=(const BigNum& n) {
  assert(BN_add(&bn_, &bn_, &n.bn_) == 1);
  return *this;
}


inline BigNum& BigNum::operator+=(int64_t n) {
  assert(BN_add_word(&bn_, n) == 1);
  return *this;
}


inline BigNum& BigNum::operator<<=(int n) {
  assert(BN_lshift(&bn_, &bn_, n) == 1);
  return *this;
}


inline BigNum& BigNum::operator>>=(int n) {
  assert(BN_rshift(&bn_, &bn_, n) == 1);
  return *this;
}


template <typename T, typename U>
inline BigNum operator+(const T& a, const U& b) {
  BigNum r(a);
  r += b;
  return r;
}


template <typename T, typename U>
inline BigNum operator-(const T& a, const U& b) {
  BigNum r(a);
  r -= b;
  return r;
}


inline BigNum operator<<(const BigNum& a, int n) {
  BigNum r(a);
  r <<= n;
  return r;
}


inline BigNum operator>>(const BigNum& a, int n) {
  BigNum r(a);
  r >>= n;
  return r;
}


inline bool operator==(const BigNum& a, const BigNum& b) {
  return BN_cmp(&a.bn_, &b.bn_) == 0;
}


namespace internal {


inline const BigNum& AsBigNum(const BigNum& n) {
  return n;
}


inline BigNum AsBigNum(int64_t n) {
  return BigNum(n);
}


}  // namespace internal


template <typename T, typename U>
inline bool operator==(const T& a, const U& b) {
  return internal::AsBigNum(a) == internal::AsBigNum(b);
}


inline bool operator<(const BigNum& a, const BigNum& b) {
  return BN_cmp(&a.bn_, &b.bn_) < 0;
}


template <typename T, typename U>
inline bool operator<(const T& a, const U& b) {
  return internal::AsBigNum(a) < internal::AsBigNum(b);
}


inline bool operator>(const BigNum& a, const BigNum& b) {
  return BN_cmp(&a.bn_, &b.bn_) > 0;
}


template <typename T, typename U>
inline bool operator>(const T& a, const U& b) {
  return internal::AsBigNum(a) > internal::AsBigNum(b);
}


}  // namespace cert_trans

#endif  // CERT_TRANS_UTIL_BIGNUM_H_