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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Types for working with [`JsBigInt`].

use std::{error, fmt, mem::MaybeUninit};

use crate::{
    context::{internal::Env, Context},
    handle::{internal::TransparentNoCopyWrapper, Handle},
    result::{NeonResult, ResultExt},
    sys::{self, raw},
    types::{private, JsBigInt, Value},
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
/// Indicates if a `JsBigInt` is positive or negative
pub enum Sign {
    Positive,
    Negative,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
/// Indicates a lossless conversion from a [`JsBigInt`] to a Rust integer
/// could not be performed.
///
/// Failures include:
/// * Negative sign on an unsigned int
/// * Overflow of an int
/// * Underflow of a signed int
pub struct RangeError<T>(T);

impl<T> RangeError<T> {
    /// Get the lossy value read from a `BigInt`. It may be truncated,
    /// sign extended or wrapped.
    pub fn into_inner(self) -> T {
        self.0
    }
}

impl<T> fmt::Display for RangeError<T>
where
    T: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Loss of precision reading BigInt ({})", self.0)
    }
}

impl<T> error::Error for RangeError<T> where T: fmt::Display + fmt::Debug {}

impl<T, E> ResultExt<T> for Result<T, RangeError<E>>
where
    E: fmt::Display,
{
    fn or_throw<'a, C: Context<'a>>(self, cx: &mut C) -> NeonResult<T> {
        self.or_else(|err| cx.throw_range_error(err.to_string()))
    }
}

impl JsBigInt {
    pub const POSITIVE: Sign = Sign::Positive;
    pub const NEGATIVE: Sign = Sign::Negative;

    /// Creates a `BigInt` from an [`i64`].
    ///
    /// # Example
    ///
    /// ```
    /// # use neon::{prelude::*, types::JsBigInt};
    /// # fn example(mut cx: FunctionContext) -> JsResult<JsBigInt> {
    /// let value: Handle<JsBigInt> = JsBigInt::from_i64(&mut cx, 42);
    /// # Ok(value)
    /// # }
    /// ```
    pub fn from_i64<'cx, C>(cx: &mut C, n: i64) -> Handle<'cx, Self>
    where
        C: Context<'cx>,
    {
        let mut v = MaybeUninit::uninit();
        let v = unsafe {
            assert_eq!(
                sys::create_bigint_int64(cx.env().to_raw(), n, v.as_mut_ptr(),),
                sys::Status::Ok,
            );

            v.assume_init()
        };

        Handle::new_internal(Self(v))
    }

    /// Creates a `BigInt` from a [`u64`].
    ///
    /// # Example
    ///
    /// ```
    /// # use neon::{prelude::*, types::JsBigInt};
    /// # fn example(mut cx: FunctionContext) -> JsResult<JsBigInt> {
    /// let value: Handle<JsBigInt> = JsBigInt::from_u64(&mut cx, 42);
    /// # Ok(value)
    /// # }
    /// ```
    pub fn from_u64<'cx, C>(cx: &mut C, n: u64) -> Handle<'cx, Self>
    where
        C: Context<'cx>,
    {
        let mut v = MaybeUninit::uninit();
        let v = unsafe {
            assert_eq!(
                sys::create_bigint_uint64(cx.env().to_raw(), n, v.as_mut_ptr(),),
                sys::Status::Ok,
            );

            v.assume_init()
        };

        Handle::new_internal(Self(v))
    }

    // Internal helper for creating a _signed_ `BigInt` from a [`u128`] magnitude
    fn from_u128_sign<'cx, C>(cx: &mut C, sign: Sign, n: u128) -> Handle<'cx, Self>
    where
        C: Context<'cx>,
    {
        let n = n.to_le();
        let digits = [n as u64, (n >> 64) as u64];

        Self::from_digits_le(cx, sign, &digits)
    }

    /// Creates a `BigInt` from an [`i128`].
    ///
    /// # Example
    ///
    /// ```
    /// # use neon::{prelude::*, types::JsBigInt};
    /// # fn example(mut cx: FunctionContext) -> JsResult<JsBigInt> {
    /// let value: Handle<JsBigInt> = JsBigInt::from_i128(&mut cx, 42);
    /// # Ok(value)
    /// # }
    /// ```
    pub fn from_i128<'cx, C>(cx: &mut C, n: i128) -> Handle<'cx, Self>
    where
        C: Context<'cx>,
    {
        if n >= 0 {
            return Self::from_u128(cx, n as u128);
        }

        // Get the magnitude from a two's compliment negative
        let n = u128::MAX - (n as u128) + 1;

        Self::from_u128_sign(cx, Self::NEGATIVE, n)
    }

    /// Creates a `BigInt` from a [`u128`].
    ///
    /// # Example
    ///
    /// ```
    /// # use neon::{prelude::*, types::JsBigInt};
    /// # fn example(mut cx: FunctionContext) -> JsResult<JsBigInt> {
    /// let value: Handle<JsBigInt> = JsBigInt::from_u128(&mut cx, 42);
    /// # Ok(value)
    /// # }
    /// ```
    pub fn from_u128<'cx, C>(cx: &mut C, n: u128) -> Handle<'cx, Self>
    where
        C: Context<'cx>,
    {
        Self::from_u128_sign(cx, Self::POSITIVE, n)
    }

    /// Creates a `BigInt` from a signed magnitude. The `BigInt` is calculated as:\
    /// `Sign * (digit[0] x (2⁶⁴)⁰ + digit[0] x (2⁶⁴)¹ + digit[0] x (2⁶⁴)² ...)`
    ///
    /// # Example
    ///
    /// ```
    /// # use neon::{prelude::*, types::JsBigInt};
    /// # fn example(mut cx: FunctionContext) -> JsResult<JsBigInt> {
    /// // Creates a `BigInt` equal to `2n ** 128n`
    /// let value: Handle<JsBigInt> = JsBigInt::from_digits_le(
    ///     &mut cx,
    ///     JsBigInt::POSITIVE,
    ///     &[0, 0, 1],
    /// );
    /// # Ok(value)
    /// # }
    /// ```
    //
    // XXX: It's unclear if individual digits are expected to be little endian or native.
    // The current code assumes _native_. Neon modules are currently broken on big-endian
    // platforms. If this is fixed in the future, unit tests will determine if this
    // assumption is accurate.
    pub fn from_digits_le<'cx, C>(cx: &mut C, sign: Sign, digits: &[u64]) -> Handle<'cx, Self>
    where
        C: Context<'cx>,
    {
        let sign_bit = match sign {
            Sign::Positive => 0,
            Sign::Negative => 1,
        };

        let mut v = MaybeUninit::uninit();
        let v = unsafe {
            assert_eq!(
                sys::create_bigint_words(
                    cx.env().to_raw(),
                    sign_bit,
                    digits.len(),
                    digits.as_ptr(),
                    v.as_mut_ptr(),
                ),
                sys::Status::Ok,
            );

            v.assume_init()
        };

        Handle::new_internal(Self(v))
    }

    /// Reads an `i64` from a `BigInt`.
    ///
    /// Fails on overflow and underflow.
    ///
    /// # Example
    ///
    /// See [`JsBigInt`].
    pub fn to_i64<'cx, C>(&self, cx: &mut C) -> Result<i64, RangeError<i64>>
    where
        C: Context<'cx>,
    {
        let mut n = 0;
        let mut lossless = false;

        unsafe {
            assert_eq!(
                sys::get_value_bigint_int64(cx.env().to_raw(), self.0, &mut n, &mut lossless),
                sys::Status::Ok,
            );
        }

        if lossless {
            Ok(n)
        } else {
            Err(RangeError(n))
        }
    }

    /// Reads a `u64` from a `BigInt`.
    ///
    /// Fails on overflow or a negative sign.
    pub fn to_u64<'cx, C>(&self, cx: &mut C) -> Result<u64, RangeError<u64>>
    where
        C: Context<'cx>,
    {
        let mut n = 0;
        let mut lossless = false;

        unsafe {
            assert_eq!(
                sys::get_value_bigint_uint64(cx.env().to_raw(), self.0, &mut n, &mut lossless),
                sys::Status::Ok,
            );
        }

        if lossless {
            Ok(n)
        } else {
            Err(RangeError(n))
        }
    }

    /// Reads an `i128` from a `BigInt`.
    ///
    /// Fails on overflow and underflow.
    pub fn to_i128<'cx, C>(&self, cx: &mut C) -> Result<i128, RangeError<i128>>
    where
        C: Context<'cx>,
    {
        let mut digits = [0; 2];
        let (sign, num_digits) = self.read_digits_le(cx, &mut digits);

        // Cast digits into a `u128` magnitude
        let n = (digits[0] as u128) | ((digits[1] as u128) << 64);
        let n = u128::from_le(n);

        // Verify that the magnitude leaves room for the sign bit
        let n = match sign {
            Sign::Positive => {
                if n > (i128::MAX as u128) {
                    return Err(RangeError(i128::MAX));
                } else {
                    n as i128
                }
            }
            Sign::Negative => {
                if n > (i128::MAX as u128) + 1 {
                    return Err(RangeError(i128::MIN));
                } else {
                    (n as i128).wrapping_neg()
                }
            }
        };

        // Leading zeroes are truncated and never returned. If there are additional
        // digits, the number is out of range.
        if num_digits > digits.len() {
            Err(RangeError(n))
        } else {
            Ok(n)
        }
    }

    /// Reads a `u128` from a `BigInt`.
    ///
    /// Fails on overflow or a negative sign.
    pub fn to_u128<'cx, C>(&self, cx: &mut C) -> Result<u128, RangeError<u128>>
    where
        C: Context<'cx>,
    {
        let mut digits = [0; 2];
        let (sign, num_digits) = self.read_digits_le(cx, &mut digits);

        // Cast digits into a `u128` magnitude
        let n = (digits[0] as u128) | ((digits[1] as u128) << 64);
        let n = u128::from_le(n);

        // Leading zeroes are truncated and never returned. If there are additional
        // digits, the number is out of range.
        if matches!(sign, Sign::Negative) || num_digits > digits.len() {
            Err(RangeError(n))
        } else {
            Ok(n)
        }
    }

    /// Gets a signed magnitude pair from a `BigInt`.
    ///
    /// The `BigInt` is calculated as:\
    /// `Sign * (digit[0] x (2⁶⁴)⁰ + digit[0] x (2⁶⁴)¹ + digit[0] x (2⁶⁴)² ...)`
    pub fn to_digits_le<'cx, C>(&self, cx: &mut C) -> (Sign, Vec<u64>)
    where
        C: Context<'cx>,
    {
        let mut v = vec![0; self.len(cx)];
        let (sign, len) = self.read_digits_le(cx, &mut v);

        // It shouldn't be possible for the number of digits to change. If it
        // it does, it's a correctness issue and not a soundness bug.
        debug_assert_eq!(v.len(), len);

        (sign, v)
    }

    /// Gets the sign from a `BigInt` and reads digits into a buffer.
    /// The returned `usize` is the total number of digits in the `BigInt`.
    ///
    /// # Example
    ///
    /// Read a `u256` from a `BigInt`.
    ///
    /// ```
    /// # use std::error::Error;
    /// # use neon::{prelude::*, types::JsBigInt};
    /// fn bigint_to_u256(cx: &mut FunctionContext, n: Handle<JsBigInt>) -> NeonResult<[u64; 4]> {
    ///     let mut digits = [0; 4];
    ///     let (sign, num_digits) = n.read_digits_le(cx, &mut digits);
    ///
    ///     if sign == JsBigInt::NEGATIVE {
    ///         return cx.throw_error("Underflow reading u256 from BigInt");
    ///     }
    ///
    ///     if num_digits > digits.len() {
    ///         return cx.throw_error("Overflow reading u256 from BigInt");
    ///     }
    ///
    ///     Ok(digits)
    /// }
    /// ```
    pub fn read_digits_le<'cx, C>(&self, cx: &mut C, digits: &mut [u64]) -> (Sign, usize)
    where
        C: Context<'cx>,
    {
        let mut sign_bit = 0;
        let mut word_count = digits.len();

        unsafe {
            assert_eq!(
                sys::get_value_bigint_words(
                    cx.env().to_raw(),
                    self.0,
                    &mut sign_bit,
                    &mut word_count,
                    digits.as_mut_ptr(),
                ),
                sys::Status::Ok,
            );
        }

        let sign = if sign_bit == 0 {
            Sign::Positive
        } else {
            Sign::Negative
        };

        (sign, word_count)
    }

    /// Gets the number of `u64` digits in a `BigInt`
    pub fn len<'cx, C>(&self, cx: &mut C) -> usize
    where
        C: Context<'cx>,
    {
        // Get the length by reading into an empty slice and ignoring the sign
        self.read_digits_le(cx, &mut []).1
    }
}

impl Value for JsBigInt {}

unsafe impl TransparentNoCopyWrapper for JsBigInt {
    type Inner = raw::Local;

    fn into_inner(self) -> Self::Inner {
        self.0
    }
}

impl private::ValueInternal for JsBigInt {
    fn name() -> &'static str {
        "BigInt"
    }

    fn is_typeof<Other: Value>(env: Env, other: &Other) -> bool {
        unsafe { sys::tag::is_bigint(env.to_raw(), other.to_local()) }
    }

    fn to_local(&self) -> raw::Local {
        self.0
    }

    unsafe fn from_local(_env: Env, h: raw::Local) -> Self {
        Self(h)
    }
}