neo-types 0.14.0

Neo N3 Core Types for Smart Contract Development
Documentation
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// Copyright (c) 2025-2026 R3E Network
// Licensed under the MIT License

use std::fmt;
use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Not, Rem, Shl, Shr, Sub};

use num_bigint::BigInt;
use num_traits::{One, ToPrimitive, Zero};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Neo N3 Integer type (arbitrary precision)
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct NeoInteger(BigInt);

impl NeoInteger {
    pub fn new<T: Into<BigInt>>(value: T) -> Self {
        Self(value.into())
    }

    pub fn zero() -> Self {
        Self(BigInt::zero())
    }

    pub fn one() -> Self {
        Self(BigInt::one())
    }

    /// Maximum byte length of an integer the NeoVM will hold
    /// (`ExecutionEngineLimits.MaxIntegerSize` in C# `neo-project/neo` —
    /// 32 bytes / 256 bits, two's-complement). The VM FAULTs when an
    /// operation produces an integer wider than this.
    pub const MAX_BYTE_LENGTH: usize = 32;

    /// Whether this value fits within the NeoVM's 256-bit integer bound.
    ///
    /// Host-mode arithmetic on `NeoInteger` is arbitrary-precision, so a
    /// computation that overflows 256 bits succeeds off-chain but FAULTs
    /// on-chain. Call this on values derived from untrusted input or
    /// unbounded accumulation to detect the divergence before it reaches
    /// the VM. Zero is one byte in `num-bigint`'s representation but is
    /// trivially within bounds.
    pub fn fits_in_neovm(&self) -> bool {
        if self.0.is_zero() {
            return true;
        }
        self.0.to_signed_bytes_le().len() <= Self::MAX_BYTE_LENGTH
    }

    /// Checked division: returns `Err(DivisionByZero)` instead of panicking
    /// (the `Div` operator faults on a zero divisor — on-chain that becomes a
    /// VM FAULT reverting the whole transaction). Use this in any contract
    /// path that cannot guarantee a non-zero divisor (D5).
    pub fn try_div(&self, rhs: &NeoInteger) -> crate::NeoResult<NeoInteger> {
        if rhs.0.is_zero() {
            return Err(crate::NeoError::DivisionByZero);
        }
        Ok(NeoInteger::new(&self.0 / &rhs.0))
    }

    /// Checked remainder: returns `Err(DivisionByZero)` instead of panicking
    /// (see `try_div`). Matches the `Rem` operator's fault-on-zero behaviour.
    pub fn try_rem(&self, rhs: &NeoInteger) -> crate::NeoResult<NeoInteger> {
        if rhs.0.is_zero() {
            return Err(crate::NeoError::DivisionByZero);
        }
        Ok(NeoInteger::new(&self.0 % &rhs.0))
    }

    pub fn min_i32() -> Self {
        Self(BigInt::from(i32::MIN))
    }

    pub fn max_i32() -> Self {
        Self(BigInt::from(i32::MAX))
    }

    pub fn as_bigint(&self) -> &BigInt {
        &self.0
    }

    /// Owned `BigInt` (clones the inner `BigInt`). Use this when you
    /// need to keep the value past the `NeoInteger`'s lifetime
    /// (e.g. pass to an API that expects `BigInt` by value).
    pub fn to_bigint(&self) -> BigInt {
        self.0.clone()
    }

    /// Construct a `NeoInteger` from a `BigInt`. The `From<BigInt>`
    /// impl already provides this, but spelled as a method for
    /// uniformity with `to_bigint` and for code that has the
    /// `BigInt` behind a reference.
    pub fn from_bigint(value: &BigInt) -> Self {
        Self(value.clone())
    }

    /// Convert to i32, returning None if the value is out of range.
    /// This is the safe alternative to `as_i32()` that doesn't panic.
    pub fn try_as_i32(&self) -> Option<i32> {
        self.0.to_i32()
    }

    /// Convert to u32, returning None if the value is out of range.
    /// This is the safe alternative to `as_u32()` that doesn't panic.
    pub fn try_as_u32(&self) -> Option<u32> {
        self.0.to_u32()
    }

    /// Convert to i64, returning None if the value is out of range.
    pub fn try_as_i64(&self) -> Option<i64> {
        self.0.to_i64()
    }

    /// Convert to u64, returning None if the value is out of range.
    pub fn try_as_u64(&self) -> Option<u64> {
        self.0.to_u64()
    }

    /// Convert to i32, returning `Result` for ergonomic `?` usage.
    pub fn try_into_i32(&self) -> crate::NeoResult<i32> {
        self.0.to_i32().ok_or(crate::NeoError::Overflow)
    }

    /// Convert to u32, returning `Result` for ergonomic `?` usage.
    pub fn try_into_u32(&self) -> crate::NeoResult<u32> {
        self.0.to_u32().ok_or(crate::NeoError::Overflow)
    }

    /// Convert to i64, returning `Result` for ergonomic `?` usage.
    pub fn try_into_i64(&self) -> crate::NeoResult<i64> {
        self.0.to_i64().ok_or(crate::NeoError::Overflow)
    }

    /// Convert to u64, returning `Result` for ergonomic `?` usage.
    pub fn try_into_u64(&self) -> crate::NeoResult<u64> {
        self.0.to_u64().ok_or(crate::NeoError::Overflow)
    }

    /// Convert to i32, saturating at the boundaries if the value is out of range.
    /// This never panics.
    pub fn as_i32_saturating(&self) -> i32 {
        self.0.to_i32().unwrap_or_else(|| {
            if self.0.sign() == num_bigint::Sign::Minus {
                i32::MIN
            } else {
                i32::MAX
            }
        })
    }

    /// Convert to u32, saturating at the boundaries if the value is out of range.
    /// This never panics.
    pub fn as_u32_saturating(&self) -> u32 {
        self.0.to_u32().unwrap_or_else(|| {
            if self.0.sign() == num_bigint::Sign::Minus {
                0
            } else {
                u32::MAX
            }
        })
    }

    /// Convert to i64, saturating at the boundaries if the value is out of range.
    /// This never panics.
    pub fn as_i64_saturating(&self) -> i64 {
        self.0.to_i64().unwrap_or_else(|| {
            if self.0.sign() == num_bigint::Sign::Minus {
                i64::MIN
            } else {
                i64::MAX
            }
        })
    }

    /// Deprecated compatibility helper that converts to `i32` using saturating semantics.
    #[deprecated(
        since = "0.1.0",
        note = "Use try_as_i32() or as_i32_saturating() explicitly"
    )]
    pub fn as_i32(&self) -> i32 {
        self.as_i32_saturating()
    }

    /// Deprecated compatibility helper that converts to `u32` using saturating semantics.
    #[deprecated(
        since = "0.1.0",
        note = "Use try_as_u32() or as_u32_saturating() explicitly"
    )]
    pub fn as_u32(&self) -> u32 {
        self.as_u32_saturating()
    }

    /// Deprecated: use `try_as_i32()` instead.
    #[deprecated(since = "0.1.0", note = "Use try_as_i32() instead")]
    pub fn to_i32(&self) -> Option<i32> {
        self.0.to_i32()
    }

    /// Deprecated: use `try_as_u32()` instead.
    #[deprecated(since = "0.1.0", note = "Use try_as_u32() instead")]
    pub fn to_u32(&self) -> Option<u32> {
        self.0.to_u32()
    }

    /// Deprecated: use `try_as_i64()` instead.
    #[deprecated(since = "0.1.0", note = "Use try_as_i64() instead")]
    pub fn to_i64(&self) -> Option<i64> {
        self.0.to_i64()
    }
}

impl fmt::Display for NeoInteger {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Not for NeoInteger {
    type Output = Self;
    fn not(self) -> Self::Output {
        Self(!self.0)
    }
}

impl Shl<u32> for NeoInteger {
    type Output = Self;
    fn shl(self, rhs: u32) -> Self::Output {
        Self(self.0 << rhs)
    }
}

impl Shl<u32> for &NeoInteger {
    type Output = NeoInteger;
    fn shl(self, rhs: u32) -> Self::Output {
        NeoInteger::new(self.0.clone() << rhs)
    }
}

impl Shr<u32> for NeoInteger {
    type Output = Self;
    fn shr(self, rhs: u32) -> Self::Output {
        Self(self.0 >> rhs)
    }
}

impl Shr<u32> for &NeoInteger {
    type Output = NeoInteger;
    fn shr(self, rhs: u32) -> Self::Output {
        NeoInteger::new(self.0.clone() >> rhs)
    }
}

impl From<i8> for NeoInteger {
    fn from(value: i8) -> Self {
        NeoInteger::new(value)
    }
}

impl From<u8> for NeoInteger {
    fn from(value: u8) -> Self {
        NeoInteger::new(value)
    }
}

impl From<i16> for NeoInteger {
    fn from(value: i16) -> Self {
        NeoInteger::new(value)
    }
}

impl From<u16> for NeoInteger {
    fn from(value: u16) -> Self {
        NeoInteger::new(value)
    }
}

impl From<i32> for NeoInteger {
    fn from(value: i32) -> Self {
        NeoInteger::new(value)
    }
}

impl From<u32> for NeoInteger {
    fn from(value: u32) -> Self {
        NeoInteger::new(value)
    }
}

impl From<i64> for NeoInteger {
    fn from(value: i64) -> Self {
        NeoInteger::new(value)
    }
}

impl From<u64> for NeoInteger {
    fn from(value: u64) -> Self {
        NeoInteger::new(value)
    }
}

impl From<i128> for NeoInteger {
    fn from(value: i128) -> Self {
        NeoInteger::new(value)
    }
}

impl From<u128> for NeoInteger {
    fn from(value: u128) -> Self {
        NeoInteger::new(value)
    }
}

impl From<BigInt> for NeoInteger {
    fn from(value: BigInt) -> Self {
        NeoInteger::new(value)
    }
}

impl From<&BigInt> for NeoInteger {
    fn from(value: &BigInt) -> Self {
        NeoInteger::new(value.clone())
    }
}

impl Add for NeoInteger {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0 + rhs.0)
    }
}

impl Add<&NeoInteger> for NeoInteger {
    type Output = Self;
    fn add(self, rhs: &NeoInteger) -> Self::Output {
        Self(self.0 + &rhs.0)
    }
}

impl Add<NeoInteger> for &NeoInteger {
    type Output = NeoInteger;
    fn add(self, rhs: NeoInteger) -> Self::Output {
        NeoInteger::new(&self.0 + rhs.0)
    }
}

impl Add<&NeoInteger> for &NeoInteger {
    type Output = NeoInteger;
    fn add(self, rhs: &NeoInteger) -> Self::Output {
        NeoInteger::new(&self.0 + &rhs.0)
    }
}

impl Sub for NeoInteger {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        Self(self.0 - rhs.0)
    }
}

impl Sub<&NeoInteger> for NeoInteger {
    type Output = Self;
    fn sub(self, rhs: &NeoInteger) -> Self::Output {
        Self(self.0 - &rhs.0)
    }
}

impl Sub<NeoInteger> for &NeoInteger {
    type Output = NeoInteger;
    fn sub(self, rhs: NeoInteger) -> Self::Output {
        NeoInteger::new(&self.0 - rhs.0)
    }
}

impl Sub<&NeoInteger> for &NeoInteger {
    type Output = NeoInteger;
    fn sub(self, rhs: &NeoInteger) -> Self::Output {
        NeoInteger::new(&self.0 - &rhs.0)
    }
}

impl Mul for NeoInteger {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self::Output {
        Self(self.0 * rhs.0)
    }
}

impl Mul<&NeoInteger> for NeoInteger {
    type Output = Self;
    fn mul(self, rhs: &NeoInteger) -> Self::Output {
        Self(self.0 * &rhs.0)
    }
}

impl Mul<NeoInteger> for &NeoInteger {
    type Output = NeoInteger;
    fn mul(self, rhs: NeoInteger) -> Self::Output {
        NeoInteger::new(&self.0 * rhs.0)
    }
}

impl Mul<&NeoInteger> for &NeoInteger {
    type Output = NeoInteger;
    fn mul(self, rhs: &NeoInteger) -> Self::Output {
        NeoInteger::new(&self.0 * &rhs.0)
    }
}

impl Div for NeoInteger {
    type Output = Self;
    fn div(self, rhs: Self) -> Self::Output {
        Self(self.0 / rhs.0)
    }
}

impl Div<&NeoInteger> for NeoInteger {
    type Output = Self;
    fn div(self, rhs: &NeoInteger) -> Self::Output {
        Self(self.0 / &rhs.0)
    }
}

impl Div<NeoInteger> for &NeoInteger {
    type Output = NeoInteger;
    fn div(self, rhs: NeoInteger) -> Self::Output {
        NeoInteger::new(&self.0 / rhs.0)
    }
}

impl Div<&NeoInteger> for &NeoInteger {
    type Output = NeoInteger;
    fn div(self, rhs: &NeoInteger) -> Self::Output {
        NeoInteger::new(&self.0 / &rhs.0)
    }
}

impl Rem for NeoInteger {
    type Output = Self;
    fn rem(self, rhs: Self) -> Self::Output {
        Self(self.0 % rhs.0)
    }
}

impl Rem<&NeoInteger> for NeoInteger {
    type Output = Self;
    fn rem(self, rhs: &NeoInteger) -> Self::Output {
        Self(self.0 % &rhs.0)
    }
}

impl Rem<NeoInteger> for &NeoInteger {
    type Output = NeoInteger;
    fn rem(self, rhs: NeoInteger) -> Self::Output {
        NeoInteger::new(&self.0 % rhs.0)
    }
}

impl Rem<&NeoInteger> for &NeoInteger {
    type Output = NeoInteger;
    fn rem(self, rhs: &NeoInteger) -> Self::Output {
        NeoInteger::new(&self.0 % &rhs.0)
    }
}

impl BitAnd for NeoInteger {
    type Output = Self;
    fn bitand(self, rhs: Self) -> Self::Output {
        Self(self.0 & rhs.0)
    }
}

impl BitAnd<&NeoInteger> for NeoInteger {
    type Output = Self;
    fn bitand(self, rhs: &NeoInteger) -> Self::Output {
        Self(self.0 & &rhs.0)
    }
}

impl BitAnd<NeoInteger> for &NeoInteger {
    type Output = NeoInteger;
    fn bitand(self, rhs: NeoInteger) -> Self::Output {
        NeoInteger::new(&self.0 & rhs.0)
    }
}

impl BitAnd<&NeoInteger> for &NeoInteger {
    type Output = NeoInteger;
    fn bitand(self, rhs: &NeoInteger) -> Self::Output {
        NeoInteger::new(&self.0 & &rhs.0)
    }
}

impl BitOr for NeoInteger {
    type Output = Self;
    fn bitor(self, rhs: Self) -> Self::Output {
        Self(self.0 | rhs.0)
    }
}

impl BitOr<&NeoInteger> for NeoInteger {
    type Output = Self;
    fn bitor(self, rhs: &NeoInteger) -> Self::Output {
        Self(self.0 | &rhs.0)
    }
}

impl BitOr<NeoInteger> for &NeoInteger {
    type Output = NeoInteger;
    fn bitor(self, rhs: NeoInteger) -> Self::Output {
        NeoInteger::new(&self.0 | rhs.0)
    }
}

impl BitOr<&NeoInteger> for &NeoInteger {
    type Output = NeoInteger;
    fn bitor(self, rhs: &NeoInteger) -> Self::Output {
        NeoInteger::new(&self.0 | &rhs.0)
    }
}

impl BitXor for NeoInteger {
    type Output = Self;
    fn bitxor(self, rhs: Self) -> Self::Output {
        Self(self.0 ^ rhs.0)
    }
}

impl BitXor<&NeoInteger> for NeoInteger {
    type Output = Self;
    fn bitxor(self, rhs: &NeoInteger) -> Self::Output {
        Self(self.0 ^ &rhs.0)
    }
}

impl BitXor<NeoInteger> for &NeoInteger {
    type Output = NeoInteger;
    fn bitxor(self, rhs: NeoInteger) -> Self::Output {
        NeoInteger::new(&self.0 ^ rhs.0)
    }
}

impl BitXor<&NeoInteger> for &NeoInteger {
    type Output = NeoInteger;
    fn bitxor(self, rhs: &NeoInteger) -> Self::Output {
        NeoInteger::new(&self.0 ^ &rhs.0)
    }
}

impl Default for NeoInteger {
    fn default() -> Self {
        NeoInteger::zero()
    }
}

#[cfg(test)]
mod d5_tests {
    use super::*;

    #[test]
    fn try_div_returns_division_by_zero_instead_of_panicking() {
        let a = NeoInteger::new(10);
        let zero = NeoInteger::zero();
        assert_eq!(a.try_div(&zero), Err(crate::NeoError::DivisionByZero));
        let two = NeoInteger::new(2);
        assert_eq!(a.try_div(&two).unwrap(), NeoInteger::new(5));
    }

    #[test]
    fn try_rem_returns_division_by_zero_instead_of_panicking() {
        let a = NeoInteger::new(10);
        let zero = NeoInteger::zero();
        assert_eq!(a.try_rem(&zero), Err(crate::NeoError::DivisionByZero));
        let three = NeoInteger::new(3);
        assert_eq!(a.try_rem(&three).unwrap(), NeoInteger::new(1));
    }

    #[test]
    fn fits_in_neovm_tracks_the_256_bit_bound() {
        assert!(NeoInteger::zero().fits_in_neovm());
        assert!(NeoInteger::new(i64::MAX).fits_in_neovm());
        assert!(NeoInteger::new(i64::MIN).fits_in_neovm());

        // 2^255 - 1 is the largest positive value that fits in 32 bytes.
        let max_pos = (BigInt::one() << 255) - BigInt::one();
        assert!(NeoInteger::from_bigint(&max_pos).fits_in_neovm());
        assert_eq!(
            max_pos.to_signed_bytes_le().len(),
            NeoInteger::MAX_BYTE_LENGTH
        );

        // 2^255 needs 33 bytes (a sign byte), so it overflows the bound.
        let over = BigInt::one() << 255;
        assert!(!NeoInteger::from_bigint(&over).fits_in_neovm());

        // A clearly oversized value (2^256) also fails.
        assert!(!NeoInteger::from_bigint(&(BigInt::one() << 256)).fits_in_neovm());
    }
}