From c7f05bd43051cf4ebb75e64dc80d831a29e23f98 Mon Sep 17 00:00:00 2001 From: bhargav Date: Thu, 30 Nov 2023 16:51:28 -0800 Subject: [PATCH] feat: `to_u32` --- src/numbers.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/numbers.rs b/src/numbers.rs index 2a7ad4d..712f1f5 100644 --- a/src/numbers.rs +++ b/src/numbers.rs @@ -120,6 +120,14 @@ impl BigInt { let is_odd: bool = self.v.retrieve().bit(0).into(); !is_odd } + + pub fn to_u32(&self) -> Result { + let ret = self.v.retrieve().as_words()[0] as u32; + if BigInt::from(ret) != *self { + return Err(format!("Overflow error -- {} exceeds u32 size limits", self).to_string()); + } + Ok(ret) + } } impl From for BigInt {