-
Notifications
You must be signed in to change notification settings - Fork 93
/
big_num.rs
344 lines (301 loc) · 9.49 KB
/
big_num.rs
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
///! 128 and 256 bit numbers
///! U128 is more efficient that u128
///! https://github.com/solana-labs/solana/issues/19549
use uint::construct_uint;
construct_uint! {
pub struct U128(2);
}
construct_uint! {
pub struct U256(4);
}
construct_uint! {
pub struct U512(8);
}
#[macro_export]
macro_rules! construct_bignum {
( $(#[$attr:meta])* $visibility:vis struct $name:ident ( $n_words:tt ); ) => {
$crate::construct_bignum! { @construct $(#[$attr])* $visibility struct $name ($n_words); }
impl $crate::core_::convert::From<u128> for $name {
fn from(value: u128) -> $name {
let mut ret = [0; $n_words];
ret[0] = value as u64;
ret[1] = (value >> 64) as u64;
$name(ret)
}
}
impl $crate::core_::convert::From<i128> for $name {
fn from(value: i128) -> $name {
match value >= 0 {
true => From::from(value as u128),
false => { panic!("Unsigned integer can't be created from negative value"); }
}
}
}
impl $name {
/// Low 2 words (u128)
#[inline]
pub const fn low_u128(&self) -> u128 {
let &$name(ref arr) = self;
((arr[1] as u128) << 64) + arr[0] as u128
}
/// Conversion to u128 with overflow checking
///
/// # Panics
///
/// Panics if the number is larger than 2^128.
#[inline]
pub fn as_u128(&self) -> u128 {
let &$name(ref arr) = self;
for i in 2..$n_words {
if arr[i] != 0 {
panic!("Integer overflow when casting to u128")
}
}
self.low_u128()
}
}
impl $crate::core_::convert::TryFrom<$name> for u128 {
type Error = &'static str;
#[inline]
fn try_from(u: $name) -> $crate::core_::result::Result<u128, &'static str> {
let $name(arr) = u;
for i in 2..$n_words {
if arr[i] != 0 {
return Err("integer overflow when casting to u128");
}
}
Ok(((arr[1] as u128) << 64) + arr[0] as u128)
}
}
impl $crate::core_::convert::TryFrom<$name> for i128 {
type Error = &'static str;
#[inline]
fn try_from(u: $name) -> $crate::core_::result::Result<i128, &'static str> {
let err_str = "integer overflow when casting to i128";
let i = u128::try_from(u).map_err(|_| err_str)?;
if i > i128::max_value() as u128 {
Err(err_str)
} else {
Ok(i as i128)
}
}
}
};
( @construct $(#[$attr:meta])* $visibility:vis struct $name:ident ( $n_words:tt ); ) => {
/// Little-endian large integer type
#[repr(C)]
$(#[$attr])*
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
$visibility struct $name (pub [u64; $n_words]);
/// Get a reference to the underlying little-endian words.
impl AsRef<[u64]> for $name {
#[inline]
fn as_ref(&self) -> &[u64] {
&self.0
}
}
impl<'a> From<&'a $name> for $name {
fn from(x: &'a $name) -> $name {
*x
}
}
impl $name {
/// Maximum value.
pub const MAX: $name = $name([u64::max_value(); $n_words]);
/// Conversion to usize with overflow checking
///
/// # Panics
///
/// Panics if the number is larger than usize::max_value().
#[inline]
pub fn as_usize(&self) -> usize {
let &$name(ref arr) = self;
if !self.fits_word() || arr[0] > usize::max_value() as u64 {
panic!("Integer overflow when casting to usize")
}
arr[0] as usize
}
/// Whether this is zero.
#[inline]
pub const fn is_zero(&self) -> bool {
let &$name(ref arr) = self;
let mut i = 0;
while i < $n_words { if arr[i] != 0 { return false; } else { i += 1; } }
return true;
}
// Whether this fits u64.
#[inline]
fn fits_word(&self) -> bool {
let &$name(ref arr) = self;
for i in 1..$n_words { if arr[i] != 0 { return false; } }
return true;
}
/// Return if specific bit is set.
///
/// # Panics
///
/// Panics if `index` exceeds the bit width of the number.
#[inline]
pub const fn bit(&self, index: usize) -> bool {
let &$name(ref arr) = self;
arr[index / 64] & (1 << (index % 64)) != 0
}
/// Returns the number of leading zeros in the binary representation of self.
pub fn leading_zeros(&self) -> u32 {
let mut r = 0;
for i in 0..$n_words {
let w = self.0[$n_words - i - 1];
if w == 0 {
r += 64;
} else {
r += w.leading_zeros();
break;
}
}
r
}
/// Returns the number of trailing zeros in the binary representation of self.
pub fn trailing_zeros(&self) -> u32 {
let mut r = 0;
for i in 0..$n_words {
let w = self.0[i];
if w == 0 {
r += 64;
} else {
r += w.trailing_zeros();
break;
}
}
r
}
/// Zero (additive identity) of this type.
#[inline]
pub const fn zero() -> Self {
Self([0; $n_words])
}
/// One (multiplicative identity) of this type.
#[inline]
pub const fn one() -> Self {
let mut words = [0; $n_words];
words[0] = 1u64;
Self(words)
}
/// The maximum value which can be inhabited by this type.
#[inline]
pub const fn max_value() -> Self {
Self::MAX
}
}
impl $crate::core_::default::Default for $name {
fn default() -> Self {
$name::zero()
}
}
impl $crate::core_::ops::BitAnd<$name> for $name {
type Output = $name;
#[inline]
fn bitand(self, other: $name) -> $name {
let $name(ref arr1) = self;
let $name(ref arr2) = other;
let mut ret = [0u64; $n_words];
for i in 0..$n_words {
ret[i] = arr1[i] & arr2[i];
}
$name(ret)
}
}
impl $crate::core_::ops::BitOr<$name> for $name {
type Output = $name;
#[inline]
fn bitor(self, other: $name) -> $name {
let $name(ref arr1) = self;
let $name(ref arr2) = other;
let mut ret = [0u64; $n_words];
for i in 0..$n_words {
ret[i] = arr1[i] | arr2[i];
}
$name(ret)
}
}
impl $crate::core_::ops::BitXor<$name> for $name {
type Output = $name;
#[inline]
fn bitxor(self, other: $name) -> $name {
let $name(ref arr1) = self;
let $name(ref arr2) = other;
let mut ret = [0u64; $n_words];
for i in 0..$n_words {
ret[i] = arr1[i] ^ arr2[i];
}
$name(ret)
}
}
impl $crate::core_::ops::Not for $name {
type Output = $name;
#[inline]
fn not(self) -> $name {
let $name(ref arr) = self;
let mut ret = [0u64; $n_words];
for i in 0..$n_words {
ret[i] = !arr[i];
}
$name(ret)
}
}
impl $crate::core_::ops::Shl<usize> for $name {
type Output = $name;
fn shl(self, shift: usize) -> $name {
let $name(ref original) = self;
let mut ret = [0u64; $n_words];
let word_shift = shift / 64;
let bit_shift = shift % 64;
// shift
for i in word_shift..$n_words {
ret[i] = original[i - word_shift] << bit_shift;
}
// carry
if bit_shift > 0 {
for i in word_shift+1..$n_words {
ret[i] += original[i - 1 - word_shift] >> (64 - bit_shift);
}
}
$name(ret)
}
}
impl<'a> $crate::core_::ops::Shl<usize> for &'a $name {
type Output = $name;
fn shl(self, shift: usize) -> $name {
*self << shift
}
}
impl $crate::core_::ops::Shr<usize> for $name {
type Output = $name;
fn shr(self, shift: usize) -> $name {
let $name(ref original) = self;
let mut ret = [0u64; $n_words];
let word_shift = shift / 64;
let bit_shift = shift % 64;
// shift
for i in word_shift..$n_words {
ret[i - word_shift] = original[i] >> bit_shift;
}
// Carry
if bit_shift > 0 {
for i in word_shift+1..$n_words {
ret[i - word_shift - 1] += original[i] << (64 - bit_shift);
}
}
$name(ret)
}
}
impl<'a> $crate::core_::ops::Shr<usize> for &'a $name {
type Output = $name;
fn shr(self, shift: usize) -> $name {
*self >> shift
}
}
};
}
construct_bignum! {
pub struct U1024(16);
}