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
use num_bigint; #[derive(Clone, Debug, Eq, PartialEq)] pub struct BigInt { bigint: num_bigint::BigInt, pub size: Option<usize>, } impl BigInt { pub fn new<T>(bigint: T, size: Option<usize>) -> BigInt where T: Into<num_bigint::BigInt> { BigInt { bigint: bigint.into(), size, } } pub fn new_from_str(s: &str) -> BigInt { let bytes = s.bytes().collect::<Vec<u8>>(); let bigint = num_bigint::BigInt::from_signed_bytes_be(&bytes); BigInt { bigint, size: Some(bytes.len() * 8), } } pub fn as_string(&self) -> String { String::from_utf8_lossy(&self.bigint.to_signed_bytes_be()).to_string() } pub fn from_bytes_be(bytes: &[u8]) -> BigInt { let bigint = num_bigint::BigInt::from_signed_bytes_be(&bytes); BigInt { bigint, size: Some(bytes.len() * 8), } } pub fn set_bit(&self, index: usize, value: bool) -> BigInt { if value { self | &Into::<BigInt>::into(1).shl(index) } else { self & &!&Into::<BigInt>::into(0).shl(index) } } pub fn get_bit(&self, index: usize) -> bool { let bytes = self.bigint.to_signed_bytes_le(); let byte_index = index / 8; if byte_index >= bytes.len() { return self.bigint.sign() == num_bigint::Sign::Minus; } let mut byte = bytes[byte_index]; let mut bit_index = index % 8; while bit_index > 0 { byte >>= 1; bit_index -= 1; } (byte & 0b1) != 0 } pub fn min_size(&self) -> usize { use num_traits::Zero; if self.bigint.is_zero() { return 1; } if self.bigint < num_bigint::BigInt::zero() { let y: num_bigint::BigInt = &self.bigint + 1; y.bits() + 1 } else { self.bigint.bits() } } pub fn size_or_min_size(&self) -> usize { if let Some(size) = self.size { size } else { self.min_size() } } pub fn sign(&self) -> isize { match self.bigint.sign() { num_bigint::Sign::Minus => -1, num_bigint::Sign::NoSign => 0, num_bigint::Sign::Plus => 1, } } pub fn checked_to_usize(&self) -> Option<usize> { use num_traits::ToPrimitive; self.bigint.to_usize() } pub fn checked_div(&self, rhs: &BigInt) -> Option<BigInt> { self.bigint.checked_div(&rhs.bigint).map(|res| res.into()) } pub fn checked_rem(&self, rhs: &BigInt) -> Option<BigInt> { use num_traits::Zero; if rhs.bigint == num_bigint::BigInt::zero() { None } else { Some((&self.bigint % &rhs.bigint).into()) } } pub fn shl(&self, rhs: usize) -> BigInt { (&self.bigint << rhs).into() } pub fn shr(&self, rhs: usize) -> BigInt { let lhs_sign = self.bigint.sign(); let result = &self.bigint >> rhs; if lhs_sign == num_bigint::Sign::Minus && result.sign() == num_bigint::Sign::NoSign { BigInt::from(-1) } else { result.into() } } pub fn checked_shl(&self, rhs: &BigInt) -> Option<BigInt> { use num_traits::ToPrimitive; rhs.bigint.to_usize().map(|rhs| self.shl(rhs).into()) } pub fn checked_shr(&self, rhs: &BigInt) -> Option<BigInt> { use num_traits::ToPrimitive; rhs.bigint.to_usize().map(|rhs| self.shr(rhs).into()) } pub fn concat(&self, lhs_slice: (usize, usize), rhs: &BigInt, rhs_slice: (usize, usize)) -> BigInt { let lhs_size = lhs_slice.0 + 1 - lhs_slice.1; let rhs_size = rhs_slice.0 + 1 - rhs_slice.1; let lhs = self.slice(lhs_slice.0, lhs_slice.1).shl(rhs_size); let rhs = rhs.slice(rhs_slice.0, rhs_slice.1); let mut result: BigInt = (&lhs | &rhs).into(); result.size = Some(lhs_size + rhs_size); result } pub fn slice(&self, left: usize, right: usize) -> BigInt { use num_traits::Zero; use num_traits::One; let mut mask = num_bigint::BigInt::zero(); for _ in 0..(left - right + 1) { mask = (mask << 1) + num_bigint::BigInt::one(); } let mut result = &self.shr(right) & &mask.into(); result.size = Some(left + 1 - right); result } fn combine_bits<F>(&self, rhs: &BigInt, f: F) -> BigInt where F: Fn(u8, u8) -> u8 { let mut lhs_bytes = self.bigint.to_signed_bytes_le(); let mut lhs_sign = self.bigint.sign(); let mut rhs_bytes = rhs.bigint.to_signed_bytes_le(); let mut rhs_sign = rhs.bigint.sign(); if lhs_sign != num_bigint::Sign::Minus { lhs_bytes.push(0); } if rhs_sign != num_bigint::Sign::Minus { rhs_bytes.push(0); } if rhs_bytes.len() > lhs_bytes.len() { std::mem::swap(&mut lhs_bytes, &mut rhs_bytes); std::mem::swap(&mut lhs_sign, &mut rhs_sign); } for i in 0..lhs_bytes.len() { let rhs_byte = if i < rhs_bytes.len() { rhs_bytes[i] } else if rhs_sign == num_bigint::Sign::Minus { 0xff } else { 0 }; lhs_bytes[i] = f(lhs_bytes[i], rhs_byte); } num_bigint::BigInt::from_signed_bytes_le(&lhs_bytes).into() } } impl<T: Into<num_bigint::BigInt>> From<T> for BigInt { fn from(value: T) -> BigInt { BigInt { bigint: value.into(), size: None, } } } impl std::ops::Not for &BigInt { type Output = BigInt; fn not(self) -> Self::Output { let mut x_bytes = self.bigint.to_signed_bytes_le(); if self.bigint.sign() != num_bigint::Sign::Minus { x_bytes.push(0); } for i in 0..x_bytes.len() { x_bytes[i] = !x_bytes[i]; } num_bigint::BigInt::from_signed_bytes_le(&x_bytes).into() } } impl std::ops::Neg for &BigInt { type Output = BigInt; fn neg(self) -> Self::Output { (-&self.bigint).into() } } impl std::ops::Add for &BigInt { type Output = BigInt; fn add(self, rhs: &BigInt) -> Self::Output { (&self.bigint + &rhs.bigint).into() } } impl std::ops::Sub for &BigInt { type Output = BigInt; fn sub(self, rhs: &BigInt) -> Self::Output { (&self.bigint - &rhs.bigint).into() } } impl std::ops::Mul for &BigInt { type Output = BigInt; fn mul(self, rhs: &BigInt) -> Self::Output { (&self.bigint * &rhs.bigint).into() } } impl std::ops::BitAnd for &BigInt { type Output = BigInt; fn bitand(self, rhs: &BigInt) -> Self::Output { self.combine_bits(rhs, |a, b| a & b).into() } } impl std::ops::BitOr for &BigInt { type Output = BigInt; fn bitor(self, rhs: &BigInt) -> Self::Output { self.combine_bits(rhs, |a, b| a | b).into() } } impl std::ops::BitXor for &BigInt { type Output = BigInt; fn bitxor(self, rhs: &BigInt) -> Self::Output { self.combine_bits(rhs, |a, b| a ^ b).into() } } impl std::cmp::PartialOrd for BigInt { fn partial_cmp(&self, rhs: &BigInt) -> Option<std::cmp::Ordering> { self.bigint.partial_cmp(&rhs.bigint) } } impl std::cmp::Ord for BigInt { fn cmp(&self, rhs: &BigInt) -> std::cmp::Ordering { self.bigint.cmp(&rhs.bigint) } } impl std::fmt::LowerHex for BigInt { fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { self.bigint.fmt(f) } }