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
use core::cmp::Ordering;

use crate::{
    declare_tag_based_signed_small_and_object_wrappers,
    declare_tag_based_small_and_object_wrappers,
    declare_tag_based_unsigned_small_and_object_wrappers, declare_tag_based_wrapper,
    raw_val::TAG_BITS, Compare, ConversionError, Env, RawVal, Tag,
};
pub use ethnum::{AsI256, AsU256, I256, U256};
use stellar_xdr::{Int256Parts, ScVal, UInt256Parts};

declare_tag_based_wrapper!(U32Val);
declare_tag_based_wrapper!(I32Val);

impl RawVal {
    #[inline(always)]
    pub const fn from_u32(u: u32) -> U32Val {
        unsafe { U32Val(RawVal::from_major_minor_and_tag(u, 0, Tag::U32Val)) }
    }

    #[inline(always)]
    pub const fn from_i32(i: i32) -> I32Val {
        unsafe { I32Val(RawVal::from_major_minor_and_tag(i as u32, 0, Tag::I32Val)) }
    }

    #[inline(always)]
    pub const fn is_i32_zero(self) -> bool {
        self.shallow_eq(&Self::I32_ZERO.0)
    }

    #[inline(always)]
    pub const fn is_u32_zero(self) -> bool {
        self.shallow_eq(&Self::U32_ZERO.0)
    }
}

impl<E: Env> Compare<U32Val> for E {
    type Error = E::Error;
    fn compare(&self, a: &U32Val, b: &U32Val) -> Result<Ordering, Self::Error> {
        Ok(u32::from(*a).cmp(&u32::from(*b)))
    }
}

impl<E: Env> Compare<I32Val> for E {
    type Error = E::Error;
    fn compare(&self, a: &I32Val, b: &I32Val) -> Result<Ordering, Self::Error> {
        Ok(i32::from(*a).cmp(&i32::from(*b)))
    }
}

declare_tag_based_unsigned_small_and_object_wrappers!(U64Val, U64Small, U64Object);
declare_tag_based_signed_small_and_object_wrappers!(I64Val, I64Small, I64Object);
declare_tag_based_unsigned_small_and_object_wrappers!(
    TimepointVal,
    TimepointSmall,
    TimepointObject
);
declare_tag_based_unsigned_small_and_object_wrappers!(DurationVal, DurationSmall, DurationObject);

declare_tag_based_unsigned_small_and_object_wrappers!(U128Val, U128Small, U128Object);
declare_tag_based_signed_small_and_object_wrappers!(I128Val, I128Small, I128Object);
declare_tag_based_unsigned_small_and_object_wrappers!(U256Val, U256Small, U256Object);
declare_tag_based_signed_small_and_object_wrappers!(I256Val, I256Small, I256Object);

impl From<u32> for U32Val {
    fn from(value: u32) -> Self {
        U32Val(value.into())
    }
}

impl From<U32Val> for u32 {
    fn from(value: U32Val) -> Self {
        value.0.get_major()
    }
}

impl From<i32> for I32Val {
    fn from(value: i32) -> Self {
        I32Val(value.into())
    }
}

impl From<I32Val> for i32 {
    fn from(value: I32Val) -> Self {
        value.0.get_major() as i32
    }
}

impl From<U64Small> for u64 {
    fn from(value: U64Small) -> Self {
        value.0.get_body()
    }
}

impl From<I64Small> for i64 {
    fn from(value: I64Small) -> Self {
        value.0.get_signed_body()
    }
}

impl From<TimepointSmall> for u64 {
    fn from(value: TimepointSmall) -> Self {
        value.0.get_body()
    }
}

impl From<DurationSmall> for u64 {
    fn from(value: DurationSmall) -> Self {
        value.0.get_body()
    }
}

impl From<U128Small> for u128 {
    fn from(value: U128Small) -> Self {
        value.0.get_body() as u128
    }
}

impl From<I128Small> for i128 {
    fn from(value: I128Small) -> Self {
        value.0.get_signed_body() as i128
    }
}

impl From<U256Small> for U256 {
    fn from(value: U256Small) -> Self {
        U256::from(value.0.get_body())
    }
}

impl From<I256Small> for I256 {
    fn from(value: I256Small) -> Self {
        I256::from(value.0.get_signed_body())
    }
}

impl TryFrom<u64> for U64Small {
    type Error = ConversionError;
    fn try_from(value: u64) -> Result<Self, Self::Error> {
        if is_small_u64(value) {
            Ok(Self(unsafe {
                RawVal::from_body_and_tag(value, Tag::U64Small)
            }))
        } else {
            Err(ConversionError)
        }
    }
}

impl TryFrom<i64> for I64Small {
    type Error = ConversionError;
    fn try_from(value: i64) -> Result<Self, Self::Error> {
        if is_small_i64(value) {
            Ok(Self(unsafe {
                RawVal::from_body_and_tag(value as u64, Tag::I64Small)
            }))
        } else {
            Err(ConversionError)
        }
    }
}

impl TryFrom<u128> for U128Small {
    type Error = ConversionError;
    fn try_from(value: u128) -> Result<Self, Self::Error> {
        if is_small_u128(value) {
            Ok(Self(unsafe {
                RawVal::from_body_and_tag(value as u64, Tag::U128Small)
            }))
        } else {
            Err(ConversionError)
        }
    }
}

impl TryFrom<i128> for I128Small {
    type Error = ConversionError;
    fn try_from(value: i128) -> Result<Self, Self::Error> {
        if is_small_i128(value) {
            Ok(Self(unsafe {
                RawVal::from_body_and_tag((value as u128) as u64, Tag::I128Small)
            }))
        } else {
            Err(ConversionError)
        }
    }
}

impl TryFrom<U256> for U256Small {
    type Error = ConversionError;
    fn try_from(value: U256) -> Result<Self, Self::Error> {
        if is_small_u256(&value) {
            Ok(Self(unsafe {
                RawVal::from_body_and_tag(value.as_u64(), Tag::U256Small)
            }))
        } else {
            Err(ConversionError)
        }
    }
}

impl TryFrom<I256> for I256Small {
    type Error = ConversionError;
    fn try_from(value: I256) -> Result<Self, Self::Error> {
        if is_small_i256(&value) {
            Ok(Self(unsafe {
                RawVal::from_body_and_tag(value.as_i64() as u64, Tag::I256Small)
            }))
        } else {
            Err(ConversionError)
        }
    }
}

impl TryFrom<U256Small> for ScVal {
    type Error = ConversionError;
    fn try_from(value: U256Small) -> Result<Self, Self::Error> {
        let val = U256::new(value.as_raw().get_body() as u128);
        let (hi_hi, hi_lo, lo_hi, lo_lo) = u256_into_pieces(val);
        Ok(ScVal::U256(UInt256Parts {
            hi_hi,
            hi_lo,
            lo_hi,
            lo_lo,
        }))
    }
}

impl TryFrom<&U256Small> for ScVal {
    type Error = ConversionError;
    fn try_from(value: &U256Small) -> Result<Self, Self::Error> {
        (*value).try_into()
    }
}

impl TryFrom<I256Small> for ScVal {
    type Error = ConversionError;
    fn try_from(value: I256Small) -> Result<Self, Self::Error> {
        let val = I256::new(value.as_raw().get_signed_body() as i128);
        let (hi_hi, hi_lo, lo_hi, lo_lo) = i256_into_pieces(val);
        Ok(ScVal::I256(Int256Parts {
            hi_hi,
            hi_lo,
            lo_hi,
            lo_lo,
        }))
    }
}

impl TryFrom<&I256Small> for ScVal {
    type Error = ConversionError;
    fn try_from(value: &I256Small) -> Result<Self, Self::Error> {
        (*value).try_into()
    }
}

pub const fn is_small_u64(u: u64) -> bool {
    u == ((u << TAG_BITS) >> TAG_BITS)
}

pub const fn is_small_i64(i: i64) -> bool {
    i == ((i << TAG_BITS) >> TAG_BITS)
}

pub fn is_small_u128(u: u128) -> bool {
    let word = u as u64;
    is_small_u64(word) && u == (word as u128)
}

pub fn is_small_i128(i: i128) -> bool {
    let word = i as i64;
    is_small_i64(word) && i == (word as i128)
}

pub fn is_small_u256(u: &U256) -> bool {
    let word = u.as_u64();
    is_small_u64(word) && *u == U256::from(word)
}

pub fn is_small_i256(i: &I256) -> bool {
    let word = i.as_i64();
    is_small_i64(word) && *i == I256::from(word)
}

pub fn is_small_u256_parts(u: &UInt256Parts) -> bool {
    u.hi_hi == 0 && u.hi_lo == 0 && u.lo_hi == 0 && is_small_u64(u.lo_lo)
}

pub fn is_small_i256_parts(i: &Int256Parts) -> bool {
    let i = i256_from_pieces(i.hi_hi, i.hi_lo, i.lo_hi, i.lo_lo);
    is_small_i256(&i)
}

pub fn u256_from_pieces(hi_hi: u64, hi_lo: u64, lo_hi: u64, lo_lo: u64) -> U256 {
    let high = (u128::from(hi_hi)) << 64 | u128::from(hi_lo);
    let low = (u128::from(lo_hi)) << 64 | u128::from(lo_lo);
    U256::from_words(high, low)
}

pub fn u256_into_pieces(u: U256) -> (u64, u64, u64, u64) {
    let (high, low) = u.into_words();
    let (hi_hi, hi_lo) = ((high >> 64) as u64, high as u64);
    let (lo_hi, lo_lo) = ((low >> 64) as u64, low as u64);
    (hi_hi, hi_lo, lo_hi, lo_lo)
}

pub fn i256_from_pieces(hi_hi: i64, hi_lo: u64, lo_hi: u64, lo_lo: u64) -> I256 {
    let high = ((u128::from(hi_hi as u64) << 64) | u128::from(hi_lo)) as i128;
    let low = ((u128::from(lo_hi) << 64) | u128::from(lo_lo)) as i128;
    I256::from_words(high, low)
}

pub fn i256_into_pieces(i: I256) -> (i64, u64, u64, u64) {
    let (high, low) = i.into_words();
    let (hi_hi, hi_lo) = ((high >> 64) as i64, high as u64);
    let (lo_hi, lo_lo) = ((low >> 64) as u64, low as u64);
    (hi_hi, hi_lo, lo_hi, lo_lo)
}

pub const MIN_SMALL_U64: u64 = 0;
pub const MAX_SMALL_U64: u64 = 0x00ff_ffff_ffff_ffff_u64;

pub const MIN_SMALL_I64: i64 = 0xff80_0000_0000_0000_u64 as i64;
pub const MAX_SMALL_I64: i64 = 0x007f_ffff_ffff_ffff_u64 as i64;

static_assertions::const_assert!(MIN_SMALL_I64 == -36_028_797_018_963_968_i64);
static_assertions::const_assert!(MAX_SMALL_I64 == 36_028_797_018_963_967_i64);

pub const MIN_SMALL_U128: u128 = MIN_SMALL_U64 as u128;
pub const MAX_SMALL_U128: u128 = MAX_SMALL_U64 as u128;

pub const MIN_SMALL_I128: i128 = MIN_SMALL_I64 as i128;
pub const MAX_SMALL_I128: i128 = MAX_SMALL_I64 as i128;

pub const MIN_SMALL_U256: U256 = U256::new(MIN_SMALL_U128);
pub const MAX_SMALL_U256: U256 = U256::new(MAX_SMALL_U128);

pub const MIN_SMALL_I256: I256 = I256::new(MIN_SMALL_I128);
pub const MAX_SMALL_I256: I256 = I256::new(MAX_SMALL_I128);

#[test]
fn test_small_ints() {
    assert!(!is_small_i64(MIN_SMALL_I64 - 1));
    assert!(is_small_i64(MIN_SMALL_I64));
    assert!(is_small_i64(MAX_SMALL_I64));
    assert!(!is_small_i64(MAX_SMALL_I64 + 1));

    assert!(is_small_u64(MIN_SMALL_U64));
    assert!(is_small_u64(MAX_SMALL_U64));
    assert!(!is_small_u64(MAX_SMALL_U64 + 1));

    assert!(!is_small_i128(MIN_SMALL_I128 - 1));
    assert!(is_small_i128(MIN_SMALL_I128));
    assert!(is_small_i128(MAX_SMALL_I128));
    assert!(!is_small_i128(MAX_SMALL_I128 + 1));

    assert!(is_small_u128(MIN_SMALL_U128));
    assert!(is_small_u128(MAX_SMALL_U128));
    assert!(!is_small_u128(MAX_SMALL_U128 + 1));

    assert!(!is_small_i256(&(MIN_SMALL_I256 - 1)));
    assert!(is_small_i256(&(MIN_SMALL_I256)));
    assert!(is_small_i256(&(MAX_SMALL_I256)));
    assert!(!is_small_i256(&(MAX_SMALL_I256 + 1)));

    assert!(is_small_u256(&(MIN_SMALL_U256)));
    assert!(is_small_u256(&(MAX_SMALL_U256)));
    assert!(!is_small_u256(&(MAX_SMALL_U256 + 1)));

    assert!(is_small_i64(-1_i64));
    assert!(is_small_i64(-12345_i64));
    assert!(is_small_i64(1_i64));
    assert!(is_small_i64(12345_i64));

    assert!(is_small_i128(-1_i128));
    assert!(is_small_i128(-12345_i128));
    assert!(is_small_i128(1_i128));
    assert!(is_small_i128(12345_i128));

    assert!(is_small_i256(&I256::new(-1_i128)));
    assert!(is_small_i256(&I256::new(-12345_i128)));
    assert!(is_small_i256(&I256::new(1_i128)));
    assert!(is_small_i256(&I256::new(12345_i128)));
}