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
use crate::big_uint_mock::*; use num_traits::sign::Signed; use core::ops::{Add, Sub, Mul, Div, Rem, Neg}; use core::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign}; use alloc::vec::Vec; use elrond_wasm::BigIntApi; use elrond_wasm::serde as serde; use num_bigint::BigInt; use core::cmp::Ordering; #[derive(Debug)] pub struct RustBigInt(pub num_bigint::BigInt); impl RustBigInt { pub fn value(&self) -> &BigInt { &self.0 } } impl From<RustBigUint> for RustBigInt { fn from(item: RustBigUint) -> Self { RustBigInt(item.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()) } } macro_rules! binary_operator { ($trait:ident, $method:ident) => { impl $trait for RustBigInt { type Output = RustBigInt; fn $method(self, other: RustBigInt) -> RustBigInt { RustBigInt((self.0).$method(other.0)) } } impl<'a, 'b> $trait<&'b RustBigInt> for &'a RustBigInt { type Output = RustBigInt; fn $method(self, other: &RustBigInt) -> RustBigInt { RustBigInt(self.0.clone().$method(other.0.clone())) } } } } binary_operator!{Add, add} binary_operator!{Sub, sub} binary_operator!{Mul, mul} binary_operator!{Div, div} binary_operator!{Rem, rem} macro_rules! binary_assign_operator { ($trait:ident, $method:ident) => { impl $trait<RustBigInt> for RustBigInt { fn $method(&mut self, other: Self) { BigInt::$method(&mut self.0, other.0) } } impl $trait<&RustBigInt> for RustBigInt { fn $method(&mut self, other: &RustBigInt) { BigInt::$method(&mut self.0, &other.0) } } } } binary_assign_operator!{AddAssign, add_assign} binary_assign_operator!{SubAssign, sub_assign} binary_assign_operator!{MulAssign, mul_assign} binary_assign_operator!{DivAssign, div_assign} binary_assign_operator!{RemAssign, rem_assign} impl Neg for RustBigInt { type Output = RustBigInt; fn neg(self) -> Self::Output { RustBigInt(-self.0) } } impl PartialEq<Self> for RustBigInt { #[inline] fn eq(&self, other: &Self) -> bool { PartialEq::eq(&self.0, &other.0) } } impl Eq for RustBigInt{} impl PartialOrd<Self> for RustBigInt { #[inline] fn partial_cmp(&self, other: &Self) -> Option<Ordering> { PartialOrd::partial_cmp(&self.0, &other.0) } } impl Ord for RustBigInt { #[inline] fn cmp(&self, other: &Self) -> Ordering { Ord::cmp(&self.0, &other.0) } } impl PartialEq<i64> for RustBigInt { #[inline] fn eq(&self, other: &i64) -> bool { PartialEq::eq(&self.0, &BigInt::from(*other)) } } impl PartialOrd<i64> for RustBigInt { #[inline] fn partial_cmp(&self, other: &i64) -> Option<Ordering> { PartialOrd::partial_cmp(&self.0, &BigInt::from(*other)) } } use elrond_wasm::esd_light::*; impl Encode for RustBigInt { fn using_top_encoded<F: FnOnce(&[u8])>(&self, f: F) { let bytes = self.to_signed_bytes_be(); f(&bytes); } fn dep_encode_to<O: Output>(&self, dest: &mut O) { let bytes = self.to_signed_bytes_be(); bytes.len().dep_encode_to(dest); dest.write(&bytes); } } impl Decode for RustBigInt { fn top_decode<I: Input>(input: &mut I) -> Result<Self, DeError> { let bytes = input.flush()?; Ok(RustBigInt::from_signed_bytes_be(bytes)) } fn dep_decode<I: Input>(input: &mut I) -> Result<Self, DeError> { let size = usize::dep_decode(input)?; let bytes = input.read_slice(size)?; Ok(RustBigInt::from_signed_bytes_be(bytes)) } } impl serde::Serialize for RustBigInt { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer, { let bytes = self.to_signed_bytes_be(); serializer.serialize_bytes(bytes.as_slice()) } } impl<'de> serde::Deserialize<'de> for RustBigInt { fn deserialize<D>(deserializer: D) -> Result<RustBigInt, D::Error> where D: serde::Deserializer<'de>, { let bytes = deserializer.deserialize_bytes(elrond_wasm::serialize_util::BorrowedBytesVisitor)?; Ok(RustBigInt::from_signed_bytes_be(bytes)) } } impl elrond_wasm::BigIntApi<RustBigUint> for RustBigInt { fn abs_uint(&self) -> RustBigUint { RustBigUint(self.0.abs()) } fn sign(&self) -> elrond_wasm::Sign { match self.0.sign() { num_bigint::Sign::Minus => elrond_wasm::Sign::NoSign, num_bigint::Sign::NoSign => elrond_wasm::Sign::NoSign, num_bigint::Sign::Plus => elrond_wasm::Sign::Plus, } } fn to_signed_bytes_be(&self) -> Vec<u8> { self.0.to_signed_bytes_be() } fn from_signed_bytes_be(bytes: &[u8]) -> Self { let bi = BigInt::from_signed_bytes_be(bytes); bi.into() } } impl RustBigInt { pub fn to_signed_bytes_be(&self) -> Vec<u8>{ self.0.to_signed_bytes_be() } }