From 7dc216ca8705aa123f83c1d4ca32affa6e5e96ee Mon Sep 17 00:00:00 2001 From: Christian Borst Date: Mon, 29 Apr 2024 14:31:25 -0400 Subject: [PATCH] Implement Pow for U/Int256 --- src/int256.rs | 10 +++++++++- src/uint256.rs | 12 ++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/int256.rs b/src/int256.rs index b8149d3..030e0ae 100644 --- a/src/int256.rs +++ b/src/int256.rs @@ -1,7 +1,7 @@ use bnum::types::I256; use bnum::BInt; use num_traits::{ - Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, One, Signed, + Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, One, Pow, Signed, ToPrimitive, Zero, }; use serde::ser::Serialize; @@ -293,6 +293,14 @@ impl Signed for Int256 { } } +impl Pow for Int256 { + type Output = Self; + + fn pow(self, p: u32) -> Self::Output { + Int256(self.0.pow(p)) + } +} + /// A macro that forwards an unary operator trait i.e. Add macro_rules! forward_op { (impl $trait_: ident for $type_: ident { fn $method: ident }) => { diff --git a/src/uint256.rs b/src/uint256.rs index feb3de9..495550b 100644 --- a/src/uint256.rs +++ b/src/uint256.rs @@ -2,8 +2,8 @@ pub use super::Int256; use bnum::types::U256; use bnum::BUint; use num_traits::{ - Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, One, ToPrimitive, - Zero, + Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, One, Pow, + ToPrimitive, Zero, }; use serde::ser::Serialize; use serde::{Deserialize, Deserializer, Serializer}; @@ -309,6 +309,14 @@ impl Into<[u8; 32]> for Uint256 { } } +impl Pow for Uint256 { + type Output = Self; + + fn pow(self, p: u32) -> Self::Output { + Uint256(self.0.pow(p)) + } +} + macro_rules! uint_impl_from_uint { ($T:ty) => { impl From<$T> for Uint256 {