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
345
346
347
348
349
350
351
352
353
354
355
356
use crate::ffi::{
    boxed::{AscCow, AscRef},
    num::{AscBigDecimal, AscBigInt},
    str::AscString,
    sys,
};
use std::{
    cmp::Ordering,
    convert::Infallible,
    fmt::{self, Debug, Display, Formatter, LowerHex, UpperHex},
    str::FromStr,
};

/// A arbitrarily sized integer type.
#[derive(Clone)]
pub struct BigInt {
    inner: AscCow<'static, AscBigInt>,
}

impl BigInt {
    pub(crate) fn from_raw(raw: &'static AscRef<AscBigInt>) -> Self {
        Self {
            inner: raw.borrowed(),
        }
    }

    pub(crate) fn as_raw(&self) -> &AscRef<AscBigInt> {
        &self.inner
    }

    /// Creates a new big integer.
    pub fn new(x: i128) -> Self {
        Self::from_signed_bytes_le(x.to_le_bytes().as_slice())
    }

    /// Creates a new big integer value from it signed little-endian
    /// reprensentation. Unsigned integers need to ensure that the most
    /// significant bit is **not** set.
    pub fn from_signed_bytes_le(bytes: impl AsRef<[u8]>) -> Self {
        Self {
            inner: AscBigInt::from_bytes(bytes.as_ref()).owned(),
        }
    }

    /// Returns the sign of the integer.
    pub fn signum(&self) -> i32 {
        signum_le(self.inner.as_slice())
    }

    /// Parses a big integer from a string.
    pub fn parse(s: impl AsRef<str>) -> Self {
        let s = AscString::new(s.as_ref());
        let result = unsafe { &*sys::big_int__from_string(s.as_ptr()) };
        Self::from_raw(result)
    }

    /// Returns the sum of two big integers.
    pub fn plus(&self, rhs: &Self) -> Self {
        self.op(rhs, sys::big_int__plus)
    }

    /// Returns the difference of two big integers.
    pub fn minus(&self, rhs: &Self) -> Self {
        self.op(rhs, sys::big_int__minus)
    }

    /// Returns the product of two big integers.
    pub fn times(&self, rhs: &Self) -> Self {
        self.op(rhs, sys::big_int__times)
    }

    /// Returns the division of two big integers.
    pub fn divided_by(&self, rhs: &Self) -> Self {
        self.op(rhs, sys::big_int__divided_by)
    }

    /// Returns the division of a big integer by a big decimal.
    pub fn divided_by_decimal(&self, rhs: &BigDecimal) -> BigDecimal {
        BigDecimal::from_raw(unsafe {
            &*sys::big_int__divided_by_decimal(self.as_raw().as_ptr(), rhs.as_raw().as_ptr())
        })
    }

    /// Returns the remainder of two big integers.
    pub fn rem(&self, rhs: &Self) -> Self {
        self.op(rhs, sys::big_int__mod)
    }

    /// function pow(x: BigInt, exp: u8): BigInt
    pub fn pow(&self, exp: u8) -> Self {
        Self::from_raw(unsafe { &*sys::big_int__pow(self.as_raw().as_ptr(), exp) })
    }

    /// Returns the bit-wise or of two big integers.
    pub fn bit_or(&self, rhs: &Self) -> Self {
        self.op(rhs, sys::big_int__bit_or)
    }

    /// Returns the bit-wise and of two big integers.
    pub fn bit_and(&self, rhs: &Self) -> Self {
        self.op(rhs, sys::big_int__bit_and)
    }

    /// Returns the left shift by `rhs` bits.
    pub fn left_shift(&self, rhs: u8) -> Self {
        Self::from_raw(unsafe { &*sys::big_int__left_shift(self.as_raw().as_ptr(), rhs) })
    }

    /// Returns the arithmetic right shift by `rhs` bits.
    pub fn right_shift(&self, rhs: u8) -> Self {
        Self::from_raw(unsafe { &*sys::big_int__right_shift(self.as_raw().as_ptr(), rhs) })
    }

    fn op(
        &self,
        other: &BigInt,
        op: unsafe extern "C" fn(
            *const AscRef<AscBigInt>,
            *const AscRef<AscBigInt>,
        ) -> *const AscRef<AscBigInt>,
    ) -> Self {
        Self::from_raw(unsafe { &*op(self.as_raw().as_ptr(), other.as_raw().as_ptr()) })
    }
}

fn signum_le(bytes: &[u8]) -> i32 {
    if bytes.last().copied().unwrap_or_default() > 0x7f {
        -1
    } else if bytes.iter().copied().all(|b| b == 0) {
        0
    } else {
        1
    }
}

impl Debug for BigInt {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        // NOTE: Work around `Formatter::debug_{lower,upper}_hex` being private
        // and not stabilized.
        #[allow(deprecated)]
        let flags = f.flags();
        const DEBUG_LOWER_HEX: u32 = 1 << 4;
        const DEBUG_UPPER_HEX: u32 = 1 << 5;

        if flags & DEBUG_LOWER_HEX != 0 {
            LowerHex::fmt(self, f)
        } else if flags & DEBUG_UPPER_HEX != 0 {
            UpperHex::fmt(self, f)
        } else {
            Display::fmt(self, f)
        }
    }
}

impl Default for BigInt {
    fn default() -> Self {
        Self::new(0)
    }
}

impl Display for BigInt {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let str = unsafe { &*sys::type_conversion__big_int_to_string(self.inner.as_ptr()) };

        let str = str.to_string_lossy();
        let (is_non_negative, abs) = match str.strip_prefix('-') {
            Some(abs) => (false, abs),
            None => (true, str.as_str()),
        };

        f.pad_integral(is_non_negative, "", abs)
    }
}

impl Eq for BigInt {}

impl FromStr for BigInt {
    type Err = Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self::parse(s))
    }
}

impl LowerHex for BigInt {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        fmt_hex(self, f, str::make_ascii_lowercase)
    }
}

impl Ord for BigInt {
    fn cmp(&self, other: &Self) -> Ordering {
        self.minus(other).signum().cmp(&0)
    }
}

impl PartialEq for BigInt {
    fn eq(&self, other: &Self) -> bool {
        matches!(self.cmp(other), Ordering::Equal)
    }
}

impl PartialOrd for BigInt {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl UpperHex for BigInt {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        fmt_hex(self, f, str::make_ascii_uppercase)
    }
}

fn fmt_hex(value: &BigInt, f: &mut Formatter, transform: impl FnOnce(&mut str)) -> fmt::Result {
    let str = unsafe { &*sys::type_conversion__big_int_to_hex(&*value.inner as _) };

    let mut str = str.to_string_lossy();
    let str = match str.starts_with("0x") {
        true => unsafe { str.get_unchecked_mut(2..) },
        false => str.as_mut_str(),
    };

    transform(str);
    // NOTE: Unexpectedly, negative numbers are being encoded as the hex of
    // their absolute value. This means we manually want to check whether or not
    // the number is negative in Rust and not rely on the host.
    let is_non_negative = value.signum() >= 0;
    let abs = str.strip_prefix('-').unwrap_or(str);

    f.pad_integral(is_non_negative, "0x", abs)
}

/// An arbitrary precision decimal number.
#[derive(Clone)]
pub struct BigDecimal {
    inner: AscCow<'static, AscBigDecimal>,
}

impl BigDecimal {
    pub(crate) fn from_raw(raw: &'static AscRef<AscBigDecimal>) -> Self {
        Self {
            inner: raw.borrowed(),
        }
    }

    pub(crate) fn as_raw(&self) -> &AscRef<AscBigDecimal> {
        &self.inner
    }

    /// Creates a new big decimal value.
    pub fn new(value: i128) -> Self {
        Self::from_big_int(BigInt::new(value))
    }

    /// Creates a new decimal value from the specified [`BigInt`].
    pub fn from_big_int(value: BigInt) -> Self {
        let value = AscBigDecimal::new(value.inner.into_owned(), AscBigInt::from_bytes(&[0]));
        Self {
            inner: value.owned(),
        }
    }

    /// Parses a big decimal from a string.
    pub fn parse(s: impl AsRef<str>) -> Self {
        let s = AscString::new(s.as_ref());
        let result = unsafe { &*sys::big_decimal__from_string(s.as_ptr()) };
        Self::from_raw(result)
    }

    /// Returns the addition of two big decimals.
    pub fn plus(&self, rhs: &Self) -> Self {
        self.op(rhs, sys::big_decimal__plus)
    }

    /// Returns the difference of two big decimals.
    pub fn minus(&self, rhs: &Self) -> Self {
        self.op(rhs, sys::big_decimal__minus)
    }

    /// Returns the product of two big decimals.
    pub fn times(&self, rhs: &Self) -> Self {
        self.op(rhs, sys::big_decimal__times)
    }

    /// Returns the division of two big decimals.
    pub fn divided_by(&self, rhs: &Self) -> Self {
        self.op(rhs, sys::big_decimal__divided_by)
    }

    fn op(
        &self,
        other: &BigDecimal,
        op: unsafe extern "C" fn(
            *const AscRef<AscBigDecimal>,
            *const AscRef<AscBigDecimal>,
        ) -> *const AscRef<AscBigDecimal>,
    ) -> Self {
        Self::from_raw(unsafe { &*op(self.as_raw().as_ptr(), other.as_raw().as_ptr()) })
    }
}

impl Debug for BigDecimal {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        Display::fmt(self, f)
    }
}

impl Default for BigDecimal {
    fn default() -> Self {
        Self::new(0)
    }
}

impl Display for BigDecimal {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let str = unsafe { &*sys::big_decimal__to_string(self.inner.as_ptr()) };

        let str = str.to_string_lossy();
        let (is_non_negative, abs) = match str.strip_prefix('-') {
            Some(abs) => (false, abs),
            None => (true, str.as_str()),
        };

        f.pad_integral(is_non_negative, "", abs)
    }
}

impl Eq for BigDecimal {}

impl FromStr for BigDecimal {
    type Err = Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self::parse(s))
    }
}

impl Ord for BigDecimal {
    fn cmp(&self, other: &Self) -> Ordering {
        let diff = self.minus(other);
        signum_le(diff.as_raw().digits().as_slice()).cmp(&0)
    }
}

impl PartialEq for BigDecimal {
    fn eq(&self, other: &Self) -> bool {
        unsafe { sys::big_decimal__equals(self.as_raw().as_ptr(), other.as_raw().as_ptr()) }
    }
}

impl PartialOrd for BigDecimal {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}