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
use core::ops::{Add, Sub, Mul}; use core::ops::{AddAssign, SubAssign, MulAssign}; use alloc::vec::Vec; use num_bigint::BigInt; use core::cmp::Ordering; pub struct RustBigInt(num_bigint::BigInt); impl RustBigInt { pub fn value(&self) -> &BigInt { &self.0 } } impl From<i64> for RustBigInt { fn from(item: i64) -> Self { RustBigInt(item.into()) } } impl From<i32> for RustBigInt { fn from(item: i32) -> Self { RustBigInt(item.into()) } } impl From<BigInt> for RustBigInt { fn from(item: BigInt) -> Self { RustBigInt(item) } } impl Clone for RustBigInt { fn clone(&self) -> Self { RustBigInt(self.0.clone()) } } impl Add for RustBigInt { type Output = RustBigInt; fn add(self, other: RustBigInt) -> RustBigInt { RustBigInt(self.0 + other.0) } } impl AddAssign<RustBigInt> for RustBigInt { fn add_assign(&mut self, other: Self) { BigInt::add_assign(&mut self.0, other.0) } } impl AddAssign<&RustBigInt> for RustBigInt { fn add_assign(&mut self, other: &RustBigInt) { BigInt::add_assign(&mut self.0, &other.0) } } impl Sub for RustBigInt { type Output = RustBigInt; fn sub(self, other: RustBigInt) -> RustBigInt { RustBigInt(self.0 - other.0) } } impl SubAssign<RustBigInt> for RustBigInt { fn sub_assign(&mut self, other: Self) { BigInt::sub_assign(&mut self.0, other.0) } } impl SubAssign<&RustBigInt> for RustBigInt { fn sub_assign(&mut self, other: &RustBigInt) { BigInt::sub_assign(&mut self.0, &other.0) } } impl Mul for RustBigInt { type Output = RustBigInt; fn mul(self, other: RustBigInt) -> RustBigInt { RustBigInt(self.0 * other.0) } } impl MulAssign<RustBigInt> for RustBigInt { fn mul_assign(&mut self, other: Self) { BigInt::mul_assign(&mut self.0, other.0) } } impl MulAssign<&RustBigInt> for RustBigInt { fn mul_assign(&mut self, other: &RustBigInt) { BigInt::mul_assign(&mut self.0, &other.0) } } impl PartialEq for RustBigInt { #[inline] fn eq(&self, other: &Self) -> bool { PartialEq::eq(&self.0, &other.0) } } impl Eq for RustBigInt{} impl Ord for RustBigInt { #[inline] fn cmp(&self, other: &Self) -> Ordering { Ord::cmp(&self.0, &other.0) } } impl PartialOrd for RustBigInt { #[inline] fn partial_cmp(&self, other: &Self) -> Option<Ordering> { PartialOrd::partial_cmp(&self.0, &other.0) } } impl elrond_wasm::BigIntApi for RustBigInt { fn byte_length(&self) -> i32 { panic!("byte_length not yet implemented") } fn copy_to_slice_big_endian(&self, _slice: &mut [u8]) -> i32 { panic!("copy_to_slice not yet implemented") } fn to_bytes_big_endian(&self) -> Vec<u8> { let (_, be) = self.0.to_bytes_be(); be } fn to_bytes_big_endian_pad_right(&self, nr_bytes: usize) -> Vec<u8> { let (_, bytes_be) = self.0.to_bytes_be(); if bytes_be.len() > nr_bytes { panic!("Number doesn't fit requested bytes"); } else if bytes_be.len() == nr_bytes { bytes_be } else { let mut res = vec![0u8; nr_bytes]; let offset = nr_bytes - bytes_be.len(); for i in 0..bytes_be.len()-1 { res[offset+i] = bytes_be[i]; } res } } fn phantom() -> Self { RustBigInt::from(0) } } impl RustBigInt { pub fn to_signed_bytes_be(&self) -> Vec<u8>{ self.0.to_signed_bytes_be() } } pub struct RustBigUint(num_bigint::BigInt); impl From<RustBigInt> for RustBigUint { fn from(item: RustBigInt) -> Self { RustBigUint(item.0) } } impl elrond_wasm::BigUintApi<RustBigInt> for RustBigUint { fn into_signed(self) -> RustBigInt { RustBigInt(self.0) } fn phantom() -> Self { RustBigUint::from(RustBigInt::from(0)) } }